blob: 173a0f80c1d2153a63ccc2b37214c3454bb3dd72 [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));
36static wininfo_T *find_wininfo __ARGS((buf_T *buf));
37#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
650}
651
652/*
653 * Free the b_wininfo list for buffer "buf".
654 */
655 static void
656clear_wininfo(buf)
657 buf_T *buf;
658{
659 wininfo_T *wip;
660
661 while (buf->b_wininfo != NULL)
662 {
663 wip = buf->b_wininfo;
664 buf->b_wininfo = wip->wi_next;
665 if (wip->wi_optset)
666 {
667 clear_winopt(&wip->wi_opt);
668#ifdef FEAT_FOLDING
669 deleteFoldRecurse(&wip->wi_folds);
670#endif
671 }
672 vim_free(wip);
673 }
674}
675
676#if defined(FEAT_LISTCMDS) || defined(PROTO)
677/*
678 * Go to another buffer. Handles the result of the ATTENTION dialog.
679 */
680 void
681goto_buffer(eap, start, dir, count)
682 exarg_T *eap;
683 int start;
684 int dir;
685 int count;
686{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000687# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 buf_T *old_curbuf = curbuf;
689
690 swap_exists_action = SEA_DIALOG;
691# endif
692 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
693 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000694# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
696 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000697# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
698 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000699
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000700 /* Reset the error/interrupt/exception state here so that
701 * aborting() returns FALSE when closing a window. */
702 enter_cleanup(&cs);
703# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000704
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000705 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 win_close(curwin, TRUE);
707 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000708 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000709
710# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
711 /* Restore the error/interrupt/exception state if not discarded by a
712 * new aborting error, interrupt, or uncaught exception. */
713 leave_cleanup(&cs);
714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 }
716 else
717 handle_swap_exists(old_curbuf);
718# endif
719}
720#endif
721
Bram Moolenaare64ac772005-12-07 20:54:59 +0000722#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723/*
724 * Handle the situation of swap_exists_action being set.
725 * It is allowed for "old_curbuf" to be NULL or invalid.
726 */
727 void
728handle_swap_exists(old_curbuf)
729 buf_T *old_curbuf;
730{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000731# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
732 cleanup_T cs;
733# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000734
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 if (swap_exists_action == SEA_QUIT)
736 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000737# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
738 /* Reset the error/interrupt/exception state here so that
739 * aborting() returns FALSE when closing a buffer. */
740 enter_cleanup(&cs);
741# endif
742
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 /* User selected Quit at ATTENTION prompt. Go back to previous
744 * buffer. If that buffer is gone or the same as the current one,
745 * open a new, empty buffer. */
746 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000747 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
749 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000750 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 if (old_curbuf != NULL)
752 enter_buffer(old_curbuf);
753 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000754
755# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
756 /* Restore the error/interrupt/exception state if not discarded by a
757 * new aborting error, interrupt, or uncaught exception. */
758 leave_cleanup(&cs);
759# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 }
761 else if (swap_exists_action == SEA_RECOVER)
762 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000763# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
764 /* Reset the error/interrupt/exception state here so that
765 * aborting() returns FALSE when closing a buffer. */
766 enter_cleanup(&cs);
767# endif
768
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 /* User selected Recover at ATTENTION prompt. */
770 msg_scroll = TRUE;
771 ml_recover();
772 MSG_PUTS("\n"); /* don't overwrite the last message */
773 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000774 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000775
776# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
777 /* Restore the error/interrupt/exception state if not discarded by a
778 * new aborting error, interrupt, or uncaught exception. */
779 leave_cleanup(&cs);
780# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 }
782 swap_exists_action = SEA_NONE;
783}
784#endif
785
786#if defined(FEAT_LISTCMDS) || defined(PROTO)
787/*
788 * do_bufdel() - delete or unload buffer(s)
789 *
790 * addr_count == 0: ":bdel" - delete current buffer
791 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
792 * buffer "end_bnr", then any other arguments.
793 * addr_count == 2: ":N,N bdel" - delete buffers in range
794 *
795 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
796 * DOBUF_DEL (":bdel")
797 *
798 * Returns error message or NULL
799 */
800 char_u *
801do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
802 int command;
803 char_u *arg; /* pointer to extra arguments */
804 int addr_count;
805 int start_bnr; /* first buffer number in a range */
806 int end_bnr; /* buffer nr or last buffer nr in a range */
807 int forceit;
808{
809 int do_current = 0; /* delete current buffer? */
810 int deleted = 0; /* number of buffers deleted */
811 char_u *errormsg = NULL; /* return value */
812 int bnr; /* buffer number */
813 char_u *p;
814
815#ifdef FEAT_NETBEANS_INTG
816 netbeansCloseFile = 1;
817#endif
818 if (addr_count == 0)
819 {
820 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
821 }
822 else
823 {
824 if (addr_count == 2)
825 {
826 if (*arg) /* both range and argument is not allowed */
827 return (char_u *)_(e_trailing);
828 bnr = start_bnr;
829 }
830 else /* addr_count == 1 */
831 bnr = end_bnr;
832
833 for ( ;!got_int; ui_breakcheck())
834 {
835 /*
836 * delete the current buffer last, otherwise when the
837 * current buffer is deleted, the next buffer becomes
838 * the current one and will be loaded, which may then
839 * also be deleted, etc.
840 */
841 if (bnr == curbuf->b_fnum)
842 do_current = bnr;
843 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
844 forceit) == OK)
845 ++deleted;
846
847 /*
848 * find next buffer number to delete/unload
849 */
850 if (addr_count == 2)
851 {
852 if (++bnr > end_bnr)
853 break;
854 }
855 else /* addr_count == 1 */
856 {
857 arg = skipwhite(arg);
858 if (*arg == NUL)
859 break;
860 if (!VIM_ISDIGIT(*arg))
861 {
862 p = skiptowhite_esc(arg);
863 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
864 if (bnr < 0) /* failed */
865 break;
866 arg = p;
867 }
868 else
869 bnr = getdigits(&arg);
870 }
871 }
872 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
873 FORWARD, do_current, forceit) == OK)
874 ++deleted;
875
876 if (deleted == 0)
877 {
878 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000879 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000881 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000883 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 errormsg = IObuff;
885 }
886 else if (deleted >= p_report)
887 {
888 if (command == DOBUF_UNLOAD)
889 {
890 if (deleted == 1)
891 MSG(_("1 buffer unloaded"));
892 else
893 smsg((char_u *)_("%d buffers unloaded"), deleted);
894 }
895 else if (command == DOBUF_DEL)
896 {
897 if (deleted == 1)
898 MSG(_("1 buffer deleted"));
899 else
900 smsg((char_u *)_("%d buffers deleted"), deleted);
901 }
902 else
903 {
904 if (deleted == 1)
905 MSG(_("1 buffer wiped out"));
906 else
907 smsg((char_u *)_("%d buffers wiped out"), deleted);
908 }
909 }
910 }
911
912#ifdef FEAT_NETBEANS_INTG
913 netbeansCloseFile = 0;
914#endif
915
916 return errormsg;
917}
918
919/*
920 * Implementation of the commands for the buffer list.
921 *
922 * action == DOBUF_GOTO go to specified buffer
923 * action == DOBUF_SPLIT split window and go to specified buffer
924 * action == DOBUF_UNLOAD unload specified buffer(s)
925 * action == DOBUF_DEL delete specified buffer(s) from buffer list
926 * action == DOBUF_WIPE delete specified buffer(s) really
927 *
928 * start == DOBUF_CURRENT go to "count" buffer from current buffer
929 * start == DOBUF_FIRST go to "count" buffer from first buffer
930 * start == DOBUF_LAST go to "count" buffer from last buffer
931 * start == DOBUF_MOD go to "count" modified buffer from current buffer
932 *
933 * Return FAIL or OK.
934 */
935 int
936do_buffer(action, start, dir, count, forceit)
937 int action;
938 int start;
939 int dir; /* FORWARD or BACKWARD */
940 int count; /* buffer number or number of buffers */
941 int forceit; /* TRUE for :...! */
942{
943 buf_T *buf;
944 buf_T *bp;
945 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
946 || action == DOBUF_WIPE);
947
948 switch (start)
949 {
950 case DOBUF_FIRST: buf = firstbuf; break;
951 case DOBUF_LAST: buf = lastbuf; break;
952 default: buf = curbuf; break;
953 }
954 if (start == DOBUF_MOD) /* find next modified buffer */
955 {
956 while (count-- > 0)
957 {
958 do
959 {
960 buf = buf->b_next;
961 if (buf == NULL)
962 buf = firstbuf;
963 }
964 while (buf != curbuf && !bufIsChanged(buf));
965 }
966 if (!bufIsChanged(buf))
967 {
968 EMSG(_("E84: No modified buffer found"));
969 return FAIL;
970 }
971 }
972 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
973 {
974 while (buf != NULL && buf->b_fnum != count)
975 buf = buf->b_next;
976 }
977 else
978 {
979 bp = NULL;
980 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
981 {
982 /* remember the buffer where we start, we come back there when all
983 * buffers are unlisted. */
984 if (bp == NULL)
985 bp = buf;
986 if (dir == FORWARD)
987 {
988 buf = buf->b_next;
989 if (buf == NULL)
990 buf = firstbuf;
991 }
992 else
993 {
994 buf = buf->b_prev;
995 if (buf == NULL)
996 buf = lastbuf;
997 }
998 /* don't count unlisted buffers */
999 if (unload || buf->b_p_bl)
1000 {
1001 --count;
1002 bp = NULL; /* use this buffer as new starting point */
1003 }
1004 if (bp == buf)
1005 {
1006 /* back where we started, didn't find anything. */
1007 EMSG(_("E85: There is no listed buffer"));
1008 return FAIL;
1009 }
1010 }
1011 }
1012
1013 if (buf == NULL) /* could not find it */
1014 {
1015 if (start == DOBUF_FIRST)
1016 {
1017 /* don't warn when deleting */
1018 if (!unload)
1019 EMSGN(_("E86: Buffer %ld does not exist"), count);
1020 }
1021 else if (dir == FORWARD)
1022 EMSG(_("E87: Cannot go beyond last buffer"));
1023 else
1024 EMSG(_("E88: Cannot go before first buffer"));
1025 return FAIL;
1026 }
1027
1028#ifdef FEAT_GUI
1029 need_mouse_correct = TRUE;
1030#endif
1031
1032#ifdef FEAT_LISTCMDS
1033 /*
1034 * delete buffer buf from memory and/or the list
1035 */
1036 if (unload)
1037 {
1038 int forward;
1039 int retval;
1040
1041 /* When unloading or deleting a buffer that's already unloaded and
1042 * unlisted: fail silently. */
1043 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1044 return FAIL;
1045
1046 if (!forceit && bufIsChanged(buf))
1047 {
1048#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1049 if ((p_confirm || cmdmod.confirm) && p_write)
1050 {
1051 dialog_changed(buf, FALSE);
1052# ifdef FEAT_AUTOCMD
1053 if (!buf_valid(buf))
1054 /* Autocommand deleted buffer, oops! It's not changed
1055 * now. */
1056 return FAIL;
1057# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001058 /* If it's still changed fail silently, the dialog already
1059 * mentioned why it fails. */
1060 if (bufIsChanged(buf))
1061 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064#endif
1065 {
1066 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1067 buf->b_fnum);
1068 return FAIL;
1069 }
1070 }
1071
1072 /*
1073 * If deleting the last (listed) buffer, make it empty.
1074 * The last (listed) buffer cannot be unloaded.
1075 */
1076 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1077 if (bp->b_p_bl && bp != buf)
1078 break;
1079 if (bp == NULL && buf == curbuf)
1080 {
1081 if (action == DOBUF_UNLOAD)
1082 {
1083 EMSG(_("E90: Cannot unload last buffer"));
1084 return FAIL;
1085 }
1086
1087 /* Close any other windows on this buffer, then make it empty. */
1088#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001089 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090#endif
1091 setpcmark();
1092 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1093 forceit ? ECMD_FORCEIT : 0);
1094
1095 /*
1096 * do_ecmd() may create a new buffer, then we have to delete
1097 * the old one. But do_ecmd() may have done that already, check
1098 * if the buffer still exists.
1099 */
1100 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1101 close_buffer(NULL, buf, action);
1102 return retval;
1103 }
1104
1105#ifdef FEAT_WINDOWS
1106 /*
1107 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001108 * (unless it's the only window). Repeat this so long as we end up in
1109 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001111 while (buf == curbuf
1112 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 win_close(curwin, FALSE);
1114#endif
1115
1116 /*
1117 * If the buffer to be deleted is not the current one, delete it here.
1118 */
1119 if (buf != curbuf)
1120 {
1121#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001122 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#endif
1124 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1125 close_buffer(NULL, buf, action);
1126 return OK;
1127 }
1128
1129 /*
1130 * Deleting the current buffer: Need to find another buffer to go to.
1131 * There must be another, otherwise it would have been handled above.
1132 * First use au_new_curbuf, if it is valid.
1133 * Then prefer the buffer we most recently visited.
1134 * Else try to find one that is loaded, after the current buffer,
1135 * then before the current buffer.
1136 * Finally use any buffer.
1137 */
1138 buf = NULL; /* selected buffer */
1139 bp = NULL; /* used when no loaded buffer found */
1140#ifdef FEAT_AUTOCMD
1141 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1142 buf = au_new_curbuf;
1143# ifdef FEAT_JUMPLIST
1144 else
1145# endif
1146#endif
1147#ifdef FEAT_JUMPLIST
1148 if (curwin->w_jumplistlen > 0)
1149 {
1150 int jumpidx;
1151
1152 jumpidx = curwin->w_jumplistidx - 1;
1153 if (jumpidx < 0)
1154 jumpidx = curwin->w_jumplistlen - 1;
1155
1156 forward = jumpidx;
1157 while (jumpidx != curwin->w_jumplistidx)
1158 {
1159 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1160 if (buf != NULL)
1161 {
1162 if (buf == curbuf || !buf->b_p_bl)
1163 buf = NULL; /* skip current and unlisted bufs */
1164 else if (buf->b_ml.ml_mfp == NULL)
1165 {
1166 /* skip unloaded buf, but may keep it for later */
1167 if (bp == NULL)
1168 bp = buf;
1169 buf = NULL;
1170 }
1171 }
1172 if (buf != NULL) /* found a valid buffer: stop searching */
1173 break;
1174 /* advance to older entry in jump list */
1175 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1176 break;
1177 if (--jumpidx < 0)
1178 jumpidx = curwin->w_jumplistlen - 1;
1179 if (jumpidx == forward) /* List exhausted for sure */
1180 break;
1181 }
1182 }
1183#endif
1184
1185 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1186 {
1187 forward = TRUE;
1188 buf = curbuf->b_next;
1189 for (;;)
1190 {
1191 if (buf == NULL)
1192 {
1193 if (!forward) /* tried both directions */
1194 break;
1195 buf = curbuf->b_prev;
1196 forward = FALSE;
1197 continue;
1198 }
1199 /* in non-help buffer, try to skip help buffers, and vv */
1200 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1201 {
1202 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1203 break;
1204 if (bp == NULL) /* remember unloaded buf for later */
1205 bp = buf;
1206 }
1207 if (forward)
1208 buf = buf->b_next;
1209 else
1210 buf = buf->b_prev;
1211 }
1212 }
1213 if (buf == NULL) /* No loaded buffer, use unloaded one */
1214 buf = bp;
1215 if (buf == NULL) /* No loaded buffer, find listed one */
1216 {
1217 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1218 if (buf->b_p_bl && buf != curbuf)
1219 break;
1220 }
1221 if (buf == NULL) /* Still no buffer, just take one */
1222 {
1223 if (curbuf->b_next != NULL)
1224 buf = curbuf->b_next;
1225 else
1226 buf = curbuf->b_prev;
1227 }
1228 }
1229
1230 /*
1231 * make buf current buffer
1232 */
1233 if (action == DOBUF_SPLIT) /* split window first */
1234 {
1235# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001236 /* If 'switchbuf' contains "useopen": jump to first window containing
1237 * "buf" if one exists */
1238 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001239 return OK;
Bram Moolenaarf2330482008-06-24 20:19:36 +00001240 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1241 * page containing "buf" if one exists */
1242 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 return OK;
1244 if (win_split(0, 0) == FAIL)
1245# endif
1246 return FAIL;
1247 }
1248#endif
1249
1250 /* go to current buffer - nothing to do */
1251 if (buf == curbuf)
1252 return OK;
1253
1254 /*
1255 * Check if the current buffer may be abandoned.
1256 */
1257 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1258 {
1259#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1260 if ((p_confirm || cmdmod.confirm) && p_write)
1261 {
1262 dialog_changed(curbuf, FALSE);
1263# ifdef FEAT_AUTOCMD
1264 if (!buf_valid(buf))
1265 /* Autocommand deleted buffer, oops! */
1266 return FAIL;
1267# endif
1268 }
1269 if (bufIsChanged(curbuf))
1270#endif
1271 {
1272 EMSG(_(e_nowrtmsg));
1273 return FAIL;
1274 }
1275 }
1276
1277 /* Go to the other buffer. */
1278 set_curbuf(buf, action);
1279
1280#if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1281 if (action == DOBUF_SPLIT)
1282 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1283#endif
1284
1285#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1286 if (aborting()) /* autocmds may abort script processing */
1287 return FAIL;
1288#endif
1289
1290 return OK;
1291}
1292
1293#endif /* FEAT_LISTCMDS */
1294
1295/*
1296 * Set current buffer to "buf". Executes autocommands and closes current
1297 * buffer. "action" tells how to close the current buffer:
1298 * DOBUF_GOTO free or hide it
1299 * DOBUF_SPLIT nothing
1300 * DOBUF_UNLOAD unload it
1301 * DOBUF_DEL delete it
1302 * DOBUF_WIPE wipe it out
1303 */
1304 void
1305set_curbuf(buf, action)
1306 buf_T *buf;
1307 int action;
1308{
1309 buf_T *prevbuf;
1310 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1311 || action == DOBUF_WIPE);
1312
1313 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001314 if (!cmdmod.keepalt)
1315 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 buflist_altfpos(); /* remember curpos */
1317
1318#ifdef FEAT_VISUAL
1319 /* Don't restart Select mode after switching to another buffer. */
1320 VIsual_reselect = FALSE;
1321#endif
1322
1323 /* close_windows() or apply_autocmds() may change curbuf */
1324 prevbuf = curbuf;
1325
1326#ifdef FEAT_AUTOCMD
1327 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1328# ifdef FEAT_EVAL
1329 if (buf_valid(prevbuf) && !aborting())
1330# else
1331 if (buf_valid(prevbuf))
1332# endif
1333#endif
1334 {
1335#ifdef FEAT_WINDOWS
1336 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001337 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338#endif
1339#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1340 if (buf_valid(prevbuf) && !aborting())
1341#else
1342 if (buf_valid(prevbuf))
1343#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001344 {
1345 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001346 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1348 unload ? action : (action == DOBUF_GOTO
1349 && !P_HID(prevbuf)
1350 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001354 /* An autocommand may have deleted "buf", already entered it (e.g., when
1355 * it did ":bunload") or aborted the script processing! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356# ifdef FEAT_EVAL
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001357 if (buf_valid(buf) && buf != curbuf && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358# else
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001359 if (buf_valid(buf) && buf != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360# endif
1361#endif
1362 enter_buffer(buf);
1363}
1364
1365/*
1366 * Enter a new current buffer.
1367 * Old curbuf must have been abandoned already!
1368 */
1369 void
1370enter_buffer(buf)
1371 buf_T *buf;
1372{
1373 /* Copy buffer and window local option values. Not for a help buffer. */
1374 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1375 if (!buf->b_help)
1376 get_winopts(buf);
1377#ifdef FEAT_FOLDING
1378 else
1379 /* Remove all folds in the window. */
1380 clearFolding(curwin);
1381 foldUpdateAll(curwin); /* update folds (later). */
1382#endif
1383
1384 /* Get the buffer in the current window. */
1385 curwin->w_buffer = buf;
1386 curbuf = buf;
1387 ++curbuf->b_nwindows;
1388
1389#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001390 if (curwin->w_p_diff)
1391 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#endif
1393
1394 /* Cursor on first line by default. */
1395 curwin->w_cursor.lnum = 1;
1396 curwin->w_cursor.col = 0;
1397#ifdef FEAT_VIRTUALEDIT
1398 curwin->w_cursor.coladd = 0;
1399#endif
1400 curwin->w_set_curswant = TRUE;
1401
1402 /* Make sure the buffer is loaded. */
1403 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001404 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001405#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001406 /* If there is no filetype, allow for detecting one. Esp. useful for
1407 * ":ball" used in a autocommand. If there already is a filetype we
1408 * might prefer to keep it. */
1409 if (*curbuf->b_p_ft == NUL)
1410 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001411#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 open_buffer(FALSE, NULL);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 else
1416 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001417 if (!msg_silent)
1418 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1420#ifdef FEAT_AUTOCMD
1421 curwin->w_topline = 1;
1422# ifdef FEAT_DIFF
1423 curwin->w_topfill = 0;
1424# endif
1425 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1426 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1427#endif
1428 }
1429
1430 /* If autocommands did not change the cursor position, restore cursor lnum
1431 * and possibly cursor col. */
1432 if (curwin->w_cursor.lnum == 1 && inindent(0))
1433 buflist_getfpos();
1434
1435 check_arg_idx(curwin); /* check for valid arg_idx */
1436#ifdef FEAT_TITLE
1437 maketitle();
1438#endif
1439#ifdef FEAT_AUTOCMD
1440 if (curwin->w_topline == 1) /* when autocmds didn't change it */
1441#endif
1442 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1443
Bram Moolenaar009b2592004-10-24 19:18:58 +00001444#ifdef FEAT_NETBEANS_INTG
1445 /* Send fileOpened event because we've changed buffers. */
1446 if (usingNetbeans && isNetbeansBuffer(curbuf))
1447 netbeans_file_activated(curbuf);
1448#endif
1449
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001450 /* Change directories when the 'acd' option is set. */
1451 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
1453#ifdef FEAT_KEYMAP
1454 if (curbuf->b_kmap_state & KEYMAP_INIT)
1455 keymap_init();
1456#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001457#ifdef FEAT_SPELL
1458 /* May need to set the spell language. Can only do this after the buffer
1459 * has been properly setup. */
1460 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
1461 did_set_spelllang(curbuf);
1462#endif
1463
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 redraw_later(NOT_VALID);
1465}
1466
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001467#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1468/*
1469 * Change to the directory of the current buffer.
1470 */
1471 void
1472do_autochdir()
1473{
1474 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1475 shorten_fnames(TRUE);
1476}
1477#endif
1478
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479/*
1480 * functions for dealing with the buffer list
1481 */
1482
1483/*
1484 * Add a file name to the buffer list. Return a pointer to the buffer.
1485 * If the same file name already exists return a pointer to that buffer.
1486 * If it does not exist, or if fname == NULL, a new entry is created.
1487 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1488 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1489 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 * This is the ONLY way to create a new buffer.
1491 */
1492static int top_file_num = 1; /* highest file number */
1493
1494 buf_T *
1495buflist_new(ffname, sfname, lnum, flags)
1496 char_u *ffname; /* full path of fname or relative */
1497 char_u *sfname; /* short fname or NULL */
1498 linenr_T lnum; /* preferred cursor line */
1499 int flags; /* BLN_ defines */
1500{
1501 buf_T *buf;
1502#ifdef UNIX
1503 struct stat st;
1504#endif
1505
1506 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1507
1508 /*
1509 * If file name already exists in the list, update the entry.
1510 */
1511#ifdef UNIX
1512 /* On Unix we can use inode numbers when the file exists. Works better
1513 * for hard links. */
1514 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1515 st.st_dev = (dev_T)-1;
1516#endif
1517 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1518#ifdef UNIX
1519 buflist_findname_stat(ffname, &st)
1520#else
1521 buflist_findname(ffname)
1522#endif
1523 ) != NULL)
1524 {
1525 vim_free(ffname);
1526 if (lnum != 0)
1527 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1528 /* copy the options now, if 'cpo' doesn't have 's' and not done
1529 * already */
1530 buf_copy_options(buf, 0);
1531 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1532 {
1533 buf->b_p_bl = TRUE;
1534#ifdef FEAT_AUTOCMD
1535 if (!(flags & BLN_DUMMY))
1536 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1537#endif
1538 }
1539 return buf;
1540 }
1541
1542 /*
1543 * If the current buffer has no name and no contents, use the current
1544 * buffer. Otherwise: Need to allocate a new buffer structure.
1545 *
1546 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001547 * (A spell file buffer is allocated in spell.c, but that's not a normal
1548 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 */
1550 buf = NULL;
1551 if ((flags & BLN_CURBUF)
1552 && curbuf != NULL
1553 && curbuf->b_ffname == NULL
1554 && curbuf->b_nwindows <= 1
1555 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1556 {
1557 buf = curbuf;
1558#ifdef FEAT_AUTOCMD
1559 /* It's like this buffer is deleted. Watch out for autocommands that
1560 * change curbuf! If that happens, allocate a new buffer anyway. */
1561 if (curbuf->b_p_bl)
1562 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1563 if (buf == curbuf)
1564 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1565# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001566 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 return NULL;
1568# endif
1569#endif
1570#ifdef FEAT_QUICKFIX
1571# ifdef FEAT_AUTOCMD
1572 if (buf == curbuf)
1573# endif
1574 {
1575 /* Make sure 'bufhidden' and 'buftype' are empty */
1576 clear_string_option(&buf->b_p_bh);
1577 clear_string_option(&buf->b_p_bt);
1578 }
1579#endif
1580 }
1581 if (buf != curbuf || curbuf == NULL)
1582 {
1583 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1584 if (buf == NULL)
1585 {
1586 vim_free(ffname);
1587 return NULL;
1588 }
1589 }
1590
1591 if (ffname != NULL)
1592 {
1593 buf->b_ffname = ffname;
1594 buf->b_sfname = vim_strsave(sfname);
1595 }
1596
1597 clear_wininfo(buf);
1598 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1599
1600 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1601 || buf->b_wininfo == NULL)
1602 {
1603 vim_free(buf->b_ffname);
1604 buf->b_ffname = NULL;
1605 vim_free(buf->b_sfname);
1606 buf->b_sfname = NULL;
1607 if (buf != curbuf)
1608 free_buffer(buf);
1609 return NULL;
1610 }
1611
1612 if (buf == curbuf)
1613 {
1614 /* free all things allocated for this buffer */
1615 buf_freeall(buf, FALSE, FALSE);
1616 if (buf != curbuf) /* autocommands deleted the buffer! */
1617 return NULL;
1618#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001619 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 return NULL;
1621#endif
1622 /* buf->b_nwindows = 0; why was this here? */
1623 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1624#ifdef FEAT_KEYMAP
1625 /* need to reload lmaps and set b:keymap_name */
1626 curbuf->b_kmap_state |= KEYMAP_INIT;
1627#endif
1628 }
1629 else
1630 {
1631 /*
1632 * put new buffer at the end of the buffer list
1633 */
1634 buf->b_next = NULL;
1635 if (firstbuf == NULL) /* buffer list is empty */
1636 {
1637 buf->b_prev = NULL;
1638 firstbuf = buf;
1639 }
1640 else /* append new buffer at end of list */
1641 {
1642 lastbuf->b_next = buf;
1643 buf->b_prev = lastbuf;
1644 }
1645 lastbuf = buf;
1646
1647 buf->b_fnum = top_file_num++;
1648 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1649 {
1650 EMSG(_("W14: Warning: List of file names overflow"));
1651 if (emsg_silent == 0)
1652 {
1653 out_flush();
1654 ui_delay(3000L, TRUE); /* make sure it is noticed */
1655 }
1656 top_file_num = 1;
1657 }
1658
1659 /*
1660 * Always copy the options from the current buffer.
1661 */
1662 buf_copy_options(buf, BCO_ALWAYS);
1663 }
1664
1665 buf->b_wininfo->wi_fpos.lnum = lnum;
1666 buf->b_wininfo->wi_win = curwin;
1667
1668#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001669 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1670#endif
1671#ifdef FEAT_SYN_HL
1672 hash_init(&buf->b_keywtab);
1673 hash_init(&buf->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674#endif
1675
1676 buf->b_fname = buf->b_sfname;
1677#ifdef UNIX
1678 if (st.st_dev == (dev_T)-1)
1679 buf->b_dev = -1;
1680 else
1681 {
1682 buf->b_dev = st.st_dev;
1683 buf->b_ino = st.st_ino;
1684 }
1685#endif
1686 buf->b_u_synced = TRUE;
1687 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001688 if (flags & BLN_DUMMY)
1689 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 buf_clear_file(buf);
1691 clrallmarks(buf); /* clear marks */
1692 fmarks_check_names(buf); /* check file marks for this file */
1693 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1694#ifdef FEAT_AUTOCMD
1695 if (!(flags & BLN_DUMMY))
1696 {
1697 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1698 if (flags & BLN_LISTED)
1699 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1700# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001701 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 return NULL;
1703# endif
1704 }
1705#endif
1706
1707 return buf;
1708}
1709
1710/*
1711 * Free the memory for the options of a buffer.
1712 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1713 * 'fileencoding'.
1714 */
1715 void
1716free_buf_options(buf, free_p_ff)
1717 buf_T *buf;
1718 int free_p_ff;
1719{
1720 if (free_p_ff)
1721 {
1722#ifdef FEAT_MBYTE
1723 clear_string_option(&buf->b_p_fenc);
1724#endif
1725 clear_string_option(&buf->b_p_ff);
1726#ifdef FEAT_QUICKFIX
1727 clear_string_option(&buf->b_p_bh);
1728 clear_string_option(&buf->b_p_bt);
1729#endif
1730 }
1731#ifdef FEAT_FIND_ID
1732 clear_string_option(&buf->b_p_def);
1733 clear_string_option(&buf->b_p_inc);
1734# ifdef FEAT_EVAL
1735 clear_string_option(&buf->b_p_inex);
1736# endif
1737#endif
1738#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1739 clear_string_option(&buf->b_p_inde);
1740 clear_string_option(&buf->b_p_indk);
1741#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001742#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1743 clear_string_option(&buf->b_p_bexpr);
1744#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001745#if defined(FEAT_EVAL)
1746 clear_string_option(&buf->b_p_fex);
1747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748#ifdef FEAT_CRYPT
1749 clear_string_option(&buf->b_p_key);
1750#endif
1751 clear_string_option(&buf->b_p_kp);
1752 clear_string_option(&buf->b_p_mps);
1753 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001754 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 clear_string_option(&buf->b_p_isk);
1756#ifdef FEAT_KEYMAP
1757 clear_string_option(&buf->b_p_keymap);
1758 ga_clear(&buf->b_kmap_ga);
1759#endif
1760#ifdef FEAT_COMMENTS
1761 clear_string_option(&buf->b_p_com);
1762#endif
1763#ifdef FEAT_FOLDING
1764 clear_string_option(&buf->b_p_cms);
1765#endif
1766 clear_string_option(&buf->b_p_nf);
1767#ifdef FEAT_SYN_HL
1768 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001769#endif
1770#ifdef FEAT_SPELL
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001771 clear_string_option(&buf->b_p_spc);
Bram Moolenaar86bc1fb2005-06-07 20:58:01 +00001772 clear_string_option(&buf->b_p_spf);
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001773 vim_free(buf->b_cap_prog);
1774 buf->b_cap_prog = NULL;
Bram Moolenaara0dea672005-03-20 22:23:10 +00001775 clear_string_option(&buf->b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#endif
1777#ifdef FEAT_SEARCHPATH
1778 clear_string_option(&buf->b_p_sua);
1779#endif
1780#ifdef FEAT_AUTOCMD
1781 clear_string_option(&buf->b_p_ft);
1782#endif
1783#ifdef FEAT_OSFILETYPE
1784 clear_string_option(&buf->b_p_oft);
1785#endif
1786#ifdef FEAT_CINDENT
1787 clear_string_option(&buf->b_p_cink);
1788 clear_string_option(&buf->b_p_cino);
1789#endif
1790#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1791 clear_string_option(&buf->b_p_cinw);
1792#endif
1793#ifdef FEAT_INS_EXPAND
1794 clear_string_option(&buf->b_p_cpt);
1795#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001796#ifdef FEAT_COMPL_FUNC
1797 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001798 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800#ifdef FEAT_QUICKFIX
1801 clear_string_option(&buf->b_p_gp);
1802 clear_string_option(&buf->b_p_mp);
1803 clear_string_option(&buf->b_p_efm);
1804#endif
1805 clear_string_option(&buf->b_p_ep);
1806 clear_string_option(&buf->b_p_path);
1807 clear_string_option(&buf->b_p_tags);
1808#ifdef FEAT_INS_EXPAND
1809 clear_string_option(&buf->b_p_dict);
1810 clear_string_option(&buf->b_p_tsr);
1811#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001812#ifdef FEAT_TEXTOBJ
1813 clear_string_option(&buf->b_p_qe);
1814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 buf->b_p_ar = -1;
1816}
1817
1818/*
1819 * get alternate file n
1820 * set linenr to lnum or altfpos.lnum if lnum == 0
1821 * also set cursor column to altfpos.col if 'startofline' is not set.
1822 * if (options & GETF_SETMARK) call setpcmark()
1823 * if (options & GETF_ALT) we are jumping to an alternate file.
1824 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1825 *
1826 * return FAIL for failure, OK for success
1827 */
1828 int
1829buflist_getfile(n, lnum, options, forceit)
1830 int n;
1831 linenr_T lnum;
1832 int options;
1833 int forceit;
1834{
1835 buf_T *buf;
1836#ifdef FEAT_WINDOWS
1837 win_T *wp = NULL;
1838#endif
1839 pos_T *fpos;
1840 colnr_T col;
1841
1842 buf = buflist_findnr(n);
1843 if (buf == NULL)
1844 {
1845 if ((options & GETF_ALT) && n == 0)
1846 EMSG(_(e_noalt));
1847 else
1848 EMSGN(_("E92: Buffer %ld not found"), n);
1849 return FAIL;
1850 }
1851
1852 /* if alternate file is the current buffer, nothing to do */
1853 if (buf == curbuf)
1854 return OK;
1855
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001856 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001857 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001858 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001860 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001861#ifdef FEAT_AUTOCMD
1862 if (curbuf_locked())
1863 return FAIL;
1864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
1866 /* altfpos may be changed by getfile(), get it now */
1867 if (lnum == 0)
1868 {
1869 fpos = buflist_findfpos(buf);
1870 lnum = fpos->lnum;
1871 col = fpos->col;
1872 }
1873 else
1874 col = 0;
1875
1876#ifdef FEAT_WINDOWS
1877 if (options & GETF_SWITCH)
1878 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001879 /* If 'switchbuf' contains "useopen": jump to first window containing
1880 * "buf" if one exists */
1881 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001883 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1884 * page containing "buf" if one exists */
1885 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001886 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001887 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1888 * isn't empty: open new window */
1889 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001891 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1892 tabpage_new();
1893 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 return FAIL;
1895# ifdef FEAT_SCROLLBIND
1896 curwin->w_p_scb = FALSE;
1897# endif
1898 }
1899 }
1900#endif
1901
1902 ++RedrawingDisabled;
1903 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1904 lnum, forceit) <= 0)
1905 {
1906 --RedrawingDisabled;
1907
1908 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1909 if (!p_sol && col != 0)
1910 {
1911 curwin->w_cursor.col = col;
1912 check_cursor_col();
1913#ifdef FEAT_VIRTUALEDIT
1914 curwin->w_cursor.coladd = 0;
1915#endif
1916 curwin->w_set_curswant = TRUE;
1917 }
1918 return OK;
1919 }
1920 --RedrawingDisabled;
1921 return FAIL;
1922}
1923
1924/*
1925 * go to the last know line number for the current buffer
1926 */
1927 void
1928buflist_getfpos()
1929{
1930 pos_T *fpos;
1931
1932 fpos = buflist_findfpos(curbuf);
1933
1934 curwin->w_cursor.lnum = fpos->lnum;
1935 check_cursor_lnum();
1936
1937 if (p_sol)
1938 curwin->w_cursor.col = 0;
1939 else
1940 {
1941 curwin->w_cursor.col = fpos->col;
1942 check_cursor_col();
1943#ifdef FEAT_VIRTUALEDIT
1944 curwin->w_cursor.coladd = 0;
1945#endif
1946 curwin->w_set_curswant = TRUE;
1947 }
1948}
1949
Bram Moolenaar81695252004-12-29 20:58:21 +00001950#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1951/*
1952 * Find file in buffer list by name (it has to be for the current window).
1953 * Returns NULL if not found.
1954 */
1955 buf_T *
1956buflist_findname_exp(fname)
1957 char_u *fname;
1958{
1959 char_u *ffname;
1960 buf_T *buf = NULL;
1961
1962 /* First make the name into a full path name */
1963 ffname = FullName_save(fname,
1964#ifdef UNIX
1965 TRUE /* force expansion, get rid of symbolic links */
1966#else
1967 FALSE
1968#endif
1969 );
1970 if (ffname != NULL)
1971 {
1972 buf = buflist_findname(ffname);
1973 vim_free(ffname);
1974 }
1975 return buf;
1976}
1977#endif
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979/*
1980 * Find file in buffer list by name (it has to be for the current window).
1981 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00001982 * Skips dummy buffers.
1983 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 */
1985 buf_T *
1986buflist_findname(ffname)
1987 char_u *ffname;
1988{
1989#ifdef UNIX
1990 struct stat st;
1991
1992 if (mch_stat((char *)ffname, &st) < 0)
1993 st.st_dev = (dev_T)-1;
1994 return buflist_findname_stat(ffname, &st);
1995}
1996
1997/*
1998 * Same as buflist_findname(), but pass the stat structure to avoid getting it
1999 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002000 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 */
2002 static buf_T *
2003buflist_findname_stat(ffname, stp)
2004 char_u *ffname;
2005 struct stat *stp;
2006{
2007#endif
2008 buf_T *buf;
2009
2010 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002011 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012#ifdef UNIX
2013 , stp
2014#endif
2015 ))
2016 return buf;
2017 return NULL;
2018}
2019
2020#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2021/*
2022 * Find file in buffer list by a regexp pattern.
2023 * Return fnum of the found buffer.
2024 * Return < 0 for error.
2025 */
2026/*ARGSUSED*/
2027 int
2028buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2029 char_u *pattern;
2030 char_u *pattern_end; /* pointer to first char after pattern */
2031 int unlisted; /* find unlisted buffers */
2032 int diffmode; /* find diff-mode buffers only */
2033{
2034 buf_T *buf;
2035 regprog_T *prog;
2036 int match = -1;
2037 int find_listed;
2038 char_u *pat;
2039 char_u *patend;
2040 int attempt;
2041 char_u *p;
2042 int toggledollar;
2043
2044 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2045 {
2046 if (*pattern == '%')
2047 match = curbuf->b_fnum;
2048 else
2049 match = curwin->w_alt_fnum;
2050#ifdef FEAT_DIFF
2051 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2052 match = -1;
2053#endif
2054 }
2055
2056 /*
2057 * Try four ways of matching a listed buffer:
2058 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002059 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 * attempt == 2: with '$' at end (only match at end)
2061 * attempt == 3: with '^' at start and '$' at end (only full match)
2062 * Repeat this for finding an unlisted buffer if there was no matching
2063 * listed buffer.
2064 */
2065 else
2066 {
2067 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2068 if (pat == NULL)
2069 return -1;
2070 patend = pat + STRLEN(pat) - 1;
2071 toggledollar = (patend > pat && *patend == '$');
2072
2073 /* First try finding a listed buffer. If not found and "unlisted"
2074 * is TRUE, try finding an unlisted buffer. */
2075 find_listed = TRUE;
2076 for (;;)
2077 {
2078 for (attempt = 0; attempt <= 3; ++attempt)
2079 {
2080 /* may add '^' and '$' */
2081 if (toggledollar)
2082 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2083 p = pat;
2084 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2085 ++p;
2086 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2087 if (prog == NULL)
2088 {
2089 vim_free(pat);
2090 return -1;
2091 }
2092
2093 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2094 if (buf->b_p_bl == find_listed
2095#ifdef FEAT_DIFF
2096 && (!diffmode || diff_mode_buf(buf))
2097#endif
2098 && buflist_match(prog, buf) != NULL)
2099 {
2100 if (match >= 0) /* already found a match */
2101 {
2102 match = -2;
2103 break;
2104 }
2105 match = buf->b_fnum; /* remember first match */
2106 }
2107
2108 vim_free(prog);
2109 if (match >= 0) /* found one match */
2110 break;
2111 }
2112
2113 /* Only search for unlisted buffers if there was no match with
2114 * a listed buffer. */
2115 if (!unlisted || !find_listed || match != -1)
2116 break;
2117 find_listed = FALSE;
2118 }
2119
2120 vim_free(pat);
2121 }
2122
2123 if (match == -2)
2124 EMSG2(_("E93: More than one match for %s"), pattern);
2125 else if (match < 0)
2126 EMSG2(_("E94: No matching buffer for %s"), pattern);
2127 return match;
2128}
2129#endif
2130
2131#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2132
2133/*
2134 * Find all buffer names that match.
2135 * For command line expansion of ":buf" and ":sbuf".
2136 * Return OK if matches found, FAIL otherwise.
2137 */
2138 int
2139ExpandBufnames(pat, num_file, file, options)
2140 char_u *pat;
2141 int *num_file;
2142 char_u ***file;
2143 int options;
2144{
2145 int count = 0;
2146 buf_T *buf;
2147 int round;
2148 char_u *p;
2149 int attempt;
2150 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002151 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152
2153 *num_file = 0; /* return values in case of FAIL */
2154 *file = NULL;
2155
Bram Moolenaar05159a02005-02-26 23:04:13 +00002156 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2157 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002159 patc = alloc((unsigned)STRLEN(pat) + 11);
2160 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002162 STRCPY(patc, "\\(^\\|[\\/]\\)");
2163 STRCPY(patc + 11, pat + 1);
2164 }
2165 else
2166 patc = pat;
2167
2168 /*
2169 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002170 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002171 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002172 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002173 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002174 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002175 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002176 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002177 if (prog == NULL)
2178 {
2179 if (patc != pat)
2180 vim_free(patc);
2181 return FAIL;
2182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
2184 /*
2185 * round == 1: Count the matches.
2186 * round == 2: Build the array to keep the matches.
2187 */
2188 for (round = 1; round <= 2; ++round)
2189 {
2190 count = 0;
2191 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2192 {
2193 if (!buf->b_p_bl) /* skip unlisted buffers */
2194 continue;
2195 p = buflist_match(prog, buf);
2196 if (p != NULL)
2197 {
2198 if (round == 1)
2199 ++count;
2200 else
2201 {
2202 if (options & WILD_HOME_REPLACE)
2203 p = home_replace_save(buf, p);
2204 else
2205 p = vim_strsave(p);
2206 (*file)[count++] = p;
2207 }
2208 }
2209 }
2210 if (count == 0) /* no match found, break here */
2211 break;
2212 if (round == 1)
2213 {
2214 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2215 if (*file == NULL)
2216 {
2217 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002218 if (patc != pat)
2219 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 return FAIL;
2221 }
2222 }
2223 }
2224 vim_free(prog);
2225 if (count) /* match(es) found, break here */
2226 break;
2227 }
2228
Bram Moolenaar05159a02005-02-26 23:04:13 +00002229 if (patc != pat)
2230 vim_free(patc);
2231
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 *num_file = count;
2233 return (count == 0 ? FAIL : OK);
2234}
2235
2236#endif /* FEAT_CMDL_COMPL */
2237
2238#ifdef HAVE_BUFLIST_MATCH
2239/*
2240 * Check for a match on the file name for buffer "buf" with regprog "prog".
2241 */
2242 static char_u *
2243buflist_match(prog, buf)
2244 regprog_T *prog;
2245 buf_T *buf;
2246{
2247 char_u *match;
2248
2249 /* First try the short file name, then the long file name. */
2250 match = fname_match(prog, buf->b_sfname);
2251 if (match == NULL)
2252 match = fname_match(prog, buf->b_ffname);
2253
2254 return match;
2255}
2256
2257/*
2258 * Try matching the regexp in "prog" with file name "name".
2259 * Return "name" when there is a match, NULL when not.
2260 */
2261 static char_u *
2262fname_match(prog, name)
2263 regprog_T *prog;
2264 char_u *name;
2265{
2266 char_u *match = NULL;
2267 char_u *p;
2268 regmatch_T regmatch;
2269
2270 if (name != NULL)
2271 {
2272 regmatch.regprog = prog;
2273#ifdef CASE_INSENSITIVE_FILENAME
2274 regmatch.rm_ic = TRUE; /* Always ignore case */
2275#else
2276 regmatch.rm_ic = FALSE; /* Never ignore case */
2277#endif
2278
2279 if (vim_regexec(&regmatch, name, (colnr_T)0))
2280 match = name;
2281 else
2282 {
2283 /* Replace $(HOME) with '~' and try matching again. */
2284 p = home_replace_save(NULL, name);
2285 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2286 match = name;
2287 vim_free(p);
2288 }
2289 }
2290
2291 return match;
2292}
2293#endif
2294
2295/*
2296 * find file in buffer list by number
2297 */
2298 buf_T *
2299buflist_findnr(nr)
2300 int nr;
2301{
2302 buf_T *buf;
2303
2304 if (nr == 0)
2305 nr = curwin->w_alt_fnum;
2306 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2307 if (buf->b_fnum == nr)
2308 return (buf);
2309 return NULL;
2310}
2311
2312/*
2313 * Get name of file 'n' in the buffer list.
2314 * When the file has no name an empty string is returned.
2315 * home_replace() is used to shorten the file name (used for marks).
2316 * Returns a pointer to allocated memory, of NULL when failed.
2317 */
2318 char_u *
2319buflist_nr2name(n, fullname, helptail)
2320 int n;
2321 int fullname;
2322 int helptail; /* for help buffers return tail only */
2323{
2324 buf_T *buf;
2325
2326 buf = buflist_findnr(n);
2327 if (buf == NULL)
2328 return NULL;
2329 return home_replace_save(helptail ? buf : NULL,
2330 fullname ? buf->b_ffname : buf->b_fname);
2331}
2332
2333/*
2334 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2335 * When "copy_options" is TRUE save the local window option values.
2336 * When "lnum" is 0 only do the options.
2337 */
2338 static void
2339buflist_setfpos(buf, win, lnum, col, copy_options)
2340 buf_T *buf;
2341 win_T *win;
2342 linenr_T lnum;
2343 colnr_T col;
2344 int copy_options;
2345{
2346 wininfo_T *wip;
2347
2348 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2349 if (wip->wi_win == win)
2350 break;
2351 if (wip == NULL)
2352 {
2353 /* allocate a new entry */
2354 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2355 if (wip == NULL)
2356 return;
2357 wip->wi_win = win;
2358 if (lnum == 0) /* set lnum even when it's 0 */
2359 lnum = 1;
2360 }
2361 else
2362 {
2363 /* remove the entry from the list */
2364 if (wip->wi_prev)
2365 wip->wi_prev->wi_next = wip->wi_next;
2366 else
2367 buf->b_wininfo = wip->wi_next;
2368 if (wip->wi_next)
2369 wip->wi_next->wi_prev = wip->wi_prev;
2370 if (copy_options && wip->wi_optset)
2371 {
2372 clear_winopt(&wip->wi_opt);
2373#ifdef FEAT_FOLDING
2374 deleteFoldRecurse(&wip->wi_folds);
2375#endif
2376 }
2377 }
2378 if (lnum != 0)
2379 {
2380 wip->wi_fpos.lnum = lnum;
2381 wip->wi_fpos.col = col;
2382 }
2383 if (copy_options)
2384 {
2385 /* Save the window-specific option values. */
2386 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2387#ifdef FEAT_FOLDING
2388 wip->wi_fold_manual = win->w_fold_manual;
2389 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2390#endif
2391 wip->wi_optset = TRUE;
2392 }
2393
2394 /* insert the entry in front of the list */
2395 wip->wi_next = buf->b_wininfo;
2396 buf->b_wininfo = wip;
2397 wip->wi_prev = NULL;
2398 if (wip->wi_next)
2399 wip->wi_next->wi_prev = wip;
2400
2401 return;
2402}
2403
2404/*
2405 * Find info for the current window in buffer "buf".
2406 * If not found, return the info for the most recently used window.
2407 * Returns NULL when there isn't any info.
2408 */
2409 static wininfo_T *
2410find_wininfo(buf)
2411 buf_T *buf;
2412{
2413 wininfo_T *wip;
2414
2415 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2416 if (wip->wi_win == curwin)
2417 break;
2418 if (wip == NULL) /* if no fpos for curwin, use the first in the list */
2419 wip = buf->b_wininfo;
2420 return wip;
2421}
2422
2423/*
2424 * Reset the local window options to the values last used in this window.
2425 * If the buffer wasn't used in this window before, use the values from
2426 * the most recently used window. If the values were never set, use the
2427 * global values for the window.
2428 */
2429 void
2430get_winopts(buf)
2431 buf_T *buf;
2432{
2433 wininfo_T *wip;
2434
2435 clear_winopt(&curwin->w_onebuf_opt);
2436#ifdef FEAT_FOLDING
2437 clearFolding(curwin);
2438#endif
2439
2440 wip = find_wininfo(buf);
2441 if (wip != NULL && wip->wi_optset)
2442 {
2443 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2444#ifdef FEAT_FOLDING
2445 curwin->w_fold_manual = wip->wi_fold_manual;
2446 curwin->w_foldinvalid = TRUE;
2447 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2448#endif
2449 }
2450 else
2451 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2452
2453#ifdef FEAT_FOLDING
2454 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2455 if (p_fdls >= 0)
2456 curwin->w_p_fdl = p_fdls;
2457#endif
2458}
2459
2460/*
2461 * Find the position (lnum and col) for the buffer 'buf' for the current
2462 * window.
2463 * Returns a pointer to no_position if no position is found.
2464 */
2465 pos_T *
2466buflist_findfpos(buf)
2467 buf_T *buf;
2468{
2469 wininfo_T *wip;
2470 static pos_T no_position = {1, 0};
2471
2472 wip = find_wininfo(buf);
2473 if (wip != NULL)
2474 return &(wip->wi_fpos);
2475 else
2476 return &no_position;
2477}
2478
2479/*
2480 * Find the lnum for the buffer 'buf' for the current window.
2481 */
2482 linenr_T
2483buflist_findlnum(buf)
2484 buf_T *buf;
2485{
2486 return buflist_findfpos(buf)->lnum;
2487}
2488
2489#if defined(FEAT_LISTCMDS) || defined(PROTO)
2490/*
2491 * List all know file names (for :files and :buffers command).
2492 */
2493/*ARGSUSED*/
2494 void
2495buflist_list(eap)
2496 exarg_T *eap;
2497{
2498 buf_T *buf;
2499 int len;
2500 int i;
2501
2502 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2503 {
2504 /* skip unlisted buffers, unless ! was used */
2505 if (!buf->b_p_bl && !eap->forceit)
2506 continue;
2507 msg_putchar('\n');
2508 if (buf_spname(buf) != NULL)
2509 STRCPY(NameBuff, buf_spname(buf));
2510 else
2511 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2512
Bram Moolenaar50cde822005-06-05 21:54:54 +00002513 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 buf->b_fnum,
2515 buf->b_p_bl ? ' ' : 'u',
2516 buf == curbuf ? '%' :
2517 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2518 buf->b_ml.ml_mfp == NULL ? ' ' :
2519 (buf->b_nwindows == 0 ? 'h' : 'a'),
2520 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2521 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002522 : (bufIsChanged(buf) ? '+' : ' '),
2523 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
2525 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 i = 40 - vim_strsize(IObuff);
2527 do
2528 {
2529 IObuff[len++] = ' ';
2530 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002531 vim_snprintf((char *)IObuff + len, IOSIZE - len, _("line %ld"),
2532 buf == curbuf ? curwin->w_cursor.lnum
2533 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 msg_outtrans(IObuff);
2535 out_flush(); /* output one line at a time */
2536 ui_breakcheck();
2537 }
2538}
2539#endif
2540
2541/*
2542 * Get file name and line number for file 'fnum'.
2543 * Used by DoOneCmd() for translating '%' and '#'.
2544 * Used by insert_reg() and cmdline_paste() for '#' register.
2545 * Return FAIL if not found, OK for success.
2546 */
2547 int
2548buflist_name_nr(fnum, fname, lnum)
2549 int fnum;
2550 char_u **fname;
2551 linenr_T *lnum;
2552{
2553 buf_T *buf;
2554
2555 buf = buflist_findnr(fnum);
2556 if (buf == NULL || buf->b_fname == NULL)
2557 return FAIL;
2558
2559 *fname = buf->b_fname;
2560 *lnum = buflist_findlnum(buf);
2561
2562 return OK;
2563}
2564
2565/*
2566 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2567 * The file name with the full path is also remembered, for when :cd is used.
2568 * Returns FAIL for failure (file name already in use by other buffer)
2569 * OK otherwise.
2570 */
2571 int
2572setfname(buf, ffname, sfname, message)
2573 buf_T *buf;
2574 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002575 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576{
Bram Moolenaar81695252004-12-29 20:58:21 +00002577 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578#ifdef UNIX
2579 struct stat st;
2580#endif
2581
2582 if (ffname == NULL || *ffname == NUL)
2583 {
2584 /* Removing the name. */
2585 vim_free(buf->b_ffname);
2586 vim_free(buf->b_sfname);
2587 buf->b_ffname = NULL;
2588 buf->b_sfname = NULL;
2589#ifdef UNIX
2590 st.st_dev = (dev_T)-1;
2591#endif
2592 }
2593 else
2594 {
2595 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2596 if (ffname == NULL) /* out of memory */
2597 return FAIL;
2598
2599 /*
2600 * if the file name is already used in another buffer:
2601 * - if the buffer is loaded, fail
2602 * - if the buffer is not loaded, delete it from the list
2603 */
2604#ifdef UNIX
2605 if (mch_stat((char *)ffname, &st) < 0)
2606 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002607#endif
2608 if (!(buf->b_flags & BF_DUMMY))
2609#ifdef UNIX
2610 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002612 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613#endif
2614 if (obuf != NULL && obuf != buf)
2615 {
2616 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2617 {
2618 if (message)
2619 EMSG(_("E95: Buffer with this name already exists"));
2620 vim_free(ffname);
2621 return FAIL;
2622 }
2623 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2624 }
2625 sfname = vim_strsave(sfname);
2626 if (ffname == NULL || sfname == NULL)
2627 {
2628 vim_free(sfname);
2629 vim_free(ffname);
2630 return FAIL;
2631 }
2632#ifdef USE_FNAME_CASE
2633# ifdef USE_LONG_FNAME
2634 if (USE_LONG_FNAME)
2635# endif
2636 fname_case(sfname, 0); /* set correct case for short file name */
2637#endif
2638 vim_free(buf->b_ffname);
2639 vim_free(buf->b_sfname);
2640 buf->b_ffname = ffname;
2641 buf->b_sfname = sfname;
2642 }
2643 buf->b_fname = buf->b_sfname;
2644#ifdef UNIX
2645 if (st.st_dev == (dev_T)-1)
2646 buf->b_dev = -1;
2647 else
2648 {
2649 buf->b_dev = st.st_dev;
2650 buf->b_ino = st.st_ino;
2651 }
2652#endif
2653
2654#ifndef SHORT_FNAME
2655 buf->b_shortname = FALSE;
2656#endif
2657
2658 buf_name_changed(buf);
2659 return OK;
2660}
2661
2662/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002663 * Crude way of changing the name of a buffer. Use with care!
2664 * The name should be relative to the current directory.
2665 */
2666 void
2667buf_set_name(fnum, name)
2668 int fnum;
2669 char_u *name;
2670{
2671 buf_T *buf;
2672
2673 buf = buflist_findnr(fnum);
2674 if (buf != NULL)
2675 {
2676 vim_free(buf->b_sfname);
2677 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002678 buf->b_ffname = vim_strsave(name);
2679 buf->b_sfname = NULL;
2680 /* Allocate ffname and expand into full path. Also resolves .lnk
2681 * files on Win32. */
2682 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002683 buf->b_fname = buf->b_sfname;
2684 }
2685}
2686
2687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 * Take care of what needs to be done when the name of buffer "buf" has
2689 * changed.
2690 */
2691 void
2692buf_name_changed(buf)
2693 buf_T *buf;
2694{
2695 /*
2696 * If the file name changed, also change the name of the swapfile
2697 */
2698 if (buf->b_ml.ml_mfp != NULL)
2699 ml_setname(buf);
2700
2701 if (curwin->w_buffer == buf)
2702 check_arg_idx(curwin); /* check file name for arg list */
2703#ifdef FEAT_TITLE
2704 maketitle(); /* set window title */
2705#endif
2706#ifdef FEAT_WINDOWS
2707 status_redraw_all(); /* status lines need to be redrawn */
2708#endif
2709 fmarks_check_names(buf); /* check named file marks */
2710 ml_timestamp(buf); /* reset timestamp */
2711}
2712
2713/*
2714 * set alternate file name for current window
2715 *
2716 * Used by do_one_cmd(), do_write() and do_ecmd().
2717 * Return the buffer.
2718 */
2719 buf_T *
2720setaltfname(ffname, sfname, lnum)
2721 char_u *ffname;
2722 char_u *sfname;
2723 linenr_T lnum;
2724{
2725 buf_T *buf;
2726
2727 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2728 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002729 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 curwin->w_alt_fnum = buf->b_fnum;
2731 return buf;
2732}
2733
2734/*
2735 * Get alternate file name for current window.
2736 * Return NULL if there isn't any, and give error message if requested.
2737 */
2738 char_u *
2739getaltfname(errmsg)
2740 int errmsg; /* give error message */
2741{
2742 char_u *fname;
2743 linenr_T dummy;
2744
2745 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2746 {
2747 if (errmsg)
2748 EMSG(_(e_noalt));
2749 return NULL;
2750 }
2751 return fname;
2752}
2753
2754/*
2755 * Add a file name to the buflist and return its number.
2756 * Uses same flags as buflist_new(), except BLN_DUMMY.
2757 *
2758 * used by qf_init(), main() and doarglist()
2759 */
2760 int
2761buflist_add(fname, flags)
2762 char_u *fname;
2763 int flags;
2764{
2765 buf_T *buf;
2766
2767 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2768 if (buf != NULL)
2769 return buf->b_fnum;
2770 return 0;
2771}
2772
2773#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2774/*
2775 * Adjust slashes in file names. Called after 'shellslash' was set.
2776 */
2777 void
2778buflist_slash_adjust()
2779{
2780 buf_T *bp;
2781
2782 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2783 {
2784 if (bp->b_ffname != NULL)
2785 slash_adjust(bp->b_ffname);
2786 if (bp->b_sfname != NULL)
2787 slash_adjust(bp->b_sfname);
2788 }
2789}
2790#endif
2791
2792/*
2793 * Set alternate cursor position for current window.
2794 * Also save the local window option values.
2795 */
2796 void
2797buflist_altfpos()
2798{
2799 buflist_setfpos(curbuf, curwin, curwin->w_cursor.lnum,
2800 curwin->w_cursor.col, TRUE);
2801}
2802
2803/*
2804 * Return TRUE if 'ffname' is not the same file as current file.
2805 * Fname must have a full path (expanded by mch_FullName()).
2806 */
2807 int
2808otherfile(ffname)
2809 char_u *ffname;
2810{
2811 return otherfile_buf(curbuf, ffname
2812#ifdef UNIX
2813 , NULL
2814#endif
2815 );
2816}
2817
2818 static int
2819otherfile_buf(buf, ffname
2820#ifdef UNIX
2821 , stp
2822#endif
2823 )
2824 buf_T *buf;
2825 char_u *ffname;
2826#ifdef UNIX
2827 struct stat *stp;
2828#endif
2829{
2830 /* no name is different */
2831 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2832 return TRUE;
2833 if (fnamecmp(ffname, buf->b_ffname) == 0)
2834 return FALSE;
2835#ifdef UNIX
2836 {
2837 struct stat st;
2838
2839 /* If no struct stat given, get it now */
2840 if (stp == NULL)
2841 {
2842 if (buf->b_dev < 0 || mch_stat((char *)ffname, &st) < 0)
2843 st.st_dev = (dev_T)-1;
2844 stp = &st;
2845 }
2846 /* Use dev/ino to check if the files are the same, even when the names
2847 * are different (possible with links). Still need to compare the
2848 * name above, for when the file doesn't exist yet.
2849 * Problem: The dev/ino changes when a file is deleted (and created
2850 * again) and remains the same when renamed/moved. We don't want to
2851 * mch_stat() each buffer each time, that would be too slow. Get the
2852 * dev/ino again when they appear to match, but not when they appear
2853 * to be different: Could skip a buffer when it's actually the same
2854 * file. */
2855 if (buf_same_ino(buf, stp))
2856 {
2857 buf_setino(buf);
2858 if (buf_same_ino(buf, stp))
2859 return FALSE;
2860 }
2861 }
2862#endif
2863 return TRUE;
2864}
2865
2866#if defined(UNIX) || defined(PROTO)
2867/*
2868 * Set inode and device number for a buffer.
2869 * Must always be called when b_fname is changed!.
2870 */
2871 void
2872buf_setino(buf)
2873 buf_T *buf;
2874{
2875 struct stat st;
2876
2877 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2878 {
2879 buf->b_dev = st.st_dev;
2880 buf->b_ino = st.st_ino;
2881 }
2882 else
2883 buf->b_dev = -1;
2884}
2885
2886/*
2887 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2888 */
2889 static int
2890buf_same_ino(buf, stp)
2891 buf_T *buf;
2892 struct stat *stp;
2893{
2894 return (buf->b_dev >= 0
2895 && stp->st_dev == buf->b_dev
2896 && stp->st_ino == buf->b_ino);
2897}
2898#endif
2899
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002900/*
2901 * Print info about the current buffer.
2902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 void
2904fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002905 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 int shorthelp;
2907 int dont_truncate;
2908{
2909 char_u *name;
2910 int n;
2911 char_u *p;
2912 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002913 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914
2915 buffer = alloc(IOSIZE);
2916 if (buffer == NULL)
2917 return;
2918
2919 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2920 {
2921 sprintf((char *)buffer, "buf %d: ", curbuf->b_fnum);
2922 p = buffer + STRLEN(buffer);
2923 }
2924 else
2925 p = buffer;
2926
2927 *p++ = '"';
2928 if (buf_spname(curbuf) != NULL)
2929 STRCPY(p, buf_spname(curbuf));
2930 else
2931 {
2932 if (!fullname && curbuf->b_fname != NULL)
2933 name = curbuf->b_fname;
2934 else
2935 name = curbuf->b_ffname;
2936 home_replace(shorthelp ? curbuf : NULL, name, p,
2937 (int)(IOSIZE - (p - buffer)), TRUE);
2938 }
2939
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002940 len = STRLEN(buffer);
2941 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 "\"%s%s%s%s%s%s",
2943 curbufIsChanged() ? (shortmess(SHM_MOD)
2944 ? " [+]" : _(" [Modified]")) : " ",
2945 (curbuf->b_flags & BF_NOTEDITED)
2946#ifdef FEAT_QUICKFIX
2947 && !bt_dontwrite(curbuf)
2948#endif
2949 ? _("[Not edited]") : "",
2950 (curbuf->b_flags & BF_NEW)
2951#ifdef FEAT_QUICKFIX
2952 && !bt_dontwrite(curbuf)
2953#endif
2954 ? _("[New file]") : "",
2955 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
2956 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
2957 : _("[readonly]")) : "",
2958 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
2959 || curbuf->b_p_ro) ?
2960 " " : "");
2961 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
2962 * causes an overflow, thus for large numbers divide instead. */
2963 if (curwin->w_cursor.lnum > 1000000L)
2964 n = (int)(((long)curwin->w_cursor.lnum) /
2965 ((long)curbuf->b_ml.ml_line_count / 100L));
2966 else
2967 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
2968 (long)curbuf->b_ml.ml_line_count);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002969 len = STRLEN(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2971 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002972 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 }
2974#ifdef FEAT_CMDL_INFO
2975 else if (p_ru)
2976 {
2977 /* Current line and column are already on the screen -- webb */
2978 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002979 vim_snprintf((char *)buffer + len, IOSIZE - len,
2980 _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002982 vim_snprintf((char *)buffer + len, IOSIZE - len,
2983 _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 (long)curbuf->b_ml.ml_line_count, n);
2985 }
2986#endif
2987 else
2988 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002989 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 _("line %ld of %ld --%d%%-- col "),
2991 (long)curwin->w_cursor.lnum,
2992 (long)curbuf->b_ml.ml_line_count,
2993 n);
2994 validate_virtcol();
2995 col_print(buffer + STRLEN(buffer),
2996 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
2997 }
2998
2999 (void)append_arg_number(curwin, buffer, !shortmess(SHM_FILE), IOSIZE);
3000
3001 if (dont_truncate)
3002 {
3003 /* Temporarily set msg_scroll to avoid the message being truncated.
3004 * First call msg_start() to get the message in the right place. */
3005 msg_start();
3006 n = msg_scroll;
3007 msg_scroll = TRUE;
3008 msg(buffer);
3009 msg_scroll = n;
3010 }
3011 else
3012 {
3013 p = msg_trunc_attr(buffer, FALSE, 0);
3014 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 /* Need to repeat the message after redrawing when:
3016 * - When restart_edit is set (otherwise there will be a delay
3017 * before redrawing).
3018 * - When the screen was scrolled but there is no wait-return
3019 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003020 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 }
3022
3023 vim_free(buffer);
3024}
3025
3026 void
3027col_print(buf, col, vcol)
3028 char_u *buf;
3029 int col;
3030 int vcol;
3031{
3032 if (col == vcol)
3033 sprintf((char *)buf, "%d", col);
3034 else
3035 sprintf((char *)buf, "%d-%d", col, vcol);
3036}
3037
3038#if defined(FEAT_TITLE) || defined(PROTO)
3039/*
3040 * put file name in title bar of window and in icon title
3041 */
3042
3043static char_u *lasttitle = NULL;
3044static char_u *lasticon = NULL;
3045
3046 void
3047maketitle()
3048{
3049 char_u *p;
3050 char_u *t_str = NULL;
3051 char_u *i_name;
3052 char_u *i_str = NULL;
3053 int maxlen = 0;
3054 int len;
3055 int mustset;
3056 char_u buf[IOSIZE];
3057 int off;
3058
3059 if (!redrawing())
3060 {
3061 /* Postpone updating the title when 'lazyredraw' is set. */
3062 need_maketitle = TRUE;
3063 return;
3064 }
3065
3066 need_maketitle = FALSE;
3067 if (!p_title && !p_icon)
3068 return;
3069
3070 if (p_title)
3071 {
3072 if (p_titlelen > 0)
3073 {
3074 maxlen = p_titlelen * Columns / 100;
3075 if (maxlen < 10)
3076 maxlen = 10;
3077 }
3078
3079 t_str = buf;
3080 if (*p_titlestring != NUL)
3081 {
3082#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003083 if (stl_syntax & STL_IN_TITLE)
3084 {
3085 int use_sandbox = FALSE;
3086 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003087
3088# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003089 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003090# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003091 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003093 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003094 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003095 if (called_emsg)
3096 set_string_option_direct((char_u *)"titlestring", -1,
3097 (char_u *)"", OPT_FREE, SID_ERROR);
3098 called_emsg |= save_called_emsg;
3099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 else
3101#endif
3102 t_str = p_titlestring;
3103 }
3104 else
3105 {
3106 /* format: "fname + (path) (1 of 2) - VIM" */
3107
3108 if (curbuf->b_fname == NULL)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003109 STRCPY(buf, _("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 else
3111 {
3112 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003113 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 }
3116
3117 switch (bufIsChanged(curbuf)
3118 + (curbuf->b_p_ro * 2)
3119 + (!curbuf->b_p_ma * 4))
3120 {
3121 case 1: STRCAT(buf, " +"); break;
3122 case 2: STRCAT(buf, " ="); break;
3123 case 3: STRCAT(buf, " =+"); break;
3124 case 4:
3125 case 6: STRCAT(buf, " -"); break;
3126 case 5:
3127 case 7: STRCAT(buf, " -+"); break;
3128 }
3129
3130 if (curbuf->b_fname != NULL)
3131 {
3132 /* Get path of file, replace home dir with ~ */
3133 off = (int)STRLEN(buf);
3134 buf[off++] = ' ';
3135 buf[off++] = '(';
3136 home_replace(curbuf, curbuf->b_ffname,
3137 buf + off, IOSIZE - off, TRUE);
3138#ifdef BACKSLASH_IN_FILENAME
3139 /* avoid "c:/name" to be reduced to "c" */
3140 if (isalpha(buf[off]) && buf[off + 1] == ':')
3141 off += 2;
3142#endif
3143 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003144 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003147 vim_strncpy(buf + off, (char_u *)_("help"),
3148 IOSIZE - off - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003151
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 /* translate unprintable chars */
3153 p = transstr(buf + off);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003154 vim_strncpy(buf + off, p, IOSIZE - off - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 STRCAT(buf, ")");
3157 }
3158
3159 append_arg_number(curwin, buf, FALSE, IOSIZE);
3160
3161#if defined(FEAT_CLIENTSERVER)
3162 if (serverName != NULL)
3163 {
3164 STRCAT(buf, " - ");
3165 STRCAT(buf, serverName);
3166 }
3167 else
3168#endif
3169 STRCAT(buf, " - VIM");
3170
3171 if (maxlen > 0)
3172 {
3173 /* make it shorter by removing a bit in the middle */
3174 len = vim_strsize(buf);
3175 if (len > maxlen)
3176 trunc_string(buf, buf, maxlen);
3177 }
3178 }
3179 }
3180 mustset = ti_change(t_str, &lasttitle);
3181
3182 if (p_icon)
3183 {
3184 i_str = buf;
3185 if (*p_iconstring != NUL)
3186 {
3187#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003188 if (stl_syntax & STL_IN_ICON)
3189 {
3190 int use_sandbox = FALSE;
3191 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003192
3193# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003194 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003195# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003196 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003198 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003199 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003200 if (called_emsg)
3201 set_string_option_direct((char_u *)"iconstring", -1,
3202 (char_u *)"", OPT_FREE, SID_ERROR);
3203 called_emsg |= save_called_emsg;
3204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 else
3206#endif
3207 i_str = p_iconstring;
3208 }
3209 else
3210 {
3211 if (buf_spname(curbuf) != NULL)
3212 i_name = (char_u *)buf_spname(curbuf);
3213 else /* use file name only in icon */
3214 i_name = gettail(curbuf->b_ffname);
3215 *i_str = NUL;
3216 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003217 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 if (len > 100)
3219 {
3220 len -= 100;
3221#ifdef FEAT_MBYTE
3222 if (has_mbyte)
3223 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3224#endif
3225 i_name += len;
3226 }
3227 STRCPY(i_str, i_name);
3228 trans_characters(i_str, IOSIZE);
3229 }
3230 }
3231
3232 mustset |= ti_change(i_str, &lasticon);
3233
3234 if (mustset)
3235 resettitle();
3236}
3237
3238/*
3239 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3240 * from "str" if it does.
3241 * Return TRUE when "*last" changed.
3242 */
3243 static int
3244ti_change(str, last)
3245 char_u *str;
3246 char_u **last;
3247{
3248 if ((str == NULL) != (*last == NULL)
3249 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3250 {
3251 vim_free(*last);
3252 if (str == NULL)
3253 *last = NULL;
3254 else
3255 *last = vim_strsave(str);
3256 return TRUE;
3257 }
3258 return FALSE;
3259}
3260
3261/*
3262 * Put current window title back (used after calling a shell)
3263 */
3264 void
3265resettitle()
3266{
3267 mch_settitle(lasttitle, lasticon);
3268}
Bram Moolenaarea408852005-06-25 22:49:46 +00003269
3270# if defined(EXITFREE) || defined(PROTO)
3271 void
3272free_titles()
3273{
3274 vim_free(lasttitle);
3275 vim_free(lasticon);
3276}
3277# endif
3278
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279#endif /* FEAT_TITLE */
3280
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003281#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003283 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 * Return length of string in screen cells.
3285 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003286 * Normally works for window "wp", except when working for 'tabline' then it
3287 * is "curwin".
3288 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 * Items are drawn interspersed with the text that surrounds it
3290 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3291 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3292 *
3293 * If maxwidth is not zero, the string will be filled at any middle marker
3294 * or truncated if too long, fillchar is used for all whitespace.
3295 */
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003296/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003298build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003300 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 size_t outlen; /* length of out[] */
3302 char_u *fmt;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003303 int use_sandbox; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 int fillchar;
3305 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003306 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3307 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308{
3309 char_u *p;
3310 char_u *s;
3311 char_u *t;
3312 char_u *linecont;
3313#ifdef FEAT_EVAL
3314 win_T *o_curwin;
3315 buf_T *o_curbuf;
3316#endif
3317 int empty_line;
3318 colnr_T virtcol;
3319 long l;
3320 long n;
3321 int prevchar_isflag;
3322 int prevchar_isitem;
3323 int itemisflag;
3324 int fillable;
3325 char_u *str;
3326 long num;
3327 int width;
3328 int itemcnt;
3329 int curitem;
3330 int groupitem[STL_MAX_ITEM];
3331 int groupdepth;
3332 struct stl_item
3333 {
3334 char_u *start;
3335 int minwid;
3336 int maxwid;
3337 enum
3338 {
3339 Normal,
3340 Empty,
3341 Group,
3342 Middle,
3343 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003344 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 Trunc
3346 } type;
3347 } item[STL_MAX_ITEM];
3348 int minwid;
3349 int maxwid;
3350 int zeropad;
3351 char_u base;
3352 char_u opt;
3353#define TMPLEN 70
3354 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003355 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003356 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003357
3358#ifdef FEAT_EVAL
3359 /*
3360 * When the format starts with "%!" then evaluate it as an expression and
3361 * use the result as the actual format string.
3362 */
3363 if (fmt[0] == '%' && fmt[1] == '!')
3364 {
3365 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3366 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003367 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003368 }
3369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370
3371 if (fillchar == 0)
3372 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003373#ifdef FEAT_MBYTE
3374 /* Can't handle a multi-byte fill character yet. */
3375 else if (mb_char2len(fillchar) > 1)
3376 fillchar = '-';
3377#endif
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 /*
3380 * Get line & check if empty (cursorpos will show "0-1").
3381 * If inversion is possible we use it. Else '=' characters are used.
3382 */
3383 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3384 empty_line = (*linecont == NUL);
3385
3386 groupdepth = 0;
3387 p = out;
3388 curitem = 0;
3389 prevchar_isflag = TRUE;
3390 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003391 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
3393 if (*s != NUL && *s != '%')
3394 prevchar_isflag = prevchar_isitem = FALSE;
3395
3396 /*
3397 * Handle up to the next '%' or the end.
3398 */
3399 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3400 *p++ = *s++;
3401 if (*s == NUL || p + 1 >= out + outlen)
3402 break;
3403
3404 /*
3405 * Handle one '%' item.
3406 */
3407 s++;
3408 if (*s == '%')
3409 {
3410 if (p + 1 >= out + outlen)
3411 break;
3412 *p++ = *s++;
3413 prevchar_isflag = prevchar_isitem = FALSE;
3414 continue;
3415 }
3416 if (*s == STL_MIDDLEMARK)
3417 {
3418 s++;
3419 if (groupdepth > 0)
3420 continue;
3421 item[curitem].type = Middle;
3422 item[curitem++].start = p;
3423 continue;
3424 }
3425 if (*s == STL_TRUNCMARK)
3426 {
3427 s++;
3428 item[curitem].type = Trunc;
3429 item[curitem++].start = p;
3430 continue;
3431 }
3432 if (*s == ')')
3433 {
3434 s++;
3435 if (groupdepth < 1)
3436 continue;
3437 groupdepth--;
3438
3439 t = item[groupitem[groupdepth]].start;
3440 *p = NUL;
3441 l = vim_strsize(t);
3442 if (curitem > groupitem[groupdepth] + 1
3443 && item[groupitem[groupdepth]].minwid == 0)
3444 {
3445 /* remove group if all items are empty */
3446 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3447 if (item[n].type == Normal)
3448 break;
3449 if (n == curitem)
3450 {
3451 p = t;
3452 l = 0;
3453 }
3454 }
3455 if (l > item[groupitem[groupdepth]].maxwid)
3456 {
3457 /* truncate, remove n bytes of text at the start */
3458#ifdef FEAT_MBYTE
3459 if (has_mbyte)
3460 {
3461 /* Find the first character that should be included. */
3462 n = 0;
3463 while (l >= item[groupitem[groupdepth]].maxwid)
3464 {
3465 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003466 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 }
3468 }
3469 else
3470#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003471 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
3473 *t = '<';
3474 mch_memmove(t + 1, t + n, p - (t + n));
3475 p = p - n + 1;
3476#ifdef FEAT_MBYTE
3477 /* Fill up space left over by half a double-wide char. */
3478 while (++l < item[groupitem[groupdepth]].minwid)
3479 *p++ = fillchar;
3480#endif
3481
3482 /* correct the start of the items for the truncation */
3483 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3484 {
3485 item[l].start -= n;
3486 if (item[l].start < t)
3487 item[l].start = t;
3488 }
3489 }
3490 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3491 {
3492 /* fill */
3493 n = item[groupitem[groupdepth]].minwid;
3494 if (n < 0)
3495 {
3496 /* fill by appending characters */
3497 n = 0 - n;
3498 while (l++ < n && p + 1 < out + outlen)
3499 *p++ = fillchar;
3500 }
3501 else
3502 {
3503 /* fill by inserting characters */
3504 mch_memmove(t + n - l, t, p - t);
3505 l = n - l;
3506 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003507 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 p += l;
3509 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3510 item[n].start += l;
3511 for ( ; l > 0; l--)
3512 *t++ = fillchar;
3513 }
3514 }
3515 continue;
3516 }
3517 minwid = 0;
3518 maxwid = 9999;
3519 zeropad = FALSE;
3520 l = 1;
3521 if (*s == '0')
3522 {
3523 s++;
3524 zeropad = TRUE;
3525 }
3526 if (*s == '-')
3527 {
3528 s++;
3529 l = -1;
3530 }
3531 if (VIM_ISDIGIT(*s))
3532 {
3533 minwid = (int)getdigits(&s);
3534 if (minwid < 0) /* overflow */
3535 minwid = 0;
3536 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003537 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
3539 item[curitem].type = Highlight;
3540 item[curitem].start = p;
3541 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3542 s++;
3543 curitem++;
3544 continue;
3545 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003546 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3547 {
3548 if (*s == STL_TABCLOSENR)
3549 {
3550 if (minwid == 0)
3551 {
3552 /* %X ends the close label, go back to the previously
3553 * define tab label nr. */
3554 for (n = curitem - 1; n >= 0; --n)
3555 if (item[n].type == TabPage && item[n].minwid >= 0)
3556 {
3557 minwid = item[n].minwid;
3558 break;
3559 }
3560 }
3561 else
3562 /* close nrs are stored as negative values */
3563 minwid = - minwid;
3564 }
3565 item[curitem].type = TabPage;
3566 item[curitem].start = p;
3567 item[curitem].minwid = minwid;
3568 s++;
3569 curitem++;
3570 continue;
3571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 if (*s == '.')
3573 {
3574 s++;
3575 if (VIM_ISDIGIT(*s))
3576 {
3577 maxwid = (int)getdigits(&s);
3578 if (maxwid <= 0) /* overflow */
3579 maxwid = 50;
3580 }
3581 }
3582 minwid = (minwid > 50 ? 50 : minwid) * l;
3583 if (*s == '(')
3584 {
3585 groupitem[groupdepth++] = curitem;
3586 item[curitem].type = Group;
3587 item[curitem].start = p;
3588 item[curitem].minwid = minwid;
3589 item[curitem].maxwid = maxwid;
3590 s++;
3591 curitem++;
3592 continue;
3593 }
3594 if (vim_strchr(STL_ALL, *s) == NULL)
3595 {
3596 s++;
3597 continue;
3598 }
3599 opt = *s++;
3600
3601 /* OK - now for the real work */
3602 base = 'D';
3603 itemisflag = FALSE;
3604 fillable = TRUE;
3605 num = -1;
3606 str = NULL;
3607 switch (opt)
3608 {
3609 case STL_FILEPATH:
3610 case STL_FULLPATH:
3611 case STL_FILENAME:
3612 fillable = FALSE; /* don't change ' ' to fillchar */
3613 if (buf_spname(wp->w_buffer) != NULL)
3614 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3615 else
3616 {
3617 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003618 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3620 }
3621 trans_characters(NameBuff, MAXPATHL);
3622 if (opt != STL_FILENAME)
3623 str = NameBuff;
3624 else
3625 str = gettail(NameBuff);
3626 break;
3627
3628 case STL_VIM_EXPR: /* '{' */
3629 itemisflag = TRUE;
3630 t = p;
3631 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3632 *p++ = *s++;
3633 if (*s != '}') /* missing '}' or out of space */
3634 break;
3635 s++;
3636 *p = 0;
3637 p = t;
3638
3639#ifdef FEAT_EVAL
3640 sprintf((char *)tmp, "%d", curbuf->b_fnum);
3641 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3642
3643 o_curbuf = curbuf;
3644 o_curwin = curwin;
3645 curwin = wp;
3646 curbuf = wp->w_buffer;
3647
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003648 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
3650 curwin = o_curwin;
3651 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003652 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653
3654 if (str != NULL && *str != 0)
3655 {
3656 if (*skipdigits(str) == NUL)
3657 {
3658 num = atoi((char *)str);
3659 vim_free(str);
3660 str = NULL;
3661 itemisflag = FALSE;
3662 }
3663 }
3664#endif
3665 break;
3666
3667 case STL_LINE:
3668 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3669 ? 0L : (long)(wp->w_cursor.lnum);
3670 break;
3671
3672 case STL_NUMLINES:
3673 num = wp->w_buffer->b_ml.ml_line_count;
3674 break;
3675
3676 case STL_COLUMN:
3677 num = !(State & INSERT) && empty_line
3678 ? 0 : (int)wp->w_cursor.col + 1;
3679 break;
3680
3681 case STL_VIRTCOL:
3682 case STL_VIRTCOL_ALT:
3683 /* In list mode virtcol needs to be recomputed */
3684 virtcol = wp->w_virtcol;
3685 if (wp->w_p_list && lcs_tab1 == NUL)
3686 {
3687 wp->w_p_list = FALSE;
3688 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3689 wp->w_p_list = TRUE;
3690 }
3691 ++virtcol;
3692 /* Don't display %V if it's the same as %c. */
3693 if (opt == STL_VIRTCOL_ALT
3694 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3695 ? 0 : (int)wp->w_cursor.col + 1)))
3696 break;
3697 num = (long)virtcol;
3698 break;
3699
3700 case STL_PERCENTAGE:
3701 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3702 (long)wp->w_buffer->b_ml.ml_line_count);
3703 break;
3704
3705 case STL_ALTPERCENT:
3706 str = tmp;
3707 get_rel_pos(wp, str);
3708 break;
3709
3710 case STL_ARGLISTSTAT:
3711 fillable = FALSE;
3712 tmp[0] = 0;
3713 if (append_arg_number(wp, tmp, FALSE, (int)sizeof(tmp)))
3714 str = tmp;
3715 break;
3716
3717 case STL_KEYMAP:
3718 fillable = FALSE;
3719 if (get_keymap_str(wp, tmp, TMPLEN))
3720 str = tmp;
3721 break;
3722 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003723#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003724 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725#else
3726 num = 0;
3727#endif
3728 break;
3729
3730 case STL_BUFNO:
3731 num = wp->w_buffer->b_fnum;
3732 break;
3733
3734 case STL_OFFSET_X:
3735 base = 'X';
3736 case STL_OFFSET:
3737#ifdef FEAT_BYTEOFF
3738 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3739 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3740 0L : l + 1 + (!(State & INSERT) && empty_line ?
3741 0 : (int)wp->w_cursor.col);
3742#endif
3743 break;
3744
3745 case STL_BYTEVAL_X:
3746 base = 'X';
3747 case STL_BYTEVAL:
Bram Moolenaar49104e42007-03-15 21:54:01 +00003748 if (wp->w_cursor.col > STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 num = 0;
3750 else
3751 {
3752#ifdef FEAT_MBYTE
3753 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3754#else
3755 num = linecont[wp->w_cursor.col];
3756#endif
3757 }
3758 if (num == NL)
3759 num = 0;
3760 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3761 num = NL;
3762 break;
3763
3764 case STL_ROFLAG:
3765 case STL_ROFLAG_ALT:
3766 itemisflag = TRUE;
3767 if (wp->w_buffer->b_p_ro)
3768 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3769 break;
3770
3771 case STL_HELPFLAG:
3772 case STL_HELPFLAG_ALT:
3773 itemisflag = TRUE;
3774 if (wp->w_buffer->b_help)
3775 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003776 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 break;
3778
3779#ifdef FEAT_AUTOCMD
3780 case STL_FILETYPE:
3781 if (*wp->w_buffer->b_p_ft != NUL
3782 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3783 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003784 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3785 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 str = tmp;
3787 }
3788 break;
3789
3790 case STL_FILETYPE_ALT:
3791 itemisflag = TRUE;
3792 if (*wp->w_buffer->b_p_ft != NUL
3793 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3794 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003795 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3796 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 for (t = tmp; *t != 0; t++)
3798 *t = TOUPPER_LOC(*t);
3799 str = tmp;
3800 }
3801 break;
3802#endif
3803
3804#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3805 case STL_PREVIEWFLAG:
3806 case STL_PREVIEWFLAG_ALT:
3807 itemisflag = TRUE;
3808 if (wp->w_p_pvw)
3809 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3810 : _("[Preview]"));
3811 break;
3812#endif
3813
3814 case STL_MODIFIED:
3815 case STL_MODIFIED_ALT:
3816 itemisflag = TRUE;
3817 switch ((opt == STL_MODIFIED_ALT)
3818 + bufIsChanged(wp->w_buffer) * 2
3819 + (!wp->w_buffer->b_p_ma) * 4)
3820 {
3821 case 2: str = (char_u *)"[+]"; break;
3822 case 3: str = (char_u *)",+"; break;
3823 case 4: str = (char_u *)"[-]"; break;
3824 case 5: str = (char_u *)",-"; break;
3825 case 6: str = (char_u *)"[+-]"; break;
3826 case 7: str = (char_u *)",+-"; break;
3827 }
3828 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003829
3830 case STL_HIGHLIGHT:
3831 t = s;
3832 while (*s != '#' && *s != NUL)
3833 ++s;
3834 if (*s == '#')
3835 {
3836 item[curitem].type = Highlight;
3837 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003838 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003839 curitem++;
3840 }
3841 ++s;
3842 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 }
3844
3845 item[curitem].start = p;
3846 item[curitem].type = Normal;
3847 if (str != NULL && *str)
3848 {
3849 t = str;
3850 if (itemisflag)
3851 {
3852 if ((t[0] && t[1])
3853 && ((!prevchar_isitem && *t == ',')
3854 || (prevchar_isflag && *t == ' ')))
3855 t++;
3856 prevchar_isflag = TRUE;
3857 }
3858 l = vim_strsize(t);
3859 if (l > 0)
3860 prevchar_isitem = TRUE;
3861 if (l > maxwid)
3862 {
3863 while (l >= maxwid)
3864#ifdef FEAT_MBYTE
3865 if (has_mbyte)
3866 {
3867 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003868 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
3870 else
3871#endif
3872 l -= byte2cells(*t++);
3873 if (p + 1 >= out + outlen)
3874 break;
3875 *p++ = '<';
3876 }
3877 if (minwid > 0)
3878 {
3879 for (; l < minwid && p + 1 < out + outlen; l++)
3880 {
3881 /* Don't put a "-" in front of a digit. */
3882 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3883 *p++ = ' ';
3884 else
3885 *p++ = fillchar;
3886 }
3887 minwid = 0;
3888 }
3889 else
3890 minwid *= -1;
3891 while (*t && p + 1 < out + outlen)
3892 {
3893 *p++ = *t++;
3894 /* Change a space by fillchar, unless fillchar is '-' and a
3895 * digit follows. */
3896 if (fillable && p[-1] == ' '
3897 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3898 p[-1] = fillchar;
3899 }
3900 for (; l < minwid && p + 1 < out + outlen; l++)
3901 *p++ = fillchar;
3902 }
3903 else if (num >= 0)
3904 {
3905 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3906 char_u nstr[20];
3907
3908 if (p + 20 >= out + outlen)
3909 break; /* not sufficient space */
3910 prevchar_isitem = TRUE;
3911 t = nstr;
3912 if (opt == STL_VIRTCOL_ALT)
3913 {
3914 *t++ = '-';
3915 minwid--;
3916 }
3917 *t++ = '%';
3918 if (zeropad)
3919 *t++ = '0';
3920 *t++ = '*';
3921 *t++ = nbase == 16 ? base : (nbase == 8 ? 'o' : 'd');
3922 *t = 0;
3923
3924 for (n = num, l = 1; n >= nbase; n /= nbase)
3925 l++;
3926 if (opt == STL_VIRTCOL_ALT)
3927 l++;
3928 if (l > maxwid)
3929 {
3930 l += 2;
3931 n = l - maxwid;
3932 while (l-- > maxwid)
3933 num /= nbase;
3934 *t++ = '>';
3935 *t++ = '%';
3936 *t = t[-3];
3937 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003938 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3939 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 }
3941 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003942 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3943 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 p += STRLEN(p);
3945 }
3946 else
3947 item[curitem].type = Empty;
3948
3949 if (opt == STL_VIM_EXPR)
3950 vim_free(str);
3951
3952 if (num >= 0 || (!itemisflag && str && *str))
3953 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
3954 curitem++;
3955 }
3956 *p = NUL;
3957 itemcnt = curitem;
3958
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003959#ifdef FEAT_EVAL
3960 if (usefmt != fmt)
3961 vim_free(usefmt);
3962#endif
3963
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 width = vim_strsize(out);
3965 if (maxwidth > 0 && width > maxwidth)
3966 {
3967 /* Result is too long, must trunctate somewhere. */
3968 l = 0;
3969 if (itemcnt == 0)
3970 s = out;
3971 else
3972 {
3973 for ( ; l < itemcnt; l++)
3974 if (item[l].type == Trunc)
3975 {
3976 /* Truncate at %< item. */
3977 s = item[l].start;
3978 break;
3979 }
3980 if (l == itemcnt)
3981 {
3982 /* No %< item, truncate first item. */
3983 s = item[0].start;
3984 l = 0;
3985 }
3986 }
3987
3988 if (width - vim_strsize(s) >= maxwidth)
3989 {
3990 /* Truncation mark is beyond max length */
3991#ifdef FEAT_MBYTE
3992 if (has_mbyte)
3993 {
3994 s = out;
3995 width = 0;
3996 for (;;)
3997 {
3998 width += ptr2cells(s);
3999 if (width >= maxwidth)
4000 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004001 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 }
4003 /* Fill up for half a double-wide character. */
4004 while (++width < maxwidth)
4005 *s++ = fillchar;
4006 }
4007 else
4008#endif
4009 s = out + maxwidth - 1;
4010 for (l = 0; l < itemcnt; l++)
4011 if (item[l].start > s)
4012 break;
4013 itemcnt = l;
4014 *s++ = '>';
4015 *s = 0;
4016 }
4017 else
4018 {
4019#ifdef FEAT_MBYTE
4020 if (has_mbyte)
4021 {
4022 n = 0;
4023 while (width >= maxwidth)
4024 {
4025 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004026 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028 }
4029 else
4030#endif
4031 n = width - maxwidth + 1;
4032 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004033 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 *s = '<';
4035
4036 /* Fill up for half a double-wide character. */
4037 while (++width < maxwidth)
4038 {
4039 s = s + STRLEN(s);
4040 *s++ = fillchar;
4041 *s = NUL;
4042 }
4043
4044 --n; /* count the '<' */
4045 for (; l < itemcnt; l++)
4046 {
4047 if (item[l].start - n >= s)
4048 item[l].start -= n;
4049 else
4050 item[l].start = s;
4051 }
4052 }
4053 width = maxwidth;
4054 }
4055 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4056 {
4057 /* Apply STL_MIDDLE if any */
4058 for (l = 0; l < itemcnt; l++)
4059 if (item[l].type == Middle)
4060 break;
4061 if (l < itemcnt)
4062 {
4063 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004064 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 for (s = item[l].start; s < p; s++)
4066 *s = fillchar;
4067 for (l++; l < itemcnt; l++)
4068 item[l].start += maxwidth - width;
4069 width = maxwidth;
4070 }
4071 }
4072
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004073 /* Store the info about highlighting. */
4074 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004076 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 for (l = 0; l < itemcnt; l++)
4078 {
4079 if (item[l].type == Highlight)
4080 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004081 sp->start = item[l].start;
4082 sp->userhl = item[l].minwid;
4083 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 }
4085 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004086 sp->start = NULL;
4087 sp->userhl = 0;
4088 }
4089
4090 /* Store the info about tab pages labels. */
4091 if (tabtab != NULL)
4092 {
4093 sp = tabtab;
4094 for (l = 0; l < itemcnt; l++)
4095 {
4096 if (item[l].type == TabPage)
4097 {
4098 sp->start = item[l].start;
4099 sp->userhl = item[l].minwid;
4100 sp++;
4101 }
4102 }
4103 sp->start = NULL;
4104 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
4106
4107 return width;
4108}
4109#endif /* FEAT_STL_OPT */
4110
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004111#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4112 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004114 * Get relative cursor position in window into "str[]", in the form 99%, using
4115 * "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 */
4117 void
4118get_rel_pos(wp, str)
4119 win_T *wp;
4120 char_u *str;
4121{
4122 long above; /* number of lines above window */
4123 long below; /* number of lines below window */
4124
4125 above = wp->w_topline - 1;
4126#ifdef FEAT_DIFF
4127 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4128#endif
4129 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4130 if (below <= 0)
4131 STRCPY(str, above == 0 ? _("All") : _("Bot"));
4132 else if (above <= 0)
4133 STRCPY(str, _("Top"));
4134 else
4135 sprintf((char *)str, "%2d%%", above > 1000000L
4136 ? (int)(above / ((above + below) / 100L))
4137 : (int)(above * 100L / (above + below)));
4138}
4139#endif
4140
4141/*
4142 * Append (file 2 of 8) to 'buf', if editing more than one file.
4143 * Return TRUE if it was appended.
4144 */
4145 int
4146append_arg_number(wp, buf, add_file, maxlen)
4147 win_T *wp;
4148 char_u *buf;
4149 int add_file; /* Add "file" before the arg number */
4150 int maxlen; /* maximum nr of chars in buf or zero*/
4151{
4152 char_u *p;
4153
4154 if (ARGCOUNT <= 1) /* nothing to do */
4155 return FALSE;
4156
4157 p = buf + STRLEN(buf); /* go to the end of the buffer */
4158 if (maxlen && p - buf + 35 >= maxlen) /* getting too long */
4159 return FALSE;
4160 *p++ = ' ';
4161 *p++ = '(';
4162 if (add_file)
4163 {
4164 STRCPY(p, "file ");
4165 p += 5;
4166 }
4167 sprintf((char *)p, wp->w_arg_idx_invalid ? "(%d) of %d)"
4168 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4169 return TRUE;
4170}
4171
4172/*
4173 * If fname is not a full path, make it a full path.
4174 * Returns pointer to allocated memory (NULL for failure).
4175 */
4176 char_u *
4177fix_fname(fname)
4178 char_u *fname;
4179{
4180 /*
4181 * Force expanding the path always for Unix, because symbolic links may
4182 * mess up the full path name, even though it starts with a '/'.
4183 * Also expand when there is ".." in the file name, try to remove it,
4184 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004185 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 * For MS-Windows also expand names like "longna~1" to "longname".
4187 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004188#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 return FullName_save(fname, TRUE);
4190#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004191 if (!vim_isAbsName(fname)
4192 || strstr((char *)fname, "..") != NULL
4193 || strstr((char *)fname, "//") != NULL
4194# ifdef BACKSLASH_IN_FILENAME
4195 || strstr((char *)fname, "\\\\") != NULL
4196# endif
4197# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 )
4201 return FullName_save(fname, FALSE);
4202
4203 fname = vim_strsave(fname);
4204
Bram Moolenaar9b942202007-10-03 12:31:33 +00004205# ifdef USE_FNAME_CASE
4206# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004208# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
4210 if (fname != NULL)
4211 fname_case(fname, 0); /* set correct case for file name */
4212 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
4215 return fname;
4216#endif
4217}
4218
4219/*
4220 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4221 * "ffname" becomes a pointer to allocated memory (or NULL).
4222 */
4223/*ARGSUSED*/
4224 void
4225fname_expand(buf, ffname, sfname)
4226 buf_T *buf;
4227 char_u **ffname;
4228 char_u **sfname;
4229{
4230 if (*ffname == NULL) /* if no file name given, nothing to do */
4231 return;
4232 if (*sfname == NULL) /* if no short file name given, use ffname */
4233 *sfname = *ffname;
4234 *ffname = fix_fname(*ffname); /* expand to full path */
4235
4236#ifdef FEAT_SHORTCUT
4237 if (!buf->b_p_bin)
4238 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004239 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
4241 /* If the file name is a shortcut file, use the file it links to. */
4242 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004243 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
4245 vim_free(*ffname);
4246 *ffname = rfname;
4247 *sfname = rfname;
4248 }
4249 }
4250#endif
4251}
4252
4253/*
4254 * Get the file name for an argument list entry.
4255 */
4256 char_u *
4257alist_name(aep)
4258 aentry_T *aep;
4259{
4260 buf_T *bp;
4261
4262 /* Use the name from the associated buffer if it exists. */
4263 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004264 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 return aep->ae_fname;
4266 return bp->b_fname;
4267}
4268
4269#if defined(FEAT_WINDOWS) || defined(PROTO)
4270/*
4271 * do_arg_all(): Open up to 'count' windows, one for each argument.
4272 */
4273 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004274do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 int count;
4276 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004277 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278{
4279 int i;
4280 win_T *wp, *wpnext;
4281 char_u *opened; /* array of flags for which args are open */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004282 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 int use_firstwin = FALSE; /* use first window for arglist */
4284 int split_ret = OK;
4285 int p_ea_save;
4286 alist_T *alist; /* argument list to be used */
4287 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004288 tabpage_T *tpnext;
4289 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004290 win_T *new_curwin = NULL;
4291 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
4293 if (ARGCOUNT <= 0)
4294 {
4295 /* Don't give an error message. We don't want it when the ":all"
4296 * command is in the .vimrc. */
4297 return;
4298 }
4299 setpcmark();
4300
4301 opened_len = ARGCOUNT;
4302 opened = alloc_clear((unsigned)opened_len);
4303 if (opened == NULL)
4304 return;
4305
4306#ifdef FEAT_GUI
4307 need_mouse_correct = TRUE;
4308#endif
4309
4310 /*
4311 * Try closing all windows that are not in the argument list.
4312 * Also close windows that are not full width;
4313 * When 'hidden' or "forceit" set the buffer becomes hidden.
4314 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004315 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004317 if (had_tab > 0)
4318 goto_tabpage_tp(first_tabpage);
4319 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004321 tpnext = curtab->tp_next;
4322 for (wp = firstwin; wp != NULL; wp = wpnext)
4323 {
4324 wpnext = wp->w_next;
4325 buf = wp->w_buffer;
4326 if (buf->b_ffname == NULL
4327 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004329 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004331 )
4332 i = ARGCOUNT;
4333 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004335 /* check if the buffer in this window is in the arglist */
4336 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004338 if (ARGLIST[i].ae_fnum == buf->b_fnum
4339 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004340 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004342 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004343 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004344 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004345 if (i == 0)
4346 {
4347 new_curwin = wp;
4348 new_curtab = curtab;
4349 }
4350 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004351 if (wp->w_alist != curwin->w_alist)
4352 {
4353 /* Use the current argument list for all windows
4354 * containing a file from it. */
4355 alist_unlink(wp->w_alist);
4356 wp->w_alist = curwin->w_alist;
4357 ++wp->w_alist->al_refcount;
4358 }
4359 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004363 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004365 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004367 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004368 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004370 /* If the buffer was changed, and we would like to hide it,
4371 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004372 if (!P_HID(buf) && buf->b_nwindows <= 1
4373 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004375 (void)autowrite(buf, FALSE);
4376#ifdef FEAT_AUTOCMD
4377 /* check if autocommands removed the window */
4378 if (!win_valid(wp) || !buf_valid(buf))
4379 {
4380 wpnext = firstwin; /* start all over... */
4381 continue;
4382 }
4383#endif
4384 }
4385#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004386 /* don't close last window */
4387 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004388#endif
4389 use_firstwin = TRUE;
4390#ifdef FEAT_WINDOWS
4391 else
4392 {
4393 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4394# ifdef FEAT_AUTOCMD
4395 /* check if autocommands removed the next window */
4396 if (!win_valid(wpnext))
4397 wpnext = firstwin; /* start all over... */
4398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 }
4400#endif
4401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 }
4403 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004404
4405 /* Without the ":tab" modifier only do the current tab page. */
4406 if (had_tab == 0 || tpnext == NULL)
4407 break;
4408
4409# ifdef FEAT_AUTOCMD
4410 /* check if autocommands removed the next tab page */
4411 if (!valid_tabpage(tpnext))
4412 tpnext = first_tabpage; /* start all over...*/
4413# endif
4414 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
4416
4417 /*
4418 * Open a window for files in the argument list that don't have one.
4419 * ARGCOUNT may change while doing this, because of autocommands.
4420 */
4421 if (count > ARGCOUNT || count <= 0)
4422 count = ARGCOUNT;
4423
4424 /* Autocommands may do anything to the argument list. Make sure it's not
4425 * freed while we are working here by "locking" it. We still have to
4426 * watch out for its size to be changed. */
4427 alist = curwin->w_alist;
4428 ++alist->al_refcount;
4429
4430#ifdef FEAT_AUTOCMD
4431 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4432 ++autocmd_no_enter;
4433 ++autocmd_no_leave;
4434#endif
4435 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004436#ifdef FEAT_WINDOWS
4437 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4438 * leaving an empty tab page when executed locally. */
4439 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4440 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4441 use_firstwin = TRUE;
4442#endif
4443
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4445 {
4446 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4447 arg_had_last = TRUE;
4448 if (i < opened_len && opened[i])
4449 {
4450 /* Move the already present window to below the current window */
4451 if (curwin->w_arg_idx != i)
4452 {
4453 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4454 {
4455 if (wpnext->w_arg_idx == i)
4456 {
4457 win_move_after(wpnext, curwin);
4458 break;
4459 }
4460 }
4461 }
4462 }
4463 else if (split_ret == OK)
4464 {
4465 if (!use_firstwin) /* split current window */
4466 {
4467 p_ea_save = p_ea;
4468 p_ea = TRUE; /* use space from all windows */
4469 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4470 p_ea = p_ea_save;
4471 if (split_ret == FAIL)
4472 continue;
4473 }
4474#ifdef FEAT_AUTOCMD
4475 else /* first window: do autocmd for leaving this buffer */
4476 --autocmd_no_leave;
4477#endif
4478
4479 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004480 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 */
4482 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004483 if (i == 0)
4484 {
4485 new_curwin = curwin;
4486 new_curtab = curtab;
4487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4489 ECMD_ONE,
4490 ((P_HID(curwin->w_buffer)
4491 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4492 + ECMD_OLDBUF);
4493#ifdef FEAT_AUTOCMD
4494 if (use_firstwin)
4495 ++autocmd_no_leave;
4496#endif
4497 use_firstwin = FALSE;
4498 }
4499 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004500
4501 /* When ":tab" was used open a new tab for a new window repeatedly. */
4502 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4503 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505
4506 /* Remove the "lock" on the argument list. */
4507 alist_unlink(alist);
4508
4509#ifdef FEAT_AUTOCMD
4510 --autocmd_no_enter;
4511#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004512 /* to window with first arg */
4513 if (valid_tabpage(new_curtab))
4514 goto_tabpage_tp(new_curtab);
4515 if (win_valid(new_curwin))
4516 win_enter(new_curwin, FALSE);
4517
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518#ifdef FEAT_AUTOCMD
4519 --autocmd_no_leave;
4520#endif
4521 vim_free(opened);
4522}
4523
4524# if defined(FEAT_LISTCMDS) || defined(PROTO)
4525/*
4526 * Open a window for a number of buffers.
4527 */
4528 void
4529ex_buffer_all(eap)
4530 exarg_T *eap;
4531{
4532 buf_T *buf;
4533 win_T *wp, *wpnext;
4534 int split_ret = OK;
4535 int p_ea_save;
4536 int open_wins = 0;
4537 int r;
4538 int count; /* Maximum number of windows to open. */
4539 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004540#ifdef FEAT_WINDOWS
4541 int had_tab = cmdmod.tab;
4542 tabpage_T *tpnext;
4543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
4545 if (eap->addr_count == 0) /* make as many windows as possible */
4546 count = 9999;
4547 else
4548 count = eap->line2; /* make as many windows as specified */
4549 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4550 all = FALSE;
4551 else
4552 all = TRUE;
4553
4554 setpcmark();
4555
4556#ifdef FEAT_GUI
4557 need_mouse_correct = TRUE;
4558#endif
4559
4560 /*
4561 * Close superfluous windows (two windows for the same buffer).
4562 * Also close windows that are not full-width.
4563 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004564#ifdef FEAT_WINDOWS
4565 if (had_tab > 0)
4566 goto_tabpage_tp(first_tabpage);
4567 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004570 tpnext = curtab->tp_next;
4571 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004573 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004574 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004575#ifdef FEAT_VERTSPLIT
4576 || ((cmdmod.split & WSP_VERT)
4577 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004578 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004579 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004581#ifdef FEAT_WINDOWS
4582 || (had_tab > 0 && wp != firstwin)
4583#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004584 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004585 {
4586 win_close(wp, FALSE);
4587#ifdef FEAT_AUTOCMD
4588 wpnext = firstwin; /* just in case an autocommand does
4589 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004590 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004591 open_wins = 0;
4592#endif
4593 }
4594 else
4595 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004597
4598#ifdef FEAT_WINDOWS
4599 /* Without the ":tab" modifier only do the current tab page. */
4600 if (had_tab == 0 || tpnext == NULL)
4601 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004602 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
4606 /*
4607 * Go through the buffer list. When a buffer doesn't have a window yet,
4608 * open one. Otherwise move the window to the right position.
4609 * Watch out for autocommands that delete buffers or windows!
4610 */
4611#ifdef FEAT_AUTOCMD
4612 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4613 ++autocmd_no_enter;
4614#endif
4615 win_enter(lastwin, FALSE);
4616#ifdef FEAT_AUTOCMD
4617 ++autocmd_no_leave;
4618#endif
4619 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4620 {
4621 /* Check if this buffer needs a window */
4622 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4623 continue;
4624
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004625#ifdef FEAT_WINDOWS
4626 if (had_tab != 0)
4627 {
4628 /* With the ":tab" modifier don't move the window. */
4629 if (buf->b_nwindows > 0)
4630 wp = lastwin; /* buffer has a window, skip it */
4631 else
4632 wp = NULL;
4633 }
4634 else
4635#endif
4636 {
4637 /* Check if this buffer already has a window */
4638 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4639 if (wp->w_buffer == buf)
4640 break;
4641 /* If the buffer already has a window, move it */
4642 if (wp != NULL)
4643 win_move_after(wp, curwin);
4644 }
4645
4646 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 {
4648 /* Split the window and put the buffer in it */
4649 p_ea_save = p_ea;
4650 p_ea = TRUE; /* use space from all windows */
4651 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4652 ++open_wins;
4653 p_ea = p_ea_save;
4654 if (split_ret == FAIL)
4655 continue;
4656
4657 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004658#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 swap_exists_action = SEA_DIALOG;
4660#endif
4661 set_curbuf(buf, DOBUF_GOTO);
4662#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004665#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004667# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 break;
4669 }
4670#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004671#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (swap_exists_action == SEA_QUIT)
4673 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004674# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4675 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004676
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004677 /* Reset the error/interrupt/exception state here so that
4678 * aborting() returns FALSE when closing a window. */
4679 enter_cleanup(&cs);
4680# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004681
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004682 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 win_close(curwin, TRUE);
4684 --open_wins;
4685 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004686 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004687
4688# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4689 /* Restore the error/interrupt/exception state if not
4690 * discarded by a new aborting error, interrupt, or uncaught
4691 * exception. */
4692 leave_cleanup(&cs);
4693# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
4695 else
4696 handle_swap_exists(NULL);
4697#endif
4698 }
4699
4700 ui_breakcheck();
4701 if (got_int)
4702 {
4703 (void)vgetc(); /* only break the file loading, not the rest */
4704 break;
4705 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004706#ifdef FEAT_EVAL
4707 /* Autocommands deleted the buffer or aborted script processing!!! */
4708 if (aborting())
4709 break;
4710#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004711#ifdef FEAT_WINDOWS
4712 /* When ":tab" was used open a new tab for a new window repeatedly. */
4713 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4714 cmdmod.tab = 9999;
4715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 }
4717#ifdef FEAT_AUTOCMD
4718 --autocmd_no_enter;
4719#endif
4720 win_enter(firstwin, FALSE); /* back to first window */
4721#ifdef FEAT_AUTOCMD
4722 --autocmd_no_leave;
4723#endif
4724
4725 /*
4726 * Close superfluous windows.
4727 */
4728 for (wp = lastwin; open_wins > count; )
4729 {
4730 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4731 || autowrite(wp->w_buffer, FALSE) == OK);
4732#ifdef FEAT_AUTOCMD
4733 if (!win_valid(wp))
4734 {
4735 /* BufWrite Autocommands made the window invalid, start over */
4736 wp = lastwin;
4737 }
4738 else
4739#endif
4740 if (r)
4741 {
4742 win_close(wp, !P_HID(wp->w_buffer));
4743 --open_wins;
4744 wp = lastwin;
4745 }
4746 else
4747 {
4748 wp = wp->w_prev;
4749 if (wp == NULL)
4750 break;
4751 }
4752 }
4753}
4754# endif /* FEAT_LISTCMDS */
4755
4756#endif /* FEAT_WINDOWS */
4757
Bram Moolenaara3227e22006-03-08 21:32:40 +00004758static int chk_modeline __ARGS((linenr_T, int));
4759
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760/*
4761 * do_modelines() - process mode lines for the current file
4762 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004763 * "flags" can be:
4764 * OPT_WINONLY only set options local to window
4765 * OPT_NOWIN don't set options local to window
4766 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 * Returns immediately if the "ml" option isn't set.
4768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004770do_modelines(flags)
4771 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004773 linenr_T lnum;
4774 int nmlines;
4775 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
4777 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4778 return;
4779
4780 /* Disallow recursive entry here. Can happen when executing a modeline
4781 * triggers an autocommand, which reloads modelines with a ":do". */
4782 if (entered)
4783 return;
4784
4785 ++entered;
4786 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4787 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004788 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 nmlines = 0;
4790
4791 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4792 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004793 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 nmlines = 0;
4795 --entered;
4796}
4797
4798#include "version.h" /* for version number */
4799
4800/*
4801 * chk_modeline() - check a single line for a mode string
4802 * Return FAIL if an error encountered.
4803 */
4804 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004805chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004807 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808{
4809 char_u *s;
4810 char_u *e;
4811 char_u *linecopy; /* local copy of any modeline found */
4812 int prev;
4813 int vers;
4814 int end;
4815 int retval = OK;
4816 char_u *save_sourcing_name;
4817 linenr_T save_sourcing_lnum;
4818#ifdef FEAT_EVAL
4819 scid_T save_SID;
4820#endif
4821
4822 prev = -1;
4823 for (s = ml_get(lnum); *s != NUL; ++s)
4824 {
4825 if (prev == -1 || vim_isspace(prev))
4826 {
4827 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4828 || STRNCMP(s, "vi:", (size_t)3) == 0)
4829 break;
4830 if (STRNCMP(s, "vim", 3) == 0)
4831 {
4832 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4833 e = s + 4;
4834 else
4835 e = s + 3;
4836 vers = getdigits(&e);
4837 if (*e == ':'
4838 && (s[3] == ':'
4839 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4840 || (VIM_VERSION_100 < vers && s[3] == '<')
4841 || (VIM_VERSION_100 > vers && s[3] == '>')
4842 || (VIM_VERSION_100 == vers && s[3] == '=')))
4843 break;
4844 }
4845 }
4846 prev = *s;
4847 }
4848
4849 if (*s)
4850 {
4851 do /* skip over "ex:", "vi:" or "vim:" */
4852 ++s;
4853 while (s[-1] != ':');
4854
4855 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4856 if (linecopy == NULL)
4857 return FAIL;
4858
4859 save_sourcing_lnum = sourcing_lnum;
4860 save_sourcing_name = sourcing_name;
4861 sourcing_lnum = lnum; /* prepare for emsg() */
4862 sourcing_name = (char_u *)"modelines";
4863
4864 end = FALSE;
4865 while (end == FALSE)
4866 {
4867 s = skipwhite(s);
4868 if (*s == NUL)
4869 break;
4870
4871 /*
4872 * Find end of set command: ':' or end of line.
4873 * Skip over "\:", replacing it with ":".
4874 */
4875 for (e = s; *e != ':' && *e != NUL; ++e)
4876 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00004877 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 if (*e == NUL)
4879 end = TRUE;
4880
4881 /*
4882 * If there is a "set" command, require a terminating ':' and
4883 * ignore the stuff after the ':'.
4884 * "vi:set opt opt opt: foo" -- foo not interpreted
4885 * "vi:opt opt opt: foo" -- foo interpreted
4886 * Accept "se" for compatibility with Elvis.
4887 */
4888 if (STRNCMP(s, "set ", (size_t)4) == 0
4889 || STRNCMP(s, "se ", (size_t)3) == 0)
4890 {
4891 if (*e != ':') /* no terminating ':'? */
4892 break;
4893 end = TRUE;
4894 s = vim_strchr(s, ' ') + 1;
4895 }
4896 *e = NUL; /* truncate the set command */
4897
4898 if (*s != NUL) /* skip over an empty "::" */
4899 {
4900#ifdef FEAT_EVAL
4901 save_SID = current_SID;
4902 current_SID = SID_MODELINE;
4903#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004904 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905#ifdef FEAT_EVAL
4906 current_SID = save_SID;
4907#endif
4908 if (retval == FAIL) /* stop if error found */
4909 break;
4910 }
4911 s = e + 1; /* advance to next part */
4912 }
4913
4914 sourcing_lnum = save_sourcing_lnum;
4915 sourcing_name = save_sourcing_name;
4916
4917 vim_free(linecopy);
4918 }
4919 return retval;
4920}
4921
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00004922#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 int
4924read_viminfo_bufferlist(virp, writing)
4925 vir_T *virp;
4926 int writing;
4927{
4928 char_u *tab;
4929 linenr_T lnum;
4930 colnr_T col;
4931 buf_T *buf;
4932 char_u *sfname;
4933 char_u *xline;
4934
4935 /* Handle long line and escaped characters. */
4936 xline = viminfo_readstring(virp, 1, FALSE);
4937
4938 /* don't read in if there are files on the command-line or if writing: */
4939 if (xline != NULL && !writing && ARGCOUNT == 0
4940 && find_viminfo_parameter('%') != NULL)
4941 {
4942 /* Format is: <fname> Tab <lnum> Tab <col>.
4943 * Watch out for a Tab in the file name, work from the end. */
4944 lnum = 0;
4945 col = 0;
4946 tab = vim_strrchr(xline, '\t');
4947 if (tab != NULL)
4948 {
4949 *tab++ = '\0';
4950 col = atoi((char *)tab);
4951 tab = vim_strrchr(xline, '\t');
4952 if (tab != NULL)
4953 {
4954 *tab++ = '\0';
4955 lnum = atol((char *)tab);
4956 }
4957 }
4958
4959 /* Expand "~/" in the file name at "line + 1" to a full path.
4960 * Then try shortening it by comparing with the current directory */
4961 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004962 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963
4964 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
4965 if (buf != NULL) /* just in case... */
4966 {
4967 buf->b_last_cursor.lnum = lnum;
4968 buf->b_last_cursor.col = col;
4969 buflist_setfpos(buf, curwin, lnum, col, FALSE);
4970 }
4971 }
4972 vim_free(xline);
4973
4974 return viminfo_readline(virp);
4975}
4976
4977 void
4978write_viminfo_bufferlist(fp)
4979 FILE *fp;
4980{
4981 buf_T *buf;
4982#ifdef FEAT_WINDOWS
4983 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004984 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985#endif
4986 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004987 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988
4989 if (find_viminfo_parameter('%') == NULL)
4990 return;
4991
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004992 /* Without a number -1 is returned: do all buffers. */
4993 max_buffers = get_viminfo_parameter('%');
4994
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004996 line = alloc(MAXPATHL + 40);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 if (line == NULL)
4998 return;
4999
5000#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005001 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 set_last_cursor(win);
5003#else
5004 set_last_cursor(curwin);
5005#endif
5006
5007 fprintf(fp, _("\n# Buffer list:\n"));
5008 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5009 {
5010 if (buf->b_fname == NULL
5011 || !buf->b_p_bl
5012#ifdef FEAT_QUICKFIX
5013 || bt_quickfix(buf)
5014#endif
5015 || removable(buf->b_ffname))
5016 continue;
5017
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005018 if (max_buffers-- == 0)
5019 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 putc('%', fp);
5021 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
5022 sprintf((char *)line + STRLEN(line), "\t%ld\t%d",
5023 (long)buf->b_last_cursor.lnum,
5024 buf->b_last_cursor.col);
5025 viminfo_writestring(fp, line);
5026 }
5027 vim_free(line);
5028}
5029#endif
5030
5031
5032/*
5033 * Return special buffer name.
5034 * Returns NULL when the buffer has a normal file name.
5035 */
5036 char *
5037buf_spname(buf)
5038 buf_T *buf;
5039{
5040#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5041 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005042 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005043 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005044 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005045
5046 /*
5047 * For location list window, w_llist_ref points to the location list.
5048 * For quickfix window, w_llist_ref is NULL.
5049 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005050 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005051 if (win->w_buffer == buf)
5052 break;
5053 if (win != NULL && win->w_llist_ref != NULL)
5054 return _("[Location List]");
5055 else
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005056 return _("[Quickfix List]");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058#endif
5059#ifdef FEAT_QUICKFIX
5060 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5061 * contains the name as specified by the user */
5062 if (bt_nofile(buf))
5063 {
5064 if (buf->b_sfname != NULL)
5065 return (char *)buf->b_sfname;
5066 return "[Scratch]";
5067 }
5068#endif
5069 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005070 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 return NULL;
5072}
5073
5074
5075#if defined(FEAT_SIGNS) || defined(PROTO)
5076/*
5077 * Insert the sign into the signlist.
5078 */
5079 static void
5080insert_sign(buf, prev, next, id, lnum, typenr)
5081 buf_T *buf; /* buffer to store sign in */
5082 signlist_T *prev; /* previous sign entry */
5083 signlist_T *next; /* next sign entry */
5084 int id; /* sign ID */
5085 linenr_T lnum; /* line number which gets the mark */
5086 int typenr; /* typenr of sign we are adding */
5087{
5088 signlist_T *newsign;
5089
5090 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5091 if (newsign != NULL)
5092 {
5093 newsign->id = id;
5094 newsign->lnum = lnum;
5095 newsign->typenr = typenr;
5096 newsign->next = next;
5097#ifdef FEAT_NETBEANS_INTG
5098 newsign->prev = prev;
5099 if (next != NULL)
5100 next->prev = newsign;
5101#endif
5102
5103 if (prev == NULL)
5104 {
5105 /* When adding first sign need to redraw the windows to create the
5106 * column for signs. */
5107 if (buf->b_signlist == NULL)
5108 {
5109 redraw_buf_later(buf, NOT_VALID);
5110 changed_cline_bef_curs();
5111 }
5112
5113 /* first sign in signlist */
5114 buf->b_signlist = newsign;
5115 }
5116 else
5117 prev->next = newsign;
5118 }
5119}
5120
5121/*
5122 * Add the sign into the signlist. Find the right spot to do it though.
5123 */
5124 void
5125buf_addsign(buf, id, lnum, typenr)
5126 buf_T *buf; /* buffer to store sign in */
5127 int id; /* sign ID */
5128 linenr_T lnum; /* line number which gets the mark */
5129 int typenr; /* typenr of sign we are adding */
5130{
5131 signlist_T *sign; /* a sign in the signlist */
5132 signlist_T *prev; /* the previous sign */
5133
5134 prev = NULL;
5135 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5136 {
5137 if (lnum == sign->lnum && id == sign->id)
5138 {
5139 sign->typenr = typenr;
5140 return;
5141 }
5142 else if (
5143#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5144 id < 0 &&
5145#endif
5146 lnum < sign->lnum)
5147 {
5148#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5149 /* XXX - GRP: Is this because of sign slide problem? Or is it
5150 * really needed? Or is it because we allow multiple signs per
5151 * line? If so, should I add that feature to FEAT_SIGNS?
5152 */
5153 while (prev != NULL && prev->lnum == lnum)
5154 prev = prev->prev;
5155 if (prev == NULL)
5156 sign = buf->b_signlist;
5157 else
5158 sign = prev->next;
5159#endif
5160 insert_sign(buf, prev, sign, id, lnum, typenr);
5161 return;
5162 }
5163 prev = sign;
5164 }
5165#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5166 /* XXX - GRP: See previous comment */
5167 while (prev != NULL && prev->lnum == lnum)
5168 prev = prev->prev;
5169 if (prev == NULL)
5170 sign = buf->b_signlist;
5171 else
5172 sign = prev->next;
5173#endif
5174 insert_sign(buf, prev, sign, id, lnum, typenr);
5175
5176 return;
5177}
5178
5179 int
5180buf_change_sign_type(buf, markId, typenr)
5181 buf_T *buf; /* buffer to store sign in */
5182 int markId; /* sign ID */
5183 int typenr; /* typenr of sign we are adding */
5184{
5185 signlist_T *sign; /* a sign in the signlist */
5186
5187 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5188 {
5189 if (sign->id == markId)
5190 {
5191 sign->typenr = typenr;
5192 return sign->lnum;
5193 }
5194 }
5195
5196 return 0;
5197}
5198
5199 int_u
5200buf_getsigntype(buf, lnum, type)
5201 buf_T *buf;
5202 linenr_T lnum;
5203 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5204{
5205 signlist_T *sign; /* a sign in a b_signlist */
5206
5207 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5208 if (sign->lnum == lnum
5209 && (type == SIGN_ANY
5210# ifdef FEAT_SIGN_ICONS
5211 || (type == SIGN_ICON
5212 && sign_get_image(sign->typenr) != NULL)
5213# endif
5214 || (type == SIGN_TEXT
5215 && sign_get_text(sign->typenr) != NULL)
5216 || (type == SIGN_LINEHL
5217 && sign_get_attr(sign->typenr, TRUE) != 0)))
5218 return sign->typenr;
5219 return 0;
5220}
5221
5222
5223 linenr_T
5224buf_delsign(buf, id)
5225 buf_T *buf; /* buffer sign is stored in */
5226 int id; /* sign id */
5227{
5228 signlist_T **lastp; /* pointer to pointer to current sign */
5229 signlist_T *sign; /* a sign in a b_signlist */
5230 signlist_T *next; /* the next sign in a b_signlist */
5231 linenr_T lnum; /* line number whose sign was deleted */
5232
5233 lastp = &buf->b_signlist;
5234 lnum = 0;
5235 for (sign = buf->b_signlist; sign != NULL; sign = next)
5236 {
5237 next = sign->next;
5238 if (sign->id == id)
5239 {
5240 *lastp = next;
5241#ifdef FEAT_NETBEANS_INTG
5242 if (next != NULL)
5243 next->prev = sign->prev;
5244#endif
5245 lnum = sign->lnum;
5246 vim_free(sign);
5247 break;
5248 }
5249 else
5250 lastp = &sign->next;
5251 }
5252
5253 /* When deleted the last sign need to redraw the windows to remove the
5254 * sign column. */
5255 if (buf->b_signlist == NULL)
5256 {
5257 redraw_buf_later(buf, NOT_VALID);
5258 changed_cline_bef_curs();
5259 }
5260
5261 return lnum;
5262}
5263
5264
5265/*
5266 * Find the line number of the sign with the requested id. If the sign does
5267 * not exist, return 0 as the line number. This will still let the correct file
5268 * get loaded.
5269 */
5270 int
5271buf_findsign(buf, id)
5272 buf_T *buf; /* buffer to store sign in */
5273 int id; /* sign ID */
5274{
5275 signlist_T *sign; /* a sign in the signlist */
5276
5277 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5278 if (sign->id == id)
5279 return sign->lnum;
5280
5281 return 0;
5282}
5283
5284 int
5285buf_findsign_id(buf, lnum)
5286 buf_T *buf; /* buffer whose sign we are searching for */
5287 linenr_T lnum; /* line number of sign */
5288{
5289 signlist_T *sign; /* a sign in the signlist */
5290
5291 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5292 if (sign->lnum == lnum)
5293 return sign->id;
5294
5295 return 0;
5296}
5297
5298
5299# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5300/* see if a given type of sign exists on a specific line */
5301 int
5302buf_findsigntype_id(buf, lnum, typenr)
5303 buf_T *buf; /* buffer whose sign we are searching for */
5304 linenr_T lnum; /* line number of sign */
5305 int typenr; /* sign type number */
5306{
5307 signlist_T *sign; /* a sign in the signlist */
5308
5309 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5310 if (sign->lnum == lnum && sign->typenr == typenr)
5311 return sign->id;
5312
5313 return 0;
5314}
5315
5316
5317# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5318/* return the number of icons on the given line */
5319 int
5320buf_signcount(buf, lnum)
5321 buf_T *buf;
5322 linenr_T lnum;
5323{
5324 signlist_T *sign; /* a sign in the signlist */
5325 int count = 0;
5326
5327 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5328 if (sign->lnum == lnum)
5329 if (sign_get_image(sign->typenr) != NULL)
5330 count++;
5331
5332 return count;
5333}
5334# endif /* FEAT_SIGN_ICONS */
5335# endif /* FEAT_NETBEANS_INTG */
5336
5337
5338/*
5339 * Delete signs in buffer "buf".
5340 */
5341 static void
5342buf_delete_signs(buf)
5343 buf_T *buf;
5344{
5345 signlist_T *next;
5346
5347 while (buf->b_signlist != NULL)
5348 {
5349 next = buf->b_signlist->next;
5350 vim_free(buf->b_signlist);
5351 buf->b_signlist = next;
5352 }
5353}
5354
5355/*
5356 * Delete all signs in all buffers.
5357 */
5358 void
5359buf_delete_all_signs()
5360{
5361 buf_T *buf; /* buffer we are checking for signs */
5362
5363 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5364 if (buf->b_signlist != NULL)
5365 {
5366 /* Need to redraw the windows to remove the sign column. */
5367 redraw_buf_later(buf, NOT_VALID);
5368 buf_delete_signs(buf);
5369 }
5370}
5371
5372/*
5373 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5374 */
5375 void
5376sign_list_placed(rbuf)
5377 buf_T *rbuf;
5378{
5379 buf_T *buf;
5380 signlist_T *p;
5381 char lbuf[BUFSIZ];
5382
5383 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5384 msg_putchar('\n');
5385 if (rbuf == NULL)
5386 buf = firstbuf;
5387 else
5388 buf = rbuf;
5389 while (buf != NULL)
5390 {
5391 if (buf->b_signlist != NULL)
5392 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005393 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5395 msg_putchar('\n');
5396 }
5397 for (p = buf->b_signlist; p != NULL; p = p->next)
5398 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005399 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5401 MSG_PUTS(lbuf);
5402 msg_putchar('\n');
5403 }
5404 if (rbuf != NULL)
5405 break;
5406 buf = buf->b_next;
5407 }
5408}
5409
5410/*
5411 * Adjust a placed sign for inserted/deleted lines.
5412 */
5413 void
5414sign_mark_adjust(line1, line2, amount, amount_after)
5415 linenr_T line1;
5416 linenr_T line2;
5417 long amount;
5418 long amount_after;
5419{
5420 signlist_T *sign; /* a sign in a b_signlist */
5421
5422 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5423 {
5424 if (sign->lnum >= line1 && sign->lnum <= line2)
5425 {
5426 if (amount == MAXLNUM)
5427 sign->lnum = line1;
5428 else
5429 sign->lnum += amount;
5430 }
5431 else if (sign->lnum > line2)
5432 sign->lnum += amount_after;
5433 }
5434}
5435#endif /* FEAT_SIGNS */
5436
5437/*
5438 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5439 */
5440 void
5441set_buflisted(on)
5442 int on;
5443{
5444 if (on != curbuf->b_p_bl)
5445 {
5446 curbuf->b_p_bl = on;
5447#ifdef FEAT_AUTOCMD
5448 if (on)
5449 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5450 else
5451 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5452#endif
5453 }
5454}
5455
5456/*
5457 * Read the file for "buf" again and check if the contents changed.
5458 * Return TRUE if it changed or this could not be checked.
5459 */
5460 int
5461buf_contents_changed(buf)
5462 buf_T *buf;
5463{
5464 buf_T *newbuf;
5465 int differ = TRUE;
5466 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 exarg_T ea;
5469
5470 /* Allocate a buffer without putting it in the buffer list. */
5471 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5472 if (newbuf == NULL)
5473 return TRUE;
5474
5475 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5476 if (prep_exarg(&ea, buf) == FAIL)
5477 {
5478 wipe_buffer(newbuf, FALSE);
5479 return TRUE;
5480 }
5481
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 /* set curwin/curbuf to buf and save a few things */
5483 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
Bram Moolenaar4770d092006-01-12 23:22:24 +00005485 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 && readfile(buf->b_ffname, buf->b_fname,
5487 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5488 &ea, READ_NEW | READ_DUMMY) == OK)
5489 {
5490 /* compare the two files line by line */
5491 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5492 {
5493 differ = FALSE;
5494 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5495 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5496 {
5497 differ = TRUE;
5498 break;
5499 }
5500 }
5501 }
5502 vim_free(ea.cmd);
5503
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 /* restore curwin/curbuf and a few other things */
5505 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506
5507 if (curbuf != newbuf) /* safety check */
5508 wipe_buffer(newbuf, FALSE);
5509
5510 return differ;
5511}
5512
5513/*
5514 * Wipe out a buffer and decrement the last buffer number if it was used for
5515 * this buffer. Call this to wipe out a temp buffer that does not contain any
5516 * marks.
5517 */
5518/*ARGSUSED*/
5519 void
5520wipe_buffer(buf, aucmd)
5521 buf_T *buf;
5522 int aucmd; /* When TRUE trigger autocommands. */
5523{
5524 if (buf->b_fnum == top_file_num - 1)
5525 --top_file_num;
5526
5527#ifdef FEAT_AUTOCMD
5528 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005529 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530#endif
5531 close_buffer(NULL, buf, DOBUF_WIPE);
5532#ifdef FEAT_AUTOCMD
5533 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005534 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535#endif
5536}