blob: a8808d396d3cb35c5136e04d807b2bc535d7606a [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
63/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +020064 * Open current buffer, that is: open the memfile and read the file into
65 * memory.
66 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 */
68 int
69open_buffer(read_stdin, eap)
70 int read_stdin; /* read file from stdin */
71 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
72{
73 int retval = OK;
74#ifdef FEAT_AUTOCMD
75 buf_T *old_curbuf;
76#endif
77
78 /*
79 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
80 * When re-entering the same buffer, it should not change, because the
81 * user may have reset the flag by hand.
82 */
83 if (readonlymode && curbuf->b_ffname != NULL
84 && (curbuf->b_flags & BF_NEVERLOADED))
85 curbuf->b_p_ro = TRUE;
86
Bram Moolenaar4770d092006-01-12 23:22:24 +000087 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 {
89 /*
90 * There MUST be a memfile, otherwise we can't do anything
91 * If we can't create one for the current buffer, take another buffer
92 */
93 close_buffer(NULL, curbuf, 0);
94 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
95 if (curbuf->b_ml.ml_mfp != NULL)
96 break;
97 /*
98 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +000099 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100 */
101 if (curbuf == NULL)
102 {
103 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
104 getout(2);
105 }
106 EMSG(_("E83: Cannot allocate buffer, using other one..."));
107 enter_buffer(curbuf);
108 return FAIL;
109 }
110
111#ifdef FEAT_AUTOCMD
112 /* The autocommands in readfile() may change the buffer, but only AFTER
113 * reading the file. */
114 old_curbuf = curbuf;
115 modified_was_set = FALSE;
116#endif
117
118 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100119 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121 if (curbuf->b_ffname != NULL
122#ifdef FEAT_NETBEANS_INTG
123 && netbeansReadFile
124#endif
125 )
126 {
127#ifdef FEAT_NETBEANS_INTG
128 int oldFire = netbeansFireChanges;
129
130 netbeansFireChanges = 0;
131#endif
132 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
133 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_NEW);
134#ifdef FEAT_NETBEANS_INTG
135 netbeansFireChanges = oldFire;
136#endif
137 /* Help buffer is filtered. */
138 if (curbuf->b_help)
139 fix_help_buffer();
140 }
141 else if (read_stdin)
142 {
143 int save_bin = curbuf->b_p_bin;
144 linenr_T line_count;
145
146 /*
147 * First read the text in binary mode into the buffer.
148 * Then read from that same buffer and append at the end. This makes
149 * it possible to retry when 'fileformat' or 'fileencoding' was
150 * guessed wrong.
151 */
152 curbuf->b_p_bin = TRUE;
153 retval = readfile(NULL, NULL, (linenr_T)0,
154 (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW + READ_STDIN);
155 curbuf->b_p_bin = save_bin;
156 if (retval == OK)
157 {
158 line_count = curbuf->b_ml.ml_line_count;
159 retval = readfile(NULL, NULL, (linenr_T)line_count,
160 (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_BUFFER);
161 if (retval == OK)
162 {
163 /* Delete the binary lines. */
164 while (--line_count >= 0)
165 ml_delete((linenr_T)1, FALSE);
166 }
167 else
168 {
169 /* Delete the converted lines. */
170 while (curbuf->b_ml.ml_line_count > line_count)
171 ml_delete(line_count, FALSE);
172 }
173 /* Put the cursor on the first line. */
174 curwin->w_cursor.lnum = 1;
175 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000176
177 /* Set or reset 'modified' before executing autocommands, so that
178 * it can be changed there. */
179 if (!readonlymode && !bufempty())
180 changed();
181 else if (retval != FAIL)
182 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#ifdef FEAT_AUTOCMD
184# ifdef FEAT_EVAL
185 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
186 curbuf, &retval);
187# else
188 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
189# endif
190#endif
191 }
192 }
193
194 /* if first time loading this buffer, init b_chartab[] */
195 if (curbuf->b_flags & BF_NEVERLOADED)
196 (void)buf_init_chartab(curbuf, FALSE);
197
198 /*
199 * Set/reset the Changed flag first, autocmds may change the buffer.
200 * Apply the automatic commands, before processing the modelines.
201 * So the modelines have priority over auto commands.
202 */
203 /* When reading stdin, the buffer contents always needs writing, so set
204 * the changed flag. Unless in readonly mode: "ls | gview -".
205 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000206 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifdef FEAT_AUTOCMD
208 || modified_was_set /* ":set modified" used in autocmd */
209# ifdef FEAT_EVAL
210 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
211# endif
212#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000213 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000215 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 unchanged(curbuf, FALSE);
217 save_file_ff(curbuf); /* keep this fileformat */
218
219 /* require "!" to overwrite the file, because it wasn't read completely */
220#ifdef FEAT_EVAL
221 if (aborting())
222#else
223 if (got_int)
224#endif
225 curbuf->b_flags |= BF_READERR;
226
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000227#ifdef FEAT_FOLDING
228 /* Need to update automatic folding. Do this before the autocommands,
229 * they may use the fold info. */
230 foldUpdateAll(curwin);
231#endif
232
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#ifdef FEAT_AUTOCMD
234 /* need to set w_topline, unless some autocommand already did that. */
235 if (!(curwin->w_valid & VALID_TOPLINE))
236 {
237 curwin->w_topline = 1;
238# ifdef FEAT_DIFF
239 curwin->w_topfill = 0;
240# endif
241 }
242# ifdef FEAT_EVAL
243 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
244# else
245 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
246# endif
247#endif
248
249 if (retval != FAIL)
250 {
251#ifdef FEAT_AUTOCMD
252 /*
253 * The autocommands may have changed the current buffer. Apply the
254 * modelines to the correct buffer, if it still exists and is loaded.
255 */
256 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
257 {
258 aco_save_T aco;
259
260 /* Go to the buffer that was opened. */
261 aucmd_prepbuf(&aco, old_curbuf);
262#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000263 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
265
266#ifdef FEAT_AUTOCMD
267# ifdef FEAT_EVAL
268 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
269 &retval);
270# else
271 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
272# endif
273
274 /* restore curwin/curbuf and a few other things */
275 aucmd_restbuf(&aco);
276 }
277#endif
278 }
279
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 return retval;
281}
282
283/*
284 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
285 */
286 int
287buf_valid(buf)
288 buf_T *buf;
289{
290 buf_T *bp;
291
292 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
293 if (bp == buf)
294 return TRUE;
295 return FALSE;
296}
297
298/*
299 * Close the link to a buffer.
300 * "action" is used when there is no longer a window for the buffer.
301 * It can be:
302 * 0 buffer becomes hidden
303 * DOBUF_UNLOAD buffer is unloaded
304 * DOBUF_DELETE buffer is unloaded and removed from buffer list
305 * DOBUF_WIPE buffer is unloaded and really deleted
306 * When doing all but the first one on the current buffer, the caller should
307 * get a new buffer very soon!
308 *
309 * The 'bufhidden' option can force freeing and deleting.
310 */
311 void
312close_buffer(win, buf, action)
313 win_T *win; /* if not NULL, set b_last_cursor */
314 buf_T *buf;
315 int action;
316{
317#ifdef FEAT_AUTOCMD
318 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100319 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#endif
321 int unload_buf = (action != 0);
322 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
323 int wipe_buf = (action == DOBUF_WIPE);
324
325#ifdef FEAT_QUICKFIX
326 /*
327 * Force unloading or deleting when 'bufhidden' says so.
328 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
329 * "hide" (otherwise we could never free or delete a buffer).
330 */
331 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
332 {
333 del_buf = TRUE;
334 unload_buf = TRUE;
335 }
336 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
337 {
338 del_buf = TRUE;
339 unload_buf = TRUE;
340 wipe_buf = TRUE;
341 }
342 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
343 unload_buf = TRUE;
344#endif
345
346 if (win != NULL)
347 {
348 /* Set b_last_cursor when closing the last window for the buffer.
349 * Remember the last cursor position and window options of the buffer.
350 * This used to be only for the current window, but then options like
351 * 'foldmethod' may be lost with a ":only" command. */
352 if (buf->b_nwindows == 1)
353 set_last_cursor(win);
354 buflist_setfpos(buf, win,
355 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
356 win->w_cursor.col, TRUE);
357 }
358
359#ifdef FEAT_AUTOCMD
360 /* When the buffer is no longer in a window, trigger BufWinLeave */
361 if (buf->b_nwindows == 1)
362 {
363 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
364 FALSE, buf);
365 if (!buf_valid(buf)) /* autocommands may delete the buffer */
366 return;
367
368 /* When the buffer becomes hidden, but is not unloaded, trigger
369 * BufHidden */
370 if (!unload_buf)
371 {
372 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
373 FALSE, buf);
374 if (!buf_valid(buf)) /* autocmds may delete the buffer */
375 return;
376 }
377# ifdef FEAT_EVAL
378 if (aborting()) /* autocmds may abort script processing */
379 return;
380# endif
381 }
382 nwindows = buf->b_nwindows;
383#endif
384
385 /* decrease the link count from windows (unless not in any window) */
386 if (buf->b_nwindows > 0)
387 --buf->b_nwindows;
388
389 /* Return when a window is displaying the buffer or when it's not
390 * unloaded. */
391 if (buf->b_nwindows > 0 || !unload_buf)
392 {
Bram Moolenaar607a95ed2006-03-28 20:57:42 +0000393#if 0 /* why was this here? */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 if (buf == curbuf)
395 u_sync(); /* sync undo before going to another buffer */
Bram Moolenaar607a95ed2006-03-28 20:57:42 +0000396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 return;
398 }
399
400 /* Always remove the buffer when there is no file name. */
401 if (buf->b_ffname == NULL)
402 del_buf = TRUE;
403
404 /*
405 * Free all things allocated for this buffer.
406 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
407 */
408#ifdef FEAT_AUTOCMD
409 /* Remember if we are closing the current buffer. Restore the number of
410 * windows, so that autocommands in buf_freeall() don't get confused. */
411 is_curbuf = (buf == curbuf);
412 buf->b_nwindows = nwindows;
413#endif
414
415 buf_freeall(buf, del_buf, wipe_buf);
416
417#ifdef FEAT_AUTOCMD
418 /* Autocommands may have deleted the buffer. */
419 if (!buf_valid(buf))
420 return;
421# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000422 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 return;
424# endif
425
426 /* Autocommands may have opened or closed windows for this buffer.
427 * Decrement the count for the close we do here. */
428 if (buf->b_nwindows > 0)
429 --buf->b_nwindows;
430
431 /*
432 * It's possible that autocommands change curbuf to the one being deleted.
433 * This might cause the previous curbuf to be deleted unexpectedly. But
434 * in some cases it's OK to delete the curbuf, because a new one is
435 * obtained anyway. Therefore only return if curbuf changed to the
436 * deleted buffer.
437 */
438 if (buf == curbuf && !is_curbuf)
439 return;
440#endif
441
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000442 /* Change directories when the 'acd' option is set. */
443 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
445 /*
446 * Remove the buffer from the list.
447 */
448 if (wipe_buf)
449 {
450#ifdef FEAT_SUN_WORKSHOP
451 if (usingSunWorkShop)
452 workshop_file_closed_lineno((char *)buf->b_ffname,
453 (int)buf->b_last_cursor.lnum);
454#endif
455 vim_free(buf->b_ffname);
456 vim_free(buf->b_sfname);
457 if (buf->b_prev == NULL)
458 firstbuf = buf->b_next;
459 else
460 buf->b_prev->b_next = buf->b_next;
461 if (buf->b_next == NULL)
462 lastbuf = buf->b_prev;
463 else
464 buf->b_next->b_prev = buf->b_prev;
465 free_buffer(buf);
466 }
467 else
468 {
469 if (del_buf)
470 {
471 /* Free all internal variables and reset option values, to make
472 * ":bdel" compatible with Vim 5.7. */
473 free_buffer_stuff(buf, TRUE);
474
475 /* Make it look like a new buffer. */
476 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
477
478 /* Init the options when loaded again. */
479 buf->b_p_initialized = FALSE;
480 }
481 buf_clear_file(buf);
482 if (del_buf)
483 buf->b_p_bl = FALSE;
484 }
485}
486
487/*
488 * Make buffer not contain a file.
489 */
490 void
491buf_clear_file(buf)
492 buf_T *buf;
493{
494 buf->b_ml.ml_line_count = 1;
495 unchanged(buf, TRUE);
496#ifndef SHORT_FNAME
497 buf->b_shortname = FALSE;
498#endif
499 buf->b_p_eol = TRUE;
500 buf->b_start_eol = TRUE;
501#ifdef FEAT_MBYTE
502 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000503 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504#endif
505 buf->b_ml.ml_mfp = NULL;
506 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
507#ifdef FEAT_NETBEANS_INTG
508 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509#endif
510}
511
512/*
513 * buf_freeall() - free all things allocated for a buffer that are related to
514 * the file.
515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 void
517buf_freeall(buf, del_buf, wipe_buf)
518 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +0000519 int del_buf UNUSED; /* buffer is going to be deleted */
520 int wipe_buf UNUSED; /* buffer is going to be wiped out */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521{
522#ifdef FEAT_AUTOCMD
523 int is_curbuf = (buf == curbuf);
524
525 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
526 if (!buf_valid(buf)) /* autocommands may delete the buffer */
527 return;
528 if (del_buf && buf->b_p_bl)
529 {
530 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
531 if (!buf_valid(buf)) /* autocommands may delete the buffer */
532 return;
533 }
534 if (wipe_buf)
535 {
536 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
537 FALSE, buf);
538 if (!buf_valid(buf)) /* autocommands may delete the buffer */
539 return;
540 }
541# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000542 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 return;
544# endif
545
546 /*
547 * It's possible that autocommands change curbuf to the one being deleted.
548 * This might cause curbuf to be deleted unexpectedly. But in some cases
549 * it's OK to delete the curbuf, because a new one is obtained anyway.
550 * Therefore only return if curbuf changed to the deleted buffer.
551 */
552 if (buf == curbuf && !is_curbuf)
553 return;
554#endif
555#ifdef FEAT_DIFF
556 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
557#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000558
559#ifdef FEAT_FOLDING
560 /* No folds in an empty buffer. */
561# ifdef FEAT_WINDOWS
562 {
563 win_T *win;
564 tabpage_T *tp;
565
566 FOR_ALL_TAB_WINDOWS(tp, win)
567 if (win->w_buffer == buf)
568 clearFolding(win);
569 }
570# else
571 if (curwin->w_buffer == buf)
572 clearFolding(curwin);
573# endif
574#endif
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576#ifdef FEAT_TCL
577 tcl_buffer_free(buf);
578#endif
579 u_blockfree(buf); /* free the memory allocated for undo */
580 ml_close(buf, TRUE); /* close and delete the memline/memfile */
581 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
582 u_clearall(buf); /* reset all undo information */
583#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200584 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000586 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587}
588
589/*
590 * Free a buffer structure and the things it contains related to the buffer
591 * itself (not the file, that must have been done already).
592 */
593 static void
594free_buffer(buf)
595 buf_T *buf;
596{
597 free_buffer_stuff(buf, TRUE);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200598#ifdef FEAT_LUA
599 lua_buffer_free(buf);
600#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000601#ifdef FEAT_MZSCHEME
602 mzscheme_buffer_free(buf);
603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604#ifdef FEAT_PERL
605 perl_buf_free(buf);
606#endif
607#ifdef FEAT_PYTHON
608 python_buffer_free(buf);
609#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200610#ifdef FEAT_PYTHON3
611 python3_buffer_free(buf);
612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613#ifdef FEAT_RUBY
614 ruby_buffer_free(buf);
615#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000616#ifdef FEAT_AUTOCMD
617 aubuflocal_remove(buf);
618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 vim_free(buf);
620}
621
622/*
623 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
624 */
625 static void
626free_buffer_stuff(buf, free_options)
627 buf_T *buf;
628 int free_options; /* free options as well */
629{
630 if (free_options)
631 {
632 clear_wininfo(buf); /* including window-local options */
633 free_buf_options(buf, TRUE);
634 }
635#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000636 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
637 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638#endif
639#ifdef FEAT_USR_CMDS
640 uc_clear(&buf->b_ucmds); /* clear local user commands */
641#endif
642#ifdef FEAT_SIGNS
643 buf_delete_signs(buf); /* delete any signs */
644#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000645#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200646 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648#ifdef FEAT_LOCALMAP
649 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
650 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
651#endif
652#ifdef FEAT_MBYTE
653 vim_free(buf->b_start_fenc);
654 buf->b_start_fenc = NULL;
655#endif
Bram Moolenaar9381ab72008-11-12 11:52:19 +0000656#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200657 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar9381ab72008-11-12 11:52:19 +0000658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659}
660
661/*
662 * Free the b_wininfo list for buffer "buf".
663 */
664 static void
665clear_wininfo(buf)
666 buf_T *buf;
667{
668 wininfo_T *wip;
669
670 while (buf->b_wininfo != NULL)
671 {
672 wip = buf->b_wininfo;
673 buf->b_wininfo = wip->wi_next;
674 if (wip->wi_optset)
675 {
676 clear_winopt(&wip->wi_opt);
677#ifdef FEAT_FOLDING
678 deleteFoldRecurse(&wip->wi_folds);
679#endif
680 }
681 vim_free(wip);
682 }
683}
684
685#if defined(FEAT_LISTCMDS) || defined(PROTO)
686/*
687 * Go to another buffer. Handles the result of the ATTENTION dialog.
688 */
689 void
690goto_buffer(eap, start, dir, count)
691 exarg_T *eap;
692 int start;
693 int dir;
694 int count;
695{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000696# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 buf_T *old_curbuf = curbuf;
698
699 swap_exists_action = SEA_DIALOG;
700# endif
701 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
702 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000703# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
705 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000706# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
707 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000708
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000709 /* Reset the error/interrupt/exception state here so that
710 * aborting() returns FALSE when closing a window. */
711 enter_cleanup(&cs);
712# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000713
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000714 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 win_close(curwin, TRUE);
716 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000717 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000718
719# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
720 /* Restore the error/interrupt/exception state if not discarded by a
721 * new aborting error, interrupt, or uncaught exception. */
722 leave_cleanup(&cs);
723# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 }
725 else
726 handle_swap_exists(old_curbuf);
727# endif
728}
729#endif
730
Bram Moolenaare64ac772005-12-07 20:54:59 +0000731#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732/*
733 * Handle the situation of swap_exists_action being set.
734 * It is allowed for "old_curbuf" to be NULL or invalid.
735 */
736 void
737handle_swap_exists(old_curbuf)
738 buf_T *old_curbuf;
739{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000740# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
741 cleanup_T cs;
742# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000743
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 if (swap_exists_action == SEA_QUIT)
745 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000746# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
747 /* Reset the error/interrupt/exception state here so that
748 * aborting() returns FALSE when closing a buffer. */
749 enter_cleanup(&cs);
750# endif
751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 /* User selected Quit at ATTENTION prompt. Go back to previous
753 * buffer. If that buffer is gone or the same as the current one,
754 * open a new, empty buffer. */
755 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000756 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
758 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000759 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 if (old_curbuf != NULL)
761 enter_buffer(old_curbuf);
762 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000763
764# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
765 /* Restore the error/interrupt/exception state if not discarded by a
766 * new aborting error, interrupt, or uncaught exception. */
767 leave_cleanup(&cs);
768# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 }
770 else if (swap_exists_action == SEA_RECOVER)
771 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000772# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
773 /* Reset the error/interrupt/exception state here so that
774 * aborting() returns FALSE when closing a buffer. */
775 enter_cleanup(&cs);
776# endif
777
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 /* User selected Recover at ATTENTION prompt. */
779 msg_scroll = TRUE;
780 ml_recover();
781 MSG_PUTS("\n"); /* don't overwrite the last message */
782 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000783 do_modelines(0);
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 swap_exists_action = SEA_NONE;
792}
793#endif
794
795#if defined(FEAT_LISTCMDS) || defined(PROTO)
796/*
797 * do_bufdel() - delete or unload buffer(s)
798 *
799 * addr_count == 0: ":bdel" - delete current buffer
800 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
801 * buffer "end_bnr", then any other arguments.
802 * addr_count == 2: ":N,N bdel" - delete buffers in range
803 *
804 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
805 * DOBUF_DEL (":bdel")
806 *
807 * Returns error message or NULL
808 */
809 char_u *
810do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
811 int command;
812 char_u *arg; /* pointer to extra arguments */
813 int addr_count;
814 int start_bnr; /* first buffer number in a range */
815 int end_bnr; /* buffer nr or last buffer nr in a range */
816 int forceit;
817{
818 int do_current = 0; /* delete current buffer? */
819 int deleted = 0; /* number of buffers deleted */
820 char_u *errormsg = NULL; /* return value */
821 int bnr; /* buffer number */
822 char_u *p;
823
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 if (addr_count == 0)
825 {
826 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
827 }
828 else
829 {
830 if (addr_count == 2)
831 {
832 if (*arg) /* both range and argument is not allowed */
833 return (char_u *)_(e_trailing);
834 bnr = start_bnr;
835 }
836 else /* addr_count == 1 */
837 bnr = end_bnr;
838
839 for ( ;!got_int; ui_breakcheck())
840 {
841 /*
842 * delete the current buffer last, otherwise when the
843 * current buffer is deleted, the next buffer becomes
844 * the current one and will be loaded, which may then
845 * also be deleted, etc.
846 */
847 if (bnr == curbuf->b_fnum)
848 do_current = bnr;
849 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
850 forceit) == OK)
851 ++deleted;
852
853 /*
854 * find next buffer number to delete/unload
855 */
856 if (addr_count == 2)
857 {
858 if (++bnr > end_bnr)
859 break;
860 }
861 else /* addr_count == 1 */
862 {
863 arg = skipwhite(arg);
864 if (*arg == NUL)
865 break;
866 if (!VIM_ISDIGIT(*arg))
867 {
868 p = skiptowhite_esc(arg);
869 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
870 if (bnr < 0) /* failed */
871 break;
872 arg = p;
873 }
874 else
875 bnr = getdigits(&arg);
876 }
877 }
878 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
879 FORWARD, do_current, forceit) == OK)
880 ++deleted;
881
882 if (deleted == 0)
883 {
884 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000885 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000887 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000889 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 errormsg = IObuff;
891 }
892 else if (deleted >= p_report)
893 {
894 if (command == DOBUF_UNLOAD)
895 {
896 if (deleted == 1)
897 MSG(_("1 buffer unloaded"));
898 else
899 smsg((char_u *)_("%d buffers unloaded"), deleted);
900 }
901 else if (command == DOBUF_DEL)
902 {
903 if (deleted == 1)
904 MSG(_("1 buffer deleted"));
905 else
906 smsg((char_u *)_("%d buffers deleted"), deleted);
907 }
908 else
909 {
910 if (deleted == 1)
911 MSG(_("1 buffer wiped out"));
912 else
913 smsg((char_u *)_("%d buffers wiped out"), deleted);
914 }
915 }
916 }
917
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
919 return errormsg;
920}
921
922/*
923 * Implementation of the commands for the buffer list.
924 *
925 * action == DOBUF_GOTO go to specified buffer
926 * action == DOBUF_SPLIT split window and go to specified buffer
927 * action == DOBUF_UNLOAD unload specified buffer(s)
928 * action == DOBUF_DEL delete specified buffer(s) from buffer list
929 * action == DOBUF_WIPE delete specified buffer(s) really
930 *
931 * start == DOBUF_CURRENT go to "count" buffer from current buffer
932 * start == DOBUF_FIRST go to "count" buffer from first buffer
933 * start == DOBUF_LAST go to "count" buffer from last buffer
934 * start == DOBUF_MOD go to "count" modified buffer from current buffer
935 *
936 * Return FAIL or OK.
937 */
938 int
939do_buffer(action, start, dir, count, forceit)
940 int action;
941 int start;
942 int dir; /* FORWARD or BACKWARD */
943 int count; /* buffer number or number of buffers */
944 int forceit; /* TRUE for :...! */
945{
946 buf_T *buf;
947 buf_T *bp;
948 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
949 || action == DOBUF_WIPE);
950
951 switch (start)
952 {
953 case DOBUF_FIRST: buf = firstbuf; break;
954 case DOBUF_LAST: buf = lastbuf; break;
955 default: buf = curbuf; break;
956 }
957 if (start == DOBUF_MOD) /* find next modified buffer */
958 {
959 while (count-- > 0)
960 {
961 do
962 {
963 buf = buf->b_next;
964 if (buf == NULL)
965 buf = firstbuf;
966 }
967 while (buf != curbuf && !bufIsChanged(buf));
968 }
969 if (!bufIsChanged(buf))
970 {
971 EMSG(_("E84: No modified buffer found"));
972 return FAIL;
973 }
974 }
975 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
976 {
977 while (buf != NULL && buf->b_fnum != count)
978 buf = buf->b_next;
979 }
980 else
981 {
982 bp = NULL;
983 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
984 {
985 /* remember the buffer where we start, we come back there when all
986 * buffers are unlisted. */
987 if (bp == NULL)
988 bp = buf;
989 if (dir == FORWARD)
990 {
991 buf = buf->b_next;
992 if (buf == NULL)
993 buf = firstbuf;
994 }
995 else
996 {
997 buf = buf->b_prev;
998 if (buf == NULL)
999 buf = lastbuf;
1000 }
1001 /* don't count unlisted buffers */
1002 if (unload || buf->b_p_bl)
1003 {
1004 --count;
1005 bp = NULL; /* use this buffer as new starting point */
1006 }
1007 if (bp == buf)
1008 {
1009 /* back where we started, didn't find anything. */
1010 EMSG(_("E85: There is no listed buffer"));
1011 return FAIL;
1012 }
1013 }
1014 }
1015
1016 if (buf == NULL) /* could not find it */
1017 {
1018 if (start == DOBUF_FIRST)
1019 {
1020 /* don't warn when deleting */
1021 if (!unload)
1022 EMSGN(_("E86: Buffer %ld does not exist"), count);
1023 }
1024 else if (dir == FORWARD)
1025 EMSG(_("E87: Cannot go beyond last buffer"));
1026 else
1027 EMSG(_("E88: Cannot go before first buffer"));
1028 return FAIL;
1029 }
1030
1031#ifdef FEAT_GUI
1032 need_mouse_correct = TRUE;
1033#endif
1034
1035#ifdef FEAT_LISTCMDS
1036 /*
1037 * delete buffer buf from memory and/or the list
1038 */
1039 if (unload)
1040 {
1041 int forward;
1042 int retval;
1043
1044 /* When unloading or deleting a buffer that's already unloaded and
1045 * unlisted: fail silently. */
1046 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1047 return FAIL;
1048
1049 if (!forceit && bufIsChanged(buf))
1050 {
1051#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1052 if ((p_confirm || cmdmod.confirm) && p_write)
1053 {
1054 dialog_changed(buf, FALSE);
1055# ifdef FEAT_AUTOCMD
1056 if (!buf_valid(buf))
1057 /* Autocommand deleted buffer, oops! It's not changed
1058 * now. */
1059 return FAIL;
1060# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001061 /* If it's still changed fail silently, the dialog already
1062 * mentioned why it fails. */
1063 if (bufIsChanged(buf))
1064 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001066 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067#endif
1068 {
1069 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1070 buf->b_fnum);
1071 return FAIL;
1072 }
1073 }
1074
1075 /*
1076 * If deleting the last (listed) buffer, make it empty.
1077 * The last (listed) buffer cannot be unloaded.
1078 */
1079 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1080 if (bp->b_p_bl && bp != buf)
1081 break;
1082 if (bp == NULL && buf == curbuf)
1083 {
1084 if (action == DOBUF_UNLOAD)
1085 {
1086 EMSG(_("E90: Cannot unload last buffer"));
1087 return FAIL;
1088 }
1089
1090 /* Close any other windows on this buffer, then make it empty. */
1091#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001092 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093#endif
1094 setpcmark();
1095 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001096 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097
1098 /*
1099 * do_ecmd() may create a new buffer, then we have to delete
1100 * the old one. But do_ecmd() may have done that already, check
1101 * if the buffer still exists.
1102 */
1103 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1104 close_buffer(NULL, buf, action);
1105 return retval;
1106 }
1107
1108#ifdef FEAT_WINDOWS
1109 /*
1110 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001111 * (unless it's the only window). Repeat this so long as we end up in
1112 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001114 while (buf == curbuf
1115 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 win_close(curwin, FALSE);
1117#endif
1118
1119 /*
1120 * If the buffer to be deleted is not the current one, delete it here.
1121 */
1122 if (buf != curbuf)
1123 {
1124#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001125 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126#endif
1127 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1128 close_buffer(NULL, buf, action);
1129 return OK;
1130 }
1131
1132 /*
1133 * Deleting the current buffer: Need to find another buffer to go to.
1134 * There must be another, otherwise it would have been handled above.
1135 * First use au_new_curbuf, if it is valid.
1136 * Then prefer the buffer we most recently visited.
1137 * Else try to find one that is loaded, after the current buffer,
1138 * then before the current buffer.
1139 * Finally use any buffer.
1140 */
1141 buf = NULL; /* selected buffer */
1142 bp = NULL; /* used when no loaded buffer found */
1143#ifdef FEAT_AUTOCMD
1144 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1145 buf = au_new_curbuf;
1146# ifdef FEAT_JUMPLIST
1147 else
1148# endif
1149#endif
1150#ifdef FEAT_JUMPLIST
1151 if (curwin->w_jumplistlen > 0)
1152 {
1153 int jumpidx;
1154
1155 jumpidx = curwin->w_jumplistidx - 1;
1156 if (jumpidx < 0)
1157 jumpidx = curwin->w_jumplistlen - 1;
1158
1159 forward = jumpidx;
1160 while (jumpidx != curwin->w_jumplistidx)
1161 {
1162 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1163 if (buf != NULL)
1164 {
1165 if (buf == curbuf || !buf->b_p_bl)
1166 buf = NULL; /* skip current and unlisted bufs */
1167 else if (buf->b_ml.ml_mfp == NULL)
1168 {
1169 /* skip unloaded buf, but may keep it for later */
1170 if (bp == NULL)
1171 bp = buf;
1172 buf = NULL;
1173 }
1174 }
1175 if (buf != NULL) /* found a valid buffer: stop searching */
1176 break;
1177 /* advance to older entry in jump list */
1178 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1179 break;
1180 if (--jumpidx < 0)
1181 jumpidx = curwin->w_jumplistlen - 1;
1182 if (jumpidx == forward) /* List exhausted for sure */
1183 break;
1184 }
1185 }
1186#endif
1187
1188 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1189 {
1190 forward = TRUE;
1191 buf = curbuf->b_next;
1192 for (;;)
1193 {
1194 if (buf == NULL)
1195 {
1196 if (!forward) /* tried both directions */
1197 break;
1198 buf = curbuf->b_prev;
1199 forward = FALSE;
1200 continue;
1201 }
1202 /* in non-help buffer, try to skip help buffers, and vv */
1203 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1204 {
1205 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1206 break;
1207 if (bp == NULL) /* remember unloaded buf for later */
1208 bp = buf;
1209 }
1210 if (forward)
1211 buf = buf->b_next;
1212 else
1213 buf = buf->b_prev;
1214 }
1215 }
1216 if (buf == NULL) /* No loaded buffer, use unloaded one */
1217 buf = bp;
1218 if (buf == NULL) /* No loaded buffer, find listed one */
1219 {
1220 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1221 if (buf->b_p_bl && buf != curbuf)
1222 break;
1223 }
1224 if (buf == NULL) /* Still no buffer, just take one */
1225 {
1226 if (curbuf->b_next != NULL)
1227 buf = curbuf->b_next;
1228 else
1229 buf = curbuf->b_prev;
1230 }
1231 }
1232
1233 /*
1234 * make buf current buffer
1235 */
1236 if (action == DOBUF_SPLIT) /* split window first */
1237 {
1238# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001239 /* If 'switchbuf' contains "useopen": jump to first window containing
1240 * "buf" if one exists */
1241 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001242 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001243 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001244 * page containing "buf" if one exists */
1245 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 return OK;
1247 if (win_split(0, 0) == FAIL)
1248# endif
1249 return FAIL;
1250 }
1251#endif
1252
1253 /* go to current buffer - nothing to do */
1254 if (buf == curbuf)
1255 return OK;
1256
1257 /*
1258 * Check if the current buffer may be abandoned.
1259 */
1260 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1261 {
1262#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1263 if ((p_confirm || cmdmod.confirm) && p_write)
1264 {
1265 dialog_changed(curbuf, FALSE);
1266# ifdef FEAT_AUTOCMD
1267 if (!buf_valid(buf))
1268 /* Autocommand deleted buffer, oops! */
1269 return FAIL;
1270# endif
1271 }
1272 if (bufIsChanged(curbuf))
1273#endif
1274 {
1275 EMSG(_(e_nowrtmsg));
1276 return FAIL;
1277 }
1278 }
1279
1280 /* Go to the other buffer. */
1281 set_curbuf(buf, action);
1282
1283#if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1284 if (action == DOBUF_SPLIT)
1285 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1286#endif
1287
1288#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1289 if (aborting()) /* autocmds may abort script processing */
1290 return FAIL;
1291#endif
1292
1293 return OK;
1294}
1295
1296#endif /* FEAT_LISTCMDS */
1297
1298/*
1299 * Set current buffer to "buf". Executes autocommands and closes current
1300 * buffer. "action" tells how to close the current buffer:
1301 * DOBUF_GOTO free or hide it
1302 * DOBUF_SPLIT nothing
1303 * DOBUF_UNLOAD unload it
1304 * DOBUF_DEL delete it
1305 * DOBUF_WIPE wipe it out
1306 */
1307 void
1308set_curbuf(buf, action)
1309 buf_T *buf;
1310 int action;
1311{
1312 buf_T *prevbuf;
1313 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1314 || action == DOBUF_WIPE);
1315
1316 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001317 if (!cmdmod.keepalt)
1318 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001319 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320
1321#ifdef FEAT_VISUAL
1322 /* Don't restart Select mode after switching to another buffer. */
1323 VIsual_reselect = FALSE;
1324#endif
1325
1326 /* close_windows() or apply_autocmds() may change curbuf */
1327 prevbuf = curbuf;
1328
1329#ifdef FEAT_AUTOCMD
1330 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1331# ifdef FEAT_EVAL
1332 if (buf_valid(prevbuf) && !aborting())
1333# else
1334 if (buf_valid(prevbuf))
1335# endif
1336#endif
1337 {
1338#ifdef FEAT_WINDOWS
1339 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001340 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341#endif
1342#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1343 if (buf_valid(prevbuf) && !aborting())
1344#else
1345 if (buf_valid(prevbuf))
1346#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001347 {
1348 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001349 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1351 unload ? action : (action == DOBUF_GOTO
1352 && !P_HID(prevbuf)
1353 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001357 /* An autocommand may have deleted "buf", already entered it (e.g., when
1358 * it did ":bunload") or aborted the script processing! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359# ifdef FEAT_EVAL
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001360 if (buf_valid(buf) && buf != curbuf && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361# else
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001362 if (buf_valid(buf) && buf != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363# endif
1364#endif
1365 enter_buffer(buf);
1366}
1367
1368/*
1369 * Enter a new current buffer.
1370 * Old curbuf must have been abandoned already!
1371 */
1372 void
1373enter_buffer(buf)
1374 buf_T *buf;
1375{
1376 /* Copy buffer and window local option values. Not for a help buffer. */
1377 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1378 if (!buf->b_help)
1379 get_winopts(buf);
1380#ifdef FEAT_FOLDING
1381 else
1382 /* Remove all folds in the window. */
1383 clearFolding(curwin);
1384 foldUpdateAll(curwin); /* update folds (later). */
1385#endif
1386
Bram Moolenaar860cae12010-06-05 23:22:07 +02001387#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001388 reset_synblock(curwin);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001389 curwin->w_s = &(buf->b_s);
1390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 /* Get the buffer in the current window. */
1392 curwin->w_buffer = buf;
1393 curbuf = buf;
1394 ++curbuf->b_nwindows;
1395
1396#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001397 if (curwin->w_p_diff)
1398 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399#endif
1400
1401 /* Cursor on first line by default. */
1402 curwin->w_cursor.lnum = 1;
1403 curwin->w_cursor.col = 0;
1404#ifdef FEAT_VIRTUALEDIT
1405 curwin->w_cursor.coladd = 0;
1406#endif
1407 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001408#ifdef FEAT_AUTOCMD
1409 curwin->w_topline_was_set = FALSE;
1410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001412 /* mark cursor position as being invalid */
1413 curwin->w_valid = 0;
1414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 /* Make sure the buffer is loaded. */
1416 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001417 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001418#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001419 /* If there is no filetype, allow for detecting one. Esp. useful for
1420 * ":ball" used in a autocommand. If there already is a filetype we
1421 * might prefer to keep it. */
1422 if (*curbuf->b_p_ft == NUL)
1423 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001424#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001425
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 open_buffer(FALSE, NULL);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 else
1429 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001430 if (!msg_silent)
1431 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1433#ifdef FEAT_AUTOCMD
1434 curwin->w_topline = 1;
1435# ifdef FEAT_DIFF
1436 curwin->w_topfill = 0;
1437# endif
1438 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1439 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1440#endif
1441 }
1442
1443 /* If autocommands did not change the cursor position, restore cursor lnum
1444 * and possibly cursor col. */
1445 if (curwin->w_cursor.lnum == 1 && inindent(0))
1446 buflist_getfpos();
1447
1448 check_arg_idx(curwin); /* check for valid arg_idx */
1449#ifdef FEAT_TITLE
1450 maketitle();
1451#endif
1452#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001453 /* when autocmds didn't change it */
1454 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455#endif
1456 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1457
Bram Moolenaar009b2592004-10-24 19:18:58 +00001458#ifdef FEAT_NETBEANS_INTG
1459 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001460 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001461#endif
1462
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001463 /* Change directories when the 'acd' option is set. */
1464 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
1466#ifdef FEAT_KEYMAP
1467 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001468 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001470#ifdef FEAT_SPELL
1471 /* May need to set the spell language. Can only do this after the buffer
1472 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001473 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1474 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001475#endif
1476
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 redraw_later(NOT_VALID);
1478}
1479
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001480#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1481/*
1482 * Change to the directory of the current buffer.
1483 */
1484 void
1485do_autochdir()
1486{
1487 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1488 shorten_fnames(TRUE);
1489}
1490#endif
1491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492/*
1493 * functions for dealing with the buffer list
1494 */
1495
1496/*
1497 * Add a file name to the buffer list. Return a pointer to the buffer.
1498 * If the same file name already exists return a pointer to that buffer.
1499 * If it does not exist, or if fname == NULL, a new entry is created.
1500 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1501 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1502 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 * This is the ONLY way to create a new buffer.
1504 */
1505static int top_file_num = 1; /* highest file number */
1506
1507 buf_T *
1508buflist_new(ffname, sfname, lnum, flags)
1509 char_u *ffname; /* full path of fname or relative */
1510 char_u *sfname; /* short fname or NULL */
1511 linenr_T lnum; /* preferred cursor line */
1512 int flags; /* BLN_ defines */
1513{
1514 buf_T *buf;
1515#ifdef UNIX
1516 struct stat st;
1517#endif
1518
1519 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1520
1521 /*
1522 * If file name already exists in the list, update the entry.
1523 */
1524#ifdef UNIX
1525 /* On Unix we can use inode numbers when the file exists. Works better
1526 * for hard links. */
1527 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1528 st.st_dev = (dev_T)-1;
1529#endif
1530 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1531#ifdef UNIX
1532 buflist_findname_stat(ffname, &st)
1533#else
1534 buflist_findname(ffname)
1535#endif
1536 ) != NULL)
1537 {
1538 vim_free(ffname);
1539 if (lnum != 0)
1540 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1541 /* copy the options now, if 'cpo' doesn't have 's' and not done
1542 * already */
1543 buf_copy_options(buf, 0);
1544 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1545 {
1546 buf->b_p_bl = TRUE;
1547#ifdef FEAT_AUTOCMD
1548 if (!(flags & BLN_DUMMY))
1549 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1550#endif
1551 }
1552 return buf;
1553 }
1554
1555 /*
1556 * If the current buffer has no name and no contents, use the current
1557 * buffer. Otherwise: Need to allocate a new buffer structure.
1558 *
1559 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001560 * (A spell file buffer is allocated in spell.c, but that's not a normal
1561 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 */
1563 buf = NULL;
1564 if ((flags & BLN_CURBUF)
1565 && curbuf != NULL
1566 && curbuf->b_ffname == NULL
1567 && curbuf->b_nwindows <= 1
1568 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1569 {
1570 buf = curbuf;
1571#ifdef FEAT_AUTOCMD
1572 /* It's like this buffer is deleted. Watch out for autocommands that
1573 * change curbuf! If that happens, allocate a new buffer anyway. */
1574 if (curbuf->b_p_bl)
1575 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1576 if (buf == curbuf)
1577 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1578# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001579 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 return NULL;
1581# endif
1582#endif
1583#ifdef FEAT_QUICKFIX
1584# ifdef FEAT_AUTOCMD
1585 if (buf == curbuf)
1586# endif
1587 {
1588 /* Make sure 'bufhidden' and 'buftype' are empty */
1589 clear_string_option(&buf->b_p_bh);
1590 clear_string_option(&buf->b_p_bt);
1591 }
1592#endif
1593 }
1594 if (buf != curbuf || curbuf == NULL)
1595 {
1596 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1597 if (buf == NULL)
1598 {
1599 vim_free(ffname);
1600 return NULL;
1601 }
1602 }
1603
1604 if (ffname != NULL)
1605 {
1606 buf->b_ffname = ffname;
1607 buf->b_sfname = vim_strsave(sfname);
1608 }
1609
1610 clear_wininfo(buf);
1611 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1612
1613 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1614 || buf->b_wininfo == NULL)
1615 {
1616 vim_free(buf->b_ffname);
1617 buf->b_ffname = NULL;
1618 vim_free(buf->b_sfname);
1619 buf->b_sfname = NULL;
1620 if (buf != curbuf)
1621 free_buffer(buf);
1622 return NULL;
1623 }
1624
1625 if (buf == curbuf)
1626 {
1627 /* free all things allocated for this buffer */
1628 buf_freeall(buf, FALSE, FALSE);
1629 if (buf != curbuf) /* autocommands deleted the buffer! */
1630 return NULL;
1631#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001632 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 return NULL;
1634#endif
1635 /* buf->b_nwindows = 0; why was this here? */
1636 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1637#ifdef FEAT_KEYMAP
1638 /* need to reload lmaps and set b:keymap_name */
1639 curbuf->b_kmap_state |= KEYMAP_INIT;
1640#endif
1641 }
1642 else
1643 {
1644 /*
1645 * put new buffer at the end of the buffer list
1646 */
1647 buf->b_next = NULL;
1648 if (firstbuf == NULL) /* buffer list is empty */
1649 {
1650 buf->b_prev = NULL;
1651 firstbuf = buf;
1652 }
1653 else /* append new buffer at end of list */
1654 {
1655 lastbuf->b_next = buf;
1656 buf->b_prev = lastbuf;
1657 }
1658 lastbuf = buf;
1659
1660 buf->b_fnum = top_file_num++;
1661 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1662 {
1663 EMSG(_("W14: Warning: List of file names overflow"));
1664 if (emsg_silent == 0)
1665 {
1666 out_flush();
1667 ui_delay(3000L, TRUE); /* make sure it is noticed */
1668 }
1669 top_file_num = 1;
1670 }
1671
1672 /*
1673 * Always copy the options from the current buffer.
1674 */
1675 buf_copy_options(buf, BCO_ALWAYS);
1676 }
1677
1678 buf->b_wininfo->wi_fpos.lnum = lnum;
1679 buf->b_wininfo->wi_win = curwin;
1680
1681#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001682 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1683#endif
1684#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001685 hash_init(&buf->b_s.b_keywtab);
1686 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687#endif
1688
1689 buf->b_fname = buf->b_sfname;
1690#ifdef UNIX
1691 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001692 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 else
1694 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001695 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 buf->b_dev = st.st_dev;
1697 buf->b_ino = st.st_ino;
1698 }
1699#endif
1700 buf->b_u_synced = TRUE;
1701 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001702 if (flags & BLN_DUMMY)
1703 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 buf_clear_file(buf);
1705 clrallmarks(buf); /* clear marks */
1706 fmarks_check_names(buf); /* check file marks for this file */
1707 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1708#ifdef FEAT_AUTOCMD
1709 if (!(flags & BLN_DUMMY))
1710 {
1711 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1712 if (flags & BLN_LISTED)
1713 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1714# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001715 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 return NULL;
1717# endif
1718 }
1719#endif
1720
1721 return buf;
1722}
1723
1724/*
1725 * Free the memory for the options of a buffer.
1726 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1727 * 'fileencoding'.
1728 */
1729 void
1730free_buf_options(buf, free_p_ff)
1731 buf_T *buf;
1732 int free_p_ff;
1733{
1734 if (free_p_ff)
1735 {
1736#ifdef FEAT_MBYTE
1737 clear_string_option(&buf->b_p_fenc);
1738#endif
1739 clear_string_option(&buf->b_p_ff);
1740#ifdef FEAT_QUICKFIX
1741 clear_string_option(&buf->b_p_bh);
1742 clear_string_option(&buf->b_p_bt);
1743#endif
1744 }
1745#ifdef FEAT_FIND_ID
1746 clear_string_option(&buf->b_p_def);
1747 clear_string_option(&buf->b_p_inc);
1748# ifdef FEAT_EVAL
1749 clear_string_option(&buf->b_p_inex);
1750# endif
1751#endif
1752#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1753 clear_string_option(&buf->b_p_inde);
1754 clear_string_option(&buf->b_p_indk);
1755#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001756#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1757 clear_string_option(&buf->b_p_bexpr);
1758#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001759#if defined(FEAT_CRYPT)
1760 clear_string_option(&buf->b_p_cm);
1761#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001762#if defined(FEAT_EVAL)
1763 clear_string_option(&buf->b_p_fex);
1764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#ifdef FEAT_CRYPT
1766 clear_string_option(&buf->b_p_key);
1767#endif
1768 clear_string_option(&buf->b_p_kp);
1769 clear_string_option(&buf->b_p_mps);
1770 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001771 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 clear_string_option(&buf->b_p_isk);
1773#ifdef FEAT_KEYMAP
1774 clear_string_option(&buf->b_p_keymap);
1775 ga_clear(&buf->b_kmap_ga);
1776#endif
1777#ifdef FEAT_COMMENTS
1778 clear_string_option(&buf->b_p_com);
1779#endif
1780#ifdef FEAT_FOLDING
1781 clear_string_option(&buf->b_p_cms);
1782#endif
1783 clear_string_option(&buf->b_p_nf);
1784#ifdef FEAT_SYN_HL
1785 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001786#endif
1787#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001788 clear_string_option(&buf->b_s.b_p_spc);
1789 clear_string_option(&buf->b_s.b_p_spf);
1790 vim_free(buf->b_s.b_cap_prog);
1791 buf->b_s.b_cap_prog = NULL;
1792 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#endif
1794#ifdef FEAT_SEARCHPATH
1795 clear_string_option(&buf->b_p_sua);
1796#endif
1797#ifdef FEAT_AUTOCMD
1798 clear_string_option(&buf->b_p_ft);
1799#endif
1800#ifdef FEAT_OSFILETYPE
1801 clear_string_option(&buf->b_p_oft);
1802#endif
1803#ifdef FEAT_CINDENT
1804 clear_string_option(&buf->b_p_cink);
1805 clear_string_option(&buf->b_p_cino);
1806#endif
1807#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1808 clear_string_option(&buf->b_p_cinw);
1809#endif
1810#ifdef FEAT_INS_EXPAND
1811 clear_string_option(&buf->b_p_cpt);
1812#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001813#ifdef FEAT_COMPL_FUNC
1814 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001815 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817#ifdef FEAT_QUICKFIX
1818 clear_string_option(&buf->b_p_gp);
1819 clear_string_option(&buf->b_p_mp);
1820 clear_string_option(&buf->b_p_efm);
1821#endif
1822 clear_string_option(&buf->b_p_ep);
1823 clear_string_option(&buf->b_p_path);
1824 clear_string_option(&buf->b_p_tags);
1825#ifdef FEAT_INS_EXPAND
1826 clear_string_option(&buf->b_p_dict);
1827 clear_string_option(&buf->b_p_tsr);
1828#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001829#ifdef FEAT_TEXTOBJ
1830 clear_string_option(&buf->b_p_qe);
1831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 buf->b_p_ar = -1;
1833}
1834
1835/*
1836 * get alternate file n
1837 * set linenr to lnum or altfpos.lnum if lnum == 0
1838 * also set cursor column to altfpos.col if 'startofline' is not set.
1839 * if (options & GETF_SETMARK) call setpcmark()
1840 * if (options & GETF_ALT) we are jumping to an alternate file.
1841 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1842 *
1843 * return FAIL for failure, OK for success
1844 */
1845 int
1846buflist_getfile(n, lnum, options, forceit)
1847 int n;
1848 linenr_T lnum;
1849 int options;
1850 int forceit;
1851{
1852 buf_T *buf;
1853#ifdef FEAT_WINDOWS
1854 win_T *wp = NULL;
1855#endif
1856 pos_T *fpos;
1857 colnr_T col;
1858
1859 buf = buflist_findnr(n);
1860 if (buf == NULL)
1861 {
1862 if ((options & GETF_ALT) && n == 0)
1863 EMSG(_(e_noalt));
1864 else
1865 EMSGN(_("E92: Buffer %ld not found"), n);
1866 return FAIL;
1867 }
1868
1869 /* if alternate file is the current buffer, nothing to do */
1870 if (buf == curbuf)
1871 return OK;
1872
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001873 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001874 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001875 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001877 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001878#ifdef FEAT_AUTOCMD
1879 if (curbuf_locked())
1880 return FAIL;
1881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
1883 /* altfpos may be changed by getfile(), get it now */
1884 if (lnum == 0)
1885 {
1886 fpos = buflist_findfpos(buf);
1887 lnum = fpos->lnum;
1888 col = fpos->col;
1889 }
1890 else
1891 col = 0;
1892
1893#ifdef FEAT_WINDOWS
1894 if (options & GETF_SWITCH)
1895 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001896 /* If 'switchbuf' contains "useopen": jump to first window containing
1897 * "buf" if one exists */
1898 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001900 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1901 * page containing "buf" if one exists */
1902 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001903 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001904 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1905 * isn't empty: open new window */
1906 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001908 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1909 tabpage_new();
1910 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 return FAIL;
1912# ifdef FEAT_SCROLLBIND
1913 curwin->w_p_scb = FALSE;
1914# endif
1915 }
1916 }
1917#endif
1918
1919 ++RedrawingDisabled;
1920 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1921 lnum, forceit) <= 0)
1922 {
1923 --RedrawingDisabled;
1924
1925 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1926 if (!p_sol && col != 0)
1927 {
1928 curwin->w_cursor.col = col;
1929 check_cursor_col();
1930#ifdef FEAT_VIRTUALEDIT
1931 curwin->w_cursor.coladd = 0;
1932#endif
1933 curwin->w_set_curswant = TRUE;
1934 }
1935 return OK;
1936 }
1937 --RedrawingDisabled;
1938 return FAIL;
1939}
1940
1941/*
1942 * go to the last know line number for the current buffer
1943 */
1944 void
1945buflist_getfpos()
1946{
1947 pos_T *fpos;
1948
1949 fpos = buflist_findfpos(curbuf);
1950
1951 curwin->w_cursor.lnum = fpos->lnum;
1952 check_cursor_lnum();
1953
1954 if (p_sol)
1955 curwin->w_cursor.col = 0;
1956 else
1957 {
1958 curwin->w_cursor.col = fpos->col;
1959 check_cursor_col();
1960#ifdef FEAT_VIRTUALEDIT
1961 curwin->w_cursor.coladd = 0;
1962#endif
1963 curwin->w_set_curswant = TRUE;
1964 }
1965}
1966
Bram Moolenaar81695252004-12-29 20:58:21 +00001967#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1968/*
1969 * Find file in buffer list by name (it has to be for the current window).
1970 * Returns NULL if not found.
1971 */
1972 buf_T *
1973buflist_findname_exp(fname)
1974 char_u *fname;
1975{
1976 char_u *ffname;
1977 buf_T *buf = NULL;
1978
1979 /* First make the name into a full path name */
1980 ffname = FullName_save(fname,
1981#ifdef UNIX
1982 TRUE /* force expansion, get rid of symbolic links */
1983#else
1984 FALSE
1985#endif
1986 );
1987 if (ffname != NULL)
1988 {
1989 buf = buflist_findname(ffname);
1990 vim_free(ffname);
1991 }
1992 return buf;
1993}
1994#endif
1995
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996/*
1997 * Find file in buffer list by name (it has to be for the current window).
1998 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00001999 * Skips dummy buffers.
2000 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 */
2002 buf_T *
2003buflist_findname(ffname)
2004 char_u *ffname;
2005{
2006#ifdef UNIX
2007 struct stat st;
2008
2009 if (mch_stat((char *)ffname, &st) < 0)
2010 st.st_dev = (dev_T)-1;
2011 return buflist_findname_stat(ffname, &st);
2012}
2013
2014/*
2015 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2016 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002017 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 */
2019 static buf_T *
2020buflist_findname_stat(ffname, stp)
2021 char_u *ffname;
2022 struct stat *stp;
2023{
2024#endif
2025 buf_T *buf;
2026
2027 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002028 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029#ifdef UNIX
2030 , stp
2031#endif
2032 ))
2033 return buf;
2034 return NULL;
2035}
2036
2037#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2038/*
2039 * Find file in buffer list by a regexp pattern.
2040 * Return fnum of the found buffer.
2041 * Return < 0 for error.
2042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 int
2044buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2045 char_u *pattern;
2046 char_u *pattern_end; /* pointer to first char after pattern */
2047 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002048 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049{
2050 buf_T *buf;
2051 regprog_T *prog;
2052 int match = -1;
2053 int find_listed;
2054 char_u *pat;
2055 char_u *patend;
2056 int attempt;
2057 char_u *p;
2058 int toggledollar;
2059
2060 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2061 {
2062 if (*pattern == '%')
2063 match = curbuf->b_fnum;
2064 else
2065 match = curwin->w_alt_fnum;
2066#ifdef FEAT_DIFF
2067 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2068 match = -1;
2069#endif
2070 }
2071
2072 /*
2073 * Try four ways of matching a listed buffer:
2074 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002075 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 * attempt == 2: with '$' at end (only match at end)
2077 * attempt == 3: with '^' at start and '$' at end (only full match)
2078 * Repeat this for finding an unlisted buffer if there was no matching
2079 * listed buffer.
2080 */
2081 else
2082 {
2083 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2084 if (pat == NULL)
2085 return -1;
2086 patend = pat + STRLEN(pat) - 1;
2087 toggledollar = (patend > pat && *patend == '$');
2088
2089 /* First try finding a listed buffer. If not found and "unlisted"
2090 * is TRUE, try finding an unlisted buffer. */
2091 find_listed = TRUE;
2092 for (;;)
2093 {
2094 for (attempt = 0; attempt <= 3; ++attempt)
2095 {
2096 /* may add '^' and '$' */
2097 if (toggledollar)
2098 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2099 p = pat;
2100 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2101 ++p;
2102 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2103 if (prog == NULL)
2104 {
2105 vim_free(pat);
2106 return -1;
2107 }
2108
2109 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2110 if (buf->b_p_bl == find_listed
2111#ifdef FEAT_DIFF
2112 && (!diffmode || diff_mode_buf(buf))
2113#endif
2114 && buflist_match(prog, buf) != NULL)
2115 {
2116 if (match >= 0) /* already found a match */
2117 {
2118 match = -2;
2119 break;
2120 }
2121 match = buf->b_fnum; /* remember first match */
2122 }
2123
2124 vim_free(prog);
2125 if (match >= 0) /* found one match */
2126 break;
2127 }
2128
2129 /* Only search for unlisted buffers if there was no match with
2130 * a listed buffer. */
2131 if (!unlisted || !find_listed || match != -1)
2132 break;
2133 find_listed = FALSE;
2134 }
2135
2136 vim_free(pat);
2137 }
2138
2139 if (match == -2)
2140 EMSG2(_("E93: More than one match for %s"), pattern);
2141 else if (match < 0)
2142 EMSG2(_("E94: No matching buffer for %s"), pattern);
2143 return match;
2144}
2145#endif
2146
2147#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2148
2149/*
2150 * Find all buffer names that match.
2151 * For command line expansion of ":buf" and ":sbuf".
2152 * Return OK if matches found, FAIL otherwise.
2153 */
2154 int
2155ExpandBufnames(pat, num_file, file, options)
2156 char_u *pat;
2157 int *num_file;
2158 char_u ***file;
2159 int options;
2160{
2161 int count = 0;
2162 buf_T *buf;
2163 int round;
2164 char_u *p;
2165 int attempt;
2166 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002167 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
2169 *num_file = 0; /* return values in case of FAIL */
2170 *file = NULL;
2171
Bram Moolenaar05159a02005-02-26 23:04:13 +00002172 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2173 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002175 patc = alloc((unsigned)STRLEN(pat) + 11);
2176 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002178 STRCPY(patc, "\\(^\\|[\\/]\\)");
2179 STRCPY(patc + 11, pat + 1);
2180 }
2181 else
2182 patc = pat;
2183
2184 /*
2185 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002186 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002187 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002188 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002189 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002190 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002191 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002192 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002193 if (prog == NULL)
2194 {
2195 if (patc != pat)
2196 vim_free(patc);
2197 return FAIL;
2198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199
2200 /*
2201 * round == 1: Count the matches.
2202 * round == 2: Build the array to keep the matches.
2203 */
2204 for (round = 1; round <= 2; ++round)
2205 {
2206 count = 0;
2207 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2208 {
2209 if (!buf->b_p_bl) /* skip unlisted buffers */
2210 continue;
2211 p = buflist_match(prog, buf);
2212 if (p != NULL)
2213 {
2214 if (round == 1)
2215 ++count;
2216 else
2217 {
2218 if (options & WILD_HOME_REPLACE)
2219 p = home_replace_save(buf, p);
2220 else
2221 p = vim_strsave(p);
2222 (*file)[count++] = p;
2223 }
2224 }
2225 }
2226 if (count == 0) /* no match found, break here */
2227 break;
2228 if (round == 1)
2229 {
2230 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2231 if (*file == NULL)
2232 {
2233 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002234 if (patc != pat)
2235 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 return FAIL;
2237 }
2238 }
2239 }
2240 vim_free(prog);
2241 if (count) /* match(es) found, break here */
2242 break;
2243 }
2244
Bram Moolenaar05159a02005-02-26 23:04:13 +00002245 if (patc != pat)
2246 vim_free(patc);
2247
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 *num_file = count;
2249 return (count == 0 ? FAIL : OK);
2250}
2251
2252#endif /* FEAT_CMDL_COMPL */
2253
2254#ifdef HAVE_BUFLIST_MATCH
2255/*
2256 * Check for a match on the file name for buffer "buf" with regprog "prog".
2257 */
2258 static char_u *
2259buflist_match(prog, buf)
2260 regprog_T *prog;
2261 buf_T *buf;
2262{
2263 char_u *match;
2264
2265 /* First try the short file name, then the long file name. */
2266 match = fname_match(prog, buf->b_sfname);
2267 if (match == NULL)
2268 match = fname_match(prog, buf->b_ffname);
2269
2270 return match;
2271}
2272
2273/*
2274 * Try matching the regexp in "prog" with file name "name".
2275 * Return "name" when there is a match, NULL when not.
2276 */
2277 static char_u *
2278fname_match(prog, name)
2279 regprog_T *prog;
2280 char_u *name;
2281{
2282 char_u *match = NULL;
2283 char_u *p;
2284 regmatch_T regmatch;
2285
2286 if (name != NULL)
2287 {
2288 regmatch.regprog = prog;
2289#ifdef CASE_INSENSITIVE_FILENAME
2290 regmatch.rm_ic = TRUE; /* Always ignore case */
2291#else
2292 regmatch.rm_ic = FALSE; /* Never ignore case */
2293#endif
2294
2295 if (vim_regexec(&regmatch, name, (colnr_T)0))
2296 match = name;
2297 else
2298 {
2299 /* Replace $(HOME) with '~' and try matching again. */
2300 p = home_replace_save(NULL, name);
2301 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2302 match = name;
2303 vim_free(p);
2304 }
2305 }
2306
2307 return match;
2308}
2309#endif
2310
2311/*
2312 * find file in buffer list by number
2313 */
2314 buf_T *
2315buflist_findnr(nr)
2316 int nr;
2317{
2318 buf_T *buf;
2319
2320 if (nr == 0)
2321 nr = curwin->w_alt_fnum;
2322 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2323 if (buf->b_fnum == nr)
2324 return (buf);
2325 return NULL;
2326}
2327
2328/*
2329 * Get name of file 'n' in the buffer list.
2330 * When the file has no name an empty string is returned.
2331 * home_replace() is used to shorten the file name (used for marks).
2332 * Returns a pointer to allocated memory, of NULL when failed.
2333 */
2334 char_u *
2335buflist_nr2name(n, fullname, helptail)
2336 int n;
2337 int fullname;
2338 int helptail; /* for help buffers return tail only */
2339{
2340 buf_T *buf;
2341
2342 buf = buflist_findnr(n);
2343 if (buf == NULL)
2344 return NULL;
2345 return home_replace_save(helptail ? buf : NULL,
2346 fullname ? buf->b_ffname : buf->b_fname);
2347}
2348
2349/*
2350 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2351 * When "copy_options" is TRUE save the local window option values.
2352 * When "lnum" is 0 only do the options.
2353 */
2354 static void
2355buflist_setfpos(buf, win, lnum, col, copy_options)
2356 buf_T *buf;
2357 win_T *win;
2358 linenr_T lnum;
2359 colnr_T col;
2360 int copy_options;
2361{
2362 wininfo_T *wip;
2363
2364 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2365 if (wip->wi_win == win)
2366 break;
2367 if (wip == NULL)
2368 {
2369 /* allocate a new entry */
2370 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2371 if (wip == NULL)
2372 return;
2373 wip->wi_win = win;
2374 if (lnum == 0) /* set lnum even when it's 0 */
2375 lnum = 1;
2376 }
2377 else
2378 {
2379 /* remove the entry from the list */
2380 if (wip->wi_prev)
2381 wip->wi_prev->wi_next = wip->wi_next;
2382 else
2383 buf->b_wininfo = wip->wi_next;
2384 if (wip->wi_next)
2385 wip->wi_next->wi_prev = wip->wi_prev;
2386 if (copy_options && wip->wi_optset)
2387 {
2388 clear_winopt(&wip->wi_opt);
2389#ifdef FEAT_FOLDING
2390 deleteFoldRecurse(&wip->wi_folds);
2391#endif
2392 }
2393 }
2394 if (lnum != 0)
2395 {
2396 wip->wi_fpos.lnum = lnum;
2397 wip->wi_fpos.col = col;
2398 }
2399 if (copy_options)
2400 {
2401 /* Save the window-specific option values. */
2402 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2403#ifdef FEAT_FOLDING
2404 wip->wi_fold_manual = win->w_fold_manual;
2405 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2406#endif
2407 wip->wi_optset = TRUE;
2408 }
2409
2410 /* insert the entry in front of the list */
2411 wip->wi_next = buf->b_wininfo;
2412 buf->b_wininfo = wip;
2413 wip->wi_prev = NULL;
2414 if (wip->wi_next)
2415 wip->wi_next->wi_prev = wip;
2416
2417 return;
2418}
2419
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002420#ifdef FEAT_DIFF
2421static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2422
2423/*
2424 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2425 * page. That's because a diff is local to a tab page.
2426 */
2427 static int
2428wininfo_other_tab_diff(wip)
2429 wininfo_T *wip;
2430{
2431 win_T *wp;
2432
2433 if (wip->wi_opt.wo_diff)
2434 {
2435 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2436 /* return FALSE when it's a window in the current tab page, thus
2437 * the buffer was in diff mode here */
2438 if (wip->wi_win == wp)
2439 return FALSE;
2440 return TRUE;
2441 }
2442 return FALSE;
2443}
2444#endif
2445
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446/*
2447 * Find info for the current window in buffer "buf".
2448 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002449 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2450 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 * Returns NULL when there isn't any info.
2452 */
2453 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002454find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002456 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457{
2458 wininfo_T *wip;
2459
2460 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002461 if (wip->wi_win == curwin
2462#ifdef FEAT_DIFF
2463 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2464#endif
2465 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002467
2468 /* If no wininfo for curwin, use the first in the list (that doesn't have
2469 * 'diff' set and is in another tab page). */
2470 if (wip == NULL)
2471 {
2472#ifdef FEAT_DIFF
2473 if (skip_diff_buffer)
2474 {
2475 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2476 if (!wininfo_other_tab_diff(wip))
2477 break;
2478 }
2479 else
2480#endif
2481 wip = buf->b_wininfo;
2482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 return wip;
2484}
2485
2486/*
2487 * Reset the local window options to the values last used in this window.
2488 * If the buffer wasn't used in this window before, use the values from
2489 * the most recently used window. If the values were never set, use the
2490 * global values for the window.
2491 */
2492 void
2493get_winopts(buf)
2494 buf_T *buf;
2495{
2496 wininfo_T *wip;
2497
2498 clear_winopt(&curwin->w_onebuf_opt);
2499#ifdef FEAT_FOLDING
2500 clearFolding(curwin);
2501#endif
2502
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002503 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 if (wip != NULL && wip->wi_optset)
2505 {
2506 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2507#ifdef FEAT_FOLDING
2508 curwin->w_fold_manual = wip->wi_fold_manual;
2509 curwin->w_foldinvalid = TRUE;
2510 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2511#endif
2512 }
2513 else
2514 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2515
2516#ifdef FEAT_FOLDING
2517 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2518 if (p_fdls >= 0)
2519 curwin->w_p_fdl = p_fdls;
2520#endif
2521}
2522
2523/*
2524 * Find the position (lnum and col) for the buffer 'buf' for the current
2525 * window.
2526 * Returns a pointer to no_position if no position is found.
2527 */
2528 pos_T *
2529buflist_findfpos(buf)
2530 buf_T *buf;
2531{
2532 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002533 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002535 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 if (wip != NULL)
2537 return &(wip->wi_fpos);
2538 else
2539 return &no_position;
2540}
2541
2542/*
2543 * Find the lnum for the buffer 'buf' for the current window.
2544 */
2545 linenr_T
2546buflist_findlnum(buf)
2547 buf_T *buf;
2548{
2549 return buflist_findfpos(buf)->lnum;
2550}
2551
2552#if defined(FEAT_LISTCMDS) || defined(PROTO)
2553/*
2554 * List all know file names (for :files and :buffers command).
2555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 void
2557buflist_list(eap)
2558 exarg_T *eap;
2559{
2560 buf_T *buf;
2561 int len;
2562 int i;
2563
2564 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2565 {
2566 /* skip unlisted buffers, unless ! was used */
2567 if (!buf->b_p_bl && !eap->forceit)
2568 continue;
2569 msg_putchar('\n');
2570 if (buf_spname(buf) != NULL)
2571 STRCPY(NameBuff, buf_spname(buf));
2572 else
2573 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2574
Bram Moolenaar50cde822005-06-05 21:54:54 +00002575 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 buf->b_fnum,
2577 buf->b_p_bl ? ' ' : 'u',
2578 buf == curbuf ? '%' :
2579 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2580 buf->b_ml.ml_mfp == NULL ? ' ' :
2581 (buf->b_nwindows == 0 ? 'h' : 'a'),
2582 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2583 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002584 : (bufIsChanged(buf) ? '+' : ' '),
2585 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
2587 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 i = 40 - vim_strsize(IObuff);
2589 do
2590 {
2591 IObuff[len++] = ' ';
2592 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002593 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2594 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002595 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 msg_outtrans(IObuff);
2597 out_flush(); /* output one line at a time */
2598 ui_breakcheck();
2599 }
2600}
2601#endif
2602
2603/*
2604 * Get file name and line number for file 'fnum'.
2605 * Used by DoOneCmd() for translating '%' and '#'.
2606 * Used by insert_reg() and cmdline_paste() for '#' register.
2607 * Return FAIL if not found, OK for success.
2608 */
2609 int
2610buflist_name_nr(fnum, fname, lnum)
2611 int fnum;
2612 char_u **fname;
2613 linenr_T *lnum;
2614{
2615 buf_T *buf;
2616
2617 buf = buflist_findnr(fnum);
2618 if (buf == NULL || buf->b_fname == NULL)
2619 return FAIL;
2620
2621 *fname = buf->b_fname;
2622 *lnum = buflist_findlnum(buf);
2623
2624 return OK;
2625}
2626
2627/*
2628 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2629 * The file name with the full path is also remembered, for when :cd is used.
2630 * Returns FAIL for failure (file name already in use by other buffer)
2631 * OK otherwise.
2632 */
2633 int
2634setfname(buf, ffname, sfname, message)
2635 buf_T *buf;
2636 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002637 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638{
Bram Moolenaar81695252004-12-29 20:58:21 +00002639 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640#ifdef UNIX
2641 struct stat st;
2642#endif
2643
2644 if (ffname == NULL || *ffname == NUL)
2645 {
2646 /* Removing the name. */
2647 vim_free(buf->b_ffname);
2648 vim_free(buf->b_sfname);
2649 buf->b_ffname = NULL;
2650 buf->b_sfname = NULL;
2651#ifdef UNIX
2652 st.st_dev = (dev_T)-1;
2653#endif
2654 }
2655 else
2656 {
2657 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2658 if (ffname == NULL) /* out of memory */
2659 return FAIL;
2660
2661 /*
2662 * if the file name is already used in another buffer:
2663 * - if the buffer is loaded, fail
2664 * - if the buffer is not loaded, delete it from the list
2665 */
2666#ifdef UNIX
2667 if (mch_stat((char *)ffname, &st) < 0)
2668 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002669#endif
2670 if (!(buf->b_flags & BF_DUMMY))
2671#ifdef UNIX
2672 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002674 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675#endif
2676 if (obuf != NULL && obuf != buf)
2677 {
2678 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2679 {
2680 if (message)
2681 EMSG(_("E95: Buffer with this name already exists"));
2682 vim_free(ffname);
2683 return FAIL;
2684 }
2685 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2686 }
2687 sfname = vim_strsave(sfname);
2688 if (ffname == NULL || sfname == NULL)
2689 {
2690 vim_free(sfname);
2691 vim_free(ffname);
2692 return FAIL;
2693 }
2694#ifdef USE_FNAME_CASE
2695# ifdef USE_LONG_FNAME
2696 if (USE_LONG_FNAME)
2697# endif
2698 fname_case(sfname, 0); /* set correct case for short file name */
2699#endif
2700 vim_free(buf->b_ffname);
2701 vim_free(buf->b_sfname);
2702 buf->b_ffname = ffname;
2703 buf->b_sfname = sfname;
2704 }
2705 buf->b_fname = buf->b_sfname;
2706#ifdef UNIX
2707 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002708 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 else
2710 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002711 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 buf->b_dev = st.st_dev;
2713 buf->b_ino = st.st_ino;
2714 }
2715#endif
2716
2717#ifndef SHORT_FNAME
2718 buf->b_shortname = FALSE;
2719#endif
2720
2721 buf_name_changed(buf);
2722 return OK;
2723}
2724
2725/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002726 * Crude way of changing the name of a buffer. Use with care!
2727 * The name should be relative to the current directory.
2728 */
2729 void
2730buf_set_name(fnum, name)
2731 int fnum;
2732 char_u *name;
2733{
2734 buf_T *buf;
2735
2736 buf = buflist_findnr(fnum);
2737 if (buf != NULL)
2738 {
2739 vim_free(buf->b_sfname);
2740 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002741 buf->b_ffname = vim_strsave(name);
2742 buf->b_sfname = NULL;
2743 /* Allocate ffname and expand into full path. Also resolves .lnk
2744 * files on Win32. */
2745 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002746 buf->b_fname = buf->b_sfname;
2747 }
2748}
2749
2750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 * Take care of what needs to be done when the name of buffer "buf" has
2752 * changed.
2753 */
2754 void
2755buf_name_changed(buf)
2756 buf_T *buf;
2757{
2758 /*
2759 * If the file name changed, also change the name of the swapfile
2760 */
2761 if (buf->b_ml.ml_mfp != NULL)
2762 ml_setname(buf);
2763
2764 if (curwin->w_buffer == buf)
2765 check_arg_idx(curwin); /* check file name for arg list */
2766#ifdef FEAT_TITLE
2767 maketitle(); /* set window title */
2768#endif
2769#ifdef FEAT_WINDOWS
2770 status_redraw_all(); /* status lines need to be redrawn */
2771#endif
2772 fmarks_check_names(buf); /* check named file marks */
2773 ml_timestamp(buf); /* reset timestamp */
2774}
2775
2776/*
2777 * set alternate file name for current window
2778 *
2779 * Used by do_one_cmd(), do_write() and do_ecmd().
2780 * Return the buffer.
2781 */
2782 buf_T *
2783setaltfname(ffname, sfname, lnum)
2784 char_u *ffname;
2785 char_u *sfname;
2786 linenr_T lnum;
2787{
2788 buf_T *buf;
2789
2790 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2791 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002792 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 curwin->w_alt_fnum = buf->b_fnum;
2794 return buf;
2795}
2796
2797/*
2798 * Get alternate file name for current window.
2799 * Return NULL if there isn't any, and give error message if requested.
2800 */
2801 char_u *
2802getaltfname(errmsg)
2803 int errmsg; /* give error message */
2804{
2805 char_u *fname;
2806 linenr_T dummy;
2807
2808 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2809 {
2810 if (errmsg)
2811 EMSG(_(e_noalt));
2812 return NULL;
2813 }
2814 return fname;
2815}
2816
2817/*
2818 * Add a file name to the buflist and return its number.
2819 * Uses same flags as buflist_new(), except BLN_DUMMY.
2820 *
2821 * used by qf_init(), main() and doarglist()
2822 */
2823 int
2824buflist_add(fname, flags)
2825 char_u *fname;
2826 int flags;
2827{
2828 buf_T *buf;
2829
2830 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2831 if (buf != NULL)
2832 return buf->b_fnum;
2833 return 0;
2834}
2835
2836#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2837/*
2838 * Adjust slashes in file names. Called after 'shellslash' was set.
2839 */
2840 void
2841buflist_slash_adjust()
2842{
2843 buf_T *bp;
2844
2845 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2846 {
2847 if (bp->b_ffname != NULL)
2848 slash_adjust(bp->b_ffname);
2849 if (bp->b_sfname != NULL)
2850 slash_adjust(bp->b_sfname);
2851 }
2852}
2853#endif
2854
2855/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002856 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 * Also save the local window option values.
2858 */
2859 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002860buflist_altfpos(win)
2861 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002863 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864}
2865
2866/*
2867 * Return TRUE if 'ffname' is not the same file as current file.
2868 * Fname must have a full path (expanded by mch_FullName()).
2869 */
2870 int
2871otherfile(ffname)
2872 char_u *ffname;
2873{
2874 return otherfile_buf(curbuf, ffname
2875#ifdef UNIX
2876 , NULL
2877#endif
2878 );
2879}
2880
2881 static int
2882otherfile_buf(buf, ffname
2883#ifdef UNIX
2884 , stp
2885#endif
2886 )
2887 buf_T *buf;
2888 char_u *ffname;
2889#ifdef UNIX
2890 struct stat *stp;
2891#endif
2892{
2893 /* no name is different */
2894 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2895 return TRUE;
2896 if (fnamecmp(ffname, buf->b_ffname) == 0)
2897 return FALSE;
2898#ifdef UNIX
2899 {
2900 struct stat st;
2901
2902 /* If no struct stat given, get it now */
2903 if (stp == NULL)
2904 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002905 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 st.st_dev = (dev_T)-1;
2907 stp = &st;
2908 }
2909 /* Use dev/ino to check if the files are the same, even when the names
2910 * are different (possible with links). Still need to compare the
2911 * name above, for when the file doesn't exist yet.
2912 * Problem: The dev/ino changes when a file is deleted (and created
2913 * again) and remains the same when renamed/moved. We don't want to
2914 * mch_stat() each buffer each time, that would be too slow. Get the
2915 * dev/ino again when they appear to match, but not when they appear
2916 * to be different: Could skip a buffer when it's actually the same
2917 * file. */
2918 if (buf_same_ino(buf, stp))
2919 {
2920 buf_setino(buf);
2921 if (buf_same_ino(buf, stp))
2922 return FALSE;
2923 }
2924 }
2925#endif
2926 return TRUE;
2927}
2928
2929#if defined(UNIX) || defined(PROTO)
2930/*
2931 * Set inode and device number for a buffer.
2932 * Must always be called when b_fname is changed!.
2933 */
2934 void
2935buf_setino(buf)
2936 buf_T *buf;
2937{
2938 struct stat st;
2939
2940 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2941 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002942 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 buf->b_dev = st.st_dev;
2944 buf->b_ino = st.st_ino;
2945 }
2946 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002947 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948}
2949
2950/*
2951 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2952 */
2953 static int
2954buf_same_ino(buf, stp)
2955 buf_T *buf;
2956 struct stat *stp;
2957{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002958 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 && stp->st_dev == buf->b_dev
2960 && stp->st_ino == buf->b_ino);
2961}
2962#endif
2963
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002964/*
2965 * Print info about the current buffer.
2966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 void
2968fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002969 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 int shorthelp;
2971 int dont_truncate;
2972{
2973 char_u *name;
2974 int n;
2975 char_u *p;
2976 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002977 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978
2979 buffer = alloc(IOSIZE);
2980 if (buffer == NULL)
2981 return;
2982
2983 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2984 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002985 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 p = buffer + STRLEN(buffer);
2987 }
2988 else
2989 p = buffer;
2990
2991 *p++ = '"';
2992 if (buf_spname(curbuf) != NULL)
2993 STRCPY(p, buf_spname(curbuf));
2994 else
2995 {
2996 if (!fullname && curbuf->b_fname != NULL)
2997 name = curbuf->b_fname;
2998 else
2999 name = curbuf->b_ffname;
3000 home_replace(shorthelp ? curbuf : NULL, name, p,
3001 (int)(IOSIZE - (p - buffer)), TRUE);
3002 }
3003
Bram Moolenaara800b422010-06-27 01:15:55 +02003004 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 curbufIsChanged() ? (shortmess(SHM_MOD)
3006 ? " [+]" : _(" [Modified]")) : " ",
3007 (curbuf->b_flags & BF_NOTEDITED)
3008#ifdef FEAT_QUICKFIX
3009 && !bt_dontwrite(curbuf)
3010#endif
3011 ? _("[Not edited]") : "",
3012 (curbuf->b_flags & BF_NEW)
3013#ifdef FEAT_QUICKFIX
3014 && !bt_dontwrite(curbuf)
3015#endif
3016 ? _("[New file]") : "",
3017 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3018 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3019 : _("[readonly]")) : "",
3020 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3021 || curbuf->b_p_ro) ?
3022 " " : "");
3023 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3024 * causes an overflow, thus for large numbers divide instead. */
3025 if (curwin->w_cursor.lnum > 1000000L)
3026 n = (int)(((long)curwin->w_cursor.lnum) /
3027 ((long)curbuf->b_ml.ml_line_count / 100L));
3028 else
3029 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3030 (long)curbuf->b_ml.ml_line_count);
3031 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3032 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003033 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 }
3035#ifdef FEAT_CMDL_INFO
3036 else if (p_ru)
3037 {
3038 /* Current line and column are already on the screen -- webb */
3039 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003040 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003042 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 (long)curbuf->b_ml.ml_line_count, n);
3044 }
3045#endif
3046 else
3047 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003048 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 _("line %ld of %ld --%d%%-- col "),
3050 (long)curwin->w_cursor.lnum,
3051 (long)curbuf->b_ml.ml_line_count,
3052 n);
3053 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003054 len = STRLEN(buffer);
3055 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3057 }
3058
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003059 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
3061 if (dont_truncate)
3062 {
3063 /* Temporarily set msg_scroll to avoid the message being truncated.
3064 * First call msg_start() to get the message in the right place. */
3065 msg_start();
3066 n = msg_scroll;
3067 msg_scroll = TRUE;
3068 msg(buffer);
3069 msg_scroll = n;
3070 }
3071 else
3072 {
3073 p = msg_trunc_attr(buffer, FALSE, 0);
3074 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 /* Need to repeat the message after redrawing when:
3076 * - When restart_edit is set (otherwise there will be a delay
3077 * before redrawing).
3078 * - When the screen was scrolled but there is no wait-return
3079 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003080 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 }
3082
3083 vim_free(buffer);
3084}
3085
3086 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003087col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003089 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 int col;
3091 int vcol;
3092{
3093 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003094 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003096 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097}
3098
3099#if defined(FEAT_TITLE) || defined(PROTO)
3100/*
3101 * put file name in title bar of window and in icon title
3102 */
3103
3104static char_u *lasttitle = NULL;
3105static char_u *lasticon = NULL;
3106
3107 void
3108maketitle()
3109{
3110 char_u *p;
3111 char_u *t_str = NULL;
3112 char_u *i_name;
3113 char_u *i_str = NULL;
3114 int maxlen = 0;
3115 int len;
3116 int mustset;
3117 char_u buf[IOSIZE];
3118 int off;
3119
3120 if (!redrawing())
3121 {
3122 /* Postpone updating the title when 'lazyredraw' is set. */
3123 need_maketitle = TRUE;
3124 return;
3125 }
3126
3127 need_maketitle = FALSE;
3128 if (!p_title && !p_icon)
3129 return;
3130
3131 if (p_title)
3132 {
3133 if (p_titlelen > 0)
3134 {
3135 maxlen = p_titlelen * Columns / 100;
3136 if (maxlen < 10)
3137 maxlen = 10;
3138 }
3139
3140 t_str = buf;
3141 if (*p_titlestring != NUL)
3142 {
3143#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003144 if (stl_syntax & STL_IN_TITLE)
3145 {
3146 int use_sandbox = FALSE;
3147 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003148
3149# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003150 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003151# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003152 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003154 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003155 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003156 if (called_emsg)
3157 set_string_option_direct((char_u *)"titlestring", -1,
3158 (char_u *)"", OPT_FREE, SID_ERROR);
3159 called_emsg |= save_called_emsg;
3160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 else
3162#endif
3163 t_str = p_titlestring;
3164 }
3165 else
3166 {
3167 /* format: "fname + (path) (1 of 2) - VIM" */
3168
3169 if (curbuf->b_fname == NULL)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003170 STRCPY(buf, _("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 else
3172 {
3173 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003174 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 }
3177
3178 switch (bufIsChanged(curbuf)
3179 + (curbuf->b_p_ro * 2)
3180 + (!curbuf->b_p_ma * 4))
3181 {
3182 case 1: STRCAT(buf, " +"); break;
3183 case 2: STRCAT(buf, " ="); break;
3184 case 3: STRCAT(buf, " =+"); break;
3185 case 4:
3186 case 6: STRCAT(buf, " -"); break;
3187 case 5:
3188 case 7: STRCAT(buf, " -+"); break;
3189 }
3190
3191 if (curbuf->b_fname != NULL)
3192 {
3193 /* Get path of file, replace home dir with ~ */
3194 off = (int)STRLEN(buf);
3195 buf[off++] = ' ';
3196 buf[off++] = '(';
3197 home_replace(curbuf, curbuf->b_ffname,
3198 buf + off, IOSIZE - off, TRUE);
3199#ifdef BACKSLASH_IN_FILENAME
3200 /* avoid "c:/name" to be reduced to "c" */
3201 if (isalpha(buf[off]) && buf[off + 1] == ':')
3202 off += 2;
3203#endif
3204 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003205 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003208 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003209 (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003212
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 /* translate unprintable chars */
3214 p = transstr(buf + off);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003215 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 STRCAT(buf, ")");
3218 }
3219
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003220 append_arg_number(curwin, buf, IOSIZE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
3222#if defined(FEAT_CLIENTSERVER)
3223 if (serverName != NULL)
3224 {
3225 STRCAT(buf, " - ");
3226 STRCAT(buf, serverName);
3227 }
3228 else
3229#endif
3230 STRCAT(buf, " - VIM");
3231
3232 if (maxlen > 0)
3233 {
3234 /* make it shorter by removing a bit in the middle */
3235 len = vim_strsize(buf);
3236 if (len > maxlen)
3237 trunc_string(buf, buf, maxlen);
3238 }
3239 }
3240 }
3241 mustset = ti_change(t_str, &lasttitle);
3242
3243 if (p_icon)
3244 {
3245 i_str = buf;
3246 if (*p_iconstring != NUL)
3247 {
3248#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003249 if (stl_syntax & STL_IN_ICON)
3250 {
3251 int use_sandbox = FALSE;
3252 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003253
3254# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003255 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003256# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003257 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003259 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003260 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003261 if (called_emsg)
3262 set_string_option_direct((char_u *)"iconstring", -1,
3263 (char_u *)"", OPT_FREE, SID_ERROR);
3264 called_emsg |= save_called_emsg;
3265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 else
3267#endif
3268 i_str = p_iconstring;
3269 }
3270 else
3271 {
3272 if (buf_spname(curbuf) != NULL)
3273 i_name = (char_u *)buf_spname(curbuf);
3274 else /* use file name only in icon */
3275 i_name = gettail(curbuf->b_ffname);
3276 *i_str = NUL;
3277 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003278 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 if (len > 100)
3280 {
3281 len -= 100;
3282#ifdef FEAT_MBYTE
3283 if (has_mbyte)
3284 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3285#endif
3286 i_name += len;
3287 }
3288 STRCPY(i_str, i_name);
3289 trans_characters(i_str, IOSIZE);
3290 }
3291 }
3292
3293 mustset |= ti_change(i_str, &lasticon);
3294
3295 if (mustset)
3296 resettitle();
3297}
3298
3299/*
3300 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3301 * from "str" if it does.
3302 * Return TRUE when "*last" changed.
3303 */
3304 static int
3305ti_change(str, last)
3306 char_u *str;
3307 char_u **last;
3308{
3309 if ((str == NULL) != (*last == NULL)
3310 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3311 {
3312 vim_free(*last);
3313 if (str == NULL)
3314 *last = NULL;
3315 else
3316 *last = vim_strsave(str);
3317 return TRUE;
3318 }
3319 return FALSE;
3320}
3321
3322/*
3323 * Put current window title back (used after calling a shell)
3324 */
3325 void
3326resettitle()
3327{
3328 mch_settitle(lasttitle, lasticon);
3329}
Bram Moolenaarea408852005-06-25 22:49:46 +00003330
3331# if defined(EXITFREE) || defined(PROTO)
3332 void
3333free_titles()
3334{
3335 vim_free(lasttitle);
3336 vim_free(lasticon);
3337}
3338# endif
3339
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340#endif /* FEAT_TITLE */
3341
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003342#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003344 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 * Return length of string in screen cells.
3346 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003347 * Normally works for window "wp", except when working for 'tabline' then it
3348 * is "curwin".
3349 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 * Items are drawn interspersed with the text that surrounds it
3351 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3352 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3353 *
3354 * If maxwidth is not zero, the string will be filled at any middle marker
3355 * or truncated if too long, fillchar is used for all whitespace.
3356 */
3357 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003358build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003360 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 size_t outlen; /* length of out[] */
3362 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003363 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 int fillchar;
3365 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003366 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3367 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
3369 char_u *p;
3370 char_u *s;
3371 char_u *t;
3372 char_u *linecont;
3373#ifdef FEAT_EVAL
3374 win_T *o_curwin;
3375 buf_T *o_curbuf;
3376#endif
3377 int empty_line;
3378 colnr_T virtcol;
3379 long l;
3380 long n;
3381 int prevchar_isflag;
3382 int prevchar_isitem;
3383 int itemisflag;
3384 int fillable;
3385 char_u *str;
3386 long num;
3387 int width;
3388 int itemcnt;
3389 int curitem;
3390 int groupitem[STL_MAX_ITEM];
3391 int groupdepth;
3392 struct stl_item
3393 {
3394 char_u *start;
3395 int minwid;
3396 int maxwid;
3397 enum
3398 {
3399 Normal,
3400 Empty,
3401 Group,
3402 Middle,
3403 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003404 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 Trunc
3406 } type;
3407 } item[STL_MAX_ITEM];
3408 int minwid;
3409 int maxwid;
3410 int zeropad;
3411 char_u base;
3412 char_u opt;
3413#define TMPLEN 70
3414 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003415 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003416 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003417
3418#ifdef FEAT_EVAL
3419 /*
3420 * When the format starts with "%!" then evaluate it as an expression and
3421 * use the result as the actual format string.
3422 */
3423 if (fmt[0] == '%' && fmt[1] == '!')
3424 {
3425 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3426 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003427 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003428 }
3429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431 if (fillchar == 0)
3432 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003433#ifdef FEAT_MBYTE
3434 /* Can't handle a multi-byte fill character yet. */
3435 else if (mb_char2len(fillchar) > 1)
3436 fillchar = '-';
3437#endif
3438
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 /*
3440 * Get line & check if empty (cursorpos will show "0-1").
3441 * If inversion is possible we use it. Else '=' characters are used.
3442 */
3443 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3444 empty_line = (*linecont == NUL);
3445
3446 groupdepth = 0;
3447 p = out;
3448 curitem = 0;
3449 prevchar_isflag = TRUE;
3450 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003451 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
3453 if (*s != NUL && *s != '%')
3454 prevchar_isflag = prevchar_isitem = FALSE;
3455
3456 /*
3457 * Handle up to the next '%' or the end.
3458 */
3459 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3460 *p++ = *s++;
3461 if (*s == NUL || p + 1 >= out + outlen)
3462 break;
3463
3464 /*
3465 * Handle one '%' item.
3466 */
3467 s++;
3468 if (*s == '%')
3469 {
3470 if (p + 1 >= out + outlen)
3471 break;
3472 *p++ = *s++;
3473 prevchar_isflag = prevchar_isitem = FALSE;
3474 continue;
3475 }
3476 if (*s == STL_MIDDLEMARK)
3477 {
3478 s++;
3479 if (groupdepth > 0)
3480 continue;
3481 item[curitem].type = Middle;
3482 item[curitem++].start = p;
3483 continue;
3484 }
3485 if (*s == STL_TRUNCMARK)
3486 {
3487 s++;
3488 item[curitem].type = Trunc;
3489 item[curitem++].start = p;
3490 continue;
3491 }
3492 if (*s == ')')
3493 {
3494 s++;
3495 if (groupdepth < 1)
3496 continue;
3497 groupdepth--;
3498
3499 t = item[groupitem[groupdepth]].start;
3500 *p = NUL;
3501 l = vim_strsize(t);
3502 if (curitem > groupitem[groupdepth] + 1
3503 && item[groupitem[groupdepth]].minwid == 0)
3504 {
3505 /* remove group if all items are empty */
3506 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3507 if (item[n].type == Normal)
3508 break;
3509 if (n == curitem)
3510 {
3511 p = t;
3512 l = 0;
3513 }
3514 }
3515 if (l > item[groupitem[groupdepth]].maxwid)
3516 {
3517 /* truncate, remove n bytes of text at the start */
3518#ifdef FEAT_MBYTE
3519 if (has_mbyte)
3520 {
3521 /* Find the first character that should be included. */
3522 n = 0;
3523 while (l >= item[groupitem[groupdepth]].maxwid)
3524 {
3525 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003526 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
3528 }
3529 else
3530#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003531 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532
3533 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003534 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 p = p - n + 1;
3536#ifdef FEAT_MBYTE
3537 /* Fill up space left over by half a double-wide char. */
3538 while (++l < item[groupitem[groupdepth]].minwid)
3539 *p++ = fillchar;
3540#endif
3541
3542 /* correct the start of the items for the truncation */
3543 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3544 {
3545 item[l].start -= n;
3546 if (item[l].start < t)
3547 item[l].start = t;
3548 }
3549 }
3550 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3551 {
3552 /* fill */
3553 n = item[groupitem[groupdepth]].minwid;
3554 if (n < 0)
3555 {
3556 /* fill by appending characters */
3557 n = 0 - n;
3558 while (l++ < n && p + 1 < out + outlen)
3559 *p++ = fillchar;
3560 }
3561 else
3562 {
3563 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003564 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 l = n - l;
3566 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003567 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 p += l;
3569 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3570 item[n].start += l;
3571 for ( ; l > 0; l--)
3572 *t++ = fillchar;
3573 }
3574 }
3575 continue;
3576 }
3577 minwid = 0;
3578 maxwid = 9999;
3579 zeropad = FALSE;
3580 l = 1;
3581 if (*s == '0')
3582 {
3583 s++;
3584 zeropad = TRUE;
3585 }
3586 if (*s == '-')
3587 {
3588 s++;
3589 l = -1;
3590 }
3591 if (VIM_ISDIGIT(*s))
3592 {
3593 minwid = (int)getdigits(&s);
3594 if (minwid < 0) /* overflow */
3595 minwid = 0;
3596 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003597 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
3599 item[curitem].type = Highlight;
3600 item[curitem].start = p;
3601 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3602 s++;
3603 curitem++;
3604 continue;
3605 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003606 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3607 {
3608 if (*s == STL_TABCLOSENR)
3609 {
3610 if (minwid == 0)
3611 {
3612 /* %X ends the close label, go back to the previously
3613 * define tab label nr. */
3614 for (n = curitem - 1; n >= 0; --n)
3615 if (item[n].type == TabPage && item[n].minwid >= 0)
3616 {
3617 minwid = item[n].minwid;
3618 break;
3619 }
3620 }
3621 else
3622 /* close nrs are stored as negative values */
3623 minwid = - minwid;
3624 }
3625 item[curitem].type = TabPage;
3626 item[curitem].start = p;
3627 item[curitem].minwid = minwid;
3628 s++;
3629 curitem++;
3630 continue;
3631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 if (*s == '.')
3633 {
3634 s++;
3635 if (VIM_ISDIGIT(*s))
3636 {
3637 maxwid = (int)getdigits(&s);
3638 if (maxwid <= 0) /* overflow */
3639 maxwid = 50;
3640 }
3641 }
3642 minwid = (minwid > 50 ? 50 : minwid) * l;
3643 if (*s == '(')
3644 {
3645 groupitem[groupdepth++] = curitem;
3646 item[curitem].type = Group;
3647 item[curitem].start = p;
3648 item[curitem].minwid = minwid;
3649 item[curitem].maxwid = maxwid;
3650 s++;
3651 curitem++;
3652 continue;
3653 }
3654 if (vim_strchr(STL_ALL, *s) == NULL)
3655 {
3656 s++;
3657 continue;
3658 }
3659 opt = *s++;
3660
3661 /* OK - now for the real work */
3662 base = 'D';
3663 itemisflag = FALSE;
3664 fillable = TRUE;
3665 num = -1;
3666 str = NULL;
3667 switch (opt)
3668 {
3669 case STL_FILEPATH:
3670 case STL_FULLPATH:
3671 case STL_FILENAME:
3672 fillable = FALSE; /* don't change ' ' to fillchar */
3673 if (buf_spname(wp->w_buffer) != NULL)
3674 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3675 else
3676 {
3677 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003678 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3680 }
3681 trans_characters(NameBuff, MAXPATHL);
3682 if (opt != STL_FILENAME)
3683 str = NameBuff;
3684 else
3685 str = gettail(NameBuff);
3686 break;
3687
3688 case STL_VIM_EXPR: /* '{' */
3689 itemisflag = TRUE;
3690 t = p;
3691 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3692 *p++ = *s++;
3693 if (*s != '}') /* missing '}' or out of space */
3694 break;
3695 s++;
3696 *p = 0;
3697 p = t;
3698
3699#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003700 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3702
3703 o_curbuf = curbuf;
3704 o_curwin = curwin;
3705 curwin = wp;
3706 curbuf = wp->w_buffer;
3707
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003708 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
3710 curwin = o_curwin;
3711 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003712 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713
3714 if (str != NULL && *str != 0)
3715 {
3716 if (*skipdigits(str) == NUL)
3717 {
3718 num = atoi((char *)str);
3719 vim_free(str);
3720 str = NULL;
3721 itemisflag = FALSE;
3722 }
3723 }
3724#endif
3725 break;
3726
3727 case STL_LINE:
3728 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3729 ? 0L : (long)(wp->w_cursor.lnum);
3730 break;
3731
3732 case STL_NUMLINES:
3733 num = wp->w_buffer->b_ml.ml_line_count;
3734 break;
3735
3736 case STL_COLUMN:
3737 num = !(State & INSERT) && empty_line
3738 ? 0 : (int)wp->w_cursor.col + 1;
3739 break;
3740
3741 case STL_VIRTCOL:
3742 case STL_VIRTCOL_ALT:
3743 /* In list mode virtcol needs to be recomputed */
3744 virtcol = wp->w_virtcol;
3745 if (wp->w_p_list && lcs_tab1 == NUL)
3746 {
3747 wp->w_p_list = FALSE;
3748 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3749 wp->w_p_list = TRUE;
3750 }
3751 ++virtcol;
3752 /* Don't display %V if it's the same as %c. */
3753 if (opt == STL_VIRTCOL_ALT
3754 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3755 ? 0 : (int)wp->w_cursor.col + 1)))
3756 break;
3757 num = (long)virtcol;
3758 break;
3759
3760 case STL_PERCENTAGE:
3761 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3762 (long)wp->w_buffer->b_ml.ml_line_count);
3763 break;
3764
3765 case STL_ALTPERCENT:
3766 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003767 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 break;
3769
3770 case STL_ARGLISTSTAT:
3771 fillable = FALSE;
3772 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003773 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 str = tmp;
3775 break;
3776
3777 case STL_KEYMAP:
3778 fillable = FALSE;
3779 if (get_keymap_str(wp, tmp, TMPLEN))
3780 str = tmp;
3781 break;
3782 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003783#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003784 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785#else
3786 num = 0;
3787#endif
3788 break;
3789
3790 case STL_BUFNO:
3791 num = wp->w_buffer->b_fnum;
3792 break;
3793
3794 case STL_OFFSET_X:
3795 base = 'X';
3796 case STL_OFFSET:
3797#ifdef FEAT_BYTEOFF
3798 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3799 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3800 0L : l + 1 + (!(State & INSERT) && empty_line ?
3801 0 : (int)wp->w_cursor.col);
3802#endif
3803 break;
3804
3805 case STL_BYTEVAL_X:
3806 base = 'X';
3807 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003808 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 num = 0;
3810 else
3811 {
3812#ifdef FEAT_MBYTE
3813 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3814#else
3815 num = linecont[wp->w_cursor.col];
3816#endif
3817 }
3818 if (num == NL)
3819 num = 0;
3820 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3821 num = NL;
3822 break;
3823
3824 case STL_ROFLAG:
3825 case STL_ROFLAG_ALT:
3826 itemisflag = TRUE;
3827 if (wp->w_buffer->b_p_ro)
3828 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3829 break;
3830
3831 case STL_HELPFLAG:
3832 case STL_HELPFLAG_ALT:
3833 itemisflag = TRUE;
3834 if (wp->w_buffer->b_help)
3835 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003836 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 break;
3838
3839#ifdef FEAT_AUTOCMD
3840 case STL_FILETYPE:
3841 if (*wp->w_buffer->b_p_ft != NUL
3842 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3843 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003844 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3845 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 str = tmp;
3847 }
3848 break;
3849
3850 case STL_FILETYPE_ALT:
3851 itemisflag = TRUE;
3852 if (*wp->w_buffer->b_p_ft != NUL
3853 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3854 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003855 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3856 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 for (t = tmp; *t != 0; t++)
3858 *t = TOUPPER_LOC(*t);
3859 str = tmp;
3860 }
3861 break;
3862#endif
3863
3864#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3865 case STL_PREVIEWFLAG:
3866 case STL_PREVIEWFLAG_ALT:
3867 itemisflag = TRUE;
3868 if (wp->w_p_pvw)
3869 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3870 : _("[Preview]"));
3871 break;
3872#endif
3873
3874 case STL_MODIFIED:
3875 case STL_MODIFIED_ALT:
3876 itemisflag = TRUE;
3877 switch ((opt == STL_MODIFIED_ALT)
3878 + bufIsChanged(wp->w_buffer) * 2
3879 + (!wp->w_buffer->b_p_ma) * 4)
3880 {
3881 case 2: str = (char_u *)"[+]"; break;
3882 case 3: str = (char_u *)",+"; break;
3883 case 4: str = (char_u *)"[-]"; break;
3884 case 5: str = (char_u *)",-"; break;
3885 case 6: str = (char_u *)"[+-]"; break;
3886 case 7: str = (char_u *)",+-"; break;
3887 }
3888 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003889
3890 case STL_HIGHLIGHT:
3891 t = s;
3892 while (*s != '#' && *s != NUL)
3893 ++s;
3894 if (*s == '#')
3895 {
3896 item[curitem].type = Highlight;
3897 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003898 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003899 curitem++;
3900 }
3901 ++s;
3902 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
3904
3905 item[curitem].start = p;
3906 item[curitem].type = Normal;
3907 if (str != NULL && *str)
3908 {
3909 t = str;
3910 if (itemisflag)
3911 {
3912 if ((t[0] && t[1])
3913 && ((!prevchar_isitem && *t == ',')
3914 || (prevchar_isflag && *t == ' ')))
3915 t++;
3916 prevchar_isflag = TRUE;
3917 }
3918 l = vim_strsize(t);
3919 if (l > 0)
3920 prevchar_isitem = TRUE;
3921 if (l > maxwid)
3922 {
3923 while (l >= maxwid)
3924#ifdef FEAT_MBYTE
3925 if (has_mbyte)
3926 {
3927 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003928 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 }
3930 else
3931#endif
3932 l -= byte2cells(*t++);
3933 if (p + 1 >= out + outlen)
3934 break;
3935 *p++ = '<';
3936 }
3937 if (minwid > 0)
3938 {
3939 for (; l < minwid && p + 1 < out + outlen; l++)
3940 {
3941 /* Don't put a "-" in front of a digit. */
3942 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3943 *p++ = ' ';
3944 else
3945 *p++ = fillchar;
3946 }
3947 minwid = 0;
3948 }
3949 else
3950 minwid *= -1;
3951 while (*t && p + 1 < out + outlen)
3952 {
3953 *p++ = *t++;
3954 /* Change a space by fillchar, unless fillchar is '-' and a
3955 * digit follows. */
3956 if (fillable && p[-1] == ' '
3957 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3958 p[-1] = fillchar;
3959 }
3960 for (; l < minwid && p + 1 < out + outlen; l++)
3961 *p++ = fillchar;
3962 }
3963 else if (num >= 0)
3964 {
3965 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3966 char_u nstr[20];
3967
3968 if (p + 20 >= out + outlen)
3969 break; /* not sufficient space */
3970 prevchar_isitem = TRUE;
3971 t = nstr;
3972 if (opt == STL_VIRTCOL_ALT)
3973 {
3974 *t++ = '-';
3975 minwid--;
3976 }
3977 *t++ = '%';
3978 if (zeropad)
3979 *t++ = '0';
3980 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003981 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 *t = 0;
3983
3984 for (n = num, l = 1; n >= nbase; n /= nbase)
3985 l++;
3986 if (opt == STL_VIRTCOL_ALT)
3987 l++;
3988 if (l > maxwid)
3989 {
3990 l += 2;
3991 n = l - maxwid;
3992 while (l-- > maxwid)
3993 num /= nbase;
3994 *t++ = '>';
3995 *t++ = '%';
3996 *t = t[-3];
3997 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003998 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3999 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
4001 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004002 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4003 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 p += STRLEN(p);
4005 }
4006 else
4007 item[curitem].type = Empty;
4008
4009 if (opt == STL_VIM_EXPR)
4010 vim_free(str);
4011
4012 if (num >= 0 || (!itemisflag && str && *str))
4013 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4014 curitem++;
4015 }
4016 *p = NUL;
4017 itemcnt = curitem;
4018
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004019#ifdef FEAT_EVAL
4020 if (usefmt != fmt)
4021 vim_free(usefmt);
4022#endif
4023
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 width = vim_strsize(out);
4025 if (maxwidth > 0 && width > maxwidth)
4026 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004027 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 l = 0;
4029 if (itemcnt == 0)
4030 s = out;
4031 else
4032 {
4033 for ( ; l < itemcnt; l++)
4034 if (item[l].type == Trunc)
4035 {
4036 /* Truncate at %< item. */
4037 s = item[l].start;
4038 break;
4039 }
4040 if (l == itemcnt)
4041 {
4042 /* No %< item, truncate first item. */
4043 s = item[0].start;
4044 l = 0;
4045 }
4046 }
4047
4048 if (width - vim_strsize(s) >= maxwidth)
4049 {
4050 /* Truncation mark is beyond max length */
4051#ifdef FEAT_MBYTE
4052 if (has_mbyte)
4053 {
4054 s = out;
4055 width = 0;
4056 for (;;)
4057 {
4058 width += ptr2cells(s);
4059 if (width >= maxwidth)
4060 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004061 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
4063 /* Fill up for half a double-wide character. */
4064 while (++width < maxwidth)
4065 *s++ = fillchar;
4066 }
4067 else
4068#endif
4069 s = out + maxwidth - 1;
4070 for (l = 0; l < itemcnt; l++)
4071 if (item[l].start > s)
4072 break;
4073 itemcnt = l;
4074 *s++ = '>';
4075 *s = 0;
4076 }
4077 else
4078 {
4079#ifdef FEAT_MBYTE
4080 if (has_mbyte)
4081 {
4082 n = 0;
4083 while (width >= maxwidth)
4084 {
4085 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004086 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088 }
4089 else
4090#endif
4091 n = width - maxwidth + 1;
4092 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004093 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 *s = '<';
4095
4096 /* Fill up for half a double-wide character. */
4097 while (++width < maxwidth)
4098 {
4099 s = s + STRLEN(s);
4100 *s++ = fillchar;
4101 *s = NUL;
4102 }
4103
4104 --n; /* count the '<' */
4105 for (; l < itemcnt; l++)
4106 {
4107 if (item[l].start - n >= s)
4108 item[l].start -= n;
4109 else
4110 item[l].start = s;
4111 }
4112 }
4113 width = maxwidth;
4114 }
4115 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4116 {
4117 /* Apply STL_MIDDLE if any */
4118 for (l = 0; l < itemcnt; l++)
4119 if (item[l].type == Middle)
4120 break;
4121 if (l < itemcnt)
4122 {
4123 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004124 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 for (s = item[l].start; s < p; s++)
4126 *s = fillchar;
4127 for (l++; l < itemcnt; l++)
4128 item[l].start += maxwidth - width;
4129 width = maxwidth;
4130 }
4131 }
4132
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004133 /* Store the info about highlighting. */
4134 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004136 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 for (l = 0; l < itemcnt; l++)
4138 {
4139 if (item[l].type == Highlight)
4140 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004141 sp->start = item[l].start;
4142 sp->userhl = item[l].minwid;
4143 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
4145 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004146 sp->start = NULL;
4147 sp->userhl = 0;
4148 }
4149
4150 /* Store the info about tab pages labels. */
4151 if (tabtab != NULL)
4152 {
4153 sp = tabtab;
4154 for (l = 0; l < itemcnt; l++)
4155 {
4156 if (item[l].type == TabPage)
4157 {
4158 sp->start = item[l].start;
4159 sp->userhl = item[l].minwid;
4160 sp++;
4161 }
4162 }
4163 sp->start = NULL;
4164 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 }
4166
4167 return width;
4168}
4169#endif /* FEAT_STL_OPT */
4170
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004171#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4172 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004174 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4175 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 */
4177 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004178get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004180 char_u *buf;
4181 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182{
4183 long above; /* number of lines above window */
4184 long below; /* number of lines below window */
4185
4186 above = wp->w_topline - 1;
4187#ifdef FEAT_DIFF
4188 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4189#endif
4190 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4191 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004192 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4193 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004195 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004197 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 ? (int)(above / ((above + below) / 100L))
4199 : (int)(above * 100L / (above + below)));
4200}
4201#endif
4202
4203/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004204 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 * Return TRUE if it was appended.
4206 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004207 static int
4208append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 win_T *wp;
4210 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004211 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213{
4214 char_u *p;
4215
4216 if (ARGCOUNT <= 1) /* nothing to do */
4217 return FALSE;
4218
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004219 p = buf + STRLEN(buf); /* go to the end of the buffer */
4220 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 return FALSE;
4222 *p++ = ' ';
4223 *p++ = '(';
4224 if (add_file)
4225 {
4226 STRCPY(p, "file ");
4227 p += 5;
4228 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004229 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4230 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4232 return TRUE;
4233}
4234
4235/*
4236 * If fname is not a full path, make it a full path.
4237 * Returns pointer to allocated memory (NULL for failure).
4238 */
4239 char_u *
4240fix_fname(fname)
4241 char_u *fname;
4242{
4243 /*
4244 * Force expanding the path always for Unix, because symbolic links may
4245 * mess up the full path name, even though it starts with a '/'.
4246 * Also expand when there is ".." in the file name, try to remove it,
4247 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004248 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 * For MS-Windows also expand names like "longna~1" to "longname".
4250 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004251#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 return FullName_save(fname, TRUE);
4253#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004254 if (!vim_isAbsName(fname)
4255 || strstr((char *)fname, "..") != NULL
4256 || strstr((char *)fname, "//") != NULL
4257# ifdef BACKSLASH_IN_FILENAME
4258 || strstr((char *)fname, "\\\\") != NULL
4259# endif
4260# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004262# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 )
4264 return FullName_save(fname, FALSE);
4265
4266 fname = vim_strsave(fname);
4267
Bram Moolenaar9b942202007-10-03 12:31:33 +00004268# ifdef USE_FNAME_CASE
4269# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 {
4273 if (fname != NULL)
4274 fname_case(fname, 0); /* set correct case for file name */
4275 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
4278 return fname;
4279#endif
4280}
4281
4282/*
4283 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4284 * "ffname" becomes a pointer to allocated memory (or NULL).
4285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 void
4287fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004288 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 char_u **ffname;
4290 char_u **sfname;
4291{
4292 if (*ffname == NULL) /* if no file name given, nothing to do */
4293 return;
4294 if (*sfname == NULL) /* if no short file name given, use ffname */
4295 *sfname = *ffname;
4296 *ffname = fix_fname(*ffname); /* expand to full path */
4297
4298#ifdef FEAT_SHORTCUT
4299 if (!buf->b_p_bin)
4300 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004301 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
4303 /* If the file name is a shortcut file, use the file it links to. */
4304 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004305 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
4307 vim_free(*ffname);
4308 *ffname = rfname;
4309 *sfname = rfname;
4310 }
4311 }
4312#endif
4313}
4314
4315/*
4316 * Get the file name for an argument list entry.
4317 */
4318 char_u *
4319alist_name(aep)
4320 aentry_T *aep;
4321{
4322 buf_T *bp;
4323
4324 /* Use the name from the associated buffer if it exists. */
4325 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004326 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return aep->ae_fname;
4328 return bp->b_fname;
4329}
4330
4331#if defined(FEAT_WINDOWS) || defined(PROTO)
4332/*
4333 * do_arg_all(): Open up to 'count' windows, one for each argument.
4334 */
4335 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004336do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 int count;
4338 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004339 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340{
4341 int i;
4342 win_T *wp, *wpnext;
4343 char_u *opened; /* array of flags for which args are open */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004344 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 int use_firstwin = FALSE; /* use first window for arglist */
4346 int split_ret = OK;
4347 int p_ea_save;
4348 alist_T *alist; /* argument list to be used */
4349 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004350 tabpage_T *tpnext;
4351 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004352 win_T *new_curwin = NULL;
4353 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
4355 if (ARGCOUNT <= 0)
4356 {
4357 /* Don't give an error message. We don't want it when the ":all"
4358 * command is in the .vimrc. */
4359 return;
4360 }
4361 setpcmark();
4362
4363 opened_len = ARGCOUNT;
4364 opened = alloc_clear((unsigned)opened_len);
4365 if (opened == NULL)
4366 return;
4367
4368#ifdef FEAT_GUI
4369 need_mouse_correct = TRUE;
4370#endif
4371
4372 /*
4373 * Try closing all windows that are not in the argument list.
4374 * Also close windows that are not full width;
4375 * When 'hidden' or "forceit" set the buffer becomes hidden.
4376 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004377 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004379 if (had_tab > 0)
4380 goto_tabpage_tp(first_tabpage);
4381 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004383 tpnext = curtab->tp_next;
4384 for (wp = firstwin; wp != NULL; wp = wpnext)
4385 {
4386 wpnext = wp->w_next;
4387 buf = wp->w_buffer;
4388 if (buf->b_ffname == NULL
4389 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004391 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004393 )
4394 i = ARGCOUNT;
4395 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004397 /* check if the buffer in this window is in the arglist */
4398 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004400 if (ARGLIST[i].ae_fnum == buf->b_fnum
4401 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004402 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004404 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004405 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004406 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004407 if (i == 0)
4408 {
4409 new_curwin = wp;
4410 new_curtab = curtab;
4411 }
4412 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004413 if (wp->w_alist != curwin->w_alist)
4414 {
4415 /* Use the current argument list for all windows
4416 * containing a file from it. */
4417 alist_unlink(wp->w_alist);
4418 wp->w_alist = curwin->w_alist;
4419 ++wp->w_alist->al_refcount;
4420 }
4421 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
4424 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004425 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004427 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004429 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004430 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004432 /* If the buffer was changed, and we would like to hide it,
4433 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004434 if (!P_HID(buf) && buf->b_nwindows <= 1
4435 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004437 (void)autowrite(buf, FALSE);
4438#ifdef FEAT_AUTOCMD
4439 /* check if autocommands removed the window */
4440 if (!win_valid(wp) || !buf_valid(buf))
4441 {
4442 wpnext = firstwin; /* start all over... */
4443 continue;
4444 }
4445#endif
4446 }
4447#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004448 /* don't close last window */
4449 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004450#endif
4451 use_firstwin = TRUE;
4452#ifdef FEAT_WINDOWS
4453 else
4454 {
4455 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4456# ifdef FEAT_AUTOCMD
4457 /* check if autocommands removed the next window */
4458 if (!win_valid(wpnext))
4459 wpnext = firstwin; /* start all over... */
4460# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462#endif
4463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004466
4467 /* Without the ":tab" modifier only do the current tab page. */
4468 if (had_tab == 0 || tpnext == NULL)
4469 break;
4470
4471# ifdef FEAT_AUTOCMD
4472 /* check if autocommands removed the next tab page */
4473 if (!valid_tabpage(tpnext))
4474 tpnext = first_tabpage; /* start all over...*/
4475# endif
4476 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478
4479 /*
4480 * Open a window for files in the argument list that don't have one.
4481 * ARGCOUNT may change while doing this, because of autocommands.
4482 */
4483 if (count > ARGCOUNT || count <= 0)
4484 count = ARGCOUNT;
4485
4486 /* Autocommands may do anything to the argument list. Make sure it's not
4487 * freed while we are working here by "locking" it. We still have to
4488 * watch out for its size to be changed. */
4489 alist = curwin->w_alist;
4490 ++alist->al_refcount;
4491
4492#ifdef FEAT_AUTOCMD
4493 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4494 ++autocmd_no_enter;
4495 ++autocmd_no_leave;
4496#endif
4497 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004498#ifdef FEAT_WINDOWS
4499 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4500 * leaving an empty tab page when executed locally. */
4501 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4502 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4503 use_firstwin = TRUE;
4504#endif
4505
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4507 {
4508 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4509 arg_had_last = TRUE;
4510 if (i < opened_len && opened[i])
4511 {
4512 /* Move the already present window to below the current window */
4513 if (curwin->w_arg_idx != i)
4514 {
4515 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4516 {
4517 if (wpnext->w_arg_idx == i)
4518 {
4519 win_move_after(wpnext, curwin);
4520 break;
4521 }
4522 }
4523 }
4524 }
4525 else if (split_ret == OK)
4526 {
4527 if (!use_firstwin) /* split current window */
4528 {
4529 p_ea_save = p_ea;
4530 p_ea = TRUE; /* use space from all windows */
4531 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4532 p_ea = p_ea_save;
4533 if (split_ret == FAIL)
4534 continue;
4535 }
4536#ifdef FEAT_AUTOCMD
4537 else /* first window: do autocmd for leaving this buffer */
4538 --autocmd_no_leave;
4539#endif
4540
4541 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004542 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 */
4544 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004545 if (i == 0)
4546 {
4547 new_curwin = curwin;
4548 new_curtab = curtab;
4549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4551 ECMD_ONE,
4552 ((P_HID(curwin->w_buffer)
4553 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004554 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555#ifdef FEAT_AUTOCMD
4556 if (use_firstwin)
4557 ++autocmd_no_leave;
4558#endif
4559 use_firstwin = FALSE;
4560 }
4561 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004562
4563 /* When ":tab" was used open a new tab for a new window repeatedly. */
4564 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4565 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
4567
4568 /* Remove the "lock" on the argument list. */
4569 alist_unlink(alist);
4570
4571#ifdef FEAT_AUTOCMD
4572 --autocmd_no_enter;
4573#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004574 /* to window with first arg */
4575 if (valid_tabpage(new_curtab))
4576 goto_tabpage_tp(new_curtab);
4577 if (win_valid(new_curwin))
4578 win_enter(new_curwin, FALSE);
4579
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580#ifdef FEAT_AUTOCMD
4581 --autocmd_no_leave;
4582#endif
4583 vim_free(opened);
4584}
4585
4586# if defined(FEAT_LISTCMDS) || defined(PROTO)
4587/*
4588 * Open a window for a number of buffers.
4589 */
4590 void
4591ex_buffer_all(eap)
4592 exarg_T *eap;
4593{
4594 buf_T *buf;
4595 win_T *wp, *wpnext;
4596 int split_ret = OK;
4597 int p_ea_save;
4598 int open_wins = 0;
4599 int r;
4600 int count; /* Maximum number of windows to open. */
4601 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004602#ifdef FEAT_WINDOWS
4603 int had_tab = cmdmod.tab;
4604 tabpage_T *tpnext;
4605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606
4607 if (eap->addr_count == 0) /* make as many windows as possible */
4608 count = 9999;
4609 else
4610 count = eap->line2; /* make as many windows as specified */
4611 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4612 all = FALSE;
4613 else
4614 all = TRUE;
4615
4616 setpcmark();
4617
4618#ifdef FEAT_GUI
4619 need_mouse_correct = TRUE;
4620#endif
4621
4622 /*
4623 * Close superfluous windows (two windows for the same buffer).
4624 * Also close windows that are not full-width.
4625 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004626#ifdef FEAT_WINDOWS
4627 if (had_tab > 0)
4628 goto_tabpage_tp(first_tabpage);
4629 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004632 tpnext = curtab->tp_next;
4633 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004635 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004636 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004637#ifdef FEAT_VERTSPLIT
4638 || ((cmdmod.split & WSP_VERT)
4639 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004640 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004641 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004643#ifdef FEAT_WINDOWS
4644 || (had_tab > 0 && wp != firstwin)
4645#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004646 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004647 {
4648 win_close(wp, FALSE);
4649#ifdef FEAT_AUTOCMD
4650 wpnext = firstwin; /* just in case an autocommand does
4651 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004652 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004653 open_wins = 0;
4654#endif
4655 }
4656 else
4657 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004659
4660#ifdef FEAT_WINDOWS
4661 /* Without the ":tab" modifier only do the current tab page. */
4662 if (had_tab == 0 || tpnext == NULL)
4663 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004664 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667
4668 /*
4669 * Go through the buffer list. When a buffer doesn't have a window yet,
4670 * open one. Otherwise move the window to the right position.
4671 * Watch out for autocommands that delete buffers or windows!
4672 */
4673#ifdef FEAT_AUTOCMD
4674 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4675 ++autocmd_no_enter;
4676#endif
4677 win_enter(lastwin, FALSE);
4678#ifdef FEAT_AUTOCMD
4679 ++autocmd_no_leave;
4680#endif
4681 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4682 {
4683 /* Check if this buffer needs a window */
4684 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4685 continue;
4686
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004687#ifdef FEAT_WINDOWS
4688 if (had_tab != 0)
4689 {
4690 /* With the ":tab" modifier don't move the window. */
4691 if (buf->b_nwindows > 0)
4692 wp = lastwin; /* buffer has a window, skip it */
4693 else
4694 wp = NULL;
4695 }
4696 else
4697#endif
4698 {
4699 /* Check if this buffer already has a window */
4700 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4701 if (wp->w_buffer == buf)
4702 break;
4703 /* If the buffer already has a window, move it */
4704 if (wp != NULL)
4705 win_move_after(wp, curwin);
4706 }
4707
4708 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 {
4710 /* Split the window and put the buffer in it */
4711 p_ea_save = p_ea;
4712 p_ea = TRUE; /* use space from all windows */
4713 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4714 ++open_wins;
4715 p_ea = p_ea_save;
4716 if (split_ret == FAIL)
4717 continue;
4718
4719 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004720#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 swap_exists_action = SEA_DIALOG;
4722#endif
4723 set_curbuf(buf, DOBUF_GOTO);
4724#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004727#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004729# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 break;
4731 }
4732#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004733#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 if (swap_exists_action == SEA_QUIT)
4735 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004736# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4737 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004738
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004739 /* Reset the error/interrupt/exception state here so that
4740 * aborting() returns FALSE when closing a window. */
4741 enter_cleanup(&cs);
4742# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004743
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004744 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 win_close(curwin, TRUE);
4746 --open_wins;
4747 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004748 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004749
4750# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4751 /* Restore the error/interrupt/exception state if not
4752 * discarded by a new aborting error, interrupt, or uncaught
4753 * exception. */
4754 leave_cleanup(&cs);
4755# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757 else
4758 handle_swap_exists(NULL);
4759#endif
4760 }
4761
4762 ui_breakcheck();
4763 if (got_int)
4764 {
4765 (void)vgetc(); /* only break the file loading, not the rest */
4766 break;
4767 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004768#ifdef FEAT_EVAL
4769 /* Autocommands deleted the buffer or aborted script processing!!! */
4770 if (aborting())
4771 break;
4772#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004773#ifdef FEAT_WINDOWS
4774 /* When ":tab" was used open a new tab for a new window repeatedly. */
4775 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4776 cmdmod.tab = 9999;
4777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 }
4779#ifdef FEAT_AUTOCMD
4780 --autocmd_no_enter;
4781#endif
4782 win_enter(firstwin, FALSE); /* back to first window */
4783#ifdef FEAT_AUTOCMD
4784 --autocmd_no_leave;
4785#endif
4786
4787 /*
4788 * Close superfluous windows.
4789 */
4790 for (wp = lastwin; open_wins > count; )
4791 {
4792 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4793 || autowrite(wp->w_buffer, FALSE) == OK);
4794#ifdef FEAT_AUTOCMD
4795 if (!win_valid(wp))
4796 {
4797 /* BufWrite Autocommands made the window invalid, start over */
4798 wp = lastwin;
4799 }
4800 else
4801#endif
4802 if (r)
4803 {
4804 win_close(wp, !P_HID(wp->w_buffer));
4805 --open_wins;
4806 wp = lastwin;
4807 }
4808 else
4809 {
4810 wp = wp->w_prev;
4811 if (wp == NULL)
4812 break;
4813 }
4814 }
4815}
4816# endif /* FEAT_LISTCMDS */
4817
4818#endif /* FEAT_WINDOWS */
4819
Bram Moolenaara3227e22006-03-08 21:32:40 +00004820static int chk_modeline __ARGS((linenr_T, int));
4821
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822/*
4823 * do_modelines() - process mode lines for the current file
4824 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004825 * "flags" can be:
4826 * OPT_WINONLY only set options local to window
4827 * OPT_NOWIN don't set options local to window
4828 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 * Returns immediately if the "ml" option isn't set.
4830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004832do_modelines(flags)
4833 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004835 linenr_T lnum;
4836 int nmlines;
4837 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838
4839 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4840 return;
4841
4842 /* Disallow recursive entry here. Can happen when executing a modeline
4843 * triggers an autocommand, which reloads modelines with a ":do". */
4844 if (entered)
4845 return;
4846
4847 ++entered;
4848 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4849 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004850 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 nmlines = 0;
4852
4853 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4854 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004855 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 nmlines = 0;
4857 --entered;
4858}
4859
4860#include "version.h" /* for version number */
4861
4862/*
4863 * chk_modeline() - check a single line for a mode string
4864 * Return FAIL if an error encountered.
4865 */
4866 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004867chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004869 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
4871 char_u *s;
4872 char_u *e;
4873 char_u *linecopy; /* local copy of any modeline found */
4874 int prev;
4875 int vers;
4876 int end;
4877 int retval = OK;
4878 char_u *save_sourcing_name;
4879 linenr_T save_sourcing_lnum;
4880#ifdef FEAT_EVAL
4881 scid_T save_SID;
4882#endif
4883
4884 prev = -1;
4885 for (s = ml_get(lnum); *s != NUL; ++s)
4886 {
4887 if (prev == -1 || vim_isspace(prev))
4888 {
4889 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4890 || STRNCMP(s, "vi:", (size_t)3) == 0)
4891 break;
4892 if (STRNCMP(s, "vim", 3) == 0)
4893 {
4894 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4895 e = s + 4;
4896 else
4897 e = s + 3;
4898 vers = getdigits(&e);
4899 if (*e == ':'
4900 && (s[3] == ':'
4901 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4902 || (VIM_VERSION_100 < vers && s[3] == '<')
4903 || (VIM_VERSION_100 > vers && s[3] == '>')
4904 || (VIM_VERSION_100 == vers && s[3] == '=')))
4905 break;
4906 }
4907 }
4908 prev = *s;
4909 }
4910
4911 if (*s)
4912 {
4913 do /* skip over "ex:", "vi:" or "vim:" */
4914 ++s;
4915 while (s[-1] != ':');
4916
4917 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4918 if (linecopy == NULL)
4919 return FAIL;
4920
4921 save_sourcing_lnum = sourcing_lnum;
4922 save_sourcing_name = sourcing_name;
4923 sourcing_lnum = lnum; /* prepare for emsg() */
4924 sourcing_name = (char_u *)"modelines";
4925
4926 end = FALSE;
4927 while (end == FALSE)
4928 {
4929 s = skipwhite(s);
4930 if (*s == NUL)
4931 break;
4932
4933 /*
4934 * Find end of set command: ':' or end of line.
4935 * Skip over "\:", replacing it with ":".
4936 */
4937 for (e = s; *e != ':' && *e != NUL; ++e)
4938 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00004939 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 if (*e == NUL)
4941 end = TRUE;
4942
4943 /*
4944 * If there is a "set" command, require a terminating ':' and
4945 * ignore the stuff after the ':'.
4946 * "vi:set opt opt opt: foo" -- foo not interpreted
4947 * "vi:opt opt opt: foo" -- foo interpreted
4948 * Accept "se" for compatibility with Elvis.
4949 */
4950 if (STRNCMP(s, "set ", (size_t)4) == 0
4951 || STRNCMP(s, "se ", (size_t)3) == 0)
4952 {
4953 if (*e != ':') /* no terminating ':'? */
4954 break;
4955 end = TRUE;
4956 s = vim_strchr(s, ' ') + 1;
4957 }
4958 *e = NUL; /* truncate the set command */
4959
4960 if (*s != NUL) /* skip over an empty "::" */
4961 {
4962#ifdef FEAT_EVAL
4963 save_SID = current_SID;
4964 current_SID = SID_MODELINE;
4965#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004966 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967#ifdef FEAT_EVAL
4968 current_SID = save_SID;
4969#endif
4970 if (retval == FAIL) /* stop if error found */
4971 break;
4972 }
4973 s = e + 1; /* advance to next part */
4974 }
4975
4976 sourcing_lnum = save_sourcing_lnum;
4977 sourcing_name = save_sourcing_name;
4978
4979 vim_free(linecopy);
4980 }
4981 return retval;
4982}
4983
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00004984#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 int
4986read_viminfo_bufferlist(virp, writing)
4987 vir_T *virp;
4988 int writing;
4989{
4990 char_u *tab;
4991 linenr_T lnum;
4992 colnr_T col;
4993 buf_T *buf;
4994 char_u *sfname;
4995 char_u *xline;
4996
4997 /* Handle long line and escaped characters. */
4998 xline = viminfo_readstring(virp, 1, FALSE);
4999
5000 /* don't read in if there are files on the command-line or if writing: */
5001 if (xline != NULL && !writing && ARGCOUNT == 0
5002 && find_viminfo_parameter('%') != NULL)
5003 {
5004 /* Format is: <fname> Tab <lnum> Tab <col>.
5005 * Watch out for a Tab in the file name, work from the end. */
5006 lnum = 0;
5007 col = 0;
5008 tab = vim_strrchr(xline, '\t');
5009 if (tab != NULL)
5010 {
5011 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005012 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 tab = vim_strrchr(xline, '\t');
5014 if (tab != NULL)
5015 {
5016 *tab++ = '\0';
5017 lnum = atol((char *)tab);
5018 }
5019 }
5020
5021 /* Expand "~/" in the file name at "line + 1" to a full path.
5022 * Then try shortening it by comparing with the current directory */
5023 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005024 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025
5026 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5027 if (buf != NULL) /* just in case... */
5028 {
5029 buf->b_last_cursor.lnum = lnum;
5030 buf->b_last_cursor.col = col;
5031 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5032 }
5033 }
5034 vim_free(xline);
5035
5036 return viminfo_readline(virp);
5037}
5038
5039 void
5040write_viminfo_bufferlist(fp)
5041 FILE *fp;
5042{
5043 buf_T *buf;
5044#ifdef FEAT_WINDOWS
5045 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005046 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047#endif
5048 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005049 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050
5051 if (find_viminfo_parameter('%') == NULL)
5052 return;
5053
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005054 /* Without a number -1 is returned: do all buffers. */
5055 max_buffers = get_viminfo_parameter('%');
5056
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005058#define LINE_BUF_LEN (MAXPATHL + 40)
5059 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 if (line == NULL)
5061 return;
5062
5063#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005064 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 set_last_cursor(win);
5066#else
5067 set_last_cursor(curwin);
5068#endif
5069
5070 fprintf(fp, _("\n# Buffer list:\n"));
5071 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5072 {
5073 if (buf->b_fname == NULL
5074 || !buf->b_p_bl
5075#ifdef FEAT_QUICKFIX
5076 || bt_quickfix(buf)
5077#endif
5078 || removable(buf->b_ffname))
5079 continue;
5080
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005081 if (max_buffers-- == 0)
5082 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 putc('%', fp);
5084 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005085 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 (long)buf->b_last_cursor.lnum,
5087 buf->b_last_cursor.col);
5088 viminfo_writestring(fp, line);
5089 }
5090 vim_free(line);
5091}
5092#endif
5093
5094
5095/*
5096 * Return special buffer name.
5097 * Returns NULL when the buffer has a normal file name.
5098 */
5099 char *
5100buf_spname(buf)
5101 buf_T *buf;
5102{
5103#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5104 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005105 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005106 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005107 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005108
5109 /*
5110 * For location list window, w_llist_ref points to the location list.
5111 * For quickfix window, w_llist_ref is NULL.
5112 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005113 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005114 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005115 goto win_found;
5116win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005117 if (win != NULL && win->w_llist_ref != NULL)
5118 return _("[Location List]");
5119 else
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005120 return _("[Quickfix List]");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122#endif
5123#ifdef FEAT_QUICKFIX
5124 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5125 * contains the name as specified by the user */
5126 if (bt_nofile(buf))
5127 {
5128 if (buf->b_sfname != NULL)
5129 return (char *)buf->b_sfname;
Bram Moolenaarf4f664c2008-12-03 10:21:57 +00005130 return _("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 }
5132#endif
5133 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005134 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 return NULL;
5136}
5137
5138
5139#if defined(FEAT_SIGNS) || defined(PROTO)
5140/*
5141 * Insert the sign into the signlist.
5142 */
5143 static void
5144insert_sign(buf, prev, next, id, lnum, typenr)
5145 buf_T *buf; /* buffer to store sign in */
5146 signlist_T *prev; /* previous sign entry */
5147 signlist_T *next; /* next sign entry */
5148 int id; /* sign ID */
5149 linenr_T lnum; /* line number which gets the mark */
5150 int typenr; /* typenr of sign we are adding */
5151{
5152 signlist_T *newsign;
5153
5154 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5155 if (newsign != NULL)
5156 {
5157 newsign->id = id;
5158 newsign->lnum = lnum;
5159 newsign->typenr = typenr;
5160 newsign->next = next;
5161#ifdef FEAT_NETBEANS_INTG
5162 newsign->prev = prev;
5163 if (next != NULL)
5164 next->prev = newsign;
5165#endif
5166
5167 if (prev == NULL)
5168 {
5169 /* When adding first sign need to redraw the windows to create the
5170 * column for signs. */
5171 if (buf->b_signlist == NULL)
5172 {
5173 redraw_buf_later(buf, NOT_VALID);
5174 changed_cline_bef_curs();
5175 }
5176
5177 /* first sign in signlist */
5178 buf->b_signlist = newsign;
5179 }
5180 else
5181 prev->next = newsign;
5182 }
5183}
5184
5185/*
5186 * Add the sign into the signlist. Find the right spot to do it though.
5187 */
5188 void
5189buf_addsign(buf, id, lnum, typenr)
5190 buf_T *buf; /* buffer to store sign in */
5191 int id; /* sign ID */
5192 linenr_T lnum; /* line number which gets the mark */
5193 int typenr; /* typenr of sign we are adding */
5194{
5195 signlist_T *sign; /* a sign in the signlist */
5196 signlist_T *prev; /* the previous sign */
5197
5198 prev = NULL;
5199 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5200 {
5201 if (lnum == sign->lnum && id == sign->id)
5202 {
5203 sign->typenr = typenr;
5204 return;
5205 }
5206 else if (
5207#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5208 id < 0 &&
5209#endif
5210 lnum < sign->lnum)
5211 {
5212#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5213 /* XXX - GRP: Is this because of sign slide problem? Or is it
5214 * really needed? Or is it because we allow multiple signs per
5215 * line? If so, should I add that feature to FEAT_SIGNS?
5216 */
5217 while (prev != NULL && prev->lnum == lnum)
5218 prev = prev->prev;
5219 if (prev == NULL)
5220 sign = buf->b_signlist;
5221 else
5222 sign = prev->next;
5223#endif
5224 insert_sign(buf, prev, sign, id, lnum, typenr);
5225 return;
5226 }
5227 prev = sign;
5228 }
5229#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5230 /* XXX - GRP: See previous comment */
5231 while (prev != NULL && prev->lnum == lnum)
5232 prev = prev->prev;
5233 if (prev == NULL)
5234 sign = buf->b_signlist;
5235 else
5236 sign = prev->next;
5237#endif
5238 insert_sign(buf, prev, sign, id, lnum, typenr);
5239
5240 return;
5241}
5242
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005243 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244buf_change_sign_type(buf, markId, typenr)
5245 buf_T *buf; /* buffer to store sign in */
5246 int markId; /* sign ID */
5247 int typenr; /* typenr of sign we are adding */
5248{
5249 signlist_T *sign; /* a sign in the signlist */
5250
5251 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5252 {
5253 if (sign->id == markId)
5254 {
5255 sign->typenr = typenr;
5256 return sign->lnum;
5257 }
5258 }
5259
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005260 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261}
5262
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005263 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264buf_getsigntype(buf, lnum, type)
5265 buf_T *buf;
5266 linenr_T lnum;
5267 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5268{
5269 signlist_T *sign; /* a sign in a b_signlist */
5270
5271 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5272 if (sign->lnum == lnum
5273 && (type == SIGN_ANY
5274# ifdef FEAT_SIGN_ICONS
5275 || (type == SIGN_ICON
5276 && sign_get_image(sign->typenr) != NULL)
5277# endif
5278 || (type == SIGN_TEXT
5279 && sign_get_text(sign->typenr) != NULL)
5280 || (type == SIGN_LINEHL
5281 && sign_get_attr(sign->typenr, TRUE) != 0)))
5282 return sign->typenr;
5283 return 0;
5284}
5285
5286
5287 linenr_T
5288buf_delsign(buf, id)
5289 buf_T *buf; /* buffer sign is stored in */
5290 int id; /* sign id */
5291{
5292 signlist_T **lastp; /* pointer to pointer to current sign */
5293 signlist_T *sign; /* a sign in a b_signlist */
5294 signlist_T *next; /* the next sign in a b_signlist */
5295 linenr_T lnum; /* line number whose sign was deleted */
5296
5297 lastp = &buf->b_signlist;
5298 lnum = 0;
5299 for (sign = buf->b_signlist; sign != NULL; sign = next)
5300 {
5301 next = sign->next;
5302 if (sign->id == id)
5303 {
5304 *lastp = next;
5305#ifdef FEAT_NETBEANS_INTG
5306 if (next != NULL)
5307 next->prev = sign->prev;
5308#endif
5309 lnum = sign->lnum;
5310 vim_free(sign);
5311 break;
5312 }
5313 else
5314 lastp = &sign->next;
5315 }
5316
5317 /* When deleted the last sign need to redraw the windows to remove the
5318 * sign column. */
5319 if (buf->b_signlist == NULL)
5320 {
5321 redraw_buf_later(buf, NOT_VALID);
5322 changed_cline_bef_curs();
5323 }
5324
5325 return lnum;
5326}
5327
5328
5329/*
5330 * Find the line number of the sign with the requested id. If the sign does
5331 * not exist, return 0 as the line number. This will still let the correct file
5332 * get loaded.
5333 */
5334 int
5335buf_findsign(buf, id)
5336 buf_T *buf; /* buffer to store sign in */
5337 int id; /* sign ID */
5338{
5339 signlist_T *sign; /* a sign in the signlist */
5340
5341 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5342 if (sign->id == id)
5343 return sign->lnum;
5344
5345 return 0;
5346}
5347
5348 int
5349buf_findsign_id(buf, lnum)
5350 buf_T *buf; /* buffer whose sign we are searching for */
5351 linenr_T lnum; /* line number of sign */
5352{
5353 signlist_T *sign; /* a sign in the signlist */
5354
5355 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5356 if (sign->lnum == lnum)
5357 return sign->id;
5358
5359 return 0;
5360}
5361
5362
5363# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5364/* see if a given type of sign exists on a specific line */
5365 int
5366buf_findsigntype_id(buf, lnum, typenr)
5367 buf_T *buf; /* buffer whose sign we are searching for */
5368 linenr_T lnum; /* line number of sign */
5369 int typenr; /* sign type number */
5370{
5371 signlist_T *sign; /* a sign in the signlist */
5372
5373 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5374 if (sign->lnum == lnum && sign->typenr == typenr)
5375 return sign->id;
5376
5377 return 0;
5378}
5379
5380
5381# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5382/* return the number of icons on the given line */
5383 int
5384buf_signcount(buf, lnum)
5385 buf_T *buf;
5386 linenr_T lnum;
5387{
5388 signlist_T *sign; /* a sign in the signlist */
5389 int count = 0;
5390
5391 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5392 if (sign->lnum == lnum)
5393 if (sign_get_image(sign->typenr) != NULL)
5394 count++;
5395
5396 return count;
5397}
5398# endif /* FEAT_SIGN_ICONS */
5399# endif /* FEAT_NETBEANS_INTG */
5400
5401
5402/*
5403 * Delete signs in buffer "buf".
5404 */
5405 static void
5406buf_delete_signs(buf)
5407 buf_T *buf;
5408{
5409 signlist_T *next;
5410
5411 while (buf->b_signlist != NULL)
5412 {
5413 next = buf->b_signlist->next;
5414 vim_free(buf->b_signlist);
5415 buf->b_signlist = next;
5416 }
5417}
5418
5419/*
5420 * Delete all signs in all buffers.
5421 */
5422 void
5423buf_delete_all_signs()
5424{
5425 buf_T *buf; /* buffer we are checking for signs */
5426
5427 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5428 if (buf->b_signlist != NULL)
5429 {
5430 /* Need to redraw the windows to remove the sign column. */
5431 redraw_buf_later(buf, NOT_VALID);
5432 buf_delete_signs(buf);
5433 }
5434}
5435
5436/*
5437 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5438 */
5439 void
5440sign_list_placed(rbuf)
5441 buf_T *rbuf;
5442{
5443 buf_T *buf;
5444 signlist_T *p;
5445 char lbuf[BUFSIZ];
5446
5447 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5448 msg_putchar('\n');
5449 if (rbuf == NULL)
5450 buf = firstbuf;
5451 else
5452 buf = rbuf;
5453 while (buf != NULL)
5454 {
5455 if (buf->b_signlist != NULL)
5456 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005457 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5459 msg_putchar('\n');
5460 }
5461 for (p = buf->b_signlist; p != NULL; p = p->next)
5462 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005463 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5465 MSG_PUTS(lbuf);
5466 msg_putchar('\n');
5467 }
5468 if (rbuf != NULL)
5469 break;
5470 buf = buf->b_next;
5471 }
5472}
5473
5474/*
5475 * Adjust a placed sign for inserted/deleted lines.
5476 */
5477 void
5478sign_mark_adjust(line1, line2, amount, amount_after)
5479 linenr_T line1;
5480 linenr_T line2;
5481 long amount;
5482 long amount_after;
5483{
5484 signlist_T *sign; /* a sign in a b_signlist */
5485
5486 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5487 {
5488 if (sign->lnum >= line1 && sign->lnum <= line2)
5489 {
5490 if (amount == MAXLNUM)
5491 sign->lnum = line1;
5492 else
5493 sign->lnum += amount;
5494 }
5495 else if (sign->lnum > line2)
5496 sign->lnum += amount_after;
5497 }
5498}
5499#endif /* FEAT_SIGNS */
5500
5501/*
5502 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5503 */
5504 void
5505set_buflisted(on)
5506 int on;
5507{
5508 if (on != curbuf->b_p_bl)
5509 {
5510 curbuf->b_p_bl = on;
5511#ifdef FEAT_AUTOCMD
5512 if (on)
5513 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5514 else
5515 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5516#endif
5517 }
5518}
5519
5520/*
5521 * Read the file for "buf" again and check if the contents changed.
5522 * Return TRUE if it changed or this could not be checked.
5523 */
5524 int
5525buf_contents_changed(buf)
5526 buf_T *buf;
5527{
5528 buf_T *newbuf;
5529 int differ = TRUE;
5530 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 exarg_T ea;
5533
5534 /* Allocate a buffer without putting it in the buffer list. */
5535 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5536 if (newbuf == NULL)
5537 return TRUE;
5538
5539 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5540 if (prep_exarg(&ea, buf) == FAIL)
5541 {
5542 wipe_buffer(newbuf, FALSE);
5543 return TRUE;
5544 }
5545
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 /* set curwin/curbuf to buf and save a few things */
5547 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548
Bram Moolenaar4770d092006-01-12 23:22:24 +00005549 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 && readfile(buf->b_ffname, buf->b_fname,
5551 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5552 &ea, READ_NEW | READ_DUMMY) == OK)
5553 {
5554 /* compare the two files line by line */
5555 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5556 {
5557 differ = FALSE;
5558 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5559 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5560 {
5561 differ = TRUE;
5562 break;
5563 }
5564 }
5565 }
5566 vim_free(ea.cmd);
5567
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 /* restore curwin/curbuf and a few other things */
5569 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570
5571 if (curbuf != newbuf) /* safety check */
5572 wipe_buffer(newbuf, FALSE);
5573
5574 return differ;
5575}
5576
5577/*
5578 * Wipe out a buffer and decrement the last buffer number if it was used for
5579 * this buffer. Call this to wipe out a temp buffer that does not contain any
5580 * marks.
5581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 void
5583wipe_buffer(buf, aucmd)
5584 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005585 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586{
5587 if (buf->b_fnum == top_file_num - 1)
5588 --top_file_num;
5589
5590#ifdef FEAT_AUTOCMD
5591 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005592 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593#endif
5594 close_buffer(NULL, buf, DOBUF_WIPE);
5595#ifdef FEAT_AUTOCMD
5596 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005597 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598#endif
5599}