vim72-20100325-kaoriya-w64j.zip
[MacVim/KaoriYa.git] / src / buffer.c
blobb94288011b4a57de297e9a12ab0f4c7e6c84be74
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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 */
11 * buffer.c: functions for dealing with the buffer structure
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
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.
25 * The current implementation remembers all file names ever used.
28 #include "vim.h"
30 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
31 static char_u *buflist_match __ARGS((regprog_T *prog, buf_T *buf));
32 # define HAVE_BUFLIST_MATCH
33 static char_u *fname_match __ARGS((regprog_T *prog, char_u *name));
34 #endif
35 static void buflist_setfpos __ARGS((buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options));
36 static wininfo_T *find_wininfo __ARGS((buf_T *buf, int skip_diff_buffer));
37 #ifdef UNIX
38 static buf_T *buflist_findname_stat __ARGS((char_u *ffname, struct stat *st));
39 static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname, struct stat *stp));
40 static int buf_same_ino __ARGS((buf_T *buf, struct stat *stp));
41 #else
42 static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname));
43 #endif
44 #ifdef FEAT_TITLE
45 static int ti_change __ARGS((char_u *str, char_u **last));
46 #endif
47 static int append_arg_number __ARGS((win_T *wp, char_u *buf, int buflen, int add_file));
48 static void free_buffer __ARGS((buf_T *));
49 static void free_buffer_stuff __ARGS((buf_T *buf, int free_options));
50 static void clear_wininfo __ARGS((buf_T *buf));
52 #ifdef UNIX
53 # define dev_T dev_t
54 #else
55 # define dev_T unsigned
56 #endif
58 #if defined(FEAT_SIGNS)
59 static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr));
60 static void buf_delete_signs __ARGS((buf_T *buf));
61 #endif
64 * Open current buffer, that is: open the memfile and read the file into memory
65 * return FAIL for failure, OK otherwise
67 int
68 open_buffer(read_stdin, eap)
69 int read_stdin; /* read file from stdin */
70 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
72 int retval = OK;
73 #ifdef FEAT_AUTOCMD
74 buf_T *old_curbuf;
75 #endif
78 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
79 * When re-entering the same buffer, it should not change, because the
80 * user may have reset the flag by hand.
82 if (readonlymode && curbuf->b_ffname != NULL
83 && (curbuf->b_flags & BF_NEVERLOADED))
84 curbuf->b_p_ro = TRUE;
86 if (ml_open(curbuf) == FAIL)
89 * There MUST be a memfile, otherwise we can't do anything
90 * If we can't create one for the current buffer, take another buffer
92 close_buffer(NULL, curbuf, 0);
93 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
94 if (curbuf->b_ml.ml_mfp != NULL)
95 break;
97 * if there is no memfile at all, exit
98 * This is OK, since there are no changes to lose.
100 if (curbuf == NULL)
102 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
103 getout(2);
105 EMSG(_("E83: Cannot allocate buffer, using other one..."));
106 enter_buffer(curbuf);
107 return FAIL;
110 #ifdef FEAT_AUTOCMD
111 /* The autocommands in readfile() may change the buffer, but only AFTER
112 * reading the file. */
113 old_curbuf = curbuf;
114 modified_was_set = FALSE;
115 #endif
117 /* mark cursor position as being invalid */
118 curwin->w_valid = 0;
120 if (curbuf->b_ffname != NULL
121 #ifdef FEAT_NETBEANS_INTG
122 && netbeansReadFile
123 #endif
126 #ifdef FEAT_NETBEANS_INTG
127 int oldFire = netbeansFireChanges;
129 netbeansFireChanges = 0;
130 #endif
131 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
132 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_NEW);
133 #ifdef FEAT_NETBEANS_INTG
134 netbeansFireChanges = oldFire;
135 #endif
136 /* Help buffer is filtered. */
137 if (curbuf->b_help)
138 fix_help_buffer();
140 else if (read_stdin)
142 int save_bin = curbuf->b_p_bin;
143 linenr_T line_count;
146 * First read the text in binary mode into the buffer.
147 * Then read from that same buffer and append at the end. This makes
148 * it possible to retry when 'fileformat' or 'fileencoding' was
149 * guessed wrong.
151 curbuf->b_p_bin = TRUE;
152 retval = readfile(NULL, NULL, (linenr_T)0,
153 (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW + READ_STDIN);
154 curbuf->b_p_bin = save_bin;
155 if (retval == OK)
157 line_count = curbuf->b_ml.ml_line_count;
158 retval = readfile(NULL, NULL, (linenr_T)line_count,
159 (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_BUFFER);
160 if (retval == OK)
162 /* Delete the binary lines. */
163 while (--line_count >= 0)
164 ml_delete((linenr_T)1, FALSE);
166 else
168 /* Delete the converted lines. */
169 while (curbuf->b_ml.ml_line_count > line_count)
170 ml_delete(line_count, FALSE);
172 /* Put the cursor on the first line. */
173 curwin->w_cursor.lnum = 1;
174 curwin->w_cursor.col = 0;
176 /* Set or reset 'modified' before executing autocommands, so that
177 * it can be changed there. */
178 if (!readonlymode && !bufempty())
179 changed();
180 else if (retval != FAIL)
181 unchanged(curbuf, FALSE);
182 #ifdef FEAT_AUTOCMD
183 # ifdef FEAT_EVAL
184 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
185 curbuf, &retval);
186 # else
187 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
188 # endif
189 #endif
193 /* if first time loading this buffer, init b_chartab[] */
194 if (curbuf->b_flags & BF_NEVERLOADED)
195 (void)buf_init_chartab(curbuf, FALSE);
198 * Set/reset the Changed flag first, autocmds may change the buffer.
199 * Apply the automatic commands, before processing the modelines.
200 * So the modelines have priority over auto commands.
202 /* When reading stdin, the buffer contents always needs writing, so set
203 * the changed flag. Unless in readonly mode: "ls | gview -".
204 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
205 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
206 #ifdef FEAT_AUTOCMD
207 || modified_was_set /* ":set modified" used in autocmd */
208 # ifdef FEAT_EVAL
209 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
210 # endif
211 #endif
213 changed();
214 else if (retval != FAIL && !read_stdin)
215 unchanged(curbuf, FALSE);
216 save_file_ff(curbuf); /* keep this fileformat */
218 /* require "!" to overwrite the file, because it wasn't read completely */
219 #ifdef FEAT_EVAL
220 if (aborting())
221 #else
222 if (got_int)
223 #endif
224 curbuf->b_flags |= BF_READERR;
226 #ifdef FEAT_FOLDING
227 /* Need to update automatic folding. Do this before the autocommands,
228 * they may use the fold info. */
229 foldUpdateAll(curwin);
230 #endif
232 #ifdef FEAT_AUTOCMD
233 /* need to set w_topline, unless some autocommand already did that. */
234 if (!(curwin->w_valid & VALID_TOPLINE))
236 curwin->w_topline = 1;
237 # ifdef FEAT_DIFF
238 curwin->w_topfill = 0;
239 # endif
241 # ifdef FEAT_EVAL
242 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
243 # else
244 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
245 # endif
246 #endif
248 if (retval != FAIL)
250 #ifdef FEAT_AUTOCMD
252 * The autocommands may have changed the current buffer. Apply the
253 * modelines to the correct buffer, if it still exists and is loaded.
255 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
257 aco_save_T aco;
259 /* Go to the buffer that was opened. */
260 aucmd_prepbuf(&aco, old_curbuf);
261 #endif
262 do_modelines(0);
264 /* specified ff and enc, bin in modelines */
265 if (file_ff_differs(curbuf))
267 /* reload buffer */
268 if (eap)
270 /* restore ++ff and ++enc, ++bin if specified */
271 #ifdef FEAT_MBYTE
272 if (eap->force_enc)
274 char_u *fenc = enc_canonize(eap->cmd + eap->force_enc);
275 if (fenc) {
276 vim_free(curbuf->b_p_fenc);
277 curbuf->b_p_fenc = fenc;
280 #endif
281 if (eap->force_ff)
282 set_fileformat(eap->force_ff, OPT_LOCAL);
283 if (eap->force_bin)
284 curbuf->b_p_bin = eap->force_bin;
286 buf_reload(curbuf, curbuf->b_orig_mode);
288 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
290 #ifdef FEAT_AUTOCMD
291 # ifdef FEAT_EVAL
292 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
293 &retval);
294 # else
295 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
296 # endif
298 /* restore curwin/curbuf and a few other things */
299 aucmd_restbuf(&aco);
301 #endif
304 return retval;
308 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
311 buf_valid(buf)
312 buf_T *buf;
314 buf_T *bp;
316 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
317 if (bp == buf)
318 return TRUE;
319 return FALSE;
323 * Close the link to a buffer.
324 * "action" is used when there is no longer a window for the buffer.
325 * It can be:
326 * 0 buffer becomes hidden
327 * DOBUF_UNLOAD buffer is unloaded
328 * DOBUF_DELETE buffer is unloaded and removed from buffer list
329 * DOBUF_WIPE buffer is unloaded and really deleted
330 * When doing all but the first one on the current buffer, the caller should
331 * get a new buffer very soon!
333 * The 'bufhidden' option can force freeing and deleting.
335 void
336 close_buffer(win, buf, action)
337 win_T *win; /* if not NULL, set b_last_cursor */
338 buf_T *buf;
339 int action;
341 #ifdef FEAT_AUTOCMD
342 int is_curbuf;
343 int nwindows;
344 #endif
345 int unload_buf = (action != 0);
346 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
347 int wipe_buf = (action == DOBUF_WIPE);
349 #ifdef FEAT_QUICKFIX
351 * Force unloading or deleting when 'bufhidden' says so.
352 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
353 * "hide" (otherwise we could never free or delete a buffer).
355 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
357 del_buf = TRUE;
358 unload_buf = TRUE;
360 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
362 del_buf = TRUE;
363 unload_buf = TRUE;
364 wipe_buf = TRUE;
366 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
367 unload_buf = TRUE;
368 #endif
370 if (win != NULL)
372 /* Set b_last_cursor when closing the last window for the buffer.
373 * Remember the last cursor position and window options of the buffer.
374 * This used to be only for the current window, but then options like
375 * 'foldmethod' may be lost with a ":only" command. */
376 if (buf->b_nwindows == 1)
377 set_last_cursor(win);
378 buflist_setfpos(buf, win,
379 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
380 win->w_cursor.col, TRUE);
383 #ifdef FEAT_AUTOCMD
384 /* When the buffer is no longer in a window, trigger BufWinLeave */
385 if (buf->b_nwindows == 1)
387 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
388 FALSE, buf);
389 if (!buf_valid(buf)) /* autocommands may delete the buffer */
390 return;
392 /* When the buffer becomes hidden, but is not unloaded, trigger
393 * BufHidden */
394 if (!unload_buf)
396 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
397 FALSE, buf);
398 if (!buf_valid(buf)) /* autocmds may delete the buffer */
399 return;
401 # ifdef FEAT_EVAL
402 if (aborting()) /* autocmds may abort script processing */
403 return;
404 # endif
406 nwindows = buf->b_nwindows;
407 #endif
409 /* decrease the link count from windows (unless not in any window) */
410 if (buf->b_nwindows > 0)
411 --buf->b_nwindows;
413 /* Return when a window is displaying the buffer or when it's not
414 * unloaded. */
415 if (buf->b_nwindows > 0 || !unload_buf)
417 #if 0 /* why was this here? */
418 if (buf == curbuf)
419 u_sync(); /* sync undo before going to another buffer */
420 #endif
421 return;
424 /* Always remove the buffer when there is no file name. */
425 if (buf->b_ffname == NULL)
426 del_buf = TRUE;
429 * Free all things allocated for this buffer.
430 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
432 #ifdef FEAT_AUTOCMD
433 /* Remember if we are closing the current buffer. Restore the number of
434 * windows, so that autocommands in buf_freeall() don't get confused. */
435 is_curbuf = (buf == curbuf);
436 buf->b_nwindows = nwindows;
437 #endif
439 buf_freeall(buf, del_buf, wipe_buf);
441 #ifdef FEAT_AUTOCMD
442 /* Autocommands may have deleted the buffer. */
443 if (!buf_valid(buf))
444 return;
445 # ifdef FEAT_EVAL
446 if (aborting()) /* autocmds may abort script processing */
447 return;
448 # endif
450 /* Autocommands may have opened or closed windows for this buffer.
451 * Decrement the count for the close we do here. */
452 if (buf->b_nwindows > 0)
453 --buf->b_nwindows;
456 * It's possible that autocommands change curbuf to the one being deleted.
457 * This might cause the previous curbuf to be deleted unexpectedly. But
458 * in some cases it's OK to delete the curbuf, because a new one is
459 * obtained anyway. Therefore only return if curbuf changed to the
460 * deleted buffer.
462 if (buf == curbuf && !is_curbuf)
463 return;
464 #endif
466 /* Change directories when the 'acd' option is set. */
467 DO_AUTOCHDIR
470 * Remove the buffer from the list.
472 if (wipe_buf)
474 #ifdef FEAT_SUN_WORKSHOP
475 if (usingSunWorkShop)
476 workshop_file_closed_lineno((char *)buf->b_ffname,
477 (int)buf->b_last_cursor.lnum);
478 #endif
479 vim_free(buf->b_ffname);
480 vim_free(buf->b_sfname);
481 if (buf->b_prev == NULL)
482 firstbuf = buf->b_next;
483 else
484 buf->b_prev->b_next = buf->b_next;
485 if (buf->b_next == NULL)
486 lastbuf = buf->b_prev;
487 else
488 buf->b_next->b_prev = buf->b_prev;
489 free_buffer(buf);
491 else
493 if (del_buf)
495 /* Free all internal variables and reset option values, to make
496 * ":bdel" compatible with Vim 5.7. */
497 free_buffer_stuff(buf, TRUE);
499 /* Make it look like a new buffer. */
500 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
502 /* Init the options when loaded again. */
503 buf->b_p_initialized = FALSE;
505 buf_clear_file(buf);
506 if (del_buf)
507 buf->b_p_bl = FALSE;
512 * Make buffer not contain a file.
514 void
515 buf_clear_file(buf)
516 buf_T *buf;
518 buf->b_ml.ml_line_count = 1;
519 unchanged(buf, TRUE);
520 #ifndef SHORT_FNAME
521 buf->b_shortname = FALSE;
522 #endif
523 buf->b_p_eol = TRUE;
524 buf->b_start_eol = TRUE;
525 #ifdef FEAT_MBYTE
526 buf->b_p_bomb = FALSE;
527 buf->b_start_bomb = FALSE;
528 #endif
529 buf->b_ml.ml_mfp = NULL;
530 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
531 #ifdef FEAT_NETBEANS_INTG
532 netbeans_deleted_all_lines(buf);
533 #endif
537 * buf_freeall() - free all things allocated for a buffer that are related to
538 * the file.
540 void
541 buf_freeall(buf, del_buf, wipe_buf)
542 buf_T *buf;
543 int del_buf UNUSED; /* buffer is going to be deleted */
544 int wipe_buf UNUSED; /* buffer is going to be wiped out */
546 #ifdef FEAT_AUTOCMD
547 int is_curbuf = (buf == curbuf);
549 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
550 if (!buf_valid(buf)) /* autocommands may delete the buffer */
551 return;
552 if (del_buf && buf->b_p_bl)
554 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
555 if (!buf_valid(buf)) /* autocommands may delete the buffer */
556 return;
558 if (wipe_buf)
560 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
561 FALSE, buf);
562 if (!buf_valid(buf)) /* autocommands may delete the buffer */
563 return;
565 # ifdef FEAT_EVAL
566 if (aborting()) /* autocmds may abort script processing */
567 return;
568 # endif
571 * It's possible that autocommands change curbuf to the one being deleted.
572 * This might cause curbuf to be deleted unexpectedly. But in some cases
573 * it's OK to delete the curbuf, because a new one is obtained anyway.
574 * Therefore only return if curbuf changed to the deleted buffer.
576 if (buf == curbuf && !is_curbuf)
577 return;
578 #endif
579 #ifdef FEAT_DIFF
580 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
581 #endif
583 #ifdef FEAT_FOLDING
584 /* No folds in an empty buffer. */
585 # ifdef FEAT_WINDOWS
587 win_T *win;
588 tabpage_T *tp;
590 FOR_ALL_TAB_WINDOWS(tp, win)
591 if (win->w_buffer == buf)
592 clearFolding(win);
594 # else
595 if (curwin->w_buffer == buf)
596 clearFolding(curwin);
597 # endif
598 #endif
600 #ifdef FEAT_TCL
601 tcl_buffer_free(buf);
602 #endif
603 u_blockfree(buf); /* free the memory allocated for undo */
604 ml_close(buf, TRUE); /* close and delete the memline/memfile */
605 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
606 u_clearall(buf); /* reset all undo information */
607 #ifdef FEAT_SYN_HL
608 syntax_clear(buf); /* reset syntax info */
609 #endif
610 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
614 * Free a buffer structure and the things it contains related to the buffer
615 * itself (not the file, that must have been done already).
617 static void
618 free_buffer(buf)
619 buf_T *buf;
621 free_buffer_stuff(buf, TRUE);
622 #ifdef FEAT_MZSCHEME
623 mzscheme_buffer_free(buf);
624 #endif
625 #ifdef FEAT_PERL
626 perl_buf_free(buf);
627 #endif
628 #ifdef FEAT_PYTHON
629 python_buffer_free(buf);
630 #endif
631 #ifdef FEAT_RUBY
632 ruby_buffer_free(buf);
633 #endif
634 #ifdef FEAT_AUTOCMD
635 aubuflocal_remove(buf);
636 #endif
637 vim_free(buf);
641 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
643 static void
644 free_buffer_stuff(buf, free_options)
645 buf_T *buf;
646 int free_options; /* free options as well */
648 if (free_options)
650 clear_wininfo(buf); /* including window-local options */
651 free_buf_options(buf, TRUE);
653 #ifdef FEAT_EVAL
654 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
655 hash_init(&buf->b_vars.dv_hashtab);
656 #endif
657 #ifdef FEAT_USR_CMDS
658 uc_clear(&buf->b_ucmds); /* clear local user commands */
659 #endif
660 #ifdef FEAT_SIGNS
661 buf_delete_signs(buf); /* delete any signs */
662 #endif
663 #ifdef FEAT_NETBEANS_INTG
664 if (usingNetbeans)
665 netbeans_file_killed(buf);
666 #endif
667 #ifdef FEAT_LOCALMAP
668 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
669 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
670 #endif
671 #ifdef FEAT_MBYTE
672 vim_free(buf->b_start_fenc);
673 buf->b_start_fenc = NULL;
674 #endif
675 #ifdef FEAT_SPELL
676 ga_clear(&buf->b_langp);
677 #endif
681 * Free the b_wininfo list for buffer "buf".
683 static void
684 clear_wininfo(buf)
685 buf_T *buf;
687 wininfo_T *wip;
689 while (buf->b_wininfo != NULL)
691 wip = buf->b_wininfo;
692 buf->b_wininfo = wip->wi_next;
693 if (wip->wi_optset)
695 clear_winopt(&wip->wi_opt);
696 #ifdef FEAT_FOLDING
697 deleteFoldRecurse(&wip->wi_folds);
698 #endif
700 vim_free(wip);
704 #if defined(FEAT_LISTCMDS) || defined(PROTO)
706 * Go to another buffer. Handles the result of the ATTENTION dialog.
708 void
709 goto_buffer(eap, start, dir, count)
710 exarg_T *eap;
711 int start;
712 int dir;
713 int count;
715 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
716 buf_T *old_curbuf = curbuf;
718 swap_exists_action = SEA_DIALOG;
719 # endif
720 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
721 start, dir, count, eap->forceit);
722 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
723 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
725 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
726 cleanup_T cs;
728 /* Reset the error/interrupt/exception state here so that
729 * aborting() returns FALSE when closing a window. */
730 enter_cleanup(&cs);
731 # endif
733 /* Quitting means closing the split window, nothing else. */
734 win_close(curwin, TRUE);
735 swap_exists_action = SEA_NONE;
736 swap_exists_did_quit = TRUE;
738 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
739 /* Restore the error/interrupt/exception state if not discarded by a
740 * new aborting error, interrupt, or uncaught exception. */
741 leave_cleanup(&cs);
742 # endif
744 else
745 handle_swap_exists(old_curbuf);
746 # endif
748 #endif
750 #if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
752 * Handle the situation of swap_exists_action being set.
753 * It is allowed for "old_curbuf" to be NULL or invalid.
755 void
756 handle_swap_exists(old_curbuf)
757 buf_T *old_curbuf;
759 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
760 cleanup_T cs;
761 # endif
763 if (swap_exists_action == SEA_QUIT)
765 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
766 /* Reset the error/interrupt/exception state here so that
767 * aborting() returns FALSE when closing a buffer. */
768 enter_cleanup(&cs);
769 # endif
771 /* User selected Quit at ATTENTION prompt. Go back to previous
772 * buffer. If that buffer is gone or the same as the current one,
773 * open a new, empty buffer. */
774 swap_exists_action = SEA_NONE; /* don't want it again */
775 swap_exists_did_quit = TRUE;
776 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
777 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
778 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
779 if (old_curbuf != NULL)
780 enter_buffer(old_curbuf);
781 /* If "old_curbuf" is NULL we are in big trouble here... */
783 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
784 /* Restore the error/interrupt/exception state if not discarded by a
785 * new aborting error, interrupt, or uncaught exception. */
786 leave_cleanup(&cs);
787 # endif
789 else if (swap_exists_action == SEA_RECOVER)
791 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
792 /* Reset the error/interrupt/exception state here so that
793 * aborting() returns FALSE when closing a buffer. */
794 enter_cleanup(&cs);
795 # endif
797 /* User selected Recover at ATTENTION prompt. */
798 msg_scroll = TRUE;
799 ml_recover();
800 MSG_PUTS("\n"); /* don't overwrite the last message */
801 cmdline_row = msg_row;
802 do_modelines(0);
804 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
805 /* Restore the error/interrupt/exception state if not discarded by a
806 * new aborting error, interrupt, or uncaught exception. */
807 leave_cleanup(&cs);
808 # endif
810 swap_exists_action = SEA_NONE;
812 #endif
814 #if defined(FEAT_LISTCMDS) || defined(PROTO)
816 * do_bufdel() - delete or unload buffer(s)
818 * addr_count == 0: ":bdel" - delete current buffer
819 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
820 * buffer "end_bnr", then any other arguments.
821 * addr_count == 2: ":N,N bdel" - delete buffers in range
823 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
824 * DOBUF_DEL (":bdel")
826 * Returns error message or NULL
828 char_u *
829 do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
830 int command;
831 char_u *arg; /* pointer to extra arguments */
832 int addr_count;
833 int start_bnr; /* first buffer number in a range */
834 int end_bnr; /* buffer nr or last buffer nr in a range */
835 int forceit;
837 int do_current = 0; /* delete current buffer? */
838 int deleted = 0; /* number of buffers deleted */
839 char_u *errormsg = NULL; /* return value */
840 int bnr; /* buffer number */
841 char_u *p;
843 if (addr_count == 0)
845 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
847 else
849 if (addr_count == 2)
851 if (*arg) /* both range and argument is not allowed */
852 return (char_u *)_(e_trailing);
853 bnr = start_bnr;
855 else /* addr_count == 1 */
856 bnr = end_bnr;
858 for ( ;!got_int; ui_breakcheck())
861 * delete the current buffer last, otherwise when the
862 * current buffer is deleted, the next buffer becomes
863 * the current one and will be loaded, which may then
864 * also be deleted, etc.
866 if (bnr == curbuf->b_fnum)
867 do_current = bnr;
868 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
869 forceit) == OK)
870 ++deleted;
873 * find next buffer number to delete/unload
875 if (addr_count == 2)
877 if (++bnr > end_bnr)
878 break;
880 else /* addr_count == 1 */
882 arg = skipwhite(arg);
883 if (*arg == NUL)
884 break;
885 if (!VIM_ISDIGIT(*arg))
887 p = skiptowhite_esc(arg);
888 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
889 if (bnr < 0) /* failed */
890 break;
891 arg = p;
893 else
894 bnr = getdigits(&arg);
897 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
898 FORWARD, do_current, forceit) == OK)
899 ++deleted;
901 if (deleted == 0)
903 if (command == DOBUF_UNLOAD)
904 STRCPY(IObuff, _("E515: No buffers were unloaded"));
905 else if (command == DOBUF_DEL)
906 STRCPY(IObuff, _("E516: No buffers were deleted"));
907 else
908 STRCPY(IObuff, _("E517: No buffers were wiped out"));
909 errormsg = IObuff;
911 else if (deleted >= p_report)
913 if (command == DOBUF_UNLOAD)
915 if (deleted == 1)
916 MSG(_("1 buffer unloaded"));
917 else
918 smsg((char_u *)_("%d buffers unloaded"), deleted);
920 else if (command == DOBUF_DEL)
922 if (deleted == 1)
923 MSG(_("1 buffer deleted"));
924 else
925 smsg((char_u *)_("%d buffers deleted"), deleted);
927 else
929 if (deleted == 1)
930 MSG(_("1 buffer wiped out"));
931 else
932 smsg((char_u *)_("%d buffers wiped out"), deleted);
938 return errormsg;
942 * Implementation of the commands for the buffer list.
944 * action == DOBUF_GOTO go to specified buffer
945 * action == DOBUF_SPLIT split window and go to specified buffer
946 * action == DOBUF_UNLOAD unload specified buffer(s)
947 * action == DOBUF_DEL delete specified buffer(s) from buffer list
948 * action == DOBUF_WIPE delete specified buffer(s) really
950 * start == DOBUF_CURRENT go to "count" buffer from current buffer
951 * start == DOBUF_FIRST go to "count" buffer from first buffer
952 * start == DOBUF_LAST go to "count" buffer from last buffer
953 * start == DOBUF_MOD go to "count" modified buffer from current buffer
955 * Return FAIL or OK.
958 do_buffer(action, start, dir, count, forceit)
959 int action;
960 int start;
961 int dir; /* FORWARD or BACKWARD */
962 int count; /* buffer number or number of buffers */
963 int forceit; /* TRUE for :...! */
965 buf_T *buf;
966 buf_T *bp;
967 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
968 || action == DOBUF_WIPE);
970 switch (start)
972 case DOBUF_FIRST: buf = firstbuf; break;
973 case DOBUF_LAST: buf = lastbuf; break;
974 default: buf = curbuf; break;
976 if (start == DOBUF_MOD) /* find next modified buffer */
978 while (count-- > 0)
982 buf = buf->b_next;
983 if (buf == NULL)
984 buf = firstbuf;
986 while (buf != curbuf && !bufIsChanged(buf));
988 if (!bufIsChanged(buf))
990 EMSG(_("E84: No modified buffer found"));
991 return FAIL;
994 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
996 while (buf != NULL && buf->b_fnum != count)
997 buf = buf->b_next;
999 else
1001 bp = NULL;
1002 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1004 /* remember the buffer where we start, we come back there when all
1005 * buffers are unlisted. */
1006 if (bp == NULL)
1007 bp = buf;
1008 if (dir == FORWARD)
1010 buf = buf->b_next;
1011 if (buf == NULL)
1012 buf = firstbuf;
1014 else
1016 buf = buf->b_prev;
1017 if (buf == NULL)
1018 buf = lastbuf;
1020 /* don't count unlisted buffers */
1021 if (unload || buf->b_p_bl)
1023 --count;
1024 bp = NULL; /* use this buffer as new starting point */
1026 if (bp == buf)
1028 /* back where we started, didn't find anything. */
1029 EMSG(_("E85: There is no listed buffer"));
1030 return FAIL;
1035 if (buf == NULL) /* could not find it */
1037 if (start == DOBUF_FIRST)
1039 /* don't warn when deleting */
1040 if (!unload)
1041 EMSGN(_("E86: Buffer %ld does not exist"), count);
1043 else if (dir == FORWARD)
1044 EMSG(_("E87: Cannot go beyond last buffer"));
1045 else
1046 EMSG(_("E88: Cannot go before first buffer"));
1047 return FAIL;
1050 #ifdef FEAT_GUI
1051 need_mouse_correct = TRUE;
1052 #endif
1054 #ifdef FEAT_LISTCMDS
1056 * delete buffer buf from memory and/or the list
1058 if (unload)
1060 int forward;
1061 int retval;
1063 /* When unloading or deleting a buffer that's already unloaded and
1064 * unlisted: fail silently. */
1065 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1066 return FAIL;
1068 if (!forceit && bufIsChanged(buf))
1070 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1071 if ((p_confirm || cmdmod.confirm) && p_write)
1073 dialog_changed(buf, FALSE);
1074 # ifdef FEAT_AUTOCMD
1075 if (!buf_valid(buf))
1076 /* Autocommand deleted buffer, oops! It's not changed
1077 * now. */
1078 return FAIL;
1079 # endif
1080 /* If it's still changed fail silently, the dialog already
1081 * mentioned why it fails. */
1082 if (bufIsChanged(buf))
1083 return FAIL;
1085 else
1086 #endif
1088 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1089 buf->b_fnum);
1090 return FAIL;
1095 * If deleting the last (listed) buffer, make it empty.
1096 * The last (listed) buffer cannot be unloaded.
1098 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1099 if (bp->b_p_bl && bp != buf)
1100 break;
1101 if (bp == NULL && buf == curbuf)
1103 if (action == DOBUF_UNLOAD)
1105 EMSG(_("E90: Cannot unload last buffer"));
1106 return FAIL;
1109 /* Close any other windows on this buffer, then make it empty. */
1110 #ifdef FEAT_WINDOWS
1111 close_windows(buf, TRUE);
1112 #endif
1113 setpcmark();
1114 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1115 forceit ? ECMD_FORCEIT : 0, curwin);
1118 * do_ecmd() may create a new buffer, then we have to delete
1119 * the old one. But do_ecmd() may have done that already, check
1120 * if the buffer still exists.
1122 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1123 close_buffer(NULL, buf, action);
1124 return retval;
1127 #ifdef FEAT_WINDOWS
1129 * If the deleted buffer is the current one, close the current window
1130 * (unless it's the only window). Repeat this so long as we end up in
1131 * a window with this buffer.
1133 while (buf == curbuf
1134 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
1135 win_close(curwin, FALSE);
1136 #endif
1139 * If the buffer to be deleted is not the current one, delete it here.
1141 if (buf != curbuf)
1143 #ifdef FEAT_WINDOWS
1144 close_windows(buf, FALSE);
1145 #endif
1146 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1147 close_buffer(NULL, buf, action);
1148 return OK;
1152 * Deleting the current buffer: Need to find another buffer to go to.
1153 * There must be another, otherwise it would have been handled above.
1154 * First use au_new_curbuf, if it is valid.
1155 * Then prefer the buffer we most recently visited.
1156 * Else try to find one that is loaded, after the current buffer,
1157 * then before the current buffer.
1158 * Finally use any buffer.
1160 buf = NULL; /* selected buffer */
1161 bp = NULL; /* used when no loaded buffer found */
1162 #ifdef FEAT_AUTOCMD
1163 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1164 buf = au_new_curbuf;
1165 # ifdef FEAT_JUMPLIST
1166 else
1167 # endif
1168 #endif
1169 #ifdef FEAT_JUMPLIST
1170 if (curwin->w_jumplistlen > 0)
1172 int jumpidx;
1174 jumpidx = curwin->w_jumplistidx - 1;
1175 if (jumpidx < 0)
1176 jumpidx = curwin->w_jumplistlen - 1;
1178 forward = jumpidx;
1179 while (jumpidx != curwin->w_jumplistidx)
1181 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1182 if (buf != NULL)
1184 if (buf == curbuf || !buf->b_p_bl)
1185 buf = NULL; /* skip current and unlisted bufs */
1186 else if (buf->b_ml.ml_mfp == NULL)
1188 /* skip unloaded buf, but may keep it for later */
1189 if (bp == NULL)
1190 bp = buf;
1191 buf = NULL;
1194 if (buf != NULL) /* found a valid buffer: stop searching */
1195 break;
1196 /* advance to older entry in jump list */
1197 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1198 break;
1199 if (--jumpidx < 0)
1200 jumpidx = curwin->w_jumplistlen - 1;
1201 if (jumpidx == forward) /* List exhausted for sure */
1202 break;
1205 #endif
1207 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1209 forward = TRUE;
1210 buf = curbuf->b_next;
1211 for (;;)
1213 if (buf == NULL)
1215 if (!forward) /* tried both directions */
1216 break;
1217 buf = curbuf->b_prev;
1218 forward = FALSE;
1219 continue;
1221 /* in non-help buffer, try to skip help buffers, and vv */
1222 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1224 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1225 break;
1226 if (bp == NULL) /* remember unloaded buf for later */
1227 bp = buf;
1229 if (forward)
1230 buf = buf->b_next;
1231 else
1232 buf = buf->b_prev;
1235 if (buf == NULL) /* No loaded buffer, use unloaded one */
1236 buf = bp;
1237 if (buf == NULL) /* No loaded buffer, find listed one */
1239 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1240 if (buf->b_p_bl && buf != curbuf)
1241 break;
1243 if (buf == NULL) /* Still no buffer, just take one */
1245 if (curbuf->b_next != NULL)
1246 buf = curbuf->b_next;
1247 else
1248 buf = curbuf->b_prev;
1253 * make buf current buffer
1255 if (action == DOBUF_SPLIT) /* split window first */
1257 # ifdef FEAT_WINDOWS
1258 /* If 'switchbuf' contains "useopen": jump to first window containing
1259 * "buf" if one exists */
1260 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
1261 return OK;
1262 /* If 'switchbuf' contains "usetab": jump to first window in any tab
1263 * page containing "buf" if one exists */
1264 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
1265 return OK;
1266 if (win_split(0, 0) == FAIL)
1267 # endif
1268 return FAIL;
1270 #endif
1272 /* go to current buffer - nothing to do */
1273 if (buf == curbuf)
1274 return OK;
1277 * Check if the current buffer may be abandoned.
1279 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1281 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1282 if ((p_confirm || cmdmod.confirm) && p_write)
1284 dialog_changed(curbuf, FALSE);
1285 # ifdef FEAT_AUTOCMD
1286 if (!buf_valid(buf))
1287 /* Autocommand deleted buffer, oops! */
1288 return FAIL;
1289 # endif
1291 if (bufIsChanged(curbuf))
1292 #endif
1294 EMSG(_(e_nowrtmsg));
1295 return FAIL;
1299 /* Go to the other buffer. */
1300 set_curbuf(buf, action);
1302 #if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1303 if (action == DOBUF_SPLIT)
1304 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1305 #endif
1307 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1308 if (aborting()) /* autocmds may abort script processing */
1309 return FAIL;
1310 #endif
1312 return OK;
1315 #endif /* FEAT_LISTCMDS */
1318 * Set current buffer to "buf". Executes autocommands and closes current
1319 * buffer. "action" tells how to close the current buffer:
1320 * DOBUF_GOTO free or hide it
1321 * DOBUF_SPLIT nothing
1322 * DOBUF_UNLOAD unload it
1323 * DOBUF_DEL delete it
1324 * DOBUF_WIPE wipe it out
1326 void
1327 set_curbuf(buf, action)
1328 buf_T *buf;
1329 int action;
1331 buf_T *prevbuf;
1332 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1333 || action == DOBUF_WIPE);
1335 setpcmark();
1336 if (!cmdmod.keepalt)
1337 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
1338 buflist_altfpos(curwin); /* remember curpos */
1340 #ifdef FEAT_VISUAL
1341 /* Don't restart Select mode after switching to another buffer. */
1342 VIsual_reselect = FALSE;
1343 #endif
1345 /* close_windows() or apply_autocmds() may change curbuf */
1346 prevbuf = curbuf;
1348 #ifdef FEAT_AUTOCMD
1349 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1350 # ifdef FEAT_EVAL
1351 if (buf_valid(prevbuf) && !aborting())
1352 # else
1353 if (buf_valid(prevbuf))
1354 # endif
1355 #endif
1357 #ifdef FEAT_WINDOWS
1358 if (unload)
1359 close_windows(prevbuf, FALSE);
1360 #endif
1361 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1362 if (buf_valid(prevbuf) && !aborting())
1363 #else
1364 if (buf_valid(prevbuf))
1365 #endif
1367 if (prevbuf == curbuf)
1368 u_sync(FALSE);
1369 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1370 unload ? action : (action == DOBUF_GOTO
1371 && !P_HID(prevbuf)
1372 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
1375 #ifdef FEAT_AUTOCMD
1376 /* An autocommand may have deleted "buf", already entered it (e.g., when
1377 * it did ":bunload") or aborted the script processing! */
1378 # ifdef FEAT_EVAL
1379 if (buf_valid(buf) && buf != curbuf && !aborting())
1380 # else
1381 if (buf_valid(buf) && buf != curbuf)
1382 # endif
1383 #endif
1384 enter_buffer(buf);
1388 * Enter a new current buffer.
1389 * Old curbuf must have been abandoned already!
1391 void
1392 enter_buffer(buf)
1393 buf_T *buf;
1395 /* Copy buffer and window local option values. Not for a help buffer. */
1396 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1397 if (!buf->b_help)
1398 get_winopts(buf);
1399 #ifdef FEAT_FOLDING
1400 else
1401 /* Remove all folds in the window. */
1402 clearFolding(curwin);
1403 foldUpdateAll(curwin); /* update folds (later). */
1404 #endif
1406 /* Get the buffer in the current window. */
1407 curwin->w_buffer = buf;
1408 curbuf = buf;
1409 ++curbuf->b_nwindows;
1411 #ifdef FEAT_DIFF
1412 if (curwin->w_p_diff)
1413 diff_buf_add(curbuf);
1414 #endif
1416 /* Cursor on first line by default. */
1417 curwin->w_cursor.lnum = 1;
1418 curwin->w_cursor.col = 0;
1419 #ifdef FEAT_VIRTUALEDIT
1420 curwin->w_cursor.coladd = 0;
1421 #endif
1422 curwin->w_set_curswant = TRUE;
1423 #ifdef FEAT_AUTOCMD
1424 curwin->w_topline_was_set = FALSE;
1425 #endif
1427 /* mark cursor position as being invalid */
1428 curwin->w_valid = 0;
1430 /* Make sure the buffer is loaded. */
1431 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
1433 #ifdef FEAT_AUTOCMD
1434 /* If there is no filetype, allow for detecting one. Esp. useful for
1435 * ":ball" used in a autocommand. If there already is a filetype we
1436 * might prefer to keep it. */
1437 if (*curbuf->b_p_ft == NUL)
1438 did_filetype = FALSE;
1439 #endif
1441 open_buffer(FALSE, NULL);
1443 else
1445 if (!msg_silent)
1446 need_fileinfo = TRUE; /* display file info after redraw */
1447 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1448 #ifdef FEAT_AUTOCMD
1449 curwin->w_topline = 1;
1450 # ifdef FEAT_DIFF
1451 curwin->w_topfill = 0;
1452 # endif
1453 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1454 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1455 #endif
1458 /* If autocommands did not change the cursor position, restore cursor lnum
1459 * and possibly cursor col. */
1460 if (curwin->w_cursor.lnum == 1 && inindent(0))
1461 buflist_getfpos();
1463 check_arg_idx(curwin); /* check for valid arg_idx */
1464 #ifdef FEAT_TITLE
1465 maketitle();
1466 #endif
1467 #ifdef FEAT_AUTOCMD
1468 /* when autocmds didn't change it */
1469 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
1470 #endif
1471 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1473 #ifdef FEAT_NETBEANS_INTG
1474 /* Send fileOpened event because we've changed buffers. */
1475 if (usingNetbeans && isNetbeansBuffer(curbuf))
1476 netbeans_file_activated(curbuf);
1477 #endif
1479 /* Change directories when the 'acd' option is set. */
1480 DO_AUTOCHDIR
1482 #ifdef FEAT_KEYMAP
1483 if (curbuf->b_kmap_state & KEYMAP_INIT)
1484 (void)keymap_init();
1485 #endif
1486 #ifdef FEAT_SPELL
1487 /* May need to set the spell language. Can only do this after the buffer
1488 * has been properly setup. */
1489 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
1490 (void)did_set_spelllang(curbuf);
1491 #endif
1493 redraw_later(NOT_VALID);
1496 #if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1498 * Change to the directory of the current buffer.
1500 void
1501 do_autochdir()
1503 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1504 shorten_fnames(TRUE);
1506 #endif
1509 * functions for dealing with the buffer list
1513 * Add a file name to the buffer list. Return a pointer to the buffer.
1514 * If the same file name already exists return a pointer to that buffer.
1515 * If it does not exist, or if fname == NULL, a new entry is created.
1516 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1517 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1518 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1519 * This is the ONLY way to create a new buffer.
1521 static int top_file_num = 1; /* highest file number */
1523 buf_T *
1524 buflist_new(ffname, sfname, lnum, flags)
1525 char_u *ffname; /* full path of fname or relative */
1526 char_u *sfname; /* short fname or NULL */
1527 linenr_T lnum; /* preferred cursor line */
1528 int flags; /* BLN_ defines */
1530 buf_T *buf;
1531 #ifdef UNIX
1532 struct stat st;
1533 #endif
1535 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1538 * If file name already exists in the list, update the entry.
1540 #ifdef UNIX
1541 /* On Unix we can use inode numbers when the file exists. Works better
1542 * for hard links. */
1543 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1544 st.st_dev = (dev_T)-1;
1545 #endif
1546 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1547 #ifdef UNIX
1548 buflist_findname_stat(ffname, &st)
1549 #else
1550 buflist_findname(ffname)
1551 #endif
1552 ) != NULL)
1554 vim_free(ffname);
1555 if (lnum != 0)
1556 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1557 /* copy the options now, if 'cpo' doesn't have 's' and not done
1558 * already */
1559 buf_copy_options(buf, 0);
1560 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1562 buf->b_p_bl = TRUE;
1563 #ifdef FEAT_AUTOCMD
1564 if (!(flags & BLN_DUMMY))
1565 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1566 #endif
1568 return buf;
1572 * If the current buffer has no name and no contents, use the current
1573 * buffer. Otherwise: Need to allocate a new buffer structure.
1575 * This is the ONLY place where a new buffer structure is allocated!
1576 * (A spell file buffer is allocated in spell.c, but that's not a normal
1577 * buffer.)
1579 buf = NULL;
1580 if ((flags & BLN_CURBUF)
1581 && curbuf != NULL
1582 && curbuf->b_ffname == NULL
1583 && curbuf->b_nwindows <= 1
1584 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1586 buf = curbuf;
1587 #ifdef FEAT_AUTOCMD
1588 /* It's like this buffer is deleted. Watch out for autocommands that
1589 * change curbuf! If that happens, allocate a new buffer anyway. */
1590 if (curbuf->b_p_bl)
1591 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1592 if (buf == curbuf)
1593 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1594 # ifdef FEAT_EVAL
1595 if (aborting()) /* autocmds may abort script processing */
1596 return NULL;
1597 # endif
1598 #endif
1599 #ifdef FEAT_QUICKFIX
1600 # ifdef FEAT_AUTOCMD
1601 if (buf == curbuf)
1602 # endif
1604 /* Make sure 'bufhidden' and 'buftype' are empty */
1605 clear_string_option(&buf->b_p_bh);
1606 clear_string_option(&buf->b_p_bt);
1608 #endif
1610 if (buf != curbuf || curbuf == NULL)
1612 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1613 if (buf == NULL)
1615 vim_free(ffname);
1616 return NULL;
1620 if (ffname != NULL)
1622 buf->b_ffname = ffname;
1623 buf->b_sfname = vim_strsave(sfname);
1626 clear_wininfo(buf);
1627 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1629 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1630 || buf->b_wininfo == NULL)
1632 vim_free(buf->b_ffname);
1633 buf->b_ffname = NULL;
1634 vim_free(buf->b_sfname);
1635 buf->b_sfname = NULL;
1636 if (buf != curbuf)
1637 free_buffer(buf);
1638 return NULL;
1641 if (buf == curbuf)
1643 /* free all things allocated for this buffer */
1644 buf_freeall(buf, FALSE, FALSE);
1645 if (buf != curbuf) /* autocommands deleted the buffer! */
1646 return NULL;
1647 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1648 if (aborting()) /* autocmds may abort script processing */
1649 return NULL;
1650 #endif
1651 /* buf->b_nwindows = 0; why was this here? */
1652 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1653 #ifdef FEAT_KEYMAP
1654 /* need to reload lmaps and set b:keymap_name */
1655 curbuf->b_kmap_state |= KEYMAP_INIT;
1656 #endif
1658 else
1661 * put new buffer at the end of the buffer list
1663 buf->b_next = NULL;
1664 if (firstbuf == NULL) /* buffer list is empty */
1666 buf->b_prev = NULL;
1667 firstbuf = buf;
1669 else /* append new buffer at end of list */
1671 lastbuf->b_next = buf;
1672 buf->b_prev = lastbuf;
1674 lastbuf = buf;
1676 buf->b_fnum = top_file_num++;
1677 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1679 EMSG(_("W14: Warning: List of file names overflow"));
1680 if (emsg_silent == 0)
1682 out_flush();
1683 ui_delay(3000L, TRUE); /* make sure it is noticed */
1685 top_file_num = 1;
1689 * Always copy the options from the current buffer.
1691 buf_copy_options(buf, BCO_ALWAYS);
1694 buf->b_wininfo->wi_fpos.lnum = lnum;
1695 buf->b_wininfo->wi_win = curwin;
1697 #ifdef FEAT_EVAL
1698 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1699 #endif
1700 #ifdef FEAT_SYN_HL
1701 hash_init(&buf->b_keywtab);
1702 hash_init(&buf->b_keywtab_ic);
1703 #endif
1705 buf->b_fname = buf->b_sfname;
1706 #ifdef UNIX
1707 if (st.st_dev == (dev_T)-1)
1708 buf->b_dev_valid = FALSE;
1709 else
1711 buf->b_dev_valid = TRUE;
1712 buf->b_dev = st.st_dev;
1713 buf->b_ino = st.st_ino;
1715 #endif
1716 buf->b_u_synced = TRUE;
1717 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
1718 if (flags & BLN_DUMMY)
1719 buf->b_flags |= BF_DUMMY;
1720 buf_clear_file(buf);
1721 clrallmarks(buf); /* clear marks */
1722 fmarks_check_names(buf); /* check file marks for this file */
1723 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1724 #ifdef FEAT_AUTOCMD
1725 if (!(flags & BLN_DUMMY))
1727 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1728 if (flags & BLN_LISTED)
1729 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1730 # ifdef FEAT_EVAL
1731 if (aborting()) /* autocmds may abort script processing */
1732 return NULL;
1733 # endif
1735 #endif
1737 return buf;
1741 * Free the memory for the options of a buffer.
1742 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1743 * 'fileencoding'.
1745 void
1746 free_buf_options(buf, free_p_ff)
1747 buf_T *buf;
1748 int free_p_ff;
1750 if (free_p_ff)
1752 #ifdef FEAT_MBYTE
1753 clear_string_option(&buf->b_p_fenc);
1754 #endif
1755 clear_string_option(&buf->b_p_ff);
1756 #ifdef FEAT_QUICKFIX
1757 clear_string_option(&buf->b_p_bh);
1758 clear_string_option(&buf->b_p_bt);
1759 #endif
1761 #ifdef FEAT_FIND_ID
1762 clear_string_option(&buf->b_p_def);
1763 clear_string_option(&buf->b_p_inc);
1764 # ifdef FEAT_EVAL
1765 clear_string_option(&buf->b_p_inex);
1766 # endif
1767 #endif
1768 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1769 clear_string_option(&buf->b_p_inde);
1770 clear_string_option(&buf->b_p_indk);
1771 #endif
1772 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1773 clear_string_option(&buf->b_p_bexpr);
1774 #endif
1775 #if defined(FEAT_EVAL)
1776 clear_string_option(&buf->b_p_fex);
1777 #endif
1778 #ifdef FEAT_CRYPT
1779 clear_string_option(&buf->b_p_key);
1780 #endif
1781 clear_string_option(&buf->b_p_kp);
1782 clear_string_option(&buf->b_p_mps);
1783 clear_string_option(&buf->b_p_fo);
1784 clear_string_option(&buf->b_p_flp);
1785 clear_string_option(&buf->b_p_isk);
1786 #ifdef FEAT_KEYMAP
1787 clear_string_option(&buf->b_p_keymap);
1788 ga_clear(&buf->b_kmap_ga);
1789 #endif
1790 #ifdef FEAT_COMMENTS
1791 clear_string_option(&buf->b_p_com);
1792 #endif
1793 #ifdef FEAT_FOLDING
1794 clear_string_option(&buf->b_p_cms);
1795 #endif
1796 clear_string_option(&buf->b_p_nf);
1797 #ifdef FEAT_SYN_HL
1798 clear_string_option(&buf->b_p_syn);
1799 #endif
1800 #ifdef FEAT_SPELL
1801 clear_string_option(&buf->b_p_spc);
1802 clear_string_option(&buf->b_p_spf);
1803 vim_free(buf->b_cap_prog);
1804 buf->b_cap_prog = NULL;
1805 clear_string_option(&buf->b_p_spl);
1806 #endif
1807 #ifdef FEAT_SEARCHPATH
1808 clear_string_option(&buf->b_p_sua);
1809 #endif
1810 #ifdef FEAT_AUTOCMD
1811 clear_string_option(&buf->b_p_ft);
1812 #endif
1813 #ifdef FEAT_OSFILETYPE
1814 clear_string_option(&buf->b_p_oft);
1815 #endif
1816 #ifdef FEAT_CINDENT
1817 clear_string_option(&buf->b_p_cink);
1818 clear_string_option(&buf->b_p_cino);
1819 #endif
1820 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1821 clear_string_option(&buf->b_p_cinw);
1822 #endif
1823 #ifdef FEAT_INS_EXPAND
1824 clear_string_option(&buf->b_p_cpt);
1825 #endif
1826 #ifdef FEAT_COMPL_FUNC
1827 clear_string_option(&buf->b_p_cfu);
1828 clear_string_option(&buf->b_p_ofu);
1829 #endif
1830 #ifdef FEAT_QUICKFIX
1831 clear_string_option(&buf->b_p_gp);
1832 clear_string_option(&buf->b_p_mp);
1833 clear_string_option(&buf->b_p_efm);
1834 #endif
1835 clear_string_option(&buf->b_p_ep);
1836 clear_string_option(&buf->b_p_path);
1837 clear_string_option(&buf->b_p_tags);
1838 #ifdef FEAT_INS_EXPAND
1839 clear_string_option(&buf->b_p_dict);
1840 clear_string_option(&buf->b_p_tsr);
1841 #endif
1842 #ifdef FEAT_TEXTOBJ
1843 clear_string_option(&buf->b_p_qe);
1844 #endif
1845 buf->b_p_ar = -1;
1849 * get alternate file n
1850 * set linenr to lnum or altfpos.lnum if lnum == 0
1851 * also set cursor column to altfpos.col if 'startofline' is not set.
1852 * if (options & GETF_SETMARK) call setpcmark()
1853 * if (options & GETF_ALT) we are jumping to an alternate file.
1854 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1856 * return FAIL for failure, OK for success
1859 buflist_getfile(n, lnum, options, forceit)
1860 int n;
1861 linenr_T lnum;
1862 int options;
1863 int forceit;
1865 buf_T *buf;
1866 #ifdef FEAT_WINDOWS
1867 win_T *wp = NULL;
1868 #endif
1869 pos_T *fpos;
1870 colnr_T col;
1872 buf = buflist_findnr(n);
1873 if (buf == NULL)
1875 if ((options & GETF_ALT) && n == 0)
1876 EMSG(_(e_noalt));
1877 else
1878 EMSGN(_("E92: Buffer %ld not found"), n);
1879 return FAIL;
1882 /* if alternate file is the current buffer, nothing to do */
1883 if (buf == curbuf)
1884 return OK;
1886 if (text_locked())
1888 text_locked_msg();
1889 return FAIL;
1891 #ifdef FEAT_AUTOCMD
1892 if (curbuf_locked())
1893 return FAIL;
1894 #endif
1896 /* altfpos may be changed by getfile(), get it now */
1897 if (lnum == 0)
1899 fpos = buflist_findfpos(buf);
1900 lnum = fpos->lnum;
1901 col = fpos->col;
1903 else
1904 col = 0;
1906 #ifdef FEAT_WINDOWS
1907 if (options & GETF_SWITCH)
1909 /* If 'switchbuf' contains "useopen": jump to first window containing
1910 * "buf" if one exists */
1911 if (swb_flags & SWB_USEOPEN)
1912 wp = buf_jump_open_win(buf);
1913 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1914 * page containing "buf" if one exists */
1915 if (wp == NULL && (swb_flags & SWB_USETAB))
1916 wp = buf_jump_open_tab(buf);
1917 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1918 * isn't empty: open new window */
1919 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
1921 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1922 tabpage_new();
1923 else if (win_split(0, 0) == FAIL) /* Open in a new window */
1924 return FAIL;
1925 # ifdef FEAT_SCROLLBIND
1926 curwin->w_p_scb = FALSE;
1927 # endif
1930 #endif
1932 ++RedrawingDisabled;
1933 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1934 lnum, forceit) <= 0)
1936 --RedrawingDisabled;
1938 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1939 if (!p_sol && col != 0)
1941 curwin->w_cursor.col = col;
1942 check_cursor_col();
1943 #ifdef FEAT_VIRTUALEDIT
1944 curwin->w_cursor.coladd = 0;
1945 #endif
1946 curwin->w_set_curswant = TRUE;
1948 return OK;
1950 --RedrawingDisabled;
1951 return FAIL;
1955 * go to the last know line number for the current buffer
1957 void
1958 buflist_getfpos()
1960 pos_T *fpos;
1962 fpos = buflist_findfpos(curbuf);
1964 curwin->w_cursor.lnum = fpos->lnum;
1965 check_cursor_lnum();
1967 if (p_sol)
1968 curwin->w_cursor.col = 0;
1969 else
1971 curwin->w_cursor.col = fpos->col;
1972 check_cursor_col();
1973 #ifdef FEAT_VIRTUALEDIT
1974 curwin->w_cursor.coladd = 0;
1975 #endif
1976 curwin->w_set_curswant = TRUE;
1980 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1982 * Find file in buffer list by name (it has to be for the current window).
1983 * Returns NULL if not found.
1985 buf_T *
1986 buflist_findname_exp(fname)
1987 char_u *fname;
1989 char_u *ffname;
1990 buf_T *buf = NULL;
1992 /* First make the name into a full path name */
1993 ffname = FullName_save(fname,
1994 #ifdef UNIX
1995 TRUE /* force expansion, get rid of symbolic links */
1996 #else
1997 FALSE
1998 #endif
2000 if (ffname != NULL)
2002 buf = buflist_findname(ffname);
2003 vim_free(ffname);
2005 return buf;
2007 #endif
2010 * Find file in buffer list by name (it has to be for the current window).
2011 * "ffname" must have a full path.
2012 * Skips dummy buffers.
2013 * Returns NULL if not found.
2015 buf_T *
2016 buflist_findname(ffname)
2017 char_u *ffname;
2019 #ifdef UNIX
2020 struct stat st;
2022 if (mch_stat((char *)ffname, &st) < 0)
2023 st.st_dev = (dev_T)-1;
2024 return buflist_findname_stat(ffname, &st);
2028 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2029 * twice for the same file.
2030 * Returns NULL if not found.
2032 static buf_T *
2033 buflist_findname_stat(ffname, stp)
2034 char_u *ffname;
2035 struct stat *stp;
2037 #endif
2038 buf_T *buf;
2040 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2041 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
2042 #ifdef UNIX
2043 , stp
2044 #endif
2046 return buf;
2047 return NULL;
2050 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2052 * Find file in buffer list by a regexp pattern.
2053 * Return fnum of the found buffer.
2054 * Return < 0 for error.
2057 buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2058 char_u *pattern;
2059 char_u *pattern_end; /* pointer to first char after pattern */
2060 int unlisted; /* find unlisted buffers */
2061 int diffmode UNUSED; /* find diff-mode buffers only */
2063 buf_T *buf;
2064 regprog_T *prog;
2065 int match = -1;
2066 int find_listed;
2067 char_u *pat;
2068 char_u *patend;
2069 int attempt;
2070 char_u *p;
2071 int toggledollar;
2073 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2075 if (*pattern == '%')
2076 match = curbuf->b_fnum;
2077 else
2078 match = curwin->w_alt_fnum;
2079 #ifdef FEAT_DIFF
2080 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2081 match = -1;
2082 #endif
2086 * Try four ways of matching a listed buffer:
2087 * attempt == 0: without '^' or '$' (at any position)
2088 * attempt == 1: with '^' at start (only at position 0)
2089 * attempt == 2: with '$' at end (only match at end)
2090 * attempt == 3: with '^' at start and '$' at end (only full match)
2091 * Repeat this for finding an unlisted buffer if there was no matching
2092 * listed buffer.
2094 else
2096 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2097 if (pat == NULL)
2098 return -1;
2099 patend = pat + STRLEN(pat) - 1;
2100 toggledollar = (patend > pat && *patend == '$');
2102 /* First try finding a listed buffer. If not found and "unlisted"
2103 * is TRUE, try finding an unlisted buffer. */
2104 find_listed = TRUE;
2105 for (;;)
2107 for (attempt = 0; attempt <= 3; ++attempt)
2109 /* may add '^' and '$' */
2110 if (toggledollar)
2111 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2112 p = pat;
2113 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2114 ++p;
2115 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2116 if (prog == NULL)
2118 vim_free(pat);
2119 return -1;
2122 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2123 if (buf->b_p_bl == find_listed
2124 #ifdef FEAT_DIFF
2125 && (!diffmode || diff_mode_buf(buf))
2126 #endif
2127 && buflist_match(prog, buf) != NULL)
2129 if (match >= 0) /* already found a match */
2131 match = -2;
2132 break;
2134 match = buf->b_fnum; /* remember first match */
2137 vim_free(prog);
2138 if (match >= 0) /* found one match */
2139 break;
2142 /* Only search for unlisted buffers if there was no match with
2143 * a listed buffer. */
2144 if (!unlisted || !find_listed || match != -1)
2145 break;
2146 find_listed = FALSE;
2149 vim_free(pat);
2152 if (match == -2)
2153 EMSG2(_("E93: More than one match for %s"), pattern);
2154 else if (match < 0)
2155 EMSG2(_("E94: No matching buffer for %s"), pattern);
2156 return match;
2158 #endif
2160 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2163 * Find all buffer names that match.
2164 * For command line expansion of ":buf" and ":sbuf".
2165 * Return OK if matches found, FAIL otherwise.
2168 ExpandBufnames(pat, num_file, file, options)
2169 char_u *pat;
2170 int *num_file;
2171 char_u ***file;
2172 int options;
2174 int count = 0;
2175 buf_T *buf;
2176 int round;
2177 char_u *p;
2178 int attempt;
2179 regprog_T *prog;
2180 char_u *patc;
2182 *num_file = 0; /* return values in case of FAIL */
2183 *file = NULL;
2185 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2186 if (*pat == '^')
2188 patc = alloc((unsigned)STRLEN(pat) + 11);
2189 if (patc == NULL)
2190 return FAIL;
2191 STRCPY(patc, "\\(^\\|[\\/]\\)");
2192 STRCPY(patc + 11, pat + 1);
2194 else
2195 patc = pat;
2198 * attempt == 0: try match with '\<', match at start of word
2199 * attempt == 1: try match without '\<', match anywhere
2201 for (attempt = 0; attempt <= 1; ++attempt)
2203 if (attempt > 0 && patc == pat)
2204 break; /* there was no anchor, no need to try again */
2205 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2206 if (prog == NULL)
2208 if (patc != pat)
2209 vim_free(patc);
2210 return FAIL;
2214 * round == 1: Count the matches.
2215 * round == 2: Build the array to keep the matches.
2217 for (round = 1; round <= 2; ++round)
2219 count = 0;
2220 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2222 if (!buf->b_p_bl) /* skip unlisted buffers */
2223 continue;
2224 p = buflist_match(prog, buf);
2225 if (p != NULL)
2227 if (round == 1)
2228 ++count;
2229 else
2231 if (options & WILD_HOME_REPLACE)
2232 p = home_replace_save(buf, p);
2233 else
2234 p = vim_strsave(p);
2235 (*file)[count++] = p;
2239 if (count == 0) /* no match found, break here */
2240 break;
2241 if (round == 1)
2243 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2244 if (*file == NULL)
2246 vim_free(prog);
2247 if (patc != pat)
2248 vim_free(patc);
2249 return FAIL;
2253 vim_free(prog);
2254 if (count) /* match(es) found, break here */
2255 break;
2258 if (patc != pat)
2259 vim_free(patc);
2261 *num_file = count;
2262 return (count == 0 ? FAIL : OK);
2265 #endif /* FEAT_CMDL_COMPL */
2267 #ifdef HAVE_BUFLIST_MATCH
2269 * Check for a match on the file name for buffer "buf" with regprog "prog".
2271 static char_u *
2272 buflist_match(prog, buf)
2273 regprog_T *prog;
2274 buf_T *buf;
2276 char_u *match;
2278 /* First try the short file name, then the long file name. */
2279 match = fname_match(prog, buf->b_sfname);
2280 if (match == NULL)
2281 match = fname_match(prog, buf->b_ffname);
2283 return match;
2287 * Try matching the regexp in "prog" with file name "name".
2288 * Return "name" when there is a match, NULL when not.
2290 static char_u *
2291 fname_match(prog, name)
2292 regprog_T *prog;
2293 char_u *name;
2295 char_u *match = NULL;
2296 char_u *p;
2297 regmatch_T regmatch;
2299 if (name != NULL)
2301 regmatch.regprog = prog;
2302 #ifdef CASE_INSENSITIVE_FILENAME
2303 regmatch.rm_ic = TRUE; /* Always ignore case */
2304 #else
2305 regmatch.rm_ic = FALSE; /* Never ignore case */
2306 #endif
2308 if (vim_regexec(&regmatch, name, (colnr_T)0))
2309 match = name;
2310 else
2312 /* Replace $(HOME) with '~' and try matching again. */
2313 p = home_replace_save(NULL, name);
2314 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2315 match = name;
2316 vim_free(p);
2320 return match;
2322 #endif
2325 * find file in buffer list by number
2327 buf_T *
2328 buflist_findnr(nr)
2329 int nr;
2331 buf_T *buf;
2333 if (nr == 0)
2334 nr = curwin->w_alt_fnum;
2335 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2336 if (buf->b_fnum == nr)
2337 return (buf);
2338 return NULL;
2342 * Get name of file 'n' in the buffer list.
2343 * When the file has no name an empty string is returned.
2344 * home_replace() is used to shorten the file name (used for marks).
2345 * Returns a pointer to allocated memory, of NULL when failed.
2347 char_u *
2348 buflist_nr2name(n, fullname, helptail)
2349 int n;
2350 int fullname;
2351 int helptail; /* for help buffers return tail only */
2353 buf_T *buf;
2355 buf = buflist_findnr(n);
2356 if (buf == NULL)
2357 return NULL;
2358 return home_replace_save(helptail ? buf : NULL,
2359 fullname ? buf->b_ffname : buf->b_fname);
2363 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2364 * When "copy_options" is TRUE save the local window option values.
2365 * When "lnum" is 0 only do the options.
2367 static void
2368 buflist_setfpos(buf, win, lnum, col, copy_options)
2369 buf_T *buf;
2370 win_T *win;
2371 linenr_T lnum;
2372 colnr_T col;
2373 int copy_options;
2375 wininfo_T *wip;
2377 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2378 if (wip->wi_win == win)
2379 break;
2380 if (wip == NULL)
2382 /* allocate a new entry */
2383 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2384 if (wip == NULL)
2385 return;
2386 wip->wi_win = win;
2387 if (lnum == 0) /* set lnum even when it's 0 */
2388 lnum = 1;
2390 else
2392 /* remove the entry from the list */
2393 if (wip->wi_prev)
2394 wip->wi_prev->wi_next = wip->wi_next;
2395 else
2396 buf->b_wininfo = wip->wi_next;
2397 if (wip->wi_next)
2398 wip->wi_next->wi_prev = wip->wi_prev;
2399 if (copy_options && wip->wi_optset)
2401 clear_winopt(&wip->wi_opt);
2402 #ifdef FEAT_FOLDING
2403 deleteFoldRecurse(&wip->wi_folds);
2404 #endif
2407 if (lnum != 0)
2409 wip->wi_fpos.lnum = lnum;
2410 wip->wi_fpos.col = col;
2412 if (copy_options)
2414 /* Save the window-specific option values. */
2415 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2416 #ifdef FEAT_FOLDING
2417 wip->wi_fold_manual = win->w_fold_manual;
2418 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2419 #endif
2420 wip->wi_optset = TRUE;
2423 /* insert the entry in front of the list */
2424 wip->wi_next = buf->b_wininfo;
2425 buf->b_wininfo = wip;
2426 wip->wi_prev = NULL;
2427 if (wip->wi_next)
2428 wip->wi_next->wi_prev = wip;
2430 return;
2433 #ifdef FEAT_DIFF
2434 static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2437 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2438 * page. That's because a diff is local to a tab page.
2440 static int
2441 wininfo_other_tab_diff(wip)
2442 wininfo_T *wip;
2444 win_T *wp;
2446 if (wip->wi_opt.wo_diff)
2448 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2449 /* return FALSE when it's a window in the current tab page, thus
2450 * the buffer was in diff mode here */
2451 if (wip->wi_win == wp)
2452 return FALSE;
2453 return TRUE;
2455 return FALSE;
2457 #endif
2460 * Find info for the current window in buffer "buf".
2461 * If not found, return the info for the most recently used window.
2462 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2463 * another tab page.
2464 * Returns NULL when there isn't any info.
2466 static wininfo_T *
2467 find_wininfo(buf, skip_diff_buffer)
2468 buf_T *buf;
2469 int skip_diff_buffer UNUSED;
2471 wininfo_T *wip;
2473 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2474 if (wip->wi_win == curwin
2475 #ifdef FEAT_DIFF
2476 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2477 #endif
2479 break;
2481 /* If no wininfo for curwin, use the first in the list (that doesn't have
2482 * 'diff' set and is in another tab page). */
2483 if (wip == NULL)
2485 #ifdef FEAT_DIFF
2486 if (skip_diff_buffer)
2488 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2489 if (!wininfo_other_tab_diff(wip))
2490 break;
2492 else
2493 #endif
2494 wip = buf->b_wininfo;
2496 return wip;
2500 * Reset the local window options to the values last used in this window.
2501 * If the buffer wasn't used in this window before, use the values from
2502 * the most recently used window. If the values were never set, use the
2503 * global values for the window.
2505 void
2506 get_winopts(buf)
2507 buf_T *buf;
2509 wininfo_T *wip;
2511 clear_winopt(&curwin->w_onebuf_opt);
2512 #ifdef FEAT_FOLDING
2513 clearFolding(curwin);
2514 #endif
2516 wip = find_wininfo(buf, TRUE);
2517 if (wip != NULL && wip->wi_optset)
2519 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2520 #ifdef FEAT_FOLDING
2521 curwin->w_fold_manual = wip->wi_fold_manual;
2522 curwin->w_foldinvalid = TRUE;
2523 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2524 #endif
2526 else
2527 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2529 #ifdef FEAT_FOLDING
2530 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2531 if (p_fdls >= 0)
2532 curwin->w_p_fdl = p_fdls;
2533 #endif
2537 * Find the position (lnum and col) for the buffer 'buf' for the current
2538 * window.
2539 * Returns a pointer to no_position if no position is found.
2541 pos_T *
2542 buflist_findfpos(buf)
2543 buf_T *buf;
2545 wininfo_T *wip;
2546 static pos_T no_position = INIT_POS_T(1, 0, 0);
2548 wip = find_wininfo(buf, FALSE);
2549 if (wip != NULL)
2550 return &(wip->wi_fpos);
2551 else
2552 return &no_position;
2556 * Find the lnum for the buffer 'buf' for the current window.
2558 linenr_T
2559 buflist_findlnum(buf)
2560 buf_T *buf;
2562 return buflist_findfpos(buf)->lnum;
2565 #if defined(FEAT_LISTCMDS) || defined(PROTO)
2567 * List all know file names (for :files and :buffers command).
2569 void
2570 buflist_list(eap)
2571 exarg_T *eap;
2573 buf_T *buf;
2574 int len;
2575 int i;
2577 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2579 /* skip unlisted buffers, unless ! was used */
2580 if (!buf->b_p_bl && !eap->forceit)
2581 continue;
2582 msg_putchar('\n');
2583 if (buf_spname(buf) != NULL)
2584 STRCPY(NameBuff, buf_spname(buf));
2585 else
2586 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2588 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
2589 buf->b_fnum,
2590 buf->b_p_bl ? ' ' : 'u',
2591 buf == curbuf ? '%' :
2592 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2593 buf->b_ml.ml_mfp == NULL ? ' ' :
2594 (buf->b_nwindows == 0 ? 'h' : 'a'),
2595 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2596 (buf->b_flags & BF_READERR) ? 'x'
2597 : (bufIsChanged(buf) ? '+' : ' '),
2598 NameBuff);
2600 /* put "line 999" in column 40 or after the file name */
2601 i = 40 - vim_strsize(IObuff);
2604 IObuff[len++] = ' ';
2605 } while (--i > 0 && len < IOSIZE - 18);
2606 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2607 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
2608 : (long)buflist_findlnum(buf));
2609 msg_outtrans(IObuff);
2610 out_flush(); /* output one line at a time */
2611 ui_breakcheck();
2614 #endif
2617 * Get file name and line number for file 'fnum'.
2618 * Used by DoOneCmd() for translating '%' and '#'.
2619 * Used by insert_reg() and cmdline_paste() for '#' register.
2620 * Return FAIL if not found, OK for success.
2623 buflist_name_nr(fnum, fname, lnum)
2624 int fnum;
2625 char_u **fname;
2626 linenr_T *lnum;
2628 buf_T *buf;
2630 buf = buflist_findnr(fnum);
2631 if (buf == NULL || buf->b_fname == NULL)
2632 return FAIL;
2634 *fname = buf->b_fname;
2635 *lnum = buflist_findlnum(buf);
2637 return OK;
2641 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2642 * The file name with the full path is also remembered, for when :cd is used.
2643 * Returns FAIL for failure (file name already in use by other buffer)
2644 * OK otherwise.
2647 setfname(buf, ffname, sfname, message)
2648 buf_T *buf;
2649 char_u *ffname, *sfname;
2650 int message; /* give message when buffer already exists */
2652 buf_T *obuf = NULL;
2653 #ifdef UNIX
2654 struct stat st;
2655 #endif
2657 if (ffname == NULL || *ffname == NUL)
2659 /* Removing the name. */
2660 vim_free(buf->b_ffname);
2661 vim_free(buf->b_sfname);
2662 buf->b_ffname = NULL;
2663 buf->b_sfname = NULL;
2664 #ifdef UNIX
2665 st.st_dev = (dev_T)-1;
2666 #endif
2668 else
2670 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2671 if (ffname == NULL) /* out of memory */
2672 return FAIL;
2675 * if the file name is already used in another buffer:
2676 * - if the buffer is loaded, fail
2677 * - if the buffer is not loaded, delete it from the list
2679 #ifdef UNIX
2680 if (mch_stat((char *)ffname, &st) < 0)
2681 st.st_dev = (dev_T)-1;
2682 #endif
2683 if (!(buf->b_flags & BF_DUMMY))
2684 #ifdef UNIX
2685 obuf = buflist_findname_stat(ffname, &st);
2686 #else
2687 obuf = buflist_findname(ffname);
2688 #endif
2689 if (obuf != NULL && obuf != buf)
2691 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2693 if (message)
2694 EMSG(_("E95: Buffer with this name already exists"));
2695 vim_free(ffname);
2696 return FAIL;
2698 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2700 sfname = vim_strsave(sfname);
2701 if (ffname == NULL || sfname == NULL)
2703 vim_free(sfname);
2704 vim_free(ffname);
2705 return FAIL;
2707 #ifdef USE_FNAME_CASE
2708 # ifdef USE_LONG_FNAME
2709 if (USE_LONG_FNAME)
2710 # endif
2711 fname_case(sfname, 0); /* set correct case for short file name */
2712 #endif
2713 vim_free(buf->b_ffname);
2714 vim_free(buf->b_sfname);
2715 buf->b_ffname = ffname;
2716 buf->b_sfname = sfname;
2718 buf->b_fname = buf->b_sfname;
2719 #ifdef UNIX
2720 if (st.st_dev == (dev_T)-1)
2721 buf->b_dev_valid = FALSE;
2722 else
2724 buf->b_dev_valid = TRUE;
2725 buf->b_dev = st.st_dev;
2726 buf->b_ino = st.st_ino;
2728 #endif
2730 #ifndef SHORT_FNAME
2731 buf->b_shortname = FALSE;
2732 #endif
2734 buf_name_changed(buf);
2735 return OK;
2739 * Crude way of changing the name of a buffer. Use with care!
2740 * The name should be relative to the current directory.
2742 void
2743 buf_set_name(fnum, name)
2744 int fnum;
2745 char_u *name;
2747 buf_T *buf;
2749 buf = buflist_findnr(fnum);
2750 if (buf != NULL)
2752 vim_free(buf->b_sfname);
2753 vim_free(buf->b_ffname);
2754 buf->b_ffname = vim_strsave(name);
2755 buf->b_sfname = NULL;
2756 /* Allocate ffname and expand into full path. Also resolves .lnk
2757 * files on Win32. */
2758 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
2759 buf->b_fname = buf->b_sfname;
2764 * Take care of what needs to be done when the name of buffer "buf" has
2765 * changed.
2767 void
2768 buf_name_changed(buf)
2769 buf_T *buf;
2772 * If the file name changed, also change the name of the swapfile
2774 if (buf->b_ml.ml_mfp != NULL)
2775 ml_setname(buf);
2777 if (curwin->w_buffer == buf)
2778 check_arg_idx(curwin); /* check file name for arg list */
2779 #ifdef FEAT_TITLE
2780 maketitle(); /* set window title */
2781 #endif
2782 #ifdef FEAT_WINDOWS
2783 status_redraw_all(); /* status lines need to be redrawn */
2784 #endif
2785 fmarks_check_names(buf); /* check named file marks */
2786 ml_timestamp(buf); /* reset timestamp */
2790 * set alternate file name for current window
2792 * Used by do_one_cmd(), do_write() and do_ecmd().
2793 * Return the buffer.
2795 buf_T *
2796 setaltfname(ffname, sfname, lnum)
2797 char_u *ffname;
2798 char_u *sfname;
2799 linenr_T lnum;
2801 buf_T *buf;
2803 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2804 buf = buflist_new(ffname, sfname, lnum, 0);
2805 if (buf != NULL && !cmdmod.keepalt)
2806 curwin->w_alt_fnum = buf->b_fnum;
2807 return buf;
2811 * Get alternate file name for current window.
2812 * Return NULL if there isn't any, and give error message if requested.
2814 char_u *
2815 getaltfname(errmsg)
2816 int errmsg; /* give error message */
2818 char_u *fname;
2819 linenr_T dummy;
2821 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2823 if (errmsg)
2824 EMSG(_(e_noalt));
2825 return NULL;
2827 return fname;
2831 * Add a file name to the buflist and return its number.
2832 * Uses same flags as buflist_new(), except BLN_DUMMY.
2834 * used by qf_init(), main() and doarglist()
2837 buflist_add(fname, flags)
2838 char_u *fname;
2839 int flags;
2841 buf_T *buf;
2843 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2844 if (buf != NULL)
2845 return buf->b_fnum;
2846 return 0;
2849 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2851 * Adjust slashes in file names. Called after 'shellslash' was set.
2853 void
2854 buflist_slash_adjust()
2856 buf_T *bp;
2858 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2860 if (bp->b_ffname != NULL)
2861 slash_adjust(bp->b_ffname);
2862 if (bp->b_sfname != NULL)
2863 slash_adjust(bp->b_sfname);
2866 #endif
2869 * Set alternate cursor position for the current buffer and window "win".
2870 * Also save the local window option values.
2872 void
2873 buflist_altfpos(win)
2874 win_T *win;
2876 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
2880 * Return TRUE if 'ffname' is not the same file as current file.
2881 * Fname must have a full path (expanded by mch_FullName()).
2884 otherfile(ffname)
2885 char_u *ffname;
2887 return otherfile_buf(curbuf, ffname
2888 #ifdef UNIX
2889 , NULL
2890 #endif
2894 static int
2895 otherfile_buf(buf, ffname
2896 #ifdef UNIX
2897 , stp
2898 #endif
2900 buf_T *buf;
2901 char_u *ffname;
2902 #ifdef UNIX
2903 struct stat *stp;
2904 #endif
2906 /* no name is different */
2907 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2908 return TRUE;
2909 if (fnamecmp(ffname, buf->b_ffname) == 0)
2910 return FALSE;
2911 #ifdef UNIX
2913 struct stat st;
2915 /* If no struct stat given, get it now */
2916 if (stp == NULL)
2918 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
2919 st.st_dev = (dev_T)-1;
2920 stp = &st;
2922 /* Use dev/ino to check if the files are the same, even when the names
2923 * are different (possible with links). Still need to compare the
2924 * name above, for when the file doesn't exist yet.
2925 * Problem: The dev/ino changes when a file is deleted (and created
2926 * again) and remains the same when renamed/moved. We don't want to
2927 * mch_stat() each buffer each time, that would be too slow. Get the
2928 * dev/ino again when they appear to match, but not when they appear
2929 * to be different: Could skip a buffer when it's actually the same
2930 * file. */
2931 if (buf_same_ino(buf, stp))
2933 buf_setino(buf);
2934 if (buf_same_ino(buf, stp))
2935 return FALSE;
2938 #endif
2939 return TRUE;
2942 #if defined(UNIX) || defined(PROTO)
2944 * Set inode and device number for a buffer.
2945 * Must always be called when b_fname is changed!.
2947 void
2948 buf_setino(buf)
2949 buf_T *buf;
2951 struct stat st;
2953 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2955 buf->b_dev_valid = TRUE;
2956 buf->b_dev = st.st_dev;
2957 buf->b_ino = st.st_ino;
2959 else
2960 buf->b_dev_valid = FALSE;
2964 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2966 static int
2967 buf_same_ino(buf, stp)
2968 buf_T *buf;
2969 struct stat *stp;
2971 return (buf->b_dev_valid
2972 && stp->st_dev == buf->b_dev
2973 && stp->st_ino == buf->b_ino);
2975 #endif
2978 * Print info about the current buffer.
2980 void
2981 fileinfo(fullname, shorthelp, dont_truncate)
2982 int fullname; /* when non-zero print full path */
2983 int shorthelp;
2984 int dont_truncate;
2986 char_u *name;
2987 int n;
2988 char_u *p;
2989 char_u *buffer;
2990 size_t len;
2992 buffer = alloc(IOSIZE);
2993 if (buffer == NULL)
2994 return;
2996 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2998 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
2999 p = buffer + STRLEN(buffer);
3001 else
3002 p = buffer;
3004 *p++ = '"';
3005 if (buf_spname(curbuf) != NULL)
3006 STRCPY(p, buf_spname(curbuf));
3007 else
3009 if (!fullname && curbuf->b_fname != NULL)
3010 name = curbuf->b_fname;
3011 else
3012 name = curbuf->b_ffname;
3013 home_replace(shorthelp ? curbuf : NULL, name, p,
3014 (int)(IOSIZE - (p - buffer)), TRUE);
3017 len = STRLEN(buffer);
3018 vim_snprintf((char *)buffer + len, IOSIZE - len,
3019 "\"%s%s%s%s%s%s",
3020 curbufIsChanged() ? (shortmess(SHM_MOD)
3021 ? " [+]" : _(" [Modified]")) : " ",
3022 (curbuf->b_flags & BF_NOTEDITED)
3023 #ifdef FEAT_QUICKFIX
3024 && !bt_dontwrite(curbuf)
3025 #endif
3026 ? _("[Not edited]") : "",
3027 (curbuf->b_flags & BF_NEW)
3028 #ifdef FEAT_QUICKFIX
3029 && !bt_dontwrite(curbuf)
3030 #endif
3031 ? _("[New file]") : "",
3032 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3033 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3034 : _("[readonly]")) : "",
3035 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3036 || curbuf->b_p_ro) ?
3037 " " : "");
3038 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3039 * causes an overflow, thus for large numbers divide instead. */
3040 if (curwin->w_cursor.lnum > 1000000L)
3041 n = (int)(((long)curwin->w_cursor.lnum) /
3042 ((long)curbuf->b_ml.ml_line_count / 100L));
3043 else
3044 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3045 (long)curbuf->b_ml.ml_line_count);
3046 len = STRLEN(buffer);
3047 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3049 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
3051 #ifdef FEAT_CMDL_INFO
3052 else if (p_ru)
3054 /* Current line and column are already on the screen -- webb */
3055 if (curbuf->b_ml.ml_line_count == 1)
3056 vim_snprintf((char *)buffer + len, IOSIZE - len,
3057 _("1 line --%d%%--"), n);
3058 else
3059 vim_snprintf((char *)buffer + len, IOSIZE - len,
3060 _("%ld lines --%d%%--"),
3061 (long)curbuf->b_ml.ml_line_count, n);
3063 #endif
3064 else
3066 vim_snprintf((char *)buffer + len, IOSIZE - len,
3067 _("line %ld of %ld --%d%%-- col "),
3068 (long)curwin->w_cursor.lnum,
3069 (long)curbuf->b_ml.ml_line_count,
3071 validate_virtcol();
3072 len = STRLEN(buffer);
3073 col_print(buffer + len, IOSIZE - len,
3074 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3077 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
3079 if (dont_truncate)
3081 /* Temporarily set msg_scroll to avoid the message being truncated.
3082 * First call msg_start() to get the message in the right place. */
3083 msg_start();
3084 n = msg_scroll;
3085 msg_scroll = TRUE;
3086 msg(buffer);
3087 msg_scroll = n;
3089 else
3091 p = msg_trunc_attr(buffer, FALSE, 0);
3092 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
3093 /* Need to repeat the message after redrawing when:
3094 * - When restart_edit is set (otherwise there will be a delay
3095 * before redrawing).
3096 * - When the screen was scrolled but there is no wait-return
3097 * prompt. */
3098 set_keep_msg(p, 0);
3101 vim_free(buffer);
3104 void
3105 col_print(buf, buflen, col, vcol)
3106 char_u *buf;
3107 size_t buflen;
3108 int col;
3109 int vcol;
3111 if (col == vcol)
3112 vim_snprintf((char *)buf, buflen, "%d", col);
3113 else
3114 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
3117 #if defined(FEAT_TITLE) || defined(PROTO)
3119 * put file name in title bar of window and in icon title
3122 static char_u *lasttitle = NULL;
3123 static char_u *lasticon = NULL;
3125 void
3126 maketitle()
3128 char_u *p;
3129 char_u *t_str = NULL;
3130 char_u *i_name;
3131 char_u *i_str = NULL;
3132 int maxlen = 0;
3133 int len;
3134 int mustset;
3135 char_u buf[IOSIZE];
3136 int off;
3138 if (!redrawing())
3140 /* Postpone updating the title when 'lazyredraw' is set. */
3141 need_maketitle = TRUE;
3142 return;
3145 need_maketitle = FALSE;
3146 if (!p_title && !p_icon)
3147 return;
3149 if (p_title)
3151 if (p_titlelen > 0)
3153 maxlen = p_titlelen * Columns / 100;
3154 if (maxlen < 10)
3155 maxlen = 10;
3158 t_str = buf;
3159 if (*p_titlestring != NUL)
3161 #ifdef FEAT_STL_OPT
3162 if (stl_syntax & STL_IN_TITLE)
3164 int use_sandbox = FALSE;
3165 int save_called_emsg = called_emsg;
3167 # ifdef FEAT_EVAL
3168 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
3169 # endif
3170 called_emsg = FALSE;
3171 build_stl_str_hl(curwin, t_str, sizeof(buf),
3172 p_titlestring, use_sandbox,
3173 0, maxlen, NULL, NULL);
3174 if (called_emsg)
3175 set_string_option_direct((char_u *)"titlestring", -1,
3176 (char_u *)"", OPT_FREE, SID_ERROR);
3177 called_emsg |= save_called_emsg;
3179 else
3180 #endif
3181 t_str = p_titlestring;
3183 else
3185 /* format: "fname + (path) (1 of 2) - VIM" */
3187 if (curbuf->b_fname == NULL)
3188 STRCPY(buf, _("[No Name]"));
3189 else
3191 p = transstr(gettail(curbuf->b_fname));
3192 vim_strncpy(buf, p, IOSIZE - 100);
3193 vim_free(p);
3196 switch (bufIsChanged(curbuf)
3197 + (curbuf->b_p_ro * 2)
3198 + (!curbuf->b_p_ma * 4))
3200 case 1: STRCAT(buf, " +"); break;
3201 case 2: STRCAT(buf, " ="); break;
3202 case 3: STRCAT(buf, " =+"); break;
3203 case 4:
3204 case 6: STRCAT(buf, " -"); break;
3205 case 5:
3206 case 7: STRCAT(buf, " -+"); break;
3209 if (curbuf->b_fname != NULL)
3211 /* Get path of file, replace home dir with ~ */
3212 off = (int)STRLEN(buf);
3213 buf[off++] = ' ';
3214 buf[off++] = '(';
3215 home_replace(curbuf, curbuf->b_ffname,
3216 buf + off, IOSIZE - off, TRUE);
3217 #ifdef BACKSLASH_IN_FILENAME
3218 /* avoid "c:/name" to be reduced to "c" */
3219 if (isalpha(buf[off]) && buf[off + 1] == ':')
3220 off += 2;
3221 #endif
3222 /* remove the file name */
3223 p = gettail_sep(buf + off);
3224 if (p == buf + off)
3225 /* must be a help buffer */
3226 vim_strncpy(buf + off, (char_u *)_("help"),
3227 (size_t)(IOSIZE - off - 1));
3228 else
3229 *p = NUL;
3231 /* translate unprintable chars */
3232 p = transstr(buf + off);
3233 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
3234 vim_free(p);
3235 STRCAT(buf, ")");
3238 append_arg_number(curwin, buf, IOSIZE, FALSE);
3240 #if defined(FEAT_CLIENTSERVER)
3241 if (serverName != NULL)
3243 STRCAT(buf, " - ");
3244 STRCAT(buf, serverName);
3246 else
3247 #endif
3248 STRCAT(buf, " - VIM");
3250 if (maxlen > 0)
3252 /* make it shorter by removing a bit in the middle */
3253 len = vim_strsize(buf);
3254 if (len > maxlen)
3255 trunc_string(buf, buf, maxlen);
3259 mustset = ti_change(t_str, &lasttitle);
3261 if (p_icon)
3263 i_str = buf;
3264 if (*p_iconstring != NUL)
3266 #ifdef FEAT_STL_OPT
3267 if (stl_syntax & STL_IN_ICON)
3269 int use_sandbox = FALSE;
3270 int save_called_emsg = called_emsg;
3272 # ifdef FEAT_EVAL
3273 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
3274 # endif
3275 called_emsg = FALSE;
3276 build_stl_str_hl(curwin, i_str, sizeof(buf),
3277 p_iconstring, use_sandbox,
3278 0, 0, NULL, NULL);
3279 if (called_emsg)
3280 set_string_option_direct((char_u *)"iconstring", -1,
3281 (char_u *)"", OPT_FREE, SID_ERROR);
3282 called_emsg |= save_called_emsg;
3284 else
3285 #endif
3286 i_str = p_iconstring;
3288 else
3290 if (buf_spname(curbuf) != NULL)
3291 i_name = (char_u *)buf_spname(curbuf);
3292 else /* use file name only in icon */
3293 i_name = gettail(curbuf->b_ffname);
3294 *i_str = NUL;
3295 /* Truncate name at 100 bytes. */
3296 len = (int)STRLEN(i_name);
3297 if (len > 100)
3299 len -= 100;
3300 #ifdef FEAT_MBYTE
3301 if (has_mbyte)
3302 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3303 #endif
3304 i_name += len;
3306 STRCPY(i_str, i_name);
3307 trans_characters(i_str, IOSIZE);
3311 mustset |= ti_change(i_str, &lasticon);
3313 if (mustset)
3314 resettitle();
3318 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3319 * from "str" if it does.
3320 * Return TRUE when "*last" changed.
3322 static int
3323 ti_change(str, last)
3324 char_u *str;
3325 char_u **last;
3327 if ((str == NULL) != (*last == NULL)
3328 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3330 vim_free(*last);
3331 if (str == NULL)
3332 *last = NULL;
3333 else
3334 *last = vim_strsave(str);
3335 return TRUE;
3337 return FALSE;
3341 * Put current window title back (used after calling a shell)
3343 void
3344 resettitle()
3346 mch_settitle(lasttitle, lasticon);
3349 # if defined(EXITFREE) || defined(PROTO)
3350 void
3351 free_titles()
3353 vim_free(lasttitle);
3354 vim_free(lasticon);
3356 # endif
3358 #endif /* FEAT_TITLE */
3360 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
3362 * Build a string from the status line items in "fmt".
3363 * Return length of string in screen cells.
3365 * Normally works for window "wp", except when working for 'tabline' then it
3366 * is "curwin".
3368 * Items are drawn interspersed with the text that surrounds it
3369 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3370 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3372 * If maxwidth is not zero, the string will be filled at any middle marker
3373 * or truncated if too long, fillchar is used for all whitespace.
3376 build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
3377 win_T *wp;
3378 char_u *out; /* buffer to write into != NameBuff */
3379 size_t outlen; /* length of out[] */
3380 char_u *fmt;
3381 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
3382 int fillchar;
3383 int maxwidth;
3384 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3385 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
3387 char_u *p;
3388 char_u *s;
3389 char_u *t;
3390 char_u *linecont;
3391 #ifdef FEAT_EVAL
3392 win_T *o_curwin;
3393 buf_T *o_curbuf;
3394 #endif
3395 int empty_line;
3396 colnr_T virtcol;
3397 long l;
3398 long n;
3399 int prevchar_isflag;
3400 int prevchar_isitem;
3401 int itemisflag;
3402 int fillable;
3403 char_u *str;
3404 long num;
3405 int width;
3406 int itemcnt;
3407 int curitem;
3408 int groupitem[STL_MAX_ITEM];
3409 int groupdepth;
3410 struct stl_item
3412 char_u *start;
3413 int minwid;
3414 int maxwid;
3415 enum
3417 Normal,
3418 Empty,
3419 Group,
3420 Middle,
3421 Highlight,
3422 TabPage,
3423 Trunc
3424 } type;
3425 } item[STL_MAX_ITEM];
3426 int minwid;
3427 int maxwid;
3428 int zeropad;
3429 char_u base;
3430 char_u opt;
3431 #define TMPLEN 70
3432 char_u tmp[TMPLEN];
3433 char_u *usefmt = fmt;
3434 struct stl_hlrec *sp;
3436 #ifdef FEAT_EVAL
3438 * When the format starts with "%!" then evaluate it as an expression and
3439 * use the result as the actual format string.
3441 if (fmt[0] == '%' && fmt[1] == '!')
3443 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3444 if (usefmt == NULL)
3445 usefmt = fmt;
3447 #endif
3449 if (fillchar == 0)
3450 fillchar = ' ';
3451 #ifdef FEAT_MBYTE
3452 /* Can't handle a multi-byte fill character yet. */
3453 else if (mb_char2len(fillchar) > 1)
3454 fillchar = '-';
3455 #endif
3458 * Get line & check if empty (cursorpos will show "0-1").
3459 * If inversion is possible we use it. Else '=' characters are used.
3461 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3462 empty_line = (*linecont == NUL);
3464 groupdepth = 0;
3465 p = out;
3466 curitem = 0;
3467 prevchar_isflag = TRUE;
3468 prevchar_isitem = FALSE;
3469 for (s = usefmt; *s; )
3471 if (*s != NUL && *s != '%')
3472 prevchar_isflag = prevchar_isitem = FALSE;
3475 * Handle up to the next '%' or the end.
3477 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3478 *p++ = *s++;
3479 if (*s == NUL || p + 1 >= out + outlen)
3480 break;
3483 * Handle one '%' item.
3485 s++;
3486 if (*s == '%')
3488 if (p + 1 >= out + outlen)
3489 break;
3490 *p++ = *s++;
3491 prevchar_isflag = prevchar_isitem = FALSE;
3492 continue;
3494 if (*s == STL_MIDDLEMARK)
3496 s++;
3497 if (groupdepth > 0)
3498 continue;
3499 item[curitem].type = Middle;
3500 item[curitem++].start = p;
3501 continue;
3503 if (*s == STL_TRUNCMARK)
3505 s++;
3506 item[curitem].type = Trunc;
3507 item[curitem++].start = p;
3508 continue;
3510 if (*s == ')')
3512 s++;
3513 if (groupdepth < 1)
3514 continue;
3515 groupdepth--;
3517 t = item[groupitem[groupdepth]].start;
3518 *p = NUL;
3519 l = vim_strsize(t);
3520 if (curitem > groupitem[groupdepth] + 1
3521 && item[groupitem[groupdepth]].minwid == 0)
3523 /* remove group if all items are empty */
3524 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3525 if (item[n].type == Normal)
3526 break;
3527 if (n == curitem)
3529 p = t;
3530 l = 0;
3533 if (l > item[groupitem[groupdepth]].maxwid)
3535 /* truncate, remove n bytes of text at the start */
3536 #ifdef FEAT_MBYTE
3537 if (has_mbyte)
3539 /* Find the first character that should be included. */
3540 n = 0;
3541 while (l >= item[groupitem[groupdepth]].maxwid)
3543 l -= ptr2cells(t + n);
3544 n += (*mb_ptr2len)(t + n);
3547 else
3548 #endif
3549 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
3551 *t = '<';
3552 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
3553 p = p - n + 1;
3554 #ifdef FEAT_MBYTE
3555 /* Fill up space left over by half a double-wide char. */
3556 while (++l < item[groupitem[groupdepth]].minwid)
3557 *p++ = fillchar;
3558 #endif
3560 /* correct the start of the items for the truncation */
3561 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3563 item[l].start -= n;
3564 if (item[l].start < t)
3565 item[l].start = t;
3568 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3570 /* fill */
3571 n = item[groupitem[groupdepth]].minwid;
3572 if (n < 0)
3574 /* fill by appending characters */
3575 n = 0 - n;
3576 while (l++ < n && p + 1 < out + outlen)
3577 *p++ = fillchar;
3579 else
3581 /* fill by inserting characters */
3582 mch_memmove(t + n - l, t, (size_t)(p - t));
3583 l = n - l;
3584 if (p + l >= out + outlen)
3585 l = (long)((out + outlen) - p - 1);
3586 p += l;
3587 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3588 item[n].start += l;
3589 for ( ; l > 0; l--)
3590 *t++ = fillchar;
3593 continue;
3595 minwid = 0;
3596 maxwid = 9999;
3597 zeropad = FALSE;
3598 l = 1;
3599 if (*s == '0')
3601 s++;
3602 zeropad = TRUE;
3604 if (*s == '-')
3606 s++;
3607 l = -1;
3609 if (VIM_ISDIGIT(*s))
3611 minwid = (int)getdigits(&s);
3612 if (minwid < 0) /* overflow */
3613 minwid = 0;
3615 if (*s == STL_USER_HL)
3617 item[curitem].type = Highlight;
3618 item[curitem].start = p;
3619 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3620 s++;
3621 curitem++;
3622 continue;
3624 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3626 if (*s == STL_TABCLOSENR)
3628 if (minwid == 0)
3630 /* %X ends the close label, go back to the previously
3631 * define tab label nr. */
3632 for (n = curitem - 1; n >= 0; --n)
3633 if (item[n].type == TabPage && item[n].minwid >= 0)
3635 minwid = item[n].minwid;
3636 break;
3639 else
3640 /* close nrs are stored as negative values */
3641 minwid = - minwid;
3643 item[curitem].type = TabPage;
3644 item[curitem].start = p;
3645 item[curitem].minwid = minwid;
3646 s++;
3647 curitem++;
3648 continue;
3650 if (*s == '.')
3652 s++;
3653 if (VIM_ISDIGIT(*s))
3655 maxwid = (int)getdigits(&s);
3656 if (maxwid <= 0) /* overflow */
3657 maxwid = 50;
3660 minwid = (minwid > 50 ? 50 : minwid) * l;
3661 if (*s == '(')
3663 groupitem[groupdepth++] = curitem;
3664 item[curitem].type = Group;
3665 item[curitem].start = p;
3666 item[curitem].minwid = minwid;
3667 item[curitem].maxwid = maxwid;
3668 s++;
3669 curitem++;
3670 continue;
3672 if (vim_strchr(STL_ALL, *s) == NULL)
3674 s++;
3675 continue;
3677 opt = *s++;
3679 /* OK - now for the real work */
3680 base = 'D';
3681 itemisflag = FALSE;
3682 fillable = TRUE;
3683 num = -1;
3684 str = NULL;
3685 switch (opt)
3687 case STL_FILEPATH:
3688 case STL_FULLPATH:
3689 case STL_FILENAME:
3690 fillable = FALSE; /* don't change ' ' to fillchar */
3691 if (buf_spname(wp->w_buffer) != NULL)
3692 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3693 else
3695 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
3696 : wp->w_buffer->b_fname;
3697 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3699 trans_characters(NameBuff, MAXPATHL);
3700 if (opt != STL_FILENAME)
3701 str = NameBuff;
3702 else
3703 str = gettail(NameBuff);
3704 break;
3706 case STL_VIM_EXPR: /* '{' */
3707 itemisflag = TRUE;
3708 t = p;
3709 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3710 *p++ = *s++;
3711 if (*s != '}') /* missing '}' or out of space */
3712 break;
3713 s++;
3714 *p = 0;
3715 p = t;
3717 #ifdef FEAT_EVAL
3718 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
3719 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3721 o_curbuf = curbuf;
3722 o_curwin = curwin;
3723 curwin = wp;
3724 curbuf = wp->w_buffer;
3726 str = eval_to_string_safe(p, &t, use_sandbox);
3728 curwin = o_curwin;
3729 curbuf = o_curbuf;
3730 do_unlet((char_u *)"g:actual_curbuf", TRUE);
3732 if (str != NULL && *str != 0)
3734 if (*skipdigits(str) == NUL)
3736 num = atoi((char *)str);
3737 vim_free(str);
3738 str = NULL;
3739 itemisflag = FALSE;
3742 #endif
3743 break;
3745 case STL_LINE:
3746 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3747 ? 0L : (long)(wp->w_cursor.lnum);
3748 break;
3750 case STL_NUMLINES:
3751 num = wp->w_buffer->b_ml.ml_line_count;
3752 break;
3754 case STL_COLUMN:
3755 num = !(State & INSERT) && empty_line
3756 ? 0 : (int)wp->w_cursor.col + 1;
3757 break;
3759 case STL_VIRTCOL:
3760 case STL_VIRTCOL_ALT:
3761 /* In list mode virtcol needs to be recomputed */
3762 virtcol = wp->w_virtcol;
3763 if (wp->w_p_list && lcs_tab1 == NUL)
3765 wp->w_p_list = FALSE;
3766 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3767 wp->w_p_list = TRUE;
3769 ++virtcol;
3770 /* Don't display %V if it's the same as %c. */
3771 if (opt == STL_VIRTCOL_ALT
3772 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3773 ? 0 : (int)wp->w_cursor.col + 1)))
3774 break;
3775 num = (long)virtcol;
3776 break;
3778 case STL_PERCENTAGE:
3779 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3780 (long)wp->w_buffer->b_ml.ml_line_count);
3781 break;
3783 case STL_ALTPERCENT:
3784 str = tmp;
3785 get_rel_pos(wp, str, TMPLEN);
3786 break;
3788 case STL_ARGLISTSTAT:
3789 fillable = FALSE;
3790 tmp[0] = 0;
3791 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
3792 str = tmp;
3793 break;
3795 case STL_KEYMAP:
3796 fillable = FALSE;
3797 if (get_keymap_str(wp, tmp, TMPLEN))
3798 str = tmp;
3799 break;
3800 case STL_PAGENUM:
3801 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
3802 num = printer_page_num;
3803 #else
3804 num = 0;
3805 #endif
3806 break;
3808 case STL_BUFNO:
3809 num = wp->w_buffer->b_fnum;
3810 break;
3812 case STL_OFFSET_X:
3813 base = 'X';
3814 case STL_OFFSET:
3815 #ifdef FEAT_BYTEOFF
3816 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3817 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3818 0L : l + 1 + (!(State & INSERT) && empty_line ?
3819 0 : (int)wp->w_cursor.col);
3820 #endif
3821 break;
3823 case STL_BYTEVAL_X:
3824 base = 'X';
3825 case STL_BYTEVAL:
3826 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
3827 num = 0;
3828 else
3830 #ifdef FEAT_MBYTE
3831 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3832 #else
3833 num = linecont[wp->w_cursor.col];
3834 #endif
3836 if (num == NL)
3837 num = 0;
3838 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3839 num = NL;
3840 break;
3842 case STL_ROFLAG:
3843 case STL_ROFLAG_ALT:
3844 itemisflag = TRUE;
3845 if (wp->w_buffer->b_p_ro)
3846 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3847 break;
3849 case STL_HELPFLAG:
3850 case STL_HELPFLAG_ALT:
3851 itemisflag = TRUE;
3852 if (wp->w_buffer->b_help)
3853 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
3854 : _("[Help]"));
3855 break;
3857 #ifdef FEAT_AUTOCMD
3858 case STL_FILETYPE:
3859 if (*wp->w_buffer->b_p_ft != NUL
3860 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3862 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3863 wp->w_buffer->b_p_ft);
3864 str = tmp;
3866 break;
3868 case STL_FILETYPE_ALT:
3869 itemisflag = TRUE;
3870 if (*wp->w_buffer->b_p_ft != NUL
3871 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3873 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3874 wp->w_buffer->b_p_ft);
3875 for (t = tmp; *t != 0; t++)
3876 *t = TOUPPER_LOC(*t);
3877 str = tmp;
3879 break;
3880 #endif
3882 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3883 case STL_PREVIEWFLAG:
3884 case STL_PREVIEWFLAG_ALT:
3885 itemisflag = TRUE;
3886 if (wp->w_p_pvw)
3887 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3888 : _("[Preview]"));
3889 break;
3890 #endif
3892 case STL_MODIFIED:
3893 case STL_MODIFIED_ALT:
3894 itemisflag = TRUE;
3895 switch ((opt == STL_MODIFIED_ALT)
3896 + bufIsChanged(wp->w_buffer) * 2
3897 + (!wp->w_buffer->b_p_ma) * 4)
3899 case 2: str = (char_u *)"[+]"; break;
3900 case 3: str = (char_u *)",+"; break;
3901 case 4: str = (char_u *)"[-]"; break;
3902 case 5: str = (char_u *)",-"; break;
3903 case 6: str = (char_u *)"[+-]"; break;
3904 case 7: str = (char_u *)",+-"; break;
3906 break;
3908 case STL_HIGHLIGHT:
3909 t = s;
3910 while (*s != '#' && *s != NUL)
3911 ++s;
3912 if (*s == '#')
3914 item[curitem].type = Highlight;
3915 item[curitem].start = p;
3916 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
3917 curitem++;
3919 ++s;
3920 continue;
3923 item[curitem].start = p;
3924 item[curitem].type = Normal;
3925 if (str != NULL && *str)
3927 t = str;
3928 if (itemisflag)
3930 if ((t[0] && t[1])
3931 && ((!prevchar_isitem && *t == ',')
3932 || (prevchar_isflag && *t == ' ')))
3933 t++;
3934 prevchar_isflag = TRUE;
3936 l = vim_strsize(t);
3937 if (l > 0)
3938 prevchar_isitem = TRUE;
3939 if (l > maxwid)
3941 while (l >= maxwid)
3942 #ifdef FEAT_MBYTE
3943 if (has_mbyte)
3945 l -= ptr2cells(t);
3946 t += (*mb_ptr2len)(t);
3948 else
3949 #endif
3950 l -= byte2cells(*t++);
3951 if (p + 1 >= out + outlen)
3952 break;
3953 *p++ = '<';
3955 if (minwid > 0)
3957 for (; l < minwid && p + 1 < out + outlen; l++)
3959 /* Don't put a "-" in front of a digit. */
3960 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3961 *p++ = ' ';
3962 else
3963 *p++ = fillchar;
3965 minwid = 0;
3967 else
3968 minwid *= -1;
3969 while (*t && p + 1 < out + outlen)
3971 *p++ = *t++;
3972 /* Change a space by fillchar, unless fillchar is '-' and a
3973 * digit follows. */
3974 if (fillable && p[-1] == ' '
3975 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3976 p[-1] = fillchar;
3978 for (; l < minwid && p + 1 < out + outlen; l++)
3979 *p++ = fillchar;
3981 else if (num >= 0)
3983 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3984 char_u nstr[20];
3986 if (p + 20 >= out + outlen)
3987 break; /* not sufficient space */
3988 prevchar_isitem = TRUE;
3989 t = nstr;
3990 if (opt == STL_VIRTCOL_ALT)
3992 *t++ = '-';
3993 minwid--;
3995 *t++ = '%';
3996 if (zeropad)
3997 *t++ = '0';
3998 *t++ = '*';
3999 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
4000 *t = 0;
4002 for (n = num, l = 1; n >= nbase; n /= nbase)
4003 l++;
4004 if (opt == STL_VIRTCOL_ALT)
4005 l++;
4006 if (l > maxwid)
4008 l += 2;
4009 n = l - maxwid;
4010 while (l-- > maxwid)
4011 num /= nbase;
4012 *t++ = '>';
4013 *t++ = '%';
4014 *t = t[-3];
4015 *++t = 0;
4016 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4017 0, num, n);
4019 else
4020 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4021 minwid, num);
4022 p += STRLEN(p);
4024 else
4025 item[curitem].type = Empty;
4027 if (opt == STL_VIM_EXPR)
4028 vim_free(str);
4030 if (num >= 0 || (!itemisflag && str && *str))
4031 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4032 curitem++;
4034 *p = NUL;
4035 itemcnt = curitem;
4037 #ifdef FEAT_EVAL
4038 if (usefmt != fmt)
4039 vim_free(usefmt);
4040 #endif
4042 width = vim_strsize(out);
4043 if (maxwidth > 0 && width > maxwidth)
4045 /* Result is too long, must truncate somewhere. */
4046 l = 0;
4047 if (itemcnt == 0)
4048 s = out;
4049 else
4051 for ( ; l < itemcnt; l++)
4052 if (item[l].type == Trunc)
4054 /* Truncate at %< item. */
4055 s = item[l].start;
4056 break;
4058 if (l == itemcnt)
4060 /* No %< item, truncate first item. */
4061 s = item[0].start;
4062 l = 0;
4066 if (width - vim_strsize(s) >= maxwidth)
4068 /* Truncation mark is beyond max length */
4069 #ifdef FEAT_MBYTE
4070 if (has_mbyte)
4072 s = out;
4073 width = 0;
4074 for (;;)
4076 width += ptr2cells(s);
4077 if (width >= maxwidth)
4078 break;
4079 s += (*mb_ptr2len)(s);
4081 /* Fill up for half a double-wide character. */
4082 while (++width < maxwidth)
4083 *s++ = fillchar;
4085 else
4086 #endif
4087 s = out + maxwidth - 1;
4088 for (l = 0; l < itemcnt; l++)
4089 if (item[l].start > s)
4090 break;
4091 itemcnt = l;
4092 *s++ = '>';
4093 *s = 0;
4095 else
4097 #ifdef FEAT_MBYTE
4098 if (has_mbyte)
4100 n = 0;
4101 while (width >= maxwidth)
4103 width -= ptr2cells(s + n);
4104 n += (*mb_ptr2len)(s + n);
4107 else
4108 #endif
4109 n = width - maxwidth + 1;
4110 p = s + n;
4111 STRMOVE(s + 1, p);
4112 *s = '<';
4114 /* Fill up for half a double-wide character. */
4115 while (++width < maxwidth)
4117 s = s + STRLEN(s);
4118 *s++ = fillchar;
4119 *s = NUL;
4122 --n; /* count the '<' */
4123 for (; l < itemcnt; l++)
4125 if (item[l].start - n >= s)
4126 item[l].start -= n;
4127 else
4128 item[l].start = s;
4131 width = maxwidth;
4133 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4135 /* Apply STL_MIDDLE if any */
4136 for (l = 0; l < itemcnt; l++)
4137 if (item[l].type == Middle)
4138 break;
4139 if (l < itemcnt)
4141 p = item[l].start + maxwidth - width;
4142 STRMOVE(p, item[l].start);
4143 for (s = item[l].start; s < p; s++)
4144 *s = fillchar;
4145 for (l++; l < itemcnt; l++)
4146 item[l].start += maxwidth - width;
4147 width = maxwidth;
4151 /* Store the info about highlighting. */
4152 if (hltab != NULL)
4154 sp = hltab;
4155 for (l = 0; l < itemcnt; l++)
4157 if (item[l].type == Highlight)
4159 sp->start = item[l].start;
4160 sp->userhl = item[l].minwid;
4161 sp++;
4164 sp->start = NULL;
4165 sp->userhl = 0;
4168 /* Store the info about tab pages labels. */
4169 if (tabtab != NULL)
4171 sp = tabtab;
4172 for (l = 0; l < itemcnt; l++)
4174 if (item[l].type == TabPage)
4176 sp->start = item[l].start;
4177 sp->userhl = item[l].minwid;
4178 sp++;
4181 sp->start = NULL;
4182 sp->userhl = 0;
4185 return width;
4187 #endif /* FEAT_STL_OPT */
4189 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4190 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
4192 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4193 * using "Top", "Bot" or "All" when appropriate.
4195 void
4196 get_rel_pos(wp, buf, buflen)
4197 win_T *wp;
4198 char_u *buf;
4199 int buflen;
4201 long above; /* number of lines above window */
4202 long below; /* number of lines below window */
4204 above = wp->w_topline - 1;
4205 #ifdef FEAT_DIFF
4206 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4207 #endif
4208 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4209 if (below <= 0)
4210 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4211 (size_t)(buflen - 1));
4212 else if (above <= 0)
4213 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
4214 else
4215 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
4216 ? (int)(above / ((above + below) / 100L))
4217 : (int)(above * 100L / (above + below)));
4219 #endif
4222 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
4223 * Return TRUE if it was appended.
4225 static int
4226 append_arg_number(wp, buf, buflen, add_file)
4227 win_T *wp;
4228 char_u *buf;
4229 int buflen;
4230 int add_file; /* Add "file" before the arg number */
4232 char_u *p;
4234 if (ARGCOUNT <= 1) /* nothing to do */
4235 return FALSE;
4237 p = buf + STRLEN(buf); /* go to the end of the buffer */
4238 if (p - buf + 35 >= buflen) /* getting too long */
4239 return FALSE;
4240 *p++ = ' ';
4241 *p++ = '(';
4242 if (add_file)
4244 STRCPY(p, "file ");
4245 p += 5;
4247 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4248 wp->w_arg_idx_invalid ? "(%d) of %d)"
4249 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4250 return TRUE;
4254 * If fname is not a full path, make it a full path.
4255 * Returns pointer to allocated memory (NULL for failure).
4257 char_u *
4258 fix_fname(fname)
4259 char_u *fname;
4262 * Force expanding the path always for Unix, because symbolic links may
4263 * mess up the full path name, even though it starts with a '/'.
4264 * Also expand when there is ".." in the file name, try to remove it,
4265 * because "c:/src/../README" is equal to "c:/README".
4266 * Similarly "c:/src//file" is equal to "c:/src/file".
4267 * For MS-Windows also expand names like "longna~1" to "longname".
4269 #ifdef UNIX
4270 return FullName_save(fname, TRUE);
4271 #else
4272 if (!vim_isAbsName(fname)
4273 || strstr((char *)fname, "..") != NULL
4274 || strstr((char *)fname, "//") != NULL
4275 # ifdef BACKSLASH_IN_FILENAME
4276 || strstr((char *)fname, "\\\\") != NULL
4277 # endif
4278 # if defined(MSWIN) || defined(DJGPP)
4279 || vim_strchr(fname, '~') != NULL
4280 # endif
4282 return FullName_save(fname, FALSE);
4284 fname = vim_strsave(fname);
4286 # ifdef USE_FNAME_CASE
4287 # ifdef USE_LONG_FNAME
4288 if (USE_LONG_FNAME)
4289 # endif
4291 if (fname != NULL)
4292 fname_case(fname, 0); /* set correct case for file name */
4294 # endif
4296 return fname;
4297 #endif
4301 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4302 * "ffname" becomes a pointer to allocated memory (or NULL).
4304 void
4305 fname_expand(buf, ffname, sfname)
4306 buf_T *buf UNUSED;
4307 char_u **ffname;
4308 char_u **sfname;
4310 if (*ffname == NULL) /* if no file name given, nothing to do */
4311 return;
4312 if (*sfname == NULL) /* if no short file name given, use ffname */
4313 *sfname = *ffname;
4314 *ffname = fix_fname(*ffname); /* expand to full path */
4316 #ifdef FEAT_SHORTCUT
4317 if (!buf->b_p_bin)
4319 char_u *rfname;
4321 /* If the file name is a shortcut file, use the file it links to. */
4322 rfname = mch_resolve_shortcut(*ffname);
4323 if (rfname != NULL)
4325 vim_free(*ffname);
4326 *ffname = rfname;
4327 *sfname = rfname;
4330 #endif
4334 * Get the file name for an argument list entry.
4336 char_u *
4337 alist_name(aep)
4338 aentry_T *aep;
4340 buf_T *bp;
4342 /* Use the name from the associated buffer if it exists. */
4343 bp = buflist_findnr(aep->ae_fnum);
4344 if (bp == NULL || bp->b_fname == NULL)
4345 return aep->ae_fname;
4346 return bp->b_fname;
4349 #if defined(FEAT_WINDOWS) || defined(PROTO)
4351 * do_arg_all(): Open up to 'count' windows, one for each argument.
4353 void
4354 do_arg_all(count, forceit, keep_tabs)
4355 int count;
4356 int forceit; /* hide buffers in current windows */
4357 int keep_tabs; /* keep current tabs, for ":tab drop file" */
4359 int i;
4360 win_T *wp, *wpnext;
4361 char_u *opened; /* array of flags for which args are open */
4362 int opened_len; /* length of opened[] */
4363 int use_firstwin = FALSE; /* use first window for arglist */
4364 int split_ret = OK;
4365 int p_ea_save;
4366 alist_T *alist; /* argument list to be used */
4367 buf_T *buf;
4368 tabpage_T *tpnext;
4369 int had_tab = cmdmod.tab;
4370 win_T *new_curwin = NULL;
4371 tabpage_T *new_curtab = NULL;
4373 if (ARGCOUNT <= 0)
4375 /* Don't give an error message. We don't want it when the ":all"
4376 * command is in the .vimrc. */
4377 return;
4379 setpcmark();
4381 opened_len = ARGCOUNT;
4382 opened = alloc_clear((unsigned)opened_len);
4383 if (opened == NULL)
4384 return;
4386 #ifdef FEAT_GUI
4387 need_mouse_correct = TRUE;
4388 #endif
4391 * Try closing all windows that are not in the argument list.
4392 * Also close windows that are not full width;
4393 * When 'hidden' or "forceit" set the buffer becomes hidden.
4394 * Windows that have a changed buffer and can't be hidden won't be closed.
4395 * When the ":tab" modifier was used do this for all tab pages.
4397 if (had_tab > 0)
4398 goto_tabpage_tp(first_tabpage);
4399 for (;;)
4401 tpnext = curtab->tp_next;
4402 for (wp = firstwin; wp != NULL; wp = wpnext)
4404 wpnext = wp->w_next;
4405 buf = wp->w_buffer;
4406 if (buf->b_ffname == NULL
4407 || buf->b_nwindows > 1
4408 #ifdef FEAT_VERTSPLIT
4409 || wp->w_width != Columns
4410 #endif
4412 i = ARGCOUNT;
4413 else
4415 /* check if the buffer in this window is in the arglist */
4416 for (i = 0; i < ARGCOUNT; ++i)
4418 if (ARGLIST[i].ae_fnum == buf->b_fnum
4419 || fullpathcmp(alist_name(&ARGLIST[i]),
4420 buf->b_ffname, TRUE) & FPC_SAME)
4422 if (i < opened_len)
4424 opened[i] = TRUE;
4425 if (i == 0)
4427 new_curwin = wp;
4428 new_curtab = curtab;
4431 if (wp->w_alist != curwin->w_alist)
4433 /* Use the current argument list for all windows
4434 * containing a file from it. */
4435 alist_unlink(wp->w_alist);
4436 wp->w_alist = curwin->w_alist;
4437 ++wp->w_alist->al_refcount;
4439 break;
4443 wp->w_arg_idx = i;
4445 if (i == ARGCOUNT && !keep_tabs) /* close this window */
4447 if (P_HID(buf) || forceit || buf->b_nwindows > 1
4448 || !bufIsChanged(buf))
4450 /* If the buffer was changed, and we would like to hide it,
4451 * try autowriting. */
4452 if (!P_HID(buf) && buf->b_nwindows <= 1
4453 && bufIsChanged(buf))
4455 (void)autowrite(buf, FALSE);
4456 #ifdef FEAT_AUTOCMD
4457 /* check if autocommands removed the window */
4458 if (!win_valid(wp) || !buf_valid(buf))
4460 wpnext = firstwin; /* start all over... */
4461 continue;
4463 #endif
4465 #ifdef FEAT_WINDOWS
4466 /* don't close last window */
4467 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
4468 #endif
4469 use_firstwin = TRUE;
4470 #ifdef FEAT_WINDOWS
4471 else
4473 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4474 # ifdef FEAT_AUTOCMD
4475 /* check if autocommands removed the next window */
4476 if (!win_valid(wpnext))
4477 wpnext = firstwin; /* start all over... */
4478 # endif
4480 #endif
4485 /* Without the ":tab" modifier only do the current tab page. */
4486 if (had_tab == 0 || tpnext == NULL)
4487 break;
4489 # ifdef FEAT_AUTOCMD
4490 /* check if autocommands removed the next tab page */
4491 if (!valid_tabpage(tpnext))
4492 tpnext = first_tabpage; /* start all over...*/
4493 # endif
4494 goto_tabpage_tp(tpnext);
4498 * Open a window for files in the argument list that don't have one.
4499 * ARGCOUNT may change while doing this, because of autocommands.
4501 if (count > ARGCOUNT || count <= 0)
4502 count = ARGCOUNT;
4504 /* Autocommands may do anything to the argument list. Make sure it's not
4505 * freed while we are working here by "locking" it. We still have to
4506 * watch out for its size to be changed. */
4507 alist = curwin->w_alist;
4508 ++alist->al_refcount;
4510 #ifdef FEAT_AUTOCMD
4511 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4512 ++autocmd_no_enter;
4513 ++autocmd_no_leave;
4514 #endif
4515 win_enter(lastwin, FALSE);
4516 #ifdef FEAT_WINDOWS
4517 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4518 * leaving an empty tab page when executed locally. */
4519 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4520 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4521 use_firstwin = TRUE;
4522 #endif
4524 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4526 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4527 arg_had_last = TRUE;
4528 if (i < opened_len && opened[i])
4530 /* Move the already present window to below the current window */
4531 if (curwin->w_arg_idx != i)
4533 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4535 if (wpnext->w_arg_idx == i)
4537 win_move_after(wpnext, curwin);
4538 break;
4543 else if (split_ret == OK)
4545 if (!use_firstwin) /* split current window */
4547 p_ea_save = p_ea;
4548 p_ea = TRUE; /* use space from all windows */
4549 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4550 p_ea = p_ea_save;
4551 if (split_ret == FAIL)
4552 continue;
4554 #ifdef FEAT_AUTOCMD
4555 else /* first window: do autocmd for leaving this buffer */
4556 --autocmd_no_leave;
4557 #endif
4560 * edit file "i"
4562 curwin->w_arg_idx = i;
4563 if (i == 0)
4565 new_curwin = curwin;
4566 new_curtab = curtab;
4568 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4569 ECMD_ONE,
4570 ((P_HID(curwin->w_buffer)
4571 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4572 + ECMD_OLDBUF, curwin);
4573 #ifdef FEAT_AUTOCMD
4574 if (use_firstwin)
4575 ++autocmd_no_leave;
4576 #endif
4577 use_firstwin = FALSE;
4579 ui_breakcheck();
4581 /* When ":tab" was used open a new tab for a new window repeatedly. */
4582 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4583 cmdmod.tab = 9999;
4586 /* Remove the "lock" on the argument list. */
4587 alist_unlink(alist);
4589 #ifdef FEAT_AUTOCMD
4590 --autocmd_no_enter;
4591 #endif
4592 /* to window with first arg */
4593 if (valid_tabpage(new_curtab))
4594 goto_tabpage_tp(new_curtab);
4595 if (win_valid(new_curwin))
4596 win_enter(new_curwin, FALSE);
4598 #ifdef FEAT_AUTOCMD
4599 --autocmd_no_leave;
4600 #endif
4601 vim_free(opened);
4604 # if defined(FEAT_LISTCMDS) || defined(PROTO)
4606 * Open a window for a number of buffers.
4608 void
4609 ex_buffer_all(eap)
4610 exarg_T *eap;
4612 buf_T *buf;
4613 win_T *wp, *wpnext;
4614 int split_ret = OK;
4615 int p_ea_save;
4616 int open_wins = 0;
4617 int r;
4618 int count; /* Maximum number of windows to open. */
4619 int all; /* When TRUE also load inactive buffers. */
4620 #ifdef FEAT_WINDOWS
4621 int had_tab = cmdmod.tab;
4622 tabpage_T *tpnext;
4623 #endif
4625 if (eap->addr_count == 0) /* make as many windows as possible */
4626 count = 9999;
4627 else
4628 count = eap->line2; /* make as many windows as specified */
4629 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4630 all = FALSE;
4631 else
4632 all = TRUE;
4634 setpcmark();
4636 #ifdef FEAT_GUI
4637 need_mouse_correct = TRUE;
4638 #endif
4641 * Close superfluous windows (two windows for the same buffer).
4642 * Also close windows that are not full-width.
4644 #ifdef FEAT_WINDOWS
4645 if (had_tab > 0)
4646 goto_tabpage_tp(first_tabpage);
4647 for (;;)
4649 #endif
4650 tpnext = curtab->tp_next;
4651 for (wp = firstwin; wp != NULL; wp = wpnext)
4653 wpnext = wp->w_next;
4654 if ((wp->w_buffer->b_nwindows > 1
4655 #ifdef FEAT_VERTSPLIT
4656 || ((cmdmod.split & WSP_VERT)
4657 ? wp->w_height + wp->w_status_height < Rows - p_ch
4658 - tabline_height()
4659 : wp->w_width != Columns)
4660 #endif
4661 #ifdef FEAT_WINDOWS
4662 || (had_tab > 0 && wp != firstwin)
4663 #endif
4664 ) && firstwin != lastwin)
4666 win_close(wp, FALSE);
4667 #ifdef FEAT_AUTOCMD
4668 wpnext = firstwin; /* just in case an autocommand does
4669 something strange with windows */
4670 tpnext = first_tabpage; /* start all over...*/
4671 open_wins = 0;
4672 #endif
4674 else
4675 ++open_wins;
4678 #ifdef FEAT_WINDOWS
4679 /* Without the ":tab" modifier only do the current tab page. */
4680 if (had_tab == 0 || tpnext == NULL)
4681 break;
4682 goto_tabpage_tp(tpnext);
4684 #endif
4687 * Go through the buffer list. When a buffer doesn't have a window yet,
4688 * open one. Otherwise move the window to the right position.
4689 * Watch out for autocommands that delete buffers or windows!
4691 #ifdef FEAT_AUTOCMD
4692 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4693 ++autocmd_no_enter;
4694 #endif
4695 win_enter(lastwin, FALSE);
4696 #ifdef FEAT_AUTOCMD
4697 ++autocmd_no_leave;
4698 #endif
4699 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4701 /* Check if this buffer needs a window */
4702 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4703 continue;
4705 #ifdef FEAT_WINDOWS
4706 if (had_tab != 0)
4708 /* With the ":tab" modifier don't move the window. */
4709 if (buf->b_nwindows > 0)
4710 wp = lastwin; /* buffer has a window, skip it */
4711 else
4712 wp = NULL;
4714 else
4715 #endif
4717 /* Check if this buffer already has a window */
4718 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4719 if (wp->w_buffer == buf)
4720 break;
4721 /* If the buffer already has a window, move it */
4722 if (wp != NULL)
4723 win_move_after(wp, curwin);
4726 if (wp == NULL && split_ret == OK)
4728 /* Split the window and put the buffer in it */
4729 p_ea_save = p_ea;
4730 p_ea = TRUE; /* use space from all windows */
4731 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4732 ++open_wins;
4733 p_ea = p_ea_save;
4734 if (split_ret == FAIL)
4735 continue;
4737 /* Open the buffer in this window. */
4738 #if defined(HAS_SWAP_EXISTS_ACTION)
4739 swap_exists_action = SEA_DIALOG;
4740 #endif
4741 set_curbuf(buf, DOBUF_GOTO);
4742 #ifdef FEAT_AUTOCMD
4743 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
4745 #if defined(HAS_SWAP_EXISTS_ACTION)
4746 swap_exists_action = SEA_NONE;
4747 # endif
4748 break;
4750 #endif
4751 #if defined(HAS_SWAP_EXISTS_ACTION)
4752 if (swap_exists_action == SEA_QUIT)
4754 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4755 cleanup_T cs;
4757 /* Reset the error/interrupt/exception state here so that
4758 * aborting() returns FALSE when closing a window. */
4759 enter_cleanup(&cs);
4760 # endif
4762 /* User selected Quit at ATTENTION prompt; close this window. */
4763 win_close(curwin, TRUE);
4764 --open_wins;
4765 swap_exists_action = SEA_NONE;
4766 swap_exists_did_quit = TRUE;
4768 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4769 /* Restore the error/interrupt/exception state if not
4770 * discarded by a new aborting error, interrupt, or uncaught
4771 * exception. */
4772 leave_cleanup(&cs);
4773 # endif
4775 else
4776 handle_swap_exists(NULL);
4777 #endif
4780 ui_breakcheck();
4781 if (got_int)
4783 (void)vgetc(); /* only break the file loading, not the rest */
4784 break;
4786 #ifdef FEAT_EVAL
4787 /* Autocommands deleted the buffer or aborted script processing!!! */
4788 if (aborting())
4789 break;
4790 #endif
4791 #ifdef FEAT_WINDOWS
4792 /* When ":tab" was used open a new tab for a new window repeatedly. */
4793 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4794 cmdmod.tab = 9999;
4795 #endif
4797 #ifdef FEAT_AUTOCMD
4798 --autocmd_no_enter;
4799 #endif
4800 win_enter(firstwin, FALSE); /* back to first window */
4801 #ifdef FEAT_AUTOCMD
4802 --autocmd_no_leave;
4803 #endif
4806 * Close superfluous windows.
4808 for (wp = lastwin; open_wins > count; )
4810 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4811 || autowrite(wp->w_buffer, FALSE) == OK);
4812 #ifdef FEAT_AUTOCMD
4813 if (!win_valid(wp))
4815 /* BufWrite Autocommands made the window invalid, start over */
4816 wp = lastwin;
4818 else
4819 #endif
4820 if (r)
4822 win_close(wp, !P_HID(wp->w_buffer));
4823 --open_wins;
4824 wp = lastwin;
4826 else
4828 wp = wp->w_prev;
4829 if (wp == NULL)
4830 break;
4834 # endif /* FEAT_LISTCMDS */
4836 #endif /* FEAT_WINDOWS */
4838 static int chk_modeline __ARGS((linenr_T, int));
4841 * do_modelines() - process mode lines for the current file
4843 * "flags" can be:
4844 * OPT_WINONLY only set options local to window
4845 * OPT_NOWIN don't set options local to window
4847 * Returns immediately if the "ml" option isn't set.
4849 void
4850 do_modelines(flags)
4851 int flags;
4853 linenr_T lnum;
4854 int nmlines;
4855 static int entered = 0;
4857 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4858 return;
4860 /* Disallow recursive entry here. Can happen when executing a modeline
4861 * triggers an autocommand, which reloads modelines with a ":do". */
4862 if (entered)
4863 return;
4865 ++entered;
4866 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4867 ++lnum)
4868 if (chk_modeline(lnum, flags) == FAIL)
4869 nmlines = 0;
4871 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4872 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
4873 if (chk_modeline(lnum, flags) == FAIL)
4874 nmlines = 0;
4875 --entered;
4878 #include "version.h" /* for version number */
4881 * chk_modeline() - check a single line for a mode string
4882 * Return FAIL if an error encountered.
4884 static int
4885 chk_modeline(lnum, flags)
4886 linenr_T lnum;
4887 int flags; /* Same as for do_modelines(). */
4889 char_u *s;
4890 char_u *e;
4891 char_u *linecopy; /* local copy of any modeline found */
4892 int prev;
4893 int vers;
4894 int end;
4895 int retval = OK;
4896 char_u *save_sourcing_name;
4897 linenr_T save_sourcing_lnum;
4898 #ifdef FEAT_EVAL
4899 scid_T save_SID;
4900 #endif
4902 prev = -1;
4903 for (s = ml_get(lnum); *s != NUL; ++s)
4905 if (prev == -1 || vim_isspace(prev))
4907 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4908 || STRNCMP(s, "vi:", (size_t)3) == 0)
4909 break;
4910 if (STRNCMP(s, "vim", 3) == 0)
4912 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4913 e = s + 4;
4914 else
4915 e = s + 3;
4916 vers = getdigits(&e);
4917 if (*e == ':'
4918 && (s[3] == ':'
4919 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4920 || (VIM_VERSION_100 < vers && s[3] == '<')
4921 || (VIM_VERSION_100 > vers && s[3] == '>')
4922 || (VIM_VERSION_100 == vers && s[3] == '=')))
4923 break;
4926 prev = *s;
4929 if (*s)
4931 do /* skip over "ex:", "vi:" or "vim:" */
4932 ++s;
4933 while (s[-1] != ':');
4935 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4936 if (linecopy == NULL)
4937 return FAIL;
4939 save_sourcing_lnum = sourcing_lnum;
4940 save_sourcing_name = sourcing_name;
4941 sourcing_lnum = lnum; /* prepare for emsg() */
4942 sourcing_name = (char_u *)"modelines";
4944 end = FALSE;
4945 while (end == FALSE)
4947 s = skipwhite(s);
4948 if (*s == NUL)
4949 break;
4952 * Find end of set command: ':' or end of line.
4953 * Skip over "\:", replacing it with ":".
4955 for (e = s; *e != ':' && *e != NUL; ++e)
4956 if (e[0] == '\\' && e[1] == ':')
4957 STRMOVE(e, e + 1);
4958 if (*e == NUL)
4959 end = TRUE;
4962 * If there is a "set" command, require a terminating ':' and
4963 * ignore the stuff after the ':'.
4964 * "vi:set opt opt opt: foo" -- foo not interpreted
4965 * "vi:opt opt opt: foo" -- foo interpreted
4966 * Accept "se" for compatibility with Elvis.
4968 if (STRNCMP(s, "set ", (size_t)4) == 0
4969 || STRNCMP(s, "se ", (size_t)3) == 0)
4971 if (*e != ':') /* no terminating ':'? */
4972 break;
4973 end = TRUE;
4974 s = vim_strchr(s, ' ') + 1;
4976 *e = NUL; /* truncate the set command */
4978 if (*s != NUL) /* skip over an empty "::" */
4980 #ifdef FEAT_EVAL
4981 save_SID = current_SID;
4982 current_SID = SID_MODELINE;
4983 #endif
4984 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
4985 #ifdef FEAT_EVAL
4986 current_SID = save_SID;
4987 #endif
4988 if (retval == FAIL) /* stop if error found */
4989 break;
4991 s = e + 1; /* advance to next part */
4994 sourcing_lnum = save_sourcing_lnum;
4995 sourcing_name = save_sourcing_name;
4997 vim_free(linecopy);
4999 return retval;
5002 #if defined(FEAT_VIMINFO) || defined(PROTO)
5004 read_viminfo_bufferlist(virp, writing)
5005 vir_T *virp;
5006 int writing;
5008 char_u *tab;
5009 linenr_T lnum;
5010 colnr_T col;
5011 buf_T *buf;
5012 char_u *sfname;
5013 char_u *xline;
5015 /* Handle long line and escaped characters. */
5016 xline = viminfo_readstring(virp, 1, FALSE);
5018 /* don't read in if there are files on the command-line or if writing: */
5019 if (xline != NULL && !writing && ARGCOUNT == 0
5020 && find_viminfo_parameter('%') != NULL)
5022 /* Format is: <fname> Tab <lnum> Tab <col>.
5023 * Watch out for a Tab in the file name, work from the end. */
5024 lnum = 0;
5025 col = 0;
5026 tab = vim_strrchr(xline, '\t');
5027 if (tab != NULL)
5029 *tab++ = '\0';
5030 col = (colnr_T)atoi((char *)tab);
5031 tab = vim_strrchr(xline, '\t');
5032 if (tab != NULL)
5034 *tab++ = '\0';
5035 lnum = atol((char *)tab);
5039 /* Expand "~/" in the file name at "line + 1" to a full path.
5040 * Then try shortening it by comparing with the current directory */
5041 expand_env(xline, NameBuff, MAXPATHL);
5042 sfname = shorten_fname1(NameBuff);
5044 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5045 if (buf != NULL) /* just in case... */
5047 buf->b_last_cursor.lnum = lnum;
5048 buf->b_last_cursor.col = col;
5049 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5052 vim_free(xline);
5054 return viminfo_readline(virp);
5057 void
5058 write_viminfo_bufferlist(fp)
5059 FILE *fp;
5061 buf_T *buf;
5062 #ifdef FEAT_WINDOWS
5063 win_T *win;
5064 tabpage_T *tp;
5065 #endif
5066 char_u *line;
5067 int max_buffers;
5068 size_t len;
5070 if (find_viminfo_parameter('%') == NULL)
5071 return;
5073 /* Without a number -1 is returned: do all buffers. */
5074 max_buffers = get_viminfo_parameter('%');
5076 /* Allocate room for the file name, lnum and col. */
5077 #define LINE_BUF_LEN (MAXPATHL + 40)
5078 line = alloc(LINE_BUF_LEN);
5079 if (line == NULL)
5080 return;
5082 #ifdef FEAT_WINDOWS
5083 FOR_ALL_TAB_WINDOWS(tp, win)
5084 set_last_cursor(win);
5085 #else
5086 set_last_cursor(curwin);
5087 #endif
5089 fprintf(fp, _("\n# Buffer list:\n"));
5090 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5092 if (buf->b_fname == NULL
5093 || !buf->b_p_bl
5094 #ifdef FEAT_QUICKFIX
5095 || bt_quickfix(buf)
5096 #endif
5097 || removable(buf->b_ffname))
5098 continue;
5100 if (max_buffers-- == 0)
5101 break;
5102 putc('%', fp);
5103 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
5104 len = STRLEN(line);
5105 vim_snprintf((char *)line + len, len - LINE_BUF_LEN, "\t%ld\t%d",
5106 (long)buf->b_last_cursor.lnum,
5107 buf->b_last_cursor.col);
5108 viminfo_writestring(fp, line);
5110 vim_free(line);
5112 #endif
5116 * Return special buffer name.
5117 * Returns NULL when the buffer has a normal file name.
5119 char *
5120 buf_spname(buf)
5121 buf_T *buf;
5123 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5124 if (bt_quickfix(buf))
5126 win_T *win = NULL;
5127 tabpage_T *tp;
5130 * For location list window, w_llist_ref points to the location list.
5131 * For quickfix window, w_llist_ref is NULL.
5133 FOR_ALL_TAB_WINDOWS(tp, win)
5134 if (win->w_buffer == buf)
5135 goto win_found;
5136 win_found:
5137 if (win != NULL && win->w_llist_ref != NULL)
5138 return _("[Location List]");
5139 else
5140 return _("[Quickfix List]");
5142 #endif
5143 #ifdef FEAT_QUICKFIX
5144 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5145 * contains the name as specified by the user */
5146 if (bt_nofile(buf))
5148 if (buf->b_sfname != NULL)
5149 return (char *)buf->b_sfname;
5150 return _("[Scratch]");
5152 #endif
5153 if (buf->b_fname == NULL)
5154 return _("[No Name]");
5155 return NULL;
5159 #if defined(FEAT_SIGNS) || defined(PROTO)
5161 * Insert the sign into the signlist.
5163 static void
5164 insert_sign(buf, prev, next, id, lnum, typenr)
5165 buf_T *buf; /* buffer to store sign in */
5166 signlist_T *prev; /* previous sign entry */
5167 signlist_T *next; /* next sign entry */
5168 int id; /* sign ID */
5169 linenr_T lnum; /* line number which gets the mark */
5170 int typenr; /* typenr of sign we are adding */
5172 signlist_T *newsign;
5174 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5175 if (newsign != NULL)
5177 newsign->id = id;
5178 newsign->lnum = lnum;
5179 newsign->typenr = typenr;
5180 newsign->next = next;
5181 #ifdef FEAT_NETBEANS_INTG
5182 newsign->prev = prev;
5183 if (next != NULL)
5184 next->prev = newsign;
5185 #endif
5187 if (prev == NULL)
5189 /* When adding first sign need to redraw the windows to create the
5190 * column for signs. */
5191 if (buf->b_signlist == NULL)
5193 redraw_buf_later(buf, NOT_VALID);
5194 changed_cline_bef_curs();
5197 /* first sign in signlist */
5198 buf->b_signlist = newsign;
5200 else
5201 prev->next = newsign;
5206 * Add the sign into the signlist. Find the right spot to do it though.
5208 void
5209 buf_addsign(buf, id, lnum, typenr)
5210 buf_T *buf; /* buffer to store sign in */
5211 int id; /* sign ID */
5212 linenr_T lnum; /* line number which gets the mark */
5213 int typenr; /* typenr of sign we are adding */
5215 signlist_T *sign; /* a sign in the signlist */
5216 signlist_T *prev; /* the previous sign */
5218 prev = NULL;
5219 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5221 if (lnum == sign->lnum && id == sign->id)
5223 sign->typenr = typenr;
5224 return;
5226 else if (
5227 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5228 id < 0 &&
5229 #endif
5230 lnum < sign->lnum)
5232 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5233 /* XXX - GRP: Is this because of sign slide problem? Or is it
5234 * really needed? Or is it because we allow multiple signs per
5235 * line? If so, should I add that feature to FEAT_SIGNS?
5237 while (prev != NULL && prev->lnum == lnum)
5238 prev = prev->prev;
5239 if (prev == NULL)
5240 sign = buf->b_signlist;
5241 else
5242 sign = prev->next;
5243 #endif
5244 insert_sign(buf, prev, sign, id, lnum, typenr);
5245 return;
5247 prev = sign;
5249 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5250 /* XXX - GRP: See previous comment */
5251 while (prev != NULL && prev->lnum == lnum)
5252 prev = prev->prev;
5253 if (prev == NULL)
5254 sign = buf->b_signlist;
5255 else
5256 sign = prev->next;
5257 #endif
5258 insert_sign(buf, prev, sign, id, lnum, typenr);
5260 return;
5263 linenr_T
5264 buf_change_sign_type(buf, markId, typenr)
5265 buf_T *buf; /* buffer to store sign in */
5266 int markId; /* sign ID */
5267 int typenr; /* typenr of sign we are adding */
5269 signlist_T *sign; /* a sign in the signlist */
5271 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5273 if (sign->id == markId)
5275 sign->typenr = typenr;
5276 return sign->lnum;
5280 return (linenr_T)0;
5284 buf_getsigntype(buf, lnum, type)
5285 buf_T *buf;
5286 linenr_T lnum;
5287 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5289 signlist_T *sign; /* a sign in a b_signlist */
5291 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5292 if (sign->lnum == lnum
5293 && (type == SIGN_ANY
5294 # ifdef FEAT_SIGN_ICONS
5295 || (type == SIGN_ICON
5296 && sign_get_image(sign->typenr) != NULL)
5297 # endif
5298 || (type == SIGN_TEXT
5299 && sign_get_text(sign->typenr) != NULL)
5300 || (type == SIGN_LINEHL
5301 && sign_get_attr(sign->typenr, TRUE) != 0)))
5302 return sign->typenr;
5303 return 0;
5307 linenr_T
5308 buf_delsign(buf, id)
5309 buf_T *buf; /* buffer sign is stored in */
5310 int id; /* sign id */
5312 signlist_T **lastp; /* pointer to pointer to current sign */
5313 signlist_T *sign; /* a sign in a b_signlist */
5314 signlist_T *next; /* the next sign in a b_signlist */
5315 linenr_T lnum; /* line number whose sign was deleted */
5317 lastp = &buf->b_signlist;
5318 lnum = 0;
5319 for (sign = buf->b_signlist; sign != NULL; sign = next)
5321 next = sign->next;
5322 if (sign->id == id)
5324 *lastp = next;
5325 #ifdef FEAT_NETBEANS_INTG
5326 if (next != NULL)
5327 next->prev = sign->prev;
5328 #endif
5329 lnum = sign->lnum;
5330 vim_free(sign);
5331 break;
5333 else
5334 lastp = &sign->next;
5337 /* When deleted the last sign need to redraw the windows to remove the
5338 * sign column. */
5339 if (buf->b_signlist == NULL)
5341 redraw_buf_later(buf, NOT_VALID);
5342 changed_cline_bef_curs();
5345 return lnum;
5350 * Find the line number of the sign with the requested id. If the sign does
5351 * not exist, return 0 as the line number. This will still let the correct file
5352 * get loaded.
5355 buf_findsign(buf, id)
5356 buf_T *buf; /* buffer to store sign in */
5357 int id; /* sign ID */
5359 signlist_T *sign; /* a sign in the signlist */
5361 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5362 if (sign->id == id)
5363 return sign->lnum;
5365 return 0;
5369 buf_findsign_id(buf, lnum)
5370 buf_T *buf; /* buffer whose sign we are searching for */
5371 linenr_T lnum; /* line number of sign */
5373 signlist_T *sign; /* a sign in the signlist */
5375 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5376 if (sign->lnum == lnum)
5377 return sign->id;
5379 return 0;
5383 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5384 /* see if a given type of sign exists on a specific line */
5386 buf_findsigntype_id(buf, lnum, typenr)
5387 buf_T *buf; /* buffer whose sign we are searching for */
5388 linenr_T lnum; /* line number of sign */
5389 int typenr; /* sign type number */
5391 signlist_T *sign; /* a sign in the signlist */
5393 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5394 if (sign->lnum == lnum && sign->typenr == typenr)
5395 return sign->id;
5397 return 0;
5401 # if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5402 /* return the number of icons on the given line */
5404 buf_signcount(buf, lnum)
5405 buf_T *buf;
5406 linenr_T lnum;
5408 signlist_T *sign; /* a sign in the signlist */
5409 int count = 0;
5411 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5412 if (sign->lnum == lnum)
5413 if (sign_get_image(sign->typenr) != NULL)
5414 count++;
5416 return count;
5418 # endif /* FEAT_SIGN_ICONS */
5419 # endif /* FEAT_NETBEANS_INTG */
5423 * Delete signs in buffer "buf".
5425 static void
5426 buf_delete_signs(buf)
5427 buf_T *buf;
5429 signlist_T *next;
5431 while (buf->b_signlist != NULL)
5433 next = buf->b_signlist->next;
5434 vim_free(buf->b_signlist);
5435 buf->b_signlist = next;
5440 * Delete all signs in all buffers.
5442 void
5443 buf_delete_all_signs()
5445 buf_T *buf; /* buffer we are checking for signs */
5447 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5448 if (buf->b_signlist != NULL)
5450 /* Need to redraw the windows to remove the sign column. */
5451 redraw_buf_later(buf, NOT_VALID);
5452 buf_delete_signs(buf);
5457 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5459 void
5460 sign_list_placed(rbuf)
5461 buf_T *rbuf;
5463 buf_T *buf;
5464 signlist_T *p;
5465 char lbuf[BUFSIZ];
5467 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5468 msg_putchar('\n');
5469 if (rbuf == NULL)
5470 buf = firstbuf;
5471 else
5472 buf = rbuf;
5473 while (buf != NULL)
5475 if (buf->b_signlist != NULL)
5477 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
5478 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5479 msg_putchar('\n');
5481 for (p = buf->b_signlist; p != NULL; p = p->next)
5483 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
5484 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5485 MSG_PUTS(lbuf);
5486 msg_putchar('\n');
5488 if (rbuf != NULL)
5489 break;
5490 buf = buf->b_next;
5495 * Adjust a placed sign for inserted/deleted lines.
5497 void
5498 sign_mark_adjust(line1, line2, amount, amount_after)
5499 linenr_T line1;
5500 linenr_T line2;
5501 long amount;
5502 long amount_after;
5504 signlist_T *sign; /* a sign in a b_signlist */
5506 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5508 if (sign->lnum >= line1 && sign->lnum <= line2)
5510 if (amount == MAXLNUM)
5511 sign->lnum = line1;
5512 else
5513 sign->lnum += amount;
5515 else if (sign->lnum > line2)
5516 sign->lnum += amount_after;
5519 #endif /* FEAT_SIGNS */
5522 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5524 void
5525 set_buflisted(on)
5526 int on;
5528 if (on != curbuf->b_p_bl)
5530 curbuf->b_p_bl = on;
5531 #ifdef FEAT_AUTOCMD
5532 if (on)
5533 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5534 else
5535 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5536 #endif
5541 * Read the file for "buf" again and check if the contents changed.
5542 * Return TRUE if it changed or this could not be checked.
5545 buf_contents_changed(buf)
5546 buf_T *buf;
5548 buf_T *newbuf;
5549 int differ = TRUE;
5550 linenr_T lnum;
5551 aco_save_T aco;
5552 exarg_T ea;
5554 /* Allocate a buffer without putting it in the buffer list. */
5555 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5556 if (newbuf == NULL)
5557 return TRUE;
5559 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5560 if (prep_exarg(&ea, buf) == FAIL)
5562 wipe_buffer(newbuf, FALSE);
5563 return TRUE;
5566 /* set curwin/curbuf to buf and save a few things */
5567 aucmd_prepbuf(&aco, newbuf);
5569 if (ml_open(curbuf) == OK
5570 && readfile(buf->b_ffname, buf->b_fname,
5571 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5572 &ea, READ_NEW | READ_DUMMY) == OK)
5574 /* compare the two files line by line */
5575 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5577 differ = FALSE;
5578 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5579 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5581 differ = TRUE;
5582 break;
5586 vim_free(ea.cmd);
5588 /* restore curwin/curbuf and a few other things */
5589 aucmd_restbuf(&aco);
5591 if (curbuf != newbuf) /* safety check */
5592 wipe_buffer(newbuf, FALSE);
5594 return differ;
5598 * Wipe out a buffer and decrement the last buffer number if it was used for
5599 * this buffer. Call this to wipe out a temp buffer that does not contain any
5600 * marks.
5602 void
5603 wipe_buffer(buf, aucmd)
5604 buf_T *buf;
5605 int aucmd UNUSED; /* When TRUE trigger autocommands. */
5607 if (buf->b_fnum == top_file_num - 1)
5608 --top_file_num;
5610 #ifdef FEAT_AUTOCMD
5611 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5612 block_autocmds();
5613 #endif
5614 close_buffer(NULL, buf, DOBUF_WIPE);
5615 #ifdef FEAT_AUTOCMD
5616 if (!aucmd)
5617 unblock_autocmds();
5618 #endif