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