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