blob: a26806540bfdd9356e341607a8fe30334067a15c [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
437#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
438 /* Change directories when the acd option is set on. */
439 if (p_acd && curbuf->b_ffname != NULL
440 && vim_chdirfile(curbuf->b_ffname) == OK)
441 shorten_fnames(TRUE);
442#endif
443
444 /*
445 * Remove the buffer from the list.
446 */
447 if (wipe_buf)
448 {
449#ifdef FEAT_SUN_WORKSHOP
450 if (usingSunWorkShop)
451 workshop_file_closed_lineno((char *)buf->b_ffname,
452 (int)buf->b_last_cursor.lnum);
453#endif
454 vim_free(buf->b_ffname);
455 vim_free(buf->b_sfname);
456 if (buf->b_prev == NULL)
457 firstbuf = buf->b_next;
458 else
459 buf->b_prev->b_next = buf->b_next;
460 if (buf->b_next == NULL)
461 lastbuf = buf->b_prev;
462 else
463 buf->b_next->b_prev = buf->b_prev;
464 free_buffer(buf);
465 }
466 else
467 {
468 if (del_buf)
469 {
470 /* Free all internal variables and reset option values, to make
471 * ":bdel" compatible with Vim 5.7. */
472 free_buffer_stuff(buf, TRUE);
473
474 /* Make it look like a new buffer. */
475 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
476
477 /* Init the options when loaded again. */
478 buf->b_p_initialized = FALSE;
479 }
480 buf_clear_file(buf);
481 if (del_buf)
482 buf->b_p_bl = FALSE;
483 }
484}
485
486/*
487 * Make buffer not contain a file.
488 */
489 void
490buf_clear_file(buf)
491 buf_T *buf;
492{
493 buf->b_ml.ml_line_count = 1;
494 unchanged(buf, TRUE);
495#ifndef SHORT_FNAME
496 buf->b_shortname = FALSE;
497#endif
498 buf->b_p_eol = TRUE;
499 buf->b_start_eol = TRUE;
500#ifdef FEAT_MBYTE
501 buf->b_p_bomb = FALSE;
502#endif
503 buf->b_ml.ml_mfp = NULL;
504 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
505#ifdef FEAT_NETBEANS_INTG
506 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507#endif
508}
509
510/*
511 * buf_freeall() - free all things allocated for a buffer that are related to
512 * the file.
513 */
514/*ARGSUSED*/
515 void
516buf_freeall(buf, del_buf, wipe_buf)
517 buf_T *buf;
518 int del_buf; /* buffer is going to be deleted */
519 int wipe_buf; /* buffer is going to be wiped out */
520{
521#ifdef FEAT_AUTOCMD
522 int is_curbuf = (buf == curbuf);
523
524 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
525 if (!buf_valid(buf)) /* autocommands may delete the buffer */
526 return;
527 if (del_buf && buf->b_p_bl)
528 {
529 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
530 if (!buf_valid(buf)) /* autocommands may delete the buffer */
531 return;
532 }
533 if (wipe_buf)
534 {
535 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
536 FALSE, buf);
537 if (!buf_valid(buf)) /* autocommands may delete the buffer */
538 return;
539 }
540# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000541 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 return;
543# endif
544
545 /*
546 * It's possible that autocommands change curbuf to the one being deleted.
547 * This might cause curbuf to be deleted unexpectedly. But in some cases
548 * it's OK to delete the curbuf, because a new one is obtained anyway.
549 * Therefore only return if curbuf changed to the deleted buffer.
550 */
551 if (buf == curbuf && !is_curbuf)
552 return;
553#endif
554#ifdef FEAT_DIFF
555 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
556#endif
557#ifdef FEAT_TCL
558 tcl_buffer_free(buf);
559#endif
560 u_blockfree(buf); /* free the memory allocated for undo */
561 ml_close(buf, TRUE); /* close and delete the memline/memfile */
562 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
563 u_clearall(buf); /* reset all undo information */
564#ifdef FEAT_SYN_HL
565 syntax_clear(buf); /* reset syntax info */
566#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000567 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568}
569
570/*
571 * Free a buffer structure and the things it contains related to the buffer
572 * itself (not the file, that must have been done already).
573 */
574 static void
575free_buffer(buf)
576 buf_T *buf;
577{
578 free_buffer_stuff(buf, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000579#ifdef FEAT_MZSCHEME
580 mzscheme_buffer_free(buf);
581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582#ifdef FEAT_PERL
583 perl_buf_free(buf);
584#endif
585#ifdef FEAT_PYTHON
586 python_buffer_free(buf);
587#endif
588#ifdef FEAT_RUBY
589 ruby_buffer_free(buf);
590#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000591#ifdef FEAT_AUTOCMD
592 aubuflocal_remove(buf);
593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 vim_free(buf);
595}
596
597/*
598 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
599 */
600 static void
601free_buffer_stuff(buf, free_options)
602 buf_T *buf;
603 int free_options; /* free options as well */
604{
605 if (free_options)
606 {
607 clear_wininfo(buf); /* including window-local options */
608 free_buf_options(buf, TRUE);
609 }
610#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000611 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
612 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613#endif
614#ifdef FEAT_USR_CMDS
615 uc_clear(&buf->b_ucmds); /* clear local user commands */
616#endif
617#ifdef FEAT_SIGNS
618 buf_delete_signs(buf); /* delete any signs */
619#endif
620#ifdef FEAT_LOCALMAP
621 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
622 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
623#endif
624#ifdef FEAT_MBYTE
625 vim_free(buf->b_start_fenc);
626 buf->b_start_fenc = NULL;
627#endif
628}
629
630/*
631 * Free the b_wininfo list for buffer "buf".
632 */
633 static void
634clear_wininfo(buf)
635 buf_T *buf;
636{
637 wininfo_T *wip;
638
639 while (buf->b_wininfo != NULL)
640 {
641 wip = buf->b_wininfo;
642 buf->b_wininfo = wip->wi_next;
643 if (wip->wi_optset)
644 {
645 clear_winopt(&wip->wi_opt);
646#ifdef FEAT_FOLDING
647 deleteFoldRecurse(&wip->wi_folds);
648#endif
649 }
650 vim_free(wip);
651 }
652}
653
654#if defined(FEAT_LISTCMDS) || defined(PROTO)
655/*
656 * Go to another buffer. Handles the result of the ATTENTION dialog.
657 */
658 void
659goto_buffer(eap, start, dir, count)
660 exarg_T *eap;
661 int start;
662 int dir;
663 int count;
664{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000665# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 buf_T *old_curbuf = curbuf;
667
668 swap_exists_action = SEA_DIALOG;
669# endif
670 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
671 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000672# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
674 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000675# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
676 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000677
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000678 /* Reset the error/interrupt/exception state here so that
679 * aborting() returns FALSE when closing a window. */
680 enter_cleanup(&cs);
681# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000682
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000683 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 win_close(curwin, TRUE);
685 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000686 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000687
688# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
689 /* Restore the error/interrupt/exception state if not discarded by a
690 * new aborting error, interrupt, or uncaught exception. */
691 leave_cleanup(&cs);
692# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
694 else
695 handle_swap_exists(old_curbuf);
696# endif
697}
698#endif
699
Bram Moolenaare64ac772005-12-07 20:54:59 +0000700#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701/*
702 * Handle the situation of swap_exists_action being set.
703 * It is allowed for "old_curbuf" to be NULL or invalid.
704 */
705 void
706handle_swap_exists(old_curbuf)
707 buf_T *old_curbuf;
708{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000709# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
710 cleanup_T cs;
711# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000712
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 if (swap_exists_action == SEA_QUIT)
714 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000715# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
716 /* Reset the error/interrupt/exception state here so that
717 * aborting() returns FALSE when closing a buffer. */
718 enter_cleanup(&cs);
719# endif
720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 /* User selected Quit at ATTENTION prompt. Go back to previous
722 * buffer. If that buffer is gone or the same as the current one,
723 * open a new, empty buffer. */
724 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000725 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
727 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000728 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 if (old_curbuf != NULL)
730 enter_buffer(old_curbuf);
731 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000732
733# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
734 /* Restore the error/interrupt/exception state if not discarded by a
735 * new aborting error, interrupt, or uncaught exception. */
736 leave_cleanup(&cs);
737# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 }
739 else if (swap_exists_action == SEA_RECOVER)
740 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000741# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
742 /* Reset the error/interrupt/exception state here so that
743 * aborting() returns FALSE when closing a buffer. */
744 enter_cleanup(&cs);
745# endif
746
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 /* User selected Recover at ATTENTION prompt. */
748 msg_scroll = TRUE;
749 ml_recover();
750 MSG_PUTS("\n"); /* don't overwrite the last message */
751 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000752 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000753
754# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
755 /* Restore the error/interrupt/exception state if not discarded by a
756 * new aborting error, interrupt, or uncaught exception. */
757 leave_cleanup(&cs);
758# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 }
760 swap_exists_action = SEA_NONE;
761}
762#endif
763
764#if defined(FEAT_LISTCMDS) || defined(PROTO)
765/*
766 * do_bufdel() - delete or unload buffer(s)
767 *
768 * addr_count == 0: ":bdel" - delete current buffer
769 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
770 * buffer "end_bnr", then any other arguments.
771 * addr_count == 2: ":N,N bdel" - delete buffers in range
772 *
773 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
774 * DOBUF_DEL (":bdel")
775 *
776 * Returns error message or NULL
777 */
778 char_u *
779do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
780 int command;
781 char_u *arg; /* pointer to extra arguments */
782 int addr_count;
783 int start_bnr; /* first buffer number in a range */
784 int end_bnr; /* buffer nr or last buffer nr in a range */
785 int forceit;
786{
787 int do_current = 0; /* delete current buffer? */
788 int deleted = 0; /* number of buffers deleted */
789 char_u *errormsg = NULL; /* return value */
790 int bnr; /* buffer number */
791 char_u *p;
792
793#ifdef FEAT_NETBEANS_INTG
794 netbeansCloseFile = 1;
795#endif
796 if (addr_count == 0)
797 {
798 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
799 }
800 else
801 {
802 if (addr_count == 2)
803 {
804 if (*arg) /* both range and argument is not allowed */
805 return (char_u *)_(e_trailing);
806 bnr = start_bnr;
807 }
808 else /* addr_count == 1 */
809 bnr = end_bnr;
810
811 for ( ;!got_int; ui_breakcheck())
812 {
813 /*
814 * delete the current buffer last, otherwise when the
815 * current buffer is deleted, the next buffer becomes
816 * the current one and will be loaded, which may then
817 * also be deleted, etc.
818 */
819 if (bnr == curbuf->b_fnum)
820 do_current = bnr;
821 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
822 forceit) == OK)
823 ++deleted;
824
825 /*
826 * find next buffer number to delete/unload
827 */
828 if (addr_count == 2)
829 {
830 if (++bnr > end_bnr)
831 break;
832 }
833 else /* addr_count == 1 */
834 {
835 arg = skipwhite(arg);
836 if (*arg == NUL)
837 break;
838 if (!VIM_ISDIGIT(*arg))
839 {
840 p = skiptowhite_esc(arg);
841 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
842 if (bnr < 0) /* failed */
843 break;
844 arg = p;
845 }
846 else
847 bnr = getdigits(&arg);
848 }
849 }
850 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
851 FORWARD, do_current, forceit) == OK)
852 ++deleted;
853
854 if (deleted == 0)
855 {
856 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000857 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000859 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000861 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 errormsg = IObuff;
863 }
864 else if (deleted >= p_report)
865 {
866 if (command == DOBUF_UNLOAD)
867 {
868 if (deleted == 1)
869 MSG(_("1 buffer unloaded"));
870 else
871 smsg((char_u *)_("%d buffers unloaded"), deleted);
872 }
873 else if (command == DOBUF_DEL)
874 {
875 if (deleted == 1)
876 MSG(_("1 buffer deleted"));
877 else
878 smsg((char_u *)_("%d buffers deleted"), deleted);
879 }
880 else
881 {
882 if (deleted == 1)
883 MSG(_("1 buffer wiped out"));
884 else
885 smsg((char_u *)_("%d buffers wiped out"), deleted);
886 }
887 }
888 }
889
890#ifdef FEAT_NETBEANS_INTG
891 netbeansCloseFile = 0;
892#endif
893
894 return errormsg;
895}
896
897/*
898 * Implementation of the commands for the buffer list.
899 *
900 * action == DOBUF_GOTO go to specified buffer
901 * action == DOBUF_SPLIT split window and go to specified buffer
902 * action == DOBUF_UNLOAD unload specified buffer(s)
903 * action == DOBUF_DEL delete specified buffer(s) from buffer list
904 * action == DOBUF_WIPE delete specified buffer(s) really
905 *
906 * start == DOBUF_CURRENT go to "count" buffer from current buffer
907 * start == DOBUF_FIRST go to "count" buffer from first buffer
908 * start == DOBUF_LAST go to "count" buffer from last buffer
909 * start == DOBUF_MOD go to "count" modified buffer from current buffer
910 *
911 * Return FAIL or OK.
912 */
913 int
914do_buffer(action, start, dir, count, forceit)
915 int action;
916 int start;
917 int dir; /* FORWARD or BACKWARD */
918 int count; /* buffer number or number of buffers */
919 int forceit; /* TRUE for :...! */
920{
921 buf_T *buf;
922 buf_T *bp;
923 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
924 || action == DOBUF_WIPE);
925
926 switch (start)
927 {
928 case DOBUF_FIRST: buf = firstbuf; break;
929 case DOBUF_LAST: buf = lastbuf; break;
930 default: buf = curbuf; break;
931 }
932 if (start == DOBUF_MOD) /* find next modified buffer */
933 {
934 while (count-- > 0)
935 {
936 do
937 {
938 buf = buf->b_next;
939 if (buf == NULL)
940 buf = firstbuf;
941 }
942 while (buf != curbuf && !bufIsChanged(buf));
943 }
944 if (!bufIsChanged(buf))
945 {
946 EMSG(_("E84: No modified buffer found"));
947 return FAIL;
948 }
949 }
950 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
951 {
952 while (buf != NULL && buf->b_fnum != count)
953 buf = buf->b_next;
954 }
955 else
956 {
957 bp = NULL;
958 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
959 {
960 /* remember the buffer where we start, we come back there when all
961 * buffers are unlisted. */
962 if (bp == NULL)
963 bp = buf;
964 if (dir == FORWARD)
965 {
966 buf = buf->b_next;
967 if (buf == NULL)
968 buf = firstbuf;
969 }
970 else
971 {
972 buf = buf->b_prev;
973 if (buf == NULL)
974 buf = lastbuf;
975 }
976 /* don't count unlisted buffers */
977 if (unload || buf->b_p_bl)
978 {
979 --count;
980 bp = NULL; /* use this buffer as new starting point */
981 }
982 if (bp == buf)
983 {
984 /* back where we started, didn't find anything. */
985 EMSG(_("E85: There is no listed buffer"));
986 return FAIL;
987 }
988 }
989 }
990
991 if (buf == NULL) /* could not find it */
992 {
993 if (start == DOBUF_FIRST)
994 {
995 /* don't warn when deleting */
996 if (!unload)
997 EMSGN(_("E86: Buffer %ld does not exist"), count);
998 }
999 else if (dir == FORWARD)
1000 EMSG(_("E87: Cannot go beyond last buffer"));
1001 else
1002 EMSG(_("E88: Cannot go before first buffer"));
1003 return FAIL;
1004 }
1005
1006#ifdef FEAT_GUI
1007 need_mouse_correct = TRUE;
1008#endif
1009
1010#ifdef FEAT_LISTCMDS
1011 /*
1012 * delete buffer buf from memory and/or the list
1013 */
1014 if (unload)
1015 {
1016 int forward;
1017 int retval;
1018
1019 /* When unloading or deleting a buffer that's already unloaded and
1020 * unlisted: fail silently. */
1021 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1022 return FAIL;
1023
1024 if (!forceit && bufIsChanged(buf))
1025 {
1026#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1027 if ((p_confirm || cmdmod.confirm) && p_write)
1028 {
1029 dialog_changed(buf, FALSE);
1030# ifdef FEAT_AUTOCMD
1031 if (!buf_valid(buf))
1032 /* Autocommand deleted buffer, oops! It's not changed
1033 * now. */
1034 return FAIL;
1035# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001036 /* If it's still changed fail silently, the dialog already
1037 * mentioned why it fails. */
1038 if (bufIsChanged(buf))
1039 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001041 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042#endif
1043 {
1044 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1045 buf->b_fnum);
1046 return FAIL;
1047 }
1048 }
1049
1050 /*
1051 * If deleting the last (listed) buffer, make it empty.
1052 * The last (listed) buffer cannot be unloaded.
1053 */
1054 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1055 if (bp->b_p_bl && bp != buf)
1056 break;
1057 if (bp == NULL && buf == curbuf)
1058 {
1059 if (action == DOBUF_UNLOAD)
1060 {
1061 EMSG(_("E90: Cannot unload last buffer"));
1062 return FAIL;
1063 }
1064
1065 /* Close any other windows on this buffer, then make it empty. */
1066#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001067 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068#endif
1069 setpcmark();
1070 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1071 forceit ? ECMD_FORCEIT : 0);
1072
1073 /*
1074 * do_ecmd() may create a new buffer, then we have to delete
1075 * the old one. But do_ecmd() may have done that already, check
1076 * if the buffer still exists.
1077 */
1078 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1079 close_buffer(NULL, buf, action);
1080 return retval;
1081 }
1082
1083#ifdef FEAT_WINDOWS
1084 /*
1085 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001086 * (unless it's the only window). Repeat this so long as we end up in
1087 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001089 while (buf == curbuf
1090 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 win_close(curwin, FALSE);
1092#endif
1093
1094 /*
1095 * If the buffer to be deleted is not the current one, delete it here.
1096 */
1097 if (buf != curbuf)
1098 {
1099#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001100 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101#endif
1102 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1103 close_buffer(NULL, buf, action);
1104 return OK;
1105 }
1106
1107 /*
1108 * Deleting the current buffer: Need to find another buffer to go to.
1109 * There must be another, otherwise it would have been handled above.
1110 * First use au_new_curbuf, if it is valid.
1111 * Then prefer the buffer we most recently visited.
1112 * Else try to find one that is loaded, after the current buffer,
1113 * then before the current buffer.
1114 * Finally use any buffer.
1115 */
1116 buf = NULL; /* selected buffer */
1117 bp = NULL; /* used when no loaded buffer found */
1118#ifdef FEAT_AUTOCMD
1119 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1120 buf = au_new_curbuf;
1121# ifdef FEAT_JUMPLIST
1122 else
1123# endif
1124#endif
1125#ifdef FEAT_JUMPLIST
1126 if (curwin->w_jumplistlen > 0)
1127 {
1128 int jumpidx;
1129
1130 jumpidx = curwin->w_jumplistidx - 1;
1131 if (jumpidx < 0)
1132 jumpidx = curwin->w_jumplistlen - 1;
1133
1134 forward = jumpidx;
1135 while (jumpidx != curwin->w_jumplistidx)
1136 {
1137 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1138 if (buf != NULL)
1139 {
1140 if (buf == curbuf || !buf->b_p_bl)
1141 buf = NULL; /* skip current and unlisted bufs */
1142 else if (buf->b_ml.ml_mfp == NULL)
1143 {
1144 /* skip unloaded buf, but may keep it for later */
1145 if (bp == NULL)
1146 bp = buf;
1147 buf = NULL;
1148 }
1149 }
1150 if (buf != NULL) /* found a valid buffer: stop searching */
1151 break;
1152 /* advance to older entry in jump list */
1153 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1154 break;
1155 if (--jumpidx < 0)
1156 jumpidx = curwin->w_jumplistlen - 1;
1157 if (jumpidx == forward) /* List exhausted for sure */
1158 break;
1159 }
1160 }
1161#endif
1162
1163 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1164 {
1165 forward = TRUE;
1166 buf = curbuf->b_next;
1167 for (;;)
1168 {
1169 if (buf == NULL)
1170 {
1171 if (!forward) /* tried both directions */
1172 break;
1173 buf = curbuf->b_prev;
1174 forward = FALSE;
1175 continue;
1176 }
1177 /* in non-help buffer, try to skip help buffers, and vv */
1178 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1179 {
1180 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1181 break;
1182 if (bp == NULL) /* remember unloaded buf for later */
1183 bp = buf;
1184 }
1185 if (forward)
1186 buf = buf->b_next;
1187 else
1188 buf = buf->b_prev;
1189 }
1190 }
1191 if (buf == NULL) /* No loaded buffer, use unloaded one */
1192 buf = bp;
1193 if (buf == NULL) /* No loaded buffer, find listed one */
1194 {
1195 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1196 if (buf->b_p_bl && buf != curbuf)
1197 break;
1198 }
1199 if (buf == NULL) /* Still no buffer, just take one */
1200 {
1201 if (curbuf->b_next != NULL)
1202 buf = curbuf->b_next;
1203 else
1204 buf = curbuf->b_prev;
1205 }
1206 }
1207
1208 /*
1209 * make buf current buffer
1210 */
1211 if (action == DOBUF_SPLIT) /* split window first */
1212 {
1213# ifdef FEAT_WINDOWS
1214 /* jump to first window containing buf if one exists ("useopen") */
1215 if (vim_strchr(p_swb, 'u') && buf_jump_open_win(buf))
1216 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)
1319 u_sync();
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 Moolenaar071d4272004-06-13 20:20:40 +00001421#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
1422 /* Change directories when the acd option is set on. */
1423 if (p_acd && curbuf->b_ffname != NULL
1424 && vim_chdirfile(curbuf->b_ffname) == OK)
1425 shorten_fnames(TRUE);
1426#endif
1427
1428#ifdef FEAT_KEYMAP
1429 if (curbuf->b_kmap_state & KEYMAP_INIT)
1430 keymap_init();
1431#endif
1432 redraw_later(NOT_VALID);
1433}
1434
1435/*
1436 * functions for dealing with the buffer list
1437 */
1438
1439/*
1440 * Add a file name to the buffer list. Return a pointer to the buffer.
1441 * If the same file name already exists return a pointer to that buffer.
1442 * If it does not exist, or if fname == NULL, a new entry is created.
1443 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1444 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1445 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 * This is the ONLY way to create a new buffer.
1447 */
1448static int top_file_num = 1; /* highest file number */
1449
1450 buf_T *
1451buflist_new(ffname, sfname, lnum, flags)
1452 char_u *ffname; /* full path of fname or relative */
1453 char_u *sfname; /* short fname or NULL */
1454 linenr_T lnum; /* preferred cursor line */
1455 int flags; /* BLN_ defines */
1456{
1457 buf_T *buf;
1458#ifdef UNIX
1459 struct stat st;
1460#endif
1461
1462 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1463
1464 /*
1465 * If file name already exists in the list, update the entry.
1466 */
1467#ifdef UNIX
1468 /* On Unix we can use inode numbers when the file exists. Works better
1469 * for hard links. */
1470 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1471 st.st_dev = (dev_T)-1;
1472#endif
1473 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1474#ifdef UNIX
1475 buflist_findname_stat(ffname, &st)
1476#else
1477 buflist_findname(ffname)
1478#endif
1479 ) != NULL)
1480 {
1481 vim_free(ffname);
1482 if (lnum != 0)
1483 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1484 /* copy the options now, if 'cpo' doesn't have 's' and not done
1485 * already */
1486 buf_copy_options(buf, 0);
1487 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1488 {
1489 buf->b_p_bl = TRUE;
1490#ifdef FEAT_AUTOCMD
1491 if (!(flags & BLN_DUMMY))
1492 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1493#endif
1494 }
1495 return buf;
1496 }
1497
1498 /*
1499 * If the current buffer has no name and no contents, use the current
1500 * buffer. Otherwise: Need to allocate a new buffer structure.
1501 *
1502 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001503 * (A spell file buffer is allocated in spell.c, but that's not a normal
1504 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 */
1506 buf = NULL;
1507 if ((flags & BLN_CURBUF)
1508 && curbuf != NULL
1509 && curbuf->b_ffname == NULL
1510 && curbuf->b_nwindows <= 1
1511 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1512 {
1513 buf = curbuf;
1514#ifdef FEAT_AUTOCMD
1515 /* It's like this buffer is deleted. Watch out for autocommands that
1516 * change curbuf! If that happens, allocate a new buffer anyway. */
1517 if (curbuf->b_p_bl)
1518 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1519 if (buf == curbuf)
1520 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1521# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001522 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 return NULL;
1524# endif
1525#endif
1526#ifdef FEAT_QUICKFIX
1527# ifdef FEAT_AUTOCMD
1528 if (buf == curbuf)
1529# endif
1530 {
1531 /* Make sure 'bufhidden' and 'buftype' are empty */
1532 clear_string_option(&buf->b_p_bh);
1533 clear_string_option(&buf->b_p_bt);
1534 }
1535#endif
1536 }
1537 if (buf != curbuf || curbuf == NULL)
1538 {
1539 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1540 if (buf == NULL)
1541 {
1542 vim_free(ffname);
1543 return NULL;
1544 }
1545 }
1546
1547 if (ffname != NULL)
1548 {
1549 buf->b_ffname = ffname;
1550 buf->b_sfname = vim_strsave(sfname);
1551 }
1552
1553 clear_wininfo(buf);
1554 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1555
1556 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1557 || buf->b_wininfo == NULL)
1558 {
1559 vim_free(buf->b_ffname);
1560 buf->b_ffname = NULL;
1561 vim_free(buf->b_sfname);
1562 buf->b_sfname = NULL;
1563 if (buf != curbuf)
1564 free_buffer(buf);
1565 return NULL;
1566 }
1567
1568 if (buf == curbuf)
1569 {
1570 /* free all things allocated for this buffer */
1571 buf_freeall(buf, FALSE, FALSE);
1572 if (buf != curbuf) /* autocommands deleted the buffer! */
1573 return NULL;
1574#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001575 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 return NULL;
1577#endif
1578 /* buf->b_nwindows = 0; why was this here? */
1579 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1580#ifdef FEAT_KEYMAP
1581 /* need to reload lmaps and set b:keymap_name */
1582 curbuf->b_kmap_state |= KEYMAP_INIT;
1583#endif
1584 }
1585 else
1586 {
1587 /*
1588 * put new buffer at the end of the buffer list
1589 */
1590 buf->b_next = NULL;
1591 if (firstbuf == NULL) /* buffer list is empty */
1592 {
1593 buf->b_prev = NULL;
1594 firstbuf = buf;
1595 }
1596 else /* append new buffer at end of list */
1597 {
1598 lastbuf->b_next = buf;
1599 buf->b_prev = lastbuf;
1600 }
1601 lastbuf = buf;
1602
1603 buf->b_fnum = top_file_num++;
1604 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1605 {
1606 EMSG(_("W14: Warning: List of file names overflow"));
1607 if (emsg_silent == 0)
1608 {
1609 out_flush();
1610 ui_delay(3000L, TRUE); /* make sure it is noticed */
1611 }
1612 top_file_num = 1;
1613 }
1614
1615 /*
1616 * Always copy the options from the current buffer.
1617 */
1618 buf_copy_options(buf, BCO_ALWAYS);
1619 }
1620
1621 buf->b_wininfo->wi_fpos.lnum = lnum;
1622 buf->b_wininfo->wi_win = curwin;
1623
1624#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001625 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1626#endif
1627#ifdef FEAT_SYN_HL
1628 hash_init(&buf->b_keywtab);
1629 hash_init(&buf->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#endif
1631
1632 buf->b_fname = buf->b_sfname;
1633#ifdef UNIX
1634 if (st.st_dev == (dev_T)-1)
1635 buf->b_dev = -1;
1636 else
1637 {
1638 buf->b_dev = st.st_dev;
1639 buf->b_ino = st.st_ino;
1640 }
1641#endif
1642 buf->b_u_synced = TRUE;
1643 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001644 if (flags & BLN_DUMMY)
1645 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 buf_clear_file(buf);
1647 clrallmarks(buf); /* clear marks */
1648 fmarks_check_names(buf); /* check file marks for this file */
1649 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1650#ifdef FEAT_AUTOCMD
1651 if (!(flags & BLN_DUMMY))
1652 {
1653 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1654 if (flags & BLN_LISTED)
1655 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1656# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001657 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 return NULL;
1659# endif
1660 }
1661#endif
1662
1663 return buf;
1664}
1665
1666/*
1667 * Free the memory for the options of a buffer.
1668 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1669 * 'fileencoding'.
1670 */
1671 void
1672free_buf_options(buf, free_p_ff)
1673 buf_T *buf;
1674 int free_p_ff;
1675{
1676 if (free_p_ff)
1677 {
1678#ifdef FEAT_MBYTE
1679 clear_string_option(&buf->b_p_fenc);
1680#endif
1681 clear_string_option(&buf->b_p_ff);
1682#ifdef FEAT_QUICKFIX
1683 clear_string_option(&buf->b_p_bh);
1684 clear_string_option(&buf->b_p_bt);
1685#endif
1686 }
1687#ifdef FEAT_FIND_ID
1688 clear_string_option(&buf->b_p_def);
1689 clear_string_option(&buf->b_p_inc);
1690# ifdef FEAT_EVAL
1691 clear_string_option(&buf->b_p_inex);
1692# endif
1693#endif
1694#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1695 clear_string_option(&buf->b_p_inde);
1696 clear_string_option(&buf->b_p_indk);
1697#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001698#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1699 clear_string_option(&buf->b_p_bexpr);
1700#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001701#if defined(FEAT_EVAL)
1702 clear_string_option(&buf->b_p_fex);
1703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704#ifdef FEAT_CRYPT
1705 clear_string_option(&buf->b_p_key);
1706#endif
1707 clear_string_option(&buf->b_p_kp);
1708 clear_string_option(&buf->b_p_mps);
1709 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001710 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 clear_string_option(&buf->b_p_isk);
1712#ifdef FEAT_KEYMAP
1713 clear_string_option(&buf->b_p_keymap);
1714 ga_clear(&buf->b_kmap_ga);
1715#endif
1716#ifdef FEAT_COMMENTS
1717 clear_string_option(&buf->b_p_com);
1718#endif
1719#ifdef FEAT_FOLDING
1720 clear_string_option(&buf->b_p_cms);
1721#endif
1722 clear_string_option(&buf->b_p_nf);
1723#ifdef FEAT_SYN_HL
1724 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001725#endif
1726#ifdef FEAT_SPELL
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001727 clear_string_option(&buf->b_p_spc);
Bram Moolenaar86bc1fb2005-06-07 20:58:01 +00001728 clear_string_option(&buf->b_p_spf);
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001729 vim_free(buf->b_cap_prog);
1730 buf->b_cap_prog = NULL;
Bram Moolenaara0dea672005-03-20 22:23:10 +00001731 clear_string_option(&buf->b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732#endif
1733#ifdef FEAT_SEARCHPATH
1734 clear_string_option(&buf->b_p_sua);
1735#endif
1736#ifdef FEAT_AUTOCMD
1737 clear_string_option(&buf->b_p_ft);
1738#endif
1739#ifdef FEAT_OSFILETYPE
1740 clear_string_option(&buf->b_p_oft);
1741#endif
1742#ifdef FEAT_CINDENT
1743 clear_string_option(&buf->b_p_cink);
1744 clear_string_option(&buf->b_p_cino);
1745#endif
1746#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1747 clear_string_option(&buf->b_p_cinw);
1748#endif
1749#ifdef FEAT_INS_EXPAND
1750 clear_string_option(&buf->b_p_cpt);
1751#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001752#ifdef FEAT_COMPL_FUNC
1753 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001754 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756#ifdef FEAT_QUICKFIX
1757 clear_string_option(&buf->b_p_gp);
1758 clear_string_option(&buf->b_p_mp);
1759 clear_string_option(&buf->b_p_efm);
1760#endif
1761 clear_string_option(&buf->b_p_ep);
1762 clear_string_option(&buf->b_p_path);
1763 clear_string_option(&buf->b_p_tags);
1764#ifdef FEAT_INS_EXPAND
1765 clear_string_option(&buf->b_p_dict);
1766 clear_string_option(&buf->b_p_tsr);
1767#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001768#ifdef FEAT_TEXTOBJ
1769 clear_string_option(&buf->b_p_qe);
1770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 buf->b_p_ar = -1;
1772}
1773
1774/*
1775 * get alternate file n
1776 * set linenr to lnum or altfpos.lnum if lnum == 0
1777 * also set cursor column to altfpos.col if 'startofline' is not set.
1778 * if (options & GETF_SETMARK) call setpcmark()
1779 * if (options & GETF_ALT) we are jumping to an alternate file.
1780 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1781 *
1782 * return FAIL for failure, OK for success
1783 */
1784 int
1785buflist_getfile(n, lnum, options, forceit)
1786 int n;
1787 linenr_T lnum;
1788 int options;
1789 int forceit;
1790{
1791 buf_T *buf;
1792#ifdef FEAT_WINDOWS
1793 win_T *wp = NULL;
1794#endif
1795 pos_T *fpos;
1796 colnr_T col;
1797
1798 buf = buflist_findnr(n);
1799 if (buf == NULL)
1800 {
1801 if ((options & GETF_ALT) && n == 0)
1802 EMSG(_(e_noalt));
1803 else
1804 EMSGN(_("E92: Buffer %ld not found"), n);
1805 return FAIL;
1806 }
1807
1808 /* if alternate file is the current buffer, nothing to do */
1809 if (buf == curbuf)
1810 return OK;
1811
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001812 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001813 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001814 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818 /* altfpos may be changed by getfile(), get it now */
1819 if (lnum == 0)
1820 {
1821 fpos = buflist_findfpos(buf);
1822 lnum = fpos->lnum;
1823 col = fpos->col;
1824 }
1825 else
1826 col = 0;
1827
1828#ifdef FEAT_WINDOWS
1829 if (options & GETF_SWITCH)
1830 {
1831 /* use existing open window for buffer if wanted */
1832 if (vim_strchr(p_swb, 'u')) /* useopen */
1833 wp = buf_jump_open_win(buf);
1834 /* split window if wanted ("split") */
1835 if (wp == NULL && vim_strchr(p_swb, 't') && !bufempty())
1836 {
1837 if (win_split(0, 0) == FAIL)
1838 return FAIL;
1839# ifdef FEAT_SCROLLBIND
1840 curwin->w_p_scb = FALSE;
1841# endif
1842 }
1843 }
1844#endif
1845
1846 ++RedrawingDisabled;
1847 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1848 lnum, forceit) <= 0)
1849 {
1850 --RedrawingDisabled;
1851
1852 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1853 if (!p_sol && col != 0)
1854 {
1855 curwin->w_cursor.col = col;
1856 check_cursor_col();
1857#ifdef FEAT_VIRTUALEDIT
1858 curwin->w_cursor.coladd = 0;
1859#endif
1860 curwin->w_set_curswant = TRUE;
1861 }
1862 return OK;
1863 }
1864 --RedrawingDisabled;
1865 return FAIL;
1866}
1867
1868/*
1869 * go to the last know line number for the current buffer
1870 */
1871 void
1872buflist_getfpos()
1873{
1874 pos_T *fpos;
1875
1876 fpos = buflist_findfpos(curbuf);
1877
1878 curwin->w_cursor.lnum = fpos->lnum;
1879 check_cursor_lnum();
1880
1881 if (p_sol)
1882 curwin->w_cursor.col = 0;
1883 else
1884 {
1885 curwin->w_cursor.col = fpos->col;
1886 check_cursor_col();
1887#ifdef FEAT_VIRTUALEDIT
1888 curwin->w_cursor.coladd = 0;
1889#endif
1890 curwin->w_set_curswant = TRUE;
1891 }
1892}
1893
Bram Moolenaar81695252004-12-29 20:58:21 +00001894#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1895/*
1896 * Find file in buffer list by name (it has to be for the current window).
1897 * Returns NULL if not found.
1898 */
1899 buf_T *
1900buflist_findname_exp(fname)
1901 char_u *fname;
1902{
1903 char_u *ffname;
1904 buf_T *buf = NULL;
1905
1906 /* First make the name into a full path name */
1907 ffname = FullName_save(fname,
1908#ifdef UNIX
1909 TRUE /* force expansion, get rid of symbolic links */
1910#else
1911 FALSE
1912#endif
1913 );
1914 if (ffname != NULL)
1915 {
1916 buf = buflist_findname(ffname);
1917 vim_free(ffname);
1918 }
1919 return buf;
1920}
1921#endif
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923/*
1924 * Find file in buffer list by name (it has to be for the current window).
1925 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00001926 * Skips dummy buffers.
1927 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 */
1929 buf_T *
1930buflist_findname(ffname)
1931 char_u *ffname;
1932{
1933#ifdef UNIX
1934 struct stat st;
1935
1936 if (mch_stat((char *)ffname, &st) < 0)
1937 st.st_dev = (dev_T)-1;
1938 return buflist_findname_stat(ffname, &st);
1939}
1940
1941/*
1942 * Same as buflist_findname(), but pass the stat structure to avoid getting it
1943 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00001944 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 */
1946 static buf_T *
1947buflist_findname_stat(ffname, stp)
1948 char_u *ffname;
1949 struct stat *stp;
1950{
1951#endif
1952 buf_T *buf;
1953
1954 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00001955 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956#ifdef UNIX
1957 , stp
1958#endif
1959 ))
1960 return buf;
1961 return NULL;
1962}
1963
1964#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
1965/*
1966 * Find file in buffer list by a regexp pattern.
1967 * Return fnum of the found buffer.
1968 * Return < 0 for error.
1969 */
1970/*ARGSUSED*/
1971 int
1972buflist_findpat(pattern, pattern_end, unlisted, diffmode)
1973 char_u *pattern;
1974 char_u *pattern_end; /* pointer to first char after pattern */
1975 int unlisted; /* find unlisted buffers */
1976 int diffmode; /* find diff-mode buffers only */
1977{
1978 buf_T *buf;
1979 regprog_T *prog;
1980 int match = -1;
1981 int find_listed;
1982 char_u *pat;
1983 char_u *patend;
1984 int attempt;
1985 char_u *p;
1986 int toggledollar;
1987
1988 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
1989 {
1990 if (*pattern == '%')
1991 match = curbuf->b_fnum;
1992 else
1993 match = curwin->w_alt_fnum;
1994#ifdef FEAT_DIFF
1995 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
1996 match = -1;
1997#endif
1998 }
1999
2000 /*
2001 * Try four ways of matching a listed buffer:
2002 * attempt == 0: without '^' or '$' (at any position)
2003 * attempt == 1: with '^' at start (only at postion 0)
2004 * attempt == 2: with '$' at end (only match at end)
2005 * attempt == 3: with '^' at start and '$' at end (only full match)
2006 * Repeat this for finding an unlisted buffer if there was no matching
2007 * listed buffer.
2008 */
2009 else
2010 {
2011 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2012 if (pat == NULL)
2013 return -1;
2014 patend = pat + STRLEN(pat) - 1;
2015 toggledollar = (patend > pat && *patend == '$');
2016
2017 /* First try finding a listed buffer. If not found and "unlisted"
2018 * is TRUE, try finding an unlisted buffer. */
2019 find_listed = TRUE;
2020 for (;;)
2021 {
2022 for (attempt = 0; attempt <= 3; ++attempt)
2023 {
2024 /* may add '^' and '$' */
2025 if (toggledollar)
2026 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2027 p = pat;
2028 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2029 ++p;
2030 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2031 if (prog == NULL)
2032 {
2033 vim_free(pat);
2034 return -1;
2035 }
2036
2037 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2038 if (buf->b_p_bl == find_listed
2039#ifdef FEAT_DIFF
2040 && (!diffmode || diff_mode_buf(buf))
2041#endif
2042 && buflist_match(prog, buf) != NULL)
2043 {
2044 if (match >= 0) /* already found a match */
2045 {
2046 match = -2;
2047 break;
2048 }
2049 match = buf->b_fnum; /* remember first match */
2050 }
2051
2052 vim_free(prog);
2053 if (match >= 0) /* found one match */
2054 break;
2055 }
2056
2057 /* Only search for unlisted buffers if there was no match with
2058 * a listed buffer. */
2059 if (!unlisted || !find_listed || match != -1)
2060 break;
2061 find_listed = FALSE;
2062 }
2063
2064 vim_free(pat);
2065 }
2066
2067 if (match == -2)
2068 EMSG2(_("E93: More than one match for %s"), pattern);
2069 else if (match < 0)
2070 EMSG2(_("E94: No matching buffer for %s"), pattern);
2071 return match;
2072}
2073#endif
2074
2075#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2076
2077/*
2078 * Find all buffer names that match.
2079 * For command line expansion of ":buf" and ":sbuf".
2080 * Return OK if matches found, FAIL otherwise.
2081 */
2082 int
2083ExpandBufnames(pat, num_file, file, options)
2084 char_u *pat;
2085 int *num_file;
2086 char_u ***file;
2087 int options;
2088{
2089 int count = 0;
2090 buf_T *buf;
2091 int round;
2092 char_u *p;
2093 int attempt;
2094 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002095 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096
2097 *num_file = 0; /* return values in case of FAIL */
2098 *file = NULL;
2099
Bram Moolenaar05159a02005-02-26 23:04:13 +00002100 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2101 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002103 patc = alloc((unsigned)STRLEN(pat) + 11);
2104 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002106 STRCPY(patc, "\\(^\\|[\\/]\\)");
2107 STRCPY(patc + 11, pat + 1);
2108 }
2109 else
2110 patc = pat;
2111
2112 /*
2113 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002114 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002115 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002116 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002117 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002118 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002119 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002120 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002121 if (prog == NULL)
2122 {
2123 if (patc != pat)
2124 vim_free(patc);
2125 return FAIL;
2126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127
2128 /*
2129 * round == 1: Count the matches.
2130 * round == 2: Build the array to keep the matches.
2131 */
2132 for (round = 1; round <= 2; ++round)
2133 {
2134 count = 0;
2135 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2136 {
2137 if (!buf->b_p_bl) /* skip unlisted buffers */
2138 continue;
2139 p = buflist_match(prog, buf);
2140 if (p != NULL)
2141 {
2142 if (round == 1)
2143 ++count;
2144 else
2145 {
2146 if (options & WILD_HOME_REPLACE)
2147 p = home_replace_save(buf, p);
2148 else
2149 p = vim_strsave(p);
2150 (*file)[count++] = p;
2151 }
2152 }
2153 }
2154 if (count == 0) /* no match found, break here */
2155 break;
2156 if (round == 1)
2157 {
2158 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2159 if (*file == NULL)
2160 {
2161 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002162 if (patc != pat)
2163 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 return FAIL;
2165 }
2166 }
2167 }
2168 vim_free(prog);
2169 if (count) /* match(es) found, break here */
2170 break;
2171 }
2172
Bram Moolenaar05159a02005-02-26 23:04:13 +00002173 if (patc != pat)
2174 vim_free(patc);
2175
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 *num_file = count;
2177 return (count == 0 ? FAIL : OK);
2178}
2179
2180#endif /* FEAT_CMDL_COMPL */
2181
2182#ifdef HAVE_BUFLIST_MATCH
2183/*
2184 * Check for a match on the file name for buffer "buf" with regprog "prog".
2185 */
2186 static char_u *
2187buflist_match(prog, buf)
2188 regprog_T *prog;
2189 buf_T *buf;
2190{
2191 char_u *match;
2192
2193 /* First try the short file name, then the long file name. */
2194 match = fname_match(prog, buf->b_sfname);
2195 if (match == NULL)
2196 match = fname_match(prog, buf->b_ffname);
2197
2198 return match;
2199}
2200
2201/*
2202 * Try matching the regexp in "prog" with file name "name".
2203 * Return "name" when there is a match, NULL when not.
2204 */
2205 static char_u *
2206fname_match(prog, name)
2207 regprog_T *prog;
2208 char_u *name;
2209{
2210 char_u *match = NULL;
2211 char_u *p;
2212 regmatch_T regmatch;
2213
2214 if (name != NULL)
2215 {
2216 regmatch.regprog = prog;
2217#ifdef CASE_INSENSITIVE_FILENAME
2218 regmatch.rm_ic = TRUE; /* Always ignore case */
2219#else
2220 regmatch.rm_ic = FALSE; /* Never ignore case */
2221#endif
2222
2223 if (vim_regexec(&regmatch, name, (colnr_T)0))
2224 match = name;
2225 else
2226 {
2227 /* Replace $(HOME) with '~' and try matching again. */
2228 p = home_replace_save(NULL, name);
2229 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2230 match = name;
2231 vim_free(p);
2232 }
2233 }
2234
2235 return match;
2236}
2237#endif
2238
2239/*
2240 * find file in buffer list by number
2241 */
2242 buf_T *
2243buflist_findnr(nr)
2244 int nr;
2245{
2246 buf_T *buf;
2247
2248 if (nr == 0)
2249 nr = curwin->w_alt_fnum;
2250 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2251 if (buf->b_fnum == nr)
2252 return (buf);
2253 return NULL;
2254}
2255
2256/*
2257 * Get name of file 'n' in the buffer list.
2258 * When the file has no name an empty string is returned.
2259 * home_replace() is used to shorten the file name (used for marks).
2260 * Returns a pointer to allocated memory, of NULL when failed.
2261 */
2262 char_u *
2263buflist_nr2name(n, fullname, helptail)
2264 int n;
2265 int fullname;
2266 int helptail; /* for help buffers return tail only */
2267{
2268 buf_T *buf;
2269
2270 buf = buflist_findnr(n);
2271 if (buf == NULL)
2272 return NULL;
2273 return home_replace_save(helptail ? buf : NULL,
2274 fullname ? buf->b_ffname : buf->b_fname);
2275}
2276
2277/*
2278 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2279 * When "copy_options" is TRUE save the local window option values.
2280 * When "lnum" is 0 only do the options.
2281 */
2282 static void
2283buflist_setfpos(buf, win, lnum, col, copy_options)
2284 buf_T *buf;
2285 win_T *win;
2286 linenr_T lnum;
2287 colnr_T col;
2288 int copy_options;
2289{
2290 wininfo_T *wip;
2291
2292 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2293 if (wip->wi_win == win)
2294 break;
2295 if (wip == NULL)
2296 {
2297 /* allocate a new entry */
2298 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2299 if (wip == NULL)
2300 return;
2301 wip->wi_win = win;
2302 if (lnum == 0) /* set lnum even when it's 0 */
2303 lnum = 1;
2304 }
2305 else
2306 {
2307 /* remove the entry from the list */
2308 if (wip->wi_prev)
2309 wip->wi_prev->wi_next = wip->wi_next;
2310 else
2311 buf->b_wininfo = wip->wi_next;
2312 if (wip->wi_next)
2313 wip->wi_next->wi_prev = wip->wi_prev;
2314 if (copy_options && wip->wi_optset)
2315 {
2316 clear_winopt(&wip->wi_opt);
2317#ifdef FEAT_FOLDING
2318 deleteFoldRecurse(&wip->wi_folds);
2319#endif
2320 }
2321 }
2322 if (lnum != 0)
2323 {
2324 wip->wi_fpos.lnum = lnum;
2325 wip->wi_fpos.col = col;
2326 }
2327 if (copy_options)
2328 {
2329 /* Save the window-specific option values. */
2330 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2331#ifdef FEAT_FOLDING
2332 wip->wi_fold_manual = win->w_fold_manual;
2333 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2334#endif
2335 wip->wi_optset = TRUE;
2336 }
2337
2338 /* insert the entry in front of the list */
2339 wip->wi_next = buf->b_wininfo;
2340 buf->b_wininfo = wip;
2341 wip->wi_prev = NULL;
2342 if (wip->wi_next)
2343 wip->wi_next->wi_prev = wip;
2344
2345 return;
2346}
2347
2348/*
2349 * Find info for the current window in buffer "buf".
2350 * If not found, return the info for the most recently used window.
2351 * Returns NULL when there isn't any info.
2352 */
2353 static wininfo_T *
2354find_wininfo(buf)
2355 buf_T *buf;
2356{
2357 wininfo_T *wip;
2358
2359 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2360 if (wip->wi_win == curwin)
2361 break;
2362 if (wip == NULL) /* if no fpos for curwin, use the first in the list */
2363 wip = buf->b_wininfo;
2364 return wip;
2365}
2366
2367/*
2368 * Reset the local window options to the values last used in this window.
2369 * If the buffer wasn't used in this window before, use the values from
2370 * the most recently used window. If the values were never set, use the
2371 * global values for the window.
2372 */
2373 void
2374get_winopts(buf)
2375 buf_T *buf;
2376{
2377 wininfo_T *wip;
2378
2379 clear_winopt(&curwin->w_onebuf_opt);
2380#ifdef FEAT_FOLDING
2381 clearFolding(curwin);
2382#endif
2383
2384 wip = find_wininfo(buf);
2385 if (wip != NULL && wip->wi_optset)
2386 {
2387 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2388#ifdef FEAT_FOLDING
2389 curwin->w_fold_manual = wip->wi_fold_manual;
2390 curwin->w_foldinvalid = TRUE;
2391 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2392#endif
2393 }
2394 else
2395 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2396
2397#ifdef FEAT_FOLDING
2398 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2399 if (p_fdls >= 0)
2400 curwin->w_p_fdl = p_fdls;
2401#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002402
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002403#ifdef FEAT_SPELL
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002404 if (curwin->w_p_spell && *buf->b_p_spl != NUL)
2405 did_set_spelllang(buf);
2406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407}
2408
2409/*
2410 * Find the position (lnum and col) for the buffer 'buf' for the current
2411 * window.
2412 * Returns a pointer to no_position if no position is found.
2413 */
2414 pos_T *
2415buflist_findfpos(buf)
2416 buf_T *buf;
2417{
2418 wininfo_T *wip;
2419 static pos_T no_position = {1, 0};
2420
2421 wip = find_wininfo(buf);
2422 if (wip != NULL)
2423 return &(wip->wi_fpos);
2424 else
2425 return &no_position;
2426}
2427
2428/*
2429 * Find the lnum for the buffer 'buf' for the current window.
2430 */
2431 linenr_T
2432buflist_findlnum(buf)
2433 buf_T *buf;
2434{
2435 return buflist_findfpos(buf)->lnum;
2436}
2437
2438#if defined(FEAT_LISTCMDS) || defined(PROTO)
2439/*
2440 * List all know file names (for :files and :buffers command).
2441 */
2442/*ARGSUSED*/
2443 void
2444buflist_list(eap)
2445 exarg_T *eap;
2446{
2447 buf_T *buf;
2448 int len;
2449 int i;
2450
2451 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2452 {
2453 /* skip unlisted buffers, unless ! was used */
2454 if (!buf->b_p_bl && !eap->forceit)
2455 continue;
2456 msg_putchar('\n');
2457 if (buf_spname(buf) != NULL)
2458 STRCPY(NameBuff, buf_spname(buf));
2459 else
2460 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2461
Bram Moolenaar50cde822005-06-05 21:54:54 +00002462 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 buf->b_fnum,
2464 buf->b_p_bl ? ' ' : 'u',
2465 buf == curbuf ? '%' :
2466 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2467 buf->b_ml.ml_mfp == NULL ? ' ' :
2468 (buf->b_nwindows == 0 ? 'h' : 'a'),
2469 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2470 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002471 : (bufIsChanged(buf) ? '+' : ' '),
2472 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 i = 40 - vim_strsize(IObuff);
2476 do
2477 {
2478 IObuff[len++] = ' ';
2479 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002480 vim_snprintf((char *)IObuff + len, IOSIZE - len, _("line %ld"),
2481 buf == curbuf ? curwin->w_cursor.lnum
2482 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 msg_outtrans(IObuff);
2484 out_flush(); /* output one line at a time */
2485 ui_breakcheck();
2486 }
2487}
2488#endif
2489
2490/*
2491 * Get file name and line number for file 'fnum'.
2492 * Used by DoOneCmd() for translating '%' and '#'.
2493 * Used by insert_reg() and cmdline_paste() for '#' register.
2494 * Return FAIL if not found, OK for success.
2495 */
2496 int
2497buflist_name_nr(fnum, fname, lnum)
2498 int fnum;
2499 char_u **fname;
2500 linenr_T *lnum;
2501{
2502 buf_T *buf;
2503
2504 buf = buflist_findnr(fnum);
2505 if (buf == NULL || buf->b_fname == NULL)
2506 return FAIL;
2507
2508 *fname = buf->b_fname;
2509 *lnum = buflist_findlnum(buf);
2510
2511 return OK;
2512}
2513
2514/*
2515 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2516 * The file name with the full path is also remembered, for when :cd is used.
2517 * Returns FAIL for failure (file name already in use by other buffer)
2518 * OK otherwise.
2519 */
2520 int
2521setfname(buf, ffname, sfname, message)
2522 buf_T *buf;
2523 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002524 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525{
Bram Moolenaar81695252004-12-29 20:58:21 +00002526 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527#ifdef UNIX
2528 struct stat st;
2529#endif
2530
2531 if (ffname == NULL || *ffname == NUL)
2532 {
2533 /* Removing the name. */
2534 vim_free(buf->b_ffname);
2535 vim_free(buf->b_sfname);
2536 buf->b_ffname = NULL;
2537 buf->b_sfname = NULL;
2538#ifdef UNIX
2539 st.st_dev = (dev_T)-1;
2540#endif
2541 }
2542 else
2543 {
2544 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2545 if (ffname == NULL) /* out of memory */
2546 return FAIL;
2547
2548 /*
2549 * if the file name is already used in another buffer:
2550 * - if the buffer is loaded, fail
2551 * - if the buffer is not loaded, delete it from the list
2552 */
2553#ifdef UNIX
2554 if (mch_stat((char *)ffname, &st) < 0)
2555 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002556#endif
2557 if (!(buf->b_flags & BF_DUMMY))
2558#ifdef UNIX
2559 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002561 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562#endif
2563 if (obuf != NULL && obuf != buf)
2564 {
2565 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2566 {
2567 if (message)
2568 EMSG(_("E95: Buffer with this name already exists"));
2569 vim_free(ffname);
2570 return FAIL;
2571 }
2572 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2573 }
2574 sfname = vim_strsave(sfname);
2575 if (ffname == NULL || sfname == NULL)
2576 {
2577 vim_free(sfname);
2578 vim_free(ffname);
2579 return FAIL;
2580 }
2581#ifdef USE_FNAME_CASE
2582# ifdef USE_LONG_FNAME
2583 if (USE_LONG_FNAME)
2584# endif
2585 fname_case(sfname, 0); /* set correct case for short file name */
2586#endif
2587 vim_free(buf->b_ffname);
2588 vim_free(buf->b_sfname);
2589 buf->b_ffname = ffname;
2590 buf->b_sfname = sfname;
2591 }
2592 buf->b_fname = buf->b_sfname;
2593#ifdef UNIX
2594 if (st.st_dev == (dev_T)-1)
2595 buf->b_dev = -1;
2596 else
2597 {
2598 buf->b_dev = st.st_dev;
2599 buf->b_ino = st.st_ino;
2600 }
2601#endif
2602
2603#ifndef SHORT_FNAME
2604 buf->b_shortname = FALSE;
2605#endif
2606
2607 buf_name_changed(buf);
2608 return OK;
2609}
2610
2611/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002612 * Crude way of changing the name of a buffer. Use with care!
2613 * The name should be relative to the current directory.
2614 */
2615 void
2616buf_set_name(fnum, name)
2617 int fnum;
2618 char_u *name;
2619{
2620 buf_T *buf;
2621
2622 buf = buflist_findnr(fnum);
2623 if (buf != NULL)
2624 {
2625 vim_free(buf->b_sfname);
2626 vim_free(buf->b_ffname);
2627 buf->b_sfname = vim_strsave(name);
2628 buf->b_ffname = FullName_save(buf->b_sfname, FALSE);
2629 buf->b_fname = buf->b_sfname;
2630 }
2631}
2632
2633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 * Take care of what needs to be done when the name of buffer "buf" has
2635 * changed.
2636 */
2637 void
2638buf_name_changed(buf)
2639 buf_T *buf;
2640{
2641 /*
2642 * If the file name changed, also change the name of the swapfile
2643 */
2644 if (buf->b_ml.ml_mfp != NULL)
2645 ml_setname(buf);
2646
2647 if (curwin->w_buffer == buf)
2648 check_arg_idx(curwin); /* check file name for arg list */
2649#ifdef FEAT_TITLE
2650 maketitle(); /* set window title */
2651#endif
2652#ifdef FEAT_WINDOWS
2653 status_redraw_all(); /* status lines need to be redrawn */
2654#endif
2655 fmarks_check_names(buf); /* check named file marks */
2656 ml_timestamp(buf); /* reset timestamp */
2657}
2658
2659/*
2660 * set alternate file name for current window
2661 *
2662 * Used by do_one_cmd(), do_write() and do_ecmd().
2663 * Return the buffer.
2664 */
2665 buf_T *
2666setaltfname(ffname, sfname, lnum)
2667 char_u *ffname;
2668 char_u *sfname;
2669 linenr_T lnum;
2670{
2671 buf_T *buf;
2672
2673 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2674 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002675 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 curwin->w_alt_fnum = buf->b_fnum;
2677 return buf;
2678}
2679
2680/*
2681 * Get alternate file name for current window.
2682 * Return NULL if there isn't any, and give error message if requested.
2683 */
2684 char_u *
2685getaltfname(errmsg)
2686 int errmsg; /* give error message */
2687{
2688 char_u *fname;
2689 linenr_T dummy;
2690
2691 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2692 {
2693 if (errmsg)
2694 EMSG(_(e_noalt));
2695 return NULL;
2696 }
2697 return fname;
2698}
2699
2700/*
2701 * Add a file name to the buflist and return its number.
2702 * Uses same flags as buflist_new(), except BLN_DUMMY.
2703 *
2704 * used by qf_init(), main() and doarglist()
2705 */
2706 int
2707buflist_add(fname, flags)
2708 char_u *fname;
2709 int flags;
2710{
2711 buf_T *buf;
2712
2713 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2714 if (buf != NULL)
2715 return buf->b_fnum;
2716 return 0;
2717}
2718
2719#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2720/*
2721 * Adjust slashes in file names. Called after 'shellslash' was set.
2722 */
2723 void
2724buflist_slash_adjust()
2725{
2726 buf_T *bp;
2727
2728 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2729 {
2730 if (bp->b_ffname != NULL)
2731 slash_adjust(bp->b_ffname);
2732 if (bp->b_sfname != NULL)
2733 slash_adjust(bp->b_sfname);
2734 }
2735}
2736#endif
2737
2738/*
2739 * Set alternate cursor position for current window.
2740 * Also save the local window option values.
2741 */
2742 void
2743buflist_altfpos()
2744{
2745 buflist_setfpos(curbuf, curwin, curwin->w_cursor.lnum,
2746 curwin->w_cursor.col, TRUE);
2747}
2748
2749/*
2750 * Return TRUE if 'ffname' is not the same file as current file.
2751 * Fname must have a full path (expanded by mch_FullName()).
2752 */
2753 int
2754otherfile(ffname)
2755 char_u *ffname;
2756{
2757 return otherfile_buf(curbuf, ffname
2758#ifdef UNIX
2759 , NULL
2760#endif
2761 );
2762}
2763
2764 static int
2765otherfile_buf(buf, ffname
2766#ifdef UNIX
2767 , stp
2768#endif
2769 )
2770 buf_T *buf;
2771 char_u *ffname;
2772#ifdef UNIX
2773 struct stat *stp;
2774#endif
2775{
2776 /* no name is different */
2777 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2778 return TRUE;
2779 if (fnamecmp(ffname, buf->b_ffname) == 0)
2780 return FALSE;
2781#ifdef UNIX
2782 {
2783 struct stat st;
2784
2785 /* If no struct stat given, get it now */
2786 if (stp == NULL)
2787 {
2788 if (buf->b_dev < 0 || mch_stat((char *)ffname, &st) < 0)
2789 st.st_dev = (dev_T)-1;
2790 stp = &st;
2791 }
2792 /* Use dev/ino to check if the files are the same, even when the names
2793 * are different (possible with links). Still need to compare the
2794 * name above, for when the file doesn't exist yet.
2795 * Problem: The dev/ino changes when a file is deleted (and created
2796 * again) and remains the same when renamed/moved. We don't want to
2797 * mch_stat() each buffer each time, that would be too slow. Get the
2798 * dev/ino again when they appear to match, but not when they appear
2799 * to be different: Could skip a buffer when it's actually the same
2800 * file. */
2801 if (buf_same_ino(buf, stp))
2802 {
2803 buf_setino(buf);
2804 if (buf_same_ino(buf, stp))
2805 return FALSE;
2806 }
2807 }
2808#endif
2809 return TRUE;
2810}
2811
2812#if defined(UNIX) || defined(PROTO)
2813/*
2814 * Set inode and device number for a buffer.
2815 * Must always be called when b_fname is changed!.
2816 */
2817 void
2818buf_setino(buf)
2819 buf_T *buf;
2820{
2821 struct stat st;
2822
2823 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2824 {
2825 buf->b_dev = st.st_dev;
2826 buf->b_ino = st.st_ino;
2827 }
2828 else
2829 buf->b_dev = -1;
2830}
2831
2832/*
2833 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2834 */
2835 static int
2836buf_same_ino(buf, stp)
2837 buf_T *buf;
2838 struct stat *stp;
2839{
2840 return (buf->b_dev >= 0
2841 && stp->st_dev == buf->b_dev
2842 && stp->st_ino == buf->b_ino);
2843}
2844#endif
2845
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002846/*
2847 * Print info about the current buffer.
2848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 void
2850fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002851 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 int shorthelp;
2853 int dont_truncate;
2854{
2855 char_u *name;
2856 int n;
2857 char_u *p;
2858 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002859 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
2861 buffer = alloc(IOSIZE);
2862 if (buffer == NULL)
2863 return;
2864
2865 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2866 {
2867 sprintf((char *)buffer, "buf %d: ", curbuf->b_fnum);
2868 p = buffer + STRLEN(buffer);
2869 }
2870 else
2871 p = buffer;
2872
2873 *p++ = '"';
2874 if (buf_spname(curbuf) != NULL)
2875 STRCPY(p, buf_spname(curbuf));
2876 else
2877 {
2878 if (!fullname && curbuf->b_fname != NULL)
2879 name = curbuf->b_fname;
2880 else
2881 name = curbuf->b_ffname;
2882 home_replace(shorthelp ? curbuf : NULL, name, p,
2883 (int)(IOSIZE - (p - buffer)), TRUE);
2884 }
2885
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002886 len = STRLEN(buffer);
2887 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 "\"%s%s%s%s%s%s",
2889 curbufIsChanged() ? (shortmess(SHM_MOD)
2890 ? " [+]" : _(" [Modified]")) : " ",
2891 (curbuf->b_flags & BF_NOTEDITED)
2892#ifdef FEAT_QUICKFIX
2893 && !bt_dontwrite(curbuf)
2894#endif
2895 ? _("[Not edited]") : "",
2896 (curbuf->b_flags & BF_NEW)
2897#ifdef FEAT_QUICKFIX
2898 && !bt_dontwrite(curbuf)
2899#endif
2900 ? _("[New file]") : "",
2901 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
2902 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
2903 : _("[readonly]")) : "",
2904 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
2905 || curbuf->b_p_ro) ?
2906 " " : "");
2907 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
2908 * causes an overflow, thus for large numbers divide instead. */
2909 if (curwin->w_cursor.lnum > 1000000L)
2910 n = (int)(((long)curwin->w_cursor.lnum) /
2911 ((long)curbuf->b_ml.ml_line_count / 100L));
2912 else
2913 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
2914 (long)curbuf->b_ml.ml_line_count);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002915 len = STRLEN(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2917 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002918 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
2920#ifdef FEAT_CMDL_INFO
2921 else if (p_ru)
2922 {
2923 /* Current line and column are already on the screen -- webb */
2924 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002925 vim_snprintf((char *)buffer + len, IOSIZE - len,
2926 _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002928 vim_snprintf((char *)buffer + len, IOSIZE - len,
2929 _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 (long)curbuf->b_ml.ml_line_count, n);
2931 }
2932#endif
2933 else
2934 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002935 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 _("line %ld of %ld --%d%%-- col "),
2937 (long)curwin->w_cursor.lnum,
2938 (long)curbuf->b_ml.ml_line_count,
2939 n);
2940 validate_virtcol();
2941 col_print(buffer + STRLEN(buffer),
2942 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
2943 }
2944
2945 (void)append_arg_number(curwin, buffer, !shortmess(SHM_FILE), IOSIZE);
2946
2947 if (dont_truncate)
2948 {
2949 /* Temporarily set msg_scroll to avoid the message being truncated.
2950 * First call msg_start() to get the message in the right place. */
2951 msg_start();
2952 n = msg_scroll;
2953 msg_scroll = TRUE;
2954 msg(buffer);
2955 msg_scroll = n;
2956 }
2957 else
2958 {
2959 p = msg_trunc_attr(buffer, FALSE, 0);
2960 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 /* Need to repeat the message after redrawing when:
2962 * - When restart_edit is set (otherwise there will be a delay
2963 * before redrawing).
2964 * - When the screen was scrolled but there is no wait-return
2965 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00002966 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 }
2968
2969 vim_free(buffer);
2970}
2971
2972 void
2973col_print(buf, col, vcol)
2974 char_u *buf;
2975 int col;
2976 int vcol;
2977{
2978 if (col == vcol)
2979 sprintf((char *)buf, "%d", col);
2980 else
2981 sprintf((char *)buf, "%d-%d", col, vcol);
2982}
2983
2984#if defined(FEAT_TITLE) || defined(PROTO)
2985/*
2986 * put file name in title bar of window and in icon title
2987 */
2988
2989static char_u *lasttitle = NULL;
2990static char_u *lasticon = NULL;
2991
2992 void
2993maketitle()
2994{
2995 char_u *p;
2996 char_u *t_str = NULL;
2997 char_u *i_name;
2998 char_u *i_str = NULL;
2999 int maxlen = 0;
3000 int len;
3001 int mustset;
3002 char_u buf[IOSIZE];
3003 int off;
3004
3005 if (!redrawing())
3006 {
3007 /* Postpone updating the title when 'lazyredraw' is set. */
3008 need_maketitle = TRUE;
3009 return;
3010 }
3011
3012 need_maketitle = FALSE;
3013 if (!p_title && !p_icon)
3014 return;
3015
3016 if (p_title)
3017 {
3018 if (p_titlelen > 0)
3019 {
3020 maxlen = p_titlelen * Columns / 100;
3021 if (maxlen < 10)
3022 maxlen = 10;
3023 }
3024
3025 t_str = buf;
3026 if (*p_titlestring != NUL)
3027 {
3028#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003029 if (stl_syntax & STL_IN_TITLE)
3030 {
3031 int use_sandbox = FALSE;
3032 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003033
3034# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003035 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003036# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003037 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003039 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003040 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003041 if (called_emsg)
3042 set_string_option_direct((char_u *)"titlestring", -1,
3043 (char_u *)"", OPT_FREE, SID_ERROR);
3044 called_emsg |= save_called_emsg;
3045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 else
3047#endif
3048 t_str = p_titlestring;
3049 }
3050 else
3051 {
3052 /* format: "fname + (path) (1 of 2) - VIM" */
3053
3054 if (curbuf->b_fname == NULL)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003055 STRCPY(buf, _("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 else
3057 {
3058 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003059 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 }
3062
3063 switch (bufIsChanged(curbuf)
3064 + (curbuf->b_p_ro * 2)
3065 + (!curbuf->b_p_ma * 4))
3066 {
3067 case 1: STRCAT(buf, " +"); break;
3068 case 2: STRCAT(buf, " ="); break;
3069 case 3: STRCAT(buf, " =+"); break;
3070 case 4:
3071 case 6: STRCAT(buf, " -"); break;
3072 case 5:
3073 case 7: STRCAT(buf, " -+"); break;
3074 }
3075
3076 if (curbuf->b_fname != NULL)
3077 {
3078 /* Get path of file, replace home dir with ~ */
3079 off = (int)STRLEN(buf);
3080 buf[off++] = ' ';
3081 buf[off++] = '(';
3082 home_replace(curbuf, curbuf->b_ffname,
3083 buf + off, IOSIZE - off, TRUE);
3084#ifdef BACKSLASH_IN_FILENAME
3085 /* avoid "c:/name" to be reduced to "c" */
3086 if (isalpha(buf[off]) && buf[off + 1] == ':')
3087 off += 2;
3088#endif
3089 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003090 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003093 vim_strncpy(buf + off, (char_u *)_("help"),
3094 IOSIZE - off - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003097
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 /* translate unprintable chars */
3099 p = transstr(buf + off);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003100 vim_strncpy(buf + off, p, IOSIZE - off - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 STRCAT(buf, ")");
3103 }
3104
3105 append_arg_number(curwin, buf, FALSE, IOSIZE);
3106
3107#if defined(FEAT_CLIENTSERVER)
3108 if (serverName != NULL)
3109 {
3110 STRCAT(buf, " - ");
3111 STRCAT(buf, serverName);
3112 }
3113 else
3114#endif
3115 STRCAT(buf, " - VIM");
3116
3117 if (maxlen > 0)
3118 {
3119 /* make it shorter by removing a bit in the middle */
3120 len = vim_strsize(buf);
3121 if (len > maxlen)
3122 trunc_string(buf, buf, maxlen);
3123 }
3124 }
3125 }
3126 mustset = ti_change(t_str, &lasttitle);
3127
3128 if (p_icon)
3129 {
3130 i_str = buf;
3131 if (*p_iconstring != NUL)
3132 {
3133#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003134 if (stl_syntax & STL_IN_ICON)
3135 {
3136 int use_sandbox = FALSE;
3137 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003138
3139# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003140 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003141# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003142 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003144 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003145 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003146 if (called_emsg)
3147 set_string_option_direct((char_u *)"iconstring", -1,
3148 (char_u *)"", OPT_FREE, SID_ERROR);
3149 called_emsg |= save_called_emsg;
3150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 else
3152#endif
3153 i_str = p_iconstring;
3154 }
3155 else
3156 {
3157 if (buf_spname(curbuf) != NULL)
3158 i_name = (char_u *)buf_spname(curbuf);
3159 else /* use file name only in icon */
3160 i_name = gettail(curbuf->b_ffname);
3161 *i_str = NUL;
3162 /* Truncate name at 100 bytes. */
3163 len = STRLEN(i_name);
3164 if (len > 100)
3165 {
3166 len -= 100;
3167#ifdef FEAT_MBYTE
3168 if (has_mbyte)
3169 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3170#endif
3171 i_name += len;
3172 }
3173 STRCPY(i_str, i_name);
3174 trans_characters(i_str, IOSIZE);
3175 }
3176 }
3177
3178 mustset |= ti_change(i_str, &lasticon);
3179
3180 if (mustset)
3181 resettitle();
3182}
3183
3184/*
3185 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3186 * from "str" if it does.
3187 * Return TRUE when "*last" changed.
3188 */
3189 static int
3190ti_change(str, last)
3191 char_u *str;
3192 char_u **last;
3193{
3194 if ((str == NULL) != (*last == NULL)
3195 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3196 {
3197 vim_free(*last);
3198 if (str == NULL)
3199 *last = NULL;
3200 else
3201 *last = vim_strsave(str);
3202 return TRUE;
3203 }
3204 return FALSE;
3205}
3206
3207/*
3208 * Put current window title back (used after calling a shell)
3209 */
3210 void
3211resettitle()
3212{
3213 mch_settitle(lasttitle, lasticon);
3214}
Bram Moolenaarea408852005-06-25 22:49:46 +00003215
3216# if defined(EXITFREE) || defined(PROTO)
3217 void
3218free_titles()
3219{
3220 vim_free(lasttitle);
3221 vim_free(lasticon);
3222}
3223# endif
3224
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225#endif /* FEAT_TITLE */
3226
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003227#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003229 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 * Return length of string in screen cells.
3231 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003232 * Normally works for window "wp", except when working for 'tabline' then it
3233 * is "curwin".
3234 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 * Items are drawn interspersed with the text that surrounds it
3236 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3237 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3238 *
3239 * If maxwidth is not zero, the string will be filled at any middle marker
3240 * or truncated if too long, fillchar is used for all whitespace.
3241 */
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003242/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003244build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003246 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 size_t outlen; /* length of out[] */
3248 char_u *fmt;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003249 int use_sandbox; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 int fillchar;
3251 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003252 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3253 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
3255 char_u *p;
3256 char_u *s;
3257 char_u *t;
3258 char_u *linecont;
3259#ifdef FEAT_EVAL
3260 win_T *o_curwin;
3261 buf_T *o_curbuf;
3262#endif
3263 int empty_line;
3264 colnr_T virtcol;
3265 long l;
3266 long n;
3267 int prevchar_isflag;
3268 int prevchar_isitem;
3269 int itemisflag;
3270 int fillable;
3271 char_u *str;
3272 long num;
3273 int width;
3274 int itemcnt;
3275 int curitem;
3276 int groupitem[STL_MAX_ITEM];
3277 int groupdepth;
3278 struct stl_item
3279 {
3280 char_u *start;
3281 int minwid;
3282 int maxwid;
3283 enum
3284 {
3285 Normal,
3286 Empty,
3287 Group,
3288 Middle,
3289 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003290 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 Trunc
3292 } type;
3293 } item[STL_MAX_ITEM];
3294 int minwid;
3295 int maxwid;
3296 int zeropad;
3297 char_u base;
3298 char_u opt;
3299#define TMPLEN 70
3300 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003301 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003302 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003303
3304#ifdef FEAT_EVAL
3305 /*
3306 * When the format starts with "%!" then evaluate it as an expression and
3307 * use the result as the actual format string.
3308 */
3309 if (fmt[0] == '%' && fmt[1] == '!')
3310 {
3311 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3312 if (usefmt == NULL)
3313 usefmt = (char_u *)"";
3314 }
3315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316
3317 if (fillchar == 0)
3318 fillchar = ' ';
3319 /*
3320 * Get line & check if empty (cursorpos will show "0-1").
3321 * If inversion is possible we use it. Else '=' characters are used.
3322 */
3323 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3324 empty_line = (*linecont == NUL);
3325
3326 groupdepth = 0;
3327 p = out;
3328 curitem = 0;
3329 prevchar_isflag = TRUE;
3330 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003331 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 {
3333 if (*s != NUL && *s != '%')
3334 prevchar_isflag = prevchar_isitem = FALSE;
3335
3336 /*
3337 * Handle up to the next '%' or the end.
3338 */
3339 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3340 *p++ = *s++;
3341 if (*s == NUL || p + 1 >= out + outlen)
3342 break;
3343
3344 /*
3345 * Handle one '%' item.
3346 */
3347 s++;
3348 if (*s == '%')
3349 {
3350 if (p + 1 >= out + outlen)
3351 break;
3352 *p++ = *s++;
3353 prevchar_isflag = prevchar_isitem = FALSE;
3354 continue;
3355 }
3356 if (*s == STL_MIDDLEMARK)
3357 {
3358 s++;
3359 if (groupdepth > 0)
3360 continue;
3361 item[curitem].type = Middle;
3362 item[curitem++].start = p;
3363 continue;
3364 }
3365 if (*s == STL_TRUNCMARK)
3366 {
3367 s++;
3368 item[curitem].type = Trunc;
3369 item[curitem++].start = p;
3370 continue;
3371 }
3372 if (*s == ')')
3373 {
3374 s++;
3375 if (groupdepth < 1)
3376 continue;
3377 groupdepth--;
3378
3379 t = item[groupitem[groupdepth]].start;
3380 *p = NUL;
3381 l = vim_strsize(t);
3382 if (curitem > groupitem[groupdepth] + 1
3383 && item[groupitem[groupdepth]].minwid == 0)
3384 {
3385 /* remove group if all items are empty */
3386 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3387 if (item[n].type == Normal)
3388 break;
3389 if (n == curitem)
3390 {
3391 p = t;
3392 l = 0;
3393 }
3394 }
3395 if (l > item[groupitem[groupdepth]].maxwid)
3396 {
3397 /* truncate, remove n bytes of text at the start */
3398#ifdef FEAT_MBYTE
3399 if (has_mbyte)
3400 {
3401 /* Find the first character that should be included. */
3402 n = 0;
3403 while (l >= item[groupitem[groupdepth]].maxwid)
3404 {
3405 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003406 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 }
3408 }
3409 else
3410#endif
3411 n = (p - t) - item[groupitem[groupdepth]].maxwid + 1;
3412
3413 *t = '<';
3414 mch_memmove(t + 1, t + n, p - (t + n));
3415 p = p - n + 1;
3416#ifdef FEAT_MBYTE
3417 /* Fill up space left over by half a double-wide char. */
3418 while (++l < item[groupitem[groupdepth]].minwid)
3419 *p++ = fillchar;
3420#endif
3421
3422 /* correct the start of the items for the truncation */
3423 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3424 {
3425 item[l].start -= n;
3426 if (item[l].start < t)
3427 item[l].start = t;
3428 }
3429 }
3430 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3431 {
3432 /* fill */
3433 n = item[groupitem[groupdepth]].minwid;
3434 if (n < 0)
3435 {
3436 /* fill by appending characters */
3437 n = 0 - n;
3438 while (l++ < n && p + 1 < out + outlen)
3439 *p++ = fillchar;
3440 }
3441 else
3442 {
3443 /* fill by inserting characters */
3444 mch_memmove(t + n - l, t, p - t);
3445 l = n - l;
3446 if (p + l >= out + outlen)
3447 l = (out + outlen) - p - 1;
3448 p += l;
3449 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3450 item[n].start += l;
3451 for ( ; l > 0; l--)
3452 *t++ = fillchar;
3453 }
3454 }
3455 continue;
3456 }
3457 minwid = 0;
3458 maxwid = 9999;
3459 zeropad = FALSE;
3460 l = 1;
3461 if (*s == '0')
3462 {
3463 s++;
3464 zeropad = TRUE;
3465 }
3466 if (*s == '-')
3467 {
3468 s++;
3469 l = -1;
3470 }
3471 if (VIM_ISDIGIT(*s))
3472 {
3473 minwid = (int)getdigits(&s);
3474 if (minwid < 0) /* overflow */
3475 minwid = 0;
3476 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003477 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 {
3479 item[curitem].type = Highlight;
3480 item[curitem].start = p;
3481 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3482 s++;
3483 curitem++;
3484 continue;
3485 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003486 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3487 {
3488 if (*s == STL_TABCLOSENR)
3489 {
3490 if (minwid == 0)
3491 {
3492 /* %X ends the close label, go back to the previously
3493 * define tab label nr. */
3494 for (n = curitem - 1; n >= 0; --n)
3495 if (item[n].type == TabPage && item[n].minwid >= 0)
3496 {
3497 minwid = item[n].minwid;
3498 break;
3499 }
3500 }
3501 else
3502 /* close nrs are stored as negative values */
3503 minwid = - minwid;
3504 }
3505 item[curitem].type = TabPage;
3506 item[curitem].start = p;
3507 item[curitem].minwid = minwid;
3508 s++;
3509 curitem++;
3510 continue;
3511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 if (*s == '.')
3513 {
3514 s++;
3515 if (VIM_ISDIGIT(*s))
3516 {
3517 maxwid = (int)getdigits(&s);
3518 if (maxwid <= 0) /* overflow */
3519 maxwid = 50;
3520 }
3521 }
3522 minwid = (minwid > 50 ? 50 : minwid) * l;
3523 if (*s == '(')
3524 {
3525 groupitem[groupdepth++] = curitem;
3526 item[curitem].type = Group;
3527 item[curitem].start = p;
3528 item[curitem].minwid = minwid;
3529 item[curitem].maxwid = maxwid;
3530 s++;
3531 curitem++;
3532 continue;
3533 }
3534 if (vim_strchr(STL_ALL, *s) == NULL)
3535 {
3536 s++;
3537 continue;
3538 }
3539 opt = *s++;
3540
3541 /* OK - now for the real work */
3542 base = 'D';
3543 itemisflag = FALSE;
3544 fillable = TRUE;
3545 num = -1;
3546 str = NULL;
3547 switch (opt)
3548 {
3549 case STL_FILEPATH:
3550 case STL_FULLPATH:
3551 case STL_FILENAME:
3552 fillable = FALSE; /* don't change ' ' to fillchar */
3553 if (buf_spname(wp->w_buffer) != NULL)
3554 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3555 else
3556 {
3557 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003558 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3560 }
3561 trans_characters(NameBuff, MAXPATHL);
3562 if (opt != STL_FILENAME)
3563 str = NameBuff;
3564 else
3565 str = gettail(NameBuff);
3566 break;
3567
3568 case STL_VIM_EXPR: /* '{' */
3569 itemisflag = TRUE;
3570 t = p;
3571 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3572 *p++ = *s++;
3573 if (*s != '}') /* missing '}' or out of space */
3574 break;
3575 s++;
3576 *p = 0;
3577 p = t;
3578
3579#ifdef FEAT_EVAL
3580 sprintf((char *)tmp, "%d", curbuf->b_fnum);
3581 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3582
3583 o_curbuf = curbuf;
3584 o_curwin = curwin;
3585 curwin = wp;
3586 curbuf = wp->w_buffer;
3587
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003588 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
3590 curwin = o_curwin;
3591 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003592 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593
3594 if (str != NULL && *str != 0)
3595 {
3596 if (*skipdigits(str) == NUL)
3597 {
3598 num = atoi((char *)str);
3599 vim_free(str);
3600 str = NULL;
3601 itemisflag = FALSE;
3602 }
3603 }
3604#endif
3605 break;
3606
3607 case STL_LINE:
3608 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3609 ? 0L : (long)(wp->w_cursor.lnum);
3610 break;
3611
3612 case STL_NUMLINES:
3613 num = wp->w_buffer->b_ml.ml_line_count;
3614 break;
3615
3616 case STL_COLUMN:
3617 num = !(State & INSERT) && empty_line
3618 ? 0 : (int)wp->w_cursor.col + 1;
3619 break;
3620
3621 case STL_VIRTCOL:
3622 case STL_VIRTCOL_ALT:
3623 /* In list mode virtcol needs to be recomputed */
3624 virtcol = wp->w_virtcol;
3625 if (wp->w_p_list && lcs_tab1 == NUL)
3626 {
3627 wp->w_p_list = FALSE;
3628 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3629 wp->w_p_list = TRUE;
3630 }
3631 ++virtcol;
3632 /* Don't display %V if it's the same as %c. */
3633 if (opt == STL_VIRTCOL_ALT
3634 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3635 ? 0 : (int)wp->w_cursor.col + 1)))
3636 break;
3637 num = (long)virtcol;
3638 break;
3639
3640 case STL_PERCENTAGE:
3641 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3642 (long)wp->w_buffer->b_ml.ml_line_count);
3643 break;
3644
3645 case STL_ALTPERCENT:
3646 str = tmp;
3647 get_rel_pos(wp, str);
3648 break;
3649
3650 case STL_ARGLISTSTAT:
3651 fillable = FALSE;
3652 tmp[0] = 0;
3653 if (append_arg_number(wp, tmp, FALSE, (int)sizeof(tmp)))
3654 str = tmp;
3655 break;
3656
3657 case STL_KEYMAP:
3658 fillable = FALSE;
3659 if (get_keymap_str(wp, tmp, TMPLEN))
3660 str = tmp;
3661 break;
3662 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003663#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003664 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665#else
3666 num = 0;
3667#endif
3668 break;
3669
3670 case STL_BUFNO:
3671 num = wp->w_buffer->b_fnum;
3672 break;
3673
3674 case STL_OFFSET_X:
3675 base = 'X';
3676 case STL_OFFSET:
3677#ifdef FEAT_BYTEOFF
3678 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3679 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3680 0L : l + 1 + (!(State & INSERT) && empty_line ?
3681 0 : (int)wp->w_cursor.col);
3682#endif
3683 break;
3684
3685 case STL_BYTEVAL_X:
3686 base = 'X';
3687 case STL_BYTEVAL:
3688 if (((State & INSERT) && wp == curwin) || empty_line)
3689 num = 0;
3690 else
3691 {
3692#ifdef FEAT_MBYTE
3693 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3694#else
3695 num = linecont[wp->w_cursor.col];
3696#endif
3697 }
3698 if (num == NL)
3699 num = 0;
3700 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3701 num = NL;
3702 break;
3703
3704 case STL_ROFLAG:
3705 case STL_ROFLAG_ALT:
3706 itemisflag = TRUE;
3707 if (wp->w_buffer->b_p_ro)
3708 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3709 break;
3710
3711 case STL_HELPFLAG:
3712 case STL_HELPFLAG_ALT:
3713 itemisflag = TRUE;
3714 if (wp->w_buffer->b_help)
3715 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003716 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 break;
3718
3719#ifdef FEAT_AUTOCMD
3720 case STL_FILETYPE:
3721 if (*wp->w_buffer->b_p_ft != NUL
3722 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3723 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003724 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3725 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 str = tmp;
3727 }
3728 break;
3729
3730 case STL_FILETYPE_ALT:
3731 itemisflag = TRUE;
3732 if (*wp->w_buffer->b_p_ft != NUL
3733 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3734 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003735 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3736 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 for (t = tmp; *t != 0; t++)
3738 *t = TOUPPER_LOC(*t);
3739 str = tmp;
3740 }
3741 break;
3742#endif
3743
3744#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3745 case STL_PREVIEWFLAG:
3746 case STL_PREVIEWFLAG_ALT:
3747 itemisflag = TRUE;
3748 if (wp->w_p_pvw)
3749 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3750 : _("[Preview]"));
3751 break;
3752#endif
3753
3754 case STL_MODIFIED:
3755 case STL_MODIFIED_ALT:
3756 itemisflag = TRUE;
3757 switch ((opt == STL_MODIFIED_ALT)
3758 + bufIsChanged(wp->w_buffer) * 2
3759 + (!wp->w_buffer->b_p_ma) * 4)
3760 {
3761 case 2: str = (char_u *)"[+]"; break;
3762 case 3: str = (char_u *)",+"; break;
3763 case 4: str = (char_u *)"[-]"; break;
3764 case 5: str = (char_u *)",-"; break;
3765 case 6: str = (char_u *)"[+-]"; break;
3766 case 7: str = (char_u *)",+-"; break;
3767 }
3768 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003769
3770 case STL_HIGHLIGHT:
3771 t = s;
3772 while (*s != '#' && *s != NUL)
3773 ++s;
3774 if (*s == '#')
3775 {
3776 item[curitem].type = Highlight;
3777 item[curitem].start = p;
3778 item[curitem].minwid = -syn_namen2id(t, s - t);
3779 curitem++;
3780 }
3781 ++s;
3782 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784
3785 item[curitem].start = p;
3786 item[curitem].type = Normal;
3787 if (str != NULL && *str)
3788 {
3789 t = str;
3790 if (itemisflag)
3791 {
3792 if ((t[0] && t[1])
3793 && ((!prevchar_isitem && *t == ',')
3794 || (prevchar_isflag && *t == ' ')))
3795 t++;
3796 prevchar_isflag = TRUE;
3797 }
3798 l = vim_strsize(t);
3799 if (l > 0)
3800 prevchar_isitem = TRUE;
3801 if (l > maxwid)
3802 {
3803 while (l >= maxwid)
3804#ifdef FEAT_MBYTE
3805 if (has_mbyte)
3806 {
3807 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003808 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810 else
3811#endif
3812 l -= byte2cells(*t++);
3813 if (p + 1 >= out + outlen)
3814 break;
3815 *p++ = '<';
3816 }
3817 if (minwid > 0)
3818 {
3819 for (; l < minwid && p + 1 < out + outlen; l++)
3820 {
3821 /* Don't put a "-" in front of a digit. */
3822 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3823 *p++ = ' ';
3824 else
3825 *p++ = fillchar;
3826 }
3827 minwid = 0;
3828 }
3829 else
3830 minwid *= -1;
3831 while (*t && p + 1 < out + outlen)
3832 {
3833 *p++ = *t++;
3834 /* Change a space by fillchar, unless fillchar is '-' and a
3835 * digit follows. */
3836 if (fillable && p[-1] == ' '
3837 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3838 p[-1] = fillchar;
3839 }
3840 for (; l < minwid && p + 1 < out + outlen; l++)
3841 *p++ = fillchar;
3842 }
3843 else if (num >= 0)
3844 {
3845 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3846 char_u nstr[20];
3847
3848 if (p + 20 >= out + outlen)
3849 break; /* not sufficient space */
3850 prevchar_isitem = TRUE;
3851 t = nstr;
3852 if (opt == STL_VIRTCOL_ALT)
3853 {
3854 *t++ = '-';
3855 minwid--;
3856 }
3857 *t++ = '%';
3858 if (zeropad)
3859 *t++ = '0';
3860 *t++ = '*';
3861 *t++ = nbase == 16 ? base : (nbase == 8 ? 'o' : 'd');
3862 *t = 0;
3863
3864 for (n = num, l = 1; n >= nbase; n /= nbase)
3865 l++;
3866 if (opt == STL_VIRTCOL_ALT)
3867 l++;
3868 if (l > maxwid)
3869 {
3870 l += 2;
3871 n = l - maxwid;
3872 while (l-- > maxwid)
3873 num /= nbase;
3874 *t++ = '>';
3875 *t++ = '%';
3876 *t = t[-3];
3877 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003878 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3879 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003882 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3883 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 p += STRLEN(p);
3885 }
3886 else
3887 item[curitem].type = Empty;
3888
3889 if (opt == STL_VIM_EXPR)
3890 vim_free(str);
3891
3892 if (num >= 0 || (!itemisflag && str && *str))
3893 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
3894 curitem++;
3895 }
3896 *p = NUL;
3897 itemcnt = curitem;
3898
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003899#ifdef FEAT_EVAL
3900 if (usefmt != fmt)
3901 vim_free(usefmt);
3902#endif
3903
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 width = vim_strsize(out);
3905 if (maxwidth > 0 && width > maxwidth)
3906 {
3907 /* Result is too long, must trunctate somewhere. */
3908 l = 0;
3909 if (itemcnt == 0)
3910 s = out;
3911 else
3912 {
3913 for ( ; l < itemcnt; l++)
3914 if (item[l].type == Trunc)
3915 {
3916 /* Truncate at %< item. */
3917 s = item[l].start;
3918 break;
3919 }
3920 if (l == itemcnt)
3921 {
3922 /* No %< item, truncate first item. */
3923 s = item[0].start;
3924 l = 0;
3925 }
3926 }
3927
3928 if (width - vim_strsize(s) >= maxwidth)
3929 {
3930 /* Truncation mark is beyond max length */
3931#ifdef FEAT_MBYTE
3932 if (has_mbyte)
3933 {
3934 s = out;
3935 width = 0;
3936 for (;;)
3937 {
3938 width += ptr2cells(s);
3939 if (width >= maxwidth)
3940 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003941 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943 /* Fill up for half a double-wide character. */
3944 while (++width < maxwidth)
3945 *s++ = fillchar;
3946 }
3947 else
3948#endif
3949 s = out + maxwidth - 1;
3950 for (l = 0; l < itemcnt; l++)
3951 if (item[l].start > s)
3952 break;
3953 itemcnt = l;
3954 *s++ = '>';
3955 *s = 0;
3956 }
3957 else
3958 {
3959#ifdef FEAT_MBYTE
3960 if (has_mbyte)
3961 {
3962 n = 0;
3963 while (width >= maxwidth)
3964 {
3965 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003966 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 }
3968 }
3969 else
3970#endif
3971 n = width - maxwidth + 1;
3972 p = s + n;
3973 mch_memmove(s + 1, p, STRLEN(p) + 1);
3974 *s = '<';
3975
3976 /* Fill up for half a double-wide character. */
3977 while (++width < maxwidth)
3978 {
3979 s = s + STRLEN(s);
3980 *s++ = fillchar;
3981 *s = NUL;
3982 }
3983
3984 --n; /* count the '<' */
3985 for (; l < itemcnt; l++)
3986 {
3987 if (item[l].start - n >= s)
3988 item[l].start -= n;
3989 else
3990 item[l].start = s;
3991 }
3992 }
3993 width = maxwidth;
3994 }
3995 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
3996 {
3997 /* Apply STL_MIDDLE if any */
3998 for (l = 0; l < itemcnt; l++)
3999 if (item[l].type == Middle)
4000 break;
4001 if (l < itemcnt)
4002 {
4003 p = item[l].start + maxwidth - width;
4004 mch_memmove(p, item[l].start, STRLEN(item[l].start) + 1);
4005 for (s = item[l].start; s < p; s++)
4006 *s = fillchar;
4007 for (l++; l < itemcnt; l++)
4008 item[l].start += maxwidth - width;
4009 width = maxwidth;
4010 }
4011 }
4012
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004013 /* Store the info about highlighting. */
4014 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004016 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 for (l = 0; l < itemcnt; l++)
4018 {
4019 if (item[l].type == Highlight)
4020 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004021 sp->start = item[l].start;
4022 sp->userhl = item[l].minwid;
4023 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 }
4025 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004026 sp->start = NULL;
4027 sp->userhl = 0;
4028 }
4029
4030 /* Store the info about tab pages labels. */
4031 if (tabtab != NULL)
4032 {
4033 sp = tabtab;
4034 for (l = 0; l < itemcnt; l++)
4035 {
4036 if (item[l].type == TabPage)
4037 {
4038 sp->start = item[l].start;
4039 sp->userhl = item[l].minwid;
4040 sp++;
4041 }
4042 }
4043 sp->start = NULL;
4044 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046
4047 return width;
4048}
4049#endif /* FEAT_STL_OPT */
4050
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004051#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4052 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004054 * Get relative cursor position in window into "str[]", in the form 99%, using
4055 * "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 */
4057 void
4058get_rel_pos(wp, str)
4059 win_T *wp;
4060 char_u *str;
4061{
4062 long above; /* number of lines above window */
4063 long below; /* number of lines below window */
4064
4065 above = wp->w_topline - 1;
4066#ifdef FEAT_DIFF
4067 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4068#endif
4069 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4070 if (below <= 0)
4071 STRCPY(str, above == 0 ? _("All") : _("Bot"));
4072 else if (above <= 0)
4073 STRCPY(str, _("Top"));
4074 else
4075 sprintf((char *)str, "%2d%%", above > 1000000L
4076 ? (int)(above / ((above + below) / 100L))
4077 : (int)(above * 100L / (above + below)));
4078}
4079#endif
4080
4081/*
4082 * Append (file 2 of 8) to 'buf', if editing more than one file.
4083 * Return TRUE if it was appended.
4084 */
4085 int
4086append_arg_number(wp, buf, add_file, maxlen)
4087 win_T *wp;
4088 char_u *buf;
4089 int add_file; /* Add "file" before the arg number */
4090 int maxlen; /* maximum nr of chars in buf or zero*/
4091{
4092 char_u *p;
4093
4094 if (ARGCOUNT <= 1) /* nothing to do */
4095 return FALSE;
4096
4097 p = buf + STRLEN(buf); /* go to the end of the buffer */
4098 if (maxlen && p - buf + 35 >= maxlen) /* getting too long */
4099 return FALSE;
4100 *p++ = ' ';
4101 *p++ = '(';
4102 if (add_file)
4103 {
4104 STRCPY(p, "file ");
4105 p += 5;
4106 }
4107 sprintf((char *)p, wp->w_arg_idx_invalid ? "(%d) of %d)"
4108 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4109 return TRUE;
4110}
4111
4112/*
4113 * If fname is not a full path, make it a full path.
4114 * Returns pointer to allocated memory (NULL for failure).
4115 */
4116 char_u *
4117fix_fname(fname)
4118 char_u *fname;
4119{
4120 /*
4121 * Force expanding the path always for Unix, because symbolic links may
4122 * mess up the full path name, even though it starts with a '/'.
4123 * Also expand when there is ".." in the file name, try to remove it,
4124 * because "c:/src/../README" is equal to "c:/README".
4125 * For MS-Windows also expand names like "longna~1" to "longname".
4126 */
4127#ifdef UNIX
4128 return FullName_save(fname, TRUE);
4129#else
4130 if (!vim_isAbsName(fname) || strstr((char *)fname, "..") != NULL
4131#if defined(MSWIN) || defined(DJGPP)
4132 || vim_strchr(fname, '~') != NULL
4133#endif
4134 )
4135 return FullName_save(fname, FALSE);
4136
4137 fname = vim_strsave(fname);
4138
4139#ifdef USE_FNAME_CASE
4140# ifdef USE_LONG_FNAME
4141 if (USE_LONG_FNAME)
4142# endif
4143 {
4144 if (fname != NULL)
4145 fname_case(fname, 0); /* set correct case for file name */
4146 }
4147#endif
4148
4149 return fname;
4150#endif
4151}
4152
4153/*
4154 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4155 * "ffname" becomes a pointer to allocated memory (or NULL).
4156 */
4157/*ARGSUSED*/
4158 void
4159fname_expand(buf, ffname, sfname)
4160 buf_T *buf;
4161 char_u **ffname;
4162 char_u **sfname;
4163{
4164 if (*ffname == NULL) /* if no file name given, nothing to do */
4165 return;
4166 if (*sfname == NULL) /* if no short file name given, use ffname */
4167 *sfname = *ffname;
4168 *ffname = fix_fname(*ffname); /* expand to full path */
4169
4170#ifdef FEAT_SHORTCUT
4171 if (!buf->b_p_bin)
4172 {
4173 char_u *rfname = NULL;
4174
4175 /* If the file name is a shortcut file, use the file it links to. */
4176 rfname = mch_resolve_shortcut(*ffname);
4177 if (rfname)
4178 {
4179 vim_free(*ffname);
4180 *ffname = rfname;
4181 *sfname = rfname;
4182 }
4183 }
4184#endif
4185}
4186
4187/*
4188 * Get the file name for an argument list entry.
4189 */
4190 char_u *
4191alist_name(aep)
4192 aentry_T *aep;
4193{
4194 buf_T *bp;
4195
4196 /* Use the name from the associated buffer if it exists. */
4197 bp = buflist_findnr(aep->ae_fnum);
4198 if (bp == NULL)
4199 return aep->ae_fname;
4200 return bp->b_fname;
4201}
4202
4203#if defined(FEAT_WINDOWS) || defined(PROTO)
4204/*
4205 * do_arg_all(): Open up to 'count' windows, one for each argument.
4206 */
4207 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004208do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 int count;
4210 int forceit; /* hide buffers in current windows */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004211 int keep_tabs; /* keep curren tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212{
4213 int i;
4214 win_T *wp, *wpnext;
4215 char_u *opened; /* array of flags for which args are open */
4216 int opened_len; /* lenght of opened[] */
4217 int use_firstwin = FALSE; /* use first window for arglist */
4218 int split_ret = OK;
4219 int p_ea_save;
4220 alist_T *alist; /* argument list to be used */
4221 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004222 tabpage_T *tpnext;
4223 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004224 win_T *new_curwin = NULL;
4225 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226
4227 if (ARGCOUNT <= 0)
4228 {
4229 /* Don't give an error message. We don't want it when the ":all"
4230 * command is in the .vimrc. */
4231 return;
4232 }
4233 setpcmark();
4234
4235 opened_len = ARGCOUNT;
4236 opened = alloc_clear((unsigned)opened_len);
4237 if (opened == NULL)
4238 return;
4239
4240#ifdef FEAT_GUI
4241 need_mouse_correct = TRUE;
4242#endif
4243
4244 /*
4245 * Try closing all windows that are not in the argument list.
4246 * Also close windows that are not full width;
4247 * When 'hidden' or "forceit" set the buffer becomes hidden.
4248 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004249 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004251 if (had_tab > 0)
4252 goto_tabpage_tp(first_tabpage);
4253 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004255 tpnext = curtab->tp_next;
4256 for (wp = firstwin; wp != NULL; wp = wpnext)
4257 {
4258 wpnext = wp->w_next;
4259 buf = wp->w_buffer;
4260 if (buf->b_ffname == NULL
4261 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004263 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004265 )
4266 i = ARGCOUNT;
4267 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004269 /* check if the buffer in this window is in the arglist */
4270 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004272 if (ARGLIST[i].ae_fnum == buf->b_fnum
4273 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004274 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004276 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004277 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004278 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004279 if (i == 0)
4280 {
4281 new_curwin = wp;
4282 new_curtab = curtab;
4283 }
4284 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004285 if (wp->w_alist != curwin->w_alist)
4286 {
4287 /* Use the current argument list for all windows
4288 * containing a file from it. */
4289 alist_unlink(wp->w_alist);
4290 wp->w_alist = curwin->w_alist;
4291 ++wp->w_alist->al_refcount;
4292 }
4293 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004297 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004299 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004301 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004302 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004304 /* If the buffer was changed, and we would like to hide it,
4305 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004306 if (!P_HID(buf) && buf->b_nwindows <= 1
4307 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004309 (void)autowrite(buf, FALSE);
4310#ifdef FEAT_AUTOCMD
4311 /* check if autocommands removed the window */
4312 if (!win_valid(wp) || !buf_valid(buf))
4313 {
4314 wpnext = firstwin; /* start all over... */
4315 continue;
4316 }
4317#endif
4318 }
4319#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004320 /* don't close last window */
4321 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004322#endif
4323 use_firstwin = TRUE;
4324#ifdef FEAT_WINDOWS
4325 else
4326 {
4327 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4328# ifdef FEAT_AUTOCMD
4329 /* check if autocommands removed the next window */
4330 if (!win_valid(wpnext))
4331 wpnext = firstwin; /* start all over... */
4332# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334#endif
4335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 }
4337 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004338
4339 /* Without the ":tab" modifier only do the current tab page. */
4340 if (had_tab == 0 || tpnext == NULL)
4341 break;
4342
4343# ifdef FEAT_AUTOCMD
4344 /* check if autocommands removed the next tab page */
4345 if (!valid_tabpage(tpnext))
4346 tpnext = first_tabpage; /* start all over...*/
4347# endif
4348 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350
4351 /*
4352 * Open a window for files in the argument list that don't have one.
4353 * ARGCOUNT may change while doing this, because of autocommands.
4354 */
4355 if (count > ARGCOUNT || count <= 0)
4356 count = ARGCOUNT;
4357
4358 /* Autocommands may do anything to the argument list. Make sure it's not
4359 * freed while we are working here by "locking" it. We still have to
4360 * watch out for its size to be changed. */
4361 alist = curwin->w_alist;
4362 ++alist->al_refcount;
4363
4364#ifdef FEAT_AUTOCMD
4365 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4366 ++autocmd_no_enter;
4367 ++autocmd_no_leave;
4368#endif
4369 win_enter(lastwin, FALSE);
4370 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4371 {
4372 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4373 arg_had_last = TRUE;
4374 if (i < opened_len && opened[i])
4375 {
4376 /* Move the already present window to below the current window */
4377 if (curwin->w_arg_idx != i)
4378 {
4379 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4380 {
4381 if (wpnext->w_arg_idx == i)
4382 {
4383 win_move_after(wpnext, curwin);
4384 break;
4385 }
4386 }
4387 }
4388 }
4389 else if (split_ret == OK)
4390 {
4391 if (!use_firstwin) /* split current window */
4392 {
4393 p_ea_save = p_ea;
4394 p_ea = TRUE; /* use space from all windows */
4395 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4396 p_ea = p_ea_save;
4397 if (split_ret == FAIL)
4398 continue;
4399 }
4400#ifdef FEAT_AUTOCMD
4401 else /* first window: do autocmd for leaving this buffer */
4402 --autocmd_no_leave;
4403#endif
4404
4405 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004406 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 */
4408 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004409 if (i == 0)
4410 {
4411 new_curwin = curwin;
4412 new_curtab = curtab;
4413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4415 ECMD_ONE,
4416 ((P_HID(curwin->w_buffer)
4417 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4418 + ECMD_OLDBUF);
4419#ifdef FEAT_AUTOCMD
4420 if (use_firstwin)
4421 ++autocmd_no_leave;
4422#endif
4423 use_firstwin = FALSE;
4424 }
4425 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004426
4427 /* When ":tab" was used open a new tab for a new window repeatedly. */
4428 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4429 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
4431
4432 /* Remove the "lock" on the argument list. */
4433 alist_unlink(alist);
4434
4435#ifdef FEAT_AUTOCMD
4436 --autocmd_no_enter;
4437#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004438 /* to window with first arg */
4439 if (valid_tabpage(new_curtab))
4440 goto_tabpage_tp(new_curtab);
4441 if (win_valid(new_curwin))
4442 win_enter(new_curwin, FALSE);
4443
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444#ifdef FEAT_AUTOCMD
4445 --autocmd_no_leave;
4446#endif
4447 vim_free(opened);
4448}
4449
4450# if defined(FEAT_LISTCMDS) || defined(PROTO)
4451/*
4452 * Open a window for a number of buffers.
4453 */
4454 void
4455ex_buffer_all(eap)
4456 exarg_T *eap;
4457{
4458 buf_T *buf;
4459 win_T *wp, *wpnext;
4460 int split_ret = OK;
4461 int p_ea_save;
4462 int open_wins = 0;
4463 int r;
4464 int count; /* Maximum number of windows to open. */
4465 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004466#ifdef FEAT_WINDOWS
4467 int had_tab = cmdmod.tab;
4468 tabpage_T *tpnext;
4469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 if (eap->addr_count == 0) /* make as many windows as possible */
4472 count = 9999;
4473 else
4474 count = eap->line2; /* make as many windows as specified */
4475 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4476 all = FALSE;
4477 else
4478 all = TRUE;
4479
4480 setpcmark();
4481
4482#ifdef FEAT_GUI
4483 need_mouse_correct = TRUE;
4484#endif
4485
4486 /*
4487 * Close superfluous windows (two windows for the same buffer).
4488 * Also close windows that are not full-width.
4489 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004490#ifdef FEAT_WINDOWS
4491 if (had_tab > 0)
4492 goto_tabpage_tp(first_tabpage);
4493 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004496 tpnext = curtab->tp_next;
4497 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004499 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004500 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004501#ifdef FEAT_VERTSPLIT
4502 || ((cmdmod.split & WSP_VERT)
4503 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004504 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004505 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004507#ifdef FEAT_WINDOWS
4508 || (had_tab > 0 && wp != firstwin)
4509#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004510 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004511 {
4512 win_close(wp, FALSE);
4513#ifdef FEAT_AUTOCMD
4514 wpnext = firstwin; /* just in case an autocommand does
4515 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004516 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004517 open_wins = 0;
4518#endif
4519 }
4520 else
4521 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004523
4524#ifdef FEAT_WINDOWS
4525 /* Without the ":tab" modifier only do the current tab page. */
4526 if (had_tab == 0 || tpnext == NULL)
4527 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004528 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
4532 /*
4533 * Go through the buffer list. When a buffer doesn't have a window yet,
4534 * open one. Otherwise move the window to the right position.
4535 * Watch out for autocommands that delete buffers or windows!
4536 */
4537#ifdef FEAT_AUTOCMD
4538 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4539 ++autocmd_no_enter;
4540#endif
4541 win_enter(lastwin, FALSE);
4542#ifdef FEAT_AUTOCMD
4543 ++autocmd_no_leave;
4544#endif
4545 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4546 {
4547 /* Check if this buffer needs a window */
4548 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4549 continue;
4550
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004551#ifdef FEAT_WINDOWS
4552 if (had_tab != 0)
4553 {
4554 /* With the ":tab" modifier don't move the window. */
4555 if (buf->b_nwindows > 0)
4556 wp = lastwin; /* buffer has a window, skip it */
4557 else
4558 wp = NULL;
4559 }
4560 else
4561#endif
4562 {
4563 /* Check if this buffer already has a window */
4564 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4565 if (wp->w_buffer == buf)
4566 break;
4567 /* If the buffer already has a window, move it */
4568 if (wp != NULL)
4569 win_move_after(wp, curwin);
4570 }
4571
4572 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 {
4574 /* Split the window and put the buffer in it */
4575 p_ea_save = p_ea;
4576 p_ea = TRUE; /* use space from all windows */
4577 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4578 ++open_wins;
4579 p_ea = p_ea_save;
4580 if (split_ret == FAIL)
4581 continue;
4582
4583 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004584#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 swap_exists_action = SEA_DIALOG;
4586#endif
4587 set_curbuf(buf, DOBUF_GOTO);
4588#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004591#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004593# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 break;
4595 }
4596#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004597#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 if (swap_exists_action == SEA_QUIT)
4599 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004600# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4601 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004602
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004603 /* Reset the error/interrupt/exception state here so that
4604 * aborting() returns FALSE when closing a window. */
4605 enter_cleanup(&cs);
4606# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004607
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004608 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 win_close(curwin, TRUE);
4610 --open_wins;
4611 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004612 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004613
4614# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4615 /* Restore the error/interrupt/exception state if not
4616 * discarded by a new aborting error, interrupt, or uncaught
4617 * exception. */
4618 leave_cleanup(&cs);
4619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
4621 else
4622 handle_swap_exists(NULL);
4623#endif
4624 }
4625
4626 ui_breakcheck();
4627 if (got_int)
4628 {
4629 (void)vgetc(); /* only break the file loading, not the rest */
4630 break;
4631 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004632#ifdef FEAT_EVAL
4633 /* Autocommands deleted the buffer or aborted script processing!!! */
4634 if (aborting())
4635 break;
4636#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004637#ifdef FEAT_WINDOWS
4638 /* When ":tab" was used open a new tab for a new window repeatedly. */
4639 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4640 cmdmod.tab = 9999;
4641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 }
4643#ifdef FEAT_AUTOCMD
4644 --autocmd_no_enter;
4645#endif
4646 win_enter(firstwin, FALSE); /* back to first window */
4647#ifdef FEAT_AUTOCMD
4648 --autocmd_no_leave;
4649#endif
4650
4651 /*
4652 * Close superfluous windows.
4653 */
4654 for (wp = lastwin; open_wins > count; )
4655 {
4656 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4657 || autowrite(wp->w_buffer, FALSE) == OK);
4658#ifdef FEAT_AUTOCMD
4659 if (!win_valid(wp))
4660 {
4661 /* BufWrite Autocommands made the window invalid, start over */
4662 wp = lastwin;
4663 }
4664 else
4665#endif
4666 if (r)
4667 {
4668 win_close(wp, !P_HID(wp->w_buffer));
4669 --open_wins;
4670 wp = lastwin;
4671 }
4672 else
4673 {
4674 wp = wp->w_prev;
4675 if (wp == NULL)
4676 break;
4677 }
4678 }
4679}
4680# endif /* FEAT_LISTCMDS */
4681
4682#endif /* FEAT_WINDOWS */
4683
Bram Moolenaara3227e22006-03-08 21:32:40 +00004684static int chk_modeline __ARGS((linenr_T, int));
4685
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686/*
4687 * do_modelines() - process mode lines for the current file
4688 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004689 * "flags" can be:
4690 * OPT_WINONLY only set options local to window
4691 * OPT_NOWIN don't set options local to window
4692 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 * Returns immediately if the "ml" option isn't set.
4694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004696do_modelines(flags)
4697 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004699 linenr_T lnum;
4700 int nmlines;
4701 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
4703 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4704 return;
4705
4706 /* Disallow recursive entry here. Can happen when executing a modeline
4707 * triggers an autocommand, which reloads modelines with a ":do". */
4708 if (entered)
4709 return;
4710
4711 ++entered;
4712 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4713 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004714 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 nmlines = 0;
4716
4717 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4718 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004719 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 nmlines = 0;
4721 --entered;
4722}
4723
4724#include "version.h" /* for version number */
4725
4726/*
4727 * chk_modeline() - check a single line for a mode string
4728 * Return FAIL if an error encountered.
4729 */
4730 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004731chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004733 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734{
4735 char_u *s;
4736 char_u *e;
4737 char_u *linecopy; /* local copy of any modeline found */
4738 int prev;
4739 int vers;
4740 int end;
4741 int retval = OK;
4742 char_u *save_sourcing_name;
4743 linenr_T save_sourcing_lnum;
4744#ifdef FEAT_EVAL
4745 scid_T save_SID;
4746#endif
4747
4748 prev = -1;
4749 for (s = ml_get(lnum); *s != NUL; ++s)
4750 {
4751 if (prev == -1 || vim_isspace(prev))
4752 {
4753 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4754 || STRNCMP(s, "vi:", (size_t)3) == 0)
4755 break;
4756 if (STRNCMP(s, "vim", 3) == 0)
4757 {
4758 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4759 e = s + 4;
4760 else
4761 e = s + 3;
4762 vers = getdigits(&e);
4763 if (*e == ':'
4764 && (s[3] == ':'
4765 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4766 || (VIM_VERSION_100 < vers && s[3] == '<')
4767 || (VIM_VERSION_100 > vers && s[3] == '>')
4768 || (VIM_VERSION_100 == vers && s[3] == '=')))
4769 break;
4770 }
4771 }
4772 prev = *s;
4773 }
4774
4775 if (*s)
4776 {
4777 do /* skip over "ex:", "vi:" or "vim:" */
4778 ++s;
4779 while (s[-1] != ':');
4780
4781 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4782 if (linecopy == NULL)
4783 return FAIL;
4784
4785 save_sourcing_lnum = sourcing_lnum;
4786 save_sourcing_name = sourcing_name;
4787 sourcing_lnum = lnum; /* prepare for emsg() */
4788 sourcing_name = (char_u *)"modelines";
4789
4790 end = FALSE;
4791 while (end == FALSE)
4792 {
4793 s = skipwhite(s);
4794 if (*s == NUL)
4795 break;
4796
4797 /*
4798 * Find end of set command: ':' or end of line.
4799 * Skip over "\:", replacing it with ":".
4800 */
4801 for (e = s; *e != ':' && *e != NUL; ++e)
4802 if (e[0] == '\\' && e[1] == ':')
4803 STRCPY(e, e + 1);
4804 if (*e == NUL)
4805 end = TRUE;
4806
4807 /*
4808 * If there is a "set" command, require a terminating ':' and
4809 * ignore the stuff after the ':'.
4810 * "vi:set opt opt opt: foo" -- foo not interpreted
4811 * "vi:opt opt opt: foo" -- foo interpreted
4812 * Accept "se" for compatibility with Elvis.
4813 */
4814 if (STRNCMP(s, "set ", (size_t)4) == 0
4815 || STRNCMP(s, "se ", (size_t)3) == 0)
4816 {
4817 if (*e != ':') /* no terminating ':'? */
4818 break;
4819 end = TRUE;
4820 s = vim_strchr(s, ' ') + 1;
4821 }
4822 *e = NUL; /* truncate the set command */
4823
4824 if (*s != NUL) /* skip over an empty "::" */
4825 {
4826#ifdef FEAT_EVAL
4827 save_SID = current_SID;
4828 current_SID = SID_MODELINE;
4829#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004830 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831#ifdef FEAT_EVAL
4832 current_SID = save_SID;
4833#endif
4834 if (retval == FAIL) /* stop if error found */
4835 break;
4836 }
4837 s = e + 1; /* advance to next part */
4838 }
4839
4840 sourcing_lnum = save_sourcing_lnum;
4841 sourcing_name = save_sourcing_name;
4842
4843 vim_free(linecopy);
4844 }
4845 return retval;
4846}
4847
4848#ifdef FEAT_VIMINFO
4849 int
4850read_viminfo_bufferlist(virp, writing)
4851 vir_T *virp;
4852 int writing;
4853{
4854 char_u *tab;
4855 linenr_T lnum;
4856 colnr_T col;
4857 buf_T *buf;
4858 char_u *sfname;
4859 char_u *xline;
4860
4861 /* Handle long line and escaped characters. */
4862 xline = viminfo_readstring(virp, 1, FALSE);
4863
4864 /* don't read in if there are files on the command-line or if writing: */
4865 if (xline != NULL && !writing && ARGCOUNT == 0
4866 && find_viminfo_parameter('%') != NULL)
4867 {
4868 /* Format is: <fname> Tab <lnum> Tab <col>.
4869 * Watch out for a Tab in the file name, work from the end. */
4870 lnum = 0;
4871 col = 0;
4872 tab = vim_strrchr(xline, '\t');
4873 if (tab != NULL)
4874 {
4875 *tab++ = '\0';
4876 col = atoi((char *)tab);
4877 tab = vim_strrchr(xline, '\t');
4878 if (tab != NULL)
4879 {
4880 *tab++ = '\0';
4881 lnum = atol((char *)tab);
4882 }
4883 }
4884
4885 /* Expand "~/" in the file name at "line + 1" to a full path.
4886 * Then try shortening it by comparing with the current directory */
4887 expand_env(xline, NameBuff, MAXPATHL);
4888 mch_dirname(IObuff, IOSIZE);
4889 sfname = shorten_fname(NameBuff, IObuff);
4890 if (sfname == NULL)
4891 sfname = NameBuff;
4892
4893 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
4894 if (buf != NULL) /* just in case... */
4895 {
4896 buf->b_last_cursor.lnum = lnum;
4897 buf->b_last_cursor.col = col;
4898 buflist_setfpos(buf, curwin, lnum, col, FALSE);
4899 }
4900 }
4901 vim_free(xline);
4902
4903 return viminfo_readline(virp);
4904}
4905
4906 void
4907write_viminfo_bufferlist(fp)
4908 FILE *fp;
4909{
4910 buf_T *buf;
4911#ifdef FEAT_WINDOWS
4912 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004913 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914#endif
4915 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004916 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917
4918 if (find_viminfo_parameter('%') == NULL)
4919 return;
4920
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004921 /* Without a number -1 is returned: do all buffers. */
4922 max_buffers = get_viminfo_parameter('%');
4923
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004925 line = alloc(MAXPATHL + 40);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 if (line == NULL)
4927 return;
4928
4929#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00004930 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 set_last_cursor(win);
4932#else
4933 set_last_cursor(curwin);
4934#endif
4935
4936 fprintf(fp, _("\n# Buffer list:\n"));
4937 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
4938 {
4939 if (buf->b_fname == NULL
4940 || !buf->b_p_bl
4941#ifdef FEAT_QUICKFIX
4942 || bt_quickfix(buf)
4943#endif
4944 || removable(buf->b_ffname))
4945 continue;
4946
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004947 if (max_buffers-- == 0)
4948 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 putc('%', fp);
4950 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
4951 sprintf((char *)line + STRLEN(line), "\t%ld\t%d",
4952 (long)buf->b_last_cursor.lnum,
4953 buf->b_last_cursor.col);
4954 viminfo_writestring(fp, line);
4955 }
4956 vim_free(line);
4957}
4958#endif
4959
4960
4961/*
4962 * Return special buffer name.
4963 * Returns NULL when the buffer has a normal file name.
4964 */
4965 char *
4966buf_spname(buf)
4967 buf_T *buf;
4968{
4969#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
4970 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004971 {
4972 win_T *win;
4973
4974 /*
4975 * For location list window, w_llist_ref points to the location list.
4976 * For quickfix window, w_llist_ref is NULL.
4977 */
4978 FOR_ALL_WINDOWS(win)
4979 if (win->w_buffer == buf)
4980 break;
4981 if (win != NULL && win->w_llist_ref != NULL)
4982 return _("[Location List]");
4983 else
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004984 return _("[Quickfix List]");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986#endif
4987#ifdef FEAT_QUICKFIX
4988 /* There is no _file_ when 'buftype' is "nofile", b_sfname
4989 * contains the name as specified by the user */
4990 if (bt_nofile(buf))
4991 {
4992 if (buf->b_sfname != NULL)
4993 return (char *)buf->b_sfname;
4994 return "[Scratch]";
4995 }
4996#endif
4997 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004998 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 return NULL;
5000}
5001
5002
5003#if defined(FEAT_SIGNS) || defined(PROTO)
5004/*
5005 * Insert the sign into the signlist.
5006 */
5007 static void
5008insert_sign(buf, prev, next, id, lnum, typenr)
5009 buf_T *buf; /* buffer to store sign in */
5010 signlist_T *prev; /* previous sign entry */
5011 signlist_T *next; /* next sign entry */
5012 int id; /* sign ID */
5013 linenr_T lnum; /* line number which gets the mark */
5014 int typenr; /* typenr of sign we are adding */
5015{
5016 signlist_T *newsign;
5017
5018 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5019 if (newsign != NULL)
5020 {
5021 newsign->id = id;
5022 newsign->lnum = lnum;
5023 newsign->typenr = typenr;
5024 newsign->next = next;
5025#ifdef FEAT_NETBEANS_INTG
5026 newsign->prev = prev;
5027 if (next != NULL)
5028 next->prev = newsign;
5029#endif
5030
5031 if (prev == NULL)
5032 {
5033 /* When adding first sign need to redraw the windows to create the
5034 * column for signs. */
5035 if (buf->b_signlist == NULL)
5036 {
5037 redraw_buf_later(buf, NOT_VALID);
5038 changed_cline_bef_curs();
5039 }
5040
5041 /* first sign in signlist */
5042 buf->b_signlist = newsign;
5043 }
5044 else
5045 prev->next = newsign;
5046 }
5047}
5048
5049/*
5050 * Add the sign into the signlist. Find the right spot to do it though.
5051 */
5052 void
5053buf_addsign(buf, id, lnum, typenr)
5054 buf_T *buf; /* buffer to store sign in */
5055 int id; /* sign ID */
5056 linenr_T lnum; /* line number which gets the mark */
5057 int typenr; /* typenr of sign we are adding */
5058{
5059 signlist_T *sign; /* a sign in the signlist */
5060 signlist_T *prev; /* the previous sign */
5061
5062 prev = NULL;
5063 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5064 {
5065 if (lnum == sign->lnum && id == sign->id)
5066 {
5067 sign->typenr = typenr;
5068 return;
5069 }
5070 else if (
5071#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5072 id < 0 &&
5073#endif
5074 lnum < sign->lnum)
5075 {
5076#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5077 /* XXX - GRP: Is this because of sign slide problem? Or is it
5078 * really needed? Or is it because we allow multiple signs per
5079 * line? If so, should I add that feature to FEAT_SIGNS?
5080 */
5081 while (prev != NULL && prev->lnum == lnum)
5082 prev = prev->prev;
5083 if (prev == NULL)
5084 sign = buf->b_signlist;
5085 else
5086 sign = prev->next;
5087#endif
5088 insert_sign(buf, prev, sign, id, lnum, typenr);
5089 return;
5090 }
5091 prev = sign;
5092 }
5093#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5094 /* XXX - GRP: See previous comment */
5095 while (prev != NULL && prev->lnum == lnum)
5096 prev = prev->prev;
5097 if (prev == NULL)
5098 sign = buf->b_signlist;
5099 else
5100 sign = prev->next;
5101#endif
5102 insert_sign(buf, prev, sign, id, lnum, typenr);
5103
5104 return;
5105}
5106
5107 int
5108buf_change_sign_type(buf, markId, typenr)
5109 buf_T *buf; /* buffer to store sign in */
5110 int markId; /* sign ID */
5111 int typenr; /* typenr of sign we are adding */
5112{
5113 signlist_T *sign; /* a sign in the signlist */
5114
5115 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5116 {
5117 if (sign->id == markId)
5118 {
5119 sign->typenr = typenr;
5120 return sign->lnum;
5121 }
5122 }
5123
5124 return 0;
5125}
5126
5127 int_u
5128buf_getsigntype(buf, lnum, type)
5129 buf_T *buf;
5130 linenr_T lnum;
5131 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5132{
5133 signlist_T *sign; /* a sign in a b_signlist */
5134
5135 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5136 if (sign->lnum == lnum
5137 && (type == SIGN_ANY
5138# ifdef FEAT_SIGN_ICONS
5139 || (type == SIGN_ICON
5140 && sign_get_image(sign->typenr) != NULL)
5141# endif
5142 || (type == SIGN_TEXT
5143 && sign_get_text(sign->typenr) != NULL)
5144 || (type == SIGN_LINEHL
5145 && sign_get_attr(sign->typenr, TRUE) != 0)))
5146 return sign->typenr;
5147 return 0;
5148}
5149
5150
5151 linenr_T
5152buf_delsign(buf, id)
5153 buf_T *buf; /* buffer sign is stored in */
5154 int id; /* sign id */
5155{
5156 signlist_T **lastp; /* pointer to pointer to current sign */
5157 signlist_T *sign; /* a sign in a b_signlist */
5158 signlist_T *next; /* the next sign in a b_signlist */
5159 linenr_T lnum; /* line number whose sign was deleted */
5160
5161 lastp = &buf->b_signlist;
5162 lnum = 0;
5163 for (sign = buf->b_signlist; sign != NULL; sign = next)
5164 {
5165 next = sign->next;
5166 if (sign->id == id)
5167 {
5168 *lastp = next;
5169#ifdef FEAT_NETBEANS_INTG
5170 if (next != NULL)
5171 next->prev = sign->prev;
5172#endif
5173 lnum = sign->lnum;
5174 vim_free(sign);
5175 break;
5176 }
5177 else
5178 lastp = &sign->next;
5179 }
5180
5181 /* When deleted the last sign need to redraw the windows to remove the
5182 * sign column. */
5183 if (buf->b_signlist == NULL)
5184 {
5185 redraw_buf_later(buf, NOT_VALID);
5186 changed_cline_bef_curs();
5187 }
5188
5189 return lnum;
5190}
5191
5192
5193/*
5194 * Find the line number of the sign with the requested id. If the sign does
5195 * not exist, return 0 as the line number. This will still let the correct file
5196 * get loaded.
5197 */
5198 int
5199buf_findsign(buf, id)
5200 buf_T *buf; /* buffer to store sign in */
5201 int id; /* sign ID */
5202{
5203 signlist_T *sign; /* a sign in the signlist */
5204
5205 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5206 if (sign->id == id)
5207 return sign->lnum;
5208
5209 return 0;
5210}
5211
5212 int
5213buf_findsign_id(buf, lnum)
5214 buf_T *buf; /* buffer whose sign we are searching for */
5215 linenr_T lnum; /* line number of sign */
5216{
5217 signlist_T *sign; /* a sign in the signlist */
5218
5219 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5220 if (sign->lnum == lnum)
5221 return sign->id;
5222
5223 return 0;
5224}
5225
5226
5227# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5228/* see if a given type of sign exists on a specific line */
5229 int
5230buf_findsigntype_id(buf, lnum, typenr)
5231 buf_T *buf; /* buffer whose sign we are searching for */
5232 linenr_T lnum; /* line number of sign */
5233 int typenr; /* sign type number */
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->lnum == lnum && sign->typenr == typenr)
5239 return sign->id;
5240
5241 return 0;
5242}
5243
5244
5245# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5246/* return the number of icons on the given line */
5247 int
5248buf_signcount(buf, lnum)
5249 buf_T *buf;
5250 linenr_T lnum;
5251{
5252 signlist_T *sign; /* a sign in the signlist */
5253 int count = 0;
5254
5255 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5256 if (sign->lnum == lnum)
5257 if (sign_get_image(sign->typenr) != NULL)
5258 count++;
5259
5260 return count;
5261}
5262# endif /* FEAT_SIGN_ICONS */
5263# endif /* FEAT_NETBEANS_INTG */
5264
5265
5266/*
5267 * Delete signs in buffer "buf".
5268 */
5269 static void
5270buf_delete_signs(buf)
5271 buf_T *buf;
5272{
5273 signlist_T *next;
5274
5275 while (buf->b_signlist != NULL)
5276 {
5277 next = buf->b_signlist->next;
5278 vim_free(buf->b_signlist);
5279 buf->b_signlist = next;
5280 }
5281}
5282
5283/*
5284 * Delete all signs in all buffers.
5285 */
5286 void
5287buf_delete_all_signs()
5288{
5289 buf_T *buf; /* buffer we are checking for signs */
5290
5291 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5292 if (buf->b_signlist != NULL)
5293 {
5294 /* Need to redraw the windows to remove the sign column. */
5295 redraw_buf_later(buf, NOT_VALID);
5296 buf_delete_signs(buf);
5297 }
5298}
5299
5300/*
5301 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5302 */
5303 void
5304sign_list_placed(rbuf)
5305 buf_T *rbuf;
5306{
5307 buf_T *buf;
5308 signlist_T *p;
5309 char lbuf[BUFSIZ];
5310
5311 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5312 msg_putchar('\n');
5313 if (rbuf == NULL)
5314 buf = firstbuf;
5315 else
5316 buf = rbuf;
5317 while (buf != NULL)
5318 {
5319 if (buf->b_signlist != NULL)
5320 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005321 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5323 msg_putchar('\n');
5324 }
5325 for (p = buf->b_signlist; p != NULL; p = p->next)
5326 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005327 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5329 MSG_PUTS(lbuf);
5330 msg_putchar('\n');
5331 }
5332 if (rbuf != NULL)
5333 break;
5334 buf = buf->b_next;
5335 }
5336}
5337
5338/*
5339 * Adjust a placed sign for inserted/deleted lines.
5340 */
5341 void
5342sign_mark_adjust(line1, line2, amount, amount_after)
5343 linenr_T line1;
5344 linenr_T line2;
5345 long amount;
5346 long amount_after;
5347{
5348 signlist_T *sign; /* a sign in a b_signlist */
5349
5350 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5351 {
5352 if (sign->lnum >= line1 && sign->lnum <= line2)
5353 {
5354 if (amount == MAXLNUM)
5355 sign->lnum = line1;
5356 else
5357 sign->lnum += amount;
5358 }
5359 else if (sign->lnum > line2)
5360 sign->lnum += amount_after;
5361 }
5362}
5363#endif /* FEAT_SIGNS */
5364
5365/*
5366 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5367 */
5368 void
5369set_buflisted(on)
5370 int on;
5371{
5372 if (on != curbuf->b_p_bl)
5373 {
5374 curbuf->b_p_bl = on;
5375#ifdef FEAT_AUTOCMD
5376 if (on)
5377 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5378 else
5379 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5380#endif
5381 }
5382}
5383
5384/*
5385 * Read the file for "buf" again and check if the contents changed.
5386 * Return TRUE if it changed or this could not be checked.
5387 */
5388 int
5389buf_contents_changed(buf)
5390 buf_T *buf;
5391{
5392 buf_T *newbuf;
5393 int differ = TRUE;
5394 linenr_T lnum;
5395#ifdef FEAT_AUTOCMD
5396 aco_save_T aco;
5397#else
5398 buf_T *old_curbuf = curbuf;
5399#endif
5400 exarg_T ea;
5401
5402 /* Allocate a buffer without putting it in the buffer list. */
5403 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5404 if (newbuf == NULL)
5405 return TRUE;
5406
5407 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5408 if (prep_exarg(&ea, buf) == FAIL)
5409 {
5410 wipe_buffer(newbuf, FALSE);
5411 return TRUE;
5412 }
5413
5414#ifdef FEAT_AUTOCMD
5415 /* set curwin/curbuf to buf and save a few things */
5416 aucmd_prepbuf(&aco, newbuf);
5417#else
5418 curbuf = newbuf;
5419 curwin->w_buffer = newbuf;
5420#endif
5421
Bram Moolenaar4770d092006-01-12 23:22:24 +00005422 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 && readfile(buf->b_ffname, buf->b_fname,
5424 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5425 &ea, READ_NEW | READ_DUMMY) == OK)
5426 {
5427 /* compare the two files line by line */
5428 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5429 {
5430 differ = FALSE;
5431 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5432 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5433 {
5434 differ = TRUE;
5435 break;
5436 }
5437 }
5438 }
5439 vim_free(ea.cmd);
5440
5441#ifdef FEAT_AUTOCMD
5442 /* restore curwin/curbuf and a few other things */
5443 aucmd_restbuf(&aco);
5444#else
5445 curbuf = old_curbuf;
5446 curwin->w_buffer = old_curbuf;
5447#endif
5448
5449 if (curbuf != newbuf) /* safety check */
5450 wipe_buffer(newbuf, FALSE);
5451
5452 return differ;
5453}
5454
5455/*
5456 * Wipe out a buffer and decrement the last buffer number if it was used for
5457 * this buffer. Call this to wipe out a temp buffer that does not contain any
5458 * marks.
5459 */
5460/*ARGSUSED*/
5461 void
5462wipe_buffer(buf, aucmd)
5463 buf_T *buf;
5464 int aucmd; /* When TRUE trigger autocommands. */
5465{
5466 if (buf->b_fnum == top_file_num - 1)
5467 --top_file_num;
5468
5469#ifdef FEAT_AUTOCMD
5470 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5471 ++autocmd_block;
5472#endif
5473 close_buffer(NULL, buf, DOBUF_WIPE);
5474#ifdef FEAT_AUTOCMD
5475 if (!aucmd)
5476 --autocmd_block;
5477#endif
5478}