blob: e05bd3935e944caab9f197e2d1e4d52b8ac7d0a4 [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 Moolenaar779b74b2006-04-10 14:55:34 +00001211 if (vim_strchr(p_swb, 'o') && buf_jump_open_win(buf))
1212 return OK;
1213 /* jump to first window in any tab page containing buf if one exists
1214 * ("usetab") */
1215 if (vim_strchr(p_swb, 'a') && 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 {
1389 need_fileinfo = TRUE; /* display file info after redraw */
1390 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1391#ifdef FEAT_AUTOCMD
1392 curwin->w_topline = 1;
1393# ifdef FEAT_DIFF
1394 curwin->w_topfill = 0;
1395# endif
1396 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1397 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1398#endif
1399 }
1400
1401 /* If autocommands did not change the cursor position, restore cursor lnum
1402 * and possibly cursor col. */
1403 if (curwin->w_cursor.lnum == 1 && inindent(0))
1404 buflist_getfpos();
1405
1406 check_arg_idx(curwin); /* check for valid arg_idx */
1407#ifdef FEAT_TITLE
1408 maketitle();
1409#endif
1410#ifdef FEAT_AUTOCMD
1411 if (curwin->w_topline == 1) /* when autocmds didn't change it */
1412#endif
1413 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1414
Bram Moolenaar009b2592004-10-24 19:18:58 +00001415#ifdef FEAT_NETBEANS_INTG
1416 /* Send fileOpened event because we've changed buffers. */
1417 if (usingNetbeans && isNetbeansBuffer(curbuf))
1418 netbeans_file_activated(curbuf);
1419#endif
1420
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001421 /* Change directories when the 'acd' option is set. */
1422 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424#ifdef FEAT_KEYMAP
1425 if (curbuf->b_kmap_state & KEYMAP_INIT)
1426 keymap_init();
1427#endif
1428 redraw_later(NOT_VALID);
1429}
1430
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001431#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1432/*
1433 * Change to the directory of the current buffer.
1434 */
1435 void
1436do_autochdir()
1437{
1438 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1439 shorten_fnames(TRUE);
1440}
1441#endif
1442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443/*
1444 * functions for dealing with the buffer list
1445 */
1446
1447/*
1448 * Add a file name to the buffer list. Return a pointer to the buffer.
1449 * If the same file name already exists return a pointer to that buffer.
1450 * If it does not exist, or if fname == NULL, a new entry is created.
1451 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1452 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1453 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 * This is the ONLY way to create a new buffer.
1455 */
1456static int top_file_num = 1; /* highest file number */
1457
1458 buf_T *
1459buflist_new(ffname, sfname, lnum, flags)
1460 char_u *ffname; /* full path of fname or relative */
1461 char_u *sfname; /* short fname or NULL */
1462 linenr_T lnum; /* preferred cursor line */
1463 int flags; /* BLN_ defines */
1464{
1465 buf_T *buf;
1466#ifdef UNIX
1467 struct stat st;
1468#endif
1469
1470 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1471
1472 /*
1473 * If file name already exists in the list, update the entry.
1474 */
1475#ifdef UNIX
1476 /* On Unix we can use inode numbers when the file exists. Works better
1477 * for hard links. */
1478 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1479 st.st_dev = (dev_T)-1;
1480#endif
1481 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1482#ifdef UNIX
1483 buflist_findname_stat(ffname, &st)
1484#else
1485 buflist_findname(ffname)
1486#endif
1487 ) != NULL)
1488 {
1489 vim_free(ffname);
1490 if (lnum != 0)
1491 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1492 /* copy the options now, if 'cpo' doesn't have 's' and not done
1493 * already */
1494 buf_copy_options(buf, 0);
1495 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1496 {
1497 buf->b_p_bl = TRUE;
1498#ifdef FEAT_AUTOCMD
1499 if (!(flags & BLN_DUMMY))
1500 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1501#endif
1502 }
1503 return buf;
1504 }
1505
1506 /*
1507 * If the current buffer has no name and no contents, use the current
1508 * buffer. Otherwise: Need to allocate a new buffer structure.
1509 *
1510 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001511 * (A spell file buffer is allocated in spell.c, but that's not a normal
1512 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 */
1514 buf = NULL;
1515 if ((flags & BLN_CURBUF)
1516 && curbuf != NULL
1517 && curbuf->b_ffname == NULL
1518 && curbuf->b_nwindows <= 1
1519 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1520 {
1521 buf = curbuf;
1522#ifdef FEAT_AUTOCMD
1523 /* It's like this buffer is deleted. Watch out for autocommands that
1524 * change curbuf! If that happens, allocate a new buffer anyway. */
1525 if (curbuf->b_p_bl)
1526 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1527 if (buf == curbuf)
1528 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1529# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001530 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 return NULL;
1532# endif
1533#endif
1534#ifdef FEAT_QUICKFIX
1535# ifdef FEAT_AUTOCMD
1536 if (buf == curbuf)
1537# endif
1538 {
1539 /* Make sure 'bufhidden' and 'buftype' are empty */
1540 clear_string_option(&buf->b_p_bh);
1541 clear_string_option(&buf->b_p_bt);
1542 }
1543#endif
1544 }
1545 if (buf != curbuf || curbuf == NULL)
1546 {
1547 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1548 if (buf == NULL)
1549 {
1550 vim_free(ffname);
1551 return NULL;
1552 }
1553 }
1554
1555 if (ffname != NULL)
1556 {
1557 buf->b_ffname = ffname;
1558 buf->b_sfname = vim_strsave(sfname);
1559 }
1560
1561 clear_wininfo(buf);
1562 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1563
1564 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1565 || buf->b_wininfo == NULL)
1566 {
1567 vim_free(buf->b_ffname);
1568 buf->b_ffname = NULL;
1569 vim_free(buf->b_sfname);
1570 buf->b_sfname = NULL;
1571 if (buf != curbuf)
1572 free_buffer(buf);
1573 return NULL;
1574 }
1575
1576 if (buf == curbuf)
1577 {
1578 /* free all things allocated for this buffer */
1579 buf_freeall(buf, FALSE, FALSE);
1580 if (buf != curbuf) /* autocommands deleted the buffer! */
1581 return NULL;
1582#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001583 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 return NULL;
1585#endif
1586 /* buf->b_nwindows = 0; why was this here? */
1587 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1588#ifdef FEAT_KEYMAP
1589 /* need to reload lmaps and set b:keymap_name */
1590 curbuf->b_kmap_state |= KEYMAP_INIT;
1591#endif
1592 }
1593 else
1594 {
1595 /*
1596 * put new buffer at the end of the buffer list
1597 */
1598 buf->b_next = NULL;
1599 if (firstbuf == NULL) /* buffer list is empty */
1600 {
1601 buf->b_prev = NULL;
1602 firstbuf = buf;
1603 }
1604 else /* append new buffer at end of list */
1605 {
1606 lastbuf->b_next = buf;
1607 buf->b_prev = lastbuf;
1608 }
1609 lastbuf = buf;
1610
1611 buf->b_fnum = top_file_num++;
1612 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1613 {
1614 EMSG(_("W14: Warning: List of file names overflow"));
1615 if (emsg_silent == 0)
1616 {
1617 out_flush();
1618 ui_delay(3000L, TRUE); /* make sure it is noticed */
1619 }
1620 top_file_num = 1;
1621 }
1622
1623 /*
1624 * Always copy the options from the current buffer.
1625 */
1626 buf_copy_options(buf, BCO_ALWAYS);
1627 }
1628
1629 buf->b_wininfo->wi_fpos.lnum = lnum;
1630 buf->b_wininfo->wi_win = curwin;
1631
1632#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001633 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1634#endif
1635#ifdef FEAT_SYN_HL
1636 hash_init(&buf->b_keywtab);
1637 hash_init(&buf->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638#endif
1639
1640 buf->b_fname = buf->b_sfname;
1641#ifdef UNIX
1642 if (st.st_dev == (dev_T)-1)
1643 buf->b_dev = -1;
1644 else
1645 {
1646 buf->b_dev = st.st_dev;
1647 buf->b_ino = st.st_ino;
1648 }
1649#endif
1650 buf->b_u_synced = TRUE;
1651 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001652 if (flags & BLN_DUMMY)
1653 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 buf_clear_file(buf);
1655 clrallmarks(buf); /* clear marks */
1656 fmarks_check_names(buf); /* check file marks for this file */
1657 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1658#ifdef FEAT_AUTOCMD
1659 if (!(flags & BLN_DUMMY))
1660 {
1661 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1662 if (flags & BLN_LISTED)
1663 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1664# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001665 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 return NULL;
1667# endif
1668 }
1669#endif
1670
1671 return buf;
1672}
1673
1674/*
1675 * Free the memory for the options of a buffer.
1676 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1677 * 'fileencoding'.
1678 */
1679 void
1680free_buf_options(buf, free_p_ff)
1681 buf_T *buf;
1682 int free_p_ff;
1683{
1684 if (free_p_ff)
1685 {
1686#ifdef FEAT_MBYTE
1687 clear_string_option(&buf->b_p_fenc);
1688#endif
1689 clear_string_option(&buf->b_p_ff);
1690#ifdef FEAT_QUICKFIX
1691 clear_string_option(&buf->b_p_bh);
1692 clear_string_option(&buf->b_p_bt);
1693#endif
1694 }
1695#ifdef FEAT_FIND_ID
1696 clear_string_option(&buf->b_p_def);
1697 clear_string_option(&buf->b_p_inc);
1698# ifdef FEAT_EVAL
1699 clear_string_option(&buf->b_p_inex);
1700# endif
1701#endif
1702#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1703 clear_string_option(&buf->b_p_inde);
1704 clear_string_option(&buf->b_p_indk);
1705#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001706#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1707 clear_string_option(&buf->b_p_bexpr);
1708#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001709#if defined(FEAT_EVAL)
1710 clear_string_option(&buf->b_p_fex);
1711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712#ifdef FEAT_CRYPT
1713 clear_string_option(&buf->b_p_key);
1714#endif
1715 clear_string_option(&buf->b_p_kp);
1716 clear_string_option(&buf->b_p_mps);
1717 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001718 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 clear_string_option(&buf->b_p_isk);
1720#ifdef FEAT_KEYMAP
1721 clear_string_option(&buf->b_p_keymap);
1722 ga_clear(&buf->b_kmap_ga);
1723#endif
1724#ifdef FEAT_COMMENTS
1725 clear_string_option(&buf->b_p_com);
1726#endif
1727#ifdef FEAT_FOLDING
1728 clear_string_option(&buf->b_p_cms);
1729#endif
1730 clear_string_option(&buf->b_p_nf);
1731#ifdef FEAT_SYN_HL
1732 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001733#endif
1734#ifdef FEAT_SPELL
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001735 clear_string_option(&buf->b_p_spc);
Bram Moolenaar86bc1fb2005-06-07 20:58:01 +00001736 clear_string_option(&buf->b_p_spf);
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001737 vim_free(buf->b_cap_prog);
1738 buf->b_cap_prog = NULL;
Bram Moolenaara0dea672005-03-20 22:23:10 +00001739 clear_string_option(&buf->b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740#endif
1741#ifdef FEAT_SEARCHPATH
1742 clear_string_option(&buf->b_p_sua);
1743#endif
1744#ifdef FEAT_AUTOCMD
1745 clear_string_option(&buf->b_p_ft);
1746#endif
1747#ifdef FEAT_OSFILETYPE
1748 clear_string_option(&buf->b_p_oft);
1749#endif
1750#ifdef FEAT_CINDENT
1751 clear_string_option(&buf->b_p_cink);
1752 clear_string_option(&buf->b_p_cino);
1753#endif
1754#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1755 clear_string_option(&buf->b_p_cinw);
1756#endif
1757#ifdef FEAT_INS_EXPAND
1758 clear_string_option(&buf->b_p_cpt);
1759#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001760#ifdef FEAT_COMPL_FUNC
1761 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001762 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764#ifdef FEAT_QUICKFIX
1765 clear_string_option(&buf->b_p_gp);
1766 clear_string_option(&buf->b_p_mp);
1767 clear_string_option(&buf->b_p_efm);
1768#endif
1769 clear_string_option(&buf->b_p_ep);
1770 clear_string_option(&buf->b_p_path);
1771 clear_string_option(&buf->b_p_tags);
1772#ifdef FEAT_INS_EXPAND
1773 clear_string_option(&buf->b_p_dict);
1774 clear_string_option(&buf->b_p_tsr);
1775#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001776#ifdef FEAT_TEXTOBJ
1777 clear_string_option(&buf->b_p_qe);
1778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 buf->b_p_ar = -1;
1780}
1781
1782/*
1783 * get alternate file n
1784 * set linenr to lnum or altfpos.lnum if lnum == 0
1785 * also set cursor column to altfpos.col if 'startofline' is not set.
1786 * if (options & GETF_SETMARK) call setpcmark()
1787 * if (options & GETF_ALT) we are jumping to an alternate file.
1788 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1789 *
1790 * return FAIL for failure, OK for success
1791 */
1792 int
1793buflist_getfile(n, lnum, options, forceit)
1794 int n;
1795 linenr_T lnum;
1796 int options;
1797 int forceit;
1798{
1799 buf_T *buf;
1800#ifdef FEAT_WINDOWS
1801 win_T *wp = NULL;
1802#endif
1803 pos_T *fpos;
1804 colnr_T col;
1805
1806 buf = buflist_findnr(n);
1807 if (buf == NULL)
1808 {
1809 if ((options & GETF_ALT) && n == 0)
1810 EMSG(_(e_noalt));
1811 else
1812 EMSGN(_("E92: Buffer %ld not found"), n);
1813 return FAIL;
1814 }
1815
1816 /* if alternate file is the current buffer, nothing to do */
1817 if (buf == curbuf)
1818 return OK;
1819
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001820 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001821 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001822 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001824 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001825#ifdef FEAT_AUTOCMD
1826 if (curbuf_locked())
1827 return FAIL;
1828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
1830 /* altfpos may be changed by getfile(), get it now */
1831 if (lnum == 0)
1832 {
1833 fpos = buflist_findfpos(buf);
1834 lnum = fpos->lnum;
1835 col = fpos->col;
1836 }
1837 else
1838 col = 0;
1839
1840#ifdef FEAT_WINDOWS
1841 if (options & GETF_SWITCH)
1842 {
1843 /* use existing open window for buffer if wanted */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001844 if (vim_strchr(p_swb, 'o')) /* useopen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001846 /* use existing open window in any tab page for buffer if wanted */
1847 if (vim_strchr(p_swb, 'a')) /* usetab */
1848 wp = buf_jump_open_tab(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 /* split window if wanted ("split") */
1850 if (wp == NULL && vim_strchr(p_swb, 't') && !bufempty())
1851 {
1852 if (win_split(0, 0) == FAIL)
1853 return FAIL;
1854# ifdef FEAT_SCROLLBIND
1855 curwin->w_p_scb = FALSE;
1856# endif
1857 }
1858 }
1859#endif
1860
1861 ++RedrawingDisabled;
1862 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1863 lnum, forceit) <= 0)
1864 {
1865 --RedrawingDisabled;
1866
1867 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1868 if (!p_sol && col != 0)
1869 {
1870 curwin->w_cursor.col = col;
1871 check_cursor_col();
1872#ifdef FEAT_VIRTUALEDIT
1873 curwin->w_cursor.coladd = 0;
1874#endif
1875 curwin->w_set_curswant = TRUE;
1876 }
1877 return OK;
1878 }
1879 --RedrawingDisabled;
1880 return FAIL;
1881}
1882
1883/*
1884 * go to the last know line number for the current buffer
1885 */
1886 void
1887buflist_getfpos()
1888{
1889 pos_T *fpos;
1890
1891 fpos = buflist_findfpos(curbuf);
1892
1893 curwin->w_cursor.lnum = fpos->lnum;
1894 check_cursor_lnum();
1895
1896 if (p_sol)
1897 curwin->w_cursor.col = 0;
1898 else
1899 {
1900 curwin->w_cursor.col = fpos->col;
1901 check_cursor_col();
1902#ifdef FEAT_VIRTUALEDIT
1903 curwin->w_cursor.coladd = 0;
1904#endif
1905 curwin->w_set_curswant = TRUE;
1906 }
1907}
1908
Bram Moolenaar81695252004-12-29 20:58:21 +00001909#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1910/*
1911 * Find file in buffer list by name (it has to be for the current window).
1912 * Returns NULL if not found.
1913 */
1914 buf_T *
1915buflist_findname_exp(fname)
1916 char_u *fname;
1917{
1918 char_u *ffname;
1919 buf_T *buf = NULL;
1920
1921 /* First make the name into a full path name */
1922 ffname = FullName_save(fname,
1923#ifdef UNIX
1924 TRUE /* force expansion, get rid of symbolic links */
1925#else
1926 FALSE
1927#endif
1928 );
1929 if (ffname != NULL)
1930 {
1931 buf = buflist_findname(ffname);
1932 vim_free(ffname);
1933 }
1934 return buf;
1935}
1936#endif
1937
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938/*
1939 * Find file in buffer list by name (it has to be for the current window).
1940 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00001941 * Skips dummy buffers.
1942 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 */
1944 buf_T *
1945buflist_findname(ffname)
1946 char_u *ffname;
1947{
1948#ifdef UNIX
1949 struct stat st;
1950
1951 if (mch_stat((char *)ffname, &st) < 0)
1952 st.st_dev = (dev_T)-1;
1953 return buflist_findname_stat(ffname, &st);
1954}
1955
1956/*
1957 * Same as buflist_findname(), but pass the stat structure to avoid getting it
1958 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00001959 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 */
1961 static buf_T *
1962buflist_findname_stat(ffname, stp)
1963 char_u *ffname;
1964 struct stat *stp;
1965{
1966#endif
1967 buf_T *buf;
1968
1969 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00001970 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971#ifdef UNIX
1972 , stp
1973#endif
1974 ))
1975 return buf;
1976 return NULL;
1977}
1978
1979#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
1980/*
1981 * Find file in buffer list by a regexp pattern.
1982 * Return fnum of the found buffer.
1983 * Return < 0 for error.
1984 */
1985/*ARGSUSED*/
1986 int
1987buflist_findpat(pattern, pattern_end, unlisted, diffmode)
1988 char_u *pattern;
1989 char_u *pattern_end; /* pointer to first char after pattern */
1990 int unlisted; /* find unlisted buffers */
1991 int diffmode; /* find diff-mode buffers only */
1992{
1993 buf_T *buf;
1994 regprog_T *prog;
1995 int match = -1;
1996 int find_listed;
1997 char_u *pat;
1998 char_u *patend;
1999 int attempt;
2000 char_u *p;
2001 int toggledollar;
2002
2003 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2004 {
2005 if (*pattern == '%')
2006 match = curbuf->b_fnum;
2007 else
2008 match = curwin->w_alt_fnum;
2009#ifdef FEAT_DIFF
2010 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2011 match = -1;
2012#endif
2013 }
2014
2015 /*
2016 * Try four ways of matching a listed buffer:
2017 * attempt == 0: without '^' or '$' (at any position)
2018 * attempt == 1: with '^' at start (only at postion 0)
2019 * attempt == 2: with '$' at end (only match at end)
2020 * attempt == 3: with '^' at start and '$' at end (only full match)
2021 * Repeat this for finding an unlisted buffer if there was no matching
2022 * listed buffer.
2023 */
2024 else
2025 {
2026 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2027 if (pat == NULL)
2028 return -1;
2029 patend = pat + STRLEN(pat) - 1;
2030 toggledollar = (patend > pat && *patend == '$');
2031
2032 /* First try finding a listed buffer. If not found and "unlisted"
2033 * is TRUE, try finding an unlisted buffer. */
2034 find_listed = TRUE;
2035 for (;;)
2036 {
2037 for (attempt = 0; attempt <= 3; ++attempt)
2038 {
2039 /* may add '^' and '$' */
2040 if (toggledollar)
2041 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2042 p = pat;
2043 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2044 ++p;
2045 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2046 if (prog == NULL)
2047 {
2048 vim_free(pat);
2049 return -1;
2050 }
2051
2052 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2053 if (buf->b_p_bl == find_listed
2054#ifdef FEAT_DIFF
2055 && (!diffmode || diff_mode_buf(buf))
2056#endif
2057 && buflist_match(prog, buf) != NULL)
2058 {
2059 if (match >= 0) /* already found a match */
2060 {
2061 match = -2;
2062 break;
2063 }
2064 match = buf->b_fnum; /* remember first match */
2065 }
2066
2067 vim_free(prog);
2068 if (match >= 0) /* found one match */
2069 break;
2070 }
2071
2072 /* Only search for unlisted buffers if there was no match with
2073 * a listed buffer. */
2074 if (!unlisted || !find_listed || match != -1)
2075 break;
2076 find_listed = FALSE;
2077 }
2078
2079 vim_free(pat);
2080 }
2081
2082 if (match == -2)
2083 EMSG2(_("E93: More than one match for %s"), pattern);
2084 else if (match < 0)
2085 EMSG2(_("E94: No matching buffer for %s"), pattern);
2086 return match;
2087}
2088#endif
2089
2090#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2091
2092/*
2093 * Find all buffer names that match.
2094 * For command line expansion of ":buf" and ":sbuf".
2095 * Return OK if matches found, FAIL otherwise.
2096 */
2097 int
2098ExpandBufnames(pat, num_file, file, options)
2099 char_u *pat;
2100 int *num_file;
2101 char_u ***file;
2102 int options;
2103{
2104 int count = 0;
2105 buf_T *buf;
2106 int round;
2107 char_u *p;
2108 int attempt;
2109 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002110 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111
2112 *num_file = 0; /* return values in case of FAIL */
2113 *file = NULL;
2114
Bram Moolenaar05159a02005-02-26 23:04:13 +00002115 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2116 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002118 patc = alloc((unsigned)STRLEN(pat) + 11);
2119 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002121 STRCPY(patc, "\\(^\\|[\\/]\\)");
2122 STRCPY(patc + 11, pat + 1);
2123 }
2124 else
2125 patc = pat;
2126
2127 /*
2128 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002129 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002130 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002131 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002132 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002133 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002134 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002135 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002136 if (prog == NULL)
2137 {
2138 if (patc != pat)
2139 vim_free(patc);
2140 return FAIL;
2141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
2143 /*
2144 * round == 1: Count the matches.
2145 * round == 2: Build the array to keep the matches.
2146 */
2147 for (round = 1; round <= 2; ++round)
2148 {
2149 count = 0;
2150 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2151 {
2152 if (!buf->b_p_bl) /* skip unlisted buffers */
2153 continue;
2154 p = buflist_match(prog, buf);
2155 if (p != NULL)
2156 {
2157 if (round == 1)
2158 ++count;
2159 else
2160 {
2161 if (options & WILD_HOME_REPLACE)
2162 p = home_replace_save(buf, p);
2163 else
2164 p = vim_strsave(p);
2165 (*file)[count++] = p;
2166 }
2167 }
2168 }
2169 if (count == 0) /* no match found, break here */
2170 break;
2171 if (round == 1)
2172 {
2173 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2174 if (*file == NULL)
2175 {
2176 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002177 if (patc != pat)
2178 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 return FAIL;
2180 }
2181 }
2182 }
2183 vim_free(prog);
2184 if (count) /* match(es) found, break here */
2185 break;
2186 }
2187
Bram Moolenaar05159a02005-02-26 23:04:13 +00002188 if (patc != pat)
2189 vim_free(patc);
2190
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 *num_file = count;
2192 return (count == 0 ? FAIL : OK);
2193}
2194
2195#endif /* FEAT_CMDL_COMPL */
2196
2197#ifdef HAVE_BUFLIST_MATCH
2198/*
2199 * Check for a match on the file name for buffer "buf" with regprog "prog".
2200 */
2201 static char_u *
2202buflist_match(prog, buf)
2203 regprog_T *prog;
2204 buf_T *buf;
2205{
2206 char_u *match;
2207
2208 /* First try the short file name, then the long file name. */
2209 match = fname_match(prog, buf->b_sfname);
2210 if (match == NULL)
2211 match = fname_match(prog, buf->b_ffname);
2212
2213 return match;
2214}
2215
2216/*
2217 * Try matching the regexp in "prog" with file name "name".
2218 * Return "name" when there is a match, NULL when not.
2219 */
2220 static char_u *
2221fname_match(prog, name)
2222 regprog_T *prog;
2223 char_u *name;
2224{
2225 char_u *match = NULL;
2226 char_u *p;
2227 regmatch_T regmatch;
2228
2229 if (name != NULL)
2230 {
2231 regmatch.regprog = prog;
2232#ifdef CASE_INSENSITIVE_FILENAME
2233 regmatch.rm_ic = TRUE; /* Always ignore case */
2234#else
2235 regmatch.rm_ic = FALSE; /* Never ignore case */
2236#endif
2237
2238 if (vim_regexec(&regmatch, name, (colnr_T)0))
2239 match = name;
2240 else
2241 {
2242 /* Replace $(HOME) with '~' and try matching again. */
2243 p = home_replace_save(NULL, name);
2244 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2245 match = name;
2246 vim_free(p);
2247 }
2248 }
2249
2250 return match;
2251}
2252#endif
2253
2254/*
2255 * find file in buffer list by number
2256 */
2257 buf_T *
2258buflist_findnr(nr)
2259 int nr;
2260{
2261 buf_T *buf;
2262
2263 if (nr == 0)
2264 nr = curwin->w_alt_fnum;
2265 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2266 if (buf->b_fnum == nr)
2267 return (buf);
2268 return NULL;
2269}
2270
2271/*
2272 * Get name of file 'n' in the buffer list.
2273 * When the file has no name an empty string is returned.
2274 * home_replace() is used to shorten the file name (used for marks).
2275 * Returns a pointer to allocated memory, of NULL when failed.
2276 */
2277 char_u *
2278buflist_nr2name(n, fullname, helptail)
2279 int n;
2280 int fullname;
2281 int helptail; /* for help buffers return tail only */
2282{
2283 buf_T *buf;
2284
2285 buf = buflist_findnr(n);
2286 if (buf == NULL)
2287 return NULL;
2288 return home_replace_save(helptail ? buf : NULL,
2289 fullname ? buf->b_ffname : buf->b_fname);
2290}
2291
2292/*
2293 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2294 * When "copy_options" is TRUE save the local window option values.
2295 * When "lnum" is 0 only do the options.
2296 */
2297 static void
2298buflist_setfpos(buf, win, lnum, col, copy_options)
2299 buf_T *buf;
2300 win_T *win;
2301 linenr_T lnum;
2302 colnr_T col;
2303 int copy_options;
2304{
2305 wininfo_T *wip;
2306
2307 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2308 if (wip->wi_win == win)
2309 break;
2310 if (wip == NULL)
2311 {
2312 /* allocate a new entry */
2313 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2314 if (wip == NULL)
2315 return;
2316 wip->wi_win = win;
2317 if (lnum == 0) /* set lnum even when it's 0 */
2318 lnum = 1;
2319 }
2320 else
2321 {
2322 /* remove the entry from the list */
2323 if (wip->wi_prev)
2324 wip->wi_prev->wi_next = wip->wi_next;
2325 else
2326 buf->b_wininfo = wip->wi_next;
2327 if (wip->wi_next)
2328 wip->wi_next->wi_prev = wip->wi_prev;
2329 if (copy_options && wip->wi_optset)
2330 {
2331 clear_winopt(&wip->wi_opt);
2332#ifdef FEAT_FOLDING
2333 deleteFoldRecurse(&wip->wi_folds);
2334#endif
2335 }
2336 }
2337 if (lnum != 0)
2338 {
2339 wip->wi_fpos.lnum = lnum;
2340 wip->wi_fpos.col = col;
2341 }
2342 if (copy_options)
2343 {
2344 /* Save the window-specific option values. */
2345 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2346#ifdef FEAT_FOLDING
2347 wip->wi_fold_manual = win->w_fold_manual;
2348 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2349#endif
2350 wip->wi_optset = TRUE;
2351 }
2352
2353 /* insert the entry in front of the list */
2354 wip->wi_next = buf->b_wininfo;
2355 buf->b_wininfo = wip;
2356 wip->wi_prev = NULL;
2357 if (wip->wi_next)
2358 wip->wi_next->wi_prev = wip;
2359
2360 return;
2361}
2362
2363/*
2364 * Find info for the current window in buffer "buf".
2365 * If not found, return the info for the most recently used window.
2366 * Returns NULL when there isn't any info.
2367 */
2368 static wininfo_T *
2369find_wininfo(buf)
2370 buf_T *buf;
2371{
2372 wininfo_T *wip;
2373
2374 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2375 if (wip->wi_win == curwin)
2376 break;
2377 if (wip == NULL) /* if no fpos for curwin, use the first in the list */
2378 wip = buf->b_wininfo;
2379 return wip;
2380}
2381
2382/*
2383 * Reset the local window options to the values last used in this window.
2384 * If the buffer wasn't used in this window before, use the values from
2385 * the most recently used window. If the values were never set, use the
2386 * global values for the window.
2387 */
2388 void
2389get_winopts(buf)
2390 buf_T *buf;
2391{
2392 wininfo_T *wip;
2393
2394 clear_winopt(&curwin->w_onebuf_opt);
2395#ifdef FEAT_FOLDING
2396 clearFolding(curwin);
2397#endif
2398
2399 wip = find_wininfo(buf);
2400 if (wip != NULL && wip->wi_optset)
2401 {
2402 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2403#ifdef FEAT_FOLDING
2404 curwin->w_fold_manual = wip->wi_fold_manual;
2405 curwin->w_foldinvalid = TRUE;
2406 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2407#endif
2408 }
2409 else
2410 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2411
2412#ifdef FEAT_FOLDING
2413 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2414 if (p_fdls >= 0)
2415 curwin->w_p_fdl = p_fdls;
2416#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002417
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002418#ifdef FEAT_SPELL
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002419 if (curwin->w_p_spell && *buf->b_p_spl != NUL)
2420 did_set_spelllang(buf);
2421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422}
2423
2424/*
2425 * Find the position (lnum and col) for the buffer 'buf' for the current
2426 * window.
2427 * Returns a pointer to no_position if no position is found.
2428 */
2429 pos_T *
2430buflist_findfpos(buf)
2431 buf_T *buf;
2432{
2433 wininfo_T *wip;
2434 static pos_T no_position = {1, 0};
2435
2436 wip = find_wininfo(buf);
2437 if (wip != NULL)
2438 return &(wip->wi_fpos);
2439 else
2440 return &no_position;
2441}
2442
2443/*
2444 * Find the lnum for the buffer 'buf' for the current window.
2445 */
2446 linenr_T
2447buflist_findlnum(buf)
2448 buf_T *buf;
2449{
2450 return buflist_findfpos(buf)->lnum;
2451}
2452
2453#if defined(FEAT_LISTCMDS) || defined(PROTO)
2454/*
2455 * List all know file names (for :files and :buffers command).
2456 */
2457/*ARGSUSED*/
2458 void
2459buflist_list(eap)
2460 exarg_T *eap;
2461{
2462 buf_T *buf;
2463 int len;
2464 int i;
2465
2466 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2467 {
2468 /* skip unlisted buffers, unless ! was used */
2469 if (!buf->b_p_bl && !eap->forceit)
2470 continue;
2471 msg_putchar('\n');
2472 if (buf_spname(buf) != NULL)
2473 STRCPY(NameBuff, buf_spname(buf));
2474 else
2475 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2476
Bram Moolenaar50cde822005-06-05 21:54:54 +00002477 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 buf->b_fnum,
2479 buf->b_p_bl ? ' ' : 'u',
2480 buf == curbuf ? '%' :
2481 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2482 buf->b_ml.ml_mfp == NULL ? ' ' :
2483 (buf->b_nwindows == 0 ? 'h' : 'a'),
2484 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2485 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002486 : (bufIsChanged(buf) ? '+' : ' '),
2487 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
2489 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 i = 40 - vim_strsize(IObuff);
2491 do
2492 {
2493 IObuff[len++] = ' ';
2494 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002495 vim_snprintf((char *)IObuff + len, IOSIZE - len, _("line %ld"),
2496 buf == curbuf ? curwin->w_cursor.lnum
2497 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 msg_outtrans(IObuff);
2499 out_flush(); /* output one line at a time */
2500 ui_breakcheck();
2501 }
2502}
2503#endif
2504
2505/*
2506 * Get file name and line number for file 'fnum'.
2507 * Used by DoOneCmd() for translating '%' and '#'.
2508 * Used by insert_reg() and cmdline_paste() for '#' register.
2509 * Return FAIL if not found, OK for success.
2510 */
2511 int
2512buflist_name_nr(fnum, fname, lnum)
2513 int fnum;
2514 char_u **fname;
2515 linenr_T *lnum;
2516{
2517 buf_T *buf;
2518
2519 buf = buflist_findnr(fnum);
2520 if (buf == NULL || buf->b_fname == NULL)
2521 return FAIL;
2522
2523 *fname = buf->b_fname;
2524 *lnum = buflist_findlnum(buf);
2525
2526 return OK;
2527}
2528
2529/*
2530 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2531 * The file name with the full path is also remembered, for when :cd is used.
2532 * Returns FAIL for failure (file name already in use by other buffer)
2533 * OK otherwise.
2534 */
2535 int
2536setfname(buf, ffname, sfname, message)
2537 buf_T *buf;
2538 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002539 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
Bram Moolenaar81695252004-12-29 20:58:21 +00002541 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542#ifdef UNIX
2543 struct stat st;
2544#endif
2545
2546 if (ffname == NULL || *ffname == NUL)
2547 {
2548 /* Removing the name. */
2549 vim_free(buf->b_ffname);
2550 vim_free(buf->b_sfname);
2551 buf->b_ffname = NULL;
2552 buf->b_sfname = NULL;
2553#ifdef UNIX
2554 st.st_dev = (dev_T)-1;
2555#endif
2556 }
2557 else
2558 {
2559 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2560 if (ffname == NULL) /* out of memory */
2561 return FAIL;
2562
2563 /*
2564 * if the file name is already used in another buffer:
2565 * - if the buffer is loaded, fail
2566 * - if the buffer is not loaded, delete it from the list
2567 */
2568#ifdef UNIX
2569 if (mch_stat((char *)ffname, &st) < 0)
2570 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002571#endif
2572 if (!(buf->b_flags & BF_DUMMY))
2573#ifdef UNIX
2574 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002576 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577#endif
2578 if (obuf != NULL && obuf != buf)
2579 {
2580 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2581 {
2582 if (message)
2583 EMSG(_("E95: Buffer with this name already exists"));
2584 vim_free(ffname);
2585 return FAIL;
2586 }
2587 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2588 }
2589 sfname = vim_strsave(sfname);
2590 if (ffname == NULL || sfname == NULL)
2591 {
2592 vim_free(sfname);
2593 vim_free(ffname);
2594 return FAIL;
2595 }
2596#ifdef USE_FNAME_CASE
2597# ifdef USE_LONG_FNAME
2598 if (USE_LONG_FNAME)
2599# endif
2600 fname_case(sfname, 0); /* set correct case for short file name */
2601#endif
2602 vim_free(buf->b_ffname);
2603 vim_free(buf->b_sfname);
2604 buf->b_ffname = ffname;
2605 buf->b_sfname = sfname;
2606 }
2607 buf->b_fname = buf->b_sfname;
2608#ifdef UNIX
2609 if (st.st_dev == (dev_T)-1)
2610 buf->b_dev = -1;
2611 else
2612 {
2613 buf->b_dev = st.st_dev;
2614 buf->b_ino = st.st_ino;
2615 }
2616#endif
2617
2618#ifndef SHORT_FNAME
2619 buf->b_shortname = FALSE;
2620#endif
2621
2622 buf_name_changed(buf);
2623 return OK;
2624}
2625
2626/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002627 * Crude way of changing the name of a buffer. Use with care!
2628 * The name should be relative to the current directory.
2629 */
2630 void
2631buf_set_name(fnum, name)
2632 int fnum;
2633 char_u *name;
2634{
2635 buf_T *buf;
2636
2637 buf = buflist_findnr(fnum);
2638 if (buf != NULL)
2639 {
2640 vim_free(buf->b_sfname);
2641 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002642 buf->b_ffname = vim_strsave(name);
2643 buf->b_sfname = NULL;
2644 /* Allocate ffname and expand into full path. Also resolves .lnk
2645 * files on Win32. */
2646 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002647 buf->b_fname = buf->b_sfname;
2648 }
2649}
2650
2651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 * Take care of what needs to be done when the name of buffer "buf" has
2653 * changed.
2654 */
2655 void
2656buf_name_changed(buf)
2657 buf_T *buf;
2658{
2659 /*
2660 * If the file name changed, also change the name of the swapfile
2661 */
2662 if (buf->b_ml.ml_mfp != NULL)
2663 ml_setname(buf);
2664
2665 if (curwin->w_buffer == buf)
2666 check_arg_idx(curwin); /* check file name for arg list */
2667#ifdef FEAT_TITLE
2668 maketitle(); /* set window title */
2669#endif
2670#ifdef FEAT_WINDOWS
2671 status_redraw_all(); /* status lines need to be redrawn */
2672#endif
2673 fmarks_check_names(buf); /* check named file marks */
2674 ml_timestamp(buf); /* reset timestamp */
2675}
2676
2677/*
2678 * set alternate file name for current window
2679 *
2680 * Used by do_one_cmd(), do_write() and do_ecmd().
2681 * Return the buffer.
2682 */
2683 buf_T *
2684setaltfname(ffname, sfname, lnum)
2685 char_u *ffname;
2686 char_u *sfname;
2687 linenr_T lnum;
2688{
2689 buf_T *buf;
2690
2691 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2692 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002693 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 curwin->w_alt_fnum = buf->b_fnum;
2695 return buf;
2696}
2697
2698/*
2699 * Get alternate file name for current window.
2700 * Return NULL if there isn't any, and give error message if requested.
2701 */
2702 char_u *
2703getaltfname(errmsg)
2704 int errmsg; /* give error message */
2705{
2706 char_u *fname;
2707 linenr_T dummy;
2708
2709 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2710 {
2711 if (errmsg)
2712 EMSG(_(e_noalt));
2713 return NULL;
2714 }
2715 return fname;
2716}
2717
2718/*
2719 * Add a file name to the buflist and return its number.
2720 * Uses same flags as buflist_new(), except BLN_DUMMY.
2721 *
2722 * used by qf_init(), main() and doarglist()
2723 */
2724 int
2725buflist_add(fname, flags)
2726 char_u *fname;
2727 int flags;
2728{
2729 buf_T *buf;
2730
2731 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2732 if (buf != NULL)
2733 return buf->b_fnum;
2734 return 0;
2735}
2736
2737#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2738/*
2739 * Adjust slashes in file names. Called after 'shellslash' was set.
2740 */
2741 void
2742buflist_slash_adjust()
2743{
2744 buf_T *bp;
2745
2746 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2747 {
2748 if (bp->b_ffname != NULL)
2749 slash_adjust(bp->b_ffname);
2750 if (bp->b_sfname != NULL)
2751 slash_adjust(bp->b_sfname);
2752 }
2753}
2754#endif
2755
2756/*
2757 * Set alternate cursor position for current window.
2758 * Also save the local window option values.
2759 */
2760 void
2761buflist_altfpos()
2762{
2763 buflist_setfpos(curbuf, curwin, curwin->w_cursor.lnum,
2764 curwin->w_cursor.col, TRUE);
2765}
2766
2767/*
2768 * Return TRUE if 'ffname' is not the same file as current file.
2769 * Fname must have a full path (expanded by mch_FullName()).
2770 */
2771 int
2772otherfile(ffname)
2773 char_u *ffname;
2774{
2775 return otherfile_buf(curbuf, ffname
2776#ifdef UNIX
2777 , NULL
2778#endif
2779 );
2780}
2781
2782 static int
2783otherfile_buf(buf, ffname
2784#ifdef UNIX
2785 , stp
2786#endif
2787 )
2788 buf_T *buf;
2789 char_u *ffname;
2790#ifdef UNIX
2791 struct stat *stp;
2792#endif
2793{
2794 /* no name is different */
2795 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2796 return TRUE;
2797 if (fnamecmp(ffname, buf->b_ffname) == 0)
2798 return FALSE;
2799#ifdef UNIX
2800 {
2801 struct stat st;
2802
2803 /* If no struct stat given, get it now */
2804 if (stp == NULL)
2805 {
2806 if (buf->b_dev < 0 || mch_stat((char *)ffname, &st) < 0)
2807 st.st_dev = (dev_T)-1;
2808 stp = &st;
2809 }
2810 /* Use dev/ino to check if the files are the same, even when the names
2811 * are different (possible with links). Still need to compare the
2812 * name above, for when the file doesn't exist yet.
2813 * Problem: The dev/ino changes when a file is deleted (and created
2814 * again) and remains the same when renamed/moved. We don't want to
2815 * mch_stat() each buffer each time, that would be too slow. Get the
2816 * dev/ino again when they appear to match, but not when they appear
2817 * to be different: Could skip a buffer when it's actually the same
2818 * file. */
2819 if (buf_same_ino(buf, stp))
2820 {
2821 buf_setino(buf);
2822 if (buf_same_ino(buf, stp))
2823 return FALSE;
2824 }
2825 }
2826#endif
2827 return TRUE;
2828}
2829
2830#if defined(UNIX) || defined(PROTO)
2831/*
2832 * Set inode and device number for a buffer.
2833 * Must always be called when b_fname is changed!.
2834 */
2835 void
2836buf_setino(buf)
2837 buf_T *buf;
2838{
2839 struct stat st;
2840
2841 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2842 {
2843 buf->b_dev = st.st_dev;
2844 buf->b_ino = st.st_ino;
2845 }
2846 else
2847 buf->b_dev = -1;
2848}
2849
2850/*
2851 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2852 */
2853 static int
2854buf_same_ino(buf, stp)
2855 buf_T *buf;
2856 struct stat *stp;
2857{
2858 return (buf->b_dev >= 0
2859 && stp->st_dev == buf->b_dev
2860 && stp->st_ino == buf->b_ino);
2861}
2862#endif
2863
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002864/*
2865 * Print info about the current buffer.
2866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 void
2868fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002869 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 int shorthelp;
2871 int dont_truncate;
2872{
2873 char_u *name;
2874 int n;
2875 char_u *p;
2876 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002877 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878
2879 buffer = alloc(IOSIZE);
2880 if (buffer == NULL)
2881 return;
2882
2883 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2884 {
2885 sprintf((char *)buffer, "buf %d: ", curbuf->b_fnum);
2886 p = buffer + STRLEN(buffer);
2887 }
2888 else
2889 p = buffer;
2890
2891 *p++ = '"';
2892 if (buf_spname(curbuf) != NULL)
2893 STRCPY(p, buf_spname(curbuf));
2894 else
2895 {
2896 if (!fullname && curbuf->b_fname != NULL)
2897 name = curbuf->b_fname;
2898 else
2899 name = curbuf->b_ffname;
2900 home_replace(shorthelp ? curbuf : NULL, name, p,
2901 (int)(IOSIZE - (p - buffer)), TRUE);
2902 }
2903
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002904 len = STRLEN(buffer);
2905 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 "\"%s%s%s%s%s%s",
2907 curbufIsChanged() ? (shortmess(SHM_MOD)
2908 ? " [+]" : _(" [Modified]")) : " ",
2909 (curbuf->b_flags & BF_NOTEDITED)
2910#ifdef FEAT_QUICKFIX
2911 && !bt_dontwrite(curbuf)
2912#endif
2913 ? _("[Not edited]") : "",
2914 (curbuf->b_flags & BF_NEW)
2915#ifdef FEAT_QUICKFIX
2916 && !bt_dontwrite(curbuf)
2917#endif
2918 ? _("[New file]") : "",
2919 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
2920 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
2921 : _("[readonly]")) : "",
2922 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
2923 || curbuf->b_p_ro) ?
2924 " " : "");
2925 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
2926 * causes an overflow, thus for large numbers divide instead. */
2927 if (curwin->w_cursor.lnum > 1000000L)
2928 n = (int)(((long)curwin->w_cursor.lnum) /
2929 ((long)curbuf->b_ml.ml_line_count / 100L));
2930 else
2931 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
2932 (long)curbuf->b_ml.ml_line_count);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002933 len = STRLEN(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2935 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002936 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 }
2938#ifdef FEAT_CMDL_INFO
2939 else if (p_ru)
2940 {
2941 /* Current line and column are already on the screen -- webb */
2942 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002943 vim_snprintf((char *)buffer + len, IOSIZE - len,
2944 _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002946 vim_snprintf((char *)buffer + len, IOSIZE - len,
2947 _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 (long)curbuf->b_ml.ml_line_count, n);
2949 }
2950#endif
2951 else
2952 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002953 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 _("line %ld of %ld --%d%%-- col "),
2955 (long)curwin->w_cursor.lnum,
2956 (long)curbuf->b_ml.ml_line_count,
2957 n);
2958 validate_virtcol();
2959 col_print(buffer + STRLEN(buffer),
2960 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
2961 }
2962
2963 (void)append_arg_number(curwin, buffer, !shortmess(SHM_FILE), IOSIZE);
2964
2965 if (dont_truncate)
2966 {
2967 /* Temporarily set msg_scroll to avoid the message being truncated.
2968 * First call msg_start() to get the message in the right place. */
2969 msg_start();
2970 n = msg_scroll;
2971 msg_scroll = TRUE;
2972 msg(buffer);
2973 msg_scroll = n;
2974 }
2975 else
2976 {
2977 p = msg_trunc_attr(buffer, FALSE, 0);
2978 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 /* Need to repeat the message after redrawing when:
2980 * - When restart_edit is set (otherwise there will be a delay
2981 * before redrawing).
2982 * - When the screen was scrolled but there is no wait-return
2983 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00002984 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 }
2986
2987 vim_free(buffer);
2988}
2989
2990 void
2991col_print(buf, col, vcol)
2992 char_u *buf;
2993 int col;
2994 int vcol;
2995{
2996 if (col == vcol)
2997 sprintf((char *)buf, "%d", col);
2998 else
2999 sprintf((char *)buf, "%d-%d", col, vcol);
3000}
3001
3002#if defined(FEAT_TITLE) || defined(PROTO)
3003/*
3004 * put file name in title bar of window and in icon title
3005 */
3006
3007static char_u *lasttitle = NULL;
3008static char_u *lasticon = NULL;
3009
3010 void
3011maketitle()
3012{
3013 char_u *p;
3014 char_u *t_str = NULL;
3015 char_u *i_name;
3016 char_u *i_str = NULL;
3017 int maxlen = 0;
3018 int len;
3019 int mustset;
3020 char_u buf[IOSIZE];
3021 int off;
3022
3023 if (!redrawing())
3024 {
3025 /* Postpone updating the title when 'lazyredraw' is set. */
3026 need_maketitle = TRUE;
3027 return;
3028 }
3029
3030 need_maketitle = FALSE;
3031 if (!p_title && !p_icon)
3032 return;
3033
3034 if (p_title)
3035 {
3036 if (p_titlelen > 0)
3037 {
3038 maxlen = p_titlelen * Columns / 100;
3039 if (maxlen < 10)
3040 maxlen = 10;
3041 }
3042
3043 t_str = buf;
3044 if (*p_titlestring != NUL)
3045 {
3046#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003047 if (stl_syntax & STL_IN_TITLE)
3048 {
3049 int use_sandbox = FALSE;
3050 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003051
3052# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003053 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003054# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003055 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003057 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003058 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003059 if (called_emsg)
3060 set_string_option_direct((char_u *)"titlestring", -1,
3061 (char_u *)"", OPT_FREE, SID_ERROR);
3062 called_emsg |= save_called_emsg;
3063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 else
3065#endif
3066 t_str = p_titlestring;
3067 }
3068 else
3069 {
3070 /* format: "fname + (path) (1 of 2) - VIM" */
3071
3072 if (curbuf->b_fname == NULL)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003073 STRCPY(buf, _("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 else
3075 {
3076 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003077 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 }
3080
3081 switch (bufIsChanged(curbuf)
3082 + (curbuf->b_p_ro * 2)
3083 + (!curbuf->b_p_ma * 4))
3084 {
3085 case 1: STRCAT(buf, " +"); break;
3086 case 2: STRCAT(buf, " ="); break;
3087 case 3: STRCAT(buf, " =+"); break;
3088 case 4:
3089 case 6: STRCAT(buf, " -"); break;
3090 case 5:
3091 case 7: STRCAT(buf, " -+"); break;
3092 }
3093
3094 if (curbuf->b_fname != NULL)
3095 {
3096 /* Get path of file, replace home dir with ~ */
3097 off = (int)STRLEN(buf);
3098 buf[off++] = ' ';
3099 buf[off++] = '(';
3100 home_replace(curbuf, curbuf->b_ffname,
3101 buf + off, IOSIZE - off, TRUE);
3102#ifdef BACKSLASH_IN_FILENAME
3103 /* avoid "c:/name" to be reduced to "c" */
3104 if (isalpha(buf[off]) && buf[off + 1] == ':')
3105 off += 2;
3106#endif
3107 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003108 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003111 vim_strncpy(buf + off, (char_u *)_("help"),
3112 IOSIZE - off - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003115
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 /* translate unprintable chars */
3117 p = transstr(buf + off);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003118 vim_strncpy(buf + off, p, IOSIZE - off - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 STRCAT(buf, ")");
3121 }
3122
3123 append_arg_number(curwin, buf, FALSE, IOSIZE);
3124
3125#if defined(FEAT_CLIENTSERVER)
3126 if (serverName != NULL)
3127 {
3128 STRCAT(buf, " - ");
3129 STRCAT(buf, serverName);
3130 }
3131 else
3132#endif
3133 STRCAT(buf, " - VIM");
3134
3135 if (maxlen > 0)
3136 {
3137 /* make it shorter by removing a bit in the middle */
3138 len = vim_strsize(buf);
3139 if (len > maxlen)
3140 trunc_string(buf, buf, maxlen);
3141 }
3142 }
3143 }
3144 mustset = ti_change(t_str, &lasttitle);
3145
3146 if (p_icon)
3147 {
3148 i_str = buf;
3149 if (*p_iconstring != NUL)
3150 {
3151#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003152 if (stl_syntax & STL_IN_ICON)
3153 {
3154 int use_sandbox = FALSE;
3155 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003156
3157# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003158 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003159# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003160 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003162 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003163 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003164 if (called_emsg)
3165 set_string_option_direct((char_u *)"iconstring", -1,
3166 (char_u *)"", OPT_FREE, SID_ERROR);
3167 called_emsg |= save_called_emsg;
3168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 else
3170#endif
3171 i_str = p_iconstring;
3172 }
3173 else
3174 {
3175 if (buf_spname(curbuf) != NULL)
3176 i_name = (char_u *)buf_spname(curbuf);
3177 else /* use file name only in icon */
3178 i_name = gettail(curbuf->b_ffname);
3179 *i_str = NUL;
3180 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003181 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 if (len > 100)
3183 {
3184 len -= 100;
3185#ifdef FEAT_MBYTE
3186 if (has_mbyte)
3187 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3188#endif
3189 i_name += len;
3190 }
3191 STRCPY(i_str, i_name);
3192 trans_characters(i_str, IOSIZE);
3193 }
3194 }
3195
3196 mustset |= ti_change(i_str, &lasticon);
3197
3198 if (mustset)
3199 resettitle();
3200}
3201
3202/*
3203 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3204 * from "str" if it does.
3205 * Return TRUE when "*last" changed.
3206 */
3207 static int
3208ti_change(str, last)
3209 char_u *str;
3210 char_u **last;
3211{
3212 if ((str == NULL) != (*last == NULL)
3213 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3214 {
3215 vim_free(*last);
3216 if (str == NULL)
3217 *last = NULL;
3218 else
3219 *last = vim_strsave(str);
3220 return TRUE;
3221 }
3222 return FALSE;
3223}
3224
3225/*
3226 * Put current window title back (used after calling a shell)
3227 */
3228 void
3229resettitle()
3230{
3231 mch_settitle(lasttitle, lasticon);
3232}
Bram Moolenaarea408852005-06-25 22:49:46 +00003233
3234# if defined(EXITFREE) || defined(PROTO)
3235 void
3236free_titles()
3237{
3238 vim_free(lasttitle);
3239 vim_free(lasticon);
3240}
3241# endif
3242
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243#endif /* FEAT_TITLE */
3244
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003245#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003247 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 * Return length of string in screen cells.
3249 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003250 * Normally works for window "wp", except when working for 'tabline' then it
3251 * is "curwin".
3252 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 * Items are drawn interspersed with the text that surrounds it
3254 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3255 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3256 *
3257 * If maxwidth is not zero, the string will be filled at any middle marker
3258 * or truncated if too long, fillchar is used for all whitespace.
3259 */
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003260/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003262build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003264 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 size_t outlen; /* length of out[] */
3266 char_u *fmt;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003267 int use_sandbox; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 int fillchar;
3269 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003270 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3271 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
3273 char_u *p;
3274 char_u *s;
3275 char_u *t;
3276 char_u *linecont;
3277#ifdef FEAT_EVAL
3278 win_T *o_curwin;
3279 buf_T *o_curbuf;
3280#endif
3281 int empty_line;
3282 colnr_T virtcol;
3283 long l;
3284 long n;
3285 int prevchar_isflag;
3286 int prevchar_isitem;
3287 int itemisflag;
3288 int fillable;
3289 char_u *str;
3290 long num;
3291 int width;
3292 int itemcnt;
3293 int curitem;
3294 int groupitem[STL_MAX_ITEM];
3295 int groupdepth;
3296 struct stl_item
3297 {
3298 char_u *start;
3299 int minwid;
3300 int maxwid;
3301 enum
3302 {
3303 Normal,
3304 Empty,
3305 Group,
3306 Middle,
3307 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003308 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 Trunc
3310 } type;
3311 } item[STL_MAX_ITEM];
3312 int minwid;
3313 int maxwid;
3314 int zeropad;
3315 char_u base;
3316 char_u opt;
3317#define TMPLEN 70
3318 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003319 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003320 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003321
3322#ifdef FEAT_EVAL
3323 /*
3324 * When the format starts with "%!" then evaluate it as an expression and
3325 * use the result as the actual format string.
3326 */
3327 if (fmt[0] == '%' && fmt[1] == '!')
3328 {
3329 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3330 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003331 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003332 }
3333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334
3335 if (fillchar == 0)
3336 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003337#ifdef FEAT_MBYTE
3338 /* Can't handle a multi-byte fill character yet. */
3339 else if (mb_char2len(fillchar) > 1)
3340 fillchar = '-';
3341#endif
3342
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 /*
3344 * Get line & check if empty (cursorpos will show "0-1").
3345 * If inversion is possible we use it. Else '=' characters are used.
3346 */
3347 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3348 empty_line = (*linecont == NUL);
3349
3350 groupdepth = 0;
3351 p = out;
3352 curitem = 0;
3353 prevchar_isflag = TRUE;
3354 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003355 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
3357 if (*s != NUL && *s != '%')
3358 prevchar_isflag = prevchar_isitem = FALSE;
3359
3360 /*
3361 * Handle up to the next '%' or the end.
3362 */
3363 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3364 *p++ = *s++;
3365 if (*s == NUL || p + 1 >= out + outlen)
3366 break;
3367
3368 /*
3369 * Handle one '%' item.
3370 */
3371 s++;
3372 if (*s == '%')
3373 {
3374 if (p + 1 >= out + outlen)
3375 break;
3376 *p++ = *s++;
3377 prevchar_isflag = prevchar_isitem = FALSE;
3378 continue;
3379 }
3380 if (*s == STL_MIDDLEMARK)
3381 {
3382 s++;
3383 if (groupdepth > 0)
3384 continue;
3385 item[curitem].type = Middle;
3386 item[curitem++].start = p;
3387 continue;
3388 }
3389 if (*s == STL_TRUNCMARK)
3390 {
3391 s++;
3392 item[curitem].type = Trunc;
3393 item[curitem++].start = p;
3394 continue;
3395 }
3396 if (*s == ')')
3397 {
3398 s++;
3399 if (groupdepth < 1)
3400 continue;
3401 groupdepth--;
3402
3403 t = item[groupitem[groupdepth]].start;
3404 *p = NUL;
3405 l = vim_strsize(t);
3406 if (curitem > groupitem[groupdepth] + 1
3407 && item[groupitem[groupdepth]].minwid == 0)
3408 {
3409 /* remove group if all items are empty */
3410 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3411 if (item[n].type == Normal)
3412 break;
3413 if (n == curitem)
3414 {
3415 p = t;
3416 l = 0;
3417 }
3418 }
3419 if (l > item[groupitem[groupdepth]].maxwid)
3420 {
3421 /* truncate, remove n bytes of text at the start */
3422#ifdef FEAT_MBYTE
3423 if (has_mbyte)
3424 {
3425 /* Find the first character that should be included. */
3426 n = 0;
3427 while (l >= item[groupitem[groupdepth]].maxwid)
3428 {
3429 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003430 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 }
3432 }
3433 else
3434#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003435 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
3437 *t = '<';
3438 mch_memmove(t + 1, t + n, p - (t + n));
3439 p = p - n + 1;
3440#ifdef FEAT_MBYTE
3441 /* Fill up space left over by half a double-wide char. */
3442 while (++l < item[groupitem[groupdepth]].minwid)
3443 *p++ = fillchar;
3444#endif
3445
3446 /* correct the start of the items for the truncation */
3447 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3448 {
3449 item[l].start -= n;
3450 if (item[l].start < t)
3451 item[l].start = t;
3452 }
3453 }
3454 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3455 {
3456 /* fill */
3457 n = item[groupitem[groupdepth]].minwid;
3458 if (n < 0)
3459 {
3460 /* fill by appending characters */
3461 n = 0 - n;
3462 while (l++ < n && p + 1 < out + outlen)
3463 *p++ = fillchar;
3464 }
3465 else
3466 {
3467 /* fill by inserting characters */
3468 mch_memmove(t + n - l, t, p - t);
3469 l = n - l;
3470 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003471 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 p += l;
3473 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3474 item[n].start += l;
3475 for ( ; l > 0; l--)
3476 *t++ = fillchar;
3477 }
3478 }
3479 continue;
3480 }
3481 minwid = 0;
3482 maxwid = 9999;
3483 zeropad = FALSE;
3484 l = 1;
3485 if (*s == '0')
3486 {
3487 s++;
3488 zeropad = TRUE;
3489 }
3490 if (*s == '-')
3491 {
3492 s++;
3493 l = -1;
3494 }
3495 if (VIM_ISDIGIT(*s))
3496 {
3497 minwid = (int)getdigits(&s);
3498 if (minwid < 0) /* overflow */
3499 minwid = 0;
3500 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003501 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
3503 item[curitem].type = Highlight;
3504 item[curitem].start = p;
3505 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3506 s++;
3507 curitem++;
3508 continue;
3509 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003510 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3511 {
3512 if (*s == STL_TABCLOSENR)
3513 {
3514 if (minwid == 0)
3515 {
3516 /* %X ends the close label, go back to the previously
3517 * define tab label nr. */
3518 for (n = curitem - 1; n >= 0; --n)
3519 if (item[n].type == TabPage && item[n].minwid >= 0)
3520 {
3521 minwid = item[n].minwid;
3522 break;
3523 }
3524 }
3525 else
3526 /* close nrs are stored as negative values */
3527 minwid = - minwid;
3528 }
3529 item[curitem].type = TabPage;
3530 item[curitem].start = p;
3531 item[curitem].minwid = minwid;
3532 s++;
3533 curitem++;
3534 continue;
3535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 if (*s == '.')
3537 {
3538 s++;
3539 if (VIM_ISDIGIT(*s))
3540 {
3541 maxwid = (int)getdigits(&s);
3542 if (maxwid <= 0) /* overflow */
3543 maxwid = 50;
3544 }
3545 }
3546 minwid = (minwid > 50 ? 50 : minwid) * l;
3547 if (*s == '(')
3548 {
3549 groupitem[groupdepth++] = curitem;
3550 item[curitem].type = Group;
3551 item[curitem].start = p;
3552 item[curitem].minwid = minwid;
3553 item[curitem].maxwid = maxwid;
3554 s++;
3555 curitem++;
3556 continue;
3557 }
3558 if (vim_strchr(STL_ALL, *s) == NULL)
3559 {
3560 s++;
3561 continue;
3562 }
3563 opt = *s++;
3564
3565 /* OK - now for the real work */
3566 base = 'D';
3567 itemisflag = FALSE;
3568 fillable = TRUE;
3569 num = -1;
3570 str = NULL;
3571 switch (opt)
3572 {
3573 case STL_FILEPATH:
3574 case STL_FULLPATH:
3575 case STL_FILENAME:
3576 fillable = FALSE; /* don't change ' ' to fillchar */
3577 if (buf_spname(wp->w_buffer) != NULL)
3578 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3579 else
3580 {
3581 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003582 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3584 }
3585 trans_characters(NameBuff, MAXPATHL);
3586 if (opt != STL_FILENAME)
3587 str = NameBuff;
3588 else
3589 str = gettail(NameBuff);
3590 break;
3591
3592 case STL_VIM_EXPR: /* '{' */
3593 itemisflag = TRUE;
3594 t = p;
3595 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3596 *p++ = *s++;
3597 if (*s != '}') /* missing '}' or out of space */
3598 break;
3599 s++;
3600 *p = 0;
3601 p = t;
3602
3603#ifdef FEAT_EVAL
3604 sprintf((char *)tmp, "%d", curbuf->b_fnum);
3605 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3606
3607 o_curbuf = curbuf;
3608 o_curwin = curwin;
3609 curwin = wp;
3610 curbuf = wp->w_buffer;
3611
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003612 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614 curwin = o_curwin;
3615 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003616 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
3618 if (str != NULL && *str != 0)
3619 {
3620 if (*skipdigits(str) == NUL)
3621 {
3622 num = atoi((char *)str);
3623 vim_free(str);
3624 str = NULL;
3625 itemisflag = FALSE;
3626 }
3627 }
3628#endif
3629 break;
3630
3631 case STL_LINE:
3632 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3633 ? 0L : (long)(wp->w_cursor.lnum);
3634 break;
3635
3636 case STL_NUMLINES:
3637 num = wp->w_buffer->b_ml.ml_line_count;
3638 break;
3639
3640 case STL_COLUMN:
3641 num = !(State & INSERT) && empty_line
3642 ? 0 : (int)wp->w_cursor.col + 1;
3643 break;
3644
3645 case STL_VIRTCOL:
3646 case STL_VIRTCOL_ALT:
3647 /* In list mode virtcol needs to be recomputed */
3648 virtcol = wp->w_virtcol;
3649 if (wp->w_p_list && lcs_tab1 == NUL)
3650 {
3651 wp->w_p_list = FALSE;
3652 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3653 wp->w_p_list = TRUE;
3654 }
3655 ++virtcol;
3656 /* Don't display %V if it's the same as %c. */
3657 if (opt == STL_VIRTCOL_ALT
3658 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3659 ? 0 : (int)wp->w_cursor.col + 1)))
3660 break;
3661 num = (long)virtcol;
3662 break;
3663
3664 case STL_PERCENTAGE:
3665 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3666 (long)wp->w_buffer->b_ml.ml_line_count);
3667 break;
3668
3669 case STL_ALTPERCENT:
3670 str = tmp;
3671 get_rel_pos(wp, str);
3672 break;
3673
3674 case STL_ARGLISTSTAT:
3675 fillable = FALSE;
3676 tmp[0] = 0;
3677 if (append_arg_number(wp, tmp, FALSE, (int)sizeof(tmp)))
3678 str = tmp;
3679 break;
3680
3681 case STL_KEYMAP:
3682 fillable = FALSE;
3683 if (get_keymap_str(wp, tmp, TMPLEN))
3684 str = tmp;
3685 break;
3686 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003687#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003688 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689#else
3690 num = 0;
3691#endif
3692 break;
3693
3694 case STL_BUFNO:
3695 num = wp->w_buffer->b_fnum;
3696 break;
3697
3698 case STL_OFFSET_X:
3699 base = 'X';
3700 case STL_OFFSET:
3701#ifdef FEAT_BYTEOFF
3702 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3703 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3704 0L : l + 1 + (!(State & INSERT) && empty_line ?
3705 0 : (int)wp->w_cursor.col);
3706#endif
3707 break;
3708
3709 case STL_BYTEVAL_X:
3710 base = 'X';
3711 case STL_BYTEVAL:
3712 if (((State & INSERT) && wp == curwin) || empty_line)
3713 num = 0;
3714 else
3715 {
3716#ifdef FEAT_MBYTE
3717 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3718#else
3719 num = linecont[wp->w_cursor.col];
3720#endif
3721 }
3722 if (num == NL)
3723 num = 0;
3724 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3725 num = NL;
3726 break;
3727
3728 case STL_ROFLAG:
3729 case STL_ROFLAG_ALT:
3730 itemisflag = TRUE;
3731 if (wp->w_buffer->b_p_ro)
3732 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3733 break;
3734
3735 case STL_HELPFLAG:
3736 case STL_HELPFLAG_ALT:
3737 itemisflag = TRUE;
3738 if (wp->w_buffer->b_help)
3739 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003740 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 break;
3742
3743#ifdef FEAT_AUTOCMD
3744 case STL_FILETYPE:
3745 if (*wp->w_buffer->b_p_ft != NUL
3746 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3747 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003748 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3749 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 str = tmp;
3751 }
3752 break;
3753
3754 case STL_FILETYPE_ALT:
3755 itemisflag = TRUE;
3756 if (*wp->w_buffer->b_p_ft != NUL
3757 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3758 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003759 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3760 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 for (t = tmp; *t != 0; t++)
3762 *t = TOUPPER_LOC(*t);
3763 str = tmp;
3764 }
3765 break;
3766#endif
3767
3768#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3769 case STL_PREVIEWFLAG:
3770 case STL_PREVIEWFLAG_ALT:
3771 itemisflag = TRUE;
3772 if (wp->w_p_pvw)
3773 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3774 : _("[Preview]"));
3775 break;
3776#endif
3777
3778 case STL_MODIFIED:
3779 case STL_MODIFIED_ALT:
3780 itemisflag = TRUE;
3781 switch ((opt == STL_MODIFIED_ALT)
3782 + bufIsChanged(wp->w_buffer) * 2
3783 + (!wp->w_buffer->b_p_ma) * 4)
3784 {
3785 case 2: str = (char_u *)"[+]"; break;
3786 case 3: str = (char_u *)",+"; break;
3787 case 4: str = (char_u *)"[-]"; break;
3788 case 5: str = (char_u *)",-"; break;
3789 case 6: str = (char_u *)"[+-]"; break;
3790 case 7: str = (char_u *)",+-"; break;
3791 }
3792 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003793
3794 case STL_HIGHLIGHT:
3795 t = s;
3796 while (*s != '#' && *s != NUL)
3797 ++s;
3798 if (*s == '#')
3799 {
3800 item[curitem].type = Highlight;
3801 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003802 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003803 curitem++;
3804 }
3805 ++s;
3806 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 }
3808
3809 item[curitem].start = p;
3810 item[curitem].type = Normal;
3811 if (str != NULL && *str)
3812 {
3813 t = str;
3814 if (itemisflag)
3815 {
3816 if ((t[0] && t[1])
3817 && ((!prevchar_isitem && *t == ',')
3818 || (prevchar_isflag && *t == ' ')))
3819 t++;
3820 prevchar_isflag = TRUE;
3821 }
3822 l = vim_strsize(t);
3823 if (l > 0)
3824 prevchar_isitem = TRUE;
3825 if (l > maxwid)
3826 {
3827 while (l >= maxwid)
3828#ifdef FEAT_MBYTE
3829 if (has_mbyte)
3830 {
3831 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003832 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834 else
3835#endif
3836 l -= byte2cells(*t++);
3837 if (p + 1 >= out + outlen)
3838 break;
3839 *p++ = '<';
3840 }
3841 if (minwid > 0)
3842 {
3843 for (; l < minwid && p + 1 < out + outlen; l++)
3844 {
3845 /* Don't put a "-" in front of a digit. */
3846 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3847 *p++ = ' ';
3848 else
3849 *p++ = fillchar;
3850 }
3851 minwid = 0;
3852 }
3853 else
3854 minwid *= -1;
3855 while (*t && p + 1 < out + outlen)
3856 {
3857 *p++ = *t++;
3858 /* Change a space by fillchar, unless fillchar is '-' and a
3859 * digit follows. */
3860 if (fillable && p[-1] == ' '
3861 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3862 p[-1] = fillchar;
3863 }
3864 for (; l < minwid && p + 1 < out + outlen; l++)
3865 *p++ = fillchar;
3866 }
3867 else if (num >= 0)
3868 {
3869 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3870 char_u nstr[20];
3871
3872 if (p + 20 >= out + outlen)
3873 break; /* not sufficient space */
3874 prevchar_isitem = TRUE;
3875 t = nstr;
3876 if (opt == STL_VIRTCOL_ALT)
3877 {
3878 *t++ = '-';
3879 minwid--;
3880 }
3881 *t++ = '%';
3882 if (zeropad)
3883 *t++ = '0';
3884 *t++ = '*';
3885 *t++ = nbase == 16 ? base : (nbase == 8 ? 'o' : 'd');
3886 *t = 0;
3887
3888 for (n = num, l = 1; n >= nbase; n /= nbase)
3889 l++;
3890 if (opt == STL_VIRTCOL_ALT)
3891 l++;
3892 if (l > maxwid)
3893 {
3894 l += 2;
3895 n = l - maxwid;
3896 while (l-- > maxwid)
3897 num /= nbase;
3898 *t++ = '>';
3899 *t++ = '%';
3900 *t = t[-3];
3901 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003902 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3903 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
3905 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003906 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3907 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 p += STRLEN(p);
3909 }
3910 else
3911 item[curitem].type = Empty;
3912
3913 if (opt == STL_VIM_EXPR)
3914 vim_free(str);
3915
3916 if (num >= 0 || (!itemisflag && str && *str))
3917 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
3918 curitem++;
3919 }
3920 *p = NUL;
3921 itemcnt = curitem;
3922
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003923#ifdef FEAT_EVAL
3924 if (usefmt != fmt)
3925 vim_free(usefmt);
3926#endif
3927
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 width = vim_strsize(out);
3929 if (maxwidth > 0 && width > maxwidth)
3930 {
3931 /* Result is too long, must trunctate somewhere. */
3932 l = 0;
3933 if (itemcnt == 0)
3934 s = out;
3935 else
3936 {
3937 for ( ; l < itemcnt; l++)
3938 if (item[l].type == Trunc)
3939 {
3940 /* Truncate at %< item. */
3941 s = item[l].start;
3942 break;
3943 }
3944 if (l == itemcnt)
3945 {
3946 /* No %< item, truncate first item. */
3947 s = item[0].start;
3948 l = 0;
3949 }
3950 }
3951
3952 if (width - vim_strsize(s) >= maxwidth)
3953 {
3954 /* Truncation mark is beyond max length */
3955#ifdef FEAT_MBYTE
3956 if (has_mbyte)
3957 {
3958 s = out;
3959 width = 0;
3960 for (;;)
3961 {
3962 width += ptr2cells(s);
3963 if (width >= maxwidth)
3964 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003965 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
3967 /* Fill up for half a double-wide character. */
3968 while (++width < maxwidth)
3969 *s++ = fillchar;
3970 }
3971 else
3972#endif
3973 s = out + maxwidth - 1;
3974 for (l = 0; l < itemcnt; l++)
3975 if (item[l].start > s)
3976 break;
3977 itemcnt = l;
3978 *s++ = '>';
3979 *s = 0;
3980 }
3981 else
3982 {
3983#ifdef FEAT_MBYTE
3984 if (has_mbyte)
3985 {
3986 n = 0;
3987 while (width >= maxwidth)
3988 {
3989 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003990 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 }
3992 }
3993 else
3994#endif
3995 n = width - maxwidth + 1;
3996 p = s + n;
3997 mch_memmove(s + 1, p, STRLEN(p) + 1);
3998 *s = '<';
3999
4000 /* Fill up for half a double-wide character. */
4001 while (++width < maxwidth)
4002 {
4003 s = s + STRLEN(s);
4004 *s++ = fillchar;
4005 *s = NUL;
4006 }
4007
4008 --n; /* count the '<' */
4009 for (; l < itemcnt; l++)
4010 {
4011 if (item[l].start - n >= s)
4012 item[l].start -= n;
4013 else
4014 item[l].start = s;
4015 }
4016 }
4017 width = maxwidth;
4018 }
4019 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4020 {
4021 /* Apply STL_MIDDLE if any */
4022 for (l = 0; l < itemcnt; l++)
4023 if (item[l].type == Middle)
4024 break;
4025 if (l < itemcnt)
4026 {
4027 p = item[l].start + maxwidth - width;
4028 mch_memmove(p, item[l].start, STRLEN(item[l].start) + 1);
4029 for (s = item[l].start; s < p; s++)
4030 *s = fillchar;
4031 for (l++; l < itemcnt; l++)
4032 item[l].start += maxwidth - width;
4033 width = maxwidth;
4034 }
4035 }
4036
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004037 /* Store the info about highlighting. */
4038 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004040 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 for (l = 0; l < itemcnt; l++)
4042 {
4043 if (item[l].type == Highlight)
4044 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004045 sp->start = item[l].start;
4046 sp->userhl = item[l].minwid;
4047 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
4049 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004050 sp->start = NULL;
4051 sp->userhl = 0;
4052 }
4053
4054 /* Store the info about tab pages labels. */
4055 if (tabtab != NULL)
4056 {
4057 sp = tabtab;
4058 for (l = 0; l < itemcnt; l++)
4059 {
4060 if (item[l].type == TabPage)
4061 {
4062 sp->start = item[l].start;
4063 sp->userhl = item[l].minwid;
4064 sp++;
4065 }
4066 }
4067 sp->start = NULL;
4068 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070
4071 return width;
4072}
4073#endif /* FEAT_STL_OPT */
4074
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004075#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4076 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004078 * Get relative cursor position in window into "str[]", in the form 99%, using
4079 * "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 */
4081 void
4082get_rel_pos(wp, str)
4083 win_T *wp;
4084 char_u *str;
4085{
4086 long above; /* number of lines above window */
4087 long below; /* number of lines below window */
4088
4089 above = wp->w_topline - 1;
4090#ifdef FEAT_DIFF
4091 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4092#endif
4093 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4094 if (below <= 0)
4095 STRCPY(str, above == 0 ? _("All") : _("Bot"));
4096 else if (above <= 0)
4097 STRCPY(str, _("Top"));
4098 else
4099 sprintf((char *)str, "%2d%%", above > 1000000L
4100 ? (int)(above / ((above + below) / 100L))
4101 : (int)(above * 100L / (above + below)));
4102}
4103#endif
4104
4105/*
4106 * Append (file 2 of 8) to 'buf', if editing more than one file.
4107 * Return TRUE if it was appended.
4108 */
4109 int
4110append_arg_number(wp, buf, add_file, maxlen)
4111 win_T *wp;
4112 char_u *buf;
4113 int add_file; /* Add "file" before the arg number */
4114 int maxlen; /* maximum nr of chars in buf or zero*/
4115{
4116 char_u *p;
4117
4118 if (ARGCOUNT <= 1) /* nothing to do */
4119 return FALSE;
4120
4121 p = buf + STRLEN(buf); /* go to the end of the buffer */
4122 if (maxlen && p - buf + 35 >= maxlen) /* getting too long */
4123 return FALSE;
4124 *p++ = ' ';
4125 *p++ = '(';
4126 if (add_file)
4127 {
4128 STRCPY(p, "file ");
4129 p += 5;
4130 }
4131 sprintf((char *)p, wp->w_arg_idx_invalid ? "(%d) of %d)"
4132 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4133 return TRUE;
4134}
4135
4136/*
4137 * If fname is not a full path, make it a full path.
4138 * Returns pointer to allocated memory (NULL for failure).
4139 */
4140 char_u *
4141fix_fname(fname)
4142 char_u *fname;
4143{
4144 /*
4145 * Force expanding the path always for Unix, because symbolic links may
4146 * mess up the full path name, even though it starts with a '/'.
4147 * Also expand when there is ".." in the file name, try to remove it,
4148 * because "c:/src/../README" is equal to "c:/README".
4149 * For MS-Windows also expand names like "longna~1" to "longname".
4150 */
4151#ifdef UNIX
4152 return FullName_save(fname, TRUE);
4153#else
4154 if (!vim_isAbsName(fname) || strstr((char *)fname, "..") != NULL
4155#if defined(MSWIN) || defined(DJGPP)
4156 || vim_strchr(fname, '~') != NULL
4157#endif
4158 )
4159 return FullName_save(fname, FALSE);
4160
4161 fname = vim_strsave(fname);
4162
4163#ifdef USE_FNAME_CASE
4164# ifdef USE_LONG_FNAME
4165 if (USE_LONG_FNAME)
4166# endif
4167 {
4168 if (fname != NULL)
4169 fname_case(fname, 0); /* set correct case for file name */
4170 }
4171#endif
4172
4173 return fname;
4174#endif
4175}
4176
4177/*
4178 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4179 * "ffname" becomes a pointer to allocated memory (or NULL).
4180 */
4181/*ARGSUSED*/
4182 void
4183fname_expand(buf, ffname, sfname)
4184 buf_T *buf;
4185 char_u **ffname;
4186 char_u **sfname;
4187{
4188 if (*ffname == NULL) /* if no file name given, nothing to do */
4189 return;
4190 if (*sfname == NULL) /* if no short file name given, use ffname */
4191 *sfname = *ffname;
4192 *ffname = fix_fname(*ffname); /* expand to full path */
4193
4194#ifdef FEAT_SHORTCUT
4195 if (!buf->b_p_bin)
4196 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004197 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198
4199 /* If the file name is a shortcut file, use the file it links to. */
4200 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004201 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
4203 vim_free(*ffname);
4204 *ffname = rfname;
4205 *sfname = rfname;
4206 }
4207 }
4208#endif
4209}
4210
4211/*
4212 * Get the file name for an argument list entry.
4213 */
4214 char_u *
4215alist_name(aep)
4216 aentry_T *aep;
4217{
4218 buf_T *bp;
4219
4220 /* Use the name from the associated buffer if it exists. */
4221 bp = buflist_findnr(aep->ae_fnum);
4222 if (bp == NULL)
4223 return aep->ae_fname;
4224 return bp->b_fname;
4225}
4226
4227#if defined(FEAT_WINDOWS) || defined(PROTO)
4228/*
4229 * do_arg_all(): Open up to 'count' windows, one for each argument.
4230 */
4231 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004232do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 int count;
4234 int forceit; /* hide buffers in current windows */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004235 int keep_tabs; /* keep curren tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
4237 int i;
4238 win_T *wp, *wpnext;
4239 char_u *opened; /* array of flags for which args are open */
4240 int opened_len; /* lenght of opened[] */
4241 int use_firstwin = FALSE; /* use first window for arglist */
4242 int split_ret = OK;
4243 int p_ea_save;
4244 alist_T *alist; /* argument list to be used */
4245 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004246 tabpage_T *tpnext;
4247 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004248 win_T *new_curwin = NULL;
4249 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251 if (ARGCOUNT <= 0)
4252 {
4253 /* Don't give an error message. We don't want it when the ":all"
4254 * command is in the .vimrc. */
4255 return;
4256 }
4257 setpcmark();
4258
4259 opened_len = ARGCOUNT;
4260 opened = alloc_clear((unsigned)opened_len);
4261 if (opened == NULL)
4262 return;
4263
4264#ifdef FEAT_GUI
4265 need_mouse_correct = TRUE;
4266#endif
4267
4268 /*
4269 * Try closing all windows that are not in the argument list.
4270 * Also close windows that are not full width;
4271 * When 'hidden' or "forceit" set the buffer becomes hidden.
4272 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004273 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004275 if (had_tab > 0)
4276 goto_tabpage_tp(first_tabpage);
4277 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004279 tpnext = curtab->tp_next;
4280 for (wp = firstwin; wp != NULL; wp = wpnext)
4281 {
4282 wpnext = wp->w_next;
4283 buf = wp->w_buffer;
4284 if (buf->b_ffname == NULL
4285 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004287 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004289 )
4290 i = ARGCOUNT;
4291 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004293 /* check if the buffer in this window is in the arglist */
4294 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004296 if (ARGLIST[i].ae_fnum == buf->b_fnum
4297 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004298 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004300 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004301 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004302 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004303 if (i == 0)
4304 {
4305 new_curwin = wp;
4306 new_curtab = curtab;
4307 }
4308 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004309 if (wp->w_alist != curwin->w_alist)
4310 {
4311 /* Use the current argument list for all windows
4312 * containing a file from it. */
4313 alist_unlink(wp->w_alist);
4314 wp->w_alist = curwin->w_alist;
4315 ++wp->w_alist->al_refcount;
4316 }
4317 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 }
4320 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004321 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004323 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004325 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004326 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004328 /* If the buffer was changed, and we would like to hide it,
4329 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004330 if (!P_HID(buf) && buf->b_nwindows <= 1
4331 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004333 (void)autowrite(buf, FALSE);
4334#ifdef FEAT_AUTOCMD
4335 /* check if autocommands removed the window */
4336 if (!win_valid(wp) || !buf_valid(buf))
4337 {
4338 wpnext = firstwin; /* start all over... */
4339 continue;
4340 }
4341#endif
4342 }
4343#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004344 /* don't close last window */
4345 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004346#endif
4347 use_firstwin = TRUE;
4348#ifdef FEAT_WINDOWS
4349 else
4350 {
4351 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4352# ifdef FEAT_AUTOCMD
4353 /* check if autocommands removed the next window */
4354 if (!win_valid(wpnext))
4355 wpnext = firstwin; /* start all over... */
4356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 }
4358#endif
4359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004362
4363 /* Without the ":tab" modifier only do the current tab page. */
4364 if (had_tab == 0 || tpnext == NULL)
4365 break;
4366
4367# ifdef FEAT_AUTOCMD
4368 /* check if autocommands removed the next tab page */
4369 if (!valid_tabpage(tpnext))
4370 tpnext = first_tabpage; /* start all over...*/
4371# endif
4372 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374
4375 /*
4376 * Open a window for files in the argument list that don't have one.
4377 * ARGCOUNT may change while doing this, because of autocommands.
4378 */
4379 if (count > ARGCOUNT || count <= 0)
4380 count = ARGCOUNT;
4381
4382 /* Autocommands may do anything to the argument list. Make sure it's not
4383 * freed while we are working here by "locking" it. We still have to
4384 * watch out for its size to be changed. */
4385 alist = curwin->w_alist;
4386 ++alist->al_refcount;
4387
4388#ifdef FEAT_AUTOCMD
4389 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4390 ++autocmd_no_enter;
4391 ++autocmd_no_leave;
4392#endif
4393 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004394#ifdef FEAT_WINDOWS
4395 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4396 * leaving an empty tab page when executed locally. */
4397 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4398 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4399 use_firstwin = TRUE;
4400#endif
4401
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4403 {
4404 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4405 arg_had_last = TRUE;
4406 if (i < opened_len && opened[i])
4407 {
4408 /* Move the already present window to below the current window */
4409 if (curwin->w_arg_idx != i)
4410 {
4411 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4412 {
4413 if (wpnext->w_arg_idx == i)
4414 {
4415 win_move_after(wpnext, curwin);
4416 break;
4417 }
4418 }
4419 }
4420 }
4421 else if (split_ret == OK)
4422 {
4423 if (!use_firstwin) /* split current window */
4424 {
4425 p_ea_save = p_ea;
4426 p_ea = TRUE; /* use space from all windows */
4427 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4428 p_ea = p_ea_save;
4429 if (split_ret == FAIL)
4430 continue;
4431 }
4432#ifdef FEAT_AUTOCMD
4433 else /* first window: do autocmd for leaving this buffer */
4434 --autocmd_no_leave;
4435#endif
4436
4437 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004438 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 */
4440 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004441 if (i == 0)
4442 {
4443 new_curwin = curwin;
4444 new_curtab = curtab;
4445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4447 ECMD_ONE,
4448 ((P_HID(curwin->w_buffer)
4449 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4450 + ECMD_OLDBUF);
4451#ifdef FEAT_AUTOCMD
4452 if (use_firstwin)
4453 ++autocmd_no_leave;
4454#endif
4455 use_firstwin = FALSE;
4456 }
4457 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004458
4459 /* When ":tab" was used open a new tab for a new window repeatedly. */
4460 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4461 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 }
4463
4464 /* Remove the "lock" on the argument list. */
4465 alist_unlink(alist);
4466
4467#ifdef FEAT_AUTOCMD
4468 --autocmd_no_enter;
4469#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004470 /* to window with first arg */
4471 if (valid_tabpage(new_curtab))
4472 goto_tabpage_tp(new_curtab);
4473 if (win_valid(new_curwin))
4474 win_enter(new_curwin, FALSE);
4475
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476#ifdef FEAT_AUTOCMD
4477 --autocmd_no_leave;
4478#endif
4479 vim_free(opened);
4480}
4481
4482# if defined(FEAT_LISTCMDS) || defined(PROTO)
4483/*
4484 * Open a window for a number of buffers.
4485 */
4486 void
4487ex_buffer_all(eap)
4488 exarg_T *eap;
4489{
4490 buf_T *buf;
4491 win_T *wp, *wpnext;
4492 int split_ret = OK;
4493 int p_ea_save;
4494 int open_wins = 0;
4495 int r;
4496 int count; /* Maximum number of windows to open. */
4497 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004498#ifdef FEAT_WINDOWS
4499 int had_tab = cmdmod.tab;
4500 tabpage_T *tpnext;
4501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503 if (eap->addr_count == 0) /* make as many windows as possible */
4504 count = 9999;
4505 else
4506 count = eap->line2; /* make as many windows as specified */
4507 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4508 all = FALSE;
4509 else
4510 all = TRUE;
4511
4512 setpcmark();
4513
4514#ifdef FEAT_GUI
4515 need_mouse_correct = TRUE;
4516#endif
4517
4518 /*
4519 * Close superfluous windows (two windows for the same buffer).
4520 * Also close windows that are not full-width.
4521 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004522#ifdef FEAT_WINDOWS
4523 if (had_tab > 0)
4524 goto_tabpage_tp(first_tabpage);
4525 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004528 tpnext = curtab->tp_next;
4529 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004531 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004532 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004533#ifdef FEAT_VERTSPLIT
4534 || ((cmdmod.split & WSP_VERT)
4535 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004536 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004537 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004539#ifdef FEAT_WINDOWS
4540 || (had_tab > 0 && wp != firstwin)
4541#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004542 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004543 {
4544 win_close(wp, FALSE);
4545#ifdef FEAT_AUTOCMD
4546 wpnext = firstwin; /* just in case an autocommand does
4547 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004548 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004549 open_wins = 0;
4550#endif
4551 }
4552 else
4553 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004555
4556#ifdef FEAT_WINDOWS
4557 /* Without the ":tab" modifier only do the current tab page. */
4558 if (had_tab == 0 || tpnext == NULL)
4559 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004560 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563
4564 /*
4565 * Go through the buffer list. When a buffer doesn't have a window yet,
4566 * open one. Otherwise move the window to the right position.
4567 * Watch out for autocommands that delete buffers or windows!
4568 */
4569#ifdef FEAT_AUTOCMD
4570 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4571 ++autocmd_no_enter;
4572#endif
4573 win_enter(lastwin, FALSE);
4574#ifdef FEAT_AUTOCMD
4575 ++autocmd_no_leave;
4576#endif
4577 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4578 {
4579 /* Check if this buffer needs a window */
4580 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4581 continue;
4582
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004583#ifdef FEAT_WINDOWS
4584 if (had_tab != 0)
4585 {
4586 /* With the ":tab" modifier don't move the window. */
4587 if (buf->b_nwindows > 0)
4588 wp = lastwin; /* buffer has a window, skip it */
4589 else
4590 wp = NULL;
4591 }
4592 else
4593#endif
4594 {
4595 /* Check if this buffer already has a window */
4596 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4597 if (wp->w_buffer == buf)
4598 break;
4599 /* If the buffer already has a window, move it */
4600 if (wp != NULL)
4601 win_move_after(wp, curwin);
4602 }
4603
4604 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
4606 /* Split the window and put the buffer in it */
4607 p_ea_save = p_ea;
4608 p_ea = TRUE; /* use space from all windows */
4609 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4610 ++open_wins;
4611 p_ea = p_ea_save;
4612 if (split_ret == FAIL)
4613 continue;
4614
4615 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004616#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 swap_exists_action = SEA_DIALOG;
4618#endif
4619 set_curbuf(buf, DOBUF_GOTO);
4620#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004623#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004625# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 break;
4627 }
4628#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004629#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (swap_exists_action == SEA_QUIT)
4631 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004632# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4633 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004634
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004635 /* Reset the error/interrupt/exception state here so that
4636 * aborting() returns FALSE when closing a window. */
4637 enter_cleanup(&cs);
4638# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004639
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004640 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 win_close(curwin, TRUE);
4642 --open_wins;
4643 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004644 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004645
4646# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4647 /* Restore the error/interrupt/exception state if not
4648 * discarded by a new aborting error, interrupt, or uncaught
4649 * exception. */
4650 leave_cleanup(&cs);
4651# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
4653 else
4654 handle_swap_exists(NULL);
4655#endif
4656 }
4657
4658 ui_breakcheck();
4659 if (got_int)
4660 {
4661 (void)vgetc(); /* only break the file loading, not the rest */
4662 break;
4663 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004664#ifdef FEAT_EVAL
4665 /* Autocommands deleted the buffer or aborted script processing!!! */
4666 if (aborting())
4667 break;
4668#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004669#ifdef FEAT_WINDOWS
4670 /* When ":tab" was used open a new tab for a new window repeatedly. */
4671 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4672 cmdmod.tab = 9999;
4673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
4675#ifdef FEAT_AUTOCMD
4676 --autocmd_no_enter;
4677#endif
4678 win_enter(firstwin, FALSE); /* back to first window */
4679#ifdef FEAT_AUTOCMD
4680 --autocmd_no_leave;
4681#endif
4682
4683 /*
4684 * Close superfluous windows.
4685 */
4686 for (wp = lastwin; open_wins > count; )
4687 {
4688 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4689 || autowrite(wp->w_buffer, FALSE) == OK);
4690#ifdef FEAT_AUTOCMD
4691 if (!win_valid(wp))
4692 {
4693 /* BufWrite Autocommands made the window invalid, start over */
4694 wp = lastwin;
4695 }
4696 else
4697#endif
4698 if (r)
4699 {
4700 win_close(wp, !P_HID(wp->w_buffer));
4701 --open_wins;
4702 wp = lastwin;
4703 }
4704 else
4705 {
4706 wp = wp->w_prev;
4707 if (wp == NULL)
4708 break;
4709 }
4710 }
4711}
4712# endif /* FEAT_LISTCMDS */
4713
4714#endif /* FEAT_WINDOWS */
4715
Bram Moolenaara3227e22006-03-08 21:32:40 +00004716static int chk_modeline __ARGS((linenr_T, int));
4717
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718/*
4719 * do_modelines() - process mode lines for the current file
4720 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004721 * "flags" can be:
4722 * OPT_WINONLY only set options local to window
4723 * OPT_NOWIN don't set options local to window
4724 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 * Returns immediately if the "ml" option isn't set.
4726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004728do_modelines(flags)
4729 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004731 linenr_T lnum;
4732 int nmlines;
4733 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734
4735 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4736 return;
4737
4738 /* Disallow recursive entry here. Can happen when executing a modeline
4739 * triggers an autocommand, which reloads modelines with a ":do". */
4740 if (entered)
4741 return;
4742
4743 ++entered;
4744 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4745 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004746 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 nmlines = 0;
4748
4749 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4750 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004751 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 nmlines = 0;
4753 --entered;
4754}
4755
4756#include "version.h" /* for version number */
4757
4758/*
4759 * chk_modeline() - check a single line for a mode string
4760 * Return FAIL if an error encountered.
4761 */
4762 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004763chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004765 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766{
4767 char_u *s;
4768 char_u *e;
4769 char_u *linecopy; /* local copy of any modeline found */
4770 int prev;
4771 int vers;
4772 int end;
4773 int retval = OK;
4774 char_u *save_sourcing_name;
4775 linenr_T save_sourcing_lnum;
4776#ifdef FEAT_EVAL
4777 scid_T save_SID;
4778#endif
4779
4780 prev = -1;
4781 for (s = ml_get(lnum); *s != NUL; ++s)
4782 {
4783 if (prev == -1 || vim_isspace(prev))
4784 {
4785 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4786 || STRNCMP(s, "vi:", (size_t)3) == 0)
4787 break;
4788 if (STRNCMP(s, "vim", 3) == 0)
4789 {
4790 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4791 e = s + 4;
4792 else
4793 e = s + 3;
4794 vers = getdigits(&e);
4795 if (*e == ':'
4796 && (s[3] == ':'
4797 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4798 || (VIM_VERSION_100 < vers && s[3] == '<')
4799 || (VIM_VERSION_100 > vers && s[3] == '>')
4800 || (VIM_VERSION_100 == vers && s[3] == '=')))
4801 break;
4802 }
4803 }
4804 prev = *s;
4805 }
4806
4807 if (*s)
4808 {
4809 do /* skip over "ex:", "vi:" or "vim:" */
4810 ++s;
4811 while (s[-1] != ':');
4812
4813 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4814 if (linecopy == NULL)
4815 return FAIL;
4816
4817 save_sourcing_lnum = sourcing_lnum;
4818 save_sourcing_name = sourcing_name;
4819 sourcing_lnum = lnum; /* prepare for emsg() */
4820 sourcing_name = (char_u *)"modelines";
4821
4822 end = FALSE;
4823 while (end == FALSE)
4824 {
4825 s = skipwhite(s);
4826 if (*s == NUL)
4827 break;
4828
4829 /*
4830 * Find end of set command: ':' or end of line.
4831 * Skip over "\:", replacing it with ":".
4832 */
4833 for (e = s; *e != ':' && *e != NUL; ++e)
4834 if (e[0] == '\\' && e[1] == ':')
4835 STRCPY(e, e + 1);
4836 if (*e == NUL)
4837 end = TRUE;
4838
4839 /*
4840 * If there is a "set" command, require a terminating ':' and
4841 * ignore the stuff after the ':'.
4842 * "vi:set opt opt opt: foo" -- foo not interpreted
4843 * "vi:opt opt opt: foo" -- foo interpreted
4844 * Accept "se" for compatibility with Elvis.
4845 */
4846 if (STRNCMP(s, "set ", (size_t)4) == 0
4847 || STRNCMP(s, "se ", (size_t)3) == 0)
4848 {
4849 if (*e != ':') /* no terminating ':'? */
4850 break;
4851 end = TRUE;
4852 s = vim_strchr(s, ' ') + 1;
4853 }
4854 *e = NUL; /* truncate the set command */
4855
4856 if (*s != NUL) /* skip over an empty "::" */
4857 {
4858#ifdef FEAT_EVAL
4859 save_SID = current_SID;
4860 current_SID = SID_MODELINE;
4861#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004862 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863#ifdef FEAT_EVAL
4864 current_SID = save_SID;
4865#endif
4866 if (retval == FAIL) /* stop if error found */
4867 break;
4868 }
4869 s = e + 1; /* advance to next part */
4870 }
4871
4872 sourcing_lnum = save_sourcing_lnum;
4873 sourcing_name = save_sourcing_name;
4874
4875 vim_free(linecopy);
4876 }
4877 return retval;
4878}
4879
4880#ifdef FEAT_VIMINFO
4881 int
4882read_viminfo_bufferlist(virp, writing)
4883 vir_T *virp;
4884 int writing;
4885{
4886 char_u *tab;
4887 linenr_T lnum;
4888 colnr_T col;
4889 buf_T *buf;
4890 char_u *sfname;
4891 char_u *xline;
4892
4893 /* Handle long line and escaped characters. */
4894 xline = viminfo_readstring(virp, 1, FALSE);
4895
4896 /* don't read in if there are files on the command-line or if writing: */
4897 if (xline != NULL && !writing && ARGCOUNT == 0
4898 && find_viminfo_parameter('%') != NULL)
4899 {
4900 /* Format is: <fname> Tab <lnum> Tab <col>.
4901 * Watch out for a Tab in the file name, work from the end. */
4902 lnum = 0;
4903 col = 0;
4904 tab = vim_strrchr(xline, '\t');
4905 if (tab != NULL)
4906 {
4907 *tab++ = '\0';
4908 col = atoi((char *)tab);
4909 tab = vim_strrchr(xline, '\t');
4910 if (tab != NULL)
4911 {
4912 *tab++ = '\0';
4913 lnum = atol((char *)tab);
4914 }
4915 }
4916
4917 /* Expand "~/" in the file name at "line + 1" to a full path.
4918 * Then try shortening it by comparing with the current directory */
4919 expand_env(xline, NameBuff, MAXPATHL);
4920 mch_dirname(IObuff, IOSIZE);
4921 sfname = shorten_fname(NameBuff, IObuff);
4922 if (sfname == NULL)
4923 sfname = NameBuff;
4924
4925 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
4926 if (buf != NULL) /* just in case... */
4927 {
4928 buf->b_last_cursor.lnum = lnum;
4929 buf->b_last_cursor.col = col;
4930 buflist_setfpos(buf, curwin, lnum, col, FALSE);
4931 }
4932 }
4933 vim_free(xline);
4934
4935 return viminfo_readline(virp);
4936}
4937
4938 void
4939write_viminfo_bufferlist(fp)
4940 FILE *fp;
4941{
4942 buf_T *buf;
4943#ifdef FEAT_WINDOWS
4944 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004945 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946#endif
4947 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004948 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
4950 if (find_viminfo_parameter('%') == NULL)
4951 return;
4952
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004953 /* Without a number -1 is returned: do all buffers. */
4954 max_buffers = get_viminfo_parameter('%');
4955
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004957 line = alloc(MAXPATHL + 40);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 if (line == NULL)
4959 return;
4960
4961#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00004962 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 set_last_cursor(win);
4964#else
4965 set_last_cursor(curwin);
4966#endif
4967
4968 fprintf(fp, _("\n# Buffer list:\n"));
4969 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
4970 {
4971 if (buf->b_fname == NULL
4972 || !buf->b_p_bl
4973#ifdef FEAT_QUICKFIX
4974 || bt_quickfix(buf)
4975#endif
4976 || removable(buf->b_ffname))
4977 continue;
4978
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004979 if (max_buffers-- == 0)
4980 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 putc('%', fp);
4982 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
4983 sprintf((char *)line + STRLEN(line), "\t%ld\t%d",
4984 (long)buf->b_last_cursor.lnum,
4985 buf->b_last_cursor.col);
4986 viminfo_writestring(fp, line);
4987 }
4988 vim_free(line);
4989}
4990#endif
4991
4992
4993/*
4994 * Return special buffer name.
4995 * Returns NULL when the buffer has a normal file name.
4996 */
4997 char *
4998buf_spname(buf)
4999 buf_T *buf;
5000{
5001#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5002 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005003 {
5004 win_T *win;
5005
5006 /*
5007 * For location list window, w_llist_ref points to the location list.
5008 * For quickfix window, w_llist_ref is NULL.
5009 */
5010 FOR_ALL_WINDOWS(win)
5011 if (win->w_buffer == buf)
5012 break;
5013 if (win != NULL && win->w_llist_ref != NULL)
5014 return _("[Location List]");
5015 else
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005016 return _("[Quickfix List]");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018#endif
5019#ifdef FEAT_QUICKFIX
5020 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5021 * contains the name as specified by the user */
5022 if (bt_nofile(buf))
5023 {
5024 if (buf->b_sfname != NULL)
5025 return (char *)buf->b_sfname;
5026 return "[Scratch]";
5027 }
5028#endif
5029 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005030 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 return NULL;
5032}
5033
5034
5035#if defined(FEAT_SIGNS) || defined(PROTO)
5036/*
5037 * Insert the sign into the signlist.
5038 */
5039 static void
5040insert_sign(buf, prev, next, id, lnum, typenr)
5041 buf_T *buf; /* buffer to store sign in */
5042 signlist_T *prev; /* previous sign entry */
5043 signlist_T *next; /* next sign entry */
5044 int id; /* sign ID */
5045 linenr_T lnum; /* line number which gets the mark */
5046 int typenr; /* typenr of sign we are adding */
5047{
5048 signlist_T *newsign;
5049
5050 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5051 if (newsign != NULL)
5052 {
5053 newsign->id = id;
5054 newsign->lnum = lnum;
5055 newsign->typenr = typenr;
5056 newsign->next = next;
5057#ifdef FEAT_NETBEANS_INTG
5058 newsign->prev = prev;
5059 if (next != NULL)
5060 next->prev = newsign;
5061#endif
5062
5063 if (prev == NULL)
5064 {
5065 /* When adding first sign need to redraw the windows to create the
5066 * column for signs. */
5067 if (buf->b_signlist == NULL)
5068 {
5069 redraw_buf_later(buf, NOT_VALID);
5070 changed_cline_bef_curs();
5071 }
5072
5073 /* first sign in signlist */
5074 buf->b_signlist = newsign;
5075 }
5076 else
5077 prev->next = newsign;
5078 }
5079}
5080
5081/*
5082 * Add the sign into the signlist. Find the right spot to do it though.
5083 */
5084 void
5085buf_addsign(buf, id, lnum, typenr)
5086 buf_T *buf; /* buffer to store sign in */
5087 int id; /* sign ID */
5088 linenr_T lnum; /* line number which gets the mark */
5089 int typenr; /* typenr of sign we are adding */
5090{
5091 signlist_T *sign; /* a sign in the signlist */
5092 signlist_T *prev; /* the previous sign */
5093
5094 prev = NULL;
5095 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5096 {
5097 if (lnum == sign->lnum && id == sign->id)
5098 {
5099 sign->typenr = typenr;
5100 return;
5101 }
5102 else if (
5103#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5104 id < 0 &&
5105#endif
5106 lnum < sign->lnum)
5107 {
5108#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5109 /* XXX - GRP: Is this because of sign slide problem? Or is it
5110 * really needed? Or is it because we allow multiple signs per
5111 * line? If so, should I add that feature to FEAT_SIGNS?
5112 */
5113 while (prev != NULL && prev->lnum == lnum)
5114 prev = prev->prev;
5115 if (prev == NULL)
5116 sign = buf->b_signlist;
5117 else
5118 sign = prev->next;
5119#endif
5120 insert_sign(buf, prev, sign, id, lnum, typenr);
5121 return;
5122 }
5123 prev = sign;
5124 }
5125#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5126 /* XXX - GRP: See previous comment */
5127 while (prev != NULL && prev->lnum == lnum)
5128 prev = prev->prev;
5129 if (prev == NULL)
5130 sign = buf->b_signlist;
5131 else
5132 sign = prev->next;
5133#endif
5134 insert_sign(buf, prev, sign, id, lnum, typenr);
5135
5136 return;
5137}
5138
5139 int
5140buf_change_sign_type(buf, markId, typenr)
5141 buf_T *buf; /* buffer to store sign in */
5142 int markId; /* sign ID */
5143 int typenr; /* typenr of sign we are adding */
5144{
5145 signlist_T *sign; /* a sign in the signlist */
5146
5147 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5148 {
5149 if (sign->id == markId)
5150 {
5151 sign->typenr = typenr;
5152 return sign->lnum;
5153 }
5154 }
5155
5156 return 0;
5157}
5158
5159 int_u
5160buf_getsigntype(buf, lnum, type)
5161 buf_T *buf;
5162 linenr_T lnum;
5163 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5164{
5165 signlist_T *sign; /* a sign in a b_signlist */
5166
5167 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5168 if (sign->lnum == lnum
5169 && (type == SIGN_ANY
5170# ifdef FEAT_SIGN_ICONS
5171 || (type == SIGN_ICON
5172 && sign_get_image(sign->typenr) != NULL)
5173# endif
5174 || (type == SIGN_TEXT
5175 && sign_get_text(sign->typenr) != NULL)
5176 || (type == SIGN_LINEHL
5177 && sign_get_attr(sign->typenr, TRUE) != 0)))
5178 return sign->typenr;
5179 return 0;
5180}
5181
5182
5183 linenr_T
5184buf_delsign(buf, id)
5185 buf_T *buf; /* buffer sign is stored in */
5186 int id; /* sign id */
5187{
5188 signlist_T **lastp; /* pointer to pointer to current sign */
5189 signlist_T *sign; /* a sign in a b_signlist */
5190 signlist_T *next; /* the next sign in a b_signlist */
5191 linenr_T lnum; /* line number whose sign was deleted */
5192
5193 lastp = &buf->b_signlist;
5194 lnum = 0;
5195 for (sign = buf->b_signlist; sign != NULL; sign = next)
5196 {
5197 next = sign->next;
5198 if (sign->id == id)
5199 {
5200 *lastp = next;
5201#ifdef FEAT_NETBEANS_INTG
5202 if (next != NULL)
5203 next->prev = sign->prev;
5204#endif
5205 lnum = sign->lnum;
5206 vim_free(sign);
5207 break;
5208 }
5209 else
5210 lastp = &sign->next;
5211 }
5212
5213 /* When deleted the last sign need to redraw the windows to remove the
5214 * sign column. */
5215 if (buf->b_signlist == NULL)
5216 {
5217 redraw_buf_later(buf, NOT_VALID);
5218 changed_cline_bef_curs();
5219 }
5220
5221 return lnum;
5222}
5223
5224
5225/*
5226 * Find the line number of the sign with the requested id. If the sign does
5227 * not exist, return 0 as the line number. This will still let the correct file
5228 * get loaded.
5229 */
5230 int
5231buf_findsign(buf, id)
5232 buf_T *buf; /* buffer to store sign in */
5233 int id; /* sign ID */
5234{
5235 signlist_T *sign; /* a sign in the signlist */
5236
5237 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5238 if (sign->id == id)
5239 return sign->lnum;
5240
5241 return 0;
5242}
5243
5244 int
5245buf_findsign_id(buf, lnum)
5246 buf_T *buf; /* buffer whose sign we are searching for */
5247 linenr_T lnum; /* line number of sign */
5248{
5249 signlist_T *sign; /* a sign in the signlist */
5250
5251 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5252 if (sign->lnum == lnum)
5253 return sign->id;
5254
5255 return 0;
5256}
5257
5258
5259# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5260/* see if a given type of sign exists on a specific line */
5261 int
5262buf_findsigntype_id(buf, lnum, typenr)
5263 buf_T *buf; /* buffer whose sign we are searching for */
5264 linenr_T lnum; /* line number of sign */
5265 int typenr; /* sign type number */
5266{
5267 signlist_T *sign; /* a sign in the signlist */
5268
5269 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5270 if (sign->lnum == lnum && sign->typenr == typenr)
5271 return sign->id;
5272
5273 return 0;
5274}
5275
5276
5277# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5278/* return the number of icons on the given line */
5279 int
5280buf_signcount(buf, lnum)
5281 buf_T *buf;
5282 linenr_T lnum;
5283{
5284 signlist_T *sign; /* a sign in the signlist */
5285 int count = 0;
5286
5287 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5288 if (sign->lnum == lnum)
5289 if (sign_get_image(sign->typenr) != NULL)
5290 count++;
5291
5292 return count;
5293}
5294# endif /* FEAT_SIGN_ICONS */
5295# endif /* FEAT_NETBEANS_INTG */
5296
5297
5298/*
5299 * Delete signs in buffer "buf".
5300 */
5301 static void
5302buf_delete_signs(buf)
5303 buf_T *buf;
5304{
5305 signlist_T *next;
5306
5307 while (buf->b_signlist != NULL)
5308 {
5309 next = buf->b_signlist->next;
5310 vim_free(buf->b_signlist);
5311 buf->b_signlist = next;
5312 }
5313}
5314
5315/*
5316 * Delete all signs in all buffers.
5317 */
5318 void
5319buf_delete_all_signs()
5320{
5321 buf_T *buf; /* buffer we are checking for signs */
5322
5323 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5324 if (buf->b_signlist != NULL)
5325 {
5326 /* Need to redraw the windows to remove the sign column. */
5327 redraw_buf_later(buf, NOT_VALID);
5328 buf_delete_signs(buf);
5329 }
5330}
5331
5332/*
5333 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5334 */
5335 void
5336sign_list_placed(rbuf)
5337 buf_T *rbuf;
5338{
5339 buf_T *buf;
5340 signlist_T *p;
5341 char lbuf[BUFSIZ];
5342
5343 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5344 msg_putchar('\n');
5345 if (rbuf == NULL)
5346 buf = firstbuf;
5347 else
5348 buf = rbuf;
5349 while (buf != NULL)
5350 {
5351 if (buf->b_signlist != NULL)
5352 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005353 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5355 msg_putchar('\n');
5356 }
5357 for (p = buf->b_signlist; p != NULL; p = p->next)
5358 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005359 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5361 MSG_PUTS(lbuf);
5362 msg_putchar('\n');
5363 }
5364 if (rbuf != NULL)
5365 break;
5366 buf = buf->b_next;
5367 }
5368}
5369
5370/*
5371 * Adjust a placed sign for inserted/deleted lines.
5372 */
5373 void
5374sign_mark_adjust(line1, line2, amount, amount_after)
5375 linenr_T line1;
5376 linenr_T line2;
5377 long amount;
5378 long amount_after;
5379{
5380 signlist_T *sign; /* a sign in a b_signlist */
5381
5382 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5383 {
5384 if (sign->lnum >= line1 && sign->lnum <= line2)
5385 {
5386 if (amount == MAXLNUM)
5387 sign->lnum = line1;
5388 else
5389 sign->lnum += amount;
5390 }
5391 else if (sign->lnum > line2)
5392 sign->lnum += amount_after;
5393 }
5394}
5395#endif /* FEAT_SIGNS */
5396
5397/*
5398 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5399 */
5400 void
5401set_buflisted(on)
5402 int on;
5403{
5404 if (on != curbuf->b_p_bl)
5405 {
5406 curbuf->b_p_bl = on;
5407#ifdef FEAT_AUTOCMD
5408 if (on)
5409 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5410 else
5411 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5412#endif
5413 }
5414}
5415
5416/*
5417 * Read the file for "buf" again and check if the contents changed.
5418 * Return TRUE if it changed or this could not be checked.
5419 */
5420 int
5421buf_contents_changed(buf)
5422 buf_T *buf;
5423{
5424 buf_T *newbuf;
5425 int differ = TRUE;
5426 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 exarg_T ea;
5429
5430 /* Allocate a buffer without putting it in the buffer list. */
5431 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5432 if (newbuf == NULL)
5433 return TRUE;
5434
5435 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5436 if (prep_exarg(&ea, buf) == FAIL)
5437 {
5438 wipe_buffer(newbuf, FALSE);
5439 return TRUE;
5440 }
5441
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 /* set curwin/curbuf to buf and save a few things */
5443 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
Bram Moolenaar4770d092006-01-12 23:22:24 +00005445 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 && readfile(buf->b_ffname, buf->b_fname,
5447 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5448 &ea, READ_NEW | READ_DUMMY) == OK)
5449 {
5450 /* compare the two files line by line */
5451 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5452 {
5453 differ = FALSE;
5454 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5455 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5456 {
5457 differ = TRUE;
5458 break;
5459 }
5460 }
5461 }
5462 vim_free(ea.cmd);
5463
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 /* restore curwin/curbuf and a few other things */
5465 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
5467 if (curbuf != newbuf) /* safety check */
5468 wipe_buffer(newbuf, FALSE);
5469
5470 return differ;
5471}
5472
5473/*
5474 * Wipe out a buffer and decrement the last buffer number if it was used for
5475 * this buffer. Call this to wipe out a temp buffer that does not contain any
5476 * marks.
5477 */
5478/*ARGSUSED*/
5479 void
5480wipe_buffer(buf, aucmd)
5481 buf_T *buf;
5482 int aucmd; /* When TRUE trigger autocommands. */
5483{
5484 if (buf->b_fnum == top_file_num - 1)
5485 --top_file_num;
5486
5487#ifdef FEAT_AUTOCMD
5488 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5489 ++autocmd_block;
5490#endif
5491 close_buffer(NULL, buf, DOBUF_WIPE);
5492#ifdef FEAT_AUTOCMD
5493 if (!aucmd)
5494 --autocmd_block;
5495#endif
5496}