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