blob: 4104977d67b8167df0953d97b09ccb335bb67c16 [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
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
30#if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
31static char_u *buflist_match __ARGS((regprog_T *prog, buf_T *buf));
32# define HAVE_BUFLIST_MATCH
33static char_u *fname_match __ARGS((regprog_T *prog, char_u *name));
34#endif
35static void buflist_setfpos __ARGS((buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options));
Bram Moolenaar701f7af2008-11-15 13:12:07 +000036static wininfo_T *find_wininfo __ARGS((buf_T *buf, int skip_diff_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef UNIX
38static buf_T *buflist_findname_stat __ARGS((char_u *ffname, struct stat *st));
39static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname, struct stat *stp));
40static int buf_same_ino __ARGS((buf_T *buf, struct stat *stp));
41#else
42static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname));
43#endif
44#ifdef FEAT_TITLE
45static int ti_change __ARGS((char_u *str, char_u **last));
46#endif
47static void free_buffer __ARGS((buf_T *));
48static void free_buffer_stuff __ARGS((buf_T *buf, int free_options));
49static void clear_wininfo __ARGS((buf_T *buf));
50
51#ifdef UNIX
52# define dev_T dev_t
53#else
54# define dev_T unsigned
55#endif
56
57#if defined(FEAT_SIGNS)
58static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr));
59static void buf_delete_signs __ARGS((buf_T *buf));
60#endif
61
62/*
63 * Open current buffer, that is: open the memfile and read the file into memory
64 * return FAIL for failure, OK otherwise
65 */
66 int
67open_buffer(read_stdin, eap)
68 int read_stdin; /* read file from stdin */
69 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
70{
71 int retval = OK;
72#ifdef FEAT_AUTOCMD
73 buf_T *old_curbuf;
74#endif
75
76 /*
77 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
78 * When re-entering the same buffer, it should not change, because the
79 * user may have reset the flag by hand.
80 */
81 if (readonlymode && curbuf->b_ffname != NULL
82 && (curbuf->b_flags & BF_NEVERLOADED))
83 curbuf->b_p_ro = TRUE;
84
Bram Moolenaar4770d092006-01-12 23:22:24 +000085 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 {
87 /*
88 * There MUST be a memfile, otherwise we can't do anything
89 * If we can't create one for the current buffer, take another buffer
90 */
91 close_buffer(NULL, curbuf, 0);
92 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
93 if (curbuf->b_ml.ml_mfp != NULL)
94 break;
95 /*
96 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +000097 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 */
99 if (curbuf == NULL)
100 {
101 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
102 getout(2);
103 }
104 EMSG(_("E83: Cannot allocate buffer, using other one..."));
105 enter_buffer(curbuf);
106 return FAIL;
107 }
108
109#ifdef FEAT_AUTOCMD
110 /* The autocommands in readfile() may change the buffer, but only AFTER
111 * reading the file. */
112 old_curbuf = curbuf;
113 modified_was_set = FALSE;
114#endif
115
116 /* mark cursor position as being invalid */
117 changed_line_abv_curs();
118
119 if (curbuf->b_ffname != NULL
120#ifdef FEAT_NETBEANS_INTG
121 && netbeansReadFile
122#endif
123 )
124 {
125#ifdef FEAT_NETBEANS_INTG
126 int oldFire = netbeansFireChanges;
127
128 netbeansFireChanges = 0;
129#endif
130 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
131 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_NEW);
132#ifdef FEAT_NETBEANS_INTG
133 netbeansFireChanges = oldFire;
134#endif
135 /* Help buffer is filtered. */
136 if (curbuf->b_help)
137 fix_help_buffer();
138 }
139 else if (read_stdin)
140 {
141 int save_bin = curbuf->b_p_bin;
142 linenr_T line_count;
143
144 /*
145 * First read the text in binary mode into the buffer.
146 * Then read from that same buffer and append at the end. This makes
147 * it possible to retry when 'fileformat' or 'fileencoding' was
148 * guessed wrong.
149 */
150 curbuf->b_p_bin = TRUE;
151 retval = readfile(NULL, NULL, (linenr_T)0,
152 (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW + READ_STDIN);
153 curbuf->b_p_bin = save_bin;
154 if (retval == OK)
155 {
156 line_count = curbuf->b_ml.ml_line_count;
157 retval = readfile(NULL, NULL, (linenr_T)line_count,
158 (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_BUFFER);
159 if (retval == OK)
160 {
161 /* Delete the binary lines. */
162 while (--line_count >= 0)
163 ml_delete((linenr_T)1, FALSE);
164 }
165 else
166 {
167 /* Delete the converted lines. */
168 while (curbuf->b_ml.ml_line_count > line_count)
169 ml_delete(line_count, FALSE);
170 }
171 /* Put the cursor on the first line. */
172 curwin->w_cursor.lnum = 1;
173 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000174
175 /* Set or reset 'modified' before executing autocommands, so that
176 * it can be changed there. */
177 if (!readonlymode && !bufempty())
178 changed();
179 else if (retval != FAIL)
180 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#ifdef FEAT_AUTOCMD
182# ifdef FEAT_EVAL
183 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
184 curbuf, &retval);
185# else
186 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
187# endif
188#endif
189 }
190 }
191
192 /* if first time loading this buffer, init b_chartab[] */
193 if (curbuf->b_flags & BF_NEVERLOADED)
194 (void)buf_init_chartab(curbuf, FALSE);
195
196 /*
197 * Set/reset the Changed flag first, autocmds may change the buffer.
198 * Apply the automatic commands, before processing the modelines.
199 * So the modelines have priority over auto commands.
200 */
201 /* When reading stdin, the buffer contents always needs writing, so set
202 * the changed flag. Unless in readonly mode: "ls | gview -".
203 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000204 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#ifdef FEAT_AUTOCMD
206 || modified_was_set /* ":set modified" used in autocmd */
207# ifdef FEAT_EVAL
208 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
209# endif
210#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000211 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000213 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 unchanged(curbuf, FALSE);
215 save_file_ff(curbuf); /* keep this fileformat */
216
217 /* require "!" to overwrite the file, because it wasn't read completely */
218#ifdef FEAT_EVAL
219 if (aborting())
220#else
221 if (got_int)
222#endif
223 curbuf->b_flags |= BF_READERR;
224
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000225#ifdef FEAT_FOLDING
226 /* Need to update automatic folding. Do this before the autocommands,
227 * they may use the fold info. */
228 foldUpdateAll(curwin);
229#endif
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#ifdef FEAT_AUTOCMD
232 /* need to set w_topline, unless some autocommand already did that. */
233 if (!(curwin->w_valid & VALID_TOPLINE))
234 {
235 curwin->w_topline = 1;
236# ifdef FEAT_DIFF
237 curwin->w_topfill = 0;
238# endif
239 }
240# ifdef FEAT_EVAL
241 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
242# else
243 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
244# endif
245#endif
246
247 if (retval != FAIL)
248 {
249#ifdef FEAT_AUTOCMD
250 /*
251 * The autocommands may have changed the current buffer. Apply the
252 * modelines to the correct buffer, if it still exists and is loaded.
253 */
254 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
255 {
256 aco_save_T aco;
257
258 /* Go to the buffer that was opened. */
259 aucmd_prepbuf(&aco, old_curbuf);
260#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000261 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
263
264#ifdef FEAT_AUTOCMD
265# ifdef FEAT_EVAL
266 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
267 &retval);
268# else
269 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
270# endif
271
272 /* restore curwin/curbuf and a few other things */
273 aucmd_restbuf(&aco);
274 }
275#endif
276 }
277
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 return retval;
279}
280
281/*
282 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
283 */
284 int
285buf_valid(buf)
286 buf_T *buf;
287{
288 buf_T *bp;
289
290 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
291 if (bp == buf)
292 return TRUE;
293 return FALSE;
294}
295
296/*
297 * Close the link to a buffer.
298 * "action" is used when there is no longer a window for the buffer.
299 * It can be:
300 * 0 buffer becomes hidden
301 * DOBUF_UNLOAD buffer is unloaded
302 * DOBUF_DELETE buffer is unloaded and removed from buffer list
303 * DOBUF_WIPE buffer is unloaded and really deleted
304 * When doing all but the first one on the current buffer, the caller should
305 * get a new buffer very soon!
306 *
307 * The 'bufhidden' option can force freeing and deleting.
308 */
309 void
310close_buffer(win, buf, action)
311 win_T *win; /* if not NULL, set b_last_cursor */
312 buf_T *buf;
313 int action;
314{
315#ifdef FEAT_AUTOCMD
316 int is_curbuf;
317 int nwindows = buf->b_nwindows;
318#endif
319 int unload_buf = (action != 0);
320 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
321 int wipe_buf = (action == DOBUF_WIPE);
322
323#ifdef FEAT_QUICKFIX
324 /*
325 * Force unloading or deleting when 'bufhidden' says so.
326 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
327 * "hide" (otherwise we could never free or delete a buffer).
328 */
329 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
330 {
331 del_buf = TRUE;
332 unload_buf = TRUE;
333 }
334 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
335 {
336 del_buf = TRUE;
337 unload_buf = TRUE;
338 wipe_buf = TRUE;
339 }
340 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
341 unload_buf = TRUE;
342#endif
343
344 if (win != NULL)
345 {
346 /* Set b_last_cursor when closing the last window for the buffer.
347 * Remember the last cursor position and window options of the buffer.
348 * This used to be only for the current window, but then options like
349 * 'foldmethod' may be lost with a ":only" command. */
350 if (buf->b_nwindows == 1)
351 set_last_cursor(win);
352 buflist_setfpos(buf, win,
353 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
354 win->w_cursor.col, TRUE);
355 }
356
357#ifdef FEAT_AUTOCMD
358 /* When the buffer is no longer in a window, trigger BufWinLeave */
359 if (buf->b_nwindows == 1)
360 {
361 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
362 FALSE, buf);
363 if (!buf_valid(buf)) /* autocommands may delete the buffer */
364 return;
365
366 /* When the buffer becomes hidden, but is not unloaded, trigger
367 * BufHidden */
368 if (!unload_buf)
369 {
370 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
371 FALSE, buf);
372 if (!buf_valid(buf)) /* autocmds may delete the buffer */
373 return;
374 }
375# ifdef FEAT_EVAL
376 if (aborting()) /* autocmds may abort script processing */
377 return;
378# endif
379 }
380 nwindows = buf->b_nwindows;
381#endif
382
383 /* decrease the link count from windows (unless not in any window) */
384 if (buf->b_nwindows > 0)
385 --buf->b_nwindows;
386
387 /* Return when a window is displaying the buffer or when it's not
388 * unloaded. */
389 if (buf->b_nwindows > 0 || !unload_buf)
390 {
Bram Moolenaar607a95ed2006-03-28 20:57:42 +0000391#if 0 /* why was this here? */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 if (buf == curbuf)
393 u_sync(); /* sync undo before going to another buffer */
Bram Moolenaar607a95ed2006-03-28 20:57:42 +0000394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 return;
396 }
397
398 /* Always remove the buffer when there is no file name. */
399 if (buf->b_ffname == NULL)
400 del_buf = TRUE;
401
402 /*
403 * Free all things allocated for this buffer.
404 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
405 */
406#ifdef FEAT_AUTOCMD
407 /* Remember if we are closing the current buffer. Restore the number of
408 * windows, so that autocommands in buf_freeall() don't get confused. */
409 is_curbuf = (buf == curbuf);
410 buf->b_nwindows = nwindows;
411#endif
412
413 buf_freeall(buf, del_buf, wipe_buf);
414
415#ifdef FEAT_AUTOCMD
416 /* Autocommands may have deleted the buffer. */
417 if (!buf_valid(buf))
418 return;
419# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000420 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 return;
422# endif
423
424 /* Autocommands may have opened or closed windows for this buffer.
425 * Decrement the count for the close we do here. */
426 if (buf->b_nwindows > 0)
427 --buf->b_nwindows;
428
429 /*
430 * It's possible that autocommands change curbuf to the one being deleted.
431 * This might cause the previous curbuf to be deleted unexpectedly. But
432 * in some cases it's OK to delete the curbuf, because a new one is
433 * obtained anyway. Therefore only return if curbuf changed to the
434 * deleted buffer.
435 */
436 if (buf == curbuf && !is_curbuf)
437 return;
438#endif
439
440#ifdef FEAT_NETBEANS_INTG
441 if (usingNetbeans)
442 netbeans_file_closed(buf);
443#endif
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000444 /* Change directories when the 'acd' option is set. */
445 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
447 /*
448 * Remove the buffer from the list.
449 */
450 if (wipe_buf)
451 {
452#ifdef FEAT_SUN_WORKSHOP
453 if (usingSunWorkShop)
454 workshop_file_closed_lineno((char *)buf->b_ffname,
455 (int)buf->b_last_cursor.lnum);
456#endif
457 vim_free(buf->b_ffname);
458 vim_free(buf->b_sfname);
459 if (buf->b_prev == NULL)
460 firstbuf = buf->b_next;
461 else
462 buf->b_prev->b_next = buf->b_next;
463 if (buf->b_next == NULL)
464 lastbuf = buf->b_prev;
465 else
466 buf->b_next->b_prev = buf->b_prev;
467 free_buffer(buf);
468 }
469 else
470 {
471 if (del_buf)
472 {
473 /* Free all internal variables and reset option values, to make
474 * ":bdel" compatible with Vim 5.7. */
475 free_buffer_stuff(buf, TRUE);
476
477 /* Make it look like a new buffer. */
478 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
479
480 /* Init the options when loaded again. */
481 buf->b_p_initialized = FALSE;
482 }
483 buf_clear_file(buf);
484 if (del_buf)
485 buf->b_p_bl = FALSE;
486 }
487}
488
489/*
490 * Make buffer not contain a file.
491 */
492 void
493buf_clear_file(buf)
494 buf_T *buf;
495{
496 buf->b_ml.ml_line_count = 1;
497 unchanged(buf, TRUE);
498#ifndef SHORT_FNAME
499 buf->b_shortname = FALSE;
500#endif
501 buf->b_p_eol = TRUE;
502 buf->b_start_eol = TRUE;
503#ifdef FEAT_MBYTE
504 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000505 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506#endif
507 buf->b_ml.ml_mfp = NULL;
508 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
509#ifdef FEAT_NETBEANS_INTG
510 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511#endif
512}
513
514/*
515 * buf_freeall() - free all things allocated for a buffer that are related to
516 * the file.
517 */
518/*ARGSUSED*/
519 void
520buf_freeall(buf, del_buf, wipe_buf)
521 buf_T *buf;
522 int del_buf; /* buffer is going to be deleted */
523 int wipe_buf; /* buffer is going to be wiped out */
524{
525#ifdef FEAT_AUTOCMD
526 int is_curbuf = (buf == curbuf);
527
528 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
529 if (!buf_valid(buf)) /* autocommands may delete the buffer */
530 return;
531 if (del_buf && buf->b_p_bl)
532 {
533 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
534 if (!buf_valid(buf)) /* autocommands may delete the buffer */
535 return;
536 }
537 if (wipe_buf)
538 {
539 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
540 FALSE, buf);
541 if (!buf_valid(buf)) /* autocommands may delete the buffer */
542 return;
543 }
544# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000545 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 return;
547# endif
548
549 /*
550 * It's possible that autocommands change curbuf to the one being deleted.
551 * This might cause curbuf to be deleted unexpectedly. But in some cases
552 * it's OK to delete the curbuf, because a new one is obtained anyway.
553 * Therefore only return if curbuf changed to the deleted buffer.
554 */
555 if (buf == curbuf && !is_curbuf)
556 return;
557#endif
558#ifdef FEAT_DIFF
559 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
560#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000561
562#ifdef FEAT_FOLDING
563 /* No folds in an empty buffer. */
564# ifdef FEAT_WINDOWS
565 {
566 win_T *win;
567 tabpage_T *tp;
568
569 FOR_ALL_TAB_WINDOWS(tp, win)
570 if (win->w_buffer == buf)
571 clearFolding(win);
572 }
573# else
574 if (curwin->w_buffer == buf)
575 clearFolding(curwin);
576# endif
577#endif
578
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579#ifdef FEAT_TCL
580 tcl_buffer_free(buf);
581#endif
582 u_blockfree(buf); /* free the memory allocated for undo */
583 ml_close(buf, TRUE); /* close and delete the memline/memfile */
584 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
585 u_clearall(buf); /* reset all undo information */
586#ifdef FEAT_SYN_HL
587 syntax_clear(buf); /* reset syntax info */
588#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000589 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590}
591
592/*
593 * Free a buffer structure and the things it contains related to the buffer
594 * itself (not the file, that must have been done already).
595 */
596 static void
597free_buffer(buf)
598 buf_T *buf;
599{
600 free_buffer_stuff(buf, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000601#ifdef FEAT_MZSCHEME
602 mzscheme_buffer_free(buf);
603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604#ifdef FEAT_PERL
605 perl_buf_free(buf);
606#endif
607#ifdef FEAT_PYTHON
608 python_buffer_free(buf);
609#endif
610#ifdef FEAT_RUBY
611 ruby_buffer_free(buf);
612#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000613#ifdef FEAT_AUTOCMD
614 aubuflocal_remove(buf);
615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 vim_free(buf);
617}
618
619/*
620 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
621 */
622 static void
623free_buffer_stuff(buf, free_options)
624 buf_T *buf;
625 int free_options; /* free options as well */
626{
627 if (free_options)
628 {
629 clear_wininfo(buf); /* including window-local options */
630 free_buf_options(buf, TRUE);
631 }
632#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000633 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
634 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635#endif
636#ifdef FEAT_USR_CMDS
637 uc_clear(&buf->b_ucmds); /* clear local user commands */
638#endif
639#ifdef FEAT_SIGNS
640 buf_delete_signs(buf); /* delete any signs */
641#endif
642#ifdef FEAT_LOCALMAP
643 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
644 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
645#endif
646#ifdef FEAT_MBYTE
647 vim_free(buf->b_start_fenc);
648 buf->b_start_fenc = NULL;
649#endif
Bram Moolenaar9381ab72008-11-12 11:52:19 +0000650#ifdef FEAT_SPELL
651 ga_clear(&buf->b_langp);
652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653}
654
655/*
656 * Free the b_wininfo list for buffer "buf".
657 */
658 static void
659clear_wininfo(buf)
660 buf_T *buf;
661{
662 wininfo_T *wip;
663
664 while (buf->b_wininfo != NULL)
665 {
666 wip = buf->b_wininfo;
667 buf->b_wininfo = wip->wi_next;
668 if (wip->wi_optset)
669 {
670 clear_winopt(&wip->wi_opt);
671#ifdef FEAT_FOLDING
672 deleteFoldRecurse(&wip->wi_folds);
673#endif
674 }
675 vim_free(wip);
676 }
677}
678
679#if defined(FEAT_LISTCMDS) || defined(PROTO)
680/*
681 * Go to another buffer. Handles the result of the ATTENTION dialog.
682 */
683 void
684goto_buffer(eap, start, dir, count)
685 exarg_T *eap;
686 int start;
687 int dir;
688 int count;
689{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000690# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 buf_T *old_curbuf = curbuf;
692
693 swap_exists_action = SEA_DIALOG;
694# endif
695 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
696 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000697# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
699 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000700# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
701 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000702
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000703 /* Reset the error/interrupt/exception state here so that
704 * aborting() returns FALSE when closing a window. */
705 enter_cleanup(&cs);
706# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000707
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000708 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 win_close(curwin, TRUE);
710 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000711 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000712
713# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
714 /* Restore the error/interrupt/exception state if not discarded by a
715 * new aborting error, interrupt, or uncaught exception. */
716 leave_cleanup(&cs);
717# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719 else
720 handle_swap_exists(old_curbuf);
721# endif
722}
723#endif
724
Bram Moolenaare64ac772005-12-07 20:54:59 +0000725#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726/*
727 * Handle the situation of swap_exists_action being set.
728 * It is allowed for "old_curbuf" to be NULL or invalid.
729 */
730 void
731handle_swap_exists(old_curbuf)
732 buf_T *old_curbuf;
733{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000734# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
735 cleanup_T cs;
736# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 if (swap_exists_action == SEA_QUIT)
739 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000740# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
741 /* Reset the error/interrupt/exception state here so that
742 * aborting() returns FALSE when closing a buffer. */
743 enter_cleanup(&cs);
744# endif
745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 /* User selected Quit at ATTENTION prompt. Go back to previous
747 * buffer. If that buffer is gone or the same as the current one,
748 * open a new, empty buffer. */
749 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000750 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
752 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000753 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 if (old_curbuf != NULL)
755 enter_buffer(old_curbuf);
756 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000757
758# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
759 /* Restore the error/interrupt/exception state if not discarded by a
760 * new aborting error, interrupt, or uncaught exception. */
761 leave_cleanup(&cs);
762# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 }
764 else if (swap_exists_action == SEA_RECOVER)
765 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000766# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
767 /* Reset the error/interrupt/exception state here so that
768 * aborting() returns FALSE when closing a buffer. */
769 enter_cleanup(&cs);
770# endif
771
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 /* User selected Recover at ATTENTION prompt. */
773 msg_scroll = TRUE;
774 ml_recover();
775 MSG_PUTS("\n"); /* don't overwrite the last message */
776 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000777 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000778
779# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
780 /* Restore the error/interrupt/exception state if not discarded by a
781 * new aborting error, interrupt, or uncaught exception. */
782 leave_cleanup(&cs);
783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785 swap_exists_action = SEA_NONE;
786}
787#endif
788
789#if defined(FEAT_LISTCMDS) || defined(PROTO)
790/*
791 * do_bufdel() - delete or unload buffer(s)
792 *
793 * addr_count == 0: ":bdel" - delete current buffer
794 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
795 * buffer "end_bnr", then any other arguments.
796 * addr_count == 2: ":N,N bdel" - delete buffers in range
797 *
798 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
799 * DOBUF_DEL (":bdel")
800 *
801 * Returns error message or NULL
802 */
803 char_u *
804do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
805 int command;
806 char_u *arg; /* pointer to extra arguments */
807 int addr_count;
808 int start_bnr; /* first buffer number in a range */
809 int end_bnr; /* buffer nr or last buffer nr in a range */
810 int forceit;
811{
812 int do_current = 0; /* delete current buffer? */
813 int deleted = 0; /* number of buffers deleted */
814 char_u *errormsg = NULL; /* return value */
815 int bnr; /* buffer number */
816 char_u *p;
817
818#ifdef FEAT_NETBEANS_INTG
819 netbeansCloseFile = 1;
820#endif
821 if (addr_count == 0)
822 {
823 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
824 }
825 else
826 {
827 if (addr_count == 2)
828 {
829 if (*arg) /* both range and argument is not allowed */
830 return (char_u *)_(e_trailing);
831 bnr = start_bnr;
832 }
833 else /* addr_count == 1 */
834 bnr = end_bnr;
835
836 for ( ;!got_int; ui_breakcheck())
837 {
838 /*
839 * delete the current buffer last, otherwise when the
840 * current buffer is deleted, the next buffer becomes
841 * the current one and will be loaded, which may then
842 * also be deleted, etc.
843 */
844 if (bnr == curbuf->b_fnum)
845 do_current = bnr;
846 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
847 forceit) == OK)
848 ++deleted;
849
850 /*
851 * find next buffer number to delete/unload
852 */
853 if (addr_count == 2)
854 {
855 if (++bnr > end_bnr)
856 break;
857 }
858 else /* addr_count == 1 */
859 {
860 arg = skipwhite(arg);
861 if (*arg == NUL)
862 break;
863 if (!VIM_ISDIGIT(*arg))
864 {
865 p = skiptowhite_esc(arg);
866 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
867 if (bnr < 0) /* failed */
868 break;
869 arg = p;
870 }
871 else
872 bnr = getdigits(&arg);
873 }
874 }
875 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
876 FORWARD, do_current, forceit) == OK)
877 ++deleted;
878
879 if (deleted == 0)
880 {
881 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000882 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000884 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000886 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 errormsg = IObuff;
888 }
889 else if (deleted >= p_report)
890 {
891 if (command == DOBUF_UNLOAD)
892 {
893 if (deleted == 1)
894 MSG(_("1 buffer unloaded"));
895 else
896 smsg((char_u *)_("%d buffers unloaded"), deleted);
897 }
898 else if (command == DOBUF_DEL)
899 {
900 if (deleted == 1)
901 MSG(_("1 buffer deleted"));
902 else
903 smsg((char_u *)_("%d buffers deleted"), deleted);
904 }
905 else
906 {
907 if (deleted == 1)
908 MSG(_("1 buffer wiped out"));
909 else
910 smsg((char_u *)_("%d buffers wiped out"), deleted);
911 }
912 }
913 }
914
915#ifdef FEAT_NETBEANS_INTG
916 netbeansCloseFile = 0;
917#endif
918
919 return errormsg;
920}
921
922/*
923 * Implementation of the commands for the buffer list.
924 *
925 * action == DOBUF_GOTO go to specified buffer
926 * action == DOBUF_SPLIT split window and go to specified buffer
927 * action == DOBUF_UNLOAD unload specified buffer(s)
928 * action == DOBUF_DEL delete specified buffer(s) from buffer list
929 * action == DOBUF_WIPE delete specified buffer(s) really
930 *
931 * start == DOBUF_CURRENT go to "count" buffer from current buffer
932 * start == DOBUF_FIRST go to "count" buffer from first buffer
933 * start == DOBUF_LAST go to "count" buffer from last buffer
934 * start == DOBUF_MOD go to "count" modified buffer from current buffer
935 *
936 * Return FAIL or OK.
937 */
938 int
939do_buffer(action, start, dir, count, forceit)
940 int action;
941 int start;
942 int dir; /* FORWARD or BACKWARD */
943 int count; /* buffer number or number of buffers */
944 int forceit; /* TRUE for :...! */
945{
946 buf_T *buf;
947 buf_T *bp;
948 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
949 || action == DOBUF_WIPE);
950
951 switch (start)
952 {
953 case DOBUF_FIRST: buf = firstbuf; break;
954 case DOBUF_LAST: buf = lastbuf; break;
955 default: buf = curbuf; break;
956 }
957 if (start == DOBUF_MOD) /* find next modified buffer */
958 {
959 while (count-- > 0)
960 {
961 do
962 {
963 buf = buf->b_next;
964 if (buf == NULL)
965 buf = firstbuf;
966 }
967 while (buf != curbuf && !bufIsChanged(buf));
968 }
969 if (!bufIsChanged(buf))
970 {
971 EMSG(_("E84: No modified buffer found"));
972 return FAIL;
973 }
974 }
975 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
976 {
977 while (buf != NULL && buf->b_fnum != count)
978 buf = buf->b_next;
979 }
980 else
981 {
982 bp = NULL;
983 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
984 {
985 /* remember the buffer where we start, we come back there when all
986 * buffers are unlisted. */
987 if (bp == NULL)
988 bp = buf;
989 if (dir == FORWARD)
990 {
991 buf = buf->b_next;
992 if (buf == NULL)
993 buf = firstbuf;
994 }
995 else
996 {
997 buf = buf->b_prev;
998 if (buf == NULL)
999 buf = lastbuf;
1000 }
1001 /* don't count unlisted buffers */
1002 if (unload || buf->b_p_bl)
1003 {
1004 --count;
1005 bp = NULL; /* use this buffer as new starting point */
1006 }
1007 if (bp == buf)
1008 {
1009 /* back where we started, didn't find anything. */
1010 EMSG(_("E85: There is no listed buffer"));
1011 return FAIL;
1012 }
1013 }
1014 }
1015
1016 if (buf == NULL) /* could not find it */
1017 {
1018 if (start == DOBUF_FIRST)
1019 {
1020 /* don't warn when deleting */
1021 if (!unload)
1022 EMSGN(_("E86: Buffer %ld does not exist"), count);
1023 }
1024 else if (dir == FORWARD)
1025 EMSG(_("E87: Cannot go beyond last buffer"));
1026 else
1027 EMSG(_("E88: Cannot go before first buffer"));
1028 return FAIL;
1029 }
1030
1031#ifdef FEAT_GUI
1032 need_mouse_correct = TRUE;
1033#endif
1034
1035#ifdef FEAT_LISTCMDS
1036 /*
1037 * delete buffer buf from memory and/or the list
1038 */
1039 if (unload)
1040 {
1041 int forward;
1042 int retval;
1043
1044 /* When unloading or deleting a buffer that's already unloaded and
1045 * unlisted: fail silently. */
1046 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1047 return FAIL;
1048
1049 if (!forceit && bufIsChanged(buf))
1050 {
1051#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1052 if ((p_confirm || cmdmod.confirm) && p_write)
1053 {
1054 dialog_changed(buf, FALSE);
1055# ifdef FEAT_AUTOCMD
1056 if (!buf_valid(buf))
1057 /* Autocommand deleted buffer, oops! It's not changed
1058 * now. */
1059 return FAIL;
1060# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001061 /* If it's still changed fail silently, the dialog already
1062 * mentioned why it fails. */
1063 if (bufIsChanged(buf))
1064 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001066 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067#endif
1068 {
1069 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1070 buf->b_fnum);
1071 return FAIL;
1072 }
1073 }
1074
1075 /*
1076 * If deleting the last (listed) buffer, make it empty.
1077 * The last (listed) buffer cannot be unloaded.
1078 */
1079 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1080 if (bp->b_p_bl && bp != buf)
1081 break;
1082 if (bp == NULL && buf == curbuf)
1083 {
1084 if (action == DOBUF_UNLOAD)
1085 {
1086 EMSG(_("E90: Cannot unload last buffer"));
1087 return FAIL;
1088 }
1089
1090 /* Close any other windows on this buffer, then make it empty. */
1091#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001092 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093#endif
1094 setpcmark();
1095 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001096 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097
1098 /*
1099 * do_ecmd() may create a new buffer, then we have to delete
1100 * the old one. But do_ecmd() may have done that already, check
1101 * if the buffer still exists.
1102 */
1103 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1104 close_buffer(NULL, buf, action);
1105 return retval;
1106 }
1107
1108#ifdef FEAT_WINDOWS
1109 /*
1110 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001111 * (unless it's the only window). Repeat this so long as we end up in
1112 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001114 while (buf == curbuf
1115 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 win_close(curwin, FALSE);
1117#endif
1118
1119 /*
1120 * If the buffer to be deleted is not the current one, delete it here.
1121 */
1122 if (buf != curbuf)
1123 {
1124#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001125 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126#endif
1127 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1128 close_buffer(NULL, buf, action);
1129 return OK;
1130 }
1131
1132 /*
1133 * Deleting the current buffer: Need to find another buffer to go to.
1134 * There must be another, otherwise it would have been handled above.
1135 * First use au_new_curbuf, if it is valid.
1136 * Then prefer the buffer we most recently visited.
1137 * Else try to find one that is loaded, after the current buffer,
1138 * then before the current buffer.
1139 * Finally use any buffer.
1140 */
1141 buf = NULL; /* selected buffer */
1142 bp = NULL; /* used when no loaded buffer found */
1143#ifdef FEAT_AUTOCMD
1144 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1145 buf = au_new_curbuf;
1146# ifdef FEAT_JUMPLIST
1147 else
1148# endif
1149#endif
1150#ifdef FEAT_JUMPLIST
1151 if (curwin->w_jumplistlen > 0)
1152 {
1153 int jumpidx;
1154
1155 jumpidx = curwin->w_jumplistidx - 1;
1156 if (jumpidx < 0)
1157 jumpidx = curwin->w_jumplistlen - 1;
1158
1159 forward = jumpidx;
1160 while (jumpidx != curwin->w_jumplistidx)
1161 {
1162 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1163 if (buf != NULL)
1164 {
1165 if (buf == curbuf || !buf->b_p_bl)
1166 buf = NULL; /* skip current and unlisted bufs */
1167 else if (buf->b_ml.ml_mfp == NULL)
1168 {
1169 /* skip unloaded buf, but may keep it for later */
1170 if (bp == NULL)
1171 bp = buf;
1172 buf = NULL;
1173 }
1174 }
1175 if (buf != NULL) /* found a valid buffer: stop searching */
1176 break;
1177 /* advance to older entry in jump list */
1178 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1179 break;
1180 if (--jumpidx < 0)
1181 jumpidx = curwin->w_jumplistlen - 1;
1182 if (jumpidx == forward) /* List exhausted for sure */
1183 break;
1184 }
1185 }
1186#endif
1187
1188 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1189 {
1190 forward = TRUE;
1191 buf = curbuf->b_next;
1192 for (;;)
1193 {
1194 if (buf == NULL)
1195 {
1196 if (!forward) /* tried both directions */
1197 break;
1198 buf = curbuf->b_prev;
1199 forward = FALSE;
1200 continue;
1201 }
1202 /* in non-help buffer, try to skip help buffers, and vv */
1203 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1204 {
1205 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1206 break;
1207 if (bp == NULL) /* remember unloaded buf for later */
1208 bp = buf;
1209 }
1210 if (forward)
1211 buf = buf->b_next;
1212 else
1213 buf = buf->b_prev;
1214 }
1215 }
1216 if (buf == NULL) /* No loaded buffer, use unloaded one */
1217 buf = bp;
1218 if (buf == NULL) /* No loaded buffer, find listed one */
1219 {
1220 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1221 if (buf->b_p_bl && buf != curbuf)
1222 break;
1223 }
1224 if (buf == NULL) /* Still no buffer, just take one */
1225 {
1226 if (curbuf->b_next != NULL)
1227 buf = curbuf->b_next;
1228 else
1229 buf = curbuf->b_prev;
1230 }
1231 }
1232
1233 /*
1234 * make buf current buffer
1235 */
1236 if (action == DOBUF_SPLIT) /* split window first */
1237 {
1238# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001239 /* If 'switchbuf' contains "useopen": jump to first window containing
1240 * "buf" if one exists */
1241 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001242 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001243 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001244 * page containing "buf" if one exists */
1245 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 return OK;
1247 if (win_split(0, 0) == FAIL)
1248# endif
1249 return FAIL;
1250 }
1251#endif
1252
1253 /* go to current buffer - nothing to do */
1254 if (buf == curbuf)
1255 return OK;
1256
1257 /*
1258 * Check if the current buffer may be abandoned.
1259 */
1260 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1261 {
1262#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1263 if ((p_confirm || cmdmod.confirm) && p_write)
1264 {
1265 dialog_changed(curbuf, FALSE);
1266# ifdef FEAT_AUTOCMD
1267 if (!buf_valid(buf))
1268 /* Autocommand deleted buffer, oops! */
1269 return FAIL;
1270# endif
1271 }
1272 if (bufIsChanged(curbuf))
1273#endif
1274 {
1275 EMSG(_(e_nowrtmsg));
1276 return FAIL;
1277 }
1278 }
1279
1280 /* Go to the other buffer. */
1281 set_curbuf(buf, action);
1282
1283#if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1284 if (action == DOBUF_SPLIT)
1285 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1286#endif
1287
1288#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1289 if (aborting()) /* autocmds may abort script processing */
1290 return FAIL;
1291#endif
1292
1293 return OK;
1294}
1295
1296#endif /* FEAT_LISTCMDS */
1297
1298/*
1299 * Set current buffer to "buf". Executes autocommands and closes current
1300 * buffer. "action" tells how to close the current buffer:
1301 * DOBUF_GOTO free or hide it
1302 * DOBUF_SPLIT nothing
1303 * DOBUF_UNLOAD unload it
1304 * DOBUF_DEL delete it
1305 * DOBUF_WIPE wipe it out
1306 */
1307 void
1308set_curbuf(buf, action)
1309 buf_T *buf;
1310 int action;
1311{
1312 buf_T *prevbuf;
1313 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1314 || action == DOBUF_WIPE);
1315
1316 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001317 if (!cmdmod.keepalt)
1318 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001319 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320
1321#ifdef FEAT_VISUAL
1322 /* Don't restart Select mode after switching to another buffer. */
1323 VIsual_reselect = FALSE;
1324#endif
1325
1326 /* close_windows() or apply_autocmds() may change curbuf */
1327 prevbuf = curbuf;
1328
1329#ifdef FEAT_AUTOCMD
1330 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1331# ifdef FEAT_EVAL
1332 if (buf_valid(prevbuf) && !aborting())
1333# else
1334 if (buf_valid(prevbuf))
1335# endif
1336#endif
1337 {
1338#ifdef FEAT_WINDOWS
1339 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001340 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341#endif
1342#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1343 if (buf_valid(prevbuf) && !aborting())
1344#else
1345 if (buf_valid(prevbuf))
1346#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001347 {
1348 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001349 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1351 unload ? action : (action == DOBUF_GOTO
1352 && !P_HID(prevbuf)
1353 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001357 /* An autocommand may have deleted "buf", already entered it (e.g., when
1358 * it did ":bunload") or aborted the script processing! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359# ifdef FEAT_EVAL
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001360 if (buf_valid(buf) && buf != curbuf && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361# else
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001362 if (buf_valid(buf) && buf != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363# endif
1364#endif
1365 enter_buffer(buf);
1366}
1367
1368/*
1369 * Enter a new current buffer.
1370 * Old curbuf must have been abandoned already!
1371 */
1372 void
1373enter_buffer(buf)
1374 buf_T *buf;
1375{
1376 /* Copy buffer and window local option values. Not for a help buffer. */
1377 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1378 if (!buf->b_help)
1379 get_winopts(buf);
1380#ifdef FEAT_FOLDING
1381 else
1382 /* Remove all folds in the window. */
1383 clearFolding(curwin);
1384 foldUpdateAll(curwin); /* update folds (later). */
1385#endif
1386
1387 /* Get the buffer in the current window. */
1388 curwin->w_buffer = buf;
1389 curbuf = buf;
1390 ++curbuf->b_nwindows;
1391
1392#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001393 if (curwin->w_p_diff)
1394 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395#endif
1396
1397 /* Cursor on first line by default. */
1398 curwin->w_cursor.lnum = 1;
1399 curwin->w_cursor.col = 0;
1400#ifdef FEAT_VIRTUALEDIT
1401 curwin->w_cursor.coladd = 0;
1402#endif
1403 curwin->w_set_curswant = TRUE;
1404
1405 /* Make sure the buffer is loaded. */
1406 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001407 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001408#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001409 /* If there is no filetype, allow for detecting one. Esp. useful for
1410 * ":ball" used in a autocommand. If there already is a filetype we
1411 * might prefer to keep it. */
1412 if (*curbuf->b_p_ft == NUL)
1413 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001414#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 open_buffer(FALSE, NULL);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 else
1419 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001420 if (!msg_silent)
1421 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1423#ifdef FEAT_AUTOCMD
1424 curwin->w_topline = 1;
1425# ifdef FEAT_DIFF
1426 curwin->w_topfill = 0;
1427# endif
1428 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1429 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1430#endif
1431 }
1432
1433 /* If autocommands did not change the cursor position, restore cursor lnum
1434 * and possibly cursor col. */
1435 if (curwin->w_cursor.lnum == 1 && inindent(0))
1436 buflist_getfpos();
1437
1438 check_arg_idx(curwin); /* check for valid arg_idx */
1439#ifdef FEAT_TITLE
1440 maketitle();
1441#endif
1442#ifdef FEAT_AUTOCMD
1443 if (curwin->w_topline == 1) /* when autocmds didn't change it */
1444#endif
1445 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1446
Bram Moolenaar009b2592004-10-24 19:18:58 +00001447#ifdef FEAT_NETBEANS_INTG
1448 /* Send fileOpened event because we've changed buffers. */
1449 if (usingNetbeans && isNetbeansBuffer(curbuf))
1450 netbeans_file_activated(curbuf);
1451#endif
1452
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001453 /* Change directories when the 'acd' option is set. */
1454 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
1456#ifdef FEAT_KEYMAP
1457 if (curbuf->b_kmap_state & KEYMAP_INIT)
1458 keymap_init();
1459#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001460#ifdef FEAT_SPELL
1461 /* May need to set the spell language. Can only do this after the buffer
1462 * has been properly setup. */
1463 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
1464 did_set_spelllang(curbuf);
1465#endif
1466
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 redraw_later(NOT_VALID);
1468}
1469
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001470#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1471/*
1472 * Change to the directory of the current buffer.
1473 */
1474 void
1475do_autochdir()
1476{
1477 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1478 shorten_fnames(TRUE);
1479}
1480#endif
1481
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482/*
1483 * functions for dealing with the buffer list
1484 */
1485
1486/*
1487 * Add a file name to the buffer list. Return a pointer to the buffer.
1488 * If the same file name already exists return a pointer to that buffer.
1489 * If it does not exist, or if fname == NULL, a new entry is created.
1490 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1491 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1492 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 * This is the ONLY way to create a new buffer.
1494 */
1495static int top_file_num = 1; /* highest file number */
1496
1497 buf_T *
1498buflist_new(ffname, sfname, lnum, flags)
1499 char_u *ffname; /* full path of fname or relative */
1500 char_u *sfname; /* short fname or NULL */
1501 linenr_T lnum; /* preferred cursor line */
1502 int flags; /* BLN_ defines */
1503{
1504 buf_T *buf;
1505#ifdef UNIX
1506 struct stat st;
1507#endif
1508
1509 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1510
1511 /*
1512 * If file name already exists in the list, update the entry.
1513 */
1514#ifdef UNIX
1515 /* On Unix we can use inode numbers when the file exists. Works better
1516 * for hard links. */
1517 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1518 st.st_dev = (dev_T)-1;
1519#endif
1520 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1521#ifdef UNIX
1522 buflist_findname_stat(ffname, &st)
1523#else
1524 buflist_findname(ffname)
1525#endif
1526 ) != NULL)
1527 {
1528 vim_free(ffname);
1529 if (lnum != 0)
1530 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1531 /* copy the options now, if 'cpo' doesn't have 's' and not done
1532 * already */
1533 buf_copy_options(buf, 0);
1534 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1535 {
1536 buf->b_p_bl = TRUE;
1537#ifdef FEAT_AUTOCMD
1538 if (!(flags & BLN_DUMMY))
1539 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1540#endif
1541 }
1542 return buf;
1543 }
1544
1545 /*
1546 * If the current buffer has no name and no contents, use the current
1547 * buffer. Otherwise: Need to allocate a new buffer structure.
1548 *
1549 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001550 * (A spell file buffer is allocated in spell.c, but that's not a normal
1551 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 */
1553 buf = NULL;
1554 if ((flags & BLN_CURBUF)
1555 && curbuf != NULL
1556 && curbuf->b_ffname == NULL
1557 && curbuf->b_nwindows <= 1
1558 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1559 {
1560 buf = curbuf;
1561#ifdef FEAT_AUTOCMD
1562 /* It's like this buffer is deleted. Watch out for autocommands that
1563 * change curbuf! If that happens, allocate a new buffer anyway. */
1564 if (curbuf->b_p_bl)
1565 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1566 if (buf == curbuf)
1567 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1568# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001569 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 return NULL;
1571# endif
1572#endif
1573#ifdef FEAT_QUICKFIX
1574# ifdef FEAT_AUTOCMD
1575 if (buf == curbuf)
1576# endif
1577 {
1578 /* Make sure 'bufhidden' and 'buftype' are empty */
1579 clear_string_option(&buf->b_p_bh);
1580 clear_string_option(&buf->b_p_bt);
1581 }
1582#endif
1583 }
1584 if (buf != curbuf || curbuf == NULL)
1585 {
1586 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1587 if (buf == NULL)
1588 {
1589 vim_free(ffname);
1590 return NULL;
1591 }
1592 }
1593
1594 if (ffname != NULL)
1595 {
1596 buf->b_ffname = ffname;
1597 buf->b_sfname = vim_strsave(sfname);
1598 }
1599
1600 clear_wininfo(buf);
1601 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1602
1603 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1604 || buf->b_wininfo == NULL)
1605 {
1606 vim_free(buf->b_ffname);
1607 buf->b_ffname = NULL;
1608 vim_free(buf->b_sfname);
1609 buf->b_sfname = NULL;
1610 if (buf != curbuf)
1611 free_buffer(buf);
1612 return NULL;
1613 }
1614
1615 if (buf == curbuf)
1616 {
1617 /* free all things allocated for this buffer */
1618 buf_freeall(buf, FALSE, FALSE);
1619 if (buf != curbuf) /* autocommands deleted the buffer! */
1620 return NULL;
1621#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001622 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 return NULL;
1624#endif
1625 /* buf->b_nwindows = 0; why was this here? */
1626 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1627#ifdef FEAT_KEYMAP
1628 /* need to reload lmaps and set b:keymap_name */
1629 curbuf->b_kmap_state |= KEYMAP_INIT;
1630#endif
1631 }
1632 else
1633 {
1634 /*
1635 * put new buffer at the end of the buffer list
1636 */
1637 buf->b_next = NULL;
1638 if (firstbuf == NULL) /* buffer list is empty */
1639 {
1640 buf->b_prev = NULL;
1641 firstbuf = buf;
1642 }
1643 else /* append new buffer at end of list */
1644 {
1645 lastbuf->b_next = buf;
1646 buf->b_prev = lastbuf;
1647 }
1648 lastbuf = buf;
1649
1650 buf->b_fnum = top_file_num++;
1651 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1652 {
1653 EMSG(_("W14: Warning: List of file names overflow"));
1654 if (emsg_silent == 0)
1655 {
1656 out_flush();
1657 ui_delay(3000L, TRUE); /* make sure it is noticed */
1658 }
1659 top_file_num = 1;
1660 }
1661
1662 /*
1663 * Always copy the options from the current buffer.
1664 */
1665 buf_copy_options(buf, BCO_ALWAYS);
1666 }
1667
1668 buf->b_wininfo->wi_fpos.lnum = lnum;
1669 buf->b_wininfo->wi_win = curwin;
1670
1671#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001672 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1673#endif
1674#ifdef FEAT_SYN_HL
1675 hash_init(&buf->b_keywtab);
1676 hash_init(&buf->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677#endif
1678
1679 buf->b_fname = buf->b_sfname;
1680#ifdef UNIX
1681 if (st.st_dev == (dev_T)-1)
1682 buf->b_dev = -1;
1683 else
1684 {
1685 buf->b_dev = st.st_dev;
1686 buf->b_ino = st.st_ino;
1687 }
1688#endif
1689 buf->b_u_synced = TRUE;
1690 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001691 if (flags & BLN_DUMMY)
1692 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 buf_clear_file(buf);
1694 clrallmarks(buf); /* clear marks */
1695 fmarks_check_names(buf); /* check file marks for this file */
1696 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1697#ifdef FEAT_AUTOCMD
1698 if (!(flags & BLN_DUMMY))
1699 {
1700 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1701 if (flags & BLN_LISTED)
1702 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1703# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001704 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 return NULL;
1706# endif
1707 }
1708#endif
1709
1710 return buf;
1711}
1712
1713/*
1714 * Free the memory for the options of a buffer.
1715 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1716 * 'fileencoding'.
1717 */
1718 void
1719free_buf_options(buf, free_p_ff)
1720 buf_T *buf;
1721 int free_p_ff;
1722{
1723 if (free_p_ff)
1724 {
1725#ifdef FEAT_MBYTE
1726 clear_string_option(&buf->b_p_fenc);
1727#endif
1728 clear_string_option(&buf->b_p_ff);
1729#ifdef FEAT_QUICKFIX
1730 clear_string_option(&buf->b_p_bh);
1731 clear_string_option(&buf->b_p_bt);
1732#endif
1733 }
1734#ifdef FEAT_FIND_ID
1735 clear_string_option(&buf->b_p_def);
1736 clear_string_option(&buf->b_p_inc);
1737# ifdef FEAT_EVAL
1738 clear_string_option(&buf->b_p_inex);
1739# endif
1740#endif
1741#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1742 clear_string_option(&buf->b_p_inde);
1743 clear_string_option(&buf->b_p_indk);
1744#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001745#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1746 clear_string_option(&buf->b_p_bexpr);
1747#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001748#if defined(FEAT_EVAL)
1749 clear_string_option(&buf->b_p_fex);
1750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751#ifdef FEAT_CRYPT
1752 clear_string_option(&buf->b_p_key);
1753#endif
1754 clear_string_option(&buf->b_p_kp);
1755 clear_string_option(&buf->b_p_mps);
1756 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001757 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 clear_string_option(&buf->b_p_isk);
1759#ifdef FEAT_KEYMAP
1760 clear_string_option(&buf->b_p_keymap);
1761 ga_clear(&buf->b_kmap_ga);
1762#endif
1763#ifdef FEAT_COMMENTS
1764 clear_string_option(&buf->b_p_com);
1765#endif
1766#ifdef FEAT_FOLDING
1767 clear_string_option(&buf->b_p_cms);
1768#endif
1769 clear_string_option(&buf->b_p_nf);
1770#ifdef FEAT_SYN_HL
1771 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001772#endif
1773#ifdef FEAT_SPELL
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001774 clear_string_option(&buf->b_p_spc);
Bram Moolenaar86bc1fb2005-06-07 20:58:01 +00001775 clear_string_option(&buf->b_p_spf);
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001776 vim_free(buf->b_cap_prog);
1777 buf->b_cap_prog = NULL;
Bram Moolenaara0dea672005-03-20 22:23:10 +00001778 clear_string_option(&buf->b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779#endif
1780#ifdef FEAT_SEARCHPATH
1781 clear_string_option(&buf->b_p_sua);
1782#endif
1783#ifdef FEAT_AUTOCMD
1784 clear_string_option(&buf->b_p_ft);
1785#endif
1786#ifdef FEAT_OSFILETYPE
1787 clear_string_option(&buf->b_p_oft);
1788#endif
1789#ifdef FEAT_CINDENT
1790 clear_string_option(&buf->b_p_cink);
1791 clear_string_option(&buf->b_p_cino);
1792#endif
1793#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1794 clear_string_option(&buf->b_p_cinw);
1795#endif
1796#ifdef FEAT_INS_EXPAND
1797 clear_string_option(&buf->b_p_cpt);
1798#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001799#ifdef FEAT_COMPL_FUNC
1800 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001801 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803#ifdef FEAT_QUICKFIX
1804 clear_string_option(&buf->b_p_gp);
1805 clear_string_option(&buf->b_p_mp);
1806 clear_string_option(&buf->b_p_efm);
1807#endif
1808 clear_string_option(&buf->b_p_ep);
1809 clear_string_option(&buf->b_p_path);
1810 clear_string_option(&buf->b_p_tags);
1811#ifdef FEAT_INS_EXPAND
1812 clear_string_option(&buf->b_p_dict);
1813 clear_string_option(&buf->b_p_tsr);
1814#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001815#ifdef FEAT_TEXTOBJ
1816 clear_string_option(&buf->b_p_qe);
1817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 buf->b_p_ar = -1;
1819}
1820
1821/*
1822 * get alternate file n
1823 * set linenr to lnum or altfpos.lnum if lnum == 0
1824 * also set cursor column to altfpos.col if 'startofline' is not set.
1825 * if (options & GETF_SETMARK) call setpcmark()
1826 * if (options & GETF_ALT) we are jumping to an alternate file.
1827 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1828 *
1829 * return FAIL for failure, OK for success
1830 */
1831 int
1832buflist_getfile(n, lnum, options, forceit)
1833 int n;
1834 linenr_T lnum;
1835 int options;
1836 int forceit;
1837{
1838 buf_T *buf;
1839#ifdef FEAT_WINDOWS
1840 win_T *wp = NULL;
1841#endif
1842 pos_T *fpos;
1843 colnr_T col;
1844
1845 buf = buflist_findnr(n);
1846 if (buf == NULL)
1847 {
1848 if ((options & GETF_ALT) && n == 0)
1849 EMSG(_(e_noalt));
1850 else
1851 EMSGN(_("E92: Buffer %ld not found"), n);
1852 return FAIL;
1853 }
1854
1855 /* if alternate file is the current buffer, nothing to do */
1856 if (buf == curbuf)
1857 return OK;
1858
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001859 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001860 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001861 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001863 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001864#ifdef FEAT_AUTOCMD
1865 if (curbuf_locked())
1866 return FAIL;
1867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
1869 /* altfpos may be changed by getfile(), get it now */
1870 if (lnum == 0)
1871 {
1872 fpos = buflist_findfpos(buf);
1873 lnum = fpos->lnum;
1874 col = fpos->col;
1875 }
1876 else
1877 col = 0;
1878
1879#ifdef FEAT_WINDOWS
1880 if (options & GETF_SWITCH)
1881 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001882 /* If 'switchbuf' contains "useopen": jump to first window containing
1883 * "buf" if one exists */
1884 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001886 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1887 * page containing "buf" if one exists */
1888 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001889 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001890 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1891 * isn't empty: open new window */
1892 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001894 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1895 tabpage_new();
1896 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 return FAIL;
1898# ifdef FEAT_SCROLLBIND
1899 curwin->w_p_scb = FALSE;
1900# endif
1901 }
1902 }
1903#endif
1904
1905 ++RedrawingDisabled;
1906 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1907 lnum, forceit) <= 0)
1908 {
1909 --RedrawingDisabled;
1910
1911 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1912 if (!p_sol && col != 0)
1913 {
1914 curwin->w_cursor.col = col;
1915 check_cursor_col();
1916#ifdef FEAT_VIRTUALEDIT
1917 curwin->w_cursor.coladd = 0;
1918#endif
1919 curwin->w_set_curswant = TRUE;
1920 }
1921 return OK;
1922 }
1923 --RedrawingDisabled;
1924 return FAIL;
1925}
1926
1927/*
1928 * go to the last know line number for the current buffer
1929 */
1930 void
1931buflist_getfpos()
1932{
1933 pos_T *fpos;
1934
1935 fpos = buflist_findfpos(curbuf);
1936
1937 curwin->w_cursor.lnum = fpos->lnum;
1938 check_cursor_lnum();
1939
1940 if (p_sol)
1941 curwin->w_cursor.col = 0;
1942 else
1943 {
1944 curwin->w_cursor.col = fpos->col;
1945 check_cursor_col();
1946#ifdef FEAT_VIRTUALEDIT
1947 curwin->w_cursor.coladd = 0;
1948#endif
1949 curwin->w_set_curswant = TRUE;
1950 }
1951}
1952
Bram Moolenaar81695252004-12-29 20:58:21 +00001953#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1954/*
1955 * Find file in buffer list by name (it has to be for the current window).
1956 * Returns NULL if not found.
1957 */
1958 buf_T *
1959buflist_findname_exp(fname)
1960 char_u *fname;
1961{
1962 char_u *ffname;
1963 buf_T *buf = NULL;
1964
1965 /* First make the name into a full path name */
1966 ffname = FullName_save(fname,
1967#ifdef UNIX
1968 TRUE /* force expansion, get rid of symbolic links */
1969#else
1970 FALSE
1971#endif
1972 );
1973 if (ffname != NULL)
1974 {
1975 buf = buflist_findname(ffname);
1976 vim_free(ffname);
1977 }
1978 return buf;
1979}
1980#endif
1981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982/*
1983 * Find file in buffer list by name (it has to be for the current window).
1984 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00001985 * Skips dummy buffers.
1986 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 */
1988 buf_T *
1989buflist_findname(ffname)
1990 char_u *ffname;
1991{
1992#ifdef UNIX
1993 struct stat st;
1994
1995 if (mch_stat((char *)ffname, &st) < 0)
1996 st.st_dev = (dev_T)-1;
1997 return buflist_findname_stat(ffname, &st);
1998}
1999
2000/*
2001 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2002 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002003 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 */
2005 static buf_T *
2006buflist_findname_stat(ffname, stp)
2007 char_u *ffname;
2008 struct stat *stp;
2009{
2010#endif
2011 buf_T *buf;
2012
2013 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002014 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015#ifdef UNIX
2016 , stp
2017#endif
2018 ))
2019 return buf;
2020 return NULL;
2021}
2022
2023#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2024/*
2025 * Find file in buffer list by a regexp pattern.
2026 * Return fnum of the found buffer.
2027 * Return < 0 for error.
2028 */
2029/*ARGSUSED*/
2030 int
2031buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2032 char_u *pattern;
2033 char_u *pattern_end; /* pointer to first char after pattern */
2034 int unlisted; /* find unlisted buffers */
2035 int diffmode; /* find diff-mode buffers only */
2036{
2037 buf_T *buf;
2038 regprog_T *prog;
2039 int match = -1;
2040 int find_listed;
2041 char_u *pat;
2042 char_u *patend;
2043 int attempt;
2044 char_u *p;
2045 int toggledollar;
2046
2047 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2048 {
2049 if (*pattern == '%')
2050 match = curbuf->b_fnum;
2051 else
2052 match = curwin->w_alt_fnum;
2053#ifdef FEAT_DIFF
2054 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2055 match = -1;
2056#endif
2057 }
2058
2059 /*
2060 * Try four ways of matching a listed buffer:
2061 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002062 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 * attempt == 2: with '$' at end (only match at end)
2064 * attempt == 3: with '^' at start and '$' at end (only full match)
2065 * Repeat this for finding an unlisted buffer if there was no matching
2066 * listed buffer.
2067 */
2068 else
2069 {
2070 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2071 if (pat == NULL)
2072 return -1;
2073 patend = pat + STRLEN(pat) - 1;
2074 toggledollar = (patend > pat && *patend == '$');
2075
2076 /* First try finding a listed buffer. If not found and "unlisted"
2077 * is TRUE, try finding an unlisted buffer. */
2078 find_listed = TRUE;
2079 for (;;)
2080 {
2081 for (attempt = 0; attempt <= 3; ++attempt)
2082 {
2083 /* may add '^' and '$' */
2084 if (toggledollar)
2085 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2086 p = pat;
2087 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2088 ++p;
2089 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2090 if (prog == NULL)
2091 {
2092 vim_free(pat);
2093 return -1;
2094 }
2095
2096 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2097 if (buf->b_p_bl == find_listed
2098#ifdef FEAT_DIFF
2099 && (!diffmode || diff_mode_buf(buf))
2100#endif
2101 && buflist_match(prog, buf) != NULL)
2102 {
2103 if (match >= 0) /* already found a match */
2104 {
2105 match = -2;
2106 break;
2107 }
2108 match = buf->b_fnum; /* remember first match */
2109 }
2110
2111 vim_free(prog);
2112 if (match >= 0) /* found one match */
2113 break;
2114 }
2115
2116 /* Only search for unlisted buffers if there was no match with
2117 * a listed buffer. */
2118 if (!unlisted || !find_listed || match != -1)
2119 break;
2120 find_listed = FALSE;
2121 }
2122
2123 vim_free(pat);
2124 }
2125
2126 if (match == -2)
2127 EMSG2(_("E93: More than one match for %s"), pattern);
2128 else if (match < 0)
2129 EMSG2(_("E94: No matching buffer for %s"), pattern);
2130 return match;
2131}
2132#endif
2133
2134#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2135
2136/*
2137 * Find all buffer names that match.
2138 * For command line expansion of ":buf" and ":sbuf".
2139 * Return OK if matches found, FAIL otherwise.
2140 */
2141 int
2142ExpandBufnames(pat, num_file, file, options)
2143 char_u *pat;
2144 int *num_file;
2145 char_u ***file;
2146 int options;
2147{
2148 int count = 0;
2149 buf_T *buf;
2150 int round;
2151 char_u *p;
2152 int attempt;
2153 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002154 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156 *num_file = 0; /* return values in case of FAIL */
2157 *file = NULL;
2158
Bram Moolenaar05159a02005-02-26 23:04:13 +00002159 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2160 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002162 patc = alloc((unsigned)STRLEN(pat) + 11);
2163 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002165 STRCPY(patc, "\\(^\\|[\\/]\\)");
2166 STRCPY(patc + 11, pat + 1);
2167 }
2168 else
2169 patc = pat;
2170
2171 /*
2172 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002173 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002174 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002175 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002176 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002177 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002178 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002179 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002180 if (prog == NULL)
2181 {
2182 if (patc != pat)
2183 vim_free(patc);
2184 return FAIL;
2185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
2187 /*
2188 * round == 1: Count the matches.
2189 * round == 2: Build the array to keep the matches.
2190 */
2191 for (round = 1; round <= 2; ++round)
2192 {
2193 count = 0;
2194 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2195 {
2196 if (!buf->b_p_bl) /* skip unlisted buffers */
2197 continue;
2198 p = buflist_match(prog, buf);
2199 if (p != NULL)
2200 {
2201 if (round == 1)
2202 ++count;
2203 else
2204 {
2205 if (options & WILD_HOME_REPLACE)
2206 p = home_replace_save(buf, p);
2207 else
2208 p = vim_strsave(p);
2209 (*file)[count++] = p;
2210 }
2211 }
2212 }
2213 if (count == 0) /* no match found, break here */
2214 break;
2215 if (round == 1)
2216 {
2217 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2218 if (*file == NULL)
2219 {
2220 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002221 if (patc != pat)
2222 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 return FAIL;
2224 }
2225 }
2226 }
2227 vim_free(prog);
2228 if (count) /* match(es) found, break here */
2229 break;
2230 }
2231
Bram Moolenaar05159a02005-02-26 23:04:13 +00002232 if (patc != pat)
2233 vim_free(patc);
2234
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 *num_file = count;
2236 return (count == 0 ? FAIL : OK);
2237}
2238
2239#endif /* FEAT_CMDL_COMPL */
2240
2241#ifdef HAVE_BUFLIST_MATCH
2242/*
2243 * Check for a match on the file name for buffer "buf" with regprog "prog".
2244 */
2245 static char_u *
2246buflist_match(prog, buf)
2247 regprog_T *prog;
2248 buf_T *buf;
2249{
2250 char_u *match;
2251
2252 /* First try the short file name, then the long file name. */
2253 match = fname_match(prog, buf->b_sfname);
2254 if (match == NULL)
2255 match = fname_match(prog, buf->b_ffname);
2256
2257 return match;
2258}
2259
2260/*
2261 * Try matching the regexp in "prog" with file name "name".
2262 * Return "name" when there is a match, NULL when not.
2263 */
2264 static char_u *
2265fname_match(prog, name)
2266 regprog_T *prog;
2267 char_u *name;
2268{
2269 char_u *match = NULL;
2270 char_u *p;
2271 regmatch_T regmatch;
2272
2273 if (name != NULL)
2274 {
2275 regmatch.regprog = prog;
2276#ifdef CASE_INSENSITIVE_FILENAME
2277 regmatch.rm_ic = TRUE; /* Always ignore case */
2278#else
2279 regmatch.rm_ic = FALSE; /* Never ignore case */
2280#endif
2281
2282 if (vim_regexec(&regmatch, name, (colnr_T)0))
2283 match = name;
2284 else
2285 {
2286 /* Replace $(HOME) with '~' and try matching again. */
2287 p = home_replace_save(NULL, name);
2288 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2289 match = name;
2290 vim_free(p);
2291 }
2292 }
2293
2294 return match;
2295}
2296#endif
2297
2298/*
2299 * find file in buffer list by number
2300 */
2301 buf_T *
2302buflist_findnr(nr)
2303 int nr;
2304{
2305 buf_T *buf;
2306
2307 if (nr == 0)
2308 nr = curwin->w_alt_fnum;
2309 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2310 if (buf->b_fnum == nr)
2311 return (buf);
2312 return NULL;
2313}
2314
2315/*
2316 * Get name of file 'n' in the buffer list.
2317 * When the file has no name an empty string is returned.
2318 * home_replace() is used to shorten the file name (used for marks).
2319 * Returns a pointer to allocated memory, of NULL when failed.
2320 */
2321 char_u *
2322buflist_nr2name(n, fullname, helptail)
2323 int n;
2324 int fullname;
2325 int helptail; /* for help buffers return tail only */
2326{
2327 buf_T *buf;
2328
2329 buf = buflist_findnr(n);
2330 if (buf == NULL)
2331 return NULL;
2332 return home_replace_save(helptail ? buf : NULL,
2333 fullname ? buf->b_ffname : buf->b_fname);
2334}
2335
2336/*
2337 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2338 * When "copy_options" is TRUE save the local window option values.
2339 * When "lnum" is 0 only do the options.
2340 */
2341 static void
2342buflist_setfpos(buf, win, lnum, col, copy_options)
2343 buf_T *buf;
2344 win_T *win;
2345 linenr_T lnum;
2346 colnr_T col;
2347 int copy_options;
2348{
2349 wininfo_T *wip;
2350
2351 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2352 if (wip->wi_win == win)
2353 break;
2354 if (wip == NULL)
2355 {
2356 /* allocate a new entry */
2357 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2358 if (wip == NULL)
2359 return;
2360 wip->wi_win = win;
2361 if (lnum == 0) /* set lnum even when it's 0 */
2362 lnum = 1;
2363 }
2364 else
2365 {
2366 /* remove the entry from the list */
2367 if (wip->wi_prev)
2368 wip->wi_prev->wi_next = wip->wi_next;
2369 else
2370 buf->b_wininfo = wip->wi_next;
2371 if (wip->wi_next)
2372 wip->wi_next->wi_prev = wip->wi_prev;
2373 if (copy_options && wip->wi_optset)
2374 {
2375 clear_winopt(&wip->wi_opt);
2376#ifdef FEAT_FOLDING
2377 deleteFoldRecurse(&wip->wi_folds);
2378#endif
2379 }
2380 }
2381 if (lnum != 0)
2382 {
2383 wip->wi_fpos.lnum = lnum;
2384 wip->wi_fpos.col = col;
2385 }
2386 if (copy_options)
2387 {
2388 /* Save the window-specific option values. */
2389 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2390#ifdef FEAT_FOLDING
2391 wip->wi_fold_manual = win->w_fold_manual;
2392 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2393#endif
2394 wip->wi_optset = TRUE;
2395 }
2396
2397 /* insert the entry in front of the list */
2398 wip->wi_next = buf->b_wininfo;
2399 buf->b_wininfo = wip;
2400 wip->wi_prev = NULL;
2401 if (wip->wi_next)
2402 wip->wi_next->wi_prev = wip;
2403
2404 return;
2405}
2406
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002407#ifdef FEAT_DIFF
2408static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2409
2410/*
2411 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2412 * page. That's because a diff is local to a tab page.
2413 */
2414 static int
2415wininfo_other_tab_diff(wip)
2416 wininfo_T *wip;
2417{
2418 win_T *wp;
2419
2420 if (wip->wi_opt.wo_diff)
2421 {
2422 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2423 /* return FALSE when it's a window in the current tab page, thus
2424 * the buffer was in diff mode here */
2425 if (wip->wi_win == wp)
2426 return FALSE;
2427 return TRUE;
2428 }
2429 return FALSE;
2430}
2431#endif
2432
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433/*
2434 * Find info for the current window in buffer "buf".
2435 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002436 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2437 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 * Returns NULL when there isn't any info.
2439 */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002440/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002442find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 buf_T *buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002444 int skip_diff_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445{
2446 wininfo_T *wip;
2447
2448 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002449 if (wip->wi_win == curwin
2450#ifdef FEAT_DIFF
2451 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2452#endif
2453 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002455
2456 /* If no wininfo for curwin, use the first in the list (that doesn't have
2457 * 'diff' set and is in another tab page). */
2458 if (wip == NULL)
2459 {
2460#ifdef FEAT_DIFF
2461 if (skip_diff_buffer)
2462 {
2463 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2464 if (!wininfo_other_tab_diff(wip))
2465 break;
2466 }
2467 else
2468#endif
2469 wip = buf->b_wininfo;
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 return wip;
2472}
2473
2474/*
2475 * Reset the local window options to the values last used in this window.
2476 * If the buffer wasn't used in this window before, use the values from
2477 * the most recently used window. If the values were never set, use the
2478 * global values for the window.
2479 */
2480 void
2481get_winopts(buf)
2482 buf_T *buf;
2483{
2484 wininfo_T *wip;
2485
2486 clear_winopt(&curwin->w_onebuf_opt);
2487#ifdef FEAT_FOLDING
2488 clearFolding(curwin);
2489#endif
2490
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002491 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 if (wip != NULL && wip->wi_optset)
2493 {
2494 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2495#ifdef FEAT_FOLDING
2496 curwin->w_fold_manual = wip->wi_fold_manual;
2497 curwin->w_foldinvalid = TRUE;
2498 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2499#endif
2500 }
2501 else
2502 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2503
2504#ifdef FEAT_FOLDING
2505 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2506 if (p_fdls >= 0)
2507 curwin->w_p_fdl = p_fdls;
2508#endif
2509}
2510
2511/*
2512 * Find the position (lnum and col) for the buffer 'buf' for the current
2513 * window.
2514 * Returns a pointer to no_position if no position is found.
2515 */
2516 pos_T *
2517buflist_findfpos(buf)
2518 buf_T *buf;
2519{
2520 wininfo_T *wip;
2521 static pos_T no_position = {1, 0};
2522
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002523 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 if (wip != NULL)
2525 return &(wip->wi_fpos);
2526 else
2527 return &no_position;
2528}
2529
2530/*
2531 * Find the lnum for the buffer 'buf' for the current window.
2532 */
2533 linenr_T
2534buflist_findlnum(buf)
2535 buf_T *buf;
2536{
2537 return buflist_findfpos(buf)->lnum;
2538}
2539
2540#if defined(FEAT_LISTCMDS) || defined(PROTO)
2541/*
2542 * List all know file names (for :files and :buffers command).
2543 */
2544/*ARGSUSED*/
2545 void
2546buflist_list(eap)
2547 exarg_T *eap;
2548{
2549 buf_T *buf;
2550 int len;
2551 int i;
2552
2553 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2554 {
2555 /* skip unlisted buffers, unless ! was used */
2556 if (!buf->b_p_bl && !eap->forceit)
2557 continue;
2558 msg_putchar('\n');
2559 if (buf_spname(buf) != NULL)
2560 STRCPY(NameBuff, buf_spname(buf));
2561 else
2562 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2563
Bram Moolenaar50cde822005-06-05 21:54:54 +00002564 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 buf->b_fnum,
2566 buf->b_p_bl ? ' ' : 'u',
2567 buf == curbuf ? '%' :
2568 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2569 buf->b_ml.ml_mfp == NULL ? ' ' :
2570 (buf->b_nwindows == 0 ? 'h' : 'a'),
2571 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2572 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002573 : (bufIsChanged(buf) ? '+' : ' '),
2574 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
2576 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 i = 40 - vim_strsize(IObuff);
2578 do
2579 {
2580 IObuff[len++] = ' ';
2581 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002582 vim_snprintf((char *)IObuff + len, IOSIZE - len, _("line %ld"),
2583 buf == curbuf ? curwin->w_cursor.lnum
2584 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 msg_outtrans(IObuff);
2586 out_flush(); /* output one line at a time */
2587 ui_breakcheck();
2588 }
2589}
2590#endif
2591
2592/*
2593 * Get file name and line number for file 'fnum'.
2594 * Used by DoOneCmd() for translating '%' and '#'.
2595 * Used by insert_reg() and cmdline_paste() for '#' register.
2596 * Return FAIL if not found, OK for success.
2597 */
2598 int
2599buflist_name_nr(fnum, fname, lnum)
2600 int fnum;
2601 char_u **fname;
2602 linenr_T *lnum;
2603{
2604 buf_T *buf;
2605
2606 buf = buflist_findnr(fnum);
2607 if (buf == NULL || buf->b_fname == NULL)
2608 return FAIL;
2609
2610 *fname = buf->b_fname;
2611 *lnum = buflist_findlnum(buf);
2612
2613 return OK;
2614}
2615
2616/*
2617 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2618 * The file name with the full path is also remembered, for when :cd is used.
2619 * Returns FAIL for failure (file name already in use by other buffer)
2620 * OK otherwise.
2621 */
2622 int
2623setfname(buf, ffname, sfname, message)
2624 buf_T *buf;
2625 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002626 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627{
Bram Moolenaar81695252004-12-29 20:58:21 +00002628 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629#ifdef UNIX
2630 struct stat st;
2631#endif
2632
2633 if (ffname == NULL || *ffname == NUL)
2634 {
2635 /* Removing the name. */
2636 vim_free(buf->b_ffname);
2637 vim_free(buf->b_sfname);
2638 buf->b_ffname = NULL;
2639 buf->b_sfname = NULL;
2640#ifdef UNIX
2641 st.st_dev = (dev_T)-1;
2642#endif
2643 }
2644 else
2645 {
2646 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2647 if (ffname == NULL) /* out of memory */
2648 return FAIL;
2649
2650 /*
2651 * if the file name is already used in another buffer:
2652 * - if the buffer is loaded, fail
2653 * - if the buffer is not loaded, delete it from the list
2654 */
2655#ifdef UNIX
2656 if (mch_stat((char *)ffname, &st) < 0)
2657 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002658#endif
2659 if (!(buf->b_flags & BF_DUMMY))
2660#ifdef UNIX
2661 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002663 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664#endif
2665 if (obuf != NULL && obuf != buf)
2666 {
2667 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2668 {
2669 if (message)
2670 EMSG(_("E95: Buffer with this name already exists"));
2671 vim_free(ffname);
2672 return FAIL;
2673 }
2674 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2675 }
2676 sfname = vim_strsave(sfname);
2677 if (ffname == NULL || sfname == NULL)
2678 {
2679 vim_free(sfname);
2680 vim_free(ffname);
2681 return FAIL;
2682 }
2683#ifdef USE_FNAME_CASE
2684# ifdef USE_LONG_FNAME
2685 if (USE_LONG_FNAME)
2686# endif
2687 fname_case(sfname, 0); /* set correct case for short file name */
2688#endif
2689 vim_free(buf->b_ffname);
2690 vim_free(buf->b_sfname);
2691 buf->b_ffname = ffname;
2692 buf->b_sfname = sfname;
2693 }
2694 buf->b_fname = buf->b_sfname;
2695#ifdef UNIX
2696 if (st.st_dev == (dev_T)-1)
2697 buf->b_dev = -1;
2698 else
2699 {
2700 buf->b_dev = st.st_dev;
2701 buf->b_ino = st.st_ino;
2702 }
2703#endif
2704
2705#ifndef SHORT_FNAME
2706 buf->b_shortname = FALSE;
2707#endif
2708
2709 buf_name_changed(buf);
2710 return OK;
2711}
2712
2713/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002714 * Crude way of changing the name of a buffer. Use with care!
2715 * The name should be relative to the current directory.
2716 */
2717 void
2718buf_set_name(fnum, name)
2719 int fnum;
2720 char_u *name;
2721{
2722 buf_T *buf;
2723
2724 buf = buflist_findnr(fnum);
2725 if (buf != NULL)
2726 {
2727 vim_free(buf->b_sfname);
2728 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002729 buf->b_ffname = vim_strsave(name);
2730 buf->b_sfname = NULL;
2731 /* Allocate ffname and expand into full path. Also resolves .lnk
2732 * files on Win32. */
2733 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002734 buf->b_fname = buf->b_sfname;
2735 }
2736}
2737
2738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 * Take care of what needs to be done when the name of buffer "buf" has
2740 * changed.
2741 */
2742 void
2743buf_name_changed(buf)
2744 buf_T *buf;
2745{
2746 /*
2747 * If the file name changed, also change the name of the swapfile
2748 */
2749 if (buf->b_ml.ml_mfp != NULL)
2750 ml_setname(buf);
2751
2752 if (curwin->w_buffer == buf)
2753 check_arg_idx(curwin); /* check file name for arg list */
2754#ifdef FEAT_TITLE
2755 maketitle(); /* set window title */
2756#endif
2757#ifdef FEAT_WINDOWS
2758 status_redraw_all(); /* status lines need to be redrawn */
2759#endif
2760 fmarks_check_names(buf); /* check named file marks */
2761 ml_timestamp(buf); /* reset timestamp */
2762}
2763
2764/*
2765 * set alternate file name for current window
2766 *
2767 * Used by do_one_cmd(), do_write() and do_ecmd().
2768 * Return the buffer.
2769 */
2770 buf_T *
2771setaltfname(ffname, sfname, lnum)
2772 char_u *ffname;
2773 char_u *sfname;
2774 linenr_T lnum;
2775{
2776 buf_T *buf;
2777
2778 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2779 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002780 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 curwin->w_alt_fnum = buf->b_fnum;
2782 return buf;
2783}
2784
2785/*
2786 * Get alternate file name for current window.
2787 * Return NULL if there isn't any, and give error message if requested.
2788 */
2789 char_u *
2790getaltfname(errmsg)
2791 int errmsg; /* give error message */
2792{
2793 char_u *fname;
2794 linenr_T dummy;
2795
2796 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2797 {
2798 if (errmsg)
2799 EMSG(_(e_noalt));
2800 return NULL;
2801 }
2802 return fname;
2803}
2804
2805/*
2806 * Add a file name to the buflist and return its number.
2807 * Uses same flags as buflist_new(), except BLN_DUMMY.
2808 *
2809 * used by qf_init(), main() and doarglist()
2810 */
2811 int
2812buflist_add(fname, flags)
2813 char_u *fname;
2814 int flags;
2815{
2816 buf_T *buf;
2817
2818 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2819 if (buf != NULL)
2820 return buf->b_fnum;
2821 return 0;
2822}
2823
2824#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2825/*
2826 * Adjust slashes in file names. Called after 'shellslash' was set.
2827 */
2828 void
2829buflist_slash_adjust()
2830{
2831 buf_T *bp;
2832
2833 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2834 {
2835 if (bp->b_ffname != NULL)
2836 slash_adjust(bp->b_ffname);
2837 if (bp->b_sfname != NULL)
2838 slash_adjust(bp->b_sfname);
2839 }
2840}
2841#endif
2842
2843/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002844 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 * Also save the local window option values.
2846 */
2847 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002848buflist_altfpos(win)
2849 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002851 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852}
2853
2854/*
2855 * Return TRUE if 'ffname' is not the same file as current file.
2856 * Fname must have a full path (expanded by mch_FullName()).
2857 */
2858 int
2859otherfile(ffname)
2860 char_u *ffname;
2861{
2862 return otherfile_buf(curbuf, ffname
2863#ifdef UNIX
2864 , NULL
2865#endif
2866 );
2867}
2868
2869 static int
2870otherfile_buf(buf, ffname
2871#ifdef UNIX
2872 , stp
2873#endif
2874 )
2875 buf_T *buf;
2876 char_u *ffname;
2877#ifdef UNIX
2878 struct stat *stp;
2879#endif
2880{
2881 /* no name is different */
2882 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2883 return TRUE;
2884 if (fnamecmp(ffname, buf->b_ffname) == 0)
2885 return FALSE;
2886#ifdef UNIX
2887 {
2888 struct stat st;
2889
2890 /* If no struct stat given, get it now */
2891 if (stp == NULL)
2892 {
2893 if (buf->b_dev < 0 || mch_stat((char *)ffname, &st) < 0)
2894 st.st_dev = (dev_T)-1;
2895 stp = &st;
2896 }
2897 /* Use dev/ino to check if the files are the same, even when the names
2898 * are different (possible with links). Still need to compare the
2899 * name above, for when the file doesn't exist yet.
2900 * Problem: The dev/ino changes when a file is deleted (and created
2901 * again) and remains the same when renamed/moved. We don't want to
2902 * mch_stat() each buffer each time, that would be too slow. Get the
2903 * dev/ino again when they appear to match, but not when they appear
2904 * to be different: Could skip a buffer when it's actually the same
2905 * file. */
2906 if (buf_same_ino(buf, stp))
2907 {
2908 buf_setino(buf);
2909 if (buf_same_ino(buf, stp))
2910 return FALSE;
2911 }
2912 }
2913#endif
2914 return TRUE;
2915}
2916
2917#if defined(UNIX) || defined(PROTO)
2918/*
2919 * Set inode and device number for a buffer.
2920 * Must always be called when b_fname is changed!.
2921 */
2922 void
2923buf_setino(buf)
2924 buf_T *buf;
2925{
2926 struct stat st;
2927
2928 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2929 {
2930 buf->b_dev = st.st_dev;
2931 buf->b_ino = st.st_ino;
2932 }
2933 else
2934 buf->b_dev = -1;
2935}
2936
2937/*
2938 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2939 */
2940 static int
2941buf_same_ino(buf, stp)
2942 buf_T *buf;
2943 struct stat *stp;
2944{
2945 return (buf->b_dev >= 0
2946 && stp->st_dev == buf->b_dev
2947 && stp->st_ino == buf->b_ino);
2948}
2949#endif
2950
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002951/*
2952 * Print info about the current buffer.
2953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 void
2955fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002956 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 int shorthelp;
2958 int dont_truncate;
2959{
2960 char_u *name;
2961 int n;
2962 char_u *p;
2963 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002964 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
2966 buffer = alloc(IOSIZE);
2967 if (buffer == NULL)
2968 return;
2969
2970 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2971 {
2972 sprintf((char *)buffer, "buf %d: ", curbuf->b_fnum);
2973 p = buffer + STRLEN(buffer);
2974 }
2975 else
2976 p = buffer;
2977
2978 *p++ = '"';
2979 if (buf_spname(curbuf) != NULL)
2980 STRCPY(p, buf_spname(curbuf));
2981 else
2982 {
2983 if (!fullname && curbuf->b_fname != NULL)
2984 name = curbuf->b_fname;
2985 else
2986 name = curbuf->b_ffname;
2987 home_replace(shorthelp ? curbuf : NULL, name, p,
2988 (int)(IOSIZE - (p - buffer)), TRUE);
2989 }
2990
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002991 len = STRLEN(buffer);
2992 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 "\"%s%s%s%s%s%s",
2994 curbufIsChanged() ? (shortmess(SHM_MOD)
2995 ? " [+]" : _(" [Modified]")) : " ",
2996 (curbuf->b_flags & BF_NOTEDITED)
2997#ifdef FEAT_QUICKFIX
2998 && !bt_dontwrite(curbuf)
2999#endif
3000 ? _("[Not edited]") : "",
3001 (curbuf->b_flags & BF_NEW)
3002#ifdef FEAT_QUICKFIX
3003 && !bt_dontwrite(curbuf)
3004#endif
3005 ? _("[New file]") : "",
3006 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3007 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3008 : _("[readonly]")) : "",
3009 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3010 || curbuf->b_p_ro) ?
3011 " " : "");
3012 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3013 * causes an overflow, thus for large numbers divide instead. */
3014 if (curwin->w_cursor.lnum > 1000000L)
3015 n = (int)(((long)curwin->w_cursor.lnum) /
3016 ((long)curbuf->b_ml.ml_line_count / 100L));
3017 else
3018 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3019 (long)curbuf->b_ml.ml_line_count);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003020 len = STRLEN(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3022 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003023 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 }
3025#ifdef FEAT_CMDL_INFO
3026 else if (p_ru)
3027 {
3028 /* Current line and column are already on the screen -- webb */
3029 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003030 vim_snprintf((char *)buffer + len, IOSIZE - len,
3031 _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003033 vim_snprintf((char *)buffer + len, IOSIZE - len,
3034 _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 (long)curbuf->b_ml.ml_line_count, n);
3036 }
3037#endif
3038 else
3039 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003040 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 _("line %ld of %ld --%d%%-- col "),
3042 (long)curwin->w_cursor.lnum,
3043 (long)curbuf->b_ml.ml_line_count,
3044 n);
3045 validate_virtcol();
3046 col_print(buffer + STRLEN(buffer),
3047 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3048 }
3049
3050 (void)append_arg_number(curwin, buffer, !shortmess(SHM_FILE), IOSIZE);
3051
3052 if (dont_truncate)
3053 {
3054 /* Temporarily set msg_scroll to avoid the message being truncated.
3055 * First call msg_start() to get the message in the right place. */
3056 msg_start();
3057 n = msg_scroll;
3058 msg_scroll = TRUE;
3059 msg(buffer);
3060 msg_scroll = n;
3061 }
3062 else
3063 {
3064 p = msg_trunc_attr(buffer, FALSE, 0);
3065 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 /* Need to repeat the message after redrawing when:
3067 * - When restart_edit is set (otherwise there will be a delay
3068 * before redrawing).
3069 * - When the screen was scrolled but there is no wait-return
3070 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003071 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 }
3073
3074 vim_free(buffer);
3075}
3076
3077 void
3078col_print(buf, col, vcol)
3079 char_u *buf;
3080 int col;
3081 int vcol;
3082{
3083 if (col == vcol)
3084 sprintf((char *)buf, "%d", col);
3085 else
3086 sprintf((char *)buf, "%d-%d", col, vcol);
3087}
3088
3089#if defined(FEAT_TITLE) || defined(PROTO)
3090/*
3091 * put file name in title bar of window and in icon title
3092 */
3093
3094static char_u *lasttitle = NULL;
3095static char_u *lasticon = NULL;
3096
3097 void
3098maketitle()
3099{
3100 char_u *p;
3101 char_u *t_str = NULL;
3102 char_u *i_name;
3103 char_u *i_str = NULL;
3104 int maxlen = 0;
3105 int len;
3106 int mustset;
3107 char_u buf[IOSIZE];
3108 int off;
3109
3110 if (!redrawing())
3111 {
3112 /* Postpone updating the title when 'lazyredraw' is set. */
3113 need_maketitle = TRUE;
3114 return;
3115 }
3116
3117 need_maketitle = FALSE;
3118 if (!p_title && !p_icon)
3119 return;
3120
3121 if (p_title)
3122 {
3123 if (p_titlelen > 0)
3124 {
3125 maxlen = p_titlelen * Columns / 100;
3126 if (maxlen < 10)
3127 maxlen = 10;
3128 }
3129
3130 t_str = buf;
3131 if (*p_titlestring != NUL)
3132 {
3133#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003134 if (stl_syntax & STL_IN_TITLE)
3135 {
3136 int use_sandbox = FALSE;
3137 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003138
3139# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003140 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003141# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003142 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003144 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003145 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003146 if (called_emsg)
3147 set_string_option_direct((char_u *)"titlestring", -1,
3148 (char_u *)"", OPT_FREE, SID_ERROR);
3149 called_emsg |= save_called_emsg;
3150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 else
3152#endif
3153 t_str = p_titlestring;
3154 }
3155 else
3156 {
3157 /* format: "fname + (path) (1 of 2) - VIM" */
3158
3159 if (curbuf->b_fname == NULL)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003160 STRCPY(buf, _("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 else
3162 {
3163 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003164 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 }
3167
3168 switch (bufIsChanged(curbuf)
3169 + (curbuf->b_p_ro * 2)
3170 + (!curbuf->b_p_ma * 4))
3171 {
3172 case 1: STRCAT(buf, " +"); break;
3173 case 2: STRCAT(buf, " ="); break;
3174 case 3: STRCAT(buf, " =+"); break;
3175 case 4:
3176 case 6: STRCAT(buf, " -"); break;
3177 case 5:
3178 case 7: STRCAT(buf, " -+"); break;
3179 }
3180
3181 if (curbuf->b_fname != NULL)
3182 {
3183 /* Get path of file, replace home dir with ~ */
3184 off = (int)STRLEN(buf);
3185 buf[off++] = ' ';
3186 buf[off++] = '(';
3187 home_replace(curbuf, curbuf->b_ffname,
3188 buf + off, IOSIZE - off, TRUE);
3189#ifdef BACKSLASH_IN_FILENAME
3190 /* avoid "c:/name" to be reduced to "c" */
3191 if (isalpha(buf[off]) && buf[off + 1] == ':')
3192 off += 2;
3193#endif
3194 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003195 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003198 vim_strncpy(buf + off, (char_u *)_("help"),
3199 IOSIZE - off - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003202
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 /* translate unprintable chars */
3204 p = transstr(buf + off);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003205 vim_strncpy(buf + off, p, IOSIZE - off - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 STRCAT(buf, ")");
3208 }
3209
3210 append_arg_number(curwin, buf, FALSE, IOSIZE);
3211
3212#if defined(FEAT_CLIENTSERVER)
3213 if (serverName != NULL)
3214 {
3215 STRCAT(buf, " - ");
3216 STRCAT(buf, serverName);
3217 }
3218 else
3219#endif
3220 STRCAT(buf, " - VIM");
3221
3222 if (maxlen > 0)
3223 {
3224 /* make it shorter by removing a bit in the middle */
3225 len = vim_strsize(buf);
3226 if (len > maxlen)
3227 trunc_string(buf, buf, maxlen);
3228 }
3229 }
3230 }
3231 mustset = ti_change(t_str, &lasttitle);
3232
3233 if (p_icon)
3234 {
3235 i_str = buf;
3236 if (*p_iconstring != NUL)
3237 {
3238#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003239 if (stl_syntax & STL_IN_ICON)
3240 {
3241 int use_sandbox = FALSE;
3242 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003243
3244# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003245 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003246# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003247 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003249 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003250 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003251 if (called_emsg)
3252 set_string_option_direct((char_u *)"iconstring", -1,
3253 (char_u *)"", OPT_FREE, SID_ERROR);
3254 called_emsg |= save_called_emsg;
3255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 else
3257#endif
3258 i_str = p_iconstring;
3259 }
3260 else
3261 {
3262 if (buf_spname(curbuf) != NULL)
3263 i_name = (char_u *)buf_spname(curbuf);
3264 else /* use file name only in icon */
3265 i_name = gettail(curbuf->b_ffname);
3266 *i_str = NUL;
3267 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003268 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 if (len > 100)
3270 {
3271 len -= 100;
3272#ifdef FEAT_MBYTE
3273 if (has_mbyte)
3274 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3275#endif
3276 i_name += len;
3277 }
3278 STRCPY(i_str, i_name);
3279 trans_characters(i_str, IOSIZE);
3280 }
3281 }
3282
3283 mustset |= ti_change(i_str, &lasticon);
3284
3285 if (mustset)
3286 resettitle();
3287}
3288
3289/*
3290 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3291 * from "str" if it does.
3292 * Return TRUE when "*last" changed.
3293 */
3294 static int
3295ti_change(str, last)
3296 char_u *str;
3297 char_u **last;
3298{
3299 if ((str == NULL) != (*last == NULL)
3300 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3301 {
3302 vim_free(*last);
3303 if (str == NULL)
3304 *last = NULL;
3305 else
3306 *last = vim_strsave(str);
3307 return TRUE;
3308 }
3309 return FALSE;
3310}
3311
3312/*
3313 * Put current window title back (used after calling a shell)
3314 */
3315 void
3316resettitle()
3317{
3318 mch_settitle(lasttitle, lasticon);
3319}
Bram Moolenaarea408852005-06-25 22:49:46 +00003320
3321# if defined(EXITFREE) || defined(PROTO)
3322 void
3323free_titles()
3324{
3325 vim_free(lasttitle);
3326 vim_free(lasticon);
3327}
3328# endif
3329
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#endif /* FEAT_TITLE */
3331
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003332#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003334 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 * Return length of string in screen cells.
3336 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003337 * Normally works for window "wp", except when working for 'tabline' then it
3338 * is "curwin".
3339 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 * Items are drawn interspersed with the text that surrounds it
3341 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3342 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3343 *
3344 * If maxwidth is not zero, the string will be filled at any middle marker
3345 * or truncated if too long, fillchar is used for all whitespace.
3346 */
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003347/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003349build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003351 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 size_t outlen; /* length of out[] */
3353 char_u *fmt;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003354 int use_sandbox; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 int fillchar;
3356 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003357 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3358 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360 char_u *p;
3361 char_u *s;
3362 char_u *t;
3363 char_u *linecont;
3364#ifdef FEAT_EVAL
3365 win_T *o_curwin;
3366 buf_T *o_curbuf;
3367#endif
3368 int empty_line;
3369 colnr_T virtcol;
3370 long l;
3371 long n;
3372 int prevchar_isflag;
3373 int prevchar_isitem;
3374 int itemisflag;
3375 int fillable;
3376 char_u *str;
3377 long num;
3378 int width;
3379 int itemcnt;
3380 int curitem;
3381 int groupitem[STL_MAX_ITEM];
3382 int groupdepth;
3383 struct stl_item
3384 {
3385 char_u *start;
3386 int minwid;
3387 int maxwid;
3388 enum
3389 {
3390 Normal,
3391 Empty,
3392 Group,
3393 Middle,
3394 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003395 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 Trunc
3397 } type;
3398 } item[STL_MAX_ITEM];
3399 int minwid;
3400 int maxwid;
3401 int zeropad;
3402 char_u base;
3403 char_u opt;
3404#define TMPLEN 70
3405 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003406 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003407 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003408
3409#ifdef FEAT_EVAL
3410 /*
3411 * When the format starts with "%!" then evaluate it as an expression and
3412 * use the result as the actual format string.
3413 */
3414 if (fmt[0] == '%' && fmt[1] == '!')
3415 {
3416 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3417 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003418 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003419 }
3420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
3422 if (fillchar == 0)
3423 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003424#ifdef FEAT_MBYTE
3425 /* Can't handle a multi-byte fill character yet. */
3426 else if (mb_char2len(fillchar) > 1)
3427 fillchar = '-';
3428#endif
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 /*
3431 * Get line & check if empty (cursorpos will show "0-1").
3432 * If inversion is possible we use it. Else '=' characters are used.
3433 */
3434 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3435 empty_line = (*linecont == NUL);
3436
3437 groupdepth = 0;
3438 p = out;
3439 curitem = 0;
3440 prevchar_isflag = TRUE;
3441 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003442 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 {
3444 if (*s != NUL && *s != '%')
3445 prevchar_isflag = prevchar_isitem = FALSE;
3446
3447 /*
3448 * Handle up to the next '%' or the end.
3449 */
3450 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3451 *p++ = *s++;
3452 if (*s == NUL || p + 1 >= out + outlen)
3453 break;
3454
3455 /*
3456 * Handle one '%' item.
3457 */
3458 s++;
3459 if (*s == '%')
3460 {
3461 if (p + 1 >= out + outlen)
3462 break;
3463 *p++ = *s++;
3464 prevchar_isflag = prevchar_isitem = FALSE;
3465 continue;
3466 }
3467 if (*s == STL_MIDDLEMARK)
3468 {
3469 s++;
3470 if (groupdepth > 0)
3471 continue;
3472 item[curitem].type = Middle;
3473 item[curitem++].start = p;
3474 continue;
3475 }
3476 if (*s == STL_TRUNCMARK)
3477 {
3478 s++;
3479 item[curitem].type = Trunc;
3480 item[curitem++].start = p;
3481 continue;
3482 }
3483 if (*s == ')')
3484 {
3485 s++;
3486 if (groupdepth < 1)
3487 continue;
3488 groupdepth--;
3489
3490 t = item[groupitem[groupdepth]].start;
3491 *p = NUL;
3492 l = vim_strsize(t);
3493 if (curitem > groupitem[groupdepth] + 1
3494 && item[groupitem[groupdepth]].minwid == 0)
3495 {
3496 /* remove group if all items are empty */
3497 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3498 if (item[n].type == Normal)
3499 break;
3500 if (n == curitem)
3501 {
3502 p = t;
3503 l = 0;
3504 }
3505 }
3506 if (l > item[groupitem[groupdepth]].maxwid)
3507 {
3508 /* truncate, remove n bytes of text at the start */
3509#ifdef FEAT_MBYTE
3510 if (has_mbyte)
3511 {
3512 /* Find the first character that should be included. */
3513 n = 0;
3514 while (l >= item[groupitem[groupdepth]].maxwid)
3515 {
3516 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003517 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519 }
3520 else
3521#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003522 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
3524 *t = '<';
3525 mch_memmove(t + 1, t + n, p - (t + n));
3526 p = p - n + 1;
3527#ifdef FEAT_MBYTE
3528 /* Fill up space left over by half a double-wide char. */
3529 while (++l < item[groupitem[groupdepth]].minwid)
3530 *p++ = fillchar;
3531#endif
3532
3533 /* correct the start of the items for the truncation */
3534 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3535 {
3536 item[l].start -= n;
3537 if (item[l].start < t)
3538 item[l].start = t;
3539 }
3540 }
3541 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3542 {
3543 /* fill */
3544 n = item[groupitem[groupdepth]].minwid;
3545 if (n < 0)
3546 {
3547 /* fill by appending characters */
3548 n = 0 - n;
3549 while (l++ < n && p + 1 < out + outlen)
3550 *p++ = fillchar;
3551 }
3552 else
3553 {
3554 /* fill by inserting characters */
3555 mch_memmove(t + n - l, t, p - t);
3556 l = n - l;
3557 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003558 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 p += l;
3560 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3561 item[n].start += l;
3562 for ( ; l > 0; l--)
3563 *t++ = fillchar;
3564 }
3565 }
3566 continue;
3567 }
3568 minwid = 0;
3569 maxwid = 9999;
3570 zeropad = FALSE;
3571 l = 1;
3572 if (*s == '0')
3573 {
3574 s++;
3575 zeropad = TRUE;
3576 }
3577 if (*s == '-')
3578 {
3579 s++;
3580 l = -1;
3581 }
3582 if (VIM_ISDIGIT(*s))
3583 {
3584 minwid = (int)getdigits(&s);
3585 if (minwid < 0) /* overflow */
3586 minwid = 0;
3587 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003588 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
3590 item[curitem].type = Highlight;
3591 item[curitem].start = p;
3592 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3593 s++;
3594 curitem++;
3595 continue;
3596 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003597 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3598 {
3599 if (*s == STL_TABCLOSENR)
3600 {
3601 if (minwid == 0)
3602 {
3603 /* %X ends the close label, go back to the previously
3604 * define tab label nr. */
3605 for (n = curitem - 1; n >= 0; --n)
3606 if (item[n].type == TabPage && item[n].minwid >= 0)
3607 {
3608 minwid = item[n].minwid;
3609 break;
3610 }
3611 }
3612 else
3613 /* close nrs are stored as negative values */
3614 minwid = - minwid;
3615 }
3616 item[curitem].type = TabPage;
3617 item[curitem].start = p;
3618 item[curitem].minwid = minwid;
3619 s++;
3620 curitem++;
3621 continue;
3622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 if (*s == '.')
3624 {
3625 s++;
3626 if (VIM_ISDIGIT(*s))
3627 {
3628 maxwid = (int)getdigits(&s);
3629 if (maxwid <= 0) /* overflow */
3630 maxwid = 50;
3631 }
3632 }
3633 minwid = (minwid > 50 ? 50 : minwid) * l;
3634 if (*s == '(')
3635 {
3636 groupitem[groupdepth++] = curitem;
3637 item[curitem].type = Group;
3638 item[curitem].start = p;
3639 item[curitem].minwid = minwid;
3640 item[curitem].maxwid = maxwid;
3641 s++;
3642 curitem++;
3643 continue;
3644 }
3645 if (vim_strchr(STL_ALL, *s) == NULL)
3646 {
3647 s++;
3648 continue;
3649 }
3650 opt = *s++;
3651
3652 /* OK - now for the real work */
3653 base = 'D';
3654 itemisflag = FALSE;
3655 fillable = TRUE;
3656 num = -1;
3657 str = NULL;
3658 switch (opt)
3659 {
3660 case STL_FILEPATH:
3661 case STL_FULLPATH:
3662 case STL_FILENAME:
3663 fillable = FALSE; /* don't change ' ' to fillchar */
3664 if (buf_spname(wp->w_buffer) != NULL)
3665 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3666 else
3667 {
3668 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003669 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3671 }
3672 trans_characters(NameBuff, MAXPATHL);
3673 if (opt != STL_FILENAME)
3674 str = NameBuff;
3675 else
3676 str = gettail(NameBuff);
3677 break;
3678
3679 case STL_VIM_EXPR: /* '{' */
3680 itemisflag = TRUE;
3681 t = p;
3682 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3683 *p++ = *s++;
3684 if (*s != '}') /* missing '}' or out of space */
3685 break;
3686 s++;
3687 *p = 0;
3688 p = t;
3689
3690#ifdef FEAT_EVAL
3691 sprintf((char *)tmp, "%d", curbuf->b_fnum);
3692 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3693
3694 o_curbuf = curbuf;
3695 o_curwin = curwin;
3696 curwin = wp;
3697 curbuf = wp->w_buffer;
3698
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003699 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
3701 curwin = o_curwin;
3702 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003703 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705 if (str != NULL && *str != 0)
3706 {
3707 if (*skipdigits(str) == NUL)
3708 {
3709 num = atoi((char *)str);
3710 vim_free(str);
3711 str = NULL;
3712 itemisflag = FALSE;
3713 }
3714 }
3715#endif
3716 break;
3717
3718 case STL_LINE:
3719 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3720 ? 0L : (long)(wp->w_cursor.lnum);
3721 break;
3722
3723 case STL_NUMLINES:
3724 num = wp->w_buffer->b_ml.ml_line_count;
3725 break;
3726
3727 case STL_COLUMN:
3728 num = !(State & INSERT) && empty_line
3729 ? 0 : (int)wp->w_cursor.col + 1;
3730 break;
3731
3732 case STL_VIRTCOL:
3733 case STL_VIRTCOL_ALT:
3734 /* In list mode virtcol needs to be recomputed */
3735 virtcol = wp->w_virtcol;
3736 if (wp->w_p_list && lcs_tab1 == NUL)
3737 {
3738 wp->w_p_list = FALSE;
3739 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3740 wp->w_p_list = TRUE;
3741 }
3742 ++virtcol;
3743 /* Don't display %V if it's the same as %c. */
3744 if (opt == STL_VIRTCOL_ALT
3745 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3746 ? 0 : (int)wp->w_cursor.col + 1)))
3747 break;
3748 num = (long)virtcol;
3749 break;
3750
3751 case STL_PERCENTAGE:
3752 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3753 (long)wp->w_buffer->b_ml.ml_line_count);
3754 break;
3755
3756 case STL_ALTPERCENT:
3757 str = tmp;
3758 get_rel_pos(wp, str);
3759 break;
3760
3761 case STL_ARGLISTSTAT:
3762 fillable = FALSE;
3763 tmp[0] = 0;
3764 if (append_arg_number(wp, tmp, FALSE, (int)sizeof(tmp)))
3765 str = tmp;
3766 break;
3767
3768 case STL_KEYMAP:
3769 fillable = FALSE;
3770 if (get_keymap_str(wp, tmp, TMPLEN))
3771 str = tmp;
3772 break;
3773 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003774#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003775 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776#else
3777 num = 0;
3778#endif
3779 break;
3780
3781 case STL_BUFNO:
3782 num = wp->w_buffer->b_fnum;
3783 break;
3784
3785 case STL_OFFSET_X:
3786 base = 'X';
3787 case STL_OFFSET:
3788#ifdef FEAT_BYTEOFF
3789 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3790 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3791 0L : l + 1 + (!(State & INSERT) && empty_line ?
3792 0 : (int)wp->w_cursor.col);
3793#endif
3794 break;
3795
3796 case STL_BYTEVAL_X:
3797 base = 'X';
3798 case STL_BYTEVAL:
Bram Moolenaar49104e42007-03-15 21:54:01 +00003799 if (wp->w_cursor.col > STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 num = 0;
3801 else
3802 {
3803#ifdef FEAT_MBYTE
3804 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3805#else
3806 num = linecont[wp->w_cursor.col];
3807#endif
3808 }
3809 if (num == NL)
3810 num = 0;
3811 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3812 num = NL;
3813 break;
3814
3815 case STL_ROFLAG:
3816 case STL_ROFLAG_ALT:
3817 itemisflag = TRUE;
3818 if (wp->w_buffer->b_p_ro)
3819 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3820 break;
3821
3822 case STL_HELPFLAG:
3823 case STL_HELPFLAG_ALT:
3824 itemisflag = TRUE;
3825 if (wp->w_buffer->b_help)
3826 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003827 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 break;
3829
3830#ifdef FEAT_AUTOCMD
3831 case STL_FILETYPE:
3832 if (*wp->w_buffer->b_p_ft != NUL
3833 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3834 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003835 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3836 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 str = tmp;
3838 }
3839 break;
3840
3841 case STL_FILETYPE_ALT:
3842 itemisflag = TRUE;
3843 if (*wp->w_buffer->b_p_ft != NUL
3844 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3845 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003846 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3847 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 for (t = tmp; *t != 0; t++)
3849 *t = TOUPPER_LOC(*t);
3850 str = tmp;
3851 }
3852 break;
3853#endif
3854
3855#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3856 case STL_PREVIEWFLAG:
3857 case STL_PREVIEWFLAG_ALT:
3858 itemisflag = TRUE;
3859 if (wp->w_p_pvw)
3860 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3861 : _("[Preview]"));
3862 break;
3863#endif
3864
3865 case STL_MODIFIED:
3866 case STL_MODIFIED_ALT:
3867 itemisflag = TRUE;
3868 switch ((opt == STL_MODIFIED_ALT)
3869 + bufIsChanged(wp->w_buffer) * 2
3870 + (!wp->w_buffer->b_p_ma) * 4)
3871 {
3872 case 2: str = (char_u *)"[+]"; break;
3873 case 3: str = (char_u *)",+"; break;
3874 case 4: str = (char_u *)"[-]"; break;
3875 case 5: str = (char_u *)",-"; break;
3876 case 6: str = (char_u *)"[+-]"; break;
3877 case 7: str = (char_u *)",+-"; break;
3878 }
3879 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003880
3881 case STL_HIGHLIGHT:
3882 t = s;
3883 while (*s != '#' && *s != NUL)
3884 ++s;
3885 if (*s == '#')
3886 {
3887 item[curitem].type = Highlight;
3888 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003889 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003890 curitem++;
3891 }
3892 ++s;
3893 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
3895
3896 item[curitem].start = p;
3897 item[curitem].type = Normal;
3898 if (str != NULL && *str)
3899 {
3900 t = str;
3901 if (itemisflag)
3902 {
3903 if ((t[0] && t[1])
3904 && ((!prevchar_isitem && *t == ',')
3905 || (prevchar_isflag && *t == ' ')))
3906 t++;
3907 prevchar_isflag = TRUE;
3908 }
3909 l = vim_strsize(t);
3910 if (l > 0)
3911 prevchar_isitem = TRUE;
3912 if (l > maxwid)
3913 {
3914 while (l >= maxwid)
3915#ifdef FEAT_MBYTE
3916 if (has_mbyte)
3917 {
3918 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003919 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921 else
3922#endif
3923 l -= byte2cells(*t++);
3924 if (p + 1 >= out + outlen)
3925 break;
3926 *p++ = '<';
3927 }
3928 if (minwid > 0)
3929 {
3930 for (; l < minwid && p + 1 < out + outlen; l++)
3931 {
3932 /* Don't put a "-" in front of a digit. */
3933 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3934 *p++ = ' ';
3935 else
3936 *p++ = fillchar;
3937 }
3938 minwid = 0;
3939 }
3940 else
3941 minwid *= -1;
3942 while (*t && p + 1 < out + outlen)
3943 {
3944 *p++ = *t++;
3945 /* Change a space by fillchar, unless fillchar is '-' and a
3946 * digit follows. */
3947 if (fillable && p[-1] == ' '
3948 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3949 p[-1] = fillchar;
3950 }
3951 for (; l < minwid && p + 1 < out + outlen; l++)
3952 *p++ = fillchar;
3953 }
3954 else if (num >= 0)
3955 {
3956 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3957 char_u nstr[20];
3958
3959 if (p + 20 >= out + outlen)
3960 break; /* not sufficient space */
3961 prevchar_isitem = TRUE;
3962 t = nstr;
3963 if (opt == STL_VIRTCOL_ALT)
3964 {
3965 *t++ = '-';
3966 minwid--;
3967 }
3968 *t++ = '%';
3969 if (zeropad)
3970 *t++ = '0';
3971 *t++ = '*';
3972 *t++ = nbase == 16 ? base : (nbase == 8 ? 'o' : 'd');
3973 *t = 0;
3974
3975 for (n = num, l = 1; n >= nbase; n /= nbase)
3976 l++;
3977 if (opt == STL_VIRTCOL_ALT)
3978 l++;
3979 if (l > maxwid)
3980 {
3981 l += 2;
3982 n = l - maxwid;
3983 while (l-- > maxwid)
3984 num /= nbase;
3985 *t++ = '>';
3986 *t++ = '%';
3987 *t = t[-3];
3988 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003989 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3990 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 }
3992 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003993 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3994 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 p += STRLEN(p);
3996 }
3997 else
3998 item[curitem].type = Empty;
3999
4000 if (opt == STL_VIM_EXPR)
4001 vim_free(str);
4002
4003 if (num >= 0 || (!itemisflag && str && *str))
4004 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4005 curitem++;
4006 }
4007 *p = NUL;
4008 itemcnt = curitem;
4009
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004010#ifdef FEAT_EVAL
4011 if (usefmt != fmt)
4012 vim_free(usefmt);
4013#endif
4014
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 width = vim_strsize(out);
4016 if (maxwidth > 0 && width > maxwidth)
4017 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004018 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 l = 0;
4020 if (itemcnt == 0)
4021 s = out;
4022 else
4023 {
4024 for ( ; l < itemcnt; l++)
4025 if (item[l].type == Trunc)
4026 {
4027 /* Truncate at %< item. */
4028 s = item[l].start;
4029 break;
4030 }
4031 if (l == itemcnt)
4032 {
4033 /* No %< item, truncate first item. */
4034 s = item[0].start;
4035 l = 0;
4036 }
4037 }
4038
4039 if (width - vim_strsize(s) >= maxwidth)
4040 {
4041 /* Truncation mark is beyond max length */
4042#ifdef FEAT_MBYTE
4043 if (has_mbyte)
4044 {
4045 s = out;
4046 width = 0;
4047 for (;;)
4048 {
4049 width += ptr2cells(s);
4050 if (width >= maxwidth)
4051 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004052 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 }
4054 /* Fill up for half a double-wide character. */
4055 while (++width < maxwidth)
4056 *s++ = fillchar;
4057 }
4058 else
4059#endif
4060 s = out + maxwidth - 1;
4061 for (l = 0; l < itemcnt; l++)
4062 if (item[l].start > s)
4063 break;
4064 itemcnt = l;
4065 *s++ = '>';
4066 *s = 0;
4067 }
4068 else
4069 {
4070#ifdef FEAT_MBYTE
4071 if (has_mbyte)
4072 {
4073 n = 0;
4074 while (width >= maxwidth)
4075 {
4076 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004077 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079 }
4080 else
4081#endif
4082 n = width - maxwidth + 1;
4083 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004084 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 *s = '<';
4086
4087 /* Fill up for half a double-wide character. */
4088 while (++width < maxwidth)
4089 {
4090 s = s + STRLEN(s);
4091 *s++ = fillchar;
4092 *s = NUL;
4093 }
4094
4095 --n; /* count the '<' */
4096 for (; l < itemcnt; l++)
4097 {
4098 if (item[l].start - n >= s)
4099 item[l].start -= n;
4100 else
4101 item[l].start = s;
4102 }
4103 }
4104 width = maxwidth;
4105 }
4106 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4107 {
4108 /* Apply STL_MIDDLE if any */
4109 for (l = 0; l < itemcnt; l++)
4110 if (item[l].type == Middle)
4111 break;
4112 if (l < itemcnt)
4113 {
4114 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004115 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 for (s = item[l].start; s < p; s++)
4117 *s = fillchar;
4118 for (l++; l < itemcnt; l++)
4119 item[l].start += maxwidth - width;
4120 width = maxwidth;
4121 }
4122 }
4123
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004124 /* Store the info about highlighting. */
4125 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004127 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 for (l = 0; l < itemcnt; l++)
4129 {
4130 if (item[l].type == Highlight)
4131 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004132 sp->start = item[l].start;
4133 sp->userhl = item[l].minwid;
4134 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
4136 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004137 sp->start = NULL;
4138 sp->userhl = 0;
4139 }
4140
4141 /* Store the info about tab pages labels. */
4142 if (tabtab != NULL)
4143 {
4144 sp = tabtab;
4145 for (l = 0; l < itemcnt; l++)
4146 {
4147 if (item[l].type == TabPage)
4148 {
4149 sp->start = item[l].start;
4150 sp->userhl = item[l].minwid;
4151 sp++;
4152 }
4153 }
4154 sp->start = NULL;
4155 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 }
4157
4158 return width;
4159}
4160#endif /* FEAT_STL_OPT */
4161
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004162#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4163 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004165 * Get relative cursor position in window into "str[]", in the form 99%, using
4166 * "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 */
4168 void
4169get_rel_pos(wp, str)
4170 win_T *wp;
4171 char_u *str;
4172{
4173 long above; /* number of lines above window */
4174 long below; /* number of lines below window */
4175
4176 above = wp->w_topline - 1;
4177#ifdef FEAT_DIFF
4178 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4179#endif
4180 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4181 if (below <= 0)
4182 STRCPY(str, above == 0 ? _("All") : _("Bot"));
4183 else if (above <= 0)
4184 STRCPY(str, _("Top"));
4185 else
4186 sprintf((char *)str, "%2d%%", above > 1000000L
4187 ? (int)(above / ((above + below) / 100L))
4188 : (int)(above * 100L / (above + below)));
4189}
4190#endif
4191
4192/*
4193 * Append (file 2 of 8) to 'buf', if editing more than one file.
4194 * Return TRUE if it was appended.
4195 */
4196 int
4197append_arg_number(wp, buf, add_file, maxlen)
4198 win_T *wp;
4199 char_u *buf;
4200 int add_file; /* Add "file" before the arg number */
4201 int maxlen; /* maximum nr of chars in buf or zero*/
4202{
4203 char_u *p;
4204
4205 if (ARGCOUNT <= 1) /* nothing to do */
4206 return FALSE;
4207
4208 p = buf + STRLEN(buf); /* go to the end of the buffer */
4209 if (maxlen && p - buf + 35 >= maxlen) /* getting too long */
4210 return FALSE;
4211 *p++ = ' ';
4212 *p++ = '(';
4213 if (add_file)
4214 {
4215 STRCPY(p, "file ");
4216 p += 5;
4217 }
4218 sprintf((char *)p, wp->w_arg_idx_invalid ? "(%d) of %d)"
4219 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4220 return TRUE;
4221}
4222
4223/*
4224 * If fname is not a full path, make it a full path.
4225 * Returns pointer to allocated memory (NULL for failure).
4226 */
4227 char_u *
4228fix_fname(fname)
4229 char_u *fname;
4230{
4231 /*
4232 * Force expanding the path always for Unix, because symbolic links may
4233 * mess up the full path name, even though it starts with a '/'.
4234 * Also expand when there is ".." in the file name, try to remove it,
4235 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004236 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 * For MS-Windows also expand names like "longna~1" to "longname".
4238 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004239#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 return FullName_save(fname, TRUE);
4241#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004242 if (!vim_isAbsName(fname)
4243 || strstr((char *)fname, "..") != NULL
4244 || strstr((char *)fname, "//") != NULL
4245# ifdef BACKSLASH_IN_FILENAME
4246 || strstr((char *)fname, "\\\\") != NULL
4247# endif
4248# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004250# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 )
4252 return FullName_save(fname, FALSE);
4253
4254 fname = vim_strsave(fname);
4255
Bram Moolenaar9b942202007-10-03 12:31:33 +00004256# ifdef USE_FNAME_CASE
4257# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 {
4261 if (fname != NULL)
4262 fname_case(fname, 0); /* set correct case for file name */
4263 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265
4266 return fname;
4267#endif
4268}
4269
4270/*
4271 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4272 * "ffname" becomes a pointer to allocated memory (or NULL).
4273 */
4274/*ARGSUSED*/
4275 void
4276fname_expand(buf, ffname, sfname)
4277 buf_T *buf;
4278 char_u **ffname;
4279 char_u **sfname;
4280{
4281 if (*ffname == NULL) /* if no file name given, nothing to do */
4282 return;
4283 if (*sfname == NULL) /* if no short file name given, use ffname */
4284 *sfname = *ffname;
4285 *ffname = fix_fname(*ffname); /* expand to full path */
4286
4287#ifdef FEAT_SHORTCUT
4288 if (!buf->b_p_bin)
4289 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004290 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
4292 /* If the file name is a shortcut file, use the file it links to. */
4293 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004294 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {
4296 vim_free(*ffname);
4297 *ffname = rfname;
4298 *sfname = rfname;
4299 }
4300 }
4301#endif
4302}
4303
4304/*
4305 * Get the file name for an argument list entry.
4306 */
4307 char_u *
4308alist_name(aep)
4309 aentry_T *aep;
4310{
4311 buf_T *bp;
4312
4313 /* Use the name from the associated buffer if it exists. */
4314 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004315 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 return aep->ae_fname;
4317 return bp->b_fname;
4318}
4319
4320#if defined(FEAT_WINDOWS) || defined(PROTO)
4321/*
4322 * do_arg_all(): Open up to 'count' windows, one for each argument.
4323 */
4324 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004325do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 int count;
4327 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004328 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329{
4330 int i;
4331 win_T *wp, *wpnext;
4332 char_u *opened; /* array of flags for which args are open */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004333 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 int use_firstwin = FALSE; /* use first window for arglist */
4335 int split_ret = OK;
4336 int p_ea_save;
4337 alist_T *alist; /* argument list to be used */
4338 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004339 tabpage_T *tpnext;
4340 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004341 win_T *new_curwin = NULL;
4342 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343
4344 if (ARGCOUNT <= 0)
4345 {
4346 /* Don't give an error message. We don't want it when the ":all"
4347 * command is in the .vimrc. */
4348 return;
4349 }
4350 setpcmark();
4351
4352 opened_len = ARGCOUNT;
4353 opened = alloc_clear((unsigned)opened_len);
4354 if (opened == NULL)
4355 return;
4356
4357#ifdef FEAT_GUI
4358 need_mouse_correct = TRUE;
4359#endif
4360
4361 /*
4362 * Try closing all windows that are not in the argument list.
4363 * Also close windows that are not full width;
4364 * When 'hidden' or "forceit" set the buffer becomes hidden.
4365 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004366 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004368 if (had_tab > 0)
4369 goto_tabpage_tp(first_tabpage);
4370 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004372 tpnext = curtab->tp_next;
4373 for (wp = firstwin; wp != NULL; wp = wpnext)
4374 {
4375 wpnext = wp->w_next;
4376 buf = wp->w_buffer;
4377 if (buf->b_ffname == NULL
4378 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004380 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004382 )
4383 i = ARGCOUNT;
4384 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004386 /* check if the buffer in this window is in the arglist */
4387 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004389 if (ARGLIST[i].ae_fnum == buf->b_fnum
4390 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004391 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004393 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004394 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004395 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004396 if (i == 0)
4397 {
4398 new_curwin = wp;
4399 new_curtab = curtab;
4400 }
4401 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004402 if (wp->w_alist != curwin->w_alist)
4403 {
4404 /* Use the current argument list for all windows
4405 * containing a file from it. */
4406 alist_unlink(wp->w_alist);
4407 wp->w_alist = curwin->w_alist;
4408 ++wp->w_alist->al_refcount;
4409 }
4410 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
4413 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004414 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004416 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004418 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004419 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004421 /* If the buffer was changed, and we would like to hide it,
4422 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004423 if (!P_HID(buf) && buf->b_nwindows <= 1
4424 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004426 (void)autowrite(buf, FALSE);
4427#ifdef FEAT_AUTOCMD
4428 /* check if autocommands removed the window */
4429 if (!win_valid(wp) || !buf_valid(buf))
4430 {
4431 wpnext = firstwin; /* start all over... */
4432 continue;
4433 }
4434#endif
4435 }
4436#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004437 /* don't close last window */
4438 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004439#endif
4440 use_firstwin = TRUE;
4441#ifdef FEAT_WINDOWS
4442 else
4443 {
4444 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4445# ifdef FEAT_AUTOCMD
4446 /* check if autocommands removed the next window */
4447 if (!win_valid(wpnext))
4448 wpnext = firstwin; /* start all over... */
4449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 }
4451#endif
4452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 }
4454 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004455
4456 /* Without the ":tab" modifier only do the current tab page. */
4457 if (had_tab == 0 || tpnext == NULL)
4458 break;
4459
4460# ifdef FEAT_AUTOCMD
4461 /* check if autocommands removed the next tab page */
4462 if (!valid_tabpage(tpnext))
4463 tpnext = first_tabpage; /* start all over...*/
4464# endif
4465 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 }
4467
4468 /*
4469 * Open a window for files in the argument list that don't have one.
4470 * ARGCOUNT may change while doing this, because of autocommands.
4471 */
4472 if (count > ARGCOUNT || count <= 0)
4473 count = ARGCOUNT;
4474
4475 /* Autocommands may do anything to the argument list. Make sure it's not
4476 * freed while we are working here by "locking" it. We still have to
4477 * watch out for its size to be changed. */
4478 alist = curwin->w_alist;
4479 ++alist->al_refcount;
4480
4481#ifdef FEAT_AUTOCMD
4482 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4483 ++autocmd_no_enter;
4484 ++autocmd_no_leave;
4485#endif
4486 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004487#ifdef FEAT_WINDOWS
4488 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4489 * leaving an empty tab page when executed locally. */
4490 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4491 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4492 use_firstwin = TRUE;
4493#endif
4494
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4496 {
4497 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4498 arg_had_last = TRUE;
4499 if (i < opened_len && opened[i])
4500 {
4501 /* Move the already present window to below the current window */
4502 if (curwin->w_arg_idx != i)
4503 {
4504 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4505 {
4506 if (wpnext->w_arg_idx == i)
4507 {
4508 win_move_after(wpnext, curwin);
4509 break;
4510 }
4511 }
4512 }
4513 }
4514 else if (split_ret == OK)
4515 {
4516 if (!use_firstwin) /* split current window */
4517 {
4518 p_ea_save = p_ea;
4519 p_ea = TRUE; /* use space from all windows */
4520 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4521 p_ea = p_ea_save;
4522 if (split_ret == FAIL)
4523 continue;
4524 }
4525#ifdef FEAT_AUTOCMD
4526 else /* first window: do autocmd for leaving this buffer */
4527 --autocmd_no_leave;
4528#endif
4529
4530 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004531 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 */
4533 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004534 if (i == 0)
4535 {
4536 new_curwin = curwin;
4537 new_curtab = curtab;
4538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4540 ECMD_ONE,
4541 ((P_HID(curwin->w_buffer)
4542 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004543 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544#ifdef FEAT_AUTOCMD
4545 if (use_firstwin)
4546 ++autocmd_no_leave;
4547#endif
4548 use_firstwin = FALSE;
4549 }
4550 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004551
4552 /* When ":tab" was used open a new tab for a new window repeatedly. */
4553 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4554 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 }
4556
4557 /* Remove the "lock" on the argument list. */
4558 alist_unlink(alist);
4559
4560#ifdef FEAT_AUTOCMD
4561 --autocmd_no_enter;
4562#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004563 /* to window with first arg */
4564 if (valid_tabpage(new_curtab))
4565 goto_tabpage_tp(new_curtab);
4566 if (win_valid(new_curwin))
4567 win_enter(new_curwin, FALSE);
4568
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569#ifdef FEAT_AUTOCMD
4570 --autocmd_no_leave;
4571#endif
4572 vim_free(opened);
4573}
4574
4575# if defined(FEAT_LISTCMDS) || defined(PROTO)
4576/*
4577 * Open a window for a number of buffers.
4578 */
4579 void
4580ex_buffer_all(eap)
4581 exarg_T *eap;
4582{
4583 buf_T *buf;
4584 win_T *wp, *wpnext;
4585 int split_ret = OK;
4586 int p_ea_save;
4587 int open_wins = 0;
4588 int r;
4589 int count; /* Maximum number of windows to open. */
4590 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004591#ifdef FEAT_WINDOWS
4592 int had_tab = cmdmod.tab;
4593 tabpage_T *tpnext;
4594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595
4596 if (eap->addr_count == 0) /* make as many windows as possible */
4597 count = 9999;
4598 else
4599 count = eap->line2; /* make as many windows as specified */
4600 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4601 all = FALSE;
4602 else
4603 all = TRUE;
4604
4605 setpcmark();
4606
4607#ifdef FEAT_GUI
4608 need_mouse_correct = TRUE;
4609#endif
4610
4611 /*
4612 * Close superfluous windows (two windows for the same buffer).
4613 * Also close windows that are not full-width.
4614 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004615#ifdef FEAT_WINDOWS
4616 if (had_tab > 0)
4617 goto_tabpage_tp(first_tabpage);
4618 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004621 tpnext = curtab->tp_next;
4622 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004624 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004625 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004626#ifdef FEAT_VERTSPLIT
4627 || ((cmdmod.split & WSP_VERT)
4628 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004629 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004630 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004632#ifdef FEAT_WINDOWS
4633 || (had_tab > 0 && wp != firstwin)
4634#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004635 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004636 {
4637 win_close(wp, FALSE);
4638#ifdef FEAT_AUTOCMD
4639 wpnext = firstwin; /* just in case an autocommand does
4640 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004641 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004642 open_wins = 0;
4643#endif
4644 }
4645 else
4646 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004648
4649#ifdef FEAT_WINDOWS
4650 /* Without the ":tab" modifier only do the current tab page. */
4651 if (had_tab == 0 || tpnext == NULL)
4652 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004653 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656
4657 /*
4658 * Go through the buffer list. When a buffer doesn't have a window yet,
4659 * open one. Otherwise move the window to the right position.
4660 * Watch out for autocommands that delete buffers or windows!
4661 */
4662#ifdef FEAT_AUTOCMD
4663 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4664 ++autocmd_no_enter;
4665#endif
4666 win_enter(lastwin, FALSE);
4667#ifdef FEAT_AUTOCMD
4668 ++autocmd_no_leave;
4669#endif
4670 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4671 {
4672 /* Check if this buffer needs a window */
4673 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4674 continue;
4675
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004676#ifdef FEAT_WINDOWS
4677 if (had_tab != 0)
4678 {
4679 /* With the ":tab" modifier don't move the window. */
4680 if (buf->b_nwindows > 0)
4681 wp = lastwin; /* buffer has a window, skip it */
4682 else
4683 wp = NULL;
4684 }
4685 else
4686#endif
4687 {
4688 /* Check if this buffer already has a window */
4689 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4690 if (wp->w_buffer == buf)
4691 break;
4692 /* If the buffer already has a window, move it */
4693 if (wp != NULL)
4694 win_move_after(wp, curwin);
4695 }
4696
4697 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
4699 /* Split the window and put the buffer in it */
4700 p_ea_save = p_ea;
4701 p_ea = TRUE; /* use space from all windows */
4702 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4703 ++open_wins;
4704 p_ea = p_ea_save;
4705 if (split_ret == FAIL)
4706 continue;
4707
4708 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004709#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 swap_exists_action = SEA_DIALOG;
4711#endif
4712 set_curbuf(buf, DOBUF_GOTO);
4713#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004716#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 break;
4720 }
4721#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004722#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 if (swap_exists_action == SEA_QUIT)
4724 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004725# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4726 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004727
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004728 /* Reset the error/interrupt/exception state here so that
4729 * aborting() returns FALSE when closing a window. */
4730 enter_cleanup(&cs);
4731# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004732
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004733 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 win_close(curwin, TRUE);
4735 --open_wins;
4736 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004737 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004738
4739# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4740 /* Restore the error/interrupt/exception state if not
4741 * discarded by a new aborting error, interrupt, or uncaught
4742 * exception. */
4743 leave_cleanup(&cs);
4744# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 }
4746 else
4747 handle_swap_exists(NULL);
4748#endif
4749 }
4750
4751 ui_breakcheck();
4752 if (got_int)
4753 {
4754 (void)vgetc(); /* only break the file loading, not the rest */
4755 break;
4756 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004757#ifdef FEAT_EVAL
4758 /* Autocommands deleted the buffer or aborted script processing!!! */
4759 if (aborting())
4760 break;
4761#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004762#ifdef FEAT_WINDOWS
4763 /* When ":tab" was used open a new tab for a new window repeatedly. */
4764 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4765 cmdmod.tab = 9999;
4766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
4768#ifdef FEAT_AUTOCMD
4769 --autocmd_no_enter;
4770#endif
4771 win_enter(firstwin, FALSE); /* back to first window */
4772#ifdef FEAT_AUTOCMD
4773 --autocmd_no_leave;
4774#endif
4775
4776 /*
4777 * Close superfluous windows.
4778 */
4779 for (wp = lastwin; open_wins > count; )
4780 {
4781 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4782 || autowrite(wp->w_buffer, FALSE) == OK);
4783#ifdef FEAT_AUTOCMD
4784 if (!win_valid(wp))
4785 {
4786 /* BufWrite Autocommands made the window invalid, start over */
4787 wp = lastwin;
4788 }
4789 else
4790#endif
4791 if (r)
4792 {
4793 win_close(wp, !P_HID(wp->w_buffer));
4794 --open_wins;
4795 wp = lastwin;
4796 }
4797 else
4798 {
4799 wp = wp->w_prev;
4800 if (wp == NULL)
4801 break;
4802 }
4803 }
4804}
4805# endif /* FEAT_LISTCMDS */
4806
4807#endif /* FEAT_WINDOWS */
4808
Bram Moolenaara3227e22006-03-08 21:32:40 +00004809static int chk_modeline __ARGS((linenr_T, int));
4810
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811/*
4812 * do_modelines() - process mode lines for the current file
4813 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004814 * "flags" can be:
4815 * OPT_WINONLY only set options local to window
4816 * OPT_NOWIN don't set options local to window
4817 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 * Returns immediately if the "ml" option isn't set.
4819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004821do_modelines(flags)
4822 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004824 linenr_T lnum;
4825 int nmlines;
4826 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827
4828 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4829 return;
4830
4831 /* Disallow recursive entry here. Can happen when executing a modeline
4832 * triggers an autocommand, which reloads modelines with a ":do". */
4833 if (entered)
4834 return;
4835
4836 ++entered;
4837 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4838 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004839 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 nmlines = 0;
4841
4842 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4843 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004844 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 nmlines = 0;
4846 --entered;
4847}
4848
4849#include "version.h" /* for version number */
4850
4851/*
4852 * chk_modeline() - check a single line for a mode string
4853 * Return FAIL if an error encountered.
4854 */
4855 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004856chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004858 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859{
4860 char_u *s;
4861 char_u *e;
4862 char_u *linecopy; /* local copy of any modeline found */
4863 int prev;
4864 int vers;
4865 int end;
4866 int retval = OK;
4867 char_u *save_sourcing_name;
4868 linenr_T save_sourcing_lnum;
4869#ifdef FEAT_EVAL
4870 scid_T save_SID;
4871#endif
4872
4873 prev = -1;
4874 for (s = ml_get(lnum); *s != NUL; ++s)
4875 {
4876 if (prev == -1 || vim_isspace(prev))
4877 {
4878 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4879 || STRNCMP(s, "vi:", (size_t)3) == 0)
4880 break;
4881 if (STRNCMP(s, "vim", 3) == 0)
4882 {
4883 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4884 e = s + 4;
4885 else
4886 e = s + 3;
4887 vers = getdigits(&e);
4888 if (*e == ':'
4889 && (s[3] == ':'
4890 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4891 || (VIM_VERSION_100 < vers && s[3] == '<')
4892 || (VIM_VERSION_100 > vers && s[3] == '>')
4893 || (VIM_VERSION_100 == vers && s[3] == '=')))
4894 break;
4895 }
4896 }
4897 prev = *s;
4898 }
4899
4900 if (*s)
4901 {
4902 do /* skip over "ex:", "vi:" or "vim:" */
4903 ++s;
4904 while (s[-1] != ':');
4905
4906 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4907 if (linecopy == NULL)
4908 return FAIL;
4909
4910 save_sourcing_lnum = sourcing_lnum;
4911 save_sourcing_name = sourcing_name;
4912 sourcing_lnum = lnum; /* prepare for emsg() */
4913 sourcing_name = (char_u *)"modelines";
4914
4915 end = FALSE;
4916 while (end == FALSE)
4917 {
4918 s = skipwhite(s);
4919 if (*s == NUL)
4920 break;
4921
4922 /*
4923 * Find end of set command: ':' or end of line.
4924 * Skip over "\:", replacing it with ":".
4925 */
4926 for (e = s; *e != ':' && *e != NUL; ++e)
4927 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00004928 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 if (*e == NUL)
4930 end = TRUE;
4931
4932 /*
4933 * If there is a "set" command, require a terminating ':' and
4934 * ignore the stuff after the ':'.
4935 * "vi:set opt opt opt: foo" -- foo not interpreted
4936 * "vi:opt opt opt: foo" -- foo interpreted
4937 * Accept "se" for compatibility with Elvis.
4938 */
4939 if (STRNCMP(s, "set ", (size_t)4) == 0
4940 || STRNCMP(s, "se ", (size_t)3) == 0)
4941 {
4942 if (*e != ':') /* no terminating ':'? */
4943 break;
4944 end = TRUE;
4945 s = vim_strchr(s, ' ') + 1;
4946 }
4947 *e = NUL; /* truncate the set command */
4948
4949 if (*s != NUL) /* skip over an empty "::" */
4950 {
4951#ifdef FEAT_EVAL
4952 save_SID = current_SID;
4953 current_SID = SID_MODELINE;
4954#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004955 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956#ifdef FEAT_EVAL
4957 current_SID = save_SID;
4958#endif
4959 if (retval == FAIL) /* stop if error found */
4960 break;
4961 }
4962 s = e + 1; /* advance to next part */
4963 }
4964
4965 sourcing_lnum = save_sourcing_lnum;
4966 sourcing_name = save_sourcing_name;
4967
4968 vim_free(linecopy);
4969 }
4970 return retval;
4971}
4972
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00004973#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 int
4975read_viminfo_bufferlist(virp, writing)
4976 vir_T *virp;
4977 int writing;
4978{
4979 char_u *tab;
4980 linenr_T lnum;
4981 colnr_T col;
4982 buf_T *buf;
4983 char_u *sfname;
4984 char_u *xline;
4985
4986 /* Handle long line and escaped characters. */
4987 xline = viminfo_readstring(virp, 1, FALSE);
4988
4989 /* don't read in if there are files on the command-line or if writing: */
4990 if (xline != NULL && !writing && ARGCOUNT == 0
4991 && find_viminfo_parameter('%') != NULL)
4992 {
4993 /* Format is: <fname> Tab <lnum> Tab <col>.
4994 * Watch out for a Tab in the file name, work from the end. */
4995 lnum = 0;
4996 col = 0;
4997 tab = vim_strrchr(xline, '\t');
4998 if (tab != NULL)
4999 {
5000 *tab++ = '\0';
5001 col = atoi((char *)tab);
5002 tab = vim_strrchr(xline, '\t');
5003 if (tab != NULL)
5004 {
5005 *tab++ = '\0';
5006 lnum = atol((char *)tab);
5007 }
5008 }
5009
5010 /* Expand "~/" in the file name at "line + 1" to a full path.
5011 * Then try shortening it by comparing with the current directory */
5012 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005013 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014
5015 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5016 if (buf != NULL) /* just in case... */
5017 {
5018 buf->b_last_cursor.lnum = lnum;
5019 buf->b_last_cursor.col = col;
5020 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5021 }
5022 }
5023 vim_free(xline);
5024
5025 return viminfo_readline(virp);
5026}
5027
5028 void
5029write_viminfo_bufferlist(fp)
5030 FILE *fp;
5031{
5032 buf_T *buf;
5033#ifdef FEAT_WINDOWS
5034 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005035 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036#endif
5037 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005038 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039
5040 if (find_viminfo_parameter('%') == NULL)
5041 return;
5042
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005043 /* Without a number -1 is returned: do all buffers. */
5044 max_buffers = get_viminfo_parameter('%');
5045
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005047 line = alloc(MAXPATHL + 40);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 if (line == NULL)
5049 return;
5050
5051#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005052 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 set_last_cursor(win);
5054#else
5055 set_last_cursor(curwin);
5056#endif
5057
5058 fprintf(fp, _("\n# Buffer list:\n"));
5059 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5060 {
5061 if (buf->b_fname == NULL
5062 || !buf->b_p_bl
5063#ifdef FEAT_QUICKFIX
5064 || bt_quickfix(buf)
5065#endif
5066 || removable(buf->b_ffname))
5067 continue;
5068
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005069 if (max_buffers-- == 0)
5070 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 putc('%', fp);
5072 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
5073 sprintf((char *)line + STRLEN(line), "\t%ld\t%d",
5074 (long)buf->b_last_cursor.lnum,
5075 buf->b_last_cursor.col);
5076 viminfo_writestring(fp, line);
5077 }
5078 vim_free(line);
5079}
5080#endif
5081
5082
5083/*
5084 * Return special buffer name.
5085 * Returns NULL when the buffer has a normal file name.
5086 */
5087 char *
5088buf_spname(buf)
5089 buf_T *buf;
5090{
5091#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5092 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005093 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005094 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005095 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005096
5097 /*
5098 * For location list window, w_llist_ref points to the location list.
5099 * For quickfix window, w_llist_ref is NULL.
5100 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005101 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005102 if (win->w_buffer == buf)
5103 break;
5104 if (win != NULL && win->w_llist_ref != NULL)
5105 return _("[Location List]");
5106 else
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005107 return _("[Quickfix List]");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109#endif
5110#ifdef FEAT_QUICKFIX
5111 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5112 * contains the name as specified by the user */
5113 if (bt_nofile(buf))
5114 {
5115 if (buf->b_sfname != NULL)
5116 return (char *)buf->b_sfname;
5117 return "[Scratch]";
5118 }
5119#endif
5120 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005121 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 return NULL;
5123}
5124
5125
5126#if defined(FEAT_SIGNS) || defined(PROTO)
5127/*
5128 * Insert the sign into the signlist.
5129 */
5130 static void
5131insert_sign(buf, prev, next, id, lnum, typenr)
5132 buf_T *buf; /* buffer to store sign in */
5133 signlist_T *prev; /* previous sign entry */
5134 signlist_T *next; /* next sign entry */
5135 int id; /* sign ID */
5136 linenr_T lnum; /* line number which gets the mark */
5137 int typenr; /* typenr of sign we are adding */
5138{
5139 signlist_T *newsign;
5140
5141 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5142 if (newsign != NULL)
5143 {
5144 newsign->id = id;
5145 newsign->lnum = lnum;
5146 newsign->typenr = typenr;
5147 newsign->next = next;
5148#ifdef FEAT_NETBEANS_INTG
5149 newsign->prev = prev;
5150 if (next != NULL)
5151 next->prev = newsign;
5152#endif
5153
5154 if (prev == NULL)
5155 {
5156 /* When adding first sign need to redraw the windows to create the
5157 * column for signs. */
5158 if (buf->b_signlist == NULL)
5159 {
5160 redraw_buf_later(buf, NOT_VALID);
5161 changed_cline_bef_curs();
5162 }
5163
5164 /* first sign in signlist */
5165 buf->b_signlist = newsign;
5166 }
5167 else
5168 prev->next = newsign;
5169 }
5170}
5171
5172/*
5173 * Add the sign into the signlist. Find the right spot to do it though.
5174 */
5175 void
5176buf_addsign(buf, id, lnum, typenr)
5177 buf_T *buf; /* buffer to store sign in */
5178 int id; /* sign ID */
5179 linenr_T lnum; /* line number which gets the mark */
5180 int typenr; /* typenr of sign we are adding */
5181{
5182 signlist_T *sign; /* a sign in the signlist */
5183 signlist_T *prev; /* the previous sign */
5184
5185 prev = NULL;
5186 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5187 {
5188 if (lnum == sign->lnum && id == sign->id)
5189 {
5190 sign->typenr = typenr;
5191 return;
5192 }
5193 else if (
5194#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5195 id < 0 &&
5196#endif
5197 lnum < sign->lnum)
5198 {
5199#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5200 /* XXX - GRP: Is this because of sign slide problem? Or is it
5201 * really needed? Or is it because we allow multiple signs per
5202 * line? If so, should I add that feature to FEAT_SIGNS?
5203 */
5204 while (prev != NULL && prev->lnum == lnum)
5205 prev = prev->prev;
5206 if (prev == NULL)
5207 sign = buf->b_signlist;
5208 else
5209 sign = prev->next;
5210#endif
5211 insert_sign(buf, prev, sign, id, lnum, typenr);
5212 return;
5213 }
5214 prev = sign;
5215 }
5216#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5217 /* XXX - GRP: See previous comment */
5218 while (prev != NULL && prev->lnum == lnum)
5219 prev = prev->prev;
5220 if (prev == NULL)
5221 sign = buf->b_signlist;
5222 else
5223 sign = prev->next;
5224#endif
5225 insert_sign(buf, prev, sign, id, lnum, typenr);
5226
5227 return;
5228}
5229
5230 int
5231buf_change_sign_type(buf, markId, typenr)
5232 buf_T *buf; /* buffer to store sign in */
5233 int markId; /* sign ID */
5234 int typenr; /* typenr of sign we are adding */
5235{
5236 signlist_T *sign; /* a sign in the signlist */
5237
5238 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5239 {
5240 if (sign->id == markId)
5241 {
5242 sign->typenr = typenr;
5243 return sign->lnum;
5244 }
5245 }
5246
5247 return 0;
5248}
5249
5250 int_u
5251buf_getsigntype(buf, lnum, type)
5252 buf_T *buf;
5253 linenr_T lnum;
5254 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5255{
5256 signlist_T *sign; /* a sign in a b_signlist */
5257
5258 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5259 if (sign->lnum == lnum
5260 && (type == SIGN_ANY
5261# ifdef FEAT_SIGN_ICONS
5262 || (type == SIGN_ICON
5263 && sign_get_image(sign->typenr) != NULL)
5264# endif
5265 || (type == SIGN_TEXT
5266 && sign_get_text(sign->typenr) != NULL)
5267 || (type == SIGN_LINEHL
5268 && sign_get_attr(sign->typenr, TRUE) != 0)))
5269 return sign->typenr;
5270 return 0;
5271}
5272
5273
5274 linenr_T
5275buf_delsign(buf, id)
5276 buf_T *buf; /* buffer sign is stored in */
5277 int id; /* sign id */
5278{
5279 signlist_T **lastp; /* pointer to pointer to current sign */
5280 signlist_T *sign; /* a sign in a b_signlist */
5281 signlist_T *next; /* the next sign in a b_signlist */
5282 linenr_T lnum; /* line number whose sign was deleted */
5283
5284 lastp = &buf->b_signlist;
5285 lnum = 0;
5286 for (sign = buf->b_signlist; sign != NULL; sign = next)
5287 {
5288 next = sign->next;
5289 if (sign->id == id)
5290 {
5291 *lastp = next;
5292#ifdef FEAT_NETBEANS_INTG
5293 if (next != NULL)
5294 next->prev = sign->prev;
5295#endif
5296 lnum = sign->lnum;
5297 vim_free(sign);
5298 break;
5299 }
5300 else
5301 lastp = &sign->next;
5302 }
5303
5304 /* When deleted the last sign need to redraw the windows to remove the
5305 * sign column. */
5306 if (buf->b_signlist == NULL)
5307 {
5308 redraw_buf_later(buf, NOT_VALID);
5309 changed_cline_bef_curs();
5310 }
5311
5312 return lnum;
5313}
5314
5315
5316/*
5317 * Find the line number of the sign with the requested id. If the sign does
5318 * not exist, return 0 as the line number. This will still let the correct file
5319 * get loaded.
5320 */
5321 int
5322buf_findsign(buf, id)
5323 buf_T *buf; /* buffer to store sign in */
5324 int id; /* sign ID */
5325{
5326 signlist_T *sign; /* a sign in the signlist */
5327
5328 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5329 if (sign->id == id)
5330 return sign->lnum;
5331
5332 return 0;
5333}
5334
5335 int
5336buf_findsign_id(buf, lnum)
5337 buf_T *buf; /* buffer whose sign we are searching for */
5338 linenr_T lnum; /* line number of sign */
5339{
5340 signlist_T *sign; /* a sign in the signlist */
5341
5342 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5343 if (sign->lnum == lnum)
5344 return sign->id;
5345
5346 return 0;
5347}
5348
5349
5350# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5351/* see if a given type of sign exists on a specific line */
5352 int
5353buf_findsigntype_id(buf, lnum, typenr)
5354 buf_T *buf; /* buffer whose sign we are searching for */
5355 linenr_T lnum; /* line number of sign */
5356 int typenr; /* sign type number */
5357{
5358 signlist_T *sign; /* a sign in the signlist */
5359
5360 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5361 if (sign->lnum == lnum && sign->typenr == typenr)
5362 return sign->id;
5363
5364 return 0;
5365}
5366
5367
5368# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5369/* return the number of icons on the given line */
5370 int
5371buf_signcount(buf, lnum)
5372 buf_T *buf;
5373 linenr_T lnum;
5374{
5375 signlist_T *sign; /* a sign in the signlist */
5376 int count = 0;
5377
5378 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5379 if (sign->lnum == lnum)
5380 if (sign_get_image(sign->typenr) != NULL)
5381 count++;
5382
5383 return count;
5384}
5385# endif /* FEAT_SIGN_ICONS */
5386# endif /* FEAT_NETBEANS_INTG */
5387
5388
5389/*
5390 * Delete signs in buffer "buf".
5391 */
5392 static void
5393buf_delete_signs(buf)
5394 buf_T *buf;
5395{
5396 signlist_T *next;
5397
5398 while (buf->b_signlist != NULL)
5399 {
5400 next = buf->b_signlist->next;
5401 vim_free(buf->b_signlist);
5402 buf->b_signlist = next;
5403 }
5404}
5405
5406/*
5407 * Delete all signs in all buffers.
5408 */
5409 void
5410buf_delete_all_signs()
5411{
5412 buf_T *buf; /* buffer we are checking for signs */
5413
5414 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5415 if (buf->b_signlist != NULL)
5416 {
5417 /* Need to redraw the windows to remove the sign column. */
5418 redraw_buf_later(buf, NOT_VALID);
5419 buf_delete_signs(buf);
5420 }
5421}
5422
5423/*
5424 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5425 */
5426 void
5427sign_list_placed(rbuf)
5428 buf_T *rbuf;
5429{
5430 buf_T *buf;
5431 signlist_T *p;
5432 char lbuf[BUFSIZ];
5433
5434 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5435 msg_putchar('\n');
5436 if (rbuf == NULL)
5437 buf = firstbuf;
5438 else
5439 buf = rbuf;
5440 while (buf != NULL)
5441 {
5442 if (buf->b_signlist != NULL)
5443 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005444 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5446 msg_putchar('\n');
5447 }
5448 for (p = buf->b_signlist; p != NULL; p = p->next)
5449 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005450 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5452 MSG_PUTS(lbuf);
5453 msg_putchar('\n');
5454 }
5455 if (rbuf != NULL)
5456 break;
5457 buf = buf->b_next;
5458 }
5459}
5460
5461/*
5462 * Adjust a placed sign for inserted/deleted lines.
5463 */
5464 void
5465sign_mark_adjust(line1, line2, amount, amount_after)
5466 linenr_T line1;
5467 linenr_T line2;
5468 long amount;
5469 long amount_after;
5470{
5471 signlist_T *sign; /* a sign in a b_signlist */
5472
5473 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5474 {
5475 if (sign->lnum >= line1 && sign->lnum <= line2)
5476 {
5477 if (amount == MAXLNUM)
5478 sign->lnum = line1;
5479 else
5480 sign->lnum += amount;
5481 }
5482 else if (sign->lnum > line2)
5483 sign->lnum += amount_after;
5484 }
5485}
5486#endif /* FEAT_SIGNS */
5487
5488/*
5489 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5490 */
5491 void
5492set_buflisted(on)
5493 int on;
5494{
5495 if (on != curbuf->b_p_bl)
5496 {
5497 curbuf->b_p_bl = on;
5498#ifdef FEAT_AUTOCMD
5499 if (on)
5500 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5501 else
5502 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5503#endif
5504 }
5505}
5506
5507/*
5508 * Read the file for "buf" again and check if the contents changed.
5509 * Return TRUE if it changed or this could not be checked.
5510 */
5511 int
5512buf_contents_changed(buf)
5513 buf_T *buf;
5514{
5515 buf_T *newbuf;
5516 int differ = TRUE;
5517 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 exarg_T ea;
5520
5521 /* Allocate a buffer without putting it in the buffer list. */
5522 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5523 if (newbuf == NULL)
5524 return TRUE;
5525
5526 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5527 if (prep_exarg(&ea, buf) == FAIL)
5528 {
5529 wipe_buffer(newbuf, FALSE);
5530 return TRUE;
5531 }
5532
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 /* set curwin/curbuf to buf and save a few things */
5534 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
Bram Moolenaar4770d092006-01-12 23:22:24 +00005536 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 && readfile(buf->b_ffname, buf->b_fname,
5538 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5539 &ea, READ_NEW | READ_DUMMY) == OK)
5540 {
5541 /* compare the two files line by line */
5542 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5543 {
5544 differ = FALSE;
5545 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5546 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5547 {
5548 differ = TRUE;
5549 break;
5550 }
5551 }
5552 }
5553 vim_free(ea.cmd);
5554
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 /* restore curwin/curbuf and a few other things */
5556 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557
5558 if (curbuf != newbuf) /* safety check */
5559 wipe_buffer(newbuf, FALSE);
5560
5561 return differ;
5562}
5563
5564/*
5565 * Wipe out a buffer and decrement the last buffer number if it was used for
5566 * this buffer. Call this to wipe out a temp buffer that does not contain any
5567 * marks.
5568 */
5569/*ARGSUSED*/
5570 void
5571wipe_buffer(buf, aucmd)
5572 buf_T *buf;
5573 int aucmd; /* When TRUE trigger autocommands. */
5574{
5575 if (buf->b_fnum == top_file_num - 1)
5576 --top_file_num;
5577
5578#ifdef FEAT_AUTOCMD
5579 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005580 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581#endif
5582 close_buffer(NULL, buf, DOBUF_WIPE);
5583#ifdef FEAT_AUTOCMD
5584 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005585 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586#endif
5587}