blob: 5df085769994f898939e885e86c1c9b745e3deee [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 Moolenaar498efdb2006-09-05 14:31:54 +0000437 /* Change directories when the 'acd' option is set. */
438 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439
440 /*
441 * Remove the buffer from the list.
442 */
443 if (wipe_buf)
444 {
445#ifdef FEAT_SUN_WORKSHOP
446 if (usingSunWorkShop)
447 workshop_file_closed_lineno((char *)buf->b_ffname,
448 (int)buf->b_last_cursor.lnum);
449#endif
450 vim_free(buf->b_ffname);
451 vim_free(buf->b_sfname);
452 if (buf->b_prev == NULL)
453 firstbuf = buf->b_next;
454 else
455 buf->b_prev->b_next = buf->b_next;
456 if (buf->b_next == NULL)
457 lastbuf = buf->b_prev;
458 else
459 buf->b_next->b_prev = buf->b_prev;
460 free_buffer(buf);
461 }
462 else
463 {
464 if (del_buf)
465 {
466 /* Free all internal variables and reset option values, to make
467 * ":bdel" compatible with Vim 5.7. */
468 free_buffer_stuff(buf, TRUE);
469
470 /* Make it look like a new buffer. */
471 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
472
473 /* Init the options when loaded again. */
474 buf->b_p_initialized = FALSE;
475 }
476 buf_clear_file(buf);
477 if (del_buf)
478 buf->b_p_bl = FALSE;
479 }
480}
481
482/*
483 * Make buffer not contain a file.
484 */
485 void
486buf_clear_file(buf)
487 buf_T *buf;
488{
489 buf->b_ml.ml_line_count = 1;
490 unchanged(buf, TRUE);
491#ifndef SHORT_FNAME
492 buf->b_shortname = FALSE;
493#endif
494 buf->b_p_eol = TRUE;
495 buf->b_start_eol = TRUE;
496#ifdef FEAT_MBYTE
497 buf->b_p_bomb = FALSE;
498#endif
499 buf->b_ml.ml_mfp = NULL;
500 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
501#ifdef FEAT_NETBEANS_INTG
502 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503#endif
504}
505
506/*
507 * buf_freeall() - free all things allocated for a buffer that are related to
508 * the file.
509 */
510/*ARGSUSED*/
511 void
512buf_freeall(buf, del_buf, wipe_buf)
513 buf_T *buf;
514 int del_buf; /* buffer is going to be deleted */
515 int wipe_buf; /* buffer is going to be wiped out */
516{
517#ifdef FEAT_AUTOCMD
518 int is_curbuf = (buf == curbuf);
519
520 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
521 if (!buf_valid(buf)) /* autocommands may delete the buffer */
522 return;
523 if (del_buf && buf->b_p_bl)
524 {
525 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
526 if (!buf_valid(buf)) /* autocommands may delete the buffer */
527 return;
528 }
529 if (wipe_buf)
530 {
531 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
532 FALSE, buf);
533 if (!buf_valid(buf)) /* autocommands may delete the buffer */
534 return;
535 }
536# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000537 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 return;
539# endif
540
541 /*
542 * It's possible that autocommands change curbuf to the one being deleted.
543 * This might cause curbuf to be deleted unexpectedly. But in some cases
544 * it's OK to delete the curbuf, because a new one is obtained anyway.
545 * Therefore only return if curbuf changed to the deleted buffer.
546 */
547 if (buf == curbuf && !is_curbuf)
548 return;
549#endif
550#ifdef FEAT_DIFF
551 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
552#endif
553#ifdef FEAT_TCL
554 tcl_buffer_free(buf);
555#endif
556 u_blockfree(buf); /* free the memory allocated for undo */
557 ml_close(buf, TRUE); /* close and delete the memline/memfile */
558 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
559 u_clearall(buf); /* reset all undo information */
560#ifdef FEAT_SYN_HL
561 syntax_clear(buf); /* reset syntax info */
562#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000563 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564}
565
566/*
567 * Free a buffer structure and the things it contains related to the buffer
568 * itself (not the file, that must have been done already).
569 */
570 static void
571free_buffer(buf)
572 buf_T *buf;
573{
574 free_buffer_stuff(buf, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000575#ifdef FEAT_MZSCHEME
576 mzscheme_buffer_free(buf);
577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578#ifdef FEAT_PERL
579 perl_buf_free(buf);
580#endif
581#ifdef FEAT_PYTHON
582 python_buffer_free(buf);
583#endif
584#ifdef FEAT_RUBY
585 ruby_buffer_free(buf);
586#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000587#ifdef FEAT_AUTOCMD
588 aubuflocal_remove(buf);
589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 vim_free(buf);
591}
592
593/*
594 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
595 */
596 static void
597free_buffer_stuff(buf, free_options)
598 buf_T *buf;
599 int free_options; /* free options as well */
600{
601 if (free_options)
602 {
603 clear_wininfo(buf); /* including window-local options */
604 free_buf_options(buf, TRUE);
605 }
606#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000607 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
608 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609#endif
610#ifdef FEAT_USR_CMDS
611 uc_clear(&buf->b_ucmds); /* clear local user commands */
612#endif
613#ifdef FEAT_SIGNS
614 buf_delete_signs(buf); /* delete any signs */
615#endif
616#ifdef FEAT_LOCALMAP
617 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
618 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
619#endif
620#ifdef FEAT_MBYTE
621 vim_free(buf->b_start_fenc);
622 buf->b_start_fenc = NULL;
623#endif
624}
625
626/*
627 * Free the b_wininfo list for buffer "buf".
628 */
629 static void
630clear_wininfo(buf)
631 buf_T *buf;
632{
633 wininfo_T *wip;
634
635 while (buf->b_wininfo != NULL)
636 {
637 wip = buf->b_wininfo;
638 buf->b_wininfo = wip->wi_next;
639 if (wip->wi_optset)
640 {
641 clear_winopt(&wip->wi_opt);
642#ifdef FEAT_FOLDING
643 deleteFoldRecurse(&wip->wi_folds);
644#endif
645 }
646 vim_free(wip);
647 }
648}
649
650#if defined(FEAT_LISTCMDS) || defined(PROTO)
651/*
652 * Go to another buffer. Handles the result of the ATTENTION dialog.
653 */
654 void
655goto_buffer(eap, start, dir, count)
656 exarg_T *eap;
657 int start;
658 int dir;
659 int count;
660{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000661# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 buf_T *old_curbuf = curbuf;
663
664 swap_exists_action = SEA_DIALOG;
665# endif
666 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
667 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000668# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
670 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000671# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
672 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000673
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000674 /* Reset the error/interrupt/exception state here so that
675 * aborting() returns FALSE when closing a window. */
676 enter_cleanup(&cs);
677# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000678
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000679 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 win_close(curwin, TRUE);
681 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000682 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000683
684# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
685 /* Restore the error/interrupt/exception state if not discarded by a
686 * new aborting error, interrupt, or uncaught exception. */
687 leave_cleanup(&cs);
688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
690 else
691 handle_swap_exists(old_curbuf);
692# endif
693}
694#endif
695
Bram Moolenaare64ac772005-12-07 20:54:59 +0000696#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697/*
698 * Handle the situation of swap_exists_action being set.
699 * It is allowed for "old_curbuf" to be NULL or invalid.
700 */
701 void
702handle_swap_exists(old_curbuf)
703 buf_T *old_curbuf;
704{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000705# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
706 cleanup_T cs;
707# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000708
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 if (swap_exists_action == SEA_QUIT)
710 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000711# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
712 /* Reset the error/interrupt/exception state here so that
713 * aborting() returns FALSE when closing a buffer. */
714 enter_cleanup(&cs);
715# endif
716
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 /* User selected Quit at ATTENTION prompt. Go back to previous
718 * buffer. If that buffer is gone or the same as the current one,
719 * open a new, empty buffer. */
720 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000721 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
723 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000724 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 if (old_curbuf != NULL)
726 enter_buffer(old_curbuf);
727 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000728
729# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
730 /* Restore the error/interrupt/exception state if not discarded by a
731 * new aborting error, interrupt, or uncaught exception. */
732 leave_cleanup(&cs);
733# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 }
735 else if (swap_exists_action == SEA_RECOVER)
736 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000737# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
738 /* Reset the error/interrupt/exception state here so that
739 * aborting() returns FALSE when closing a buffer. */
740 enter_cleanup(&cs);
741# endif
742
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 /* User selected Recover at ATTENTION prompt. */
744 msg_scroll = TRUE;
745 ml_recover();
746 MSG_PUTS("\n"); /* don't overwrite the last message */
747 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000748 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000749
750# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
751 /* Restore the error/interrupt/exception state if not discarded by a
752 * new aborting error, interrupt, or uncaught exception. */
753 leave_cleanup(&cs);
754# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 }
756 swap_exists_action = SEA_NONE;
757}
758#endif
759
760#if defined(FEAT_LISTCMDS) || defined(PROTO)
761/*
762 * do_bufdel() - delete or unload buffer(s)
763 *
764 * addr_count == 0: ":bdel" - delete current buffer
765 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
766 * buffer "end_bnr", then any other arguments.
767 * addr_count == 2: ":N,N bdel" - delete buffers in range
768 *
769 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
770 * DOBUF_DEL (":bdel")
771 *
772 * Returns error message or NULL
773 */
774 char_u *
775do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
776 int command;
777 char_u *arg; /* pointer to extra arguments */
778 int addr_count;
779 int start_bnr; /* first buffer number in a range */
780 int end_bnr; /* buffer nr or last buffer nr in a range */
781 int forceit;
782{
783 int do_current = 0; /* delete current buffer? */
784 int deleted = 0; /* number of buffers deleted */
785 char_u *errormsg = NULL; /* return value */
786 int bnr; /* buffer number */
787 char_u *p;
788
789#ifdef FEAT_NETBEANS_INTG
790 netbeansCloseFile = 1;
791#endif
792 if (addr_count == 0)
793 {
794 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
795 }
796 else
797 {
798 if (addr_count == 2)
799 {
800 if (*arg) /* both range and argument is not allowed */
801 return (char_u *)_(e_trailing);
802 bnr = start_bnr;
803 }
804 else /* addr_count == 1 */
805 bnr = end_bnr;
806
807 for ( ;!got_int; ui_breakcheck())
808 {
809 /*
810 * delete the current buffer last, otherwise when the
811 * current buffer is deleted, the next buffer becomes
812 * the current one and will be loaded, which may then
813 * also be deleted, etc.
814 */
815 if (bnr == curbuf->b_fnum)
816 do_current = bnr;
817 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
818 forceit) == OK)
819 ++deleted;
820
821 /*
822 * find next buffer number to delete/unload
823 */
824 if (addr_count == 2)
825 {
826 if (++bnr > end_bnr)
827 break;
828 }
829 else /* addr_count == 1 */
830 {
831 arg = skipwhite(arg);
832 if (*arg == NUL)
833 break;
834 if (!VIM_ISDIGIT(*arg))
835 {
836 p = skiptowhite_esc(arg);
837 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
838 if (bnr < 0) /* failed */
839 break;
840 arg = p;
841 }
842 else
843 bnr = getdigits(&arg);
844 }
845 }
846 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
847 FORWARD, do_current, forceit) == OK)
848 ++deleted;
849
850 if (deleted == 0)
851 {
852 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000853 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000855 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000857 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 errormsg = IObuff;
859 }
860 else if (deleted >= p_report)
861 {
862 if (command == DOBUF_UNLOAD)
863 {
864 if (deleted == 1)
865 MSG(_("1 buffer unloaded"));
866 else
867 smsg((char_u *)_("%d buffers unloaded"), deleted);
868 }
869 else if (command == DOBUF_DEL)
870 {
871 if (deleted == 1)
872 MSG(_("1 buffer deleted"));
873 else
874 smsg((char_u *)_("%d buffers deleted"), deleted);
875 }
876 else
877 {
878 if (deleted == 1)
879 MSG(_("1 buffer wiped out"));
880 else
881 smsg((char_u *)_("%d buffers wiped out"), deleted);
882 }
883 }
884 }
885
886#ifdef FEAT_NETBEANS_INTG
887 netbeansCloseFile = 0;
888#endif
889
890 return errormsg;
891}
892
893/*
894 * Implementation of the commands for the buffer list.
895 *
896 * action == DOBUF_GOTO go to specified buffer
897 * action == DOBUF_SPLIT split window and go to specified buffer
898 * action == DOBUF_UNLOAD unload specified buffer(s)
899 * action == DOBUF_DEL delete specified buffer(s) from buffer list
900 * action == DOBUF_WIPE delete specified buffer(s) really
901 *
902 * start == DOBUF_CURRENT go to "count" buffer from current buffer
903 * start == DOBUF_FIRST go to "count" buffer from first buffer
904 * start == DOBUF_LAST go to "count" buffer from last buffer
905 * start == DOBUF_MOD go to "count" modified buffer from current buffer
906 *
907 * Return FAIL or OK.
908 */
909 int
910do_buffer(action, start, dir, count, forceit)
911 int action;
912 int start;
913 int dir; /* FORWARD or BACKWARD */
914 int count; /* buffer number or number of buffers */
915 int forceit; /* TRUE for :...! */
916{
917 buf_T *buf;
918 buf_T *bp;
919 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
920 || action == DOBUF_WIPE);
921
922 switch (start)
923 {
924 case DOBUF_FIRST: buf = firstbuf; break;
925 case DOBUF_LAST: buf = lastbuf; break;
926 default: buf = curbuf; break;
927 }
928 if (start == DOBUF_MOD) /* find next modified buffer */
929 {
930 while (count-- > 0)
931 {
932 do
933 {
934 buf = buf->b_next;
935 if (buf == NULL)
936 buf = firstbuf;
937 }
938 while (buf != curbuf && !bufIsChanged(buf));
939 }
940 if (!bufIsChanged(buf))
941 {
942 EMSG(_("E84: No modified buffer found"));
943 return FAIL;
944 }
945 }
946 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
947 {
948 while (buf != NULL && buf->b_fnum != count)
949 buf = buf->b_next;
950 }
951 else
952 {
953 bp = NULL;
954 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
955 {
956 /* remember the buffer where we start, we come back there when all
957 * buffers are unlisted. */
958 if (bp == NULL)
959 bp = buf;
960 if (dir == FORWARD)
961 {
962 buf = buf->b_next;
963 if (buf == NULL)
964 buf = firstbuf;
965 }
966 else
967 {
968 buf = buf->b_prev;
969 if (buf == NULL)
970 buf = lastbuf;
971 }
972 /* don't count unlisted buffers */
973 if (unload || buf->b_p_bl)
974 {
975 --count;
976 bp = NULL; /* use this buffer as new starting point */
977 }
978 if (bp == buf)
979 {
980 /* back where we started, didn't find anything. */
981 EMSG(_("E85: There is no listed buffer"));
982 return FAIL;
983 }
984 }
985 }
986
987 if (buf == NULL) /* could not find it */
988 {
989 if (start == DOBUF_FIRST)
990 {
991 /* don't warn when deleting */
992 if (!unload)
993 EMSGN(_("E86: Buffer %ld does not exist"), count);
994 }
995 else if (dir == FORWARD)
996 EMSG(_("E87: Cannot go beyond last buffer"));
997 else
998 EMSG(_("E88: Cannot go before first buffer"));
999 return FAIL;
1000 }
1001
1002#ifdef FEAT_GUI
1003 need_mouse_correct = TRUE;
1004#endif
1005
1006#ifdef FEAT_LISTCMDS
1007 /*
1008 * delete buffer buf from memory and/or the list
1009 */
1010 if (unload)
1011 {
1012 int forward;
1013 int retval;
1014
1015 /* When unloading or deleting a buffer that's already unloaded and
1016 * unlisted: fail silently. */
1017 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1018 return FAIL;
1019
1020 if (!forceit && bufIsChanged(buf))
1021 {
1022#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1023 if ((p_confirm || cmdmod.confirm) && p_write)
1024 {
1025 dialog_changed(buf, FALSE);
1026# ifdef FEAT_AUTOCMD
1027 if (!buf_valid(buf))
1028 /* Autocommand deleted buffer, oops! It's not changed
1029 * now. */
1030 return FAIL;
1031# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001032 /* If it's still changed fail silently, the dialog already
1033 * mentioned why it fails. */
1034 if (bufIsChanged(buf))
1035 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001037 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038#endif
1039 {
1040 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1041 buf->b_fnum);
1042 return FAIL;
1043 }
1044 }
1045
1046 /*
1047 * If deleting the last (listed) buffer, make it empty.
1048 * The last (listed) buffer cannot be unloaded.
1049 */
1050 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1051 if (bp->b_p_bl && bp != buf)
1052 break;
1053 if (bp == NULL && buf == curbuf)
1054 {
1055 if (action == DOBUF_UNLOAD)
1056 {
1057 EMSG(_("E90: Cannot unload last buffer"));
1058 return FAIL;
1059 }
1060
1061 /* Close any other windows on this buffer, then make it empty. */
1062#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001063 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064#endif
1065 setpcmark();
1066 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1067 forceit ? ECMD_FORCEIT : 0);
1068
1069 /*
1070 * do_ecmd() may create a new buffer, then we have to delete
1071 * the old one. But do_ecmd() may have done that already, check
1072 * if the buffer still exists.
1073 */
1074 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1075 close_buffer(NULL, buf, action);
1076 return retval;
1077 }
1078
1079#ifdef FEAT_WINDOWS
1080 /*
1081 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001082 * (unless it's the only window). Repeat this so long as we end up in
1083 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001085 while (buf == curbuf
1086 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 win_close(curwin, FALSE);
1088#endif
1089
1090 /*
1091 * If the buffer to be deleted is not the current one, delete it here.
1092 */
1093 if (buf != curbuf)
1094 {
1095#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001096 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097#endif
1098 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1099 close_buffer(NULL, buf, action);
1100 return OK;
1101 }
1102
1103 /*
1104 * Deleting the current buffer: Need to find another buffer to go to.
1105 * There must be another, otherwise it would have been handled above.
1106 * First use au_new_curbuf, if it is valid.
1107 * Then prefer the buffer we most recently visited.
1108 * Else try to find one that is loaded, after the current buffer,
1109 * then before the current buffer.
1110 * Finally use any buffer.
1111 */
1112 buf = NULL; /* selected buffer */
1113 bp = NULL; /* used when no loaded buffer found */
1114#ifdef FEAT_AUTOCMD
1115 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1116 buf = au_new_curbuf;
1117# ifdef FEAT_JUMPLIST
1118 else
1119# endif
1120#endif
1121#ifdef FEAT_JUMPLIST
1122 if (curwin->w_jumplistlen > 0)
1123 {
1124 int jumpidx;
1125
1126 jumpidx = curwin->w_jumplistidx - 1;
1127 if (jumpidx < 0)
1128 jumpidx = curwin->w_jumplistlen - 1;
1129
1130 forward = jumpidx;
1131 while (jumpidx != curwin->w_jumplistidx)
1132 {
1133 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1134 if (buf != NULL)
1135 {
1136 if (buf == curbuf || !buf->b_p_bl)
1137 buf = NULL; /* skip current and unlisted bufs */
1138 else if (buf->b_ml.ml_mfp == NULL)
1139 {
1140 /* skip unloaded buf, but may keep it for later */
1141 if (bp == NULL)
1142 bp = buf;
1143 buf = NULL;
1144 }
1145 }
1146 if (buf != NULL) /* found a valid buffer: stop searching */
1147 break;
1148 /* advance to older entry in jump list */
1149 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1150 break;
1151 if (--jumpidx < 0)
1152 jumpidx = curwin->w_jumplistlen - 1;
1153 if (jumpidx == forward) /* List exhausted for sure */
1154 break;
1155 }
1156 }
1157#endif
1158
1159 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1160 {
1161 forward = TRUE;
1162 buf = curbuf->b_next;
1163 for (;;)
1164 {
1165 if (buf == NULL)
1166 {
1167 if (!forward) /* tried both directions */
1168 break;
1169 buf = curbuf->b_prev;
1170 forward = FALSE;
1171 continue;
1172 }
1173 /* in non-help buffer, try to skip help buffers, and vv */
1174 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1175 {
1176 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1177 break;
1178 if (bp == NULL) /* remember unloaded buf for later */
1179 bp = buf;
1180 }
1181 if (forward)
1182 buf = buf->b_next;
1183 else
1184 buf = buf->b_prev;
1185 }
1186 }
1187 if (buf == NULL) /* No loaded buffer, use unloaded one */
1188 buf = bp;
1189 if (buf == NULL) /* No loaded buffer, find listed one */
1190 {
1191 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1192 if (buf->b_p_bl && buf != curbuf)
1193 break;
1194 }
1195 if (buf == NULL) /* Still no buffer, just take one */
1196 {
1197 if (curbuf->b_next != NULL)
1198 buf = curbuf->b_next;
1199 else
1200 buf = curbuf->b_prev;
1201 }
1202 }
1203
1204 /*
1205 * make buf current buffer
1206 */
1207 if (action == DOBUF_SPLIT) /* split window first */
1208 {
1209# ifdef FEAT_WINDOWS
1210 /* jump to first window containing buf if one exists ("useopen") */
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001211 if (vim_strchr(p_swb, 'o') != NULL && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001212 return OK;
1213 /* jump to first window in any tab page containing buf if one exists
1214 * ("usetab") */
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001215 if (vim_strchr(p_swb, 'a') != NULL && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 return OK;
1217 if (win_split(0, 0) == FAIL)
1218# endif
1219 return FAIL;
1220 }
1221#endif
1222
1223 /* go to current buffer - nothing to do */
1224 if (buf == curbuf)
1225 return OK;
1226
1227 /*
1228 * Check if the current buffer may be abandoned.
1229 */
1230 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1231 {
1232#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1233 if ((p_confirm || cmdmod.confirm) && p_write)
1234 {
1235 dialog_changed(curbuf, FALSE);
1236# ifdef FEAT_AUTOCMD
1237 if (!buf_valid(buf))
1238 /* Autocommand deleted buffer, oops! */
1239 return FAIL;
1240# endif
1241 }
1242 if (bufIsChanged(curbuf))
1243#endif
1244 {
1245 EMSG(_(e_nowrtmsg));
1246 return FAIL;
1247 }
1248 }
1249
1250 /* Go to the other buffer. */
1251 set_curbuf(buf, action);
1252
1253#if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1254 if (action == DOBUF_SPLIT)
1255 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1256#endif
1257
1258#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1259 if (aborting()) /* autocmds may abort script processing */
1260 return FAIL;
1261#endif
1262
1263 return OK;
1264}
1265
1266#endif /* FEAT_LISTCMDS */
1267
1268/*
1269 * Set current buffer to "buf". Executes autocommands and closes current
1270 * buffer. "action" tells how to close the current buffer:
1271 * DOBUF_GOTO free or hide it
1272 * DOBUF_SPLIT nothing
1273 * DOBUF_UNLOAD unload it
1274 * DOBUF_DEL delete it
1275 * DOBUF_WIPE wipe it out
1276 */
1277 void
1278set_curbuf(buf, action)
1279 buf_T *buf;
1280 int action;
1281{
1282 buf_T *prevbuf;
1283 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1284 || action == DOBUF_WIPE);
1285
1286 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001287 if (!cmdmod.keepalt)
1288 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 buflist_altfpos(); /* remember curpos */
1290
1291#ifdef FEAT_VISUAL
1292 /* Don't restart Select mode after switching to another buffer. */
1293 VIsual_reselect = FALSE;
1294#endif
1295
1296 /* close_windows() or apply_autocmds() may change curbuf */
1297 prevbuf = curbuf;
1298
1299#ifdef FEAT_AUTOCMD
1300 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1301# ifdef FEAT_EVAL
1302 if (buf_valid(prevbuf) && !aborting())
1303# else
1304 if (buf_valid(prevbuf))
1305# endif
1306#endif
1307 {
1308#ifdef FEAT_WINDOWS
1309 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001310 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311#endif
1312#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1313 if (buf_valid(prevbuf) && !aborting())
1314#else
1315 if (buf_valid(prevbuf))
1316#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001317 {
1318 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001319 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1321 unload ? action : (action == DOBUF_GOTO
1322 && !P_HID(prevbuf)
1323 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
1326#ifdef FEAT_AUTOCMD
1327# ifdef FEAT_EVAL
1328 /* An autocommand may have deleted buf or aborted the script processing! */
1329 if (buf_valid(buf) && !aborting())
1330# else
1331 if (buf_valid(buf)) /* an autocommand may have deleted buf! */
1332# endif
1333#endif
1334 enter_buffer(buf);
1335}
1336
1337/*
1338 * Enter a new current buffer.
1339 * Old curbuf must have been abandoned already!
1340 */
1341 void
1342enter_buffer(buf)
1343 buf_T *buf;
1344{
1345 /* Copy buffer and window local option values. Not for a help buffer. */
1346 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1347 if (!buf->b_help)
1348 get_winopts(buf);
1349#ifdef FEAT_FOLDING
1350 else
1351 /* Remove all folds in the window. */
1352 clearFolding(curwin);
1353 foldUpdateAll(curwin); /* update folds (later). */
1354#endif
1355
1356 /* Get the buffer in the current window. */
1357 curwin->w_buffer = buf;
1358 curbuf = buf;
1359 ++curbuf->b_nwindows;
1360
1361#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001362 if (curwin->w_p_diff)
1363 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364#endif
1365
1366 /* Cursor on first line by default. */
1367 curwin->w_cursor.lnum = 1;
1368 curwin->w_cursor.col = 0;
1369#ifdef FEAT_VIRTUALEDIT
1370 curwin->w_cursor.coladd = 0;
1371#endif
1372 curwin->w_set_curswant = TRUE;
1373
1374 /* Make sure the buffer is loaded. */
1375 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001376 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001377#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001378 /* If there is no filetype, allow for detecting one. Esp. useful for
1379 * ":ball" used in a autocommand. If there already is a filetype we
1380 * might prefer to keep it. */
1381 if (*curbuf->b_p_ft == NUL)
1382 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001383#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 open_buffer(FALSE, NULL);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 else
1388 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001389 if (!msg_silent)
1390 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1392#ifdef FEAT_AUTOCMD
1393 curwin->w_topline = 1;
1394# ifdef FEAT_DIFF
1395 curwin->w_topfill = 0;
1396# endif
1397 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1398 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1399#endif
1400 }
1401
1402 /* If autocommands did not change the cursor position, restore cursor lnum
1403 * and possibly cursor col. */
1404 if (curwin->w_cursor.lnum == 1 && inindent(0))
1405 buflist_getfpos();
1406
1407 check_arg_idx(curwin); /* check for valid arg_idx */
1408#ifdef FEAT_TITLE
1409 maketitle();
1410#endif
1411#ifdef FEAT_AUTOCMD
1412 if (curwin->w_topline == 1) /* when autocmds didn't change it */
1413#endif
1414 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1415
Bram Moolenaar009b2592004-10-24 19:18:58 +00001416#ifdef FEAT_NETBEANS_INTG
1417 /* Send fileOpened event because we've changed buffers. */
1418 if (usingNetbeans && isNetbeansBuffer(curbuf))
1419 netbeans_file_activated(curbuf);
1420#endif
1421
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001422 /* Change directories when the 'acd' option is set. */
1423 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424
1425#ifdef FEAT_KEYMAP
1426 if (curbuf->b_kmap_state & KEYMAP_INIT)
1427 keymap_init();
1428#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001429#ifdef FEAT_SPELL
1430 /* May need to set the spell language. Can only do this after the buffer
1431 * has been properly setup. */
1432 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
1433 did_set_spelllang(curbuf);
1434#endif
1435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 redraw_later(NOT_VALID);
1437}
1438
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001439#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1440/*
1441 * Change to the directory of the current buffer.
1442 */
1443 void
1444do_autochdir()
1445{
1446 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1447 shorten_fnames(TRUE);
1448}
1449#endif
1450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451/*
1452 * functions for dealing with the buffer list
1453 */
1454
1455/*
1456 * Add a file name to the buffer list. Return a pointer to the buffer.
1457 * If the same file name already exists return a pointer to that buffer.
1458 * If it does not exist, or if fname == NULL, a new entry is created.
1459 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1460 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1461 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 * This is the ONLY way to create a new buffer.
1463 */
1464static int top_file_num = 1; /* highest file number */
1465
1466 buf_T *
1467buflist_new(ffname, sfname, lnum, flags)
1468 char_u *ffname; /* full path of fname or relative */
1469 char_u *sfname; /* short fname or NULL */
1470 linenr_T lnum; /* preferred cursor line */
1471 int flags; /* BLN_ defines */
1472{
1473 buf_T *buf;
1474#ifdef UNIX
1475 struct stat st;
1476#endif
1477
1478 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1479
1480 /*
1481 * If file name already exists in the list, update the entry.
1482 */
1483#ifdef UNIX
1484 /* On Unix we can use inode numbers when the file exists. Works better
1485 * for hard links. */
1486 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1487 st.st_dev = (dev_T)-1;
1488#endif
1489 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1490#ifdef UNIX
1491 buflist_findname_stat(ffname, &st)
1492#else
1493 buflist_findname(ffname)
1494#endif
1495 ) != NULL)
1496 {
1497 vim_free(ffname);
1498 if (lnum != 0)
1499 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1500 /* copy the options now, if 'cpo' doesn't have 's' and not done
1501 * already */
1502 buf_copy_options(buf, 0);
1503 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1504 {
1505 buf->b_p_bl = TRUE;
1506#ifdef FEAT_AUTOCMD
1507 if (!(flags & BLN_DUMMY))
1508 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1509#endif
1510 }
1511 return buf;
1512 }
1513
1514 /*
1515 * If the current buffer has no name and no contents, use the current
1516 * buffer. Otherwise: Need to allocate a new buffer structure.
1517 *
1518 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001519 * (A spell file buffer is allocated in spell.c, but that's not a normal
1520 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 */
1522 buf = NULL;
1523 if ((flags & BLN_CURBUF)
1524 && curbuf != NULL
1525 && curbuf->b_ffname == NULL
1526 && curbuf->b_nwindows <= 1
1527 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1528 {
1529 buf = curbuf;
1530#ifdef FEAT_AUTOCMD
1531 /* It's like this buffer is deleted. Watch out for autocommands that
1532 * change curbuf! If that happens, allocate a new buffer anyway. */
1533 if (curbuf->b_p_bl)
1534 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1535 if (buf == curbuf)
1536 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1537# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001538 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 return NULL;
1540# endif
1541#endif
1542#ifdef FEAT_QUICKFIX
1543# ifdef FEAT_AUTOCMD
1544 if (buf == curbuf)
1545# endif
1546 {
1547 /* Make sure 'bufhidden' and 'buftype' are empty */
1548 clear_string_option(&buf->b_p_bh);
1549 clear_string_option(&buf->b_p_bt);
1550 }
1551#endif
1552 }
1553 if (buf != curbuf || curbuf == NULL)
1554 {
1555 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1556 if (buf == NULL)
1557 {
1558 vim_free(ffname);
1559 return NULL;
1560 }
1561 }
1562
1563 if (ffname != NULL)
1564 {
1565 buf->b_ffname = ffname;
1566 buf->b_sfname = vim_strsave(sfname);
1567 }
1568
1569 clear_wininfo(buf);
1570 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1571
1572 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1573 || buf->b_wininfo == NULL)
1574 {
1575 vim_free(buf->b_ffname);
1576 buf->b_ffname = NULL;
1577 vim_free(buf->b_sfname);
1578 buf->b_sfname = NULL;
1579 if (buf != curbuf)
1580 free_buffer(buf);
1581 return NULL;
1582 }
1583
1584 if (buf == curbuf)
1585 {
1586 /* free all things allocated for this buffer */
1587 buf_freeall(buf, FALSE, FALSE);
1588 if (buf != curbuf) /* autocommands deleted the buffer! */
1589 return NULL;
1590#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001591 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 return NULL;
1593#endif
1594 /* buf->b_nwindows = 0; why was this here? */
1595 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1596#ifdef FEAT_KEYMAP
1597 /* need to reload lmaps and set b:keymap_name */
1598 curbuf->b_kmap_state |= KEYMAP_INIT;
1599#endif
1600 }
1601 else
1602 {
1603 /*
1604 * put new buffer at the end of the buffer list
1605 */
1606 buf->b_next = NULL;
1607 if (firstbuf == NULL) /* buffer list is empty */
1608 {
1609 buf->b_prev = NULL;
1610 firstbuf = buf;
1611 }
1612 else /* append new buffer at end of list */
1613 {
1614 lastbuf->b_next = buf;
1615 buf->b_prev = lastbuf;
1616 }
1617 lastbuf = buf;
1618
1619 buf->b_fnum = top_file_num++;
1620 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1621 {
1622 EMSG(_("W14: Warning: List of file names overflow"));
1623 if (emsg_silent == 0)
1624 {
1625 out_flush();
1626 ui_delay(3000L, TRUE); /* make sure it is noticed */
1627 }
1628 top_file_num = 1;
1629 }
1630
1631 /*
1632 * Always copy the options from the current buffer.
1633 */
1634 buf_copy_options(buf, BCO_ALWAYS);
1635 }
1636
1637 buf->b_wininfo->wi_fpos.lnum = lnum;
1638 buf->b_wininfo->wi_win = curwin;
1639
1640#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001641 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1642#endif
1643#ifdef FEAT_SYN_HL
1644 hash_init(&buf->b_keywtab);
1645 hash_init(&buf->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646#endif
1647
1648 buf->b_fname = buf->b_sfname;
1649#ifdef UNIX
1650 if (st.st_dev == (dev_T)-1)
1651 buf->b_dev = -1;
1652 else
1653 {
1654 buf->b_dev = st.st_dev;
1655 buf->b_ino = st.st_ino;
1656 }
1657#endif
1658 buf->b_u_synced = TRUE;
1659 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001660 if (flags & BLN_DUMMY)
1661 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 buf_clear_file(buf);
1663 clrallmarks(buf); /* clear marks */
1664 fmarks_check_names(buf); /* check file marks for this file */
1665 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1666#ifdef FEAT_AUTOCMD
1667 if (!(flags & BLN_DUMMY))
1668 {
1669 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1670 if (flags & BLN_LISTED)
1671 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1672# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001673 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 return NULL;
1675# endif
1676 }
1677#endif
1678
1679 return buf;
1680}
1681
1682/*
1683 * Free the memory for the options of a buffer.
1684 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1685 * 'fileencoding'.
1686 */
1687 void
1688free_buf_options(buf, free_p_ff)
1689 buf_T *buf;
1690 int free_p_ff;
1691{
1692 if (free_p_ff)
1693 {
1694#ifdef FEAT_MBYTE
1695 clear_string_option(&buf->b_p_fenc);
1696#endif
1697 clear_string_option(&buf->b_p_ff);
1698#ifdef FEAT_QUICKFIX
1699 clear_string_option(&buf->b_p_bh);
1700 clear_string_option(&buf->b_p_bt);
1701#endif
1702 }
1703#ifdef FEAT_FIND_ID
1704 clear_string_option(&buf->b_p_def);
1705 clear_string_option(&buf->b_p_inc);
1706# ifdef FEAT_EVAL
1707 clear_string_option(&buf->b_p_inex);
1708# endif
1709#endif
1710#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1711 clear_string_option(&buf->b_p_inde);
1712 clear_string_option(&buf->b_p_indk);
1713#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001714#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1715 clear_string_option(&buf->b_p_bexpr);
1716#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001717#if defined(FEAT_EVAL)
1718 clear_string_option(&buf->b_p_fex);
1719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720#ifdef FEAT_CRYPT
1721 clear_string_option(&buf->b_p_key);
1722#endif
1723 clear_string_option(&buf->b_p_kp);
1724 clear_string_option(&buf->b_p_mps);
1725 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001726 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 clear_string_option(&buf->b_p_isk);
1728#ifdef FEAT_KEYMAP
1729 clear_string_option(&buf->b_p_keymap);
1730 ga_clear(&buf->b_kmap_ga);
1731#endif
1732#ifdef FEAT_COMMENTS
1733 clear_string_option(&buf->b_p_com);
1734#endif
1735#ifdef FEAT_FOLDING
1736 clear_string_option(&buf->b_p_cms);
1737#endif
1738 clear_string_option(&buf->b_p_nf);
1739#ifdef FEAT_SYN_HL
1740 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001741#endif
1742#ifdef FEAT_SPELL
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001743 clear_string_option(&buf->b_p_spc);
Bram Moolenaar86bc1fb2005-06-07 20:58:01 +00001744 clear_string_option(&buf->b_p_spf);
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001745 vim_free(buf->b_cap_prog);
1746 buf->b_cap_prog = NULL;
Bram Moolenaara0dea672005-03-20 22:23:10 +00001747 clear_string_option(&buf->b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748#endif
1749#ifdef FEAT_SEARCHPATH
1750 clear_string_option(&buf->b_p_sua);
1751#endif
1752#ifdef FEAT_AUTOCMD
1753 clear_string_option(&buf->b_p_ft);
1754#endif
1755#ifdef FEAT_OSFILETYPE
1756 clear_string_option(&buf->b_p_oft);
1757#endif
1758#ifdef FEAT_CINDENT
1759 clear_string_option(&buf->b_p_cink);
1760 clear_string_option(&buf->b_p_cino);
1761#endif
1762#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1763 clear_string_option(&buf->b_p_cinw);
1764#endif
1765#ifdef FEAT_INS_EXPAND
1766 clear_string_option(&buf->b_p_cpt);
1767#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001768#ifdef FEAT_COMPL_FUNC
1769 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001770 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#ifdef FEAT_QUICKFIX
1773 clear_string_option(&buf->b_p_gp);
1774 clear_string_option(&buf->b_p_mp);
1775 clear_string_option(&buf->b_p_efm);
1776#endif
1777 clear_string_option(&buf->b_p_ep);
1778 clear_string_option(&buf->b_p_path);
1779 clear_string_option(&buf->b_p_tags);
1780#ifdef FEAT_INS_EXPAND
1781 clear_string_option(&buf->b_p_dict);
1782 clear_string_option(&buf->b_p_tsr);
1783#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001784#ifdef FEAT_TEXTOBJ
1785 clear_string_option(&buf->b_p_qe);
1786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 buf->b_p_ar = -1;
1788}
1789
1790/*
1791 * get alternate file n
1792 * set linenr to lnum or altfpos.lnum if lnum == 0
1793 * also set cursor column to altfpos.col if 'startofline' is not set.
1794 * if (options & GETF_SETMARK) call setpcmark()
1795 * if (options & GETF_ALT) we are jumping to an alternate file.
1796 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1797 *
1798 * return FAIL for failure, OK for success
1799 */
1800 int
1801buflist_getfile(n, lnum, options, forceit)
1802 int n;
1803 linenr_T lnum;
1804 int options;
1805 int forceit;
1806{
1807 buf_T *buf;
1808#ifdef FEAT_WINDOWS
1809 win_T *wp = NULL;
1810#endif
1811 pos_T *fpos;
1812 colnr_T col;
1813
1814 buf = buflist_findnr(n);
1815 if (buf == NULL)
1816 {
1817 if ((options & GETF_ALT) && n == 0)
1818 EMSG(_(e_noalt));
1819 else
1820 EMSGN(_("E92: Buffer %ld not found"), n);
1821 return FAIL;
1822 }
1823
1824 /* if alternate file is the current buffer, nothing to do */
1825 if (buf == curbuf)
1826 return OK;
1827
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001828 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001829 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001830 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001832 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001833#ifdef FEAT_AUTOCMD
1834 if (curbuf_locked())
1835 return FAIL;
1836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837
1838 /* altfpos may be changed by getfile(), get it now */
1839 if (lnum == 0)
1840 {
1841 fpos = buflist_findfpos(buf);
1842 lnum = fpos->lnum;
1843 col = fpos->col;
1844 }
1845 else
1846 col = 0;
1847
1848#ifdef FEAT_WINDOWS
1849 if (options & GETF_SWITCH)
1850 {
1851 /* use existing open window for buffer if wanted */
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001852 if (vim_strchr(p_swb, 'o') != NULL) /* useopen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001854 /* use existing open window in any tab page for buffer if wanted */
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001855 if (vim_strchr(p_swb, 'a') != NULL) /* usetab */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001856 wp = buf_jump_open_tab(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 /* split window if wanted ("split") */
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001858 if (wp == NULL && vim_strchr(p_swb, 'l') != NULL && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 {
1860 if (win_split(0, 0) == FAIL)
1861 return FAIL;
1862# ifdef FEAT_SCROLLBIND
1863 curwin->w_p_scb = FALSE;
1864# endif
1865 }
1866 }
1867#endif
1868
1869 ++RedrawingDisabled;
1870 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1871 lnum, forceit) <= 0)
1872 {
1873 --RedrawingDisabled;
1874
1875 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1876 if (!p_sol && col != 0)
1877 {
1878 curwin->w_cursor.col = col;
1879 check_cursor_col();
1880#ifdef FEAT_VIRTUALEDIT
1881 curwin->w_cursor.coladd = 0;
1882#endif
1883 curwin->w_set_curswant = TRUE;
1884 }
1885 return OK;
1886 }
1887 --RedrawingDisabled;
1888 return FAIL;
1889}
1890
1891/*
1892 * go to the last know line number for the current buffer
1893 */
1894 void
1895buflist_getfpos()
1896{
1897 pos_T *fpos;
1898
1899 fpos = buflist_findfpos(curbuf);
1900
1901 curwin->w_cursor.lnum = fpos->lnum;
1902 check_cursor_lnum();
1903
1904 if (p_sol)
1905 curwin->w_cursor.col = 0;
1906 else
1907 {
1908 curwin->w_cursor.col = fpos->col;
1909 check_cursor_col();
1910#ifdef FEAT_VIRTUALEDIT
1911 curwin->w_cursor.coladd = 0;
1912#endif
1913 curwin->w_set_curswant = TRUE;
1914 }
1915}
1916
Bram Moolenaar81695252004-12-29 20:58:21 +00001917#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1918/*
1919 * Find file in buffer list by name (it has to be for the current window).
1920 * Returns NULL if not found.
1921 */
1922 buf_T *
1923buflist_findname_exp(fname)
1924 char_u *fname;
1925{
1926 char_u *ffname;
1927 buf_T *buf = NULL;
1928
1929 /* First make the name into a full path name */
1930 ffname = FullName_save(fname,
1931#ifdef UNIX
1932 TRUE /* force expansion, get rid of symbolic links */
1933#else
1934 FALSE
1935#endif
1936 );
1937 if (ffname != NULL)
1938 {
1939 buf = buflist_findname(ffname);
1940 vim_free(ffname);
1941 }
1942 return buf;
1943}
1944#endif
1945
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946/*
1947 * Find file in buffer list by name (it has to be for the current window).
1948 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00001949 * Skips dummy buffers.
1950 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 */
1952 buf_T *
1953buflist_findname(ffname)
1954 char_u *ffname;
1955{
1956#ifdef UNIX
1957 struct stat st;
1958
1959 if (mch_stat((char *)ffname, &st) < 0)
1960 st.st_dev = (dev_T)-1;
1961 return buflist_findname_stat(ffname, &st);
1962}
1963
1964/*
1965 * Same as buflist_findname(), but pass the stat structure to avoid getting it
1966 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00001967 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 */
1969 static buf_T *
1970buflist_findname_stat(ffname, stp)
1971 char_u *ffname;
1972 struct stat *stp;
1973{
1974#endif
1975 buf_T *buf;
1976
1977 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00001978 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979#ifdef UNIX
1980 , stp
1981#endif
1982 ))
1983 return buf;
1984 return NULL;
1985}
1986
1987#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
1988/*
1989 * Find file in buffer list by a regexp pattern.
1990 * Return fnum of the found buffer.
1991 * Return < 0 for error.
1992 */
1993/*ARGSUSED*/
1994 int
1995buflist_findpat(pattern, pattern_end, unlisted, diffmode)
1996 char_u *pattern;
1997 char_u *pattern_end; /* pointer to first char after pattern */
1998 int unlisted; /* find unlisted buffers */
1999 int diffmode; /* find diff-mode buffers only */
2000{
2001 buf_T *buf;
2002 regprog_T *prog;
2003 int match = -1;
2004 int find_listed;
2005 char_u *pat;
2006 char_u *patend;
2007 int attempt;
2008 char_u *p;
2009 int toggledollar;
2010
2011 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2012 {
2013 if (*pattern == '%')
2014 match = curbuf->b_fnum;
2015 else
2016 match = curwin->w_alt_fnum;
2017#ifdef FEAT_DIFF
2018 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2019 match = -1;
2020#endif
2021 }
2022
2023 /*
2024 * Try four ways of matching a listed buffer:
2025 * attempt == 0: without '^' or '$' (at any position)
2026 * attempt == 1: with '^' at start (only at postion 0)
2027 * attempt == 2: with '$' at end (only match at end)
2028 * attempt == 3: with '^' at start and '$' at end (only full match)
2029 * Repeat this for finding an unlisted buffer if there was no matching
2030 * listed buffer.
2031 */
2032 else
2033 {
2034 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2035 if (pat == NULL)
2036 return -1;
2037 patend = pat + STRLEN(pat) - 1;
2038 toggledollar = (patend > pat && *patend == '$');
2039
2040 /* First try finding a listed buffer. If not found and "unlisted"
2041 * is TRUE, try finding an unlisted buffer. */
2042 find_listed = TRUE;
2043 for (;;)
2044 {
2045 for (attempt = 0; attempt <= 3; ++attempt)
2046 {
2047 /* may add '^' and '$' */
2048 if (toggledollar)
2049 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2050 p = pat;
2051 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2052 ++p;
2053 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2054 if (prog == NULL)
2055 {
2056 vim_free(pat);
2057 return -1;
2058 }
2059
2060 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2061 if (buf->b_p_bl == find_listed
2062#ifdef FEAT_DIFF
2063 && (!diffmode || diff_mode_buf(buf))
2064#endif
2065 && buflist_match(prog, buf) != NULL)
2066 {
2067 if (match >= 0) /* already found a match */
2068 {
2069 match = -2;
2070 break;
2071 }
2072 match = buf->b_fnum; /* remember first match */
2073 }
2074
2075 vim_free(prog);
2076 if (match >= 0) /* found one match */
2077 break;
2078 }
2079
2080 /* Only search for unlisted buffers if there was no match with
2081 * a listed buffer. */
2082 if (!unlisted || !find_listed || match != -1)
2083 break;
2084 find_listed = FALSE;
2085 }
2086
2087 vim_free(pat);
2088 }
2089
2090 if (match == -2)
2091 EMSG2(_("E93: More than one match for %s"), pattern);
2092 else if (match < 0)
2093 EMSG2(_("E94: No matching buffer for %s"), pattern);
2094 return match;
2095}
2096#endif
2097
2098#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2099
2100/*
2101 * Find all buffer names that match.
2102 * For command line expansion of ":buf" and ":sbuf".
2103 * Return OK if matches found, FAIL otherwise.
2104 */
2105 int
2106ExpandBufnames(pat, num_file, file, options)
2107 char_u *pat;
2108 int *num_file;
2109 char_u ***file;
2110 int options;
2111{
2112 int count = 0;
2113 buf_T *buf;
2114 int round;
2115 char_u *p;
2116 int attempt;
2117 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002118 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
2120 *num_file = 0; /* return values in case of FAIL */
2121 *file = NULL;
2122
Bram Moolenaar05159a02005-02-26 23:04:13 +00002123 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2124 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002126 patc = alloc((unsigned)STRLEN(pat) + 11);
2127 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002129 STRCPY(patc, "\\(^\\|[\\/]\\)");
2130 STRCPY(patc + 11, pat + 1);
2131 }
2132 else
2133 patc = pat;
2134
2135 /*
2136 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002137 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002138 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002139 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002140 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002141 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002142 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002143 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002144 if (prog == NULL)
2145 {
2146 if (patc != pat)
2147 vim_free(patc);
2148 return FAIL;
2149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
2151 /*
2152 * round == 1: Count the matches.
2153 * round == 2: Build the array to keep the matches.
2154 */
2155 for (round = 1; round <= 2; ++round)
2156 {
2157 count = 0;
2158 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2159 {
2160 if (!buf->b_p_bl) /* skip unlisted buffers */
2161 continue;
2162 p = buflist_match(prog, buf);
2163 if (p != NULL)
2164 {
2165 if (round == 1)
2166 ++count;
2167 else
2168 {
2169 if (options & WILD_HOME_REPLACE)
2170 p = home_replace_save(buf, p);
2171 else
2172 p = vim_strsave(p);
2173 (*file)[count++] = p;
2174 }
2175 }
2176 }
2177 if (count == 0) /* no match found, break here */
2178 break;
2179 if (round == 1)
2180 {
2181 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2182 if (*file == NULL)
2183 {
2184 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002185 if (patc != pat)
2186 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 return FAIL;
2188 }
2189 }
2190 }
2191 vim_free(prog);
2192 if (count) /* match(es) found, break here */
2193 break;
2194 }
2195
Bram Moolenaar05159a02005-02-26 23:04:13 +00002196 if (patc != pat)
2197 vim_free(patc);
2198
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 *num_file = count;
2200 return (count == 0 ? FAIL : OK);
2201}
2202
2203#endif /* FEAT_CMDL_COMPL */
2204
2205#ifdef HAVE_BUFLIST_MATCH
2206/*
2207 * Check for a match on the file name for buffer "buf" with regprog "prog".
2208 */
2209 static char_u *
2210buflist_match(prog, buf)
2211 regprog_T *prog;
2212 buf_T *buf;
2213{
2214 char_u *match;
2215
2216 /* First try the short file name, then the long file name. */
2217 match = fname_match(prog, buf->b_sfname);
2218 if (match == NULL)
2219 match = fname_match(prog, buf->b_ffname);
2220
2221 return match;
2222}
2223
2224/*
2225 * Try matching the regexp in "prog" with file name "name".
2226 * Return "name" when there is a match, NULL when not.
2227 */
2228 static char_u *
2229fname_match(prog, name)
2230 regprog_T *prog;
2231 char_u *name;
2232{
2233 char_u *match = NULL;
2234 char_u *p;
2235 regmatch_T regmatch;
2236
2237 if (name != NULL)
2238 {
2239 regmatch.regprog = prog;
2240#ifdef CASE_INSENSITIVE_FILENAME
2241 regmatch.rm_ic = TRUE; /* Always ignore case */
2242#else
2243 regmatch.rm_ic = FALSE; /* Never ignore case */
2244#endif
2245
2246 if (vim_regexec(&regmatch, name, (colnr_T)0))
2247 match = name;
2248 else
2249 {
2250 /* Replace $(HOME) with '~' and try matching again. */
2251 p = home_replace_save(NULL, name);
2252 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2253 match = name;
2254 vim_free(p);
2255 }
2256 }
2257
2258 return match;
2259}
2260#endif
2261
2262/*
2263 * find file in buffer list by number
2264 */
2265 buf_T *
2266buflist_findnr(nr)
2267 int nr;
2268{
2269 buf_T *buf;
2270
2271 if (nr == 0)
2272 nr = curwin->w_alt_fnum;
2273 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2274 if (buf->b_fnum == nr)
2275 return (buf);
2276 return NULL;
2277}
2278
2279/*
2280 * Get name of file 'n' in the buffer list.
2281 * When the file has no name an empty string is returned.
2282 * home_replace() is used to shorten the file name (used for marks).
2283 * Returns a pointer to allocated memory, of NULL when failed.
2284 */
2285 char_u *
2286buflist_nr2name(n, fullname, helptail)
2287 int n;
2288 int fullname;
2289 int helptail; /* for help buffers return tail only */
2290{
2291 buf_T *buf;
2292
2293 buf = buflist_findnr(n);
2294 if (buf == NULL)
2295 return NULL;
2296 return home_replace_save(helptail ? buf : NULL,
2297 fullname ? buf->b_ffname : buf->b_fname);
2298}
2299
2300/*
2301 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2302 * When "copy_options" is TRUE save the local window option values.
2303 * When "lnum" is 0 only do the options.
2304 */
2305 static void
2306buflist_setfpos(buf, win, lnum, col, copy_options)
2307 buf_T *buf;
2308 win_T *win;
2309 linenr_T lnum;
2310 colnr_T col;
2311 int copy_options;
2312{
2313 wininfo_T *wip;
2314
2315 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2316 if (wip->wi_win == win)
2317 break;
2318 if (wip == NULL)
2319 {
2320 /* allocate a new entry */
2321 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2322 if (wip == NULL)
2323 return;
2324 wip->wi_win = win;
2325 if (lnum == 0) /* set lnum even when it's 0 */
2326 lnum = 1;
2327 }
2328 else
2329 {
2330 /* remove the entry from the list */
2331 if (wip->wi_prev)
2332 wip->wi_prev->wi_next = wip->wi_next;
2333 else
2334 buf->b_wininfo = wip->wi_next;
2335 if (wip->wi_next)
2336 wip->wi_next->wi_prev = wip->wi_prev;
2337 if (copy_options && wip->wi_optset)
2338 {
2339 clear_winopt(&wip->wi_opt);
2340#ifdef FEAT_FOLDING
2341 deleteFoldRecurse(&wip->wi_folds);
2342#endif
2343 }
2344 }
2345 if (lnum != 0)
2346 {
2347 wip->wi_fpos.lnum = lnum;
2348 wip->wi_fpos.col = col;
2349 }
2350 if (copy_options)
2351 {
2352 /* Save the window-specific option values. */
2353 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2354#ifdef FEAT_FOLDING
2355 wip->wi_fold_manual = win->w_fold_manual;
2356 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2357#endif
2358 wip->wi_optset = TRUE;
2359 }
2360
2361 /* insert the entry in front of the list */
2362 wip->wi_next = buf->b_wininfo;
2363 buf->b_wininfo = wip;
2364 wip->wi_prev = NULL;
2365 if (wip->wi_next)
2366 wip->wi_next->wi_prev = wip;
2367
2368 return;
2369}
2370
2371/*
2372 * Find info for the current window in buffer "buf".
2373 * If not found, return the info for the most recently used window.
2374 * Returns NULL when there isn't any info.
2375 */
2376 static wininfo_T *
2377find_wininfo(buf)
2378 buf_T *buf;
2379{
2380 wininfo_T *wip;
2381
2382 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2383 if (wip->wi_win == curwin)
2384 break;
2385 if (wip == NULL) /* if no fpos for curwin, use the first in the list */
2386 wip = buf->b_wininfo;
2387 return wip;
2388}
2389
2390/*
2391 * Reset the local window options to the values last used in this window.
2392 * If the buffer wasn't used in this window before, use the values from
2393 * the most recently used window. If the values were never set, use the
2394 * global values for the window.
2395 */
2396 void
2397get_winopts(buf)
2398 buf_T *buf;
2399{
2400 wininfo_T *wip;
2401
2402 clear_winopt(&curwin->w_onebuf_opt);
2403#ifdef FEAT_FOLDING
2404 clearFolding(curwin);
2405#endif
2406
2407 wip = find_wininfo(buf);
2408 if (wip != NULL && wip->wi_optset)
2409 {
2410 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2411#ifdef FEAT_FOLDING
2412 curwin->w_fold_manual = wip->wi_fold_manual;
2413 curwin->w_foldinvalid = TRUE;
2414 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2415#endif
2416 }
2417 else
2418 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2419
2420#ifdef FEAT_FOLDING
2421 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2422 if (p_fdls >= 0)
2423 curwin->w_p_fdl = p_fdls;
2424#endif
2425}
2426
2427/*
2428 * Find the position (lnum and col) for the buffer 'buf' for the current
2429 * window.
2430 * Returns a pointer to no_position if no position is found.
2431 */
2432 pos_T *
2433buflist_findfpos(buf)
2434 buf_T *buf;
2435{
2436 wininfo_T *wip;
2437 static pos_T no_position = {1, 0};
2438
2439 wip = find_wininfo(buf);
2440 if (wip != NULL)
2441 return &(wip->wi_fpos);
2442 else
2443 return &no_position;
2444}
2445
2446/*
2447 * Find the lnum for the buffer 'buf' for the current window.
2448 */
2449 linenr_T
2450buflist_findlnum(buf)
2451 buf_T *buf;
2452{
2453 return buflist_findfpos(buf)->lnum;
2454}
2455
2456#if defined(FEAT_LISTCMDS) || defined(PROTO)
2457/*
2458 * List all know file names (for :files and :buffers command).
2459 */
2460/*ARGSUSED*/
2461 void
2462buflist_list(eap)
2463 exarg_T *eap;
2464{
2465 buf_T *buf;
2466 int len;
2467 int i;
2468
2469 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2470 {
2471 /* skip unlisted buffers, unless ! was used */
2472 if (!buf->b_p_bl && !eap->forceit)
2473 continue;
2474 msg_putchar('\n');
2475 if (buf_spname(buf) != NULL)
2476 STRCPY(NameBuff, buf_spname(buf));
2477 else
2478 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2479
Bram Moolenaar50cde822005-06-05 21:54:54 +00002480 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 buf->b_fnum,
2482 buf->b_p_bl ? ' ' : 'u',
2483 buf == curbuf ? '%' :
2484 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2485 buf->b_ml.ml_mfp == NULL ? ' ' :
2486 (buf->b_nwindows == 0 ? 'h' : 'a'),
2487 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2488 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002489 : (bufIsChanged(buf) ? '+' : ' '),
2490 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
2492 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 i = 40 - vim_strsize(IObuff);
2494 do
2495 {
2496 IObuff[len++] = ' ';
2497 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002498 vim_snprintf((char *)IObuff + len, IOSIZE - len, _("line %ld"),
2499 buf == curbuf ? curwin->w_cursor.lnum
2500 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 msg_outtrans(IObuff);
2502 out_flush(); /* output one line at a time */
2503 ui_breakcheck();
2504 }
2505}
2506#endif
2507
2508/*
2509 * Get file name and line number for file 'fnum'.
2510 * Used by DoOneCmd() for translating '%' and '#'.
2511 * Used by insert_reg() and cmdline_paste() for '#' register.
2512 * Return FAIL if not found, OK for success.
2513 */
2514 int
2515buflist_name_nr(fnum, fname, lnum)
2516 int fnum;
2517 char_u **fname;
2518 linenr_T *lnum;
2519{
2520 buf_T *buf;
2521
2522 buf = buflist_findnr(fnum);
2523 if (buf == NULL || buf->b_fname == NULL)
2524 return FAIL;
2525
2526 *fname = buf->b_fname;
2527 *lnum = buflist_findlnum(buf);
2528
2529 return OK;
2530}
2531
2532/*
2533 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2534 * The file name with the full path is also remembered, for when :cd is used.
2535 * Returns FAIL for failure (file name already in use by other buffer)
2536 * OK otherwise.
2537 */
2538 int
2539setfname(buf, ffname, sfname, message)
2540 buf_T *buf;
2541 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002542 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543{
Bram Moolenaar81695252004-12-29 20:58:21 +00002544 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545#ifdef UNIX
2546 struct stat st;
2547#endif
2548
2549 if (ffname == NULL || *ffname == NUL)
2550 {
2551 /* Removing the name. */
2552 vim_free(buf->b_ffname);
2553 vim_free(buf->b_sfname);
2554 buf->b_ffname = NULL;
2555 buf->b_sfname = NULL;
2556#ifdef UNIX
2557 st.st_dev = (dev_T)-1;
2558#endif
2559 }
2560 else
2561 {
2562 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2563 if (ffname == NULL) /* out of memory */
2564 return FAIL;
2565
2566 /*
2567 * if the file name is already used in another buffer:
2568 * - if the buffer is loaded, fail
2569 * - if the buffer is not loaded, delete it from the list
2570 */
2571#ifdef UNIX
2572 if (mch_stat((char *)ffname, &st) < 0)
2573 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002574#endif
2575 if (!(buf->b_flags & BF_DUMMY))
2576#ifdef UNIX
2577 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002579 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580#endif
2581 if (obuf != NULL && obuf != buf)
2582 {
2583 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2584 {
2585 if (message)
2586 EMSG(_("E95: Buffer with this name already exists"));
2587 vim_free(ffname);
2588 return FAIL;
2589 }
2590 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2591 }
2592 sfname = vim_strsave(sfname);
2593 if (ffname == NULL || sfname == NULL)
2594 {
2595 vim_free(sfname);
2596 vim_free(ffname);
2597 return FAIL;
2598 }
2599#ifdef USE_FNAME_CASE
2600# ifdef USE_LONG_FNAME
2601 if (USE_LONG_FNAME)
2602# endif
2603 fname_case(sfname, 0); /* set correct case for short file name */
2604#endif
2605 vim_free(buf->b_ffname);
2606 vim_free(buf->b_sfname);
2607 buf->b_ffname = ffname;
2608 buf->b_sfname = sfname;
2609 }
2610 buf->b_fname = buf->b_sfname;
2611#ifdef UNIX
2612 if (st.st_dev == (dev_T)-1)
2613 buf->b_dev = -1;
2614 else
2615 {
2616 buf->b_dev = st.st_dev;
2617 buf->b_ino = st.st_ino;
2618 }
2619#endif
2620
2621#ifndef SHORT_FNAME
2622 buf->b_shortname = FALSE;
2623#endif
2624
2625 buf_name_changed(buf);
2626 return OK;
2627}
2628
2629/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002630 * Crude way of changing the name of a buffer. Use with care!
2631 * The name should be relative to the current directory.
2632 */
2633 void
2634buf_set_name(fnum, name)
2635 int fnum;
2636 char_u *name;
2637{
2638 buf_T *buf;
2639
2640 buf = buflist_findnr(fnum);
2641 if (buf != NULL)
2642 {
2643 vim_free(buf->b_sfname);
2644 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002645 buf->b_ffname = vim_strsave(name);
2646 buf->b_sfname = NULL;
2647 /* Allocate ffname and expand into full path. Also resolves .lnk
2648 * files on Win32. */
2649 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002650 buf->b_fname = buf->b_sfname;
2651 }
2652}
2653
2654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 * Take care of what needs to be done when the name of buffer "buf" has
2656 * changed.
2657 */
2658 void
2659buf_name_changed(buf)
2660 buf_T *buf;
2661{
2662 /*
2663 * If the file name changed, also change the name of the swapfile
2664 */
2665 if (buf->b_ml.ml_mfp != NULL)
2666 ml_setname(buf);
2667
2668 if (curwin->w_buffer == buf)
2669 check_arg_idx(curwin); /* check file name for arg list */
2670#ifdef FEAT_TITLE
2671 maketitle(); /* set window title */
2672#endif
2673#ifdef FEAT_WINDOWS
2674 status_redraw_all(); /* status lines need to be redrawn */
2675#endif
2676 fmarks_check_names(buf); /* check named file marks */
2677 ml_timestamp(buf); /* reset timestamp */
2678}
2679
2680/*
2681 * set alternate file name for current window
2682 *
2683 * Used by do_one_cmd(), do_write() and do_ecmd().
2684 * Return the buffer.
2685 */
2686 buf_T *
2687setaltfname(ffname, sfname, lnum)
2688 char_u *ffname;
2689 char_u *sfname;
2690 linenr_T lnum;
2691{
2692 buf_T *buf;
2693
2694 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2695 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002696 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 curwin->w_alt_fnum = buf->b_fnum;
2698 return buf;
2699}
2700
2701/*
2702 * Get alternate file name for current window.
2703 * Return NULL if there isn't any, and give error message if requested.
2704 */
2705 char_u *
2706getaltfname(errmsg)
2707 int errmsg; /* give error message */
2708{
2709 char_u *fname;
2710 linenr_T dummy;
2711
2712 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2713 {
2714 if (errmsg)
2715 EMSG(_(e_noalt));
2716 return NULL;
2717 }
2718 return fname;
2719}
2720
2721/*
2722 * Add a file name to the buflist and return its number.
2723 * Uses same flags as buflist_new(), except BLN_DUMMY.
2724 *
2725 * used by qf_init(), main() and doarglist()
2726 */
2727 int
2728buflist_add(fname, flags)
2729 char_u *fname;
2730 int flags;
2731{
2732 buf_T *buf;
2733
2734 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2735 if (buf != NULL)
2736 return buf->b_fnum;
2737 return 0;
2738}
2739
2740#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2741/*
2742 * Adjust slashes in file names. Called after 'shellslash' was set.
2743 */
2744 void
2745buflist_slash_adjust()
2746{
2747 buf_T *bp;
2748
2749 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2750 {
2751 if (bp->b_ffname != NULL)
2752 slash_adjust(bp->b_ffname);
2753 if (bp->b_sfname != NULL)
2754 slash_adjust(bp->b_sfname);
2755 }
2756}
2757#endif
2758
2759/*
2760 * Set alternate cursor position for current window.
2761 * Also save the local window option values.
2762 */
2763 void
2764buflist_altfpos()
2765{
2766 buflist_setfpos(curbuf, curwin, curwin->w_cursor.lnum,
2767 curwin->w_cursor.col, TRUE);
2768}
2769
2770/*
2771 * Return TRUE if 'ffname' is not the same file as current file.
2772 * Fname must have a full path (expanded by mch_FullName()).
2773 */
2774 int
2775otherfile(ffname)
2776 char_u *ffname;
2777{
2778 return otherfile_buf(curbuf, ffname
2779#ifdef UNIX
2780 , NULL
2781#endif
2782 );
2783}
2784
2785 static int
2786otherfile_buf(buf, ffname
2787#ifdef UNIX
2788 , stp
2789#endif
2790 )
2791 buf_T *buf;
2792 char_u *ffname;
2793#ifdef UNIX
2794 struct stat *stp;
2795#endif
2796{
2797 /* no name is different */
2798 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2799 return TRUE;
2800 if (fnamecmp(ffname, buf->b_ffname) == 0)
2801 return FALSE;
2802#ifdef UNIX
2803 {
2804 struct stat st;
2805
2806 /* If no struct stat given, get it now */
2807 if (stp == NULL)
2808 {
2809 if (buf->b_dev < 0 || mch_stat((char *)ffname, &st) < 0)
2810 st.st_dev = (dev_T)-1;
2811 stp = &st;
2812 }
2813 /* Use dev/ino to check if the files are the same, even when the names
2814 * are different (possible with links). Still need to compare the
2815 * name above, for when the file doesn't exist yet.
2816 * Problem: The dev/ino changes when a file is deleted (and created
2817 * again) and remains the same when renamed/moved. We don't want to
2818 * mch_stat() each buffer each time, that would be too slow. Get the
2819 * dev/ino again when they appear to match, but not when they appear
2820 * to be different: Could skip a buffer when it's actually the same
2821 * file. */
2822 if (buf_same_ino(buf, stp))
2823 {
2824 buf_setino(buf);
2825 if (buf_same_ino(buf, stp))
2826 return FALSE;
2827 }
2828 }
2829#endif
2830 return TRUE;
2831}
2832
2833#if defined(UNIX) || defined(PROTO)
2834/*
2835 * Set inode and device number for a buffer.
2836 * Must always be called when b_fname is changed!.
2837 */
2838 void
2839buf_setino(buf)
2840 buf_T *buf;
2841{
2842 struct stat st;
2843
2844 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2845 {
2846 buf->b_dev = st.st_dev;
2847 buf->b_ino = st.st_ino;
2848 }
2849 else
2850 buf->b_dev = -1;
2851}
2852
2853/*
2854 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2855 */
2856 static int
2857buf_same_ino(buf, stp)
2858 buf_T *buf;
2859 struct stat *stp;
2860{
2861 return (buf->b_dev >= 0
2862 && stp->st_dev == buf->b_dev
2863 && stp->st_ino == buf->b_ino);
2864}
2865#endif
2866
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002867/*
2868 * Print info about the current buffer.
2869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 void
2871fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002872 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 int shorthelp;
2874 int dont_truncate;
2875{
2876 char_u *name;
2877 int n;
2878 char_u *p;
2879 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002880 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
2882 buffer = alloc(IOSIZE);
2883 if (buffer == NULL)
2884 return;
2885
2886 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2887 {
2888 sprintf((char *)buffer, "buf %d: ", curbuf->b_fnum);
2889 p = buffer + STRLEN(buffer);
2890 }
2891 else
2892 p = buffer;
2893
2894 *p++ = '"';
2895 if (buf_spname(curbuf) != NULL)
2896 STRCPY(p, buf_spname(curbuf));
2897 else
2898 {
2899 if (!fullname && curbuf->b_fname != NULL)
2900 name = curbuf->b_fname;
2901 else
2902 name = curbuf->b_ffname;
2903 home_replace(shorthelp ? curbuf : NULL, name, p,
2904 (int)(IOSIZE - (p - buffer)), TRUE);
2905 }
2906
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002907 len = STRLEN(buffer);
2908 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 "\"%s%s%s%s%s%s",
2910 curbufIsChanged() ? (shortmess(SHM_MOD)
2911 ? " [+]" : _(" [Modified]")) : " ",
2912 (curbuf->b_flags & BF_NOTEDITED)
2913#ifdef FEAT_QUICKFIX
2914 && !bt_dontwrite(curbuf)
2915#endif
2916 ? _("[Not edited]") : "",
2917 (curbuf->b_flags & BF_NEW)
2918#ifdef FEAT_QUICKFIX
2919 && !bt_dontwrite(curbuf)
2920#endif
2921 ? _("[New file]") : "",
2922 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
2923 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
2924 : _("[readonly]")) : "",
2925 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
2926 || curbuf->b_p_ro) ?
2927 " " : "");
2928 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
2929 * causes an overflow, thus for large numbers divide instead. */
2930 if (curwin->w_cursor.lnum > 1000000L)
2931 n = (int)(((long)curwin->w_cursor.lnum) /
2932 ((long)curbuf->b_ml.ml_line_count / 100L));
2933 else
2934 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
2935 (long)curbuf->b_ml.ml_line_count);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002936 len = STRLEN(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2938 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002939 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 }
2941#ifdef FEAT_CMDL_INFO
2942 else if (p_ru)
2943 {
2944 /* Current line and column are already on the screen -- webb */
2945 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002946 vim_snprintf((char *)buffer + len, IOSIZE - len,
2947 _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002949 vim_snprintf((char *)buffer + len, IOSIZE - len,
2950 _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 (long)curbuf->b_ml.ml_line_count, n);
2952 }
2953#endif
2954 else
2955 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002956 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 _("line %ld of %ld --%d%%-- col "),
2958 (long)curwin->w_cursor.lnum,
2959 (long)curbuf->b_ml.ml_line_count,
2960 n);
2961 validate_virtcol();
2962 col_print(buffer + STRLEN(buffer),
2963 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
2964 }
2965
2966 (void)append_arg_number(curwin, buffer, !shortmess(SHM_FILE), IOSIZE);
2967
2968 if (dont_truncate)
2969 {
2970 /* Temporarily set msg_scroll to avoid the message being truncated.
2971 * First call msg_start() to get the message in the right place. */
2972 msg_start();
2973 n = msg_scroll;
2974 msg_scroll = TRUE;
2975 msg(buffer);
2976 msg_scroll = n;
2977 }
2978 else
2979 {
2980 p = msg_trunc_attr(buffer, FALSE, 0);
2981 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 /* Need to repeat the message after redrawing when:
2983 * - When restart_edit is set (otherwise there will be a delay
2984 * before redrawing).
2985 * - When the screen was scrolled but there is no wait-return
2986 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00002987 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 }
2989
2990 vim_free(buffer);
2991}
2992
2993 void
2994col_print(buf, col, vcol)
2995 char_u *buf;
2996 int col;
2997 int vcol;
2998{
2999 if (col == vcol)
3000 sprintf((char *)buf, "%d", col);
3001 else
3002 sprintf((char *)buf, "%d-%d", col, vcol);
3003}
3004
3005#if defined(FEAT_TITLE) || defined(PROTO)
3006/*
3007 * put file name in title bar of window and in icon title
3008 */
3009
3010static char_u *lasttitle = NULL;
3011static char_u *lasticon = NULL;
3012
3013 void
3014maketitle()
3015{
3016 char_u *p;
3017 char_u *t_str = NULL;
3018 char_u *i_name;
3019 char_u *i_str = NULL;
3020 int maxlen = 0;
3021 int len;
3022 int mustset;
3023 char_u buf[IOSIZE];
3024 int off;
3025
3026 if (!redrawing())
3027 {
3028 /* Postpone updating the title when 'lazyredraw' is set. */
3029 need_maketitle = TRUE;
3030 return;
3031 }
3032
3033 need_maketitle = FALSE;
3034 if (!p_title && !p_icon)
3035 return;
3036
3037 if (p_title)
3038 {
3039 if (p_titlelen > 0)
3040 {
3041 maxlen = p_titlelen * Columns / 100;
3042 if (maxlen < 10)
3043 maxlen = 10;
3044 }
3045
3046 t_str = buf;
3047 if (*p_titlestring != NUL)
3048 {
3049#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003050 if (stl_syntax & STL_IN_TITLE)
3051 {
3052 int use_sandbox = FALSE;
3053 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003054
3055# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003056 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003057# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003058 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003060 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003061 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003062 if (called_emsg)
3063 set_string_option_direct((char_u *)"titlestring", -1,
3064 (char_u *)"", OPT_FREE, SID_ERROR);
3065 called_emsg |= save_called_emsg;
3066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 else
3068#endif
3069 t_str = p_titlestring;
3070 }
3071 else
3072 {
3073 /* format: "fname + (path) (1 of 2) - VIM" */
3074
3075 if (curbuf->b_fname == NULL)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003076 STRCPY(buf, _("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 else
3078 {
3079 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003080 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 }
3083
3084 switch (bufIsChanged(curbuf)
3085 + (curbuf->b_p_ro * 2)
3086 + (!curbuf->b_p_ma * 4))
3087 {
3088 case 1: STRCAT(buf, " +"); break;
3089 case 2: STRCAT(buf, " ="); break;
3090 case 3: STRCAT(buf, " =+"); break;
3091 case 4:
3092 case 6: STRCAT(buf, " -"); break;
3093 case 5:
3094 case 7: STRCAT(buf, " -+"); break;
3095 }
3096
3097 if (curbuf->b_fname != NULL)
3098 {
3099 /* Get path of file, replace home dir with ~ */
3100 off = (int)STRLEN(buf);
3101 buf[off++] = ' ';
3102 buf[off++] = '(';
3103 home_replace(curbuf, curbuf->b_ffname,
3104 buf + off, IOSIZE - off, TRUE);
3105#ifdef BACKSLASH_IN_FILENAME
3106 /* avoid "c:/name" to be reduced to "c" */
3107 if (isalpha(buf[off]) && buf[off + 1] == ':')
3108 off += 2;
3109#endif
3110 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003111 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003114 vim_strncpy(buf + off, (char_u *)_("help"),
3115 IOSIZE - off - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003118
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 /* translate unprintable chars */
3120 p = transstr(buf + off);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003121 vim_strncpy(buf + off, p, IOSIZE - off - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 STRCAT(buf, ")");
3124 }
3125
3126 append_arg_number(curwin, buf, FALSE, IOSIZE);
3127
3128#if defined(FEAT_CLIENTSERVER)
3129 if (serverName != NULL)
3130 {
3131 STRCAT(buf, " - ");
3132 STRCAT(buf, serverName);
3133 }
3134 else
3135#endif
3136 STRCAT(buf, " - VIM");
3137
3138 if (maxlen > 0)
3139 {
3140 /* make it shorter by removing a bit in the middle */
3141 len = vim_strsize(buf);
3142 if (len > maxlen)
3143 trunc_string(buf, buf, maxlen);
3144 }
3145 }
3146 }
3147 mustset = ti_change(t_str, &lasttitle);
3148
3149 if (p_icon)
3150 {
3151 i_str = buf;
3152 if (*p_iconstring != NUL)
3153 {
3154#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003155 if (stl_syntax & STL_IN_ICON)
3156 {
3157 int use_sandbox = FALSE;
3158 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003159
3160# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003161 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003162# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003163 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003165 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003166 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003167 if (called_emsg)
3168 set_string_option_direct((char_u *)"iconstring", -1,
3169 (char_u *)"", OPT_FREE, SID_ERROR);
3170 called_emsg |= save_called_emsg;
3171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 else
3173#endif
3174 i_str = p_iconstring;
3175 }
3176 else
3177 {
3178 if (buf_spname(curbuf) != NULL)
3179 i_name = (char_u *)buf_spname(curbuf);
3180 else /* use file name only in icon */
3181 i_name = gettail(curbuf->b_ffname);
3182 *i_str = NUL;
3183 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003184 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 if (len > 100)
3186 {
3187 len -= 100;
3188#ifdef FEAT_MBYTE
3189 if (has_mbyte)
3190 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3191#endif
3192 i_name += len;
3193 }
3194 STRCPY(i_str, i_name);
3195 trans_characters(i_str, IOSIZE);
3196 }
3197 }
3198
3199 mustset |= ti_change(i_str, &lasticon);
3200
3201 if (mustset)
3202 resettitle();
3203}
3204
3205/*
3206 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3207 * from "str" if it does.
3208 * Return TRUE when "*last" changed.
3209 */
3210 static int
3211ti_change(str, last)
3212 char_u *str;
3213 char_u **last;
3214{
3215 if ((str == NULL) != (*last == NULL)
3216 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3217 {
3218 vim_free(*last);
3219 if (str == NULL)
3220 *last = NULL;
3221 else
3222 *last = vim_strsave(str);
3223 return TRUE;
3224 }
3225 return FALSE;
3226}
3227
3228/*
3229 * Put current window title back (used after calling a shell)
3230 */
3231 void
3232resettitle()
3233{
3234 mch_settitle(lasttitle, lasticon);
3235}
Bram Moolenaarea408852005-06-25 22:49:46 +00003236
3237# if defined(EXITFREE) || defined(PROTO)
3238 void
3239free_titles()
3240{
3241 vim_free(lasttitle);
3242 vim_free(lasticon);
3243}
3244# endif
3245
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246#endif /* FEAT_TITLE */
3247
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003248#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003250 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 * Return length of string in screen cells.
3252 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003253 * Normally works for window "wp", except when working for 'tabline' then it
3254 * is "curwin".
3255 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 * Items are drawn interspersed with the text that surrounds it
3257 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3258 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3259 *
3260 * If maxwidth is not zero, the string will be filled at any middle marker
3261 * or truncated if too long, fillchar is used for all whitespace.
3262 */
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003263/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003265build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003267 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 size_t outlen; /* length of out[] */
3269 char_u *fmt;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003270 int use_sandbox; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 int fillchar;
3272 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003273 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3274 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
3276 char_u *p;
3277 char_u *s;
3278 char_u *t;
3279 char_u *linecont;
3280#ifdef FEAT_EVAL
3281 win_T *o_curwin;
3282 buf_T *o_curbuf;
3283#endif
3284 int empty_line;
3285 colnr_T virtcol;
3286 long l;
3287 long n;
3288 int prevchar_isflag;
3289 int prevchar_isitem;
3290 int itemisflag;
3291 int fillable;
3292 char_u *str;
3293 long num;
3294 int width;
3295 int itemcnt;
3296 int curitem;
3297 int groupitem[STL_MAX_ITEM];
3298 int groupdepth;
3299 struct stl_item
3300 {
3301 char_u *start;
3302 int minwid;
3303 int maxwid;
3304 enum
3305 {
3306 Normal,
3307 Empty,
3308 Group,
3309 Middle,
3310 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003311 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 Trunc
3313 } type;
3314 } item[STL_MAX_ITEM];
3315 int minwid;
3316 int maxwid;
3317 int zeropad;
3318 char_u base;
3319 char_u opt;
3320#define TMPLEN 70
3321 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003322 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003323 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003324
3325#ifdef FEAT_EVAL
3326 /*
3327 * When the format starts with "%!" then evaluate it as an expression and
3328 * use the result as the actual format string.
3329 */
3330 if (fmt[0] == '%' && fmt[1] == '!')
3331 {
3332 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3333 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003334 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003335 }
3336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
3338 if (fillchar == 0)
3339 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003340#ifdef FEAT_MBYTE
3341 /* Can't handle a multi-byte fill character yet. */
3342 else if (mb_char2len(fillchar) > 1)
3343 fillchar = '-';
3344#endif
3345
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 /*
3347 * Get line & check if empty (cursorpos will show "0-1").
3348 * If inversion is possible we use it. Else '=' characters are used.
3349 */
3350 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3351 empty_line = (*linecont == NUL);
3352
3353 groupdepth = 0;
3354 p = out;
3355 curitem = 0;
3356 prevchar_isflag = TRUE;
3357 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003358 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
3360 if (*s != NUL && *s != '%')
3361 prevchar_isflag = prevchar_isitem = FALSE;
3362
3363 /*
3364 * Handle up to the next '%' or the end.
3365 */
3366 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3367 *p++ = *s++;
3368 if (*s == NUL || p + 1 >= out + outlen)
3369 break;
3370
3371 /*
3372 * Handle one '%' item.
3373 */
3374 s++;
3375 if (*s == '%')
3376 {
3377 if (p + 1 >= out + outlen)
3378 break;
3379 *p++ = *s++;
3380 prevchar_isflag = prevchar_isitem = FALSE;
3381 continue;
3382 }
3383 if (*s == STL_MIDDLEMARK)
3384 {
3385 s++;
3386 if (groupdepth > 0)
3387 continue;
3388 item[curitem].type = Middle;
3389 item[curitem++].start = p;
3390 continue;
3391 }
3392 if (*s == STL_TRUNCMARK)
3393 {
3394 s++;
3395 item[curitem].type = Trunc;
3396 item[curitem++].start = p;
3397 continue;
3398 }
3399 if (*s == ')')
3400 {
3401 s++;
3402 if (groupdepth < 1)
3403 continue;
3404 groupdepth--;
3405
3406 t = item[groupitem[groupdepth]].start;
3407 *p = NUL;
3408 l = vim_strsize(t);
3409 if (curitem > groupitem[groupdepth] + 1
3410 && item[groupitem[groupdepth]].minwid == 0)
3411 {
3412 /* remove group if all items are empty */
3413 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3414 if (item[n].type == Normal)
3415 break;
3416 if (n == curitem)
3417 {
3418 p = t;
3419 l = 0;
3420 }
3421 }
3422 if (l > item[groupitem[groupdepth]].maxwid)
3423 {
3424 /* truncate, remove n bytes of text at the start */
3425#ifdef FEAT_MBYTE
3426 if (has_mbyte)
3427 {
3428 /* Find the first character that should be included. */
3429 n = 0;
3430 while (l >= item[groupitem[groupdepth]].maxwid)
3431 {
3432 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003433 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 }
3435 }
3436 else
3437#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003438 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 *t = '<';
3441 mch_memmove(t + 1, t + n, p - (t + n));
3442 p = p - n + 1;
3443#ifdef FEAT_MBYTE
3444 /* Fill up space left over by half a double-wide char. */
3445 while (++l < item[groupitem[groupdepth]].minwid)
3446 *p++ = fillchar;
3447#endif
3448
3449 /* correct the start of the items for the truncation */
3450 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3451 {
3452 item[l].start -= n;
3453 if (item[l].start < t)
3454 item[l].start = t;
3455 }
3456 }
3457 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3458 {
3459 /* fill */
3460 n = item[groupitem[groupdepth]].minwid;
3461 if (n < 0)
3462 {
3463 /* fill by appending characters */
3464 n = 0 - n;
3465 while (l++ < n && p + 1 < out + outlen)
3466 *p++ = fillchar;
3467 }
3468 else
3469 {
3470 /* fill by inserting characters */
3471 mch_memmove(t + n - l, t, p - t);
3472 l = n - l;
3473 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003474 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 p += l;
3476 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3477 item[n].start += l;
3478 for ( ; l > 0; l--)
3479 *t++ = fillchar;
3480 }
3481 }
3482 continue;
3483 }
3484 minwid = 0;
3485 maxwid = 9999;
3486 zeropad = FALSE;
3487 l = 1;
3488 if (*s == '0')
3489 {
3490 s++;
3491 zeropad = TRUE;
3492 }
3493 if (*s == '-')
3494 {
3495 s++;
3496 l = -1;
3497 }
3498 if (VIM_ISDIGIT(*s))
3499 {
3500 minwid = (int)getdigits(&s);
3501 if (minwid < 0) /* overflow */
3502 minwid = 0;
3503 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003504 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 item[curitem].type = Highlight;
3507 item[curitem].start = p;
3508 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3509 s++;
3510 curitem++;
3511 continue;
3512 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003513 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3514 {
3515 if (*s == STL_TABCLOSENR)
3516 {
3517 if (minwid == 0)
3518 {
3519 /* %X ends the close label, go back to the previously
3520 * define tab label nr. */
3521 for (n = curitem - 1; n >= 0; --n)
3522 if (item[n].type == TabPage && item[n].minwid >= 0)
3523 {
3524 minwid = item[n].minwid;
3525 break;
3526 }
3527 }
3528 else
3529 /* close nrs are stored as negative values */
3530 minwid = - minwid;
3531 }
3532 item[curitem].type = TabPage;
3533 item[curitem].start = p;
3534 item[curitem].minwid = minwid;
3535 s++;
3536 curitem++;
3537 continue;
3538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 if (*s == '.')
3540 {
3541 s++;
3542 if (VIM_ISDIGIT(*s))
3543 {
3544 maxwid = (int)getdigits(&s);
3545 if (maxwid <= 0) /* overflow */
3546 maxwid = 50;
3547 }
3548 }
3549 minwid = (minwid > 50 ? 50 : minwid) * l;
3550 if (*s == '(')
3551 {
3552 groupitem[groupdepth++] = curitem;
3553 item[curitem].type = Group;
3554 item[curitem].start = p;
3555 item[curitem].minwid = minwid;
3556 item[curitem].maxwid = maxwid;
3557 s++;
3558 curitem++;
3559 continue;
3560 }
3561 if (vim_strchr(STL_ALL, *s) == NULL)
3562 {
3563 s++;
3564 continue;
3565 }
3566 opt = *s++;
3567
3568 /* OK - now for the real work */
3569 base = 'D';
3570 itemisflag = FALSE;
3571 fillable = TRUE;
3572 num = -1;
3573 str = NULL;
3574 switch (opt)
3575 {
3576 case STL_FILEPATH:
3577 case STL_FULLPATH:
3578 case STL_FILENAME:
3579 fillable = FALSE; /* don't change ' ' to fillchar */
3580 if (buf_spname(wp->w_buffer) != NULL)
3581 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3582 else
3583 {
3584 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003585 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3587 }
3588 trans_characters(NameBuff, MAXPATHL);
3589 if (opt != STL_FILENAME)
3590 str = NameBuff;
3591 else
3592 str = gettail(NameBuff);
3593 break;
3594
3595 case STL_VIM_EXPR: /* '{' */
3596 itemisflag = TRUE;
3597 t = p;
3598 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3599 *p++ = *s++;
3600 if (*s != '}') /* missing '}' or out of space */
3601 break;
3602 s++;
3603 *p = 0;
3604 p = t;
3605
3606#ifdef FEAT_EVAL
3607 sprintf((char *)tmp, "%d", curbuf->b_fnum);
3608 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3609
3610 o_curbuf = curbuf;
3611 o_curwin = curwin;
3612 curwin = wp;
3613 curbuf = wp->w_buffer;
3614
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003615 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
3617 curwin = o_curwin;
3618 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003619 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
3621 if (str != NULL && *str != 0)
3622 {
3623 if (*skipdigits(str) == NUL)
3624 {
3625 num = atoi((char *)str);
3626 vim_free(str);
3627 str = NULL;
3628 itemisflag = FALSE;
3629 }
3630 }
3631#endif
3632 break;
3633
3634 case STL_LINE:
3635 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3636 ? 0L : (long)(wp->w_cursor.lnum);
3637 break;
3638
3639 case STL_NUMLINES:
3640 num = wp->w_buffer->b_ml.ml_line_count;
3641 break;
3642
3643 case STL_COLUMN:
3644 num = !(State & INSERT) && empty_line
3645 ? 0 : (int)wp->w_cursor.col + 1;
3646 break;
3647
3648 case STL_VIRTCOL:
3649 case STL_VIRTCOL_ALT:
3650 /* In list mode virtcol needs to be recomputed */
3651 virtcol = wp->w_virtcol;
3652 if (wp->w_p_list && lcs_tab1 == NUL)
3653 {
3654 wp->w_p_list = FALSE;
3655 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3656 wp->w_p_list = TRUE;
3657 }
3658 ++virtcol;
3659 /* Don't display %V if it's the same as %c. */
3660 if (opt == STL_VIRTCOL_ALT
3661 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3662 ? 0 : (int)wp->w_cursor.col + 1)))
3663 break;
3664 num = (long)virtcol;
3665 break;
3666
3667 case STL_PERCENTAGE:
3668 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3669 (long)wp->w_buffer->b_ml.ml_line_count);
3670 break;
3671
3672 case STL_ALTPERCENT:
3673 str = tmp;
3674 get_rel_pos(wp, str);
3675 break;
3676
3677 case STL_ARGLISTSTAT:
3678 fillable = FALSE;
3679 tmp[0] = 0;
3680 if (append_arg_number(wp, tmp, FALSE, (int)sizeof(tmp)))
3681 str = tmp;
3682 break;
3683
3684 case STL_KEYMAP:
3685 fillable = FALSE;
3686 if (get_keymap_str(wp, tmp, TMPLEN))
3687 str = tmp;
3688 break;
3689 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003690#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003691 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692#else
3693 num = 0;
3694#endif
3695 break;
3696
3697 case STL_BUFNO:
3698 num = wp->w_buffer->b_fnum;
3699 break;
3700
3701 case STL_OFFSET_X:
3702 base = 'X';
3703 case STL_OFFSET:
3704#ifdef FEAT_BYTEOFF
3705 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3706 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3707 0L : l + 1 + (!(State & INSERT) && empty_line ?
3708 0 : (int)wp->w_cursor.col);
3709#endif
3710 break;
3711
3712 case STL_BYTEVAL_X:
3713 base = 'X';
3714 case STL_BYTEVAL:
Bram Moolenaar49104e42007-03-15 21:54:01 +00003715 if (wp->w_cursor.col > STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 num = 0;
3717 else
3718 {
3719#ifdef FEAT_MBYTE
3720 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3721#else
3722 num = linecont[wp->w_cursor.col];
3723#endif
3724 }
3725 if (num == NL)
3726 num = 0;
3727 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3728 num = NL;
3729 break;
3730
3731 case STL_ROFLAG:
3732 case STL_ROFLAG_ALT:
3733 itemisflag = TRUE;
3734 if (wp->w_buffer->b_p_ro)
3735 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3736 break;
3737
3738 case STL_HELPFLAG:
3739 case STL_HELPFLAG_ALT:
3740 itemisflag = TRUE;
3741 if (wp->w_buffer->b_help)
3742 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003743 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 break;
3745
3746#ifdef FEAT_AUTOCMD
3747 case STL_FILETYPE:
3748 if (*wp->w_buffer->b_p_ft != NUL
3749 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3750 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003751 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3752 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 str = tmp;
3754 }
3755 break;
3756
3757 case STL_FILETYPE_ALT:
3758 itemisflag = TRUE;
3759 if (*wp->w_buffer->b_p_ft != NUL
3760 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3761 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003762 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3763 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 for (t = tmp; *t != 0; t++)
3765 *t = TOUPPER_LOC(*t);
3766 str = tmp;
3767 }
3768 break;
3769#endif
3770
3771#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3772 case STL_PREVIEWFLAG:
3773 case STL_PREVIEWFLAG_ALT:
3774 itemisflag = TRUE;
3775 if (wp->w_p_pvw)
3776 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3777 : _("[Preview]"));
3778 break;
3779#endif
3780
3781 case STL_MODIFIED:
3782 case STL_MODIFIED_ALT:
3783 itemisflag = TRUE;
3784 switch ((opt == STL_MODIFIED_ALT)
3785 + bufIsChanged(wp->w_buffer) * 2
3786 + (!wp->w_buffer->b_p_ma) * 4)
3787 {
3788 case 2: str = (char_u *)"[+]"; break;
3789 case 3: str = (char_u *)",+"; break;
3790 case 4: str = (char_u *)"[-]"; break;
3791 case 5: str = (char_u *)",-"; break;
3792 case 6: str = (char_u *)"[+-]"; break;
3793 case 7: str = (char_u *)",+-"; break;
3794 }
3795 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003796
3797 case STL_HIGHLIGHT:
3798 t = s;
3799 while (*s != '#' && *s != NUL)
3800 ++s;
3801 if (*s == '#')
3802 {
3803 item[curitem].type = Highlight;
3804 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003805 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003806 curitem++;
3807 }
3808 ++s;
3809 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811
3812 item[curitem].start = p;
3813 item[curitem].type = Normal;
3814 if (str != NULL && *str)
3815 {
3816 t = str;
3817 if (itemisflag)
3818 {
3819 if ((t[0] && t[1])
3820 && ((!prevchar_isitem && *t == ',')
3821 || (prevchar_isflag && *t == ' ')))
3822 t++;
3823 prevchar_isflag = TRUE;
3824 }
3825 l = vim_strsize(t);
3826 if (l > 0)
3827 prevchar_isitem = TRUE;
3828 if (l > maxwid)
3829 {
3830 while (l >= maxwid)
3831#ifdef FEAT_MBYTE
3832 if (has_mbyte)
3833 {
3834 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003835 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837 else
3838#endif
3839 l -= byte2cells(*t++);
3840 if (p + 1 >= out + outlen)
3841 break;
3842 *p++ = '<';
3843 }
3844 if (minwid > 0)
3845 {
3846 for (; l < minwid && p + 1 < out + outlen; l++)
3847 {
3848 /* Don't put a "-" in front of a digit. */
3849 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3850 *p++ = ' ';
3851 else
3852 *p++ = fillchar;
3853 }
3854 minwid = 0;
3855 }
3856 else
3857 minwid *= -1;
3858 while (*t && p + 1 < out + outlen)
3859 {
3860 *p++ = *t++;
3861 /* Change a space by fillchar, unless fillchar is '-' and a
3862 * digit follows. */
3863 if (fillable && p[-1] == ' '
3864 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3865 p[-1] = fillchar;
3866 }
3867 for (; l < minwid && p + 1 < out + outlen; l++)
3868 *p++ = fillchar;
3869 }
3870 else if (num >= 0)
3871 {
3872 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3873 char_u nstr[20];
3874
3875 if (p + 20 >= out + outlen)
3876 break; /* not sufficient space */
3877 prevchar_isitem = TRUE;
3878 t = nstr;
3879 if (opt == STL_VIRTCOL_ALT)
3880 {
3881 *t++ = '-';
3882 minwid--;
3883 }
3884 *t++ = '%';
3885 if (zeropad)
3886 *t++ = '0';
3887 *t++ = '*';
3888 *t++ = nbase == 16 ? base : (nbase == 8 ? 'o' : 'd');
3889 *t = 0;
3890
3891 for (n = num, l = 1; n >= nbase; n /= nbase)
3892 l++;
3893 if (opt == STL_VIRTCOL_ALT)
3894 l++;
3895 if (l > maxwid)
3896 {
3897 l += 2;
3898 n = l - maxwid;
3899 while (l-- > maxwid)
3900 num /= nbase;
3901 *t++ = '>';
3902 *t++ = '%';
3903 *t = t[-3];
3904 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003905 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3906 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003909 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3910 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 p += STRLEN(p);
3912 }
3913 else
3914 item[curitem].type = Empty;
3915
3916 if (opt == STL_VIM_EXPR)
3917 vim_free(str);
3918
3919 if (num >= 0 || (!itemisflag && str && *str))
3920 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
3921 curitem++;
3922 }
3923 *p = NUL;
3924 itemcnt = curitem;
3925
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003926#ifdef FEAT_EVAL
3927 if (usefmt != fmt)
3928 vim_free(usefmt);
3929#endif
3930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 width = vim_strsize(out);
3932 if (maxwidth > 0 && width > maxwidth)
3933 {
3934 /* Result is too long, must trunctate somewhere. */
3935 l = 0;
3936 if (itemcnt == 0)
3937 s = out;
3938 else
3939 {
3940 for ( ; l < itemcnt; l++)
3941 if (item[l].type == Trunc)
3942 {
3943 /* Truncate at %< item. */
3944 s = item[l].start;
3945 break;
3946 }
3947 if (l == itemcnt)
3948 {
3949 /* No %< item, truncate first item. */
3950 s = item[0].start;
3951 l = 0;
3952 }
3953 }
3954
3955 if (width - vim_strsize(s) >= maxwidth)
3956 {
3957 /* Truncation mark is beyond max length */
3958#ifdef FEAT_MBYTE
3959 if (has_mbyte)
3960 {
3961 s = out;
3962 width = 0;
3963 for (;;)
3964 {
3965 width += ptr2cells(s);
3966 if (width >= maxwidth)
3967 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003968 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970 /* Fill up for half a double-wide character. */
3971 while (++width < maxwidth)
3972 *s++ = fillchar;
3973 }
3974 else
3975#endif
3976 s = out + maxwidth - 1;
3977 for (l = 0; l < itemcnt; l++)
3978 if (item[l].start > s)
3979 break;
3980 itemcnt = l;
3981 *s++ = '>';
3982 *s = 0;
3983 }
3984 else
3985 {
3986#ifdef FEAT_MBYTE
3987 if (has_mbyte)
3988 {
3989 n = 0;
3990 while (width >= maxwidth)
3991 {
3992 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003993 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 }
3995 }
3996 else
3997#endif
3998 n = width - maxwidth + 1;
3999 p = s + n;
4000 mch_memmove(s + 1, p, STRLEN(p) + 1);
4001 *s = '<';
4002
4003 /* Fill up for half a double-wide character. */
4004 while (++width < maxwidth)
4005 {
4006 s = s + STRLEN(s);
4007 *s++ = fillchar;
4008 *s = NUL;
4009 }
4010
4011 --n; /* count the '<' */
4012 for (; l < itemcnt; l++)
4013 {
4014 if (item[l].start - n >= s)
4015 item[l].start -= n;
4016 else
4017 item[l].start = s;
4018 }
4019 }
4020 width = maxwidth;
4021 }
4022 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4023 {
4024 /* Apply STL_MIDDLE if any */
4025 for (l = 0; l < itemcnt; l++)
4026 if (item[l].type == Middle)
4027 break;
4028 if (l < itemcnt)
4029 {
4030 p = item[l].start + maxwidth - width;
4031 mch_memmove(p, item[l].start, STRLEN(item[l].start) + 1);
4032 for (s = item[l].start; s < p; s++)
4033 *s = fillchar;
4034 for (l++; l < itemcnt; l++)
4035 item[l].start += maxwidth - width;
4036 width = maxwidth;
4037 }
4038 }
4039
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004040 /* Store the info about highlighting. */
4041 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004043 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 for (l = 0; l < itemcnt; l++)
4045 {
4046 if (item[l].type == Highlight)
4047 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004048 sp->start = item[l].start;
4049 sp->userhl = item[l].minwid;
4050 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 }
4052 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004053 sp->start = NULL;
4054 sp->userhl = 0;
4055 }
4056
4057 /* Store the info about tab pages labels. */
4058 if (tabtab != NULL)
4059 {
4060 sp = tabtab;
4061 for (l = 0; l < itemcnt; l++)
4062 {
4063 if (item[l].type == TabPage)
4064 {
4065 sp->start = item[l].start;
4066 sp->userhl = item[l].minwid;
4067 sp++;
4068 }
4069 }
4070 sp->start = NULL;
4071 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 }
4073
4074 return width;
4075}
4076#endif /* FEAT_STL_OPT */
4077
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004078#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4079 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004081 * Get relative cursor position in window into "str[]", in the form 99%, using
4082 * "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 */
4084 void
4085get_rel_pos(wp, str)
4086 win_T *wp;
4087 char_u *str;
4088{
4089 long above; /* number of lines above window */
4090 long below; /* number of lines below window */
4091
4092 above = wp->w_topline - 1;
4093#ifdef FEAT_DIFF
4094 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4095#endif
4096 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4097 if (below <= 0)
4098 STRCPY(str, above == 0 ? _("All") : _("Bot"));
4099 else if (above <= 0)
4100 STRCPY(str, _("Top"));
4101 else
4102 sprintf((char *)str, "%2d%%", above > 1000000L
4103 ? (int)(above / ((above + below) / 100L))
4104 : (int)(above * 100L / (above + below)));
4105}
4106#endif
4107
4108/*
4109 * Append (file 2 of 8) to 'buf', if editing more than one file.
4110 * Return TRUE if it was appended.
4111 */
4112 int
4113append_arg_number(wp, buf, add_file, maxlen)
4114 win_T *wp;
4115 char_u *buf;
4116 int add_file; /* Add "file" before the arg number */
4117 int maxlen; /* maximum nr of chars in buf or zero*/
4118{
4119 char_u *p;
4120
4121 if (ARGCOUNT <= 1) /* nothing to do */
4122 return FALSE;
4123
4124 p = buf + STRLEN(buf); /* go to the end of the buffer */
4125 if (maxlen && p - buf + 35 >= maxlen) /* getting too long */
4126 return FALSE;
4127 *p++ = ' ';
4128 *p++ = '(';
4129 if (add_file)
4130 {
4131 STRCPY(p, "file ");
4132 p += 5;
4133 }
4134 sprintf((char *)p, wp->w_arg_idx_invalid ? "(%d) of %d)"
4135 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4136 return TRUE;
4137}
4138
4139/*
4140 * If fname is not a full path, make it a full path.
4141 * Returns pointer to allocated memory (NULL for failure).
4142 */
4143 char_u *
4144fix_fname(fname)
4145 char_u *fname;
4146{
4147 /*
4148 * Force expanding the path always for Unix, because symbolic links may
4149 * mess up the full path name, even though it starts with a '/'.
4150 * Also expand when there is ".." in the file name, try to remove it,
4151 * because "c:/src/../README" is equal to "c:/README".
4152 * For MS-Windows also expand names like "longna~1" to "longname".
4153 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004154#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 return FullName_save(fname, TRUE);
4156#else
4157 if (!vim_isAbsName(fname) || strstr((char *)fname, "..") != NULL
4158#if defined(MSWIN) || defined(DJGPP)
4159 || vim_strchr(fname, '~') != NULL
4160#endif
4161 )
4162 return FullName_save(fname, FALSE);
4163
4164 fname = vim_strsave(fname);
4165
4166#ifdef USE_FNAME_CASE
4167# ifdef USE_LONG_FNAME
4168 if (USE_LONG_FNAME)
4169# endif
4170 {
4171 if (fname != NULL)
4172 fname_case(fname, 0); /* set correct case for file name */
4173 }
4174#endif
4175
4176 return fname;
4177#endif
4178}
4179
4180/*
4181 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4182 * "ffname" becomes a pointer to allocated memory (or NULL).
4183 */
4184/*ARGSUSED*/
4185 void
4186fname_expand(buf, ffname, sfname)
4187 buf_T *buf;
4188 char_u **ffname;
4189 char_u **sfname;
4190{
4191 if (*ffname == NULL) /* if no file name given, nothing to do */
4192 return;
4193 if (*sfname == NULL) /* if no short file name given, use ffname */
4194 *sfname = *ffname;
4195 *ffname = fix_fname(*ffname); /* expand to full path */
4196
4197#ifdef FEAT_SHORTCUT
4198 if (!buf->b_p_bin)
4199 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004200 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
4202 /* If the file name is a shortcut file, use the file it links to. */
4203 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004204 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 {
4206 vim_free(*ffname);
4207 *ffname = rfname;
4208 *sfname = rfname;
4209 }
4210 }
4211#endif
4212}
4213
4214/*
4215 * Get the file name for an argument list entry.
4216 */
4217 char_u *
4218alist_name(aep)
4219 aentry_T *aep;
4220{
4221 buf_T *bp;
4222
4223 /* Use the name from the associated buffer if it exists. */
4224 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004225 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 return aep->ae_fname;
4227 return bp->b_fname;
4228}
4229
4230#if defined(FEAT_WINDOWS) || defined(PROTO)
4231/*
4232 * do_arg_all(): Open up to 'count' windows, one for each argument.
4233 */
4234 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004235do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 int count;
4237 int forceit; /* hide buffers in current windows */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004238 int keep_tabs; /* keep curren tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
4240 int i;
4241 win_T *wp, *wpnext;
4242 char_u *opened; /* array of flags for which args are open */
4243 int opened_len; /* lenght of opened[] */
4244 int use_firstwin = FALSE; /* use first window for arglist */
4245 int split_ret = OK;
4246 int p_ea_save;
4247 alist_T *alist; /* argument list to be used */
4248 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004249 tabpage_T *tpnext;
4250 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004251 win_T *new_curwin = NULL;
4252 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
4254 if (ARGCOUNT <= 0)
4255 {
4256 /* Don't give an error message. We don't want it when the ":all"
4257 * command is in the .vimrc. */
4258 return;
4259 }
4260 setpcmark();
4261
4262 opened_len = ARGCOUNT;
4263 opened = alloc_clear((unsigned)opened_len);
4264 if (opened == NULL)
4265 return;
4266
4267#ifdef FEAT_GUI
4268 need_mouse_correct = TRUE;
4269#endif
4270
4271 /*
4272 * Try closing all windows that are not in the argument list.
4273 * Also close windows that are not full width;
4274 * When 'hidden' or "forceit" set the buffer becomes hidden.
4275 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004276 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004278 if (had_tab > 0)
4279 goto_tabpage_tp(first_tabpage);
4280 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004282 tpnext = curtab->tp_next;
4283 for (wp = firstwin; wp != NULL; wp = wpnext)
4284 {
4285 wpnext = wp->w_next;
4286 buf = wp->w_buffer;
4287 if (buf->b_ffname == NULL
4288 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004290 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004292 )
4293 i = ARGCOUNT;
4294 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004296 /* check if the buffer in this window is in the arglist */
4297 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004299 if (ARGLIST[i].ae_fnum == buf->b_fnum
4300 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004301 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004303 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004304 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004305 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004306 if (i == 0)
4307 {
4308 new_curwin = wp;
4309 new_curtab = curtab;
4310 }
4311 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004312 if (wp->w_alist != curwin->w_alist)
4313 {
4314 /* Use the current argument list for all windows
4315 * containing a file from it. */
4316 alist_unlink(wp->w_alist);
4317 wp->w_alist = curwin->w_alist;
4318 ++wp->w_alist->al_refcount;
4319 }
4320 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 }
4323 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004324 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004326 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004328 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004329 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004331 /* If the buffer was changed, and we would like to hide it,
4332 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004333 if (!P_HID(buf) && buf->b_nwindows <= 1
4334 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004336 (void)autowrite(buf, FALSE);
4337#ifdef FEAT_AUTOCMD
4338 /* check if autocommands removed the window */
4339 if (!win_valid(wp) || !buf_valid(buf))
4340 {
4341 wpnext = firstwin; /* start all over... */
4342 continue;
4343 }
4344#endif
4345 }
4346#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004347 /* don't close last window */
4348 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004349#endif
4350 use_firstwin = TRUE;
4351#ifdef FEAT_WINDOWS
4352 else
4353 {
4354 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4355# ifdef FEAT_AUTOCMD
4356 /* check if autocommands removed the next window */
4357 if (!win_valid(wpnext))
4358 wpnext = firstwin; /* start all over... */
4359# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361#endif
4362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
4364 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004365
4366 /* Without the ":tab" modifier only do the current tab page. */
4367 if (had_tab == 0 || tpnext == NULL)
4368 break;
4369
4370# ifdef FEAT_AUTOCMD
4371 /* check if autocommands removed the next tab page */
4372 if (!valid_tabpage(tpnext))
4373 tpnext = first_tabpage; /* start all over...*/
4374# endif
4375 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
4377
4378 /*
4379 * Open a window for files in the argument list that don't have one.
4380 * ARGCOUNT may change while doing this, because of autocommands.
4381 */
4382 if (count > ARGCOUNT || count <= 0)
4383 count = ARGCOUNT;
4384
4385 /* Autocommands may do anything to the argument list. Make sure it's not
4386 * freed while we are working here by "locking" it. We still have to
4387 * watch out for its size to be changed. */
4388 alist = curwin->w_alist;
4389 ++alist->al_refcount;
4390
4391#ifdef FEAT_AUTOCMD
4392 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4393 ++autocmd_no_enter;
4394 ++autocmd_no_leave;
4395#endif
4396 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004397#ifdef FEAT_WINDOWS
4398 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4399 * leaving an empty tab page when executed locally. */
4400 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4401 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4402 use_firstwin = TRUE;
4403#endif
4404
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4406 {
4407 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4408 arg_had_last = TRUE;
4409 if (i < opened_len && opened[i])
4410 {
4411 /* Move the already present window to below the current window */
4412 if (curwin->w_arg_idx != i)
4413 {
4414 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4415 {
4416 if (wpnext->w_arg_idx == i)
4417 {
4418 win_move_after(wpnext, curwin);
4419 break;
4420 }
4421 }
4422 }
4423 }
4424 else if (split_ret == OK)
4425 {
4426 if (!use_firstwin) /* split current window */
4427 {
4428 p_ea_save = p_ea;
4429 p_ea = TRUE; /* use space from all windows */
4430 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4431 p_ea = p_ea_save;
4432 if (split_ret == FAIL)
4433 continue;
4434 }
4435#ifdef FEAT_AUTOCMD
4436 else /* first window: do autocmd for leaving this buffer */
4437 --autocmd_no_leave;
4438#endif
4439
4440 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004441 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 */
4443 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004444 if (i == 0)
4445 {
4446 new_curwin = curwin;
4447 new_curtab = curtab;
4448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4450 ECMD_ONE,
4451 ((P_HID(curwin->w_buffer)
4452 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4453 + ECMD_OLDBUF);
4454#ifdef FEAT_AUTOCMD
4455 if (use_firstwin)
4456 ++autocmd_no_leave;
4457#endif
4458 use_firstwin = FALSE;
4459 }
4460 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004461
4462 /* When ":tab" was used open a new tab for a new window repeatedly. */
4463 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4464 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 }
4466
4467 /* Remove the "lock" on the argument list. */
4468 alist_unlink(alist);
4469
4470#ifdef FEAT_AUTOCMD
4471 --autocmd_no_enter;
4472#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004473 /* to window with first arg */
4474 if (valid_tabpage(new_curtab))
4475 goto_tabpage_tp(new_curtab);
4476 if (win_valid(new_curwin))
4477 win_enter(new_curwin, FALSE);
4478
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479#ifdef FEAT_AUTOCMD
4480 --autocmd_no_leave;
4481#endif
4482 vim_free(opened);
4483}
4484
4485# if defined(FEAT_LISTCMDS) || defined(PROTO)
4486/*
4487 * Open a window for a number of buffers.
4488 */
4489 void
4490ex_buffer_all(eap)
4491 exarg_T *eap;
4492{
4493 buf_T *buf;
4494 win_T *wp, *wpnext;
4495 int split_ret = OK;
4496 int p_ea_save;
4497 int open_wins = 0;
4498 int r;
4499 int count; /* Maximum number of windows to open. */
4500 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004501#ifdef FEAT_WINDOWS
4502 int had_tab = cmdmod.tab;
4503 tabpage_T *tpnext;
4504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
4506 if (eap->addr_count == 0) /* make as many windows as possible */
4507 count = 9999;
4508 else
4509 count = eap->line2; /* make as many windows as specified */
4510 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4511 all = FALSE;
4512 else
4513 all = TRUE;
4514
4515 setpcmark();
4516
4517#ifdef FEAT_GUI
4518 need_mouse_correct = TRUE;
4519#endif
4520
4521 /*
4522 * Close superfluous windows (two windows for the same buffer).
4523 * Also close windows that are not full-width.
4524 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004525#ifdef FEAT_WINDOWS
4526 if (had_tab > 0)
4527 goto_tabpage_tp(first_tabpage);
4528 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004531 tpnext = curtab->tp_next;
4532 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004534 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004535 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004536#ifdef FEAT_VERTSPLIT
4537 || ((cmdmod.split & WSP_VERT)
4538 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004539 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004540 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004542#ifdef FEAT_WINDOWS
4543 || (had_tab > 0 && wp != firstwin)
4544#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004545 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004546 {
4547 win_close(wp, FALSE);
4548#ifdef FEAT_AUTOCMD
4549 wpnext = firstwin; /* just in case an autocommand does
4550 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004551 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004552 open_wins = 0;
4553#endif
4554 }
4555 else
4556 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004558
4559#ifdef FEAT_WINDOWS
4560 /* Without the ":tab" modifier only do the current tab page. */
4561 if (had_tab == 0 || tpnext == NULL)
4562 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004563 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
4567 /*
4568 * Go through the buffer list. When a buffer doesn't have a window yet,
4569 * open one. Otherwise move the window to the right position.
4570 * Watch out for autocommands that delete buffers or windows!
4571 */
4572#ifdef FEAT_AUTOCMD
4573 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4574 ++autocmd_no_enter;
4575#endif
4576 win_enter(lastwin, FALSE);
4577#ifdef FEAT_AUTOCMD
4578 ++autocmd_no_leave;
4579#endif
4580 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4581 {
4582 /* Check if this buffer needs a window */
4583 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4584 continue;
4585
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004586#ifdef FEAT_WINDOWS
4587 if (had_tab != 0)
4588 {
4589 /* With the ":tab" modifier don't move the window. */
4590 if (buf->b_nwindows > 0)
4591 wp = lastwin; /* buffer has a window, skip it */
4592 else
4593 wp = NULL;
4594 }
4595 else
4596#endif
4597 {
4598 /* Check if this buffer already has a window */
4599 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4600 if (wp->w_buffer == buf)
4601 break;
4602 /* If the buffer already has a window, move it */
4603 if (wp != NULL)
4604 win_move_after(wp, curwin);
4605 }
4606
4607 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {
4609 /* Split the window and put the buffer in it */
4610 p_ea_save = p_ea;
4611 p_ea = TRUE; /* use space from all windows */
4612 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4613 ++open_wins;
4614 p_ea = p_ea_save;
4615 if (split_ret == FAIL)
4616 continue;
4617
4618 /* Open the buffer in this window. */
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_DIALOG;
4621#endif
4622 set_curbuf(buf, DOBUF_GOTO);
4623#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004626#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 break;
4630 }
4631#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004632#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (swap_exists_action == SEA_QUIT)
4634 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004635# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4636 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004637
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004638 /* Reset the error/interrupt/exception state here so that
4639 * aborting() returns FALSE when closing a window. */
4640 enter_cleanup(&cs);
4641# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004642
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004643 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 win_close(curwin, TRUE);
4645 --open_wins;
4646 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004647 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004648
4649# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4650 /* Restore the error/interrupt/exception state if not
4651 * discarded by a new aborting error, interrupt, or uncaught
4652 * exception. */
4653 leave_cleanup(&cs);
4654# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656 else
4657 handle_swap_exists(NULL);
4658#endif
4659 }
4660
4661 ui_breakcheck();
4662 if (got_int)
4663 {
4664 (void)vgetc(); /* only break the file loading, not the rest */
4665 break;
4666 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004667#ifdef FEAT_EVAL
4668 /* Autocommands deleted the buffer or aborted script processing!!! */
4669 if (aborting())
4670 break;
4671#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004672#ifdef FEAT_WINDOWS
4673 /* When ":tab" was used open a new tab for a new window repeatedly. */
4674 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4675 cmdmod.tab = 9999;
4676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 }
4678#ifdef FEAT_AUTOCMD
4679 --autocmd_no_enter;
4680#endif
4681 win_enter(firstwin, FALSE); /* back to first window */
4682#ifdef FEAT_AUTOCMD
4683 --autocmd_no_leave;
4684#endif
4685
4686 /*
4687 * Close superfluous windows.
4688 */
4689 for (wp = lastwin; open_wins > count; )
4690 {
4691 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4692 || autowrite(wp->w_buffer, FALSE) == OK);
4693#ifdef FEAT_AUTOCMD
4694 if (!win_valid(wp))
4695 {
4696 /* BufWrite Autocommands made the window invalid, start over */
4697 wp = lastwin;
4698 }
4699 else
4700#endif
4701 if (r)
4702 {
4703 win_close(wp, !P_HID(wp->w_buffer));
4704 --open_wins;
4705 wp = lastwin;
4706 }
4707 else
4708 {
4709 wp = wp->w_prev;
4710 if (wp == NULL)
4711 break;
4712 }
4713 }
4714}
4715# endif /* FEAT_LISTCMDS */
4716
4717#endif /* FEAT_WINDOWS */
4718
Bram Moolenaara3227e22006-03-08 21:32:40 +00004719static int chk_modeline __ARGS((linenr_T, int));
4720
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721/*
4722 * do_modelines() - process mode lines for the current file
4723 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004724 * "flags" can be:
4725 * OPT_WINONLY only set options local to window
4726 * OPT_NOWIN don't set options local to window
4727 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 * Returns immediately if the "ml" option isn't set.
4729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004731do_modelines(flags)
4732 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004734 linenr_T lnum;
4735 int nmlines;
4736 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
4738 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4739 return;
4740
4741 /* Disallow recursive entry here. Can happen when executing a modeline
4742 * triggers an autocommand, which reloads modelines with a ":do". */
4743 if (entered)
4744 return;
4745
4746 ++entered;
4747 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4748 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004749 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 nmlines = 0;
4751
4752 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4753 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004754 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 nmlines = 0;
4756 --entered;
4757}
4758
4759#include "version.h" /* for version number */
4760
4761/*
4762 * chk_modeline() - check a single line for a mode string
4763 * Return FAIL if an error encountered.
4764 */
4765 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004766chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004768 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769{
4770 char_u *s;
4771 char_u *e;
4772 char_u *linecopy; /* local copy of any modeline found */
4773 int prev;
4774 int vers;
4775 int end;
4776 int retval = OK;
4777 char_u *save_sourcing_name;
4778 linenr_T save_sourcing_lnum;
4779#ifdef FEAT_EVAL
4780 scid_T save_SID;
4781#endif
4782
4783 prev = -1;
4784 for (s = ml_get(lnum); *s != NUL; ++s)
4785 {
4786 if (prev == -1 || vim_isspace(prev))
4787 {
4788 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4789 || STRNCMP(s, "vi:", (size_t)3) == 0)
4790 break;
4791 if (STRNCMP(s, "vim", 3) == 0)
4792 {
4793 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4794 e = s + 4;
4795 else
4796 e = s + 3;
4797 vers = getdigits(&e);
4798 if (*e == ':'
4799 && (s[3] == ':'
4800 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4801 || (VIM_VERSION_100 < vers && s[3] == '<')
4802 || (VIM_VERSION_100 > vers && s[3] == '>')
4803 || (VIM_VERSION_100 == vers && s[3] == '=')))
4804 break;
4805 }
4806 }
4807 prev = *s;
4808 }
4809
4810 if (*s)
4811 {
4812 do /* skip over "ex:", "vi:" or "vim:" */
4813 ++s;
4814 while (s[-1] != ':');
4815
4816 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4817 if (linecopy == NULL)
4818 return FAIL;
4819
4820 save_sourcing_lnum = sourcing_lnum;
4821 save_sourcing_name = sourcing_name;
4822 sourcing_lnum = lnum; /* prepare for emsg() */
4823 sourcing_name = (char_u *)"modelines";
4824
4825 end = FALSE;
4826 while (end == FALSE)
4827 {
4828 s = skipwhite(s);
4829 if (*s == NUL)
4830 break;
4831
4832 /*
4833 * Find end of set command: ':' or end of line.
4834 * Skip over "\:", replacing it with ":".
4835 */
4836 for (e = s; *e != ':' && *e != NUL; ++e)
4837 if (e[0] == '\\' && e[1] == ':')
4838 STRCPY(e, e + 1);
4839 if (*e == NUL)
4840 end = TRUE;
4841
4842 /*
4843 * If there is a "set" command, require a terminating ':' and
4844 * ignore the stuff after the ':'.
4845 * "vi:set opt opt opt: foo" -- foo not interpreted
4846 * "vi:opt opt opt: foo" -- foo interpreted
4847 * Accept "se" for compatibility with Elvis.
4848 */
4849 if (STRNCMP(s, "set ", (size_t)4) == 0
4850 || STRNCMP(s, "se ", (size_t)3) == 0)
4851 {
4852 if (*e != ':') /* no terminating ':'? */
4853 break;
4854 end = TRUE;
4855 s = vim_strchr(s, ' ') + 1;
4856 }
4857 *e = NUL; /* truncate the set command */
4858
4859 if (*s != NUL) /* skip over an empty "::" */
4860 {
4861#ifdef FEAT_EVAL
4862 save_SID = current_SID;
4863 current_SID = SID_MODELINE;
4864#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004865 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866#ifdef FEAT_EVAL
4867 current_SID = save_SID;
4868#endif
4869 if (retval == FAIL) /* stop if error found */
4870 break;
4871 }
4872 s = e + 1; /* advance to next part */
4873 }
4874
4875 sourcing_lnum = save_sourcing_lnum;
4876 sourcing_name = save_sourcing_name;
4877
4878 vim_free(linecopy);
4879 }
4880 return retval;
4881}
4882
4883#ifdef FEAT_VIMINFO
4884 int
4885read_viminfo_bufferlist(virp, writing)
4886 vir_T *virp;
4887 int writing;
4888{
4889 char_u *tab;
4890 linenr_T lnum;
4891 colnr_T col;
4892 buf_T *buf;
4893 char_u *sfname;
4894 char_u *xline;
4895
4896 /* Handle long line and escaped characters. */
4897 xline = viminfo_readstring(virp, 1, FALSE);
4898
4899 /* don't read in if there are files on the command-line or if writing: */
4900 if (xline != NULL && !writing && ARGCOUNT == 0
4901 && find_viminfo_parameter('%') != NULL)
4902 {
4903 /* Format is: <fname> Tab <lnum> Tab <col>.
4904 * Watch out for a Tab in the file name, work from the end. */
4905 lnum = 0;
4906 col = 0;
4907 tab = vim_strrchr(xline, '\t');
4908 if (tab != NULL)
4909 {
4910 *tab++ = '\0';
4911 col = atoi((char *)tab);
4912 tab = vim_strrchr(xline, '\t');
4913 if (tab != NULL)
4914 {
4915 *tab++ = '\0';
4916 lnum = atol((char *)tab);
4917 }
4918 }
4919
4920 /* Expand "~/" in the file name at "line + 1" to a full path.
4921 * Then try shortening it by comparing with the current directory */
4922 expand_env(xline, NameBuff, MAXPATHL);
4923 mch_dirname(IObuff, IOSIZE);
4924 sfname = shorten_fname(NameBuff, IObuff);
4925 if (sfname == NULL)
4926 sfname = NameBuff;
4927
4928 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
4929 if (buf != NULL) /* just in case... */
4930 {
4931 buf->b_last_cursor.lnum = lnum;
4932 buf->b_last_cursor.col = col;
4933 buflist_setfpos(buf, curwin, lnum, col, FALSE);
4934 }
4935 }
4936 vim_free(xline);
4937
4938 return viminfo_readline(virp);
4939}
4940
4941 void
4942write_viminfo_bufferlist(fp)
4943 FILE *fp;
4944{
4945 buf_T *buf;
4946#ifdef FEAT_WINDOWS
4947 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004948 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949#endif
4950 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004951 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952
4953 if (find_viminfo_parameter('%') == NULL)
4954 return;
4955
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004956 /* Without a number -1 is returned: do all buffers. */
4957 max_buffers = get_viminfo_parameter('%');
4958
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004960 line = alloc(MAXPATHL + 40);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 if (line == NULL)
4962 return;
4963
4964#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00004965 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 set_last_cursor(win);
4967#else
4968 set_last_cursor(curwin);
4969#endif
4970
4971 fprintf(fp, _("\n# Buffer list:\n"));
4972 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
4973 {
4974 if (buf->b_fname == NULL
4975 || !buf->b_p_bl
4976#ifdef FEAT_QUICKFIX
4977 || bt_quickfix(buf)
4978#endif
4979 || removable(buf->b_ffname))
4980 continue;
4981
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004982 if (max_buffers-- == 0)
4983 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 putc('%', fp);
4985 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
4986 sprintf((char *)line + STRLEN(line), "\t%ld\t%d",
4987 (long)buf->b_last_cursor.lnum,
4988 buf->b_last_cursor.col);
4989 viminfo_writestring(fp, line);
4990 }
4991 vim_free(line);
4992}
4993#endif
4994
4995
4996/*
4997 * Return special buffer name.
4998 * Returns NULL when the buffer has a normal file name.
4999 */
5000 char *
5001buf_spname(buf)
5002 buf_T *buf;
5003{
5004#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5005 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005006 {
5007 win_T *win;
5008
5009 /*
5010 * For location list window, w_llist_ref points to the location list.
5011 * For quickfix window, w_llist_ref is NULL.
5012 */
5013 FOR_ALL_WINDOWS(win)
5014 if (win->w_buffer == buf)
5015 break;
5016 if (win != NULL && win->w_llist_ref != NULL)
5017 return _("[Location List]");
5018 else
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005019 return _("[Quickfix List]");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021#endif
5022#ifdef FEAT_QUICKFIX
5023 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5024 * contains the name as specified by the user */
5025 if (bt_nofile(buf))
5026 {
5027 if (buf->b_sfname != NULL)
5028 return (char *)buf->b_sfname;
5029 return "[Scratch]";
5030 }
5031#endif
5032 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005033 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 return NULL;
5035}
5036
5037
5038#if defined(FEAT_SIGNS) || defined(PROTO)
5039/*
5040 * Insert the sign into the signlist.
5041 */
5042 static void
5043insert_sign(buf, prev, next, id, lnum, typenr)
5044 buf_T *buf; /* buffer to store sign in */
5045 signlist_T *prev; /* previous sign entry */
5046 signlist_T *next; /* next sign entry */
5047 int id; /* sign ID */
5048 linenr_T lnum; /* line number which gets the mark */
5049 int typenr; /* typenr of sign we are adding */
5050{
5051 signlist_T *newsign;
5052
5053 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5054 if (newsign != NULL)
5055 {
5056 newsign->id = id;
5057 newsign->lnum = lnum;
5058 newsign->typenr = typenr;
5059 newsign->next = next;
5060#ifdef FEAT_NETBEANS_INTG
5061 newsign->prev = prev;
5062 if (next != NULL)
5063 next->prev = newsign;
5064#endif
5065
5066 if (prev == NULL)
5067 {
5068 /* When adding first sign need to redraw the windows to create the
5069 * column for signs. */
5070 if (buf->b_signlist == NULL)
5071 {
5072 redraw_buf_later(buf, NOT_VALID);
5073 changed_cline_bef_curs();
5074 }
5075
5076 /* first sign in signlist */
5077 buf->b_signlist = newsign;
5078 }
5079 else
5080 prev->next = newsign;
5081 }
5082}
5083
5084/*
5085 * Add the sign into the signlist. Find the right spot to do it though.
5086 */
5087 void
5088buf_addsign(buf, id, lnum, typenr)
5089 buf_T *buf; /* buffer to store sign in */
5090 int id; /* sign ID */
5091 linenr_T lnum; /* line number which gets the mark */
5092 int typenr; /* typenr of sign we are adding */
5093{
5094 signlist_T *sign; /* a sign in the signlist */
5095 signlist_T *prev; /* the previous sign */
5096
5097 prev = NULL;
5098 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5099 {
5100 if (lnum == sign->lnum && id == sign->id)
5101 {
5102 sign->typenr = typenr;
5103 return;
5104 }
5105 else if (
5106#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5107 id < 0 &&
5108#endif
5109 lnum < sign->lnum)
5110 {
5111#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5112 /* XXX - GRP: Is this because of sign slide problem? Or is it
5113 * really needed? Or is it because we allow multiple signs per
5114 * line? If so, should I add that feature to FEAT_SIGNS?
5115 */
5116 while (prev != NULL && prev->lnum == lnum)
5117 prev = prev->prev;
5118 if (prev == NULL)
5119 sign = buf->b_signlist;
5120 else
5121 sign = prev->next;
5122#endif
5123 insert_sign(buf, prev, sign, id, lnum, typenr);
5124 return;
5125 }
5126 prev = sign;
5127 }
5128#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5129 /* XXX - GRP: See previous comment */
5130 while (prev != NULL && prev->lnum == lnum)
5131 prev = prev->prev;
5132 if (prev == NULL)
5133 sign = buf->b_signlist;
5134 else
5135 sign = prev->next;
5136#endif
5137 insert_sign(buf, prev, sign, id, lnum, typenr);
5138
5139 return;
5140}
5141
5142 int
5143buf_change_sign_type(buf, markId, typenr)
5144 buf_T *buf; /* buffer to store sign in */
5145 int markId; /* sign ID */
5146 int typenr; /* typenr of sign we are adding */
5147{
5148 signlist_T *sign; /* a sign in the signlist */
5149
5150 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5151 {
5152 if (sign->id == markId)
5153 {
5154 sign->typenr = typenr;
5155 return sign->lnum;
5156 }
5157 }
5158
5159 return 0;
5160}
5161
5162 int_u
5163buf_getsigntype(buf, lnum, type)
5164 buf_T *buf;
5165 linenr_T lnum;
5166 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5167{
5168 signlist_T *sign; /* a sign in a b_signlist */
5169
5170 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5171 if (sign->lnum == lnum
5172 && (type == SIGN_ANY
5173# ifdef FEAT_SIGN_ICONS
5174 || (type == SIGN_ICON
5175 && sign_get_image(sign->typenr) != NULL)
5176# endif
5177 || (type == SIGN_TEXT
5178 && sign_get_text(sign->typenr) != NULL)
5179 || (type == SIGN_LINEHL
5180 && sign_get_attr(sign->typenr, TRUE) != 0)))
5181 return sign->typenr;
5182 return 0;
5183}
5184
5185
5186 linenr_T
5187buf_delsign(buf, id)
5188 buf_T *buf; /* buffer sign is stored in */
5189 int id; /* sign id */
5190{
5191 signlist_T **lastp; /* pointer to pointer to current sign */
5192 signlist_T *sign; /* a sign in a b_signlist */
5193 signlist_T *next; /* the next sign in a b_signlist */
5194 linenr_T lnum; /* line number whose sign was deleted */
5195
5196 lastp = &buf->b_signlist;
5197 lnum = 0;
5198 for (sign = buf->b_signlist; sign != NULL; sign = next)
5199 {
5200 next = sign->next;
5201 if (sign->id == id)
5202 {
5203 *lastp = next;
5204#ifdef FEAT_NETBEANS_INTG
5205 if (next != NULL)
5206 next->prev = sign->prev;
5207#endif
5208 lnum = sign->lnum;
5209 vim_free(sign);
5210 break;
5211 }
5212 else
5213 lastp = &sign->next;
5214 }
5215
5216 /* When deleted the last sign need to redraw the windows to remove the
5217 * sign column. */
5218 if (buf->b_signlist == NULL)
5219 {
5220 redraw_buf_later(buf, NOT_VALID);
5221 changed_cline_bef_curs();
5222 }
5223
5224 return lnum;
5225}
5226
5227
5228/*
5229 * Find the line number of the sign with the requested id. If the sign does
5230 * not exist, return 0 as the line number. This will still let the correct file
5231 * get loaded.
5232 */
5233 int
5234buf_findsign(buf, id)
5235 buf_T *buf; /* buffer to store sign in */
5236 int id; /* sign ID */
5237{
5238 signlist_T *sign; /* a sign in the signlist */
5239
5240 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5241 if (sign->id == id)
5242 return sign->lnum;
5243
5244 return 0;
5245}
5246
5247 int
5248buf_findsign_id(buf, lnum)
5249 buf_T *buf; /* buffer whose sign we are searching for */
5250 linenr_T lnum; /* line number of sign */
5251{
5252 signlist_T *sign; /* a sign in the signlist */
5253
5254 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5255 if (sign->lnum == lnum)
5256 return sign->id;
5257
5258 return 0;
5259}
5260
5261
5262# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5263/* see if a given type of sign exists on a specific line */
5264 int
5265buf_findsigntype_id(buf, lnum, typenr)
5266 buf_T *buf; /* buffer whose sign we are searching for */
5267 linenr_T lnum; /* line number of sign */
5268 int typenr; /* sign type number */
5269{
5270 signlist_T *sign; /* a sign in the signlist */
5271
5272 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5273 if (sign->lnum == lnum && sign->typenr == typenr)
5274 return sign->id;
5275
5276 return 0;
5277}
5278
5279
5280# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5281/* return the number of icons on the given line */
5282 int
5283buf_signcount(buf, lnum)
5284 buf_T *buf;
5285 linenr_T lnum;
5286{
5287 signlist_T *sign; /* a sign in the signlist */
5288 int count = 0;
5289
5290 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5291 if (sign->lnum == lnum)
5292 if (sign_get_image(sign->typenr) != NULL)
5293 count++;
5294
5295 return count;
5296}
5297# endif /* FEAT_SIGN_ICONS */
5298# endif /* FEAT_NETBEANS_INTG */
5299
5300
5301/*
5302 * Delete signs in buffer "buf".
5303 */
5304 static void
5305buf_delete_signs(buf)
5306 buf_T *buf;
5307{
5308 signlist_T *next;
5309
5310 while (buf->b_signlist != NULL)
5311 {
5312 next = buf->b_signlist->next;
5313 vim_free(buf->b_signlist);
5314 buf->b_signlist = next;
5315 }
5316}
5317
5318/*
5319 * Delete all signs in all buffers.
5320 */
5321 void
5322buf_delete_all_signs()
5323{
5324 buf_T *buf; /* buffer we are checking for signs */
5325
5326 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5327 if (buf->b_signlist != NULL)
5328 {
5329 /* Need to redraw the windows to remove the sign column. */
5330 redraw_buf_later(buf, NOT_VALID);
5331 buf_delete_signs(buf);
5332 }
5333}
5334
5335/*
5336 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5337 */
5338 void
5339sign_list_placed(rbuf)
5340 buf_T *rbuf;
5341{
5342 buf_T *buf;
5343 signlist_T *p;
5344 char lbuf[BUFSIZ];
5345
5346 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5347 msg_putchar('\n');
5348 if (rbuf == NULL)
5349 buf = firstbuf;
5350 else
5351 buf = rbuf;
5352 while (buf != NULL)
5353 {
5354 if (buf->b_signlist != NULL)
5355 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005356 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5358 msg_putchar('\n');
5359 }
5360 for (p = buf->b_signlist; p != NULL; p = p->next)
5361 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005362 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5364 MSG_PUTS(lbuf);
5365 msg_putchar('\n');
5366 }
5367 if (rbuf != NULL)
5368 break;
5369 buf = buf->b_next;
5370 }
5371}
5372
5373/*
5374 * Adjust a placed sign for inserted/deleted lines.
5375 */
5376 void
5377sign_mark_adjust(line1, line2, amount, amount_after)
5378 linenr_T line1;
5379 linenr_T line2;
5380 long amount;
5381 long amount_after;
5382{
5383 signlist_T *sign; /* a sign in a b_signlist */
5384
5385 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5386 {
5387 if (sign->lnum >= line1 && sign->lnum <= line2)
5388 {
5389 if (amount == MAXLNUM)
5390 sign->lnum = line1;
5391 else
5392 sign->lnum += amount;
5393 }
5394 else if (sign->lnum > line2)
5395 sign->lnum += amount_after;
5396 }
5397}
5398#endif /* FEAT_SIGNS */
5399
5400/*
5401 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5402 */
5403 void
5404set_buflisted(on)
5405 int on;
5406{
5407 if (on != curbuf->b_p_bl)
5408 {
5409 curbuf->b_p_bl = on;
5410#ifdef FEAT_AUTOCMD
5411 if (on)
5412 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5413 else
5414 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5415#endif
5416 }
5417}
5418
5419/*
5420 * Read the file for "buf" again and check if the contents changed.
5421 * Return TRUE if it changed or this could not be checked.
5422 */
5423 int
5424buf_contents_changed(buf)
5425 buf_T *buf;
5426{
5427 buf_T *newbuf;
5428 int differ = TRUE;
5429 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 exarg_T ea;
5432
5433 /* Allocate a buffer without putting it in the buffer list. */
5434 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5435 if (newbuf == NULL)
5436 return TRUE;
5437
5438 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5439 if (prep_exarg(&ea, buf) == FAIL)
5440 {
5441 wipe_buffer(newbuf, FALSE);
5442 return TRUE;
5443 }
5444
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 /* set curwin/curbuf to buf and save a few things */
5446 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447
Bram Moolenaar4770d092006-01-12 23:22:24 +00005448 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 && readfile(buf->b_ffname, buf->b_fname,
5450 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5451 &ea, READ_NEW | READ_DUMMY) == OK)
5452 {
5453 /* compare the two files line by line */
5454 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5455 {
5456 differ = FALSE;
5457 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5458 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5459 {
5460 differ = TRUE;
5461 break;
5462 }
5463 }
5464 }
5465 vim_free(ea.cmd);
5466
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 /* restore curwin/curbuf and a few other things */
5468 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469
5470 if (curbuf != newbuf) /* safety check */
5471 wipe_buffer(newbuf, FALSE);
5472
5473 return differ;
5474}
5475
5476/*
5477 * Wipe out a buffer and decrement the last buffer number if it was used for
5478 * this buffer. Call this to wipe out a temp buffer that does not contain any
5479 * marks.
5480 */
5481/*ARGSUSED*/
5482 void
5483wipe_buffer(buf, aucmd)
5484 buf_T *buf;
5485 int aucmd; /* When TRUE trigger autocommands. */
5486{
5487 if (buf->b_fnum == top_file_num - 1)
5488 --top_file_num;
5489
5490#ifdef FEAT_AUTOCMD
5491 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5492 ++autocmd_block;
5493#endif
5494 close_buffer(NULL, buf, DOBUF_WIPE);
5495#ifdef FEAT_AUTOCMD
5496 if (!aucmd)
5497 --autocmd_block;
5498#endif
5499}