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