blob: 3359bc24091016eb0e0f5a0c1c45347507bf86e9 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
30#if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
31static char_u *buflist_match __ARGS((regprog_T *prog, buf_T *buf));
32# define HAVE_BUFLIST_MATCH
33static char_u *fname_match __ARGS((regprog_T *prog, char_u *name));
34#endif
35static void buflist_setfpos __ARGS((buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options));
36static wininfo_T *find_wininfo __ARGS((buf_T *buf));
37#ifdef UNIX
38static buf_T *buflist_findname_stat __ARGS((char_u *ffname, struct stat *st));
39static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname, struct stat *stp));
40static int buf_same_ino __ARGS((buf_T *buf, struct stat *stp));
41#else
42static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname));
43#endif
44#ifdef FEAT_TITLE
45static int ti_change __ARGS((char_u *str, char_u **last));
46#endif
47static void free_buffer __ARGS((buf_T *));
48static void free_buffer_stuff __ARGS((buf_T *buf, int free_options));
49static void clear_wininfo __ARGS((buf_T *buf));
50
51#ifdef UNIX
52# define dev_T dev_t
53#else
54# define dev_T unsigned
55#endif
56
57#if defined(FEAT_SIGNS)
58static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr));
59static void buf_delete_signs __ARGS((buf_T *buf));
60#endif
61
62/*
63 * Open current buffer, that is: open the memfile and read the file into memory
64 * return FAIL for failure, OK otherwise
65 */
66 int
67open_buffer(read_stdin, eap)
68 int read_stdin; /* read file from stdin */
69 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
70{
71 int retval = OK;
72#ifdef FEAT_AUTOCMD
73 buf_T *old_curbuf;
74#endif
75
76 /*
77 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
78 * When re-entering the same buffer, it should not change, because the
79 * user may have reset the flag by hand.
80 */
81 if (readonlymode && curbuf->b_ffname != NULL
82 && (curbuf->b_flags & BF_NEVERLOADED))
83 curbuf->b_p_ro = TRUE;
84
Bram Moolenaar4770d092006-01-12 23:22:24 +000085 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 {
87 /*
88 * There MUST be a memfile, otherwise we can't do anything
89 * If we can't create one for the current buffer, take another buffer
90 */
91 close_buffer(NULL, curbuf, 0);
92 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
93 if (curbuf->b_ml.ml_mfp != NULL)
94 break;
95 /*
96 * if there is no memfile at all, exit
97 * This is OK, since there are no changes to loose.
98 */
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;
174#ifdef FEAT_AUTOCMD
175# ifdef FEAT_EVAL
176 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
177 curbuf, &retval);
178# else
179 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
180# endif
181#endif
182 }
183 }
184
185 /* if first time loading this buffer, init b_chartab[] */
186 if (curbuf->b_flags & BF_NEVERLOADED)
187 (void)buf_init_chartab(curbuf, FALSE);
188
189 /*
190 * Set/reset the Changed flag first, autocmds may change the buffer.
191 * Apply the automatic commands, before processing the modelines.
192 * So the modelines have priority over auto commands.
193 */
194 /* When reading stdin, the buffer contents always needs writing, so set
195 * the changed flag. Unless in readonly mode: "ls | gview -".
196 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
197 if ((read_stdin && !readonlymode && !bufempty())
198#ifdef FEAT_AUTOCMD
199 || modified_was_set /* ":set modified" used in autocmd */
200# ifdef FEAT_EVAL
201 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
202# endif
203#endif
204 || (got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL))
205 changed();
206 else if (retval != FAIL)
207 unchanged(curbuf, FALSE);
208 save_file_ff(curbuf); /* keep this fileformat */
209
210 /* require "!" to overwrite the file, because it wasn't read completely */
211#ifdef FEAT_EVAL
212 if (aborting())
213#else
214 if (got_int)
215#endif
216 curbuf->b_flags |= BF_READERR;
217
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000218#ifdef FEAT_FOLDING
219 /* Need to update automatic folding. Do this before the autocommands,
220 * they may use the fold info. */
221 foldUpdateAll(curwin);
222#endif
223
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifdef FEAT_AUTOCMD
225 /* need to set w_topline, unless some autocommand already did that. */
226 if (!(curwin->w_valid & VALID_TOPLINE))
227 {
228 curwin->w_topline = 1;
229# ifdef FEAT_DIFF
230 curwin->w_topfill = 0;
231# endif
232 }
233# ifdef FEAT_EVAL
234 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
235# else
236 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
237# endif
238#endif
239
240 if (retval != FAIL)
241 {
242#ifdef FEAT_AUTOCMD
243 /*
244 * The autocommands may have changed the current buffer. Apply the
245 * modelines to the correct buffer, if it still exists and is loaded.
246 */
247 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
248 {
249 aco_save_T aco;
250
251 /* Go to the buffer that was opened. */
252 aucmd_prepbuf(&aco, old_curbuf);
253#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000254 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
256
257#ifdef FEAT_AUTOCMD
258# ifdef FEAT_EVAL
259 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
260 &retval);
261# else
262 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
263# endif
264
265 /* restore curwin/curbuf and a few other things */
266 aucmd_restbuf(&aco);
267 }
268#endif
269 }
270
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 return retval;
272}
273
274/*
275 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
276 */
277 int
278buf_valid(buf)
279 buf_T *buf;
280{
281 buf_T *bp;
282
283 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
284 if (bp == buf)
285 return TRUE;
286 return FALSE;
287}
288
289/*
290 * Close the link to a buffer.
291 * "action" is used when there is no longer a window for the buffer.
292 * It can be:
293 * 0 buffer becomes hidden
294 * DOBUF_UNLOAD buffer is unloaded
295 * DOBUF_DELETE buffer is unloaded and removed from buffer list
296 * DOBUF_WIPE buffer is unloaded and really deleted
297 * When doing all but the first one on the current buffer, the caller should
298 * get a new buffer very soon!
299 *
300 * The 'bufhidden' option can force freeing and deleting.
301 */
302 void
303close_buffer(win, buf, action)
304 win_T *win; /* if not NULL, set b_last_cursor */
305 buf_T *buf;
306 int action;
307{
308#ifdef FEAT_AUTOCMD
309 int is_curbuf;
310 int nwindows = buf->b_nwindows;
311#endif
312 int unload_buf = (action != 0);
313 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
314 int wipe_buf = (action == DOBUF_WIPE);
315
316#ifdef FEAT_QUICKFIX
317 /*
318 * Force unloading or deleting when 'bufhidden' says so.
319 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
320 * "hide" (otherwise we could never free or delete a buffer).
321 */
322 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
323 {
324 del_buf = TRUE;
325 unload_buf = TRUE;
326 }
327 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
328 {
329 del_buf = TRUE;
330 unload_buf = TRUE;
331 wipe_buf = TRUE;
332 }
333 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
334 unload_buf = TRUE;
335#endif
336
337 if (win != NULL)
338 {
339 /* Set b_last_cursor when closing the last window for the buffer.
340 * Remember the last cursor position and window options of the buffer.
341 * This used to be only for the current window, but then options like
342 * 'foldmethod' may be lost with a ":only" command. */
343 if (buf->b_nwindows == 1)
344 set_last_cursor(win);
345 buflist_setfpos(buf, win,
346 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
347 win->w_cursor.col, TRUE);
348 }
349
350#ifdef FEAT_AUTOCMD
351 /* When the buffer is no longer in a window, trigger BufWinLeave */
352 if (buf->b_nwindows == 1)
353 {
354 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
355 FALSE, buf);
356 if (!buf_valid(buf)) /* autocommands may delete the buffer */
357 return;
358
359 /* When the buffer becomes hidden, but is not unloaded, trigger
360 * BufHidden */
361 if (!unload_buf)
362 {
363 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
364 FALSE, buf);
365 if (!buf_valid(buf)) /* autocmds may delete the buffer */
366 return;
367 }
368# ifdef FEAT_EVAL
369 if (aborting()) /* autocmds may abort script processing */
370 return;
371# endif
372 }
373 nwindows = buf->b_nwindows;
374#endif
375
376 /* decrease the link count from windows (unless not in any window) */
377 if (buf->b_nwindows > 0)
378 --buf->b_nwindows;
379
380 /* Return when a window is displaying the buffer or when it's not
381 * unloaded. */
382 if (buf->b_nwindows > 0 || !unload_buf)
383 {
Bram Moolenaar607a95ed2006-03-28 20:57:42 +0000384#if 0 /* why was this here? */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 if (buf == curbuf)
386 u_sync(); /* sync undo before going to another buffer */
Bram Moolenaar607a95ed2006-03-28 20:57:42 +0000387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 return;
389 }
390
391 /* Always remove the buffer when there is no file name. */
392 if (buf->b_ffname == NULL)
393 del_buf = TRUE;
394
395 /*
396 * Free all things allocated for this buffer.
397 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
398 */
399#ifdef FEAT_AUTOCMD
400 /* Remember if we are closing the current buffer. Restore the number of
401 * windows, so that autocommands in buf_freeall() don't get confused. */
402 is_curbuf = (buf == curbuf);
403 buf->b_nwindows = nwindows;
404#endif
405
406 buf_freeall(buf, del_buf, wipe_buf);
407
408#ifdef FEAT_AUTOCMD
409 /* Autocommands may have deleted the buffer. */
410 if (!buf_valid(buf))
411 return;
412# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000413 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 return;
415# endif
416
417 /* Autocommands may have opened or closed windows for this buffer.
418 * Decrement the count for the close we do here. */
419 if (buf->b_nwindows > 0)
420 --buf->b_nwindows;
421
422 /*
423 * It's possible that autocommands change curbuf to the one being deleted.
424 * This might cause the previous curbuf to be deleted unexpectedly. But
425 * in some cases it's OK to delete the curbuf, because a new one is
426 * obtained anyway. Therefore only return if curbuf changed to the
427 * deleted buffer.
428 */
429 if (buf == curbuf && !is_curbuf)
430 return;
431#endif
432
433#ifdef FEAT_NETBEANS_INTG
434 if (usingNetbeans)
435 netbeans_file_closed(buf);
436#endif
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000437#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 /* Change directories when the acd option is set on. */
439 if (p_acd && curbuf->b_ffname != NULL
440 && vim_chdirfile(curbuf->b_ffname) == OK)
441 shorten_fnames(TRUE);
442#endif
443
444 /*
445 * Remove the buffer from the list.
446 */
447 if (wipe_buf)
448 {
449#ifdef FEAT_SUN_WORKSHOP
450 if (usingSunWorkShop)
451 workshop_file_closed_lineno((char *)buf->b_ffname,
452 (int)buf->b_last_cursor.lnum);
453#endif
454 vim_free(buf->b_ffname);
455 vim_free(buf->b_sfname);
456 if (buf->b_prev == NULL)
457 firstbuf = buf->b_next;
458 else
459 buf->b_prev->b_next = buf->b_next;
460 if (buf->b_next == NULL)
461 lastbuf = buf->b_prev;
462 else
463 buf->b_next->b_prev = buf->b_prev;
464 free_buffer(buf);
465 }
466 else
467 {
468 if (del_buf)
469 {
470 /* Free all internal variables and reset option values, to make
471 * ":bdel" compatible with Vim 5.7. */
472 free_buffer_stuff(buf, TRUE);
473
474 /* Make it look like a new buffer. */
475 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
476
477 /* Init the options when loaded again. */
478 buf->b_p_initialized = FALSE;
479 }
480 buf_clear_file(buf);
481 if (del_buf)
482 buf->b_p_bl = FALSE;
483 }
484}
485
486/*
487 * Make buffer not contain a file.
488 */
489 void
490buf_clear_file(buf)
491 buf_T *buf;
492{
493 buf->b_ml.ml_line_count = 1;
494 unchanged(buf, TRUE);
495#ifndef SHORT_FNAME
496 buf->b_shortname = FALSE;
497#endif
498 buf->b_p_eol = TRUE;
499 buf->b_start_eol = TRUE;
500#ifdef FEAT_MBYTE
501 buf->b_p_bomb = FALSE;
502#endif
503 buf->b_ml.ml_mfp = NULL;
504 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
505#ifdef FEAT_NETBEANS_INTG
506 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507#endif
508}
509
510/*
511 * buf_freeall() - free all things allocated for a buffer that are related to
512 * the file.
513 */
514/*ARGSUSED*/
515 void
516buf_freeall(buf, del_buf, wipe_buf)
517 buf_T *buf;
518 int del_buf; /* buffer is going to be deleted */
519 int wipe_buf; /* buffer is going to be wiped out */
520{
521#ifdef FEAT_AUTOCMD
522 int is_curbuf = (buf == curbuf);
523
524 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
525 if (!buf_valid(buf)) /* autocommands may delete the buffer */
526 return;
527 if (del_buf && buf->b_p_bl)
528 {
529 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
530 if (!buf_valid(buf)) /* autocommands may delete the buffer */
531 return;
532 }
533 if (wipe_buf)
534 {
535 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
536 FALSE, buf);
537 if (!buf_valid(buf)) /* autocommands may delete the buffer */
538 return;
539 }
540# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000541 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 return;
543# endif
544
545 /*
546 * It's possible that autocommands change curbuf to the one being deleted.
547 * This might cause curbuf to be deleted unexpectedly. But in some cases
548 * it's OK to delete the curbuf, because a new one is obtained anyway.
549 * Therefore only return if curbuf changed to the deleted buffer.
550 */
551 if (buf == curbuf && !is_curbuf)
552 return;
553#endif
554#ifdef FEAT_DIFF
555 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
556#endif
557#ifdef FEAT_TCL
558 tcl_buffer_free(buf);
559#endif
560 u_blockfree(buf); /* free the memory allocated for undo */
561 ml_close(buf, TRUE); /* close and delete the memline/memfile */
562 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
563 u_clearall(buf); /* reset all undo information */
564#ifdef FEAT_SYN_HL
565 syntax_clear(buf); /* reset syntax info */
566#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000567 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568}
569
570/*
571 * Free a buffer structure and the things it contains related to the buffer
572 * itself (not the file, that must have been done already).
573 */
574 static void
575free_buffer(buf)
576 buf_T *buf;
577{
578 free_buffer_stuff(buf, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000579#ifdef FEAT_MZSCHEME
580 mzscheme_buffer_free(buf);
581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582#ifdef FEAT_PERL
583 perl_buf_free(buf);
584#endif
585#ifdef FEAT_PYTHON
586 python_buffer_free(buf);
587#endif
588#ifdef FEAT_RUBY
589 ruby_buffer_free(buf);
590#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000591#ifdef FEAT_AUTOCMD
592 aubuflocal_remove(buf);
593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 vim_free(buf);
595}
596
597/*
598 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
599 */
600 static void
601free_buffer_stuff(buf, free_options)
602 buf_T *buf;
603 int free_options; /* free options as well */
604{
605 if (free_options)
606 {
607 clear_wininfo(buf); /* including window-local options */
608 free_buf_options(buf, TRUE);
609 }
610#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000611 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
612 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613#endif
614#ifdef FEAT_USR_CMDS
615 uc_clear(&buf->b_ucmds); /* clear local user commands */
616#endif
617#ifdef FEAT_SIGNS
618 buf_delete_signs(buf); /* delete any signs */
619#endif
620#ifdef FEAT_LOCALMAP
621 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
622 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
623#endif
624#ifdef FEAT_MBYTE
625 vim_free(buf->b_start_fenc);
626 buf->b_start_fenc = NULL;
627#endif
628}
629
630/*
631 * Free the b_wininfo list for buffer "buf".
632 */
633 static void
634clear_wininfo(buf)
635 buf_T *buf;
636{
637 wininfo_T *wip;
638
639 while (buf->b_wininfo != NULL)
640 {
641 wip = buf->b_wininfo;
642 buf->b_wininfo = wip->wi_next;
643 if (wip->wi_optset)
644 {
645 clear_winopt(&wip->wi_opt);
646#ifdef FEAT_FOLDING
647 deleteFoldRecurse(&wip->wi_folds);
648#endif
649 }
650 vim_free(wip);
651 }
652}
653
654#if defined(FEAT_LISTCMDS) || defined(PROTO)
655/*
656 * Go to another buffer. Handles the result of the ATTENTION dialog.
657 */
658 void
659goto_buffer(eap, start, dir, count)
660 exarg_T *eap;
661 int start;
662 int dir;
663 int count;
664{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000665# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 buf_T *old_curbuf = curbuf;
667
668 swap_exists_action = SEA_DIALOG;
669# endif
670 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
671 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000672# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
674 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000675# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
676 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000677
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000678 /* Reset the error/interrupt/exception state here so that
679 * aborting() returns FALSE when closing a window. */
680 enter_cleanup(&cs);
681# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000682
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000683 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 win_close(curwin, TRUE);
685 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000686 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000687
688# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
689 /* Restore the error/interrupt/exception state if not discarded by a
690 * new aborting error, interrupt, or uncaught exception. */
691 leave_cleanup(&cs);
692# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
694 else
695 handle_swap_exists(old_curbuf);
696# endif
697}
698#endif
699
Bram Moolenaare64ac772005-12-07 20:54:59 +0000700#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701/*
702 * Handle the situation of swap_exists_action being set.
703 * It is allowed for "old_curbuf" to be NULL or invalid.
704 */
705 void
706handle_swap_exists(old_curbuf)
707 buf_T *old_curbuf;
708{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000709# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
710 cleanup_T cs;
711# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000712
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 if (swap_exists_action == SEA_QUIT)
714 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000715# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
716 /* Reset the error/interrupt/exception state here so that
717 * aborting() returns FALSE when closing a buffer. */
718 enter_cleanup(&cs);
719# endif
720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 /* User selected Quit at ATTENTION prompt. Go back to previous
722 * buffer. If that buffer is gone or the same as the current one,
723 * open a new, empty buffer. */
724 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000725 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
727 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000728 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 if (old_curbuf != NULL)
730 enter_buffer(old_curbuf);
731 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000732
733# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
734 /* Restore the error/interrupt/exception state if not discarded by a
735 * new aborting error, interrupt, or uncaught exception. */
736 leave_cleanup(&cs);
737# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 }
739 else if (swap_exists_action == SEA_RECOVER)
740 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000741# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
742 /* Reset the error/interrupt/exception state here so that
743 * aborting() returns FALSE when closing a buffer. */
744 enter_cleanup(&cs);
745# endif
746
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 /* User selected Recover at ATTENTION prompt. */
748 msg_scroll = TRUE;
749 ml_recover();
750 MSG_PUTS("\n"); /* don't overwrite the last message */
751 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000752 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000753
754# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
755 /* Restore the error/interrupt/exception state if not discarded by a
756 * new aborting error, interrupt, or uncaught exception. */
757 leave_cleanup(&cs);
758# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 }
760 swap_exists_action = SEA_NONE;
761}
762#endif
763
764#if defined(FEAT_LISTCMDS) || defined(PROTO)
765/*
766 * do_bufdel() - delete or unload buffer(s)
767 *
768 * addr_count == 0: ":bdel" - delete current buffer
769 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
770 * buffer "end_bnr", then any other arguments.
771 * addr_count == 2: ":N,N bdel" - delete buffers in range
772 *
773 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
774 * DOBUF_DEL (":bdel")
775 *
776 * Returns error message or NULL
777 */
778 char_u *
779do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
780 int command;
781 char_u *arg; /* pointer to extra arguments */
782 int addr_count;
783 int start_bnr; /* first buffer number in a range */
784 int end_bnr; /* buffer nr or last buffer nr in a range */
785 int forceit;
786{
787 int do_current = 0; /* delete current buffer? */
788 int deleted = 0; /* number of buffers deleted */
789 char_u *errormsg = NULL; /* return value */
790 int bnr; /* buffer number */
791 char_u *p;
792
793#ifdef FEAT_NETBEANS_INTG
794 netbeansCloseFile = 1;
795#endif
796 if (addr_count == 0)
797 {
798 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
799 }
800 else
801 {
802 if (addr_count == 2)
803 {
804 if (*arg) /* both range and argument is not allowed */
805 return (char_u *)_(e_trailing);
806 bnr = start_bnr;
807 }
808 else /* addr_count == 1 */
809 bnr = end_bnr;
810
811 for ( ;!got_int; ui_breakcheck())
812 {
813 /*
814 * delete the current buffer last, otherwise when the
815 * current buffer is deleted, the next buffer becomes
816 * the current one and will be loaded, which may then
817 * also be deleted, etc.
818 */
819 if (bnr == curbuf->b_fnum)
820 do_current = bnr;
821 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
822 forceit) == OK)
823 ++deleted;
824
825 /*
826 * find next buffer number to delete/unload
827 */
828 if (addr_count == 2)
829 {
830 if (++bnr > end_bnr)
831 break;
832 }
833 else /* addr_count == 1 */
834 {
835 arg = skipwhite(arg);
836 if (*arg == NUL)
837 break;
838 if (!VIM_ISDIGIT(*arg))
839 {
840 p = skiptowhite_esc(arg);
841 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
842 if (bnr < 0) /* failed */
843 break;
844 arg = p;
845 }
846 else
847 bnr = getdigits(&arg);
848 }
849 }
850 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
851 FORWARD, do_current, forceit) == OK)
852 ++deleted;
853
854 if (deleted == 0)
855 {
856 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000857 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000859 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000861 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 errormsg = IObuff;
863 }
864 else if (deleted >= p_report)
865 {
866 if (command == DOBUF_UNLOAD)
867 {
868 if (deleted == 1)
869 MSG(_("1 buffer unloaded"));
870 else
871 smsg((char_u *)_("%d buffers unloaded"), deleted);
872 }
873 else if (command == DOBUF_DEL)
874 {
875 if (deleted == 1)
876 MSG(_("1 buffer deleted"));
877 else
878 smsg((char_u *)_("%d buffers deleted"), deleted);
879 }
880 else
881 {
882 if (deleted == 1)
883 MSG(_("1 buffer wiped out"));
884 else
885 smsg((char_u *)_("%d buffers wiped out"), deleted);
886 }
887 }
888 }
889
890#ifdef FEAT_NETBEANS_INTG
891 netbeansCloseFile = 0;
892#endif
893
894 return errormsg;
895}
896
897/*
898 * Implementation of the commands for the buffer list.
899 *
900 * action == DOBUF_GOTO go to specified buffer
901 * action == DOBUF_SPLIT split window and go to specified buffer
902 * action == DOBUF_UNLOAD unload specified buffer(s)
903 * action == DOBUF_DEL delete specified buffer(s) from buffer list
904 * action == DOBUF_WIPE delete specified buffer(s) really
905 *
906 * start == DOBUF_CURRENT go to "count" buffer from current buffer
907 * start == DOBUF_FIRST go to "count" buffer from first buffer
908 * start == DOBUF_LAST go to "count" buffer from last buffer
909 * start == DOBUF_MOD go to "count" modified buffer from current buffer
910 *
911 * Return FAIL or OK.
912 */
913 int
914do_buffer(action, start, dir, count, forceit)
915 int action;
916 int start;
917 int dir; /* FORWARD or BACKWARD */
918 int count; /* buffer number or number of buffers */
919 int forceit; /* TRUE for :...! */
920{
921 buf_T *buf;
922 buf_T *bp;
923 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
924 || action == DOBUF_WIPE);
925
926 switch (start)
927 {
928 case DOBUF_FIRST: buf = firstbuf; break;
929 case DOBUF_LAST: buf = lastbuf; break;
930 default: buf = curbuf; break;
931 }
932 if (start == DOBUF_MOD) /* find next modified buffer */
933 {
934 while (count-- > 0)
935 {
936 do
937 {
938 buf = buf->b_next;
939 if (buf == NULL)
940 buf = firstbuf;
941 }
942 while (buf != curbuf && !bufIsChanged(buf));
943 }
944 if (!bufIsChanged(buf))
945 {
946 EMSG(_("E84: No modified buffer found"));
947 return FAIL;
948 }
949 }
950 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
951 {
952 while (buf != NULL && buf->b_fnum != count)
953 buf = buf->b_next;
954 }
955 else
956 {
957 bp = NULL;
958 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
959 {
960 /* remember the buffer where we start, we come back there when all
961 * buffers are unlisted. */
962 if (bp == NULL)
963 bp = buf;
964 if (dir == FORWARD)
965 {
966 buf = buf->b_next;
967 if (buf == NULL)
968 buf = firstbuf;
969 }
970 else
971 {
972 buf = buf->b_prev;
973 if (buf == NULL)
974 buf = lastbuf;
975 }
976 /* don't count unlisted buffers */
977 if (unload || buf->b_p_bl)
978 {
979 --count;
980 bp = NULL; /* use this buffer as new starting point */
981 }
982 if (bp == buf)
983 {
984 /* back where we started, didn't find anything. */
985 EMSG(_("E85: There is no listed buffer"));
986 return FAIL;
987 }
988 }
989 }
990
991 if (buf == NULL) /* could not find it */
992 {
993 if (start == DOBUF_FIRST)
994 {
995 /* don't warn when deleting */
996 if (!unload)
997 EMSGN(_("E86: Buffer %ld does not exist"), count);
998 }
999 else if (dir == FORWARD)
1000 EMSG(_("E87: Cannot go beyond last buffer"));
1001 else
1002 EMSG(_("E88: Cannot go before first buffer"));
1003 return FAIL;
1004 }
1005
1006#ifdef FEAT_GUI
1007 need_mouse_correct = TRUE;
1008#endif
1009
1010#ifdef FEAT_LISTCMDS
1011 /*
1012 * delete buffer buf from memory and/or the list
1013 */
1014 if (unload)
1015 {
1016 int forward;
1017 int retval;
1018
1019 /* When unloading or deleting a buffer that's already unloaded and
1020 * unlisted: fail silently. */
1021 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1022 return FAIL;
1023
1024 if (!forceit && bufIsChanged(buf))
1025 {
1026#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1027 if ((p_confirm || cmdmod.confirm) && p_write)
1028 {
1029 dialog_changed(buf, FALSE);
1030# ifdef FEAT_AUTOCMD
1031 if (!buf_valid(buf))
1032 /* Autocommand deleted buffer, oops! It's not changed
1033 * now. */
1034 return FAIL;
1035# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001036 /* If it's still changed fail silently, the dialog already
1037 * mentioned why it fails. */
1038 if (bufIsChanged(buf))
1039 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001041 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042#endif
1043 {
1044 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1045 buf->b_fnum);
1046 return FAIL;
1047 }
1048 }
1049
1050 /*
1051 * If deleting the last (listed) buffer, make it empty.
1052 * The last (listed) buffer cannot be unloaded.
1053 */
1054 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1055 if (bp->b_p_bl && bp != buf)
1056 break;
1057 if (bp == NULL && buf == curbuf)
1058 {
1059 if (action == DOBUF_UNLOAD)
1060 {
1061 EMSG(_("E90: Cannot unload last buffer"));
1062 return FAIL;
1063 }
1064
1065 /* Close any other windows on this buffer, then make it empty. */
1066#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001067 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068#endif
1069 setpcmark();
1070 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1071 forceit ? ECMD_FORCEIT : 0);
1072
1073 /*
1074 * do_ecmd() may create a new buffer, then we have to delete
1075 * the old one. But do_ecmd() may have done that already, check
1076 * if the buffer still exists.
1077 */
1078 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1079 close_buffer(NULL, buf, action);
1080 return retval;
1081 }
1082
1083#ifdef FEAT_WINDOWS
1084 /*
1085 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001086 * (unless it's the only window). Repeat this so long as we end up in
1087 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001089 while (buf == curbuf
1090 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 win_close(curwin, FALSE);
1092#endif
1093
1094 /*
1095 * If the buffer to be deleted is not the current one, delete it here.
1096 */
1097 if (buf != curbuf)
1098 {
1099#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001100 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101#endif
1102 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1103 close_buffer(NULL, buf, action);
1104 return OK;
1105 }
1106
1107 /*
1108 * Deleting the current buffer: Need to find another buffer to go to.
1109 * There must be another, otherwise it would have been handled above.
1110 * First use au_new_curbuf, if it is valid.
1111 * Then prefer the buffer we most recently visited.
1112 * Else try to find one that is loaded, after the current buffer,
1113 * then before the current buffer.
1114 * Finally use any buffer.
1115 */
1116 buf = NULL; /* selected buffer */
1117 bp = NULL; /* used when no loaded buffer found */
1118#ifdef FEAT_AUTOCMD
1119 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1120 buf = au_new_curbuf;
1121# ifdef FEAT_JUMPLIST
1122 else
1123# endif
1124#endif
1125#ifdef FEAT_JUMPLIST
1126 if (curwin->w_jumplistlen > 0)
1127 {
1128 int jumpidx;
1129
1130 jumpidx = curwin->w_jumplistidx - 1;
1131 if (jumpidx < 0)
1132 jumpidx = curwin->w_jumplistlen - 1;
1133
1134 forward = jumpidx;
1135 while (jumpidx != curwin->w_jumplistidx)
1136 {
1137 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1138 if (buf != NULL)
1139 {
1140 if (buf == curbuf || !buf->b_p_bl)
1141 buf = NULL; /* skip current and unlisted bufs */
1142 else if (buf->b_ml.ml_mfp == NULL)
1143 {
1144 /* skip unloaded buf, but may keep it for later */
1145 if (bp == NULL)
1146 bp = buf;
1147 buf = NULL;
1148 }
1149 }
1150 if (buf != NULL) /* found a valid buffer: stop searching */
1151 break;
1152 /* advance to older entry in jump list */
1153 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1154 break;
1155 if (--jumpidx < 0)
1156 jumpidx = curwin->w_jumplistlen - 1;
1157 if (jumpidx == forward) /* List exhausted for sure */
1158 break;
1159 }
1160 }
1161#endif
1162
1163 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1164 {
1165 forward = TRUE;
1166 buf = curbuf->b_next;
1167 for (;;)
1168 {
1169 if (buf == NULL)
1170 {
1171 if (!forward) /* tried both directions */
1172 break;
1173 buf = curbuf->b_prev;
1174 forward = FALSE;
1175 continue;
1176 }
1177 /* in non-help buffer, try to skip help buffers, and vv */
1178 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1179 {
1180 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1181 break;
1182 if (bp == NULL) /* remember unloaded buf for later */
1183 bp = buf;
1184 }
1185 if (forward)
1186 buf = buf->b_next;
1187 else
1188 buf = buf->b_prev;
1189 }
1190 }
1191 if (buf == NULL) /* No loaded buffer, use unloaded one */
1192 buf = bp;
1193 if (buf == NULL) /* No loaded buffer, find listed one */
1194 {
1195 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1196 if (buf->b_p_bl && buf != curbuf)
1197 break;
1198 }
1199 if (buf == NULL) /* Still no buffer, just take one */
1200 {
1201 if (curbuf->b_next != NULL)
1202 buf = curbuf->b_next;
1203 else
1204 buf = curbuf->b_prev;
1205 }
1206 }
1207
1208 /*
1209 * make buf current buffer
1210 */
1211 if (action == DOBUF_SPLIT) /* split window first */
1212 {
1213# ifdef FEAT_WINDOWS
1214 /* jump to first window containing buf if one exists ("useopen") */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001215 if (vim_strchr(p_swb, 'o') && buf_jump_open_win(buf))
1216 return OK;
1217 /* jump to first window in any tab page containing buf if one exists
1218 * ("usetab") */
1219 if (vim_strchr(p_swb, 'a') && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 return OK;
1221 if (win_split(0, 0) == FAIL)
1222# endif
1223 return FAIL;
1224 }
1225#endif
1226
1227 /* go to current buffer - nothing to do */
1228 if (buf == curbuf)
1229 return OK;
1230
1231 /*
1232 * Check if the current buffer may be abandoned.
1233 */
1234 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1235 {
1236#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1237 if ((p_confirm || cmdmod.confirm) && p_write)
1238 {
1239 dialog_changed(curbuf, FALSE);
1240# ifdef FEAT_AUTOCMD
1241 if (!buf_valid(buf))
1242 /* Autocommand deleted buffer, oops! */
1243 return FAIL;
1244# endif
1245 }
1246 if (bufIsChanged(curbuf))
1247#endif
1248 {
1249 EMSG(_(e_nowrtmsg));
1250 return FAIL;
1251 }
1252 }
1253
1254 /* Go to the other buffer. */
1255 set_curbuf(buf, action);
1256
1257#if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1258 if (action == DOBUF_SPLIT)
1259 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1260#endif
1261
1262#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1263 if (aborting()) /* autocmds may abort script processing */
1264 return FAIL;
1265#endif
1266
1267 return OK;
1268}
1269
1270#endif /* FEAT_LISTCMDS */
1271
1272/*
1273 * Set current buffer to "buf". Executes autocommands and closes current
1274 * buffer. "action" tells how to close the current buffer:
1275 * DOBUF_GOTO free or hide it
1276 * DOBUF_SPLIT nothing
1277 * DOBUF_UNLOAD unload it
1278 * DOBUF_DEL delete it
1279 * DOBUF_WIPE wipe it out
1280 */
1281 void
1282set_curbuf(buf, action)
1283 buf_T *buf;
1284 int action;
1285{
1286 buf_T *prevbuf;
1287 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1288 || action == DOBUF_WIPE);
1289
1290 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001291 if (!cmdmod.keepalt)
1292 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 buflist_altfpos(); /* remember curpos */
1294
1295#ifdef FEAT_VISUAL
1296 /* Don't restart Select mode after switching to another buffer. */
1297 VIsual_reselect = FALSE;
1298#endif
1299
1300 /* close_windows() or apply_autocmds() may change curbuf */
1301 prevbuf = curbuf;
1302
1303#ifdef FEAT_AUTOCMD
1304 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1305# ifdef FEAT_EVAL
1306 if (buf_valid(prevbuf) && !aborting())
1307# else
1308 if (buf_valid(prevbuf))
1309# endif
1310#endif
1311 {
1312#ifdef FEAT_WINDOWS
1313 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001314 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315#endif
1316#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1317 if (buf_valid(prevbuf) && !aborting())
1318#else
1319 if (buf_valid(prevbuf))
1320#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001321 {
1322 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001323 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1325 unload ? action : (action == DOBUF_GOTO
1326 && !P_HID(prevbuf)
1327 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 }
1330#ifdef FEAT_AUTOCMD
1331# ifdef FEAT_EVAL
1332 /* An autocommand may have deleted buf or aborted the script processing! */
1333 if (buf_valid(buf) && !aborting())
1334# else
1335 if (buf_valid(buf)) /* an autocommand may have deleted buf! */
1336# endif
1337#endif
1338 enter_buffer(buf);
1339}
1340
1341/*
1342 * Enter a new current buffer.
1343 * Old curbuf must have been abandoned already!
1344 */
1345 void
1346enter_buffer(buf)
1347 buf_T *buf;
1348{
1349 /* Copy buffer and window local option values. Not for a help buffer. */
1350 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1351 if (!buf->b_help)
1352 get_winopts(buf);
1353#ifdef FEAT_FOLDING
1354 else
1355 /* Remove all folds in the window. */
1356 clearFolding(curwin);
1357 foldUpdateAll(curwin); /* update folds (later). */
1358#endif
1359
1360 /* Get the buffer in the current window. */
1361 curwin->w_buffer = buf;
1362 curbuf = buf;
1363 ++curbuf->b_nwindows;
1364
1365#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001366 if (curwin->w_p_diff)
1367 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368#endif
1369
1370 /* Cursor on first line by default. */
1371 curwin->w_cursor.lnum = 1;
1372 curwin->w_cursor.col = 0;
1373#ifdef FEAT_VIRTUALEDIT
1374 curwin->w_cursor.coladd = 0;
1375#endif
1376 curwin->w_set_curswant = TRUE;
1377
1378 /* Make sure the buffer is loaded. */
1379 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001380 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001381#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001382 /* If there is no filetype, allow for detecting one. Esp. useful for
1383 * ":ball" used in a autocommand. If there already is a filetype we
1384 * might prefer to keep it. */
1385 if (*curbuf->b_p_ft == NUL)
1386 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001387#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 open_buffer(FALSE, NULL);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 else
1392 {
1393 need_fileinfo = TRUE; /* display file info after redraw */
1394 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1395#ifdef FEAT_AUTOCMD
1396 curwin->w_topline = 1;
1397# ifdef FEAT_DIFF
1398 curwin->w_topfill = 0;
1399# endif
1400 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1401 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1402#endif
1403 }
1404
1405 /* If autocommands did not change the cursor position, restore cursor lnum
1406 * and possibly cursor col. */
1407 if (curwin->w_cursor.lnum == 1 && inindent(0))
1408 buflist_getfpos();
1409
1410 check_arg_idx(curwin); /* check for valid arg_idx */
1411#ifdef FEAT_TITLE
1412 maketitle();
1413#endif
1414#ifdef FEAT_AUTOCMD
1415 if (curwin->w_topline == 1) /* when autocmds didn't change it */
1416#endif
1417 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1418
Bram Moolenaar009b2592004-10-24 19:18:58 +00001419#ifdef FEAT_NETBEANS_INTG
1420 /* Send fileOpened event because we've changed buffers. */
1421 if (usingNetbeans && isNetbeansBuffer(curbuf))
1422 netbeans_file_activated(curbuf);
1423#endif
1424
Bram Moolenaar8dff8182006-04-06 20:18:50 +00001425#ifdef FEAT_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 /* Change directories when the acd option is set on. */
1427 if (p_acd && curbuf->b_ffname != NULL
1428 && vim_chdirfile(curbuf->b_ffname) == OK)
1429 shorten_fnames(TRUE);
1430#endif
1431
1432#ifdef FEAT_KEYMAP
1433 if (curbuf->b_kmap_state & KEYMAP_INIT)
1434 keymap_init();
1435#endif
1436 redraw_later(NOT_VALID);
1437}
1438
1439/*
1440 * functions for dealing with the buffer list
1441 */
1442
1443/*
1444 * Add a file name to the buffer list. Return a pointer to the buffer.
1445 * If the same file name already exists return a pointer to that buffer.
1446 * If it does not exist, or if fname == NULL, a new entry is created.
1447 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1448 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1449 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 * This is the ONLY way to create a new buffer.
1451 */
1452static int top_file_num = 1; /* highest file number */
1453
1454 buf_T *
1455buflist_new(ffname, sfname, lnum, flags)
1456 char_u *ffname; /* full path of fname or relative */
1457 char_u *sfname; /* short fname or NULL */
1458 linenr_T lnum; /* preferred cursor line */
1459 int flags; /* BLN_ defines */
1460{
1461 buf_T *buf;
1462#ifdef UNIX
1463 struct stat st;
1464#endif
1465
1466 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1467
1468 /*
1469 * If file name already exists in the list, update the entry.
1470 */
1471#ifdef UNIX
1472 /* On Unix we can use inode numbers when the file exists. Works better
1473 * for hard links. */
1474 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1475 st.st_dev = (dev_T)-1;
1476#endif
1477 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1478#ifdef UNIX
1479 buflist_findname_stat(ffname, &st)
1480#else
1481 buflist_findname(ffname)
1482#endif
1483 ) != NULL)
1484 {
1485 vim_free(ffname);
1486 if (lnum != 0)
1487 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1488 /* copy the options now, if 'cpo' doesn't have 's' and not done
1489 * already */
1490 buf_copy_options(buf, 0);
1491 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1492 {
1493 buf->b_p_bl = TRUE;
1494#ifdef FEAT_AUTOCMD
1495 if (!(flags & BLN_DUMMY))
1496 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1497#endif
1498 }
1499 return buf;
1500 }
1501
1502 /*
1503 * If the current buffer has no name and no contents, use the current
1504 * buffer. Otherwise: Need to allocate a new buffer structure.
1505 *
1506 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001507 * (A spell file buffer is allocated in spell.c, but that's not a normal
1508 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 */
1510 buf = NULL;
1511 if ((flags & BLN_CURBUF)
1512 && curbuf != NULL
1513 && curbuf->b_ffname == NULL
1514 && curbuf->b_nwindows <= 1
1515 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1516 {
1517 buf = curbuf;
1518#ifdef FEAT_AUTOCMD
1519 /* It's like this buffer is deleted. Watch out for autocommands that
1520 * change curbuf! If that happens, allocate a new buffer anyway. */
1521 if (curbuf->b_p_bl)
1522 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1523 if (buf == curbuf)
1524 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1525# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001526 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 return NULL;
1528# endif
1529#endif
1530#ifdef FEAT_QUICKFIX
1531# ifdef FEAT_AUTOCMD
1532 if (buf == curbuf)
1533# endif
1534 {
1535 /* Make sure 'bufhidden' and 'buftype' are empty */
1536 clear_string_option(&buf->b_p_bh);
1537 clear_string_option(&buf->b_p_bt);
1538 }
1539#endif
1540 }
1541 if (buf != curbuf || curbuf == NULL)
1542 {
1543 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1544 if (buf == NULL)
1545 {
1546 vim_free(ffname);
1547 return NULL;
1548 }
1549 }
1550
1551 if (ffname != NULL)
1552 {
1553 buf->b_ffname = ffname;
1554 buf->b_sfname = vim_strsave(sfname);
1555 }
1556
1557 clear_wininfo(buf);
1558 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1559
1560 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1561 || buf->b_wininfo == NULL)
1562 {
1563 vim_free(buf->b_ffname);
1564 buf->b_ffname = NULL;
1565 vim_free(buf->b_sfname);
1566 buf->b_sfname = NULL;
1567 if (buf != curbuf)
1568 free_buffer(buf);
1569 return NULL;
1570 }
1571
1572 if (buf == curbuf)
1573 {
1574 /* free all things allocated for this buffer */
1575 buf_freeall(buf, FALSE, FALSE);
1576 if (buf != curbuf) /* autocommands deleted the buffer! */
1577 return NULL;
1578#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001579 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 return NULL;
1581#endif
1582 /* buf->b_nwindows = 0; why was this here? */
1583 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1584#ifdef FEAT_KEYMAP
1585 /* need to reload lmaps and set b:keymap_name */
1586 curbuf->b_kmap_state |= KEYMAP_INIT;
1587#endif
1588 }
1589 else
1590 {
1591 /*
1592 * put new buffer at the end of the buffer list
1593 */
1594 buf->b_next = NULL;
1595 if (firstbuf == NULL) /* buffer list is empty */
1596 {
1597 buf->b_prev = NULL;
1598 firstbuf = buf;
1599 }
1600 else /* append new buffer at end of list */
1601 {
1602 lastbuf->b_next = buf;
1603 buf->b_prev = lastbuf;
1604 }
1605 lastbuf = buf;
1606
1607 buf->b_fnum = top_file_num++;
1608 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1609 {
1610 EMSG(_("W14: Warning: List of file names overflow"));
1611 if (emsg_silent == 0)
1612 {
1613 out_flush();
1614 ui_delay(3000L, TRUE); /* make sure it is noticed */
1615 }
1616 top_file_num = 1;
1617 }
1618
1619 /*
1620 * Always copy the options from the current buffer.
1621 */
1622 buf_copy_options(buf, BCO_ALWAYS);
1623 }
1624
1625 buf->b_wininfo->wi_fpos.lnum = lnum;
1626 buf->b_wininfo->wi_win = curwin;
1627
1628#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001629 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1630#endif
1631#ifdef FEAT_SYN_HL
1632 hash_init(&buf->b_keywtab);
1633 hash_init(&buf->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634#endif
1635
1636 buf->b_fname = buf->b_sfname;
1637#ifdef UNIX
1638 if (st.st_dev == (dev_T)-1)
1639 buf->b_dev = -1;
1640 else
1641 {
1642 buf->b_dev = st.st_dev;
1643 buf->b_ino = st.st_ino;
1644 }
1645#endif
1646 buf->b_u_synced = TRUE;
1647 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001648 if (flags & BLN_DUMMY)
1649 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 buf_clear_file(buf);
1651 clrallmarks(buf); /* clear marks */
1652 fmarks_check_names(buf); /* check file marks for this file */
1653 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1654#ifdef FEAT_AUTOCMD
1655 if (!(flags & BLN_DUMMY))
1656 {
1657 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1658 if (flags & BLN_LISTED)
1659 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1660# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001661 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 return NULL;
1663# endif
1664 }
1665#endif
1666
1667 return buf;
1668}
1669
1670/*
1671 * Free the memory for the options of a buffer.
1672 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1673 * 'fileencoding'.
1674 */
1675 void
1676free_buf_options(buf, free_p_ff)
1677 buf_T *buf;
1678 int free_p_ff;
1679{
1680 if (free_p_ff)
1681 {
1682#ifdef FEAT_MBYTE
1683 clear_string_option(&buf->b_p_fenc);
1684#endif
1685 clear_string_option(&buf->b_p_ff);
1686#ifdef FEAT_QUICKFIX
1687 clear_string_option(&buf->b_p_bh);
1688 clear_string_option(&buf->b_p_bt);
1689#endif
1690 }
1691#ifdef FEAT_FIND_ID
1692 clear_string_option(&buf->b_p_def);
1693 clear_string_option(&buf->b_p_inc);
1694# ifdef FEAT_EVAL
1695 clear_string_option(&buf->b_p_inex);
1696# endif
1697#endif
1698#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1699 clear_string_option(&buf->b_p_inde);
1700 clear_string_option(&buf->b_p_indk);
1701#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001702#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1703 clear_string_option(&buf->b_p_bexpr);
1704#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001705#if defined(FEAT_EVAL)
1706 clear_string_option(&buf->b_p_fex);
1707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#ifdef FEAT_CRYPT
1709 clear_string_option(&buf->b_p_key);
1710#endif
1711 clear_string_option(&buf->b_p_kp);
1712 clear_string_option(&buf->b_p_mps);
1713 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001714 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 clear_string_option(&buf->b_p_isk);
1716#ifdef FEAT_KEYMAP
1717 clear_string_option(&buf->b_p_keymap);
1718 ga_clear(&buf->b_kmap_ga);
1719#endif
1720#ifdef FEAT_COMMENTS
1721 clear_string_option(&buf->b_p_com);
1722#endif
1723#ifdef FEAT_FOLDING
1724 clear_string_option(&buf->b_p_cms);
1725#endif
1726 clear_string_option(&buf->b_p_nf);
1727#ifdef FEAT_SYN_HL
1728 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001729#endif
1730#ifdef FEAT_SPELL
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001731 clear_string_option(&buf->b_p_spc);
Bram Moolenaar86bc1fb2005-06-07 20:58:01 +00001732 clear_string_option(&buf->b_p_spf);
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001733 vim_free(buf->b_cap_prog);
1734 buf->b_cap_prog = NULL;
Bram Moolenaara0dea672005-03-20 22:23:10 +00001735 clear_string_option(&buf->b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736#endif
1737#ifdef FEAT_SEARCHPATH
1738 clear_string_option(&buf->b_p_sua);
1739#endif
1740#ifdef FEAT_AUTOCMD
1741 clear_string_option(&buf->b_p_ft);
1742#endif
1743#ifdef FEAT_OSFILETYPE
1744 clear_string_option(&buf->b_p_oft);
1745#endif
1746#ifdef FEAT_CINDENT
1747 clear_string_option(&buf->b_p_cink);
1748 clear_string_option(&buf->b_p_cino);
1749#endif
1750#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1751 clear_string_option(&buf->b_p_cinw);
1752#endif
1753#ifdef FEAT_INS_EXPAND
1754 clear_string_option(&buf->b_p_cpt);
1755#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001756#ifdef FEAT_COMPL_FUNC
1757 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001758 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760#ifdef FEAT_QUICKFIX
1761 clear_string_option(&buf->b_p_gp);
1762 clear_string_option(&buf->b_p_mp);
1763 clear_string_option(&buf->b_p_efm);
1764#endif
1765 clear_string_option(&buf->b_p_ep);
1766 clear_string_option(&buf->b_p_path);
1767 clear_string_option(&buf->b_p_tags);
1768#ifdef FEAT_INS_EXPAND
1769 clear_string_option(&buf->b_p_dict);
1770 clear_string_option(&buf->b_p_tsr);
1771#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001772#ifdef FEAT_TEXTOBJ
1773 clear_string_option(&buf->b_p_qe);
1774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 buf->b_p_ar = -1;
1776}
1777
1778/*
1779 * get alternate file n
1780 * set linenr to lnum or altfpos.lnum if lnum == 0
1781 * also set cursor column to altfpos.col if 'startofline' is not set.
1782 * if (options & GETF_SETMARK) call setpcmark()
1783 * if (options & GETF_ALT) we are jumping to an alternate file.
1784 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1785 *
1786 * return FAIL for failure, OK for success
1787 */
1788 int
1789buflist_getfile(n, lnum, options, forceit)
1790 int n;
1791 linenr_T lnum;
1792 int options;
1793 int forceit;
1794{
1795 buf_T *buf;
1796#ifdef FEAT_WINDOWS
1797 win_T *wp = NULL;
1798#endif
1799 pos_T *fpos;
1800 colnr_T col;
1801
1802 buf = buflist_findnr(n);
1803 if (buf == NULL)
1804 {
1805 if ((options & GETF_ALT) && n == 0)
1806 EMSG(_(e_noalt));
1807 else
1808 EMSGN(_("E92: Buffer %ld not found"), n);
1809 return FAIL;
1810 }
1811
1812 /* if alternate file is the current buffer, nothing to do */
1813 if (buf == curbuf)
1814 return OK;
1815
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001816 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001817 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001818 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001820 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001821#ifdef FEAT_AUTOCMD
1822 if (curbuf_locked())
1823 return FAIL;
1824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826 /* altfpos may be changed by getfile(), get it now */
1827 if (lnum == 0)
1828 {
1829 fpos = buflist_findfpos(buf);
1830 lnum = fpos->lnum;
1831 col = fpos->col;
1832 }
1833 else
1834 col = 0;
1835
1836#ifdef FEAT_WINDOWS
1837 if (options & GETF_SWITCH)
1838 {
1839 /* use existing open window for buffer if wanted */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001840 if (vim_strchr(p_swb, 'o')) /* useopen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001842 /* use existing open window in any tab page for buffer if wanted */
1843 if (vim_strchr(p_swb, 'a')) /* usetab */
1844 wp = buf_jump_open_tab(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 /* split window if wanted ("split") */
1846 if (wp == NULL && vim_strchr(p_swb, 't') && !bufempty())
1847 {
1848 if (win_split(0, 0) == FAIL)
1849 return FAIL;
1850# ifdef FEAT_SCROLLBIND
1851 curwin->w_p_scb = FALSE;
1852# endif
1853 }
1854 }
1855#endif
1856
1857 ++RedrawingDisabled;
1858 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1859 lnum, forceit) <= 0)
1860 {
1861 --RedrawingDisabled;
1862
1863 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1864 if (!p_sol && col != 0)
1865 {
1866 curwin->w_cursor.col = col;
1867 check_cursor_col();
1868#ifdef FEAT_VIRTUALEDIT
1869 curwin->w_cursor.coladd = 0;
1870#endif
1871 curwin->w_set_curswant = TRUE;
1872 }
1873 return OK;
1874 }
1875 --RedrawingDisabled;
1876 return FAIL;
1877}
1878
1879/*
1880 * go to the last know line number for the current buffer
1881 */
1882 void
1883buflist_getfpos()
1884{
1885 pos_T *fpos;
1886
1887 fpos = buflist_findfpos(curbuf);
1888
1889 curwin->w_cursor.lnum = fpos->lnum;
1890 check_cursor_lnum();
1891
1892 if (p_sol)
1893 curwin->w_cursor.col = 0;
1894 else
1895 {
1896 curwin->w_cursor.col = fpos->col;
1897 check_cursor_col();
1898#ifdef FEAT_VIRTUALEDIT
1899 curwin->w_cursor.coladd = 0;
1900#endif
1901 curwin->w_set_curswant = TRUE;
1902 }
1903}
1904
Bram Moolenaar81695252004-12-29 20:58:21 +00001905#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1906/*
1907 * Find file in buffer list by name (it has to be for the current window).
1908 * Returns NULL if not found.
1909 */
1910 buf_T *
1911buflist_findname_exp(fname)
1912 char_u *fname;
1913{
1914 char_u *ffname;
1915 buf_T *buf = NULL;
1916
1917 /* First make the name into a full path name */
1918 ffname = FullName_save(fname,
1919#ifdef UNIX
1920 TRUE /* force expansion, get rid of symbolic links */
1921#else
1922 FALSE
1923#endif
1924 );
1925 if (ffname != NULL)
1926 {
1927 buf = buflist_findname(ffname);
1928 vim_free(ffname);
1929 }
1930 return buf;
1931}
1932#endif
1933
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934/*
1935 * Find file in buffer list by name (it has to be for the current window).
1936 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00001937 * Skips dummy buffers.
1938 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 */
1940 buf_T *
1941buflist_findname(ffname)
1942 char_u *ffname;
1943{
1944#ifdef UNIX
1945 struct stat st;
1946
1947 if (mch_stat((char *)ffname, &st) < 0)
1948 st.st_dev = (dev_T)-1;
1949 return buflist_findname_stat(ffname, &st);
1950}
1951
1952/*
1953 * Same as buflist_findname(), but pass the stat structure to avoid getting it
1954 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00001955 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 */
1957 static buf_T *
1958buflist_findname_stat(ffname, stp)
1959 char_u *ffname;
1960 struct stat *stp;
1961{
1962#endif
1963 buf_T *buf;
1964
1965 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00001966 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967#ifdef UNIX
1968 , stp
1969#endif
1970 ))
1971 return buf;
1972 return NULL;
1973}
1974
1975#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
1976/*
1977 * Find file in buffer list by a regexp pattern.
1978 * Return fnum of the found buffer.
1979 * Return < 0 for error.
1980 */
1981/*ARGSUSED*/
1982 int
1983buflist_findpat(pattern, pattern_end, unlisted, diffmode)
1984 char_u *pattern;
1985 char_u *pattern_end; /* pointer to first char after pattern */
1986 int unlisted; /* find unlisted buffers */
1987 int diffmode; /* find diff-mode buffers only */
1988{
1989 buf_T *buf;
1990 regprog_T *prog;
1991 int match = -1;
1992 int find_listed;
1993 char_u *pat;
1994 char_u *patend;
1995 int attempt;
1996 char_u *p;
1997 int toggledollar;
1998
1999 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2000 {
2001 if (*pattern == '%')
2002 match = curbuf->b_fnum;
2003 else
2004 match = curwin->w_alt_fnum;
2005#ifdef FEAT_DIFF
2006 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2007 match = -1;
2008#endif
2009 }
2010
2011 /*
2012 * Try four ways of matching a listed buffer:
2013 * attempt == 0: without '^' or '$' (at any position)
2014 * attempt == 1: with '^' at start (only at postion 0)
2015 * attempt == 2: with '$' at end (only match at end)
2016 * attempt == 3: with '^' at start and '$' at end (only full match)
2017 * Repeat this for finding an unlisted buffer if there was no matching
2018 * listed buffer.
2019 */
2020 else
2021 {
2022 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2023 if (pat == NULL)
2024 return -1;
2025 patend = pat + STRLEN(pat) - 1;
2026 toggledollar = (patend > pat && *patend == '$');
2027
2028 /* First try finding a listed buffer. If not found and "unlisted"
2029 * is TRUE, try finding an unlisted buffer. */
2030 find_listed = TRUE;
2031 for (;;)
2032 {
2033 for (attempt = 0; attempt <= 3; ++attempt)
2034 {
2035 /* may add '^' and '$' */
2036 if (toggledollar)
2037 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2038 p = pat;
2039 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2040 ++p;
2041 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2042 if (prog == NULL)
2043 {
2044 vim_free(pat);
2045 return -1;
2046 }
2047
2048 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2049 if (buf->b_p_bl == find_listed
2050#ifdef FEAT_DIFF
2051 && (!diffmode || diff_mode_buf(buf))
2052#endif
2053 && buflist_match(prog, buf) != NULL)
2054 {
2055 if (match >= 0) /* already found a match */
2056 {
2057 match = -2;
2058 break;
2059 }
2060 match = buf->b_fnum; /* remember first match */
2061 }
2062
2063 vim_free(prog);
2064 if (match >= 0) /* found one match */
2065 break;
2066 }
2067
2068 /* Only search for unlisted buffers if there was no match with
2069 * a listed buffer. */
2070 if (!unlisted || !find_listed || match != -1)
2071 break;
2072 find_listed = FALSE;
2073 }
2074
2075 vim_free(pat);
2076 }
2077
2078 if (match == -2)
2079 EMSG2(_("E93: More than one match for %s"), pattern);
2080 else if (match < 0)
2081 EMSG2(_("E94: No matching buffer for %s"), pattern);
2082 return match;
2083}
2084#endif
2085
2086#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2087
2088/*
2089 * Find all buffer names that match.
2090 * For command line expansion of ":buf" and ":sbuf".
2091 * Return OK if matches found, FAIL otherwise.
2092 */
2093 int
2094ExpandBufnames(pat, num_file, file, options)
2095 char_u *pat;
2096 int *num_file;
2097 char_u ***file;
2098 int options;
2099{
2100 int count = 0;
2101 buf_T *buf;
2102 int round;
2103 char_u *p;
2104 int attempt;
2105 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002106 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
2108 *num_file = 0; /* return values in case of FAIL */
2109 *file = NULL;
2110
Bram Moolenaar05159a02005-02-26 23:04:13 +00002111 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2112 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002114 patc = alloc((unsigned)STRLEN(pat) + 11);
2115 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002117 STRCPY(patc, "\\(^\\|[\\/]\\)");
2118 STRCPY(patc + 11, pat + 1);
2119 }
2120 else
2121 patc = pat;
2122
2123 /*
2124 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002125 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002126 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002127 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002128 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002129 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002130 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002131 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002132 if (prog == NULL)
2133 {
2134 if (patc != pat)
2135 vim_free(patc);
2136 return FAIL;
2137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
2139 /*
2140 * round == 1: Count the matches.
2141 * round == 2: Build the array to keep the matches.
2142 */
2143 for (round = 1; round <= 2; ++round)
2144 {
2145 count = 0;
2146 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2147 {
2148 if (!buf->b_p_bl) /* skip unlisted buffers */
2149 continue;
2150 p = buflist_match(prog, buf);
2151 if (p != NULL)
2152 {
2153 if (round == 1)
2154 ++count;
2155 else
2156 {
2157 if (options & WILD_HOME_REPLACE)
2158 p = home_replace_save(buf, p);
2159 else
2160 p = vim_strsave(p);
2161 (*file)[count++] = p;
2162 }
2163 }
2164 }
2165 if (count == 0) /* no match found, break here */
2166 break;
2167 if (round == 1)
2168 {
2169 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2170 if (*file == NULL)
2171 {
2172 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002173 if (patc != pat)
2174 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 return FAIL;
2176 }
2177 }
2178 }
2179 vim_free(prog);
2180 if (count) /* match(es) found, break here */
2181 break;
2182 }
2183
Bram Moolenaar05159a02005-02-26 23:04:13 +00002184 if (patc != pat)
2185 vim_free(patc);
2186
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 *num_file = count;
2188 return (count == 0 ? FAIL : OK);
2189}
2190
2191#endif /* FEAT_CMDL_COMPL */
2192
2193#ifdef HAVE_BUFLIST_MATCH
2194/*
2195 * Check for a match on the file name for buffer "buf" with regprog "prog".
2196 */
2197 static char_u *
2198buflist_match(prog, buf)
2199 regprog_T *prog;
2200 buf_T *buf;
2201{
2202 char_u *match;
2203
2204 /* First try the short file name, then the long file name. */
2205 match = fname_match(prog, buf->b_sfname);
2206 if (match == NULL)
2207 match = fname_match(prog, buf->b_ffname);
2208
2209 return match;
2210}
2211
2212/*
2213 * Try matching the regexp in "prog" with file name "name".
2214 * Return "name" when there is a match, NULL when not.
2215 */
2216 static char_u *
2217fname_match(prog, name)
2218 regprog_T *prog;
2219 char_u *name;
2220{
2221 char_u *match = NULL;
2222 char_u *p;
2223 regmatch_T regmatch;
2224
2225 if (name != NULL)
2226 {
2227 regmatch.regprog = prog;
2228#ifdef CASE_INSENSITIVE_FILENAME
2229 regmatch.rm_ic = TRUE; /* Always ignore case */
2230#else
2231 regmatch.rm_ic = FALSE; /* Never ignore case */
2232#endif
2233
2234 if (vim_regexec(&regmatch, name, (colnr_T)0))
2235 match = name;
2236 else
2237 {
2238 /* Replace $(HOME) with '~' and try matching again. */
2239 p = home_replace_save(NULL, name);
2240 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2241 match = name;
2242 vim_free(p);
2243 }
2244 }
2245
2246 return match;
2247}
2248#endif
2249
2250/*
2251 * find file in buffer list by number
2252 */
2253 buf_T *
2254buflist_findnr(nr)
2255 int nr;
2256{
2257 buf_T *buf;
2258
2259 if (nr == 0)
2260 nr = curwin->w_alt_fnum;
2261 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2262 if (buf->b_fnum == nr)
2263 return (buf);
2264 return NULL;
2265}
2266
2267/*
2268 * Get name of file 'n' in the buffer list.
2269 * When the file has no name an empty string is returned.
2270 * home_replace() is used to shorten the file name (used for marks).
2271 * Returns a pointer to allocated memory, of NULL when failed.
2272 */
2273 char_u *
2274buflist_nr2name(n, fullname, helptail)
2275 int n;
2276 int fullname;
2277 int helptail; /* for help buffers return tail only */
2278{
2279 buf_T *buf;
2280
2281 buf = buflist_findnr(n);
2282 if (buf == NULL)
2283 return NULL;
2284 return home_replace_save(helptail ? buf : NULL,
2285 fullname ? buf->b_ffname : buf->b_fname);
2286}
2287
2288/*
2289 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2290 * When "copy_options" is TRUE save the local window option values.
2291 * When "lnum" is 0 only do the options.
2292 */
2293 static void
2294buflist_setfpos(buf, win, lnum, col, copy_options)
2295 buf_T *buf;
2296 win_T *win;
2297 linenr_T lnum;
2298 colnr_T col;
2299 int copy_options;
2300{
2301 wininfo_T *wip;
2302
2303 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2304 if (wip->wi_win == win)
2305 break;
2306 if (wip == NULL)
2307 {
2308 /* allocate a new entry */
2309 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2310 if (wip == NULL)
2311 return;
2312 wip->wi_win = win;
2313 if (lnum == 0) /* set lnum even when it's 0 */
2314 lnum = 1;
2315 }
2316 else
2317 {
2318 /* remove the entry from the list */
2319 if (wip->wi_prev)
2320 wip->wi_prev->wi_next = wip->wi_next;
2321 else
2322 buf->b_wininfo = wip->wi_next;
2323 if (wip->wi_next)
2324 wip->wi_next->wi_prev = wip->wi_prev;
2325 if (copy_options && wip->wi_optset)
2326 {
2327 clear_winopt(&wip->wi_opt);
2328#ifdef FEAT_FOLDING
2329 deleteFoldRecurse(&wip->wi_folds);
2330#endif
2331 }
2332 }
2333 if (lnum != 0)
2334 {
2335 wip->wi_fpos.lnum = lnum;
2336 wip->wi_fpos.col = col;
2337 }
2338 if (copy_options)
2339 {
2340 /* Save the window-specific option values. */
2341 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2342#ifdef FEAT_FOLDING
2343 wip->wi_fold_manual = win->w_fold_manual;
2344 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2345#endif
2346 wip->wi_optset = TRUE;
2347 }
2348
2349 /* insert the entry in front of the list */
2350 wip->wi_next = buf->b_wininfo;
2351 buf->b_wininfo = wip;
2352 wip->wi_prev = NULL;
2353 if (wip->wi_next)
2354 wip->wi_next->wi_prev = wip;
2355
2356 return;
2357}
2358
2359/*
2360 * Find info for the current window in buffer "buf".
2361 * If not found, return the info for the most recently used window.
2362 * Returns NULL when there isn't any info.
2363 */
2364 static wininfo_T *
2365find_wininfo(buf)
2366 buf_T *buf;
2367{
2368 wininfo_T *wip;
2369
2370 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2371 if (wip->wi_win == curwin)
2372 break;
2373 if (wip == NULL) /* if no fpos for curwin, use the first in the list */
2374 wip = buf->b_wininfo;
2375 return wip;
2376}
2377
2378/*
2379 * Reset the local window options to the values last used in this window.
2380 * If the buffer wasn't used in this window before, use the values from
2381 * the most recently used window. If the values were never set, use the
2382 * global values for the window.
2383 */
2384 void
2385get_winopts(buf)
2386 buf_T *buf;
2387{
2388 wininfo_T *wip;
2389
2390 clear_winopt(&curwin->w_onebuf_opt);
2391#ifdef FEAT_FOLDING
2392 clearFolding(curwin);
2393#endif
2394
2395 wip = find_wininfo(buf);
2396 if (wip != NULL && wip->wi_optset)
2397 {
2398 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2399#ifdef FEAT_FOLDING
2400 curwin->w_fold_manual = wip->wi_fold_manual;
2401 curwin->w_foldinvalid = TRUE;
2402 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2403#endif
2404 }
2405 else
2406 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2407
2408#ifdef FEAT_FOLDING
2409 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2410 if (p_fdls >= 0)
2411 curwin->w_p_fdl = p_fdls;
2412#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002413
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002414#ifdef FEAT_SPELL
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002415 if (curwin->w_p_spell && *buf->b_p_spl != NUL)
2416 did_set_spelllang(buf);
2417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418}
2419
2420/*
2421 * Find the position (lnum and col) for the buffer 'buf' for the current
2422 * window.
2423 * Returns a pointer to no_position if no position is found.
2424 */
2425 pos_T *
2426buflist_findfpos(buf)
2427 buf_T *buf;
2428{
2429 wininfo_T *wip;
2430 static pos_T no_position = {1, 0};
2431
2432 wip = find_wininfo(buf);
2433 if (wip != NULL)
2434 return &(wip->wi_fpos);
2435 else
2436 return &no_position;
2437}
2438
2439/*
2440 * Find the lnum for the buffer 'buf' for the current window.
2441 */
2442 linenr_T
2443buflist_findlnum(buf)
2444 buf_T *buf;
2445{
2446 return buflist_findfpos(buf)->lnum;
2447}
2448
2449#if defined(FEAT_LISTCMDS) || defined(PROTO)
2450/*
2451 * List all know file names (for :files and :buffers command).
2452 */
2453/*ARGSUSED*/
2454 void
2455buflist_list(eap)
2456 exarg_T *eap;
2457{
2458 buf_T *buf;
2459 int len;
2460 int i;
2461
2462 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2463 {
2464 /* skip unlisted buffers, unless ! was used */
2465 if (!buf->b_p_bl && !eap->forceit)
2466 continue;
2467 msg_putchar('\n');
2468 if (buf_spname(buf) != NULL)
2469 STRCPY(NameBuff, buf_spname(buf));
2470 else
2471 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2472
Bram Moolenaar50cde822005-06-05 21:54:54 +00002473 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 buf->b_fnum,
2475 buf->b_p_bl ? ' ' : 'u',
2476 buf == curbuf ? '%' :
2477 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2478 buf->b_ml.ml_mfp == NULL ? ' ' :
2479 (buf->b_nwindows == 0 ? 'h' : 'a'),
2480 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2481 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002482 : (bufIsChanged(buf) ? '+' : ' '),
2483 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 i = 40 - vim_strsize(IObuff);
2487 do
2488 {
2489 IObuff[len++] = ' ';
2490 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002491 vim_snprintf((char *)IObuff + len, IOSIZE - len, _("line %ld"),
2492 buf == curbuf ? curwin->w_cursor.lnum
2493 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 msg_outtrans(IObuff);
2495 out_flush(); /* output one line at a time */
2496 ui_breakcheck();
2497 }
2498}
2499#endif
2500
2501/*
2502 * Get file name and line number for file 'fnum'.
2503 * Used by DoOneCmd() for translating '%' and '#'.
2504 * Used by insert_reg() and cmdline_paste() for '#' register.
2505 * Return FAIL if not found, OK for success.
2506 */
2507 int
2508buflist_name_nr(fnum, fname, lnum)
2509 int fnum;
2510 char_u **fname;
2511 linenr_T *lnum;
2512{
2513 buf_T *buf;
2514
2515 buf = buflist_findnr(fnum);
2516 if (buf == NULL || buf->b_fname == NULL)
2517 return FAIL;
2518
2519 *fname = buf->b_fname;
2520 *lnum = buflist_findlnum(buf);
2521
2522 return OK;
2523}
2524
2525/*
2526 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2527 * The file name with the full path is also remembered, for when :cd is used.
2528 * Returns FAIL for failure (file name already in use by other buffer)
2529 * OK otherwise.
2530 */
2531 int
2532setfname(buf, ffname, sfname, message)
2533 buf_T *buf;
2534 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002535 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
Bram Moolenaar81695252004-12-29 20:58:21 +00002537 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538#ifdef UNIX
2539 struct stat st;
2540#endif
2541
2542 if (ffname == NULL || *ffname == NUL)
2543 {
2544 /* Removing the name. */
2545 vim_free(buf->b_ffname);
2546 vim_free(buf->b_sfname);
2547 buf->b_ffname = NULL;
2548 buf->b_sfname = NULL;
2549#ifdef UNIX
2550 st.st_dev = (dev_T)-1;
2551#endif
2552 }
2553 else
2554 {
2555 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2556 if (ffname == NULL) /* out of memory */
2557 return FAIL;
2558
2559 /*
2560 * if the file name is already used in another buffer:
2561 * - if the buffer is loaded, fail
2562 * - if the buffer is not loaded, delete it from the list
2563 */
2564#ifdef UNIX
2565 if (mch_stat((char *)ffname, &st) < 0)
2566 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002567#endif
2568 if (!(buf->b_flags & BF_DUMMY))
2569#ifdef UNIX
2570 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002572 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573#endif
2574 if (obuf != NULL && obuf != buf)
2575 {
2576 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2577 {
2578 if (message)
2579 EMSG(_("E95: Buffer with this name already exists"));
2580 vim_free(ffname);
2581 return FAIL;
2582 }
2583 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2584 }
2585 sfname = vim_strsave(sfname);
2586 if (ffname == NULL || sfname == NULL)
2587 {
2588 vim_free(sfname);
2589 vim_free(ffname);
2590 return FAIL;
2591 }
2592#ifdef USE_FNAME_CASE
2593# ifdef USE_LONG_FNAME
2594 if (USE_LONG_FNAME)
2595# endif
2596 fname_case(sfname, 0); /* set correct case for short file name */
2597#endif
2598 vim_free(buf->b_ffname);
2599 vim_free(buf->b_sfname);
2600 buf->b_ffname = ffname;
2601 buf->b_sfname = sfname;
2602 }
2603 buf->b_fname = buf->b_sfname;
2604#ifdef UNIX
2605 if (st.st_dev == (dev_T)-1)
2606 buf->b_dev = -1;
2607 else
2608 {
2609 buf->b_dev = st.st_dev;
2610 buf->b_ino = st.st_ino;
2611 }
2612#endif
2613
2614#ifndef SHORT_FNAME
2615 buf->b_shortname = FALSE;
2616#endif
2617
2618 buf_name_changed(buf);
2619 return OK;
2620}
2621
2622/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002623 * Crude way of changing the name of a buffer. Use with care!
2624 * The name should be relative to the current directory.
2625 */
2626 void
2627buf_set_name(fnum, name)
2628 int fnum;
2629 char_u *name;
2630{
2631 buf_T *buf;
2632
2633 buf = buflist_findnr(fnum);
2634 if (buf != NULL)
2635 {
2636 vim_free(buf->b_sfname);
2637 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002638 buf->b_ffname = vim_strsave(name);
2639 buf->b_sfname = NULL;
2640 /* Allocate ffname and expand into full path. Also resolves .lnk
2641 * files on Win32. */
2642 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002643 buf->b_fname = buf->b_sfname;
2644 }
2645}
2646
2647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 * Take care of what needs to be done when the name of buffer "buf" has
2649 * changed.
2650 */
2651 void
2652buf_name_changed(buf)
2653 buf_T *buf;
2654{
2655 /*
2656 * If the file name changed, also change the name of the swapfile
2657 */
2658 if (buf->b_ml.ml_mfp != NULL)
2659 ml_setname(buf);
2660
2661 if (curwin->w_buffer == buf)
2662 check_arg_idx(curwin); /* check file name for arg list */
2663#ifdef FEAT_TITLE
2664 maketitle(); /* set window title */
2665#endif
2666#ifdef FEAT_WINDOWS
2667 status_redraw_all(); /* status lines need to be redrawn */
2668#endif
2669 fmarks_check_names(buf); /* check named file marks */
2670 ml_timestamp(buf); /* reset timestamp */
2671}
2672
2673/*
2674 * set alternate file name for current window
2675 *
2676 * Used by do_one_cmd(), do_write() and do_ecmd().
2677 * Return the buffer.
2678 */
2679 buf_T *
2680setaltfname(ffname, sfname, lnum)
2681 char_u *ffname;
2682 char_u *sfname;
2683 linenr_T lnum;
2684{
2685 buf_T *buf;
2686
2687 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2688 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002689 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 curwin->w_alt_fnum = buf->b_fnum;
2691 return buf;
2692}
2693
2694/*
2695 * Get alternate file name for current window.
2696 * Return NULL if there isn't any, and give error message if requested.
2697 */
2698 char_u *
2699getaltfname(errmsg)
2700 int errmsg; /* give error message */
2701{
2702 char_u *fname;
2703 linenr_T dummy;
2704
2705 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2706 {
2707 if (errmsg)
2708 EMSG(_(e_noalt));
2709 return NULL;
2710 }
2711 return fname;
2712}
2713
2714/*
2715 * Add a file name to the buflist and return its number.
2716 * Uses same flags as buflist_new(), except BLN_DUMMY.
2717 *
2718 * used by qf_init(), main() and doarglist()
2719 */
2720 int
2721buflist_add(fname, flags)
2722 char_u *fname;
2723 int flags;
2724{
2725 buf_T *buf;
2726
2727 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2728 if (buf != NULL)
2729 return buf->b_fnum;
2730 return 0;
2731}
2732
2733#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2734/*
2735 * Adjust slashes in file names. Called after 'shellslash' was set.
2736 */
2737 void
2738buflist_slash_adjust()
2739{
2740 buf_T *bp;
2741
2742 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2743 {
2744 if (bp->b_ffname != NULL)
2745 slash_adjust(bp->b_ffname);
2746 if (bp->b_sfname != NULL)
2747 slash_adjust(bp->b_sfname);
2748 }
2749}
2750#endif
2751
2752/*
2753 * Set alternate cursor position for current window.
2754 * Also save the local window option values.
2755 */
2756 void
2757buflist_altfpos()
2758{
2759 buflist_setfpos(curbuf, curwin, curwin->w_cursor.lnum,
2760 curwin->w_cursor.col, TRUE);
2761}
2762
2763/*
2764 * Return TRUE if 'ffname' is not the same file as current file.
2765 * Fname must have a full path (expanded by mch_FullName()).
2766 */
2767 int
2768otherfile(ffname)
2769 char_u *ffname;
2770{
2771 return otherfile_buf(curbuf, ffname
2772#ifdef UNIX
2773 , NULL
2774#endif
2775 );
2776}
2777
2778 static int
2779otherfile_buf(buf, ffname
2780#ifdef UNIX
2781 , stp
2782#endif
2783 )
2784 buf_T *buf;
2785 char_u *ffname;
2786#ifdef UNIX
2787 struct stat *stp;
2788#endif
2789{
2790 /* no name is different */
2791 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2792 return TRUE;
2793 if (fnamecmp(ffname, buf->b_ffname) == 0)
2794 return FALSE;
2795#ifdef UNIX
2796 {
2797 struct stat st;
2798
2799 /* If no struct stat given, get it now */
2800 if (stp == NULL)
2801 {
2802 if (buf->b_dev < 0 || mch_stat((char *)ffname, &st) < 0)
2803 st.st_dev = (dev_T)-1;
2804 stp = &st;
2805 }
2806 /* Use dev/ino to check if the files are the same, even when the names
2807 * are different (possible with links). Still need to compare the
2808 * name above, for when the file doesn't exist yet.
2809 * Problem: The dev/ino changes when a file is deleted (and created
2810 * again) and remains the same when renamed/moved. We don't want to
2811 * mch_stat() each buffer each time, that would be too slow. Get the
2812 * dev/ino again when they appear to match, but not when they appear
2813 * to be different: Could skip a buffer when it's actually the same
2814 * file. */
2815 if (buf_same_ino(buf, stp))
2816 {
2817 buf_setino(buf);
2818 if (buf_same_ino(buf, stp))
2819 return FALSE;
2820 }
2821 }
2822#endif
2823 return TRUE;
2824}
2825
2826#if defined(UNIX) || defined(PROTO)
2827/*
2828 * Set inode and device number for a buffer.
2829 * Must always be called when b_fname is changed!.
2830 */
2831 void
2832buf_setino(buf)
2833 buf_T *buf;
2834{
2835 struct stat st;
2836
2837 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2838 {
2839 buf->b_dev = st.st_dev;
2840 buf->b_ino = st.st_ino;
2841 }
2842 else
2843 buf->b_dev = -1;
2844}
2845
2846/*
2847 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2848 */
2849 static int
2850buf_same_ino(buf, stp)
2851 buf_T *buf;
2852 struct stat *stp;
2853{
2854 return (buf->b_dev >= 0
2855 && stp->st_dev == buf->b_dev
2856 && stp->st_ino == buf->b_ino);
2857}
2858#endif
2859
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002860/*
2861 * Print info about the current buffer.
2862 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 void
2864fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002865 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 int shorthelp;
2867 int dont_truncate;
2868{
2869 char_u *name;
2870 int n;
2871 char_u *p;
2872 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002873 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874
2875 buffer = alloc(IOSIZE);
2876 if (buffer == NULL)
2877 return;
2878
2879 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2880 {
2881 sprintf((char *)buffer, "buf %d: ", curbuf->b_fnum);
2882 p = buffer + STRLEN(buffer);
2883 }
2884 else
2885 p = buffer;
2886
2887 *p++ = '"';
2888 if (buf_spname(curbuf) != NULL)
2889 STRCPY(p, buf_spname(curbuf));
2890 else
2891 {
2892 if (!fullname && curbuf->b_fname != NULL)
2893 name = curbuf->b_fname;
2894 else
2895 name = curbuf->b_ffname;
2896 home_replace(shorthelp ? curbuf : NULL, name, p,
2897 (int)(IOSIZE - (p - buffer)), TRUE);
2898 }
2899
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002900 len = STRLEN(buffer);
2901 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 "\"%s%s%s%s%s%s",
2903 curbufIsChanged() ? (shortmess(SHM_MOD)
2904 ? " [+]" : _(" [Modified]")) : " ",
2905 (curbuf->b_flags & BF_NOTEDITED)
2906#ifdef FEAT_QUICKFIX
2907 && !bt_dontwrite(curbuf)
2908#endif
2909 ? _("[Not edited]") : "",
2910 (curbuf->b_flags & BF_NEW)
2911#ifdef FEAT_QUICKFIX
2912 && !bt_dontwrite(curbuf)
2913#endif
2914 ? _("[New file]") : "",
2915 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
2916 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
2917 : _("[readonly]")) : "",
2918 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
2919 || curbuf->b_p_ro) ?
2920 " " : "");
2921 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
2922 * causes an overflow, thus for large numbers divide instead. */
2923 if (curwin->w_cursor.lnum > 1000000L)
2924 n = (int)(((long)curwin->w_cursor.lnum) /
2925 ((long)curbuf->b_ml.ml_line_count / 100L));
2926 else
2927 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
2928 (long)curbuf->b_ml.ml_line_count);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002929 len = STRLEN(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2931 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002932 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
2934#ifdef FEAT_CMDL_INFO
2935 else if (p_ru)
2936 {
2937 /* Current line and column are already on the screen -- webb */
2938 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002939 vim_snprintf((char *)buffer + len, IOSIZE - len,
2940 _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002942 vim_snprintf((char *)buffer + len, IOSIZE - len,
2943 _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 (long)curbuf->b_ml.ml_line_count, n);
2945 }
2946#endif
2947 else
2948 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002949 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 _("line %ld of %ld --%d%%-- col "),
2951 (long)curwin->w_cursor.lnum,
2952 (long)curbuf->b_ml.ml_line_count,
2953 n);
2954 validate_virtcol();
2955 col_print(buffer + STRLEN(buffer),
2956 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
2957 }
2958
2959 (void)append_arg_number(curwin, buffer, !shortmess(SHM_FILE), IOSIZE);
2960
2961 if (dont_truncate)
2962 {
2963 /* Temporarily set msg_scroll to avoid the message being truncated.
2964 * First call msg_start() to get the message in the right place. */
2965 msg_start();
2966 n = msg_scroll;
2967 msg_scroll = TRUE;
2968 msg(buffer);
2969 msg_scroll = n;
2970 }
2971 else
2972 {
2973 p = msg_trunc_attr(buffer, FALSE, 0);
2974 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 /* Need to repeat the message after redrawing when:
2976 * - When restart_edit is set (otherwise there will be a delay
2977 * before redrawing).
2978 * - When the screen was scrolled but there is no wait-return
2979 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00002980 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 }
2982
2983 vim_free(buffer);
2984}
2985
2986 void
2987col_print(buf, col, vcol)
2988 char_u *buf;
2989 int col;
2990 int vcol;
2991{
2992 if (col == vcol)
2993 sprintf((char *)buf, "%d", col);
2994 else
2995 sprintf((char *)buf, "%d-%d", col, vcol);
2996}
2997
2998#if defined(FEAT_TITLE) || defined(PROTO)
2999/*
3000 * put file name in title bar of window and in icon title
3001 */
3002
3003static char_u *lasttitle = NULL;
3004static char_u *lasticon = NULL;
3005
3006 void
3007maketitle()
3008{
3009 char_u *p;
3010 char_u *t_str = NULL;
3011 char_u *i_name;
3012 char_u *i_str = NULL;
3013 int maxlen = 0;
3014 int len;
3015 int mustset;
3016 char_u buf[IOSIZE];
3017 int off;
3018
3019 if (!redrawing())
3020 {
3021 /* Postpone updating the title when 'lazyredraw' is set. */
3022 need_maketitle = TRUE;
3023 return;
3024 }
3025
3026 need_maketitle = FALSE;
3027 if (!p_title && !p_icon)
3028 return;
3029
3030 if (p_title)
3031 {
3032 if (p_titlelen > 0)
3033 {
3034 maxlen = p_titlelen * Columns / 100;
3035 if (maxlen < 10)
3036 maxlen = 10;
3037 }
3038
3039 t_str = buf;
3040 if (*p_titlestring != NUL)
3041 {
3042#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003043 if (stl_syntax & STL_IN_TITLE)
3044 {
3045 int use_sandbox = FALSE;
3046 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003047
3048# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003049 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003050# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003051 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003053 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003054 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003055 if (called_emsg)
3056 set_string_option_direct((char_u *)"titlestring", -1,
3057 (char_u *)"", OPT_FREE, SID_ERROR);
3058 called_emsg |= save_called_emsg;
3059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 else
3061#endif
3062 t_str = p_titlestring;
3063 }
3064 else
3065 {
3066 /* format: "fname + (path) (1 of 2) - VIM" */
3067
3068 if (curbuf->b_fname == NULL)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003069 STRCPY(buf, _("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 else
3071 {
3072 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003073 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 }
3076
3077 switch (bufIsChanged(curbuf)
3078 + (curbuf->b_p_ro * 2)
3079 + (!curbuf->b_p_ma * 4))
3080 {
3081 case 1: STRCAT(buf, " +"); break;
3082 case 2: STRCAT(buf, " ="); break;
3083 case 3: STRCAT(buf, " =+"); break;
3084 case 4:
3085 case 6: STRCAT(buf, " -"); break;
3086 case 5:
3087 case 7: STRCAT(buf, " -+"); break;
3088 }
3089
3090 if (curbuf->b_fname != NULL)
3091 {
3092 /* Get path of file, replace home dir with ~ */
3093 off = (int)STRLEN(buf);
3094 buf[off++] = ' ';
3095 buf[off++] = '(';
3096 home_replace(curbuf, curbuf->b_ffname,
3097 buf + off, IOSIZE - off, TRUE);
3098#ifdef BACKSLASH_IN_FILENAME
3099 /* avoid "c:/name" to be reduced to "c" */
3100 if (isalpha(buf[off]) && buf[off + 1] == ':')
3101 off += 2;
3102#endif
3103 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003104 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003107 vim_strncpy(buf + off, (char_u *)_("help"),
3108 IOSIZE - off - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003111
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 /* translate unprintable chars */
3113 p = transstr(buf + off);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003114 vim_strncpy(buf + off, p, IOSIZE - off - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 STRCAT(buf, ")");
3117 }
3118
3119 append_arg_number(curwin, buf, FALSE, IOSIZE);
3120
3121#if defined(FEAT_CLIENTSERVER)
3122 if (serverName != NULL)
3123 {
3124 STRCAT(buf, " - ");
3125 STRCAT(buf, serverName);
3126 }
3127 else
3128#endif
3129 STRCAT(buf, " - VIM");
3130
3131 if (maxlen > 0)
3132 {
3133 /* make it shorter by removing a bit in the middle */
3134 len = vim_strsize(buf);
3135 if (len > maxlen)
3136 trunc_string(buf, buf, maxlen);
3137 }
3138 }
3139 }
3140 mustset = ti_change(t_str, &lasttitle);
3141
3142 if (p_icon)
3143 {
3144 i_str = buf;
3145 if (*p_iconstring != NUL)
3146 {
3147#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003148 if (stl_syntax & STL_IN_ICON)
3149 {
3150 int use_sandbox = FALSE;
3151 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003152
3153# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003154 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003155# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003156 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003158 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003159 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003160 if (called_emsg)
3161 set_string_option_direct((char_u *)"iconstring", -1,
3162 (char_u *)"", OPT_FREE, SID_ERROR);
3163 called_emsg |= save_called_emsg;
3164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 else
3166#endif
3167 i_str = p_iconstring;
3168 }
3169 else
3170 {
3171 if (buf_spname(curbuf) != NULL)
3172 i_name = (char_u *)buf_spname(curbuf);
3173 else /* use file name only in icon */
3174 i_name = gettail(curbuf->b_ffname);
3175 *i_str = NUL;
3176 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003177 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 if (len > 100)
3179 {
3180 len -= 100;
3181#ifdef FEAT_MBYTE
3182 if (has_mbyte)
3183 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3184#endif
3185 i_name += len;
3186 }
3187 STRCPY(i_str, i_name);
3188 trans_characters(i_str, IOSIZE);
3189 }
3190 }
3191
3192 mustset |= ti_change(i_str, &lasticon);
3193
3194 if (mustset)
3195 resettitle();
3196}
3197
3198/*
3199 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3200 * from "str" if it does.
3201 * Return TRUE when "*last" changed.
3202 */
3203 static int
3204ti_change(str, last)
3205 char_u *str;
3206 char_u **last;
3207{
3208 if ((str == NULL) != (*last == NULL)
3209 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3210 {
3211 vim_free(*last);
3212 if (str == NULL)
3213 *last = NULL;
3214 else
3215 *last = vim_strsave(str);
3216 return TRUE;
3217 }
3218 return FALSE;
3219}
3220
3221/*
3222 * Put current window title back (used after calling a shell)
3223 */
3224 void
3225resettitle()
3226{
3227 mch_settitle(lasttitle, lasticon);
3228}
Bram Moolenaarea408852005-06-25 22:49:46 +00003229
3230# if defined(EXITFREE) || defined(PROTO)
3231 void
3232free_titles()
3233{
3234 vim_free(lasttitle);
3235 vim_free(lasticon);
3236}
3237# endif
3238
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239#endif /* FEAT_TITLE */
3240
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003241#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003243 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 * Return length of string in screen cells.
3245 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003246 * Normally works for window "wp", except when working for 'tabline' then it
3247 * is "curwin".
3248 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 * Items are drawn interspersed with the text that surrounds it
3250 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3251 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3252 *
3253 * If maxwidth is not zero, the string will be filled at any middle marker
3254 * or truncated if too long, fillchar is used for all whitespace.
3255 */
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003256/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003258build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003260 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 size_t outlen; /* length of out[] */
3262 char_u *fmt;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003263 int use_sandbox; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 int fillchar;
3265 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003266 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3267 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
3269 char_u *p;
3270 char_u *s;
3271 char_u *t;
3272 char_u *linecont;
3273#ifdef FEAT_EVAL
3274 win_T *o_curwin;
3275 buf_T *o_curbuf;
3276#endif
3277 int empty_line;
3278 colnr_T virtcol;
3279 long l;
3280 long n;
3281 int prevchar_isflag;
3282 int prevchar_isitem;
3283 int itemisflag;
3284 int fillable;
3285 char_u *str;
3286 long num;
3287 int width;
3288 int itemcnt;
3289 int curitem;
3290 int groupitem[STL_MAX_ITEM];
3291 int groupdepth;
3292 struct stl_item
3293 {
3294 char_u *start;
3295 int minwid;
3296 int maxwid;
3297 enum
3298 {
3299 Normal,
3300 Empty,
3301 Group,
3302 Middle,
3303 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003304 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 Trunc
3306 } type;
3307 } item[STL_MAX_ITEM];
3308 int minwid;
3309 int maxwid;
3310 int zeropad;
3311 char_u base;
3312 char_u opt;
3313#define TMPLEN 70
3314 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003315 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003316 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003317
3318#ifdef FEAT_EVAL
3319 /*
3320 * When the format starts with "%!" then evaluate it as an expression and
3321 * use the result as the actual format string.
3322 */
3323 if (fmt[0] == '%' && fmt[1] == '!')
3324 {
3325 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3326 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003327 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003328 }
3329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
3331 if (fillchar == 0)
3332 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003333#ifdef FEAT_MBYTE
3334 /* Can't handle a multi-byte fill character yet. */
3335 else if (mb_char2len(fillchar) > 1)
3336 fillchar = '-';
3337#endif
3338
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 /*
3340 * Get line & check if empty (cursorpos will show "0-1").
3341 * If inversion is possible we use it. Else '=' characters are used.
3342 */
3343 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3344 empty_line = (*linecont == NUL);
3345
3346 groupdepth = 0;
3347 p = out;
3348 curitem = 0;
3349 prevchar_isflag = TRUE;
3350 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003351 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
3353 if (*s != NUL && *s != '%')
3354 prevchar_isflag = prevchar_isitem = FALSE;
3355
3356 /*
3357 * Handle up to the next '%' or the end.
3358 */
3359 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3360 *p++ = *s++;
3361 if (*s == NUL || p + 1 >= out + outlen)
3362 break;
3363
3364 /*
3365 * Handle one '%' item.
3366 */
3367 s++;
3368 if (*s == '%')
3369 {
3370 if (p + 1 >= out + outlen)
3371 break;
3372 *p++ = *s++;
3373 prevchar_isflag = prevchar_isitem = FALSE;
3374 continue;
3375 }
3376 if (*s == STL_MIDDLEMARK)
3377 {
3378 s++;
3379 if (groupdepth > 0)
3380 continue;
3381 item[curitem].type = Middle;
3382 item[curitem++].start = p;
3383 continue;
3384 }
3385 if (*s == STL_TRUNCMARK)
3386 {
3387 s++;
3388 item[curitem].type = Trunc;
3389 item[curitem++].start = p;
3390 continue;
3391 }
3392 if (*s == ')')
3393 {
3394 s++;
3395 if (groupdepth < 1)
3396 continue;
3397 groupdepth--;
3398
3399 t = item[groupitem[groupdepth]].start;
3400 *p = NUL;
3401 l = vim_strsize(t);
3402 if (curitem > groupitem[groupdepth] + 1
3403 && item[groupitem[groupdepth]].minwid == 0)
3404 {
3405 /* remove group if all items are empty */
3406 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3407 if (item[n].type == Normal)
3408 break;
3409 if (n == curitem)
3410 {
3411 p = t;
3412 l = 0;
3413 }
3414 }
3415 if (l > item[groupitem[groupdepth]].maxwid)
3416 {
3417 /* truncate, remove n bytes of text at the start */
3418#ifdef FEAT_MBYTE
3419 if (has_mbyte)
3420 {
3421 /* Find the first character that should be included. */
3422 n = 0;
3423 while (l >= item[groupitem[groupdepth]].maxwid)
3424 {
3425 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003426 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
3428 }
3429 else
3430#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003431 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
3433 *t = '<';
3434 mch_memmove(t + 1, t + n, p - (t + n));
3435 p = p - n + 1;
3436#ifdef FEAT_MBYTE
3437 /* Fill up space left over by half a double-wide char. */
3438 while (++l < item[groupitem[groupdepth]].minwid)
3439 *p++ = fillchar;
3440#endif
3441
3442 /* correct the start of the items for the truncation */
3443 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3444 {
3445 item[l].start -= n;
3446 if (item[l].start < t)
3447 item[l].start = t;
3448 }
3449 }
3450 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3451 {
3452 /* fill */
3453 n = item[groupitem[groupdepth]].minwid;
3454 if (n < 0)
3455 {
3456 /* fill by appending characters */
3457 n = 0 - n;
3458 while (l++ < n && p + 1 < out + outlen)
3459 *p++ = fillchar;
3460 }
3461 else
3462 {
3463 /* fill by inserting characters */
3464 mch_memmove(t + n - l, t, p - t);
3465 l = n - l;
3466 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003467 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 p += l;
3469 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3470 item[n].start += l;
3471 for ( ; l > 0; l--)
3472 *t++ = fillchar;
3473 }
3474 }
3475 continue;
3476 }
3477 minwid = 0;
3478 maxwid = 9999;
3479 zeropad = FALSE;
3480 l = 1;
3481 if (*s == '0')
3482 {
3483 s++;
3484 zeropad = TRUE;
3485 }
3486 if (*s == '-')
3487 {
3488 s++;
3489 l = -1;
3490 }
3491 if (VIM_ISDIGIT(*s))
3492 {
3493 minwid = (int)getdigits(&s);
3494 if (minwid < 0) /* overflow */
3495 minwid = 0;
3496 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003497 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
3499 item[curitem].type = Highlight;
3500 item[curitem].start = p;
3501 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3502 s++;
3503 curitem++;
3504 continue;
3505 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003506 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3507 {
3508 if (*s == STL_TABCLOSENR)
3509 {
3510 if (minwid == 0)
3511 {
3512 /* %X ends the close label, go back to the previously
3513 * define tab label nr. */
3514 for (n = curitem - 1; n >= 0; --n)
3515 if (item[n].type == TabPage && item[n].minwid >= 0)
3516 {
3517 minwid = item[n].minwid;
3518 break;
3519 }
3520 }
3521 else
3522 /* close nrs are stored as negative values */
3523 minwid = - minwid;
3524 }
3525 item[curitem].type = TabPage;
3526 item[curitem].start = p;
3527 item[curitem].minwid = minwid;
3528 s++;
3529 curitem++;
3530 continue;
3531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (*s == '.')
3533 {
3534 s++;
3535 if (VIM_ISDIGIT(*s))
3536 {
3537 maxwid = (int)getdigits(&s);
3538 if (maxwid <= 0) /* overflow */
3539 maxwid = 50;
3540 }
3541 }
3542 minwid = (minwid > 50 ? 50 : minwid) * l;
3543 if (*s == '(')
3544 {
3545 groupitem[groupdepth++] = curitem;
3546 item[curitem].type = Group;
3547 item[curitem].start = p;
3548 item[curitem].minwid = minwid;
3549 item[curitem].maxwid = maxwid;
3550 s++;
3551 curitem++;
3552 continue;
3553 }
3554 if (vim_strchr(STL_ALL, *s) == NULL)
3555 {
3556 s++;
3557 continue;
3558 }
3559 opt = *s++;
3560
3561 /* OK - now for the real work */
3562 base = 'D';
3563 itemisflag = FALSE;
3564 fillable = TRUE;
3565 num = -1;
3566 str = NULL;
3567 switch (opt)
3568 {
3569 case STL_FILEPATH:
3570 case STL_FULLPATH:
3571 case STL_FILENAME:
3572 fillable = FALSE; /* don't change ' ' to fillchar */
3573 if (buf_spname(wp->w_buffer) != NULL)
3574 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3575 else
3576 {
3577 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003578 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3580 }
3581 trans_characters(NameBuff, MAXPATHL);
3582 if (opt != STL_FILENAME)
3583 str = NameBuff;
3584 else
3585 str = gettail(NameBuff);
3586 break;
3587
3588 case STL_VIM_EXPR: /* '{' */
3589 itemisflag = TRUE;
3590 t = p;
3591 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3592 *p++ = *s++;
3593 if (*s != '}') /* missing '}' or out of space */
3594 break;
3595 s++;
3596 *p = 0;
3597 p = t;
3598
3599#ifdef FEAT_EVAL
3600 sprintf((char *)tmp, "%d", curbuf->b_fnum);
3601 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3602
3603 o_curbuf = curbuf;
3604 o_curwin = curwin;
3605 curwin = wp;
3606 curbuf = wp->w_buffer;
3607
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003608 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
3610 curwin = o_curwin;
3611 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003612 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614 if (str != NULL && *str != 0)
3615 {
3616 if (*skipdigits(str) == NUL)
3617 {
3618 num = atoi((char *)str);
3619 vim_free(str);
3620 str = NULL;
3621 itemisflag = FALSE;
3622 }
3623 }
3624#endif
3625 break;
3626
3627 case STL_LINE:
3628 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3629 ? 0L : (long)(wp->w_cursor.lnum);
3630 break;
3631
3632 case STL_NUMLINES:
3633 num = wp->w_buffer->b_ml.ml_line_count;
3634 break;
3635
3636 case STL_COLUMN:
3637 num = !(State & INSERT) && empty_line
3638 ? 0 : (int)wp->w_cursor.col + 1;
3639 break;
3640
3641 case STL_VIRTCOL:
3642 case STL_VIRTCOL_ALT:
3643 /* In list mode virtcol needs to be recomputed */
3644 virtcol = wp->w_virtcol;
3645 if (wp->w_p_list && lcs_tab1 == NUL)
3646 {
3647 wp->w_p_list = FALSE;
3648 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3649 wp->w_p_list = TRUE;
3650 }
3651 ++virtcol;
3652 /* Don't display %V if it's the same as %c. */
3653 if (opt == STL_VIRTCOL_ALT
3654 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3655 ? 0 : (int)wp->w_cursor.col + 1)))
3656 break;
3657 num = (long)virtcol;
3658 break;
3659
3660 case STL_PERCENTAGE:
3661 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3662 (long)wp->w_buffer->b_ml.ml_line_count);
3663 break;
3664
3665 case STL_ALTPERCENT:
3666 str = tmp;
3667 get_rel_pos(wp, str);
3668 break;
3669
3670 case STL_ARGLISTSTAT:
3671 fillable = FALSE;
3672 tmp[0] = 0;
3673 if (append_arg_number(wp, tmp, FALSE, (int)sizeof(tmp)))
3674 str = tmp;
3675 break;
3676
3677 case STL_KEYMAP:
3678 fillable = FALSE;
3679 if (get_keymap_str(wp, tmp, TMPLEN))
3680 str = tmp;
3681 break;
3682 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003683#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003684 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685#else
3686 num = 0;
3687#endif
3688 break;
3689
3690 case STL_BUFNO:
3691 num = wp->w_buffer->b_fnum;
3692 break;
3693
3694 case STL_OFFSET_X:
3695 base = 'X';
3696 case STL_OFFSET:
3697#ifdef FEAT_BYTEOFF
3698 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3699 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3700 0L : l + 1 + (!(State & INSERT) && empty_line ?
3701 0 : (int)wp->w_cursor.col);
3702#endif
3703 break;
3704
3705 case STL_BYTEVAL_X:
3706 base = 'X';
3707 case STL_BYTEVAL:
3708 if (((State & INSERT) && wp == curwin) || empty_line)
3709 num = 0;
3710 else
3711 {
3712#ifdef FEAT_MBYTE
3713 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3714#else
3715 num = linecont[wp->w_cursor.col];
3716#endif
3717 }
3718 if (num == NL)
3719 num = 0;
3720 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3721 num = NL;
3722 break;
3723
3724 case STL_ROFLAG:
3725 case STL_ROFLAG_ALT:
3726 itemisflag = TRUE;
3727 if (wp->w_buffer->b_p_ro)
3728 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3729 break;
3730
3731 case STL_HELPFLAG:
3732 case STL_HELPFLAG_ALT:
3733 itemisflag = TRUE;
3734 if (wp->w_buffer->b_help)
3735 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003736 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 break;
3738
3739#ifdef FEAT_AUTOCMD
3740 case STL_FILETYPE:
3741 if (*wp->w_buffer->b_p_ft != NUL
3742 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3743 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003744 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3745 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 str = tmp;
3747 }
3748 break;
3749
3750 case STL_FILETYPE_ALT:
3751 itemisflag = TRUE;
3752 if (*wp->w_buffer->b_p_ft != NUL
3753 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3754 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003755 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3756 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 for (t = tmp; *t != 0; t++)
3758 *t = TOUPPER_LOC(*t);
3759 str = tmp;
3760 }
3761 break;
3762#endif
3763
3764#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3765 case STL_PREVIEWFLAG:
3766 case STL_PREVIEWFLAG_ALT:
3767 itemisflag = TRUE;
3768 if (wp->w_p_pvw)
3769 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3770 : _("[Preview]"));
3771 break;
3772#endif
3773
3774 case STL_MODIFIED:
3775 case STL_MODIFIED_ALT:
3776 itemisflag = TRUE;
3777 switch ((opt == STL_MODIFIED_ALT)
3778 + bufIsChanged(wp->w_buffer) * 2
3779 + (!wp->w_buffer->b_p_ma) * 4)
3780 {
3781 case 2: str = (char_u *)"[+]"; break;
3782 case 3: str = (char_u *)",+"; break;
3783 case 4: str = (char_u *)"[-]"; break;
3784 case 5: str = (char_u *)",-"; break;
3785 case 6: str = (char_u *)"[+-]"; break;
3786 case 7: str = (char_u *)",+-"; break;
3787 }
3788 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003789
3790 case STL_HIGHLIGHT:
3791 t = s;
3792 while (*s != '#' && *s != NUL)
3793 ++s;
3794 if (*s == '#')
3795 {
3796 item[curitem].type = Highlight;
3797 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003798 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003799 curitem++;
3800 }
3801 ++s;
3802 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804
3805 item[curitem].start = p;
3806 item[curitem].type = Normal;
3807 if (str != NULL && *str)
3808 {
3809 t = str;
3810 if (itemisflag)
3811 {
3812 if ((t[0] && t[1])
3813 && ((!prevchar_isitem && *t == ',')
3814 || (prevchar_isflag && *t == ' ')))
3815 t++;
3816 prevchar_isflag = TRUE;
3817 }
3818 l = vim_strsize(t);
3819 if (l > 0)
3820 prevchar_isitem = TRUE;
3821 if (l > maxwid)
3822 {
3823 while (l >= maxwid)
3824#ifdef FEAT_MBYTE
3825 if (has_mbyte)
3826 {
3827 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003828 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830 else
3831#endif
3832 l -= byte2cells(*t++);
3833 if (p + 1 >= out + outlen)
3834 break;
3835 *p++ = '<';
3836 }
3837 if (minwid > 0)
3838 {
3839 for (; l < minwid && p + 1 < out + outlen; l++)
3840 {
3841 /* Don't put a "-" in front of a digit. */
3842 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3843 *p++ = ' ';
3844 else
3845 *p++ = fillchar;
3846 }
3847 minwid = 0;
3848 }
3849 else
3850 minwid *= -1;
3851 while (*t && p + 1 < out + outlen)
3852 {
3853 *p++ = *t++;
3854 /* Change a space by fillchar, unless fillchar is '-' and a
3855 * digit follows. */
3856 if (fillable && p[-1] == ' '
3857 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3858 p[-1] = fillchar;
3859 }
3860 for (; l < minwid && p + 1 < out + outlen; l++)
3861 *p++ = fillchar;
3862 }
3863 else if (num >= 0)
3864 {
3865 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3866 char_u nstr[20];
3867
3868 if (p + 20 >= out + outlen)
3869 break; /* not sufficient space */
3870 prevchar_isitem = TRUE;
3871 t = nstr;
3872 if (opt == STL_VIRTCOL_ALT)
3873 {
3874 *t++ = '-';
3875 minwid--;
3876 }
3877 *t++ = '%';
3878 if (zeropad)
3879 *t++ = '0';
3880 *t++ = '*';
3881 *t++ = nbase == 16 ? base : (nbase == 8 ? 'o' : 'd');
3882 *t = 0;
3883
3884 for (n = num, l = 1; n >= nbase; n /= nbase)
3885 l++;
3886 if (opt == STL_VIRTCOL_ALT)
3887 l++;
3888 if (l > maxwid)
3889 {
3890 l += 2;
3891 n = l - maxwid;
3892 while (l-- > maxwid)
3893 num /= nbase;
3894 *t++ = '>';
3895 *t++ = '%';
3896 *t = t[-3];
3897 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003898 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3899 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
3901 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003902 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3903 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 p += STRLEN(p);
3905 }
3906 else
3907 item[curitem].type = Empty;
3908
3909 if (opt == STL_VIM_EXPR)
3910 vim_free(str);
3911
3912 if (num >= 0 || (!itemisflag && str && *str))
3913 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
3914 curitem++;
3915 }
3916 *p = NUL;
3917 itemcnt = curitem;
3918
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003919#ifdef FEAT_EVAL
3920 if (usefmt != fmt)
3921 vim_free(usefmt);
3922#endif
3923
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 width = vim_strsize(out);
3925 if (maxwidth > 0 && width > maxwidth)
3926 {
3927 /* Result is too long, must trunctate somewhere. */
3928 l = 0;
3929 if (itemcnt == 0)
3930 s = out;
3931 else
3932 {
3933 for ( ; l < itemcnt; l++)
3934 if (item[l].type == Trunc)
3935 {
3936 /* Truncate at %< item. */
3937 s = item[l].start;
3938 break;
3939 }
3940 if (l == itemcnt)
3941 {
3942 /* No %< item, truncate first item. */
3943 s = item[0].start;
3944 l = 0;
3945 }
3946 }
3947
3948 if (width - vim_strsize(s) >= maxwidth)
3949 {
3950 /* Truncation mark is beyond max length */
3951#ifdef FEAT_MBYTE
3952 if (has_mbyte)
3953 {
3954 s = out;
3955 width = 0;
3956 for (;;)
3957 {
3958 width += ptr2cells(s);
3959 if (width >= maxwidth)
3960 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003961 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
3963 /* Fill up for half a double-wide character. */
3964 while (++width < maxwidth)
3965 *s++ = fillchar;
3966 }
3967 else
3968#endif
3969 s = out + maxwidth - 1;
3970 for (l = 0; l < itemcnt; l++)
3971 if (item[l].start > s)
3972 break;
3973 itemcnt = l;
3974 *s++ = '>';
3975 *s = 0;
3976 }
3977 else
3978 {
3979#ifdef FEAT_MBYTE
3980 if (has_mbyte)
3981 {
3982 n = 0;
3983 while (width >= maxwidth)
3984 {
3985 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003986 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 }
3988 }
3989 else
3990#endif
3991 n = width - maxwidth + 1;
3992 p = s + n;
3993 mch_memmove(s + 1, p, STRLEN(p) + 1);
3994 *s = '<';
3995
3996 /* Fill up for half a double-wide character. */
3997 while (++width < maxwidth)
3998 {
3999 s = s + STRLEN(s);
4000 *s++ = fillchar;
4001 *s = NUL;
4002 }
4003
4004 --n; /* count the '<' */
4005 for (; l < itemcnt; l++)
4006 {
4007 if (item[l].start - n >= s)
4008 item[l].start -= n;
4009 else
4010 item[l].start = s;
4011 }
4012 }
4013 width = maxwidth;
4014 }
4015 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4016 {
4017 /* Apply STL_MIDDLE if any */
4018 for (l = 0; l < itemcnt; l++)
4019 if (item[l].type == Middle)
4020 break;
4021 if (l < itemcnt)
4022 {
4023 p = item[l].start + maxwidth - width;
4024 mch_memmove(p, item[l].start, STRLEN(item[l].start) + 1);
4025 for (s = item[l].start; s < p; s++)
4026 *s = fillchar;
4027 for (l++; l < itemcnt; l++)
4028 item[l].start += maxwidth - width;
4029 width = maxwidth;
4030 }
4031 }
4032
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004033 /* Store the info about highlighting. */
4034 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004036 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 for (l = 0; l < itemcnt; l++)
4038 {
4039 if (item[l].type == Highlight)
4040 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004041 sp->start = item[l].start;
4042 sp->userhl = item[l].minwid;
4043 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
4045 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004046 sp->start = NULL;
4047 sp->userhl = 0;
4048 }
4049
4050 /* Store the info about tab pages labels. */
4051 if (tabtab != NULL)
4052 {
4053 sp = tabtab;
4054 for (l = 0; l < itemcnt; l++)
4055 {
4056 if (item[l].type == TabPage)
4057 {
4058 sp->start = item[l].start;
4059 sp->userhl = item[l].minwid;
4060 sp++;
4061 }
4062 }
4063 sp->start = NULL;
4064 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
4066
4067 return width;
4068}
4069#endif /* FEAT_STL_OPT */
4070
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004071#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4072 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004074 * Get relative cursor position in window into "str[]", in the form 99%, using
4075 * "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 */
4077 void
4078get_rel_pos(wp, str)
4079 win_T *wp;
4080 char_u *str;
4081{
4082 long above; /* number of lines above window */
4083 long below; /* number of lines below window */
4084
4085 above = wp->w_topline - 1;
4086#ifdef FEAT_DIFF
4087 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4088#endif
4089 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4090 if (below <= 0)
4091 STRCPY(str, above == 0 ? _("All") : _("Bot"));
4092 else if (above <= 0)
4093 STRCPY(str, _("Top"));
4094 else
4095 sprintf((char *)str, "%2d%%", above > 1000000L
4096 ? (int)(above / ((above + below) / 100L))
4097 : (int)(above * 100L / (above + below)));
4098}
4099#endif
4100
4101/*
4102 * Append (file 2 of 8) to 'buf', if editing more than one file.
4103 * Return TRUE if it was appended.
4104 */
4105 int
4106append_arg_number(wp, buf, add_file, maxlen)
4107 win_T *wp;
4108 char_u *buf;
4109 int add_file; /* Add "file" before the arg number */
4110 int maxlen; /* maximum nr of chars in buf or zero*/
4111{
4112 char_u *p;
4113
4114 if (ARGCOUNT <= 1) /* nothing to do */
4115 return FALSE;
4116
4117 p = buf + STRLEN(buf); /* go to the end of the buffer */
4118 if (maxlen && p - buf + 35 >= maxlen) /* getting too long */
4119 return FALSE;
4120 *p++ = ' ';
4121 *p++ = '(';
4122 if (add_file)
4123 {
4124 STRCPY(p, "file ");
4125 p += 5;
4126 }
4127 sprintf((char *)p, wp->w_arg_idx_invalid ? "(%d) of %d)"
4128 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4129 return TRUE;
4130}
4131
4132/*
4133 * If fname is not a full path, make it a full path.
4134 * Returns pointer to allocated memory (NULL for failure).
4135 */
4136 char_u *
4137fix_fname(fname)
4138 char_u *fname;
4139{
4140 /*
4141 * Force expanding the path always for Unix, because symbolic links may
4142 * mess up the full path name, even though it starts with a '/'.
4143 * Also expand when there is ".." in the file name, try to remove it,
4144 * because "c:/src/../README" is equal to "c:/README".
4145 * For MS-Windows also expand names like "longna~1" to "longname".
4146 */
4147#ifdef UNIX
4148 return FullName_save(fname, TRUE);
4149#else
4150 if (!vim_isAbsName(fname) || strstr((char *)fname, "..") != NULL
4151#if defined(MSWIN) || defined(DJGPP)
4152 || vim_strchr(fname, '~') != NULL
4153#endif
4154 )
4155 return FullName_save(fname, FALSE);
4156
4157 fname = vim_strsave(fname);
4158
4159#ifdef USE_FNAME_CASE
4160# ifdef USE_LONG_FNAME
4161 if (USE_LONG_FNAME)
4162# endif
4163 {
4164 if (fname != NULL)
4165 fname_case(fname, 0); /* set correct case for file name */
4166 }
4167#endif
4168
4169 return fname;
4170#endif
4171}
4172
4173/*
4174 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4175 * "ffname" becomes a pointer to allocated memory (or NULL).
4176 */
4177/*ARGSUSED*/
4178 void
4179fname_expand(buf, ffname, sfname)
4180 buf_T *buf;
4181 char_u **ffname;
4182 char_u **sfname;
4183{
4184 if (*ffname == NULL) /* if no file name given, nothing to do */
4185 return;
4186 if (*sfname == NULL) /* if no short file name given, use ffname */
4187 *sfname = *ffname;
4188 *ffname = fix_fname(*ffname); /* expand to full path */
4189
4190#ifdef FEAT_SHORTCUT
4191 if (!buf->b_p_bin)
4192 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004193 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
4195 /* If the file name is a shortcut file, use the file it links to. */
4196 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004197 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 {
4199 vim_free(*ffname);
4200 *ffname = rfname;
4201 *sfname = rfname;
4202 }
4203 }
4204#endif
4205}
4206
4207/*
4208 * Get the file name for an argument list entry.
4209 */
4210 char_u *
4211alist_name(aep)
4212 aentry_T *aep;
4213{
4214 buf_T *bp;
4215
4216 /* Use the name from the associated buffer if it exists. */
4217 bp = buflist_findnr(aep->ae_fnum);
4218 if (bp == NULL)
4219 return aep->ae_fname;
4220 return bp->b_fname;
4221}
4222
4223#if defined(FEAT_WINDOWS) || defined(PROTO)
4224/*
4225 * do_arg_all(): Open up to 'count' windows, one for each argument.
4226 */
4227 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004228do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 int count;
4230 int forceit; /* hide buffers in current windows */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004231 int keep_tabs; /* keep curren tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232{
4233 int i;
4234 win_T *wp, *wpnext;
4235 char_u *opened; /* array of flags for which args are open */
4236 int opened_len; /* lenght of opened[] */
4237 int use_firstwin = FALSE; /* use first window for arglist */
4238 int split_ret = OK;
4239 int p_ea_save;
4240 alist_T *alist; /* argument list to be used */
4241 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004242 tabpage_T *tpnext;
4243 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004244 win_T *new_curwin = NULL;
4245 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246
4247 if (ARGCOUNT <= 0)
4248 {
4249 /* Don't give an error message. We don't want it when the ":all"
4250 * command is in the .vimrc. */
4251 return;
4252 }
4253 setpcmark();
4254
4255 opened_len = ARGCOUNT;
4256 opened = alloc_clear((unsigned)opened_len);
4257 if (opened == NULL)
4258 return;
4259
4260#ifdef FEAT_GUI
4261 need_mouse_correct = TRUE;
4262#endif
4263
4264 /*
4265 * Try closing all windows that are not in the argument list.
4266 * Also close windows that are not full width;
4267 * When 'hidden' or "forceit" set the buffer becomes hidden.
4268 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004269 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004271 if (had_tab > 0)
4272 goto_tabpage_tp(first_tabpage);
4273 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004275 tpnext = curtab->tp_next;
4276 for (wp = firstwin; wp != NULL; wp = wpnext)
4277 {
4278 wpnext = wp->w_next;
4279 buf = wp->w_buffer;
4280 if (buf->b_ffname == NULL
4281 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004283 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004285 )
4286 i = ARGCOUNT;
4287 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004289 /* check if the buffer in this window is in the arglist */
4290 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004292 if (ARGLIST[i].ae_fnum == buf->b_fnum
4293 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004294 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004296 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004297 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004298 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004299 if (i == 0)
4300 {
4301 new_curwin = wp;
4302 new_curtab = curtab;
4303 }
4304 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004305 if (wp->w_alist != curwin->w_alist)
4306 {
4307 /* Use the current argument list for all windows
4308 * containing a file from it. */
4309 alist_unlink(wp->w_alist);
4310 wp->w_alist = curwin->w_alist;
4311 ++wp->w_alist->al_refcount;
4312 }
4313 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
4316 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004317 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004319 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004321 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004322 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004324 /* If the buffer was changed, and we would like to hide it,
4325 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004326 if (!P_HID(buf) && buf->b_nwindows <= 1
4327 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004329 (void)autowrite(buf, FALSE);
4330#ifdef FEAT_AUTOCMD
4331 /* check if autocommands removed the window */
4332 if (!win_valid(wp) || !buf_valid(buf))
4333 {
4334 wpnext = firstwin; /* start all over... */
4335 continue;
4336 }
4337#endif
4338 }
4339#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004340 /* don't close last window */
4341 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004342#endif
4343 use_firstwin = TRUE;
4344#ifdef FEAT_WINDOWS
4345 else
4346 {
4347 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4348# ifdef FEAT_AUTOCMD
4349 /* check if autocommands removed the next window */
4350 if (!win_valid(wpnext))
4351 wpnext = firstwin; /* start all over... */
4352# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354#endif
4355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004358
4359 /* Without the ":tab" modifier only do the current tab page. */
4360 if (had_tab == 0 || tpnext == NULL)
4361 break;
4362
4363# ifdef FEAT_AUTOCMD
4364 /* check if autocommands removed the next tab page */
4365 if (!valid_tabpage(tpnext))
4366 tpnext = first_tabpage; /* start all over...*/
4367# endif
4368 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370
4371 /*
4372 * Open a window for files in the argument list that don't have one.
4373 * ARGCOUNT may change while doing this, because of autocommands.
4374 */
4375 if (count > ARGCOUNT || count <= 0)
4376 count = ARGCOUNT;
4377
4378 /* Autocommands may do anything to the argument list. Make sure it's not
4379 * freed while we are working here by "locking" it. We still have to
4380 * watch out for its size to be changed. */
4381 alist = curwin->w_alist;
4382 ++alist->al_refcount;
4383
4384#ifdef FEAT_AUTOCMD
4385 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4386 ++autocmd_no_enter;
4387 ++autocmd_no_leave;
4388#endif
4389 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004390#ifdef FEAT_WINDOWS
4391 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4392 * leaving an empty tab page when executed locally. */
4393 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4394 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4395 use_firstwin = TRUE;
4396#endif
4397
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4399 {
4400 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4401 arg_had_last = TRUE;
4402 if (i < opened_len && opened[i])
4403 {
4404 /* Move the already present window to below the current window */
4405 if (curwin->w_arg_idx != i)
4406 {
4407 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4408 {
4409 if (wpnext->w_arg_idx == i)
4410 {
4411 win_move_after(wpnext, curwin);
4412 break;
4413 }
4414 }
4415 }
4416 }
4417 else if (split_ret == OK)
4418 {
4419 if (!use_firstwin) /* split current window */
4420 {
4421 p_ea_save = p_ea;
4422 p_ea = TRUE; /* use space from all windows */
4423 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4424 p_ea = p_ea_save;
4425 if (split_ret == FAIL)
4426 continue;
4427 }
4428#ifdef FEAT_AUTOCMD
4429 else /* first window: do autocmd for leaving this buffer */
4430 --autocmd_no_leave;
4431#endif
4432
4433 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004434 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 */
4436 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004437 if (i == 0)
4438 {
4439 new_curwin = curwin;
4440 new_curtab = curtab;
4441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4443 ECMD_ONE,
4444 ((P_HID(curwin->w_buffer)
4445 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4446 + ECMD_OLDBUF);
4447#ifdef FEAT_AUTOCMD
4448 if (use_firstwin)
4449 ++autocmd_no_leave;
4450#endif
4451 use_firstwin = FALSE;
4452 }
4453 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004454
4455 /* When ":tab" was used open a new tab for a new window repeatedly. */
4456 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4457 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 }
4459
4460 /* Remove the "lock" on the argument list. */
4461 alist_unlink(alist);
4462
4463#ifdef FEAT_AUTOCMD
4464 --autocmd_no_enter;
4465#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004466 /* to window with first arg */
4467 if (valid_tabpage(new_curtab))
4468 goto_tabpage_tp(new_curtab);
4469 if (win_valid(new_curwin))
4470 win_enter(new_curwin, FALSE);
4471
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472#ifdef FEAT_AUTOCMD
4473 --autocmd_no_leave;
4474#endif
4475 vim_free(opened);
4476}
4477
4478# if defined(FEAT_LISTCMDS) || defined(PROTO)
4479/*
4480 * Open a window for a number of buffers.
4481 */
4482 void
4483ex_buffer_all(eap)
4484 exarg_T *eap;
4485{
4486 buf_T *buf;
4487 win_T *wp, *wpnext;
4488 int split_ret = OK;
4489 int p_ea_save;
4490 int open_wins = 0;
4491 int r;
4492 int count; /* Maximum number of windows to open. */
4493 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004494#ifdef FEAT_WINDOWS
4495 int had_tab = cmdmod.tab;
4496 tabpage_T *tpnext;
4497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
4499 if (eap->addr_count == 0) /* make as many windows as possible */
4500 count = 9999;
4501 else
4502 count = eap->line2; /* make as many windows as specified */
4503 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4504 all = FALSE;
4505 else
4506 all = TRUE;
4507
4508 setpcmark();
4509
4510#ifdef FEAT_GUI
4511 need_mouse_correct = TRUE;
4512#endif
4513
4514 /*
4515 * Close superfluous windows (two windows for the same buffer).
4516 * Also close windows that are not full-width.
4517 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004518#ifdef FEAT_WINDOWS
4519 if (had_tab > 0)
4520 goto_tabpage_tp(first_tabpage);
4521 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004524 tpnext = curtab->tp_next;
4525 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004527 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004528 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004529#ifdef FEAT_VERTSPLIT
4530 || ((cmdmod.split & WSP_VERT)
4531 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004532 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004533 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004535#ifdef FEAT_WINDOWS
4536 || (had_tab > 0 && wp != firstwin)
4537#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004538 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004539 {
4540 win_close(wp, FALSE);
4541#ifdef FEAT_AUTOCMD
4542 wpnext = firstwin; /* just in case an autocommand does
4543 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004544 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004545 open_wins = 0;
4546#endif
4547 }
4548 else
4549 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004551
4552#ifdef FEAT_WINDOWS
4553 /* Without the ":tab" modifier only do the current tab page. */
4554 if (had_tab == 0 || tpnext == NULL)
4555 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004556 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
4560 /*
4561 * Go through the buffer list. When a buffer doesn't have a window yet,
4562 * open one. Otherwise move the window to the right position.
4563 * Watch out for autocommands that delete buffers or windows!
4564 */
4565#ifdef FEAT_AUTOCMD
4566 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4567 ++autocmd_no_enter;
4568#endif
4569 win_enter(lastwin, FALSE);
4570#ifdef FEAT_AUTOCMD
4571 ++autocmd_no_leave;
4572#endif
4573 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4574 {
4575 /* Check if this buffer needs a window */
4576 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4577 continue;
4578
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004579#ifdef FEAT_WINDOWS
4580 if (had_tab != 0)
4581 {
4582 /* With the ":tab" modifier don't move the window. */
4583 if (buf->b_nwindows > 0)
4584 wp = lastwin; /* buffer has a window, skip it */
4585 else
4586 wp = NULL;
4587 }
4588 else
4589#endif
4590 {
4591 /* Check if this buffer already has a window */
4592 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4593 if (wp->w_buffer == buf)
4594 break;
4595 /* If the buffer already has a window, move it */
4596 if (wp != NULL)
4597 win_move_after(wp, curwin);
4598 }
4599
4600 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 {
4602 /* Split the window and put the buffer in it */
4603 p_ea_save = p_ea;
4604 p_ea = TRUE; /* use space from all windows */
4605 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4606 ++open_wins;
4607 p_ea = p_ea_save;
4608 if (split_ret == FAIL)
4609 continue;
4610
4611 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004612#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 swap_exists_action = SEA_DIALOG;
4614#endif
4615 set_curbuf(buf, DOBUF_GOTO);
4616#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004619#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 break;
4623 }
4624#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004625#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if (swap_exists_action == SEA_QUIT)
4627 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004628# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4629 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004630
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004631 /* Reset the error/interrupt/exception state here so that
4632 * aborting() returns FALSE when closing a window. */
4633 enter_cleanup(&cs);
4634# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004635
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004636 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 win_close(curwin, TRUE);
4638 --open_wins;
4639 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004640 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004641
4642# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4643 /* Restore the error/interrupt/exception state if not
4644 * discarded by a new aborting error, interrupt, or uncaught
4645 * exception. */
4646 leave_cleanup(&cs);
4647# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 }
4649 else
4650 handle_swap_exists(NULL);
4651#endif
4652 }
4653
4654 ui_breakcheck();
4655 if (got_int)
4656 {
4657 (void)vgetc(); /* only break the file loading, not the rest */
4658 break;
4659 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004660#ifdef FEAT_EVAL
4661 /* Autocommands deleted the buffer or aborted script processing!!! */
4662 if (aborting())
4663 break;
4664#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004665#ifdef FEAT_WINDOWS
4666 /* When ":tab" was used open a new tab for a new window repeatedly. */
4667 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4668 cmdmod.tab = 9999;
4669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 }
4671#ifdef FEAT_AUTOCMD
4672 --autocmd_no_enter;
4673#endif
4674 win_enter(firstwin, FALSE); /* back to first window */
4675#ifdef FEAT_AUTOCMD
4676 --autocmd_no_leave;
4677#endif
4678
4679 /*
4680 * Close superfluous windows.
4681 */
4682 for (wp = lastwin; open_wins > count; )
4683 {
4684 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4685 || autowrite(wp->w_buffer, FALSE) == OK);
4686#ifdef FEAT_AUTOCMD
4687 if (!win_valid(wp))
4688 {
4689 /* BufWrite Autocommands made the window invalid, start over */
4690 wp = lastwin;
4691 }
4692 else
4693#endif
4694 if (r)
4695 {
4696 win_close(wp, !P_HID(wp->w_buffer));
4697 --open_wins;
4698 wp = lastwin;
4699 }
4700 else
4701 {
4702 wp = wp->w_prev;
4703 if (wp == NULL)
4704 break;
4705 }
4706 }
4707}
4708# endif /* FEAT_LISTCMDS */
4709
4710#endif /* FEAT_WINDOWS */
4711
Bram Moolenaara3227e22006-03-08 21:32:40 +00004712static int chk_modeline __ARGS((linenr_T, int));
4713
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714/*
4715 * do_modelines() - process mode lines for the current file
4716 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004717 * "flags" can be:
4718 * OPT_WINONLY only set options local to window
4719 * OPT_NOWIN don't set options local to window
4720 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 * Returns immediately if the "ml" option isn't set.
4722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004724do_modelines(flags)
4725 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004727 linenr_T lnum;
4728 int nmlines;
4729 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
4731 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4732 return;
4733
4734 /* Disallow recursive entry here. Can happen when executing a modeline
4735 * triggers an autocommand, which reloads modelines with a ":do". */
4736 if (entered)
4737 return;
4738
4739 ++entered;
4740 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4741 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004742 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 nmlines = 0;
4744
4745 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4746 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004747 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 nmlines = 0;
4749 --entered;
4750}
4751
4752#include "version.h" /* for version number */
4753
4754/*
4755 * chk_modeline() - check a single line for a mode string
4756 * Return FAIL if an error encountered.
4757 */
4758 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004759chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004761 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
4763 char_u *s;
4764 char_u *e;
4765 char_u *linecopy; /* local copy of any modeline found */
4766 int prev;
4767 int vers;
4768 int end;
4769 int retval = OK;
4770 char_u *save_sourcing_name;
4771 linenr_T save_sourcing_lnum;
4772#ifdef FEAT_EVAL
4773 scid_T save_SID;
4774#endif
4775
4776 prev = -1;
4777 for (s = ml_get(lnum); *s != NUL; ++s)
4778 {
4779 if (prev == -1 || vim_isspace(prev))
4780 {
4781 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4782 || STRNCMP(s, "vi:", (size_t)3) == 0)
4783 break;
4784 if (STRNCMP(s, "vim", 3) == 0)
4785 {
4786 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4787 e = s + 4;
4788 else
4789 e = s + 3;
4790 vers = getdigits(&e);
4791 if (*e == ':'
4792 && (s[3] == ':'
4793 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4794 || (VIM_VERSION_100 < vers && s[3] == '<')
4795 || (VIM_VERSION_100 > vers && s[3] == '>')
4796 || (VIM_VERSION_100 == vers && s[3] == '=')))
4797 break;
4798 }
4799 }
4800 prev = *s;
4801 }
4802
4803 if (*s)
4804 {
4805 do /* skip over "ex:", "vi:" or "vim:" */
4806 ++s;
4807 while (s[-1] != ':');
4808
4809 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4810 if (linecopy == NULL)
4811 return FAIL;
4812
4813 save_sourcing_lnum = sourcing_lnum;
4814 save_sourcing_name = sourcing_name;
4815 sourcing_lnum = lnum; /* prepare for emsg() */
4816 sourcing_name = (char_u *)"modelines";
4817
4818 end = FALSE;
4819 while (end == FALSE)
4820 {
4821 s = skipwhite(s);
4822 if (*s == NUL)
4823 break;
4824
4825 /*
4826 * Find end of set command: ':' or end of line.
4827 * Skip over "\:", replacing it with ":".
4828 */
4829 for (e = s; *e != ':' && *e != NUL; ++e)
4830 if (e[0] == '\\' && e[1] == ':')
4831 STRCPY(e, e + 1);
4832 if (*e == NUL)
4833 end = TRUE;
4834
4835 /*
4836 * If there is a "set" command, require a terminating ':' and
4837 * ignore the stuff after the ':'.
4838 * "vi:set opt opt opt: foo" -- foo not interpreted
4839 * "vi:opt opt opt: foo" -- foo interpreted
4840 * Accept "se" for compatibility with Elvis.
4841 */
4842 if (STRNCMP(s, "set ", (size_t)4) == 0
4843 || STRNCMP(s, "se ", (size_t)3) == 0)
4844 {
4845 if (*e != ':') /* no terminating ':'? */
4846 break;
4847 end = TRUE;
4848 s = vim_strchr(s, ' ') + 1;
4849 }
4850 *e = NUL; /* truncate the set command */
4851
4852 if (*s != NUL) /* skip over an empty "::" */
4853 {
4854#ifdef FEAT_EVAL
4855 save_SID = current_SID;
4856 current_SID = SID_MODELINE;
4857#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004858 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859#ifdef FEAT_EVAL
4860 current_SID = save_SID;
4861#endif
4862 if (retval == FAIL) /* stop if error found */
4863 break;
4864 }
4865 s = e + 1; /* advance to next part */
4866 }
4867
4868 sourcing_lnum = save_sourcing_lnum;
4869 sourcing_name = save_sourcing_name;
4870
4871 vim_free(linecopy);
4872 }
4873 return retval;
4874}
4875
4876#ifdef FEAT_VIMINFO
4877 int
4878read_viminfo_bufferlist(virp, writing)
4879 vir_T *virp;
4880 int writing;
4881{
4882 char_u *tab;
4883 linenr_T lnum;
4884 colnr_T col;
4885 buf_T *buf;
4886 char_u *sfname;
4887 char_u *xline;
4888
4889 /* Handle long line and escaped characters. */
4890 xline = viminfo_readstring(virp, 1, FALSE);
4891
4892 /* don't read in if there are files on the command-line or if writing: */
4893 if (xline != NULL && !writing && ARGCOUNT == 0
4894 && find_viminfo_parameter('%') != NULL)
4895 {
4896 /* Format is: <fname> Tab <lnum> Tab <col>.
4897 * Watch out for a Tab in the file name, work from the end. */
4898 lnum = 0;
4899 col = 0;
4900 tab = vim_strrchr(xline, '\t');
4901 if (tab != NULL)
4902 {
4903 *tab++ = '\0';
4904 col = atoi((char *)tab);
4905 tab = vim_strrchr(xline, '\t');
4906 if (tab != NULL)
4907 {
4908 *tab++ = '\0';
4909 lnum = atol((char *)tab);
4910 }
4911 }
4912
4913 /* Expand "~/" in the file name at "line + 1" to a full path.
4914 * Then try shortening it by comparing with the current directory */
4915 expand_env(xline, NameBuff, MAXPATHL);
4916 mch_dirname(IObuff, IOSIZE);
4917 sfname = shorten_fname(NameBuff, IObuff);
4918 if (sfname == NULL)
4919 sfname = NameBuff;
4920
4921 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
4922 if (buf != NULL) /* just in case... */
4923 {
4924 buf->b_last_cursor.lnum = lnum;
4925 buf->b_last_cursor.col = col;
4926 buflist_setfpos(buf, curwin, lnum, col, FALSE);
4927 }
4928 }
4929 vim_free(xline);
4930
4931 return viminfo_readline(virp);
4932}
4933
4934 void
4935write_viminfo_bufferlist(fp)
4936 FILE *fp;
4937{
4938 buf_T *buf;
4939#ifdef FEAT_WINDOWS
4940 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004941 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942#endif
4943 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004944 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945
4946 if (find_viminfo_parameter('%') == NULL)
4947 return;
4948
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004949 /* Without a number -1 is returned: do all buffers. */
4950 max_buffers = get_viminfo_parameter('%');
4951
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004953 line = alloc(MAXPATHL + 40);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (line == NULL)
4955 return;
4956
4957#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00004958 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 set_last_cursor(win);
4960#else
4961 set_last_cursor(curwin);
4962#endif
4963
4964 fprintf(fp, _("\n# Buffer list:\n"));
4965 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
4966 {
4967 if (buf->b_fname == NULL
4968 || !buf->b_p_bl
4969#ifdef FEAT_QUICKFIX
4970 || bt_quickfix(buf)
4971#endif
4972 || removable(buf->b_ffname))
4973 continue;
4974
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004975 if (max_buffers-- == 0)
4976 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 putc('%', fp);
4978 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
4979 sprintf((char *)line + STRLEN(line), "\t%ld\t%d",
4980 (long)buf->b_last_cursor.lnum,
4981 buf->b_last_cursor.col);
4982 viminfo_writestring(fp, line);
4983 }
4984 vim_free(line);
4985}
4986#endif
4987
4988
4989/*
4990 * Return special buffer name.
4991 * Returns NULL when the buffer has a normal file name.
4992 */
4993 char *
4994buf_spname(buf)
4995 buf_T *buf;
4996{
4997#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
4998 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004999 {
5000 win_T *win;
5001
5002 /*
5003 * For location list window, w_llist_ref points to the location list.
5004 * For quickfix window, w_llist_ref is NULL.
5005 */
5006 FOR_ALL_WINDOWS(win)
5007 if (win->w_buffer == buf)
5008 break;
5009 if (win != NULL && win->w_llist_ref != NULL)
5010 return _("[Location List]");
5011 else
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005012 return _("[Quickfix List]");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014#endif
5015#ifdef FEAT_QUICKFIX
5016 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5017 * contains the name as specified by the user */
5018 if (bt_nofile(buf))
5019 {
5020 if (buf->b_sfname != NULL)
5021 return (char *)buf->b_sfname;
5022 return "[Scratch]";
5023 }
5024#endif
5025 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005026 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 return NULL;
5028}
5029
5030
5031#if defined(FEAT_SIGNS) || defined(PROTO)
5032/*
5033 * Insert the sign into the signlist.
5034 */
5035 static void
5036insert_sign(buf, prev, next, id, lnum, typenr)
5037 buf_T *buf; /* buffer to store sign in */
5038 signlist_T *prev; /* previous sign entry */
5039 signlist_T *next; /* next sign entry */
5040 int id; /* sign ID */
5041 linenr_T lnum; /* line number which gets the mark */
5042 int typenr; /* typenr of sign we are adding */
5043{
5044 signlist_T *newsign;
5045
5046 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5047 if (newsign != NULL)
5048 {
5049 newsign->id = id;
5050 newsign->lnum = lnum;
5051 newsign->typenr = typenr;
5052 newsign->next = next;
5053#ifdef FEAT_NETBEANS_INTG
5054 newsign->prev = prev;
5055 if (next != NULL)
5056 next->prev = newsign;
5057#endif
5058
5059 if (prev == NULL)
5060 {
5061 /* When adding first sign need to redraw the windows to create the
5062 * column for signs. */
5063 if (buf->b_signlist == NULL)
5064 {
5065 redraw_buf_later(buf, NOT_VALID);
5066 changed_cline_bef_curs();
5067 }
5068
5069 /* first sign in signlist */
5070 buf->b_signlist = newsign;
5071 }
5072 else
5073 prev->next = newsign;
5074 }
5075}
5076
5077/*
5078 * Add the sign into the signlist. Find the right spot to do it though.
5079 */
5080 void
5081buf_addsign(buf, id, lnum, typenr)
5082 buf_T *buf; /* buffer to store sign in */
5083 int id; /* sign ID */
5084 linenr_T lnum; /* line number which gets the mark */
5085 int typenr; /* typenr of sign we are adding */
5086{
5087 signlist_T *sign; /* a sign in the signlist */
5088 signlist_T *prev; /* the previous sign */
5089
5090 prev = NULL;
5091 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5092 {
5093 if (lnum == sign->lnum && id == sign->id)
5094 {
5095 sign->typenr = typenr;
5096 return;
5097 }
5098 else if (
5099#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5100 id < 0 &&
5101#endif
5102 lnum < sign->lnum)
5103 {
5104#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5105 /* XXX - GRP: Is this because of sign slide problem? Or is it
5106 * really needed? Or is it because we allow multiple signs per
5107 * line? If so, should I add that feature to FEAT_SIGNS?
5108 */
5109 while (prev != NULL && prev->lnum == lnum)
5110 prev = prev->prev;
5111 if (prev == NULL)
5112 sign = buf->b_signlist;
5113 else
5114 sign = prev->next;
5115#endif
5116 insert_sign(buf, prev, sign, id, lnum, typenr);
5117 return;
5118 }
5119 prev = sign;
5120 }
5121#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5122 /* XXX - GRP: See previous comment */
5123 while (prev != NULL && prev->lnum == lnum)
5124 prev = prev->prev;
5125 if (prev == NULL)
5126 sign = buf->b_signlist;
5127 else
5128 sign = prev->next;
5129#endif
5130 insert_sign(buf, prev, sign, id, lnum, typenr);
5131
5132 return;
5133}
5134
5135 int
5136buf_change_sign_type(buf, markId, typenr)
5137 buf_T *buf; /* buffer to store sign in */
5138 int markId; /* sign ID */
5139 int typenr; /* typenr of sign we are adding */
5140{
5141 signlist_T *sign; /* a sign in the signlist */
5142
5143 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5144 {
5145 if (sign->id == markId)
5146 {
5147 sign->typenr = typenr;
5148 return sign->lnum;
5149 }
5150 }
5151
5152 return 0;
5153}
5154
5155 int_u
5156buf_getsigntype(buf, lnum, type)
5157 buf_T *buf;
5158 linenr_T lnum;
5159 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5160{
5161 signlist_T *sign; /* a sign in a b_signlist */
5162
5163 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5164 if (sign->lnum == lnum
5165 && (type == SIGN_ANY
5166# ifdef FEAT_SIGN_ICONS
5167 || (type == SIGN_ICON
5168 && sign_get_image(sign->typenr) != NULL)
5169# endif
5170 || (type == SIGN_TEXT
5171 && sign_get_text(sign->typenr) != NULL)
5172 || (type == SIGN_LINEHL
5173 && sign_get_attr(sign->typenr, TRUE) != 0)))
5174 return sign->typenr;
5175 return 0;
5176}
5177
5178
5179 linenr_T
5180buf_delsign(buf, id)
5181 buf_T *buf; /* buffer sign is stored in */
5182 int id; /* sign id */
5183{
5184 signlist_T **lastp; /* pointer to pointer to current sign */
5185 signlist_T *sign; /* a sign in a b_signlist */
5186 signlist_T *next; /* the next sign in a b_signlist */
5187 linenr_T lnum; /* line number whose sign was deleted */
5188
5189 lastp = &buf->b_signlist;
5190 lnum = 0;
5191 for (sign = buf->b_signlist; sign != NULL; sign = next)
5192 {
5193 next = sign->next;
5194 if (sign->id == id)
5195 {
5196 *lastp = next;
5197#ifdef FEAT_NETBEANS_INTG
5198 if (next != NULL)
5199 next->prev = sign->prev;
5200#endif
5201 lnum = sign->lnum;
5202 vim_free(sign);
5203 break;
5204 }
5205 else
5206 lastp = &sign->next;
5207 }
5208
5209 /* When deleted the last sign need to redraw the windows to remove the
5210 * sign column. */
5211 if (buf->b_signlist == NULL)
5212 {
5213 redraw_buf_later(buf, NOT_VALID);
5214 changed_cline_bef_curs();
5215 }
5216
5217 return lnum;
5218}
5219
5220
5221/*
5222 * Find the line number of the sign with the requested id. If the sign does
5223 * not exist, return 0 as the line number. This will still let the correct file
5224 * get loaded.
5225 */
5226 int
5227buf_findsign(buf, id)
5228 buf_T *buf; /* buffer to store sign in */
5229 int id; /* sign ID */
5230{
5231 signlist_T *sign; /* a sign in the signlist */
5232
5233 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5234 if (sign->id == id)
5235 return sign->lnum;
5236
5237 return 0;
5238}
5239
5240 int
5241buf_findsign_id(buf, lnum)
5242 buf_T *buf; /* buffer whose sign we are searching for */
5243 linenr_T lnum; /* line number of sign */
5244{
5245 signlist_T *sign; /* a sign in the signlist */
5246
5247 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5248 if (sign->lnum == lnum)
5249 return sign->id;
5250
5251 return 0;
5252}
5253
5254
5255# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5256/* see if a given type of sign exists on a specific line */
5257 int
5258buf_findsigntype_id(buf, lnum, typenr)
5259 buf_T *buf; /* buffer whose sign we are searching for */
5260 linenr_T lnum; /* line number of sign */
5261 int typenr; /* sign type number */
5262{
5263 signlist_T *sign; /* a sign in the signlist */
5264
5265 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5266 if (sign->lnum == lnum && sign->typenr == typenr)
5267 return sign->id;
5268
5269 return 0;
5270}
5271
5272
5273# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5274/* return the number of icons on the given line */
5275 int
5276buf_signcount(buf, lnum)
5277 buf_T *buf;
5278 linenr_T lnum;
5279{
5280 signlist_T *sign; /* a sign in the signlist */
5281 int count = 0;
5282
5283 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5284 if (sign->lnum == lnum)
5285 if (sign_get_image(sign->typenr) != NULL)
5286 count++;
5287
5288 return count;
5289}
5290# endif /* FEAT_SIGN_ICONS */
5291# endif /* FEAT_NETBEANS_INTG */
5292
5293
5294/*
5295 * Delete signs in buffer "buf".
5296 */
5297 static void
5298buf_delete_signs(buf)
5299 buf_T *buf;
5300{
5301 signlist_T *next;
5302
5303 while (buf->b_signlist != NULL)
5304 {
5305 next = buf->b_signlist->next;
5306 vim_free(buf->b_signlist);
5307 buf->b_signlist = next;
5308 }
5309}
5310
5311/*
5312 * Delete all signs in all buffers.
5313 */
5314 void
5315buf_delete_all_signs()
5316{
5317 buf_T *buf; /* buffer we are checking for signs */
5318
5319 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5320 if (buf->b_signlist != NULL)
5321 {
5322 /* Need to redraw the windows to remove the sign column. */
5323 redraw_buf_later(buf, NOT_VALID);
5324 buf_delete_signs(buf);
5325 }
5326}
5327
5328/*
5329 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5330 */
5331 void
5332sign_list_placed(rbuf)
5333 buf_T *rbuf;
5334{
5335 buf_T *buf;
5336 signlist_T *p;
5337 char lbuf[BUFSIZ];
5338
5339 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5340 msg_putchar('\n');
5341 if (rbuf == NULL)
5342 buf = firstbuf;
5343 else
5344 buf = rbuf;
5345 while (buf != NULL)
5346 {
5347 if (buf->b_signlist != NULL)
5348 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005349 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5351 msg_putchar('\n');
5352 }
5353 for (p = buf->b_signlist; p != NULL; p = p->next)
5354 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005355 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5357 MSG_PUTS(lbuf);
5358 msg_putchar('\n');
5359 }
5360 if (rbuf != NULL)
5361 break;
5362 buf = buf->b_next;
5363 }
5364}
5365
5366/*
5367 * Adjust a placed sign for inserted/deleted lines.
5368 */
5369 void
5370sign_mark_adjust(line1, line2, amount, amount_after)
5371 linenr_T line1;
5372 linenr_T line2;
5373 long amount;
5374 long amount_after;
5375{
5376 signlist_T *sign; /* a sign in a b_signlist */
5377
5378 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5379 {
5380 if (sign->lnum >= line1 && sign->lnum <= line2)
5381 {
5382 if (amount == MAXLNUM)
5383 sign->lnum = line1;
5384 else
5385 sign->lnum += amount;
5386 }
5387 else if (sign->lnum > line2)
5388 sign->lnum += amount_after;
5389 }
5390}
5391#endif /* FEAT_SIGNS */
5392
5393/*
5394 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5395 */
5396 void
5397set_buflisted(on)
5398 int on;
5399{
5400 if (on != curbuf->b_p_bl)
5401 {
5402 curbuf->b_p_bl = on;
5403#ifdef FEAT_AUTOCMD
5404 if (on)
5405 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5406 else
5407 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5408#endif
5409 }
5410}
5411
5412/*
5413 * Read the file for "buf" again and check if the contents changed.
5414 * Return TRUE if it changed or this could not be checked.
5415 */
5416 int
5417buf_contents_changed(buf)
5418 buf_T *buf;
5419{
5420 buf_T *newbuf;
5421 int differ = TRUE;
5422 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 exarg_T ea;
5425
5426 /* Allocate a buffer without putting it in the buffer list. */
5427 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5428 if (newbuf == NULL)
5429 return TRUE;
5430
5431 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5432 if (prep_exarg(&ea, buf) == FAIL)
5433 {
5434 wipe_buffer(newbuf, FALSE);
5435 return TRUE;
5436 }
5437
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 /* set curwin/curbuf to buf and save a few things */
5439 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
Bram Moolenaar4770d092006-01-12 23:22:24 +00005441 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 && readfile(buf->b_ffname, buf->b_fname,
5443 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5444 &ea, READ_NEW | READ_DUMMY) == OK)
5445 {
5446 /* compare the two files line by line */
5447 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5448 {
5449 differ = FALSE;
5450 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5451 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5452 {
5453 differ = TRUE;
5454 break;
5455 }
5456 }
5457 }
5458 vim_free(ea.cmd);
5459
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 /* restore curwin/curbuf and a few other things */
5461 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
5463 if (curbuf != newbuf) /* safety check */
5464 wipe_buffer(newbuf, FALSE);
5465
5466 return differ;
5467}
5468
5469/*
5470 * Wipe out a buffer and decrement the last buffer number if it was used for
5471 * this buffer. Call this to wipe out a temp buffer that does not contain any
5472 * marks.
5473 */
5474/*ARGSUSED*/
5475 void
5476wipe_buffer(buf, aucmd)
5477 buf_T *buf;
5478 int aucmd; /* When TRUE trigger autocommands. */
5479{
5480 if (buf->b_fnum == top_file_num - 1)
5481 --top_file_num;
5482
5483#ifdef FEAT_AUTOCMD
5484 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5485 ++autocmd_block;
5486#endif
5487 close_buffer(NULL, buf, DOBUF_WIPE);
5488#ifdef FEAT_AUTOCMD
5489 if (!aucmd)
5490 --autocmd_block;
5491#endif
5492}