blob: 8cf5f8ac00540aa59d0e5de600df94bac364eb2c [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));
60static void buf_delete_signs __ARGS((buf_T *buf));
61#endif
62
Bram Moolenaar7fd73202010-07-25 16:58:46 +020063#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
64static char *msg_loclist = N_("[Location List]");
65static char *msg_qflist = N_("[Quickfix List]");
66#endif
67
Bram Moolenaar071d4272004-06-13 20:20:40 +000068/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +020069 * Open current buffer, that is: open the memfile and read the file into
70 * memory.
71 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000072 */
73 int
Bram Moolenaar59f931e2010-07-24 20:27:03 +020074open_buffer(read_stdin, eap, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 int read_stdin; /* read file from stdin */
76 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
Bram Moolenaar59f931e2010-07-24 20:27:03 +020077 int flags; /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078{
79 int retval = OK;
80#ifdef FEAT_AUTOCMD
81 buf_T *old_curbuf;
82#endif
83
84 /*
85 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
86 * When re-entering the same buffer, it should not change, because the
87 * user may have reset the flag by hand.
88 */
89 if (readonlymode && curbuf->b_ffname != NULL
90 && (curbuf->b_flags & BF_NEVERLOADED))
91 curbuf->b_p_ro = TRUE;
92
Bram Moolenaar4770d092006-01-12 23:22:24 +000093 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 {
95 /*
96 * There MUST be a memfile, otherwise we can't do anything
97 * If we can't create one for the current buffer, take another buffer
98 */
99 close_buffer(NULL, curbuf, 0);
100 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
101 if (curbuf->b_ml.ml_mfp != NULL)
102 break;
103 /*
104 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000105 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 */
107 if (curbuf == NULL)
108 {
109 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
110 getout(2);
111 }
112 EMSG(_("E83: Cannot allocate buffer, using other one..."));
113 enter_buffer(curbuf);
114 return FAIL;
115 }
116
117#ifdef FEAT_AUTOCMD
118 /* The autocommands in readfile() may change the buffer, but only AFTER
119 * reading the file. */
120 old_curbuf = curbuf;
121 modified_was_set = FALSE;
122#endif
123
124 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100125 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
127 if (curbuf->b_ffname != NULL
128#ifdef FEAT_NETBEANS_INTG
129 && netbeansReadFile
130#endif
131 )
132 {
133#ifdef FEAT_NETBEANS_INTG
134 int oldFire = netbeansFireChanges;
135
136 netbeansFireChanges = 0;
137#endif
138 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200139 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
140 flags | READ_NEW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_NETBEANS_INTG
142 netbeansFireChanges = oldFire;
143#endif
144 /* Help buffer is filtered. */
145 if (curbuf->b_help)
146 fix_help_buffer();
147 }
148 else if (read_stdin)
149 {
150 int save_bin = curbuf->b_p_bin;
151 linenr_T line_count;
152
153 /*
154 * First read the text in binary mode into the buffer.
155 * Then read from that same buffer and append at the end. This makes
156 * it possible to retry when 'fileformat' or 'fileencoding' was
157 * guessed wrong.
158 */
159 curbuf->b_p_bin = TRUE;
160 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200161 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
162 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 curbuf->b_p_bin = save_bin;
164 if (retval == OK)
165 {
166 line_count = curbuf->b_ml.ml_line_count;
167 retval = readfile(NULL, NULL, (linenr_T)line_count,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200168 (linenr_T)0, (linenr_T)MAXLNUM, eap,
169 flags | READ_BUFFER);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 if (retval == OK)
171 {
172 /* Delete the binary lines. */
173 while (--line_count >= 0)
174 ml_delete((linenr_T)1, FALSE);
175 }
176 else
177 {
178 /* Delete the converted lines. */
179 while (curbuf->b_ml.ml_line_count > line_count)
180 ml_delete(line_count, FALSE);
181 }
182 /* Put the cursor on the first line. */
183 curwin->w_cursor.lnum = 1;
184 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000185
186 /* Set or reset 'modified' before executing autocommands, so that
187 * it can be changed there. */
188 if (!readonlymode && !bufempty())
189 changed();
190 else if (retval != FAIL)
191 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192#ifdef FEAT_AUTOCMD
193# ifdef FEAT_EVAL
194 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
195 curbuf, &retval);
196# else
197 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
198# endif
199#endif
200 }
201 }
202
203 /* if first time loading this buffer, init b_chartab[] */
204 if (curbuf->b_flags & BF_NEVERLOADED)
205 (void)buf_init_chartab(curbuf, FALSE);
206
207 /*
208 * Set/reset the Changed flag first, autocmds may change the buffer.
209 * Apply the automatic commands, before processing the modelines.
210 * So the modelines have priority over auto commands.
211 */
212 /* When reading stdin, the buffer contents always needs writing, so set
213 * the changed flag. Unless in readonly mode: "ls | gview -".
214 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000215 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#ifdef FEAT_AUTOCMD
217 || modified_was_set /* ":set modified" used in autocmd */
218# ifdef FEAT_EVAL
219 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
220# endif
221#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000222 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000224 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 unchanged(curbuf, FALSE);
226 save_file_ff(curbuf); /* keep this fileformat */
227
228 /* require "!" to overwrite the file, because it wasn't read completely */
229#ifdef FEAT_EVAL
230 if (aborting())
231#else
232 if (got_int)
233#endif
234 curbuf->b_flags |= BF_READERR;
235
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000236#ifdef FEAT_FOLDING
237 /* Need to update automatic folding. Do this before the autocommands,
238 * they may use the fold info. */
239 foldUpdateAll(curwin);
240#endif
241
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242#ifdef FEAT_AUTOCMD
243 /* need to set w_topline, unless some autocommand already did that. */
244 if (!(curwin->w_valid & VALID_TOPLINE))
245 {
246 curwin->w_topline = 1;
247# ifdef FEAT_DIFF
248 curwin->w_topfill = 0;
249# endif
250 }
251# ifdef FEAT_EVAL
252 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
253# else
254 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
255# endif
256#endif
257
258 if (retval != FAIL)
259 {
260#ifdef FEAT_AUTOCMD
261 /*
262 * The autocommands may have changed the current buffer. Apply the
263 * modelines to the correct buffer, if it still exists and is loaded.
264 */
265 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
266 {
267 aco_save_T aco;
268
269 /* Go to the buffer that was opened. */
270 aucmd_prepbuf(&aco, old_curbuf);
271#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000272 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
274
275#ifdef FEAT_AUTOCMD
276# ifdef FEAT_EVAL
277 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
278 &retval);
279# else
280 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
281# endif
282
283 /* restore curwin/curbuf and a few other things */
284 aucmd_restbuf(&aco);
285 }
286#endif
287 }
288
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 return retval;
290}
291
292/*
293 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
294 */
295 int
296buf_valid(buf)
297 buf_T *buf;
298{
299 buf_T *bp;
300
301 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
302 if (bp == buf)
303 return TRUE;
304 return FALSE;
305}
306
307/*
308 * Close the link to a buffer.
309 * "action" is used when there is no longer a window for the buffer.
310 * It can be:
311 * 0 buffer becomes hidden
312 * DOBUF_UNLOAD buffer is unloaded
313 * DOBUF_DELETE buffer is unloaded and removed from buffer list
314 * DOBUF_WIPE buffer is unloaded and really deleted
315 * When doing all but the first one on the current buffer, the caller should
316 * get a new buffer very soon!
317 *
318 * The 'bufhidden' option can force freeing and deleting.
319 */
320 void
321close_buffer(win, buf, action)
322 win_T *win; /* if not NULL, set b_last_cursor */
323 buf_T *buf;
324 int action;
325{
326#ifdef FEAT_AUTOCMD
327 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100328 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#endif
330 int unload_buf = (action != 0);
331 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
332 int wipe_buf = (action == DOBUF_WIPE);
333
334#ifdef FEAT_QUICKFIX
335 /*
336 * Force unloading or deleting when 'bufhidden' says so.
337 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
338 * "hide" (otherwise we could never free or delete a buffer).
339 */
340 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
341 {
342 del_buf = TRUE;
343 unload_buf = TRUE;
344 }
345 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
346 {
347 del_buf = TRUE;
348 unload_buf = TRUE;
349 wipe_buf = TRUE;
350 }
351 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
352 unload_buf = TRUE;
353#endif
354
355 if (win != NULL)
356 {
357 /* Set b_last_cursor when closing the last window for the buffer.
358 * Remember the last cursor position and window options of the buffer.
359 * This used to be only for the current window, but then options like
360 * 'foldmethod' may be lost with a ":only" command. */
361 if (buf->b_nwindows == 1)
362 set_last_cursor(win);
363 buflist_setfpos(buf, win,
364 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
365 win->w_cursor.col, TRUE);
366 }
367
368#ifdef FEAT_AUTOCMD
369 /* When the buffer is no longer in a window, trigger BufWinLeave */
370 if (buf->b_nwindows == 1)
371 {
372 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
373 FALSE, buf);
374 if (!buf_valid(buf)) /* autocommands may delete the buffer */
375 return;
376
377 /* When the buffer becomes hidden, but is not unloaded, trigger
378 * BufHidden */
379 if (!unload_buf)
380 {
381 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
382 FALSE, buf);
383 if (!buf_valid(buf)) /* autocmds may delete the buffer */
384 return;
385 }
386# ifdef FEAT_EVAL
387 if (aborting()) /* autocmds may abort script processing */
388 return;
389# endif
390 }
391 nwindows = buf->b_nwindows;
392#endif
393
394 /* decrease the link count from windows (unless not in any window) */
395 if (buf->b_nwindows > 0)
396 --buf->b_nwindows;
397
398 /* Return when a window is displaying the buffer or when it's not
399 * unloaded. */
400 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402
403 /* Always remove the buffer when there is no file name. */
404 if (buf->b_ffname == NULL)
405 del_buf = TRUE;
406
407 /*
408 * Free all things allocated for this buffer.
409 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
410 */
411#ifdef FEAT_AUTOCMD
412 /* Remember if we are closing the current buffer. Restore the number of
413 * windows, so that autocommands in buf_freeall() don't get confused. */
414 is_curbuf = (buf == curbuf);
415 buf->b_nwindows = nwindows;
416#endif
417
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200418 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaarddab3322011-09-14 17:50:14 +0200419 if (
420#ifdef FEAT_WINDOWS
421 win_valid(win) &&
Bram Moolenaar83bac4c2011-12-30 13:39:10 +0100422#else
423 win != NULL &&
Bram Moolenaarddab3322011-09-14 17:50:14 +0200424#endif
425 win->w_buffer == buf)
Bram Moolenaara971b822011-09-14 14:43:25 +0200426 win->w_buffer = NULL; /* make sure we don't use the buffer now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427
428#ifdef FEAT_AUTOCMD
429 /* Autocommands may have deleted the buffer. */
430 if (!buf_valid(buf))
431 return;
432# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000433 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 return;
435# endif
436
437 /* Autocommands may have opened or closed windows for this buffer.
438 * Decrement the count for the close we do here. */
439 if (buf->b_nwindows > 0)
440 --buf->b_nwindows;
441
442 /*
443 * It's possible that autocommands change curbuf to the one being deleted.
444 * This might cause the previous curbuf to be deleted unexpectedly. But
445 * in some cases it's OK to delete the curbuf, because a new one is
446 * obtained anyway. Therefore only return if curbuf changed to the
447 * deleted buffer.
448 */
449 if (buf == curbuf && !is_curbuf)
450 return;
451#endif
452
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000453 /* Change directories when the 'acd' option is set. */
454 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455
456 /*
457 * Remove the buffer from the list.
458 */
459 if (wipe_buf)
460 {
461#ifdef FEAT_SUN_WORKSHOP
462 if (usingSunWorkShop)
463 workshop_file_closed_lineno((char *)buf->b_ffname,
464 (int)buf->b_last_cursor.lnum);
465#endif
466 vim_free(buf->b_ffname);
467 vim_free(buf->b_sfname);
468 if (buf->b_prev == NULL)
469 firstbuf = buf->b_next;
470 else
471 buf->b_prev->b_next = buf->b_next;
472 if (buf->b_next == NULL)
473 lastbuf = buf->b_prev;
474 else
475 buf->b_next->b_prev = buf->b_prev;
476 free_buffer(buf);
477 }
478 else
479 {
480 if (del_buf)
481 {
482 /* Free all internal variables and reset option values, to make
483 * ":bdel" compatible with Vim 5.7. */
484 free_buffer_stuff(buf, TRUE);
485
486 /* Make it look like a new buffer. */
487 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
488
489 /* Init the options when loaded again. */
490 buf->b_p_initialized = FALSE;
491 }
492 buf_clear_file(buf);
493 if (del_buf)
494 buf->b_p_bl = FALSE;
495 }
496}
497
498/*
499 * Make buffer not contain a file.
500 */
501 void
502buf_clear_file(buf)
503 buf_T *buf;
504{
505 buf->b_ml.ml_line_count = 1;
506 unchanged(buf, TRUE);
507#ifndef SHORT_FNAME
508 buf->b_shortname = FALSE;
509#endif
510 buf->b_p_eol = TRUE;
511 buf->b_start_eol = TRUE;
512#ifdef FEAT_MBYTE
513 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000514 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515#endif
516 buf->b_ml.ml_mfp = NULL;
517 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
518#ifdef FEAT_NETBEANS_INTG
519 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520#endif
521}
522
523/*
524 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200525 * the file. flags:
526 * BFA_DEL buffer is going to be deleted
527 * BFA_WIPE buffer is going to be wiped out
528 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 void
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200531buf_freeall(buf, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 buf_T *buf;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200533 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
535#ifdef FEAT_AUTOCMD
536 int is_curbuf = (buf == curbuf);
537
538 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
539 if (!buf_valid(buf)) /* autocommands may delete the buffer */
540 return;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200541 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 {
543 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
544 if (!buf_valid(buf)) /* autocommands may delete the buffer */
545 return;
546 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200547 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 {
549 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
550 FALSE, buf);
551 if (!buf_valid(buf)) /* autocommands may delete the buffer */
552 return;
553 }
554# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000555 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 return;
557# endif
558
559 /*
560 * It's possible that autocommands change curbuf to the one being deleted.
561 * This might cause curbuf to be deleted unexpectedly. But in some cases
562 * it's OK to delete the curbuf, because a new one is obtained anyway.
563 * Therefore only return if curbuf changed to the deleted buffer.
564 */
565 if (buf == curbuf && !is_curbuf)
566 return;
567#endif
568#ifdef FEAT_DIFF
569 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
570#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200571#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100572 /* Remove any ownsyntax, unless exiting. */
573 if (firstwin != NULL && curwin->w_buffer == buf)
574 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200575#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000576
577#ifdef FEAT_FOLDING
578 /* No folds in an empty buffer. */
579# ifdef FEAT_WINDOWS
580 {
581 win_T *win;
582 tabpage_T *tp;
583
584 FOR_ALL_TAB_WINDOWS(tp, win)
585 if (win->w_buffer == buf)
586 clearFolding(win);
587 }
588# else
589 if (curwin->w_buffer == buf)
590 clearFolding(curwin);
591# endif
592#endif
593
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594#ifdef FEAT_TCL
595 tcl_buffer_free(buf);
596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 ml_close(buf, TRUE); /* close and delete the memline/memfile */
598 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200599 if ((flags & BFA_KEEP_UNDO) == 0)
600 {
601 u_blockfree(buf); /* free the memory allocated for undo */
602 u_clearall(buf); /* reset all undo information */
603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200605 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000607 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608}
609
610/*
611 * Free a buffer structure and the things it contains related to the buffer
612 * itself (not the file, that must have been done already).
613 */
614 static void
615free_buffer(buf)
616 buf_T *buf;
617{
618 free_buffer_stuff(buf, TRUE);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200619#ifdef FEAT_LUA
620 lua_buffer_free(buf);
621#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000622#ifdef FEAT_MZSCHEME
623 mzscheme_buffer_free(buf);
624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625#ifdef FEAT_PERL
626 perl_buf_free(buf);
627#endif
628#ifdef FEAT_PYTHON
629 python_buffer_free(buf);
630#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200631#ifdef FEAT_PYTHON3
632 python3_buffer_free(buf);
633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634#ifdef FEAT_RUBY
635 ruby_buffer_free(buf);
636#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000637#ifdef FEAT_AUTOCMD
638 aubuflocal_remove(buf);
639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 vim_free(buf);
641}
642
643/*
644 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
645 */
646 static void
647free_buffer_stuff(buf, free_options)
648 buf_T *buf;
649 int free_options; /* free options as well */
650{
651 if (free_options)
652 {
653 clear_wininfo(buf); /* including window-local options */
654 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200655#ifdef FEAT_SPELL
656 ga_clear(&buf->b_s.b_langp);
657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 }
659#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000660 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
661 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662#endif
663#ifdef FEAT_USR_CMDS
664 uc_clear(&buf->b_ucmds); /* clear local user commands */
665#endif
666#ifdef FEAT_SIGNS
667 buf_delete_signs(buf); /* delete any signs */
668#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000669#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200670 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672#ifdef FEAT_LOCALMAP
673 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
674 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
675#endif
676#ifdef FEAT_MBYTE
677 vim_free(buf->b_start_fenc);
678 buf->b_start_fenc = NULL;
679#endif
680}
681
682/*
683 * Free the b_wininfo list for buffer "buf".
684 */
685 static void
686clear_wininfo(buf)
687 buf_T *buf;
688{
689 wininfo_T *wip;
690
691 while (buf->b_wininfo != NULL)
692 {
693 wip = buf->b_wininfo;
694 buf->b_wininfo = wip->wi_next;
695 if (wip->wi_optset)
696 {
697 clear_winopt(&wip->wi_opt);
698#ifdef FEAT_FOLDING
699 deleteFoldRecurse(&wip->wi_folds);
700#endif
701 }
702 vim_free(wip);
703 }
704}
705
706#if defined(FEAT_LISTCMDS) || defined(PROTO)
707/*
708 * Go to another buffer. Handles the result of the ATTENTION dialog.
709 */
710 void
711goto_buffer(eap, start, dir, count)
712 exarg_T *eap;
713 int start;
714 int dir;
715 int count;
716{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000717# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 buf_T *old_curbuf = curbuf;
719
720 swap_exists_action = SEA_DIALOG;
721# endif
722 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
723 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000724# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
726 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000727# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
728 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000729
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000730 /* Reset the error/interrupt/exception state here so that
731 * aborting() returns FALSE when closing a window. */
732 enter_cleanup(&cs);
733# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000734
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000735 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 win_close(curwin, TRUE);
737 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000738 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000739
740# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
741 /* Restore the error/interrupt/exception state if not discarded by a
742 * new aborting error, interrupt, or uncaught exception. */
743 leave_cleanup(&cs);
744# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 }
746 else
747 handle_swap_exists(old_curbuf);
748# endif
749}
750#endif
751
Bram Moolenaare64ac772005-12-07 20:54:59 +0000752#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753/*
754 * Handle the situation of swap_exists_action being set.
755 * It is allowed for "old_curbuf" to be NULL or invalid.
756 */
757 void
758handle_swap_exists(old_curbuf)
759 buf_T *old_curbuf;
760{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000761# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
762 cleanup_T cs;
763# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 if (swap_exists_action == SEA_QUIT)
766 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000767# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
768 /* Reset the error/interrupt/exception state here so that
769 * aborting() returns FALSE when closing a buffer. */
770 enter_cleanup(&cs);
771# endif
772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 /* User selected Quit at ATTENTION prompt. Go back to previous
774 * buffer. If that buffer is gone or the same as the current one,
775 * open a new, empty buffer. */
776 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000777 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
779 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000780 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if (old_curbuf != NULL)
782 enter_buffer(old_curbuf);
783 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000784
785# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
786 /* Restore the error/interrupt/exception state if not discarded by a
787 * new aborting error, interrupt, or uncaught exception. */
788 leave_cleanup(&cs);
789# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791 else if (swap_exists_action == SEA_RECOVER)
792 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000793# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
794 /* Reset the error/interrupt/exception state here so that
795 * aborting() returns FALSE when closing a buffer. */
796 enter_cleanup(&cs);
797# endif
798
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 /* User selected Recover at ATTENTION prompt. */
800 msg_scroll = TRUE;
801 ml_recover();
802 MSG_PUTS("\n"); /* don't overwrite the last message */
803 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000804 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000805
806# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
807 /* Restore the error/interrupt/exception state if not discarded by a
808 * new aborting error, interrupt, or uncaught exception. */
809 leave_cleanup(&cs);
810# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812 swap_exists_action = SEA_NONE;
813}
814#endif
815
816#if defined(FEAT_LISTCMDS) || defined(PROTO)
817/*
818 * do_bufdel() - delete or unload buffer(s)
819 *
820 * addr_count == 0: ":bdel" - delete current buffer
821 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
822 * buffer "end_bnr", then any other arguments.
823 * addr_count == 2: ":N,N bdel" - delete buffers in range
824 *
825 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
826 * DOBUF_DEL (":bdel")
827 *
828 * Returns error message or NULL
829 */
830 char_u *
831do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
832 int command;
833 char_u *arg; /* pointer to extra arguments */
834 int addr_count;
835 int start_bnr; /* first buffer number in a range */
836 int end_bnr; /* buffer nr or last buffer nr in a range */
837 int forceit;
838{
839 int do_current = 0; /* delete current buffer? */
840 int deleted = 0; /* number of buffers deleted */
841 char_u *errormsg = NULL; /* return value */
842 int bnr; /* buffer number */
843 char_u *p;
844
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 if (addr_count == 0)
846 {
847 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
848 }
849 else
850 {
851 if (addr_count == 2)
852 {
853 if (*arg) /* both range and argument is not allowed */
854 return (char_u *)_(e_trailing);
855 bnr = start_bnr;
856 }
857 else /* addr_count == 1 */
858 bnr = end_bnr;
859
860 for ( ;!got_int; ui_breakcheck())
861 {
862 /*
863 * delete the current buffer last, otherwise when the
864 * current buffer is deleted, the next buffer becomes
865 * the current one and will be loaded, which may then
866 * also be deleted, etc.
867 */
868 if (bnr == curbuf->b_fnum)
869 do_current = bnr;
870 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
871 forceit) == OK)
872 ++deleted;
873
874 /*
875 * find next buffer number to delete/unload
876 */
877 if (addr_count == 2)
878 {
879 if (++bnr > end_bnr)
880 break;
881 }
882 else /* addr_count == 1 */
883 {
884 arg = skipwhite(arg);
885 if (*arg == NUL)
886 break;
887 if (!VIM_ISDIGIT(*arg))
888 {
889 p = skiptowhite_esc(arg);
890 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
891 if (bnr < 0) /* failed */
892 break;
893 arg = p;
894 }
895 else
896 bnr = getdigits(&arg);
897 }
898 }
899 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
900 FORWARD, do_current, forceit) == OK)
901 ++deleted;
902
903 if (deleted == 0)
904 {
905 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000906 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000908 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000910 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 errormsg = IObuff;
912 }
913 else if (deleted >= p_report)
914 {
915 if (command == DOBUF_UNLOAD)
916 {
917 if (deleted == 1)
918 MSG(_("1 buffer unloaded"));
919 else
920 smsg((char_u *)_("%d buffers unloaded"), deleted);
921 }
922 else if (command == DOBUF_DEL)
923 {
924 if (deleted == 1)
925 MSG(_("1 buffer deleted"));
926 else
927 smsg((char_u *)_("%d buffers deleted"), deleted);
928 }
929 else
930 {
931 if (deleted == 1)
932 MSG(_("1 buffer wiped out"));
933 else
934 smsg((char_u *)_("%d buffers wiped out"), deleted);
935 }
936 }
937 }
938
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
940 return errormsg;
941}
942
943/*
944 * Implementation of the commands for the buffer list.
945 *
946 * action == DOBUF_GOTO go to specified buffer
947 * action == DOBUF_SPLIT split window and go to specified buffer
948 * action == DOBUF_UNLOAD unload specified buffer(s)
949 * action == DOBUF_DEL delete specified buffer(s) from buffer list
950 * action == DOBUF_WIPE delete specified buffer(s) really
951 *
952 * start == DOBUF_CURRENT go to "count" buffer from current buffer
953 * start == DOBUF_FIRST go to "count" buffer from first buffer
954 * start == DOBUF_LAST go to "count" buffer from last buffer
955 * start == DOBUF_MOD go to "count" modified buffer from current buffer
956 *
957 * Return FAIL or OK.
958 */
959 int
960do_buffer(action, start, dir, count, forceit)
961 int action;
962 int start;
963 int dir; /* FORWARD or BACKWARD */
964 int count; /* buffer number or number of buffers */
965 int forceit; /* TRUE for :...! */
966{
967 buf_T *buf;
968 buf_T *bp;
969 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
970 || action == DOBUF_WIPE);
971
972 switch (start)
973 {
974 case DOBUF_FIRST: buf = firstbuf; break;
975 case DOBUF_LAST: buf = lastbuf; break;
976 default: buf = curbuf; break;
977 }
978 if (start == DOBUF_MOD) /* find next modified buffer */
979 {
980 while (count-- > 0)
981 {
982 do
983 {
984 buf = buf->b_next;
985 if (buf == NULL)
986 buf = firstbuf;
987 }
988 while (buf != curbuf && !bufIsChanged(buf));
989 }
990 if (!bufIsChanged(buf))
991 {
992 EMSG(_("E84: No modified buffer found"));
993 return FAIL;
994 }
995 }
996 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
997 {
998 while (buf != NULL && buf->b_fnum != count)
999 buf = buf->b_next;
1000 }
1001 else
1002 {
1003 bp = NULL;
1004 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1005 {
1006 /* remember the buffer where we start, we come back there when all
1007 * buffers are unlisted. */
1008 if (bp == NULL)
1009 bp = buf;
1010 if (dir == FORWARD)
1011 {
1012 buf = buf->b_next;
1013 if (buf == NULL)
1014 buf = firstbuf;
1015 }
1016 else
1017 {
1018 buf = buf->b_prev;
1019 if (buf == NULL)
1020 buf = lastbuf;
1021 }
1022 /* don't count unlisted buffers */
1023 if (unload || buf->b_p_bl)
1024 {
1025 --count;
1026 bp = NULL; /* use this buffer as new starting point */
1027 }
1028 if (bp == buf)
1029 {
1030 /* back where we started, didn't find anything. */
1031 EMSG(_("E85: There is no listed buffer"));
1032 return FAIL;
1033 }
1034 }
1035 }
1036
1037 if (buf == NULL) /* could not find it */
1038 {
1039 if (start == DOBUF_FIRST)
1040 {
1041 /* don't warn when deleting */
1042 if (!unload)
1043 EMSGN(_("E86: Buffer %ld does not exist"), count);
1044 }
1045 else if (dir == FORWARD)
1046 EMSG(_("E87: Cannot go beyond last buffer"));
1047 else
1048 EMSG(_("E88: Cannot go before first buffer"));
1049 return FAIL;
1050 }
1051
1052#ifdef FEAT_GUI
1053 need_mouse_correct = TRUE;
1054#endif
1055
1056#ifdef FEAT_LISTCMDS
1057 /*
1058 * delete buffer buf from memory and/or the list
1059 */
1060 if (unload)
1061 {
1062 int forward;
1063 int retval;
1064
1065 /* When unloading or deleting a buffer that's already unloaded and
1066 * unlisted: fail silently. */
1067 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1068 return FAIL;
1069
1070 if (!forceit && bufIsChanged(buf))
1071 {
1072#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1073 if ((p_confirm || cmdmod.confirm) && p_write)
1074 {
1075 dialog_changed(buf, FALSE);
1076# ifdef FEAT_AUTOCMD
1077 if (!buf_valid(buf))
1078 /* Autocommand deleted buffer, oops! It's not changed
1079 * now. */
1080 return FAIL;
1081# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001082 /* If it's still changed fail silently, the dialog already
1083 * mentioned why it fails. */
1084 if (bufIsChanged(buf))
1085 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001087 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088#endif
1089 {
1090 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1091 buf->b_fnum);
1092 return FAIL;
1093 }
1094 }
1095
1096 /*
1097 * If deleting the last (listed) buffer, make it empty.
1098 * The last (listed) buffer cannot be unloaded.
1099 */
1100 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1101 if (bp->b_p_bl && bp != buf)
1102 break;
1103 if (bp == NULL && buf == curbuf)
1104 {
1105 if (action == DOBUF_UNLOAD)
1106 {
1107 EMSG(_("E90: Cannot unload last buffer"));
1108 return FAIL;
1109 }
1110
1111 /* Close any other windows on this buffer, then make it empty. */
1112#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001113 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114#endif
1115 setpcmark();
1116 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001117 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
1119 /*
1120 * do_ecmd() may create a new buffer, then we have to delete
1121 * the old one. But do_ecmd() may have done that already, check
1122 * if the buffer still exists.
1123 */
1124 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1125 close_buffer(NULL, buf, action);
1126 return retval;
1127 }
1128
1129#ifdef FEAT_WINDOWS
1130 /*
1131 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001132 * (unless it's the only window). Repeat this so long as we end up in
1133 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001135 while (buf == curbuf
1136 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 win_close(curwin, FALSE);
1138#endif
1139
1140 /*
1141 * If the buffer to be deleted is not the current one, delete it here.
1142 */
1143 if (buf != curbuf)
1144 {
1145#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001146 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147#endif
1148 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1149 close_buffer(NULL, buf, action);
1150 return OK;
1151 }
1152
1153 /*
1154 * Deleting the current buffer: Need to find another buffer to go to.
1155 * There must be another, otherwise it would have been handled above.
1156 * First use au_new_curbuf, if it is valid.
1157 * Then prefer the buffer we most recently visited.
1158 * Else try to find one that is loaded, after the current buffer,
1159 * then before the current buffer.
1160 * Finally use any buffer.
1161 */
1162 buf = NULL; /* selected buffer */
1163 bp = NULL; /* used when no loaded buffer found */
1164#ifdef FEAT_AUTOCMD
1165 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1166 buf = au_new_curbuf;
1167# ifdef FEAT_JUMPLIST
1168 else
1169# endif
1170#endif
1171#ifdef FEAT_JUMPLIST
1172 if (curwin->w_jumplistlen > 0)
1173 {
1174 int jumpidx;
1175
1176 jumpidx = curwin->w_jumplistidx - 1;
1177 if (jumpidx < 0)
1178 jumpidx = curwin->w_jumplistlen - 1;
1179
1180 forward = jumpidx;
1181 while (jumpidx != curwin->w_jumplistidx)
1182 {
1183 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1184 if (buf != NULL)
1185 {
1186 if (buf == curbuf || !buf->b_p_bl)
1187 buf = NULL; /* skip current and unlisted bufs */
1188 else if (buf->b_ml.ml_mfp == NULL)
1189 {
1190 /* skip unloaded buf, but may keep it for later */
1191 if (bp == NULL)
1192 bp = buf;
1193 buf = NULL;
1194 }
1195 }
1196 if (buf != NULL) /* found a valid buffer: stop searching */
1197 break;
1198 /* advance to older entry in jump list */
1199 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1200 break;
1201 if (--jumpidx < 0)
1202 jumpidx = curwin->w_jumplistlen - 1;
1203 if (jumpidx == forward) /* List exhausted for sure */
1204 break;
1205 }
1206 }
1207#endif
1208
1209 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1210 {
1211 forward = TRUE;
1212 buf = curbuf->b_next;
1213 for (;;)
1214 {
1215 if (buf == NULL)
1216 {
1217 if (!forward) /* tried both directions */
1218 break;
1219 buf = curbuf->b_prev;
1220 forward = FALSE;
1221 continue;
1222 }
1223 /* in non-help buffer, try to skip help buffers, and vv */
1224 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1225 {
1226 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1227 break;
1228 if (bp == NULL) /* remember unloaded buf for later */
1229 bp = buf;
1230 }
1231 if (forward)
1232 buf = buf->b_next;
1233 else
1234 buf = buf->b_prev;
1235 }
1236 }
1237 if (buf == NULL) /* No loaded buffer, use unloaded one */
1238 buf = bp;
1239 if (buf == NULL) /* No loaded buffer, find listed one */
1240 {
1241 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1242 if (buf->b_p_bl && buf != curbuf)
1243 break;
1244 }
1245 if (buf == NULL) /* Still no buffer, just take one */
1246 {
1247 if (curbuf->b_next != NULL)
1248 buf = curbuf->b_next;
1249 else
1250 buf = curbuf->b_prev;
1251 }
1252 }
1253
1254 /*
1255 * make buf current buffer
1256 */
1257 if (action == DOBUF_SPLIT) /* split window first */
1258 {
1259# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001260 /* If 'switchbuf' contains "useopen": jump to first window containing
1261 * "buf" if one exists */
1262 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001263 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001264 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001265 * page containing "buf" if one exists */
1266 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 return OK;
1268 if (win_split(0, 0) == FAIL)
1269# endif
1270 return FAIL;
1271 }
1272#endif
1273
1274 /* go to current buffer - nothing to do */
1275 if (buf == curbuf)
1276 return OK;
1277
1278 /*
1279 * Check if the current buffer may be abandoned.
1280 */
1281 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1282 {
1283#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1284 if ((p_confirm || cmdmod.confirm) && p_write)
1285 {
1286 dialog_changed(curbuf, FALSE);
1287# ifdef FEAT_AUTOCMD
1288 if (!buf_valid(buf))
1289 /* Autocommand deleted buffer, oops! */
1290 return FAIL;
1291# endif
1292 }
1293 if (bufIsChanged(curbuf))
1294#endif
1295 {
1296 EMSG(_(e_nowrtmsg));
1297 return FAIL;
1298 }
1299 }
1300
1301 /* Go to the other buffer. */
1302 set_curbuf(buf, action);
1303
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001304#if defined(FEAT_LISTCMDS) \
1305 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001307 {
1308 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310#endif
1311
1312#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1313 if (aborting()) /* autocmds may abort script processing */
1314 return FAIL;
1315#endif
1316
1317 return OK;
1318}
1319
1320#endif /* FEAT_LISTCMDS */
1321
1322/*
1323 * Set current buffer to "buf". Executes autocommands and closes current
1324 * buffer. "action" tells how to close the current buffer:
1325 * DOBUF_GOTO free or hide it
1326 * DOBUF_SPLIT nothing
1327 * DOBUF_UNLOAD unload it
1328 * DOBUF_DEL delete it
1329 * DOBUF_WIPE wipe it out
1330 */
1331 void
1332set_curbuf(buf, action)
1333 buf_T *buf;
1334 int action;
1335{
1336 buf_T *prevbuf;
1337 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1338 || action == DOBUF_WIPE);
1339
1340 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001341 if (!cmdmod.keepalt)
1342 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001343 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344
1345#ifdef FEAT_VISUAL
1346 /* Don't restart Select mode after switching to another buffer. */
1347 VIsual_reselect = FALSE;
1348#endif
1349
1350 /* close_windows() or apply_autocmds() may change curbuf */
1351 prevbuf = curbuf;
1352
1353#ifdef FEAT_AUTOCMD
1354 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1355# ifdef FEAT_EVAL
1356 if (buf_valid(prevbuf) && !aborting())
1357# else
1358 if (buf_valid(prevbuf))
1359# endif
1360#endif
1361 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001362#ifdef FEAT_SYN_HL
1363 if (prevbuf == curwin->w_buffer)
1364 reset_synblock(curwin);
1365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366#ifdef FEAT_WINDOWS
1367 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001368 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369#endif
1370#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1371 if (buf_valid(prevbuf) && !aborting())
1372#else
1373 if (buf_valid(prevbuf))
1374#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001375 {
1376 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001377 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1379 unload ? action : (action == DOBUF_GOTO
1380 && !P_HID(prevbuf)
1381 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
1384#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001385 /* An autocommand may have deleted "buf", already entered it (e.g., when
1386 * it did ":bunload") or aborted the script processing! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387# ifdef FEAT_EVAL
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001388 if (buf_valid(buf) && buf != curbuf && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389# else
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001390 if (buf_valid(buf) && buf != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391# endif
1392#endif
1393 enter_buffer(buf);
1394}
1395
1396/*
1397 * Enter a new current buffer.
1398 * Old curbuf must have been abandoned already!
1399 */
1400 void
1401enter_buffer(buf)
1402 buf_T *buf;
1403{
1404 /* Copy buffer and window local option values. Not for a help buffer. */
1405 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1406 if (!buf->b_help)
1407 get_winopts(buf);
1408#ifdef FEAT_FOLDING
1409 else
1410 /* Remove all folds in the window. */
1411 clearFolding(curwin);
1412 foldUpdateAll(curwin); /* update folds (later). */
1413#endif
1414
1415 /* Get the buffer in the current window. */
1416 curwin->w_buffer = buf;
1417 curbuf = buf;
1418 ++curbuf->b_nwindows;
1419
1420#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001421 if (curwin->w_p_diff)
1422 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423#endif
1424
Bram Moolenaara971b822011-09-14 14:43:25 +02001425#ifdef FEAT_SYN_HL
1426 curwin->w_s = &(buf->b_s);
1427#endif
1428
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 /* Cursor on first line by default. */
1430 curwin->w_cursor.lnum = 1;
1431 curwin->w_cursor.col = 0;
1432#ifdef FEAT_VIRTUALEDIT
1433 curwin->w_cursor.coladd = 0;
1434#endif
1435 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001436#ifdef FEAT_AUTOCMD
1437 curwin->w_topline_was_set = FALSE;
1438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001440 /* mark cursor position as being invalid */
1441 curwin->w_valid = 0;
1442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 /* Make sure the buffer is loaded. */
1444 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001445 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001446#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001447 /* If there is no filetype, allow for detecting one. Esp. useful for
1448 * ":ball" used in a autocommand. If there already is a filetype we
1449 * might prefer to keep it. */
1450 if (*curbuf->b_p_ft == NUL)
1451 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001452#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001453
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001454 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 else
1457 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001458 if (!msg_silent)
1459 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1461#ifdef FEAT_AUTOCMD
1462 curwin->w_topline = 1;
1463# ifdef FEAT_DIFF
1464 curwin->w_topfill = 0;
1465# endif
1466 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1467 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1468#endif
1469 }
1470
1471 /* If autocommands did not change the cursor position, restore cursor lnum
1472 * and possibly cursor col. */
1473 if (curwin->w_cursor.lnum == 1 && inindent(0))
1474 buflist_getfpos();
1475
1476 check_arg_idx(curwin); /* check for valid arg_idx */
1477#ifdef FEAT_TITLE
1478 maketitle();
1479#endif
1480#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001481 /* when autocmds didn't change it */
1482 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483#endif
1484 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1485
Bram Moolenaar009b2592004-10-24 19:18:58 +00001486#ifdef FEAT_NETBEANS_INTG
1487 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001488 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001489#endif
1490
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001491 /* Change directories when the 'acd' option is set. */
1492 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
1494#ifdef FEAT_KEYMAP
1495 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001496 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001498#ifdef FEAT_SPELL
1499 /* May need to set the spell language. Can only do this after the buffer
1500 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001501 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1502 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001503#endif
1504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 redraw_later(NOT_VALID);
1506}
1507
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001508#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1509/*
1510 * Change to the directory of the current buffer.
1511 */
1512 void
1513do_autochdir()
1514{
1515 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1516 shorten_fnames(TRUE);
1517}
1518#endif
1519
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520/*
1521 * functions for dealing with the buffer list
1522 */
1523
1524/*
1525 * Add a file name to the buffer list. Return a pointer to the buffer.
1526 * If the same file name already exists return a pointer to that buffer.
1527 * If it does not exist, or if fname == NULL, a new entry is created.
1528 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1529 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1530 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 * This is the ONLY way to create a new buffer.
1532 */
1533static int top_file_num = 1; /* highest file number */
1534
1535 buf_T *
1536buflist_new(ffname, sfname, lnum, flags)
1537 char_u *ffname; /* full path of fname or relative */
1538 char_u *sfname; /* short fname or NULL */
1539 linenr_T lnum; /* preferred cursor line */
1540 int flags; /* BLN_ defines */
1541{
1542 buf_T *buf;
1543#ifdef UNIX
1544 struct stat st;
1545#endif
1546
1547 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1548
1549 /*
1550 * If file name already exists in the list, update the entry.
1551 */
1552#ifdef UNIX
1553 /* On Unix we can use inode numbers when the file exists. Works better
1554 * for hard links. */
1555 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1556 st.st_dev = (dev_T)-1;
1557#endif
1558 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1559#ifdef UNIX
1560 buflist_findname_stat(ffname, &st)
1561#else
1562 buflist_findname(ffname)
1563#endif
1564 ) != NULL)
1565 {
1566 vim_free(ffname);
1567 if (lnum != 0)
1568 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1569 /* copy the options now, if 'cpo' doesn't have 's' and not done
1570 * already */
1571 buf_copy_options(buf, 0);
1572 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1573 {
1574 buf->b_p_bl = TRUE;
1575#ifdef FEAT_AUTOCMD
1576 if (!(flags & BLN_DUMMY))
1577 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1578#endif
1579 }
1580 return buf;
1581 }
1582
1583 /*
1584 * If the current buffer has no name and no contents, use the current
1585 * buffer. Otherwise: Need to allocate a new buffer structure.
1586 *
1587 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001588 * (A spell file buffer is allocated in spell.c, but that's not a normal
1589 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 */
1591 buf = NULL;
1592 if ((flags & BLN_CURBUF)
1593 && curbuf != NULL
1594 && curbuf->b_ffname == NULL
1595 && curbuf->b_nwindows <= 1
1596 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1597 {
1598 buf = curbuf;
1599#ifdef FEAT_AUTOCMD
1600 /* It's like this buffer is deleted. Watch out for autocommands that
1601 * change curbuf! If that happens, allocate a new buffer anyway. */
1602 if (curbuf->b_p_bl)
1603 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1604 if (buf == curbuf)
1605 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1606# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001607 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 return NULL;
1609# endif
1610#endif
1611#ifdef FEAT_QUICKFIX
1612# ifdef FEAT_AUTOCMD
1613 if (buf == curbuf)
1614# endif
1615 {
1616 /* Make sure 'bufhidden' and 'buftype' are empty */
1617 clear_string_option(&buf->b_p_bh);
1618 clear_string_option(&buf->b_p_bt);
1619 }
1620#endif
1621 }
1622 if (buf != curbuf || curbuf == NULL)
1623 {
1624 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1625 if (buf == NULL)
1626 {
1627 vim_free(ffname);
1628 return NULL;
1629 }
1630 }
1631
1632 if (ffname != NULL)
1633 {
1634 buf->b_ffname = ffname;
1635 buf->b_sfname = vim_strsave(sfname);
1636 }
1637
1638 clear_wininfo(buf);
1639 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1640
1641 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1642 || buf->b_wininfo == NULL)
1643 {
1644 vim_free(buf->b_ffname);
1645 buf->b_ffname = NULL;
1646 vim_free(buf->b_sfname);
1647 buf->b_sfname = NULL;
1648 if (buf != curbuf)
1649 free_buffer(buf);
1650 return NULL;
1651 }
1652
1653 if (buf == curbuf)
1654 {
1655 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001656 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (buf != curbuf) /* autocommands deleted the buffer! */
1658 return NULL;
1659#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001660 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 return NULL;
1662#endif
1663 /* buf->b_nwindows = 0; why was this here? */
1664 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1665#ifdef FEAT_KEYMAP
1666 /* need to reload lmaps and set b:keymap_name */
1667 curbuf->b_kmap_state |= KEYMAP_INIT;
1668#endif
1669 }
1670 else
1671 {
1672 /*
1673 * put new buffer at the end of the buffer list
1674 */
1675 buf->b_next = NULL;
1676 if (firstbuf == NULL) /* buffer list is empty */
1677 {
1678 buf->b_prev = NULL;
1679 firstbuf = buf;
1680 }
1681 else /* append new buffer at end of list */
1682 {
1683 lastbuf->b_next = buf;
1684 buf->b_prev = lastbuf;
1685 }
1686 lastbuf = buf;
1687
1688 buf->b_fnum = top_file_num++;
1689 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1690 {
1691 EMSG(_("W14: Warning: List of file names overflow"));
1692 if (emsg_silent == 0)
1693 {
1694 out_flush();
1695 ui_delay(3000L, TRUE); /* make sure it is noticed */
1696 }
1697 top_file_num = 1;
1698 }
1699
1700 /*
1701 * Always copy the options from the current buffer.
1702 */
1703 buf_copy_options(buf, BCO_ALWAYS);
1704 }
1705
1706 buf->b_wininfo->wi_fpos.lnum = lnum;
1707 buf->b_wininfo->wi_win = curwin;
1708
1709#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001710 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1711#endif
1712#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001713 hash_init(&buf->b_s.b_keywtab);
1714 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715#endif
1716
1717 buf->b_fname = buf->b_sfname;
1718#ifdef UNIX
1719 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001720 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 else
1722 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001723 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 buf->b_dev = st.st_dev;
1725 buf->b_ino = st.st_ino;
1726 }
1727#endif
1728 buf->b_u_synced = TRUE;
1729 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001730 if (flags & BLN_DUMMY)
1731 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 buf_clear_file(buf);
1733 clrallmarks(buf); /* clear marks */
1734 fmarks_check_names(buf); /* check file marks for this file */
1735 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1736#ifdef FEAT_AUTOCMD
1737 if (!(flags & BLN_DUMMY))
1738 {
1739 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1740 if (flags & BLN_LISTED)
1741 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1742# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001743 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 return NULL;
1745# endif
1746 }
1747#endif
1748
1749 return buf;
1750}
1751
1752/*
1753 * Free the memory for the options of a buffer.
1754 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1755 * 'fileencoding'.
1756 */
1757 void
1758free_buf_options(buf, free_p_ff)
1759 buf_T *buf;
1760 int free_p_ff;
1761{
1762 if (free_p_ff)
1763 {
1764#ifdef FEAT_MBYTE
1765 clear_string_option(&buf->b_p_fenc);
1766#endif
1767 clear_string_option(&buf->b_p_ff);
1768#ifdef FEAT_QUICKFIX
1769 clear_string_option(&buf->b_p_bh);
1770 clear_string_option(&buf->b_p_bt);
1771#endif
1772 }
1773#ifdef FEAT_FIND_ID
1774 clear_string_option(&buf->b_p_def);
1775 clear_string_option(&buf->b_p_inc);
1776# ifdef FEAT_EVAL
1777 clear_string_option(&buf->b_p_inex);
1778# endif
1779#endif
1780#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1781 clear_string_option(&buf->b_p_inde);
1782 clear_string_option(&buf->b_p_indk);
1783#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001784#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1785 clear_string_option(&buf->b_p_bexpr);
1786#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001787#if defined(FEAT_CRYPT)
1788 clear_string_option(&buf->b_p_cm);
1789#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001790#if defined(FEAT_EVAL)
1791 clear_string_option(&buf->b_p_fex);
1792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#ifdef FEAT_CRYPT
1794 clear_string_option(&buf->b_p_key);
1795#endif
1796 clear_string_option(&buf->b_p_kp);
1797 clear_string_option(&buf->b_p_mps);
1798 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001799 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 clear_string_option(&buf->b_p_isk);
1801#ifdef FEAT_KEYMAP
1802 clear_string_option(&buf->b_p_keymap);
1803 ga_clear(&buf->b_kmap_ga);
1804#endif
1805#ifdef FEAT_COMMENTS
1806 clear_string_option(&buf->b_p_com);
1807#endif
1808#ifdef FEAT_FOLDING
1809 clear_string_option(&buf->b_p_cms);
1810#endif
1811 clear_string_option(&buf->b_p_nf);
1812#ifdef FEAT_SYN_HL
1813 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001814#endif
1815#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001816 clear_string_option(&buf->b_s.b_p_spc);
1817 clear_string_option(&buf->b_s.b_p_spf);
1818 vim_free(buf->b_s.b_cap_prog);
1819 buf->b_s.b_cap_prog = NULL;
1820 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821#endif
1822#ifdef FEAT_SEARCHPATH
1823 clear_string_option(&buf->b_p_sua);
1824#endif
1825#ifdef FEAT_AUTOCMD
1826 clear_string_option(&buf->b_p_ft);
1827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828#ifdef FEAT_CINDENT
1829 clear_string_option(&buf->b_p_cink);
1830 clear_string_option(&buf->b_p_cino);
1831#endif
1832#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1833 clear_string_option(&buf->b_p_cinw);
1834#endif
1835#ifdef FEAT_INS_EXPAND
1836 clear_string_option(&buf->b_p_cpt);
1837#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001838#ifdef FEAT_COMPL_FUNC
1839 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001840 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842#ifdef FEAT_QUICKFIX
1843 clear_string_option(&buf->b_p_gp);
1844 clear_string_option(&buf->b_p_mp);
1845 clear_string_option(&buf->b_p_efm);
1846#endif
1847 clear_string_option(&buf->b_p_ep);
1848 clear_string_option(&buf->b_p_path);
1849 clear_string_option(&buf->b_p_tags);
1850#ifdef FEAT_INS_EXPAND
1851 clear_string_option(&buf->b_p_dict);
1852 clear_string_option(&buf->b_p_tsr);
1853#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001854#ifdef FEAT_TEXTOBJ
1855 clear_string_option(&buf->b_p_qe);
1856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 buf->b_p_ar = -1;
1858}
1859
1860/*
1861 * get alternate file n
1862 * set linenr to lnum or altfpos.lnum if lnum == 0
1863 * also set cursor column to altfpos.col if 'startofline' is not set.
1864 * if (options & GETF_SETMARK) call setpcmark()
1865 * if (options & GETF_ALT) we are jumping to an alternate file.
1866 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1867 *
1868 * return FAIL for failure, OK for success
1869 */
1870 int
1871buflist_getfile(n, lnum, options, forceit)
1872 int n;
1873 linenr_T lnum;
1874 int options;
1875 int forceit;
1876{
1877 buf_T *buf;
1878#ifdef FEAT_WINDOWS
1879 win_T *wp = NULL;
1880#endif
1881 pos_T *fpos;
1882 colnr_T col;
1883
1884 buf = buflist_findnr(n);
1885 if (buf == NULL)
1886 {
1887 if ((options & GETF_ALT) && n == 0)
1888 EMSG(_(e_noalt));
1889 else
1890 EMSGN(_("E92: Buffer %ld not found"), n);
1891 return FAIL;
1892 }
1893
1894 /* if alternate file is the current buffer, nothing to do */
1895 if (buf == curbuf)
1896 return OK;
1897
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001898 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001899 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001900 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001902 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001903#ifdef FEAT_AUTOCMD
1904 if (curbuf_locked())
1905 return FAIL;
1906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907
1908 /* altfpos may be changed by getfile(), get it now */
1909 if (lnum == 0)
1910 {
1911 fpos = buflist_findfpos(buf);
1912 lnum = fpos->lnum;
1913 col = fpos->col;
1914 }
1915 else
1916 col = 0;
1917
1918#ifdef FEAT_WINDOWS
1919 if (options & GETF_SWITCH)
1920 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001921 /* If 'switchbuf' contains "useopen": jump to first window containing
1922 * "buf" if one exists */
1923 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001925 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1926 * page containing "buf" if one exists */
1927 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001928 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001929 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1930 * isn't empty: open new window */
1931 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001933 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1934 tabpage_new();
1935 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001937 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939 }
1940#endif
1941
1942 ++RedrawingDisabled;
1943 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1944 lnum, forceit) <= 0)
1945 {
1946 --RedrawingDisabled;
1947
1948 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1949 if (!p_sol && col != 0)
1950 {
1951 curwin->w_cursor.col = col;
1952 check_cursor_col();
1953#ifdef FEAT_VIRTUALEDIT
1954 curwin->w_cursor.coladd = 0;
1955#endif
1956 curwin->w_set_curswant = TRUE;
1957 }
1958 return OK;
1959 }
1960 --RedrawingDisabled;
1961 return FAIL;
1962}
1963
1964/*
1965 * go to the last know line number for the current buffer
1966 */
1967 void
1968buflist_getfpos()
1969{
1970 pos_T *fpos;
1971
1972 fpos = buflist_findfpos(curbuf);
1973
1974 curwin->w_cursor.lnum = fpos->lnum;
1975 check_cursor_lnum();
1976
1977 if (p_sol)
1978 curwin->w_cursor.col = 0;
1979 else
1980 {
1981 curwin->w_cursor.col = fpos->col;
1982 check_cursor_col();
1983#ifdef FEAT_VIRTUALEDIT
1984 curwin->w_cursor.coladd = 0;
1985#endif
1986 curwin->w_set_curswant = TRUE;
1987 }
1988}
1989
Bram Moolenaar81695252004-12-29 20:58:21 +00001990#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1991/*
1992 * Find file in buffer list by name (it has to be for the current window).
1993 * Returns NULL if not found.
1994 */
1995 buf_T *
1996buflist_findname_exp(fname)
1997 char_u *fname;
1998{
1999 char_u *ffname;
2000 buf_T *buf = NULL;
2001
2002 /* First make the name into a full path name */
2003 ffname = FullName_save(fname,
2004#ifdef UNIX
2005 TRUE /* force expansion, get rid of symbolic links */
2006#else
2007 FALSE
2008#endif
2009 );
2010 if (ffname != NULL)
2011 {
2012 buf = buflist_findname(ffname);
2013 vim_free(ffname);
2014 }
2015 return buf;
2016}
2017#endif
2018
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019/*
2020 * Find file in buffer list by name (it has to be for the current window).
2021 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002022 * Skips dummy buffers.
2023 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 */
2025 buf_T *
2026buflist_findname(ffname)
2027 char_u *ffname;
2028{
2029#ifdef UNIX
2030 struct stat st;
2031
2032 if (mch_stat((char *)ffname, &st) < 0)
2033 st.st_dev = (dev_T)-1;
2034 return buflist_findname_stat(ffname, &st);
2035}
2036
2037/*
2038 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2039 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002040 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 */
2042 static buf_T *
2043buflist_findname_stat(ffname, stp)
2044 char_u *ffname;
2045 struct stat *stp;
2046{
2047#endif
2048 buf_T *buf;
2049
2050 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002051 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052#ifdef UNIX
2053 , stp
2054#endif
2055 ))
2056 return buf;
2057 return NULL;
2058}
2059
2060#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2061/*
2062 * Find file in buffer list by a regexp pattern.
2063 * Return fnum of the found buffer.
2064 * Return < 0 for error.
2065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 int
2067buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2068 char_u *pattern;
2069 char_u *pattern_end; /* pointer to first char after pattern */
2070 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002071 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072{
2073 buf_T *buf;
2074 regprog_T *prog;
2075 int match = -1;
2076 int find_listed;
2077 char_u *pat;
2078 char_u *patend;
2079 int attempt;
2080 char_u *p;
2081 int toggledollar;
2082
2083 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2084 {
2085 if (*pattern == '%')
2086 match = curbuf->b_fnum;
2087 else
2088 match = curwin->w_alt_fnum;
2089#ifdef FEAT_DIFF
2090 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2091 match = -1;
2092#endif
2093 }
2094
2095 /*
2096 * Try four ways of matching a listed buffer:
2097 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002098 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 * attempt == 2: with '$' at end (only match at end)
2100 * attempt == 3: with '^' at start and '$' at end (only full match)
2101 * Repeat this for finding an unlisted buffer if there was no matching
2102 * listed buffer.
2103 */
2104 else
2105 {
2106 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2107 if (pat == NULL)
2108 return -1;
2109 patend = pat + STRLEN(pat) - 1;
2110 toggledollar = (patend > pat && *patend == '$');
2111
2112 /* First try finding a listed buffer. If not found and "unlisted"
2113 * is TRUE, try finding an unlisted buffer. */
2114 find_listed = TRUE;
2115 for (;;)
2116 {
2117 for (attempt = 0; attempt <= 3; ++attempt)
2118 {
2119 /* may add '^' and '$' */
2120 if (toggledollar)
2121 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2122 p = pat;
2123 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2124 ++p;
2125 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2126 if (prog == NULL)
2127 {
2128 vim_free(pat);
2129 return -1;
2130 }
2131
2132 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2133 if (buf->b_p_bl == find_listed
2134#ifdef FEAT_DIFF
2135 && (!diffmode || diff_mode_buf(buf))
2136#endif
2137 && buflist_match(prog, buf) != NULL)
2138 {
2139 if (match >= 0) /* already found a match */
2140 {
2141 match = -2;
2142 break;
2143 }
2144 match = buf->b_fnum; /* remember first match */
2145 }
2146
2147 vim_free(prog);
2148 if (match >= 0) /* found one match */
2149 break;
2150 }
2151
2152 /* Only search for unlisted buffers if there was no match with
2153 * a listed buffer. */
2154 if (!unlisted || !find_listed || match != -1)
2155 break;
2156 find_listed = FALSE;
2157 }
2158
2159 vim_free(pat);
2160 }
2161
2162 if (match == -2)
2163 EMSG2(_("E93: More than one match for %s"), pattern);
2164 else if (match < 0)
2165 EMSG2(_("E94: No matching buffer for %s"), pattern);
2166 return match;
2167}
2168#endif
2169
2170#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2171
2172/*
2173 * Find all buffer names that match.
2174 * For command line expansion of ":buf" and ":sbuf".
2175 * Return OK if matches found, FAIL otherwise.
2176 */
2177 int
2178ExpandBufnames(pat, num_file, file, options)
2179 char_u *pat;
2180 int *num_file;
2181 char_u ***file;
2182 int options;
2183{
2184 int count = 0;
2185 buf_T *buf;
2186 int round;
2187 char_u *p;
2188 int attempt;
2189 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002190 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
2192 *num_file = 0; /* return values in case of FAIL */
2193 *file = NULL;
2194
Bram Moolenaar05159a02005-02-26 23:04:13 +00002195 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2196 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002198 patc = alloc((unsigned)STRLEN(pat) + 11);
2199 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002201 STRCPY(patc, "\\(^\\|[\\/]\\)");
2202 STRCPY(patc + 11, pat + 1);
2203 }
2204 else
2205 patc = pat;
2206
2207 /*
2208 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002209 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002210 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002211 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002212 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002213 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002214 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002215 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002216 if (prog == NULL)
2217 {
2218 if (patc != pat)
2219 vim_free(patc);
2220 return FAIL;
2221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223 /*
2224 * round == 1: Count the matches.
2225 * round == 2: Build the array to keep the matches.
2226 */
2227 for (round = 1; round <= 2; ++round)
2228 {
2229 count = 0;
2230 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2231 {
2232 if (!buf->b_p_bl) /* skip unlisted buffers */
2233 continue;
2234 p = buflist_match(prog, buf);
2235 if (p != NULL)
2236 {
2237 if (round == 1)
2238 ++count;
2239 else
2240 {
2241 if (options & WILD_HOME_REPLACE)
2242 p = home_replace_save(buf, p);
2243 else
2244 p = vim_strsave(p);
2245 (*file)[count++] = p;
2246 }
2247 }
2248 }
2249 if (count == 0) /* no match found, break here */
2250 break;
2251 if (round == 1)
2252 {
2253 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2254 if (*file == NULL)
2255 {
2256 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002257 if (patc != pat)
2258 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 return FAIL;
2260 }
2261 }
2262 }
2263 vim_free(prog);
2264 if (count) /* match(es) found, break here */
2265 break;
2266 }
2267
Bram Moolenaar05159a02005-02-26 23:04:13 +00002268 if (patc != pat)
2269 vim_free(patc);
2270
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 *num_file = count;
2272 return (count == 0 ? FAIL : OK);
2273}
2274
2275#endif /* FEAT_CMDL_COMPL */
2276
2277#ifdef HAVE_BUFLIST_MATCH
2278/*
2279 * Check for a match on the file name for buffer "buf" with regprog "prog".
2280 */
2281 static char_u *
2282buflist_match(prog, buf)
2283 regprog_T *prog;
2284 buf_T *buf;
2285{
2286 char_u *match;
2287
2288 /* First try the short file name, then the long file name. */
2289 match = fname_match(prog, buf->b_sfname);
2290 if (match == NULL)
2291 match = fname_match(prog, buf->b_ffname);
2292
2293 return match;
2294}
2295
2296/*
2297 * Try matching the regexp in "prog" with file name "name".
2298 * Return "name" when there is a match, NULL when not.
2299 */
2300 static char_u *
2301fname_match(prog, name)
2302 regprog_T *prog;
2303 char_u *name;
2304{
2305 char_u *match = NULL;
2306 char_u *p;
2307 regmatch_T regmatch;
2308
2309 if (name != NULL)
2310 {
2311 regmatch.regprog = prog;
2312#ifdef CASE_INSENSITIVE_FILENAME
2313 regmatch.rm_ic = TRUE; /* Always ignore case */
2314#else
2315 regmatch.rm_ic = FALSE; /* Never ignore case */
2316#endif
2317
2318 if (vim_regexec(&regmatch, name, (colnr_T)0))
2319 match = name;
2320 else
2321 {
2322 /* Replace $(HOME) with '~' and try matching again. */
2323 p = home_replace_save(NULL, name);
2324 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2325 match = name;
2326 vim_free(p);
2327 }
2328 }
2329
2330 return match;
2331}
2332#endif
2333
2334/*
2335 * find file in buffer list by number
2336 */
2337 buf_T *
2338buflist_findnr(nr)
2339 int nr;
2340{
2341 buf_T *buf;
2342
2343 if (nr == 0)
2344 nr = curwin->w_alt_fnum;
2345 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2346 if (buf->b_fnum == nr)
2347 return (buf);
2348 return NULL;
2349}
2350
2351/*
2352 * Get name of file 'n' in the buffer list.
2353 * When the file has no name an empty string is returned.
2354 * home_replace() is used to shorten the file name (used for marks).
2355 * Returns a pointer to allocated memory, of NULL when failed.
2356 */
2357 char_u *
2358buflist_nr2name(n, fullname, helptail)
2359 int n;
2360 int fullname;
2361 int helptail; /* for help buffers return tail only */
2362{
2363 buf_T *buf;
2364
2365 buf = buflist_findnr(n);
2366 if (buf == NULL)
2367 return NULL;
2368 return home_replace_save(helptail ? buf : NULL,
2369 fullname ? buf->b_ffname : buf->b_fname);
2370}
2371
2372/*
2373 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2374 * When "copy_options" is TRUE save the local window option values.
2375 * When "lnum" is 0 only do the options.
2376 */
2377 static void
2378buflist_setfpos(buf, win, lnum, col, copy_options)
2379 buf_T *buf;
2380 win_T *win;
2381 linenr_T lnum;
2382 colnr_T col;
2383 int copy_options;
2384{
2385 wininfo_T *wip;
2386
2387 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2388 if (wip->wi_win == win)
2389 break;
2390 if (wip == NULL)
2391 {
2392 /* allocate a new entry */
2393 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2394 if (wip == NULL)
2395 return;
2396 wip->wi_win = win;
2397 if (lnum == 0) /* set lnum even when it's 0 */
2398 lnum = 1;
2399 }
2400 else
2401 {
2402 /* remove the entry from the list */
2403 if (wip->wi_prev)
2404 wip->wi_prev->wi_next = wip->wi_next;
2405 else
2406 buf->b_wininfo = wip->wi_next;
2407 if (wip->wi_next)
2408 wip->wi_next->wi_prev = wip->wi_prev;
2409 if (copy_options && wip->wi_optset)
2410 {
2411 clear_winopt(&wip->wi_opt);
2412#ifdef FEAT_FOLDING
2413 deleteFoldRecurse(&wip->wi_folds);
2414#endif
2415 }
2416 }
2417 if (lnum != 0)
2418 {
2419 wip->wi_fpos.lnum = lnum;
2420 wip->wi_fpos.col = col;
2421 }
2422 if (copy_options)
2423 {
2424 /* Save the window-specific option values. */
2425 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2426#ifdef FEAT_FOLDING
2427 wip->wi_fold_manual = win->w_fold_manual;
2428 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2429#endif
2430 wip->wi_optset = TRUE;
2431 }
2432
2433 /* insert the entry in front of the list */
2434 wip->wi_next = buf->b_wininfo;
2435 buf->b_wininfo = wip;
2436 wip->wi_prev = NULL;
2437 if (wip->wi_next)
2438 wip->wi_next->wi_prev = wip;
2439
2440 return;
2441}
2442
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002443#ifdef FEAT_DIFF
2444static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2445
2446/*
2447 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2448 * page. That's because a diff is local to a tab page.
2449 */
2450 static int
2451wininfo_other_tab_diff(wip)
2452 wininfo_T *wip;
2453{
2454 win_T *wp;
2455
2456 if (wip->wi_opt.wo_diff)
2457 {
2458 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2459 /* return FALSE when it's a window in the current tab page, thus
2460 * the buffer was in diff mode here */
2461 if (wip->wi_win == wp)
2462 return FALSE;
2463 return TRUE;
2464 }
2465 return FALSE;
2466}
2467#endif
2468
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469/*
2470 * Find info for the current window in buffer "buf".
2471 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002472 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2473 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 * Returns NULL when there isn't any info.
2475 */
2476 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002477find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002479 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480{
2481 wininfo_T *wip;
2482
2483 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002484 if (wip->wi_win == curwin
2485#ifdef FEAT_DIFF
2486 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2487#endif
2488 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002490
2491 /* If no wininfo for curwin, use the first in the list (that doesn't have
2492 * 'diff' set and is in another tab page). */
2493 if (wip == NULL)
2494 {
2495#ifdef FEAT_DIFF
2496 if (skip_diff_buffer)
2497 {
2498 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2499 if (!wininfo_other_tab_diff(wip))
2500 break;
2501 }
2502 else
2503#endif
2504 wip = buf->b_wininfo;
2505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 return wip;
2507}
2508
2509/*
2510 * Reset the local window options to the values last used in this window.
2511 * If the buffer wasn't used in this window before, use the values from
2512 * the most recently used window. If the values were never set, use the
2513 * global values for the window.
2514 */
2515 void
2516get_winopts(buf)
2517 buf_T *buf;
2518{
2519 wininfo_T *wip;
2520
2521 clear_winopt(&curwin->w_onebuf_opt);
2522#ifdef FEAT_FOLDING
2523 clearFolding(curwin);
2524#endif
2525
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002526 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 if (wip != NULL && wip->wi_optset)
2528 {
2529 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2530#ifdef FEAT_FOLDING
2531 curwin->w_fold_manual = wip->wi_fold_manual;
2532 curwin->w_foldinvalid = TRUE;
2533 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2534#endif
2535 }
2536 else
2537 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2538
2539#ifdef FEAT_FOLDING
2540 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2541 if (p_fdls >= 0)
2542 curwin->w_p_fdl = p_fdls;
2543#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002544#ifdef FEAT_SYN_HL
2545 check_colorcolumn(curwin);
2546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547}
2548
2549/*
2550 * Find the position (lnum and col) for the buffer 'buf' for the current
2551 * window.
2552 * Returns a pointer to no_position if no position is found.
2553 */
2554 pos_T *
2555buflist_findfpos(buf)
2556 buf_T *buf;
2557{
2558 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002559 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002561 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 if (wip != NULL)
2563 return &(wip->wi_fpos);
2564 else
2565 return &no_position;
2566}
2567
2568/*
2569 * Find the lnum for the buffer 'buf' for the current window.
2570 */
2571 linenr_T
2572buflist_findlnum(buf)
2573 buf_T *buf;
2574{
2575 return buflist_findfpos(buf)->lnum;
2576}
2577
2578#if defined(FEAT_LISTCMDS) || defined(PROTO)
2579/*
2580 * List all know file names (for :files and :buffers command).
2581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 void
2583buflist_list(eap)
2584 exarg_T *eap;
2585{
2586 buf_T *buf;
2587 int len;
2588 int i;
2589
2590 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2591 {
2592 /* skip unlisted buffers, unless ! was used */
2593 if (!buf->b_p_bl && !eap->forceit)
2594 continue;
2595 msg_putchar('\n');
2596 if (buf_spname(buf) != NULL)
2597 STRCPY(NameBuff, buf_spname(buf));
2598 else
2599 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2600
Bram Moolenaar50cde822005-06-05 21:54:54 +00002601 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 buf->b_fnum,
2603 buf->b_p_bl ? ' ' : 'u',
2604 buf == curbuf ? '%' :
2605 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2606 buf->b_ml.ml_mfp == NULL ? ' ' :
2607 (buf->b_nwindows == 0 ? 'h' : 'a'),
2608 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2609 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002610 : (bufIsChanged(buf) ? '+' : ' '),
2611 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
2613 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 i = 40 - vim_strsize(IObuff);
2615 do
2616 {
2617 IObuff[len++] = ' ';
2618 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002619 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2620 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002621 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 msg_outtrans(IObuff);
2623 out_flush(); /* output one line at a time */
2624 ui_breakcheck();
2625 }
2626}
2627#endif
2628
2629/*
2630 * Get file name and line number for file 'fnum'.
2631 * Used by DoOneCmd() for translating '%' and '#'.
2632 * Used by insert_reg() and cmdline_paste() for '#' register.
2633 * Return FAIL if not found, OK for success.
2634 */
2635 int
2636buflist_name_nr(fnum, fname, lnum)
2637 int fnum;
2638 char_u **fname;
2639 linenr_T *lnum;
2640{
2641 buf_T *buf;
2642
2643 buf = buflist_findnr(fnum);
2644 if (buf == NULL || buf->b_fname == NULL)
2645 return FAIL;
2646
2647 *fname = buf->b_fname;
2648 *lnum = buflist_findlnum(buf);
2649
2650 return OK;
2651}
2652
2653/*
2654 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2655 * The file name with the full path is also remembered, for when :cd is used.
2656 * Returns FAIL for failure (file name already in use by other buffer)
2657 * OK otherwise.
2658 */
2659 int
2660setfname(buf, ffname, sfname, message)
2661 buf_T *buf;
2662 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002663 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664{
Bram Moolenaar81695252004-12-29 20:58:21 +00002665 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666#ifdef UNIX
2667 struct stat st;
2668#endif
2669
2670 if (ffname == NULL || *ffname == NUL)
2671 {
2672 /* Removing the name. */
2673 vim_free(buf->b_ffname);
2674 vim_free(buf->b_sfname);
2675 buf->b_ffname = NULL;
2676 buf->b_sfname = NULL;
2677#ifdef UNIX
2678 st.st_dev = (dev_T)-1;
2679#endif
2680 }
2681 else
2682 {
2683 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2684 if (ffname == NULL) /* out of memory */
2685 return FAIL;
2686
2687 /*
2688 * if the file name is already used in another buffer:
2689 * - if the buffer is loaded, fail
2690 * - if the buffer is not loaded, delete it from the list
2691 */
2692#ifdef UNIX
2693 if (mch_stat((char *)ffname, &st) < 0)
2694 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002695#endif
2696 if (!(buf->b_flags & BF_DUMMY))
2697#ifdef UNIX
2698 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002700 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701#endif
2702 if (obuf != NULL && obuf != buf)
2703 {
2704 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2705 {
2706 if (message)
2707 EMSG(_("E95: Buffer with this name already exists"));
2708 vim_free(ffname);
2709 return FAIL;
2710 }
2711 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2712 }
2713 sfname = vim_strsave(sfname);
2714 if (ffname == NULL || sfname == NULL)
2715 {
2716 vim_free(sfname);
2717 vim_free(ffname);
2718 return FAIL;
2719 }
2720#ifdef USE_FNAME_CASE
2721# ifdef USE_LONG_FNAME
2722 if (USE_LONG_FNAME)
2723# endif
2724 fname_case(sfname, 0); /* set correct case for short file name */
2725#endif
2726 vim_free(buf->b_ffname);
2727 vim_free(buf->b_sfname);
2728 buf->b_ffname = ffname;
2729 buf->b_sfname = sfname;
2730 }
2731 buf->b_fname = buf->b_sfname;
2732#ifdef UNIX
2733 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002734 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 else
2736 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002737 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 buf->b_dev = st.st_dev;
2739 buf->b_ino = st.st_ino;
2740 }
2741#endif
2742
2743#ifndef SHORT_FNAME
2744 buf->b_shortname = FALSE;
2745#endif
2746
2747 buf_name_changed(buf);
2748 return OK;
2749}
2750
2751/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002752 * Crude way of changing the name of a buffer. Use with care!
2753 * The name should be relative to the current directory.
2754 */
2755 void
2756buf_set_name(fnum, name)
2757 int fnum;
2758 char_u *name;
2759{
2760 buf_T *buf;
2761
2762 buf = buflist_findnr(fnum);
2763 if (buf != NULL)
2764 {
2765 vim_free(buf->b_sfname);
2766 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002767 buf->b_ffname = vim_strsave(name);
2768 buf->b_sfname = NULL;
2769 /* Allocate ffname and expand into full path. Also resolves .lnk
2770 * files on Win32. */
2771 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002772 buf->b_fname = buf->b_sfname;
2773 }
2774}
2775
2776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 * Take care of what needs to be done when the name of buffer "buf" has
2778 * changed.
2779 */
2780 void
2781buf_name_changed(buf)
2782 buf_T *buf;
2783{
2784 /*
2785 * If the file name changed, also change the name of the swapfile
2786 */
2787 if (buf->b_ml.ml_mfp != NULL)
2788 ml_setname(buf);
2789
2790 if (curwin->w_buffer == buf)
2791 check_arg_idx(curwin); /* check file name for arg list */
2792#ifdef FEAT_TITLE
2793 maketitle(); /* set window title */
2794#endif
2795#ifdef FEAT_WINDOWS
2796 status_redraw_all(); /* status lines need to be redrawn */
2797#endif
2798 fmarks_check_names(buf); /* check named file marks */
2799 ml_timestamp(buf); /* reset timestamp */
2800}
2801
2802/*
2803 * set alternate file name for current window
2804 *
2805 * Used by do_one_cmd(), do_write() and do_ecmd().
2806 * Return the buffer.
2807 */
2808 buf_T *
2809setaltfname(ffname, sfname, lnum)
2810 char_u *ffname;
2811 char_u *sfname;
2812 linenr_T lnum;
2813{
2814 buf_T *buf;
2815
2816 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2817 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002818 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 curwin->w_alt_fnum = buf->b_fnum;
2820 return buf;
2821}
2822
2823/*
2824 * Get alternate file name for current window.
2825 * Return NULL if there isn't any, and give error message if requested.
2826 */
2827 char_u *
2828getaltfname(errmsg)
2829 int errmsg; /* give error message */
2830{
2831 char_u *fname;
2832 linenr_T dummy;
2833
2834 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2835 {
2836 if (errmsg)
2837 EMSG(_(e_noalt));
2838 return NULL;
2839 }
2840 return fname;
2841}
2842
2843/*
2844 * Add a file name to the buflist and return its number.
2845 * Uses same flags as buflist_new(), except BLN_DUMMY.
2846 *
2847 * used by qf_init(), main() and doarglist()
2848 */
2849 int
2850buflist_add(fname, flags)
2851 char_u *fname;
2852 int flags;
2853{
2854 buf_T *buf;
2855
2856 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2857 if (buf != NULL)
2858 return buf->b_fnum;
2859 return 0;
2860}
2861
2862#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2863/*
2864 * Adjust slashes in file names. Called after 'shellslash' was set.
2865 */
2866 void
2867buflist_slash_adjust()
2868{
2869 buf_T *bp;
2870
2871 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2872 {
2873 if (bp->b_ffname != NULL)
2874 slash_adjust(bp->b_ffname);
2875 if (bp->b_sfname != NULL)
2876 slash_adjust(bp->b_sfname);
2877 }
2878}
2879#endif
2880
2881/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002882 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 * Also save the local window option values.
2884 */
2885 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002886buflist_altfpos(win)
2887 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002889 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890}
2891
2892/*
2893 * Return TRUE if 'ffname' is not the same file as current file.
2894 * Fname must have a full path (expanded by mch_FullName()).
2895 */
2896 int
2897otherfile(ffname)
2898 char_u *ffname;
2899{
2900 return otherfile_buf(curbuf, ffname
2901#ifdef UNIX
2902 , NULL
2903#endif
2904 );
2905}
2906
2907 static int
2908otherfile_buf(buf, ffname
2909#ifdef UNIX
2910 , stp
2911#endif
2912 )
2913 buf_T *buf;
2914 char_u *ffname;
2915#ifdef UNIX
2916 struct stat *stp;
2917#endif
2918{
2919 /* no name is different */
2920 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2921 return TRUE;
2922 if (fnamecmp(ffname, buf->b_ffname) == 0)
2923 return FALSE;
2924#ifdef UNIX
2925 {
2926 struct stat st;
2927
2928 /* If no struct stat given, get it now */
2929 if (stp == NULL)
2930 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002931 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 st.st_dev = (dev_T)-1;
2933 stp = &st;
2934 }
2935 /* Use dev/ino to check if the files are the same, even when the names
2936 * are different (possible with links). Still need to compare the
2937 * name above, for when the file doesn't exist yet.
2938 * Problem: The dev/ino changes when a file is deleted (and created
2939 * again) and remains the same when renamed/moved. We don't want to
2940 * mch_stat() each buffer each time, that would be too slow. Get the
2941 * dev/ino again when they appear to match, but not when they appear
2942 * to be different: Could skip a buffer when it's actually the same
2943 * file. */
2944 if (buf_same_ino(buf, stp))
2945 {
2946 buf_setino(buf);
2947 if (buf_same_ino(buf, stp))
2948 return FALSE;
2949 }
2950 }
2951#endif
2952 return TRUE;
2953}
2954
2955#if defined(UNIX) || defined(PROTO)
2956/*
2957 * Set inode and device number for a buffer.
2958 * Must always be called when b_fname is changed!.
2959 */
2960 void
2961buf_setino(buf)
2962 buf_T *buf;
2963{
2964 struct stat st;
2965
2966 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2967 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002968 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 buf->b_dev = st.st_dev;
2970 buf->b_ino = st.st_ino;
2971 }
2972 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002973 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974}
2975
2976/*
2977 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2978 */
2979 static int
2980buf_same_ino(buf, stp)
2981 buf_T *buf;
2982 struct stat *stp;
2983{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002984 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 && stp->st_dev == buf->b_dev
2986 && stp->st_ino == buf->b_ino);
2987}
2988#endif
2989
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002990/*
2991 * Print info about the current buffer.
2992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 void
2994fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002995 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 int shorthelp;
2997 int dont_truncate;
2998{
2999 char_u *name;
3000 int n;
3001 char_u *p;
3002 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003003 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
3005 buffer = alloc(IOSIZE);
3006 if (buffer == NULL)
3007 return;
3008
3009 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3010 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003011 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 p = buffer + STRLEN(buffer);
3013 }
3014 else
3015 p = buffer;
3016
3017 *p++ = '"';
3018 if (buf_spname(curbuf) != NULL)
3019 STRCPY(p, buf_spname(curbuf));
3020 else
3021 {
3022 if (!fullname && curbuf->b_fname != NULL)
3023 name = curbuf->b_fname;
3024 else
3025 name = curbuf->b_ffname;
3026 home_replace(shorthelp ? curbuf : NULL, name, p,
3027 (int)(IOSIZE - (p - buffer)), TRUE);
3028 }
3029
Bram Moolenaara800b422010-06-27 01:15:55 +02003030 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 curbufIsChanged() ? (shortmess(SHM_MOD)
3032 ? " [+]" : _(" [Modified]")) : " ",
3033 (curbuf->b_flags & BF_NOTEDITED)
3034#ifdef FEAT_QUICKFIX
3035 && !bt_dontwrite(curbuf)
3036#endif
3037 ? _("[Not edited]") : "",
3038 (curbuf->b_flags & BF_NEW)
3039#ifdef FEAT_QUICKFIX
3040 && !bt_dontwrite(curbuf)
3041#endif
3042 ? _("[New file]") : "",
3043 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3044 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3045 : _("[readonly]")) : "",
3046 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3047 || curbuf->b_p_ro) ?
3048 " " : "");
3049 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3050 * causes an overflow, thus for large numbers divide instead. */
3051 if (curwin->w_cursor.lnum > 1000000L)
3052 n = (int)(((long)curwin->w_cursor.lnum) /
3053 ((long)curbuf->b_ml.ml_line_count / 100L));
3054 else
3055 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3056 (long)curbuf->b_ml.ml_line_count);
3057 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3058 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003059 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 }
3061#ifdef FEAT_CMDL_INFO
3062 else if (p_ru)
3063 {
3064 /* Current line and column are already on the screen -- webb */
3065 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003066 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003068 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 (long)curbuf->b_ml.ml_line_count, n);
3070 }
3071#endif
3072 else
3073 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003074 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 _("line %ld of %ld --%d%%-- col "),
3076 (long)curwin->w_cursor.lnum,
3077 (long)curbuf->b_ml.ml_line_count,
3078 n);
3079 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003080 len = STRLEN(buffer);
3081 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3083 }
3084
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003085 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086
3087 if (dont_truncate)
3088 {
3089 /* Temporarily set msg_scroll to avoid the message being truncated.
3090 * First call msg_start() to get the message in the right place. */
3091 msg_start();
3092 n = msg_scroll;
3093 msg_scroll = TRUE;
3094 msg(buffer);
3095 msg_scroll = n;
3096 }
3097 else
3098 {
3099 p = msg_trunc_attr(buffer, FALSE, 0);
3100 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 /* Need to repeat the message after redrawing when:
3102 * - When restart_edit is set (otherwise there will be a delay
3103 * before redrawing).
3104 * - When the screen was scrolled but there is no wait-return
3105 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003106 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
3108
3109 vim_free(buffer);
3110}
3111
3112 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003113col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003115 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 int col;
3117 int vcol;
3118{
3119 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003120 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003122 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123}
3124
3125#if defined(FEAT_TITLE) || defined(PROTO)
3126/*
3127 * put file name in title bar of window and in icon title
3128 */
3129
3130static char_u *lasttitle = NULL;
3131static char_u *lasticon = NULL;
3132
3133 void
3134maketitle()
3135{
3136 char_u *p;
3137 char_u *t_str = NULL;
3138 char_u *i_name;
3139 char_u *i_str = NULL;
3140 int maxlen = 0;
3141 int len;
3142 int mustset;
3143 char_u buf[IOSIZE];
3144 int off;
3145
3146 if (!redrawing())
3147 {
3148 /* Postpone updating the title when 'lazyredraw' is set. */
3149 need_maketitle = TRUE;
3150 return;
3151 }
3152
3153 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003154 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 return;
3156
3157 if (p_title)
3158 {
3159 if (p_titlelen > 0)
3160 {
3161 maxlen = p_titlelen * Columns / 100;
3162 if (maxlen < 10)
3163 maxlen = 10;
3164 }
3165
3166 t_str = buf;
3167 if (*p_titlestring != NUL)
3168 {
3169#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003170 if (stl_syntax & STL_IN_TITLE)
3171 {
3172 int use_sandbox = FALSE;
3173 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003174
3175# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003176 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003177# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003178 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003180 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003181 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003182 if (called_emsg)
3183 set_string_option_direct((char_u *)"titlestring", -1,
3184 (char_u *)"", OPT_FREE, SID_ERROR);
3185 called_emsg |= save_called_emsg;
3186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 else
3188#endif
3189 t_str = p_titlestring;
3190 }
3191 else
3192 {
3193 /* format: "fname + (path) (1 of 2) - VIM" */
3194
3195 if (curbuf->b_fname == NULL)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003196 vim_strncpy(buf, (char_u *)_("[No Name]"), IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 else
3198 {
3199 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003200 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 }
3203
3204 switch (bufIsChanged(curbuf)
3205 + (curbuf->b_p_ro * 2)
3206 + (!curbuf->b_p_ma * 4))
3207 {
3208 case 1: STRCAT(buf, " +"); break;
3209 case 2: STRCAT(buf, " ="); break;
3210 case 3: STRCAT(buf, " =+"); break;
3211 case 4:
3212 case 6: STRCAT(buf, " -"); break;
3213 case 5:
3214 case 7: STRCAT(buf, " -+"); break;
3215 }
3216
3217 if (curbuf->b_fname != NULL)
3218 {
3219 /* Get path of file, replace home dir with ~ */
3220 off = (int)STRLEN(buf);
3221 buf[off++] = ' ';
3222 buf[off++] = '(';
3223 home_replace(curbuf, curbuf->b_ffname,
3224 buf + off, IOSIZE - off, TRUE);
3225#ifdef BACKSLASH_IN_FILENAME
3226 /* avoid "c:/name" to be reduced to "c" */
3227 if (isalpha(buf[off]) && buf[off + 1] == ':')
3228 off += 2;
3229#endif
3230 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003231 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003234 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003235 (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003238
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 /* translate unprintable chars */
3240 p = transstr(buf + off);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003241 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 STRCAT(buf, ")");
3244 }
3245
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003246 append_arg_number(curwin, buf, IOSIZE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247
3248#if defined(FEAT_CLIENTSERVER)
3249 if (serverName != NULL)
3250 {
3251 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003252 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 }
3254 else
3255#endif
3256 STRCAT(buf, " - VIM");
3257
3258 if (maxlen > 0)
3259 {
3260 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003261 if (vim_strsize(buf) > maxlen)
3262 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 }
3264 }
3265 }
3266 mustset = ti_change(t_str, &lasttitle);
3267
3268 if (p_icon)
3269 {
3270 i_str = buf;
3271 if (*p_iconstring != NUL)
3272 {
3273#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003274 if (stl_syntax & STL_IN_ICON)
3275 {
3276 int use_sandbox = FALSE;
3277 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003278
3279# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003280 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003281# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003282 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003284 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003285 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003286 if (called_emsg)
3287 set_string_option_direct((char_u *)"iconstring", -1,
3288 (char_u *)"", OPT_FREE, SID_ERROR);
3289 called_emsg |= save_called_emsg;
3290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 else
3292#endif
3293 i_str = p_iconstring;
3294 }
3295 else
3296 {
3297 if (buf_spname(curbuf) != NULL)
3298 i_name = (char_u *)buf_spname(curbuf);
3299 else /* use file name only in icon */
3300 i_name = gettail(curbuf->b_ffname);
3301 *i_str = NUL;
3302 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003303 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 if (len > 100)
3305 {
3306 len -= 100;
3307#ifdef FEAT_MBYTE
3308 if (has_mbyte)
3309 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3310#endif
3311 i_name += len;
3312 }
3313 STRCPY(i_str, i_name);
3314 trans_characters(i_str, IOSIZE);
3315 }
3316 }
3317
3318 mustset |= ti_change(i_str, &lasticon);
3319
3320 if (mustset)
3321 resettitle();
3322}
3323
3324/*
3325 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3326 * from "str" if it does.
3327 * Return TRUE when "*last" changed.
3328 */
3329 static int
3330ti_change(str, last)
3331 char_u *str;
3332 char_u **last;
3333{
3334 if ((str == NULL) != (*last == NULL)
3335 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3336 {
3337 vim_free(*last);
3338 if (str == NULL)
3339 *last = NULL;
3340 else
3341 *last = vim_strsave(str);
3342 return TRUE;
3343 }
3344 return FALSE;
3345}
3346
3347/*
3348 * Put current window title back (used after calling a shell)
3349 */
3350 void
3351resettitle()
3352{
3353 mch_settitle(lasttitle, lasticon);
3354}
Bram Moolenaarea408852005-06-25 22:49:46 +00003355
3356# if defined(EXITFREE) || defined(PROTO)
3357 void
3358free_titles()
3359{
3360 vim_free(lasttitle);
3361 vim_free(lasticon);
3362}
3363# endif
3364
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365#endif /* FEAT_TITLE */
3366
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003367#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003369 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 * Return length of string in screen cells.
3371 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003372 * Normally works for window "wp", except when working for 'tabline' then it
3373 * is "curwin".
3374 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 * Items are drawn interspersed with the text that surrounds it
3376 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3377 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3378 *
3379 * If maxwidth is not zero, the string will be filled at any middle marker
3380 * or truncated if too long, fillchar is used for all whitespace.
3381 */
3382 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003383build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3384 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003386 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 size_t outlen; /* length of out[] */
3388 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003389 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 int fillchar;
3391 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003392 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3393 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
3395 char_u *p;
3396 char_u *s;
3397 char_u *t;
3398 char_u *linecont;
3399#ifdef FEAT_EVAL
3400 win_T *o_curwin;
3401 buf_T *o_curbuf;
3402#endif
3403 int empty_line;
3404 colnr_T virtcol;
3405 long l;
3406 long n;
3407 int prevchar_isflag;
3408 int prevchar_isitem;
3409 int itemisflag;
3410 int fillable;
3411 char_u *str;
3412 long num;
3413 int width;
3414 int itemcnt;
3415 int curitem;
3416 int groupitem[STL_MAX_ITEM];
3417 int groupdepth;
3418 struct stl_item
3419 {
3420 char_u *start;
3421 int minwid;
3422 int maxwid;
3423 enum
3424 {
3425 Normal,
3426 Empty,
3427 Group,
3428 Middle,
3429 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003430 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 Trunc
3432 } type;
3433 } item[STL_MAX_ITEM];
3434 int minwid;
3435 int maxwid;
3436 int zeropad;
3437 char_u base;
3438 char_u opt;
3439#define TMPLEN 70
3440 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003441 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003442 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003443
3444#ifdef FEAT_EVAL
3445 /*
3446 * When the format starts with "%!" then evaluate it as an expression and
3447 * use the result as the actual format string.
3448 */
3449 if (fmt[0] == '%' && fmt[1] == '!')
3450 {
3451 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3452 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003453 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003454 }
3455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
3457 if (fillchar == 0)
3458 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003459#ifdef FEAT_MBYTE
3460 /* Can't handle a multi-byte fill character yet. */
3461 else if (mb_char2len(fillchar) > 1)
3462 fillchar = '-';
3463#endif
3464
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 /*
3466 * Get line & check if empty (cursorpos will show "0-1").
3467 * If inversion is possible we use it. Else '=' characters are used.
3468 */
3469 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3470 empty_line = (*linecont == NUL);
3471
3472 groupdepth = 0;
3473 p = out;
3474 curitem = 0;
3475 prevchar_isflag = TRUE;
3476 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003477 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003479 if (curitem == STL_MAX_ITEM)
3480 {
3481 /* There are too many items. Add the error code to the statusline
3482 * to give the user a hint about what went wrong. */
3483 if (p + 6 < out + outlen)
3484 {
3485 mch_memmove(p, " E541", (size_t)5);
3486 p += 5;
3487 }
3488 break;
3489 }
3490
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (*s != NUL && *s != '%')
3492 prevchar_isflag = prevchar_isitem = FALSE;
3493
3494 /*
3495 * Handle up to the next '%' or the end.
3496 */
3497 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3498 *p++ = *s++;
3499 if (*s == NUL || p + 1 >= out + outlen)
3500 break;
3501
3502 /*
3503 * Handle one '%' item.
3504 */
3505 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003506 if (*s == NUL) /* ignore trailing % */
3507 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (*s == '%')
3509 {
3510 if (p + 1 >= out + outlen)
3511 break;
3512 *p++ = *s++;
3513 prevchar_isflag = prevchar_isitem = FALSE;
3514 continue;
3515 }
3516 if (*s == STL_MIDDLEMARK)
3517 {
3518 s++;
3519 if (groupdepth > 0)
3520 continue;
3521 item[curitem].type = Middle;
3522 item[curitem++].start = p;
3523 continue;
3524 }
3525 if (*s == STL_TRUNCMARK)
3526 {
3527 s++;
3528 item[curitem].type = Trunc;
3529 item[curitem++].start = p;
3530 continue;
3531 }
3532 if (*s == ')')
3533 {
3534 s++;
3535 if (groupdepth < 1)
3536 continue;
3537 groupdepth--;
3538
3539 t = item[groupitem[groupdepth]].start;
3540 *p = NUL;
3541 l = vim_strsize(t);
3542 if (curitem > groupitem[groupdepth] + 1
3543 && item[groupitem[groupdepth]].minwid == 0)
3544 {
3545 /* remove group if all items are empty */
3546 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3547 if (item[n].type == Normal)
3548 break;
3549 if (n == curitem)
3550 {
3551 p = t;
3552 l = 0;
3553 }
3554 }
3555 if (l > item[groupitem[groupdepth]].maxwid)
3556 {
3557 /* truncate, remove n bytes of text at the start */
3558#ifdef FEAT_MBYTE
3559 if (has_mbyte)
3560 {
3561 /* Find the first character that should be included. */
3562 n = 0;
3563 while (l >= item[groupitem[groupdepth]].maxwid)
3564 {
3565 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003566 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
3568 }
3569 else
3570#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003571 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572
3573 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003574 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 p = p - n + 1;
3576#ifdef FEAT_MBYTE
3577 /* Fill up space left over by half a double-wide char. */
3578 while (++l < item[groupitem[groupdepth]].minwid)
3579 *p++ = fillchar;
3580#endif
3581
3582 /* correct the start of the items for the truncation */
3583 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3584 {
3585 item[l].start -= n;
3586 if (item[l].start < t)
3587 item[l].start = t;
3588 }
3589 }
3590 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3591 {
3592 /* fill */
3593 n = item[groupitem[groupdepth]].minwid;
3594 if (n < 0)
3595 {
3596 /* fill by appending characters */
3597 n = 0 - n;
3598 while (l++ < n && p + 1 < out + outlen)
3599 *p++ = fillchar;
3600 }
3601 else
3602 {
3603 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003604 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 l = n - l;
3606 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003607 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 p += l;
3609 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3610 item[n].start += l;
3611 for ( ; l > 0; l--)
3612 *t++ = fillchar;
3613 }
3614 }
3615 continue;
3616 }
3617 minwid = 0;
3618 maxwid = 9999;
3619 zeropad = FALSE;
3620 l = 1;
3621 if (*s == '0')
3622 {
3623 s++;
3624 zeropad = TRUE;
3625 }
3626 if (*s == '-')
3627 {
3628 s++;
3629 l = -1;
3630 }
3631 if (VIM_ISDIGIT(*s))
3632 {
3633 minwid = (int)getdigits(&s);
3634 if (minwid < 0) /* overflow */
3635 minwid = 0;
3636 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003637 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
3639 item[curitem].type = Highlight;
3640 item[curitem].start = p;
3641 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3642 s++;
3643 curitem++;
3644 continue;
3645 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003646 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3647 {
3648 if (*s == STL_TABCLOSENR)
3649 {
3650 if (minwid == 0)
3651 {
3652 /* %X ends the close label, go back to the previously
3653 * define tab label nr. */
3654 for (n = curitem - 1; n >= 0; --n)
3655 if (item[n].type == TabPage && item[n].minwid >= 0)
3656 {
3657 minwid = item[n].minwid;
3658 break;
3659 }
3660 }
3661 else
3662 /* close nrs are stored as negative values */
3663 minwid = - minwid;
3664 }
3665 item[curitem].type = TabPage;
3666 item[curitem].start = p;
3667 item[curitem].minwid = minwid;
3668 s++;
3669 curitem++;
3670 continue;
3671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 if (*s == '.')
3673 {
3674 s++;
3675 if (VIM_ISDIGIT(*s))
3676 {
3677 maxwid = (int)getdigits(&s);
3678 if (maxwid <= 0) /* overflow */
3679 maxwid = 50;
3680 }
3681 }
3682 minwid = (minwid > 50 ? 50 : minwid) * l;
3683 if (*s == '(')
3684 {
3685 groupitem[groupdepth++] = curitem;
3686 item[curitem].type = Group;
3687 item[curitem].start = p;
3688 item[curitem].minwid = minwid;
3689 item[curitem].maxwid = maxwid;
3690 s++;
3691 curitem++;
3692 continue;
3693 }
3694 if (vim_strchr(STL_ALL, *s) == NULL)
3695 {
3696 s++;
3697 continue;
3698 }
3699 opt = *s++;
3700
3701 /* OK - now for the real work */
3702 base = 'D';
3703 itemisflag = FALSE;
3704 fillable = TRUE;
3705 num = -1;
3706 str = NULL;
3707 switch (opt)
3708 {
3709 case STL_FILEPATH:
3710 case STL_FULLPATH:
3711 case STL_FILENAME:
3712 fillable = FALSE; /* don't change ' ' to fillchar */
3713 if (buf_spname(wp->w_buffer) != NULL)
3714 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3715 else
3716 {
3717 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003718 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3720 }
3721 trans_characters(NameBuff, MAXPATHL);
3722 if (opt != STL_FILENAME)
3723 str = NameBuff;
3724 else
3725 str = gettail(NameBuff);
3726 break;
3727
3728 case STL_VIM_EXPR: /* '{' */
3729 itemisflag = TRUE;
3730 t = p;
3731 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3732 *p++ = *s++;
3733 if (*s != '}') /* missing '}' or out of space */
3734 break;
3735 s++;
3736 *p = 0;
3737 p = t;
3738
3739#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003740 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3742
3743 o_curbuf = curbuf;
3744 o_curwin = curwin;
3745 curwin = wp;
3746 curbuf = wp->w_buffer;
3747
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003748 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
3750 curwin = o_curwin;
3751 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003752 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753
3754 if (str != NULL && *str != 0)
3755 {
3756 if (*skipdigits(str) == NUL)
3757 {
3758 num = atoi((char *)str);
3759 vim_free(str);
3760 str = NULL;
3761 itemisflag = FALSE;
3762 }
3763 }
3764#endif
3765 break;
3766
3767 case STL_LINE:
3768 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3769 ? 0L : (long)(wp->w_cursor.lnum);
3770 break;
3771
3772 case STL_NUMLINES:
3773 num = wp->w_buffer->b_ml.ml_line_count;
3774 break;
3775
3776 case STL_COLUMN:
3777 num = !(State & INSERT) && empty_line
3778 ? 0 : (int)wp->w_cursor.col + 1;
3779 break;
3780
3781 case STL_VIRTCOL:
3782 case STL_VIRTCOL_ALT:
3783 /* In list mode virtcol needs to be recomputed */
3784 virtcol = wp->w_virtcol;
3785 if (wp->w_p_list && lcs_tab1 == NUL)
3786 {
3787 wp->w_p_list = FALSE;
3788 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3789 wp->w_p_list = TRUE;
3790 }
3791 ++virtcol;
3792 /* Don't display %V if it's the same as %c. */
3793 if (opt == STL_VIRTCOL_ALT
3794 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3795 ? 0 : (int)wp->w_cursor.col + 1)))
3796 break;
3797 num = (long)virtcol;
3798 break;
3799
3800 case STL_PERCENTAGE:
3801 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3802 (long)wp->w_buffer->b_ml.ml_line_count);
3803 break;
3804
3805 case STL_ALTPERCENT:
3806 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003807 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 break;
3809
3810 case STL_ARGLISTSTAT:
3811 fillable = FALSE;
3812 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003813 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 str = tmp;
3815 break;
3816
3817 case STL_KEYMAP:
3818 fillable = FALSE;
3819 if (get_keymap_str(wp, tmp, TMPLEN))
3820 str = tmp;
3821 break;
3822 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003823#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003824 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825#else
3826 num = 0;
3827#endif
3828 break;
3829
3830 case STL_BUFNO:
3831 num = wp->w_buffer->b_fnum;
3832 break;
3833
3834 case STL_OFFSET_X:
3835 base = 'X';
3836 case STL_OFFSET:
3837#ifdef FEAT_BYTEOFF
3838 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3839 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3840 0L : l + 1 + (!(State & INSERT) && empty_line ?
3841 0 : (int)wp->w_cursor.col);
3842#endif
3843 break;
3844
3845 case STL_BYTEVAL_X:
3846 base = 'X';
3847 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003848 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 num = 0;
3850 else
3851 {
3852#ifdef FEAT_MBYTE
3853 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3854#else
3855 num = linecont[wp->w_cursor.col];
3856#endif
3857 }
3858 if (num == NL)
3859 num = 0;
3860 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3861 num = NL;
3862 break;
3863
3864 case STL_ROFLAG:
3865 case STL_ROFLAG_ALT:
3866 itemisflag = TRUE;
3867 if (wp->w_buffer->b_p_ro)
3868 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3869 break;
3870
3871 case STL_HELPFLAG:
3872 case STL_HELPFLAG_ALT:
3873 itemisflag = TRUE;
3874 if (wp->w_buffer->b_help)
3875 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003876 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 break;
3878
3879#ifdef FEAT_AUTOCMD
3880 case STL_FILETYPE:
3881 if (*wp->w_buffer->b_p_ft != NUL
3882 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3883 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003884 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3885 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 str = tmp;
3887 }
3888 break;
3889
3890 case STL_FILETYPE_ALT:
3891 itemisflag = TRUE;
3892 if (*wp->w_buffer->b_p_ft != NUL
3893 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3894 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003895 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3896 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 for (t = tmp; *t != 0; t++)
3898 *t = TOUPPER_LOC(*t);
3899 str = tmp;
3900 }
3901 break;
3902#endif
3903
3904#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3905 case STL_PREVIEWFLAG:
3906 case STL_PREVIEWFLAG_ALT:
3907 itemisflag = TRUE;
3908 if (wp->w_p_pvw)
3909 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3910 : _("[Preview]"));
3911 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003912
3913 case STL_QUICKFIX:
3914 if (bt_quickfix(wp->w_buffer))
3915 str = (char_u *)(wp->w_llist_ref
3916 ? _(msg_loclist)
3917 : _(msg_qflist));
3918 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919#endif
3920
3921 case STL_MODIFIED:
3922 case STL_MODIFIED_ALT:
3923 itemisflag = TRUE;
3924 switch ((opt == STL_MODIFIED_ALT)
3925 + bufIsChanged(wp->w_buffer) * 2
3926 + (!wp->w_buffer->b_p_ma) * 4)
3927 {
3928 case 2: str = (char_u *)"[+]"; break;
3929 case 3: str = (char_u *)",+"; break;
3930 case 4: str = (char_u *)"[-]"; break;
3931 case 5: str = (char_u *)",-"; break;
3932 case 6: str = (char_u *)"[+-]"; break;
3933 case 7: str = (char_u *)",+-"; break;
3934 }
3935 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003936
3937 case STL_HIGHLIGHT:
3938 t = s;
3939 while (*s != '#' && *s != NUL)
3940 ++s;
3941 if (*s == '#')
3942 {
3943 item[curitem].type = Highlight;
3944 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003945 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003946 curitem++;
3947 }
3948 ++s;
3949 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951
3952 item[curitem].start = p;
3953 item[curitem].type = Normal;
3954 if (str != NULL && *str)
3955 {
3956 t = str;
3957 if (itemisflag)
3958 {
3959 if ((t[0] && t[1])
3960 && ((!prevchar_isitem && *t == ',')
3961 || (prevchar_isflag && *t == ' ')))
3962 t++;
3963 prevchar_isflag = TRUE;
3964 }
3965 l = vim_strsize(t);
3966 if (l > 0)
3967 prevchar_isitem = TRUE;
3968 if (l > maxwid)
3969 {
3970 while (l >= maxwid)
3971#ifdef FEAT_MBYTE
3972 if (has_mbyte)
3973 {
3974 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003975 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 }
3977 else
3978#endif
3979 l -= byte2cells(*t++);
3980 if (p + 1 >= out + outlen)
3981 break;
3982 *p++ = '<';
3983 }
3984 if (minwid > 0)
3985 {
3986 for (; l < minwid && p + 1 < out + outlen; l++)
3987 {
3988 /* Don't put a "-" in front of a digit. */
3989 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3990 *p++ = ' ';
3991 else
3992 *p++ = fillchar;
3993 }
3994 minwid = 0;
3995 }
3996 else
3997 minwid *= -1;
3998 while (*t && p + 1 < out + outlen)
3999 {
4000 *p++ = *t++;
4001 /* Change a space by fillchar, unless fillchar is '-' and a
4002 * digit follows. */
4003 if (fillable && p[-1] == ' '
4004 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4005 p[-1] = fillchar;
4006 }
4007 for (; l < minwid && p + 1 < out + outlen; l++)
4008 *p++ = fillchar;
4009 }
4010 else if (num >= 0)
4011 {
4012 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4013 char_u nstr[20];
4014
4015 if (p + 20 >= out + outlen)
4016 break; /* not sufficient space */
4017 prevchar_isitem = TRUE;
4018 t = nstr;
4019 if (opt == STL_VIRTCOL_ALT)
4020 {
4021 *t++ = '-';
4022 minwid--;
4023 }
4024 *t++ = '%';
4025 if (zeropad)
4026 *t++ = '0';
4027 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004028 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 *t = 0;
4030
4031 for (n = num, l = 1; n >= nbase; n /= nbase)
4032 l++;
4033 if (opt == STL_VIRTCOL_ALT)
4034 l++;
4035 if (l > maxwid)
4036 {
4037 l += 2;
4038 n = l - maxwid;
4039 while (l-- > maxwid)
4040 num /= nbase;
4041 *t++ = '>';
4042 *t++ = '%';
4043 *t = t[-3];
4044 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004045 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4046 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
4048 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004049 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4050 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 p += STRLEN(p);
4052 }
4053 else
4054 item[curitem].type = Empty;
4055
4056 if (opt == STL_VIM_EXPR)
4057 vim_free(str);
4058
4059 if (num >= 0 || (!itemisflag && str && *str))
4060 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4061 curitem++;
4062 }
4063 *p = NUL;
4064 itemcnt = curitem;
4065
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004066#ifdef FEAT_EVAL
4067 if (usefmt != fmt)
4068 vim_free(usefmt);
4069#endif
4070
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 width = vim_strsize(out);
4072 if (maxwidth > 0 && width > maxwidth)
4073 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004074 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 l = 0;
4076 if (itemcnt == 0)
4077 s = out;
4078 else
4079 {
4080 for ( ; l < itemcnt; l++)
4081 if (item[l].type == Trunc)
4082 {
4083 /* Truncate at %< item. */
4084 s = item[l].start;
4085 break;
4086 }
4087 if (l == itemcnt)
4088 {
4089 /* No %< item, truncate first item. */
4090 s = item[0].start;
4091 l = 0;
4092 }
4093 }
4094
4095 if (width - vim_strsize(s) >= maxwidth)
4096 {
4097 /* Truncation mark is beyond max length */
4098#ifdef FEAT_MBYTE
4099 if (has_mbyte)
4100 {
4101 s = out;
4102 width = 0;
4103 for (;;)
4104 {
4105 width += ptr2cells(s);
4106 if (width >= maxwidth)
4107 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004108 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110 /* Fill up for half a double-wide character. */
4111 while (++width < maxwidth)
4112 *s++ = fillchar;
4113 }
4114 else
4115#endif
4116 s = out + maxwidth - 1;
4117 for (l = 0; l < itemcnt; l++)
4118 if (item[l].start > s)
4119 break;
4120 itemcnt = l;
4121 *s++ = '>';
4122 *s = 0;
4123 }
4124 else
4125 {
4126#ifdef FEAT_MBYTE
4127 if (has_mbyte)
4128 {
4129 n = 0;
4130 while (width >= maxwidth)
4131 {
4132 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004133 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 }
4135 }
4136 else
4137#endif
4138 n = width - maxwidth + 1;
4139 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004140 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 *s = '<';
4142
4143 /* Fill up for half a double-wide character. */
4144 while (++width < maxwidth)
4145 {
4146 s = s + STRLEN(s);
4147 *s++ = fillchar;
4148 *s = NUL;
4149 }
4150
4151 --n; /* count the '<' */
4152 for (; l < itemcnt; l++)
4153 {
4154 if (item[l].start - n >= s)
4155 item[l].start -= n;
4156 else
4157 item[l].start = s;
4158 }
4159 }
4160 width = maxwidth;
4161 }
4162 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4163 {
4164 /* Apply STL_MIDDLE if any */
4165 for (l = 0; l < itemcnt; l++)
4166 if (item[l].type == Middle)
4167 break;
4168 if (l < itemcnt)
4169 {
4170 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004171 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 for (s = item[l].start; s < p; s++)
4173 *s = fillchar;
4174 for (l++; l < itemcnt; l++)
4175 item[l].start += maxwidth - width;
4176 width = maxwidth;
4177 }
4178 }
4179
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004180 /* Store the info about highlighting. */
4181 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004183 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 for (l = 0; l < itemcnt; l++)
4185 {
4186 if (item[l].type == Highlight)
4187 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004188 sp->start = item[l].start;
4189 sp->userhl = item[l].minwid;
4190 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004193 sp->start = NULL;
4194 sp->userhl = 0;
4195 }
4196
4197 /* Store the info about tab pages labels. */
4198 if (tabtab != NULL)
4199 {
4200 sp = tabtab;
4201 for (l = 0; l < itemcnt; l++)
4202 {
4203 if (item[l].type == TabPage)
4204 {
4205 sp->start = item[l].start;
4206 sp->userhl = item[l].minwid;
4207 sp++;
4208 }
4209 }
4210 sp->start = NULL;
4211 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 }
4213
4214 return width;
4215}
4216#endif /* FEAT_STL_OPT */
4217
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004218#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4219 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004221 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4222 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 */
4224 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004225get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004227 char_u *buf;
4228 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229{
4230 long above; /* number of lines above window */
4231 long below; /* number of lines below window */
4232
4233 above = wp->w_topline - 1;
4234#ifdef FEAT_DIFF
4235 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4236#endif
4237 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4238 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004239 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4240 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004242 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004244 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 ? (int)(above / ((above + below) / 100L))
4246 : (int)(above * 100L / (above + below)));
4247}
4248#endif
4249
4250/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004251 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 * Return TRUE if it was appended.
4253 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004254 static int
4255append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 win_T *wp;
4257 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004258 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260{
4261 char_u *p;
4262
4263 if (ARGCOUNT <= 1) /* nothing to do */
4264 return FALSE;
4265
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004266 p = buf + STRLEN(buf); /* go to the end of the buffer */
4267 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 return FALSE;
4269 *p++ = ' ';
4270 *p++ = '(';
4271 if (add_file)
4272 {
4273 STRCPY(p, "file ");
4274 p += 5;
4275 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004276 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4277 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4279 return TRUE;
4280}
4281
4282/*
4283 * If fname is not a full path, make it a full path.
4284 * Returns pointer to allocated memory (NULL for failure).
4285 */
4286 char_u *
4287fix_fname(fname)
4288 char_u *fname;
4289{
4290 /*
4291 * Force expanding the path always for Unix, because symbolic links may
4292 * mess up the full path name, even though it starts with a '/'.
4293 * Also expand when there is ".." in the file name, try to remove it,
4294 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004295 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 * For MS-Windows also expand names like "longna~1" to "longname".
4297 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004298#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 return FullName_save(fname, TRUE);
4300#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004301 if (!vim_isAbsName(fname)
4302 || strstr((char *)fname, "..") != NULL
4303 || strstr((char *)fname, "//") != NULL
4304# ifdef BACKSLASH_IN_FILENAME
4305 || strstr((char *)fname, "\\\\") != NULL
4306# endif
4307# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004309# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 )
4311 return FullName_save(fname, FALSE);
4312
4313 fname = vim_strsave(fname);
4314
Bram Moolenaar9b942202007-10-03 12:31:33 +00004315# ifdef USE_FNAME_CASE
4316# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004318# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
4320 if (fname != NULL)
4321 fname_case(fname, 0); /* set correct case for file name */
4322 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004323# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324
4325 return fname;
4326#endif
4327}
4328
4329/*
4330 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4331 * "ffname" becomes a pointer to allocated memory (or NULL).
4332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 void
4334fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004335 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 char_u **ffname;
4337 char_u **sfname;
4338{
4339 if (*ffname == NULL) /* if no file name given, nothing to do */
4340 return;
4341 if (*sfname == NULL) /* if no short file name given, use ffname */
4342 *sfname = *ffname;
4343 *ffname = fix_fname(*ffname); /* expand to full path */
4344
4345#ifdef FEAT_SHORTCUT
4346 if (!buf->b_p_bin)
4347 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004348 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349
4350 /* If the file name is a shortcut file, use the file it links to. */
4351 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004352 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 {
4354 vim_free(*ffname);
4355 *ffname = rfname;
4356 *sfname = rfname;
4357 }
4358 }
4359#endif
4360}
4361
4362/*
4363 * Get the file name for an argument list entry.
4364 */
4365 char_u *
4366alist_name(aep)
4367 aentry_T *aep;
4368{
4369 buf_T *bp;
4370
4371 /* Use the name from the associated buffer if it exists. */
4372 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004373 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 return aep->ae_fname;
4375 return bp->b_fname;
4376}
4377
4378#if defined(FEAT_WINDOWS) || defined(PROTO)
4379/*
4380 * do_arg_all(): Open up to 'count' windows, one for each argument.
4381 */
4382 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004383do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 int count;
4385 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004386 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387{
4388 int i;
4389 win_T *wp, *wpnext;
4390 char_u *opened; /* array of flags for which args are open */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004391 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 int use_firstwin = FALSE; /* use first window for arglist */
4393 int split_ret = OK;
4394 int p_ea_save;
4395 alist_T *alist; /* argument list to be used */
4396 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004397 tabpage_T *tpnext;
4398 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004399 win_T *new_curwin = NULL;
4400 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
4402 if (ARGCOUNT <= 0)
4403 {
4404 /* Don't give an error message. We don't want it when the ":all"
4405 * command is in the .vimrc. */
4406 return;
4407 }
4408 setpcmark();
4409
4410 opened_len = ARGCOUNT;
4411 opened = alloc_clear((unsigned)opened_len);
4412 if (opened == NULL)
4413 return;
4414
4415#ifdef FEAT_GUI
4416 need_mouse_correct = TRUE;
4417#endif
4418
4419 /*
4420 * Try closing all windows that are not in the argument list.
4421 * Also close windows that are not full width;
4422 * When 'hidden' or "forceit" set the buffer becomes hidden.
4423 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004424 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004426 if (had_tab > 0)
4427 goto_tabpage_tp(first_tabpage);
4428 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004430 tpnext = curtab->tp_next;
4431 for (wp = firstwin; wp != NULL; wp = wpnext)
4432 {
4433 wpnext = wp->w_next;
4434 buf = wp->w_buffer;
4435 if (buf->b_ffname == NULL
4436 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004438 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004440 )
4441 i = ARGCOUNT;
4442 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004444 /* check if the buffer in this window is in the arglist */
4445 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004447 if (ARGLIST[i].ae_fnum == buf->b_fnum
4448 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004449 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004451 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004452 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004453 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004454 if (i == 0)
4455 {
4456 new_curwin = wp;
4457 new_curtab = curtab;
4458 }
4459 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004460 if (wp->w_alist != curwin->w_alist)
4461 {
4462 /* Use the current argument list for all windows
4463 * containing a file from it. */
4464 alist_unlink(wp->w_alist);
4465 wp->w_alist = curwin->w_alist;
4466 ++wp->w_alist->al_refcount;
4467 }
4468 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004472 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004474 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004476 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004477 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004479 /* If the buffer was changed, and we would like to hide it,
4480 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004481 if (!P_HID(buf) && buf->b_nwindows <= 1
4482 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004484 (void)autowrite(buf, FALSE);
4485#ifdef FEAT_AUTOCMD
4486 /* check if autocommands removed the window */
4487 if (!win_valid(wp) || !buf_valid(buf))
4488 {
4489 wpnext = firstwin; /* start all over... */
4490 continue;
4491 }
4492#endif
4493 }
4494#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004495 /* don't close last window */
4496 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004497#endif
4498 use_firstwin = TRUE;
4499#ifdef FEAT_WINDOWS
4500 else
4501 {
4502 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4503# ifdef FEAT_AUTOCMD
4504 /* check if autocommands removed the next window */
4505 if (!win_valid(wpnext))
4506 wpnext = firstwin; /* start all over... */
4507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
4509#endif
4510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 }
4512 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004513
4514 /* Without the ":tab" modifier only do the current tab page. */
4515 if (had_tab == 0 || tpnext == NULL)
4516 break;
4517
4518# ifdef FEAT_AUTOCMD
4519 /* check if autocommands removed the next tab page */
4520 if (!valid_tabpage(tpnext))
4521 tpnext = first_tabpage; /* start all over...*/
4522# endif
4523 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 }
4525
4526 /*
4527 * Open a window for files in the argument list that don't have one.
4528 * ARGCOUNT may change while doing this, because of autocommands.
4529 */
4530 if (count > ARGCOUNT || count <= 0)
4531 count = ARGCOUNT;
4532
4533 /* Autocommands may do anything to the argument list. Make sure it's not
4534 * freed while we are working here by "locking" it. We still have to
4535 * watch out for its size to be changed. */
4536 alist = curwin->w_alist;
4537 ++alist->al_refcount;
4538
4539#ifdef FEAT_AUTOCMD
4540 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4541 ++autocmd_no_enter;
4542 ++autocmd_no_leave;
4543#endif
4544 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004545#ifdef FEAT_WINDOWS
4546 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4547 * leaving an empty tab page when executed locally. */
4548 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4549 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4550 use_firstwin = TRUE;
4551#endif
4552
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4554 {
4555 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4556 arg_had_last = TRUE;
4557 if (i < opened_len && opened[i])
4558 {
4559 /* Move the already present window to below the current window */
4560 if (curwin->w_arg_idx != i)
4561 {
4562 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4563 {
4564 if (wpnext->w_arg_idx == i)
4565 {
4566 win_move_after(wpnext, curwin);
4567 break;
4568 }
4569 }
4570 }
4571 }
4572 else if (split_ret == OK)
4573 {
4574 if (!use_firstwin) /* split current window */
4575 {
4576 p_ea_save = p_ea;
4577 p_ea = TRUE; /* use space from all windows */
4578 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4579 p_ea = p_ea_save;
4580 if (split_ret == FAIL)
4581 continue;
4582 }
4583#ifdef FEAT_AUTOCMD
4584 else /* first window: do autocmd for leaving this buffer */
4585 --autocmd_no_leave;
4586#endif
4587
4588 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004589 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 */
4591 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004592 if (i == 0)
4593 {
4594 new_curwin = curwin;
4595 new_curtab = curtab;
4596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4598 ECMD_ONE,
4599 ((P_HID(curwin->w_buffer)
4600 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004601 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602#ifdef FEAT_AUTOCMD
4603 if (use_firstwin)
4604 ++autocmd_no_leave;
4605#endif
4606 use_firstwin = FALSE;
4607 }
4608 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004609
4610 /* When ":tab" was used open a new tab for a new window repeatedly. */
4611 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4612 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 }
4614
4615 /* Remove the "lock" on the argument list. */
4616 alist_unlink(alist);
4617
4618#ifdef FEAT_AUTOCMD
4619 --autocmd_no_enter;
4620#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004621 /* to window with first arg */
4622 if (valid_tabpage(new_curtab))
4623 goto_tabpage_tp(new_curtab);
4624 if (win_valid(new_curwin))
4625 win_enter(new_curwin, FALSE);
4626
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627#ifdef FEAT_AUTOCMD
4628 --autocmd_no_leave;
4629#endif
4630 vim_free(opened);
4631}
4632
4633# if defined(FEAT_LISTCMDS) || defined(PROTO)
4634/*
4635 * Open a window for a number of buffers.
4636 */
4637 void
4638ex_buffer_all(eap)
4639 exarg_T *eap;
4640{
4641 buf_T *buf;
4642 win_T *wp, *wpnext;
4643 int split_ret = OK;
4644 int p_ea_save;
4645 int open_wins = 0;
4646 int r;
4647 int count; /* Maximum number of windows to open. */
4648 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004649#ifdef FEAT_WINDOWS
4650 int had_tab = cmdmod.tab;
4651 tabpage_T *tpnext;
4652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
4654 if (eap->addr_count == 0) /* make as many windows as possible */
4655 count = 9999;
4656 else
4657 count = eap->line2; /* make as many windows as specified */
4658 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4659 all = FALSE;
4660 else
4661 all = TRUE;
4662
4663 setpcmark();
4664
4665#ifdef FEAT_GUI
4666 need_mouse_correct = TRUE;
4667#endif
4668
4669 /*
4670 * Close superfluous windows (two windows for the same buffer).
4671 * Also close windows that are not full-width.
4672 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004673#ifdef FEAT_WINDOWS
4674 if (had_tab > 0)
4675 goto_tabpage_tp(first_tabpage);
4676 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004679 tpnext = curtab->tp_next;
4680 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004682 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004683 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004684#ifdef FEAT_VERTSPLIT
4685 || ((cmdmod.split & WSP_VERT)
4686 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004687 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004688 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004690#ifdef FEAT_WINDOWS
4691 || (had_tab > 0 && wp != firstwin)
4692#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004693 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004694 {
4695 win_close(wp, FALSE);
4696#ifdef FEAT_AUTOCMD
4697 wpnext = firstwin; /* just in case an autocommand does
4698 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004699 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004700 open_wins = 0;
4701#endif
4702 }
4703 else
4704 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004706
4707#ifdef FEAT_WINDOWS
4708 /* Without the ":tab" modifier only do the current tab page. */
4709 if (had_tab == 0 || tpnext == NULL)
4710 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004711 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714
4715 /*
4716 * Go through the buffer list. When a buffer doesn't have a window yet,
4717 * open one. Otherwise move the window to the right position.
4718 * Watch out for autocommands that delete buffers or windows!
4719 */
4720#ifdef FEAT_AUTOCMD
4721 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4722 ++autocmd_no_enter;
4723#endif
4724 win_enter(lastwin, FALSE);
4725#ifdef FEAT_AUTOCMD
4726 ++autocmd_no_leave;
4727#endif
4728 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4729 {
4730 /* Check if this buffer needs a window */
4731 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4732 continue;
4733
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004734#ifdef FEAT_WINDOWS
4735 if (had_tab != 0)
4736 {
4737 /* With the ":tab" modifier don't move the window. */
4738 if (buf->b_nwindows > 0)
4739 wp = lastwin; /* buffer has a window, skip it */
4740 else
4741 wp = NULL;
4742 }
4743 else
4744#endif
4745 {
4746 /* Check if this buffer already has a window */
4747 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4748 if (wp->w_buffer == buf)
4749 break;
4750 /* If the buffer already has a window, move it */
4751 if (wp != NULL)
4752 win_move_after(wp, curwin);
4753 }
4754
4755 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
4757 /* Split the window and put the buffer in it */
4758 p_ea_save = p_ea;
4759 p_ea = TRUE; /* use space from all windows */
4760 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4761 ++open_wins;
4762 p_ea = p_ea_save;
4763 if (split_ret == FAIL)
4764 continue;
4765
4766 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004767#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 swap_exists_action = SEA_DIALOG;
4769#endif
4770 set_curbuf(buf, DOBUF_GOTO);
4771#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004774#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 break;
4778 }
4779#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004780#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 if (swap_exists_action == SEA_QUIT)
4782 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004783# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4784 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004785
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004786 /* Reset the error/interrupt/exception state here so that
4787 * aborting() returns FALSE when closing a window. */
4788 enter_cleanup(&cs);
4789# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004790
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004791 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 win_close(curwin, TRUE);
4793 --open_wins;
4794 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004795 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004796
4797# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4798 /* Restore the error/interrupt/exception state if not
4799 * discarded by a new aborting error, interrupt, or uncaught
4800 * exception. */
4801 leave_cleanup(&cs);
4802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 }
4804 else
4805 handle_swap_exists(NULL);
4806#endif
4807 }
4808
4809 ui_breakcheck();
4810 if (got_int)
4811 {
4812 (void)vgetc(); /* only break the file loading, not the rest */
4813 break;
4814 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004815#ifdef FEAT_EVAL
4816 /* Autocommands deleted the buffer or aborted script processing!!! */
4817 if (aborting())
4818 break;
4819#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004820#ifdef FEAT_WINDOWS
4821 /* When ":tab" was used open a new tab for a new window repeatedly. */
4822 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4823 cmdmod.tab = 9999;
4824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
4826#ifdef FEAT_AUTOCMD
4827 --autocmd_no_enter;
4828#endif
4829 win_enter(firstwin, FALSE); /* back to first window */
4830#ifdef FEAT_AUTOCMD
4831 --autocmd_no_leave;
4832#endif
4833
4834 /*
4835 * Close superfluous windows.
4836 */
4837 for (wp = lastwin; open_wins > count; )
4838 {
4839 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4840 || autowrite(wp->w_buffer, FALSE) == OK);
4841#ifdef FEAT_AUTOCMD
4842 if (!win_valid(wp))
4843 {
4844 /* BufWrite Autocommands made the window invalid, start over */
4845 wp = lastwin;
4846 }
4847 else
4848#endif
4849 if (r)
4850 {
4851 win_close(wp, !P_HID(wp->w_buffer));
4852 --open_wins;
4853 wp = lastwin;
4854 }
4855 else
4856 {
4857 wp = wp->w_prev;
4858 if (wp == NULL)
4859 break;
4860 }
4861 }
4862}
4863# endif /* FEAT_LISTCMDS */
4864
4865#endif /* FEAT_WINDOWS */
4866
Bram Moolenaara3227e22006-03-08 21:32:40 +00004867static int chk_modeline __ARGS((linenr_T, int));
4868
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869/*
4870 * do_modelines() - process mode lines for the current file
4871 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004872 * "flags" can be:
4873 * OPT_WINONLY only set options local to window
4874 * OPT_NOWIN don't set options local to window
4875 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 * Returns immediately if the "ml" option isn't set.
4877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004879do_modelines(flags)
4880 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004882 linenr_T lnum;
4883 int nmlines;
4884 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885
4886 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4887 return;
4888
4889 /* Disallow recursive entry here. Can happen when executing a modeline
4890 * triggers an autocommand, which reloads modelines with a ":do". */
4891 if (entered)
4892 return;
4893
4894 ++entered;
4895 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4896 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004897 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 nmlines = 0;
4899
4900 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4901 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004902 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 nmlines = 0;
4904 --entered;
4905}
4906
4907#include "version.h" /* for version number */
4908
4909/*
4910 * chk_modeline() - check a single line for a mode string
4911 * Return FAIL if an error encountered.
4912 */
4913 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004914chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004916 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917{
4918 char_u *s;
4919 char_u *e;
4920 char_u *linecopy; /* local copy of any modeline found */
4921 int prev;
4922 int vers;
4923 int end;
4924 int retval = OK;
4925 char_u *save_sourcing_name;
4926 linenr_T save_sourcing_lnum;
4927#ifdef FEAT_EVAL
4928 scid_T save_SID;
4929#endif
4930
4931 prev = -1;
4932 for (s = ml_get(lnum); *s != NUL; ++s)
4933 {
4934 if (prev == -1 || vim_isspace(prev))
4935 {
4936 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4937 || STRNCMP(s, "vi:", (size_t)3) == 0)
4938 break;
4939 if (STRNCMP(s, "vim", 3) == 0)
4940 {
4941 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4942 e = s + 4;
4943 else
4944 e = s + 3;
4945 vers = getdigits(&e);
4946 if (*e == ':'
4947 && (s[3] == ':'
4948 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4949 || (VIM_VERSION_100 < vers && s[3] == '<')
4950 || (VIM_VERSION_100 > vers && s[3] == '>')
4951 || (VIM_VERSION_100 == vers && s[3] == '=')))
4952 break;
4953 }
4954 }
4955 prev = *s;
4956 }
4957
4958 if (*s)
4959 {
4960 do /* skip over "ex:", "vi:" or "vim:" */
4961 ++s;
4962 while (s[-1] != ':');
4963
4964 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4965 if (linecopy == NULL)
4966 return FAIL;
4967
4968 save_sourcing_lnum = sourcing_lnum;
4969 save_sourcing_name = sourcing_name;
4970 sourcing_lnum = lnum; /* prepare for emsg() */
4971 sourcing_name = (char_u *)"modelines";
4972
4973 end = FALSE;
4974 while (end == FALSE)
4975 {
4976 s = skipwhite(s);
4977 if (*s == NUL)
4978 break;
4979
4980 /*
4981 * Find end of set command: ':' or end of line.
4982 * Skip over "\:", replacing it with ":".
4983 */
4984 for (e = s; *e != ':' && *e != NUL; ++e)
4985 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00004986 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 if (*e == NUL)
4988 end = TRUE;
4989
4990 /*
4991 * If there is a "set" command, require a terminating ':' and
4992 * ignore the stuff after the ':'.
4993 * "vi:set opt opt opt: foo" -- foo not interpreted
4994 * "vi:opt opt opt: foo" -- foo interpreted
4995 * Accept "se" for compatibility with Elvis.
4996 */
4997 if (STRNCMP(s, "set ", (size_t)4) == 0
4998 || STRNCMP(s, "se ", (size_t)3) == 0)
4999 {
5000 if (*e != ':') /* no terminating ':'? */
5001 break;
5002 end = TRUE;
5003 s = vim_strchr(s, ' ') + 1;
5004 }
5005 *e = NUL; /* truncate the set command */
5006
5007 if (*s != NUL) /* skip over an empty "::" */
5008 {
5009#ifdef FEAT_EVAL
5010 save_SID = current_SID;
5011 current_SID = SID_MODELINE;
5012#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005013 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014#ifdef FEAT_EVAL
5015 current_SID = save_SID;
5016#endif
5017 if (retval == FAIL) /* stop if error found */
5018 break;
5019 }
5020 s = e + 1; /* advance to next part */
5021 }
5022
5023 sourcing_lnum = save_sourcing_lnum;
5024 sourcing_name = save_sourcing_name;
5025
5026 vim_free(linecopy);
5027 }
5028 return retval;
5029}
5030
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005031#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 int
5033read_viminfo_bufferlist(virp, writing)
5034 vir_T *virp;
5035 int writing;
5036{
5037 char_u *tab;
5038 linenr_T lnum;
5039 colnr_T col;
5040 buf_T *buf;
5041 char_u *sfname;
5042 char_u *xline;
5043
5044 /* Handle long line and escaped characters. */
5045 xline = viminfo_readstring(virp, 1, FALSE);
5046
5047 /* don't read in if there are files on the command-line or if writing: */
5048 if (xline != NULL && !writing && ARGCOUNT == 0
5049 && find_viminfo_parameter('%') != NULL)
5050 {
5051 /* Format is: <fname> Tab <lnum> Tab <col>.
5052 * Watch out for a Tab in the file name, work from the end. */
5053 lnum = 0;
5054 col = 0;
5055 tab = vim_strrchr(xline, '\t');
5056 if (tab != NULL)
5057 {
5058 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005059 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 tab = vim_strrchr(xline, '\t');
5061 if (tab != NULL)
5062 {
5063 *tab++ = '\0';
5064 lnum = atol((char *)tab);
5065 }
5066 }
5067
5068 /* Expand "~/" in the file name at "line + 1" to a full path.
5069 * Then try shortening it by comparing with the current directory */
5070 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005071 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5074 if (buf != NULL) /* just in case... */
5075 {
5076 buf->b_last_cursor.lnum = lnum;
5077 buf->b_last_cursor.col = col;
5078 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5079 }
5080 }
5081 vim_free(xline);
5082
5083 return viminfo_readline(virp);
5084}
5085
5086 void
5087write_viminfo_bufferlist(fp)
5088 FILE *fp;
5089{
5090 buf_T *buf;
5091#ifdef FEAT_WINDOWS
5092 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005093 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094#endif
5095 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005096 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097
5098 if (find_viminfo_parameter('%') == NULL)
5099 return;
5100
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005101 /* Without a number -1 is returned: do all buffers. */
5102 max_buffers = get_viminfo_parameter('%');
5103
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005105#define LINE_BUF_LEN (MAXPATHL + 40)
5106 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 if (line == NULL)
5108 return;
5109
5110#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005111 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 set_last_cursor(win);
5113#else
5114 set_last_cursor(curwin);
5115#endif
5116
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005117 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5119 {
5120 if (buf->b_fname == NULL
5121 || !buf->b_p_bl
5122#ifdef FEAT_QUICKFIX
5123 || bt_quickfix(buf)
5124#endif
5125 || removable(buf->b_ffname))
5126 continue;
5127
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005128 if (max_buffers-- == 0)
5129 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 putc('%', fp);
5131 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005132 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 (long)buf->b_last_cursor.lnum,
5134 buf->b_last_cursor.col);
5135 viminfo_writestring(fp, line);
5136 }
5137 vim_free(line);
5138}
5139#endif
5140
5141
5142/*
5143 * Return special buffer name.
5144 * Returns NULL when the buffer has a normal file name.
5145 */
5146 char *
5147buf_spname(buf)
5148 buf_T *buf;
5149{
5150#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5151 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005152 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005153 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005154 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005155
5156 /*
5157 * For location list window, w_llist_ref points to the location list.
5158 * For quickfix window, w_llist_ref is NULL.
5159 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005160 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005161 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005162 goto win_found;
5163win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005164 if (win != NULL && win->w_llist_ref != NULL)
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005165 return _(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005166 else
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005167 return _(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169#endif
5170#ifdef FEAT_QUICKFIX
5171 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5172 * contains the name as specified by the user */
5173 if (bt_nofile(buf))
5174 {
5175 if (buf->b_sfname != NULL)
5176 return (char *)buf->b_sfname;
Bram Moolenaarf4f664c2008-12-03 10:21:57 +00005177 return _("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
5179#endif
5180 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005181 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 return NULL;
5183}
5184
5185
5186#if defined(FEAT_SIGNS) || defined(PROTO)
5187/*
5188 * Insert the sign into the signlist.
5189 */
5190 static void
5191insert_sign(buf, prev, next, id, lnum, typenr)
5192 buf_T *buf; /* buffer to store sign in */
5193 signlist_T *prev; /* previous sign entry */
5194 signlist_T *next; /* next sign entry */
5195 int id; /* sign ID */
5196 linenr_T lnum; /* line number which gets the mark */
5197 int typenr; /* typenr of sign we are adding */
5198{
5199 signlist_T *newsign;
5200
5201 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5202 if (newsign != NULL)
5203 {
5204 newsign->id = id;
5205 newsign->lnum = lnum;
5206 newsign->typenr = typenr;
5207 newsign->next = next;
5208#ifdef FEAT_NETBEANS_INTG
5209 newsign->prev = prev;
5210 if (next != NULL)
5211 next->prev = newsign;
5212#endif
5213
5214 if (prev == NULL)
5215 {
5216 /* When adding first sign need to redraw the windows to create the
5217 * column for signs. */
5218 if (buf->b_signlist == NULL)
5219 {
5220 redraw_buf_later(buf, NOT_VALID);
5221 changed_cline_bef_curs();
5222 }
5223
5224 /* first sign in signlist */
5225 buf->b_signlist = newsign;
5226 }
5227 else
5228 prev->next = newsign;
5229 }
5230}
5231
5232/*
5233 * Add the sign into the signlist. Find the right spot to do it though.
5234 */
5235 void
5236buf_addsign(buf, id, lnum, typenr)
5237 buf_T *buf; /* buffer to store sign in */
5238 int id; /* sign ID */
5239 linenr_T lnum; /* line number which gets the mark */
5240 int typenr; /* typenr of sign we are adding */
5241{
5242 signlist_T *sign; /* a sign in the signlist */
5243 signlist_T *prev; /* the previous sign */
5244
5245 prev = NULL;
5246 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5247 {
5248 if (lnum == sign->lnum && id == sign->id)
5249 {
5250 sign->typenr = typenr;
5251 return;
5252 }
5253 else if (
5254#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5255 id < 0 &&
5256#endif
5257 lnum < sign->lnum)
5258 {
5259#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5260 /* XXX - GRP: Is this because of sign slide problem? Or is it
5261 * really needed? Or is it because we allow multiple signs per
5262 * line? If so, should I add that feature to FEAT_SIGNS?
5263 */
5264 while (prev != NULL && prev->lnum == lnum)
5265 prev = prev->prev;
5266 if (prev == NULL)
5267 sign = buf->b_signlist;
5268 else
5269 sign = prev->next;
5270#endif
5271 insert_sign(buf, prev, sign, id, lnum, typenr);
5272 return;
5273 }
5274 prev = sign;
5275 }
5276#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5277 /* XXX - GRP: See previous comment */
5278 while (prev != NULL && prev->lnum == lnum)
5279 prev = prev->prev;
5280 if (prev == NULL)
5281 sign = buf->b_signlist;
5282 else
5283 sign = prev->next;
5284#endif
5285 insert_sign(buf, prev, sign, id, lnum, typenr);
5286
5287 return;
5288}
5289
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005290 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291buf_change_sign_type(buf, markId, typenr)
5292 buf_T *buf; /* buffer to store sign in */
5293 int markId; /* sign ID */
5294 int typenr; /* typenr of sign we are adding */
5295{
5296 signlist_T *sign; /* a sign in the signlist */
5297
5298 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5299 {
5300 if (sign->id == markId)
5301 {
5302 sign->typenr = typenr;
5303 return sign->lnum;
5304 }
5305 }
5306
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005307 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308}
5309
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005310 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311buf_getsigntype(buf, lnum, type)
5312 buf_T *buf;
5313 linenr_T lnum;
5314 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5315{
5316 signlist_T *sign; /* a sign in a b_signlist */
5317
5318 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5319 if (sign->lnum == lnum
5320 && (type == SIGN_ANY
5321# ifdef FEAT_SIGN_ICONS
5322 || (type == SIGN_ICON
5323 && sign_get_image(sign->typenr) != NULL)
5324# endif
5325 || (type == SIGN_TEXT
5326 && sign_get_text(sign->typenr) != NULL)
5327 || (type == SIGN_LINEHL
5328 && sign_get_attr(sign->typenr, TRUE) != 0)))
5329 return sign->typenr;
5330 return 0;
5331}
5332
5333
5334 linenr_T
5335buf_delsign(buf, id)
5336 buf_T *buf; /* buffer sign is stored in */
5337 int id; /* sign id */
5338{
5339 signlist_T **lastp; /* pointer to pointer to current sign */
5340 signlist_T *sign; /* a sign in a b_signlist */
5341 signlist_T *next; /* the next sign in a b_signlist */
5342 linenr_T lnum; /* line number whose sign was deleted */
5343
5344 lastp = &buf->b_signlist;
5345 lnum = 0;
5346 for (sign = buf->b_signlist; sign != NULL; sign = next)
5347 {
5348 next = sign->next;
5349 if (sign->id == id)
5350 {
5351 *lastp = next;
5352#ifdef FEAT_NETBEANS_INTG
5353 if (next != NULL)
5354 next->prev = sign->prev;
5355#endif
5356 lnum = sign->lnum;
5357 vim_free(sign);
5358 break;
5359 }
5360 else
5361 lastp = &sign->next;
5362 }
5363
5364 /* When deleted the last sign need to redraw the windows to remove the
5365 * sign column. */
5366 if (buf->b_signlist == NULL)
5367 {
5368 redraw_buf_later(buf, NOT_VALID);
5369 changed_cline_bef_curs();
5370 }
5371
5372 return lnum;
5373}
5374
5375
5376/*
5377 * Find the line number of the sign with the requested id. If the sign does
5378 * not exist, return 0 as the line number. This will still let the correct file
5379 * get loaded.
5380 */
5381 int
5382buf_findsign(buf, id)
5383 buf_T *buf; /* buffer to store sign in */
5384 int id; /* sign ID */
5385{
5386 signlist_T *sign; /* a sign in the signlist */
5387
5388 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5389 if (sign->id == id)
5390 return sign->lnum;
5391
5392 return 0;
5393}
5394
5395 int
5396buf_findsign_id(buf, lnum)
5397 buf_T *buf; /* buffer whose sign we are searching for */
5398 linenr_T lnum; /* line number of sign */
5399{
5400 signlist_T *sign; /* a sign in the signlist */
5401
5402 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5403 if (sign->lnum == lnum)
5404 return sign->id;
5405
5406 return 0;
5407}
5408
5409
5410# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5411/* see if a given type of sign exists on a specific line */
5412 int
5413buf_findsigntype_id(buf, lnum, typenr)
5414 buf_T *buf; /* buffer whose sign we are searching for */
5415 linenr_T lnum; /* line number of sign */
5416 int typenr; /* sign type number */
5417{
5418 signlist_T *sign; /* a sign in the signlist */
5419
5420 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5421 if (sign->lnum == lnum && sign->typenr == typenr)
5422 return sign->id;
5423
5424 return 0;
5425}
5426
5427
5428# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5429/* return the number of icons on the given line */
5430 int
5431buf_signcount(buf, lnum)
5432 buf_T *buf;
5433 linenr_T lnum;
5434{
5435 signlist_T *sign; /* a sign in the signlist */
5436 int count = 0;
5437
5438 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5439 if (sign->lnum == lnum)
5440 if (sign_get_image(sign->typenr) != NULL)
5441 count++;
5442
5443 return count;
5444}
5445# endif /* FEAT_SIGN_ICONS */
5446# endif /* FEAT_NETBEANS_INTG */
5447
5448
5449/*
5450 * Delete signs in buffer "buf".
5451 */
5452 static void
5453buf_delete_signs(buf)
5454 buf_T *buf;
5455{
5456 signlist_T *next;
5457
5458 while (buf->b_signlist != NULL)
5459 {
5460 next = buf->b_signlist->next;
5461 vim_free(buf->b_signlist);
5462 buf->b_signlist = next;
5463 }
5464}
5465
5466/*
5467 * Delete all signs in all buffers.
5468 */
5469 void
5470buf_delete_all_signs()
5471{
5472 buf_T *buf; /* buffer we are checking for signs */
5473
5474 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5475 if (buf->b_signlist != NULL)
5476 {
5477 /* Need to redraw the windows to remove the sign column. */
5478 redraw_buf_later(buf, NOT_VALID);
5479 buf_delete_signs(buf);
5480 }
5481}
5482
5483/*
5484 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5485 */
5486 void
5487sign_list_placed(rbuf)
5488 buf_T *rbuf;
5489{
5490 buf_T *buf;
5491 signlist_T *p;
5492 char lbuf[BUFSIZ];
5493
5494 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5495 msg_putchar('\n');
5496 if (rbuf == NULL)
5497 buf = firstbuf;
5498 else
5499 buf = rbuf;
5500 while (buf != NULL)
5501 {
5502 if (buf->b_signlist != NULL)
5503 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005504 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5506 msg_putchar('\n');
5507 }
5508 for (p = buf->b_signlist; p != NULL; p = p->next)
5509 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005510 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5512 MSG_PUTS(lbuf);
5513 msg_putchar('\n');
5514 }
5515 if (rbuf != NULL)
5516 break;
5517 buf = buf->b_next;
5518 }
5519}
5520
5521/*
5522 * Adjust a placed sign for inserted/deleted lines.
5523 */
5524 void
5525sign_mark_adjust(line1, line2, amount, amount_after)
5526 linenr_T line1;
5527 linenr_T line2;
5528 long amount;
5529 long amount_after;
5530{
5531 signlist_T *sign; /* a sign in a b_signlist */
5532
5533 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5534 {
5535 if (sign->lnum >= line1 && sign->lnum <= line2)
5536 {
5537 if (amount == MAXLNUM)
5538 sign->lnum = line1;
5539 else
5540 sign->lnum += amount;
5541 }
5542 else if (sign->lnum > line2)
5543 sign->lnum += amount_after;
5544 }
5545}
5546#endif /* FEAT_SIGNS */
5547
5548/*
5549 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5550 */
5551 void
5552set_buflisted(on)
5553 int on;
5554{
5555 if (on != curbuf->b_p_bl)
5556 {
5557 curbuf->b_p_bl = on;
5558#ifdef FEAT_AUTOCMD
5559 if (on)
5560 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5561 else
5562 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5563#endif
5564 }
5565}
5566
5567/*
5568 * Read the file for "buf" again and check if the contents changed.
5569 * Return TRUE if it changed or this could not be checked.
5570 */
5571 int
5572buf_contents_changed(buf)
5573 buf_T *buf;
5574{
5575 buf_T *newbuf;
5576 int differ = TRUE;
5577 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 exarg_T ea;
5580
5581 /* Allocate a buffer without putting it in the buffer list. */
5582 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5583 if (newbuf == NULL)
5584 return TRUE;
5585
5586 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5587 if (prep_exarg(&ea, buf) == FAIL)
5588 {
5589 wipe_buffer(newbuf, FALSE);
5590 return TRUE;
5591 }
5592
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 /* set curwin/curbuf to buf and save a few things */
5594 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595
Bram Moolenaar4770d092006-01-12 23:22:24 +00005596 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 && readfile(buf->b_ffname, buf->b_fname,
5598 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5599 &ea, READ_NEW | READ_DUMMY) == OK)
5600 {
5601 /* compare the two files line by line */
5602 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5603 {
5604 differ = FALSE;
5605 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5606 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5607 {
5608 differ = TRUE;
5609 break;
5610 }
5611 }
5612 }
5613 vim_free(ea.cmd);
5614
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 /* restore curwin/curbuf and a few other things */
5616 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
5618 if (curbuf != newbuf) /* safety check */
5619 wipe_buffer(newbuf, FALSE);
5620
5621 return differ;
5622}
5623
5624/*
5625 * Wipe out a buffer and decrement the last buffer number if it was used for
5626 * this buffer. Call this to wipe out a temp buffer that does not contain any
5627 * marks.
5628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 void
5630wipe_buffer(buf, aucmd)
5631 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005632 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633{
5634 if (buf->b_fnum == top_file_num - 1)
5635 --top_file_num;
5636
5637#ifdef FEAT_AUTOCMD
5638 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005639 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640#endif
5641 close_buffer(NULL, buf, DOBUF_WIPE);
5642#ifdef FEAT_AUTOCMD
5643 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005644 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645#endif
5646}