blob: 1a3497e74ac3bf287bde46a2eda81d5eabc7ba4e [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
30#if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
31static char_u *buflist_match __ARGS((regprog_T *prog, buf_T *buf));
32# define HAVE_BUFLIST_MATCH
33static char_u *fname_match __ARGS((regprog_T *prog, char_u *name));
34#endif
35static void buflist_setfpos __ARGS((buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options));
Bram Moolenaar701f7af2008-11-15 13:12:07 +000036static wininfo_T *find_wininfo __ARGS((buf_T *buf, int skip_diff_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef UNIX
38static buf_T *buflist_findname_stat __ARGS((char_u *ffname, struct stat *st));
39static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname, struct stat *stp));
40static int buf_same_ino __ARGS((buf_T *buf, struct stat *stp));
41#else
42static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname));
43#endif
44#ifdef FEAT_TITLE
45static int ti_change __ARGS((char_u *str, char_u **last));
46#endif
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000047static int append_arg_number __ARGS((win_T *wp, char_u *buf, int buflen, int add_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static void free_buffer __ARGS((buf_T *));
49static void free_buffer_stuff __ARGS((buf_T *buf, int free_options));
50static void clear_wininfo __ARGS((buf_T *buf));
51
52#ifdef UNIX
53# define dev_T dev_t
54#else
55# define dev_T unsigned
56#endif
57
58#if defined(FEAT_SIGNS)
59static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr));
60static void buf_delete_signs __ARGS((buf_T *buf));
61#endif
62
63/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +020064 * Open current buffer, that is: open the memfile and read the file into
65 * memory.
66 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 */
68 int
69open_buffer(read_stdin, eap)
70 int read_stdin; /* read file from stdin */
71 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
72{
73 int retval = OK;
74#ifdef FEAT_AUTOCMD
75 buf_T *old_curbuf;
76#endif
77
78 /*
79 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
80 * When re-entering the same buffer, it should not change, because the
81 * user may have reset the flag by hand.
82 */
83 if (readonlymode && curbuf->b_ffname != NULL
84 && (curbuf->b_flags & BF_NEVERLOADED))
85 curbuf->b_p_ro = TRUE;
86
Bram Moolenaar4770d092006-01-12 23:22:24 +000087 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 {
89 /*
90 * There MUST be a memfile, otherwise we can't do anything
91 * If we can't create one for the current buffer, take another buffer
92 */
93 close_buffer(NULL, curbuf, 0);
94 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
95 if (curbuf->b_ml.ml_mfp != NULL)
96 break;
97 /*
98 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +000099 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100 */
101 if (curbuf == NULL)
102 {
103 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
104 getout(2);
105 }
106 EMSG(_("E83: Cannot allocate buffer, using other one..."));
107 enter_buffer(curbuf);
108 return FAIL;
109 }
110
111#ifdef FEAT_AUTOCMD
112 /* The autocommands in readfile() may change the buffer, but only AFTER
113 * reading the file. */
114 old_curbuf = curbuf;
115 modified_was_set = FALSE;
116#endif
117
118 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100119 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120
121 if (curbuf->b_ffname != NULL
122#ifdef FEAT_NETBEANS_INTG
123 && netbeansReadFile
124#endif
125 )
126 {
127#ifdef FEAT_NETBEANS_INTG
128 int oldFire = netbeansFireChanges;
129
130 netbeansFireChanges = 0;
131#endif
132 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
133 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_NEW);
134#ifdef FEAT_NETBEANS_INTG
135 netbeansFireChanges = oldFire;
136#endif
137 /* Help buffer is filtered. */
138 if (curbuf->b_help)
139 fix_help_buffer();
140 }
141 else if (read_stdin)
142 {
143 int save_bin = curbuf->b_p_bin;
144 linenr_T line_count;
145
146 /*
147 * First read the text in binary mode into the buffer.
148 * Then read from that same buffer and append at the end. This makes
149 * it possible to retry when 'fileformat' or 'fileencoding' was
150 * guessed wrong.
151 */
152 curbuf->b_p_bin = TRUE;
153 retval = readfile(NULL, NULL, (linenr_T)0,
154 (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW + READ_STDIN);
155 curbuf->b_p_bin = save_bin;
156 if (retval == OK)
157 {
158 line_count = curbuf->b_ml.ml_line_count;
159 retval = readfile(NULL, NULL, (linenr_T)line_count,
160 (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_BUFFER);
161 if (retval == OK)
162 {
163 /* Delete the binary lines. */
164 while (--line_count >= 0)
165 ml_delete((linenr_T)1, FALSE);
166 }
167 else
168 {
169 /* Delete the converted lines. */
170 while (curbuf->b_ml.ml_line_count > line_count)
171 ml_delete(line_count, FALSE);
172 }
173 /* Put the cursor on the first line. */
174 curwin->w_cursor.lnum = 1;
175 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000176
177 /* Set or reset 'modified' before executing autocommands, so that
178 * it can be changed there. */
179 if (!readonlymode && !bufempty())
180 changed();
181 else if (retval != FAIL)
182 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183#ifdef FEAT_AUTOCMD
184# ifdef FEAT_EVAL
185 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
186 curbuf, &retval);
187# else
188 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
189# endif
190#endif
191 }
192 }
193
194 /* if first time loading this buffer, init b_chartab[] */
195 if (curbuf->b_flags & BF_NEVERLOADED)
196 (void)buf_init_chartab(curbuf, FALSE);
197
198 /*
199 * Set/reset the Changed flag first, autocmds may change the buffer.
200 * Apply the automatic commands, before processing the modelines.
201 * So the modelines have priority over auto commands.
202 */
203 /* When reading stdin, the buffer contents always needs writing, so set
204 * the changed flag. Unless in readonly mode: "ls | gview -".
205 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000206 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifdef FEAT_AUTOCMD
208 || modified_was_set /* ":set modified" used in autocmd */
209# ifdef FEAT_EVAL
210 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
211# endif
212#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000213 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000215 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 unchanged(curbuf, FALSE);
217 save_file_ff(curbuf); /* keep this fileformat */
218
219 /* require "!" to overwrite the file, because it wasn't read completely */
220#ifdef FEAT_EVAL
221 if (aborting())
222#else
223 if (got_int)
224#endif
225 curbuf->b_flags |= BF_READERR;
226
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000227#ifdef FEAT_FOLDING
228 /* Need to update automatic folding. Do this before the autocommands,
229 * they may use the fold info. */
230 foldUpdateAll(curwin);
231#endif
232
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#ifdef FEAT_AUTOCMD
234 /* need to set w_topline, unless some autocommand already did that. */
235 if (!(curwin->w_valid & VALID_TOPLINE))
236 {
237 curwin->w_topline = 1;
238# ifdef FEAT_DIFF
239 curwin->w_topfill = 0;
240# endif
241 }
242# ifdef FEAT_EVAL
243 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
244# else
245 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
246# endif
247#endif
248
249 if (retval != FAIL)
250 {
251#ifdef FEAT_AUTOCMD
252 /*
253 * The autocommands may have changed the current buffer. Apply the
254 * modelines to the correct buffer, if it still exists and is loaded.
255 */
256 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
257 {
258 aco_save_T aco;
259
260 /* Go to the buffer that was opened. */
261 aucmd_prepbuf(&aco, old_curbuf);
262#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000263 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
265
266#ifdef FEAT_AUTOCMD
267# ifdef FEAT_EVAL
268 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
269 &retval);
270# else
271 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
272# endif
273
274 /* restore curwin/curbuf and a few other things */
275 aucmd_restbuf(&aco);
276 }
277#endif
278 }
279
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 return retval;
281}
282
283/*
284 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
285 */
286 int
287buf_valid(buf)
288 buf_T *buf;
289{
290 buf_T *bp;
291
292 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
293 if (bp == buf)
294 return TRUE;
295 return FALSE;
296}
297
298/*
299 * Close the link to a buffer.
300 * "action" is used when there is no longer a window for the buffer.
301 * It can be:
302 * 0 buffer becomes hidden
303 * DOBUF_UNLOAD buffer is unloaded
304 * DOBUF_DELETE buffer is unloaded and removed from buffer list
305 * DOBUF_WIPE buffer is unloaded and really deleted
306 * When doing all but the first one on the current buffer, the caller should
307 * get a new buffer very soon!
308 *
309 * The 'bufhidden' option can force freeing and deleting.
310 */
311 void
312close_buffer(win, buf, action)
313 win_T *win; /* if not NULL, set b_last_cursor */
314 buf_T *buf;
315 int action;
316{
317#ifdef FEAT_AUTOCMD
318 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100319 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#endif
321 int unload_buf = (action != 0);
322 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
323 int wipe_buf = (action == DOBUF_WIPE);
324
325#ifdef FEAT_QUICKFIX
326 /*
327 * Force unloading or deleting when 'bufhidden' says so.
328 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
329 * "hide" (otherwise we could never free or delete a buffer).
330 */
331 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
332 {
333 del_buf = TRUE;
334 unload_buf = TRUE;
335 }
336 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
337 {
338 del_buf = TRUE;
339 unload_buf = TRUE;
340 wipe_buf = TRUE;
341 }
342 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
343 unload_buf = TRUE;
344#endif
345
346 if (win != NULL)
347 {
348 /* Set b_last_cursor when closing the last window for the buffer.
349 * Remember the last cursor position and window options of the buffer.
350 * This used to be only for the current window, but then options like
351 * 'foldmethod' may be lost with a ":only" command. */
352 if (buf->b_nwindows == 1)
353 set_last_cursor(win);
354 buflist_setfpos(buf, win,
355 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
356 win->w_cursor.col, TRUE);
357 }
358
359#ifdef FEAT_AUTOCMD
360 /* When the buffer is no longer in a window, trigger BufWinLeave */
361 if (buf->b_nwindows == 1)
362 {
363 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
364 FALSE, buf);
365 if (!buf_valid(buf)) /* autocommands may delete the buffer */
366 return;
367
368 /* When the buffer becomes hidden, but is not unloaded, trigger
369 * BufHidden */
370 if (!unload_buf)
371 {
372 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
373 FALSE, buf);
374 if (!buf_valid(buf)) /* autocmds may delete the buffer */
375 return;
376 }
377# ifdef FEAT_EVAL
378 if (aborting()) /* autocmds may abort script processing */
379 return;
380# endif
381 }
382 nwindows = buf->b_nwindows;
383#endif
384
385 /* decrease the link count from windows (unless not in any window) */
386 if (buf->b_nwindows > 0)
387 --buf->b_nwindows;
388
389 /* Return when a window is displaying the buffer or when it's not
390 * unloaded. */
391 if (buf->b_nwindows > 0 || !unload_buf)
392 {
Bram Moolenaar607a95ed2006-03-28 20:57:42 +0000393#if 0 /* why was this here? */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 if (buf == curbuf)
395 u_sync(); /* sync undo before going to another buffer */
Bram Moolenaar607a95ed2006-03-28 20:57:42 +0000396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 return;
398 }
399
400 /* Always remove the buffer when there is no file name. */
401 if (buf->b_ffname == NULL)
402 del_buf = TRUE;
403
404 /*
405 * Free all things allocated for this buffer.
406 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
407 */
408#ifdef FEAT_AUTOCMD
409 /* Remember if we are closing the current buffer. Restore the number of
410 * windows, so that autocommands in buf_freeall() don't get confused. */
411 is_curbuf = (buf == curbuf);
412 buf->b_nwindows = nwindows;
413#endif
414
415 buf_freeall(buf, del_buf, wipe_buf);
416
417#ifdef FEAT_AUTOCMD
418 /* Autocommands may have deleted the buffer. */
419 if (!buf_valid(buf))
420 return;
421# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000422 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 return;
424# endif
425
426 /* Autocommands may have opened or closed windows for this buffer.
427 * Decrement the count for the close we do here. */
428 if (buf->b_nwindows > 0)
429 --buf->b_nwindows;
430
431 /*
432 * It's possible that autocommands change curbuf to the one being deleted.
433 * This might cause the previous curbuf to be deleted unexpectedly. But
434 * in some cases it's OK to delete the curbuf, because a new one is
435 * obtained anyway. Therefore only return if curbuf changed to the
436 * deleted buffer.
437 */
438 if (buf == curbuf && !is_curbuf)
439 return;
440#endif
441
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000442 /* Change directories when the 'acd' option is set. */
443 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
445 /*
446 * Remove the buffer from the list.
447 */
448 if (wipe_buf)
449 {
450#ifdef FEAT_SUN_WORKSHOP
451 if (usingSunWorkShop)
452 workshop_file_closed_lineno((char *)buf->b_ffname,
453 (int)buf->b_last_cursor.lnum);
454#endif
455 vim_free(buf->b_ffname);
456 vim_free(buf->b_sfname);
457 if (buf->b_prev == NULL)
458 firstbuf = buf->b_next;
459 else
460 buf->b_prev->b_next = buf->b_next;
461 if (buf->b_next == NULL)
462 lastbuf = buf->b_prev;
463 else
464 buf->b_next->b_prev = buf->b_prev;
465 free_buffer(buf);
466 }
467 else
468 {
469 if (del_buf)
470 {
471 /* Free all internal variables and reset option values, to make
472 * ":bdel" compatible with Vim 5.7. */
473 free_buffer_stuff(buf, TRUE);
474
475 /* Make it look like a new buffer. */
476 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
477
478 /* Init the options when loaded again. */
479 buf->b_p_initialized = FALSE;
480 }
481 buf_clear_file(buf);
482 if (del_buf)
483 buf->b_p_bl = FALSE;
484 }
485}
486
487/*
488 * Make buffer not contain a file.
489 */
490 void
491buf_clear_file(buf)
492 buf_T *buf;
493{
494 buf->b_ml.ml_line_count = 1;
495 unchanged(buf, TRUE);
496#ifndef SHORT_FNAME
497 buf->b_shortname = FALSE;
498#endif
499 buf->b_p_eol = TRUE;
500 buf->b_start_eol = TRUE;
501#ifdef FEAT_MBYTE
502 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000503 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504#endif
505 buf->b_ml.ml_mfp = NULL;
506 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
507#ifdef FEAT_NETBEANS_INTG
508 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509#endif
510}
511
512/*
513 * buf_freeall() - free all things allocated for a buffer that are related to
514 * the file.
515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 void
517buf_freeall(buf, del_buf, wipe_buf)
518 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +0000519 int del_buf UNUSED; /* buffer is going to be deleted */
520 int wipe_buf UNUSED; /* buffer is going to be wiped out */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521{
522#ifdef FEAT_AUTOCMD
523 int is_curbuf = (buf == curbuf);
524
525 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
526 if (!buf_valid(buf)) /* autocommands may delete the buffer */
527 return;
528 if (del_buf && buf->b_p_bl)
529 {
530 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
531 if (!buf_valid(buf)) /* autocommands may delete the buffer */
532 return;
533 }
534 if (wipe_buf)
535 {
536 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
537 FALSE, buf);
538 if (!buf_valid(buf)) /* autocommands may delete the buffer */
539 return;
540 }
541# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000542 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 return;
544# endif
545
546 /*
547 * It's possible that autocommands change curbuf to the one being deleted.
548 * This might cause curbuf to be deleted unexpectedly. But in some cases
549 * it's OK to delete the curbuf, because a new one is obtained anyway.
550 * Therefore only return if curbuf changed to the deleted buffer.
551 */
552 if (buf == curbuf && !is_curbuf)
553 return;
554#endif
555#ifdef FEAT_DIFF
556 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
557#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000558
559#ifdef FEAT_FOLDING
560 /* No folds in an empty buffer. */
561# ifdef FEAT_WINDOWS
562 {
563 win_T *win;
564 tabpage_T *tp;
565
566 FOR_ALL_TAB_WINDOWS(tp, win)
567 if (win->w_buffer == buf)
568 clearFolding(win);
569 }
570# else
571 if (curwin->w_buffer == buf)
572 clearFolding(curwin);
573# endif
574#endif
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576#ifdef FEAT_TCL
577 tcl_buffer_free(buf);
578#endif
579 u_blockfree(buf); /* free the memory allocated for undo */
580 ml_close(buf, TRUE); /* close and delete the memline/memfile */
581 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
582 u_clearall(buf); /* reset all undo information */
583#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200584 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000586 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587}
588
589/*
590 * Free a buffer structure and the things it contains related to the buffer
591 * itself (not the file, that must have been done already).
592 */
593 static void
594free_buffer(buf)
595 buf_T *buf;
596{
597 free_buffer_stuff(buf, TRUE);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200598#ifdef FEAT_LUA
599 lua_buffer_free(buf);
600#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000601#ifdef FEAT_MZSCHEME
602 mzscheme_buffer_free(buf);
603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604#ifdef FEAT_PERL
605 perl_buf_free(buf);
606#endif
607#ifdef FEAT_PYTHON
608 python_buffer_free(buf);
609#endif
610#ifdef FEAT_RUBY
611 ruby_buffer_free(buf);
612#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000613#ifdef FEAT_AUTOCMD
614 aubuflocal_remove(buf);
615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 vim_free(buf);
617}
618
619/*
620 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
621 */
622 static void
623free_buffer_stuff(buf, free_options)
624 buf_T *buf;
625 int free_options; /* free options as well */
626{
627 if (free_options)
628 {
629 clear_wininfo(buf); /* including window-local options */
630 free_buf_options(buf, TRUE);
631 }
632#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000633 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
634 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635#endif
636#ifdef FEAT_USR_CMDS
637 uc_clear(&buf->b_ucmds); /* clear local user commands */
638#endif
639#ifdef FEAT_SIGNS
640 buf_delete_signs(buf); /* delete any signs */
641#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000642#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200643 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#ifdef FEAT_LOCALMAP
646 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
647 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
648#endif
649#ifdef FEAT_MBYTE
650 vim_free(buf->b_start_fenc);
651 buf->b_start_fenc = NULL;
652#endif
Bram Moolenaar9381ab72008-11-12 11:52:19 +0000653#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200654 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar9381ab72008-11-12 11:52:19 +0000655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656}
657
658/*
659 * Free the b_wininfo list for buffer "buf".
660 */
661 static void
662clear_wininfo(buf)
663 buf_T *buf;
664{
665 wininfo_T *wip;
666
667 while (buf->b_wininfo != NULL)
668 {
669 wip = buf->b_wininfo;
670 buf->b_wininfo = wip->wi_next;
671 if (wip->wi_optset)
672 {
673 clear_winopt(&wip->wi_opt);
674#ifdef FEAT_FOLDING
675 deleteFoldRecurse(&wip->wi_folds);
676#endif
677 }
678 vim_free(wip);
679 }
680}
681
682#if defined(FEAT_LISTCMDS) || defined(PROTO)
683/*
684 * Go to another buffer. Handles the result of the ATTENTION dialog.
685 */
686 void
687goto_buffer(eap, start, dir, count)
688 exarg_T *eap;
689 int start;
690 int dir;
691 int count;
692{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000693# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 buf_T *old_curbuf = curbuf;
695
696 swap_exists_action = SEA_DIALOG;
697# endif
698 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
699 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000700# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
702 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000703# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
704 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000705
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000706 /* Reset the error/interrupt/exception state here so that
707 * aborting() returns FALSE when closing a window. */
708 enter_cleanup(&cs);
709# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000710
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000711 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 win_close(curwin, TRUE);
713 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000714 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000715
716# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
717 /* Restore the error/interrupt/exception state if not discarded by a
718 * new aborting error, interrupt, or uncaught exception. */
719 leave_cleanup(&cs);
720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 }
722 else
723 handle_swap_exists(old_curbuf);
724# endif
725}
726#endif
727
Bram Moolenaare64ac772005-12-07 20:54:59 +0000728#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729/*
730 * Handle the situation of swap_exists_action being set.
731 * It is allowed for "old_curbuf" to be NULL or invalid.
732 */
733 void
734handle_swap_exists(old_curbuf)
735 buf_T *old_curbuf;
736{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000737# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
738 cleanup_T cs;
739# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000740
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 if (swap_exists_action == SEA_QUIT)
742 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000743# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
744 /* Reset the error/interrupt/exception state here so that
745 * aborting() returns FALSE when closing a buffer. */
746 enter_cleanup(&cs);
747# endif
748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 /* User selected Quit at ATTENTION prompt. Go back to previous
750 * buffer. If that buffer is gone or the same as the current one,
751 * open a new, empty buffer. */
752 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000753 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
755 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000756 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 if (old_curbuf != NULL)
758 enter_buffer(old_curbuf);
759 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000760
761# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
762 /* Restore the error/interrupt/exception state if not discarded by a
763 * new aborting error, interrupt, or uncaught exception. */
764 leave_cleanup(&cs);
765# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 }
767 else if (swap_exists_action == SEA_RECOVER)
768 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000769# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
770 /* Reset the error/interrupt/exception state here so that
771 * aborting() returns FALSE when closing a buffer. */
772 enter_cleanup(&cs);
773# endif
774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 /* User selected Recover at ATTENTION prompt. */
776 msg_scroll = TRUE;
777 ml_recover();
778 MSG_PUTS("\n"); /* don't overwrite the last message */
779 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000780 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000781
782# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
783 /* Restore the error/interrupt/exception state if not discarded by a
784 * new aborting error, interrupt, or uncaught exception. */
785 leave_cleanup(&cs);
786# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
788 swap_exists_action = SEA_NONE;
789}
790#endif
791
792#if defined(FEAT_LISTCMDS) || defined(PROTO)
793/*
794 * do_bufdel() - delete or unload buffer(s)
795 *
796 * addr_count == 0: ":bdel" - delete current buffer
797 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
798 * buffer "end_bnr", then any other arguments.
799 * addr_count == 2: ":N,N bdel" - delete buffers in range
800 *
801 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
802 * DOBUF_DEL (":bdel")
803 *
804 * Returns error message or NULL
805 */
806 char_u *
807do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
808 int command;
809 char_u *arg; /* pointer to extra arguments */
810 int addr_count;
811 int start_bnr; /* first buffer number in a range */
812 int end_bnr; /* buffer nr or last buffer nr in a range */
813 int forceit;
814{
815 int do_current = 0; /* delete current buffer? */
816 int deleted = 0; /* number of buffers deleted */
817 char_u *errormsg = NULL; /* return value */
818 int bnr; /* buffer number */
819 char_u *p;
820
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (addr_count == 0)
822 {
823 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
824 }
825 else
826 {
827 if (addr_count == 2)
828 {
829 if (*arg) /* both range and argument is not allowed */
830 return (char_u *)_(e_trailing);
831 bnr = start_bnr;
832 }
833 else /* addr_count == 1 */
834 bnr = end_bnr;
835
836 for ( ;!got_int; ui_breakcheck())
837 {
838 /*
839 * delete the current buffer last, otherwise when the
840 * current buffer is deleted, the next buffer becomes
841 * the current one and will be loaded, which may then
842 * also be deleted, etc.
843 */
844 if (bnr == curbuf->b_fnum)
845 do_current = bnr;
846 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
847 forceit) == OK)
848 ++deleted;
849
850 /*
851 * find next buffer number to delete/unload
852 */
853 if (addr_count == 2)
854 {
855 if (++bnr > end_bnr)
856 break;
857 }
858 else /* addr_count == 1 */
859 {
860 arg = skipwhite(arg);
861 if (*arg == NUL)
862 break;
863 if (!VIM_ISDIGIT(*arg))
864 {
865 p = skiptowhite_esc(arg);
866 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
867 if (bnr < 0) /* failed */
868 break;
869 arg = p;
870 }
871 else
872 bnr = getdigits(&arg);
873 }
874 }
875 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
876 FORWARD, do_current, forceit) == OK)
877 ++deleted;
878
879 if (deleted == 0)
880 {
881 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000882 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000884 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000886 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 errormsg = IObuff;
888 }
889 else if (deleted >= p_report)
890 {
891 if (command == DOBUF_UNLOAD)
892 {
893 if (deleted == 1)
894 MSG(_("1 buffer unloaded"));
895 else
896 smsg((char_u *)_("%d buffers unloaded"), deleted);
897 }
898 else if (command == DOBUF_DEL)
899 {
900 if (deleted == 1)
901 MSG(_("1 buffer deleted"));
902 else
903 smsg((char_u *)_("%d buffers deleted"), deleted);
904 }
905 else
906 {
907 if (deleted == 1)
908 MSG(_("1 buffer wiped out"));
909 else
910 smsg((char_u *)_("%d buffers wiped out"), deleted);
911 }
912 }
913 }
914
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
916 return errormsg;
917}
918
919/*
920 * Implementation of the commands for the buffer list.
921 *
922 * action == DOBUF_GOTO go to specified buffer
923 * action == DOBUF_SPLIT split window and go to specified buffer
924 * action == DOBUF_UNLOAD unload specified buffer(s)
925 * action == DOBUF_DEL delete specified buffer(s) from buffer list
926 * action == DOBUF_WIPE delete specified buffer(s) really
927 *
928 * start == DOBUF_CURRENT go to "count" buffer from current buffer
929 * start == DOBUF_FIRST go to "count" buffer from first buffer
930 * start == DOBUF_LAST go to "count" buffer from last buffer
931 * start == DOBUF_MOD go to "count" modified buffer from current buffer
932 *
933 * Return FAIL or OK.
934 */
935 int
936do_buffer(action, start, dir, count, forceit)
937 int action;
938 int start;
939 int dir; /* FORWARD or BACKWARD */
940 int count; /* buffer number or number of buffers */
941 int forceit; /* TRUE for :...! */
942{
943 buf_T *buf;
944 buf_T *bp;
945 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
946 || action == DOBUF_WIPE);
947
948 switch (start)
949 {
950 case DOBUF_FIRST: buf = firstbuf; break;
951 case DOBUF_LAST: buf = lastbuf; break;
952 default: buf = curbuf; break;
953 }
954 if (start == DOBUF_MOD) /* find next modified buffer */
955 {
956 while (count-- > 0)
957 {
958 do
959 {
960 buf = buf->b_next;
961 if (buf == NULL)
962 buf = firstbuf;
963 }
964 while (buf != curbuf && !bufIsChanged(buf));
965 }
966 if (!bufIsChanged(buf))
967 {
968 EMSG(_("E84: No modified buffer found"));
969 return FAIL;
970 }
971 }
972 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
973 {
974 while (buf != NULL && buf->b_fnum != count)
975 buf = buf->b_next;
976 }
977 else
978 {
979 bp = NULL;
980 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
981 {
982 /* remember the buffer where we start, we come back there when all
983 * buffers are unlisted. */
984 if (bp == NULL)
985 bp = buf;
986 if (dir == FORWARD)
987 {
988 buf = buf->b_next;
989 if (buf == NULL)
990 buf = firstbuf;
991 }
992 else
993 {
994 buf = buf->b_prev;
995 if (buf == NULL)
996 buf = lastbuf;
997 }
998 /* don't count unlisted buffers */
999 if (unload || buf->b_p_bl)
1000 {
1001 --count;
1002 bp = NULL; /* use this buffer as new starting point */
1003 }
1004 if (bp == buf)
1005 {
1006 /* back where we started, didn't find anything. */
1007 EMSG(_("E85: There is no listed buffer"));
1008 return FAIL;
1009 }
1010 }
1011 }
1012
1013 if (buf == NULL) /* could not find it */
1014 {
1015 if (start == DOBUF_FIRST)
1016 {
1017 /* don't warn when deleting */
1018 if (!unload)
1019 EMSGN(_("E86: Buffer %ld does not exist"), count);
1020 }
1021 else if (dir == FORWARD)
1022 EMSG(_("E87: Cannot go beyond last buffer"));
1023 else
1024 EMSG(_("E88: Cannot go before first buffer"));
1025 return FAIL;
1026 }
1027
1028#ifdef FEAT_GUI
1029 need_mouse_correct = TRUE;
1030#endif
1031
1032#ifdef FEAT_LISTCMDS
1033 /*
1034 * delete buffer buf from memory and/or the list
1035 */
1036 if (unload)
1037 {
1038 int forward;
1039 int retval;
1040
1041 /* When unloading or deleting a buffer that's already unloaded and
1042 * unlisted: fail silently. */
1043 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1044 return FAIL;
1045
1046 if (!forceit && bufIsChanged(buf))
1047 {
1048#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1049 if ((p_confirm || cmdmod.confirm) && p_write)
1050 {
1051 dialog_changed(buf, FALSE);
1052# ifdef FEAT_AUTOCMD
1053 if (!buf_valid(buf))
1054 /* Autocommand deleted buffer, oops! It's not changed
1055 * now. */
1056 return FAIL;
1057# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001058 /* If it's still changed fail silently, the dialog already
1059 * mentioned why it fails. */
1060 if (bufIsChanged(buf))
1061 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064#endif
1065 {
1066 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1067 buf->b_fnum);
1068 return FAIL;
1069 }
1070 }
1071
1072 /*
1073 * If deleting the last (listed) buffer, make it empty.
1074 * The last (listed) buffer cannot be unloaded.
1075 */
1076 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1077 if (bp->b_p_bl && bp != buf)
1078 break;
1079 if (bp == NULL && buf == curbuf)
1080 {
1081 if (action == DOBUF_UNLOAD)
1082 {
1083 EMSG(_("E90: Cannot unload last buffer"));
1084 return FAIL;
1085 }
1086
1087 /* Close any other windows on this buffer, then make it empty. */
1088#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001089 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090#endif
1091 setpcmark();
1092 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001093 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
1095 /*
1096 * do_ecmd() may create a new buffer, then we have to delete
1097 * the old one. But do_ecmd() may have done that already, check
1098 * if the buffer still exists.
1099 */
1100 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1101 close_buffer(NULL, buf, action);
1102 return retval;
1103 }
1104
1105#ifdef FEAT_WINDOWS
1106 /*
1107 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001108 * (unless it's the only window). Repeat this so long as we end up in
1109 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001111 while (buf == curbuf
1112 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 win_close(curwin, FALSE);
1114#endif
1115
1116 /*
1117 * If the buffer to be deleted is not the current one, delete it here.
1118 */
1119 if (buf != curbuf)
1120 {
1121#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001122 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#endif
1124 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1125 close_buffer(NULL, buf, action);
1126 return OK;
1127 }
1128
1129 /*
1130 * Deleting the current buffer: Need to find another buffer to go to.
1131 * There must be another, otherwise it would have been handled above.
1132 * First use au_new_curbuf, if it is valid.
1133 * Then prefer the buffer we most recently visited.
1134 * Else try to find one that is loaded, after the current buffer,
1135 * then before the current buffer.
1136 * Finally use any buffer.
1137 */
1138 buf = NULL; /* selected buffer */
1139 bp = NULL; /* used when no loaded buffer found */
1140#ifdef FEAT_AUTOCMD
1141 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1142 buf = au_new_curbuf;
1143# ifdef FEAT_JUMPLIST
1144 else
1145# endif
1146#endif
1147#ifdef FEAT_JUMPLIST
1148 if (curwin->w_jumplistlen > 0)
1149 {
1150 int jumpidx;
1151
1152 jumpidx = curwin->w_jumplistidx - 1;
1153 if (jumpidx < 0)
1154 jumpidx = curwin->w_jumplistlen - 1;
1155
1156 forward = jumpidx;
1157 while (jumpidx != curwin->w_jumplistidx)
1158 {
1159 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1160 if (buf != NULL)
1161 {
1162 if (buf == curbuf || !buf->b_p_bl)
1163 buf = NULL; /* skip current and unlisted bufs */
1164 else if (buf->b_ml.ml_mfp == NULL)
1165 {
1166 /* skip unloaded buf, but may keep it for later */
1167 if (bp == NULL)
1168 bp = buf;
1169 buf = NULL;
1170 }
1171 }
1172 if (buf != NULL) /* found a valid buffer: stop searching */
1173 break;
1174 /* advance to older entry in jump list */
1175 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1176 break;
1177 if (--jumpidx < 0)
1178 jumpidx = curwin->w_jumplistlen - 1;
1179 if (jumpidx == forward) /* List exhausted for sure */
1180 break;
1181 }
1182 }
1183#endif
1184
1185 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1186 {
1187 forward = TRUE;
1188 buf = curbuf->b_next;
1189 for (;;)
1190 {
1191 if (buf == NULL)
1192 {
1193 if (!forward) /* tried both directions */
1194 break;
1195 buf = curbuf->b_prev;
1196 forward = FALSE;
1197 continue;
1198 }
1199 /* in non-help buffer, try to skip help buffers, and vv */
1200 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1201 {
1202 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1203 break;
1204 if (bp == NULL) /* remember unloaded buf for later */
1205 bp = buf;
1206 }
1207 if (forward)
1208 buf = buf->b_next;
1209 else
1210 buf = buf->b_prev;
1211 }
1212 }
1213 if (buf == NULL) /* No loaded buffer, use unloaded one */
1214 buf = bp;
1215 if (buf == NULL) /* No loaded buffer, find listed one */
1216 {
1217 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1218 if (buf->b_p_bl && buf != curbuf)
1219 break;
1220 }
1221 if (buf == NULL) /* Still no buffer, just take one */
1222 {
1223 if (curbuf->b_next != NULL)
1224 buf = curbuf->b_next;
1225 else
1226 buf = curbuf->b_prev;
1227 }
1228 }
1229
1230 /*
1231 * make buf current buffer
1232 */
1233 if (action == DOBUF_SPLIT) /* split window first */
1234 {
1235# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001236 /* If 'switchbuf' contains "useopen": jump to first window containing
1237 * "buf" if one exists */
1238 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001239 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001240 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001241 * page containing "buf" if one exists */
1242 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 return OK;
1244 if (win_split(0, 0) == FAIL)
1245# endif
1246 return FAIL;
1247 }
1248#endif
1249
1250 /* go to current buffer - nothing to do */
1251 if (buf == curbuf)
1252 return OK;
1253
1254 /*
1255 * Check if the current buffer may be abandoned.
1256 */
1257 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1258 {
1259#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1260 if ((p_confirm || cmdmod.confirm) && p_write)
1261 {
1262 dialog_changed(curbuf, FALSE);
1263# ifdef FEAT_AUTOCMD
1264 if (!buf_valid(buf))
1265 /* Autocommand deleted buffer, oops! */
1266 return FAIL;
1267# endif
1268 }
1269 if (bufIsChanged(curbuf))
1270#endif
1271 {
1272 EMSG(_(e_nowrtmsg));
1273 return FAIL;
1274 }
1275 }
1276
1277 /* Go to the other buffer. */
1278 set_curbuf(buf, action);
1279
1280#if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1281 if (action == DOBUF_SPLIT)
1282 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1283#endif
1284
1285#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1286 if (aborting()) /* autocmds may abort script processing */
1287 return FAIL;
1288#endif
1289
1290 return OK;
1291}
1292
1293#endif /* FEAT_LISTCMDS */
1294
1295/*
1296 * Set current buffer to "buf". Executes autocommands and closes current
1297 * buffer. "action" tells how to close the current buffer:
1298 * DOBUF_GOTO free or hide it
1299 * DOBUF_SPLIT nothing
1300 * DOBUF_UNLOAD unload it
1301 * DOBUF_DEL delete it
1302 * DOBUF_WIPE wipe it out
1303 */
1304 void
1305set_curbuf(buf, action)
1306 buf_T *buf;
1307 int action;
1308{
1309 buf_T *prevbuf;
1310 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1311 || action == DOBUF_WIPE);
1312
1313 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001314 if (!cmdmod.keepalt)
1315 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001316 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317
1318#ifdef FEAT_VISUAL
1319 /* Don't restart Select mode after switching to another buffer. */
1320 VIsual_reselect = FALSE;
1321#endif
1322
1323 /* close_windows() or apply_autocmds() may change curbuf */
1324 prevbuf = curbuf;
1325
1326#ifdef FEAT_AUTOCMD
1327 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1328# ifdef FEAT_EVAL
1329 if (buf_valid(prevbuf) && !aborting())
1330# else
1331 if (buf_valid(prevbuf))
1332# endif
1333#endif
1334 {
1335#ifdef FEAT_WINDOWS
1336 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001337 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338#endif
1339#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1340 if (buf_valid(prevbuf) && !aborting())
1341#else
1342 if (buf_valid(prevbuf))
1343#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001344 {
1345 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001346 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1348 unload ? action : (action == DOBUF_GOTO
1349 && !P_HID(prevbuf)
1350 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001354 /* An autocommand may have deleted "buf", already entered it (e.g., when
1355 * it did ":bunload") or aborted the script processing! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356# ifdef FEAT_EVAL
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001357 if (buf_valid(buf) && buf != curbuf && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358# else
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001359 if (buf_valid(buf) && buf != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360# endif
1361#endif
1362 enter_buffer(buf);
1363}
1364
1365/*
1366 * Enter a new current buffer.
1367 * Old curbuf must have been abandoned already!
1368 */
1369 void
1370enter_buffer(buf)
1371 buf_T *buf;
1372{
1373 /* Copy buffer and window local option values. Not for a help buffer. */
1374 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1375 if (!buf->b_help)
1376 get_winopts(buf);
1377#ifdef FEAT_FOLDING
1378 else
1379 /* Remove all folds in the window. */
1380 clearFolding(curwin);
1381 foldUpdateAll(curwin); /* update folds (later). */
1382#endif
1383
Bram Moolenaar860cae12010-06-05 23:22:07 +02001384#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001385 reset_synblock(curwin);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001386 curwin->w_s = &(buf->b_s);
1387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 /* Get the buffer in the current window. */
1389 curwin->w_buffer = buf;
1390 curbuf = buf;
1391 ++curbuf->b_nwindows;
1392
1393#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001394 if (curwin->w_p_diff)
1395 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396#endif
1397
1398 /* Cursor on first line by default. */
1399 curwin->w_cursor.lnum = 1;
1400 curwin->w_cursor.col = 0;
1401#ifdef FEAT_VIRTUALEDIT
1402 curwin->w_cursor.coladd = 0;
1403#endif
1404 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001405#ifdef FEAT_AUTOCMD
1406 curwin->w_topline_was_set = FALSE;
1407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001409 /* mark cursor position as being invalid */
1410 curwin->w_valid = 0;
1411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 /* Make sure the buffer is loaded. */
1413 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001414 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001415#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001416 /* If there is no filetype, allow for detecting one. Esp. useful for
1417 * ":ball" used in a autocommand. If there already is a filetype we
1418 * might prefer to keep it. */
1419 if (*curbuf->b_p_ft == NUL)
1420 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001421#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001422
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 open_buffer(FALSE, NULL);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 else
1426 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001427 if (!msg_silent)
1428 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1430#ifdef FEAT_AUTOCMD
1431 curwin->w_topline = 1;
1432# ifdef FEAT_DIFF
1433 curwin->w_topfill = 0;
1434# endif
1435 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1436 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1437#endif
1438 }
1439
1440 /* If autocommands did not change the cursor position, restore cursor lnum
1441 * and possibly cursor col. */
1442 if (curwin->w_cursor.lnum == 1 && inindent(0))
1443 buflist_getfpos();
1444
1445 check_arg_idx(curwin); /* check for valid arg_idx */
1446#ifdef FEAT_TITLE
1447 maketitle();
1448#endif
1449#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001450 /* when autocmds didn't change it */
1451 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452#endif
1453 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1454
Bram Moolenaar009b2592004-10-24 19:18:58 +00001455#ifdef FEAT_NETBEANS_INTG
1456 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001457 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001458#endif
1459
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001460 /* Change directories when the 'acd' option is set. */
1461 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462
1463#ifdef FEAT_KEYMAP
1464 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001465 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001467#ifdef FEAT_SPELL
1468 /* May need to set the spell language. Can only do this after the buffer
1469 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001470 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1471 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001472#endif
1473
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 redraw_later(NOT_VALID);
1475}
1476
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001477#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1478/*
1479 * Change to the directory of the current buffer.
1480 */
1481 void
1482do_autochdir()
1483{
1484 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1485 shorten_fnames(TRUE);
1486}
1487#endif
1488
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489/*
1490 * functions for dealing with the buffer list
1491 */
1492
1493/*
1494 * Add a file name to the buffer list. Return a pointer to the buffer.
1495 * If the same file name already exists return a pointer to that buffer.
1496 * If it does not exist, or if fname == NULL, a new entry is created.
1497 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1498 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1499 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 * This is the ONLY way to create a new buffer.
1501 */
1502static int top_file_num = 1; /* highest file number */
1503
1504 buf_T *
1505buflist_new(ffname, sfname, lnum, flags)
1506 char_u *ffname; /* full path of fname or relative */
1507 char_u *sfname; /* short fname or NULL */
1508 linenr_T lnum; /* preferred cursor line */
1509 int flags; /* BLN_ defines */
1510{
1511 buf_T *buf;
1512#ifdef UNIX
1513 struct stat st;
1514#endif
1515
1516 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1517
1518 /*
1519 * If file name already exists in the list, update the entry.
1520 */
1521#ifdef UNIX
1522 /* On Unix we can use inode numbers when the file exists. Works better
1523 * for hard links. */
1524 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1525 st.st_dev = (dev_T)-1;
1526#endif
1527 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1528#ifdef UNIX
1529 buflist_findname_stat(ffname, &st)
1530#else
1531 buflist_findname(ffname)
1532#endif
1533 ) != NULL)
1534 {
1535 vim_free(ffname);
1536 if (lnum != 0)
1537 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1538 /* copy the options now, if 'cpo' doesn't have 's' and not done
1539 * already */
1540 buf_copy_options(buf, 0);
1541 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1542 {
1543 buf->b_p_bl = TRUE;
1544#ifdef FEAT_AUTOCMD
1545 if (!(flags & BLN_DUMMY))
1546 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1547#endif
1548 }
1549 return buf;
1550 }
1551
1552 /*
1553 * If the current buffer has no name and no contents, use the current
1554 * buffer. Otherwise: Need to allocate a new buffer structure.
1555 *
1556 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001557 * (A spell file buffer is allocated in spell.c, but that's not a normal
1558 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 */
1560 buf = NULL;
1561 if ((flags & BLN_CURBUF)
1562 && curbuf != NULL
1563 && curbuf->b_ffname == NULL
1564 && curbuf->b_nwindows <= 1
1565 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1566 {
1567 buf = curbuf;
1568#ifdef FEAT_AUTOCMD
1569 /* It's like this buffer is deleted. Watch out for autocommands that
1570 * change curbuf! If that happens, allocate a new buffer anyway. */
1571 if (curbuf->b_p_bl)
1572 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1573 if (buf == curbuf)
1574 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1575# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001576 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 return NULL;
1578# endif
1579#endif
1580#ifdef FEAT_QUICKFIX
1581# ifdef FEAT_AUTOCMD
1582 if (buf == curbuf)
1583# endif
1584 {
1585 /* Make sure 'bufhidden' and 'buftype' are empty */
1586 clear_string_option(&buf->b_p_bh);
1587 clear_string_option(&buf->b_p_bt);
1588 }
1589#endif
1590 }
1591 if (buf != curbuf || curbuf == NULL)
1592 {
1593 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1594 if (buf == NULL)
1595 {
1596 vim_free(ffname);
1597 return NULL;
1598 }
1599 }
1600
1601 if (ffname != NULL)
1602 {
1603 buf->b_ffname = ffname;
1604 buf->b_sfname = vim_strsave(sfname);
1605 }
1606
1607 clear_wininfo(buf);
1608 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1609
1610 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1611 || buf->b_wininfo == NULL)
1612 {
1613 vim_free(buf->b_ffname);
1614 buf->b_ffname = NULL;
1615 vim_free(buf->b_sfname);
1616 buf->b_sfname = NULL;
1617 if (buf != curbuf)
1618 free_buffer(buf);
1619 return NULL;
1620 }
1621
1622 if (buf == curbuf)
1623 {
1624 /* free all things allocated for this buffer */
1625 buf_freeall(buf, FALSE, FALSE);
1626 if (buf != curbuf) /* autocommands deleted the buffer! */
1627 return NULL;
1628#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001629 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 return NULL;
1631#endif
1632 /* buf->b_nwindows = 0; why was this here? */
1633 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1634#ifdef FEAT_KEYMAP
1635 /* need to reload lmaps and set b:keymap_name */
1636 curbuf->b_kmap_state |= KEYMAP_INIT;
1637#endif
1638 }
1639 else
1640 {
1641 /*
1642 * put new buffer at the end of the buffer list
1643 */
1644 buf->b_next = NULL;
1645 if (firstbuf == NULL) /* buffer list is empty */
1646 {
1647 buf->b_prev = NULL;
1648 firstbuf = buf;
1649 }
1650 else /* append new buffer at end of list */
1651 {
1652 lastbuf->b_next = buf;
1653 buf->b_prev = lastbuf;
1654 }
1655 lastbuf = buf;
1656
1657 buf->b_fnum = top_file_num++;
1658 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1659 {
1660 EMSG(_("W14: Warning: List of file names overflow"));
1661 if (emsg_silent == 0)
1662 {
1663 out_flush();
1664 ui_delay(3000L, TRUE); /* make sure it is noticed */
1665 }
1666 top_file_num = 1;
1667 }
1668
1669 /*
1670 * Always copy the options from the current buffer.
1671 */
1672 buf_copy_options(buf, BCO_ALWAYS);
1673 }
1674
1675 buf->b_wininfo->wi_fpos.lnum = lnum;
1676 buf->b_wininfo->wi_win = curwin;
1677
1678#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001679 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1680#endif
1681#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001682 hash_init(&buf->b_s.b_keywtab);
1683 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684#endif
1685
1686 buf->b_fname = buf->b_sfname;
1687#ifdef UNIX
1688 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001689 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 else
1691 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001692 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 buf->b_dev = st.st_dev;
1694 buf->b_ino = st.st_ino;
1695 }
1696#endif
1697 buf->b_u_synced = TRUE;
1698 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001699 if (flags & BLN_DUMMY)
1700 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 buf_clear_file(buf);
1702 clrallmarks(buf); /* clear marks */
1703 fmarks_check_names(buf); /* check file marks for this file */
1704 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1705#ifdef FEAT_AUTOCMD
1706 if (!(flags & BLN_DUMMY))
1707 {
1708 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1709 if (flags & BLN_LISTED)
1710 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1711# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001712 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 return NULL;
1714# endif
1715 }
1716#endif
1717
1718 return buf;
1719}
1720
1721/*
1722 * Free the memory for the options of a buffer.
1723 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1724 * 'fileencoding'.
1725 */
1726 void
1727free_buf_options(buf, free_p_ff)
1728 buf_T *buf;
1729 int free_p_ff;
1730{
1731 if (free_p_ff)
1732 {
1733#ifdef FEAT_MBYTE
1734 clear_string_option(&buf->b_p_fenc);
1735#endif
1736 clear_string_option(&buf->b_p_ff);
1737#ifdef FEAT_QUICKFIX
1738 clear_string_option(&buf->b_p_bh);
1739 clear_string_option(&buf->b_p_bt);
1740#endif
1741 }
1742#ifdef FEAT_FIND_ID
1743 clear_string_option(&buf->b_p_def);
1744 clear_string_option(&buf->b_p_inc);
1745# ifdef FEAT_EVAL
1746 clear_string_option(&buf->b_p_inex);
1747# endif
1748#endif
1749#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1750 clear_string_option(&buf->b_p_inde);
1751 clear_string_option(&buf->b_p_indk);
1752#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001753#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1754 clear_string_option(&buf->b_p_bexpr);
1755#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001756#if defined(FEAT_EVAL)
1757 clear_string_option(&buf->b_p_fex);
1758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759#ifdef FEAT_CRYPT
1760 clear_string_option(&buf->b_p_key);
1761#endif
1762 clear_string_option(&buf->b_p_kp);
1763 clear_string_option(&buf->b_p_mps);
1764 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001765 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 clear_string_option(&buf->b_p_isk);
1767#ifdef FEAT_KEYMAP
1768 clear_string_option(&buf->b_p_keymap);
1769 ga_clear(&buf->b_kmap_ga);
1770#endif
1771#ifdef FEAT_COMMENTS
1772 clear_string_option(&buf->b_p_com);
1773#endif
1774#ifdef FEAT_FOLDING
1775 clear_string_option(&buf->b_p_cms);
1776#endif
1777 clear_string_option(&buf->b_p_nf);
1778#ifdef FEAT_SYN_HL
1779 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001780#endif
1781#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001782 clear_string_option(&buf->b_s.b_p_spc);
1783 clear_string_option(&buf->b_s.b_p_spf);
1784 vim_free(buf->b_s.b_cap_prog);
1785 buf->b_s.b_cap_prog = NULL;
1786 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787#endif
1788#ifdef FEAT_SEARCHPATH
1789 clear_string_option(&buf->b_p_sua);
1790#endif
1791#ifdef FEAT_AUTOCMD
1792 clear_string_option(&buf->b_p_ft);
1793#endif
1794#ifdef FEAT_OSFILETYPE
1795 clear_string_option(&buf->b_p_oft);
1796#endif
1797#ifdef FEAT_CINDENT
1798 clear_string_option(&buf->b_p_cink);
1799 clear_string_option(&buf->b_p_cino);
1800#endif
1801#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1802 clear_string_option(&buf->b_p_cinw);
1803#endif
1804#ifdef FEAT_INS_EXPAND
1805 clear_string_option(&buf->b_p_cpt);
1806#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001807#ifdef FEAT_COMPL_FUNC
1808 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001809 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811#ifdef FEAT_QUICKFIX
1812 clear_string_option(&buf->b_p_gp);
1813 clear_string_option(&buf->b_p_mp);
1814 clear_string_option(&buf->b_p_efm);
1815#endif
1816 clear_string_option(&buf->b_p_ep);
1817 clear_string_option(&buf->b_p_path);
1818 clear_string_option(&buf->b_p_tags);
1819#ifdef FEAT_INS_EXPAND
1820 clear_string_option(&buf->b_p_dict);
1821 clear_string_option(&buf->b_p_tsr);
1822#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001823#ifdef FEAT_TEXTOBJ
1824 clear_string_option(&buf->b_p_qe);
1825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 buf->b_p_ar = -1;
1827}
1828
1829/*
1830 * get alternate file n
1831 * set linenr to lnum or altfpos.lnum if lnum == 0
1832 * also set cursor column to altfpos.col if 'startofline' is not set.
1833 * if (options & GETF_SETMARK) call setpcmark()
1834 * if (options & GETF_ALT) we are jumping to an alternate file.
1835 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1836 *
1837 * return FAIL for failure, OK for success
1838 */
1839 int
1840buflist_getfile(n, lnum, options, forceit)
1841 int n;
1842 linenr_T lnum;
1843 int options;
1844 int forceit;
1845{
1846 buf_T *buf;
1847#ifdef FEAT_WINDOWS
1848 win_T *wp = NULL;
1849#endif
1850 pos_T *fpos;
1851 colnr_T col;
1852
1853 buf = buflist_findnr(n);
1854 if (buf == NULL)
1855 {
1856 if ((options & GETF_ALT) && n == 0)
1857 EMSG(_(e_noalt));
1858 else
1859 EMSGN(_("E92: Buffer %ld not found"), n);
1860 return FAIL;
1861 }
1862
1863 /* if alternate file is the current buffer, nothing to do */
1864 if (buf == curbuf)
1865 return OK;
1866
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001867 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001868 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001869 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001871 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001872#ifdef FEAT_AUTOCMD
1873 if (curbuf_locked())
1874 return FAIL;
1875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
1877 /* altfpos may be changed by getfile(), get it now */
1878 if (lnum == 0)
1879 {
1880 fpos = buflist_findfpos(buf);
1881 lnum = fpos->lnum;
1882 col = fpos->col;
1883 }
1884 else
1885 col = 0;
1886
1887#ifdef FEAT_WINDOWS
1888 if (options & GETF_SWITCH)
1889 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001890 /* If 'switchbuf' contains "useopen": jump to first window containing
1891 * "buf" if one exists */
1892 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001894 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1895 * page containing "buf" if one exists */
1896 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001897 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001898 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1899 * isn't empty: open new window */
1900 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001902 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1903 tabpage_new();
1904 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 return FAIL;
1906# ifdef FEAT_SCROLLBIND
1907 curwin->w_p_scb = FALSE;
1908# endif
1909 }
1910 }
1911#endif
1912
1913 ++RedrawingDisabled;
1914 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1915 lnum, forceit) <= 0)
1916 {
1917 --RedrawingDisabled;
1918
1919 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1920 if (!p_sol && col != 0)
1921 {
1922 curwin->w_cursor.col = col;
1923 check_cursor_col();
1924#ifdef FEAT_VIRTUALEDIT
1925 curwin->w_cursor.coladd = 0;
1926#endif
1927 curwin->w_set_curswant = TRUE;
1928 }
1929 return OK;
1930 }
1931 --RedrawingDisabled;
1932 return FAIL;
1933}
1934
1935/*
1936 * go to the last know line number for the current buffer
1937 */
1938 void
1939buflist_getfpos()
1940{
1941 pos_T *fpos;
1942
1943 fpos = buflist_findfpos(curbuf);
1944
1945 curwin->w_cursor.lnum = fpos->lnum;
1946 check_cursor_lnum();
1947
1948 if (p_sol)
1949 curwin->w_cursor.col = 0;
1950 else
1951 {
1952 curwin->w_cursor.col = fpos->col;
1953 check_cursor_col();
1954#ifdef FEAT_VIRTUALEDIT
1955 curwin->w_cursor.coladd = 0;
1956#endif
1957 curwin->w_set_curswant = TRUE;
1958 }
1959}
1960
Bram Moolenaar81695252004-12-29 20:58:21 +00001961#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1962/*
1963 * Find file in buffer list by name (it has to be for the current window).
1964 * Returns NULL if not found.
1965 */
1966 buf_T *
1967buflist_findname_exp(fname)
1968 char_u *fname;
1969{
1970 char_u *ffname;
1971 buf_T *buf = NULL;
1972
1973 /* First make the name into a full path name */
1974 ffname = FullName_save(fname,
1975#ifdef UNIX
1976 TRUE /* force expansion, get rid of symbolic links */
1977#else
1978 FALSE
1979#endif
1980 );
1981 if (ffname != NULL)
1982 {
1983 buf = buflist_findname(ffname);
1984 vim_free(ffname);
1985 }
1986 return buf;
1987}
1988#endif
1989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990/*
1991 * Find file in buffer list by name (it has to be for the current window).
1992 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00001993 * Skips dummy buffers.
1994 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 */
1996 buf_T *
1997buflist_findname(ffname)
1998 char_u *ffname;
1999{
2000#ifdef UNIX
2001 struct stat st;
2002
2003 if (mch_stat((char *)ffname, &st) < 0)
2004 st.st_dev = (dev_T)-1;
2005 return buflist_findname_stat(ffname, &st);
2006}
2007
2008/*
2009 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2010 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002011 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 */
2013 static buf_T *
2014buflist_findname_stat(ffname, stp)
2015 char_u *ffname;
2016 struct stat *stp;
2017{
2018#endif
2019 buf_T *buf;
2020
2021 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002022 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023#ifdef UNIX
2024 , stp
2025#endif
2026 ))
2027 return buf;
2028 return NULL;
2029}
2030
2031#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2032/*
2033 * Find file in buffer list by a regexp pattern.
2034 * Return fnum of the found buffer.
2035 * Return < 0 for error.
2036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 int
2038buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2039 char_u *pattern;
2040 char_u *pattern_end; /* pointer to first char after pattern */
2041 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002042 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043{
2044 buf_T *buf;
2045 regprog_T *prog;
2046 int match = -1;
2047 int find_listed;
2048 char_u *pat;
2049 char_u *patend;
2050 int attempt;
2051 char_u *p;
2052 int toggledollar;
2053
2054 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2055 {
2056 if (*pattern == '%')
2057 match = curbuf->b_fnum;
2058 else
2059 match = curwin->w_alt_fnum;
2060#ifdef FEAT_DIFF
2061 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2062 match = -1;
2063#endif
2064 }
2065
2066 /*
2067 * Try four ways of matching a listed buffer:
2068 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002069 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 * attempt == 2: with '$' at end (only match at end)
2071 * attempt == 3: with '^' at start and '$' at end (only full match)
2072 * Repeat this for finding an unlisted buffer if there was no matching
2073 * listed buffer.
2074 */
2075 else
2076 {
2077 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2078 if (pat == NULL)
2079 return -1;
2080 patend = pat + STRLEN(pat) - 1;
2081 toggledollar = (patend > pat && *patend == '$');
2082
2083 /* First try finding a listed buffer. If not found and "unlisted"
2084 * is TRUE, try finding an unlisted buffer. */
2085 find_listed = TRUE;
2086 for (;;)
2087 {
2088 for (attempt = 0; attempt <= 3; ++attempt)
2089 {
2090 /* may add '^' and '$' */
2091 if (toggledollar)
2092 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2093 p = pat;
2094 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2095 ++p;
2096 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2097 if (prog == NULL)
2098 {
2099 vim_free(pat);
2100 return -1;
2101 }
2102
2103 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2104 if (buf->b_p_bl == find_listed
2105#ifdef FEAT_DIFF
2106 && (!diffmode || diff_mode_buf(buf))
2107#endif
2108 && buflist_match(prog, buf) != NULL)
2109 {
2110 if (match >= 0) /* already found a match */
2111 {
2112 match = -2;
2113 break;
2114 }
2115 match = buf->b_fnum; /* remember first match */
2116 }
2117
2118 vim_free(prog);
2119 if (match >= 0) /* found one match */
2120 break;
2121 }
2122
2123 /* Only search for unlisted buffers if there was no match with
2124 * a listed buffer. */
2125 if (!unlisted || !find_listed || match != -1)
2126 break;
2127 find_listed = FALSE;
2128 }
2129
2130 vim_free(pat);
2131 }
2132
2133 if (match == -2)
2134 EMSG2(_("E93: More than one match for %s"), pattern);
2135 else if (match < 0)
2136 EMSG2(_("E94: No matching buffer for %s"), pattern);
2137 return match;
2138}
2139#endif
2140
2141#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2142
2143/*
2144 * Find all buffer names that match.
2145 * For command line expansion of ":buf" and ":sbuf".
2146 * Return OK if matches found, FAIL otherwise.
2147 */
2148 int
2149ExpandBufnames(pat, num_file, file, options)
2150 char_u *pat;
2151 int *num_file;
2152 char_u ***file;
2153 int options;
2154{
2155 int count = 0;
2156 buf_T *buf;
2157 int round;
2158 char_u *p;
2159 int attempt;
2160 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002161 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
2163 *num_file = 0; /* return values in case of FAIL */
2164 *file = NULL;
2165
Bram Moolenaar05159a02005-02-26 23:04:13 +00002166 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2167 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002169 patc = alloc((unsigned)STRLEN(pat) + 11);
2170 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002172 STRCPY(patc, "\\(^\\|[\\/]\\)");
2173 STRCPY(patc + 11, pat + 1);
2174 }
2175 else
2176 patc = pat;
2177
2178 /*
2179 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002180 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002181 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002182 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002183 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002184 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002185 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002186 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002187 if (prog == NULL)
2188 {
2189 if (patc != pat)
2190 vim_free(patc);
2191 return FAIL;
2192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
2194 /*
2195 * round == 1: Count the matches.
2196 * round == 2: Build the array to keep the matches.
2197 */
2198 for (round = 1; round <= 2; ++round)
2199 {
2200 count = 0;
2201 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2202 {
2203 if (!buf->b_p_bl) /* skip unlisted buffers */
2204 continue;
2205 p = buflist_match(prog, buf);
2206 if (p != NULL)
2207 {
2208 if (round == 1)
2209 ++count;
2210 else
2211 {
2212 if (options & WILD_HOME_REPLACE)
2213 p = home_replace_save(buf, p);
2214 else
2215 p = vim_strsave(p);
2216 (*file)[count++] = p;
2217 }
2218 }
2219 }
2220 if (count == 0) /* no match found, break here */
2221 break;
2222 if (round == 1)
2223 {
2224 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2225 if (*file == NULL)
2226 {
2227 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002228 if (patc != pat)
2229 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 return FAIL;
2231 }
2232 }
2233 }
2234 vim_free(prog);
2235 if (count) /* match(es) found, break here */
2236 break;
2237 }
2238
Bram Moolenaar05159a02005-02-26 23:04:13 +00002239 if (patc != pat)
2240 vim_free(patc);
2241
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 *num_file = count;
2243 return (count == 0 ? FAIL : OK);
2244}
2245
2246#endif /* FEAT_CMDL_COMPL */
2247
2248#ifdef HAVE_BUFLIST_MATCH
2249/*
2250 * Check for a match on the file name for buffer "buf" with regprog "prog".
2251 */
2252 static char_u *
2253buflist_match(prog, buf)
2254 regprog_T *prog;
2255 buf_T *buf;
2256{
2257 char_u *match;
2258
2259 /* First try the short file name, then the long file name. */
2260 match = fname_match(prog, buf->b_sfname);
2261 if (match == NULL)
2262 match = fname_match(prog, buf->b_ffname);
2263
2264 return match;
2265}
2266
2267/*
2268 * Try matching the regexp in "prog" with file name "name".
2269 * Return "name" when there is a match, NULL when not.
2270 */
2271 static char_u *
2272fname_match(prog, name)
2273 regprog_T *prog;
2274 char_u *name;
2275{
2276 char_u *match = NULL;
2277 char_u *p;
2278 regmatch_T regmatch;
2279
2280 if (name != NULL)
2281 {
2282 regmatch.regprog = prog;
2283#ifdef CASE_INSENSITIVE_FILENAME
2284 regmatch.rm_ic = TRUE; /* Always ignore case */
2285#else
2286 regmatch.rm_ic = FALSE; /* Never ignore case */
2287#endif
2288
2289 if (vim_regexec(&regmatch, name, (colnr_T)0))
2290 match = name;
2291 else
2292 {
2293 /* Replace $(HOME) with '~' and try matching again. */
2294 p = home_replace_save(NULL, name);
2295 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2296 match = name;
2297 vim_free(p);
2298 }
2299 }
2300
2301 return match;
2302}
2303#endif
2304
2305/*
2306 * find file in buffer list by number
2307 */
2308 buf_T *
2309buflist_findnr(nr)
2310 int nr;
2311{
2312 buf_T *buf;
2313
2314 if (nr == 0)
2315 nr = curwin->w_alt_fnum;
2316 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2317 if (buf->b_fnum == nr)
2318 return (buf);
2319 return NULL;
2320}
2321
2322/*
2323 * Get name of file 'n' in the buffer list.
2324 * When the file has no name an empty string is returned.
2325 * home_replace() is used to shorten the file name (used for marks).
2326 * Returns a pointer to allocated memory, of NULL when failed.
2327 */
2328 char_u *
2329buflist_nr2name(n, fullname, helptail)
2330 int n;
2331 int fullname;
2332 int helptail; /* for help buffers return tail only */
2333{
2334 buf_T *buf;
2335
2336 buf = buflist_findnr(n);
2337 if (buf == NULL)
2338 return NULL;
2339 return home_replace_save(helptail ? buf : NULL,
2340 fullname ? buf->b_ffname : buf->b_fname);
2341}
2342
2343/*
2344 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2345 * When "copy_options" is TRUE save the local window option values.
2346 * When "lnum" is 0 only do the options.
2347 */
2348 static void
2349buflist_setfpos(buf, win, lnum, col, copy_options)
2350 buf_T *buf;
2351 win_T *win;
2352 linenr_T lnum;
2353 colnr_T col;
2354 int copy_options;
2355{
2356 wininfo_T *wip;
2357
2358 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2359 if (wip->wi_win == win)
2360 break;
2361 if (wip == NULL)
2362 {
2363 /* allocate a new entry */
2364 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2365 if (wip == NULL)
2366 return;
2367 wip->wi_win = win;
2368 if (lnum == 0) /* set lnum even when it's 0 */
2369 lnum = 1;
2370 }
2371 else
2372 {
2373 /* remove the entry from the list */
2374 if (wip->wi_prev)
2375 wip->wi_prev->wi_next = wip->wi_next;
2376 else
2377 buf->b_wininfo = wip->wi_next;
2378 if (wip->wi_next)
2379 wip->wi_next->wi_prev = wip->wi_prev;
2380 if (copy_options && wip->wi_optset)
2381 {
2382 clear_winopt(&wip->wi_opt);
2383#ifdef FEAT_FOLDING
2384 deleteFoldRecurse(&wip->wi_folds);
2385#endif
2386 }
2387 }
2388 if (lnum != 0)
2389 {
2390 wip->wi_fpos.lnum = lnum;
2391 wip->wi_fpos.col = col;
2392 }
2393 if (copy_options)
2394 {
2395 /* Save the window-specific option values. */
2396 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2397#ifdef FEAT_FOLDING
2398 wip->wi_fold_manual = win->w_fold_manual;
2399 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2400#endif
2401 wip->wi_optset = TRUE;
2402 }
2403
2404 /* insert the entry in front of the list */
2405 wip->wi_next = buf->b_wininfo;
2406 buf->b_wininfo = wip;
2407 wip->wi_prev = NULL;
2408 if (wip->wi_next)
2409 wip->wi_next->wi_prev = wip;
2410
2411 return;
2412}
2413
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002414#ifdef FEAT_DIFF
2415static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2416
2417/*
2418 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2419 * page. That's because a diff is local to a tab page.
2420 */
2421 static int
2422wininfo_other_tab_diff(wip)
2423 wininfo_T *wip;
2424{
2425 win_T *wp;
2426
2427 if (wip->wi_opt.wo_diff)
2428 {
2429 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2430 /* return FALSE when it's a window in the current tab page, thus
2431 * the buffer was in diff mode here */
2432 if (wip->wi_win == wp)
2433 return FALSE;
2434 return TRUE;
2435 }
2436 return FALSE;
2437}
2438#endif
2439
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440/*
2441 * Find info for the current window in buffer "buf".
2442 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002443 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2444 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 * Returns NULL when there isn't any info.
2446 */
2447 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002448find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002450 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
2452 wininfo_T *wip;
2453
2454 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002455 if (wip->wi_win == curwin
2456#ifdef FEAT_DIFF
2457 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2458#endif
2459 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002461
2462 /* If no wininfo for curwin, use the first in the list (that doesn't have
2463 * 'diff' set and is in another tab page). */
2464 if (wip == NULL)
2465 {
2466#ifdef FEAT_DIFF
2467 if (skip_diff_buffer)
2468 {
2469 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2470 if (!wininfo_other_tab_diff(wip))
2471 break;
2472 }
2473 else
2474#endif
2475 wip = buf->b_wininfo;
2476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 return wip;
2478}
2479
2480/*
2481 * Reset the local window options to the values last used in this window.
2482 * If the buffer wasn't used in this window before, use the values from
2483 * the most recently used window. If the values were never set, use the
2484 * global values for the window.
2485 */
2486 void
2487get_winopts(buf)
2488 buf_T *buf;
2489{
2490 wininfo_T *wip;
2491
2492 clear_winopt(&curwin->w_onebuf_opt);
2493#ifdef FEAT_FOLDING
2494 clearFolding(curwin);
2495#endif
2496
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002497 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 if (wip != NULL && wip->wi_optset)
2499 {
2500 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2501#ifdef FEAT_FOLDING
2502 curwin->w_fold_manual = wip->wi_fold_manual;
2503 curwin->w_foldinvalid = TRUE;
2504 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2505#endif
2506 }
2507 else
2508 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2509
2510#ifdef FEAT_FOLDING
2511 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2512 if (p_fdls >= 0)
2513 curwin->w_p_fdl = p_fdls;
2514#endif
2515}
2516
2517/*
2518 * Find the position (lnum and col) for the buffer 'buf' for the current
2519 * window.
2520 * Returns a pointer to no_position if no position is found.
2521 */
2522 pos_T *
2523buflist_findfpos(buf)
2524 buf_T *buf;
2525{
2526 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002527 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002529 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 if (wip != NULL)
2531 return &(wip->wi_fpos);
2532 else
2533 return &no_position;
2534}
2535
2536/*
2537 * Find the lnum for the buffer 'buf' for the current window.
2538 */
2539 linenr_T
2540buflist_findlnum(buf)
2541 buf_T *buf;
2542{
2543 return buflist_findfpos(buf)->lnum;
2544}
2545
2546#if defined(FEAT_LISTCMDS) || defined(PROTO)
2547/*
2548 * List all know file names (for :files and :buffers command).
2549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 void
2551buflist_list(eap)
2552 exarg_T *eap;
2553{
2554 buf_T *buf;
2555 int len;
2556 int i;
2557
2558 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2559 {
2560 /* skip unlisted buffers, unless ! was used */
2561 if (!buf->b_p_bl && !eap->forceit)
2562 continue;
2563 msg_putchar('\n');
2564 if (buf_spname(buf) != NULL)
2565 STRCPY(NameBuff, buf_spname(buf));
2566 else
2567 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2568
Bram Moolenaar50cde822005-06-05 21:54:54 +00002569 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 buf->b_fnum,
2571 buf->b_p_bl ? ' ' : 'u',
2572 buf == curbuf ? '%' :
2573 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2574 buf->b_ml.ml_mfp == NULL ? ' ' :
2575 (buf->b_nwindows == 0 ? 'h' : 'a'),
2576 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2577 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002578 : (bufIsChanged(buf) ? '+' : ' '),
2579 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
2581 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 i = 40 - vim_strsize(IObuff);
2583 do
2584 {
2585 IObuff[len++] = ' ';
2586 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002587 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2588 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002589 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 msg_outtrans(IObuff);
2591 out_flush(); /* output one line at a time */
2592 ui_breakcheck();
2593 }
2594}
2595#endif
2596
2597/*
2598 * Get file name and line number for file 'fnum'.
2599 * Used by DoOneCmd() for translating '%' and '#'.
2600 * Used by insert_reg() and cmdline_paste() for '#' register.
2601 * Return FAIL if not found, OK for success.
2602 */
2603 int
2604buflist_name_nr(fnum, fname, lnum)
2605 int fnum;
2606 char_u **fname;
2607 linenr_T *lnum;
2608{
2609 buf_T *buf;
2610
2611 buf = buflist_findnr(fnum);
2612 if (buf == NULL || buf->b_fname == NULL)
2613 return FAIL;
2614
2615 *fname = buf->b_fname;
2616 *lnum = buflist_findlnum(buf);
2617
2618 return OK;
2619}
2620
2621/*
2622 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2623 * The file name with the full path is also remembered, for when :cd is used.
2624 * Returns FAIL for failure (file name already in use by other buffer)
2625 * OK otherwise.
2626 */
2627 int
2628setfname(buf, ffname, sfname, message)
2629 buf_T *buf;
2630 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002631 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632{
Bram Moolenaar81695252004-12-29 20:58:21 +00002633 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634#ifdef UNIX
2635 struct stat st;
2636#endif
2637
2638 if (ffname == NULL || *ffname == NUL)
2639 {
2640 /* Removing the name. */
2641 vim_free(buf->b_ffname);
2642 vim_free(buf->b_sfname);
2643 buf->b_ffname = NULL;
2644 buf->b_sfname = NULL;
2645#ifdef UNIX
2646 st.st_dev = (dev_T)-1;
2647#endif
2648 }
2649 else
2650 {
2651 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2652 if (ffname == NULL) /* out of memory */
2653 return FAIL;
2654
2655 /*
2656 * if the file name is already used in another buffer:
2657 * - if the buffer is loaded, fail
2658 * - if the buffer is not loaded, delete it from the list
2659 */
2660#ifdef UNIX
2661 if (mch_stat((char *)ffname, &st) < 0)
2662 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002663#endif
2664 if (!(buf->b_flags & BF_DUMMY))
2665#ifdef UNIX
2666 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002668 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669#endif
2670 if (obuf != NULL && obuf != buf)
2671 {
2672 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2673 {
2674 if (message)
2675 EMSG(_("E95: Buffer with this name already exists"));
2676 vim_free(ffname);
2677 return FAIL;
2678 }
2679 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2680 }
2681 sfname = vim_strsave(sfname);
2682 if (ffname == NULL || sfname == NULL)
2683 {
2684 vim_free(sfname);
2685 vim_free(ffname);
2686 return FAIL;
2687 }
2688#ifdef USE_FNAME_CASE
2689# ifdef USE_LONG_FNAME
2690 if (USE_LONG_FNAME)
2691# endif
2692 fname_case(sfname, 0); /* set correct case for short file name */
2693#endif
2694 vim_free(buf->b_ffname);
2695 vim_free(buf->b_sfname);
2696 buf->b_ffname = ffname;
2697 buf->b_sfname = sfname;
2698 }
2699 buf->b_fname = buf->b_sfname;
2700#ifdef UNIX
2701 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002702 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 else
2704 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002705 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 buf->b_dev = st.st_dev;
2707 buf->b_ino = st.st_ino;
2708 }
2709#endif
2710
2711#ifndef SHORT_FNAME
2712 buf->b_shortname = FALSE;
2713#endif
2714
2715 buf_name_changed(buf);
2716 return OK;
2717}
2718
2719/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002720 * Crude way of changing the name of a buffer. Use with care!
2721 * The name should be relative to the current directory.
2722 */
2723 void
2724buf_set_name(fnum, name)
2725 int fnum;
2726 char_u *name;
2727{
2728 buf_T *buf;
2729
2730 buf = buflist_findnr(fnum);
2731 if (buf != NULL)
2732 {
2733 vim_free(buf->b_sfname);
2734 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002735 buf->b_ffname = vim_strsave(name);
2736 buf->b_sfname = NULL;
2737 /* Allocate ffname and expand into full path. Also resolves .lnk
2738 * files on Win32. */
2739 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002740 buf->b_fname = buf->b_sfname;
2741 }
2742}
2743
2744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 * Take care of what needs to be done when the name of buffer "buf" has
2746 * changed.
2747 */
2748 void
2749buf_name_changed(buf)
2750 buf_T *buf;
2751{
2752 /*
2753 * If the file name changed, also change the name of the swapfile
2754 */
2755 if (buf->b_ml.ml_mfp != NULL)
2756 ml_setname(buf);
2757
2758 if (curwin->w_buffer == buf)
2759 check_arg_idx(curwin); /* check file name for arg list */
2760#ifdef FEAT_TITLE
2761 maketitle(); /* set window title */
2762#endif
2763#ifdef FEAT_WINDOWS
2764 status_redraw_all(); /* status lines need to be redrawn */
2765#endif
2766 fmarks_check_names(buf); /* check named file marks */
2767 ml_timestamp(buf); /* reset timestamp */
2768}
2769
2770/*
2771 * set alternate file name for current window
2772 *
2773 * Used by do_one_cmd(), do_write() and do_ecmd().
2774 * Return the buffer.
2775 */
2776 buf_T *
2777setaltfname(ffname, sfname, lnum)
2778 char_u *ffname;
2779 char_u *sfname;
2780 linenr_T lnum;
2781{
2782 buf_T *buf;
2783
2784 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2785 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002786 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 curwin->w_alt_fnum = buf->b_fnum;
2788 return buf;
2789}
2790
2791/*
2792 * Get alternate file name for current window.
2793 * Return NULL if there isn't any, and give error message if requested.
2794 */
2795 char_u *
2796getaltfname(errmsg)
2797 int errmsg; /* give error message */
2798{
2799 char_u *fname;
2800 linenr_T dummy;
2801
2802 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2803 {
2804 if (errmsg)
2805 EMSG(_(e_noalt));
2806 return NULL;
2807 }
2808 return fname;
2809}
2810
2811/*
2812 * Add a file name to the buflist and return its number.
2813 * Uses same flags as buflist_new(), except BLN_DUMMY.
2814 *
2815 * used by qf_init(), main() and doarglist()
2816 */
2817 int
2818buflist_add(fname, flags)
2819 char_u *fname;
2820 int flags;
2821{
2822 buf_T *buf;
2823
2824 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2825 if (buf != NULL)
2826 return buf->b_fnum;
2827 return 0;
2828}
2829
2830#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2831/*
2832 * Adjust slashes in file names. Called after 'shellslash' was set.
2833 */
2834 void
2835buflist_slash_adjust()
2836{
2837 buf_T *bp;
2838
2839 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2840 {
2841 if (bp->b_ffname != NULL)
2842 slash_adjust(bp->b_ffname);
2843 if (bp->b_sfname != NULL)
2844 slash_adjust(bp->b_sfname);
2845 }
2846}
2847#endif
2848
2849/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002850 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 * Also save the local window option values.
2852 */
2853 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002854buflist_altfpos(win)
2855 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002857 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858}
2859
2860/*
2861 * Return TRUE if 'ffname' is not the same file as current file.
2862 * Fname must have a full path (expanded by mch_FullName()).
2863 */
2864 int
2865otherfile(ffname)
2866 char_u *ffname;
2867{
2868 return otherfile_buf(curbuf, ffname
2869#ifdef UNIX
2870 , NULL
2871#endif
2872 );
2873}
2874
2875 static int
2876otherfile_buf(buf, ffname
2877#ifdef UNIX
2878 , stp
2879#endif
2880 )
2881 buf_T *buf;
2882 char_u *ffname;
2883#ifdef UNIX
2884 struct stat *stp;
2885#endif
2886{
2887 /* no name is different */
2888 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2889 return TRUE;
2890 if (fnamecmp(ffname, buf->b_ffname) == 0)
2891 return FALSE;
2892#ifdef UNIX
2893 {
2894 struct stat st;
2895
2896 /* If no struct stat given, get it now */
2897 if (stp == NULL)
2898 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002899 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 st.st_dev = (dev_T)-1;
2901 stp = &st;
2902 }
2903 /* Use dev/ino to check if the files are the same, even when the names
2904 * are different (possible with links). Still need to compare the
2905 * name above, for when the file doesn't exist yet.
2906 * Problem: The dev/ino changes when a file is deleted (and created
2907 * again) and remains the same when renamed/moved. We don't want to
2908 * mch_stat() each buffer each time, that would be too slow. Get the
2909 * dev/ino again when they appear to match, but not when they appear
2910 * to be different: Could skip a buffer when it's actually the same
2911 * file. */
2912 if (buf_same_ino(buf, stp))
2913 {
2914 buf_setino(buf);
2915 if (buf_same_ino(buf, stp))
2916 return FALSE;
2917 }
2918 }
2919#endif
2920 return TRUE;
2921}
2922
2923#if defined(UNIX) || defined(PROTO)
2924/*
2925 * Set inode and device number for a buffer.
2926 * Must always be called when b_fname is changed!.
2927 */
2928 void
2929buf_setino(buf)
2930 buf_T *buf;
2931{
2932 struct stat st;
2933
2934 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2935 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002936 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 buf->b_dev = st.st_dev;
2938 buf->b_ino = st.st_ino;
2939 }
2940 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002941 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942}
2943
2944/*
2945 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2946 */
2947 static int
2948buf_same_ino(buf, stp)
2949 buf_T *buf;
2950 struct stat *stp;
2951{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002952 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 && stp->st_dev == buf->b_dev
2954 && stp->st_ino == buf->b_ino);
2955}
2956#endif
2957
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002958/*
2959 * Print info about the current buffer.
2960 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 void
2962fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002963 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 int shorthelp;
2965 int dont_truncate;
2966{
2967 char_u *name;
2968 int n;
2969 char_u *p;
2970 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002971 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972
2973 buffer = alloc(IOSIZE);
2974 if (buffer == NULL)
2975 return;
2976
2977 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2978 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002979 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 p = buffer + STRLEN(buffer);
2981 }
2982 else
2983 p = buffer;
2984
2985 *p++ = '"';
2986 if (buf_spname(curbuf) != NULL)
2987 STRCPY(p, buf_spname(curbuf));
2988 else
2989 {
2990 if (!fullname && curbuf->b_fname != NULL)
2991 name = curbuf->b_fname;
2992 else
2993 name = curbuf->b_ffname;
2994 home_replace(shorthelp ? curbuf : NULL, name, p,
2995 (int)(IOSIZE - (p - buffer)), TRUE);
2996 }
2997
Bram Moolenaara800b422010-06-27 01:15:55 +02002998 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 curbufIsChanged() ? (shortmess(SHM_MOD)
3000 ? " [+]" : _(" [Modified]")) : " ",
3001 (curbuf->b_flags & BF_NOTEDITED)
3002#ifdef FEAT_QUICKFIX
3003 && !bt_dontwrite(curbuf)
3004#endif
3005 ? _("[Not edited]") : "",
3006 (curbuf->b_flags & BF_NEW)
3007#ifdef FEAT_QUICKFIX
3008 && !bt_dontwrite(curbuf)
3009#endif
3010 ? _("[New file]") : "",
3011 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3012 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3013 : _("[readonly]")) : "",
3014 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3015 || curbuf->b_p_ro) ?
3016 " " : "");
3017 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3018 * causes an overflow, thus for large numbers divide instead. */
3019 if (curwin->w_cursor.lnum > 1000000L)
3020 n = (int)(((long)curwin->w_cursor.lnum) /
3021 ((long)curbuf->b_ml.ml_line_count / 100L));
3022 else
3023 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3024 (long)curbuf->b_ml.ml_line_count);
3025 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3026 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003027 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 }
3029#ifdef FEAT_CMDL_INFO
3030 else if (p_ru)
3031 {
3032 /* Current line and column are already on the screen -- webb */
3033 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003034 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003036 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 (long)curbuf->b_ml.ml_line_count, n);
3038 }
3039#endif
3040 else
3041 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003042 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 _("line %ld of %ld --%d%%-- col "),
3044 (long)curwin->w_cursor.lnum,
3045 (long)curbuf->b_ml.ml_line_count,
3046 n);
3047 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003048 len = STRLEN(buffer);
3049 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3051 }
3052
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003053 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
3055 if (dont_truncate)
3056 {
3057 /* Temporarily set msg_scroll to avoid the message being truncated.
3058 * First call msg_start() to get the message in the right place. */
3059 msg_start();
3060 n = msg_scroll;
3061 msg_scroll = TRUE;
3062 msg(buffer);
3063 msg_scroll = n;
3064 }
3065 else
3066 {
3067 p = msg_trunc_attr(buffer, FALSE, 0);
3068 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 /* Need to repeat the message after redrawing when:
3070 * - When restart_edit is set (otherwise there will be a delay
3071 * before redrawing).
3072 * - When the screen was scrolled but there is no wait-return
3073 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003074 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 }
3076
3077 vim_free(buffer);
3078}
3079
3080 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003081col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003083 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 int col;
3085 int vcol;
3086{
3087 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003088 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003090 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091}
3092
3093#if defined(FEAT_TITLE) || defined(PROTO)
3094/*
3095 * put file name in title bar of window and in icon title
3096 */
3097
3098static char_u *lasttitle = NULL;
3099static char_u *lasticon = NULL;
3100
3101 void
3102maketitle()
3103{
3104 char_u *p;
3105 char_u *t_str = NULL;
3106 char_u *i_name;
3107 char_u *i_str = NULL;
3108 int maxlen = 0;
3109 int len;
3110 int mustset;
3111 char_u buf[IOSIZE];
3112 int off;
3113
3114 if (!redrawing())
3115 {
3116 /* Postpone updating the title when 'lazyredraw' is set. */
3117 need_maketitle = TRUE;
3118 return;
3119 }
3120
3121 need_maketitle = FALSE;
3122 if (!p_title && !p_icon)
3123 return;
3124
3125 if (p_title)
3126 {
3127 if (p_titlelen > 0)
3128 {
3129 maxlen = p_titlelen * Columns / 100;
3130 if (maxlen < 10)
3131 maxlen = 10;
3132 }
3133
3134 t_str = buf;
3135 if (*p_titlestring != NUL)
3136 {
3137#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003138 if (stl_syntax & STL_IN_TITLE)
3139 {
3140 int use_sandbox = FALSE;
3141 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003142
3143# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003144 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003145# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003146 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003148 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003149 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003150 if (called_emsg)
3151 set_string_option_direct((char_u *)"titlestring", -1,
3152 (char_u *)"", OPT_FREE, SID_ERROR);
3153 called_emsg |= save_called_emsg;
3154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 else
3156#endif
3157 t_str = p_titlestring;
3158 }
3159 else
3160 {
3161 /* format: "fname + (path) (1 of 2) - VIM" */
3162
3163 if (curbuf->b_fname == NULL)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003164 STRCPY(buf, _("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 else
3166 {
3167 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003168 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 }
3171
3172 switch (bufIsChanged(curbuf)
3173 + (curbuf->b_p_ro * 2)
3174 + (!curbuf->b_p_ma * 4))
3175 {
3176 case 1: STRCAT(buf, " +"); break;
3177 case 2: STRCAT(buf, " ="); break;
3178 case 3: STRCAT(buf, " =+"); break;
3179 case 4:
3180 case 6: STRCAT(buf, " -"); break;
3181 case 5:
3182 case 7: STRCAT(buf, " -+"); break;
3183 }
3184
3185 if (curbuf->b_fname != NULL)
3186 {
3187 /* Get path of file, replace home dir with ~ */
3188 off = (int)STRLEN(buf);
3189 buf[off++] = ' ';
3190 buf[off++] = '(';
3191 home_replace(curbuf, curbuf->b_ffname,
3192 buf + off, IOSIZE - off, TRUE);
3193#ifdef BACKSLASH_IN_FILENAME
3194 /* avoid "c:/name" to be reduced to "c" */
3195 if (isalpha(buf[off]) && buf[off + 1] == ':')
3196 off += 2;
3197#endif
3198 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003199 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003202 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003203 (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003206
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 /* translate unprintable chars */
3208 p = transstr(buf + off);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003209 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 STRCAT(buf, ")");
3212 }
3213
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003214 append_arg_number(curwin, buf, IOSIZE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
3216#if defined(FEAT_CLIENTSERVER)
3217 if (serverName != NULL)
3218 {
3219 STRCAT(buf, " - ");
3220 STRCAT(buf, serverName);
3221 }
3222 else
3223#endif
3224 STRCAT(buf, " - VIM");
3225
3226 if (maxlen > 0)
3227 {
3228 /* make it shorter by removing a bit in the middle */
3229 len = vim_strsize(buf);
3230 if (len > maxlen)
3231 trunc_string(buf, buf, maxlen);
3232 }
3233 }
3234 }
3235 mustset = ti_change(t_str, &lasttitle);
3236
3237 if (p_icon)
3238 {
3239 i_str = buf;
3240 if (*p_iconstring != NUL)
3241 {
3242#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003243 if (stl_syntax & STL_IN_ICON)
3244 {
3245 int use_sandbox = FALSE;
3246 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003247
3248# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003249 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003250# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003251 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003253 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003254 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003255 if (called_emsg)
3256 set_string_option_direct((char_u *)"iconstring", -1,
3257 (char_u *)"", OPT_FREE, SID_ERROR);
3258 called_emsg |= save_called_emsg;
3259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 else
3261#endif
3262 i_str = p_iconstring;
3263 }
3264 else
3265 {
3266 if (buf_spname(curbuf) != NULL)
3267 i_name = (char_u *)buf_spname(curbuf);
3268 else /* use file name only in icon */
3269 i_name = gettail(curbuf->b_ffname);
3270 *i_str = NUL;
3271 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003272 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 if (len > 100)
3274 {
3275 len -= 100;
3276#ifdef FEAT_MBYTE
3277 if (has_mbyte)
3278 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3279#endif
3280 i_name += len;
3281 }
3282 STRCPY(i_str, i_name);
3283 trans_characters(i_str, IOSIZE);
3284 }
3285 }
3286
3287 mustset |= ti_change(i_str, &lasticon);
3288
3289 if (mustset)
3290 resettitle();
3291}
3292
3293/*
3294 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3295 * from "str" if it does.
3296 * Return TRUE when "*last" changed.
3297 */
3298 static int
3299ti_change(str, last)
3300 char_u *str;
3301 char_u **last;
3302{
3303 if ((str == NULL) != (*last == NULL)
3304 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3305 {
3306 vim_free(*last);
3307 if (str == NULL)
3308 *last = NULL;
3309 else
3310 *last = vim_strsave(str);
3311 return TRUE;
3312 }
3313 return FALSE;
3314}
3315
3316/*
3317 * Put current window title back (used after calling a shell)
3318 */
3319 void
3320resettitle()
3321{
3322 mch_settitle(lasttitle, lasticon);
3323}
Bram Moolenaarea408852005-06-25 22:49:46 +00003324
3325# if defined(EXITFREE) || defined(PROTO)
3326 void
3327free_titles()
3328{
3329 vim_free(lasttitle);
3330 vim_free(lasticon);
3331}
3332# endif
3333
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334#endif /* FEAT_TITLE */
3335
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003336#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003338 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 * Return length of string in screen cells.
3340 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003341 * Normally works for window "wp", except when working for 'tabline' then it
3342 * is "curwin".
3343 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 * Items are drawn interspersed with the text that surrounds it
3345 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3346 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3347 *
3348 * If maxwidth is not zero, the string will be filled at any middle marker
3349 * or truncated if too long, fillchar is used for all whitespace.
3350 */
3351 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003352build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003354 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 size_t outlen; /* length of out[] */
3356 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003357 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 int fillchar;
3359 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003360 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3361 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363 char_u *p;
3364 char_u *s;
3365 char_u *t;
3366 char_u *linecont;
3367#ifdef FEAT_EVAL
3368 win_T *o_curwin;
3369 buf_T *o_curbuf;
3370#endif
3371 int empty_line;
3372 colnr_T virtcol;
3373 long l;
3374 long n;
3375 int prevchar_isflag;
3376 int prevchar_isitem;
3377 int itemisflag;
3378 int fillable;
3379 char_u *str;
3380 long num;
3381 int width;
3382 int itemcnt;
3383 int curitem;
3384 int groupitem[STL_MAX_ITEM];
3385 int groupdepth;
3386 struct stl_item
3387 {
3388 char_u *start;
3389 int minwid;
3390 int maxwid;
3391 enum
3392 {
3393 Normal,
3394 Empty,
3395 Group,
3396 Middle,
3397 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003398 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 Trunc
3400 } type;
3401 } item[STL_MAX_ITEM];
3402 int minwid;
3403 int maxwid;
3404 int zeropad;
3405 char_u base;
3406 char_u opt;
3407#define TMPLEN 70
3408 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003409 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003410 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003411
3412#ifdef FEAT_EVAL
3413 /*
3414 * When the format starts with "%!" then evaluate it as an expression and
3415 * use the result as the actual format string.
3416 */
3417 if (fmt[0] == '%' && fmt[1] == '!')
3418 {
3419 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3420 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003421 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003422 }
3423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425 if (fillchar == 0)
3426 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003427#ifdef FEAT_MBYTE
3428 /* Can't handle a multi-byte fill character yet. */
3429 else if (mb_char2len(fillchar) > 1)
3430 fillchar = '-';
3431#endif
3432
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 /*
3434 * Get line & check if empty (cursorpos will show "0-1").
3435 * If inversion is possible we use it. Else '=' characters are used.
3436 */
3437 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3438 empty_line = (*linecont == NUL);
3439
3440 groupdepth = 0;
3441 p = out;
3442 curitem = 0;
3443 prevchar_isflag = TRUE;
3444 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003445 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 {
3447 if (*s != NUL && *s != '%')
3448 prevchar_isflag = prevchar_isitem = FALSE;
3449
3450 /*
3451 * Handle up to the next '%' or the end.
3452 */
3453 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3454 *p++ = *s++;
3455 if (*s == NUL || p + 1 >= out + outlen)
3456 break;
3457
3458 /*
3459 * Handle one '%' item.
3460 */
3461 s++;
3462 if (*s == '%')
3463 {
3464 if (p + 1 >= out + outlen)
3465 break;
3466 *p++ = *s++;
3467 prevchar_isflag = prevchar_isitem = FALSE;
3468 continue;
3469 }
3470 if (*s == STL_MIDDLEMARK)
3471 {
3472 s++;
3473 if (groupdepth > 0)
3474 continue;
3475 item[curitem].type = Middle;
3476 item[curitem++].start = p;
3477 continue;
3478 }
3479 if (*s == STL_TRUNCMARK)
3480 {
3481 s++;
3482 item[curitem].type = Trunc;
3483 item[curitem++].start = p;
3484 continue;
3485 }
3486 if (*s == ')')
3487 {
3488 s++;
3489 if (groupdepth < 1)
3490 continue;
3491 groupdepth--;
3492
3493 t = item[groupitem[groupdepth]].start;
3494 *p = NUL;
3495 l = vim_strsize(t);
3496 if (curitem > groupitem[groupdepth] + 1
3497 && item[groupitem[groupdepth]].minwid == 0)
3498 {
3499 /* remove group if all items are empty */
3500 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3501 if (item[n].type == Normal)
3502 break;
3503 if (n == curitem)
3504 {
3505 p = t;
3506 l = 0;
3507 }
3508 }
3509 if (l > item[groupitem[groupdepth]].maxwid)
3510 {
3511 /* truncate, remove n bytes of text at the start */
3512#ifdef FEAT_MBYTE
3513 if (has_mbyte)
3514 {
3515 /* Find the first character that should be included. */
3516 n = 0;
3517 while (l >= item[groupitem[groupdepth]].maxwid)
3518 {
3519 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003520 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 }
3522 }
3523 else
3524#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003525 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526
3527 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003528 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 p = p - n + 1;
3530#ifdef FEAT_MBYTE
3531 /* Fill up space left over by half a double-wide char. */
3532 while (++l < item[groupitem[groupdepth]].minwid)
3533 *p++ = fillchar;
3534#endif
3535
3536 /* correct the start of the items for the truncation */
3537 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3538 {
3539 item[l].start -= n;
3540 if (item[l].start < t)
3541 item[l].start = t;
3542 }
3543 }
3544 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3545 {
3546 /* fill */
3547 n = item[groupitem[groupdepth]].minwid;
3548 if (n < 0)
3549 {
3550 /* fill by appending characters */
3551 n = 0 - n;
3552 while (l++ < n && p + 1 < out + outlen)
3553 *p++ = fillchar;
3554 }
3555 else
3556 {
3557 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003558 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 l = n - l;
3560 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003561 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 p += l;
3563 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3564 item[n].start += l;
3565 for ( ; l > 0; l--)
3566 *t++ = fillchar;
3567 }
3568 }
3569 continue;
3570 }
3571 minwid = 0;
3572 maxwid = 9999;
3573 zeropad = FALSE;
3574 l = 1;
3575 if (*s == '0')
3576 {
3577 s++;
3578 zeropad = TRUE;
3579 }
3580 if (*s == '-')
3581 {
3582 s++;
3583 l = -1;
3584 }
3585 if (VIM_ISDIGIT(*s))
3586 {
3587 minwid = (int)getdigits(&s);
3588 if (minwid < 0) /* overflow */
3589 minwid = 0;
3590 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003591 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
3593 item[curitem].type = Highlight;
3594 item[curitem].start = p;
3595 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3596 s++;
3597 curitem++;
3598 continue;
3599 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003600 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3601 {
3602 if (*s == STL_TABCLOSENR)
3603 {
3604 if (minwid == 0)
3605 {
3606 /* %X ends the close label, go back to the previously
3607 * define tab label nr. */
3608 for (n = curitem - 1; n >= 0; --n)
3609 if (item[n].type == TabPage && item[n].minwid >= 0)
3610 {
3611 minwid = item[n].minwid;
3612 break;
3613 }
3614 }
3615 else
3616 /* close nrs are stored as negative values */
3617 minwid = - minwid;
3618 }
3619 item[curitem].type = TabPage;
3620 item[curitem].start = p;
3621 item[curitem].minwid = minwid;
3622 s++;
3623 curitem++;
3624 continue;
3625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 if (*s == '.')
3627 {
3628 s++;
3629 if (VIM_ISDIGIT(*s))
3630 {
3631 maxwid = (int)getdigits(&s);
3632 if (maxwid <= 0) /* overflow */
3633 maxwid = 50;
3634 }
3635 }
3636 minwid = (minwid > 50 ? 50 : minwid) * l;
3637 if (*s == '(')
3638 {
3639 groupitem[groupdepth++] = curitem;
3640 item[curitem].type = Group;
3641 item[curitem].start = p;
3642 item[curitem].minwid = minwid;
3643 item[curitem].maxwid = maxwid;
3644 s++;
3645 curitem++;
3646 continue;
3647 }
3648 if (vim_strchr(STL_ALL, *s) == NULL)
3649 {
3650 s++;
3651 continue;
3652 }
3653 opt = *s++;
3654
3655 /* OK - now for the real work */
3656 base = 'D';
3657 itemisflag = FALSE;
3658 fillable = TRUE;
3659 num = -1;
3660 str = NULL;
3661 switch (opt)
3662 {
3663 case STL_FILEPATH:
3664 case STL_FULLPATH:
3665 case STL_FILENAME:
3666 fillable = FALSE; /* don't change ' ' to fillchar */
3667 if (buf_spname(wp->w_buffer) != NULL)
3668 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3669 else
3670 {
3671 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003672 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3674 }
3675 trans_characters(NameBuff, MAXPATHL);
3676 if (opt != STL_FILENAME)
3677 str = NameBuff;
3678 else
3679 str = gettail(NameBuff);
3680 break;
3681
3682 case STL_VIM_EXPR: /* '{' */
3683 itemisflag = TRUE;
3684 t = p;
3685 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3686 *p++ = *s++;
3687 if (*s != '}') /* missing '}' or out of space */
3688 break;
3689 s++;
3690 *p = 0;
3691 p = t;
3692
3693#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003694 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3696
3697 o_curbuf = curbuf;
3698 o_curwin = curwin;
3699 curwin = wp;
3700 curbuf = wp->w_buffer;
3701
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003702 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703
3704 curwin = o_curwin;
3705 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003706 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
3708 if (str != NULL && *str != 0)
3709 {
3710 if (*skipdigits(str) == NUL)
3711 {
3712 num = atoi((char *)str);
3713 vim_free(str);
3714 str = NULL;
3715 itemisflag = FALSE;
3716 }
3717 }
3718#endif
3719 break;
3720
3721 case STL_LINE:
3722 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3723 ? 0L : (long)(wp->w_cursor.lnum);
3724 break;
3725
3726 case STL_NUMLINES:
3727 num = wp->w_buffer->b_ml.ml_line_count;
3728 break;
3729
3730 case STL_COLUMN:
3731 num = !(State & INSERT) && empty_line
3732 ? 0 : (int)wp->w_cursor.col + 1;
3733 break;
3734
3735 case STL_VIRTCOL:
3736 case STL_VIRTCOL_ALT:
3737 /* In list mode virtcol needs to be recomputed */
3738 virtcol = wp->w_virtcol;
3739 if (wp->w_p_list && lcs_tab1 == NUL)
3740 {
3741 wp->w_p_list = FALSE;
3742 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3743 wp->w_p_list = TRUE;
3744 }
3745 ++virtcol;
3746 /* Don't display %V if it's the same as %c. */
3747 if (opt == STL_VIRTCOL_ALT
3748 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3749 ? 0 : (int)wp->w_cursor.col + 1)))
3750 break;
3751 num = (long)virtcol;
3752 break;
3753
3754 case STL_PERCENTAGE:
3755 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3756 (long)wp->w_buffer->b_ml.ml_line_count);
3757 break;
3758
3759 case STL_ALTPERCENT:
3760 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003761 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 break;
3763
3764 case STL_ARGLISTSTAT:
3765 fillable = FALSE;
3766 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003767 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 str = tmp;
3769 break;
3770
3771 case STL_KEYMAP:
3772 fillable = FALSE;
3773 if (get_keymap_str(wp, tmp, TMPLEN))
3774 str = tmp;
3775 break;
3776 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003777#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003778 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779#else
3780 num = 0;
3781#endif
3782 break;
3783
3784 case STL_BUFNO:
3785 num = wp->w_buffer->b_fnum;
3786 break;
3787
3788 case STL_OFFSET_X:
3789 base = 'X';
3790 case STL_OFFSET:
3791#ifdef FEAT_BYTEOFF
3792 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3793 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3794 0L : l + 1 + (!(State & INSERT) && empty_line ?
3795 0 : (int)wp->w_cursor.col);
3796#endif
3797 break;
3798
3799 case STL_BYTEVAL_X:
3800 base = 'X';
3801 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003802 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 num = 0;
3804 else
3805 {
3806#ifdef FEAT_MBYTE
3807 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3808#else
3809 num = linecont[wp->w_cursor.col];
3810#endif
3811 }
3812 if (num == NL)
3813 num = 0;
3814 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3815 num = NL;
3816 break;
3817
3818 case STL_ROFLAG:
3819 case STL_ROFLAG_ALT:
3820 itemisflag = TRUE;
3821 if (wp->w_buffer->b_p_ro)
3822 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3823 break;
3824
3825 case STL_HELPFLAG:
3826 case STL_HELPFLAG_ALT:
3827 itemisflag = TRUE;
3828 if (wp->w_buffer->b_help)
3829 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003830 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 break;
3832
3833#ifdef FEAT_AUTOCMD
3834 case STL_FILETYPE:
3835 if (*wp->w_buffer->b_p_ft != NUL
3836 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3837 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003838 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3839 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 str = tmp;
3841 }
3842 break;
3843
3844 case STL_FILETYPE_ALT:
3845 itemisflag = TRUE;
3846 if (*wp->w_buffer->b_p_ft != NUL
3847 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3848 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003849 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3850 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 for (t = tmp; *t != 0; t++)
3852 *t = TOUPPER_LOC(*t);
3853 str = tmp;
3854 }
3855 break;
3856#endif
3857
3858#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3859 case STL_PREVIEWFLAG:
3860 case STL_PREVIEWFLAG_ALT:
3861 itemisflag = TRUE;
3862 if (wp->w_p_pvw)
3863 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3864 : _("[Preview]"));
3865 break;
3866#endif
3867
3868 case STL_MODIFIED:
3869 case STL_MODIFIED_ALT:
3870 itemisflag = TRUE;
3871 switch ((opt == STL_MODIFIED_ALT)
3872 + bufIsChanged(wp->w_buffer) * 2
3873 + (!wp->w_buffer->b_p_ma) * 4)
3874 {
3875 case 2: str = (char_u *)"[+]"; break;
3876 case 3: str = (char_u *)",+"; break;
3877 case 4: str = (char_u *)"[-]"; break;
3878 case 5: str = (char_u *)",-"; break;
3879 case 6: str = (char_u *)"[+-]"; break;
3880 case 7: str = (char_u *)",+-"; break;
3881 }
3882 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003883
3884 case STL_HIGHLIGHT:
3885 t = s;
3886 while (*s != '#' && *s != NUL)
3887 ++s;
3888 if (*s == '#')
3889 {
3890 item[curitem].type = Highlight;
3891 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003892 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003893 curitem++;
3894 }
3895 ++s;
3896 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
3898
3899 item[curitem].start = p;
3900 item[curitem].type = Normal;
3901 if (str != NULL && *str)
3902 {
3903 t = str;
3904 if (itemisflag)
3905 {
3906 if ((t[0] && t[1])
3907 && ((!prevchar_isitem && *t == ',')
3908 || (prevchar_isflag && *t == ' ')))
3909 t++;
3910 prevchar_isflag = TRUE;
3911 }
3912 l = vim_strsize(t);
3913 if (l > 0)
3914 prevchar_isitem = TRUE;
3915 if (l > maxwid)
3916 {
3917 while (l >= maxwid)
3918#ifdef FEAT_MBYTE
3919 if (has_mbyte)
3920 {
3921 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003922 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 }
3924 else
3925#endif
3926 l -= byte2cells(*t++);
3927 if (p + 1 >= out + outlen)
3928 break;
3929 *p++ = '<';
3930 }
3931 if (minwid > 0)
3932 {
3933 for (; l < minwid && p + 1 < out + outlen; l++)
3934 {
3935 /* Don't put a "-" in front of a digit. */
3936 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3937 *p++ = ' ';
3938 else
3939 *p++ = fillchar;
3940 }
3941 minwid = 0;
3942 }
3943 else
3944 minwid *= -1;
3945 while (*t && p + 1 < out + outlen)
3946 {
3947 *p++ = *t++;
3948 /* Change a space by fillchar, unless fillchar is '-' and a
3949 * digit follows. */
3950 if (fillable && p[-1] == ' '
3951 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3952 p[-1] = fillchar;
3953 }
3954 for (; l < minwid && p + 1 < out + outlen; l++)
3955 *p++ = fillchar;
3956 }
3957 else if (num >= 0)
3958 {
3959 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3960 char_u nstr[20];
3961
3962 if (p + 20 >= out + outlen)
3963 break; /* not sufficient space */
3964 prevchar_isitem = TRUE;
3965 t = nstr;
3966 if (opt == STL_VIRTCOL_ALT)
3967 {
3968 *t++ = '-';
3969 minwid--;
3970 }
3971 *t++ = '%';
3972 if (zeropad)
3973 *t++ = '0';
3974 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003975 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 *t = 0;
3977
3978 for (n = num, l = 1; n >= nbase; n /= nbase)
3979 l++;
3980 if (opt == STL_VIRTCOL_ALT)
3981 l++;
3982 if (l > maxwid)
3983 {
3984 l += 2;
3985 n = l - maxwid;
3986 while (l-- > maxwid)
3987 num /= nbase;
3988 *t++ = '>';
3989 *t++ = '%';
3990 *t = t[-3];
3991 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003992 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3993 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 }
3995 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003996 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3997 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 p += STRLEN(p);
3999 }
4000 else
4001 item[curitem].type = Empty;
4002
4003 if (opt == STL_VIM_EXPR)
4004 vim_free(str);
4005
4006 if (num >= 0 || (!itemisflag && str && *str))
4007 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4008 curitem++;
4009 }
4010 *p = NUL;
4011 itemcnt = curitem;
4012
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004013#ifdef FEAT_EVAL
4014 if (usefmt != fmt)
4015 vim_free(usefmt);
4016#endif
4017
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 width = vim_strsize(out);
4019 if (maxwidth > 0 && width > maxwidth)
4020 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004021 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 l = 0;
4023 if (itemcnt == 0)
4024 s = out;
4025 else
4026 {
4027 for ( ; l < itemcnt; l++)
4028 if (item[l].type == Trunc)
4029 {
4030 /* Truncate at %< item. */
4031 s = item[l].start;
4032 break;
4033 }
4034 if (l == itemcnt)
4035 {
4036 /* No %< item, truncate first item. */
4037 s = item[0].start;
4038 l = 0;
4039 }
4040 }
4041
4042 if (width - vim_strsize(s) >= maxwidth)
4043 {
4044 /* Truncation mark is beyond max length */
4045#ifdef FEAT_MBYTE
4046 if (has_mbyte)
4047 {
4048 s = out;
4049 width = 0;
4050 for (;;)
4051 {
4052 width += ptr2cells(s);
4053 if (width >= maxwidth)
4054 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004055 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 }
4057 /* Fill up for half a double-wide character. */
4058 while (++width < maxwidth)
4059 *s++ = fillchar;
4060 }
4061 else
4062#endif
4063 s = out + maxwidth - 1;
4064 for (l = 0; l < itemcnt; l++)
4065 if (item[l].start > s)
4066 break;
4067 itemcnt = l;
4068 *s++ = '>';
4069 *s = 0;
4070 }
4071 else
4072 {
4073#ifdef FEAT_MBYTE
4074 if (has_mbyte)
4075 {
4076 n = 0;
4077 while (width >= maxwidth)
4078 {
4079 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004080 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
4082 }
4083 else
4084#endif
4085 n = width - maxwidth + 1;
4086 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004087 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 *s = '<';
4089
4090 /* Fill up for half a double-wide character. */
4091 while (++width < maxwidth)
4092 {
4093 s = s + STRLEN(s);
4094 *s++ = fillchar;
4095 *s = NUL;
4096 }
4097
4098 --n; /* count the '<' */
4099 for (; l < itemcnt; l++)
4100 {
4101 if (item[l].start - n >= s)
4102 item[l].start -= n;
4103 else
4104 item[l].start = s;
4105 }
4106 }
4107 width = maxwidth;
4108 }
4109 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4110 {
4111 /* Apply STL_MIDDLE if any */
4112 for (l = 0; l < itemcnt; l++)
4113 if (item[l].type == Middle)
4114 break;
4115 if (l < itemcnt)
4116 {
4117 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004118 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 for (s = item[l].start; s < p; s++)
4120 *s = fillchar;
4121 for (l++; l < itemcnt; l++)
4122 item[l].start += maxwidth - width;
4123 width = maxwidth;
4124 }
4125 }
4126
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004127 /* Store the info about highlighting. */
4128 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004130 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 for (l = 0; l < itemcnt; l++)
4132 {
4133 if (item[l].type == Highlight)
4134 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004135 sp->start = item[l].start;
4136 sp->userhl = item[l].minwid;
4137 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
4139 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004140 sp->start = NULL;
4141 sp->userhl = 0;
4142 }
4143
4144 /* Store the info about tab pages labels. */
4145 if (tabtab != NULL)
4146 {
4147 sp = tabtab;
4148 for (l = 0; l < itemcnt; l++)
4149 {
4150 if (item[l].type == TabPage)
4151 {
4152 sp->start = item[l].start;
4153 sp->userhl = item[l].minwid;
4154 sp++;
4155 }
4156 }
4157 sp->start = NULL;
4158 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 }
4160
4161 return width;
4162}
4163#endif /* FEAT_STL_OPT */
4164
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004165#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4166 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004168 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4169 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 */
4171 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004172get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004174 char_u *buf;
4175 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176{
4177 long above; /* number of lines above window */
4178 long below; /* number of lines below window */
4179
4180 above = wp->w_topline - 1;
4181#ifdef FEAT_DIFF
4182 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4183#endif
4184 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4185 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004186 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4187 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004189 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004191 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 ? (int)(above / ((above + below) / 100L))
4193 : (int)(above * 100L / (above + below)));
4194}
4195#endif
4196
4197/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004198 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 * Return TRUE if it was appended.
4200 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004201 static int
4202append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 win_T *wp;
4204 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004205 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
4208 char_u *p;
4209
4210 if (ARGCOUNT <= 1) /* nothing to do */
4211 return FALSE;
4212
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004213 p = buf + STRLEN(buf); /* go to the end of the buffer */
4214 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 return FALSE;
4216 *p++ = ' ';
4217 *p++ = '(';
4218 if (add_file)
4219 {
4220 STRCPY(p, "file ");
4221 p += 5;
4222 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004223 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4224 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4226 return TRUE;
4227}
4228
4229/*
4230 * If fname is not a full path, make it a full path.
4231 * Returns pointer to allocated memory (NULL for failure).
4232 */
4233 char_u *
4234fix_fname(fname)
4235 char_u *fname;
4236{
4237 /*
4238 * Force expanding the path always for Unix, because symbolic links may
4239 * mess up the full path name, even though it starts with a '/'.
4240 * Also expand when there is ".." in the file name, try to remove it,
4241 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004242 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 * For MS-Windows also expand names like "longna~1" to "longname".
4244 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004245#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 return FullName_save(fname, TRUE);
4247#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004248 if (!vim_isAbsName(fname)
4249 || strstr((char *)fname, "..") != NULL
4250 || strstr((char *)fname, "//") != NULL
4251# ifdef BACKSLASH_IN_FILENAME
4252 || strstr((char *)fname, "\\\\") != NULL
4253# endif
4254# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 )
4258 return FullName_save(fname, FALSE);
4259
4260 fname = vim_strsave(fname);
4261
Bram Moolenaar9b942202007-10-03 12:31:33 +00004262# ifdef USE_FNAME_CASE
4263# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 {
4267 if (fname != NULL)
4268 fname_case(fname, 0); /* set correct case for file name */
4269 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
4272 return fname;
4273#endif
4274}
4275
4276/*
4277 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4278 * "ffname" becomes a pointer to allocated memory (or NULL).
4279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 void
4281fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004282 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 char_u **ffname;
4284 char_u **sfname;
4285{
4286 if (*ffname == NULL) /* if no file name given, nothing to do */
4287 return;
4288 if (*sfname == NULL) /* if no short file name given, use ffname */
4289 *sfname = *ffname;
4290 *ffname = fix_fname(*ffname); /* expand to full path */
4291
4292#ifdef FEAT_SHORTCUT
4293 if (!buf->b_p_bin)
4294 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004295 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
4297 /* If the file name is a shortcut file, use the file it links to. */
4298 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004299 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 {
4301 vim_free(*ffname);
4302 *ffname = rfname;
4303 *sfname = rfname;
4304 }
4305 }
4306#endif
4307}
4308
4309/*
4310 * Get the file name for an argument list entry.
4311 */
4312 char_u *
4313alist_name(aep)
4314 aentry_T *aep;
4315{
4316 buf_T *bp;
4317
4318 /* Use the name from the associated buffer if it exists. */
4319 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004320 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 return aep->ae_fname;
4322 return bp->b_fname;
4323}
4324
4325#if defined(FEAT_WINDOWS) || defined(PROTO)
4326/*
4327 * do_arg_all(): Open up to 'count' windows, one for each argument.
4328 */
4329 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004330do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 int count;
4332 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004333 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334{
4335 int i;
4336 win_T *wp, *wpnext;
4337 char_u *opened; /* array of flags for which args are open */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004338 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 int use_firstwin = FALSE; /* use first window for arglist */
4340 int split_ret = OK;
4341 int p_ea_save;
4342 alist_T *alist; /* argument list to be used */
4343 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004344 tabpage_T *tpnext;
4345 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004346 win_T *new_curwin = NULL;
4347 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348
4349 if (ARGCOUNT <= 0)
4350 {
4351 /* Don't give an error message. We don't want it when the ":all"
4352 * command is in the .vimrc. */
4353 return;
4354 }
4355 setpcmark();
4356
4357 opened_len = ARGCOUNT;
4358 opened = alloc_clear((unsigned)opened_len);
4359 if (opened == NULL)
4360 return;
4361
4362#ifdef FEAT_GUI
4363 need_mouse_correct = TRUE;
4364#endif
4365
4366 /*
4367 * Try closing all windows that are not in the argument list.
4368 * Also close windows that are not full width;
4369 * When 'hidden' or "forceit" set the buffer becomes hidden.
4370 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004371 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004373 if (had_tab > 0)
4374 goto_tabpage_tp(first_tabpage);
4375 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004377 tpnext = curtab->tp_next;
4378 for (wp = firstwin; wp != NULL; wp = wpnext)
4379 {
4380 wpnext = wp->w_next;
4381 buf = wp->w_buffer;
4382 if (buf->b_ffname == NULL
4383 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004385 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004387 )
4388 i = ARGCOUNT;
4389 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004391 /* check if the buffer in this window is in the arglist */
4392 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004394 if (ARGLIST[i].ae_fnum == buf->b_fnum
4395 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004396 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004398 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004399 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004400 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004401 if (i == 0)
4402 {
4403 new_curwin = wp;
4404 new_curtab = curtab;
4405 }
4406 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004407 if (wp->w_alist != curwin->w_alist)
4408 {
4409 /* Use the current argument list for all windows
4410 * containing a file from it. */
4411 alist_unlink(wp->w_alist);
4412 wp->w_alist = curwin->w_alist;
4413 ++wp->w_alist->al_refcount;
4414 }
4415 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
4418 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004419 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004421 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004423 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004424 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004426 /* If the buffer was changed, and we would like to hide it,
4427 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004428 if (!P_HID(buf) && buf->b_nwindows <= 1
4429 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004431 (void)autowrite(buf, FALSE);
4432#ifdef FEAT_AUTOCMD
4433 /* check if autocommands removed the window */
4434 if (!win_valid(wp) || !buf_valid(buf))
4435 {
4436 wpnext = firstwin; /* start all over... */
4437 continue;
4438 }
4439#endif
4440 }
4441#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004442 /* don't close last window */
4443 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004444#endif
4445 use_firstwin = TRUE;
4446#ifdef FEAT_WINDOWS
4447 else
4448 {
4449 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4450# ifdef FEAT_AUTOCMD
4451 /* check if autocommands removed the next window */
4452 if (!win_valid(wpnext))
4453 wpnext = firstwin; /* start all over... */
4454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 }
4456#endif
4457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 }
4459 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004460
4461 /* Without the ":tab" modifier only do the current tab page. */
4462 if (had_tab == 0 || tpnext == NULL)
4463 break;
4464
4465# ifdef FEAT_AUTOCMD
4466 /* check if autocommands removed the next tab page */
4467 if (!valid_tabpage(tpnext))
4468 tpnext = first_tabpage; /* start all over...*/
4469# endif
4470 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472
4473 /*
4474 * Open a window for files in the argument list that don't have one.
4475 * ARGCOUNT may change while doing this, because of autocommands.
4476 */
4477 if (count > ARGCOUNT || count <= 0)
4478 count = ARGCOUNT;
4479
4480 /* Autocommands may do anything to the argument list. Make sure it's not
4481 * freed while we are working here by "locking" it. We still have to
4482 * watch out for its size to be changed. */
4483 alist = curwin->w_alist;
4484 ++alist->al_refcount;
4485
4486#ifdef FEAT_AUTOCMD
4487 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4488 ++autocmd_no_enter;
4489 ++autocmd_no_leave;
4490#endif
4491 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004492#ifdef FEAT_WINDOWS
4493 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4494 * leaving an empty tab page when executed locally. */
4495 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4496 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4497 use_firstwin = TRUE;
4498#endif
4499
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4501 {
4502 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4503 arg_had_last = TRUE;
4504 if (i < opened_len && opened[i])
4505 {
4506 /* Move the already present window to below the current window */
4507 if (curwin->w_arg_idx != i)
4508 {
4509 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4510 {
4511 if (wpnext->w_arg_idx == i)
4512 {
4513 win_move_after(wpnext, curwin);
4514 break;
4515 }
4516 }
4517 }
4518 }
4519 else if (split_ret == OK)
4520 {
4521 if (!use_firstwin) /* split current window */
4522 {
4523 p_ea_save = p_ea;
4524 p_ea = TRUE; /* use space from all windows */
4525 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4526 p_ea = p_ea_save;
4527 if (split_ret == FAIL)
4528 continue;
4529 }
4530#ifdef FEAT_AUTOCMD
4531 else /* first window: do autocmd for leaving this buffer */
4532 --autocmd_no_leave;
4533#endif
4534
4535 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004536 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 */
4538 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004539 if (i == 0)
4540 {
4541 new_curwin = curwin;
4542 new_curtab = curtab;
4543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4545 ECMD_ONE,
4546 ((P_HID(curwin->w_buffer)
4547 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004548 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549#ifdef FEAT_AUTOCMD
4550 if (use_firstwin)
4551 ++autocmd_no_leave;
4552#endif
4553 use_firstwin = FALSE;
4554 }
4555 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004556
4557 /* When ":tab" was used open a new tab for a new window repeatedly. */
4558 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4559 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
4561
4562 /* Remove the "lock" on the argument list. */
4563 alist_unlink(alist);
4564
4565#ifdef FEAT_AUTOCMD
4566 --autocmd_no_enter;
4567#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004568 /* to window with first arg */
4569 if (valid_tabpage(new_curtab))
4570 goto_tabpage_tp(new_curtab);
4571 if (win_valid(new_curwin))
4572 win_enter(new_curwin, FALSE);
4573
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574#ifdef FEAT_AUTOCMD
4575 --autocmd_no_leave;
4576#endif
4577 vim_free(opened);
4578}
4579
4580# if defined(FEAT_LISTCMDS) || defined(PROTO)
4581/*
4582 * Open a window for a number of buffers.
4583 */
4584 void
4585ex_buffer_all(eap)
4586 exarg_T *eap;
4587{
4588 buf_T *buf;
4589 win_T *wp, *wpnext;
4590 int split_ret = OK;
4591 int p_ea_save;
4592 int open_wins = 0;
4593 int r;
4594 int count; /* Maximum number of windows to open. */
4595 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004596#ifdef FEAT_WINDOWS
4597 int had_tab = cmdmod.tab;
4598 tabpage_T *tpnext;
4599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600
4601 if (eap->addr_count == 0) /* make as many windows as possible */
4602 count = 9999;
4603 else
4604 count = eap->line2; /* make as many windows as specified */
4605 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4606 all = FALSE;
4607 else
4608 all = TRUE;
4609
4610 setpcmark();
4611
4612#ifdef FEAT_GUI
4613 need_mouse_correct = TRUE;
4614#endif
4615
4616 /*
4617 * Close superfluous windows (two windows for the same buffer).
4618 * Also close windows that are not full-width.
4619 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004620#ifdef FEAT_WINDOWS
4621 if (had_tab > 0)
4622 goto_tabpage_tp(first_tabpage);
4623 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004626 tpnext = curtab->tp_next;
4627 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004629 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004630 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004631#ifdef FEAT_VERTSPLIT
4632 || ((cmdmod.split & WSP_VERT)
4633 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004634 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004635 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004637#ifdef FEAT_WINDOWS
4638 || (had_tab > 0 && wp != firstwin)
4639#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004640 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004641 {
4642 win_close(wp, FALSE);
4643#ifdef FEAT_AUTOCMD
4644 wpnext = firstwin; /* just in case an autocommand does
4645 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004646 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004647 open_wins = 0;
4648#endif
4649 }
4650 else
4651 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004653
4654#ifdef FEAT_WINDOWS
4655 /* Without the ":tab" modifier only do the current tab page. */
4656 if (had_tab == 0 || tpnext == NULL)
4657 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004658 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
4662 /*
4663 * Go through the buffer list. When a buffer doesn't have a window yet,
4664 * open one. Otherwise move the window to the right position.
4665 * Watch out for autocommands that delete buffers or windows!
4666 */
4667#ifdef FEAT_AUTOCMD
4668 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4669 ++autocmd_no_enter;
4670#endif
4671 win_enter(lastwin, FALSE);
4672#ifdef FEAT_AUTOCMD
4673 ++autocmd_no_leave;
4674#endif
4675 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4676 {
4677 /* Check if this buffer needs a window */
4678 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4679 continue;
4680
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004681#ifdef FEAT_WINDOWS
4682 if (had_tab != 0)
4683 {
4684 /* With the ":tab" modifier don't move the window. */
4685 if (buf->b_nwindows > 0)
4686 wp = lastwin; /* buffer has a window, skip it */
4687 else
4688 wp = NULL;
4689 }
4690 else
4691#endif
4692 {
4693 /* Check if this buffer already has a window */
4694 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4695 if (wp->w_buffer == buf)
4696 break;
4697 /* If the buffer already has a window, move it */
4698 if (wp != NULL)
4699 win_move_after(wp, curwin);
4700 }
4701
4702 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
4704 /* Split the window and put the buffer in it */
4705 p_ea_save = p_ea;
4706 p_ea = TRUE; /* use space from all windows */
4707 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4708 ++open_wins;
4709 p_ea = p_ea_save;
4710 if (split_ret == FAIL)
4711 continue;
4712
4713 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004714#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 swap_exists_action = SEA_DIALOG;
4716#endif
4717 set_curbuf(buf, DOBUF_GOTO);
4718#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004721#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004723# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 break;
4725 }
4726#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004727#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 if (swap_exists_action == SEA_QUIT)
4729 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004730# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4731 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004732
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004733 /* Reset the error/interrupt/exception state here so that
4734 * aborting() returns FALSE when closing a window. */
4735 enter_cleanup(&cs);
4736# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004737
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004738 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 win_close(curwin, TRUE);
4740 --open_wins;
4741 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004742 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004743
4744# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4745 /* Restore the error/interrupt/exception state if not
4746 * discarded by a new aborting error, interrupt, or uncaught
4747 * exception. */
4748 leave_cleanup(&cs);
4749# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 }
4751 else
4752 handle_swap_exists(NULL);
4753#endif
4754 }
4755
4756 ui_breakcheck();
4757 if (got_int)
4758 {
4759 (void)vgetc(); /* only break the file loading, not the rest */
4760 break;
4761 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004762#ifdef FEAT_EVAL
4763 /* Autocommands deleted the buffer or aborted script processing!!! */
4764 if (aborting())
4765 break;
4766#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004767#ifdef FEAT_WINDOWS
4768 /* When ":tab" was used open a new tab for a new window repeatedly. */
4769 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4770 cmdmod.tab = 9999;
4771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 }
4773#ifdef FEAT_AUTOCMD
4774 --autocmd_no_enter;
4775#endif
4776 win_enter(firstwin, FALSE); /* back to first window */
4777#ifdef FEAT_AUTOCMD
4778 --autocmd_no_leave;
4779#endif
4780
4781 /*
4782 * Close superfluous windows.
4783 */
4784 for (wp = lastwin; open_wins > count; )
4785 {
4786 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4787 || autowrite(wp->w_buffer, FALSE) == OK);
4788#ifdef FEAT_AUTOCMD
4789 if (!win_valid(wp))
4790 {
4791 /* BufWrite Autocommands made the window invalid, start over */
4792 wp = lastwin;
4793 }
4794 else
4795#endif
4796 if (r)
4797 {
4798 win_close(wp, !P_HID(wp->w_buffer));
4799 --open_wins;
4800 wp = lastwin;
4801 }
4802 else
4803 {
4804 wp = wp->w_prev;
4805 if (wp == NULL)
4806 break;
4807 }
4808 }
4809}
4810# endif /* FEAT_LISTCMDS */
4811
4812#endif /* FEAT_WINDOWS */
4813
Bram Moolenaara3227e22006-03-08 21:32:40 +00004814static int chk_modeline __ARGS((linenr_T, int));
4815
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816/*
4817 * do_modelines() - process mode lines for the current file
4818 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004819 * "flags" can be:
4820 * OPT_WINONLY only set options local to window
4821 * OPT_NOWIN don't set options local to window
4822 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 * Returns immediately if the "ml" option isn't set.
4824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004826do_modelines(flags)
4827 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004829 linenr_T lnum;
4830 int nmlines;
4831 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832
4833 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4834 return;
4835
4836 /* Disallow recursive entry here. Can happen when executing a modeline
4837 * triggers an autocommand, which reloads modelines with a ":do". */
4838 if (entered)
4839 return;
4840
4841 ++entered;
4842 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4843 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004844 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 nmlines = 0;
4846
4847 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4848 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004849 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 nmlines = 0;
4851 --entered;
4852}
4853
4854#include "version.h" /* for version number */
4855
4856/*
4857 * chk_modeline() - check a single line for a mode string
4858 * Return FAIL if an error encountered.
4859 */
4860 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004861chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004863 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
4865 char_u *s;
4866 char_u *e;
4867 char_u *linecopy; /* local copy of any modeline found */
4868 int prev;
4869 int vers;
4870 int end;
4871 int retval = OK;
4872 char_u *save_sourcing_name;
4873 linenr_T save_sourcing_lnum;
4874#ifdef FEAT_EVAL
4875 scid_T save_SID;
4876#endif
4877
4878 prev = -1;
4879 for (s = ml_get(lnum); *s != NUL; ++s)
4880 {
4881 if (prev == -1 || vim_isspace(prev))
4882 {
4883 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4884 || STRNCMP(s, "vi:", (size_t)3) == 0)
4885 break;
4886 if (STRNCMP(s, "vim", 3) == 0)
4887 {
4888 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4889 e = s + 4;
4890 else
4891 e = s + 3;
4892 vers = getdigits(&e);
4893 if (*e == ':'
4894 && (s[3] == ':'
4895 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4896 || (VIM_VERSION_100 < vers && s[3] == '<')
4897 || (VIM_VERSION_100 > vers && s[3] == '>')
4898 || (VIM_VERSION_100 == vers && s[3] == '=')))
4899 break;
4900 }
4901 }
4902 prev = *s;
4903 }
4904
4905 if (*s)
4906 {
4907 do /* skip over "ex:", "vi:" or "vim:" */
4908 ++s;
4909 while (s[-1] != ':');
4910
4911 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4912 if (linecopy == NULL)
4913 return FAIL;
4914
4915 save_sourcing_lnum = sourcing_lnum;
4916 save_sourcing_name = sourcing_name;
4917 sourcing_lnum = lnum; /* prepare for emsg() */
4918 sourcing_name = (char_u *)"modelines";
4919
4920 end = FALSE;
4921 while (end == FALSE)
4922 {
4923 s = skipwhite(s);
4924 if (*s == NUL)
4925 break;
4926
4927 /*
4928 * Find end of set command: ':' or end of line.
4929 * Skip over "\:", replacing it with ":".
4930 */
4931 for (e = s; *e != ':' && *e != NUL; ++e)
4932 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00004933 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 if (*e == NUL)
4935 end = TRUE;
4936
4937 /*
4938 * If there is a "set" command, require a terminating ':' and
4939 * ignore the stuff after the ':'.
4940 * "vi:set opt opt opt: foo" -- foo not interpreted
4941 * "vi:opt opt opt: foo" -- foo interpreted
4942 * Accept "se" for compatibility with Elvis.
4943 */
4944 if (STRNCMP(s, "set ", (size_t)4) == 0
4945 || STRNCMP(s, "se ", (size_t)3) == 0)
4946 {
4947 if (*e != ':') /* no terminating ':'? */
4948 break;
4949 end = TRUE;
4950 s = vim_strchr(s, ' ') + 1;
4951 }
4952 *e = NUL; /* truncate the set command */
4953
4954 if (*s != NUL) /* skip over an empty "::" */
4955 {
4956#ifdef FEAT_EVAL
4957 save_SID = current_SID;
4958 current_SID = SID_MODELINE;
4959#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004960 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961#ifdef FEAT_EVAL
4962 current_SID = save_SID;
4963#endif
4964 if (retval == FAIL) /* stop if error found */
4965 break;
4966 }
4967 s = e + 1; /* advance to next part */
4968 }
4969
4970 sourcing_lnum = save_sourcing_lnum;
4971 sourcing_name = save_sourcing_name;
4972
4973 vim_free(linecopy);
4974 }
4975 return retval;
4976}
4977
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00004978#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 int
4980read_viminfo_bufferlist(virp, writing)
4981 vir_T *virp;
4982 int writing;
4983{
4984 char_u *tab;
4985 linenr_T lnum;
4986 colnr_T col;
4987 buf_T *buf;
4988 char_u *sfname;
4989 char_u *xline;
4990
4991 /* Handle long line and escaped characters. */
4992 xline = viminfo_readstring(virp, 1, FALSE);
4993
4994 /* don't read in if there are files on the command-line or if writing: */
4995 if (xline != NULL && !writing && ARGCOUNT == 0
4996 && find_viminfo_parameter('%') != NULL)
4997 {
4998 /* Format is: <fname> Tab <lnum> Tab <col>.
4999 * Watch out for a Tab in the file name, work from the end. */
5000 lnum = 0;
5001 col = 0;
5002 tab = vim_strrchr(xline, '\t');
5003 if (tab != NULL)
5004 {
5005 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005006 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 tab = vim_strrchr(xline, '\t');
5008 if (tab != NULL)
5009 {
5010 *tab++ = '\0';
5011 lnum = atol((char *)tab);
5012 }
5013 }
5014
5015 /* Expand "~/" in the file name at "line + 1" to a full path.
5016 * Then try shortening it by comparing with the current directory */
5017 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005018 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019
5020 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5021 if (buf != NULL) /* just in case... */
5022 {
5023 buf->b_last_cursor.lnum = lnum;
5024 buf->b_last_cursor.col = col;
5025 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5026 }
5027 }
5028 vim_free(xline);
5029
5030 return viminfo_readline(virp);
5031}
5032
5033 void
5034write_viminfo_bufferlist(fp)
5035 FILE *fp;
5036{
5037 buf_T *buf;
5038#ifdef FEAT_WINDOWS
5039 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005040 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041#endif
5042 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005043 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044
5045 if (find_viminfo_parameter('%') == NULL)
5046 return;
5047
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005048 /* Without a number -1 is returned: do all buffers. */
5049 max_buffers = get_viminfo_parameter('%');
5050
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005052#define LINE_BUF_LEN (MAXPATHL + 40)
5053 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 if (line == NULL)
5055 return;
5056
5057#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005058 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 set_last_cursor(win);
5060#else
5061 set_last_cursor(curwin);
5062#endif
5063
5064 fprintf(fp, _("\n# Buffer list:\n"));
5065 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5066 {
5067 if (buf->b_fname == NULL
5068 || !buf->b_p_bl
5069#ifdef FEAT_QUICKFIX
5070 || bt_quickfix(buf)
5071#endif
5072 || removable(buf->b_ffname))
5073 continue;
5074
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005075 if (max_buffers-- == 0)
5076 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 putc('%', fp);
5078 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005079 vim_snprintf_add((char *)line, 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}