blob: 5ecb63872732f9f8ca9344cd6c36e23d640e97dd [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
584 syntax_clear(buf); /* reset syntax info */
585#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 Moolenaar325b7a22004-07-05 15:58:32 +0000598#ifdef FEAT_MZSCHEME
599 mzscheme_buffer_free(buf);
600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601#ifdef FEAT_PERL
602 perl_buf_free(buf);
603#endif
604#ifdef FEAT_PYTHON
605 python_buffer_free(buf);
606#endif
607#ifdef FEAT_RUBY
608 ruby_buffer_free(buf);
609#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000610#ifdef FEAT_AUTOCMD
611 aubuflocal_remove(buf);
612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 vim_free(buf);
614}
615
616/*
617 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
618 */
619 static void
620free_buffer_stuff(buf, free_options)
621 buf_T *buf;
622 int free_options; /* free options as well */
623{
624 if (free_options)
625 {
626 clear_wininfo(buf); /* including window-local options */
627 free_buf_options(buf, TRUE);
628 }
629#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000630 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
631 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632#endif
633#ifdef FEAT_USR_CMDS
634 uc_clear(&buf->b_ucmds); /* clear local user commands */
635#endif
636#ifdef FEAT_SIGNS
637 buf_delete_signs(buf); /* delete any signs */
638#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000639#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200640 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642#ifdef FEAT_LOCALMAP
643 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
644 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
645#endif
646#ifdef FEAT_MBYTE
647 vim_free(buf->b_start_fenc);
648 buf->b_start_fenc = NULL;
649#endif
Bram Moolenaar9381ab72008-11-12 11:52:19 +0000650#ifdef FEAT_SPELL
651 ga_clear(&buf->b_langp);
652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653}
654
655/*
656 * Free the b_wininfo list for buffer "buf".
657 */
658 static void
659clear_wininfo(buf)
660 buf_T *buf;
661{
662 wininfo_T *wip;
663
664 while (buf->b_wininfo != NULL)
665 {
666 wip = buf->b_wininfo;
667 buf->b_wininfo = wip->wi_next;
668 if (wip->wi_optset)
669 {
670 clear_winopt(&wip->wi_opt);
671#ifdef FEAT_FOLDING
672 deleteFoldRecurse(&wip->wi_folds);
673#endif
674 }
675 vim_free(wip);
676 }
677}
678
679#if defined(FEAT_LISTCMDS) || defined(PROTO)
680/*
681 * Go to another buffer. Handles the result of the ATTENTION dialog.
682 */
683 void
684goto_buffer(eap, start, dir, count)
685 exarg_T *eap;
686 int start;
687 int dir;
688 int count;
689{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000690# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 buf_T *old_curbuf = curbuf;
692
693 swap_exists_action = SEA_DIALOG;
694# endif
695 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
696 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000697# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
699 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000700# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
701 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000702
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000703 /* Reset the error/interrupt/exception state here so that
704 * aborting() returns FALSE when closing a window. */
705 enter_cleanup(&cs);
706# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000707
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000708 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 win_close(curwin, TRUE);
710 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000711 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000712
713# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
714 /* Restore the error/interrupt/exception state if not discarded by a
715 * new aborting error, interrupt, or uncaught exception. */
716 leave_cleanup(&cs);
717# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719 else
720 handle_swap_exists(old_curbuf);
721# endif
722}
723#endif
724
Bram Moolenaare64ac772005-12-07 20:54:59 +0000725#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726/*
727 * Handle the situation of swap_exists_action being set.
728 * It is allowed for "old_curbuf" to be NULL or invalid.
729 */
730 void
731handle_swap_exists(old_curbuf)
732 buf_T *old_curbuf;
733{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000734# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
735 cleanup_T cs;
736# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 if (swap_exists_action == SEA_QUIT)
739 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000740# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
741 /* Reset the error/interrupt/exception state here so that
742 * aborting() returns FALSE when closing a buffer. */
743 enter_cleanup(&cs);
744# endif
745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 /* User selected Quit at ATTENTION prompt. Go back to previous
747 * buffer. If that buffer is gone or the same as the current one,
748 * open a new, empty buffer. */
749 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000750 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
752 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000753 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 if (old_curbuf != NULL)
755 enter_buffer(old_curbuf);
756 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000757
758# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
759 /* Restore the error/interrupt/exception state if not discarded by a
760 * new aborting error, interrupt, or uncaught exception. */
761 leave_cleanup(&cs);
762# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 }
764 else if (swap_exists_action == SEA_RECOVER)
765 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000766# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
767 /* Reset the error/interrupt/exception state here so that
768 * aborting() returns FALSE when closing a buffer. */
769 enter_cleanup(&cs);
770# endif
771
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 /* User selected Recover at ATTENTION prompt. */
773 msg_scroll = TRUE;
774 ml_recover();
775 MSG_PUTS("\n"); /* don't overwrite the last message */
776 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000777 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000778
779# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
780 /* Restore the error/interrupt/exception state if not discarded by a
781 * new aborting error, interrupt, or uncaught exception. */
782 leave_cleanup(&cs);
783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785 swap_exists_action = SEA_NONE;
786}
787#endif
788
789#if defined(FEAT_LISTCMDS) || defined(PROTO)
790/*
791 * do_bufdel() - delete or unload buffer(s)
792 *
793 * addr_count == 0: ":bdel" - delete current buffer
794 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
795 * buffer "end_bnr", then any other arguments.
796 * addr_count == 2: ":N,N bdel" - delete buffers in range
797 *
798 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
799 * DOBUF_DEL (":bdel")
800 *
801 * Returns error message or NULL
802 */
803 char_u *
804do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
805 int command;
806 char_u *arg; /* pointer to extra arguments */
807 int addr_count;
808 int start_bnr; /* first buffer number in a range */
809 int end_bnr; /* buffer nr or last buffer nr in a range */
810 int forceit;
811{
812 int do_current = 0; /* delete current buffer? */
813 int deleted = 0; /* number of buffers deleted */
814 char_u *errormsg = NULL; /* return value */
815 int bnr; /* buffer number */
816 char_u *p;
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 if (addr_count == 0)
819 {
820 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
821 }
822 else
823 {
824 if (addr_count == 2)
825 {
826 if (*arg) /* both range and argument is not allowed */
827 return (char_u *)_(e_trailing);
828 bnr = start_bnr;
829 }
830 else /* addr_count == 1 */
831 bnr = end_bnr;
832
833 for ( ;!got_int; ui_breakcheck())
834 {
835 /*
836 * delete the current buffer last, otherwise when the
837 * current buffer is deleted, the next buffer becomes
838 * the current one and will be loaded, which may then
839 * also be deleted, etc.
840 */
841 if (bnr == curbuf->b_fnum)
842 do_current = bnr;
843 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
844 forceit) == OK)
845 ++deleted;
846
847 /*
848 * find next buffer number to delete/unload
849 */
850 if (addr_count == 2)
851 {
852 if (++bnr > end_bnr)
853 break;
854 }
855 else /* addr_count == 1 */
856 {
857 arg = skipwhite(arg);
858 if (*arg == NUL)
859 break;
860 if (!VIM_ISDIGIT(*arg))
861 {
862 p = skiptowhite_esc(arg);
863 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
864 if (bnr < 0) /* failed */
865 break;
866 arg = p;
867 }
868 else
869 bnr = getdigits(&arg);
870 }
871 }
872 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
873 FORWARD, do_current, forceit) == OK)
874 ++deleted;
875
876 if (deleted == 0)
877 {
878 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000879 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000881 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000883 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 errormsg = IObuff;
885 }
886 else if (deleted >= p_report)
887 {
888 if (command == DOBUF_UNLOAD)
889 {
890 if (deleted == 1)
891 MSG(_("1 buffer unloaded"));
892 else
893 smsg((char_u *)_("%d buffers unloaded"), deleted);
894 }
895 else if (command == DOBUF_DEL)
896 {
897 if (deleted == 1)
898 MSG(_("1 buffer deleted"));
899 else
900 smsg((char_u *)_("%d buffers deleted"), deleted);
901 }
902 else
903 {
904 if (deleted == 1)
905 MSG(_("1 buffer wiped out"));
906 else
907 smsg((char_u *)_("%d buffers wiped out"), deleted);
908 }
909 }
910 }
911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
913 return errormsg;
914}
915
916/*
917 * Implementation of the commands for the buffer list.
918 *
919 * action == DOBUF_GOTO go to specified buffer
920 * action == DOBUF_SPLIT split window and go to specified buffer
921 * action == DOBUF_UNLOAD unload specified buffer(s)
922 * action == DOBUF_DEL delete specified buffer(s) from buffer list
923 * action == DOBUF_WIPE delete specified buffer(s) really
924 *
925 * start == DOBUF_CURRENT go to "count" buffer from current buffer
926 * start == DOBUF_FIRST go to "count" buffer from first buffer
927 * start == DOBUF_LAST go to "count" buffer from last buffer
928 * start == DOBUF_MOD go to "count" modified buffer from current buffer
929 *
930 * Return FAIL or OK.
931 */
932 int
933do_buffer(action, start, dir, count, forceit)
934 int action;
935 int start;
936 int dir; /* FORWARD or BACKWARD */
937 int count; /* buffer number or number of buffers */
938 int forceit; /* TRUE for :...! */
939{
940 buf_T *buf;
941 buf_T *bp;
942 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
943 || action == DOBUF_WIPE);
944
945 switch (start)
946 {
947 case DOBUF_FIRST: buf = firstbuf; break;
948 case DOBUF_LAST: buf = lastbuf; break;
949 default: buf = curbuf; break;
950 }
951 if (start == DOBUF_MOD) /* find next modified buffer */
952 {
953 while (count-- > 0)
954 {
955 do
956 {
957 buf = buf->b_next;
958 if (buf == NULL)
959 buf = firstbuf;
960 }
961 while (buf != curbuf && !bufIsChanged(buf));
962 }
963 if (!bufIsChanged(buf))
964 {
965 EMSG(_("E84: No modified buffer found"));
966 return FAIL;
967 }
968 }
969 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
970 {
971 while (buf != NULL && buf->b_fnum != count)
972 buf = buf->b_next;
973 }
974 else
975 {
976 bp = NULL;
977 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
978 {
979 /* remember the buffer where we start, we come back there when all
980 * buffers are unlisted. */
981 if (bp == NULL)
982 bp = buf;
983 if (dir == FORWARD)
984 {
985 buf = buf->b_next;
986 if (buf == NULL)
987 buf = firstbuf;
988 }
989 else
990 {
991 buf = buf->b_prev;
992 if (buf == NULL)
993 buf = lastbuf;
994 }
995 /* don't count unlisted buffers */
996 if (unload || buf->b_p_bl)
997 {
998 --count;
999 bp = NULL; /* use this buffer as new starting point */
1000 }
1001 if (bp == buf)
1002 {
1003 /* back where we started, didn't find anything. */
1004 EMSG(_("E85: There is no listed buffer"));
1005 return FAIL;
1006 }
1007 }
1008 }
1009
1010 if (buf == NULL) /* could not find it */
1011 {
1012 if (start == DOBUF_FIRST)
1013 {
1014 /* don't warn when deleting */
1015 if (!unload)
1016 EMSGN(_("E86: Buffer %ld does not exist"), count);
1017 }
1018 else if (dir == FORWARD)
1019 EMSG(_("E87: Cannot go beyond last buffer"));
1020 else
1021 EMSG(_("E88: Cannot go before first buffer"));
1022 return FAIL;
1023 }
1024
1025#ifdef FEAT_GUI
1026 need_mouse_correct = TRUE;
1027#endif
1028
1029#ifdef FEAT_LISTCMDS
1030 /*
1031 * delete buffer buf from memory and/or the list
1032 */
1033 if (unload)
1034 {
1035 int forward;
1036 int retval;
1037
1038 /* When unloading or deleting a buffer that's already unloaded and
1039 * unlisted: fail silently. */
1040 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1041 return FAIL;
1042
1043 if (!forceit && bufIsChanged(buf))
1044 {
1045#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1046 if ((p_confirm || cmdmod.confirm) && p_write)
1047 {
1048 dialog_changed(buf, FALSE);
1049# ifdef FEAT_AUTOCMD
1050 if (!buf_valid(buf))
1051 /* Autocommand deleted buffer, oops! It's not changed
1052 * now. */
1053 return FAIL;
1054# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001055 /* If it's still changed fail silently, the dialog already
1056 * mentioned why it fails. */
1057 if (bufIsChanged(buf))
1058 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001060 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061#endif
1062 {
1063 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1064 buf->b_fnum);
1065 return FAIL;
1066 }
1067 }
1068
1069 /*
1070 * If deleting the last (listed) buffer, make it empty.
1071 * The last (listed) buffer cannot be unloaded.
1072 */
1073 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1074 if (bp->b_p_bl && bp != buf)
1075 break;
1076 if (bp == NULL && buf == curbuf)
1077 {
1078 if (action == DOBUF_UNLOAD)
1079 {
1080 EMSG(_("E90: Cannot unload last buffer"));
1081 return FAIL;
1082 }
1083
1084 /* Close any other windows on this buffer, then make it empty. */
1085#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001086 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087#endif
1088 setpcmark();
1089 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001090 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091
1092 /*
1093 * do_ecmd() may create a new buffer, then we have to delete
1094 * the old one. But do_ecmd() may have done that already, check
1095 * if the buffer still exists.
1096 */
1097 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1098 close_buffer(NULL, buf, action);
1099 return retval;
1100 }
1101
1102#ifdef FEAT_WINDOWS
1103 /*
1104 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001105 * (unless it's the only window). Repeat this so long as we end up in
1106 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001108 while (buf == curbuf
1109 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 win_close(curwin, FALSE);
1111#endif
1112
1113 /*
1114 * If the buffer to be deleted is not the current one, delete it here.
1115 */
1116 if (buf != curbuf)
1117 {
1118#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001119 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120#endif
1121 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1122 close_buffer(NULL, buf, action);
1123 return OK;
1124 }
1125
1126 /*
1127 * Deleting the current buffer: Need to find another buffer to go to.
1128 * There must be another, otherwise it would have been handled above.
1129 * First use au_new_curbuf, if it is valid.
1130 * Then prefer the buffer we most recently visited.
1131 * Else try to find one that is loaded, after the current buffer,
1132 * then before the current buffer.
1133 * Finally use any buffer.
1134 */
1135 buf = NULL; /* selected buffer */
1136 bp = NULL; /* used when no loaded buffer found */
1137#ifdef FEAT_AUTOCMD
1138 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1139 buf = au_new_curbuf;
1140# ifdef FEAT_JUMPLIST
1141 else
1142# endif
1143#endif
1144#ifdef FEAT_JUMPLIST
1145 if (curwin->w_jumplistlen > 0)
1146 {
1147 int jumpidx;
1148
1149 jumpidx = curwin->w_jumplistidx - 1;
1150 if (jumpidx < 0)
1151 jumpidx = curwin->w_jumplistlen - 1;
1152
1153 forward = jumpidx;
1154 while (jumpidx != curwin->w_jumplistidx)
1155 {
1156 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1157 if (buf != NULL)
1158 {
1159 if (buf == curbuf || !buf->b_p_bl)
1160 buf = NULL; /* skip current and unlisted bufs */
1161 else if (buf->b_ml.ml_mfp == NULL)
1162 {
1163 /* skip unloaded buf, but may keep it for later */
1164 if (bp == NULL)
1165 bp = buf;
1166 buf = NULL;
1167 }
1168 }
1169 if (buf != NULL) /* found a valid buffer: stop searching */
1170 break;
1171 /* advance to older entry in jump list */
1172 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1173 break;
1174 if (--jumpidx < 0)
1175 jumpidx = curwin->w_jumplistlen - 1;
1176 if (jumpidx == forward) /* List exhausted for sure */
1177 break;
1178 }
1179 }
1180#endif
1181
1182 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1183 {
1184 forward = TRUE;
1185 buf = curbuf->b_next;
1186 for (;;)
1187 {
1188 if (buf == NULL)
1189 {
1190 if (!forward) /* tried both directions */
1191 break;
1192 buf = curbuf->b_prev;
1193 forward = FALSE;
1194 continue;
1195 }
1196 /* in non-help buffer, try to skip help buffers, and vv */
1197 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1198 {
1199 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1200 break;
1201 if (bp == NULL) /* remember unloaded buf for later */
1202 bp = buf;
1203 }
1204 if (forward)
1205 buf = buf->b_next;
1206 else
1207 buf = buf->b_prev;
1208 }
1209 }
1210 if (buf == NULL) /* No loaded buffer, use unloaded one */
1211 buf = bp;
1212 if (buf == NULL) /* No loaded buffer, find listed one */
1213 {
1214 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1215 if (buf->b_p_bl && buf != curbuf)
1216 break;
1217 }
1218 if (buf == NULL) /* Still no buffer, just take one */
1219 {
1220 if (curbuf->b_next != NULL)
1221 buf = curbuf->b_next;
1222 else
1223 buf = curbuf->b_prev;
1224 }
1225 }
1226
1227 /*
1228 * make buf current buffer
1229 */
1230 if (action == DOBUF_SPLIT) /* split window first */
1231 {
1232# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001233 /* If 'switchbuf' contains "useopen": jump to first window containing
1234 * "buf" if one exists */
1235 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001236 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001237 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001238 * page containing "buf" if one exists */
1239 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 return OK;
1241 if (win_split(0, 0) == FAIL)
1242# endif
1243 return FAIL;
1244 }
1245#endif
1246
1247 /* go to current buffer - nothing to do */
1248 if (buf == curbuf)
1249 return OK;
1250
1251 /*
1252 * Check if the current buffer may be abandoned.
1253 */
1254 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1255 {
1256#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1257 if ((p_confirm || cmdmod.confirm) && p_write)
1258 {
1259 dialog_changed(curbuf, FALSE);
1260# ifdef FEAT_AUTOCMD
1261 if (!buf_valid(buf))
1262 /* Autocommand deleted buffer, oops! */
1263 return FAIL;
1264# endif
1265 }
1266 if (bufIsChanged(curbuf))
1267#endif
1268 {
1269 EMSG(_(e_nowrtmsg));
1270 return FAIL;
1271 }
1272 }
1273
1274 /* Go to the other buffer. */
1275 set_curbuf(buf, action);
1276
1277#if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1278 if (action == DOBUF_SPLIT)
1279 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1280#endif
1281
1282#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1283 if (aborting()) /* autocmds may abort script processing */
1284 return FAIL;
1285#endif
1286
1287 return OK;
1288}
1289
1290#endif /* FEAT_LISTCMDS */
1291
1292/*
1293 * Set current buffer to "buf". Executes autocommands and closes current
1294 * buffer. "action" tells how to close the current buffer:
1295 * DOBUF_GOTO free or hide it
1296 * DOBUF_SPLIT nothing
1297 * DOBUF_UNLOAD unload it
1298 * DOBUF_DEL delete it
1299 * DOBUF_WIPE wipe it out
1300 */
1301 void
1302set_curbuf(buf, action)
1303 buf_T *buf;
1304 int action;
1305{
1306 buf_T *prevbuf;
1307 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1308 || action == DOBUF_WIPE);
1309
1310 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001311 if (!cmdmod.keepalt)
1312 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001313 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314
1315#ifdef FEAT_VISUAL
1316 /* Don't restart Select mode after switching to another buffer. */
1317 VIsual_reselect = FALSE;
1318#endif
1319
1320 /* close_windows() or apply_autocmds() may change curbuf */
1321 prevbuf = curbuf;
1322
1323#ifdef FEAT_AUTOCMD
1324 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1325# ifdef FEAT_EVAL
1326 if (buf_valid(prevbuf) && !aborting())
1327# else
1328 if (buf_valid(prevbuf))
1329# endif
1330#endif
1331 {
1332#ifdef FEAT_WINDOWS
1333 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001334 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335#endif
1336#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1337 if (buf_valid(prevbuf) && !aborting())
1338#else
1339 if (buf_valid(prevbuf))
1340#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001341 {
1342 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001343 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1345 unload ? action : (action == DOBUF_GOTO
1346 && !P_HID(prevbuf)
1347 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001351 /* An autocommand may have deleted "buf", already entered it (e.g., when
1352 * it did ":bunload") or aborted the script processing! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353# ifdef FEAT_EVAL
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001354 if (buf_valid(buf) && buf != curbuf && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355# else
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001356 if (buf_valid(buf) && buf != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357# endif
1358#endif
1359 enter_buffer(buf);
1360}
1361
1362/*
1363 * Enter a new current buffer.
1364 * Old curbuf must have been abandoned already!
1365 */
1366 void
1367enter_buffer(buf)
1368 buf_T *buf;
1369{
1370 /* Copy buffer and window local option values. Not for a help buffer. */
1371 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1372 if (!buf->b_help)
1373 get_winopts(buf);
1374#ifdef FEAT_FOLDING
1375 else
1376 /* Remove all folds in the window. */
1377 clearFolding(curwin);
1378 foldUpdateAll(curwin); /* update folds (later). */
1379#endif
1380
1381 /* Get the buffer in the current window. */
1382 curwin->w_buffer = buf;
1383 curbuf = buf;
1384 ++curbuf->b_nwindows;
1385
1386#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001387 if (curwin->w_p_diff)
1388 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389#endif
1390
1391 /* Cursor on first line by default. */
1392 curwin->w_cursor.lnum = 1;
1393 curwin->w_cursor.col = 0;
1394#ifdef FEAT_VIRTUALEDIT
1395 curwin->w_cursor.coladd = 0;
1396#endif
1397 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001398#ifdef FEAT_AUTOCMD
1399 curwin->w_topline_was_set = FALSE;
1400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001402 /* mark cursor position as being invalid */
1403 curwin->w_valid = 0;
1404
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 /* Make sure the buffer is loaded. */
1406 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001407 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001408#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001409 /* If there is no filetype, allow for detecting one. Esp. useful for
1410 * ":ball" used in a autocommand. If there already is a filetype we
1411 * might prefer to keep it. */
1412 if (*curbuf->b_p_ft == NUL)
1413 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001414#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 open_buffer(FALSE, NULL);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 else
1419 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001420 if (!msg_silent)
1421 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1423#ifdef FEAT_AUTOCMD
1424 curwin->w_topline = 1;
1425# ifdef FEAT_DIFF
1426 curwin->w_topfill = 0;
1427# endif
1428 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1429 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1430#endif
1431 }
1432
1433 /* If autocommands did not change the cursor position, restore cursor lnum
1434 * and possibly cursor col. */
1435 if (curwin->w_cursor.lnum == 1 && inindent(0))
1436 buflist_getfpos();
1437
1438 check_arg_idx(curwin); /* check for valid arg_idx */
1439#ifdef FEAT_TITLE
1440 maketitle();
1441#endif
1442#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001443 /* when autocmds didn't change it */
1444 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445#endif
1446 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1447
Bram Moolenaar009b2592004-10-24 19:18:58 +00001448#ifdef FEAT_NETBEANS_INTG
1449 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001450 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001451#endif
1452
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001453 /* Change directories when the 'acd' option is set. */
1454 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
1456#ifdef FEAT_KEYMAP
1457 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001458 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001460#ifdef FEAT_SPELL
1461 /* May need to set the spell language. Can only do this after the buffer
1462 * has been properly setup. */
1463 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001464 (void)did_set_spelllang(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001465#endif
1466
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 redraw_later(NOT_VALID);
1468}
1469
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001470#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1471/*
1472 * Change to the directory of the current buffer.
1473 */
1474 void
1475do_autochdir()
1476{
1477 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1478 shorten_fnames(TRUE);
1479}
1480#endif
1481
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482/*
1483 * functions for dealing with the buffer list
1484 */
1485
1486/*
1487 * Add a file name to the buffer list. Return a pointer to the buffer.
1488 * If the same file name already exists return a pointer to that buffer.
1489 * If it does not exist, or if fname == NULL, a new entry is created.
1490 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1491 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1492 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 * This is the ONLY way to create a new buffer.
1494 */
1495static int top_file_num = 1; /* highest file number */
1496
1497 buf_T *
1498buflist_new(ffname, sfname, lnum, flags)
1499 char_u *ffname; /* full path of fname or relative */
1500 char_u *sfname; /* short fname or NULL */
1501 linenr_T lnum; /* preferred cursor line */
1502 int flags; /* BLN_ defines */
1503{
1504 buf_T *buf;
1505#ifdef UNIX
1506 struct stat st;
1507#endif
1508
1509 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1510
1511 /*
1512 * If file name already exists in the list, update the entry.
1513 */
1514#ifdef UNIX
1515 /* On Unix we can use inode numbers when the file exists. Works better
1516 * for hard links. */
1517 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1518 st.st_dev = (dev_T)-1;
1519#endif
1520 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1521#ifdef UNIX
1522 buflist_findname_stat(ffname, &st)
1523#else
1524 buflist_findname(ffname)
1525#endif
1526 ) != NULL)
1527 {
1528 vim_free(ffname);
1529 if (lnum != 0)
1530 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1531 /* copy the options now, if 'cpo' doesn't have 's' and not done
1532 * already */
1533 buf_copy_options(buf, 0);
1534 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1535 {
1536 buf->b_p_bl = TRUE;
1537#ifdef FEAT_AUTOCMD
1538 if (!(flags & BLN_DUMMY))
1539 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1540#endif
1541 }
1542 return buf;
1543 }
1544
1545 /*
1546 * If the current buffer has no name and no contents, use the current
1547 * buffer. Otherwise: Need to allocate a new buffer structure.
1548 *
1549 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001550 * (A spell file buffer is allocated in spell.c, but that's not a normal
1551 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 */
1553 buf = NULL;
1554 if ((flags & BLN_CURBUF)
1555 && curbuf != NULL
1556 && curbuf->b_ffname == NULL
1557 && curbuf->b_nwindows <= 1
1558 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1559 {
1560 buf = curbuf;
1561#ifdef FEAT_AUTOCMD
1562 /* It's like this buffer is deleted. Watch out for autocommands that
1563 * change curbuf! If that happens, allocate a new buffer anyway. */
1564 if (curbuf->b_p_bl)
1565 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1566 if (buf == curbuf)
1567 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1568# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001569 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 return NULL;
1571# endif
1572#endif
1573#ifdef FEAT_QUICKFIX
1574# ifdef FEAT_AUTOCMD
1575 if (buf == curbuf)
1576# endif
1577 {
1578 /* Make sure 'bufhidden' and 'buftype' are empty */
1579 clear_string_option(&buf->b_p_bh);
1580 clear_string_option(&buf->b_p_bt);
1581 }
1582#endif
1583 }
1584 if (buf != curbuf || curbuf == NULL)
1585 {
1586 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1587 if (buf == NULL)
1588 {
1589 vim_free(ffname);
1590 return NULL;
1591 }
1592 }
1593
1594 if (ffname != NULL)
1595 {
1596 buf->b_ffname = ffname;
1597 buf->b_sfname = vim_strsave(sfname);
1598 }
1599
1600 clear_wininfo(buf);
1601 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1602
1603 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1604 || buf->b_wininfo == NULL)
1605 {
1606 vim_free(buf->b_ffname);
1607 buf->b_ffname = NULL;
1608 vim_free(buf->b_sfname);
1609 buf->b_sfname = NULL;
1610 if (buf != curbuf)
1611 free_buffer(buf);
1612 return NULL;
1613 }
1614
1615 if (buf == curbuf)
1616 {
1617 /* free all things allocated for this buffer */
1618 buf_freeall(buf, FALSE, FALSE);
1619 if (buf != curbuf) /* autocommands deleted the buffer! */
1620 return NULL;
1621#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001622 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 return NULL;
1624#endif
1625 /* buf->b_nwindows = 0; why was this here? */
1626 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1627#ifdef FEAT_KEYMAP
1628 /* need to reload lmaps and set b:keymap_name */
1629 curbuf->b_kmap_state |= KEYMAP_INIT;
1630#endif
1631 }
1632 else
1633 {
1634 /*
1635 * put new buffer at the end of the buffer list
1636 */
1637 buf->b_next = NULL;
1638 if (firstbuf == NULL) /* buffer list is empty */
1639 {
1640 buf->b_prev = NULL;
1641 firstbuf = buf;
1642 }
1643 else /* append new buffer at end of list */
1644 {
1645 lastbuf->b_next = buf;
1646 buf->b_prev = lastbuf;
1647 }
1648 lastbuf = buf;
1649
1650 buf->b_fnum = top_file_num++;
1651 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1652 {
1653 EMSG(_("W14: Warning: List of file names overflow"));
1654 if (emsg_silent == 0)
1655 {
1656 out_flush();
1657 ui_delay(3000L, TRUE); /* make sure it is noticed */
1658 }
1659 top_file_num = 1;
1660 }
1661
1662 /*
1663 * Always copy the options from the current buffer.
1664 */
1665 buf_copy_options(buf, BCO_ALWAYS);
1666 }
1667
1668 buf->b_wininfo->wi_fpos.lnum = lnum;
1669 buf->b_wininfo->wi_win = curwin;
1670
1671#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001672 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1673#endif
1674#ifdef FEAT_SYN_HL
1675 hash_init(&buf->b_keywtab);
1676 hash_init(&buf->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677#endif
1678
1679 buf->b_fname = buf->b_sfname;
1680#ifdef UNIX
1681 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001682 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 else
1684 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001685 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 buf->b_dev = st.st_dev;
1687 buf->b_ino = st.st_ino;
1688 }
1689#endif
1690 buf->b_u_synced = TRUE;
1691 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001692 if (flags & BLN_DUMMY)
1693 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 buf_clear_file(buf);
1695 clrallmarks(buf); /* clear marks */
1696 fmarks_check_names(buf); /* check file marks for this file */
1697 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1698#ifdef FEAT_AUTOCMD
1699 if (!(flags & BLN_DUMMY))
1700 {
1701 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1702 if (flags & BLN_LISTED)
1703 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1704# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001705 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 return NULL;
1707# endif
1708 }
1709#endif
1710
1711 return buf;
1712}
1713
1714/*
1715 * Free the memory for the options of a buffer.
1716 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1717 * 'fileencoding'.
1718 */
1719 void
1720free_buf_options(buf, free_p_ff)
1721 buf_T *buf;
1722 int free_p_ff;
1723{
1724 if (free_p_ff)
1725 {
1726#ifdef FEAT_MBYTE
1727 clear_string_option(&buf->b_p_fenc);
1728#endif
1729 clear_string_option(&buf->b_p_ff);
1730#ifdef FEAT_QUICKFIX
1731 clear_string_option(&buf->b_p_bh);
1732 clear_string_option(&buf->b_p_bt);
1733#endif
1734 }
1735#ifdef FEAT_FIND_ID
1736 clear_string_option(&buf->b_p_def);
1737 clear_string_option(&buf->b_p_inc);
1738# ifdef FEAT_EVAL
1739 clear_string_option(&buf->b_p_inex);
1740# endif
1741#endif
1742#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1743 clear_string_option(&buf->b_p_inde);
1744 clear_string_option(&buf->b_p_indk);
1745#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001746#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1747 clear_string_option(&buf->b_p_bexpr);
1748#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001749#if defined(FEAT_EVAL)
1750 clear_string_option(&buf->b_p_fex);
1751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752#ifdef FEAT_CRYPT
1753 clear_string_option(&buf->b_p_key);
1754#endif
1755 clear_string_option(&buf->b_p_kp);
1756 clear_string_option(&buf->b_p_mps);
1757 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001758 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 clear_string_option(&buf->b_p_isk);
1760#ifdef FEAT_KEYMAP
1761 clear_string_option(&buf->b_p_keymap);
1762 ga_clear(&buf->b_kmap_ga);
1763#endif
1764#ifdef FEAT_COMMENTS
1765 clear_string_option(&buf->b_p_com);
1766#endif
1767#ifdef FEAT_FOLDING
1768 clear_string_option(&buf->b_p_cms);
1769#endif
1770 clear_string_option(&buf->b_p_nf);
1771#ifdef FEAT_SYN_HL
1772 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001773#endif
1774#ifdef FEAT_SPELL
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001775 clear_string_option(&buf->b_p_spc);
Bram Moolenaar86bc1fb2005-06-07 20:58:01 +00001776 clear_string_option(&buf->b_p_spf);
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001777 vim_free(buf->b_cap_prog);
1778 buf->b_cap_prog = NULL;
Bram Moolenaara0dea672005-03-20 22:23:10 +00001779 clear_string_option(&buf->b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780#endif
1781#ifdef FEAT_SEARCHPATH
1782 clear_string_option(&buf->b_p_sua);
1783#endif
1784#ifdef FEAT_AUTOCMD
1785 clear_string_option(&buf->b_p_ft);
1786#endif
1787#ifdef FEAT_OSFILETYPE
1788 clear_string_option(&buf->b_p_oft);
1789#endif
1790#ifdef FEAT_CINDENT
1791 clear_string_option(&buf->b_p_cink);
1792 clear_string_option(&buf->b_p_cino);
1793#endif
1794#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1795 clear_string_option(&buf->b_p_cinw);
1796#endif
1797#ifdef FEAT_INS_EXPAND
1798 clear_string_option(&buf->b_p_cpt);
1799#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001800#ifdef FEAT_COMPL_FUNC
1801 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001802 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804#ifdef FEAT_QUICKFIX
1805 clear_string_option(&buf->b_p_gp);
1806 clear_string_option(&buf->b_p_mp);
1807 clear_string_option(&buf->b_p_efm);
1808#endif
1809 clear_string_option(&buf->b_p_ep);
1810 clear_string_option(&buf->b_p_path);
1811 clear_string_option(&buf->b_p_tags);
1812#ifdef FEAT_INS_EXPAND
1813 clear_string_option(&buf->b_p_dict);
1814 clear_string_option(&buf->b_p_tsr);
1815#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001816#ifdef FEAT_TEXTOBJ
1817 clear_string_option(&buf->b_p_qe);
1818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 buf->b_p_ar = -1;
1820}
1821
1822/*
1823 * get alternate file n
1824 * set linenr to lnum or altfpos.lnum if lnum == 0
1825 * also set cursor column to altfpos.col if 'startofline' is not set.
1826 * if (options & GETF_SETMARK) call setpcmark()
1827 * if (options & GETF_ALT) we are jumping to an alternate file.
1828 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1829 *
1830 * return FAIL for failure, OK for success
1831 */
1832 int
1833buflist_getfile(n, lnum, options, forceit)
1834 int n;
1835 linenr_T lnum;
1836 int options;
1837 int forceit;
1838{
1839 buf_T *buf;
1840#ifdef FEAT_WINDOWS
1841 win_T *wp = NULL;
1842#endif
1843 pos_T *fpos;
1844 colnr_T col;
1845
1846 buf = buflist_findnr(n);
1847 if (buf == NULL)
1848 {
1849 if ((options & GETF_ALT) && n == 0)
1850 EMSG(_(e_noalt));
1851 else
1852 EMSGN(_("E92: Buffer %ld not found"), n);
1853 return FAIL;
1854 }
1855
1856 /* if alternate file is the current buffer, nothing to do */
1857 if (buf == curbuf)
1858 return OK;
1859
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001860 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001861 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001862 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001864 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001865#ifdef FEAT_AUTOCMD
1866 if (curbuf_locked())
1867 return FAIL;
1868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
1870 /* altfpos may be changed by getfile(), get it now */
1871 if (lnum == 0)
1872 {
1873 fpos = buflist_findfpos(buf);
1874 lnum = fpos->lnum;
1875 col = fpos->col;
1876 }
1877 else
1878 col = 0;
1879
1880#ifdef FEAT_WINDOWS
1881 if (options & GETF_SWITCH)
1882 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001883 /* If 'switchbuf' contains "useopen": jump to first window containing
1884 * "buf" if one exists */
1885 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001887 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1888 * page containing "buf" if one exists */
1889 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001890 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001891 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1892 * isn't empty: open new window */
1893 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001895 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1896 tabpage_new();
1897 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 return FAIL;
1899# ifdef FEAT_SCROLLBIND
1900 curwin->w_p_scb = FALSE;
1901# endif
1902 }
1903 }
1904#endif
1905
1906 ++RedrawingDisabled;
1907 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1908 lnum, forceit) <= 0)
1909 {
1910 --RedrawingDisabled;
1911
1912 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1913 if (!p_sol && col != 0)
1914 {
1915 curwin->w_cursor.col = col;
1916 check_cursor_col();
1917#ifdef FEAT_VIRTUALEDIT
1918 curwin->w_cursor.coladd = 0;
1919#endif
1920 curwin->w_set_curswant = TRUE;
1921 }
1922 return OK;
1923 }
1924 --RedrawingDisabled;
1925 return FAIL;
1926}
1927
1928/*
1929 * go to the last know line number for the current buffer
1930 */
1931 void
1932buflist_getfpos()
1933{
1934 pos_T *fpos;
1935
1936 fpos = buflist_findfpos(curbuf);
1937
1938 curwin->w_cursor.lnum = fpos->lnum;
1939 check_cursor_lnum();
1940
1941 if (p_sol)
1942 curwin->w_cursor.col = 0;
1943 else
1944 {
1945 curwin->w_cursor.col = fpos->col;
1946 check_cursor_col();
1947#ifdef FEAT_VIRTUALEDIT
1948 curwin->w_cursor.coladd = 0;
1949#endif
1950 curwin->w_set_curswant = TRUE;
1951 }
1952}
1953
Bram Moolenaar81695252004-12-29 20:58:21 +00001954#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1955/*
1956 * Find file in buffer list by name (it has to be for the current window).
1957 * Returns NULL if not found.
1958 */
1959 buf_T *
1960buflist_findname_exp(fname)
1961 char_u *fname;
1962{
1963 char_u *ffname;
1964 buf_T *buf = NULL;
1965
1966 /* First make the name into a full path name */
1967 ffname = FullName_save(fname,
1968#ifdef UNIX
1969 TRUE /* force expansion, get rid of symbolic links */
1970#else
1971 FALSE
1972#endif
1973 );
1974 if (ffname != NULL)
1975 {
1976 buf = buflist_findname(ffname);
1977 vim_free(ffname);
1978 }
1979 return buf;
1980}
1981#endif
1982
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983/*
1984 * Find file in buffer list by name (it has to be for the current window).
1985 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00001986 * Skips dummy buffers.
1987 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 */
1989 buf_T *
1990buflist_findname(ffname)
1991 char_u *ffname;
1992{
1993#ifdef UNIX
1994 struct stat st;
1995
1996 if (mch_stat((char *)ffname, &st) < 0)
1997 st.st_dev = (dev_T)-1;
1998 return buflist_findname_stat(ffname, &st);
1999}
2000
2001/*
2002 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2003 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002004 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 */
2006 static buf_T *
2007buflist_findname_stat(ffname, stp)
2008 char_u *ffname;
2009 struct stat *stp;
2010{
2011#endif
2012 buf_T *buf;
2013
2014 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002015 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016#ifdef UNIX
2017 , stp
2018#endif
2019 ))
2020 return buf;
2021 return NULL;
2022}
2023
2024#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2025/*
2026 * Find file in buffer list by a regexp pattern.
2027 * Return fnum of the found buffer.
2028 * Return < 0 for error.
2029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 int
2031buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2032 char_u *pattern;
2033 char_u *pattern_end; /* pointer to first char after pattern */
2034 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002035 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036{
2037 buf_T *buf;
2038 regprog_T *prog;
2039 int match = -1;
2040 int find_listed;
2041 char_u *pat;
2042 char_u *patend;
2043 int attempt;
2044 char_u *p;
2045 int toggledollar;
2046
2047 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2048 {
2049 if (*pattern == '%')
2050 match = curbuf->b_fnum;
2051 else
2052 match = curwin->w_alt_fnum;
2053#ifdef FEAT_DIFF
2054 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2055 match = -1;
2056#endif
2057 }
2058
2059 /*
2060 * Try four ways of matching a listed buffer:
2061 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002062 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 * attempt == 2: with '$' at end (only match at end)
2064 * attempt == 3: with '^' at start and '$' at end (only full match)
2065 * Repeat this for finding an unlisted buffer if there was no matching
2066 * listed buffer.
2067 */
2068 else
2069 {
2070 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2071 if (pat == NULL)
2072 return -1;
2073 patend = pat + STRLEN(pat) - 1;
2074 toggledollar = (patend > pat && *patend == '$');
2075
2076 /* First try finding a listed buffer. If not found and "unlisted"
2077 * is TRUE, try finding an unlisted buffer. */
2078 find_listed = TRUE;
2079 for (;;)
2080 {
2081 for (attempt = 0; attempt <= 3; ++attempt)
2082 {
2083 /* may add '^' and '$' */
2084 if (toggledollar)
2085 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2086 p = pat;
2087 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2088 ++p;
2089 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2090 if (prog == NULL)
2091 {
2092 vim_free(pat);
2093 return -1;
2094 }
2095
2096 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2097 if (buf->b_p_bl == find_listed
2098#ifdef FEAT_DIFF
2099 && (!diffmode || diff_mode_buf(buf))
2100#endif
2101 && buflist_match(prog, buf) != NULL)
2102 {
2103 if (match >= 0) /* already found a match */
2104 {
2105 match = -2;
2106 break;
2107 }
2108 match = buf->b_fnum; /* remember first match */
2109 }
2110
2111 vim_free(prog);
2112 if (match >= 0) /* found one match */
2113 break;
2114 }
2115
2116 /* Only search for unlisted buffers if there was no match with
2117 * a listed buffer. */
2118 if (!unlisted || !find_listed || match != -1)
2119 break;
2120 find_listed = FALSE;
2121 }
2122
2123 vim_free(pat);
2124 }
2125
2126 if (match == -2)
2127 EMSG2(_("E93: More than one match for %s"), pattern);
2128 else if (match < 0)
2129 EMSG2(_("E94: No matching buffer for %s"), pattern);
2130 return match;
2131}
2132#endif
2133
2134#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2135
2136/*
2137 * Find all buffer names that match.
2138 * For command line expansion of ":buf" and ":sbuf".
2139 * Return OK if matches found, FAIL otherwise.
2140 */
2141 int
2142ExpandBufnames(pat, num_file, file, options)
2143 char_u *pat;
2144 int *num_file;
2145 char_u ***file;
2146 int options;
2147{
2148 int count = 0;
2149 buf_T *buf;
2150 int round;
2151 char_u *p;
2152 int attempt;
2153 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002154 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156 *num_file = 0; /* return values in case of FAIL */
2157 *file = NULL;
2158
Bram Moolenaar05159a02005-02-26 23:04:13 +00002159 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2160 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002162 patc = alloc((unsigned)STRLEN(pat) + 11);
2163 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002165 STRCPY(patc, "\\(^\\|[\\/]\\)");
2166 STRCPY(patc + 11, pat + 1);
2167 }
2168 else
2169 patc = pat;
2170
2171 /*
2172 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002173 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002174 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002175 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002176 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002177 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002178 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002179 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002180 if (prog == NULL)
2181 {
2182 if (patc != pat)
2183 vim_free(patc);
2184 return FAIL;
2185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
2187 /*
2188 * round == 1: Count the matches.
2189 * round == 2: Build the array to keep the matches.
2190 */
2191 for (round = 1; round <= 2; ++round)
2192 {
2193 count = 0;
2194 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2195 {
2196 if (!buf->b_p_bl) /* skip unlisted buffers */
2197 continue;
2198 p = buflist_match(prog, buf);
2199 if (p != NULL)
2200 {
2201 if (round == 1)
2202 ++count;
2203 else
2204 {
2205 if (options & WILD_HOME_REPLACE)
2206 p = home_replace_save(buf, p);
2207 else
2208 p = vim_strsave(p);
2209 (*file)[count++] = p;
2210 }
2211 }
2212 }
2213 if (count == 0) /* no match found, break here */
2214 break;
2215 if (round == 1)
2216 {
2217 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2218 if (*file == NULL)
2219 {
2220 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002221 if (patc != pat)
2222 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 return FAIL;
2224 }
2225 }
2226 }
2227 vim_free(prog);
2228 if (count) /* match(es) found, break here */
2229 break;
2230 }
2231
Bram Moolenaar05159a02005-02-26 23:04:13 +00002232 if (patc != pat)
2233 vim_free(patc);
2234
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 *num_file = count;
2236 return (count == 0 ? FAIL : OK);
2237}
2238
2239#endif /* FEAT_CMDL_COMPL */
2240
2241#ifdef HAVE_BUFLIST_MATCH
2242/*
2243 * Check for a match on the file name for buffer "buf" with regprog "prog".
2244 */
2245 static char_u *
2246buflist_match(prog, buf)
2247 regprog_T *prog;
2248 buf_T *buf;
2249{
2250 char_u *match;
2251
2252 /* First try the short file name, then the long file name. */
2253 match = fname_match(prog, buf->b_sfname);
2254 if (match == NULL)
2255 match = fname_match(prog, buf->b_ffname);
2256
2257 return match;
2258}
2259
2260/*
2261 * Try matching the regexp in "prog" with file name "name".
2262 * Return "name" when there is a match, NULL when not.
2263 */
2264 static char_u *
2265fname_match(prog, name)
2266 regprog_T *prog;
2267 char_u *name;
2268{
2269 char_u *match = NULL;
2270 char_u *p;
2271 regmatch_T regmatch;
2272
2273 if (name != NULL)
2274 {
2275 regmatch.regprog = prog;
2276#ifdef CASE_INSENSITIVE_FILENAME
2277 regmatch.rm_ic = TRUE; /* Always ignore case */
2278#else
2279 regmatch.rm_ic = FALSE; /* Never ignore case */
2280#endif
2281
2282 if (vim_regexec(&regmatch, name, (colnr_T)0))
2283 match = name;
2284 else
2285 {
2286 /* Replace $(HOME) with '~' and try matching again. */
2287 p = home_replace_save(NULL, name);
2288 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2289 match = name;
2290 vim_free(p);
2291 }
2292 }
2293
2294 return match;
2295}
2296#endif
2297
2298/*
2299 * find file in buffer list by number
2300 */
2301 buf_T *
2302buflist_findnr(nr)
2303 int nr;
2304{
2305 buf_T *buf;
2306
2307 if (nr == 0)
2308 nr = curwin->w_alt_fnum;
2309 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2310 if (buf->b_fnum == nr)
2311 return (buf);
2312 return NULL;
2313}
2314
2315/*
2316 * Get name of file 'n' in the buffer list.
2317 * When the file has no name an empty string is returned.
2318 * home_replace() is used to shorten the file name (used for marks).
2319 * Returns a pointer to allocated memory, of NULL when failed.
2320 */
2321 char_u *
2322buflist_nr2name(n, fullname, helptail)
2323 int n;
2324 int fullname;
2325 int helptail; /* for help buffers return tail only */
2326{
2327 buf_T *buf;
2328
2329 buf = buflist_findnr(n);
2330 if (buf == NULL)
2331 return NULL;
2332 return home_replace_save(helptail ? buf : NULL,
2333 fullname ? buf->b_ffname : buf->b_fname);
2334}
2335
2336/*
2337 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2338 * When "copy_options" is TRUE save the local window option values.
2339 * When "lnum" is 0 only do the options.
2340 */
2341 static void
2342buflist_setfpos(buf, win, lnum, col, copy_options)
2343 buf_T *buf;
2344 win_T *win;
2345 linenr_T lnum;
2346 colnr_T col;
2347 int copy_options;
2348{
2349 wininfo_T *wip;
2350
2351 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2352 if (wip->wi_win == win)
2353 break;
2354 if (wip == NULL)
2355 {
2356 /* allocate a new entry */
2357 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2358 if (wip == NULL)
2359 return;
2360 wip->wi_win = win;
2361 if (lnum == 0) /* set lnum even when it's 0 */
2362 lnum = 1;
2363 }
2364 else
2365 {
2366 /* remove the entry from the list */
2367 if (wip->wi_prev)
2368 wip->wi_prev->wi_next = wip->wi_next;
2369 else
2370 buf->b_wininfo = wip->wi_next;
2371 if (wip->wi_next)
2372 wip->wi_next->wi_prev = wip->wi_prev;
2373 if (copy_options && wip->wi_optset)
2374 {
2375 clear_winopt(&wip->wi_opt);
2376#ifdef FEAT_FOLDING
2377 deleteFoldRecurse(&wip->wi_folds);
2378#endif
2379 }
2380 }
2381 if (lnum != 0)
2382 {
2383 wip->wi_fpos.lnum = lnum;
2384 wip->wi_fpos.col = col;
2385 }
2386 if (copy_options)
2387 {
2388 /* Save the window-specific option values. */
2389 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2390#ifdef FEAT_FOLDING
2391 wip->wi_fold_manual = win->w_fold_manual;
2392 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2393#endif
2394 wip->wi_optset = TRUE;
2395 }
2396
2397 /* insert the entry in front of the list */
2398 wip->wi_next = buf->b_wininfo;
2399 buf->b_wininfo = wip;
2400 wip->wi_prev = NULL;
2401 if (wip->wi_next)
2402 wip->wi_next->wi_prev = wip;
2403
2404 return;
2405}
2406
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002407#ifdef FEAT_DIFF
2408static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2409
2410/*
2411 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2412 * page. That's because a diff is local to a tab page.
2413 */
2414 static int
2415wininfo_other_tab_diff(wip)
2416 wininfo_T *wip;
2417{
2418 win_T *wp;
2419
2420 if (wip->wi_opt.wo_diff)
2421 {
2422 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2423 /* return FALSE when it's a window in the current tab page, thus
2424 * the buffer was in diff mode here */
2425 if (wip->wi_win == wp)
2426 return FALSE;
2427 return TRUE;
2428 }
2429 return FALSE;
2430}
2431#endif
2432
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433/*
2434 * Find info for the current window in buffer "buf".
2435 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002436 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2437 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 * Returns NULL when there isn't any info.
2439 */
2440 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002441find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002443 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444{
2445 wininfo_T *wip;
2446
2447 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002448 if (wip->wi_win == curwin
2449#ifdef FEAT_DIFF
2450 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2451#endif
2452 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002454
2455 /* If no wininfo for curwin, use the first in the list (that doesn't have
2456 * 'diff' set and is in another tab page). */
2457 if (wip == NULL)
2458 {
2459#ifdef FEAT_DIFF
2460 if (skip_diff_buffer)
2461 {
2462 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2463 if (!wininfo_other_tab_diff(wip))
2464 break;
2465 }
2466 else
2467#endif
2468 wip = buf->b_wininfo;
2469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 return wip;
2471}
2472
2473/*
2474 * Reset the local window options to the values last used in this window.
2475 * If the buffer wasn't used in this window before, use the values from
2476 * the most recently used window. If the values were never set, use the
2477 * global values for the window.
2478 */
2479 void
2480get_winopts(buf)
2481 buf_T *buf;
2482{
2483 wininfo_T *wip;
2484
2485 clear_winopt(&curwin->w_onebuf_opt);
2486#ifdef FEAT_FOLDING
2487 clearFolding(curwin);
2488#endif
2489
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002490 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if (wip != NULL && wip->wi_optset)
2492 {
2493 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2494#ifdef FEAT_FOLDING
2495 curwin->w_fold_manual = wip->wi_fold_manual;
2496 curwin->w_foldinvalid = TRUE;
2497 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2498#endif
2499 }
2500 else
2501 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2502
2503#ifdef FEAT_FOLDING
2504 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2505 if (p_fdls >= 0)
2506 curwin->w_p_fdl = p_fdls;
2507#endif
2508}
2509
2510/*
2511 * Find the position (lnum and col) for the buffer 'buf' for the current
2512 * window.
2513 * Returns a pointer to no_position if no position is found.
2514 */
2515 pos_T *
2516buflist_findfpos(buf)
2517 buf_T *buf;
2518{
2519 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002520 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002522 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 if (wip != NULL)
2524 return &(wip->wi_fpos);
2525 else
2526 return &no_position;
2527}
2528
2529/*
2530 * Find the lnum for the buffer 'buf' for the current window.
2531 */
2532 linenr_T
2533buflist_findlnum(buf)
2534 buf_T *buf;
2535{
2536 return buflist_findfpos(buf)->lnum;
2537}
2538
2539#if defined(FEAT_LISTCMDS) || defined(PROTO)
2540/*
2541 * List all know file names (for :files and :buffers command).
2542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 void
2544buflist_list(eap)
2545 exarg_T *eap;
2546{
2547 buf_T *buf;
2548 int len;
2549 int i;
2550
2551 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2552 {
2553 /* skip unlisted buffers, unless ! was used */
2554 if (!buf->b_p_bl && !eap->forceit)
2555 continue;
2556 msg_putchar('\n');
2557 if (buf_spname(buf) != NULL)
2558 STRCPY(NameBuff, buf_spname(buf));
2559 else
2560 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2561
Bram Moolenaar50cde822005-06-05 21:54:54 +00002562 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 buf->b_fnum,
2564 buf->b_p_bl ? ' ' : 'u',
2565 buf == curbuf ? '%' :
2566 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2567 buf->b_ml.ml_mfp == NULL ? ' ' :
2568 (buf->b_nwindows == 0 ? 'h' : 'a'),
2569 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2570 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002571 : (bufIsChanged(buf) ? '+' : ' '),
2572 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
2574 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 i = 40 - vim_strsize(IObuff);
2576 do
2577 {
2578 IObuff[len++] = ' ';
2579 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002580 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2581 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002582 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 msg_outtrans(IObuff);
2584 out_flush(); /* output one line at a time */
2585 ui_breakcheck();
2586 }
2587}
2588#endif
2589
2590/*
2591 * Get file name and line number for file 'fnum'.
2592 * Used by DoOneCmd() for translating '%' and '#'.
2593 * Used by insert_reg() and cmdline_paste() for '#' register.
2594 * Return FAIL if not found, OK for success.
2595 */
2596 int
2597buflist_name_nr(fnum, fname, lnum)
2598 int fnum;
2599 char_u **fname;
2600 linenr_T *lnum;
2601{
2602 buf_T *buf;
2603
2604 buf = buflist_findnr(fnum);
2605 if (buf == NULL || buf->b_fname == NULL)
2606 return FAIL;
2607
2608 *fname = buf->b_fname;
2609 *lnum = buflist_findlnum(buf);
2610
2611 return OK;
2612}
2613
2614/*
2615 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2616 * The file name with the full path is also remembered, for when :cd is used.
2617 * Returns FAIL for failure (file name already in use by other buffer)
2618 * OK otherwise.
2619 */
2620 int
2621setfname(buf, ffname, sfname, message)
2622 buf_T *buf;
2623 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002624 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
Bram Moolenaar81695252004-12-29 20:58:21 +00002626 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627#ifdef UNIX
2628 struct stat st;
2629#endif
2630
2631 if (ffname == NULL || *ffname == NUL)
2632 {
2633 /* Removing the name. */
2634 vim_free(buf->b_ffname);
2635 vim_free(buf->b_sfname);
2636 buf->b_ffname = NULL;
2637 buf->b_sfname = NULL;
2638#ifdef UNIX
2639 st.st_dev = (dev_T)-1;
2640#endif
2641 }
2642 else
2643 {
2644 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2645 if (ffname == NULL) /* out of memory */
2646 return FAIL;
2647
2648 /*
2649 * if the file name is already used in another buffer:
2650 * - if the buffer is loaded, fail
2651 * - if the buffer is not loaded, delete it from the list
2652 */
2653#ifdef UNIX
2654 if (mch_stat((char *)ffname, &st) < 0)
2655 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002656#endif
2657 if (!(buf->b_flags & BF_DUMMY))
2658#ifdef UNIX
2659 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002661 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662#endif
2663 if (obuf != NULL && obuf != buf)
2664 {
2665 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2666 {
2667 if (message)
2668 EMSG(_("E95: Buffer with this name already exists"));
2669 vim_free(ffname);
2670 return FAIL;
2671 }
2672 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2673 }
2674 sfname = vim_strsave(sfname);
2675 if (ffname == NULL || sfname == NULL)
2676 {
2677 vim_free(sfname);
2678 vim_free(ffname);
2679 return FAIL;
2680 }
2681#ifdef USE_FNAME_CASE
2682# ifdef USE_LONG_FNAME
2683 if (USE_LONG_FNAME)
2684# endif
2685 fname_case(sfname, 0); /* set correct case for short file name */
2686#endif
2687 vim_free(buf->b_ffname);
2688 vim_free(buf->b_sfname);
2689 buf->b_ffname = ffname;
2690 buf->b_sfname = sfname;
2691 }
2692 buf->b_fname = buf->b_sfname;
2693#ifdef UNIX
2694 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002695 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 else
2697 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002698 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 buf->b_dev = st.st_dev;
2700 buf->b_ino = st.st_ino;
2701 }
2702#endif
2703
2704#ifndef SHORT_FNAME
2705 buf->b_shortname = FALSE;
2706#endif
2707
2708 buf_name_changed(buf);
2709 return OK;
2710}
2711
2712/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002713 * Crude way of changing the name of a buffer. Use with care!
2714 * The name should be relative to the current directory.
2715 */
2716 void
2717buf_set_name(fnum, name)
2718 int fnum;
2719 char_u *name;
2720{
2721 buf_T *buf;
2722
2723 buf = buflist_findnr(fnum);
2724 if (buf != NULL)
2725 {
2726 vim_free(buf->b_sfname);
2727 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002728 buf->b_ffname = vim_strsave(name);
2729 buf->b_sfname = NULL;
2730 /* Allocate ffname and expand into full path. Also resolves .lnk
2731 * files on Win32. */
2732 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002733 buf->b_fname = buf->b_sfname;
2734 }
2735}
2736
2737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 * Take care of what needs to be done when the name of buffer "buf" has
2739 * changed.
2740 */
2741 void
2742buf_name_changed(buf)
2743 buf_T *buf;
2744{
2745 /*
2746 * If the file name changed, also change the name of the swapfile
2747 */
2748 if (buf->b_ml.ml_mfp != NULL)
2749 ml_setname(buf);
2750
2751 if (curwin->w_buffer == buf)
2752 check_arg_idx(curwin); /* check file name for arg list */
2753#ifdef FEAT_TITLE
2754 maketitle(); /* set window title */
2755#endif
2756#ifdef FEAT_WINDOWS
2757 status_redraw_all(); /* status lines need to be redrawn */
2758#endif
2759 fmarks_check_names(buf); /* check named file marks */
2760 ml_timestamp(buf); /* reset timestamp */
2761}
2762
2763/*
2764 * set alternate file name for current window
2765 *
2766 * Used by do_one_cmd(), do_write() and do_ecmd().
2767 * Return the buffer.
2768 */
2769 buf_T *
2770setaltfname(ffname, sfname, lnum)
2771 char_u *ffname;
2772 char_u *sfname;
2773 linenr_T lnum;
2774{
2775 buf_T *buf;
2776
2777 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2778 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002779 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 curwin->w_alt_fnum = buf->b_fnum;
2781 return buf;
2782}
2783
2784/*
2785 * Get alternate file name for current window.
2786 * Return NULL if there isn't any, and give error message if requested.
2787 */
2788 char_u *
2789getaltfname(errmsg)
2790 int errmsg; /* give error message */
2791{
2792 char_u *fname;
2793 linenr_T dummy;
2794
2795 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2796 {
2797 if (errmsg)
2798 EMSG(_(e_noalt));
2799 return NULL;
2800 }
2801 return fname;
2802}
2803
2804/*
2805 * Add a file name to the buflist and return its number.
2806 * Uses same flags as buflist_new(), except BLN_DUMMY.
2807 *
2808 * used by qf_init(), main() and doarglist()
2809 */
2810 int
2811buflist_add(fname, flags)
2812 char_u *fname;
2813 int flags;
2814{
2815 buf_T *buf;
2816
2817 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2818 if (buf != NULL)
2819 return buf->b_fnum;
2820 return 0;
2821}
2822
2823#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2824/*
2825 * Adjust slashes in file names. Called after 'shellslash' was set.
2826 */
2827 void
2828buflist_slash_adjust()
2829{
2830 buf_T *bp;
2831
2832 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2833 {
2834 if (bp->b_ffname != NULL)
2835 slash_adjust(bp->b_ffname);
2836 if (bp->b_sfname != NULL)
2837 slash_adjust(bp->b_sfname);
2838 }
2839}
2840#endif
2841
2842/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002843 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 * Also save the local window option values.
2845 */
2846 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002847buflist_altfpos(win)
2848 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002850 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851}
2852
2853/*
2854 * Return TRUE if 'ffname' is not the same file as current file.
2855 * Fname must have a full path (expanded by mch_FullName()).
2856 */
2857 int
2858otherfile(ffname)
2859 char_u *ffname;
2860{
2861 return otherfile_buf(curbuf, ffname
2862#ifdef UNIX
2863 , NULL
2864#endif
2865 );
2866}
2867
2868 static int
2869otherfile_buf(buf, ffname
2870#ifdef UNIX
2871 , stp
2872#endif
2873 )
2874 buf_T *buf;
2875 char_u *ffname;
2876#ifdef UNIX
2877 struct stat *stp;
2878#endif
2879{
2880 /* no name is different */
2881 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2882 return TRUE;
2883 if (fnamecmp(ffname, buf->b_ffname) == 0)
2884 return FALSE;
2885#ifdef UNIX
2886 {
2887 struct stat st;
2888
2889 /* If no struct stat given, get it now */
2890 if (stp == NULL)
2891 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002892 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 st.st_dev = (dev_T)-1;
2894 stp = &st;
2895 }
2896 /* Use dev/ino to check if the files are the same, even when the names
2897 * are different (possible with links). Still need to compare the
2898 * name above, for when the file doesn't exist yet.
2899 * Problem: The dev/ino changes when a file is deleted (and created
2900 * again) and remains the same when renamed/moved. We don't want to
2901 * mch_stat() each buffer each time, that would be too slow. Get the
2902 * dev/ino again when they appear to match, but not when they appear
2903 * to be different: Could skip a buffer when it's actually the same
2904 * file. */
2905 if (buf_same_ino(buf, stp))
2906 {
2907 buf_setino(buf);
2908 if (buf_same_ino(buf, stp))
2909 return FALSE;
2910 }
2911 }
2912#endif
2913 return TRUE;
2914}
2915
2916#if defined(UNIX) || defined(PROTO)
2917/*
2918 * Set inode and device number for a buffer.
2919 * Must always be called when b_fname is changed!.
2920 */
2921 void
2922buf_setino(buf)
2923 buf_T *buf;
2924{
2925 struct stat st;
2926
2927 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2928 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002929 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 buf->b_dev = st.st_dev;
2931 buf->b_ino = st.st_ino;
2932 }
2933 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002934 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935}
2936
2937/*
2938 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2939 */
2940 static int
2941buf_same_ino(buf, stp)
2942 buf_T *buf;
2943 struct stat *stp;
2944{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002945 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 && stp->st_dev == buf->b_dev
2947 && stp->st_ino == buf->b_ino);
2948}
2949#endif
2950
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002951/*
2952 * Print info about the current buffer.
2953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 void
2955fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002956 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 int shorthelp;
2958 int dont_truncate;
2959{
2960 char_u *name;
2961 int n;
2962 char_u *p;
2963 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002964 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
2966 buffer = alloc(IOSIZE);
2967 if (buffer == NULL)
2968 return;
2969
2970 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2971 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002972 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 p = buffer + STRLEN(buffer);
2974 }
2975 else
2976 p = buffer;
2977
2978 *p++ = '"';
2979 if (buf_spname(curbuf) != NULL)
2980 STRCPY(p, buf_spname(curbuf));
2981 else
2982 {
2983 if (!fullname && curbuf->b_fname != NULL)
2984 name = curbuf->b_fname;
2985 else
2986 name = curbuf->b_ffname;
2987 home_replace(shorthelp ? curbuf : NULL, name, p,
2988 (int)(IOSIZE - (p - buffer)), TRUE);
2989 }
2990
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002991 len = STRLEN(buffer);
2992 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 "\"%s%s%s%s%s%s",
2994 curbufIsChanged() ? (shortmess(SHM_MOD)
2995 ? " [+]" : _(" [Modified]")) : " ",
2996 (curbuf->b_flags & BF_NOTEDITED)
2997#ifdef FEAT_QUICKFIX
2998 && !bt_dontwrite(curbuf)
2999#endif
3000 ? _("[Not edited]") : "",
3001 (curbuf->b_flags & BF_NEW)
3002#ifdef FEAT_QUICKFIX
3003 && !bt_dontwrite(curbuf)
3004#endif
3005 ? _("[New file]") : "",
3006 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3007 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3008 : _("[readonly]")) : "",
3009 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3010 || curbuf->b_p_ro) ?
3011 " " : "");
3012 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3013 * causes an overflow, thus for large numbers divide instead. */
3014 if (curwin->w_cursor.lnum > 1000000L)
3015 n = (int)(((long)curwin->w_cursor.lnum) /
3016 ((long)curbuf->b_ml.ml_line_count / 100L));
3017 else
3018 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3019 (long)curbuf->b_ml.ml_line_count);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003020 len = STRLEN(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3022 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003023 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 }
3025#ifdef FEAT_CMDL_INFO
3026 else if (p_ru)
3027 {
3028 /* Current line and column are already on the screen -- webb */
3029 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003030 vim_snprintf((char *)buffer + len, IOSIZE - len,
3031 _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003033 vim_snprintf((char *)buffer + len, IOSIZE - len,
3034 _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 (long)curbuf->b_ml.ml_line_count, n);
3036 }
3037#endif
3038 else
3039 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003040 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 _("line %ld of %ld --%d%%-- col "),
3042 (long)curwin->w_cursor.lnum,
3043 (long)curbuf->b_ml.ml_line_count,
3044 n);
3045 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003046 len = STRLEN(buffer);
3047 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3049 }
3050
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003051 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
3053 if (dont_truncate)
3054 {
3055 /* Temporarily set msg_scroll to avoid the message being truncated.
3056 * First call msg_start() to get the message in the right place. */
3057 msg_start();
3058 n = msg_scroll;
3059 msg_scroll = TRUE;
3060 msg(buffer);
3061 msg_scroll = n;
3062 }
3063 else
3064 {
3065 p = msg_trunc_attr(buffer, FALSE, 0);
3066 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 /* Need to repeat the message after redrawing when:
3068 * - When restart_edit is set (otherwise there will be a delay
3069 * before redrawing).
3070 * - When the screen was scrolled but there is no wait-return
3071 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003072 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 }
3074
3075 vim_free(buffer);
3076}
3077
3078 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003079col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003081 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 int col;
3083 int vcol;
3084{
3085 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003086 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003088 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089}
3090
3091#if defined(FEAT_TITLE) || defined(PROTO)
3092/*
3093 * put file name in title bar of window and in icon title
3094 */
3095
3096static char_u *lasttitle = NULL;
3097static char_u *lasticon = NULL;
3098
3099 void
3100maketitle()
3101{
3102 char_u *p;
3103 char_u *t_str = NULL;
3104 char_u *i_name;
3105 char_u *i_str = NULL;
3106 int maxlen = 0;
3107 int len;
3108 int mustset;
3109 char_u buf[IOSIZE];
3110 int off;
3111
3112 if (!redrawing())
3113 {
3114 /* Postpone updating the title when 'lazyredraw' is set. */
3115 need_maketitle = TRUE;
3116 return;
3117 }
3118
3119 need_maketitle = FALSE;
3120 if (!p_title && !p_icon)
3121 return;
3122
3123 if (p_title)
3124 {
3125 if (p_titlelen > 0)
3126 {
3127 maxlen = p_titlelen * Columns / 100;
3128 if (maxlen < 10)
3129 maxlen = 10;
3130 }
3131
3132 t_str = buf;
3133 if (*p_titlestring != NUL)
3134 {
3135#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003136 if (stl_syntax & STL_IN_TITLE)
3137 {
3138 int use_sandbox = FALSE;
3139 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003140
3141# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003142 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003143# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003144 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003146 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003147 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003148 if (called_emsg)
3149 set_string_option_direct((char_u *)"titlestring", -1,
3150 (char_u *)"", OPT_FREE, SID_ERROR);
3151 called_emsg |= save_called_emsg;
3152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 else
3154#endif
3155 t_str = p_titlestring;
3156 }
3157 else
3158 {
3159 /* format: "fname + (path) (1 of 2) - VIM" */
3160
3161 if (curbuf->b_fname == NULL)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003162 STRCPY(buf, _("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 else
3164 {
3165 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003166 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 }
3169
3170 switch (bufIsChanged(curbuf)
3171 + (curbuf->b_p_ro * 2)
3172 + (!curbuf->b_p_ma * 4))
3173 {
3174 case 1: STRCAT(buf, " +"); break;
3175 case 2: STRCAT(buf, " ="); break;
3176 case 3: STRCAT(buf, " =+"); break;
3177 case 4:
3178 case 6: STRCAT(buf, " -"); break;
3179 case 5:
3180 case 7: STRCAT(buf, " -+"); break;
3181 }
3182
3183 if (curbuf->b_fname != NULL)
3184 {
3185 /* Get path of file, replace home dir with ~ */
3186 off = (int)STRLEN(buf);
3187 buf[off++] = ' ';
3188 buf[off++] = '(';
3189 home_replace(curbuf, curbuf->b_ffname,
3190 buf + off, IOSIZE - off, TRUE);
3191#ifdef BACKSLASH_IN_FILENAME
3192 /* avoid "c:/name" to be reduced to "c" */
3193 if (isalpha(buf[off]) && buf[off + 1] == ':')
3194 off += 2;
3195#endif
3196 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003197 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003200 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003201 (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003204
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 /* translate unprintable chars */
3206 p = transstr(buf + off);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003207 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 STRCAT(buf, ")");
3210 }
3211
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003212 append_arg_number(curwin, buf, IOSIZE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
3214#if defined(FEAT_CLIENTSERVER)
3215 if (serverName != NULL)
3216 {
3217 STRCAT(buf, " - ");
3218 STRCAT(buf, serverName);
3219 }
3220 else
3221#endif
3222 STRCAT(buf, " - VIM");
3223
3224 if (maxlen > 0)
3225 {
3226 /* make it shorter by removing a bit in the middle */
3227 len = vim_strsize(buf);
3228 if (len > maxlen)
3229 trunc_string(buf, buf, maxlen);
3230 }
3231 }
3232 }
3233 mustset = ti_change(t_str, &lasttitle);
3234
3235 if (p_icon)
3236 {
3237 i_str = buf;
3238 if (*p_iconstring != NUL)
3239 {
3240#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003241 if (stl_syntax & STL_IN_ICON)
3242 {
3243 int use_sandbox = FALSE;
3244 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003245
3246# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003247 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003248# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003249 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003251 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003252 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003253 if (called_emsg)
3254 set_string_option_direct((char_u *)"iconstring", -1,
3255 (char_u *)"", OPT_FREE, SID_ERROR);
3256 called_emsg |= save_called_emsg;
3257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 else
3259#endif
3260 i_str = p_iconstring;
3261 }
3262 else
3263 {
3264 if (buf_spname(curbuf) != NULL)
3265 i_name = (char_u *)buf_spname(curbuf);
3266 else /* use file name only in icon */
3267 i_name = gettail(curbuf->b_ffname);
3268 *i_str = NUL;
3269 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003270 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 if (len > 100)
3272 {
3273 len -= 100;
3274#ifdef FEAT_MBYTE
3275 if (has_mbyte)
3276 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3277#endif
3278 i_name += len;
3279 }
3280 STRCPY(i_str, i_name);
3281 trans_characters(i_str, IOSIZE);
3282 }
3283 }
3284
3285 mustset |= ti_change(i_str, &lasticon);
3286
3287 if (mustset)
3288 resettitle();
3289}
3290
3291/*
3292 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3293 * from "str" if it does.
3294 * Return TRUE when "*last" changed.
3295 */
3296 static int
3297ti_change(str, last)
3298 char_u *str;
3299 char_u **last;
3300{
3301 if ((str == NULL) != (*last == NULL)
3302 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3303 {
3304 vim_free(*last);
3305 if (str == NULL)
3306 *last = NULL;
3307 else
3308 *last = vim_strsave(str);
3309 return TRUE;
3310 }
3311 return FALSE;
3312}
3313
3314/*
3315 * Put current window title back (used after calling a shell)
3316 */
3317 void
3318resettitle()
3319{
3320 mch_settitle(lasttitle, lasticon);
3321}
Bram Moolenaarea408852005-06-25 22:49:46 +00003322
3323# if defined(EXITFREE) || defined(PROTO)
3324 void
3325free_titles()
3326{
3327 vim_free(lasttitle);
3328 vim_free(lasticon);
3329}
3330# endif
3331
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332#endif /* FEAT_TITLE */
3333
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003334#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003336 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 * Return length of string in screen cells.
3338 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003339 * Normally works for window "wp", except when working for 'tabline' then it
3340 * is "curwin".
3341 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 * Items are drawn interspersed with the text that surrounds it
3343 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3344 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3345 *
3346 * If maxwidth is not zero, the string will be filled at any middle marker
3347 * or truncated if too long, fillchar is used for all whitespace.
3348 */
3349 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003350build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003352 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 size_t outlen; /* length of out[] */
3354 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003355 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 int fillchar;
3357 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003358 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3359 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 char_u *p;
3362 char_u *s;
3363 char_u *t;
3364 char_u *linecont;
3365#ifdef FEAT_EVAL
3366 win_T *o_curwin;
3367 buf_T *o_curbuf;
3368#endif
3369 int empty_line;
3370 colnr_T virtcol;
3371 long l;
3372 long n;
3373 int prevchar_isflag;
3374 int prevchar_isitem;
3375 int itemisflag;
3376 int fillable;
3377 char_u *str;
3378 long num;
3379 int width;
3380 int itemcnt;
3381 int curitem;
3382 int groupitem[STL_MAX_ITEM];
3383 int groupdepth;
3384 struct stl_item
3385 {
3386 char_u *start;
3387 int minwid;
3388 int maxwid;
3389 enum
3390 {
3391 Normal,
3392 Empty,
3393 Group,
3394 Middle,
3395 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003396 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 Trunc
3398 } type;
3399 } item[STL_MAX_ITEM];
3400 int minwid;
3401 int maxwid;
3402 int zeropad;
3403 char_u base;
3404 char_u opt;
3405#define TMPLEN 70
3406 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003407 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003408 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003409
3410#ifdef FEAT_EVAL
3411 /*
3412 * When the format starts with "%!" then evaluate it as an expression and
3413 * use the result as the actual format string.
3414 */
3415 if (fmt[0] == '%' && fmt[1] == '!')
3416 {
3417 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3418 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003419 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003420 }
3421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
3423 if (fillchar == 0)
3424 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003425#ifdef FEAT_MBYTE
3426 /* Can't handle a multi-byte fill character yet. */
3427 else if (mb_char2len(fillchar) > 1)
3428 fillchar = '-';
3429#endif
3430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 /*
3432 * Get line & check if empty (cursorpos will show "0-1").
3433 * If inversion is possible we use it. Else '=' characters are used.
3434 */
3435 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3436 empty_line = (*linecont == NUL);
3437
3438 groupdepth = 0;
3439 p = out;
3440 curitem = 0;
3441 prevchar_isflag = TRUE;
3442 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003443 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 {
3445 if (*s != NUL && *s != '%')
3446 prevchar_isflag = prevchar_isitem = FALSE;
3447
3448 /*
3449 * Handle up to the next '%' or the end.
3450 */
3451 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3452 *p++ = *s++;
3453 if (*s == NUL || p + 1 >= out + outlen)
3454 break;
3455
3456 /*
3457 * Handle one '%' item.
3458 */
3459 s++;
3460 if (*s == '%')
3461 {
3462 if (p + 1 >= out + outlen)
3463 break;
3464 *p++ = *s++;
3465 prevchar_isflag = prevchar_isitem = FALSE;
3466 continue;
3467 }
3468 if (*s == STL_MIDDLEMARK)
3469 {
3470 s++;
3471 if (groupdepth > 0)
3472 continue;
3473 item[curitem].type = Middle;
3474 item[curitem++].start = p;
3475 continue;
3476 }
3477 if (*s == STL_TRUNCMARK)
3478 {
3479 s++;
3480 item[curitem].type = Trunc;
3481 item[curitem++].start = p;
3482 continue;
3483 }
3484 if (*s == ')')
3485 {
3486 s++;
3487 if (groupdepth < 1)
3488 continue;
3489 groupdepth--;
3490
3491 t = item[groupitem[groupdepth]].start;
3492 *p = NUL;
3493 l = vim_strsize(t);
3494 if (curitem > groupitem[groupdepth] + 1
3495 && item[groupitem[groupdepth]].minwid == 0)
3496 {
3497 /* remove group if all items are empty */
3498 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3499 if (item[n].type == Normal)
3500 break;
3501 if (n == curitem)
3502 {
3503 p = t;
3504 l = 0;
3505 }
3506 }
3507 if (l > item[groupitem[groupdepth]].maxwid)
3508 {
3509 /* truncate, remove n bytes of text at the start */
3510#ifdef FEAT_MBYTE
3511 if (has_mbyte)
3512 {
3513 /* Find the first character that should be included. */
3514 n = 0;
3515 while (l >= item[groupitem[groupdepth]].maxwid)
3516 {
3517 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003518 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
3520 }
3521 else
3522#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003523 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
3525 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003526 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 p = p - n + 1;
3528#ifdef FEAT_MBYTE
3529 /* Fill up space left over by half a double-wide char. */
3530 while (++l < item[groupitem[groupdepth]].minwid)
3531 *p++ = fillchar;
3532#endif
3533
3534 /* correct the start of the items for the truncation */
3535 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3536 {
3537 item[l].start -= n;
3538 if (item[l].start < t)
3539 item[l].start = t;
3540 }
3541 }
3542 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3543 {
3544 /* fill */
3545 n = item[groupitem[groupdepth]].minwid;
3546 if (n < 0)
3547 {
3548 /* fill by appending characters */
3549 n = 0 - n;
3550 while (l++ < n && p + 1 < out + outlen)
3551 *p++ = fillchar;
3552 }
3553 else
3554 {
3555 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003556 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 l = n - l;
3558 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003559 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 p += l;
3561 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3562 item[n].start += l;
3563 for ( ; l > 0; l--)
3564 *t++ = fillchar;
3565 }
3566 }
3567 continue;
3568 }
3569 minwid = 0;
3570 maxwid = 9999;
3571 zeropad = FALSE;
3572 l = 1;
3573 if (*s == '0')
3574 {
3575 s++;
3576 zeropad = TRUE;
3577 }
3578 if (*s == '-')
3579 {
3580 s++;
3581 l = -1;
3582 }
3583 if (VIM_ISDIGIT(*s))
3584 {
3585 minwid = (int)getdigits(&s);
3586 if (minwid < 0) /* overflow */
3587 minwid = 0;
3588 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003589 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 {
3591 item[curitem].type = Highlight;
3592 item[curitem].start = p;
3593 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3594 s++;
3595 curitem++;
3596 continue;
3597 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003598 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3599 {
3600 if (*s == STL_TABCLOSENR)
3601 {
3602 if (minwid == 0)
3603 {
3604 /* %X ends the close label, go back to the previously
3605 * define tab label nr. */
3606 for (n = curitem - 1; n >= 0; --n)
3607 if (item[n].type == TabPage && item[n].minwid >= 0)
3608 {
3609 minwid = item[n].minwid;
3610 break;
3611 }
3612 }
3613 else
3614 /* close nrs are stored as negative values */
3615 minwid = - minwid;
3616 }
3617 item[curitem].type = TabPage;
3618 item[curitem].start = p;
3619 item[curitem].minwid = minwid;
3620 s++;
3621 curitem++;
3622 continue;
3623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 if (*s == '.')
3625 {
3626 s++;
3627 if (VIM_ISDIGIT(*s))
3628 {
3629 maxwid = (int)getdigits(&s);
3630 if (maxwid <= 0) /* overflow */
3631 maxwid = 50;
3632 }
3633 }
3634 minwid = (minwid > 50 ? 50 : minwid) * l;
3635 if (*s == '(')
3636 {
3637 groupitem[groupdepth++] = curitem;
3638 item[curitem].type = Group;
3639 item[curitem].start = p;
3640 item[curitem].minwid = minwid;
3641 item[curitem].maxwid = maxwid;
3642 s++;
3643 curitem++;
3644 continue;
3645 }
3646 if (vim_strchr(STL_ALL, *s) == NULL)
3647 {
3648 s++;
3649 continue;
3650 }
3651 opt = *s++;
3652
3653 /* OK - now for the real work */
3654 base = 'D';
3655 itemisflag = FALSE;
3656 fillable = TRUE;
3657 num = -1;
3658 str = NULL;
3659 switch (opt)
3660 {
3661 case STL_FILEPATH:
3662 case STL_FULLPATH:
3663 case STL_FILENAME:
3664 fillable = FALSE; /* don't change ' ' to fillchar */
3665 if (buf_spname(wp->w_buffer) != NULL)
3666 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3667 else
3668 {
3669 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003670 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3672 }
3673 trans_characters(NameBuff, MAXPATHL);
3674 if (opt != STL_FILENAME)
3675 str = NameBuff;
3676 else
3677 str = gettail(NameBuff);
3678 break;
3679
3680 case STL_VIM_EXPR: /* '{' */
3681 itemisflag = TRUE;
3682 t = p;
3683 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3684 *p++ = *s++;
3685 if (*s != '}') /* missing '}' or out of space */
3686 break;
3687 s++;
3688 *p = 0;
3689 p = t;
3690
3691#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003692 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3694
3695 o_curbuf = curbuf;
3696 o_curwin = curwin;
3697 curwin = wp;
3698 curbuf = wp->w_buffer;
3699
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003700 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701
3702 curwin = o_curwin;
3703 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003704 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
3706 if (str != NULL && *str != 0)
3707 {
3708 if (*skipdigits(str) == NUL)
3709 {
3710 num = atoi((char *)str);
3711 vim_free(str);
3712 str = NULL;
3713 itemisflag = FALSE;
3714 }
3715 }
3716#endif
3717 break;
3718
3719 case STL_LINE:
3720 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3721 ? 0L : (long)(wp->w_cursor.lnum);
3722 break;
3723
3724 case STL_NUMLINES:
3725 num = wp->w_buffer->b_ml.ml_line_count;
3726 break;
3727
3728 case STL_COLUMN:
3729 num = !(State & INSERT) && empty_line
3730 ? 0 : (int)wp->w_cursor.col + 1;
3731 break;
3732
3733 case STL_VIRTCOL:
3734 case STL_VIRTCOL_ALT:
3735 /* In list mode virtcol needs to be recomputed */
3736 virtcol = wp->w_virtcol;
3737 if (wp->w_p_list && lcs_tab1 == NUL)
3738 {
3739 wp->w_p_list = FALSE;
3740 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3741 wp->w_p_list = TRUE;
3742 }
3743 ++virtcol;
3744 /* Don't display %V if it's the same as %c. */
3745 if (opt == STL_VIRTCOL_ALT
3746 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3747 ? 0 : (int)wp->w_cursor.col + 1)))
3748 break;
3749 num = (long)virtcol;
3750 break;
3751
3752 case STL_PERCENTAGE:
3753 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3754 (long)wp->w_buffer->b_ml.ml_line_count);
3755 break;
3756
3757 case STL_ALTPERCENT:
3758 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003759 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 break;
3761
3762 case STL_ARGLISTSTAT:
3763 fillable = FALSE;
3764 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003765 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 str = tmp;
3767 break;
3768
3769 case STL_KEYMAP:
3770 fillable = FALSE;
3771 if (get_keymap_str(wp, tmp, TMPLEN))
3772 str = tmp;
3773 break;
3774 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003775#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003776 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777#else
3778 num = 0;
3779#endif
3780 break;
3781
3782 case STL_BUFNO:
3783 num = wp->w_buffer->b_fnum;
3784 break;
3785
3786 case STL_OFFSET_X:
3787 base = 'X';
3788 case STL_OFFSET:
3789#ifdef FEAT_BYTEOFF
3790 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3791 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3792 0L : l + 1 + (!(State & INSERT) && empty_line ?
3793 0 : (int)wp->w_cursor.col);
3794#endif
3795 break;
3796
3797 case STL_BYTEVAL_X:
3798 base = 'X';
3799 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003800 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 num = 0;
3802 else
3803 {
3804#ifdef FEAT_MBYTE
3805 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3806#else
3807 num = linecont[wp->w_cursor.col];
3808#endif
3809 }
3810 if (num == NL)
3811 num = 0;
3812 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3813 num = NL;
3814 break;
3815
3816 case STL_ROFLAG:
3817 case STL_ROFLAG_ALT:
3818 itemisflag = TRUE;
3819 if (wp->w_buffer->b_p_ro)
3820 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3821 break;
3822
3823 case STL_HELPFLAG:
3824 case STL_HELPFLAG_ALT:
3825 itemisflag = TRUE;
3826 if (wp->w_buffer->b_help)
3827 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003828 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 break;
3830
3831#ifdef FEAT_AUTOCMD
3832 case STL_FILETYPE:
3833 if (*wp->w_buffer->b_p_ft != NUL
3834 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3835 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003836 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3837 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 str = tmp;
3839 }
3840 break;
3841
3842 case STL_FILETYPE_ALT:
3843 itemisflag = TRUE;
3844 if (*wp->w_buffer->b_p_ft != NUL
3845 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3846 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003847 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3848 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 for (t = tmp; *t != 0; t++)
3850 *t = TOUPPER_LOC(*t);
3851 str = tmp;
3852 }
3853 break;
3854#endif
3855
3856#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3857 case STL_PREVIEWFLAG:
3858 case STL_PREVIEWFLAG_ALT:
3859 itemisflag = TRUE;
3860 if (wp->w_p_pvw)
3861 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3862 : _("[Preview]"));
3863 break;
3864#endif
3865
3866 case STL_MODIFIED:
3867 case STL_MODIFIED_ALT:
3868 itemisflag = TRUE;
3869 switch ((opt == STL_MODIFIED_ALT)
3870 + bufIsChanged(wp->w_buffer) * 2
3871 + (!wp->w_buffer->b_p_ma) * 4)
3872 {
3873 case 2: str = (char_u *)"[+]"; break;
3874 case 3: str = (char_u *)",+"; break;
3875 case 4: str = (char_u *)"[-]"; break;
3876 case 5: str = (char_u *)",-"; break;
3877 case 6: str = (char_u *)"[+-]"; break;
3878 case 7: str = (char_u *)",+-"; break;
3879 }
3880 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003881
3882 case STL_HIGHLIGHT:
3883 t = s;
3884 while (*s != '#' && *s != NUL)
3885 ++s;
3886 if (*s == '#')
3887 {
3888 item[curitem].type = Highlight;
3889 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003890 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003891 curitem++;
3892 }
3893 ++s;
3894 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 }
3896
3897 item[curitem].start = p;
3898 item[curitem].type = Normal;
3899 if (str != NULL && *str)
3900 {
3901 t = str;
3902 if (itemisflag)
3903 {
3904 if ((t[0] && t[1])
3905 && ((!prevchar_isitem && *t == ',')
3906 || (prevchar_isflag && *t == ' ')))
3907 t++;
3908 prevchar_isflag = TRUE;
3909 }
3910 l = vim_strsize(t);
3911 if (l > 0)
3912 prevchar_isitem = TRUE;
3913 if (l > maxwid)
3914 {
3915 while (l >= maxwid)
3916#ifdef FEAT_MBYTE
3917 if (has_mbyte)
3918 {
3919 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003920 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 }
3922 else
3923#endif
3924 l -= byte2cells(*t++);
3925 if (p + 1 >= out + outlen)
3926 break;
3927 *p++ = '<';
3928 }
3929 if (minwid > 0)
3930 {
3931 for (; l < minwid && p + 1 < out + outlen; l++)
3932 {
3933 /* Don't put a "-" in front of a digit. */
3934 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3935 *p++ = ' ';
3936 else
3937 *p++ = fillchar;
3938 }
3939 minwid = 0;
3940 }
3941 else
3942 minwid *= -1;
3943 while (*t && p + 1 < out + outlen)
3944 {
3945 *p++ = *t++;
3946 /* Change a space by fillchar, unless fillchar is '-' and a
3947 * digit follows. */
3948 if (fillable && p[-1] == ' '
3949 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3950 p[-1] = fillchar;
3951 }
3952 for (; l < minwid && p + 1 < out + outlen; l++)
3953 *p++ = fillchar;
3954 }
3955 else if (num >= 0)
3956 {
3957 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3958 char_u nstr[20];
3959
3960 if (p + 20 >= out + outlen)
3961 break; /* not sufficient space */
3962 prevchar_isitem = TRUE;
3963 t = nstr;
3964 if (opt == STL_VIRTCOL_ALT)
3965 {
3966 *t++ = '-';
3967 minwid--;
3968 }
3969 *t++ = '%';
3970 if (zeropad)
3971 *t++ = '0';
3972 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003973 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 *t = 0;
3975
3976 for (n = num, l = 1; n >= nbase; n /= nbase)
3977 l++;
3978 if (opt == STL_VIRTCOL_ALT)
3979 l++;
3980 if (l > maxwid)
3981 {
3982 l += 2;
3983 n = l - maxwid;
3984 while (l-- > maxwid)
3985 num /= nbase;
3986 *t++ = '>';
3987 *t++ = '%';
3988 *t = t[-3];
3989 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003990 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3991 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003994 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3995 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 p += STRLEN(p);
3997 }
3998 else
3999 item[curitem].type = Empty;
4000
4001 if (opt == STL_VIM_EXPR)
4002 vim_free(str);
4003
4004 if (num >= 0 || (!itemisflag && str && *str))
4005 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4006 curitem++;
4007 }
4008 *p = NUL;
4009 itemcnt = curitem;
4010
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004011#ifdef FEAT_EVAL
4012 if (usefmt != fmt)
4013 vim_free(usefmt);
4014#endif
4015
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 width = vim_strsize(out);
4017 if (maxwidth > 0 && width > maxwidth)
4018 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004019 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 l = 0;
4021 if (itemcnt == 0)
4022 s = out;
4023 else
4024 {
4025 for ( ; l < itemcnt; l++)
4026 if (item[l].type == Trunc)
4027 {
4028 /* Truncate at %< item. */
4029 s = item[l].start;
4030 break;
4031 }
4032 if (l == itemcnt)
4033 {
4034 /* No %< item, truncate first item. */
4035 s = item[0].start;
4036 l = 0;
4037 }
4038 }
4039
4040 if (width - vim_strsize(s) >= maxwidth)
4041 {
4042 /* Truncation mark is beyond max length */
4043#ifdef FEAT_MBYTE
4044 if (has_mbyte)
4045 {
4046 s = out;
4047 width = 0;
4048 for (;;)
4049 {
4050 width += ptr2cells(s);
4051 if (width >= maxwidth)
4052 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004053 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 }
4055 /* Fill up for half a double-wide character. */
4056 while (++width < maxwidth)
4057 *s++ = fillchar;
4058 }
4059 else
4060#endif
4061 s = out + maxwidth - 1;
4062 for (l = 0; l < itemcnt; l++)
4063 if (item[l].start > s)
4064 break;
4065 itemcnt = l;
4066 *s++ = '>';
4067 *s = 0;
4068 }
4069 else
4070 {
4071#ifdef FEAT_MBYTE
4072 if (has_mbyte)
4073 {
4074 n = 0;
4075 while (width >= maxwidth)
4076 {
4077 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004078 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 }
4080 }
4081 else
4082#endif
4083 n = width - maxwidth + 1;
4084 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004085 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 *s = '<';
4087
4088 /* Fill up for half a double-wide character. */
4089 while (++width < maxwidth)
4090 {
4091 s = s + STRLEN(s);
4092 *s++ = fillchar;
4093 *s = NUL;
4094 }
4095
4096 --n; /* count the '<' */
4097 for (; l < itemcnt; l++)
4098 {
4099 if (item[l].start - n >= s)
4100 item[l].start -= n;
4101 else
4102 item[l].start = s;
4103 }
4104 }
4105 width = maxwidth;
4106 }
4107 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4108 {
4109 /* Apply STL_MIDDLE if any */
4110 for (l = 0; l < itemcnt; l++)
4111 if (item[l].type == Middle)
4112 break;
4113 if (l < itemcnt)
4114 {
4115 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004116 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 for (s = item[l].start; s < p; s++)
4118 *s = fillchar;
4119 for (l++; l < itemcnt; l++)
4120 item[l].start += maxwidth - width;
4121 width = maxwidth;
4122 }
4123 }
4124
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004125 /* Store the info about highlighting. */
4126 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004128 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 for (l = 0; l < itemcnt; l++)
4130 {
4131 if (item[l].type == Highlight)
4132 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004133 sp->start = item[l].start;
4134 sp->userhl = item[l].minwid;
4135 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
4137 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004138 sp->start = NULL;
4139 sp->userhl = 0;
4140 }
4141
4142 /* Store the info about tab pages labels. */
4143 if (tabtab != NULL)
4144 {
4145 sp = tabtab;
4146 for (l = 0; l < itemcnt; l++)
4147 {
4148 if (item[l].type == TabPage)
4149 {
4150 sp->start = item[l].start;
4151 sp->userhl = item[l].minwid;
4152 sp++;
4153 }
4154 }
4155 sp->start = NULL;
4156 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 }
4158
4159 return width;
4160}
4161#endif /* FEAT_STL_OPT */
4162
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004163#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4164 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004166 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4167 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 */
4169 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004170get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004172 char_u *buf;
4173 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174{
4175 long above; /* number of lines above window */
4176 long below; /* number of lines below window */
4177
4178 above = wp->w_topline - 1;
4179#ifdef FEAT_DIFF
4180 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4181#endif
4182 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4183 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004184 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4185 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004187 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004189 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 ? (int)(above / ((above + below) / 100L))
4191 : (int)(above * 100L / (above + below)));
4192}
4193#endif
4194
4195/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004196 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 * Return TRUE if it was appended.
4198 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004199 static int
4200append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 win_T *wp;
4202 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004203 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205{
4206 char_u *p;
4207
4208 if (ARGCOUNT <= 1) /* nothing to do */
4209 return FALSE;
4210
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004211 p = buf + STRLEN(buf); /* go to the end of the buffer */
4212 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 return FALSE;
4214 *p++ = ' ';
4215 *p++ = '(';
4216 if (add_file)
4217 {
4218 STRCPY(p, "file ");
4219 p += 5;
4220 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004221 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4222 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4224 return TRUE;
4225}
4226
4227/*
4228 * If fname is not a full path, make it a full path.
4229 * Returns pointer to allocated memory (NULL for failure).
4230 */
4231 char_u *
4232fix_fname(fname)
4233 char_u *fname;
4234{
4235 /*
4236 * Force expanding the path always for Unix, because symbolic links may
4237 * mess up the full path name, even though it starts with a '/'.
4238 * Also expand when there is ".." in the file name, try to remove it,
4239 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004240 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 * For MS-Windows also expand names like "longna~1" to "longname".
4242 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004243#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 return FullName_save(fname, TRUE);
4245#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004246 if (!vim_isAbsName(fname)
4247 || strstr((char *)fname, "..") != NULL
4248 || strstr((char *)fname, "//") != NULL
4249# ifdef BACKSLASH_IN_FILENAME
4250 || strstr((char *)fname, "\\\\") != NULL
4251# endif
4252# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004254# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 )
4256 return FullName_save(fname, FALSE);
4257
4258 fname = vim_strsave(fname);
4259
Bram Moolenaar9b942202007-10-03 12:31:33 +00004260# ifdef USE_FNAME_CASE
4261# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 {
4265 if (fname != NULL)
4266 fname_case(fname, 0); /* set correct case for file name */
4267 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269
4270 return fname;
4271#endif
4272}
4273
4274/*
4275 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4276 * "ffname" becomes a pointer to allocated memory (or NULL).
4277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 void
4279fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004280 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 char_u **ffname;
4282 char_u **sfname;
4283{
4284 if (*ffname == NULL) /* if no file name given, nothing to do */
4285 return;
4286 if (*sfname == NULL) /* if no short file name given, use ffname */
4287 *sfname = *ffname;
4288 *ffname = fix_fname(*ffname); /* expand to full path */
4289
4290#ifdef FEAT_SHORTCUT
4291 if (!buf->b_p_bin)
4292 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004293 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294
4295 /* If the file name is a shortcut file, use the file it links to. */
4296 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004297 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 {
4299 vim_free(*ffname);
4300 *ffname = rfname;
4301 *sfname = rfname;
4302 }
4303 }
4304#endif
4305}
4306
4307/*
4308 * Get the file name for an argument list entry.
4309 */
4310 char_u *
4311alist_name(aep)
4312 aentry_T *aep;
4313{
4314 buf_T *bp;
4315
4316 /* Use the name from the associated buffer if it exists. */
4317 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004318 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 return aep->ae_fname;
4320 return bp->b_fname;
4321}
4322
4323#if defined(FEAT_WINDOWS) || defined(PROTO)
4324/*
4325 * do_arg_all(): Open up to 'count' windows, one for each argument.
4326 */
4327 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004328do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 int count;
4330 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004331 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332{
4333 int i;
4334 win_T *wp, *wpnext;
4335 char_u *opened; /* array of flags for which args are open */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004336 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 int use_firstwin = FALSE; /* use first window for arglist */
4338 int split_ret = OK;
4339 int p_ea_save;
4340 alist_T *alist; /* argument list to be used */
4341 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004342 tabpage_T *tpnext;
4343 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004344 win_T *new_curwin = NULL;
4345 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
4347 if (ARGCOUNT <= 0)
4348 {
4349 /* Don't give an error message. We don't want it when the ":all"
4350 * command is in the .vimrc. */
4351 return;
4352 }
4353 setpcmark();
4354
4355 opened_len = ARGCOUNT;
4356 opened = alloc_clear((unsigned)opened_len);
4357 if (opened == NULL)
4358 return;
4359
4360#ifdef FEAT_GUI
4361 need_mouse_correct = TRUE;
4362#endif
4363
4364 /*
4365 * Try closing all windows that are not in the argument list.
4366 * Also close windows that are not full width;
4367 * When 'hidden' or "forceit" set the buffer becomes hidden.
4368 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004369 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004371 if (had_tab > 0)
4372 goto_tabpage_tp(first_tabpage);
4373 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004375 tpnext = curtab->tp_next;
4376 for (wp = firstwin; wp != NULL; wp = wpnext)
4377 {
4378 wpnext = wp->w_next;
4379 buf = wp->w_buffer;
4380 if (buf->b_ffname == NULL
4381 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004383 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004385 )
4386 i = ARGCOUNT;
4387 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004389 /* check if the buffer in this window is in the arglist */
4390 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004392 if (ARGLIST[i].ae_fnum == buf->b_fnum
4393 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004394 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004396 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004397 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004398 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004399 if (i == 0)
4400 {
4401 new_curwin = wp;
4402 new_curtab = curtab;
4403 }
4404 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004405 if (wp->w_alist != curwin->w_alist)
4406 {
4407 /* Use the current argument list for all windows
4408 * containing a file from it. */
4409 alist_unlink(wp->w_alist);
4410 wp->w_alist = curwin->w_alist;
4411 ++wp->w_alist->al_refcount;
4412 }
4413 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
4416 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004417 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004419 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004421 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004422 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004424 /* If the buffer was changed, and we would like to hide it,
4425 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004426 if (!P_HID(buf) && buf->b_nwindows <= 1
4427 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004429 (void)autowrite(buf, FALSE);
4430#ifdef FEAT_AUTOCMD
4431 /* check if autocommands removed the window */
4432 if (!win_valid(wp) || !buf_valid(buf))
4433 {
4434 wpnext = firstwin; /* start all over... */
4435 continue;
4436 }
4437#endif
4438 }
4439#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004440 /* don't close last window */
4441 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004442#endif
4443 use_firstwin = TRUE;
4444#ifdef FEAT_WINDOWS
4445 else
4446 {
4447 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4448# ifdef FEAT_AUTOCMD
4449 /* check if autocommands removed the next window */
4450 if (!win_valid(wpnext))
4451 wpnext = firstwin; /* start all over... */
4452# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 }
4454#endif
4455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 }
4457 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004458
4459 /* Without the ":tab" modifier only do the current tab page. */
4460 if (had_tab == 0 || tpnext == NULL)
4461 break;
4462
4463# ifdef FEAT_AUTOCMD
4464 /* check if autocommands removed the next tab page */
4465 if (!valid_tabpage(tpnext))
4466 tpnext = first_tabpage; /* start all over...*/
4467# endif
4468 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470
4471 /*
4472 * Open a window for files in the argument list that don't have one.
4473 * ARGCOUNT may change while doing this, because of autocommands.
4474 */
4475 if (count > ARGCOUNT || count <= 0)
4476 count = ARGCOUNT;
4477
4478 /* Autocommands may do anything to the argument list. Make sure it's not
4479 * freed while we are working here by "locking" it. We still have to
4480 * watch out for its size to be changed. */
4481 alist = curwin->w_alist;
4482 ++alist->al_refcount;
4483
4484#ifdef FEAT_AUTOCMD
4485 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4486 ++autocmd_no_enter;
4487 ++autocmd_no_leave;
4488#endif
4489 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004490#ifdef FEAT_WINDOWS
4491 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4492 * leaving an empty tab page when executed locally. */
4493 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4494 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4495 use_firstwin = TRUE;
4496#endif
4497
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4499 {
4500 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4501 arg_had_last = TRUE;
4502 if (i < opened_len && opened[i])
4503 {
4504 /* Move the already present window to below the current window */
4505 if (curwin->w_arg_idx != i)
4506 {
4507 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4508 {
4509 if (wpnext->w_arg_idx == i)
4510 {
4511 win_move_after(wpnext, curwin);
4512 break;
4513 }
4514 }
4515 }
4516 }
4517 else if (split_ret == OK)
4518 {
4519 if (!use_firstwin) /* split current window */
4520 {
4521 p_ea_save = p_ea;
4522 p_ea = TRUE; /* use space from all windows */
4523 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4524 p_ea = p_ea_save;
4525 if (split_ret == FAIL)
4526 continue;
4527 }
4528#ifdef FEAT_AUTOCMD
4529 else /* first window: do autocmd for leaving this buffer */
4530 --autocmd_no_leave;
4531#endif
4532
4533 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004534 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 */
4536 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004537 if (i == 0)
4538 {
4539 new_curwin = curwin;
4540 new_curtab = curtab;
4541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4543 ECMD_ONE,
4544 ((P_HID(curwin->w_buffer)
4545 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004546 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547#ifdef FEAT_AUTOCMD
4548 if (use_firstwin)
4549 ++autocmd_no_leave;
4550#endif
4551 use_firstwin = FALSE;
4552 }
4553 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004554
4555 /* When ":tab" was used open a new tab for a new window repeatedly. */
4556 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4557 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 }
4559
4560 /* Remove the "lock" on the argument list. */
4561 alist_unlink(alist);
4562
4563#ifdef FEAT_AUTOCMD
4564 --autocmd_no_enter;
4565#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004566 /* to window with first arg */
4567 if (valid_tabpage(new_curtab))
4568 goto_tabpage_tp(new_curtab);
4569 if (win_valid(new_curwin))
4570 win_enter(new_curwin, FALSE);
4571
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572#ifdef FEAT_AUTOCMD
4573 --autocmd_no_leave;
4574#endif
4575 vim_free(opened);
4576}
4577
4578# if defined(FEAT_LISTCMDS) || defined(PROTO)
4579/*
4580 * Open a window for a number of buffers.
4581 */
4582 void
4583ex_buffer_all(eap)
4584 exarg_T *eap;
4585{
4586 buf_T *buf;
4587 win_T *wp, *wpnext;
4588 int split_ret = OK;
4589 int p_ea_save;
4590 int open_wins = 0;
4591 int r;
4592 int count; /* Maximum number of windows to open. */
4593 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004594#ifdef FEAT_WINDOWS
4595 int had_tab = cmdmod.tab;
4596 tabpage_T *tpnext;
4597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598
4599 if (eap->addr_count == 0) /* make as many windows as possible */
4600 count = 9999;
4601 else
4602 count = eap->line2; /* make as many windows as specified */
4603 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4604 all = FALSE;
4605 else
4606 all = TRUE;
4607
4608 setpcmark();
4609
4610#ifdef FEAT_GUI
4611 need_mouse_correct = TRUE;
4612#endif
4613
4614 /*
4615 * Close superfluous windows (two windows for the same buffer).
4616 * Also close windows that are not full-width.
4617 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004618#ifdef FEAT_WINDOWS
4619 if (had_tab > 0)
4620 goto_tabpage_tp(first_tabpage);
4621 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004624 tpnext = curtab->tp_next;
4625 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004627 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004628 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004629#ifdef FEAT_VERTSPLIT
4630 || ((cmdmod.split & WSP_VERT)
4631 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004632 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004633 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004635#ifdef FEAT_WINDOWS
4636 || (had_tab > 0 && wp != firstwin)
4637#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004638 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004639 {
4640 win_close(wp, FALSE);
4641#ifdef FEAT_AUTOCMD
4642 wpnext = firstwin; /* just in case an autocommand does
4643 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004644 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004645 open_wins = 0;
4646#endif
4647 }
4648 else
4649 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004651
4652#ifdef FEAT_WINDOWS
4653 /* Without the ":tab" modifier only do the current tab page. */
4654 if (had_tab == 0 || tpnext == NULL)
4655 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004656 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659
4660 /*
4661 * Go through the buffer list. When a buffer doesn't have a window yet,
4662 * open one. Otherwise move the window to the right position.
4663 * Watch out for autocommands that delete buffers or windows!
4664 */
4665#ifdef FEAT_AUTOCMD
4666 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4667 ++autocmd_no_enter;
4668#endif
4669 win_enter(lastwin, FALSE);
4670#ifdef FEAT_AUTOCMD
4671 ++autocmd_no_leave;
4672#endif
4673 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4674 {
4675 /* Check if this buffer needs a window */
4676 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4677 continue;
4678
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004679#ifdef FEAT_WINDOWS
4680 if (had_tab != 0)
4681 {
4682 /* With the ":tab" modifier don't move the window. */
4683 if (buf->b_nwindows > 0)
4684 wp = lastwin; /* buffer has a window, skip it */
4685 else
4686 wp = NULL;
4687 }
4688 else
4689#endif
4690 {
4691 /* Check if this buffer already has a window */
4692 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4693 if (wp->w_buffer == buf)
4694 break;
4695 /* If the buffer already has a window, move it */
4696 if (wp != NULL)
4697 win_move_after(wp, curwin);
4698 }
4699
4700 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 {
4702 /* Split the window and put the buffer in it */
4703 p_ea_save = p_ea;
4704 p_ea = TRUE; /* use space from all windows */
4705 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4706 ++open_wins;
4707 p_ea = p_ea_save;
4708 if (split_ret == FAIL)
4709 continue;
4710
4711 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004712#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 swap_exists_action = SEA_DIALOG;
4714#endif
4715 set_curbuf(buf, DOBUF_GOTO);
4716#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004719#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004721# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 break;
4723 }
4724#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004725#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 if (swap_exists_action == SEA_QUIT)
4727 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004728# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4729 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004730
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004731 /* Reset the error/interrupt/exception state here so that
4732 * aborting() returns FALSE when closing a window. */
4733 enter_cleanup(&cs);
4734# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004735
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004736 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 win_close(curwin, TRUE);
4738 --open_wins;
4739 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004740 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004741
4742# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4743 /* Restore the error/interrupt/exception state if not
4744 * discarded by a new aborting error, interrupt, or uncaught
4745 * exception. */
4746 leave_cleanup(&cs);
4747# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 }
4749 else
4750 handle_swap_exists(NULL);
4751#endif
4752 }
4753
4754 ui_breakcheck();
4755 if (got_int)
4756 {
4757 (void)vgetc(); /* only break the file loading, not the rest */
4758 break;
4759 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004760#ifdef FEAT_EVAL
4761 /* Autocommands deleted the buffer or aborted script processing!!! */
4762 if (aborting())
4763 break;
4764#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004765#ifdef FEAT_WINDOWS
4766 /* When ":tab" was used open a new tab for a new window repeatedly. */
4767 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4768 cmdmod.tab = 9999;
4769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771#ifdef FEAT_AUTOCMD
4772 --autocmd_no_enter;
4773#endif
4774 win_enter(firstwin, FALSE); /* back to first window */
4775#ifdef FEAT_AUTOCMD
4776 --autocmd_no_leave;
4777#endif
4778
4779 /*
4780 * Close superfluous windows.
4781 */
4782 for (wp = lastwin; open_wins > count; )
4783 {
4784 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4785 || autowrite(wp->w_buffer, FALSE) == OK);
4786#ifdef FEAT_AUTOCMD
4787 if (!win_valid(wp))
4788 {
4789 /* BufWrite Autocommands made the window invalid, start over */
4790 wp = lastwin;
4791 }
4792 else
4793#endif
4794 if (r)
4795 {
4796 win_close(wp, !P_HID(wp->w_buffer));
4797 --open_wins;
4798 wp = lastwin;
4799 }
4800 else
4801 {
4802 wp = wp->w_prev;
4803 if (wp == NULL)
4804 break;
4805 }
4806 }
4807}
4808# endif /* FEAT_LISTCMDS */
4809
4810#endif /* FEAT_WINDOWS */
4811
Bram Moolenaara3227e22006-03-08 21:32:40 +00004812static int chk_modeline __ARGS((linenr_T, int));
4813
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814/*
4815 * do_modelines() - process mode lines for the current file
4816 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004817 * "flags" can be:
4818 * OPT_WINONLY only set options local to window
4819 * OPT_NOWIN don't set options local to window
4820 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 * Returns immediately if the "ml" option isn't set.
4822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004824do_modelines(flags)
4825 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004827 linenr_T lnum;
4828 int nmlines;
4829 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830
4831 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4832 return;
4833
4834 /* Disallow recursive entry here. Can happen when executing a modeline
4835 * triggers an autocommand, which reloads modelines with a ":do". */
4836 if (entered)
4837 return;
4838
4839 ++entered;
4840 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4841 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004842 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 nmlines = 0;
4844
4845 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4846 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004847 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 nmlines = 0;
4849 --entered;
4850}
4851
4852#include "version.h" /* for version number */
4853
4854/*
4855 * chk_modeline() - check a single line for a mode string
4856 * Return FAIL if an error encountered.
4857 */
4858 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004859chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004861 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
4863 char_u *s;
4864 char_u *e;
4865 char_u *linecopy; /* local copy of any modeline found */
4866 int prev;
4867 int vers;
4868 int end;
4869 int retval = OK;
4870 char_u *save_sourcing_name;
4871 linenr_T save_sourcing_lnum;
4872#ifdef FEAT_EVAL
4873 scid_T save_SID;
4874#endif
4875
4876 prev = -1;
4877 for (s = ml_get(lnum); *s != NUL; ++s)
4878 {
4879 if (prev == -1 || vim_isspace(prev))
4880 {
4881 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4882 || STRNCMP(s, "vi:", (size_t)3) == 0)
4883 break;
4884 if (STRNCMP(s, "vim", 3) == 0)
4885 {
4886 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4887 e = s + 4;
4888 else
4889 e = s + 3;
4890 vers = getdigits(&e);
4891 if (*e == ':'
4892 && (s[3] == ':'
4893 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4894 || (VIM_VERSION_100 < vers && s[3] == '<')
4895 || (VIM_VERSION_100 > vers && s[3] == '>')
4896 || (VIM_VERSION_100 == vers && s[3] == '=')))
4897 break;
4898 }
4899 }
4900 prev = *s;
4901 }
4902
4903 if (*s)
4904 {
4905 do /* skip over "ex:", "vi:" or "vim:" */
4906 ++s;
4907 while (s[-1] != ':');
4908
4909 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4910 if (linecopy == NULL)
4911 return FAIL;
4912
4913 save_sourcing_lnum = sourcing_lnum;
4914 save_sourcing_name = sourcing_name;
4915 sourcing_lnum = lnum; /* prepare for emsg() */
4916 sourcing_name = (char_u *)"modelines";
4917
4918 end = FALSE;
4919 while (end == FALSE)
4920 {
4921 s = skipwhite(s);
4922 if (*s == NUL)
4923 break;
4924
4925 /*
4926 * Find end of set command: ':' or end of line.
4927 * Skip over "\:", replacing it with ":".
4928 */
4929 for (e = s; *e != ':' && *e != NUL; ++e)
4930 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00004931 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (*e == NUL)
4933 end = TRUE;
4934
4935 /*
4936 * If there is a "set" command, require a terminating ':' and
4937 * ignore the stuff after the ':'.
4938 * "vi:set opt opt opt: foo" -- foo not interpreted
4939 * "vi:opt opt opt: foo" -- foo interpreted
4940 * Accept "se" for compatibility with Elvis.
4941 */
4942 if (STRNCMP(s, "set ", (size_t)4) == 0
4943 || STRNCMP(s, "se ", (size_t)3) == 0)
4944 {
4945 if (*e != ':') /* no terminating ':'? */
4946 break;
4947 end = TRUE;
4948 s = vim_strchr(s, ' ') + 1;
4949 }
4950 *e = NUL; /* truncate the set command */
4951
4952 if (*s != NUL) /* skip over an empty "::" */
4953 {
4954#ifdef FEAT_EVAL
4955 save_SID = current_SID;
4956 current_SID = SID_MODELINE;
4957#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004958 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959#ifdef FEAT_EVAL
4960 current_SID = save_SID;
4961#endif
4962 if (retval == FAIL) /* stop if error found */
4963 break;
4964 }
4965 s = e + 1; /* advance to next part */
4966 }
4967
4968 sourcing_lnum = save_sourcing_lnum;
4969 sourcing_name = save_sourcing_name;
4970
4971 vim_free(linecopy);
4972 }
4973 return retval;
4974}
4975
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00004976#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 int
4978read_viminfo_bufferlist(virp, writing)
4979 vir_T *virp;
4980 int writing;
4981{
4982 char_u *tab;
4983 linenr_T lnum;
4984 colnr_T col;
4985 buf_T *buf;
4986 char_u *sfname;
4987 char_u *xline;
4988
4989 /* Handle long line and escaped characters. */
4990 xline = viminfo_readstring(virp, 1, FALSE);
4991
4992 /* don't read in if there are files on the command-line or if writing: */
4993 if (xline != NULL && !writing && ARGCOUNT == 0
4994 && find_viminfo_parameter('%') != NULL)
4995 {
4996 /* Format is: <fname> Tab <lnum> Tab <col>.
4997 * Watch out for a Tab in the file name, work from the end. */
4998 lnum = 0;
4999 col = 0;
5000 tab = vim_strrchr(xline, '\t');
5001 if (tab != NULL)
5002 {
5003 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005004 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 tab = vim_strrchr(xline, '\t');
5006 if (tab != NULL)
5007 {
5008 *tab++ = '\0';
5009 lnum = atol((char *)tab);
5010 }
5011 }
5012
5013 /* Expand "~/" in the file name at "line + 1" to a full path.
5014 * Then try shortening it by comparing with the current directory */
5015 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005016 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
5018 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5019 if (buf != NULL) /* just in case... */
5020 {
5021 buf->b_last_cursor.lnum = lnum;
5022 buf->b_last_cursor.col = col;
5023 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5024 }
5025 }
5026 vim_free(xline);
5027
5028 return viminfo_readline(virp);
5029}
5030
5031 void
5032write_viminfo_bufferlist(fp)
5033 FILE *fp;
5034{
5035 buf_T *buf;
5036#ifdef FEAT_WINDOWS
5037 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005038 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039#endif
5040 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005041 int max_buffers;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005042 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043
5044 if (find_viminfo_parameter('%') == NULL)
5045 return;
5046
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005047 /* Without a number -1 is returned: do all buffers. */
5048 max_buffers = get_viminfo_parameter('%');
5049
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005051#define LINE_BUF_LEN (MAXPATHL + 40)
5052 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 if (line == NULL)
5054 return;
5055
5056#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005057 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 set_last_cursor(win);
5059#else
5060 set_last_cursor(curwin);
5061#endif
5062
5063 fprintf(fp, _("\n# Buffer list:\n"));
5064 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5065 {
5066 if (buf->b_fname == NULL
5067 || !buf->b_p_bl
5068#ifdef FEAT_QUICKFIX
5069 || bt_quickfix(buf)
5070#endif
5071 || removable(buf->b_ffname))
5072 continue;
5073
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005074 if (max_buffers-- == 0)
5075 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 putc('%', fp);
5077 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005078 len = STRLEN(line);
5079 vim_snprintf((char *)line + len, len - LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 (long)buf->b_last_cursor.lnum,
5081 buf->b_last_cursor.col);
5082 viminfo_writestring(fp, line);
5083 }
5084 vim_free(line);
5085}
5086#endif
5087
5088
5089/*
5090 * Return special buffer name.
5091 * Returns NULL when the buffer has a normal file name.
5092 */
5093 char *
5094buf_spname(buf)
5095 buf_T *buf;
5096{
5097#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5098 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005099 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005100 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005101 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005102
5103 /*
5104 * For location list window, w_llist_ref points to the location list.
5105 * For quickfix window, w_llist_ref is NULL.
5106 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005107 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005108 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005109 goto win_found;
5110win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005111 if (win != NULL && win->w_llist_ref != NULL)
5112 return _("[Location List]");
5113 else
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005114 return _("[Quickfix List]");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116#endif
5117#ifdef FEAT_QUICKFIX
5118 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5119 * contains the name as specified by the user */
5120 if (bt_nofile(buf))
5121 {
5122 if (buf->b_sfname != NULL)
5123 return (char *)buf->b_sfname;
Bram Moolenaarf4f664c2008-12-03 10:21:57 +00005124 return _("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
5126#endif
5127 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005128 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 return NULL;
5130}
5131
5132
5133#if defined(FEAT_SIGNS) || defined(PROTO)
5134/*
5135 * Insert the sign into the signlist.
5136 */
5137 static void
5138insert_sign(buf, prev, next, id, lnum, typenr)
5139 buf_T *buf; /* buffer to store sign in */
5140 signlist_T *prev; /* previous sign entry */
5141 signlist_T *next; /* next sign entry */
5142 int id; /* sign ID */
5143 linenr_T lnum; /* line number which gets the mark */
5144 int typenr; /* typenr of sign we are adding */
5145{
5146 signlist_T *newsign;
5147
5148 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5149 if (newsign != NULL)
5150 {
5151 newsign->id = id;
5152 newsign->lnum = lnum;
5153 newsign->typenr = typenr;
5154 newsign->next = next;
5155#ifdef FEAT_NETBEANS_INTG
5156 newsign->prev = prev;
5157 if (next != NULL)
5158 next->prev = newsign;
5159#endif
5160
5161 if (prev == NULL)
5162 {
5163 /* When adding first sign need to redraw the windows to create the
5164 * column for signs. */
5165 if (buf->b_signlist == NULL)
5166 {
5167 redraw_buf_later(buf, NOT_VALID);
5168 changed_cline_bef_curs();
5169 }
5170
5171 /* first sign in signlist */
5172 buf->b_signlist = newsign;
5173 }
5174 else
5175 prev->next = newsign;
5176 }
5177}
5178
5179/*
5180 * Add the sign into the signlist. Find the right spot to do it though.
5181 */
5182 void
5183buf_addsign(buf, id, lnum, typenr)
5184 buf_T *buf; /* buffer to store sign in */
5185 int id; /* sign ID */
5186 linenr_T lnum; /* line number which gets the mark */
5187 int typenr; /* typenr of sign we are adding */
5188{
5189 signlist_T *sign; /* a sign in the signlist */
5190 signlist_T *prev; /* the previous sign */
5191
5192 prev = NULL;
5193 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5194 {
5195 if (lnum == sign->lnum && id == sign->id)
5196 {
5197 sign->typenr = typenr;
5198 return;
5199 }
5200 else if (
5201#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5202 id < 0 &&
5203#endif
5204 lnum < sign->lnum)
5205 {
5206#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5207 /* XXX - GRP: Is this because of sign slide problem? Or is it
5208 * really needed? Or is it because we allow multiple signs per
5209 * line? If so, should I add that feature to FEAT_SIGNS?
5210 */
5211 while (prev != NULL && prev->lnum == lnum)
5212 prev = prev->prev;
5213 if (prev == NULL)
5214 sign = buf->b_signlist;
5215 else
5216 sign = prev->next;
5217#endif
5218 insert_sign(buf, prev, sign, id, lnum, typenr);
5219 return;
5220 }
5221 prev = sign;
5222 }
5223#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5224 /* XXX - GRP: See previous comment */
5225 while (prev != NULL && prev->lnum == lnum)
5226 prev = prev->prev;
5227 if (prev == NULL)
5228 sign = buf->b_signlist;
5229 else
5230 sign = prev->next;
5231#endif
5232 insert_sign(buf, prev, sign, id, lnum, typenr);
5233
5234 return;
5235}
5236
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005237 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238buf_change_sign_type(buf, markId, typenr)
5239 buf_T *buf; /* buffer to store sign in */
5240 int markId; /* sign ID */
5241 int typenr; /* typenr of sign we are adding */
5242{
5243 signlist_T *sign; /* a sign in the signlist */
5244
5245 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5246 {
5247 if (sign->id == markId)
5248 {
5249 sign->typenr = typenr;
5250 return sign->lnum;
5251 }
5252 }
5253
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005254 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255}
5256
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005257 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258buf_getsigntype(buf, lnum, type)
5259 buf_T *buf;
5260 linenr_T lnum;
5261 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5262{
5263 signlist_T *sign; /* a sign in a b_signlist */
5264
5265 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5266 if (sign->lnum == lnum
5267 && (type == SIGN_ANY
5268# ifdef FEAT_SIGN_ICONS
5269 || (type == SIGN_ICON
5270 && sign_get_image(sign->typenr) != NULL)
5271# endif
5272 || (type == SIGN_TEXT
5273 && sign_get_text(sign->typenr) != NULL)
5274 || (type == SIGN_LINEHL
5275 && sign_get_attr(sign->typenr, TRUE) != 0)))
5276 return sign->typenr;
5277 return 0;
5278}
5279
5280
5281 linenr_T
5282buf_delsign(buf, id)
5283 buf_T *buf; /* buffer sign is stored in */
5284 int id; /* sign id */
5285{
5286 signlist_T **lastp; /* pointer to pointer to current sign */
5287 signlist_T *sign; /* a sign in a b_signlist */
5288 signlist_T *next; /* the next sign in a b_signlist */
5289 linenr_T lnum; /* line number whose sign was deleted */
5290
5291 lastp = &buf->b_signlist;
5292 lnum = 0;
5293 for (sign = buf->b_signlist; sign != NULL; sign = next)
5294 {
5295 next = sign->next;
5296 if (sign->id == id)
5297 {
5298 *lastp = next;
5299#ifdef FEAT_NETBEANS_INTG
5300 if (next != NULL)
5301 next->prev = sign->prev;
5302#endif
5303 lnum = sign->lnum;
5304 vim_free(sign);
5305 break;
5306 }
5307 else
5308 lastp = &sign->next;
5309 }
5310
5311 /* When deleted the last sign need to redraw the windows to remove the
5312 * sign column. */
5313 if (buf->b_signlist == NULL)
5314 {
5315 redraw_buf_later(buf, NOT_VALID);
5316 changed_cline_bef_curs();
5317 }
5318
5319 return lnum;
5320}
5321
5322
5323/*
5324 * Find the line number of the sign with the requested id. If the sign does
5325 * not exist, return 0 as the line number. This will still let the correct file
5326 * get loaded.
5327 */
5328 int
5329buf_findsign(buf, id)
5330 buf_T *buf; /* buffer to store sign in */
5331 int id; /* sign ID */
5332{
5333 signlist_T *sign; /* a sign in the signlist */
5334
5335 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5336 if (sign->id == id)
5337 return sign->lnum;
5338
5339 return 0;
5340}
5341
5342 int
5343buf_findsign_id(buf, lnum)
5344 buf_T *buf; /* buffer whose sign we are searching for */
5345 linenr_T lnum; /* line number of sign */
5346{
5347 signlist_T *sign; /* a sign in the signlist */
5348
5349 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5350 if (sign->lnum == lnum)
5351 return sign->id;
5352
5353 return 0;
5354}
5355
5356
5357# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5358/* see if a given type of sign exists on a specific line */
5359 int
5360buf_findsigntype_id(buf, lnum, typenr)
5361 buf_T *buf; /* buffer whose sign we are searching for */
5362 linenr_T lnum; /* line number of sign */
5363 int typenr; /* sign type number */
5364{
5365 signlist_T *sign; /* a sign in the signlist */
5366
5367 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5368 if (sign->lnum == lnum && sign->typenr == typenr)
5369 return sign->id;
5370
5371 return 0;
5372}
5373
5374
5375# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5376/* return the number of icons on the given line */
5377 int
5378buf_signcount(buf, lnum)
5379 buf_T *buf;
5380 linenr_T lnum;
5381{
5382 signlist_T *sign; /* a sign in the signlist */
5383 int count = 0;
5384
5385 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5386 if (sign->lnum == lnum)
5387 if (sign_get_image(sign->typenr) != NULL)
5388 count++;
5389
5390 return count;
5391}
5392# endif /* FEAT_SIGN_ICONS */
5393# endif /* FEAT_NETBEANS_INTG */
5394
5395
5396/*
5397 * Delete signs in buffer "buf".
5398 */
5399 static void
5400buf_delete_signs(buf)
5401 buf_T *buf;
5402{
5403 signlist_T *next;
5404
5405 while (buf->b_signlist != NULL)
5406 {
5407 next = buf->b_signlist->next;
5408 vim_free(buf->b_signlist);
5409 buf->b_signlist = next;
5410 }
5411}
5412
5413/*
5414 * Delete all signs in all buffers.
5415 */
5416 void
5417buf_delete_all_signs()
5418{
5419 buf_T *buf; /* buffer we are checking for signs */
5420
5421 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5422 if (buf->b_signlist != NULL)
5423 {
5424 /* Need to redraw the windows to remove the sign column. */
5425 redraw_buf_later(buf, NOT_VALID);
5426 buf_delete_signs(buf);
5427 }
5428}
5429
5430/*
5431 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5432 */
5433 void
5434sign_list_placed(rbuf)
5435 buf_T *rbuf;
5436{
5437 buf_T *buf;
5438 signlist_T *p;
5439 char lbuf[BUFSIZ];
5440
5441 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5442 msg_putchar('\n');
5443 if (rbuf == NULL)
5444 buf = firstbuf;
5445 else
5446 buf = rbuf;
5447 while (buf != NULL)
5448 {
5449 if (buf->b_signlist != NULL)
5450 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005451 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5453 msg_putchar('\n');
5454 }
5455 for (p = buf->b_signlist; p != NULL; p = p->next)
5456 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005457 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5459 MSG_PUTS(lbuf);
5460 msg_putchar('\n');
5461 }
5462 if (rbuf != NULL)
5463 break;
5464 buf = buf->b_next;
5465 }
5466}
5467
5468/*
5469 * Adjust a placed sign for inserted/deleted lines.
5470 */
5471 void
5472sign_mark_adjust(line1, line2, amount, amount_after)
5473 linenr_T line1;
5474 linenr_T line2;
5475 long amount;
5476 long amount_after;
5477{
5478 signlist_T *sign; /* a sign in a b_signlist */
5479
5480 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5481 {
5482 if (sign->lnum >= line1 && sign->lnum <= line2)
5483 {
5484 if (amount == MAXLNUM)
5485 sign->lnum = line1;
5486 else
5487 sign->lnum += amount;
5488 }
5489 else if (sign->lnum > line2)
5490 sign->lnum += amount_after;
5491 }
5492}
5493#endif /* FEAT_SIGNS */
5494
5495/*
5496 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5497 */
5498 void
5499set_buflisted(on)
5500 int on;
5501{
5502 if (on != curbuf->b_p_bl)
5503 {
5504 curbuf->b_p_bl = on;
5505#ifdef FEAT_AUTOCMD
5506 if (on)
5507 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5508 else
5509 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5510#endif
5511 }
5512}
5513
5514/*
5515 * Read the file for "buf" again and check if the contents changed.
5516 * Return TRUE if it changed or this could not be checked.
5517 */
5518 int
5519buf_contents_changed(buf)
5520 buf_T *buf;
5521{
5522 buf_T *newbuf;
5523 int differ = TRUE;
5524 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 exarg_T ea;
5527
5528 /* Allocate a buffer without putting it in the buffer list. */
5529 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5530 if (newbuf == NULL)
5531 return TRUE;
5532
5533 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5534 if (prep_exarg(&ea, buf) == FAIL)
5535 {
5536 wipe_buffer(newbuf, FALSE);
5537 return TRUE;
5538 }
5539
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 /* set curwin/curbuf to buf and save a few things */
5541 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542
Bram Moolenaar4770d092006-01-12 23:22:24 +00005543 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 && readfile(buf->b_ffname, buf->b_fname,
5545 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5546 &ea, READ_NEW | READ_DUMMY) == OK)
5547 {
5548 /* compare the two files line by line */
5549 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5550 {
5551 differ = FALSE;
5552 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5553 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5554 {
5555 differ = TRUE;
5556 break;
5557 }
5558 }
5559 }
5560 vim_free(ea.cmd);
5561
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 /* restore curwin/curbuf and a few other things */
5563 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
5565 if (curbuf != newbuf) /* safety check */
5566 wipe_buffer(newbuf, FALSE);
5567
5568 return differ;
5569}
5570
5571/*
5572 * Wipe out a buffer and decrement the last buffer number if it was used for
5573 * this buffer. Call this to wipe out a temp buffer that does not contain any
5574 * marks.
5575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 void
5577wipe_buffer(buf, aucmd)
5578 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005579 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
5581 if (buf->b_fnum == top_file_num - 1)
5582 --top_file_num;
5583
5584#ifdef FEAT_AUTOCMD
5585 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005586 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587#endif
5588 close_buffer(NULL, buf, DOBUF_WIPE);
5589#ifdef FEAT_AUTOCMD
5590 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005591 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592#endif
5593}