blob: cf7ecbbe9517f7995af956af4f43681a882c44d9 [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 Moolenaar325b7a22004-07-05 15:58:32 +0000598#ifdef FEAT_MZSCHEME
599 mzscheme_buffer_free(buf);
600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601#ifdef FEAT_PERL
602 perl_buf_free(buf);
603#endif
604#ifdef FEAT_PYTHON
605 python_buffer_free(buf);
606#endif
607#ifdef FEAT_RUBY
608 ruby_buffer_free(buf);
609#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000610#ifdef FEAT_AUTOCMD
611 aubuflocal_remove(buf);
612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 vim_free(buf);
614}
615
616/*
617 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
618 */
619 static void
620free_buffer_stuff(buf, free_options)
621 buf_T *buf;
622 int free_options; /* free options as well */
623{
624 if (free_options)
625 {
626 clear_wininfo(buf); /* including window-local options */
627 free_buf_options(buf, TRUE);
628 }
629#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000630 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
631 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632#endif
633#ifdef FEAT_USR_CMDS
634 uc_clear(&buf->b_ucmds); /* clear local user commands */
635#endif
636#ifdef FEAT_SIGNS
637 buf_delete_signs(buf); /* delete any signs */
638#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000639#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200640 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642#ifdef FEAT_LOCALMAP
643 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
644 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
645#endif
646#ifdef FEAT_MBYTE
647 vim_free(buf->b_start_fenc);
648 buf->b_start_fenc = NULL;
649#endif
Bram Moolenaar9381ab72008-11-12 11:52:19 +0000650#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200651 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar9381ab72008-11-12 11:52:19 +0000652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653}
654
655/*
656 * Free the b_wininfo list for buffer "buf".
657 */
658 static void
659clear_wininfo(buf)
660 buf_T *buf;
661{
662 wininfo_T *wip;
663
664 while (buf->b_wininfo != NULL)
665 {
666 wip = buf->b_wininfo;
667 buf->b_wininfo = wip->wi_next;
668 if (wip->wi_optset)
669 {
670 clear_winopt(&wip->wi_opt);
671#ifdef FEAT_FOLDING
672 deleteFoldRecurse(&wip->wi_folds);
673#endif
674 }
675 vim_free(wip);
676 }
677}
678
679#if defined(FEAT_LISTCMDS) || defined(PROTO)
680/*
681 * Go to another buffer. Handles the result of the ATTENTION dialog.
682 */
683 void
684goto_buffer(eap, start, dir, count)
685 exarg_T *eap;
686 int start;
687 int dir;
688 int count;
689{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000690# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 buf_T *old_curbuf = curbuf;
692
693 swap_exists_action = SEA_DIALOG;
694# endif
695 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
696 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000697# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
699 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000700# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
701 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000702
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000703 /* Reset the error/interrupt/exception state here so that
704 * aborting() returns FALSE when closing a window. */
705 enter_cleanup(&cs);
706# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000707
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000708 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 win_close(curwin, TRUE);
710 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000711 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000712
713# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
714 /* Restore the error/interrupt/exception state if not discarded by a
715 * new aborting error, interrupt, or uncaught exception. */
716 leave_cleanup(&cs);
717# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719 else
720 handle_swap_exists(old_curbuf);
721# endif
722}
723#endif
724
Bram Moolenaare64ac772005-12-07 20:54:59 +0000725#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726/*
727 * Handle the situation of swap_exists_action being set.
728 * It is allowed for "old_curbuf" to be NULL or invalid.
729 */
730 void
731handle_swap_exists(old_curbuf)
732 buf_T *old_curbuf;
733{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000734# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
735 cleanup_T cs;
736# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 if (swap_exists_action == SEA_QUIT)
739 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000740# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
741 /* Reset the error/interrupt/exception state here so that
742 * aborting() returns FALSE when closing a buffer. */
743 enter_cleanup(&cs);
744# endif
745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 /* User selected Quit at ATTENTION prompt. Go back to previous
747 * buffer. If that buffer is gone or the same as the current one,
748 * open a new, empty buffer. */
749 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000750 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
752 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000753 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 if (old_curbuf != NULL)
755 enter_buffer(old_curbuf);
756 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000757
758# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
759 /* Restore the error/interrupt/exception state if not discarded by a
760 * new aborting error, interrupt, or uncaught exception. */
761 leave_cleanup(&cs);
762# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 }
764 else if (swap_exists_action == SEA_RECOVER)
765 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000766# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
767 /* Reset the error/interrupt/exception state here so that
768 * aborting() returns FALSE when closing a buffer. */
769 enter_cleanup(&cs);
770# endif
771
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 /* User selected Recover at ATTENTION prompt. */
773 msg_scroll = TRUE;
774 ml_recover();
775 MSG_PUTS("\n"); /* don't overwrite the last message */
776 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000777 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000778
779# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
780 /* Restore the error/interrupt/exception state if not discarded by a
781 * new aborting error, interrupt, or uncaught exception. */
782 leave_cleanup(&cs);
783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785 swap_exists_action = SEA_NONE;
786}
787#endif
788
789#if defined(FEAT_LISTCMDS) || defined(PROTO)
790/*
791 * do_bufdel() - delete or unload buffer(s)
792 *
793 * addr_count == 0: ":bdel" - delete current buffer
794 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
795 * buffer "end_bnr", then any other arguments.
796 * addr_count == 2: ":N,N bdel" - delete buffers in range
797 *
798 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
799 * DOBUF_DEL (":bdel")
800 *
801 * Returns error message or NULL
802 */
803 char_u *
804do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
805 int command;
806 char_u *arg; /* pointer to extra arguments */
807 int addr_count;
808 int start_bnr; /* first buffer number in a range */
809 int end_bnr; /* buffer nr or last buffer nr in a range */
810 int forceit;
811{
812 int do_current = 0; /* delete current buffer? */
813 int deleted = 0; /* number of buffers deleted */
814 char_u *errormsg = NULL; /* return value */
815 int bnr; /* buffer number */
816 char_u *p;
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 if (addr_count == 0)
819 {
820 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
821 }
822 else
823 {
824 if (addr_count == 2)
825 {
826 if (*arg) /* both range and argument is not allowed */
827 return (char_u *)_(e_trailing);
828 bnr = start_bnr;
829 }
830 else /* addr_count == 1 */
831 bnr = end_bnr;
832
833 for ( ;!got_int; ui_breakcheck())
834 {
835 /*
836 * delete the current buffer last, otherwise when the
837 * current buffer is deleted, the next buffer becomes
838 * the current one and will be loaded, which may then
839 * also be deleted, etc.
840 */
841 if (bnr == curbuf->b_fnum)
842 do_current = bnr;
843 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
844 forceit) == OK)
845 ++deleted;
846
847 /*
848 * find next buffer number to delete/unload
849 */
850 if (addr_count == 2)
851 {
852 if (++bnr > end_bnr)
853 break;
854 }
855 else /* addr_count == 1 */
856 {
857 arg = skipwhite(arg);
858 if (*arg == NUL)
859 break;
860 if (!VIM_ISDIGIT(*arg))
861 {
862 p = skiptowhite_esc(arg);
863 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
864 if (bnr < 0) /* failed */
865 break;
866 arg = p;
867 }
868 else
869 bnr = getdigits(&arg);
870 }
871 }
872 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
873 FORWARD, do_current, forceit) == OK)
874 ++deleted;
875
876 if (deleted == 0)
877 {
878 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000879 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000881 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000883 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 errormsg = IObuff;
885 }
886 else if (deleted >= p_report)
887 {
888 if (command == DOBUF_UNLOAD)
889 {
890 if (deleted == 1)
891 MSG(_("1 buffer unloaded"));
892 else
893 smsg((char_u *)_("%d buffers unloaded"), deleted);
894 }
895 else if (command == DOBUF_DEL)
896 {
897 if (deleted == 1)
898 MSG(_("1 buffer deleted"));
899 else
900 smsg((char_u *)_("%d buffers deleted"), deleted);
901 }
902 else
903 {
904 if (deleted == 1)
905 MSG(_("1 buffer wiped out"));
906 else
907 smsg((char_u *)_("%d buffers wiped out"), deleted);
908 }
909 }
910 }
911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
913 return errormsg;
914}
915
916/*
917 * Implementation of the commands for the buffer list.
918 *
919 * action == DOBUF_GOTO go to specified buffer
920 * action == DOBUF_SPLIT split window and go to specified buffer
921 * action == DOBUF_UNLOAD unload specified buffer(s)
922 * action == DOBUF_DEL delete specified buffer(s) from buffer list
923 * action == DOBUF_WIPE delete specified buffer(s) really
924 *
925 * start == DOBUF_CURRENT go to "count" buffer from current buffer
926 * start == DOBUF_FIRST go to "count" buffer from first buffer
927 * start == DOBUF_LAST go to "count" buffer from last buffer
928 * start == DOBUF_MOD go to "count" modified buffer from current buffer
929 *
930 * Return FAIL or OK.
931 */
932 int
933do_buffer(action, start, dir, count, forceit)
934 int action;
935 int start;
936 int dir; /* FORWARD or BACKWARD */
937 int count; /* buffer number or number of buffers */
938 int forceit; /* TRUE for :...! */
939{
940 buf_T *buf;
941 buf_T *bp;
942 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
943 || action == DOBUF_WIPE);
944
945 switch (start)
946 {
947 case DOBUF_FIRST: buf = firstbuf; break;
948 case DOBUF_LAST: buf = lastbuf; break;
949 default: buf = curbuf; break;
950 }
951 if (start == DOBUF_MOD) /* find next modified buffer */
952 {
953 while (count-- > 0)
954 {
955 do
956 {
957 buf = buf->b_next;
958 if (buf == NULL)
959 buf = firstbuf;
960 }
961 while (buf != curbuf && !bufIsChanged(buf));
962 }
963 if (!bufIsChanged(buf))
964 {
965 EMSG(_("E84: No modified buffer found"));
966 return FAIL;
967 }
968 }
969 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
970 {
971 while (buf != NULL && buf->b_fnum != count)
972 buf = buf->b_next;
973 }
974 else
975 {
976 bp = NULL;
977 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
978 {
979 /* remember the buffer where we start, we come back there when all
980 * buffers are unlisted. */
981 if (bp == NULL)
982 bp = buf;
983 if (dir == FORWARD)
984 {
985 buf = buf->b_next;
986 if (buf == NULL)
987 buf = firstbuf;
988 }
989 else
990 {
991 buf = buf->b_prev;
992 if (buf == NULL)
993 buf = lastbuf;
994 }
995 /* don't count unlisted buffers */
996 if (unload || buf->b_p_bl)
997 {
998 --count;
999 bp = NULL; /* use this buffer as new starting point */
1000 }
1001 if (bp == buf)
1002 {
1003 /* back where we started, didn't find anything. */
1004 EMSG(_("E85: There is no listed buffer"));
1005 return FAIL;
1006 }
1007 }
1008 }
1009
1010 if (buf == NULL) /* could not find it */
1011 {
1012 if (start == DOBUF_FIRST)
1013 {
1014 /* don't warn when deleting */
1015 if (!unload)
1016 EMSGN(_("E86: Buffer %ld does not exist"), count);
1017 }
1018 else if (dir == FORWARD)
1019 EMSG(_("E87: Cannot go beyond last buffer"));
1020 else
1021 EMSG(_("E88: Cannot go before first buffer"));
1022 return FAIL;
1023 }
1024
1025#ifdef FEAT_GUI
1026 need_mouse_correct = TRUE;
1027#endif
1028
1029#ifdef FEAT_LISTCMDS
1030 /*
1031 * delete buffer buf from memory and/or the list
1032 */
1033 if (unload)
1034 {
1035 int forward;
1036 int retval;
1037
1038 /* When unloading or deleting a buffer that's already unloaded and
1039 * unlisted: fail silently. */
1040 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1041 return FAIL;
1042
1043 if (!forceit && bufIsChanged(buf))
1044 {
1045#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1046 if ((p_confirm || cmdmod.confirm) && p_write)
1047 {
1048 dialog_changed(buf, FALSE);
1049# ifdef FEAT_AUTOCMD
1050 if (!buf_valid(buf))
1051 /* Autocommand deleted buffer, oops! It's not changed
1052 * now. */
1053 return FAIL;
1054# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001055 /* If it's still changed fail silently, the dialog already
1056 * mentioned why it fails. */
1057 if (bufIsChanged(buf))
1058 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001060 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061#endif
1062 {
1063 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1064 buf->b_fnum);
1065 return FAIL;
1066 }
1067 }
1068
1069 /*
1070 * If deleting the last (listed) buffer, make it empty.
1071 * The last (listed) buffer cannot be unloaded.
1072 */
1073 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1074 if (bp->b_p_bl && bp != buf)
1075 break;
1076 if (bp == NULL && buf == curbuf)
1077 {
1078 if (action == DOBUF_UNLOAD)
1079 {
1080 EMSG(_("E90: Cannot unload last buffer"));
1081 return FAIL;
1082 }
1083
1084 /* Close any other windows on this buffer, then make it empty. */
1085#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001086 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087#endif
1088 setpcmark();
1089 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001090 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091
1092 /*
1093 * do_ecmd() may create a new buffer, then we have to delete
1094 * the old one. But do_ecmd() may have done that already, check
1095 * if the buffer still exists.
1096 */
1097 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1098 close_buffer(NULL, buf, action);
1099 return retval;
1100 }
1101
1102#ifdef FEAT_WINDOWS
1103 /*
1104 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001105 * (unless it's the only window). Repeat this so long as we end up in
1106 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001108 while (buf == curbuf
1109 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 win_close(curwin, FALSE);
1111#endif
1112
1113 /*
1114 * If the buffer to be deleted is not the current one, delete it here.
1115 */
1116 if (buf != curbuf)
1117 {
1118#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001119 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120#endif
1121 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1122 close_buffer(NULL, buf, action);
1123 return OK;
1124 }
1125
1126 /*
1127 * Deleting the current buffer: Need to find another buffer to go to.
1128 * There must be another, otherwise it would have been handled above.
1129 * First use au_new_curbuf, if it is valid.
1130 * Then prefer the buffer we most recently visited.
1131 * Else try to find one that is loaded, after the current buffer,
1132 * then before the current buffer.
1133 * Finally use any buffer.
1134 */
1135 buf = NULL; /* selected buffer */
1136 bp = NULL; /* used when no loaded buffer found */
1137#ifdef FEAT_AUTOCMD
1138 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1139 buf = au_new_curbuf;
1140# ifdef FEAT_JUMPLIST
1141 else
1142# endif
1143#endif
1144#ifdef FEAT_JUMPLIST
1145 if (curwin->w_jumplistlen > 0)
1146 {
1147 int jumpidx;
1148
1149 jumpidx = curwin->w_jumplistidx - 1;
1150 if (jumpidx < 0)
1151 jumpidx = curwin->w_jumplistlen - 1;
1152
1153 forward = jumpidx;
1154 while (jumpidx != curwin->w_jumplistidx)
1155 {
1156 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1157 if (buf != NULL)
1158 {
1159 if (buf == curbuf || !buf->b_p_bl)
1160 buf = NULL; /* skip current and unlisted bufs */
1161 else if (buf->b_ml.ml_mfp == NULL)
1162 {
1163 /* skip unloaded buf, but may keep it for later */
1164 if (bp == NULL)
1165 bp = buf;
1166 buf = NULL;
1167 }
1168 }
1169 if (buf != NULL) /* found a valid buffer: stop searching */
1170 break;
1171 /* advance to older entry in jump list */
1172 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1173 break;
1174 if (--jumpidx < 0)
1175 jumpidx = curwin->w_jumplistlen - 1;
1176 if (jumpidx == forward) /* List exhausted for sure */
1177 break;
1178 }
1179 }
1180#endif
1181
1182 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1183 {
1184 forward = TRUE;
1185 buf = curbuf->b_next;
1186 for (;;)
1187 {
1188 if (buf == NULL)
1189 {
1190 if (!forward) /* tried both directions */
1191 break;
1192 buf = curbuf->b_prev;
1193 forward = FALSE;
1194 continue;
1195 }
1196 /* in non-help buffer, try to skip help buffers, and vv */
1197 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1198 {
1199 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1200 break;
1201 if (bp == NULL) /* remember unloaded buf for later */
1202 bp = buf;
1203 }
1204 if (forward)
1205 buf = buf->b_next;
1206 else
1207 buf = buf->b_prev;
1208 }
1209 }
1210 if (buf == NULL) /* No loaded buffer, use unloaded one */
1211 buf = bp;
1212 if (buf == NULL) /* No loaded buffer, find listed one */
1213 {
1214 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1215 if (buf->b_p_bl && buf != curbuf)
1216 break;
1217 }
1218 if (buf == NULL) /* Still no buffer, just take one */
1219 {
1220 if (curbuf->b_next != NULL)
1221 buf = curbuf->b_next;
1222 else
1223 buf = curbuf->b_prev;
1224 }
1225 }
1226
1227 /*
1228 * make buf current buffer
1229 */
1230 if (action == DOBUF_SPLIT) /* split window first */
1231 {
1232# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001233 /* If 'switchbuf' contains "useopen": jump to first window containing
1234 * "buf" if one exists */
1235 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001236 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001237 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001238 * page containing "buf" if one exists */
1239 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 return OK;
1241 if (win_split(0, 0) == FAIL)
1242# endif
1243 return FAIL;
1244 }
1245#endif
1246
1247 /* go to current buffer - nothing to do */
1248 if (buf == curbuf)
1249 return OK;
1250
1251 /*
1252 * Check if the current buffer may be abandoned.
1253 */
1254 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1255 {
1256#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1257 if ((p_confirm || cmdmod.confirm) && p_write)
1258 {
1259 dialog_changed(curbuf, FALSE);
1260# ifdef FEAT_AUTOCMD
1261 if (!buf_valid(buf))
1262 /* Autocommand deleted buffer, oops! */
1263 return FAIL;
1264# endif
1265 }
1266 if (bufIsChanged(curbuf))
1267#endif
1268 {
1269 EMSG(_(e_nowrtmsg));
1270 return FAIL;
1271 }
1272 }
1273
1274 /* Go to the other buffer. */
1275 set_curbuf(buf, action);
1276
1277#if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1278 if (action == DOBUF_SPLIT)
1279 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1280#endif
1281
1282#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1283 if (aborting()) /* autocmds may abort script processing */
1284 return FAIL;
1285#endif
1286
1287 return OK;
1288}
1289
1290#endif /* FEAT_LISTCMDS */
1291
1292/*
1293 * Set current buffer to "buf". Executes autocommands and closes current
1294 * buffer. "action" tells how to close the current buffer:
1295 * DOBUF_GOTO free or hide it
1296 * DOBUF_SPLIT nothing
1297 * DOBUF_UNLOAD unload it
1298 * DOBUF_DEL delete it
1299 * DOBUF_WIPE wipe it out
1300 */
1301 void
1302set_curbuf(buf, action)
1303 buf_T *buf;
1304 int action;
1305{
1306 buf_T *prevbuf;
1307 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1308 || action == DOBUF_WIPE);
1309
1310 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001311 if (!cmdmod.keepalt)
1312 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001313 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314
1315#ifdef FEAT_VISUAL
1316 /* Don't restart Select mode after switching to another buffer. */
1317 VIsual_reselect = FALSE;
1318#endif
1319
1320 /* close_windows() or apply_autocmds() may change curbuf */
1321 prevbuf = curbuf;
1322
1323#ifdef FEAT_AUTOCMD
1324 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1325# ifdef FEAT_EVAL
1326 if (buf_valid(prevbuf) && !aborting())
1327# else
1328 if (buf_valid(prevbuf))
1329# endif
1330#endif
1331 {
1332#ifdef FEAT_WINDOWS
1333 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001334 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335#endif
1336#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1337 if (buf_valid(prevbuf) && !aborting())
1338#else
1339 if (buf_valid(prevbuf))
1340#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001341 {
1342 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001343 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1345 unload ? action : (action == DOBUF_GOTO
1346 && !P_HID(prevbuf)
1347 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001351 /* An autocommand may have deleted "buf", already entered it (e.g., when
1352 * it did ":bunload") or aborted the script processing! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353# ifdef FEAT_EVAL
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001354 if (buf_valid(buf) && buf != curbuf && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355# else
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001356 if (buf_valid(buf) && buf != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357# endif
1358#endif
1359 enter_buffer(buf);
1360}
1361
1362/*
1363 * Enter a new current buffer.
1364 * Old curbuf must have been abandoned already!
1365 */
1366 void
1367enter_buffer(buf)
1368 buf_T *buf;
1369{
1370 /* Copy buffer and window local option values. Not for a help buffer. */
1371 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1372 if (!buf->b_help)
1373 get_winopts(buf);
1374#ifdef FEAT_FOLDING
1375 else
1376 /* Remove all folds in the window. */
1377 clearFolding(curwin);
1378 foldUpdateAll(curwin); /* update folds (later). */
1379#endif
1380
Bram Moolenaar860cae12010-06-05 23:22:07 +02001381#ifdef FEAT_SYN_HL
1382 if (curwin->w_s != &curwin->w_buffer->b_s)
1383 {
1384 /* Get rid of independant syntax */
1385 syntax_clear(curwin->w_s);
1386 vim_free(curwin->w_s);
1387 }
1388 curwin->w_s = &(buf->b_s);
1389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 /* Get the buffer in the current window. */
1391 curwin->w_buffer = buf;
1392 curbuf = buf;
1393 ++curbuf->b_nwindows;
1394
1395#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001396 if (curwin->w_p_diff)
1397 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#endif
1399
1400 /* Cursor on first line by default. */
1401 curwin->w_cursor.lnum = 1;
1402 curwin->w_cursor.col = 0;
1403#ifdef FEAT_VIRTUALEDIT
1404 curwin->w_cursor.coladd = 0;
1405#endif
1406 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001407#ifdef FEAT_AUTOCMD
1408 curwin->w_topline_was_set = FALSE;
1409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001411 /* mark cursor position as being invalid */
1412 curwin->w_valid = 0;
1413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 /* Make sure the buffer is loaded. */
1415 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001416 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001417#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001418 /* If there is no filetype, allow for detecting one. Esp. useful for
1419 * ":ball" used in a autocommand. If there already is a filetype we
1420 * might prefer to keep it. */
1421 if (*curbuf->b_p_ft == NUL)
1422 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001423#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001424
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 open_buffer(FALSE, NULL);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 else
1428 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001429 if (!msg_silent)
1430 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1432#ifdef FEAT_AUTOCMD
1433 curwin->w_topline = 1;
1434# ifdef FEAT_DIFF
1435 curwin->w_topfill = 0;
1436# endif
1437 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1438 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1439#endif
1440 }
1441
1442 /* If autocommands did not change the cursor position, restore cursor lnum
1443 * and possibly cursor col. */
1444 if (curwin->w_cursor.lnum == 1 && inindent(0))
1445 buflist_getfpos();
1446
1447 check_arg_idx(curwin); /* check for valid arg_idx */
1448#ifdef FEAT_TITLE
1449 maketitle();
1450#endif
1451#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001452 /* when autocmds didn't change it */
1453 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454#endif
1455 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1456
Bram Moolenaar009b2592004-10-24 19:18:58 +00001457#ifdef FEAT_NETBEANS_INTG
1458 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001459 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001460#endif
1461
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001462 /* Change directories when the 'acd' option is set. */
1463 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
1465#ifdef FEAT_KEYMAP
1466 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001467 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001469#ifdef FEAT_SPELL
1470 /* May need to set the spell language. Can only do this after the buffer
1471 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001472 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1473 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001474#endif
1475
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 redraw_later(NOT_VALID);
1477}
1478
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001479#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1480/*
1481 * Change to the directory of the current buffer.
1482 */
1483 void
1484do_autochdir()
1485{
1486 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1487 shorten_fnames(TRUE);
1488}
1489#endif
1490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491/*
1492 * functions for dealing with the buffer list
1493 */
1494
1495/*
1496 * Add a file name to the buffer list. Return a pointer to the buffer.
1497 * If the same file name already exists return a pointer to that buffer.
1498 * If it does not exist, or if fname == NULL, a new entry is created.
1499 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1500 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1501 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 * This is the ONLY way to create a new buffer.
1503 */
1504static int top_file_num = 1; /* highest file number */
1505
1506 buf_T *
1507buflist_new(ffname, sfname, lnum, flags)
1508 char_u *ffname; /* full path of fname or relative */
1509 char_u *sfname; /* short fname or NULL */
1510 linenr_T lnum; /* preferred cursor line */
1511 int flags; /* BLN_ defines */
1512{
1513 buf_T *buf;
1514#ifdef UNIX
1515 struct stat st;
1516#endif
1517
1518 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1519
1520 /*
1521 * If file name already exists in the list, update the entry.
1522 */
1523#ifdef UNIX
1524 /* On Unix we can use inode numbers when the file exists. Works better
1525 * for hard links. */
1526 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1527 st.st_dev = (dev_T)-1;
1528#endif
1529 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1530#ifdef UNIX
1531 buflist_findname_stat(ffname, &st)
1532#else
1533 buflist_findname(ffname)
1534#endif
1535 ) != NULL)
1536 {
1537 vim_free(ffname);
1538 if (lnum != 0)
1539 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1540 /* copy the options now, if 'cpo' doesn't have 's' and not done
1541 * already */
1542 buf_copy_options(buf, 0);
1543 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1544 {
1545 buf->b_p_bl = TRUE;
1546#ifdef FEAT_AUTOCMD
1547 if (!(flags & BLN_DUMMY))
1548 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1549#endif
1550 }
1551 return buf;
1552 }
1553
1554 /*
1555 * If the current buffer has no name and no contents, use the current
1556 * buffer. Otherwise: Need to allocate a new buffer structure.
1557 *
1558 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001559 * (A spell file buffer is allocated in spell.c, but that's not a normal
1560 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 */
1562 buf = NULL;
1563 if ((flags & BLN_CURBUF)
1564 && curbuf != NULL
1565 && curbuf->b_ffname == NULL
1566 && curbuf->b_nwindows <= 1
1567 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1568 {
1569 buf = curbuf;
1570#ifdef FEAT_AUTOCMD
1571 /* It's like this buffer is deleted. Watch out for autocommands that
1572 * change curbuf! If that happens, allocate a new buffer anyway. */
1573 if (curbuf->b_p_bl)
1574 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1575 if (buf == curbuf)
1576 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1577# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001578 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 return NULL;
1580# endif
1581#endif
1582#ifdef FEAT_QUICKFIX
1583# ifdef FEAT_AUTOCMD
1584 if (buf == curbuf)
1585# endif
1586 {
1587 /* Make sure 'bufhidden' and 'buftype' are empty */
1588 clear_string_option(&buf->b_p_bh);
1589 clear_string_option(&buf->b_p_bt);
1590 }
1591#endif
1592 }
1593 if (buf != curbuf || curbuf == NULL)
1594 {
1595 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1596 if (buf == NULL)
1597 {
1598 vim_free(ffname);
1599 return NULL;
1600 }
1601 }
1602
1603 if (ffname != NULL)
1604 {
1605 buf->b_ffname = ffname;
1606 buf->b_sfname = vim_strsave(sfname);
1607 }
1608
1609 clear_wininfo(buf);
1610 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1611
1612 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1613 || buf->b_wininfo == NULL)
1614 {
1615 vim_free(buf->b_ffname);
1616 buf->b_ffname = NULL;
1617 vim_free(buf->b_sfname);
1618 buf->b_sfname = NULL;
1619 if (buf != curbuf)
1620 free_buffer(buf);
1621 return NULL;
1622 }
1623
1624 if (buf == curbuf)
1625 {
1626 /* free all things allocated for this buffer */
1627 buf_freeall(buf, FALSE, FALSE);
1628 if (buf != curbuf) /* autocommands deleted the buffer! */
1629 return NULL;
1630#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001631 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 return NULL;
1633#endif
1634 /* buf->b_nwindows = 0; why was this here? */
1635 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1636#ifdef FEAT_KEYMAP
1637 /* need to reload lmaps and set b:keymap_name */
1638 curbuf->b_kmap_state |= KEYMAP_INIT;
1639#endif
1640 }
1641 else
1642 {
1643 /*
1644 * put new buffer at the end of the buffer list
1645 */
1646 buf->b_next = NULL;
1647 if (firstbuf == NULL) /* buffer list is empty */
1648 {
1649 buf->b_prev = NULL;
1650 firstbuf = buf;
1651 }
1652 else /* append new buffer at end of list */
1653 {
1654 lastbuf->b_next = buf;
1655 buf->b_prev = lastbuf;
1656 }
1657 lastbuf = buf;
1658
1659 buf->b_fnum = top_file_num++;
1660 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1661 {
1662 EMSG(_("W14: Warning: List of file names overflow"));
1663 if (emsg_silent == 0)
1664 {
1665 out_flush();
1666 ui_delay(3000L, TRUE); /* make sure it is noticed */
1667 }
1668 top_file_num = 1;
1669 }
1670
1671 /*
1672 * Always copy the options from the current buffer.
1673 */
1674 buf_copy_options(buf, BCO_ALWAYS);
1675 }
1676
1677 buf->b_wininfo->wi_fpos.lnum = lnum;
1678 buf->b_wininfo->wi_win = curwin;
1679
1680#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001681 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1682#endif
1683#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001684 hash_init(&buf->b_s.b_keywtab);
1685 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686#endif
1687
1688 buf->b_fname = buf->b_sfname;
1689#ifdef UNIX
1690 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001691 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 else
1693 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001694 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 buf->b_dev = st.st_dev;
1696 buf->b_ino = st.st_ino;
1697 }
1698#endif
1699 buf->b_u_synced = TRUE;
1700 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001701 if (flags & BLN_DUMMY)
1702 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 buf_clear_file(buf);
1704 clrallmarks(buf); /* clear marks */
1705 fmarks_check_names(buf); /* check file marks for this file */
1706 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1707#ifdef FEAT_AUTOCMD
1708 if (!(flags & BLN_DUMMY))
1709 {
1710 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1711 if (flags & BLN_LISTED)
1712 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1713# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001714 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 return NULL;
1716# endif
1717 }
1718#endif
1719
1720 return buf;
1721}
1722
1723/*
1724 * Free the memory for the options of a buffer.
1725 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1726 * 'fileencoding'.
1727 */
1728 void
1729free_buf_options(buf, free_p_ff)
1730 buf_T *buf;
1731 int free_p_ff;
1732{
1733 if (free_p_ff)
1734 {
1735#ifdef FEAT_MBYTE
1736 clear_string_option(&buf->b_p_fenc);
1737#endif
1738 clear_string_option(&buf->b_p_ff);
1739#ifdef FEAT_QUICKFIX
1740 clear_string_option(&buf->b_p_bh);
1741 clear_string_option(&buf->b_p_bt);
1742#endif
1743 }
1744#ifdef FEAT_FIND_ID
1745 clear_string_option(&buf->b_p_def);
1746 clear_string_option(&buf->b_p_inc);
1747# ifdef FEAT_EVAL
1748 clear_string_option(&buf->b_p_inex);
1749# endif
1750#endif
1751#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1752 clear_string_option(&buf->b_p_inde);
1753 clear_string_option(&buf->b_p_indk);
1754#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001755#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1756 clear_string_option(&buf->b_p_bexpr);
1757#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001758#if defined(FEAT_EVAL)
1759 clear_string_option(&buf->b_p_fex);
1760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761#ifdef FEAT_CRYPT
1762 clear_string_option(&buf->b_p_key);
1763#endif
1764 clear_string_option(&buf->b_p_kp);
1765 clear_string_option(&buf->b_p_mps);
1766 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001767 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 clear_string_option(&buf->b_p_isk);
1769#ifdef FEAT_KEYMAP
1770 clear_string_option(&buf->b_p_keymap);
1771 ga_clear(&buf->b_kmap_ga);
1772#endif
1773#ifdef FEAT_COMMENTS
1774 clear_string_option(&buf->b_p_com);
1775#endif
1776#ifdef FEAT_FOLDING
1777 clear_string_option(&buf->b_p_cms);
1778#endif
1779 clear_string_option(&buf->b_p_nf);
1780#ifdef FEAT_SYN_HL
1781 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001782#endif
1783#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001784 clear_string_option(&buf->b_s.b_p_spc);
1785 clear_string_option(&buf->b_s.b_p_spf);
1786 vim_free(buf->b_s.b_cap_prog);
1787 buf->b_s.b_cap_prog = NULL;
1788 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789#endif
1790#ifdef FEAT_SEARCHPATH
1791 clear_string_option(&buf->b_p_sua);
1792#endif
1793#ifdef FEAT_AUTOCMD
1794 clear_string_option(&buf->b_p_ft);
1795#endif
1796#ifdef FEAT_OSFILETYPE
1797 clear_string_option(&buf->b_p_oft);
1798#endif
1799#ifdef FEAT_CINDENT
1800 clear_string_option(&buf->b_p_cink);
1801 clear_string_option(&buf->b_p_cino);
1802#endif
1803#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1804 clear_string_option(&buf->b_p_cinw);
1805#endif
1806#ifdef FEAT_INS_EXPAND
1807 clear_string_option(&buf->b_p_cpt);
1808#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001809#ifdef FEAT_COMPL_FUNC
1810 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001811 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813#ifdef FEAT_QUICKFIX
1814 clear_string_option(&buf->b_p_gp);
1815 clear_string_option(&buf->b_p_mp);
1816 clear_string_option(&buf->b_p_efm);
1817#endif
1818 clear_string_option(&buf->b_p_ep);
1819 clear_string_option(&buf->b_p_path);
1820 clear_string_option(&buf->b_p_tags);
1821#ifdef FEAT_INS_EXPAND
1822 clear_string_option(&buf->b_p_dict);
1823 clear_string_option(&buf->b_p_tsr);
1824#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001825#ifdef FEAT_TEXTOBJ
1826 clear_string_option(&buf->b_p_qe);
1827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 buf->b_p_ar = -1;
1829}
1830
1831/*
1832 * get alternate file n
1833 * set linenr to lnum or altfpos.lnum if lnum == 0
1834 * also set cursor column to altfpos.col if 'startofline' is not set.
1835 * if (options & GETF_SETMARK) call setpcmark()
1836 * if (options & GETF_ALT) we are jumping to an alternate file.
1837 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1838 *
1839 * return FAIL for failure, OK for success
1840 */
1841 int
1842buflist_getfile(n, lnum, options, forceit)
1843 int n;
1844 linenr_T lnum;
1845 int options;
1846 int forceit;
1847{
1848 buf_T *buf;
1849#ifdef FEAT_WINDOWS
1850 win_T *wp = NULL;
1851#endif
1852 pos_T *fpos;
1853 colnr_T col;
1854
1855 buf = buflist_findnr(n);
1856 if (buf == NULL)
1857 {
1858 if ((options & GETF_ALT) && n == 0)
1859 EMSG(_(e_noalt));
1860 else
1861 EMSGN(_("E92: Buffer %ld not found"), n);
1862 return FAIL;
1863 }
1864
1865 /* if alternate file is the current buffer, nothing to do */
1866 if (buf == curbuf)
1867 return OK;
1868
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001869 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001870 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001871 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001873 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001874#ifdef FEAT_AUTOCMD
1875 if (curbuf_locked())
1876 return FAIL;
1877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
1879 /* altfpos may be changed by getfile(), get it now */
1880 if (lnum == 0)
1881 {
1882 fpos = buflist_findfpos(buf);
1883 lnum = fpos->lnum;
1884 col = fpos->col;
1885 }
1886 else
1887 col = 0;
1888
1889#ifdef FEAT_WINDOWS
1890 if (options & GETF_SWITCH)
1891 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001892 /* If 'switchbuf' contains "useopen": jump to first window containing
1893 * "buf" if one exists */
1894 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001896 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1897 * page containing "buf" if one exists */
1898 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001899 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001900 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1901 * isn't empty: open new window */
1902 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001904 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1905 tabpage_new();
1906 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 return FAIL;
1908# ifdef FEAT_SCROLLBIND
1909 curwin->w_p_scb = FALSE;
1910# endif
1911 }
1912 }
1913#endif
1914
1915 ++RedrawingDisabled;
1916 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1917 lnum, forceit) <= 0)
1918 {
1919 --RedrawingDisabled;
1920
1921 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1922 if (!p_sol && col != 0)
1923 {
1924 curwin->w_cursor.col = col;
1925 check_cursor_col();
1926#ifdef FEAT_VIRTUALEDIT
1927 curwin->w_cursor.coladd = 0;
1928#endif
1929 curwin->w_set_curswant = TRUE;
1930 }
1931 return OK;
1932 }
1933 --RedrawingDisabled;
1934 return FAIL;
1935}
1936
1937/*
1938 * go to the last know line number for the current buffer
1939 */
1940 void
1941buflist_getfpos()
1942{
1943 pos_T *fpos;
1944
1945 fpos = buflist_findfpos(curbuf);
1946
1947 curwin->w_cursor.lnum = fpos->lnum;
1948 check_cursor_lnum();
1949
1950 if (p_sol)
1951 curwin->w_cursor.col = 0;
1952 else
1953 {
1954 curwin->w_cursor.col = fpos->col;
1955 check_cursor_col();
1956#ifdef FEAT_VIRTUALEDIT
1957 curwin->w_cursor.coladd = 0;
1958#endif
1959 curwin->w_set_curswant = TRUE;
1960 }
1961}
1962
Bram Moolenaar81695252004-12-29 20:58:21 +00001963#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1964/*
1965 * Find file in buffer list by name (it has to be for the current window).
1966 * Returns NULL if not found.
1967 */
1968 buf_T *
1969buflist_findname_exp(fname)
1970 char_u *fname;
1971{
1972 char_u *ffname;
1973 buf_T *buf = NULL;
1974
1975 /* First make the name into a full path name */
1976 ffname = FullName_save(fname,
1977#ifdef UNIX
1978 TRUE /* force expansion, get rid of symbolic links */
1979#else
1980 FALSE
1981#endif
1982 );
1983 if (ffname != NULL)
1984 {
1985 buf = buflist_findname(ffname);
1986 vim_free(ffname);
1987 }
1988 return buf;
1989}
1990#endif
1991
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992/*
1993 * Find file in buffer list by name (it has to be for the current window).
1994 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00001995 * Skips dummy buffers.
1996 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 */
1998 buf_T *
1999buflist_findname(ffname)
2000 char_u *ffname;
2001{
2002#ifdef UNIX
2003 struct stat st;
2004
2005 if (mch_stat((char *)ffname, &st) < 0)
2006 st.st_dev = (dev_T)-1;
2007 return buflist_findname_stat(ffname, &st);
2008}
2009
2010/*
2011 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2012 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002013 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 */
2015 static buf_T *
2016buflist_findname_stat(ffname, stp)
2017 char_u *ffname;
2018 struct stat *stp;
2019{
2020#endif
2021 buf_T *buf;
2022
2023 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002024 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025#ifdef UNIX
2026 , stp
2027#endif
2028 ))
2029 return buf;
2030 return NULL;
2031}
2032
2033#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2034/*
2035 * Find file in buffer list by a regexp pattern.
2036 * Return fnum of the found buffer.
2037 * Return < 0 for error.
2038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 int
2040buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2041 char_u *pattern;
2042 char_u *pattern_end; /* pointer to first char after pattern */
2043 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002044 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
2046 buf_T *buf;
2047 regprog_T *prog;
2048 int match = -1;
2049 int find_listed;
2050 char_u *pat;
2051 char_u *patend;
2052 int attempt;
2053 char_u *p;
2054 int toggledollar;
2055
2056 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2057 {
2058 if (*pattern == '%')
2059 match = curbuf->b_fnum;
2060 else
2061 match = curwin->w_alt_fnum;
2062#ifdef FEAT_DIFF
2063 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2064 match = -1;
2065#endif
2066 }
2067
2068 /*
2069 * Try four ways of matching a listed buffer:
2070 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002071 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 * attempt == 2: with '$' at end (only match at end)
2073 * attempt == 3: with '^' at start and '$' at end (only full match)
2074 * Repeat this for finding an unlisted buffer if there was no matching
2075 * listed buffer.
2076 */
2077 else
2078 {
2079 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2080 if (pat == NULL)
2081 return -1;
2082 patend = pat + STRLEN(pat) - 1;
2083 toggledollar = (patend > pat && *patend == '$');
2084
2085 /* First try finding a listed buffer. If not found and "unlisted"
2086 * is TRUE, try finding an unlisted buffer. */
2087 find_listed = TRUE;
2088 for (;;)
2089 {
2090 for (attempt = 0; attempt <= 3; ++attempt)
2091 {
2092 /* may add '^' and '$' */
2093 if (toggledollar)
2094 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2095 p = pat;
2096 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2097 ++p;
2098 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2099 if (prog == NULL)
2100 {
2101 vim_free(pat);
2102 return -1;
2103 }
2104
2105 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2106 if (buf->b_p_bl == find_listed
2107#ifdef FEAT_DIFF
2108 && (!diffmode || diff_mode_buf(buf))
2109#endif
2110 && buflist_match(prog, buf) != NULL)
2111 {
2112 if (match >= 0) /* already found a match */
2113 {
2114 match = -2;
2115 break;
2116 }
2117 match = buf->b_fnum; /* remember first match */
2118 }
2119
2120 vim_free(prog);
2121 if (match >= 0) /* found one match */
2122 break;
2123 }
2124
2125 /* Only search for unlisted buffers if there was no match with
2126 * a listed buffer. */
2127 if (!unlisted || !find_listed || match != -1)
2128 break;
2129 find_listed = FALSE;
2130 }
2131
2132 vim_free(pat);
2133 }
2134
2135 if (match == -2)
2136 EMSG2(_("E93: More than one match for %s"), pattern);
2137 else if (match < 0)
2138 EMSG2(_("E94: No matching buffer for %s"), pattern);
2139 return match;
2140}
2141#endif
2142
2143#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2144
2145/*
2146 * Find all buffer names that match.
2147 * For command line expansion of ":buf" and ":sbuf".
2148 * Return OK if matches found, FAIL otherwise.
2149 */
2150 int
2151ExpandBufnames(pat, num_file, file, options)
2152 char_u *pat;
2153 int *num_file;
2154 char_u ***file;
2155 int options;
2156{
2157 int count = 0;
2158 buf_T *buf;
2159 int round;
2160 char_u *p;
2161 int attempt;
2162 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002163 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165 *num_file = 0; /* return values in case of FAIL */
2166 *file = NULL;
2167
Bram Moolenaar05159a02005-02-26 23:04:13 +00002168 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2169 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002171 patc = alloc((unsigned)STRLEN(pat) + 11);
2172 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002174 STRCPY(patc, "\\(^\\|[\\/]\\)");
2175 STRCPY(patc + 11, pat + 1);
2176 }
2177 else
2178 patc = pat;
2179
2180 /*
2181 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002182 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002183 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002184 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002185 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002186 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002187 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002188 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002189 if (prog == NULL)
2190 {
2191 if (patc != pat)
2192 vim_free(patc);
2193 return FAIL;
2194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
2196 /*
2197 * round == 1: Count the matches.
2198 * round == 2: Build the array to keep the matches.
2199 */
2200 for (round = 1; round <= 2; ++round)
2201 {
2202 count = 0;
2203 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2204 {
2205 if (!buf->b_p_bl) /* skip unlisted buffers */
2206 continue;
2207 p = buflist_match(prog, buf);
2208 if (p != NULL)
2209 {
2210 if (round == 1)
2211 ++count;
2212 else
2213 {
2214 if (options & WILD_HOME_REPLACE)
2215 p = home_replace_save(buf, p);
2216 else
2217 p = vim_strsave(p);
2218 (*file)[count++] = p;
2219 }
2220 }
2221 }
2222 if (count == 0) /* no match found, break here */
2223 break;
2224 if (round == 1)
2225 {
2226 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2227 if (*file == NULL)
2228 {
2229 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002230 if (patc != pat)
2231 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 return FAIL;
2233 }
2234 }
2235 }
2236 vim_free(prog);
2237 if (count) /* match(es) found, break here */
2238 break;
2239 }
2240
Bram Moolenaar05159a02005-02-26 23:04:13 +00002241 if (patc != pat)
2242 vim_free(patc);
2243
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 *num_file = count;
2245 return (count == 0 ? FAIL : OK);
2246}
2247
2248#endif /* FEAT_CMDL_COMPL */
2249
2250#ifdef HAVE_BUFLIST_MATCH
2251/*
2252 * Check for a match on the file name for buffer "buf" with regprog "prog".
2253 */
2254 static char_u *
2255buflist_match(prog, buf)
2256 regprog_T *prog;
2257 buf_T *buf;
2258{
2259 char_u *match;
2260
2261 /* First try the short file name, then the long file name. */
2262 match = fname_match(prog, buf->b_sfname);
2263 if (match == NULL)
2264 match = fname_match(prog, buf->b_ffname);
2265
2266 return match;
2267}
2268
2269/*
2270 * Try matching the regexp in "prog" with file name "name".
2271 * Return "name" when there is a match, NULL when not.
2272 */
2273 static char_u *
2274fname_match(prog, name)
2275 regprog_T *prog;
2276 char_u *name;
2277{
2278 char_u *match = NULL;
2279 char_u *p;
2280 regmatch_T regmatch;
2281
2282 if (name != NULL)
2283 {
2284 regmatch.regprog = prog;
2285#ifdef CASE_INSENSITIVE_FILENAME
2286 regmatch.rm_ic = TRUE; /* Always ignore case */
2287#else
2288 regmatch.rm_ic = FALSE; /* Never ignore case */
2289#endif
2290
2291 if (vim_regexec(&regmatch, name, (colnr_T)0))
2292 match = name;
2293 else
2294 {
2295 /* Replace $(HOME) with '~' and try matching again. */
2296 p = home_replace_save(NULL, name);
2297 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2298 match = name;
2299 vim_free(p);
2300 }
2301 }
2302
2303 return match;
2304}
2305#endif
2306
2307/*
2308 * find file in buffer list by number
2309 */
2310 buf_T *
2311buflist_findnr(nr)
2312 int nr;
2313{
2314 buf_T *buf;
2315
2316 if (nr == 0)
2317 nr = curwin->w_alt_fnum;
2318 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2319 if (buf->b_fnum == nr)
2320 return (buf);
2321 return NULL;
2322}
2323
2324/*
2325 * Get name of file 'n' in the buffer list.
2326 * When the file has no name an empty string is returned.
2327 * home_replace() is used to shorten the file name (used for marks).
2328 * Returns a pointer to allocated memory, of NULL when failed.
2329 */
2330 char_u *
2331buflist_nr2name(n, fullname, helptail)
2332 int n;
2333 int fullname;
2334 int helptail; /* for help buffers return tail only */
2335{
2336 buf_T *buf;
2337
2338 buf = buflist_findnr(n);
2339 if (buf == NULL)
2340 return NULL;
2341 return home_replace_save(helptail ? buf : NULL,
2342 fullname ? buf->b_ffname : buf->b_fname);
2343}
2344
2345/*
2346 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2347 * When "copy_options" is TRUE save the local window option values.
2348 * When "lnum" is 0 only do the options.
2349 */
2350 static void
2351buflist_setfpos(buf, win, lnum, col, copy_options)
2352 buf_T *buf;
2353 win_T *win;
2354 linenr_T lnum;
2355 colnr_T col;
2356 int copy_options;
2357{
2358 wininfo_T *wip;
2359
2360 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2361 if (wip->wi_win == win)
2362 break;
2363 if (wip == NULL)
2364 {
2365 /* allocate a new entry */
2366 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2367 if (wip == NULL)
2368 return;
2369 wip->wi_win = win;
2370 if (lnum == 0) /* set lnum even when it's 0 */
2371 lnum = 1;
2372 }
2373 else
2374 {
2375 /* remove the entry from the list */
2376 if (wip->wi_prev)
2377 wip->wi_prev->wi_next = wip->wi_next;
2378 else
2379 buf->b_wininfo = wip->wi_next;
2380 if (wip->wi_next)
2381 wip->wi_next->wi_prev = wip->wi_prev;
2382 if (copy_options && wip->wi_optset)
2383 {
2384 clear_winopt(&wip->wi_opt);
2385#ifdef FEAT_FOLDING
2386 deleteFoldRecurse(&wip->wi_folds);
2387#endif
2388 }
2389 }
2390 if (lnum != 0)
2391 {
2392 wip->wi_fpos.lnum = lnum;
2393 wip->wi_fpos.col = col;
2394 }
2395 if (copy_options)
2396 {
2397 /* Save the window-specific option values. */
2398 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2399#ifdef FEAT_FOLDING
2400 wip->wi_fold_manual = win->w_fold_manual;
2401 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2402#endif
2403 wip->wi_optset = TRUE;
2404 }
2405
2406 /* insert the entry in front of the list */
2407 wip->wi_next = buf->b_wininfo;
2408 buf->b_wininfo = wip;
2409 wip->wi_prev = NULL;
2410 if (wip->wi_next)
2411 wip->wi_next->wi_prev = wip;
2412
2413 return;
2414}
2415
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002416#ifdef FEAT_DIFF
2417static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2418
2419/*
2420 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2421 * page. That's because a diff is local to a tab page.
2422 */
2423 static int
2424wininfo_other_tab_diff(wip)
2425 wininfo_T *wip;
2426{
2427 win_T *wp;
2428
2429 if (wip->wi_opt.wo_diff)
2430 {
2431 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2432 /* return FALSE when it's a window in the current tab page, thus
2433 * the buffer was in diff mode here */
2434 if (wip->wi_win == wp)
2435 return FALSE;
2436 return TRUE;
2437 }
2438 return FALSE;
2439}
2440#endif
2441
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442/*
2443 * Find info for the current window in buffer "buf".
2444 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002445 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2446 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 * Returns NULL when there isn't any info.
2448 */
2449 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002450find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002452 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453{
2454 wininfo_T *wip;
2455
2456 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002457 if (wip->wi_win == curwin
2458#ifdef FEAT_DIFF
2459 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2460#endif
2461 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002463
2464 /* If no wininfo for curwin, use the first in the list (that doesn't have
2465 * 'diff' set and is in another tab page). */
2466 if (wip == NULL)
2467 {
2468#ifdef FEAT_DIFF
2469 if (skip_diff_buffer)
2470 {
2471 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2472 if (!wininfo_other_tab_diff(wip))
2473 break;
2474 }
2475 else
2476#endif
2477 wip = buf->b_wininfo;
2478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 return wip;
2480}
2481
2482/*
2483 * Reset the local window options to the values last used in this window.
2484 * If the buffer wasn't used in this window before, use the values from
2485 * the most recently used window. If the values were never set, use the
2486 * global values for the window.
2487 */
2488 void
2489get_winopts(buf)
2490 buf_T *buf;
2491{
2492 wininfo_T *wip;
2493
2494 clear_winopt(&curwin->w_onebuf_opt);
2495#ifdef FEAT_FOLDING
2496 clearFolding(curwin);
2497#endif
2498
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002499 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 if (wip != NULL && wip->wi_optset)
2501 {
2502 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2503#ifdef FEAT_FOLDING
2504 curwin->w_fold_manual = wip->wi_fold_manual;
2505 curwin->w_foldinvalid = TRUE;
2506 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2507#endif
2508 }
2509 else
2510 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2511
2512#ifdef FEAT_FOLDING
2513 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2514 if (p_fdls >= 0)
2515 curwin->w_p_fdl = p_fdls;
2516#endif
2517}
2518
2519/*
2520 * Find the position (lnum and col) for the buffer 'buf' for the current
2521 * window.
2522 * Returns a pointer to no_position if no position is found.
2523 */
2524 pos_T *
2525buflist_findfpos(buf)
2526 buf_T *buf;
2527{
2528 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002529 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002531 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (wip != NULL)
2533 return &(wip->wi_fpos);
2534 else
2535 return &no_position;
2536}
2537
2538/*
2539 * Find the lnum for the buffer 'buf' for the current window.
2540 */
2541 linenr_T
2542buflist_findlnum(buf)
2543 buf_T *buf;
2544{
2545 return buflist_findfpos(buf)->lnum;
2546}
2547
2548#if defined(FEAT_LISTCMDS) || defined(PROTO)
2549/*
2550 * List all know file names (for :files and :buffers command).
2551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 void
2553buflist_list(eap)
2554 exarg_T *eap;
2555{
2556 buf_T *buf;
2557 int len;
2558 int i;
2559
2560 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2561 {
2562 /* skip unlisted buffers, unless ! was used */
2563 if (!buf->b_p_bl && !eap->forceit)
2564 continue;
2565 msg_putchar('\n');
2566 if (buf_spname(buf) != NULL)
2567 STRCPY(NameBuff, buf_spname(buf));
2568 else
2569 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2570
Bram Moolenaar50cde822005-06-05 21:54:54 +00002571 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 buf->b_fnum,
2573 buf->b_p_bl ? ' ' : 'u',
2574 buf == curbuf ? '%' :
2575 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2576 buf->b_ml.ml_mfp == NULL ? ' ' :
2577 (buf->b_nwindows == 0 ? 'h' : 'a'),
2578 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2579 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002580 : (bufIsChanged(buf) ? '+' : ' '),
2581 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
2583 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 i = 40 - vim_strsize(IObuff);
2585 do
2586 {
2587 IObuff[len++] = ' ';
2588 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002589 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2590 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002591 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 msg_outtrans(IObuff);
2593 out_flush(); /* output one line at a time */
2594 ui_breakcheck();
2595 }
2596}
2597#endif
2598
2599/*
2600 * Get file name and line number for file 'fnum'.
2601 * Used by DoOneCmd() for translating '%' and '#'.
2602 * Used by insert_reg() and cmdline_paste() for '#' register.
2603 * Return FAIL if not found, OK for success.
2604 */
2605 int
2606buflist_name_nr(fnum, fname, lnum)
2607 int fnum;
2608 char_u **fname;
2609 linenr_T *lnum;
2610{
2611 buf_T *buf;
2612
2613 buf = buflist_findnr(fnum);
2614 if (buf == NULL || buf->b_fname == NULL)
2615 return FAIL;
2616
2617 *fname = buf->b_fname;
2618 *lnum = buflist_findlnum(buf);
2619
2620 return OK;
2621}
2622
2623/*
2624 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2625 * The file name with the full path is also remembered, for when :cd is used.
2626 * Returns FAIL for failure (file name already in use by other buffer)
2627 * OK otherwise.
2628 */
2629 int
2630setfname(buf, ffname, sfname, message)
2631 buf_T *buf;
2632 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002633 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634{
Bram Moolenaar81695252004-12-29 20:58:21 +00002635 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636#ifdef UNIX
2637 struct stat st;
2638#endif
2639
2640 if (ffname == NULL || *ffname == NUL)
2641 {
2642 /* Removing the name. */
2643 vim_free(buf->b_ffname);
2644 vim_free(buf->b_sfname);
2645 buf->b_ffname = NULL;
2646 buf->b_sfname = NULL;
2647#ifdef UNIX
2648 st.st_dev = (dev_T)-1;
2649#endif
2650 }
2651 else
2652 {
2653 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2654 if (ffname == NULL) /* out of memory */
2655 return FAIL;
2656
2657 /*
2658 * if the file name is already used in another buffer:
2659 * - if the buffer is loaded, fail
2660 * - if the buffer is not loaded, delete it from the list
2661 */
2662#ifdef UNIX
2663 if (mch_stat((char *)ffname, &st) < 0)
2664 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002665#endif
2666 if (!(buf->b_flags & BF_DUMMY))
2667#ifdef UNIX
2668 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002670 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671#endif
2672 if (obuf != NULL && obuf != buf)
2673 {
2674 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2675 {
2676 if (message)
2677 EMSG(_("E95: Buffer with this name already exists"));
2678 vim_free(ffname);
2679 return FAIL;
2680 }
2681 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2682 }
2683 sfname = vim_strsave(sfname);
2684 if (ffname == NULL || sfname == NULL)
2685 {
2686 vim_free(sfname);
2687 vim_free(ffname);
2688 return FAIL;
2689 }
2690#ifdef USE_FNAME_CASE
2691# ifdef USE_LONG_FNAME
2692 if (USE_LONG_FNAME)
2693# endif
2694 fname_case(sfname, 0); /* set correct case for short file name */
2695#endif
2696 vim_free(buf->b_ffname);
2697 vim_free(buf->b_sfname);
2698 buf->b_ffname = ffname;
2699 buf->b_sfname = sfname;
2700 }
2701 buf->b_fname = buf->b_sfname;
2702#ifdef UNIX
2703 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002704 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 else
2706 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002707 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 buf->b_dev = st.st_dev;
2709 buf->b_ino = st.st_ino;
2710 }
2711#endif
2712
2713#ifndef SHORT_FNAME
2714 buf->b_shortname = FALSE;
2715#endif
2716
2717 buf_name_changed(buf);
2718 return OK;
2719}
2720
2721/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002722 * Crude way of changing the name of a buffer. Use with care!
2723 * The name should be relative to the current directory.
2724 */
2725 void
2726buf_set_name(fnum, name)
2727 int fnum;
2728 char_u *name;
2729{
2730 buf_T *buf;
2731
2732 buf = buflist_findnr(fnum);
2733 if (buf != NULL)
2734 {
2735 vim_free(buf->b_sfname);
2736 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002737 buf->b_ffname = vim_strsave(name);
2738 buf->b_sfname = NULL;
2739 /* Allocate ffname and expand into full path. Also resolves .lnk
2740 * files on Win32. */
2741 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002742 buf->b_fname = buf->b_sfname;
2743 }
2744}
2745
2746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 * Take care of what needs to be done when the name of buffer "buf" has
2748 * changed.
2749 */
2750 void
2751buf_name_changed(buf)
2752 buf_T *buf;
2753{
2754 /*
2755 * If the file name changed, also change the name of the swapfile
2756 */
2757 if (buf->b_ml.ml_mfp != NULL)
2758 ml_setname(buf);
2759
2760 if (curwin->w_buffer == buf)
2761 check_arg_idx(curwin); /* check file name for arg list */
2762#ifdef FEAT_TITLE
2763 maketitle(); /* set window title */
2764#endif
2765#ifdef FEAT_WINDOWS
2766 status_redraw_all(); /* status lines need to be redrawn */
2767#endif
2768 fmarks_check_names(buf); /* check named file marks */
2769 ml_timestamp(buf); /* reset timestamp */
2770}
2771
2772/*
2773 * set alternate file name for current window
2774 *
2775 * Used by do_one_cmd(), do_write() and do_ecmd().
2776 * Return the buffer.
2777 */
2778 buf_T *
2779setaltfname(ffname, sfname, lnum)
2780 char_u *ffname;
2781 char_u *sfname;
2782 linenr_T lnum;
2783{
2784 buf_T *buf;
2785
2786 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2787 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002788 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 curwin->w_alt_fnum = buf->b_fnum;
2790 return buf;
2791}
2792
2793/*
2794 * Get alternate file name for current window.
2795 * Return NULL if there isn't any, and give error message if requested.
2796 */
2797 char_u *
2798getaltfname(errmsg)
2799 int errmsg; /* give error message */
2800{
2801 char_u *fname;
2802 linenr_T dummy;
2803
2804 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2805 {
2806 if (errmsg)
2807 EMSG(_(e_noalt));
2808 return NULL;
2809 }
2810 return fname;
2811}
2812
2813/*
2814 * Add a file name to the buflist and return its number.
2815 * Uses same flags as buflist_new(), except BLN_DUMMY.
2816 *
2817 * used by qf_init(), main() and doarglist()
2818 */
2819 int
2820buflist_add(fname, flags)
2821 char_u *fname;
2822 int flags;
2823{
2824 buf_T *buf;
2825
2826 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2827 if (buf != NULL)
2828 return buf->b_fnum;
2829 return 0;
2830}
2831
2832#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2833/*
2834 * Adjust slashes in file names. Called after 'shellslash' was set.
2835 */
2836 void
2837buflist_slash_adjust()
2838{
2839 buf_T *bp;
2840
2841 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2842 {
2843 if (bp->b_ffname != NULL)
2844 slash_adjust(bp->b_ffname);
2845 if (bp->b_sfname != NULL)
2846 slash_adjust(bp->b_sfname);
2847 }
2848}
2849#endif
2850
2851/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002852 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 * Also save the local window option values.
2854 */
2855 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002856buflist_altfpos(win)
2857 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002859 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860}
2861
2862/*
2863 * Return TRUE if 'ffname' is not the same file as current file.
2864 * Fname must have a full path (expanded by mch_FullName()).
2865 */
2866 int
2867otherfile(ffname)
2868 char_u *ffname;
2869{
2870 return otherfile_buf(curbuf, ffname
2871#ifdef UNIX
2872 , NULL
2873#endif
2874 );
2875}
2876
2877 static int
2878otherfile_buf(buf, ffname
2879#ifdef UNIX
2880 , stp
2881#endif
2882 )
2883 buf_T *buf;
2884 char_u *ffname;
2885#ifdef UNIX
2886 struct stat *stp;
2887#endif
2888{
2889 /* no name is different */
2890 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2891 return TRUE;
2892 if (fnamecmp(ffname, buf->b_ffname) == 0)
2893 return FALSE;
2894#ifdef UNIX
2895 {
2896 struct stat st;
2897
2898 /* If no struct stat given, get it now */
2899 if (stp == NULL)
2900 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002901 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 st.st_dev = (dev_T)-1;
2903 stp = &st;
2904 }
2905 /* Use dev/ino to check if the files are the same, even when the names
2906 * are different (possible with links). Still need to compare the
2907 * name above, for when the file doesn't exist yet.
2908 * Problem: The dev/ino changes when a file is deleted (and created
2909 * again) and remains the same when renamed/moved. We don't want to
2910 * mch_stat() each buffer each time, that would be too slow. Get the
2911 * dev/ino again when they appear to match, but not when they appear
2912 * to be different: Could skip a buffer when it's actually the same
2913 * file. */
2914 if (buf_same_ino(buf, stp))
2915 {
2916 buf_setino(buf);
2917 if (buf_same_ino(buf, stp))
2918 return FALSE;
2919 }
2920 }
2921#endif
2922 return TRUE;
2923}
2924
2925#if defined(UNIX) || defined(PROTO)
2926/*
2927 * Set inode and device number for a buffer.
2928 * Must always be called when b_fname is changed!.
2929 */
2930 void
2931buf_setino(buf)
2932 buf_T *buf;
2933{
2934 struct stat st;
2935
2936 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2937 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002938 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 buf->b_dev = st.st_dev;
2940 buf->b_ino = st.st_ino;
2941 }
2942 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002943 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944}
2945
2946/*
2947 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2948 */
2949 static int
2950buf_same_ino(buf, stp)
2951 buf_T *buf;
2952 struct stat *stp;
2953{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002954 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 && stp->st_dev == buf->b_dev
2956 && stp->st_ino == buf->b_ino);
2957}
2958#endif
2959
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002960/*
2961 * Print info about the current buffer.
2962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 void
2964fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002965 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 int shorthelp;
2967 int dont_truncate;
2968{
2969 char_u *name;
2970 int n;
2971 char_u *p;
2972 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002973 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974
2975 buffer = alloc(IOSIZE);
2976 if (buffer == NULL)
2977 return;
2978
2979 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2980 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002981 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 p = buffer + STRLEN(buffer);
2983 }
2984 else
2985 p = buffer;
2986
2987 *p++ = '"';
2988 if (buf_spname(curbuf) != NULL)
2989 STRCPY(p, buf_spname(curbuf));
2990 else
2991 {
2992 if (!fullname && curbuf->b_fname != NULL)
2993 name = curbuf->b_fname;
2994 else
2995 name = curbuf->b_ffname;
2996 home_replace(shorthelp ? curbuf : NULL, name, p,
2997 (int)(IOSIZE - (p - buffer)), TRUE);
2998 }
2999
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003000 len = STRLEN(buffer);
3001 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 "\"%s%s%s%s%s%s",
3003 curbufIsChanged() ? (shortmess(SHM_MOD)
3004 ? " [+]" : _(" [Modified]")) : " ",
3005 (curbuf->b_flags & BF_NOTEDITED)
3006#ifdef FEAT_QUICKFIX
3007 && !bt_dontwrite(curbuf)
3008#endif
3009 ? _("[Not edited]") : "",
3010 (curbuf->b_flags & BF_NEW)
3011#ifdef FEAT_QUICKFIX
3012 && !bt_dontwrite(curbuf)
3013#endif
3014 ? _("[New file]") : "",
3015 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3016 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3017 : _("[readonly]")) : "",
3018 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3019 || curbuf->b_p_ro) ?
3020 " " : "");
3021 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3022 * causes an overflow, thus for large numbers divide instead. */
3023 if (curwin->w_cursor.lnum > 1000000L)
3024 n = (int)(((long)curwin->w_cursor.lnum) /
3025 ((long)curbuf->b_ml.ml_line_count / 100L));
3026 else
3027 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3028 (long)curbuf->b_ml.ml_line_count);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003029 len = STRLEN(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3031 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003032 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 }
3034#ifdef FEAT_CMDL_INFO
3035 else if (p_ru)
3036 {
3037 /* Current line and column are already on the screen -- webb */
3038 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003039 vim_snprintf((char *)buffer + len, IOSIZE - len,
3040 _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003042 vim_snprintf((char *)buffer + len, IOSIZE - len,
3043 _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 (long)curbuf->b_ml.ml_line_count, n);
3045 }
3046#endif
3047 else
3048 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003049 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 _("line %ld of %ld --%d%%-- col "),
3051 (long)curwin->w_cursor.lnum,
3052 (long)curbuf->b_ml.ml_line_count,
3053 n);
3054 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003055 len = STRLEN(buffer);
3056 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3058 }
3059
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003060 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
3062 if (dont_truncate)
3063 {
3064 /* Temporarily set msg_scroll to avoid the message being truncated.
3065 * First call msg_start() to get the message in the right place. */
3066 msg_start();
3067 n = msg_scroll;
3068 msg_scroll = TRUE;
3069 msg(buffer);
3070 msg_scroll = n;
3071 }
3072 else
3073 {
3074 p = msg_trunc_attr(buffer, FALSE, 0);
3075 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 /* Need to repeat the message after redrawing when:
3077 * - When restart_edit is set (otherwise there will be a delay
3078 * before redrawing).
3079 * - When the screen was scrolled but there is no wait-return
3080 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003081 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 }
3083
3084 vim_free(buffer);
3085}
3086
3087 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003088col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003090 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 int col;
3092 int vcol;
3093{
3094 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003095 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003097 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098}
3099
3100#if defined(FEAT_TITLE) || defined(PROTO)
3101/*
3102 * put file name in title bar of window and in icon title
3103 */
3104
3105static char_u *lasttitle = NULL;
3106static char_u *lasticon = NULL;
3107
3108 void
3109maketitle()
3110{
3111 char_u *p;
3112 char_u *t_str = NULL;
3113 char_u *i_name;
3114 char_u *i_str = NULL;
3115 int maxlen = 0;
3116 int len;
3117 int mustset;
3118 char_u buf[IOSIZE];
3119 int off;
3120
3121 if (!redrawing())
3122 {
3123 /* Postpone updating the title when 'lazyredraw' is set. */
3124 need_maketitle = TRUE;
3125 return;
3126 }
3127
3128 need_maketitle = FALSE;
3129 if (!p_title && !p_icon)
3130 return;
3131
3132 if (p_title)
3133 {
3134 if (p_titlelen > 0)
3135 {
3136 maxlen = p_titlelen * Columns / 100;
3137 if (maxlen < 10)
3138 maxlen = 10;
3139 }
3140
3141 t_str = buf;
3142 if (*p_titlestring != NUL)
3143 {
3144#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003145 if (stl_syntax & STL_IN_TITLE)
3146 {
3147 int use_sandbox = FALSE;
3148 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003149
3150# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003151 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003152# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003153 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003155 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003156 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003157 if (called_emsg)
3158 set_string_option_direct((char_u *)"titlestring", -1,
3159 (char_u *)"", OPT_FREE, SID_ERROR);
3160 called_emsg |= save_called_emsg;
3161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 else
3163#endif
3164 t_str = p_titlestring;
3165 }
3166 else
3167 {
3168 /* format: "fname + (path) (1 of 2) - VIM" */
3169
3170 if (curbuf->b_fname == NULL)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003171 STRCPY(buf, _("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 else
3173 {
3174 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003175 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 }
3178
3179 switch (bufIsChanged(curbuf)
3180 + (curbuf->b_p_ro * 2)
3181 + (!curbuf->b_p_ma * 4))
3182 {
3183 case 1: STRCAT(buf, " +"); break;
3184 case 2: STRCAT(buf, " ="); break;
3185 case 3: STRCAT(buf, " =+"); break;
3186 case 4:
3187 case 6: STRCAT(buf, " -"); break;
3188 case 5:
3189 case 7: STRCAT(buf, " -+"); break;
3190 }
3191
3192 if (curbuf->b_fname != NULL)
3193 {
3194 /* Get path of file, replace home dir with ~ */
3195 off = (int)STRLEN(buf);
3196 buf[off++] = ' ';
3197 buf[off++] = '(';
3198 home_replace(curbuf, curbuf->b_ffname,
3199 buf + off, IOSIZE - off, TRUE);
3200#ifdef BACKSLASH_IN_FILENAME
3201 /* avoid "c:/name" to be reduced to "c" */
3202 if (isalpha(buf[off]) && buf[off + 1] == ':')
3203 off += 2;
3204#endif
3205 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003206 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003209 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003210 (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 /* translate unprintable chars */
3215 p = transstr(buf + off);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003216 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 STRCAT(buf, ")");
3219 }
3220
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003221 append_arg_number(curwin, buf, IOSIZE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
3223#if defined(FEAT_CLIENTSERVER)
3224 if (serverName != NULL)
3225 {
3226 STRCAT(buf, " - ");
3227 STRCAT(buf, serverName);
3228 }
3229 else
3230#endif
3231 STRCAT(buf, " - VIM");
3232
3233 if (maxlen > 0)
3234 {
3235 /* make it shorter by removing a bit in the middle */
3236 len = vim_strsize(buf);
3237 if (len > maxlen)
3238 trunc_string(buf, buf, maxlen);
3239 }
3240 }
3241 }
3242 mustset = ti_change(t_str, &lasttitle);
3243
3244 if (p_icon)
3245 {
3246 i_str = buf;
3247 if (*p_iconstring != NUL)
3248 {
3249#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003250 if (stl_syntax & STL_IN_ICON)
3251 {
3252 int use_sandbox = FALSE;
3253 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003254
3255# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003256 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003257# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003258 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003260 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003261 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003262 if (called_emsg)
3263 set_string_option_direct((char_u *)"iconstring", -1,
3264 (char_u *)"", OPT_FREE, SID_ERROR);
3265 called_emsg |= save_called_emsg;
3266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 else
3268#endif
3269 i_str = p_iconstring;
3270 }
3271 else
3272 {
3273 if (buf_spname(curbuf) != NULL)
3274 i_name = (char_u *)buf_spname(curbuf);
3275 else /* use file name only in icon */
3276 i_name = gettail(curbuf->b_ffname);
3277 *i_str = NUL;
3278 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003279 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 if (len > 100)
3281 {
3282 len -= 100;
3283#ifdef FEAT_MBYTE
3284 if (has_mbyte)
3285 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3286#endif
3287 i_name += len;
3288 }
3289 STRCPY(i_str, i_name);
3290 trans_characters(i_str, IOSIZE);
3291 }
3292 }
3293
3294 mustset |= ti_change(i_str, &lasticon);
3295
3296 if (mustset)
3297 resettitle();
3298}
3299
3300/*
3301 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3302 * from "str" if it does.
3303 * Return TRUE when "*last" changed.
3304 */
3305 static int
3306ti_change(str, last)
3307 char_u *str;
3308 char_u **last;
3309{
3310 if ((str == NULL) != (*last == NULL)
3311 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3312 {
3313 vim_free(*last);
3314 if (str == NULL)
3315 *last = NULL;
3316 else
3317 *last = vim_strsave(str);
3318 return TRUE;
3319 }
3320 return FALSE;
3321}
3322
3323/*
3324 * Put current window title back (used after calling a shell)
3325 */
3326 void
3327resettitle()
3328{
3329 mch_settitle(lasttitle, lasticon);
3330}
Bram Moolenaarea408852005-06-25 22:49:46 +00003331
3332# if defined(EXITFREE) || defined(PROTO)
3333 void
3334free_titles()
3335{
3336 vim_free(lasttitle);
3337 vim_free(lasticon);
3338}
3339# endif
3340
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341#endif /* FEAT_TITLE */
3342
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003343#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003345 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 * Return length of string in screen cells.
3347 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003348 * Normally works for window "wp", except when working for 'tabline' then it
3349 * is "curwin".
3350 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 * Items are drawn interspersed with the text that surrounds it
3352 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3353 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3354 *
3355 * If maxwidth is not zero, the string will be filled at any middle marker
3356 * or truncated if too long, fillchar is used for all whitespace.
3357 */
3358 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003359build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003361 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 size_t outlen; /* length of out[] */
3363 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003364 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 int fillchar;
3366 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003367 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3368 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
3370 char_u *p;
3371 char_u *s;
3372 char_u *t;
3373 char_u *linecont;
3374#ifdef FEAT_EVAL
3375 win_T *o_curwin;
3376 buf_T *o_curbuf;
3377#endif
3378 int empty_line;
3379 colnr_T virtcol;
3380 long l;
3381 long n;
3382 int prevchar_isflag;
3383 int prevchar_isitem;
3384 int itemisflag;
3385 int fillable;
3386 char_u *str;
3387 long num;
3388 int width;
3389 int itemcnt;
3390 int curitem;
3391 int groupitem[STL_MAX_ITEM];
3392 int groupdepth;
3393 struct stl_item
3394 {
3395 char_u *start;
3396 int minwid;
3397 int maxwid;
3398 enum
3399 {
3400 Normal,
3401 Empty,
3402 Group,
3403 Middle,
3404 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003405 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 Trunc
3407 } type;
3408 } item[STL_MAX_ITEM];
3409 int minwid;
3410 int maxwid;
3411 int zeropad;
3412 char_u base;
3413 char_u opt;
3414#define TMPLEN 70
3415 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003416 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003417 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003418
3419#ifdef FEAT_EVAL
3420 /*
3421 * When the format starts with "%!" then evaluate it as an expression and
3422 * use the result as the actual format string.
3423 */
3424 if (fmt[0] == '%' && fmt[1] == '!')
3425 {
3426 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3427 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003428 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003429 }
3430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
3432 if (fillchar == 0)
3433 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003434#ifdef FEAT_MBYTE
3435 /* Can't handle a multi-byte fill character yet. */
3436 else if (mb_char2len(fillchar) > 1)
3437 fillchar = '-';
3438#endif
3439
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 /*
3441 * Get line & check if empty (cursorpos will show "0-1").
3442 * If inversion is possible we use it. Else '=' characters are used.
3443 */
3444 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3445 empty_line = (*linecont == NUL);
3446
3447 groupdepth = 0;
3448 p = out;
3449 curitem = 0;
3450 prevchar_isflag = TRUE;
3451 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003452 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
3454 if (*s != NUL && *s != '%')
3455 prevchar_isflag = prevchar_isitem = FALSE;
3456
3457 /*
3458 * Handle up to the next '%' or the end.
3459 */
3460 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3461 *p++ = *s++;
3462 if (*s == NUL || p + 1 >= out + outlen)
3463 break;
3464
3465 /*
3466 * Handle one '%' item.
3467 */
3468 s++;
3469 if (*s == '%')
3470 {
3471 if (p + 1 >= out + outlen)
3472 break;
3473 *p++ = *s++;
3474 prevchar_isflag = prevchar_isitem = FALSE;
3475 continue;
3476 }
3477 if (*s == STL_MIDDLEMARK)
3478 {
3479 s++;
3480 if (groupdepth > 0)
3481 continue;
3482 item[curitem].type = Middle;
3483 item[curitem++].start = p;
3484 continue;
3485 }
3486 if (*s == STL_TRUNCMARK)
3487 {
3488 s++;
3489 item[curitem].type = Trunc;
3490 item[curitem++].start = p;
3491 continue;
3492 }
3493 if (*s == ')')
3494 {
3495 s++;
3496 if (groupdepth < 1)
3497 continue;
3498 groupdepth--;
3499
3500 t = item[groupitem[groupdepth]].start;
3501 *p = NUL;
3502 l = vim_strsize(t);
3503 if (curitem > groupitem[groupdepth] + 1
3504 && item[groupitem[groupdepth]].minwid == 0)
3505 {
3506 /* remove group if all items are empty */
3507 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3508 if (item[n].type == Normal)
3509 break;
3510 if (n == curitem)
3511 {
3512 p = t;
3513 l = 0;
3514 }
3515 }
3516 if (l > item[groupitem[groupdepth]].maxwid)
3517 {
3518 /* truncate, remove n bytes of text at the start */
3519#ifdef FEAT_MBYTE
3520 if (has_mbyte)
3521 {
3522 /* Find the first character that should be included. */
3523 n = 0;
3524 while (l >= item[groupitem[groupdepth]].maxwid)
3525 {
3526 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003527 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
3529 }
3530 else
3531#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003532 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
3534 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003535 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 p = p - n + 1;
3537#ifdef FEAT_MBYTE
3538 /* Fill up space left over by half a double-wide char. */
3539 while (++l < item[groupitem[groupdepth]].minwid)
3540 *p++ = fillchar;
3541#endif
3542
3543 /* correct the start of the items for the truncation */
3544 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3545 {
3546 item[l].start -= n;
3547 if (item[l].start < t)
3548 item[l].start = t;
3549 }
3550 }
3551 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3552 {
3553 /* fill */
3554 n = item[groupitem[groupdepth]].minwid;
3555 if (n < 0)
3556 {
3557 /* fill by appending characters */
3558 n = 0 - n;
3559 while (l++ < n && p + 1 < out + outlen)
3560 *p++ = fillchar;
3561 }
3562 else
3563 {
3564 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003565 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 l = n - l;
3567 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003568 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 p += l;
3570 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3571 item[n].start += l;
3572 for ( ; l > 0; l--)
3573 *t++ = fillchar;
3574 }
3575 }
3576 continue;
3577 }
3578 minwid = 0;
3579 maxwid = 9999;
3580 zeropad = FALSE;
3581 l = 1;
3582 if (*s == '0')
3583 {
3584 s++;
3585 zeropad = TRUE;
3586 }
3587 if (*s == '-')
3588 {
3589 s++;
3590 l = -1;
3591 }
3592 if (VIM_ISDIGIT(*s))
3593 {
3594 minwid = (int)getdigits(&s);
3595 if (minwid < 0) /* overflow */
3596 minwid = 0;
3597 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003598 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
3600 item[curitem].type = Highlight;
3601 item[curitem].start = p;
3602 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3603 s++;
3604 curitem++;
3605 continue;
3606 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003607 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3608 {
3609 if (*s == STL_TABCLOSENR)
3610 {
3611 if (minwid == 0)
3612 {
3613 /* %X ends the close label, go back to the previously
3614 * define tab label nr. */
3615 for (n = curitem - 1; n >= 0; --n)
3616 if (item[n].type == TabPage && item[n].minwid >= 0)
3617 {
3618 minwid = item[n].minwid;
3619 break;
3620 }
3621 }
3622 else
3623 /* close nrs are stored as negative values */
3624 minwid = - minwid;
3625 }
3626 item[curitem].type = TabPage;
3627 item[curitem].start = p;
3628 item[curitem].minwid = minwid;
3629 s++;
3630 curitem++;
3631 continue;
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 if (*s == '.')
3634 {
3635 s++;
3636 if (VIM_ISDIGIT(*s))
3637 {
3638 maxwid = (int)getdigits(&s);
3639 if (maxwid <= 0) /* overflow */
3640 maxwid = 50;
3641 }
3642 }
3643 minwid = (minwid > 50 ? 50 : minwid) * l;
3644 if (*s == '(')
3645 {
3646 groupitem[groupdepth++] = curitem;
3647 item[curitem].type = Group;
3648 item[curitem].start = p;
3649 item[curitem].minwid = minwid;
3650 item[curitem].maxwid = maxwid;
3651 s++;
3652 curitem++;
3653 continue;
3654 }
3655 if (vim_strchr(STL_ALL, *s) == NULL)
3656 {
3657 s++;
3658 continue;
3659 }
3660 opt = *s++;
3661
3662 /* OK - now for the real work */
3663 base = 'D';
3664 itemisflag = FALSE;
3665 fillable = TRUE;
3666 num = -1;
3667 str = NULL;
3668 switch (opt)
3669 {
3670 case STL_FILEPATH:
3671 case STL_FULLPATH:
3672 case STL_FILENAME:
3673 fillable = FALSE; /* don't change ' ' to fillchar */
3674 if (buf_spname(wp->w_buffer) != NULL)
3675 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3676 else
3677 {
3678 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003679 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3681 }
3682 trans_characters(NameBuff, MAXPATHL);
3683 if (opt != STL_FILENAME)
3684 str = NameBuff;
3685 else
3686 str = gettail(NameBuff);
3687 break;
3688
3689 case STL_VIM_EXPR: /* '{' */
3690 itemisflag = TRUE;
3691 t = p;
3692 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3693 *p++ = *s++;
3694 if (*s != '}') /* missing '}' or out of space */
3695 break;
3696 s++;
3697 *p = 0;
3698 p = t;
3699
3700#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003701 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3703
3704 o_curbuf = curbuf;
3705 o_curwin = curwin;
3706 curwin = wp;
3707 curbuf = wp->w_buffer;
3708
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003709 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
3711 curwin = o_curwin;
3712 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003713 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
3715 if (str != NULL && *str != 0)
3716 {
3717 if (*skipdigits(str) == NUL)
3718 {
3719 num = atoi((char *)str);
3720 vim_free(str);
3721 str = NULL;
3722 itemisflag = FALSE;
3723 }
3724 }
3725#endif
3726 break;
3727
3728 case STL_LINE:
3729 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3730 ? 0L : (long)(wp->w_cursor.lnum);
3731 break;
3732
3733 case STL_NUMLINES:
3734 num = wp->w_buffer->b_ml.ml_line_count;
3735 break;
3736
3737 case STL_COLUMN:
3738 num = !(State & INSERT) && empty_line
3739 ? 0 : (int)wp->w_cursor.col + 1;
3740 break;
3741
3742 case STL_VIRTCOL:
3743 case STL_VIRTCOL_ALT:
3744 /* In list mode virtcol needs to be recomputed */
3745 virtcol = wp->w_virtcol;
3746 if (wp->w_p_list && lcs_tab1 == NUL)
3747 {
3748 wp->w_p_list = FALSE;
3749 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3750 wp->w_p_list = TRUE;
3751 }
3752 ++virtcol;
3753 /* Don't display %V if it's the same as %c. */
3754 if (opt == STL_VIRTCOL_ALT
3755 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3756 ? 0 : (int)wp->w_cursor.col + 1)))
3757 break;
3758 num = (long)virtcol;
3759 break;
3760
3761 case STL_PERCENTAGE:
3762 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3763 (long)wp->w_buffer->b_ml.ml_line_count);
3764 break;
3765
3766 case STL_ALTPERCENT:
3767 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003768 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 break;
3770
3771 case STL_ARGLISTSTAT:
3772 fillable = FALSE;
3773 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003774 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 str = tmp;
3776 break;
3777
3778 case STL_KEYMAP:
3779 fillable = FALSE;
3780 if (get_keymap_str(wp, tmp, TMPLEN))
3781 str = tmp;
3782 break;
3783 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003784#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003785 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786#else
3787 num = 0;
3788#endif
3789 break;
3790
3791 case STL_BUFNO:
3792 num = wp->w_buffer->b_fnum;
3793 break;
3794
3795 case STL_OFFSET_X:
3796 base = 'X';
3797 case STL_OFFSET:
3798#ifdef FEAT_BYTEOFF
3799 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3800 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3801 0L : l + 1 + (!(State & INSERT) && empty_line ?
3802 0 : (int)wp->w_cursor.col);
3803#endif
3804 break;
3805
3806 case STL_BYTEVAL_X:
3807 base = 'X';
3808 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003809 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 num = 0;
3811 else
3812 {
3813#ifdef FEAT_MBYTE
3814 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3815#else
3816 num = linecont[wp->w_cursor.col];
3817#endif
3818 }
3819 if (num == NL)
3820 num = 0;
3821 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3822 num = NL;
3823 break;
3824
3825 case STL_ROFLAG:
3826 case STL_ROFLAG_ALT:
3827 itemisflag = TRUE;
3828 if (wp->w_buffer->b_p_ro)
3829 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3830 break;
3831
3832 case STL_HELPFLAG:
3833 case STL_HELPFLAG_ALT:
3834 itemisflag = TRUE;
3835 if (wp->w_buffer->b_help)
3836 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003837 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 break;
3839
3840#ifdef FEAT_AUTOCMD
3841 case STL_FILETYPE:
3842 if (*wp->w_buffer->b_p_ft != NUL
3843 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3844 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003845 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3846 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 str = tmp;
3848 }
3849 break;
3850
3851 case STL_FILETYPE_ALT:
3852 itemisflag = TRUE;
3853 if (*wp->w_buffer->b_p_ft != NUL
3854 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3855 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003856 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3857 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 for (t = tmp; *t != 0; t++)
3859 *t = TOUPPER_LOC(*t);
3860 str = tmp;
3861 }
3862 break;
3863#endif
3864
3865#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3866 case STL_PREVIEWFLAG:
3867 case STL_PREVIEWFLAG_ALT:
3868 itemisflag = TRUE;
3869 if (wp->w_p_pvw)
3870 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3871 : _("[Preview]"));
3872 break;
3873#endif
3874
3875 case STL_MODIFIED:
3876 case STL_MODIFIED_ALT:
3877 itemisflag = TRUE;
3878 switch ((opt == STL_MODIFIED_ALT)
3879 + bufIsChanged(wp->w_buffer) * 2
3880 + (!wp->w_buffer->b_p_ma) * 4)
3881 {
3882 case 2: str = (char_u *)"[+]"; break;
3883 case 3: str = (char_u *)",+"; break;
3884 case 4: str = (char_u *)"[-]"; break;
3885 case 5: str = (char_u *)",-"; break;
3886 case 6: str = (char_u *)"[+-]"; break;
3887 case 7: str = (char_u *)",+-"; break;
3888 }
3889 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003890
3891 case STL_HIGHLIGHT:
3892 t = s;
3893 while (*s != '#' && *s != NUL)
3894 ++s;
3895 if (*s == '#')
3896 {
3897 item[curitem].type = Highlight;
3898 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003899 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003900 curitem++;
3901 }
3902 ++s;
3903 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
3905
3906 item[curitem].start = p;
3907 item[curitem].type = Normal;
3908 if (str != NULL && *str)
3909 {
3910 t = str;
3911 if (itemisflag)
3912 {
3913 if ((t[0] && t[1])
3914 && ((!prevchar_isitem && *t == ',')
3915 || (prevchar_isflag && *t == ' ')))
3916 t++;
3917 prevchar_isflag = TRUE;
3918 }
3919 l = vim_strsize(t);
3920 if (l > 0)
3921 prevchar_isitem = TRUE;
3922 if (l > maxwid)
3923 {
3924 while (l >= maxwid)
3925#ifdef FEAT_MBYTE
3926 if (has_mbyte)
3927 {
3928 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003929 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931 else
3932#endif
3933 l -= byte2cells(*t++);
3934 if (p + 1 >= out + outlen)
3935 break;
3936 *p++ = '<';
3937 }
3938 if (minwid > 0)
3939 {
3940 for (; l < minwid && p + 1 < out + outlen; l++)
3941 {
3942 /* Don't put a "-" in front of a digit. */
3943 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3944 *p++ = ' ';
3945 else
3946 *p++ = fillchar;
3947 }
3948 minwid = 0;
3949 }
3950 else
3951 minwid *= -1;
3952 while (*t && p + 1 < out + outlen)
3953 {
3954 *p++ = *t++;
3955 /* Change a space by fillchar, unless fillchar is '-' and a
3956 * digit follows. */
3957 if (fillable && p[-1] == ' '
3958 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3959 p[-1] = fillchar;
3960 }
3961 for (; l < minwid && p + 1 < out + outlen; l++)
3962 *p++ = fillchar;
3963 }
3964 else if (num >= 0)
3965 {
3966 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3967 char_u nstr[20];
3968
3969 if (p + 20 >= out + outlen)
3970 break; /* not sufficient space */
3971 prevchar_isitem = TRUE;
3972 t = nstr;
3973 if (opt == STL_VIRTCOL_ALT)
3974 {
3975 *t++ = '-';
3976 minwid--;
3977 }
3978 *t++ = '%';
3979 if (zeropad)
3980 *t++ = '0';
3981 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003982 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 *t = 0;
3984
3985 for (n = num, l = 1; n >= nbase; n /= nbase)
3986 l++;
3987 if (opt == STL_VIRTCOL_ALT)
3988 l++;
3989 if (l > maxwid)
3990 {
3991 l += 2;
3992 n = l - maxwid;
3993 while (l-- > maxwid)
3994 num /= nbase;
3995 *t++ = '>';
3996 *t++ = '%';
3997 *t = t[-3];
3998 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003999 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4000 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 }
4002 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004003 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4004 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 p += STRLEN(p);
4006 }
4007 else
4008 item[curitem].type = Empty;
4009
4010 if (opt == STL_VIM_EXPR)
4011 vim_free(str);
4012
4013 if (num >= 0 || (!itemisflag && str && *str))
4014 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4015 curitem++;
4016 }
4017 *p = NUL;
4018 itemcnt = curitem;
4019
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004020#ifdef FEAT_EVAL
4021 if (usefmt != fmt)
4022 vim_free(usefmt);
4023#endif
4024
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 width = vim_strsize(out);
4026 if (maxwidth > 0 && width > maxwidth)
4027 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004028 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 l = 0;
4030 if (itemcnt == 0)
4031 s = out;
4032 else
4033 {
4034 for ( ; l < itemcnt; l++)
4035 if (item[l].type == Trunc)
4036 {
4037 /* Truncate at %< item. */
4038 s = item[l].start;
4039 break;
4040 }
4041 if (l == itemcnt)
4042 {
4043 /* No %< item, truncate first item. */
4044 s = item[0].start;
4045 l = 0;
4046 }
4047 }
4048
4049 if (width - vim_strsize(s) >= maxwidth)
4050 {
4051 /* Truncation mark is beyond max length */
4052#ifdef FEAT_MBYTE
4053 if (has_mbyte)
4054 {
4055 s = out;
4056 width = 0;
4057 for (;;)
4058 {
4059 width += ptr2cells(s);
4060 if (width >= maxwidth)
4061 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004062 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
4064 /* Fill up for half a double-wide character. */
4065 while (++width < maxwidth)
4066 *s++ = fillchar;
4067 }
4068 else
4069#endif
4070 s = out + maxwidth - 1;
4071 for (l = 0; l < itemcnt; l++)
4072 if (item[l].start > s)
4073 break;
4074 itemcnt = l;
4075 *s++ = '>';
4076 *s = 0;
4077 }
4078 else
4079 {
4080#ifdef FEAT_MBYTE
4081 if (has_mbyte)
4082 {
4083 n = 0;
4084 while (width >= maxwidth)
4085 {
4086 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004087 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 }
4089 }
4090 else
4091#endif
4092 n = width - maxwidth + 1;
4093 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004094 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 *s = '<';
4096
4097 /* Fill up for half a double-wide character. */
4098 while (++width < maxwidth)
4099 {
4100 s = s + STRLEN(s);
4101 *s++ = fillchar;
4102 *s = NUL;
4103 }
4104
4105 --n; /* count the '<' */
4106 for (; l < itemcnt; l++)
4107 {
4108 if (item[l].start - n >= s)
4109 item[l].start -= n;
4110 else
4111 item[l].start = s;
4112 }
4113 }
4114 width = maxwidth;
4115 }
4116 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4117 {
4118 /* Apply STL_MIDDLE if any */
4119 for (l = 0; l < itemcnt; l++)
4120 if (item[l].type == Middle)
4121 break;
4122 if (l < itemcnt)
4123 {
4124 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004125 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 for (s = item[l].start; s < p; s++)
4127 *s = fillchar;
4128 for (l++; l < itemcnt; l++)
4129 item[l].start += maxwidth - width;
4130 width = maxwidth;
4131 }
4132 }
4133
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004134 /* Store the info about highlighting. */
4135 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004137 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 for (l = 0; l < itemcnt; l++)
4139 {
4140 if (item[l].type == Highlight)
4141 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004142 sp->start = item[l].start;
4143 sp->userhl = item[l].minwid;
4144 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
4146 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004147 sp->start = NULL;
4148 sp->userhl = 0;
4149 }
4150
4151 /* Store the info about tab pages labels. */
4152 if (tabtab != NULL)
4153 {
4154 sp = tabtab;
4155 for (l = 0; l < itemcnt; l++)
4156 {
4157 if (item[l].type == TabPage)
4158 {
4159 sp->start = item[l].start;
4160 sp->userhl = item[l].minwid;
4161 sp++;
4162 }
4163 }
4164 sp->start = NULL;
4165 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
4167
4168 return width;
4169}
4170#endif /* FEAT_STL_OPT */
4171
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004172#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4173 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004175 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4176 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 */
4178 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004179get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004181 char_u *buf;
4182 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183{
4184 long above; /* number of lines above window */
4185 long below; /* number of lines below window */
4186
4187 above = wp->w_topline - 1;
4188#ifdef FEAT_DIFF
4189 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4190#endif
4191 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4192 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004193 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4194 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004196 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004198 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 ? (int)(above / ((above + below) / 100L))
4200 : (int)(above * 100L / (above + below)));
4201}
4202#endif
4203
4204/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004205 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 * Return TRUE if it was appended.
4207 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004208 static int
4209append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 win_T *wp;
4211 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004212 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214{
4215 char_u *p;
4216
4217 if (ARGCOUNT <= 1) /* nothing to do */
4218 return FALSE;
4219
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004220 p = buf + STRLEN(buf); /* go to the end of the buffer */
4221 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 return FALSE;
4223 *p++ = ' ';
4224 *p++ = '(';
4225 if (add_file)
4226 {
4227 STRCPY(p, "file ");
4228 p += 5;
4229 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004230 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4231 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4233 return TRUE;
4234}
4235
4236/*
4237 * If fname is not a full path, make it a full path.
4238 * Returns pointer to allocated memory (NULL for failure).
4239 */
4240 char_u *
4241fix_fname(fname)
4242 char_u *fname;
4243{
4244 /*
4245 * Force expanding the path always for Unix, because symbolic links may
4246 * mess up the full path name, even though it starts with a '/'.
4247 * Also expand when there is ".." in the file name, try to remove it,
4248 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004249 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 * For MS-Windows also expand names like "longna~1" to "longname".
4251 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004252#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 return FullName_save(fname, TRUE);
4254#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004255 if (!vim_isAbsName(fname)
4256 || strstr((char *)fname, "..") != NULL
4257 || strstr((char *)fname, "//") != NULL
4258# ifdef BACKSLASH_IN_FILENAME
4259 || strstr((char *)fname, "\\\\") != NULL
4260# endif
4261# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 )
4265 return FullName_save(fname, FALSE);
4266
4267 fname = vim_strsave(fname);
4268
Bram Moolenaar9b942202007-10-03 12:31:33 +00004269# ifdef USE_FNAME_CASE
4270# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004272# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 {
4274 if (fname != NULL)
4275 fname_case(fname, 0); /* set correct case for file name */
4276 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004277# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
4279 return fname;
4280#endif
4281}
4282
4283/*
4284 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4285 * "ffname" becomes a pointer to allocated memory (or NULL).
4286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 void
4288fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004289 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 char_u **ffname;
4291 char_u **sfname;
4292{
4293 if (*ffname == NULL) /* if no file name given, nothing to do */
4294 return;
4295 if (*sfname == NULL) /* if no short file name given, use ffname */
4296 *sfname = *ffname;
4297 *ffname = fix_fname(*ffname); /* expand to full path */
4298
4299#ifdef FEAT_SHORTCUT
4300 if (!buf->b_p_bin)
4301 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004302 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
4304 /* If the file name is a shortcut file, use the file it links to. */
4305 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004306 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 {
4308 vim_free(*ffname);
4309 *ffname = rfname;
4310 *sfname = rfname;
4311 }
4312 }
4313#endif
4314}
4315
4316/*
4317 * Get the file name for an argument list entry.
4318 */
4319 char_u *
4320alist_name(aep)
4321 aentry_T *aep;
4322{
4323 buf_T *bp;
4324
4325 /* Use the name from the associated buffer if it exists. */
4326 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004327 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 return aep->ae_fname;
4329 return bp->b_fname;
4330}
4331
4332#if defined(FEAT_WINDOWS) || defined(PROTO)
4333/*
4334 * do_arg_all(): Open up to 'count' windows, one for each argument.
4335 */
4336 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004337do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 int count;
4339 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004340 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341{
4342 int i;
4343 win_T *wp, *wpnext;
4344 char_u *opened; /* array of flags for which args are open */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004345 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 int use_firstwin = FALSE; /* use first window for arglist */
4347 int split_ret = OK;
4348 int p_ea_save;
4349 alist_T *alist; /* argument list to be used */
4350 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004351 tabpage_T *tpnext;
4352 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004353 win_T *new_curwin = NULL;
4354 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
4356 if (ARGCOUNT <= 0)
4357 {
4358 /* Don't give an error message. We don't want it when the ":all"
4359 * command is in the .vimrc. */
4360 return;
4361 }
4362 setpcmark();
4363
4364 opened_len = ARGCOUNT;
4365 opened = alloc_clear((unsigned)opened_len);
4366 if (opened == NULL)
4367 return;
4368
4369#ifdef FEAT_GUI
4370 need_mouse_correct = TRUE;
4371#endif
4372
4373 /*
4374 * Try closing all windows that are not in the argument list.
4375 * Also close windows that are not full width;
4376 * When 'hidden' or "forceit" set the buffer becomes hidden.
4377 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004378 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004380 if (had_tab > 0)
4381 goto_tabpage_tp(first_tabpage);
4382 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004384 tpnext = curtab->tp_next;
4385 for (wp = firstwin; wp != NULL; wp = wpnext)
4386 {
4387 wpnext = wp->w_next;
4388 buf = wp->w_buffer;
4389 if (buf->b_ffname == NULL
4390 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004392 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004394 )
4395 i = ARGCOUNT;
4396 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004398 /* check if the buffer in this window is in the arglist */
4399 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004401 if (ARGLIST[i].ae_fnum == buf->b_fnum
4402 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004403 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004405 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004406 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004407 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004408 if (i == 0)
4409 {
4410 new_curwin = wp;
4411 new_curtab = curtab;
4412 }
4413 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004414 if (wp->w_alist != curwin->w_alist)
4415 {
4416 /* Use the current argument list for all windows
4417 * containing a file from it. */
4418 alist_unlink(wp->w_alist);
4419 wp->w_alist = curwin->w_alist;
4420 ++wp->w_alist->al_refcount;
4421 }
4422 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004426 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004428 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004430 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004431 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004433 /* If the buffer was changed, and we would like to hide it,
4434 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004435 if (!P_HID(buf) && buf->b_nwindows <= 1
4436 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004438 (void)autowrite(buf, FALSE);
4439#ifdef FEAT_AUTOCMD
4440 /* check if autocommands removed the window */
4441 if (!win_valid(wp) || !buf_valid(buf))
4442 {
4443 wpnext = firstwin; /* start all over... */
4444 continue;
4445 }
4446#endif
4447 }
4448#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004449 /* don't close last window */
4450 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004451#endif
4452 use_firstwin = TRUE;
4453#ifdef FEAT_WINDOWS
4454 else
4455 {
4456 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4457# ifdef FEAT_AUTOCMD
4458 /* check if autocommands removed the next window */
4459 if (!win_valid(wpnext))
4460 wpnext = firstwin; /* start all over... */
4461# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 }
4463#endif
4464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 }
4466 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004467
4468 /* Without the ":tab" modifier only do the current tab page. */
4469 if (had_tab == 0 || tpnext == NULL)
4470 break;
4471
4472# ifdef FEAT_AUTOCMD
4473 /* check if autocommands removed the next tab page */
4474 if (!valid_tabpage(tpnext))
4475 tpnext = first_tabpage; /* start all over...*/
4476# endif
4477 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 }
4479
4480 /*
4481 * Open a window for files in the argument list that don't have one.
4482 * ARGCOUNT may change while doing this, because of autocommands.
4483 */
4484 if (count > ARGCOUNT || count <= 0)
4485 count = ARGCOUNT;
4486
4487 /* Autocommands may do anything to the argument list. Make sure it's not
4488 * freed while we are working here by "locking" it. We still have to
4489 * watch out for its size to be changed. */
4490 alist = curwin->w_alist;
4491 ++alist->al_refcount;
4492
4493#ifdef FEAT_AUTOCMD
4494 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4495 ++autocmd_no_enter;
4496 ++autocmd_no_leave;
4497#endif
4498 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004499#ifdef FEAT_WINDOWS
4500 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4501 * leaving an empty tab page when executed locally. */
4502 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4503 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4504 use_firstwin = TRUE;
4505#endif
4506
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4508 {
4509 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4510 arg_had_last = TRUE;
4511 if (i < opened_len && opened[i])
4512 {
4513 /* Move the already present window to below the current window */
4514 if (curwin->w_arg_idx != i)
4515 {
4516 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4517 {
4518 if (wpnext->w_arg_idx == i)
4519 {
4520 win_move_after(wpnext, curwin);
4521 break;
4522 }
4523 }
4524 }
4525 }
4526 else if (split_ret == OK)
4527 {
4528 if (!use_firstwin) /* split current window */
4529 {
4530 p_ea_save = p_ea;
4531 p_ea = TRUE; /* use space from all windows */
4532 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4533 p_ea = p_ea_save;
4534 if (split_ret == FAIL)
4535 continue;
4536 }
4537#ifdef FEAT_AUTOCMD
4538 else /* first window: do autocmd for leaving this buffer */
4539 --autocmd_no_leave;
4540#endif
4541
4542 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004543 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 */
4545 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004546 if (i == 0)
4547 {
4548 new_curwin = curwin;
4549 new_curtab = curtab;
4550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4552 ECMD_ONE,
4553 ((P_HID(curwin->w_buffer)
4554 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004555 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556#ifdef FEAT_AUTOCMD
4557 if (use_firstwin)
4558 ++autocmd_no_leave;
4559#endif
4560 use_firstwin = FALSE;
4561 }
4562 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004563
4564 /* When ":tab" was used open a new tab for a new window repeatedly. */
4565 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4566 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 }
4568
4569 /* Remove the "lock" on the argument list. */
4570 alist_unlink(alist);
4571
4572#ifdef FEAT_AUTOCMD
4573 --autocmd_no_enter;
4574#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004575 /* to window with first arg */
4576 if (valid_tabpage(new_curtab))
4577 goto_tabpage_tp(new_curtab);
4578 if (win_valid(new_curwin))
4579 win_enter(new_curwin, FALSE);
4580
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581#ifdef FEAT_AUTOCMD
4582 --autocmd_no_leave;
4583#endif
4584 vim_free(opened);
4585}
4586
4587# if defined(FEAT_LISTCMDS) || defined(PROTO)
4588/*
4589 * Open a window for a number of buffers.
4590 */
4591 void
4592ex_buffer_all(eap)
4593 exarg_T *eap;
4594{
4595 buf_T *buf;
4596 win_T *wp, *wpnext;
4597 int split_ret = OK;
4598 int p_ea_save;
4599 int open_wins = 0;
4600 int r;
4601 int count; /* Maximum number of windows to open. */
4602 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004603#ifdef FEAT_WINDOWS
4604 int had_tab = cmdmod.tab;
4605 tabpage_T *tpnext;
4606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607
4608 if (eap->addr_count == 0) /* make as many windows as possible */
4609 count = 9999;
4610 else
4611 count = eap->line2; /* make as many windows as specified */
4612 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4613 all = FALSE;
4614 else
4615 all = TRUE;
4616
4617 setpcmark();
4618
4619#ifdef FEAT_GUI
4620 need_mouse_correct = TRUE;
4621#endif
4622
4623 /*
4624 * Close superfluous windows (two windows for the same buffer).
4625 * Also close windows that are not full-width.
4626 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004627#ifdef FEAT_WINDOWS
4628 if (had_tab > 0)
4629 goto_tabpage_tp(first_tabpage);
4630 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004633 tpnext = curtab->tp_next;
4634 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004636 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004637 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004638#ifdef FEAT_VERTSPLIT
4639 || ((cmdmod.split & WSP_VERT)
4640 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004641 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004642 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004644#ifdef FEAT_WINDOWS
4645 || (had_tab > 0 && wp != firstwin)
4646#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004647 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004648 {
4649 win_close(wp, FALSE);
4650#ifdef FEAT_AUTOCMD
4651 wpnext = firstwin; /* just in case an autocommand does
4652 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004653 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004654 open_wins = 0;
4655#endif
4656 }
4657 else
4658 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004660
4661#ifdef FEAT_WINDOWS
4662 /* Without the ":tab" modifier only do the current tab page. */
4663 if (had_tab == 0 || tpnext == NULL)
4664 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004665 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668
4669 /*
4670 * Go through the buffer list. When a buffer doesn't have a window yet,
4671 * open one. Otherwise move the window to the right position.
4672 * Watch out for autocommands that delete buffers or windows!
4673 */
4674#ifdef FEAT_AUTOCMD
4675 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4676 ++autocmd_no_enter;
4677#endif
4678 win_enter(lastwin, FALSE);
4679#ifdef FEAT_AUTOCMD
4680 ++autocmd_no_leave;
4681#endif
4682 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4683 {
4684 /* Check if this buffer needs a window */
4685 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4686 continue;
4687
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004688#ifdef FEAT_WINDOWS
4689 if (had_tab != 0)
4690 {
4691 /* With the ":tab" modifier don't move the window. */
4692 if (buf->b_nwindows > 0)
4693 wp = lastwin; /* buffer has a window, skip it */
4694 else
4695 wp = NULL;
4696 }
4697 else
4698#endif
4699 {
4700 /* Check if this buffer already has a window */
4701 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4702 if (wp->w_buffer == buf)
4703 break;
4704 /* If the buffer already has a window, move it */
4705 if (wp != NULL)
4706 win_move_after(wp, curwin);
4707 }
4708
4709 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
4711 /* Split the window and put the buffer in it */
4712 p_ea_save = p_ea;
4713 p_ea = TRUE; /* use space from all windows */
4714 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4715 ++open_wins;
4716 p_ea = p_ea_save;
4717 if (split_ret == FAIL)
4718 continue;
4719
4720 /* Open the buffer in this window. */
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_DIALOG;
4723#endif
4724 set_curbuf(buf, DOBUF_GOTO);
4725#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004728#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 break;
4732 }
4733#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004734#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 if (swap_exists_action == SEA_QUIT)
4736 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004737# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4738 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004739
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004740 /* Reset the error/interrupt/exception state here so that
4741 * aborting() returns FALSE when closing a window. */
4742 enter_cleanup(&cs);
4743# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004744
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004745 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 win_close(curwin, TRUE);
4747 --open_wins;
4748 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004749 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004750
4751# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4752 /* Restore the error/interrupt/exception state if not
4753 * discarded by a new aborting error, interrupt, or uncaught
4754 * exception. */
4755 leave_cleanup(&cs);
4756# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 }
4758 else
4759 handle_swap_exists(NULL);
4760#endif
4761 }
4762
4763 ui_breakcheck();
4764 if (got_int)
4765 {
4766 (void)vgetc(); /* only break the file loading, not the rest */
4767 break;
4768 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004769#ifdef FEAT_EVAL
4770 /* Autocommands deleted the buffer or aborted script processing!!! */
4771 if (aborting())
4772 break;
4773#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004774#ifdef FEAT_WINDOWS
4775 /* When ":tab" was used open a new tab for a new window repeatedly. */
4776 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4777 cmdmod.tab = 9999;
4778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
4780#ifdef FEAT_AUTOCMD
4781 --autocmd_no_enter;
4782#endif
4783 win_enter(firstwin, FALSE); /* back to first window */
4784#ifdef FEAT_AUTOCMD
4785 --autocmd_no_leave;
4786#endif
4787
4788 /*
4789 * Close superfluous windows.
4790 */
4791 for (wp = lastwin; open_wins > count; )
4792 {
4793 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4794 || autowrite(wp->w_buffer, FALSE) == OK);
4795#ifdef FEAT_AUTOCMD
4796 if (!win_valid(wp))
4797 {
4798 /* BufWrite Autocommands made the window invalid, start over */
4799 wp = lastwin;
4800 }
4801 else
4802#endif
4803 if (r)
4804 {
4805 win_close(wp, !P_HID(wp->w_buffer));
4806 --open_wins;
4807 wp = lastwin;
4808 }
4809 else
4810 {
4811 wp = wp->w_prev;
4812 if (wp == NULL)
4813 break;
4814 }
4815 }
4816}
4817# endif /* FEAT_LISTCMDS */
4818
4819#endif /* FEAT_WINDOWS */
4820
Bram Moolenaara3227e22006-03-08 21:32:40 +00004821static int chk_modeline __ARGS((linenr_T, int));
4822
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823/*
4824 * do_modelines() - process mode lines for the current file
4825 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004826 * "flags" can be:
4827 * OPT_WINONLY only set options local to window
4828 * OPT_NOWIN don't set options local to window
4829 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 * Returns immediately if the "ml" option isn't set.
4831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004833do_modelines(flags)
4834 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004836 linenr_T lnum;
4837 int nmlines;
4838 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
4840 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4841 return;
4842
4843 /* Disallow recursive entry here. Can happen when executing a modeline
4844 * triggers an autocommand, which reloads modelines with a ":do". */
4845 if (entered)
4846 return;
4847
4848 ++entered;
4849 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4850 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004851 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 nmlines = 0;
4853
4854 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4855 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004856 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 nmlines = 0;
4858 --entered;
4859}
4860
4861#include "version.h" /* for version number */
4862
4863/*
4864 * chk_modeline() - check a single line for a mode string
4865 * Return FAIL if an error encountered.
4866 */
4867 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004868chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004870 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871{
4872 char_u *s;
4873 char_u *e;
4874 char_u *linecopy; /* local copy of any modeline found */
4875 int prev;
4876 int vers;
4877 int end;
4878 int retval = OK;
4879 char_u *save_sourcing_name;
4880 linenr_T save_sourcing_lnum;
4881#ifdef FEAT_EVAL
4882 scid_T save_SID;
4883#endif
4884
4885 prev = -1;
4886 for (s = ml_get(lnum); *s != NUL; ++s)
4887 {
4888 if (prev == -1 || vim_isspace(prev))
4889 {
4890 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4891 || STRNCMP(s, "vi:", (size_t)3) == 0)
4892 break;
4893 if (STRNCMP(s, "vim", 3) == 0)
4894 {
4895 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4896 e = s + 4;
4897 else
4898 e = s + 3;
4899 vers = getdigits(&e);
4900 if (*e == ':'
4901 && (s[3] == ':'
4902 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4903 || (VIM_VERSION_100 < vers && s[3] == '<')
4904 || (VIM_VERSION_100 > vers && s[3] == '>')
4905 || (VIM_VERSION_100 == vers && s[3] == '=')))
4906 break;
4907 }
4908 }
4909 prev = *s;
4910 }
4911
4912 if (*s)
4913 {
4914 do /* skip over "ex:", "vi:" or "vim:" */
4915 ++s;
4916 while (s[-1] != ':');
4917
4918 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4919 if (linecopy == NULL)
4920 return FAIL;
4921
4922 save_sourcing_lnum = sourcing_lnum;
4923 save_sourcing_name = sourcing_name;
4924 sourcing_lnum = lnum; /* prepare for emsg() */
4925 sourcing_name = (char_u *)"modelines";
4926
4927 end = FALSE;
4928 while (end == FALSE)
4929 {
4930 s = skipwhite(s);
4931 if (*s == NUL)
4932 break;
4933
4934 /*
4935 * Find end of set command: ':' or end of line.
4936 * Skip over "\:", replacing it with ":".
4937 */
4938 for (e = s; *e != ':' && *e != NUL; ++e)
4939 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00004940 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 if (*e == NUL)
4942 end = TRUE;
4943
4944 /*
4945 * If there is a "set" command, require a terminating ':' and
4946 * ignore the stuff after the ':'.
4947 * "vi:set opt opt opt: foo" -- foo not interpreted
4948 * "vi:opt opt opt: foo" -- foo interpreted
4949 * Accept "se" for compatibility with Elvis.
4950 */
4951 if (STRNCMP(s, "set ", (size_t)4) == 0
4952 || STRNCMP(s, "se ", (size_t)3) == 0)
4953 {
4954 if (*e != ':') /* no terminating ':'? */
4955 break;
4956 end = TRUE;
4957 s = vim_strchr(s, ' ') + 1;
4958 }
4959 *e = NUL; /* truncate the set command */
4960
4961 if (*s != NUL) /* skip over an empty "::" */
4962 {
4963#ifdef FEAT_EVAL
4964 save_SID = current_SID;
4965 current_SID = SID_MODELINE;
4966#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004967 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968#ifdef FEAT_EVAL
4969 current_SID = save_SID;
4970#endif
4971 if (retval == FAIL) /* stop if error found */
4972 break;
4973 }
4974 s = e + 1; /* advance to next part */
4975 }
4976
4977 sourcing_lnum = save_sourcing_lnum;
4978 sourcing_name = save_sourcing_name;
4979
4980 vim_free(linecopy);
4981 }
4982 return retval;
4983}
4984
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00004985#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 int
4987read_viminfo_bufferlist(virp, writing)
4988 vir_T *virp;
4989 int writing;
4990{
4991 char_u *tab;
4992 linenr_T lnum;
4993 colnr_T col;
4994 buf_T *buf;
4995 char_u *sfname;
4996 char_u *xline;
4997
4998 /* Handle long line and escaped characters. */
4999 xline = viminfo_readstring(virp, 1, FALSE);
5000
5001 /* don't read in if there are files on the command-line or if writing: */
5002 if (xline != NULL && !writing && ARGCOUNT == 0
5003 && find_viminfo_parameter('%') != NULL)
5004 {
5005 /* Format is: <fname> Tab <lnum> Tab <col>.
5006 * Watch out for a Tab in the file name, work from the end. */
5007 lnum = 0;
5008 col = 0;
5009 tab = vim_strrchr(xline, '\t');
5010 if (tab != NULL)
5011 {
5012 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005013 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 tab = vim_strrchr(xline, '\t');
5015 if (tab != NULL)
5016 {
5017 *tab++ = '\0';
5018 lnum = atol((char *)tab);
5019 }
5020 }
5021
5022 /* Expand "~/" in the file name at "line + 1" to a full path.
5023 * Then try shortening it by comparing with the current directory */
5024 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005025 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
5027 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5028 if (buf != NULL) /* just in case... */
5029 {
5030 buf->b_last_cursor.lnum = lnum;
5031 buf->b_last_cursor.col = col;
5032 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5033 }
5034 }
5035 vim_free(xline);
5036
5037 return viminfo_readline(virp);
5038}
5039
5040 void
5041write_viminfo_bufferlist(fp)
5042 FILE *fp;
5043{
5044 buf_T *buf;
5045#ifdef FEAT_WINDOWS
5046 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005047 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048#endif
5049 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005050 int max_buffers;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005051 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052
5053 if (find_viminfo_parameter('%') == NULL)
5054 return;
5055
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005056 /* Without a number -1 is returned: do all buffers. */
5057 max_buffers = get_viminfo_parameter('%');
5058
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005060#define LINE_BUF_LEN (MAXPATHL + 40)
5061 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 if (line == NULL)
5063 return;
5064
5065#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005066 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 set_last_cursor(win);
5068#else
5069 set_last_cursor(curwin);
5070#endif
5071
5072 fprintf(fp, _("\n# Buffer list:\n"));
5073 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5074 {
5075 if (buf->b_fname == NULL
5076 || !buf->b_p_bl
5077#ifdef FEAT_QUICKFIX
5078 || bt_quickfix(buf)
5079#endif
5080 || removable(buf->b_ffname))
5081 continue;
5082
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005083 if (max_buffers-- == 0)
5084 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 putc('%', fp);
5086 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005087 len = STRLEN(line);
5088 vim_snprintf((char *)line + len, len - LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 (long)buf->b_last_cursor.lnum,
5090 buf->b_last_cursor.col);
5091 viminfo_writestring(fp, line);
5092 }
5093 vim_free(line);
5094}
5095#endif
5096
5097
5098/*
5099 * Return special buffer name.
5100 * Returns NULL when the buffer has a normal file name.
5101 */
5102 char *
5103buf_spname(buf)
5104 buf_T *buf;
5105{
5106#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5107 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005108 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005109 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005110 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005111
5112 /*
5113 * For location list window, w_llist_ref points to the location list.
5114 * For quickfix window, w_llist_ref is NULL.
5115 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005116 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005117 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005118 goto win_found;
5119win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005120 if (win != NULL && win->w_llist_ref != NULL)
5121 return _("[Location List]");
5122 else
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005123 return _("[Quickfix List]");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125#endif
5126#ifdef FEAT_QUICKFIX
5127 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5128 * contains the name as specified by the user */
5129 if (bt_nofile(buf))
5130 {
5131 if (buf->b_sfname != NULL)
5132 return (char *)buf->b_sfname;
Bram Moolenaarf4f664c2008-12-03 10:21:57 +00005133 return _("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
5135#endif
5136 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005137 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 return NULL;
5139}
5140
5141
5142#if defined(FEAT_SIGNS) || defined(PROTO)
5143/*
5144 * Insert the sign into the signlist.
5145 */
5146 static void
5147insert_sign(buf, prev, next, id, lnum, typenr)
5148 buf_T *buf; /* buffer to store sign in */
5149 signlist_T *prev; /* previous sign entry */
5150 signlist_T *next; /* next sign entry */
5151 int id; /* sign ID */
5152 linenr_T lnum; /* line number which gets the mark */
5153 int typenr; /* typenr of sign we are adding */
5154{
5155 signlist_T *newsign;
5156
5157 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5158 if (newsign != NULL)
5159 {
5160 newsign->id = id;
5161 newsign->lnum = lnum;
5162 newsign->typenr = typenr;
5163 newsign->next = next;
5164#ifdef FEAT_NETBEANS_INTG
5165 newsign->prev = prev;
5166 if (next != NULL)
5167 next->prev = newsign;
5168#endif
5169
5170 if (prev == NULL)
5171 {
5172 /* When adding first sign need to redraw the windows to create the
5173 * column for signs. */
5174 if (buf->b_signlist == NULL)
5175 {
5176 redraw_buf_later(buf, NOT_VALID);
5177 changed_cline_bef_curs();
5178 }
5179
5180 /* first sign in signlist */
5181 buf->b_signlist = newsign;
5182 }
5183 else
5184 prev->next = newsign;
5185 }
5186}
5187
5188/*
5189 * Add the sign into the signlist. Find the right spot to do it though.
5190 */
5191 void
5192buf_addsign(buf, id, lnum, typenr)
5193 buf_T *buf; /* buffer to store sign in */
5194 int id; /* sign ID */
5195 linenr_T lnum; /* line number which gets the mark */
5196 int typenr; /* typenr of sign we are adding */
5197{
5198 signlist_T *sign; /* a sign in the signlist */
5199 signlist_T *prev; /* the previous sign */
5200
5201 prev = NULL;
5202 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5203 {
5204 if (lnum == sign->lnum && id == sign->id)
5205 {
5206 sign->typenr = typenr;
5207 return;
5208 }
5209 else if (
5210#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5211 id < 0 &&
5212#endif
5213 lnum < sign->lnum)
5214 {
5215#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5216 /* XXX - GRP: Is this because of sign slide problem? Or is it
5217 * really needed? Or is it because we allow multiple signs per
5218 * line? If so, should I add that feature to FEAT_SIGNS?
5219 */
5220 while (prev != NULL && prev->lnum == lnum)
5221 prev = prev->prev;
5222 if (prev == NULL)
5223 sign = buf->b_signlist;
5224 else
5225 sign = prev->next;
5226#endif
5227 insert_sign(buf, prev, sign, id, lnum, typenr);
5228 return;
5229 }
5230 prev = sign;
5231 }
5232#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5233 /* XXX - GRP: See previous comment */
5234 while (prev != NULL && prev->lnum == lnum)
5235 prev = prev->prev;
5236 if (prev == NULL)
5237 sign = buf->b_signlist;
5238 else
5239 sign = prev->next;
5240#endif
5241 insert_sign(buf, prev, sign, id, lnum, typenr);
5242
5243 return;
5244}
5245
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005246 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247buf_change_sign_type(buf, markId, typenr)
5248 buf_T *buf; /* buffer to store sign in */
5249 int markId; /* sign ID */
5250 int typenr; /* typenr of sign we are adding */
5251{
5252 signlist_T *sign; /* a sign in the signlist */
5253
5254 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5255 {
5256 if (sign->id == markId)
5257 {
5258 sign->typenr = typenr;
5259 return sign->lnum;
5260 }
5261 }
5262
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005263 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264}
5265
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005266 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267buf_getsigntype(buf, lnum, type)
5268 buf_T *buf;
5269 linenr_T lnum;
5270 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5271{
5272 signlist_T *sign; /* a sign in a b_signlist */
5273
5274 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5275 if (sign->lnum == lnum
5276 && (type == SIGN_ANY
5277# ifdef FEAT_SIGN_ICONS
5278 || (type == SIGN_ICON
5279 && sign_get_image(sign->typenr) != NULL)
5280# endif
5281 || (type == SIGN_TEXT
5282 && sign_get_text(sign->typenr) != NULL)
5283 || (type == SIGN_LINEHL
5284 && sign_get_attr(sign->typenr, TRUE) != 0)))
5285 return sign->typenr;
5286 return 0;
5287}
5288
5289
5290 linenr_T
5291buf_delsign(buf, id)
5292 buf_T *buf; /* buffer sign is stored in */
5293 int id; /* sign id */
5294{
5295 signlist_T **lastp; /* pointer to pointer to current sign */
5296 signlist_T *sign; /* a sign in a b_signlist */
5297 signlist_T *next; /* the next sign in a b_signlist */
5298 linenr_T lnum; /* line number whose sign was deleted */
5299
5300 lastp = &buf->b_signlist;
5301 lnum = 0;
5302 for (sign = buf->b_signlist; sign != NULL; sign = next)
5303 {
5304 next = sign->next;
5305 if (sign->id == id)
5306 {
5307 *lastp = next;
5308#ifdef FEAT_NETBEANS_INTG
5309 if (next != NULL)
5310 next->prev = sign->prev;
5311#endif
5312 lnum = sign->lnum;
5313 vim_free(sign);
5314 break;
5315 }
5316 else
5317 lastp = &sign->next;
5318 }
5319
5320 /* When deleted the last sign need to redraw the windows to remove the
5321 * sign column. */
5322 if (buf->b_signlist == NULL)
5323 {
5324 redraw_buf_later(buf, NOT_VALID);
5325 changed_cline_bef_curs();
5326 }
5327
5328 return lnum;
5329}
5330
5331
5332/*
5333 * Find the line number of the sign with the requested id. If the sign does
5334 * not exist, return 0 as the line number. This will still let the correct file
5335 * get loaded.
5336 */
5337 int
5338buf_findsign(buf, id)
5339 buf_T *buf; /* buffer to store sign in */
5340 int id; /* sign ID */
5341{
5342 signlist_T *sign; /* a sign in the signlist */
5343
5344 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5345 if (sign->id == id)
5346 return sign->lnum;
5347
5348 return 0;
5349}
5350
5351 int
5352buf_findsign_id(buf, lnum)
5353 buf_T *buf; /* buffer whose sign we are searching for */
5354 linenr_T lnum; /* line number of sign */
5355{
5356 signlist_T *sign; /* a sign in the signlist */
5357
5358 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5359 if (sign->lnum == lnum)
5360 return sign->id;
5361
5362 return 0;
5363}
5364
5365
5366# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5367/* see if a given type of sign exists on a specific line */
5368 int
5369buf_findsigntype_id(buf, lnum, typenr)
5370 buf_T *buf; /* buffer whose sign we are searching for */
5371 linenr_T lnum; /* line number of sign */
5372 int typenr; /* sign type number */
5373{
5374 signlist_T *sign; /* a sign in the signlist */
5375
5376 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5377 if (sign->lnum == lnum && sign->typenr == typenr)
5378 return sign->id;
5379
5380 return 0;
5381}
5382
5383
5384# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5385/* return the number of icons on the given line */
5386 int
5387buf_signcount(buf, lnum)
5388 buf_T *buf;
5389 linenr_T lnum;
5390{
5391 signlist_T *sign; /* a sign in the signlist */
5392 int count = 0;
5393
5394 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5395 if (sign->lnum == lnum)
5396 if (sign_get_image(sign->typenr) != NULL)
5397 count++;
5398
5399 return count;
5400}
5401# endif /* FEAT_SIGN_ICONS */
5402# endif /* FEAT_NETBEANS_INTG */
5403
5404
5405/*
5406 * Delete signs in buffer "buf".
5407 */
5408 static void
5409buf_delete_signs(buf)
5410 buf_T *buf;
5411{
5412 signlist_T *next;
5413
5414 while (buf->b_signlist != NULL)
5415 {
5416 next = buf->b_signlist->next;
5417 vim_free(buf->b_signlist);
5418 buf->b_signlist = next;
5419 }
5420}
5421
5422/*
5423 * Delete all signs in all buffers.
5424 */
5425 void
5426buf_delete_all_signs()
5427{
5428 buf_T *buf; /* buffer we are checking for signs */
5429
5430 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5431 if (buf->b_signlist != NULL)
5432 {
5433 /* Need to redraw the windows to remove the sign column. */
5434 redraw_buf_later(buf, NOT_VALID);
5435 buf_delete_signs(buf);
5436 }
5437}
5438
5439/*
5440 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5441 */
5442 void
5443sign_list_placed(rbuf)
5444 buf_T *rbuf;
5445{
5446 buf_T *buf;
5447 signlist_T *p;
5448 char lbuf[BUFSIZ];
5449
5450 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5451 msg_putchar('\n');
5452 if (rbuf == NULL)
5453 buf = firstbuf;
5454 else
5455 buf = rbuf;
5456 while (buf != NULL)
5457 {
5458 if (buf->b_signlist != NULL)
5459 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005460 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5462 msg_putchar('\n');
5463 }
5464 for (p = buf->b_signlist; p != NULL; p = p->next)
5465 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005466 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5468 MSG_PUTS(lbuf);
5469 msg_putchar('\n');
5470 }
5471 if (rbuf != NULL)
5472 break;
5473 buf = buf->b_next;
5474 }
5475}
5476
5477/*
5478 * Adjust a placed sign for inserted/deleted lines.
5479 */
5480 void
5481sign_mark_adjust(line1, line2, amount, amount_after)
5482 linenr_T line1;
5483 linenr_T line2;
5484 long amount;
5485 long amount_after;
5486{
5487 signlist_T *sign; /* a sign in a b_signlist */
5488
5489 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5490 {
5491 if (sign->lnum >= line1 && sign->lnum <= line2)
5492 {
5493 if (amount == MAXLNUM)
5494 sign->lnum = line1;
5495 else
5496 sign->lnum += amount;
5497 }
5498 else if (sign->lnum > line2)
5499 sign->lnum += amount_after;
5500 }
5501}
5502#endif /* FEAT_SIGNS */
5503
5504/*
5505 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5506 */
5507 void
5508set_buflisted(on)
5509 int on;
5510{
5511 if (on != curbuf->b_p_bl)
5512 {
5513 curbuf->b_p_bl = on;
5514#ifdef FEAT_AUTOCMD
5515 if (on)
5516 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5517 else
5518 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5519#endif
5520 }
5521}
5522
5523/*
5524 * Read the file for "buf" again and check if the contents changed.
5525 * Return TRUE if it changed or this could not be checked.
5526 */
5527 int
5528buf_contents_changed(buf)
5529 buf_T *buf;
5530{
5531 buf_T *newbuf;
5532 int differ = TRUE;
5533 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 exarg_T ea;
5536
5537 /* Allocate a buffer without putting it in the buffer list. */
5538 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5539 if (newbuf == NULL)
5540 return TRUE;
5541
5542 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5543 if (prep_exarg(&ea, buf) == FAIL)
5544 {
5545 wipe_buffer(newbuf, FALSE);
5546 return TRUE;
5547 }
5548
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 /* set curwin/curbuf to buf and save a few things */
5550 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
Bram Moolenaar4770d092006-01-12 23:22:24 +00005552 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 && readfile(buf->b_ffname, buf->b_fname,
5554 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5555 &ea, READ_NEW | READ_DUMMY) == OK)
5556 {
5557 /* compare the two files line by line */
5558 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5559 {
5560 differ = FALSE;
5561 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5562 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5563 {
5564 differ = TRUE;
5565 break;
5566 }
5567 }
5568 }
5569 vim_free(ea.cmd);
5570
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 /* restore curwin/curbuf and a few other things */
5572 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573
5574 if (curbuf != newbuf) /* safety check */
5575 wipe_buffer(newbuf, FALSE);
5576
5577 return differ;
5578}
5579
5580/*
5581 * Wipe out a buffer and decrement the last buffer number if it was used for
5582 * this buffer. Call this to wipe out a temp buffer that does not contain any
5583 * marks.
5584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 void
5586wipe_buffer(buf, aucmd)
5587 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005588 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589{
5590 if (buf->b_fnum == top_file_num - 1)
5591 --top_file_num;
5592
5593#ifdef FEAT_AUTOCMD
5594 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005595 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596#endif
5597 close_buffer(NULL, buf, DOBUF_WIPE);
5598#ifdef FEAT_AUTOCMD
5599 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005600 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601#endif
5602}