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