blob: 4d07eb63e0382ad4937ee5386c1f85448a40b763 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
28
29#include "vim.h"
30
31#if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
32static char_u *buflist_match __ARGS((regprog_T *prog, buf_T *buf));
33# define HAVE_BUFLIST_MATCH
34static char_u *fname_match __ARGS((regprog_T *prog, char_u *name));
35#endif
36static void buflist_setfpos __ARGS((buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options));
37static wininfo_T *find_wininfo __ARGS((buf_T *buf));
38#ifdef UNIX
39static buf_T *buflist_findname_stat __ARGS((char_u *ffname, struct stat *st));
40static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname, struct stat *stp));
41static int buf_same_ino __ARGS((buf_T *buf, struct stat *stp));
42#else
43static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname));
44#endif
45#ifdef FEAT_TITLE
46static int ti_change __ARGS((char_u *str, char_u **last));
47#endif
48static void free_buffer __ARGS((buf_T *));
49static void free_buffer_stuff __ARGS((buf_T *buf, int free_options));
50static void clear_wininfo __ARGS((buf_T *buf));
51
52#ifdef UNIX
53# define dev_T dev_t
54#else
55# define dev_T unsigned
56#endif
57
58#if defined(FEAT_SIGNS)
59static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr));
60static void buf_delete_signs __ARGS((buf_T *buf));
61#endif
62
63/*
64 * Open current buffer, that is: open the memfile and read the file into memory
65 * return FAIL for failure, OK otherwise
66 */
67 int
68open_buffer(read_stdin, eap)
69 int read_stdin; /* read file from stdin */
70 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
71{
72 int retval = OK;
73#ifdef FEAT_AUTOCMD
74 buf_T *old_curbuf;
75#endif
76
77 /*
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.
81 */
82 if (readonlymode && curbuf->b_ffname != NULL
83 && (curbuf->b_flags & BF_NEVERLOADED))
84 curbuf->b_p_ro = TRUE;
85
86 if (ml_open() == FAIL)
87 {
88 /*
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
91 */
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;
96 /*
97 * if there is no memfile at all, exit
98 * This is OK, since there are no changes to loose.
99 */
100 if (curbuf == NULL)
101 {
102 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
103 getout(2);
104 }
105 EMSG(_("E83: Cannot allocate buffer, using other one..."));
106 enter_buffer(curbuf);
107 return FAIL;
108 }
109
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
116
117 /* mark cursor position as being invalid */
118 changed_line_abv_curs();
119
120 if (curbuf->b_ffname != NULL
121#ifdef FEAT_NETBEANS_INTG
122 && netbeansReadFile
123#endif
124 )
125 {
126#ifdef FEAT_NETBEANS_INTG
127 int oldFire = netbeansFireChanges;
128
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();
139 }
140 else if (read_stdin)
141 {
142 int save_bin = curbuf->b_p_bin;
143 linenr_T line_count;
144
145 /*
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.
150 */
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)
156 {
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)
161 {
162 /* Delete the binary lines. */
163 while (--line_count >= 0)
164 ml_delete((linenr_T)1, FALSE);
165 }
166 else
167 {
168 /* Delete the converted lines. */
169 while (curbuf->b_ml.ml_line_count > line_count)
170 ml_delete(line_count, FALSE);
171 }
172 /* Put the cursor on the first line. */
173 curwin->w_cursor.lnum = 1;
174 curwin->w_cursor.col = 0;
175#ifdef FEAT_AUTOCMD
176# ifdef FEAT_EVAL
177 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
178 curbuf, &retval);
179# else
180 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
181# endif
182#endif
183 }
184 }
185
186 /* if first time loading this buffer, init b_chartab[] */
187 if (curbuf->b_flags & BF_NEVERLOADED)
188 (void)buf_init_chartab(curbuf, FALSE);
189
190 /*
191 * Set/reset the Changed flag first, autocmds may change the buffer.
192 * Apply the automatic commands, before processing the modelines.
193 * So the modelines have priority over auto commands.
194 */
195 /* When reading stdin, the buffer contents always needs writing, so set
196 * the changed flag. Unless in readonly mode: "ls | gview -".
197 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
198 if ((read_stdin && !readonlymode && !bufempty())
199#ifdef FEAT_AUTOCMD
200 || modified_was_set /* ":set modified" used in autocmd */
201# ifdef FEAT_EVAL
202 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
203# endif
204#endif
205 || (got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL))
206 changed();
207 else if (retval != FAIL)
208 unchanged(curbuf, FALSE);
209 save_file_ff(curbuf); /* keep this fileformat */
210
211 /* require "!" to overwrite the file, because it wasn't read completely */
212#ifdef FEAT_EVAL
213 if (aborting())
214#else
215 if (got_int)
216#endif
217 curbuf->b_flags |= BF_READERR;
218
219#ifdef FEAT_AUTOCMD
220 /* need to set w_topline, unless some autocommand already did that. */
221 if (!(curwin->w_valid & VALID_TOPLINE))
222 {
223 curwin->w_topline = 1;
224# ifdef FEAT_DIFF
225 curwin->w_topfill = 0;
226# endif
227 }
228# ifdef FEAT_EVAL
229 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
230# else
231 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
232# endif
233#endif
234
235 if (retval != FAIL)
236 {
237#ifdef FEAT_AUTOCMD
238 /*
239 * The autocommands may have changed the current buffer. Apply the
240 * modelines to the correct buffer, if it still exists and is loaded.
241 */
242 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
243 {
244 aco_save_T aco;
245
246 /* Go to the buffer that was opened. */
247 aucmd_prepbuf(&aco, old_curbuf);
248#endif
249 do_modelines();
250 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
251
252#ifdef FEAT_AUTOCMD
253# ifdef FEAT_EVAL
254 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
255 &retval);
256# else
257 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
258# endif
259
260 /* restore curwin/curbuf and a few other things */
261 aucmd_restbuf(&aco);
262 }
263#endif
264 }
265
266#ifdef FEAT_FOLDING
267 /* Need to update automatic folding. */
268 foldUpdateAll(curwin);
269#endif
270
271 return retval;
272}
273
274/*
275 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
276 */
277 int
278buf_valid(buf)
279 buf_T *buf;
280{
281 buf_T *bp;
282
283 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
284 if (bp == buf)
285 return TRUE;
286 return FALSE;
287}
288
289/*
290 * Close the link to a buffer.
291 * "action" is used when there is no longer a window for the buffer.
292 * It can be:
293 * 0 buffer becomes hidden
294 * DOBUF_UNLOAD buffer is unloaded
295 * DOBUF_DELETE buffer is unloaded and removed from buffer list
296 * DOBUF_WIPE buffer is unloaded and really deleted
297 * When doing all but the first one on the current buffer, the caller should
298 * get a new buffer very soon!
299 *
300 * The 'bufhidden' option can force freeing and deleting.
301 */
302 void
303close_buffer(win, buf, action)
304 win_T *win; /* if not NULL, set b_last_cursor */
305 buf_T *buf;
306 int action;
307{
308#ifdef FEAT_AUTOCMD
309 int is_curbuf;
310 int nwindows = buf->b_nwindows;
311#endif
312 int unload_buf = (action != 0);
313 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
314 int wipe_buf = (action == DOBUF_WIPE);
315
316#ifdef FEAT_QUICKFIX
317 /*
318 * Force unloading or deleting when 'bufhidden' says so.
319 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
320 * "hide" (otherwise we could never free or delete a buffer).
321 */
322 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
323 {
324 del_buf = TRUE;
325 unload_buf = TRUE;
326 }
327 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
328 {
329 del_buf = TRUE;
330 unload_buf = TRUE;
331 wipe_buf = TRUE;
332 }
333 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
334 unload_buf = TRUE;
335#endif
336
337 if (win != NULL)
338 {
339 /* Set b_last_cursor when closing the last window for the buffer.
340 * Remember the last cursor position and window options of the buffer.
341 * This used to be only for the current window, but then options like
342 * 'foldmethod' may be lost with a ":only" command. */
343 if (buf->b_nwindows == 1)
344 set_last_cursor(win);
345 buflist_setfpos(buf, win,
346 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
347 win->w_cursor.col, TRUE);
348 }
349
350#ifdef FEAT_AUTOCMD
351 /* When the buffer is no longer in a window, trigger BufWinLeave */
352 if (buf->b_nwindows == 1)
353 {
354 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
355 FALSE, buf);
356 if (!buf_valid(buf)) /* autocommands may delete the buffer */
357 return;
358
359 /* When the buffer becomes hidden, but is not unloaded, trigger
360 * BufHidden */
361 if (!unload_buf)
362 {
363 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
364 FALSE, buf);
365 if (!buf_valid(buf)) /* autocmds may delete the buffer */
366 return;
367 }
368# ifdef FEAT_EVAL
369 if (aborting()) /* autocmds may abort script processing */
370 return;
371# endif
372 }
373 nwindows = buf->b_nwindows;
374#endif
375
376 /* decrease the link count from windows (unless not in any window) */
377 if (buf->b_nwindows > 0)
378 --buf->b_nwindows;
379
380 /* Return when a window is displaying the buffer or when it's not
381 * unloaded. */
382 if (buf->b_nwindows > 0 || !unload_buf)
383 {
384 if (buf == curbuf)
385 u_sync(); /* sync undo before going to another buffer */
386 return;
387 }
388
389 /* Always remove the buffer when there is no file name. */
390 if (buf->b_ffname == NULL)
391 del_buf = TRUE;
392
393 /*
394 * Free all things allocated for this buffer.
395 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
396 */
397#ifdef FEAT_AUTOCMD
398 /* Remember if we are closing the current buffer. Restore the number of
399 * windows, so that autocommands in buf_freeall() don't get confused. */
400 is_curbuf = (buf == curbuf);
401 buf->b_nwindows = nwindows;
402#endif
403
404 buf_freeall(buf, del_buf, wipe_buf);
405
406#ifdef FEAT_AUTOCMD
407 /* Autocommands may have deleted the buffer. */
408 if (!buf_valid(buf))
409 return;
410# ifdef FEAT_EVAL
411 /* Autocommands may abort script processing. */
412 if (aborting())
413 return;
414# endif
415
416 /* Autocommands may have opened or closed windows for this buffer.
417 * Decrement the count for the close we do here. */
418 if (buf->b_nwindows > 0)
419 --buf->b_nwindows;
420
421 /*
422 * It's possible that autocommands change curbuf to the one being deleted.
423 * This might cause the previous curbuf to be deleted unexpectedly. But
424 * in some cases it's OK to delete the curbuf, because a new one is
425 * obtained anyway. Therefore only return if curbuf changed to the
426 * deleted buffer.
427 */
428 if (buf == curbuf && !is_curbuf)
429 return;
430#endif
431
432#ifdef FEAT_NETBEANS_INTG
433 if (usingNetbeans)
434 netbeans_file_closed(buf);
435#endif
436#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
437 /* Change directories when the acd option is set on. */
438 if (p_acd && curbuf->b_ffname != NULL
439 && vim_chdirfile(curbuf->b_ffname) == OK)
440 shorten_fnames(TRUE);
441#endif
442
443 /*
444 * Remove the buffer from the list.
445 */
446 if (wipe_buf)
447 {
448#ifdef FEAT_SUN_WORKSHOP
449 if (usingSunWorkShop)
450 workshop_file_closed_lineno((char *)buf->b_ffname,
451 (int)buf->b_last_cursor.lnum);
452#endif
453 vim_free(buf->b_ffname);
454 vim_free(buf->b_sfname);
455 if (buf->b_prev == NULL)
456 firstbuf = buf->b_next;
457 else
458 buf->b_prev->b_next = buf->b_next;
459 if (buf->b_next == NULL)
460 lastbuf = buf->b_prev;
461 else
462 buf->b_next->b_prev = buf->b_prev;
463 free_buffer(buf);
464 }
465 else
466 {
467 if (del_buf)
468 {
469 /* Free all internal variables and reset option values, to make
470 * ":bdel" compatible with Vim 5.7. */
471 free_buffer_stuff(buf, TRUE);
472
473 /* Make it look like a new buffer. */
474 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
475
476 /* Init the options when loaded again. */
477 buf->b_p_initialized = FALSE;
478 }
479 buf_clear_file(buf);
480 if (del_buf)
481 buf->b_p_bl = FALSE;
482 }
483}
484
485/*
486 * Make buffer not contain a file.
487 */
488 void
489buf_clear_file(buf)
490 buf_T *buf;
491{
492 buf->b_ml.ml_line_count = 1;
493 unchanged(buf, TRUE);
494#ifndef SHORT_FNAME
495 buf->b_shortname = FALSE;
496#endif
497 buf->b_p_eol = TRUE;
498 buf->b_start_eol = TRUE;
499#ifdef FEAT_MBYTE
500 buf->b_p_bomb = FALSE;
501#endif
502 buf->b_ml.ml_mfp = NULL;
503 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
504#ifdef FEAT_NETBEANS_INTG
505 netbeans_deleted_all_lines(buf);
506 netbeansOpenFile = 0; /* reset in netbeans_file_opened() */
507#endif
508}
509
510/*
511 * buf_freeall() - free all things allocated for a buffer that are related to
512 * the file.
513 */
514/*ARGSUSED*/
515 void
516buf_freeall(buf, del_buf, wipe_buf)
517 buf_T *buf;
518 int del_buf; /* buffer is going to be deleted */
519 int wipe_buf; /* buffer is going to be wiped out */
520{
521#ifdef FEAT_AUTOCMD
522 int is_curbuf = (buf == curbuf);
523
524 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
525 if (!buf_valid(buf)) /* autocommands may delete the buffer */
526 return;
527 if (del_buf && buf->b_p_bl)
528 {
529 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
530 if (!buf_valid(buf)) /* autocommands may delete the buffer */
531 return;
532 }
533 if (wipe_buf)
534 {
535 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
536 FALSE, buf);
537 if (!buf_valid(buf)) /* autocommands may delete the buffer */
538 return;
539 }
540# ifdef FEAT_EVAL
541 if (aborting()) /* autocmds may abort script processing */
542 return;
543# endif
544
545 /*
546 * It's possible that autocommands change curbuf to the one being deleted.
547 * This might cause curbuf to be deleted unexpectedly. But in some cases
548 * it's OK to delete the curbuf, because a new one is obtained anyway.
549 * Therefore only return if curbuf changed to the deleted buffer.
550 */
551 if (buf == curbuf && !is_curbuf)
552 return;
553#endif
554#ifdef FEAT_DIFF
555 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
556#endif
557#ifdef FEAT_TCL
558 tcl_buffer_free(buf);
559#endif
560 u_blockfree(buf); /* free the memory allocated for undo */
561 ml_close(buf, TRUE); /* close and delete the memline/memfile */
562 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
563 u_clearall(buf); /* reset all undo information */
564#ifdef FEAT_SYN_HL
565 syntax_clear(buf); /* reset syntax info */
566#endif
567}
568
569/*
570 * Free a buffer structure and the things it contains related to the buffer
571 * itself (not the file, that must have been done already).
572 */
573 static void
574free_buffer(buf)
575 buf_T *buf;
576{
577 free_buffer_stuff(buf, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000578#ifdef FEAT_MZSCHEME
579 mzscheme_buffer_free(buf);
580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581#ifdef FEAT_PERL
582 perl_buf_free(buf);
583#endif
584#ifdef FEAT_PYTHON
585 python_buffer_free(buf);
586#endif
587#ifdef FEAT_RUBY
588 ruby_buffer_free(buf);
589#endif
590 vim_free(buf);
591}
592
593/*
594 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
595 */
596 static void
597free_buffer_stuff(buf, free_options)
598 buf_T *buf;
599 int free_options; /* free options as well */
600{
601 if (free_options)
602 {
603 clear_wininfo(buf); /* including window-local options */
604 free_buf_options(buf, TRUE);
605 }
606#ifdef FEAT_EVAL
607 var_clear(&buf->b_vars); /* free all internal variables */
608#endif
609#ifdef FEAT_USR_CMDS
610 uc_clear(&buf->b_ucmds); /* clear local user commands */
611#endif
612#ifdef FEAT_SIGNS
613 buf_delete_signs(buf); /* delete any signs */
614#endif
615#ifdef FEAT_LOCALMAP
616 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
617 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
618#endif
619#ifdef FEAT_MBYTE
620 vim_free(buf->b_start_fenc);
621 buf->b_start_fenc = NULL;
622#endif
623}
624
625/*
626 * Free the b_wininfo list for buffer "buf".
627 */
628 static void
629clear_wininfo(buf)
630 buf_T *buf;
631{
632 wininfo_T *wip;
633
634 while (buf->b_wininfo != NULL)
635 {
636 wip = buf->b_wininfo;
637 buf->b_wininfo = wip->wi_next;
638 if (wip->wi_optset)
639 {
640 clear_winopt(&wip->wi_opt);
641#ifdef FEAT_FOLDING
642 deleteFoldRecurse(&wip->wi_folds);
643#endif
644 }
645 vim_free(wip);
646 }
647}
648
649#if defined(FEAT_LISTCMDS) || defined(PROTO)
650/*
651 * Go to another buffer. Handles the result of the ATTENTION dialog.
652 */
653 void
654goto_buffer(eap, start, dir, count)
655 exarg_T *eap;
656 int start;
657 int dir;
658 int count;
659{
660# if defined(FEAT_WINDOWS) \
661 && (defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG))
662 buf_T *old_curbuf = curbuf;
663
664 swap_exists_action = SEA_DIALOG;
665# endif
666 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
667 start, dir, count, eap->forceit);
668# if defined(FEAT_WINDOWS) \
669 && (defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG))
670 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
671 {
672 /* Quitting means closing the split window, nothing else. */
673 win_close(curwin, TRUE);
674 swap_exists_action = SEA_NONE;
675 }
676 else
677 handle_swap_exists(old_curbuf);
678# endif
679}
680#endif
681
682#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
683/*
684 * Handle the situation of swap_exists_action being set.
685 * It is allowed for "old_curbuf" to be NULL or invalid.
686 */
687 void
688handle_swap_exists(old_curbuf)
689 buf_T *old_curbuf;
690{
691 if (swap_exists_action == SEA_QUIT)
692 {
693 /* User selected Quit at ATTENTION prompt. Go back to previous
694 * buffer. If that buffer is gone or the same as the current one,
695 * open a new, empty buffer. */
696 swap_exists_action = SEA_NONE; /* don't want it again */
697 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
698 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
699 old_curbuf = buflist_new(NULL, NULL, 1L,
700 BLN_CURBUF | BLN_LISTED | BLN_FORCE);
701 if (old_curbuf != NULL)
702 enter_buffer(old_curbuf);
703 /* If "old_curbuf" is NULL we are in big trouble here... */
704 }
705 else if (swap_exists_action == SEA_RECOVER)
706 {
707 /* User selected Recover at ATTENTION prompt. */
708 msg_scroll = TRUE;
709 ml_recover();
710 MSG_PUTS("\n"); /* don't overwrite the last message */
711 cmdline_row = msg_row;
712 do_modelines();
713 }
714 swap_exists_action = SEA_NONE;
715}
716#endif
717
718#if defined(FEAT_LISTCMDS) || defined(PROTO)
719/*
720 * do_bufdel() - delete or unload buffer(s)
721 *
722 * addr_count == 0: ":bdel" - delete current buffer
723 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
724 * buffer "end_bnr", then any other arguments.
725 * addr_count == 2: ":N,N bdel" - delete buffers in range
726 *
727 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
728 * DOBUF_DEL (":bdel")
729 *
730 * Returns error message or NULL
731 */
732 char_u *
733do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
734 int command;
735 char_u *arg; /* pointer to extra arguments */
736 int addr_count;
737 int start_bnr; /* first buffer number in a range */
738 int end_bnr; /* buffer nr or last buffer nr in a range */
739 int forceit;
740{
741 int do_current = 0; /* delete current buffer? */
742 int deleted = 0; /* number of buffers deleted */
743 char_u *errormsg = NULL; /* return value */
744 int bnr; /* buffer number */
745 char_u *p;
746
747#ifdef FEAT_NETBEANS_INTG
748 netbeansCloseFile = 1;
749#endif
750 if (addr_count == 0)
751 {
752 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
753 }
754 else
755 {
756 if (addr_count == 2)
757 {
758 if (*arg) /* both range and argument is not allowed */
759 return (char_u *)_(e_trailing);
760 bnr = start_bnr;
761 }
762 else /* addr_count == 1 */
763 bnr = end_bnr;
764
765 for ( ;!got_int; ui_breakcheck())
766 {
767 /*
768 * delete the current buffer last, otherwise when the
769 * current buffer is deleted, the next buffer becomes
770 * the current one and will be loaded, which may then
771 * also be deleted, etc.
772 */
773 if (bnr == curbuf->b_fnum)
774 do_current = bnr;
775 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
776 forceit) == OK)
777 ++deleted;
778
779 /*
780 * find next buffer number to delete/unload
781 */
782 if (addr_count == 2)
783 {
784 if (++bnr > end_bnr)
785 break;
786 }
787 else /* addr_count == 1 */
788 {
789 arg = skipwhite(arg);
790 if (*arg == NUL)
791 break;
792 if (!VIM_ISDIGIT(*arg))
793 {
794 p = skiptowhite_esc(arg);
795 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
796 if (bnr < 0) /* failed */
797 break;
798 arg = p;
799 }
800 else
801 bnr = getdigits(&arg);
802 }
803 }
804 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
805 FORWARD, do_current, forceit) == OK)
806 ++deleted;
807
808 if (deleted == 0)
809 {
810 if (command == DOBUF_UNLOAD)
811 sprintf((char *)IObuff, _("E515: No buffers were unloaded"));
812 else if (command == DOBUF_DEL)
813 sprintf((char *)IObuff, _("E516: No buffers were deleted"));
814 else
815 sprintf((char *)IObuff, _("E517: No buffers were wiped out"));
816 errormsg = IObuff;
817 }
818 else if (deleted >= p_report)
819 {
820 if (command == DOBUF_UNLOAD)
821 {
822 if (deleted == 1)
823 MSG(_("1 buffer unloaded"));
824 else
825 smsg((char_u *)_("%d buffers unloaded"), deleted);
826 }
827 else if (command == DOBUF_DEL)
828 {
829 if (deleted == 1)
830 MSG(_("1 buffer deleted"));
831 else
832 smsg((char_u *)_("%d buffers deleted"), deleted);
833 }
834 else
835 {
836 if (deleted == 1)
837 MSG(_("1 buffer wiped out"));
838 else
839 smsg((char_u *)_("%d buffers wiped out"), deleted);
840 }
841 }
842 }
843
844#ifdef FEAT_NETBEANS_INTG
845 netbeansCloseFile = 0;
846#endif
847
848 return errormsg;
849}
850
851/*
852 * Implementation of the commands for the buffer list.
853 *
854 * action == DOBUF_GOTO go to specified buffer
855 * action == DOBUF_SPLIT split window and go to specified buffer
856 * action == DOBUF_UNLOAD unload specified buffer(s)
857 * action == DOBUF_DEL delete specified buffer(s) from buffer list
858 * action == DOBUF_WIPE delete specified buffer(s) really
859 *
860 * start == DOBUF_CURRENT go to "count" buffer from current buffer
861 * start == DOBUF_FIRST go to "count" buffer from first buffer
862 * start == DOBUF_LAST go to "count" buffer from last buffer
863 * start == DOBUF_MOD go to "count" modified buffer from current buffer
864 *
865 * Return FAIL or OK.
866 */
867 int
868do_buffer(action, start, dir, count, forceit)
869 int action;
870 int start;
871 int dir; /* FORWARD or BACKWARD */
872 int count; /* buffer number or number of buffers */
873 int forceit; /* TRUE for :...! */
874{
875 buf_T *buf;
876 buf_T *bp;
877 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
878 || action == DOBUF_WIPE);
879
880 switch (start)
881 {
882 case DOBUF_FIRST: buf = firstbuf; break;
883 case DOBUF_LAST: buf = lastbuf; break;
884 default: buf = curbuf; break;
885 }
886 if (start == DOBUF_MOD) /* find next modified buffer */
887 {
888 while (count-- > 0)
889 {
890 do
891 {
892 buf = buf->b_next;
893 if (buf == NULL)
894 buf = firstbuf;
895 }
896 while (buf != curbuf && !bufIsChanged(buf));
897 }
898 if (!bufIsChanged(buf))
899 {
900 EMSG(_("E84: No modified buffer found"));
901 return FAIL;
902 }
903 }
904 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
905 {
906 while (buf != NULL && buf->b_fnum != count)
907 buf = buf->b_next;
908 }
909 else
910 {
911 bp = NULL;
912 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
913 {
914 /* remember the buffer where we start, we come back there when all
915 * buffers are unlisted. */
916 if (bp == NULL)
917 bp = buf;
918 if (dir == FORWARD)
919 {
920 buf = buf->b_next;
921 if (buf == NULL)
922 buf = firstbuf;
923 }
924 else
925 {
926 buf = buf->b_prev;
927 if (buf == NULL)
928 buf = lastbuf;
929 }
930 /* don't count unlisted buffers */
931 if (unload || buf->b_p_bl)
932 {
933 --count;
934 bp = NULL; /* use this buffer as new starting point */
935 }
936 if (bp == buf)
937 {
938 /* back where we started, didn't find anything. */
939 EMSG(_("E85: There is no listed buffer"));
940 return FAIL;
941 }
942 }
943 }
944
945 if (buf == NULL) /* could not find it */
946 {
947 if (start == DOBUF_FIRST)
948 {
949 /* don't warn when deleting */
950 if (!unload)
951 EMSGN(_("E86: Buffer %ld does not exist"), count);
952 }
953 else if (dir == FORWARD)
954 EMSG(_("E87: Cannot go beyond last buffer"));
955 else
956 EMSG(_("E88: Cannot go before first buffer"));
957 return FAIL;
958 }
959
960#ifdef FEAT_GUI
961 need_mouse_correct = TRUE;
962#endif
963
964#ifdef FEAT_LISTCMDS
965 /*
966 * delete buffer buf from memory and/or the list
967 */
968 if (unload)
969 {
970 int forward;
971 int retval;
972
973 /* When unloading or deleting a buffer that's already unloaded and
974 * unlisted: fail silently. */
975 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
976 return FAIL;
977
978 if (!forceit && bufIsChanged(buf))
979 {
980#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
981 if ((p_confirm || cmdmod.confirm) && p_write)
982 {
983 dialog_changed(buf, FALSE);
984# ifdef FEAT_AUTOCMD
985 if (!buf_valid(buf))
986 /* Autocommand deleted buffer, oops! It's not changed
987 * now. */
988 return FAIL;
989# endif
990 }
991 if (bufIsChanged(buf))
992#endif
993 {
994 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
995 buf->b_fnum);
996 return FAIL;
997 }
998 }
999
1000 /*
1001 * If deleting the last (listed) buffer, make it empty.
1002 * The last (listed) buffer cannot be unloaded.
1003 */
1004 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1005 if (bp->b_p_bl && bp != buf)
1006 break;
1007 if (bp == NULL && buf == curbuf)
1008 {
1009 if (action == DOBUF_UNLOAD)
1010 {
1011 EMSG(_("E90: Cannot unload last buffer"));
1012 return FAIL;
1013 }
1014
1015 /* Close any other windows on this buffer, then make it empty. */
1016#ifdef FEAT_WINDOWS
1017 {
1018 win_T *wp, *nextwp;
1019
1020 for (wp = firstwin; wp != NULL; wp = nextwp)
1021 {
1022 nextwp = wp->w_next;
1023 if (wp != curwin && wp->w_buffer == buf)
1024 {
1025 /* Start all over, autocommands may change the window
1026 * layout. */
1027 nextwp = firstwin;
1028 win_close(wp, FALSE);
1029 }
1030 }
1031 }
1032#endif
1033 setpcmark();
1034 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1035 forceit ? ECMD_FORCEIT : 0);
1036
1037 /*
1038 * do_ecmd() may create a new buffer, then we have to delete
1039 * the old one. But do_ecmd() may have done that already, check
1040 * if the buffer still exists.
1041 */
1042 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1043 close_buffer(NULL, buf, action);
1044 return retval;
1045 }
1046
1047#ifdef FEAT_WINDOWS
1048 /*
1049 * If the deleted buffer is the current one, close the current window
1050 * (unless it's the only window).
1051 */
1052 while (buf == curbuf && firstwin != lastwin)
1053 win_close(curwin, FALSE);
1054#endif
1055
1056 /*
1057 * If the buffer to be deleted is not the current one, delete it here.
1058 */
1059 if (buf != curbuf)
1060 {
1061#ifdef FEAT_WINDOWS
1062 close_windows(buf);
1063#endif
1064 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1065 close_buffer(NULL, buf, action);
1066 return OK;
1067 }
1068
1069 /*
1070 * Deleting the current buffer: Need to find another buffer to go to.
1071 * There must be another, otherwise it would have been handled above.
1072 * First use au_new_curbuf, if it is valid.
1073 * Then prefer the buffer we most recently visited.
1074 * Else try to find one that is loaded, after the current buffer,
1075 * then before the current buffer.
1076 * Finally use any buffer.
1077 */
1078 buf = NULL; /* selected buffer */
1079 bp = NULL; /* used when no loaded buffer found */
1080#ifdef FEAT_AUTOCMD
1081 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1082 buf = au_new_curbuf;
1083# ifdef FEAT_JUMPLIST
1084 else
1085# endif
1086#endif
1087#ifdef FEAT_JUMPLIST
1088 if (curwin->w_jumplistlen > 0)
1089 {
1090 int jumpidx;
1091
1092 jumpidx = curwin->w_jumplistidx - 1;
1093 if (jumpidx < 0)
1094 jumpidx = curwin->w_jumplistlen - 1;
1095
1096 forward = jumpidx;
1097 while (jumpidx != curwin->w_jumplistidx)
1098 {
1099 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1100 if (buf != NULL)
1101 {
1102 if (buf == curbuf || !buf->b_p_bl)
1103 buf = NULL; /* skip current and unlisted bufs */
1104 else if (buf->b_ml.ml_mfp == NULL)
1105 {
1106 /* skip unloaded buf, but may keep it for later */
1107 if (bp == NULL)
1108 bp = buf;
1109 buf = NULL;
1110 }
1111 }
1112 if (buf != NULL) /* found a valid buffer: stop searching */
1113 break;
1114 /* advance to older entry in jump list */
1115 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1116 break;
1117 if (--jumpidx < 0)
1118 jumpidx = curwin->w_jumplistlen - 1;
1119 if (jumpidx == forward) /* List exhausted for sure */
1120 break;
1121 }
1122 }
1123#endif
1124
1125 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1126 {
1127 forward = TRUE;
1128 buf = curbuf->b_next;
1129 for (;;)
1130 {
1131 if (buf == NULL)
1132 {
1133 if (!forward) /* tried both directions */
1134 break;
1135 buf = curbuf->b_prev;
1136 forward = FALSE;
1137 continue;
1138 }
1139 /* in non-help buffer, try to skip help buffers, and vv */
1140 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1141 {
1142 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1143 break;
1144 if (bp == NULL) /* remember unloaded buf for later */
1145 bp = buf;
1146 }
1147 if (forward)
1148 buf = buf->b_next;
1149 else
1150 buf = buf->b_prev;
1151 }
1152 }
1153 if (buf == NULL) /* No loaded buffer, use unloaded one */
1154 buf = bp;
1155 if (buf == NULL) /* No loaded buffer, find listed one */
1156 {
1157 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1158 if (buf->b_p_bl && buf != curbuf)
1159 break;
1160 }
1161 if (buf == NULL) /* Still no buffer, just take one */
1162 {
1163 if (curbuf->b_next != NULL)
1164 buf = curbuf->b_next;
1165 else
1166 buf = curbuf->b_prev;
1167 }
1168 }
1169
1170 /*
1171 * make buf current buffer
1172 */
1173 if (action == DOBUF_SPLIT) /* split window first */
1174 {
1175# ifdef FEAT_WINDOWS
1176 /* jump to first window containing buf if one exists ("useopen") */
1177 if (vim_strchr(p_swb, 'u') && buf_jump_open_win(buf))
1178 return OK;
1179 if (win_split(0, 0) == FAIL)
1180# endif
1181 return FAIL;
1182 }
1183#endif
1184
1185 /* go to current buffer - nothing to do */
1186 if (buf == curbuf)
1187 return OK;
1188
1189 /*
1190 * Check if the current buffer may be abandoned.
1191 */
1192 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1193 {
1194#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1195 if ((p_confirm || cmdmod.confirm) && p_write)
1196 {
1197 dialog_changed(curbuf, FALSE);
1198# ifdef FEAT_AUTOCMD
1199 if (!buf_valid(buf))
1200 /* Autocommand deleted buffer, oops! */
1201 return FAIL;
1202# endif
1203 }
1204 if (bufIsChanged(curbuf))
1205#endif
1206 {
1207 EMSG(_(e_nowrtmsg));
1208 return FAIL;
1209 }
1210 }
1211
1212 /* Go to the other buffer. */
1213 set_curbuf(buf, action);
1214
1215#if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1216 if (action == DOBUF_SPLIT)
1217 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1218#endif
1219
1220#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1221 if (aborting()) /* autocmds may abort script processing */
1222 return FAIL;
1223#endif
1224
1225 return OK;
1226}
1227
1228#endif /* FEAT_LISTCMDS */
1229
1230/*
1231 * Set current buffer to "buf". Executes autocommands and closes current
1232 * buffer. "action" tells how to close the current buffer:
1233 * DOBUF_GOTO free or hide it
1234 * DOBUF_SPLIT nothing
1235 * DOBUF_UNLOAD unload it
1236 * DOBUF_DEL delete it
1237 * DOBUF_WIPE wipe it out
1238 */
1239 void
1240set_curbuf(buf, action)
1241 buf_T *buf;
1242 int action;
1243{
1244 buf_T *prevbuf;
1245 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1246 || action == DOBUF_WIPE);
1247
1248 setpcmark();
1249 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
1250 buflist_altfpos(); /* remember curpos */
1251
1252#ifdef FEAT_VISUAL
1253 /* Don't restart Select mode after switching to another buffer. */
1254 VIsual_reselect = FALSE;
1255#endif
1256
1257 /* close_windows() or apply_autocmds() may change curbuf */
1258 prevbuf = curbuf;
1259
1260#ifdef FEAT_AUTOCMD
1261 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1262# ifdef FEAT_EVAL
1263 if (buf_valid(prevbuf) && !aborting())
1264# else
1265 if (buf_valid(prevbuf))
1266# endif
1267#endif
1268 {
1269#ifdef FEAT_WINDOWS
1270 if (unload)
1271 close_windows(prevbuf);
1272#endif
1273#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1274 if (buf_valid(prevbuf) && !aborting())
1275#else
1276 if (buf_valid(prevbuf))
1277#endif
1278 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1279 unload ? action : (action == DOBUF_GOTO
1280 && !P_HID(prevbuf)
1281 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
1282 }
1283#ifdef FEAT_AUTOCMD
1284# ifdef FEAT_EVAL
1285 /* An autocommand may have deleted buf or aborted the script processing! */
1286 if (buf_valid(buf) && !aborting())
1287# else
1288 if (buf_valid(buf)) /* an autocommand may have deleted buf! */
1289# endif
1290#endif
1291 enter_buffer(buf);
1292}
1293
1294/*
1295 * Enter a new current buffer.
1296 * Old curbuf must have been abandoned already!
1297 */
1298 void
1299enter_buffer(buf)
1300 buf_T *buf;
1301{
1302 /* Copy buffer and window local option values. Not for a help buffer. */
1303 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1304 if (!buf->b_help)
1305 get_winopts(buf);
1306#ifdef FEAT_FOLDING
1307 else
1308 /* Remove all folds in the window. */
1309 clearFolding(curwin);
1310 foldUpdateAll(curwin); /* update folds (later). */
1311#endif
1312
1313 /* Get the buffer in the current window. */
1314 curwin->w_buffer = buf;
1315 curbuf = buf;
1316 ++curbuf->b_nwindows;
1317
1318#ifdef FEAT_DIFF
1319 diff_new_buffer();
1320#endif
1321
1322 /* Cursor on first line by default. */
1323 curwin->w_cursor.lnum = 1;
1324 curwin->w_cursor.col = 0;
1325#ifdef FEAT_VIRTUALEDIT
1326 curwin->w_cursor.coladd = 0;
1327#endif
1328 curwin->w_set_curswant = TRUE;
1329
1330 /* Make sure the buffer is loaded. */
1331 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
1332 open_buffer(FALSE, NULL);
1333 else
1334 {
1335 need_fileinfo = TRUE; /* display file info after redraw */
1336 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1337#ifdef FEAT_AUTOCMD
1338 curwin->w_topline = 1;
1339# ifdef FEAT_DIFF
1340 curwin->w_topfill = 0;
1341# endif
1342 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1343 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1344#endif
1345 }
1346
1347 /* If autocommands did not change the cursor position, restore cursor lnum
1348 * and possibly cursor col. */
1349 if (curwin->w_cursor.lnum == 1 && inindent(0))
1350 buflist_getfpos();
1351
1352 check_arg_idx(curwin); /* check for valid arg_idx */
1353#ifdef FEAT_TITLE
1354 maketitle();
1355#endif
1356#ifdef FEAT_AUTOCMD
1357 if (curwin->w_topline == 1) /* when autocmds didn't change it */
1358#endif
1359 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1360
1361#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
1362 /* Change directories when the acd option is set on. */
1363 if (p_acd && curbuf->b_ffname != NULL
1364 && vim_chdirfile(curbuf->b_ffname) == OK)
1365 shorten_fnames(TRUE);
1366#endif
1367
1368#ifdef FEAT_KEYMAP
1369 if (curbuf->b_kmap_state & KEYMAP_INIT)
1370 keymap_init();
1371#endif
1372 redraw_later(NOT_VALID);
1373}
1374
1375/*
1376 * functions for dealing with the buffer list
1377 */
1378
1379/*
1380 * Add a file name to the buffer list. Return a pointer to the buffer.
1381 * If the same file name already exists return a pointer to that buffer.
1382 * If it does not exist, or if fname == NULL, a new entry is created.
1383 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1384 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1385 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1386 * If (flags & BLN_FORCE) is TRUE, don't abort on an error.
1387 * This is the ONLY way to create a new buffer.
1388 */
1389static int top_file_num = 1; /* highest file number */
1390
1391 buf_T *
1392buflist_new(ffname, sfname, lnum, flags)
1393 char_u *ffname; /* full path of fname or relative */
1394 char_u *sfname; /* short fname or NULL */
1395 linenr_T lnum; /* preferred cursor line */
1396 int flags; /* BLN_ defines */
1397{
1398 buf_T *buf;
1399#ifdef UNIX
1400 struct stat st;
1401#endif
1402
1403 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1404
1405 /*
1406 * If file name already exists in the list, update the entry.
1407 */
1408#ifdef UNIX
1409 /* On Unix we can use inode numbers when the file exists. Works better
1410 * for hard links. */
1411 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1412 st.st_dev = (dev_T)-1;
1413#endif
1414 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1415#ifdef UNIX
1416 buflist_findname_stat(ffname, &st)
1417#else
1418 buflist_findname(ffname)
1419#endif
1420 ) != NULL)
1421 {
1422 vim_free(ffname);
1423 if (lnum != 0)
1424 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1425 /* copy the options now, if 'cpo' doesn't have 's' and not done
1426 * already */
1427 buf_copy_options(buf, 0);
1428 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1429 {
1430 buf->b_p_bl = TRUE;
1431#ifdef FEAT_AUTOCMD
1432 if (!(flags & BLN_DUMMY))
1433 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1434#endif
1435 }
1436 return buf;
1437 }
1438
1439 /*
1440 * If the current buffer has no name and no contents, use the current
1441 * buffer. Otherwise: Need to allocate a new buffer structure.
1442 *
1443 * This is the ONLY place where a new buffer structure is allocated!
1444 */
1445 buf = NULL;
1446 if ((flags & BLN_CURBUF)
1447 && curbuf != NULL
1448 && curbuf->b_ffname == NULL
1449 && curbuf->b_nwindows <= 1
1450 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1451 {
1452 buf = curbuf;
1453#ifdef FEAT_AUTOCMD
1454 /* It's like this buffer is deleted. Watch out for autocommands that
1455 * change curbuf! If that happens, allocate a new buffer anyway. */
1456 if (curbuf->b_p_bl)
1457 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1458 if (buf == curbuf)
1459 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1460# ifdef FEAT_EVAL
1461 /* autocmds may abort script processing */
1462 if (!(flags & BLN_FORCE) && aborting())
1463 return NULL;
1464# endif
1465#endif
1466#ifdef FEAT_QUICKFIX
1467# ifdef FEAT_AUTOCMD
1468 if (buf == curbuf)
1469# endif
1470 {
1471 /* Make sure 'bufhidden' and 'buftype' are empty */
1472 clear_string_option(&buf->b_p_bh);
1473 clear_string_option(&buf->b_p_bt);
1474 }
1475#endif
1476 }
1477 if (buf != curbuf || curbuf == NULL)
1478 {
1479 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1480 if (buf == NULL)
1481 {
1482 vim_free(ffname);
1483 return NULL;
1484 }
1485 }
1486
1487 if (ffname != NULL)
1488 {
1489 buf->b_ffname = ffname;
1490 buf->b_sfname = vim_strsave(sfname);
1491 }
1492
1493 clear_wininfo(buf);
1494 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1495
1496 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1497 || buf->b_wininfo == NULL)
1498 {
1499 vim_free(buf->b_ffname);
1500 buf->b_ffname = NULL;
1501 vim_free(buf->b_sfname);
1502 buf->b_sfname = NULL;
1503 if (buf != curbuf)
1504 free_buffer(buf);
1505 return NULL;
1506 }
1507
1508 if (buf == curbuf)
1509 {
1510 /* free all things allocated for this buffer */
1511 buf_freeall(buf, FALSE, FALSE);
1512 if (buf != curbuf) /* autocommands deleted the buffer! */
1513 return NULL;
1514#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1515 /* autocmds may abort script processing */
1516 if (!(flags & BLN_FORCE) && aborting())
1517 return NULL;
1518#endif
1519 /* buf->b_nwindows = 0; why was this here? */
1520 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1521#ifdef FEAT_KEYMAP
1522 /* need to reload lmaps and set b:keymap_name */
1523 curbuf->b_kmap_state |= KEYMAP_INIT;
1524#endif
1525 }
1526 else
1527 {
1528 /*
1529 * put new buffer at the end of the buffer list
1530 */
1531 buf->b_next = NULL;
1532 if (firstbuf == NULL) /* buffer list is empty */
1533 {
1534 buf->b_prev = NULL;
1535 firstbuf = buf;
1536 }
1537 else /* append new buffer at end of list */
1538 {
1539 lastbuf->b_next = buf;
1540 buf->b_prev = lastbuf;
1541 }
1542 lastbuf = buf;
1543
1544 buf->b_fnum = top_file_num++;
1545 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1546 {
1547 EMSG(_("W14: Warning: List of file names overflow"));
1548 if (emsg_silent == 0)
1549 {
1550 out_flush();
1551 ui_delay(3000L, TRUE); /* make sure it is noticed */
1552 }
1553 top_file_num = 1;
1554 }
1555
1556 /*
1557 * Always copy the options from the current buffer.
1558 */
1559 buf_copy_options(buf, BCO_ALWAYS);
1560 }
1561
1562 buf->b_wininfo->wi_fpos.lnum = lnum;
1563 buf->b_wininfo->wi_win = curwin;
1564
1565#ifdef FEAT_EVAL
1566 var_init(&buf->b_vars); /* init internal variables */
1567#endif
1568
1569 buf->b_fname = buf->b_sfname;
1570#ifdef UNIX
1571 if (st.st_dev == (dev_T)-1)
1572 buf->b_dev = -1;
1573 else
1574 {
1575 buf->b_dev = st.st_dev;
1576 buf->b_ino = st.st_ino;
1577 }
1578#endif
1579 buf->b_u_synced = TRUE;
1580 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
1581 buf_clear_file(buf);
1582 clrallmarks(buf); /* clear marks */
1583 fmarks_check_names(buf); /* check file marks for this file */
1584 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1585#ifdef FEAT_AUTOCMD
1586 if (!(flags & BLN_DUMMY))
1587 {
1588 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1589 if (flags & BLN_LISTED)
1590 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1591# ifdef FEAT_EVAL
1592 /* autocmds may abort script processing */
1593 if (!(flags & BLN_FORCE) && aborting())
1594 return NULL;
1595# endif
1596 }
1597#endif
1598
1599 return buf;
1600}
1601
1602/*
1603 * Free the memory for the options of a buffer.
1604 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1605 * 'fileencoding'.
1606 */
1607 void
1608free_buf_options(buf, free_p_ff)
1609 buf_T *buf;
1610 int free_p_ff;
1611{
1612 if (free_p_ff)
1613 {
1614#ifdef FEAT_MBYTE
1615 clear_string_option(&buf->b_p_fenc);
1616#endif
1617 clear_string_option(&buf->b_p_ff);
1618#ifdef FEAT_QUICKFIX
1619 clear_string_option(&buf->b_p_bh);
1620 clear_string_option(&buf->b_p_bt);
1621#endif
1622 }
1623#ifdef FEAT_FIND_ID
1624 clear_string_option(&buf->b_p_def);
1625 clear_string_option(&buf->b_p_inc);
1626# ifdef FEAT_EVAL
1627 clear_string_option(&buf->b_p_inex);
1628# endif
1629#endif
1630#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1631 clear_string_option(&buf->b_p_inde);
1632 clear_string_option(&buf->b_p_indk);
1633#endif
1634#ifdef FEAT_CRYPT
1635 clear_string_option(&buf->b_p_key);
1636#endif
1637 clear_string_option(&buf->b_p_kp);
1638 clear_string_option(&buf->b_p_mps);
1639 clear_string_option(&buf->b_p_fo);
1640 clear_string_option(&buf->b_p_isk);
1641#ifdef FEAT_KEYMAP
1642 clear_string_option(&buf->b_p_keymap);
1643 ga_clear(&buf->b_kmap_ga);
1644#endif
1645#ifdef FEAT_COMMENTS
1646 clear_string_option(&buf->b_p_com);
1647#endif
1648#ifdef FEAT_FOLDING
1649 clear_string_option(&buf->b_p_cms);
1650#endif
1651 clear_string_option(&buf->b_p_nf);
1652#ifdef FEAT_SYN_HL
1653 clear_string_option(&buf->b_p_syn);
1654#endif
1655#ifdef FEAT_SEARCHPATH
1656 clear_string_option(&buf->b_p_sua);
1657#endif
1658#ifdef FEAT_AUTOCMD
1659 clear_string_option(&buf->b_p_ft);
1660#endif
1661#ifdef FEAT_OSFILETYPE
1662 clear_string_option(&buf->b_p_oft);
1663#endif
1664#ifdef FEAT_CINDENT
1665 clear_string_option(&buf->b_p_cink);
1666 clear_string_option(&buf->b_p_cino);
1667#endif
1668#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1669 clear_string_option(&buf->b_p_cinw);
1670#endif
1671#ifdef FEAT_INS_EXPAND
1672 clear_string_option(&buf->b_p_cpt);
1673#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001674#ifdef FEAT_COMPL_FUNC
1675 clear_string_option(&buf->b_p_cfu);
1676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677#ifdef FEAT_QUICKFIX
1678 clear_string_option(&buf->b_p_gp);
1679 clear_string_option(&buf->b_p_mp);
1680 clear_string_option(&buf->b_p_efm);
1681#endif
1682 clear_string_option(&buf->b_p_ep);
1683 clear_string_option(&buf->b_p_path);
1684 clear_string_option(&buf->b_p_tags);
1685#ifdef FEAT_INS_EXPAND
1686 clear_string_option(&buf->b_p_dict);
1687 clear_string_option(&buf->b_p_tsr);
1688#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001689#ifdef FEAT_TEXTOBJ
1690 clear_string_option(&buf->b_p_qe);
1691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 buf->b_p_ar = -1;
1693}
1694
1695/*
1696 * get alternate file n
1697 * set linenr to lnum or altfpos.lnum if lnum == 0
1698 * also set cursor column to altfpos.col if 'startofline' is not set.
1699 * if (options & GETF_SETMARK) call setpcmark()
1700 * if (options & GETF_ALT) we are jumping to an alternate file.
1701 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1702 *
1703 * return FAIL for failure, OK for success
1704 */
1705 int
1706buflist_getfile(n, lnum, options, forceit)
1707 int n;
1708 linenr_T lnum;
1709 int options;
1710 int forceit;
1711{
1712 buf_T *buf;
1713#ifdef FEAT_WINDOWS
1714 win_T *wp = NULL;
1715#endif
1716 pos_T *fpos;
1717 colnr_T col;
1718
1719 buf = buflist_findnr(n);
1720 if (buf == NULL)
1721 {
1722 if ((options & GETF_ALT) && n == 0)
1723 EMSG(_(e_noalt));
1724 else
1725 EMSGN(_("E92: Buffer %ld not found"), n);
1726 return FAIL;
1727 }
1728
1729 /* if alternate file is the current buffer, nothing to do */
1730 if (buf == curbuf)
1731 return OK;
1732
1733#ifdef FEAT_CMDWIN
1734 if (cmdwin_type != 0)
1735 return FAIL;
1736#endif
1737
1738 /* altfpos may be changed by getfile(), get it now */
1739 if (lnum == 0)
1740 {
1741 fpos = buflist_findfpos(buf);
1742 lnum = fpos->lnum;
1743 col = fpos->col;
1744 }
1745 else
1746 col = 0;
1747
1748#ifdef FEAT_WINDOWS
1749 if (options & GETF_SWITCH)
1750 {
1751 /* use existing open window for buffer if wanted */
1752 if (vim_strchr(p_swb, 'u')) /* useopen */
1753 wp = buf_jump_open_win(buf);
1754 /* split window if wanted ("split") */
1755 if (wp == NULL && vim_strchr(p_swb, 't') && !bufempty())
1756 {
1757 if (win_split(0, 0) == FAIL)
1758 return FAIL;
1759# ifdef FEAT_SCROLLBIND
1760 curwin->w_p_scb = FALSE;
1761# endif
1762 }
1763 }
1764#endif
1765
1766 ++RedrawingDisabled;
1767 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1768 lnum, forceit) <= 0)
1769 {
1770 --RedrawingDisabled;
1771
1772 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1773 if (!p_sol && col != 0)
1774 {
1775 curwin->w_cursor.col = col;
1776 check_cursor_col();
1777#ifdef FEAT_VIRTUALEDIT
1778 curwin->w_cursor.coladd = 0;
1779#endif
1780 curwin->w_set_curswant = TRUE;
1781 }
1782 return OK;
1783 }
1784 --RedrawingDisabled;
1785 return FAIL;
1786}
1787
1788/*
1789 * go to the last know line number for the current buffer
1790 */
1791 void
1792buflist_getfpos()
1793{
1794 pos_T *fpos;
1795
1796 fpos = buflist_findfpos(curbuf);
1797
1798 curwin->w_cursor.lnum = fpos->lnum;
1799 check_cursor_lnum();
1800
1801 if (p_sol)
1802 curwin->w_cursor.col = 0;
1803 else
1804 {
1805 curwin->w_cursor.col = fpos->col;
1806 check_cursor_col();
1807#ifdef FEAT_VIRTUALEDIT
1808 curwin->w_cursor.coladd = 0;
1809#endif
1810 curwin->w_set_curswant = TRUE;
1811 }
1812}
1813
1814/*
1815 * Find file in buffer list by name (it has to be for the current window).
1816 * "ffname" must have a full path.
1817 */
1818 buf_T *
1819buflist_findname(ffname)
1820 char_u *ffname;
1821{
1822#ifdef UNIX
1823 struct stat st;
1824
1825 if (mch_stat((char *)ffname, &st) < 0)
1826 st.st_dev = (dev_T)-1;
1827 return buflist_findname_stat(ffname, &st);
1828}
1829
1830/*
1831 * Same as buflist_findname(), but pass the stat structure to avoid getting it
1832 * twice for the same file.
1833 */
1834 static buf_T *
1835buflist_findname_stat(ffname, stp)
1836 char_u *ffname;
1837 struct stat *stp;
1838{
1839#endif
1840 buf_T *buf;
1841
1842 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1843 if (!otherfile_buf(buf, ffname
1844#ifdef UNIX
1845 , stp
1846#endif
1847 ))
1848 return buf;
1849 return NULL;
1850}
1851
1852#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
1853/*
1854 * Find file in buffer list by a regexp pattern.
1855 * Return fnum of the found buffer.
1856 * Return < 0 for error.
1857 */
1858/*ARGSUSED*/
1859 int
1860buflist_findpat(pattern, pattern_end, unlisted, diffmode)
1861 char_u *pattern;
1862 char_u *pattern_end; /* pointer to first char after pattern */
1863 int unlisted; /* find unlisted buffers */
1864 int diffmode; /* find diff-mode buffers only */
1865{
1866 buf_T *buf;
1867 regprog_T *prog;
1868 int match = -1;
1869 int find_listed;
1870 char_u *pat;
1871 char_u *patend;
1872 int attempt;
1873 char_u *p;
1874 int toggledollar;
1875
1876 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
1877 {
1878 if (*pattern == '%')
1879 match = curbuf->b_fnum;
1880 else
1881 match = curwin->w_alt_fnum;
1882#ifdef FEAT_DIFF
1883 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
1884 match = -1;
1885#endif
1886 }
1887
1888 /*
1889 * Try four ways of matching a listed buffer:
1890 * attempt == 0: without '^' or '$' (at any position)
1891 * attempt == 1: with '^' at start (only at postion 0)
1892 * attempt == 2: with '$' at end (only match at end)
1893 * attempt == 3: with '^' at start and '$' at end (only full match)
1894 * Repeat this for finding an unlisted buffer if there was no matching
1895 * listed buffer.
1896 */
1897 else
1898 {
1899 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
1900 if (pat == NULL)
1901 return -1;
1902 patend = pat + STRLEN(pat) - 1;
1903 toggledollar = (patend > pat && *patend == '$');
1904
1905 /* First try finding a listed buffer. If not found and "unlisted"
1906 * is TRUE, try finding an unlisted buffer. */
1907 find_listed = TRUE;
1908 for (;;)
1909 {
1910 for (attempt = 0; attempt <= 3; ++attempt)
1911 {
1912 /* may add '^' and '$' */
1913 if (toggledollar)
1914 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
1915 p = pat;
1916 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
1917 ++p;
1918 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
1919 if (prog == NULL)
1920 {
1921 vim_free(pat);
1922 return -1;
1923 }
1924
1925 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1926 if (buf->b_p_bl == find_listed
1927#ifdef FEAT_DIFF
1928 && (!diffmode || diff_mode_buf(buf))
1929#endif
1930 && buflist_match(prog, buf) != NULL)
1931 {
1932 if (match >= 0) /* already found a match */
1933 {
1934 match = -2;
1935 break;
1936 }
1937 match = buf->b_fnum; /* remember first match */
1938 }
1939
1940 vim_free(prog);
1941 if (match >= 0) /* found one match */
1942 break;
1943 }
1944
1945 /* Only search for unlisted buffers if there was no match with
1946 * a listed buffer. */
1947 if (!unlisted || !find_listed || match != -1)
1948 break;
1949 find_listed = FALSE;
1950 }
1951
1952 vim_free(pat);
1953 }
1954
1955 if (match == -2)
1956 EMSG2(_("E93: More than one match for %s"), pattern);
1957 else if (match < 0)
1958 EMSG2(_("E94: No matching buffer for %s"), pattern);
1959 return match;
1960}
1961#endif
1962
1963#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1964
1965/*
1966 * Find all buffer names that match.
1967 * For command line expansion of ":buf" and ":sbuf".
1968 * Return OK if matches found, FAIL otherwise.
1969 */
1970 int
1971ExpandBufnames(pat, num_file, file, options)
1972 char_u *pat;
1973 int *num_file;
1974 char_u ***file;
1975 int options;
1976{
1977 int count = 0;
1978 buf_T *buf;
1979 int round;
1980 char_u *p;
1981 int attempt;
1982 regprog_T *prog;
1983
1984 *num_file = 0; /* return values in case of FAIL */
1985 *file = NULL;
1986
1987 /*
1988 * attempt == 1: try match with '^', match at start
1989 * attempt == 2: try match without '^', match anywhere
1990 */
1991 for (attempt = 1; attempt <= 2; ++attempt)
1992 {
1993 if (attempt == 2)
1994 {
1995 if (*pat != '^') /* there's no '^', no need to try again */
1996 break;
1997 ++pat; /* skip the '^' */
1998 }
1999 prog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2000 if (prog == NULL)
2001 return FAIL;
2002
2003 /*
2004 * round == 1: Count the matches.
2005 * round == 2: Build the array to keep the matches.
2006 */
2007 for (round = 1; round <= 2; ++round)
2008 {
2009 count = 0;
2010 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2011 {
2012 if (!buf->b_p_bl) /* skip unlisted buffers */
2013 continue;
2014 p = buflist_match(prog, buf);
2015 if (p != NULL)
2016 {
2017 if (round == 1)
2018 ++count;
2019 else
2020 {
2021 if (options & WILD_HOME_REPLACE)
2022 p = home_replace_save(buf, p);
2023 else
2024 p = vim_strsave(p);
2025 (*file)[count++] = p;
2026 }
2027 }
2028 }
2029 if (count == 0) /* no match found, break here */
2030 break;
2031 if (round == 1)
2032 {
2033 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2034 if (*file == NULL)
2035 {
2036 vim_free(prog);
2037 return FAIL;
2038 }
2039 }
2040 }
2041 vim_free(prog);
2042 if (count) /* match(es) found, break here */
2043 break;
2044 }
2045
2046 *num_file = count;
2047 return (count == 0 ? FAIL : OK);
2048}
2049
2050#endif /* FEAT_CMDL_COMPL */
2051
2052#ifdef HAVE_BUFLIST_MATCH
2053/*
2054 * Check for a match on the file name for buffer "buf" with regprog "prog".
2055 */
2056 static char_u *
2057buflist_match(prog, buf)
2058 regprog_T *prog;
2059 buf_T *buf;
2060{
2061 char_u *match;
2062
2063 /* First try the short file name, then the long file name. */
2064 match = fname_match(prog, buf->b_sfname);
2065 if (match == NULL)
2066 match = fname_match(prog, buf->b_ffname);
2067
2068 return match;
2069}
2070
2071/*
2072 * Try matching the regexp in "prog" with file name "name".
2073 * Return "name" when there is a match, NULL when not.
2074 */
2075 static char_u *
2076fname_match(prog, name)
2077 regprog_T *prog;
2078 char_u *name;
2079{
2080 char_u *match = NULL;
2081 char_u *p;
2082 regmatch_T regmatch;
2083
2084 if (name != NULL)
2085 {
2086 regmatch.regprog = prog;
2087#ifdef CASE_INSENSITIVE_FILENAME
2088 regmatch.rm_ic = TRUE; /* Always ignore case */
2089#else
2090 regmatch.rm_ic = FALSE; /* Never ignore case */
2091#endif
2092
2093 if (vim_regexec(&regmatch, name, (colnr_T)0))
2094 match = name;
2095 else
2096 {
2097 /* Replace $(HOME) with '~' and try matching again. */
2098 p = home_replace_save(NULL, name);
2099 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2100 match = name;
2101 vim_free(p);
2102 }
2103 }
2104
2105 return match;
2106}
2107#endif
2108
2109/*
2110 * find file in buffer list by number
2111 */
2112 buf_T *
2113buflist_findnr(nr)
2114 int nr;
2115{
2116 buf_T *buf;
2117
2118 if (nr == 0)
2119 nr = curwin->w_alt_fnum;
2120 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2121 if (buf->b_fnum == nr)
2122 return (buf);
2123 return NULL;
2124}
2125
2126/*
2127 * Get name of file 'n' in the buffer list.
2128 * When the file has no name an empty string is returned.
2129 * home_replace() is used to shorten the file name (used for marks).
2130 * Returns a pointer to allocated memory, of NULL when failed.
2131 */
2132 char_u *
2133buflist_nr2name(n, fullname, helptail)
2134 int n;
2135 int fullname;
2136 int helptail; /* for help buffers return tail only */
2137{
2138 buf_T *buf;
2139
2140 buf = buflist_findnr(n);
2141 if (buf == NULL)
2142 return NULL;
2143 return home_replace_save(helptail ? buf : NULL,
2144 fullname ? buf->b_ffname : buf->b_fname);
2145}
2146
2147/*
2148 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2149 * When "copy_options" is TRUE save the local window option values.
2150 * When "lnum" is 0 only do the options.
2151 */
2152 static void
2153buflist_setfpos(buf, win, lnum, col, copy_options)
2154 buf_T *buf;
2155 win_T *win;
2156 linenr_T lnum;
2157 colnr_T col;
2158 int copy_options;
2159{
2160 wininfo_T *wip;
2161
2162 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2163 if (wip->wi_win == win)
2164 break;
2165 if (wip == NULL)
2166 {
2167 /* allocate a new entry */
2168 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2169 if (wip == NULL)
2170 return;
2171 wip->wi_win = win;
2172 if (lnum == 0) /* set lnum even when it's 0 */
2173 lnum = 1;
2174 }
2175 else
2176 {
2177 /* remove the entry from the list */
2178 if (wip->wi_prev)
2179 wip->wi_prev->wi_next = wip->wi_next;
2180 else
2181 buf->b_wininfo = wip->wi_next;
2182 if (wip->wi_next)
2183 wip->wi_next->wi_prev = wip->wi_prev;
2184 if (copy_options && wip->wi_optset)
2185 {
2186 clear_winopt(&wip->wi_opt);
2187#ifdef FEAT_FOLDING
2188 deleteFoldRecurse(&wip->wi_folds);
2189#endif
2190 }
2191 }
2192 if (lnum != 0)
2193 {
2194 wip->wi_fpos.lnum = lnum;
2195 wip->wi_fpos.col = col;
2196 }
2197 if (copy_options)
2198 {
2199 /* Save the window-specific option values. */
2200 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2201#ifdef FEAT_FOLDING
2202 wip->wi_fold_manual = win->w_fold_manual;
2203 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2204#endif
2205 wip->wi_optset = TRUE;
2206 }
2207
2208 /* insert the entry in front of the list */
2209 wip->wi_next = buf->b_wininfo;
2210 buf->b_wininfo = wip;
2211 wip->wi_prev = NULL;
2212 if (wip->wi_next)
2213 wip->wi_next->wi_prev = wip;
2214
2215 return;
2216}
2217
2218/*
2219 * Find info for the current window in buffer "buf".
2220 * If not found, return the info for the most recently used window.
2221 * Returns NULL when there isn't any info.
2222 */
2223 static wininfo_T *
2224find_wininfo(buf)
2225 buf_T *buf;
2226{
2227 wininfo_T *wip;
2228
2229 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2230 if (wip->wi_win == curwin)
2231 break;
2232 if (wip == NULL) /* if no fpos for curwin, use the first in the list */
2233 wip = buf->b_wininfo;
2234 return wip;
2235}
2236
2237/*
2238 * Reset the local window options to the values last used in this window.
2239 * If the buffer wasn't used in this window before, use the values from
2240 * the most recently used window. If the values were never set, use the
2241 * global values for the window.
2242 */
2243 void
2244get_winopts(buf)
2245 buf_T *buf;
2246{
2247 wininfo_T *wip;
2248
2249 clear_winopt(&curwin->w_onebuf_opt);
2250#ifdef FEAT_FOLDING
2251 clearFolding(curwin);
2252#endif
2253
2254 wip = find_wininfo(buf);
2255 if (wip != NULL && wip->wi_optset)
2256 {
2257 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2258#ifdef FEAT_FOLDING
2259 curwin->w_fold_manual = wip->wi_fold_manual;
2260 curwin->w_foldinvalid = TRUE;
2261 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2262#endif
2263 }
2264 else
2265 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2266
2267#ifdef FEAT_FOLDING
2268 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2269 if (p_fdls >= 0)
2270 curwin->w_p_fdl = p_fdls;
2271#endif
2272}
2273
2274/*
2275 * Find the position (lnum and col) for the buffer 'buf' for the current
2276 * window.
2277 * Returns a pointer to no_position if no position is found.
2278 */
2279 pos_T *
2280buflist_findfpos(buf)
2281 buf_T *buf;
2282{
2283 wininfo_T *wip;
2284 static pos_T no_position = {1, 0};
2285
2286 wip = find_wininfo(buf);
2287 if (wip != NULL)
2288 return &(wip->wi_fpos);
2289 else
2290 return &no_position;
2291}
2292
2293/*
2294 * Find the lnum for the buffer 'buf' for the current window.
2295 */
2296 linenr_T
2297buflist_findlnum(buf)
2298 buf_T *buf;
2299{
2300 return buflist_findfpos(buf)->lnum;
2301}
2302
2303#if defined(FEAT_LISTCMDS) || defined(PROTO)
2304/*
2305 * List all know file names (for :files and :buffers command).
2306 */
2307/*ARGSUSED*/
2308 void
2309buflist_list(eap)
2310 exarg_T *eap;
2311{
2312 buf_T *buf;
2313 int len;
2314 int i;
2315
2316 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2317 {
2318 /* skip unlisted buffers, unless ! was used */
2319 if (!buf->b_p_bl && !eap->forceit)
2320 continue;
2321 msg_putchar('\n');
2322 if (buf_spname(buf) != NULL)
2323 STRCPY(NameBuff, buf_spname(buf));
2324 else
2325 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2326
2327 sprintf((char *)IObuff, "%3d%c%c%c%c%c \"",
2328 buf->b_fnum,
2329 buf->b_p_bl ? ' ' : 'u',
2330 buf == curbuf ? '%' :
2331 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2332 buf->b_ml.ml_mfp == NULL ? ' ' :
2333 (buf->b_nwindows == 0 ? 'h' : 'a'),
2334 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2335 (buf->b_flags & BF_READERR) ? 'x'
2336 : (bufIsChanged(buf) ? '+' : ' ')
2337 );
2338
2339 len = (int)STRLEN(IObuff);
2340 STRNCPY(IObuff + len, NameBuff, IOSIZE - 20 - len);
2341 IObuff[IOSIZE - 20 - len] = NUL; /* make sure it's terminated */
2342
2343 len = (int)STRLEN(IObuff);
2344 IObuff[len++] = '"';
2345
2346 /* put "line 999" in column 40 or after the file name */
2347 IObuff[len] = NUL;
2348 i = 40 - vim_strsize(IObuff);
2349 do
2350 {
2351 IObuff[len++] = ' ';
2352 } while (--i > 0 && len < IOSIZE - 18);
2353 sprintf((char *)IObuff + len, _("line %ld"),
2354 buf == curbuf ? curwin->w_cursor.lnum :
2355 (long)buflist_findlnum(buf));
2356 msg_outtrans(IObuff);
2357 out_flush(); /* output one line at a time */
2358 ui_breakcheck();
2359 }
2360}
2361#endif
2362
2363/*
2364 * Get file name and line number for file 'fnum'.
2365 * Used by DoOneCmd() for translating '%' and '#'.
2366 * Used by insert_reg() and cmdline_paste() for '#' register.
2367 * Return FAIL if not found, OK for success.
2368 */
2369 int
2370buflist_name_nr(fnum, fname, lnum)
2371 int fnum;
2372 char_u **fname;
2373 linenr_T *lnum;
2374{
2375 buf_T *buf;
2376
2377 buf = buflist_findnr(fnum);
2378 if (buf == NULL || buf->b_fname == NULL)
2379 return FAIL;
2380
2381 *fname = buf->b_fname;
2382 *lnum = buflist_findlnum(buf);
2383
2384 return OK;
2385}
2386
2387/*
2388 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2389 * The file name with the full path is also remembered, for when :cd is used.
2390 * Returns FAIL for failure (file name already in use by other buffer)
2391 * OK otherwise.
2392 */
2393 int
2394setfname(buf, ffname, sfname, message)
2395 buf_T *buf;
2396 char_u *ffname, *sfname;
2397 int message;
2398{
2399 buf_T *obuf;
2400#ifdef UNIX
2401 struct stat st;
2402#endif
2403
2404 if (ffname == NULL || *ffname == NUL)
2405 {
2406 /* Removing the name. */
2407 vim_free(buf->b_ffname);
2408 vim_free(buf->b_sfname);
2409 buf->b_ffname = NULL;
2410 buf->b_sfname = NULL;
2411#ifdef UNIX
2412 st.st_dev = (dev_T)-1;
2413#endif
2414 }
2415 else
2416 {
2417 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2418 if (ffname == NULL) /* out of memory */
2419 return FAIL;
2420
2421 /*
2422 * if the file name is already used in another buffer:
2423 * - if the buffer is loaded, fail
2424 * - if the buffer is not loaded, delete it from the list
2425 */
2426#ifdef UNIX
2427 if (mch_stat((char *)ffname, &st) < 0)
2428 st.st_dev = (dev_T)-1;
2429 obuf = buflist_findname_stat(ffname, &st);
2430#else
2431 obuf = buflist_findname(ffname);
2432#endif
2433 if (obuf != NULL && obuf != buf)
2434 {
2435 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2436 {
2437 if (message)
2438 EMSG(_("E95: Buffer with this name already exists"));
2439 vim_free(ffname);
2440 return FAIL;
2441 }
2442 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2443 }
2444 sfname = vim_strsave(sfname);
2445 if (ffname == NULL || sfname == NULL)
2446 {
2447 vim_free(sfname);
2448 vim_free(ffname);
2449 return FAIL;
2450 }
2451#ifdef USE_FNAME_CASE
2452# ifdef USE_LONG_FNAME
2453 if (USE_LONG_FNAME)
2454# endif
2455 fname_case(sfname, 0); /* set correct case for short file name */
2456#endif
2457 vim_free(buf->b_ffname);
2458 vim_free(buf->b_sfname);
2459 buf->b_ffname = ffname;
2460 buf->b_sfname = sfname;
2461 }
2462 buf->b_fname = buf->b_sfname;
2463#ifdef UNIX
2464 if (st.st_dev == (dev_T)-1)
2465 buf->b_dev = -1;
2466 else
2467 {
2468 buf->b_dev = st.st_dev;
2469 buf->b_ino = st.st_ino;
2470 }
2471#endif
2472
2473#ifndef SHORT_FNAME
2474 buf->b_shortname = FALSE;
2475#endif
2476
2477 buf_name_changed(buf);
2478 return OK;
2479}
2480
2481/*
2482 * Take care of what needs to be done when the name of buffer "buf" has
2483 * changed.
2484 */
2485 void
2486buf_name_changed(buf)
2487 buf_T *buf;
2488{
2489 /*
2490 * If the file name changed, also change the name of the swapfile
2491 */
2492 if (buf->b_ml.ml_mfp != NULL)
2493 ml_setname(buf);
2494
2495 if (curwin->w_buffer == buf)
2496 check_arg_idx(curwin); /* check file name for arg list */
2497#ifdef FEAT_TITLE
2498 maketitle(); /* set window title */
2499#endif
2500#ifdef FEAT_WINDOWS
2501 status_redraw_all(); /* status lines need to be redrawn */
2502#endif
2503 fmarks_check_names(buf); /* check named file marks */
2504 ml_timestamp(buf); /* reset timestamp */
2505}
2506
2507/*
2508 * set alternate file name for current window
2509 *
2510 * Used by do_one_cmd(), do_write() and do_ecmd().
2511 * Return the buffer.
2512 */
2513 buf_T *
2514setaltfname(ffname, sfname, lnum)
2515 char_u *ffname;
2516 char_u *sfname;
2517 linenr_T lnum;
2518{
2519 buf_T *buf;
2520
2521 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2522 buf = buflist_new(ffname, sfname, lnum, 0);
2523 if (buf != NULL)
2524 curwin->w_alt_fnum = buf->b_fnum;
2525 return buf;
2526}
2527
2528/*
2529 * Get alternate file name for current window.
2530 * Return NULL if there isn't any, and give error message if requested.
2531 */
2532 char_u *
2533getaltfname(errmsg)
2534 int errmsg; /* give error message */
2535{
2536 char_u *fname;
2537 linenr_T dummy;
2538
2539 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2540 {
2541 if (errmsg)
2542 EMSG(_(e_noalt));
2543 return NULL;
2544 }
2545 return fname;
2546}
2547
2548/*
2549 * Add a file name to the buflist and return its number.
2550 * Uses same flags as buflist_new(), except BLN_DUMMY.
2551 *
2552 * used by qf_init(), main() and doarglist()
2553 */
2554 int
2555buflist_add(fname, flags)
2556 char_u *fname;
2557 int flags;
2558{
2559 buf_T *buf;
2560
2561 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2562 if (buf != NULL)
2563 return buf->b_fnum;
2564 return 0;
2565}
2566
2567#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2568/*
2569 * Adjust slashes in file names. Called after 'shellslash' was set.
2570 */
2571 void
2572buflist_slash_adjust()
2573{
2574 buf_T *bp;
2575
2576 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2577 {
2578 if (bp->b_ffname != NULL)
2579 slash_adjust(bp->b_ffname);
2580 if (bp->b_sfname != NULL)
2581 slash_adjust(bp->b_sfname);
2582 }
2583}
2584#endif
2585
2586/*
2587 * Set alternate cursor position for current window.
2588 * Also save the local window option values.
2589 */
2590 void
2591buflist_altfpos()
2592{
2593 buflist_setfpos(curbuf, curwin, curwin->w_cursor.lnum,
2594 curwin->w_cursor.col, TRUE);
2595}
2596
2597/*
2598 * Return TRUE if 'ffname' is not the same file as current file.
2599 * Fname must have a full path (expanded by mch_FullName()).
2600 */
2601 int
2602otherfile(ffname)
2603 char_u *ffname;
2604{
2605 return otherfile_buf(curbuf, ffname
2606#ifdef UNIX
2607 , NULL
2608#endif
2609 );
2610}
2611
2612 static int
2613otherfile_buf(buf, ffname
2614#ifdef UNIX
2615 , stp
2616#endif
2617 )
2618 buf_T *buf;
2619 char_u *ffname;
2620#ifdef UNIX
2621 struct stat *stp;
2622#endif
2623{
2624 /* no name is different */
2625 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2626 return TRUE;
2627 if (fnamecmp(ffname, buf->b_ffname) == 0)
2628 return FALSE;
2629#ifdef UNIX
2630 {
2631 struct stat st;
2632
2633 /* If no struct stat given, get it now */
2634 if (stp == NULL)
2635 {
2636 if (buf->b_dev < 0 || mch_stat((char *)ffname, &st) < 0)
2637 st.st_dev = (dev_T)-1;
2638 stp = &st;
2639 }
2640 /* Use dev/ino to check if the files are the same, even when the names
2641 * are different (possible with links). Still need to compare the
2642 * name above, for when the file doesn't exist yet.
2643 * Problem: The dev/ino changes when a file is deleted (and created
2644 * again) and remains the same when renamed/moved. We don't want to
2645 * mch_stat() each buffer each time, that would be too slow. Get the
2646 * dev/ino again when they appear to match, but not when they appear
2647 * to be different: Could skip a buffer when it's actually the same
2648 * file. */
2649 if (buf_same_ino(buf, stp))
2650 {
2651 buf_setino(buf);
2652 if (buf_same_ino(buf, stp))
2653 return FALSE;
2654 }
2655 }
2656#endif
2657 return TRUE;
2658}
2659
2660#if defined(UNIX) || defined(PROTO)
2661/*
2662 * Set inode and device number for a buffer.
2663 * Must always be called when b_fname is changed!.
2664 */
2665 void
2666buf_setino(buf)
2667 buf_T *buf;
2668{
2669 struct stat st;
2670
2671 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2672 {
2673 buf->b_dev = st.st_dev;
2674 buf->b_ino = st.st_ino;
2675 }
2676 else
2677 buf->b_dev = -1;
2678}
2679
2680/*
2681 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2682 */
2683 static int
2684buf_same_ino(buf, stp)
2685 buf_T *buf;
2686 struct stat *stp;
2687{
2688 return (buf->b_dev >= 0
2689 && stp->st_dev == buf->b_dev
2690 && stp->st_ino == buf->b_ino);
2691}
2692#endif
2693
2694 void
2695fileinfo(fullname, shorthelp, dont_truncate)
2696 int fullname;
2697 int shorthelp;
2698 int dont_truncate;
2699{
2700 char_u *name;
2701 int n;
2702 char_u *p;
2703 char_u *buffer;
2704
2705 buffer = alloc(IOSIZE);
2706 if (buffer == NULL)
2707 return;
2708
2709 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2710 {
2711 sprintf((char *)buffer, "buf %d: ", curbuf->b_fnum);
2712 p = buffer + STRLEN(buffer);
2713 }
2714 else
2715 p = buffer;
2716
2717 *p++ = '"';
2718 if (buf_spname(curbuf) != NULL)
2719 STRCPY(p, buf_spname(curbuf));
2720 else
2721 {
2722 if (!fullname && curbuf->b_fname != NULL)
2723 name = curbuf->b_fname;
2724 else
2725 name = curbuf->b_ffname;
2726 home_replace(shorthelp ? curbuf : NULL, name, p,
2727 (int)(IOSIZE - (p - buffer)), TRUE);
2728 }
2729
2730 sprintf((char *)buffer + STRLEN(buffer),
2731 "\"%s%s%s%s%s%s",
2732 curbufIsChanged() ? (shortmess(SHM_MOD)
2733 ? " [+]" : _(" [Modified]")) : " ",
2734 (curbuf->b_flags & BF_NOTEDITED)
2735#ifdef FEAT_QUICKFIX
2736 && !bt_dontwrite(curbuf)
2737#endif
2738 ? _("[Not edited]") : "",
2739 (curbuf->b_flags & BF_NEW)
2740#ifdef FEAT_QUICKFIX
2741 && !bt_dontwrite(curbuf)
2742#endif
2743 ? _("[New file]") : "",
2744 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
2745 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
2746 : _("[readonly]")) : "",
2747 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
2748 || curbuf->b_p_ro) ?
2749 " " : "");
2750 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
2751 * causes an overflow, thus for large numbers divide instead. */
2752 if (curwin->w_cursor.lnum > 1000000L)
2753 n = (int)(((long)curwin->w_cursor.lnum) /
2754 ((long)curbuf->b_ml.ml_line_count / 100L));
2755 else
2756 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
2757 (long)curbuf->b_ml.ml_line_count);
2758 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2759 {
2760 STRCPY(buffer + STRLEN(buffer), _(no_lines_msg));
2761 }
2762#ifdef FEAT_CMDL_INFO
2763 else if (p_ru)
2764 {
2765 /* Current line and column are already on the screen -- webb */
2766 if (curbuf->b_ml.ml_line_count == 1)
2767 sprintf((char *)buffer + STRLEN(buffer), _("1 line --%d%%--"), n);
2768 else
2769 sprintf((char *)buffer + STRLEN(buffer), _("%ld lines --%d%%--"),
2770 (long)curbuf->b_ml.ml_line_count, n);
2771 }
2772#endif
2773 else
2774 {
2775 sprintf((char *)buffer + STRLEN(buffer),
2776 _("line %ld of %ld --%d%%-- col "),
2777 (long)curwin->w_cursor.lnum,
2778 (long)curbuf->b_ml.ml_line_count,
2779 n);
2780 validate_virtcol();
2781 col_print(buffer + STRLEN(buffer),
2782 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
2783 }
2784
2785 (void)append_arg_number(curwin, buffer, !shortmess(SHM_FILE), IOSIZE);
2786
2787 if (dont_truncate)
2788 {
2789 /* Temporarily set msg_scroll to avoid the message being truncated.
2790 * First call msg_start() to get the message in the right place. */
2791 msg_start();
2792 n = msg_scroll;
2793 msg_scroll = TRUE;
2794 msg(buffer);
2795 msg_scroll = n;
2796 }
2797 else
2798 {
2799 p = msg_trunc_attr(buffer, FALSE, 0);
2800 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
2801 {
2802 /* Need to repeat the message after redrawing when:
2803 * - When restart_edit is set (otherwise there will be a delay
2804 * before redrawing).
2805 * - When the screen was scrolled but there is no wait-return
2806 * prompt. */
2807 set_keep_msg(p);
2808 keep_msg_attr = 0;
2809 }
2810 }
2811
2812 vim_free(buffer);
2813}
2814
2815 void
2816col_print(buf, col, vcol)
2817 char_u *buf;
2818 int col;
2819 int vcol;
2820{
2821 if (col == vcol)
2822 sprintf((char *)buf, "%d", col);
2823 else
2824 sprintf((char *)buf, "%d-%d", col, vcol);
2825}
2826
2827#if defined(FEAT_TITLE) || defined(PROTO)
2828/*
2829 * put file name in title bar of window and in icon title
2830 */
2831
2832static char_u *lasttitle = NULL;
2833static char_u *lasticon = NULL;
2834
2835 void
2836maketitle()
2837{
2838 char_u *p;
2839 char_u *t_str = NULL;
2840 char_u *i_name;
2841 char_u *i_str = NULL;
2842 int maxlen = 0;
2843 int len;
2844 int mustset;
2845 char_u buf[IOSIZE];
2846 int off;
2847
2848 if (!redrawing())
2849 {
2850 /* Postpone updating the title when 'lazyredraw' is set. */
2851 need_maketitle = TRUE;
2852 return;
2853 }
2854
2855 need_maketitle = FALSE;
2856 if (!p_title && !p_icon)
2857 return;
2858
2859 if (p_title)
2860 {
2861 if (p_titlelen > 0)
2862 {
2863 maxlen = p_titlelen * Columns / 100;
2864 if (maxlen < 10)
2865 maxlen = 10;
2866 }
2867
2868 t_str = buf;
2869 if (*p_titlestring != NUL)
2870 {
2871#ifdef FEAT_STL_OPT
2872 if (stl_syntax & STL_IN_TITLE)
2873 build_stl_str_hl(curwin, t_str, sizeof(buf),
2874 p_titlestring, 0, maxlen, NULL);
2875 else
2876#endif
2877 t_str = p_titlestring;
2878 }
2879 else
2880 {
2881 /* format: "fname + (path) (1 of 2) - VIM" */
2882
2883 if (curbuf->b_fname == NULL)
2884 STRCPY(buf, _("[No file]"));
2885 else
2886 {
2887 p = transstr(gettail(curbuf->b_fname));
2888 STRNCPY(buf, p, IOSIZE - 100);
2889 vim_free(p);
2890 buf[IOSIZE - 100] = NUL; /* in case it was too long */
2891 }
2892
2893 switch (bufIsChanged(curbuf)
2894 + (curbuf->b_p_ro * 2)
2895 + (!curbuf->b_p_ma * 4))
2896 {
2897 case 1: STRCAT(buf, " +"); break;
2898 case 2: STRCAT(buf, " ="); break;
2899 case 3: STRCAT(buf, " =+"); break;
2900 case 4:
2901 case 6: STRCAT(buf, " -"); break;
2902 case 5:
2903 case 7: STRCAT(buf, " -+"); break;
2904 }
2905
2906 if (curbuf->b_fname != NULL)
2907 {
2908 /* Get path of file, replace home dir with ~ */
2909 off = (int)STRLEN(buf);
2910 buf[off++] = ' ';
2911 buf[off++] = '(';
2912 home_replace(curbuf, curbuf->b_ffname,
2913 buf + off, IOSIZE - off, TRUE);
2914#ifdef BACKSLASH_IN_FILENAME
2915 /* avoid "c:/name" to be reduced to "c" */
2916 if (isalpha(buf[off]) && buf[off + 1] == ':')
2917 off += 2;
2918#endif
2919 /* remove the file name */
2920 p = gettail(buf + off);
2921 if (p == buf + off)
2922 {
2923 /* must be a help buffer */
2924 STRCPY(buf + off, _("help"));
2925 }
2926 else
2927 {
2928 while (p > buf + off + 1 && vim_ispathsep(p[-1]))
2929 --p;
2930#ifdef VMS
2931 /* path separator is part of the path */
2932 ++p;
2933#endif
2934 *p = NUL;
2935 }
2936 /* translate unprintable chars */
2937 p = transstr(buf + off);
2938 STRNCPY(buf + off, p, IOSIZE - off);
2939 vim_free(p);
2940 buf[IOSIZE - 1] = NUL; /* in case it was too long */
2941 STRCAT(buf, ")");
2942 }
2943
2944 append_arg_number(curwin, buf, FALSE, IOSIZE);
2945
2946#if defined(FEAT_CLIENTSERVER)
2947 if (serverName != NULL)
2948 {
2949 STRCAT(buf, " - ");
2950 STRCAT(buf, serverName);
2951 }
2952 else
2953#endif
2954 STRCAT(buf, " - VIM");
2955
2956 if (maxlen > 0)
2957 {
2958 /* make it shorter by removing a bit in the middle */
2959 len = vim_strsize(buf);
2960 if (len > maxlen)
2961 trunc_string(buf, buf, maxlen);
2962 }
2963 }
2964 }
2965 mustset = ti_change(t_str, &lasttitle);
2966
2967 if (p_icon)
2968 {
2969 i_str = buf;
2970 if (*p_iconstring != NUL)
2971 {
2972#ifdef FEAT_STL_OPT
2973 if (stl_syntax & STL_IN_ICON)
2974 build_stl_str_hl(curwin, i_str, sizeof(buf),
2975 p_iconstring, 0, 0, NULL);
2976 else
2977#endif
2978 i_str = p_iconstring;
2979 }
2980 else
2981 {
2982 if (buf_spname(curbuf) != NULL)
2983 i_name = (char_u *)buf_spname(curbuf);
2984 else /* use file name only in icon */
2985 i_name = gettail(curbuf->b_ffname);
2986 *i_str = NUL;
2987 /* Truncate name at 100 bytes. */
2988 len = STRLEN(i_name);
2989 if (len > 100)
2990 {
2991 len -= 100;
2992#ifdef FEAT_MBYTE
2993 if (has_mbyte)
2994 len += (*mb_tail_off)(i_name, i_name + len) + 1;
2995#endif
2996 i_name += len;
2997 }
2998 STRCPY(i_str, i_name);
2999 trans_characters(i_str, IOSIZE);
3000 }
3001 }
3002
3003 mustset |= ti_change(i_str, &lasticon);
3004
3005 if (mustset)
3006 resettitle();
3007}
3008
3009/*
3010 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3011 * from "str" if it does.
3012 * Return TRUE when "*last" changed.
3013 */
3014 static int
3015ti_change(str, last)
3016 char_u *str;
3017 char_u **last;
3018{
3019 if ((str == NULL) != (*last == NULL)
3020 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3021 {
3022 vim_free(*last);
3023 if (str == NULL)
3024 *last = NULL;
3025 else
3026 *last = vim_strsave(str);
3027 return TRUE;
3028 }
3029 return FALSE;
3030}
3031
3032/*
3033 * Put current window title back (used after calling a shell)
3034 */
3035 void
3036resettitle()
3037{
3038 mch_settitle(lasttitle, lasticon);
3039}
3040#endif /* FEAT_TITLE */
3041
3042#if defined(FEAT_STL_OPT) || defined(PROTO)
3043/*
3044 * Build a string from the status line items in fmt.
3045 * Return length of string in screen cells.
3046 *
3047 * Items are drawn interspersed with the text that surrounds it
3048 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3049 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3050 *
3051 * If maxwidth is not zero, the string will be filled at any middle marker
3052 * or truncated if too long, fillchar is used for all whitespace.
3053 */
3054 int
3055build_stl_str_hl(wp, out, outlen, fmt, fillchar, maxwidth, hl)
3056 win_T *wp;
3057 char_u *out; /* buffer to write into */
3058 size_t outlen; /* length of out[] */
3059 char_u *fmt;
3060 int fillchar;
3061 int maxwidth;
3062 struct stl_hlrec *hl;
3063{
3064 char_u *p;
3065 char_u *s;
3066 char_u *t;
3067 char_u *linecont;
3068#ifdef FEAT_EVAL
3069 win_T *o_curwin;
3070 buf_T *o_curbuf;
3071#endif
3072 int empty_line;
3073 colnr_T virtcol;
3074 long l;
3075 long n;
3076 int prevchar_isflag;
3077 int prevchar_isitem;
3078 int itemisflag;
3079 int fillable;
3080 char_u *str;
3081 long num;
3082 int width;
3083 int itemcnt;
3084 int curitem;
3085 int groupitem[STL_MAX_ITEM];
3086 int groupdepth;
3087 struct stl_item
3088 {
3089 char_u *start;
3090 int minwid;
3091 int maxwid;
3092 enum
3093 {
3094 Normal,
3095 Empty,
3096 Group,
3097 Middle,
3098 Highlight,
3099 Trunc
3100 } type;
3101 } item[STL_MAX_ITEM];
3102 int minwid;
3103 int maxwid;
3104 int zeropad;
3105 char_u base;
3106 char_u opt;
3107#define TMPLEN 70
3108 char_u tmp[TMPLEN];
3109
3110 if (fillchar == 0)
3111 fillchar = ' ';
3112 /*
3113 * Get line & check if empty (cursorpos will show "0-1").
3114 * If inversion is possible we use it. Else '=' characters are used.
3115 */
3116 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3117 empty_line = (*linecont == NUL);
3118
3119 groupdepth = 0;
3120 p = out;
3121 curitem = 0;
3122 prevchar_isflag = TRUE;
3123 prevchar_isitem = FALSE;
3124 for (s = fmt; *s;)
3125 {
3126 if (*s != NUL && *s != '%')
3127 prevchar_isflag = prevchar_isitem = FALSE;
3128
3129 /*
3130 * Handle up to the next '%' or the end.
3131 */
3132 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3133 *p++ = *s++;
3134 if (*s == NUL || p + 1 >= out + outlen)
3135 break;
3136
3137 /*
3138 * Handle one '%' item.
3139 */
3140 s++;
3141 if (*s == '%')
3142 {
3143 if (p + 1 >= out + outlen)
3144 break;
3145 *p++ = *s++;
3146 prevchar_isflag = prevchar_isitem = FALSE;
3147 continue;
3148 }
3149 if (*s == STL_MIDDLEMARK)
3150 {
3151 s++;
3152 if (groupdepth > 0)
3153 continue;
3154 item[curitem].type = Middle;
3155 item[curitem++].start = p;
3156 continue;
3157 }
3158 if (*s == STL_TRUNCMARK)
3159 {
3160 s++;
3161 item[curitem].type = Trunc;
3162 item[curitem++].start = p;
3163 continue;
3164 }
3165 if (*s == ')')
3166 {
3167 s++;
3168 if (groupdepth < 1)
3169 continue;
3170 groupdepth--;
3171
3172 t = item[groupitem[groupdepth]].start;
3173 *p = NUL;
3174 l = vim_strsize(t);
3175 if (curitem > groupitem[groupdepth] + 1
3176 && item[groupitem[groupdepth]].minwid == 0)
3177 {
3178 /* remove group if all items are empty */
3179 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3180 if (item[n].type == Normal)
3181 break;
3182 if (n == curitem)
3183 {
3184 p = t;
3185 l = 0;
3186 }
3187 }
3188 if (l > item[groupitem[groupdepth]].maxwid)
3189 {
3190 /* truncate, remove n bytes of text at the start */
3191#ifdef FEAT_MBYTE
3192 if (has_mbyte)
3193 {
3194 /* Find the first character that should be included. */
3195 n = 0;
3196 while (l >= item[groupitem[groupdepth]].maxwid)
3197 {
3198 l -= ptr2cells(t + n);
3199 n += (*mb_ptr2len_check)(t + n);
3200 }
3201 }
3202 else
3203#endif
3204 n = (p - t) - item[groupitem[groupdepth]].maxwid + 1;
3205
3206 *t = '<';
3207 mch_memmove(t + 1, t + n, p - (t + n));
3208 p = p - n + 1;
3209#ifdef FEAT_MBYTE
3210 /* Fill up space left over by half a double-wide char. */
3211 while (++l < item[groupitem[groupdepth]].minwid)
3212 *p++ = fillchar;
3213#endif
3214
3215 /* correct the start of the items for the truncation */
3216 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3217 {
3218 item[l].start -= n;
3219 if (item[l].start < t)
3220 item[l].start = t;
3221 }
3222 }
3223 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3224 {
3225 /* fill */
3226 n = item[groupitem[groupdepth]].minwid;
3227 if (n < 0)
3228 {
3229 /* fill by appending characters */
3230 n = 0 - n;
3231 while (l++ < n && p + 1 < out + outlen)
3232 *p++ = fillchar;
3233 }
3234 else
3235 {
3236 /* fill by inserting characters */
3237 mch_memmove(t + n - l, t, p - t);
3238 l = n - l;
3239 if (p + l >= out + outlen)
3240 l = (out + outlen) - p - 1;
3241 p += l;
3242 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3243 item[n].start += l;
3244 for ( ; l > 0; l--)
3245 *t++ = fillchar;
3246 }
3247 }
3248 continue;
3249 }
3250 minwid = 0;
3251 maxwid = 9999;
3252 zeropad = FALSE;
3253 l = 1;
3254 if (*s == '0')
3255 {
3256 s++;
3257 zeropad = TRUE;
3258 }
3259 if (*s == '-')
3260 {
3261 s++;
3262 l = -1;
3263 }
3264 if (VIM_ISDIGIT(*s))
3265 {
3266 minwid = (int)getdigits(&s);
3267 if (minwid < 0) /* overflow */
3268 minwid = 0;
3269 }
3270 if (*s == STL_HIGHLIGHT)
3271 {
3272 item[curitem].type = Highlight;
3273 item[curitem].start = p;
3274 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3275 s++;
3276 curitem++;
3277 continue;
3278 }
3279 if (*s == '.')
3280 {
3281 s++;
3282 if (VIM_ISDIGIT(*s))
3283 {
3284 maxwid = (int)getdigits(&s);
3285 if (maxwid <= 0) /* overflow */
3286 maxwid = 50;
3287 }
3288 }
3289 minwid = (minwid > 50 ? 50 : minwid) * l;
3290 if (*s == '(')
3291 {
3292 groupitem[groupdepth++] = curitem;
3293 item[curitem].type = Group;
3294 item[curitem].start = p;
3295 item[curitem].minwid = minwid;
3296 item[curitem].maxwid = maxwid;
3297 s++;
3298 curitem++;
3299 continue;
3300 }
3301 if (vim_strchr(STL_ALL, *s) == NULL)
3302 {
3303 s++;
3304 continue;
3305 }
3306 opt = *s++;
3307
3308 /* OK - now for the real work */
3309 base = 'D';
3310 itemisflag = FALSE;
3311 fillable = TRUE;
3312 num = -1;
3313 str = NULL;
3314 switch (opt)
3315 {
3316 case STL_FILEPATH:
3317 case STL_FULLPATH:
3318 case STL_FILENAME:
3319 fillable = FALSE; /* don't change ' ' to fillchar */
3320 if (buf_spname(wp->w_buffer) != NULL)
3321 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3322 else
3323 {
3324 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
3325 : wp->w_buffer->b_fname;
3326 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3327 }
3328 trans_characters(NameBuff, MAXPATHL);
3329 if (opt != STL_FILENAME)
3330 str = NameBuff;
3331 else
3332 str = gettail(NameBuff);
3333 break;
3334
3335 case STL_VIM_EXPR: /* '{' */
3336 itemisflag = TRUE;
3337 t = p;
3338 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3339 *p++ = *s++;
3340 if (*s != '}') /* missing '}' or out of space */
3341 break;
3342 s++;
3343 *p = 0;
3344 p = t;
3345
3346#ifdef FEAT_EVAL
3347 sprintf((char *)tmp, "%d", curbuf->b_fnum);
3348 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3349
3350 o_curbuf = curbuf;
3351 o_curwin = curwin;
3352 curwin = wp;
3353 curbuf = wp->w_buffer;
3354
3355 str = eval_to_string_safe(p, &t);
3356
3357 curwin = o_curwin;
3358 curbuf = o_curbuf;
3359 do_unlet((char_u *)"g:actual_curbuf");
3360
3361 if (str != NULL && *str != 0)
3362 {
3363 if (*skipdigits(str) == NUL)
3364 {
3365 num = atoi((char *)str);
3366 vim_free(str);
3367 str = NULL;
3368 itemisflag = FALSE;
3369 }
3370 }
3371#endif
3372 break;
3373
3374 case STL_LINE:
3375 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3376 ? 0L : (long)(wp->w_cursor.lnum);
3377 break;
3378
3379 case STL_NUMLINES:
3380 num = wp->w_buffer->b_ml.ml_line_count;
3381 break;
3382
3383 case STL_COLUMN:
3384 num = !(State & INSERT) && empty_line
3385 ? 0 : (int)wp->w_cursor.col + 1;
3386 break;
3387
3388 case STL_VIRTCOL:
3389 case STL_VIRTCOL_ALT:
3390 /* In list mode virtcol needs to be recomputed */
3391 virtcol = wp->w_virtcol;
3392 if (wp->w_p_list && lcs_tab1 == NUL)
3393 {
3394 wp->w_p_list = FALSE;
3395 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3396 wp->w_p_list = TRUE;
3397 }
3398 ++virtcol;
3399 /* Don't display %V if it's the same as %c. */
3400 if (opt == STL_VIRTCOL_ALT
3401 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3402 ? 0 : (int)wp->w_cursor.col + 1)))
3403 break;
3404 num = (long)virtcol;
3405 break;
3406
3407 case STL_PERCENTAGE:
3408 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3409 (long)wp->w_buffer->b_ml.ml_line_count);
3410 break;
3411
3412 case STL_ALTPERCENT:
3413 str = tmp;
3414 get_rel_pos(wp, str);
3415 break;
3416
3417 case STL_ARGLISTSTAT:
3418 fillable = FALSE;
3419 tmp[0] = 0;
3420 if (append_arg_number(wp, tmp, FALSE, (int)sizeof(tmp)))
3421 str = tmp;
3422 break;
3423
3424 case STL_KEYMAP:
3425 fillable = FALSE;
3426 if (get_keymap_str(wp, tmp, TMPLEN))
3427 str = tmp;
3428 break;
3429 case STL_PAGENUM:
3430#ifdef FEAT_PRINTER
3431 num = get_printer_page_num();
3432#else
3433 num = 0;
3434#endif
3435 break;
3436
3437 case STL_BUFNO:
3438 num = wp->w_buffer->b_fnum;
3439 break;
3440
3441 case STL_OFFSET_X:
3442 base = 'X';
3443 case STL_OFFSET:
3444#ifdef FEAT_BYTEOFF
3445 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3446 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3447 0L : l + 1 + (!(State & INSERT) && empty_line ?
3448 0 : (int)wp->w_cursor.col);
3449#endif
3450 break;
3451
3452 case STL_BYTEVAL_X:
3453 base = 'X';
3454 case STL_BYTEVAL:
3455 if (((State & INSERT) && wp == curwin) || empty_line)
3456 num = 0;
3457 else
3458 {
3459#ifdef FEAT_MBYTE
3460 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3461#else
3462 num = linecont[wp->w_cursor.col];
3463#endif
3464 }
3465 if (num == NL)
3466 num = 0;
3467 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3468 num = NL;
3469 break;
3470
3471 case STL_ROFLAG:
3472 case STL_ROFLAG_ALT:
3473 itemisflag = TRUE;
3474 if (wp->w_buffer->b_p_ro)
3475 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3476 break;
3477
3478 case STL_HELPFLAG:
3479 case STL_HELPFLAG_ALT:
3480 itemisflag = TRUE;
3481 if (wp->w_buffer->b_help)
3482 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
3483 : _("[help]"));
3484 break;
3485
3486#ifdef FEAT_AUTOCMD
3487 case STL_FILETYPE:
3488 if (*wp->w_buffer->b_p_ft != NUL
3489 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3490 {
3491 sprintf((char *)tmp, "[%s]", wp->w_buffer->b_p_ft);
3492 str = tmp;
3493 }
3494 break;
3495
3496 case STL_FILETYPE_ALT:
3497 itemisflag = TRUE;
3498 if (*wp->w_buffer->b_p_ft != NUL
3499 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3500 {
3501 sprintf((char *)tmp, ",%s", wp->w_buffer->b_p_ft);
3502 for (t = tmp; *t != 0; t++)
3503 *t = TOUPPER_LOC(*t);
3504 str = tmp;
3505 }
3506 break;
3507#endif
3508
3509#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3510 case STL_PREVIEWFLAG:
3511 case STL_PREVIEWFLAG_ALT:
3512 itemisflag = TRUE;
3513 if (wp->w_p_pvw)
3514 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3515 : _("[Preview]"));
3516 break;
3517#endif
3518
3519 case STL_MODIFIED:
3520 case STL_MODIFIED_ALT:
3521 itemisflag = TRUE;
3522 switch ((opt == STL_MODIFIED_ALT)
3523 + bufIsChanged(wp->w_buffer) * 2
3524 + (!wp->w_buffer->b_p_ma) * 4)
3525 {
3526 case 2: str = (char_u *)"[+]"; break;
3527 case 3: str = (char_u *)",+"; break;
3528 case 4: str = (char_u *)"[-]"; break;
3529 case 5: str = (char_u *)",-"; break;
3530 case 6: str = (char_u *)"[+-]"; break;
3531 case 7: str = (char_u *)",+-"; break;
3532 }
3533 break;
3534 }
3535
3536 item[curitem].start = p;
3537 item[curitem].type = Normal;
3538 if (str != NULL && *str)
3539 {
3540 t = str;
3541 if (itemisflag)
3542 {
3543 if ((t[0] && t[1])
3544 && ((!prevchar_isitem && *t == ',')
3545 || (prevchar_isflag && *t == ' ')))
3546 t++;
3547 prevchar_isflag = TRUE;
3548 }
3549 l = vim_strsize(t);
3550 if (l > 0)
3551 prevchar_isitem = TRUE;
3552 if (l > maxwid)
3553 {
3554 while (l >= maxwid)
3555#ifdef FEAT_MBYTE
3556 if (has_mbyte)
3557 {
3558 l -= ptr2cells(t);
3559 t += (*mb_ptr2len_check)(t);
3560 }
3561 else
3562#endif
3563 l -= byte2cells(*t++);
3564 if (p + 1 >= out + outlen)
3565 break;
3566 *p++ = '<';
3567 }
3568 if (minwid > 0)
3569 {
3570 for (; l < minwid && p + 1 < out + outlen; l++)
3571 {
3572 /* Don't put a "-" in front of a digit. */
3573 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3574 *p++ = ' ';
3575 else
3576 *p++ = fillchar;
3577 }
3578 minwid = 0;
3579 }
3580 else
3581 minwid *= -1;
3582 while (*t && p + 1 < out + outlen)
3583 {
3584 *p++ = *t++;
3585 /* Change a space by fillchar, unless fillchar is '-' and a
3586 * digit follows. */
3587 if (fillable && p[-1] == ' '
3588 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3589 p[-1] = fillchar;
3590 }
3591 for (; l < minwid && p + 1 < out + outlen; l++)
3592 *p++ = fillchar;
3593 }
3594 else if (num >= 0)
3595 {
3596 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3597 char_u nstr[20];
3598
3599 if (p + 20 >= out + outlen)
3600 break; /* not sufficient space */
3601 prevchar_isitem = TRUE;
3602 t = nstr;
3603 if (opt == STL_VIRTCOL_ALT)
3604 {
3605 *t++ = '-';
3606 minwid--;
3607 }
3608 *t++ = '%';
3609 if (zeropad)
3610 *t++ = '0';
3611 *t++ = '*';
3612 *t++ = nbase == 16 ? base : (nbase == 8 ? 'o' : 'd');
3613 *t = 0;
3614
3615 for (n = num, l = 1; n >= nbase; n /= nbase)
3616 l++;
3617 if (opt == STL_VIRTCOL_ALT)
3618 l++;
3619 if (l > maxwid)
3620 {
3621 l += 2;
3622 n = l - maxwid;
3623 while (l-- > maxwid)
3624 num /= nbase;
3625 *t++ = '>';
3626 *t++ = '%';
3627 *t = t[-3];
3628 *++t = 0;
3629 sprintf((char *)p, (char *)nstr, 0, num, n);
3630 }
3631 else
3632 sprintf((char *)p, (char *)nstr, minwid, num);
3633 p += STRLEN(p);
3634 }
3635 else
3636 item[curitem].type = Empty;
3637
3638 if (opt == STL_VIM_EXPR)
3639 vim_free(str);
3640
3641 if (num >= 0 || (!itemisflag && str && *str))
3642 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
3643 curitem++;
3644 }
3645 *p = NUL;
3646 itemcnt = curitem;
3647
3648 width = vim_strsize(out);
3649 if (maxwidth > 0 && width > maxwidth)
3650 {
3651 /* Result is too long, must trunctate somewhere. */
3652 l = 0;
3653 if (itemcnt == 0)
3654 s = out;
3655 else
3656 {
3657 for ( ; l < itemcnt; l++)
3658 if (item[l].type == Trunc)
3659 {
3660 /* Truncate at %< item. */
3661 s = item[l].start;
3662 break;
3663 }
3664 if (l == itemcnt)
3665 {
3666 /* No %< item, truncate first item. */
3667 s = item[0].start;
3668 l = 0;
3669 }
3670 }
3671
3672 if (width - vim_strsize(s) >= maxwidth)
3673 {
3674 /* Truncation mark is beyond max length */
3675#ifdef FEAT_MBYTE
3676 if (has_mbyte)
3677 {
3678 s = out;
3679 width = 0;
3680 for (;;)
3681 {
3682 width += ptr2cells(s);
3683 if (width >= maxwidth)
3684 break;
3685 s += (*mb_ptr2len_check)(s);
3686 }
3687 /* Fill up for half a double-wide character. */
3688 while (++width < maxwidth)
3689 *s++ = fillchar;
3690 }
3691 else
3692#endif
3693 s = out + maxwidth - 1;
3694 for (l = 0; l < itemcnt; l++)
3695 if (item[l].start > s)
3696 break;
3697 itemcnt = l;
3698 *s++ = '>';
3699 *s = 0;
3700 }
3701 else
3702 {
3703#ifdef FEAT_MBYTE
3704 if (has_mbyte)
3705 {
3706 n = 0;
3707 while (width >= maxwidth)
3708 {
3709 width -= ptr2cells(s + n);
3710 n += (*mb_ptr2len_check)(s + n);
3711 }
3712 }
3713 else
3714#endif
3715 n = width - maxwidth + 1;
3716 p = s + n;
3717 mch_memmove(s + 1, p, STRLEN(p) + 1);
3718 *s = '<';
3719
3720 /* Fill up for half a double-wide character. */
3721 while (++width < maxwidth)
3722 {
3723 s = s + STRLEN(s);
3724 *s++ = fillchar;
3725 *s = NUL;
3726 }
3727
3728 --n; /* count the '<' */
3729 for (; l < itemcnt; l++)
3730 {
3731 if (item[l].start - n >= s)
3732 item[l].start -= n;
3733 else
3734 item[l].start = s;
3735 }
3736 }
3737 width = maxwidth;
3738 }
3739 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
3740 {
3741 /* Apply STL_MIDDLE if any */
3742 for (l = 0; l < itemcnt; l++)
3743 if (item[l].type == Middle)
3744 break;
3745 if (l < itemcnt)
3746 {
3747 p = item[l].start + maxwidth - width;
3748 mch_memmove(p, item[l].start, STRLEN(item[l].start) + 1);
3749 for (s = item[l].start; s < p; s++)
3750 *s = fillchar;
3751 for (l++; l < itemcnt; l++)
3752 item[l].start += maxwidth - width;
3753 width = maxwidth;
3754 }
3755 }
3756
3757 if (hl != NULL)
3758 {
3759 for (l = 0; l < itemcnt; l++)
3760 {
3761 if (item[l].type == Highlight)
3762 {
3763 hl->start = item[l].start;
3764 hl->userhl = item[l].minwid;
3765 hl++;
3766 }
3767 }
3768 hl->start = NULL;
3769 hl->userhl = 0;
3770 }
3771
3772 return width;
3773}
3774#endif /* FEAT_STL_OPT */
3775
3776#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) || defined(PROTO)
3777/*
3778 * Get relative cursor position in window, in the form 99%, using "Top", "Bot"
3779 * or "All" when appropriate.
3780 */
3781 void
3782get_rel_pos(wp, str)
3783 win_T *wp;
3784 char_u *str;
3785{
3786 long above; /* number of lines above window */
3787 long below; /* number of lines below window */
3788
3789 above = wp->w_topline - 1;
3790#ifdef FEAT_DIFF
3791 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
3792#endif
3793 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
3794 if (below <= 0)
3795 STRCPY(str, above == 0 ? _("All") : _("Bot"));
3796 else if (above <= 0)
3797 STRCPY(str, _("Top"));
3798 else
3799 sprintf((char *)str, "%2d%%", above > 1000000L
3800 ? (int)(above / ((above + below) / 100L))
3801 : (int)(above * 100L / (above + below)));
3802}
3803#endif
3804
3805/*
3806 * Append (file 2 of 8) to 'buf', if editing more than one file.
3807 * Return TRUE if it was appended.
3808 */
3809 int
3810append_arg_number(wp, buf, add_file, maxlen)
3811 win_T *wp;
3812 char_u *buf;
3813 int add_file; /* Add "file" before the arg number */
3814 int maxlen; /* maximum nr of chars in buf or zero*/
3815{
3816 char_u *p;
3817
3818 if (ARGCOUNT <= 1) /* nothing to do */
3819 return FALSE;
3820
3821 p = buf + STRLEN(buf); /* go to the end of the buffer */
3822 if (maxlen && p - buf + 35 >= maxlen) /* getting too long */
3823 return FALSE;
3824 *p++ = ' ';
3825 *p++ = '(';
3826 if (add_file)
3827 {
3828 STRCPY(p, "file ");
3829 p += 5;
3830 }
3831 sprintf((char *)p, wp->w_arg_idx_invalid ? "(%d) of %d)"
3832 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
3833 return TRUE;
3834}
3835
3836/*
3837 * If fname is not a full path, make it a full path.
3838 * Returns pointer to allocated memory (NULL for failure).
3839 */
3840 char_u *
3841fix_fname(fname)
3842 char_u *fname;
3843{
3844 /*
3845 * Force expanding the path always for Unix, because symbolic links may
3846 * mess up the full path name, even though it starts with a '/'.
3847 * Also expand when there is ".." in the file name, try to remove it,
3848 * because "c:/src/../README" is equal to "c:/README".
3849 * For MS-Windows also expand names like "longna~1" to "longname".
3850 */
3851#ifdef UNIX
3852 return FullName_save(fname, TRUE);
3853#else
3854 if (!vim_isAbsName(fname) || strstr((char *)fname, "..") != NULL
3855#if defined(MSWIN) || defined(DJGPP)
3856 || vim_strchr(fname, '~') != NULL
3857#endif
3858 )
3859 return FullName_save(fname, FALSE);
3860
3861 fname = vim_strsave(fname);
3862
3863#ifdef USE_FNAME_CASE
3864# ifdef USE_LONG_FNAME
3865 if (USE_LONG_FNAME)
3866# endif
3867 {
3868 if (fname != NULL)
3869 fname_case(fname, 0); /* set correct case for file name */
3870 }
3871#endif
3872
3873 return fname;
3874#endif
3875}
3876
3877/*
3878 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
3879 * "ffname" becomes a pointer to allocated memory (or NULL).
3880 */
3881/*ARGSUSED*/
3882 void
3883fname_expand(buf, ffname, sfname)
3884 buf_T *buf;
3885 char_u **ffname;
3886 char_u **sfname;
3887{
3888 if (*ffname == NULL) /* if no file name given, nothing to do */
3889 return;
3890 if (*sfname == NULL) /* if no short file name given, use ffname */
3891 *sfname = *ffname;
3892 *ffname = fix_fname(*ffname); /* expand to full path */
3893
3894#ifdef FEAT_SHORTCUT
3895 if (!buf->b_p_bin)
3896 {
3897 char_u *rfname = NULL;
3898
3899 /* If the file name is a shortcut file, use the file it links to. */
3900 rfname = mch_resolve_shortcut(*ffname);
3901 if (rfname)
3902 {
3903 vim_free(*ffname);
3904 *ffname = rfname;
3905 *sfname = rfname;
3906 }
3907 }
3908#endif
3909}
3910
3911/*
3912 * Get the file name for an argument list entry.
3913 */
3914 char_u *
3915alist_name(aep)
3916 aentry_T *aep;
3917{
3918 buf_T *bp;
3919
3920 /* Use the name from the associated buffer if it exists. */
3921 bp = buflist_findnr(aep->ae_fnum);
3922 if (bp == NULL)
3923 return aep->ae_fname;
3924 return bp->b_fname;
3925}
3926
3927#if defined(FEAT_WINDOWS) || defined(PROTO)
3928/*
3929 * do_arg_all(): Open up to 'count' windows, one for each argument.
3930 */
3931 void
3932do_arg_all(count, forceit)
3933 int count;
3934 int forceit; /* hide buffers in current windows */
3935{
3936 int i;
3937 win_T *wp, *wpnext;
3938 char_u *opened; /* array of flags for which args are open */
3939 int opened_len; /* lenght of opened[] */
3940 int use_firstwin = FALSE; /* use first window for arglist */
3941 int split_ret = OK;
3942 int p_ea_save;
3943 alist_T *alist; /* argument list to be used */
3944 buf_T *buf;
3945
3946 if (ARGCOUNT <= 0)
3947 {
3948 /* Don't give an error message. We don't want it when the ":all"
3949 * command is in the .vimrc. */
3950 return;
3951 }
3952 setpcmark();
3953
3954 opened_len = ARGCOUNT;
3955 opened = alloc_clear((unsigned)opened_len);
3956 if (opened == NULL)
3957 return;
3958
3959#ifdef FEAT_GUI
3960 need_mouse_correct = TRUE;
3961#endif
3962
3963 /*
3964 * Try closing all windows that are not in the argument list.
3965 * Also close windows that are not full width;
3966 * When 'hidden' or "forceit" set the buffer becomes hidden.
3967 * Windows that have a changed buffer and can't be hidden won't be closed.
3968 */
3969 for (wp = firstwin; wp != NULL; wp = wpnext)
3970 {
3971 wpnext = wp->w_next;
3972 buf = wp->w_buffer;
3973 if (buf->b_ffname == NULL
3974 || buf->b_nwindows > 1
3975#ifdef FEAT_VERTSPLIT
3976 || wp->w_width != Columns
3977#endif
3978 )
3979 i = ARGCOUNT;
3980 else
3981 {
3982 /* check if the buffer in this window is in the arglist */
3983 for (i = 0; i < ARGCOUNT; ++i)
3984 {
3985 if (ARGLIST[i].ae_fnum == buf->b_fnum
3986 || fullpathcmp(alist_name(&ARGLIST[i]),
3987 buf->b_ffname, TRUE) & FPC_SAME)
3988 {
3989 if (i < opened_len)
3990 opened[i] = TRUE;
3991 if (wp->w_alist != curwin->w_alist)
3992 {
3993 /* Use the current argument list for all windows
3994 * containing a file from it. */
3995 alist_unlink(wp->w_alist);
3996 wp->w_alist = curwin->w_alist;
3997 ++wp->w_alist->al_refcount;
3998 }
3999 break;
4000 }
4001 }
4002 }
4003 wp->w_arg_idx = i;
4004
4005 if (i == ARGCOUNT) /* close this window */
4006 {
4007 if (P_HID(buf) || forceit || buf->b_nwindows > 1
4008 || !bufIsChanged(buf))
4009 {
4010 /* If the buffer was changed, and we would like to hide it,
4011 * try autowriting. */
4012 if (!P_HID(buf) && buf->b_nwindows <= 1 && bufIsChanged(buf))
4013 {
4014 (void)autowrite(buf, FALSE);
4015#ifdef FEAT_AUTOCMD
4016 /* check if autocommands removed the window */
4017 if (!win_valid(wp) || !buf_valid(buf))
4018 {
4019 wpnext = firstwin; /* start all over... */
4020 continue;
4021 }
4022#endif
4023 }
4024#ifdef FEAT_WINDOWS
4025 if (firstwin == lastwin) /* can't close last window */
4026#endif
4027 use_firstwin = TRUE;
4028#ifdef FEAT_WINDOWS
4029 else
4030 {
4031 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4032# ifdef FEAT_AUTOCMD
4033 /* check if autocommands removed the next window */
4034 if (!win_valid(wpnext))
4035 wpnext = firstwin; /* start all over... */
4036# endif
4037 }
4038#endif
4039 }
4040 }
4041 }
4042
4043 /*
4044 * Open a window for files in the argument list that don't have one.
4045 * ARGCOUNT may change while doing this, because of autocommands.
4046 */
4047 if (count > ARGCOUNT || count <= 0)
4048 count = ARGCOUNT;
4049
4050 /* Autocommands may do anything to the argument list. Make sure it's not
4051 * freed while we are working here by "locking" it. We still have to
4052 * watch out for its size to be changed. */
4053 alist = curwin->w_alist;
4054 ++alist->al_refcount;
4055
4056#ifdef FEAT_AUTOCMD
4057 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4058 ++autocmd_no_enter;
4059 ++autocmd_no_leave;
4060#endif
4061 win_enter(lastwin, FALSE);
4062 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4063 {
4064 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4065 arg_had_last = TRUE;
4066 if (i < opened_len && opened[i])
4067 {
4068 /* Move the already present window to below the current window */
4069 if (curwin->w_arg_idx != i)
4070 {
4071 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4072 {
4073 if (wpnext->w_arg_idx == i)
4074 {
4075 win_move_after(wpnext, curwin);
4076 break;
4077 }
4078 }
4079 }
4080 }
4081 else if (split_ret == OK)
4082 {
4083 if (!use_firstwin) /* split current window */
4084 {
4085 p_ea_save = p_ea;
4086 p_ea = TRUE; /* use space from all windows */
4087 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4088 p_ea = p_ea_save;
4089 if (split_ret == FAIL)
4090 continue;
4091 }
4092#ifdef FEAT_AUTOCMD
4093 else /* first window: do autocmd for leaving this buffer */
4094 --autocmd_no_leave;
4095#endif
4096
4097 /*
4098 * edit file i
4099 */
4100 curwin->w_arg_idx = i;
4101 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4102 ECMD_ONE,
4103 ((P_HID(curwin->w_buffer)
4104 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4105 + ECMD_OLDBUF);
4106#ifdef FEAT_AUTOCMD
4107 if (use_firstwin)
4108 ++autocmd_no_leave;
4109#endif
4110 use_firstwin = FALSE;
4111 }
4112 ui_breakcheck();
4113 }
4114
4115 /* Remove the "lock" on the argument list. */
4116 alist_unlink(alist);
4117
4118#ifdef FEAT_AUTOCMD
4119 --autocmd_no_enter;
4120#endif
4121 win_enter(firstwin, FALSE); /* back to first window */
4122#ifdef FEAT_AUTOCMD
4123 --autocmd_no_leave;
4124#endif
4125 vim_free(opened);
4126}
4127
4128# if defined(FEAT_LISTCMDS) || defined(PROTO)
4129/*
4130 * Open a window for a number of buffers.
4131 */
4132 void
4133ex_buffer_all(eap)
4134 exarg_T *eap;
4135{
4136 buf_T *buf;
4137 win_T *wp, *wpnext;
4138 int split_ret = OK;
4139 int p_ea_save;
4140 int open_wins = 0;
4141 int r;
4142 int count; /* Maximum number of windows to open. */
4143 int all; /* When TRUE also load inactive buffers. */
4144
4145 if (eap->addr_count == 0) /* make as many windows as possible */
4146 count = 9999;
4147 else
4148 count = eap->line2; /* make as many windows as specified */
4149 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4150 all = FALSE;
4151 else
4152 all = TRUE;
4153
4154 setpcmark();
4155
4156#ifdef FEAT_GUI
4157 need_mouse_correct = TRUE;
4158#endif
4159
4160 /*
4161 * Close superfluous windows (two windows for the same buffer).
4162 * Also close windows that are not full-width.
4163 */
4164 for (wp = firstwin; wp != NULL; wp = wpnext)
4165 {
4166 wpnext = wp->w_next;
4167 if (wp->w_buffer->b_nwindows > 1
4168#ifdef FEAT_VERTSPLIT
4169 || ((cmdmod.split & WSP_VERT)
4170 ? wp->w_height + wp->w_status_height < Rows - p_ch
4171 : wp->w_width != Columns)
4172#endif
4173 )
4174 {
4175 win_close(wp, FALSE);
4176#ifdef FEAT_AUTOCMD
4177 wpnext = firstwin; /* just in case an autocommand does something
4178 strange with windows */
4179 open_wins = 0;
4180#endif
4181 }
4182 else
4183 ++open_wins;
4184 }
4185
4186 /*
4187 * Go through the buffer list. When a buffer doesn't have a window yet,
4188 * open one. Otherwise move the window to the right position.
4189 * Watch out for autocommands that delete buffers or windows!
4190 */
4191#ifdef FEAT_AUTOCMD
4192 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4193 ++autocmd_no_enter;
4194#endif
4195 win_enter(lastwin, FALSE);
4196#ifdef FEAT_AUTOCMD
4197 ++autocmd_no_leave;
4198#endif
4199 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4200 {
4201 /* Check if this buffer needs a window */
4202 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4203 continue;
4204
4205 /* Check if this buffer already has a window */
4206 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4207 if (wp->w_buffer == buf)
4208 break;
4209 /* If the buffer already has a window, move it */
4210 if (wp != NULL)
4211 win_move_after(wp, curwin);
4212 else if (split_ret == OK)
4213 {
4214 /* Split the window and put the buffer in it */
4215 p_ea_save = p_ea;
4216 p_ea = TRUE; /* use space from all windows */
4217 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4218 ++open_wins;
4219 p_ea = p_ea_save;
4220 if (split_ret == FAIL)
4221 continue;
4222
4223 /* Open the buffer in this window. */
4224#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4225 swap_exists_action = SEA_DIALOG;
4226#endif
4227 set_curbuf(buf, DOBUF_GOTO);
4228#ifdef FEAT_AUTOCMD
4229# ifdef FEAT_EVAL
4230 /* Autocommands deleted the buffer or aborted script
4231 * processing!!! */
4232 if (!buf_valid(buf) || aborting())
4233# else
4234 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
4235# endif
4236 {
4237#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4238 swap_exists_action = SEA_NONE;
4239#endif
4240 break;
4241 }
4242#endif
4243#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4244 if (swap_exists_action == SEA_QUIT)
4245 {
4246 /* User selected Quit at ATTENTION prompt; close this window. */
4247 win_close(curwin, TRUE);
4248 --open_wins;
4249 swap_exists_action = SEA_NONE;
4250 }
4251 else
4252 handle_swap_exists(NULL);
4253#endif
4254 }
4255
4256 ui_breakcheck();
4257 if (got_int)
4258 {
4259 (void)vgetc(); /* only break the file loading, not the rest */
4260 break;
4261 }
4262 }
4263#ifdef FEAT_AUTOCMD
4264 --autocmd_no_enter;
4265#endif
4266 win_enter(firstwin, FALSE); /* back to first window */
4267#ifdef FEAT_AUTOCMD
4268 --autocmd_no_leave;
4269#endif
4270
4271 /*
4272 * Close superfluous windows.
4273 */
4274 for (wp = lastwin; open_wins > count; )
4275 {
4276 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4277 || autowrite(wp->w_buffer, FALSE) == OK);
4278#ifdef FEAT_AUTOCMD
4279 if (!win_valid(wp))
4280 {
4281 /* BufWrite Autocommands made the window invalid, start over */
4282 wp = lastwin;
4283 }
4284 else
4285#endif
4286 if (r)
4287 {
4288 win_close(wp, !P_HID(wp->w_buffer));
4289 --open_wins;
4290 wp = lastwin;
4291 }
4292 else
4293 {
4294 wp = wp->w_prev;
4295 if (wp == NULL)
4296 break;
4297 }
4298 }
4299}
4300# endif /* FEAT_LISTCMDS */
4301
4302#endif /* FEAT_WINDOWS */
4303
4304/*
4305 * do_modelines() - process mode lines for the current file
4306 *
4307 * Returns immediately if the "ml" option isn't set.
4308 */
4309static int chk_modeline __ARGS((linenr_T));
4310
4311 void
4312do_modelines()
4313{
4314 linenr_T lnum;
4315 int nmlines;
4316 static int entered = 0;
4317
4318 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4319 return;
4320
4321 /* Disallow recursive entry here. Can happen when executing a modeline
4322 * triggers an autocommand, which reloads modelines with a ":do". */
4323 if (entered)
4324 return;
4325
4326 ++entered;
4327 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4328 ++lnum)
4329 if (chk_modeline(lnum) == FAIL)
4330 nmlines = 0;
4331
4332 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4333 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
4334 if (chk_modeline(lnum) == FAIL)
4335 nmlines = 0;
4336 --entered;
4337}
4338
4339#include "version.h" /* for version number */
4340
4341/*
4342 * chk_modeline() - check a single line for a mode string
4343 * Return FAIL if an error encountered.
4344 */
4345 static int
4346chk_modeline(lnum)
4347 linenr_T lnum;
4348{
4349 char_u *s;
4350 char_u *e;
4351 char_u *linecopy; /* local copy of any modeline found */
4352 int prev;
4353 int vers;
4354 int end;
4355 int retval = OK;
4356 char_u *save_sourcing_name;
4357 linenr_T save_sourcing_lnum;
4358#ifdef FEAT_EVAL
4359 scid_T save_SID;
4360#endif
4361
4362 prev = -1;
4363 for (s = ml_get(lnum); *s != NUL; ++s)
4364 {
4365 if (prev == -1 || vim_isspace(prev))
4366 {
4367 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4368 || STRNCMP(s, "vi:", (size_t)3) == 0)
4369 break;
4370 if (STRNCMP(s, "vim", 3) == 0)
4371 {
4372 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4373 e = s + 4;
4374 else
4375 e = s + 3;
4376 vers = getdigits(&e);
4377 if (*e == ':'
4378 && (s[3] == ':'
4379 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4380 || (VIM_VERSION_100 < vers && s[3] == '<')
4381 || (VIM_VERSION_100 > vers && s[3] == '>')
4382 || (VIM_VERSION_100 == vers && s[3] == '=')))
4383 break;
4384 }
4385 }
4386 prev = *s;
4387 }
4388
4389 if (*s)
4390 {
4391 do /* skip over "ex:", "vi:" or "vim:" */
4392 ++s;
4393 while (s[-1] != ':');
4394
4395 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4396 if (linecopy == NULL)
4397 return FAIL;
4398
4399 save_sourcing_lnum = sourcing_lnum;
4400 save_sourcing_name = sourcing_name;
4401 sourcing_lnum = lnum; /* prepare for emsg() */
4402 sourcing_name = (char_u *)"modelines";
4403
4404 end = FALSE;
4405 while (end == FALSE)
4406 {
4407 s = skipwhite(s);
4408 if (*s == NUL)
4409 break;
4410
4411 /*
4412 * Find end of set command: ':' or end of line.
4413 * Skip over "\:", replacing it with ":".
4414 */
4415 for (e = s; *e != ':' && *e != NUL; ++e)
4416 if (e[0] == '\\' && e[1] == ':')
4417 STRCPY(e, e + 1);
4418 if (*e == NUL)
4419 end = TRUE;
4420
4421 /*
4422 * If there is a "set" command, require a terminating ':' and
4423 * ignore the stuff after the ':'.
4424 * "vi:set opt opt opt: foo" -- foo not interpreted
4425 * "vi:opt opt opt: foo" -- foo interpreted
4426 * Accept "se" for compatibility with Elvis.
4427 */
4428 if (STRNCMP(s, "set ", (size_t)4) == 0
4429 || STRNCMP(s, "se ", (size_t)3) == 0)
4430 {
4431 if (*e != ':') /* no terminating ':'? */
4432 break;
4433 end = TRUE;
4434 s = vim_strchr(s, ' ') + 1;
4435 }
4436 *e = NUL; /* truncate the set command */
4437
4438 if (*s != NUL) /* skip over an empty "::" */
4439 {
4440#ifdef FEAT_EVAL
4441 save_SID = current_SID;
4442 current_SID = SID_MODELINE;
4443#endif
4444 retval = do_set(s, OPT_MODELINE | OPT_LOCAL);
4445#ifdef FEAT_EVAL
4446 current_SID = save_SID;
4447#endif
4448 if (retval == FAIL) /* stop if error found */
4449 break;
4450 }
4451 s = e + 1; /* advance to next part */
4452 }
4453
4454 sourcing_lnum = save_sourcing_lnum;
4455 sourcing_name = save_sourcing_name;
4456
4457 vim_free(linecopy);
4458 }
4459 return retval;
4460}
4461
4462#ifdef FEAT_VIMINFO
4463 int
4464read_viminfo_bufferlist(virp, writing)
4465 vir_T *virp;
4466 int writing;
4467{
4468 char_u *tab;
4469 linenr_T lnum;
4470 colnr_T col;
4471 buf_T *buf;
4472 char_u *sfname;
4473 char_u *xline;
4474
4475 /* Handle long line and escaped characters. */
4476 xline = viminfo_readstring(virp, 1, FALSE);
4477
4478 /* don't read in if there are files on the command-line or if writing: */
4479 if (xline != NULL && !writing && ARGCOUNT == 0
4480 && find_viminfo_parameter('%') != NULL)
4481 {
4482 /* Format is: <fname> Tab <lnum> Tab <col>.
4483 * Watch out for a Tab in the file name, work from the end. */
4484 lnum = 0;
4485 col = 0;
4486 tab = vim_strrchr(xline, '\t');
4487 if (tab != NULL)
4488 {
4489 *tab++ = '\0';
4490 col = atoi((char *)tab);
4491 tab = vim_strrchr(xline, '\t');
4492 if (tab != NULL)
4493 {
4494 *tab++ = '\0';
4495 lnum = atol((char *)tab);
4496 }
4497 }
4498
4499 /* Expand "~/" in the file name at "line + 1" to a full path.
4500 * Then try shortening it by comparing with the current directory */
4501 expand_env(xline, NameBuff, MAXPATHL);
4502 mch_dirname(IObuff, IOSIZE);
4503 sfname = shorten_fname(NameBuff, IObuff);
4504 if (sfname == NULL)
4505 sfname = NameBuff;
4506
4507 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
4508 if (buf != NULL) /* just in case... */
4509 {
4510 buf->b_last_cursor.lnum = lnum;
4511 buf->b_last_cursor.col = col;
4512 buflist_setfpos(buf, curwin, lnum, col, FALSE);
4513 }
4514 }
4515 vim_free(xline);
4516
4517 return viminfo_readline(virp);
4518}
4519
4520 void
4521write_viminfo_bufferlist(fp)
4522 FILE *fp;
4523{
4524 buf_T *buf;
4525#ifdef FEAT_WINDOWS
4526 win_T *win;
4527#endif
4528 char_u *line;
4529
4530 if (find_viminfo_parameter('%') == NULL)
4531 return;
4532
4533 /* Allocate room for the file name, lnum and col. */
4534 line = alloc(MAXPATHL + 30);
4535 if (line == NULL)
4536 return;
4537
4538#ifdef FEAT_WINDOWS
4539 for (win = firstwin; win != NULL; win = win->w_next)
4540 set_last_cursor(win);
4541#else
4542 set_last_cursor(curwin);
4543#endif
4544
4545 fprintf(fp, _("\n# Buffer list:\n"));
4546 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
4547 {
4548 if (buf->b_fname == NULL
4549 || !buf->b_p_bl
4550#ifdef FEAT_QUICKFIX
4551 || bt_quickfix(buf)
4552#endif
4553 || removable(buf->b_ffname))
4554 continue;
4555
4556 putc('%', fp);
4557 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
4558 sprintf((char *)line + STRLEN(line), "\t%ld\t%d",
4559 (long)buf->b_last_cursor.lnum,
4560 buf->b_last_cursor.col);
4561 viminfo_writestring(fp, line);
4562 }
4563 vim_free(line);
4564}
4565#endif
4566
4567
4568/*
4569 * Return special buffer name.
4570 * Returns NULL when the buffer has a normal file name.
4571 */
4572 char *
4573buf_spname(buf)
4574 buf_T *buf;
4575{
4576#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
4577 if (bt_quickfix(buf))
4578 return _("[Error List]");
4579#endif
4580#ifdef FEAT_QUICKFIX
4581 /* There is no _file_ when 'buftype' is "nofile", b_sfname
4582 * contains the name as specified by the user */
4583 if (bt_nofile(buf))
4584 {
4585 if (buf->b_sfname != NULL)
4586 return (char *)buf->b_sfname;
4587 return "[Scratch]";
4588 }
4589#endif
4590 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004591 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 return NULL;
4593}
4594
4595
4596#if defined(FEAT_SIGNS) || defined(PROTO)
4597/*
4598 * Insert the sign into the signlist.
4599 */
4600 static void
4601insert_sign(buf, prev, next, id, lnum, typenr)
4602 buf_T *buf; /* buffer to store sign in */
4603 signlist_T *prev; /* previous sign entry */
4604 signlist_T *next; /* next sign entry */
4605 int id; /* sign ID */
4606 linenr_T lnum; /* line number which gets the mark */
4607 int typenr; /* typenr of sign we are adding */
4608{
4609 signlist_T *newsign;
4610
4611 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
4612 if (newsign != NULL)
4613 {
4614 newsign->id = id;
4615 newsign->lnum = lnum;
4616 newsign->typenr = typenr;
4617 newsign->next = next;
4618#ifdef FEAT_NETBEANS_INTG
4619 newsign->prev = prev;
4620 if (next != NULL)
4621 next->prev = newsign;
4622#endif
4623
4624 if (prev == NULL)
4625 {
4626 /* When adding first sign need to redraw the windows to create the
4627 * column for signs. */
4628 if (buf->b_signlist == NULL)
4629 {
4630 redraw_buf_later(buf, NOT_VALID);
4631 changed_cline_bef_curs();
4632 }
4633
4634 /* first sign in signlist */
4635 buf->b_signlist = newsign;
4636 }
4637 else
4638 prev->next = newsign;
4639 }
4640}
4641
4642/*
4643 * Add the sign into the signlist. Find the right spot to do it though.
4644 */
4645 void
4646buf_addsign(buf, id, lnum, typenr)
4647 buf_T *buf; /* buffer to store sign in */
4648 int id; /* sign ID */
4649 linenr_T lnum; /* line number which gets the mark */
4650 int typenr; /* typenr of sign we are adding */
4651{
4652 signlist_T *sign; /* a sign in the signlist */
4653 signlist_T *prev; /* the previous sign */
4654
4655 prev = NULL;
4656 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
4657 {
4658 if (lnum == sign->lnum && id == sign->id)
4659 {
4660 sign->typenr = typenr;
4661 return;
4662 }
4663 else if (
4664#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
4665 id < 0 &&
4666#endif
4667 lnum < sign->lnum)
4668 {
4669#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
4670 /* XXX - GRP: Is this because of sign slide problem? Or is it
4671 * really needed? Or is it because we allow multiple signs per
4672 * line? If so, should I add that feature to FEAT_SIGNS?
4673 */
4674 while (prev != NULL && prev->lnum == lnum)
4675 prev = prev->prev;
4676 if (prev == NULL)
4677 sign = buf->b_signlist;
4678 else
4679 sign = prev->next;
4680#endif
4681 insert_sign(buf, prev, sign, id, lnum, typenr);
4682 return;
4683 }
4684 prev = sign;
4685 }
4686#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
4687 /* XXX - GRP: See previous comment */
4688 while (prev != NULL && prev->lnum == lnum)
4689 prev = prev->prev;
4690 if (prev == NULL)
4691 sign = buf->b_signlist;
4692 else
4693 sign = prev->next;
4694#endif
4695 insert_sign(buf, prev, sign, id, lnum, typenr);
4696
4697 return;
4698}
4699
4700 int
4701buf_change_sign_type(buf, markId, typenr)
4702 buf_T *buf; /* buffer to store sign in */
4703 int markId; /* sign ID */
4704 int typenr; /* typenr of sign we are adding */
4705{
4706 signlist_T *sign; /* a sign in the signlist */
4707
4708 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
4709 {
4710 if (sign->id == markId)
4711 {
4712 sign->typenr = typenr;
4713 return sign->lnum;
4714 }
4715 }
4716
4717 return 0;
4718}
4719
4720 int_u
4721buf_getsigntype(buf, lnum, type)
4722 buf_T *buf;
4723 linenr_T lnum;
4724 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
4725{
4726 signlist_T *sign; /* a sign in a b_signlist */
4727
4728 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
4729 if (sign->lnum == lnum
4730 && (type == SIGN_ANY
4731# ifdef FEAT_SIGN_ICONS
4732 || (type == SIGN_ICON
4733 && sign_get_image(sign->typenr) != NULL)
4734# endif
4735 || (type == SIGN_TEXT
4736 && sign_get_text(sign->typenr) != NULL)
4737 || (type == SIGN_LINEHL
4738 && sign_get_attr(sign->typenr, TRUE) != 0)))
4739 return sign->typenr;
4740 return 0;
4741}
4742
4743
4744 linenr_T
4745buf_delsign(buf, id)
4746 buf_T *buf; /* buffer sign is stored in */
4747 int id; /* sign id */
4748{
4749 signlist_T **lastp; /* pointer to pointer to current sign */
4750 signlist_T *sign; /* a sign in a b_signlist */
4751 signlist_T *next; /* the next sign in a b_signlist */
4752 linenr_T lnum; /* line number whose sign was deleted */
4753
4754 lastp = &buf->b_signlist;
4755 lnum = 0;
4756 for (sign = buf->b_signlist; sign != NULL; sign = next)
4757 {
4758 next = sign->next;
4759 if (sign->id == id)
4760 {
4761 *lastp = next;
4762#ifdef FEAT_NETBEANS_INTG
4763 if (next != NULL)
4764 next->prev = sign->prev;
4765#endif
4766 lnum = sign->lnum;
4767 vim_free(sign);
4768 break;
4769 }
4770 else
4771 lastp = &sign->next;
4772 }
4773
4774 /* When deleted the last sign need to redraw the windows to remove the
4775 * sign column. */
4776 if (buf->b_signlist == NULL)
4777 {
4778 redraw_buf_later(buf, NOT_VALID);
4779 changed_cline_bef_curs();
4780 }
4781
4782 return lnum;
4783}
4784
4785
4786/*
4787 * Find the line number of the sign with the requested id. If the sign does
4788 * not exist, return 0 as the line number. This will still let the correct file
4789 * get loaded.
4790 */
4791 int
4792buf_findsign(buf, id)
4793 buf_T *buf; /* buffer to store sign in */
4794 int id; /* sign ID */
4795{
4796 signlist_T *sign; /* a sign in the signlist */
4797
4798 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
4799 if (sign->id == id)
4800 return sign->lnum;
4801
4802 return 0;
4803}
4804
4805 int
4806buf_findsign_id(buf, lnum)
4807 buf_T *buf; /* buffer whose sign we are searching for */
4808 linenr_T lnum; /* line number of sign */
4809{
4810 signlist_T *sign; /* a sign in the signlist */
4811
4812 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
4813 if (sign->lnum == lnum)
4814 return sign->id;
4815
4816 return 0;
4817}
4818
4819
4820# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
4821/* see if a given type of sign exists on a specific line */
4822 int
4823buf_findsigntype_id(buf, lnum, typenr)
4824 buf_T *buf; /* buffer whose sign we are searching for */
4825 linenr_T lnum; /* line number of sign */
4826 int typenr; /* sign type number */
4827{
4828 signlist_T *sign; /* a sign in the signlist */
4829
4830 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
4831 if (sign->lnum == lnum && sign->typenr == typenr)
4832 return sign->id;
4833
4834 return 0;
4835}
4836
4837
4838# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
4839/* return the number of icons on the given line */
4840 int
4841buf_signcount(buf, lnum)
4842 buf_T *buf;
4843 linenr_T lnum;
4844{
4845 signlist_T *sign; /* a sign in the signlist */
4846 int count = 0;
4847
4848 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
4849 if (sign->lnum == lnum)
4850 if (sign_get_image(sign->typenr) != NULL)
4851 count++;
4852
4853 return count;
4854}
4855# endif /* FEAT_SIGN_ICONS */
4856# endif /* FEAT_NETBEANS_INTG */
4857
4858
4859/*
4860 * Delete signs in buffer "buf".
4861 */
4862 static void
4863buf_delete_signs(buf)
4864 buf_T *buf;
4865{
4866 signlist_T *next;
4867
4868 while (buf->b_signlist != NULL)
4869 {
4870 next = buf->b_signlist->next;
4871 vim_free(buf->b_signlist);
4872 buf->b_signlist = next;
4873 }
4874}
4875
4876/*
4877 * Delete all signs in all buffers.
4878 */
4879 void
4880buf_delete_all_signs()
4881{
4882 buf_T *buf; /* buffer we are checking for signs */
4883
4884 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4885 if (buf->b_signlist != NULL)
4886 {
4887 /* Need to redraw the windows to remove the sign column. */
4888 redraw_buf_later(buf, NOT_VALID);
4889 buf_delete_signs(buf);
4890 }
4891}
4892
4893/*
4894 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
4895 */
4896 void
4897sign_list_placed(rbuf)
4898 buf_T *rbuf;
4899{
4900 buf_T *buf;
4901 signlist_T *p;
4902 char lbuf[BUFSIZ];
4903
4904 MSG_PUTS_TITLE(_("\n--- Signs ---"));
4905 msg_putchar('\n');
4906 if (rbuf == NULL)
4907 buf = firstbuf;
4908 else
4909 buf = rbuf;
4910 while (buf != NULL)
4911 {
4912 if (buf->b_signlist != NULL)
4913 {
4914#ifdef HAVE_SNPRINTF
4915 snprintf
4916#else
4917 sprintf
4918#endif
4919 (lbuf,
4920#ifdef HAVE_SNPRINTF
4921 BUFSIZ,
4922#endif
4923 _("Signs for %s:"), buf->b_fname);
4924 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
4925 msg_putchar('\n');
4926 }
4927 for (p = buf->b_signlist; p != NULL; p = p->next)
4928 {
4929 sprintf(lbuf, _(" line=%ld id=%d name=%s"),
4930 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
4931 MSG_PUTS(lbuf);
4932 msg_putchar('\n');
4933 }
4934 if (rbuf != NULL)
4935 break;
4936 buf = buf->b_next;
4937 }
4938}
4939
4940/*
4941 * Adjust a placed sign for inserted/deleted lines.
4942 */
4943 void
4944sign_mark_adjust(line1, line2, amount, amount_after)
4945 linenr_T line1;
4946 linenr_T line2;
4947 long amount;
4948 long amount_after;
4949{
4950 signlist_T *sign; /* a sign in a b_signlist */
4951
4952 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
4953 {
4954 if (sign->lnum >= line1 && sign->lnum <= line2)
4955 {
4956 if (amount == MAXLNUM)
4957 sign->lnum = line1;
4958 else
4959 sign->lnum += amount;
4960 }
4961 else if (sign->lnum > line2)
4962 sign->lnum += amount_after;
4963 }
4964}
4965#endif /* FEAT_SIGNS */
4966
4967/*
4968 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
4969 */
4970 void
4971set_buflisted(on)
4972 int on;
4973{
4974 if (on != curbuf->b_p_bl)
4975 {
4976 curbuf->b_p_bl = on;
4977#ifdef FEAT_AUTOCMD
4978 if (on)
4979 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
4980 else
4981 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
4982#endif
4983 }
4984}
4985
4986/*
4987 * Read the file for "buf" again and check if the contents changed.
4988 * Return TRUE if it changed or this could not be checked.
4989 */
4990 int
4991buf_contents_changed(buf)
4992 buf_T *buf;
4993{
4994 buf_T *newbuf;
4995 int differ = TRUE;
4996 linenr_T lnum;
4997#ifdef FEAT_AUTOCMD
4998 aco_save_T aco;
4999#else
5000 buf_T *old_curbuf = curbuf;
5001#endif
5002 exarg_T ea;
5003
5004 /* Allocate a buffer without putting it in the buffer list. */
5005 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5006 if (newbuf == NULL)
5007 return TRUE;
5008
5009 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5010 if (prep_exarg(&ea, buf) == FAIL)
5011 {
5012 wipe_buffer(newbuf, FALSE);
5013 return TRUE;
5014 }
5015
5016#ifdef FEAT_AUTOCMD
5017 /* set curwin/curbuf to buf and save a few things */
5018 aucmd_prepbuf(&aco, newbuf);
5019#else
5020 curbuf = newbuf;
5021 curwin->w_buffer = newbuf;
5022#endif
5023
5024 if (ml_open() == OK
5025 && readfile(buf->b_ffname, buf->b_fname,
5026 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5027 &ea, READ_NEW | READ_DUMMY) == OK)
5028 {
5029 /* compare the two files line by line */
5030 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5031 {
5032 differ = FALSE;
5033 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5034 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5035 {
5036 differ = TRUE;
5037 break;
5038 }
5039 }
5040 }
5041 vim_free(ea.cmd);
5042
5043#ifdef FEAT_AUTOCMD
5044 /* restore curwin/curbuf and a few other things */
5045 aucmd_restbuf(&aco);
5046#else
5047 curbuf = old_curbuf;
5048 curwin->w_buffer = old_curbuf;
5049#endif
5050
5051 if (curbuf != newbuf) /* safety check */
5052 wipe_buffer(newbuf, FALSE);
5053
5054 return differ;
5055}
5056
5057/*
5058 * Wipe out a buffer and decrement the last buffer number if it was used for
5059 * this buffer. Call this to wipe out a temp buffer that does not contain any
5060 * marks.
5061 */
5062/*ARGSUSED*/
5063 void
5064wipe_buffer(buf, aucmd)
5065 buf_T *buf;
5066 int aucmd; /* When TRUE trigger autocommands. */
5067{
5068 if (buf->b_fnum == top_file_num - 1)
5069 --top_file_num;
5070
5071#ifdef FEAT_AUTOCMD
5072 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5073 ++autocmd_block;
5074#endif
5075 close_buffer(NULL, buf, DOBUF_WIPE);
5076#ifdef FEAT_AUTOCMD
5077 if (!aucmd)
5078 --autocmd_block;
5079#endif
5080}