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