blob: 9f4c3deb4919a8b039fc237a372e2689f2fb0ad3 [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;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000174
175 /* Set or reset 'modified' before executing autocommands, so that
176 * it can be changed there. */
177 if (!readonlymode && !bufempty())
178 changed();
179 else if (retval != FAIL)
180 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#ifdef FEAT_AUTOCMD
182# ifdef FEAT_EVAL
183 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
184 curbuf, &retval);
185# else
186 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
187# endif
188#endif
189 }
190 }
191
192 /* if first time loading this buffer, init b_chartab[] */
193 if (curbuf->b_flags & BF_NEVERLOADED)
194 (void)buf_init_chartab(curbuf, FALSE);
195
196 /*
197 * Set/reset the Changed flag first, autocmds may change the buffer.
198 * Apply the automatic commands, before processing the modelines.
199 * So the modelines have priority over auto commands.
200 */
201 /* When reading stdin, the buffer contents always needs writing, so set
202 * the changed flag. Unless in readonly mode: "ls | gview -".
203 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000204 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#ifdef FEAT_AUTOCMD
206 || modified_was_set /* ":set modified" used in autocmd */
207# ifdef FEAT_EVAL
208 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
209# endif
210#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000211 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000213 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 unchanged(curbuf, FALSE);
215 save_file_ff(curbuf); /* keep this fileformat */
216
217 /* require "!" to overwrite the file, because it wasn't read completely */
218#ifdef FEAT_EVAL
219 if (aborting())
220#else
221 if (got_int)
222#endif
223 curbuf->b_flags |= BF_READERR;
224
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000225#ifdef FEAT_FOLDING
226 /* Need to update automatic folding. Do this before the autocommands,
227 * they may use the fold info. */
228 foldUpdateAll(curwin);
229#endif
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#ifdef FEAT_AUTOCMD
232 /* need to set w_topline, unless some autocommand already did that. */
233 if (!(curwin->w_valid & VALID_TOPLINE))
234 {
235 curwin->w_topline = 1;
236# ifdef FEAT_DIFF
237 curwin->w_topfill = 0;
238# endif
239 }
240# ifdef FEAT_EVAL
241 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
242# else
243 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
244# endif
245#endif
246
247 if (retval != FAIL)
248 {
249#ifdef FEAT_AUTOCMD
250 /*
251 * The autocommands may have changed the current buffer. Apply the
252 * modelines to the correct buffer, if it still exists and is loaded.
253 */
254 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
255 {
256 aco_save_T aco;
257
258 /* Go to the buffer that was opened. */
259 aucmd_prepbuf(&aco, old_curbuf);
260#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000261 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
263
264#ifdef FEAT_AUTOCMD
265# ifdef FEAT_EVAL
266 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
267 &retval);
268# else
269 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
270# endif
271
272 /* restore curwin/curbuf and a few other things */
273 aucmd_restbuf(&aco);
274 }
275#endif
276 }
277
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 return retval;
279}
280
281/*
282 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
283 */
284 int
285buf_valid(buf)
286 buf_T *buf;
287{
288 buf_T *bp;
289
290 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
291 if (bp == buf)
292 return TRUE;
293 return FALSE;
294}
295
296/*
297 * Close the link to a buffer.
298 * "action" is used when there is no longer a window for the buffer.
299 * It can be:
300 * 0 buffer becomes hidden
301 * DOBUF_UNLOAD buffer is unloaded
302 * DOBUF_DELETE buffer is unloaded and removed from buffer list
303 * DOBUF_WIPE buffer is unloaded and really deleted
304 * When doing all but the first one on the current buffer, the caller should
305 * get a new buffer very soon!
306 *
307 * The 'bufhidden' option can force freeing and deleting.
308 */
309 void
310close_buffer(win, buf, action)
311 win_T *win; /* if not NULL, set b_last_cursor */
312 buf_T *buf;
313 int action;
314{
315#ifdef FEAT_AUTOCMD
316 int is_curbuf;
317 int nwindows = buf->b_nwindows;
318#endif
319 int unload_buf = (action != 0);
320 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
321 int wipe_buf = (action == DOBUF_WIPE);
322
323#ifdef FEAT_QUICKFIX
324 /*
325 * Force unloading or deleting when 'bufhidden' says so.
326 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
327 * "hide" (otherwise we could never free or delete a buffer).
328 */
329 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
330 {
331 del_buf = TRUE;
332 unload_buf = TRUE;
333 }
334 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
335 {
336 del_buf = TRUE;
337 unload_buf = TRUE;
338 wipe_buf = TRUE;
339 }
340 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
341 unload_buf = TRUE;
342#endif
343
344 if (win != NULL)
345 {
346 /* Set b_last_cursor when closing the last window for the buffer.
347 * Remember the last cursor position and window options of the buffer.
348 * This used to be only for the current window, but then options like
349 * 'foldmethod' may be lost with a ":only" command. */
350 if (buf->b_nwindows == 1)
351 set_last_cursor(win);
352 buflist_setfpos(buf, win,
353 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
354 win->w_cursor.col, TRUE);
355 }
356
357#ifdef FEAT_AUTOCMD
358 /* When the buffer is no longer in a window, trigger BufWinLeave */
359 if (buf->b_nwindows == 1)
360 {
361 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
362 FALSE, buf);
363 if (!buf_valid(buf)) /* autocommands may delete the buffer */
364 return;
365
366 /* When the buffer becomes hidden, but is not unloaded, trigger
367 * BufHidden */
368 if (!unload_buf)
369 {
370 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
371 FALSE, buf);
372 if (!buf_valid(buf)) /* autocmds may delete the buffer */
373 return;
374 }
375# ifdef FEAT_EVAL
376 if (aborting()) /* autocmds may abort script processing */
377 return;
378# endif
379 }
380 nwindows = buf->b_nwindows;
381#endif
382
383 /* decrease the link count from windows (unless not in any window) */
384 if (buf->b_nwindows > 0)
385 --buf->b_nwindows;
386
387 /* Return when a window is displaying the buffer or when it's not
388 * unloaded. */
389 if (buf->b_nwindows > 0 || !unload_buf)
390 {
Bram Moolenaar607a95ed2006-03-28 20:57:42 +0000391#if 0 /* why was this here? */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 if (buf == curbuf)
393 u_sync(); /* sync undo before going to another buffer */
Bram Moolenaar607a95ed2006-03-28 20:57:42 +0000394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 return;
396 }
397
398 /* Always remove the buffer when there is no file name. */
399 if (buf->b_ffname == NULL)
400 del_buf = TRUE;
401
402 /*
403 * Free all things allocated for this buffer.
404 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
405 */
406#ifdef FEAT_AUTOCMD
407 /* Remember if we are closing the current buffer. Restore the number of
408 * windows, so that autocommands in buf_freeall() don't get confused. */
409 is_curbuf = (buf == curbuf);
410 buf->b_nwindows = nwindows;
411#endif
412
413 buf_freeall(buf, del_buf, wipe_buf);
414
415#ifdef FEAT_AUTOCMD
416 /* Autocommands may have deleted the buffer. */
417 if (!buf_valid(buf))
418 return;
419# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000420 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 return;
422# endif
423
424 /* Autocommands may have opened or closed windows for this buffer.
425 * Decrement the count for the close we do here. */
426 if (buf->b_nwindows > 0)
427 --buf->b_nwindows;
428
429 /*
430 * It's possible that autocommands change curbuf to the one being deleted.
431 * This might cause the previous curbuf to be deleted unexpectedly. But
432 * in some cases it's OK to delete the curbuf, because a new one is
433 * obtained anyway. Therefore only return if curbuf changed to the
434 * deleted buffer.
435 */
436 if (buf == curbuf && !is_curbuf)
437 return;
438#endif
439
440#ifdef FEAT_NETBEANS_INTG
441 if (usingNetbeans)
442 netbeans_file_closed(buf);
443#endif
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000444 /* Change directories when the 'acd' option is set. */
445 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
447 /*
448 * Remove the buffer from the list.
449 */
450 if (wipe_buf)
451 {
452#ifdef FEAT_SUN_WORKSHOP
453 if (usingSunWorkShop)
454 workshop_file_closed_lineno((char *)buf->b_ffname,
455 (int)buf->b_last_cursor.lnum);
456#endif
457 vim_free(buf->b_ffname);
458 vim_free(buf->b_sfname);
459 if (buf->b_prev == NULL)
460 firstbuf = buf->b_next;
461 else
462 buf->b_prev->b_next = buf->b_next;
463 if (buf->b_next == NULL)
464 lastbuf = buf->b_prev;
465 else
466 buf->b_next->b_prev = buf->b_prev;
467 free_buffer(buf);
468 }
469 else
470 {
471 if (del_buf)
472 {
473 /* Free all internal variables and reset option values, to make
474 * ":bdel" compatible with Vim 5.7. */
475 free_buffer_stuff(buf, TRUE);
476
477 /* Make it look like a new buffer. */
478 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
479
480 /* Init the options when loaded again. */
481 buf->b_p_initialized = FALSE;
482 }
483 buf_clear_file(buf);
484 if (del_buf)
485 buf->b_p_bl = FALSE;
486 }
487}
488
489/*
490 * Make buffer not contain a file.
491 */
492 void
493buf_clear_file(buf)
494 buf_T *buf;
495{
496 buf->b_ml.ml_line_count = 1;
497 unchanged(buf, TRUE);
498#ifndef SHORT_FNAME
499 buf->b_shortname = FALSE;
500#endif
501 buf->b_p_eol = TRUE;
502 buf->b_start_eol = TRUE;
503#ifdef FEAT_MBYTE
504 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000505 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506#endif
507 buf->b_ml.ml_mfp = NULL;
508 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
509#ifdef FEAT_NETBEANS_INTG
510 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511#endif
512}
513
514/*
515 * buf_freeall() - free all things allocated for a buffer that are related to
516 * the file.
517 */
518/*ARGSUSED*/
519 void
520buf_freeall(buf, del_buf, wipe_buf)
521 buf_T *buf;
522 int del_buf; /* buffer is going to be deleted */
523 int wipe_buf; /* buffer is going to be wiped out */
524{
525#ifdef FEAT_AUTOCMD
526 int is_curbuf = (buf == curbuf);
527
528 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
529 if (!buf_valid(buf)) /* autocommands may delete the buffer */
530 return;
531 if (del_buf && buf->b_p_bl)
532 {
533 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
534 if (!buf_valid(buf)) /* autocommands may delete the buffer */
535 return;
536 }
537 if (wipe_buf)
538 {
539 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
540 FALSE, buf);
541 if (!buf_valid(buf)) /* autocommands may delete the buffer */
542 return;
543 }
544# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000545 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 return;
547# endif
548
549 /*
550 * It's possible that autocommands change curbuf to the one being deleted.
551 * This might cause curbuf to be deleted unexpectedly. But in some cases
552 * it's OK to delete the curbuf, because a new one is obtained anyway.
553 * Therefore only return if curbuf changed to the deleted buffer.
554 */
555 if (buf == curbuf && !is_curbuf)
556 return;
557#endif
558#ifdef FEAT_DIFF
559 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
560#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000561
562#ifdef FEAT_FOLDING
563 /* No folds in an empty buffer. */
564# ifdef FEAT_WINDOWS
565 {
566 win_T *win;
567 tabpage_T *tp;
568
569 FOR_ALL_TAB_WINDOWS(tp, win)
570 if (win->w_buffer == buf)
571 clearFolding(win);
572 }
573# else
574 if (curwin->w_buffer == buf)
575 clearFolding(curwin);
576# endif
577#endif
578
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579#ifdef FEAT_TCL
580 tcl_buffer_free(buf);
581#endif
582 u_blockfree(buf); /* free the memory allocated for undo */
583 ml_close(buf, TRUE); /* close and delete the memline/memfile */
584 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
585 u_clearall(buf); /* reset all undo information */
586#ifdef FEAT_SYN_HL
587 syntax_clear(buf); /* reset syntax info */
588#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000589 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590}
591
592/*
593 * Free a buffer structure and the things it contains related to the buffer
594 * itself (not the file, that must have been done already).
595 */
596 static void
597free_buffer(buf)
598 buf_T *buf;
599{
600 free_buffer_stuff(buf, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000601#ifdef FEAT_MZSCHEME
602 mzscheme_buffer_free(buf);
603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604#ifdef FEAT_PERL
605 perl_buf_free(buf);
606#endif
607#ifdef FEAT_PYTHON
608 python_buffer_free(buf);
609#endif
610#ifdef FEAT_RUBY
611 ruby_buffer_free(buf);
612#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000613#ifdef FEAT_AUTOCMD
614 aubuflocal_remove(buf);
615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 vim_free(buf);
617}
618
619/*
620 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
621 */
622 static void
623free_buffer_stuff(buf, free_options)
624 buf_T *buf;
625 int free_options; /* free options as well */
626{
627 if (free_options)
628 {
629 clear_wininfo(buf); /* including window-local options */
630 free_buf_options(buf, TRUE);
631 }
632#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000633 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
634 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635#endif
636#ifdef FEAT_USR_CMDS
637 uc_clear(&buf->b_ucmds); /* clear local user commands */
638#endif
639#ifdef FEAT_SIGNS
640 buf_delete_signs(buf); /* delete any signs */
641#endif
642#ifdef FEAT_LOCALMAP
643 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
644 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
645#endif
646#ifdef FEAT_MBYTE
647 vim_free(buf->b_start_fenc);
648 buf->b_start_fenc = NULL;
649#endif
650}
651
652/*
653 * Free the b_wininfo list for buffer "buf".
654 */
655 static void
656clear_wininfo(buf)
657 buf_T *buf;
658{
659 wininfo_T *wip;
660
661 while (buf->b_wininfo != NULL)
662 {
663 wip = buf->b_wininfo;
664 buf->b_wininfo = wip->wi_next;
665 if (wip->wi_optset)
666 {
667 clear_winopt(&wip->wi_opt);
668#ifdef FEAT_FOLDING
669 deleteFoldRecurse(&wip->wi_folds);
670#endif
671 }
672 vim_free(wip);
673 }
674}
675
676#if defined(FEAT_LISTCMDS) || defined(PROTO)
677/*
678 * Go to another buffer. Handles the result of the ATTENTION dialog.
679 */
680 void
681goto_buffer(eap, start, dir, count)
682 exarg_T *eap;
683 int start;
684 int dir;
685 int count;
686{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000687# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 buf_T *old_curbuf = curbuf;
689
690 swap_exists_action = SEA_DIALOG;
691# endif
692 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
693 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000694# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
696 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000697# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
698 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000699
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000700 /* Reset the error/interrupt/exception state here so that
701 * aborting() returns FALSE when closing a window. */
702 enter_cleanup(&cs);
703# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000704
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000705 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 win_close(curwin, TRUE);
707 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000708 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000709
710# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
711 /* Restore the error/interrupt/exception state if not discarded by a
712 * new aborting error, interrupt, or uncaught exception. */
713 leave_cleanup(&cs);
714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 }
716 else
717 handle_swap_exists(old_curbuf);
718# endif
719}
720#endif
721
Bram Moolenaare64ac772005-12-07 20:54:59 +0000722#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723/*
724 * Handle the situation of swap_exists_action being set.
725 * It is allowed for "old_curbuf" to be NULL or invalid.
726 */
727 void
728handle_swap_exists(old_curbuf)
729 buf_T *old_curbuf;
730{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000731# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
732 cleanup_T cs;
733# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000734
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 if (swap_exists_action == SEA_QUIT)
736 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000737# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
738 /* Reset the error/interrupt/exception state here so that
739 * aborting() returns FALSE when closing a buffer. */
740 enter_cleanup(&cs);
741# endif
742
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 /* User selected Quit at ATTENTION prompt. Go back to previous
744 * buffer. If that buffer is gone or the same as the current one,
745 * open a new, empty buffer. */
746 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000747 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
749 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000750 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 if (old_curbuf != NULL)
752 enter_buffer(old_curbuf);
753 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000754
755# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
756 /* Restore the error/interrupt/exception state if not discarded by a
757 * new aborting error, interrupt, or uncaught exception. */
758 leave_cleanup(&cs);
759# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 }
761 else if (swap_exists_action == SEA_RECOVER)
762 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000763# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
764 /* Reset the error/interrupt/exception state here so that
765 * aborting() returns FALSE when closing a buffer. */
766 enter_cleanup(&cs);
767# endif
768
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 /* User selected Recover at ATTENTION prompt. */
770 msg_scroll = TRUE;
771 ml_recover();
772 MSG_PUTS("\n"); /* don't overwrite the last message */
773 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000774 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000775
776# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
777 /* Restore the error/interrupt/exception state if not discarded by a
778 * new aborting error, interrupt, or uncaught exception. */
779 leave_cleanup(&cs);
780# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 }
782 swap_exists_action = SEA_NONE;
783}
784#endif
785
786#if defined(FEAT_LISTCMDS) || defined(PROTO)
787/*
788 * do_bufdel() - delete or unload buffer(s)
789 *
790 * addr_count == 0: ":bdel" - delete current buffer
791 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
792 * buffer "end_bnr", then any other arguments.
793 * addr_count == 2: ":N,N bdel" - delete buffers in range
794 *
795 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
796 * DOBUF_DEL (":bdel")
797 *
798 * Returns error message or NULL
799 */
800 char_u *
801do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
802 int command;
803 char_u *arg; /* pointer to extra arguments */
804 int addr_count;
805 int start_bnr; /* first buffer number in a range */
806 int end_bnr; /* buffer nr or last buffer nr in a range */
807 int forceit;
808{
809 int do_current = 0; /* delete current buffer? */
810 int deleted = 0; /* number of buffers deleted */
811 char_u *errormsg = NULL; /* return value */
812 int bnr; /* buffer number */
813 char_u *p;
814
815#ifdef FEAT_NETBEANS_INTG
816 netbeansCloseFile = 1;
817#endif
818 if (addr_count == 0)
819 {
820 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
821 }
822 else
823 {
824 if (addr_count == 2)
825 {
826 if (*arg) /* both range and argument is not allowed */
827 return (char_u *)_(e_trailing);
828 bnr = start_bnr;
829 }
830 else /* addr_count == 1 */
831 bnr = end_bnr;
832
833 for ( ;!got_int; ui_breakcheck())
834 {
835 /*
836 * delete the current buffer last, otherwise when the
837 * current buffer is deleted, the next buffer becomes
838 * the current one and will be loaded, which may then
839 * also be deleted, etc.
840 */
841 if (bnr == curbuf->b_fnum)
842 do_current = bnr;
843 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
844 forceit) == OK)
845 ++deleted;
846
847 /*
848 * find next buffer number to delete/unload
849 */
850 if (addr_count == 2)
851 {
852 if (++bnr > end_bnr)
853 break;
854 }
855 else /* addr_count == 1 */
856 {
857 arg = skipwhite(arg);
858 if (*arg == NUL)
859 break;
860 if (!VIM_ISDIGIT(*arg))
861 {
862 p = skiptowhite_esc(arg);
863 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
864 if (bnr < 0) /* failed */
865 break;
866 arg = p;
867 }
868 else
869 bnr = getdigits(&arg);
870 }
871 }
872 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
873 FORWARD, do_current, forceit) == OK)
874 ++deleted;
875
876 if (deleted == 0)
877 {
878 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000879 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000881 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000883 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 errormsg = IObuff;
885 }
886 else if (deleted >= p_report)
887 {
888 if (command == DOBUF_UNLOAD)
889 {
890 if (deleted == 1)
891 MSG(_("1 buffer unloaded"));
892 else
893 smsg((char_u *)_("%d buffers unloaded"), deleted);
894 }
895 else if (command == DOBUF_DEL)
896 {
897 if (deleted == 1)
898 MSG(_("1 buffer deleted"));
899 else
900 smsg((char_u *)_("%d buffers deleted"), deleted);
901 }
902 else
903 {
904 if (deleted == 1)
905 MSG(_("1 buffer wiped out"));
906 else
907 smsg((char_u *)_("%d buffers wiped out"), deleted);
908 }
909 }
910 }
911
912#ifdef FEAT_NETBEANS_INTG
913 netbeansCloseFile = 0;
914#endif
915
916 return errormsg;
917}
918
919/*
920 * Implementation of the commands for the buffer list.
921 *
922 * action == DOBUF_GOTO go to specified buffer
923 * action == DOBUF_SPLIT split window and go to specified buffer
924 * action == DOBUF_UNLOAD unload specified buffer(s)
925 * action == DOBUF_DEL delete specified buffer(s) from buffer list
926 * action == DOBUF_WIPE delete specified buffer(s) really
927 *
928 * start == DOBUF_CURRENT go to "count" buffer from current buffer
929 * start == DOBUF_FIRST go to "count" buffer from first buffer
930 * start == DOBUF_LAST go to "count" buffer from last buffer
931 * start == DOBUF_MOD go to "count" modified buffer from current buffer
932 *
933 * Return FAIL or OK.
934 */
935 int
936do_buffer(action, start, dir, count, forceit)
937 int action;
938 int start;
939 int dir; /* FORWARD or BACKWARD */
940 int count; /* buffer number or number of buffers */
941 int forceit; /* TRUE for :...! */
942{
943 buf_T *buf;
944 buf_T *bp;
945 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
946 || action == DOBUF_WIPE);
947
948 switch (start)
949 {
950 case DOBUF_FIRST: buf = firstbuf; break;
951 case DOBUF_LAST: buf = lastbuf; break;
952 default: buf = curbuf; break;
953 }
954 if (start == DOBUF_MOD) /* find next modified buffer */
955 {
956 while (count-- > 0)
957 {
958 do
959 {
960 buf = buf->b_next;
961 if (buf == NULL)
962 buf = firstbuf;
963 }
964 while (buf != curbuf && !bufIsChanged(buf));
965 }
966 if (!bufIsChanged(buf))
967 {
968 EMSG(_("E84: No modified buffer found"));
969 return FAIL;
970 }
971 }
972 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
973 {
974 while (buf != NULL && buf->b_fnum != count)
975 buf = buf->b_next;
976 }
977 else
978 {
979 bp = NULL;
980 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
981 {
982 /* remember the buffer where we start, we come back there when all
983 * buffers are unlisted. */
984 if (bp == NULL)
985 bp = buf;
986 if (dir == FORWARD)
987 {
988 buf = buf->b_next;
989 if (buf == NULL)
990 buf = firstbuf;
991 }
992 else
993 {
994 buf = buf->b_prev;
995 if (buf == NULL)
996 buf = lastbuf;
997 }
998 /* don't count unlisted buffers */
999 if (unload || buf->b_p_bl)
1000 {
1001 --count;
1002 bp = NULL; /* use this buffer as new starting point */
1003 }
1004 if (bp == buf)
1005 {
1006 /* back where we started, didn't find anything. */
1007 EMSG(_("E85: There is no listed buffer"));
1008 return FAIL;
1009 }
1010 }
1011 }
1012
1013 if (buf == NULL) /* could not find it */
1014 {
1015 if (start == DOBUF_FIRST)
1016 {
1017 /* don't warn when deleting */
1018 if (!unload)
1019 EMSGN(_("E86: Buffer %ld does not exist"), count);
1020 }
1021 else if (dir == FORWARD)
1022 EMSG(_("E87: Cannot go beyond last buffer"));
1023 else
1024 EMSG(_("E88: Cannot go before first buffer"));
1025 return FAIL;
1026 }
1027
1028#ifdef FEAT_GUI
1029 need_mouse_correct = TRUE;
1030#endif
1031
1032#ifdef FEAT_LISTCMDS
1033 /*
1034 * delete buffer buf from memory and/or the list
1035 */
1036 if (unload)
1037 {
1038 int forward;
1039 int retval;
1040
1041 /* When unloading or deleting a buffer that's already unloaded and
1042 * unlisted: fail silently. */
1043 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1044 return FAIL;
1045
1046 if (!forceit && bufIsChanged(buf))
1047 {
1048#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1049 if ((p_confirm || cmdmod.confirm) && p_write)
1050 {
1051 dialog_changed(buf, FALSE);
1052# ifdef FEAT_AUTOCMD
1053 if (!buf_valid(buf))
1054 /* Autocommand deleted buffer, oops! It's not changed
1055 * now. */
1056 return FAIL;
1057# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001058 /* If it's still changed fail silently, the dialog already
1059 * mentioned why it fails. */
1060 if (bufIsChanged(buf))
1061 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064#endif
1065 {
1066 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1067 buf->b_fnum);
1068 return FAIL;
1069 }
1070 }
1071
1072 /*
1073 * If deleting the last (listed) buffer, make it empty.
1074 * The last (listed) buffer cannot be unloaded.
1075 */
1076 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1077 if (bp->b_p_bl && bp != buf)
1078 break;
1079 if (bp == NULL && buf == curbuf)
1080 {
1081 if (action == DOBUF_UNLOAD)
1082 {
1083 EMSG(_("E90: Cannot unload last buffer"));
1084 return FAIL;
1085 }
1086
1087 /* Close any other windows on this buffer, then make it empty. */
1088#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001089 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090#endif
1091 setpcmark();
1092 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1093 forceit ? ECMD_FORCEIT : 0);
1094
1095 /*
1096 * do_ecmd() may create a new buffer, then we have to delete
1097 * the old one. But do_ecmd() may have done that already, check
1098 * if the buffer still exists.
1099 */
1100 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1101 close_buffer(NULL, buf, action);
1102 return retval;
1103 }
1104
1105#ifdef FEAT_WINDOWS
1106 /*
1107 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001108 * (unless it's the only window). Repeat this so long as we end up in
1109 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001111 while (buf == curbuf
1112 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 win_close(curwin, FALSE);
1114#endif
1115
1116 /*
1117 * If the buffer to be deleted is not the current one, delete it here.
1118 */
1119 if (buf != curbuf)
1120 {
1121#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001122 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#endif
1124 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1125 close_buffer(NULL, buf, action);
1126 return OK;
1127 }
1128
1129 /*
1130 * Deleting the current buffer: Need to find another buffer to go to.
1131 * There must be another, otherwise it would have been handled above.
1132 * First use au_new_curbuf, if it is valid.
1133 * Then prefer the buffer we most recently visited.
1134 * Else try to find one that is loaded, after the current buffer,
1135 * then before the current buffer.
1136 * Finally use any buffer.
1137 */
1138 buf = NULL; /* selected buffer */
1139 bp = NULL; /* used when no loaded buffer found */
1140#ifdef FEAT_AUTOCMD
1141 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1142 buf = au_new_curbuf;
1143# ifdef FEAT_JUMPLIST
1144 else
1145# endif
1146#endif
1147#ifdef FEAT_JUMPLIST
1148 if (curwin->w_jumplistlen > 0)
1149 {
1150 int jumpidx;
1151
1152 jumpidx = curwin->w_jumplistidx - 1;
1153 if (jumpidx < 0)
1154 jumpidx = curwin->w_jumplistlen - 1;
1155
1156 forward = jumpidx;
1157 while (jumpidx != curwin->w_jumplistidx)
1158 {
1159 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1160 if (buf != NULL)
1161 {
1162 if (buf == curbuf || !buf->b_p_bl)
1163 buf = NULL; /* skip current and unlisted bufs */
1164 else if (buf->b_ml.ml_mfp == NULL)
1165 {
1166 /* skip unloaded buf, but may keep it for later */
1167 if (bp == NULL)
1168 bp = buf;
1169 buf = NULL;
1170 }
1171 }
1172 if (buf != NULL) /* found a valid buffer: stop searching */
1173 break;
1174 /* advance to older entry in jump list */
1175 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1176 break;
1177 if (--jumpidx < 0)
1178 jumpidx = curwin->w_jumplistlen - 1;
1179 if (jumpidx == forward) /* List exhausted for sure */
1180 break;
1181 }
1182 }
1183#endif
1184
1185 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1186 {
1187 forward = TRUE;
1188 buf = curbuf->b_next;
1189 for (;;)
1190 {
1191 if (buf == NULL)
1192 {
1193 if (!forward) /* tried both directions */
1194 break;
1195 buf = curbuf->b_prev;
1196 forward = FALSE;
1197 continue;
1198 }
1199 /* in non-help buffer, try to skip help buffers, and vv */
1200 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1201 {
1202 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1203 break;
1204 if (bp == NULL) /* remember unloaded buf for later */
1205 bp = buf;
1206 }
1207 if (forward)
1208 buf = buf->b_next;
1209 else
1210 buf = buf->b_prev;
1211 }
1212 }
1213 if (buf == NULL) /* No loaded buffer, use unloaded one */
1214 buf = bp;
1215 if (buf == NULL) /* No loaded buffer, find listed one */
1216 {
1217 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1218 if (buf->b_p_bl && buf != curbuf)
1219 break;
1220 }
1221 if (buf == NULL) /* Still no buffer, just take one */
1222 {
1223 if (curbuf->b_next != NULL)
1224 buf = curbuf->b_next;
1225 else
1226 buf = curbuf->b_prev;
1227 }
1228 }
1229
1230 /*
1231 * make buf current buffer
1232 */
1233 if (action == DOBUF_SPLIT) /* split window first */
1234 {
1235# ifdef FEAT_WINDOWS
1236 /* jump to first window containing buf if one exists ("useopen") */
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001237 if (vim_strchr(p_swb, 'o') != NULL && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001238 return OK;
1239 /* jump to first window in any tab page containing buf if one exists
1240 * ("usetab") */
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001241 if (vim_strchr(p_swb, 'a') != NULL && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 return OK;
1243 if (win_split(0, 0) == FAIL)
1244# endif
1245 return FAIL;
1246 }
1247#endif
1248
1249 /* go to current buffer - nothing to do */
1250 if (buf == curbuf)
1251 return OK;
1252
1253 /*
1254 * Check if the current buffer may be abandoned.
1255 */
1256 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1257 {
1258#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1259 if ((p_confirm || cmdmod.confirm) && p_write)
1260 {
1261 dialog_changed(curbuf, FALSE);
1262# ifdef FEAT_AUTOCMD
1263 if (!buf_valid(buf))
1264 /* Autocommand deleted buffer, oops! */
1265 return FAIL;
1266# endif
1267 }
1268 if (bufIsChanged(curbuf))
1269#endif
1270 {
1271 EMSG(_(e_nowrtmsg));
1272 return FAIL;
1273 }
1274 }
1275
1276 /* Go to the other buffer. */
1277 set_curbuf(buf, action);
1278
1279#if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1280 if (action == DOBUF_SPLIT)
1281 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1282#endif
1283
1284#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1285 if (aborting()) /* autocmds may abort script processing */
1286 return FAIL;
1287#endif
1288
1289 return OK;
1290}
1291
1292#endif /* FEAT_LISTCMDS */
1293
1294/*
1295 * Set current buffer to "buf". Executes autocommands and closes current
1296 * buffer. "action" tells how to close the current buffer:
1297 * DOBUF_GOTO free or hide it
1298 * DOBUF_SPLIT nothing
1299 * DOBUF_UNLOAD unload it
1300 * DOBUF_DEL delete it
1301 * DOBUF_WIPE wipe it out
1302 */
1303 void
1304set_curbuf(buf, action)
1305 buf_T *buf;
1306 int action;
1307{
1308 buf_T *prevbuf;
1309 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1310 || action == DOBUF_WIPE);
1311
1312 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001313 if (!cmdmod.keepalt)
1314 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 buflist_altfpos(); /* remember curpos */
1316
1317#ifdef FEAT_VISUAL
1318 /* Don't restart Select mode after switching to another buffer. */
1319 VIsual_reselect = FALSE;
1320#endif
1321
1322 /* close_windows() or apply_autocmds() may change curbuf */
1323 prevbuf = curbuf;
1324
1325#ifdef FEAT_AUTOCMD
1326 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1327# ifdef FEAT_EVAL
1328 if (buf_valid(prevbuf) && !aborting())
1329# else
1330 if (buf_valid(prevbuf))
1331# endif
1332#endif
1333 {
1334#ifdef FEAT_WINDOWS
1335 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001336 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337#endif
1338#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1339 if (buf_valid(prevbuf) && !aborting())
1340#else
1341 if (buf_valid(prevbuf))
1342#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001343 {
1344 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001345 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1347 unload ? action : (action == DOBUF_GOTO
1348 && !P_HID(prevbuf)
1349 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 }
1352#ifdef FEAT_AUTOCMD
1353# ifdef FEAT_EVAL
1354 /* An autocommand may have deleted buf or aborted the script processing! */
1355 if (buf_valid(buf) && !aborting())
1356# else
1357 if (buf_valid(buf)) /* an autocommand may have deleted buf! */
1358# endif
1359#endif
1360 enter_buffer(buf);
1361}
1362
1363/*
1364 * Enter a new current buffer.
1365 * Old curbuf must have been abandoned already!
1366 */
1367 void
1368enter_buffer(buf)
1369 buf_T *buf;
1370{
1371 /* Copy buffer and window local option values. Not for a help buffer. */
1372 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1373 if (!buf->b_help)
1374 get_winopts(buf);
1375#ifdef FEAT_FOLDING
1376 else
1377 /* Remove all folds in the window. */
1378 clearFolding(curwin);
1379 foldUpdateAll(curwin); /* update folds (later). */
1380#endif
1381
1382 /* Get the buffer in the current window. */
1383 curwin->w_buffer = buf;
1384 curbuf = buf;
1385 ++curbuf->b_nwindows;
1386
1387#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001388 if (curwin->w_p_diff)
1389 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390#endif
1391
1392 /* Cursor on first line by default. */
1393 curwin->w_cursor.lnum = 1;
1394 curwin->w_cursor.col = 0;
1395#ifdef FEAT_VIRTUALEDIT
1396 curwin->w_cursor.coladd = 0;
1397#endif
1398 curwin->w_set_curswant = TRUE;
1399
1400 /* Make sure the buffer is loaded. */
1401 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001402 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001403#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001404 /* If there is no filetype, allow for detecting one. Esp. useful for
1405 * ":ball" used in a autocommand. If there already is a filetype we
1406 * might prefer to keep it. */
1407 if (*curbuf->b_p_ft == NUL)
1408 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001409#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 open_buffer(FALSE, NULL);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 else
1414 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001415 if (!msg_silent)
1416 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1418#ifdef FEAT_AUTOCMD
1419 curwin->w_topline = 1;
1420# ifdef FEAT_DIFF
1421 curwin->w_topfill = 0;
1422# endif
1423 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1424 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1425#endif
1426 }
1427
1428 /* If autocommands did not change the cursor position, restore cursor lnum
1429 * and possibly cursor col. */
1430 if (curwin->w_cursor.lnum == 1 && inindent(0))
1431 buflist_getfpos();
1432
1433 check_arg_idx(curwin); /* check for valid arg_idx */
1434#ifdef FEAT_TITLE
1435 maketitle();
1436#endif
1437#ifdef FEAT_AUTOCMD
1438 if (curwin->w_topline == 1) /* when autocmds didn't change it */
1439#endif
1440 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1441
Bram Moolenaar009b2592004-10-24 19:18:58 +00001442#ifdef FEAT_NETBEANS_INTG
1443 /* Send fileOpened event because we've changed buffers. */
1444 if (usingNetbeans && isNetbeansBuffer(curbuf))
1445 netbeans_file_activated(curbuf);
1446#endif
1447
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001448 /* Change directories when the 'acd' option is set. */
1449 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450
1451#ifdef FEAT_KEYMAP
1452 if (curbuf->b_kmap_state & KEYMAP_INIT)
1453 keymap_init();
1454#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001455#ifdef FEAT_SPELL
1456 /* May need to set the spell language. Can only do this after the buffer
1457 * has been properly setup. */
1458 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
1459 did_set_spelllang(curbuf);
1460#endif
1461
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 redraw_later(NOT_VALID);
1463}
1464
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001465#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1466/*
1467 * Change to the directory of the current buffer.
1468 */
1469 void
1470do_autochdir()
1471{
1472 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1473 shorten_fnames(TRUE);
1474}
1475#endif
1476
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477/*
1478 * functions for dealing with the buffer list
1479 */
1480
1481/*
1482 * Add a file name to the buffer list. Return a pointer to the buffer.
1483 * If the same file name already exists return a pointer to that buffer.
1484 * If it does not exist, or if fname == NULL, a new entry is created.
1485 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1486 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1487 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 * This is the ONLY way to create a new buffer.
1489 */
1490static int top_file_num = 1; /* highest file number */
1491
1492 buf_T *
1493buflist_new(ffname, sfname, lnum, flags)
1494 char_u *ffname; /* full path of fname or relative */
1495 char_u *sfname; /* short fname or NULL */
1496 linenr_T lnum; /* preferred cursor line */
1497 int flags; /* BLN_ defines */
1498{
1499 buf_T *buf;
1500#ifdef UNIX
1501 struct stat st;
1502#endif
1503
1504 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1505
1506 /*
1507 * If file name already exists in the list, update the entry.
1508 */
1509#ifdef UNIX
1510 /* On Unix we can use inode numbers when the file exists. Works better
1511 * for hard links. */
1512 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1513 st.st_dev = (dev_T)-1;
1514#endif
1515 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1516#ifdef UNIX
1517 buflist_findname_stat(ffname, &st)
1518#else
1519 buflist_findname(ffname)
1520#endif
1521 ) != NULL)
1522 {
1523 vim_free(ffname);
1524 if (lnum != 0)
1525 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1526 /* copy the options now, if 'cpo' doesn't have 's' and not done
1527 * already */
1528 buf_copy_options(buf, 0);
1529 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1530 {
1531 buf->b_p_bl = TRUE;
1532#ifdef FEAT_AUTOCMD
1533 if (!(flags & BLN_DUMMY))
1534 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1535#endif
1536 }
1537 return buf;
1538 }
1539
1540 /*
1541 * If the current buffer has no name and no contents, use the current
1542 * buffer. Otherwise: Need to allocate a new buffer structure.
1543 *
1544 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 * (A spell file buffer is allocated in spell.c, but that's not a normal
1546 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 */
1548 buf = NULL;
1549 if ((flags & BLN_CURBUF)
1550 && curbuf != NULL
1551 && curbuf->b_ffname == NULL
1552 && curbuf->b_nwindows <= 1
1553 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1554 {
1555 buf = curbuf;
1556#ifdef FEAT_AUTOCMD
1557 /* It's like this buffer is deleted. Watch out for autocommands that
1558 * change curbuf! If that happens, allocate a new buffer anyway. */
1559 if (curbuf->b_p_bl)
1560 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1561 if (buf == curbuf)
1562 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1563# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001564 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 return NULL;
1566# endif
1567#endif
1568#ifdef FEAT_QUICKFIX
1569# ifdef FEAT_AUTOCMD
1570 if (buf == curbuf)
1571# endif
1572 {
1573 /* Make sure 'bufhidden' and 'buftype' are empty */
1574 clear_string_option(&buf->b_p_bh);
1575 clear_string_option(&buf->b_p_bt);
1576 }
1577#endif
1578 }
1579 if (buf != curbuf || curbuf == NULL)
1580 {
1581 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1582 if (buf == NULL)
1583 {
1584 vim_free(ffname);
1585 return NULL;
1586 }
1587 }
1588
1589 if (ffname != NULL)
1590 {
1591 buf->b_ffname = ffname;
1592 buf->b_sfname = vim_strsave(sfname);
1593 }
1594
1595 clear_wininfo(buf);
1596 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1597
1598 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1599 || buf->b_wininfo == NULL)
1600 {
1601 vim_free(buf->b_ffname);
1602 buf->b_ffname = NULL;
1603 vim_free(buf->b_sfname);
1604 buf->b_sfname = NULL;
1605 if (buf != curbuf)
1606 free_buffer(buf);
1607 return NULL;
1608 }
1609
1610 if (buf == curbuf)
1611 {
1612 /* free all things allocated for this buffer */
1613 buf_freeall(buf, FALSE, FALSE);
1614 if (buf != curbuf) /* autocommands deleted the buffer! */
1615 return NULL;
1616#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001617 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 return NULL;
1619#endif
1620 /* buf->b_nwindows = 0; why was this here? */
1621 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1622#ifdef FEAT_KEYMAP
1623 /* need to reload lmaps and set b:keymap_name */
1624 curbuf->b_kmap_state |= KEYMAP_INIT;
1625#endif
1626 }
1627 else
1628 {
1629 /*
1630 * put new buffer at the end of the buffer list
1631 */
1632 buf->b_next = NULL;
1633 if (firstbuf == NULL) /* buffer list is empty */
1634 {
1635 buf->b_prev = NULL;
1636 firstbuf = buf;
1637 }
1638 else /* append new buffer at end of list */
1639 {
1640 lastbuf->b_next = buf;
1641 buf->b_prev = lastbuf;
1642 }
1643 lastbuf = buf;
1644
1645 buf->b_fnum = top_file_num++;
1646 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1647 {
1648 EMSG(_("W14: Warning: List of file names overflow"));
1649 if (emsg_silent == 0)
1650 {
1651 out_flush();
1652 ui_delay(3000L, TRUE); /* make sure it is noticed */
1653 }
1654 top_file_num = 1;
1655 }
1656
1657 /*
1658 * Always copy the options from the current buffer.
1659 */
1660 buf_copy_options(buf, BCO_ALWAYS);
1661 }
1662
1663 buf->b_wininfo->wi_fpos.lnum = lnum;
1664 buf->b_wininfo->wi_win = curwin;
1665
1666#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001667 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1668#endif
1669#ifdef FEAT_SYN_HL
1670 hash_init(&buf->b_keywtab);
1671 hash_init(&buf->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672#endif
1673
1674 buf->b_fname = buf->b_sfname;
1675#ifdef UNIX
1676 if (st.st_dev == (dev_T)-1)
1677 buf->b_dev = -1;
1678 else
1679 {
1680 buf->b_dev = st.st_dev;
1681 buf->b_ino = st.st_ino;
1682 }
1683#endif
1684 buf->b_u_synced = TRUE;
1685 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001686 if (flags & BLN_DUMMY)
1687 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 buf_clear_file(buf);
1689 clrallmarks(buf); /* clear marks */
1690 fmarks_check_names(buf); /* check file marks for this file */
1691 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1692#ifdef FEAT_AUTOCMD
1693 if (!(flags & BLN_DUMMY))
1694 {
1695 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1696 if (flags & BLN_LISTED)
1697 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1698# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001699 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 return NULL;
1701# endif
1702 }
1703#endif
1704
1705 return buf;
1706}
1707
1708/*
1709 * Free the memory for the options of a buffer.
1710 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1711 * 'fileencoding'.
1712 */
1713 void
1714free_buf_options(buf, free_p_ff)
1715 buf_T *buf;
1716 int free_p_ff;
1717{
1718 if (free_p_ff)
1719 {
1720#ifdef FEAT_MBYTE
1721 clear_string_option(&buf->b_p_fenc);
1722#endif
1723 clear_string_option(&buf->b_p_ff);
1724#ifdef FEAT_QUICKFIX
1725 clear_string_option(&buf->b_p_bh);
1726 clear_string_option(&buf->b_p_bt);
1727#endif
1728 }
1729#ifdef FEAT_FIND_ID
1730 clear_string_option(&buf->b_p_def);
1731 clear_string_option(&buf->b_p_inc);
1732# ifdef FEAT_EVAL
1733 clear_string_option(&buf->b_p_inex);
1734# endif
1735#endif
1736#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1737 clear_string_option(&buf->b_p_inde);
1738 clear_string_option(&buf->b_p_indk);
1739#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001740#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1741 clear_string_option(&buf->b_p_bexpr);
1742#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001743#if defined(FEAT_EVAL)
1744 clear_string_option(&buf->b_p_fex);
1745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#ifdef FEAT_CRYPT
1747 clear_string_option(&buf->b_p_key);
1748#endif
1749 clear_string_option(&buf->b_p_kp);
1750 clear_string_option(&buf->b_p_mps);
1751 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001752 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 clear_string_option(&buf->b_p_isk);
1754#ifdef FEAT_KEYMAP
1755 clear_string_option(&buf->b_p_keymap);
1756 ga_clear(&buf->b_kmap_ga);
1757#endif
1758#ifdef FEAT_COMMENTS
1759 clear_string_option(&buf->b_p_com);
1760#endif
1761#ifdef FEAT_FOLDING
1762 clear_string_option(&buf->b_p_cms);
1763#endif
1764 clear_string_option(&buf->b_p_nf);
1765#ifdef FEAT_SYN_HL
1766 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001767#endif
1768#ifdef FEAT_SPELL
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001769 clear_string_option(&buf->b_p_spc);
Bram Moolenaar86bc1fb2005-06-07 20:58:01 +00001770 clear_string_option(&buf->b_p_spf);
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001771 vim_free(buf->b_cap_prog);
1772 buf->b_cap_prog = NULL;
Bram Moolenaara0dea672005-03-20 22:23:10 +00001773 clear_string_option(&buf->b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#endif
1775#ifdef FEAT_SEARCHPATH
1776 clear_string_option(&buf->b_p_sua);
1777#endif
1778#ifdef FEAT_AUTOCMD
1779 clear_string_option(&buf->b_p_ft);
1780#endif
1781#ifdef FEAT_OSFILETYPE
1782 clear_string_option(&buf->b_p_oft);
1783#endif
1784#ifdef FEAT_CINDENT
1785 clear_string_option(&buf->b_p_cink);
1786 clear_string_option(&buf->b_p_cino);
1787#endif
1788#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1789 clear_string_option(&buf->b_p_cinw);
1790#endif
1791#ifdef FEAT_INS_EXPAND
1792 clear_string_option(&buf->b_p_cpt);
1793#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001794#ifdef FEAT_COMPL_FUNC
1795 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001796 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#ifdef FEAT_QUICKFIX
1799 clear_string_option(&buf->b_p_gp);
1800 clear_string_option(&buf->b_p_mp);
1801 clear_string_option(&buf->b_p_efm);
1802#endif
1803 clear_string_option(&buf->b_p_ep);
1804 clear_string_option(&buf->b_p_path);
1805 clear_string_option(&buf->b_p_tags);
1806#ifdef FEAT_INS_EXPAND
1807 clear_string_option(&buf->b_p_dict);
1808 clear_string_option(&buf->b_p_tsr);
1809#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001810#ifdef FEAT_TEXTOBJ
1811 clear_string_option(&buf->b_p_qe);
1812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 buf->b_p_ar = -1;
1814}
1815
1816/*
1817 * get alternate file n
1818 * set linenr to lnum or altfpos.lnum if lnum == 0
1819 * also set cursor column to altfpos.col if 'startofline' is not set.
1820 * if (options & GETF_SETMARK) call setpcmark()
1821 * if (options & GETF_ALT) we are jumping to an alternate file.
1822 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1823 *
1824 * return FAIL for failure, OK for success
1825 */
1826 int
1827buflist_getfile(n, lnum, options, forceit)
1828 int n;
1829 linenr_T lnum;
1830 int options;
1831 int forceit;
1832{
1833 buf_T *buf;
1834#ifdef FEAT_WINDOWS
1835 win_T *wp = NULL;
1836#endif
1837 pos_T *fpos;
1838 colnr_T col;
1839
1840 buf = buflist_findnr(n);
1841 if (buf == NULL)
1842 {
1843 if ((options & GETF_ALT) && n == 0)
1844 EMSG(_(e_noalt));
1845 else
1846 EMSGN(_("E92: Buffer %ld not found"), n);
1847 return FAIL;
1848 }
1849
1850 /* if alternate file is the current buffer, nothing to do */
1851 if (buf == curbuf)
1852 return OK;
1853
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001854 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001855 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001856 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001858 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001859#ifdef FEAT_AUTOCMD
1860 if (curbuf_locked())
1861 return FAIL;
1862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
1864 /* altfpos may be changed by getfile(), get it now */
1865 if (lnum == 0)
1866 {
1867 fpos = buflist_findfpos(buf);
1868 lnum = fpos->lnum;
1869 col = fpos->col;
1870 }
1871 else
1872 col = 0;
1873
1874#ifdef FEAT_WINDOWS
1875 if (options & GETF_SWITCH)
1876 {
1877 /* use existing open window for buffer if wanted */
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001878 if (vim_strchr(p_swb, 'o') != NULL) /* useopen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001880 /* use existing open window in any tab page for buffer if wanted */
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001881 if (vim_strchr(p_swb, 'a') != NULL) /* usetab */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001882 wp = buf_jump_open_tab(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 /* split window if wanted ("split") */
Bram Moolenaar38c0a6e2006-10-20 18:13:14 +00001884 if (wp == NULL && vim_strchr(p_swb, 'l') != NULL && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 {
1886 if (win_split(0, 0) == FAIL)
1887 return FAIL;
1888# ifdef FEAT_SCROLLBIND
1889 curwin->w_p_scb = FALSE;
1890# endif
1891 }
1892 }
1893#endif
1894
1895 ++RedrawingDisabled;
1896 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1897 lnum, forceit) <= 0)
1898 {
1899 --RedrawingDisabled;
1900
1901 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1902 if (!p_sol && col != 0)
1903 {
1904 curwin->w_cursor.col = col;
1905 check_cursor_col();
1906#ifdef FEAT_VIRTUALEDIT
1907 curwin->w_cursor.coladd = 0;
1908#endif
1909 curwin->w_set_curswant = TRUE;
1910 }
1911 return OK;
1912 }
1913 --RedrawingDisabled;
1914 return FAIL;
1915}
1916
1917/*
1918 * go to the last know line number for the current buffer
1919 */
1920 void
1921buflist_getfpos()
1922{
1923 pos_T *fpos;
1924
1925 fpos = buflist_findfpos(curbuf);
1926
1927 curwin->w_cursor.lnum = fpos->lnum;
1928 check_cursor_lnum();
1929
1930 if (p_sol)
1931 curwin->w_cursor.col = 0;
1932 else
1933 {
1934 curwin->w_cursor.col = fpos->col;
1935 check_cursor_col();
1936#ifdef FEAT_VIRTUALEDIT
1937 curwin->w_cursor.coladd = 0;
1938#endif
1939 curwin->w_set_curswant = TRUE;
1940 }
1941}
1942
Bram Moolenaar81695252004-12-29 20:58:21 +00001943#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1944/*
1945 * Find file in buffer list by name (it has to be for the current window).
1946 * Returns NULL if not found.
1947 */
1948 buf_T *
1949buflist_findname_exp(fname)
1950 char_u *fname;
1951{
1952 char_u *ffname;
1953 buf_T *buf = NULL;
1954
1955 /* First make the name into a full path name */
1956 ffname = FullName_save(fname,
1957#ifdef UNIX
1958 TRUE /* force expansion, get rid of symbolic links */
1959#else
1960 FALSE
1961#endif
1962 );
1963 if (ffname != NULL)
1964 {
1965 buf = buflist_findname(ffname);
1966 vim_free(ffname);
1967 }
1968 return buf;
1969}
1970#endif
1971
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972/*
1973 * Find file in buffer list by name (it has to be for the current window).
1974 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00001975 * Skips dummy buffers.
1976 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 */
1978 buf_T *
1979buflist_findname(ffname)
1980 char_u *ffname;
1981{
1982#ifdef UNIX
1983 struct stat st;
1984
1985 if (mch_stat((char *)ffname, &st) < 0)
1986 st.st_dev = (dev_T)-1;
1987 return buflist_findname_stat(ffname, &st);
1988}
1989
1990/*
1991 * Same as buflist_findname(), but pass the stat structure to avoid getting it
1992 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00001993 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 */
1995 static buf_T *
1996buflist_findname_stat(ffname, stp)
1997 char_u *ffname;
1998 struct stat *stp;
1999{
2000#endif
2001 buf_T *buf;
2002
2003 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002004 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005#ifdef UNIX
2006 , stp
2007#endif
2008 ))
2009 return buf;
2010 return NULL;
2011}
2012
2013#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2014/*
2015 * Find file in buffer list by a regexp pattern.
2016 * Return fnum of the found buffer.
2017 * Return < 0 for error.
2018 */
2019/*ARGSUSED*/
2020 int
2021buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2022 char_u *pattern;
2023 char_u *pattern_end; /* pointer to first char after pattern */
2024 int unlisted; /* find unlisted buffers */
2025 int diffmode; /* find diff-mode buffers only */
2026{
2027 buf_T *buf;
2028 regprog_T *prog;
2029 int match = -1;
2030 int find_listed;
2031 char_u *pat;
2032 char_u *patend;
2033 int attempt;
2034 char_u *p;
2035 int toggledollar;
2036
2037 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2038 {
2039 if (*pattern == '%')
2040 match = curbuf->b_fnum;
2041 else
2042 match = curwin->w_alt_fnum;
2043#ifdef FEAT_DIFF
2044 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2045 match = -1;
2046#endif
2047 }
2048
2049 /*
2050 * Try four ways of matching a listed buffer:
2051 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002052 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 * attempt == 2: with '$' at end (only match at end)
2054 * attempt == 3: with '^' at start and '$' at end (only full match)
2055 * Repeat this for finding an unlisted buffer if there was no matching
2056 * listed buffer.
2057 */
2058 else
2059 {
2060 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2061 if (pat == NULL)
2062 return -1;
2063 patend = pat + STRLEN(pat) - 1;
2064 toggledollar = (patend > pat && *patend == '$');
2065
2066 /* First try finding a listed buffer. If not found and "unlisted"
2067 * is TRUE, try finding an unlisted buffer. */
2068 find_listed = TRUE;
2069 for (;;)
2070 {
2071 for (attempt = 0; attempt <= 3; ++attempt)
2072 {
2073 /* may add '^' and '$' */
2074 if (toggledollar)
2075 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2076 p = pat;
2077 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2078 ++p;
2079 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2080 if (prog == NULL)
2081 {
2082 vim_free(pat);
2083 return -1;
2084 }
2085
2086 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2087 if (buf->b_p_bl == find_listed
2088#ifdef FEAT_DIFF
2089 && (!diffmode || diff_mode_buf(buf))
2090#endif
2091 && buflist_match(prog, buf) != NULL)
2092 {
2093 if (match >= 0) /* already found a match */
2094 {
2095 match = -2;
2096 break;
2097 }
2098 match = buf->b_fnum; /* remember first match */
2099 }
2100
2101 vim_free(prog);
2102 if (match >= 0) /* found one match */
2103 break;
2104 }
2105
2106 /* Only search for unlisted buffers if there was no match with
2107 * a listed buffer. */
2108 if (!unlisted || !find_listed || match != -1)
2109 break;
2110 find_listed = FALSE;
2111 }
2112
2113 vim_free(pat);
2114 }
2115
2116 if (match == -2)
2117 EMSG2(_("E93: More than one match for %s"), pattern);
2118 else if (match < 0)
2119 EMSG2(_("E94: No matching buffer for %s"), pattern);
2120 return match;
2121}
2122#endif
2123
2124#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2125
2126/*
2127 * Find all buffer names that match.
2128 * For command line expansion of ":buf" and ":sbuf".
2129 * Return OK if matches found, FAIL otherwise.
2130 */
2131 int
2132ExpandBufnames(pat, num_file, file, options)
2133 char_u *pat;
2134 int *num_file;
2135 char_u ***file;
2136 int options;
2137{
2138 int count = 0;
2139 buf_T *buf;
2140 int round;
2141 char_u *p;
2142 int attempt;
2143 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002144 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145
2146 *num_file = 0; /* return values in case of FAIL */
2147 *file = NULL;
2148
Bram Moolenaar05159a02005-02-26 23:04:13 +00002149 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2150 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002152 patc = alloc((unsigned)STRLEN(pat) + 11);
2153 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002155 STRCPY(patc, "\\(^\\|[\\/]\\)");
2156 STRCPY(patc + 11, pat + 1);
2157 }
2158 else
2159 patc = pat;
2160
2161 /*
2162 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002163 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002164 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002165 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002166 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002167 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002168 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002169 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002170 if (prog == NULL)
2171 {
2172 if (patc != pat)
2173 vim_free(patc);
2174 return FAIL;
2175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
2177 /*
2178 * round == 1: Count the matches.
2179 * round == 2: Build the array to keep the matches.
2180 */
2181 for (round = 1; round <= 2; ++round)
2182 {
2183 count = 0;
2184 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2185 {
2186 if (!buf->b_p_bl) /* skip unlisted buffers */
2187 continue;
2188 p = buflist_match(prog, buf);
2189 if (p != NULL)
2190 {
2191 if (round == 1)
2192 ++count;
2193 else
2194 {
2195 if (options & WILD_HOME_REPLACE)
2196 p = home_replace_save(buf, p);
2197 else
2198 p = vim_strsave(p);
2199 (*file)[count++] = p;
2200 }
2201 }
2202 }
2203 if (count == 0) /* no match found, break here */
2204 break;
2205 if (round == 1)
2206 {
2207 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2208 if (*file == NULL)
2209 {
2210 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002211 if (patc != pat)
2212 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 return FAIL;
2214 }
2215 }
2216 }
2217 vim_free(prog);
2218 if (count) /* match(es) found, break here */
2219 break;
2220 }
2221
Bram Moolenaar05159a02005-02-26 23:04:13 +00002222 if (patc != pat)
2223 vim_free(patc);
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 *num_file = count;
2226 return (count == 0 ? FAIL : OK);
2227}
2228
2229#endif /* FEAT_CMDL_COMPL */
2230
2231#ifdef HAVE_BUFLIST_MATCH
2232/*
2233 * Check for a match on the file name for buffer "buf" with regprog "prog".
2234 */
2235 static char_u *
2236buflist_match(prog, buf)
2237 regprog_T *prog;
2238 buf_T *buf;
2239{
2240 char_u *match;
2241
2242 /* First try the short file name, then the long file name. */
2243 match = fname_match(prog, buf->b_sfname);
2244 if (match == NULL)
2245 match = fname_match(prog, buf->b_ffname);
2246
2247 return match;
2248}
2249
2250/*
2251 * Try matching the regexp in "prog" with file name "name".
2252 * Return "name" when there is a match, NULL when not.
2253 */
2254 static char_u *
2255fname_match(prog, name)
2256 regprog_T *prog;
2257 char_u *name;
2258{
2259 char_u *match = NULL;
2260 char_u *p;
2261 regmatch_T regmatch;
2262
2263 if (name != NULL)
2264 {
2265 regmatch.regprog = prog;
2266#ifdef CASE_INSENSITIVE_FILENAME
2267 regmatch.rm_ic = TRUE; /* Always ignore case */
2268#else
2269 regmatch.rm_ic = FALSE; /* Never ignore case */
2270#endif
2271
2272 if (vim_regexec(&regmatch, name, (colnr_T)0))
2273 match = name;
2274 else
2275 {
2276 /* Replace $(HOME) with '~' and try matching again. */
2277 p = home_replace_save(NULL, name);
2278 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2279 match = name;
2280 vim_free(p);
2281 }
2282 }
2283
2284 return match;
2285}
2286#endif
2287
2288/*
2289 * find file in buffer list by number
2290 */
2291 buf_T *
2292buflist_findnr(nr)
2293 int nr;
2294{
2295 buf_T *buf;
2296
2297 if (nr == 0)
2298 nr = curwin->w_alt_fnum;
2299 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2300 if (buf->b_fnum == nr)
2301 return (buf);
2302 return NULL;
2303}
2304
2305/*
2306 * Get name of file 'n' in the buffer list.
2307 * When the file has no name an empty string is returned.
2308 * home_replace() is used to shorten the file name (used for marks).
2309 * Returns a pointer to allocated memory, of NULL when failed.
2310 */
2311 char_u *
2312buflist_nr2name(n, fullname, helptail)
2313 int n;
2314 int fullname;
2315 int helptail; /* for help buffers return tail only */
2316{
2317 buf_T *buf;
2318
2319 buf = buflist_findnr(n);
2320 if (buf == NULL)
2321 return NULL;
2322 return home_replace_save(helptail ? buf : NULL,
2323 fullname ? buf->b_ffname : buf->b_fname);
2324}
2325
2326/*
2327 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2328 * When "copy_options" is TRUE save the local window option values.
2329 * When "lnum" is 0 only do the options.
2330 */
2331 static void
2332buflist_setfpos(buf, win, lnum, col, copy_options)
2333 buf_T *buf;
2334 win_T *win;
2335 linenr_T lnum;
2336 colnr_T col;
2337 int copy_options;
2338{
2339 wininfo_T *wip;
2340
2341 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2342 if (wip->wi_win == win)
2343 break;
2344 if (wip == NULL)
2345 {
2346 /* allocate a new entry */
2347 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2348 if (wip == NULL)
2349 return;
2350 wip->wi_win = win;
2351 if (lnum == 0) /* set lnum even when it's 0 */
2352 lnum = 1;
2353 }
2354 else
2355 {
2356 /* remove the entry from the list */
2357 if (wip->wi_prev)
2358 wip->wi_prev->wi_next = wip->wi_next;
2359 else
2360 buf->b_wininfo = wip->wi_next;
2361 if (wip->wi_next)
2362 wip->wi_next->wi_prev = wip->wi_prev;
2363 if (copy_options && wip->wi_optset)
2364 {
2365 clear_winopt(&wip->wi_opt);
2366#ifdef FEAT_FOLDING
2367 deleteFoldRecurse(&wip->wi_folds);
2368#endif
2369 }
2370 }
2371 if (lnum != 0)
2372 {
2373 wip->wi_fpos.lnum = lnum;
2374 wip->wi_fpos.col = col;
2375 }
2376 if (copy_options)
2377 {
2378 /* Save the window-specific option values. */
2379 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2380#ifdef FEAT_FOLDING
2381 wip->wi_fold_manual = win->w_fold_manual;
2382 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2383#endif
2384 wip->wi_optset = TRUE;
2385 }
2386
2387 /* insert the entry in front of the list */
2388 wip->wi_next = buf->b_wininfo;
2389 buf->b_wininfo = wip;
2390 wip->wi_prev = NULL;
2391 if (wip->wi_next)
2392 wip->wi_next->wi_prev = wip;
2393
2394 return;
2395}
2396
2397/*
2398 * Find info for the current window in buffer "buf".
2399 * If not found, return the info for the most recently used window.
2400 * Returns NULL when there isn't any info.
2401 */
2402 static wininfo_T *
2403find_wininfo(buf)
2404 buf_T *buf;
2405{
2406 wininfo_T *wip;
2407
2408 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2409 if (wip->wi_win == curwin)
2410 break;
2411 if (wip == NULL) /* if no fpos for curwin, use the first in the list */
2412 wip = buf->b_wininfo;
2413 return wip;
2414}
2415
2416/*
2417 * Reset the local window options to the values last used in this window.
2418 * If the buffer wasn't used in this window before, use the values from
2419 * the most recently used window. If the values were never set, use the
2420 * global values for the window.
2421 */
2422 void
2423get_winopts(buf)
2424 buf_T *buf;
2425{
2426 wininfo_T *wip;
2427
2428 clear_winopt(&curwin->w_onebuf_opt);
2429#ifdef FEAT_FOLDING
2430 clearFolding(curwin);
2431#endif
2432
2433 wip = find_wininfo(buf);
2434 if (wip != NULL && wip->wi_optset)
2435 {
2436 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2437#ifdef FEAT_FOLDING
2438 curwin->w_fold_manual = wip->wi_fold_manual;
2439 curwin->w_foldinvalid = TRUE;
2440 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2441#endif
2442 }
2443 else
2444 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2445
2446#ifdef FEAT_FOLDING
2447 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2448 if (p_fdls >= 0)
2449 curwin->w_p_fdl = p_fdls;
2450#endif
2451}
2452
2453/*
2454 * Find the position (lnum and col) for the buffer 'buf' for the current
2455 * window.
2456 * Returns a pointer to no_position if no position is found.
2457 */
2458 pos_T *
2459buflist_findfpos(buf)
2460 buf_T *buf;
2461{
2462 wininfo_T *wip;
2463 static pos_T no_position = {1, 0};
2464
2465 wip = find_wininfo(buf);
2466 if (wip != NULL)
2467 return &(wip->wi_fpos);
2468 else
2469 return &no_position;
2470}
2471
2472/*
2473 * Find the lnum for the buffer 'buf' for the current window.
2474 */
2475 linenr_T
2476buflist_findlnum(buf)
2477 buf_T *buf;
2478{
2479 return buflist_findfpos(buf)->lnum;
2480}
2481
2482#if defined(FEAT_LISTCMDS) || defined(PROTO)
2483/*
2484 * List all know file names (for :files and :buffers command).
2485 */
2486/*ARGSUSED*/
2487 void
2488buflist_list(eap)
2489 exarg_T *eap;
2490{
2491 buf_T *buf;
2492 int len;
2493 int i;
2494
2495 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2496 {
2497 /* skip unlisted buffers, unless ! was used */
2498 if (!buf->b_p_bl && !eap->forceit)
2499 continue;
2500 msg_putchar('\n');
2501 if (buf_spname(buf) != NULL)
2502 STRCPY(NameBuff, buf_spname(buf));
2503 else
2504 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2505
Bram Moolenaar50cde822005-06-05 21:54:54 +00002506 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 buf->b_fnum,
2508 buf->b_p_bl ? ' ' : 'u',
2509 buf == curbuf ? '%' :
2510 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2511 buf->b_ml.ml_mfp == NULL ? ' ' :
2512 (buf->b_nwindows == 0 ? 'h' : 'a'),
2513 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2514 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002515 : (bufIsChanged(buf) ? '+' : ' '),
2516 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
2518 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 i = 40 - vim_strsize(IObuff);
2520 do
2521 {
2522 IObuff[len++] = ' ';
2523 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002524 vim_snprintf((char *)IObuff + len, IOSIZE - len, _("line %ld"),
2525 buf == curbuf ? curwin->w_cursor.lnum
2526 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 msg_outtrans(IObuff);
2528 out_flush(); /* output one line at a time */
2529 ui_breakcheck();
2530 }
2531}
2532#endif
2533
2534/*
2535 * Get file name and line number for file 'fnum'.
2536 * Used by DoOneCmd() for translating '%' and '#'.
2537 * Used by insert_reg() and cmdline_paste() for '#' register.
2538 * Return FAIL if not found, OK for success.
2539 */
2540 int
2541buflist_name_nr(fnum, fname, lnum)
2542 int fnum;
2543 char_u **fname;
2544 linenr_T *lnum;
2545{
2546 buf_T *buf;
2547
2548 buf = buflist_findnr(fnum);
2549 if (buf == NULL || buf->b_fname == NULL)
2550 return FAIL;
2551
2552 *fname = buf->b_fname;
2553 *lnum = buflist_findlnum(buf);
2554
2555 return OK;
2556}
2557
2558/*
2559 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2560 * The file name with the full path is also remembered, for when :cd is used.
2561 * Returns FAIL for failure (file name already in use by other buffer)
2562 * OK otherwise.
2563 */
2564 int
2565setfname(buf, ffname, sfname, message)
2566 buf_T *buf;
2567 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002568 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569{
Bram Moolenaar81695252004-12-29 20:58:21 +00002570 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571#ifdef UNIX
2572 struct stat st;
2573#endif
2574
2575 if (ffname == NULL || *ffname == NUL)
2576 {
2577 /* Removing the name. */
2578 vim_free(buf->b_ffname);
2579 vim_free(buf->b_sfname);
2580 buf->b_ffname = NULL;
2581 buf->b_sfname = NULL;
2582#ifdef UNIX
2583 st.st_dev = (dev_T)-1;
2584#endif
2585 }
2586 else
2587 {
2588 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2589 if (ffname == NULL) /* out of memory */
2590 return FAIL;
2591
2592 /*
2593 * if the file name is already used in another buffer:
2594 * - if the buffer is loaded, fail
2595 * - if the buffer is not loaded, delete it from the list
2596 */
2597#ifdef UNIX
2598 if (mch_stat((char *)ffname, &st) < 0)
2599 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002600#endif
2601 if (!(buf->b_flags & BF_DUMMY))
2602#ifdef UNIX
2603 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002605 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606#endif
2607 if (obuf != NULL && obuf != buf)
2608 {
2609 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2610 {
2611 if (message)
2612 EMSG(_("E95: Buffer with this name already exists"));
2613 vim_free(ffname);
2614 return FAIL;
2615 }
2616 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2617 }
2618 sfname = vim_strsave(sfname);
2619 if (ffname == NULL || sfname == NULL)
2620 {
2621 vim_free(sfname);
2622 vim_free(ffname);
2623 return FAIL;
2624 }
2625#ifdef USE_FNAME_CASE
2626# ifdef USE_LONG_FNAME
2627 if (USE_LONG_FNAME)
2628# endif
2629 fname_case(sfname, 0); /* set correct case for short file name */
2630#endif
2631 vim_free(buf->b_ffname);
2632 vim_free(buf->b_sfname);
2633 buf->b_ffname = ffname;
2634 buf->b_sfname = sfname;
2635 }
2636 buf->b_fname = buf->b_sfname;
2637#ifdef UNIX
2638 if (st.st_dev == (dev_T)-1)
2639 buf->b_dev = -1;
2640 else
2641 {
2642 buf->b_dev = st.st_dev;
2643 buf->b_ino = st.st_ino;
2644 }
2645#endif
2646
2647#ifndef SHORT_FNAME
2648 buf->b_shortname = FALSE;
2649#endif
2650
2651 buf_name_changed(buf);
2652 return OK;
2653}
2654
2655/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002656 * Crude way of changing the name of a buffer. Use with care!
2657 * The name should be relative to the current directory.
2658 */
2659 void
2660buf_set_name(fnum, name)
2661 int fnum;
2662 char_u *name;
2663{
2664 buf_T *buf;
2665
2666 buf = buflist_findnr(fnum);
2667 if (buf != NULL)
2668 {
2669 vim_free(buf->b_sfname);
2670 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002671 buf->b_ffname = vim_strsave(name);
2672 buf->b_sfname = NULL;
2673 /* Allocate ffname and expand into full path. Also resolves .lnk
2674 * files on Win32. */
2675 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002676 buf->b_fname = buf->b_sfname;
2677 }
2678}
2679
2680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 * Take care of what needs to be done when the name of buffer "buf" has
2682 * changed.
2683 */
2684 void
2685buf_name_changed(buf)
2686 buf_T *buf;
2687{
2688 /*
2689 * If the file name changed, also change the name of the swapfile
2690 */
2691 if (buf->b_ml.ml_mfp != NULL)
2692 ml_setname(buf);
2693
2694 if (curwin->w_buffer == buf)
2695 check_arg_idx(curwin); /* check file name for arg list */
2696#ifdef FEAT_TITLE
2697 maketitle(); /* set window title */
2698#endif
2699#ifdef FEAT_WINDOWS
2700 status_redraw_all(); /* status lines need to be redrawn */
2701#endif
2702 fmarks_check_names(buf); /* check named file marks */
2703 ml_timestamp(buf); /* reset timestamp */
2704}
2705
2706/*
2707 * set alternate file name for current window
2708 *
2709 * Used by do_one_cmd(), do_write() and do_ecmd().
2710 * Return the buffer.
2711 */
2712 buf_T *
2713setaltfname(ffname, sfname, lnum)
2714 char_u *ffname;
2715 char_u *sfname;
2716 linenr_T lnum;
2717{
2718 buf_T *buf;
2719
2720 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2721 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002722 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 curwin->w_alt_fnum = buf->b_fnum;
2724 return buf;
2725}
2726
2727/*
2728 * Get alternate file name for current window.
2729 * Return NULL if there isn't any, and give error message if requested.
2730 */
2731 char_u *
2732getaltfname(errmsg)
2733 int errmsg; /* give error message */
2734{
2735 char_u *fname;
2736 linenr_T dummy;
2737
2738 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2739 {
2740 if (errmsg)
2741 EMSG(_(e_noalt));
2742 return NULL;
2743 }
2744 return fname;
2745}
2746
2747/*
2748 * Add a file name to the buflist and return its number.
2749 * Uses same flags as buflist_new(), except BLN_DUMMY.
2750 *
2751 * used by qf_init(), main() and doarglist()
2752 */
2753 int
2754buflist_add(fname, flags)
2755 char_u *fname;
2756 int flags;
2757{
2758 buf_T *buf;
2759
2760 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2761 if (buf != NULL)
2762 return buf->b_fnum;
2763 return 0;
2764}
2765
2766#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2767/*
2768 * Adjust slashes in file names. Called after 'shellslash' was set.
2769 */
2770 void
2771buflist_slash_adjust()
2772{
2773 buf_T *bp;
2774
2775 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2776 {
2777 if (bp->b_ffname != NULL)
2778 slash_adjust(bp->b_ffname);
2779 if (bp->b_sfname != NULL)
2780 slash_adjust(bp->b_sfname);
2781 }
2782}
2783#endif
2784
2785/*
2786 * Set alternate cursor position for current window.
2787 * Also save the local window option values.
2788 */
2789 void
2790buflist_altfpos()
2791{
2792 buflist_setfpos(curbuf, curwin, curwin->w_cursor.lnum,
2793 curwin->w_cursor.col, TRUE);
2794}
2795
2796/*
2797 * Return TRUE if 'ffname' is not the same file as current file.
2798 * Fname must have a full path (expanded by mch_FullName()).
2799 */
2800 int
2801otherfile(ffname)
2802 char_u *ffname;
2803{
2804 return otherfile_buf(curbuf, ffname
2805#ifdef UNIX
2806 , NULL
2807#endif
2808 );
2809}
2810
2811 static int
2812otherfile_buf(buf, ffname
2813#ifdef UNIX
2814 , stp
2815#endif
2816 )
2817 buf_T *buf;
2818 char_u *ffname;
2819#ifdef UNIX
2820 struct stat *stp;
2821#endif
2822{
2823 /* no name is different */
2824 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2825 return TRUE;
2826 if (fnamecmp(ffname, buf->b_ffname) == 0)
2827 return FALSE;
2828#ifdef UNIX
2829 {
2830 struct stat st;
2831
2832 /* If no struct stat given, get it now */
2833 if (stp == NULL)
2834 {
2835 if (buf->b_dev < 0 || mch_stat((char *)ffname, &st) < 0)
2836 st.st_dev = (dev_T)-1;
2837 stp = &st;
2838 }
2839 /* Use dev/ino to check if the files are the same, even when the names
2840 * are different (possible with links). Still need to compare the
2841 * name above, for when the file doesn't exist yet.
2842 * Problem: The dev/ino changes when a file is deleted (and created
2843 * again) and remains the same when renamed/moved. We don't want to
2844 * mch_stat() each buffer each time, that would be too slow. Get the
2845 * dev/ino again when they appear to match, but not when they appear
2846 * to be different: Could skip a buffer when it's actually the same
2847 * file. */
2848 if (buf_same_ino(buf, stp))
2849 {
2850 buf_setino(buf);
2851 if (buf_same_ino(buf, stp))
2852 return FALSE;
2853 }
2854 }
2855#endif
2856 return TRUE;
2857}
2858
2859#if defined(UNIX) || defined(PROTO)
2860/*
2861 * Set inode and device number for a buffer.
2862 * Must always be called when b_fname is changed!.
2863 */
2864 void
2865buf_setino(buf)
2866 buf_T *buf;
2867{
2868 struct stat st;
2869
2870 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2871 {
2872 buf->b_dev = st.st_dev;
2873 buf->b_ino = st.st_ino;
2874 }
2875 else
2876 buf->b_dev = -1;
2877}
2878
2879/*
2880 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2881 */
2882 static int
2883buf_same_ino(buf, stp)
2884 buf_T *buf;
2885 struct stat *stp;
2886{
2887 return (buf->b_dev >= 0
2888 && stp->st_dev == buf->b_dev
2889 && stp->st_ino == buf->b_ino);
2890}
2891#endif
2892
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002893/*
2894 * Print info about the current buffer.
2895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 void
2897fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002898 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 int shorthelp;
2900 int dont_truncate;
2901{
2902 char_u *name;
2903 int n;
2904 char_u *p;
2905 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002906 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907
2908 buffer = alloc(IOSIZE);
2909 if (buffer == NULL)
2910 return;
2911
2912 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2913 {
2914 sprintf((char *)buffer, "buf %d: ", curbuf->b_fnum);
2915 p = buffer + STRLEN(buffer);
2916 }
2917 else
2918 p = buffer;
2919
2920 *p++ = '"';
2921 if (buf_spname(curbuf) != NULL)
2922 STRCPY(p, buf_spname(curbuf));
2923 else
2924 {
2925 if (!fullname && curbuf->b_fname != NULL)
2926 name = curbuf->b_fname;
2927 else
2928 name = curbuf->b_ffname;
2929 home_replace(shorthelp ? curbuf : NULL, name, p,
2930 (int)(IOSIZE - (p - buffer)), TRUE);
2931 }
2932
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002933 len = STRLEN(buffer);
2934 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 "\"%s%s%s%s%s%s",
2936 curbufIsChanged() ? (shortmess(SHM_MOD)
2937 ? " [+]" : _(" [Modified]")) : " ",
2938 (curbuf->b_flags & BF_NOTEDITED)
2939#ifdef FEAT_QUICKFIX
2940 && !bt_dontwrite(curbuf)
2941#endif
2942 ? _("[Not edited]") : "",
2943 (curbuf->b_flags & BF_NEW)
2944#ifdef FEAT_QUICKFIX
2945 && !bt_dontwrite(curbuf)
2946#endif
2947 ? _("[New file]") : "",
2948 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
2949 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
2950 : _("[readonly]")) : "",
2951 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
2952 || curbuf->b_p_ro) ?
2953 " " : "");
2954 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
2955 * causes an overflow, thus for large numbers divide instead. */
2956 if (curwin->w_cursor.lnum > 1000000L)
2957 n = (int)(((long)curwin->w_cursor.lnum) /
2958 ((long)curbuf->b_ml.ml_line_count / 100L));
2959 else
2960 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
2961 (long)curbuf->b_ml.ml_line_count);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002962 len = STRLEN(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2964 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002965 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 }
2967#ifdef FEAT_CMDL_INFO
2968 else if (p_ru)
2969 {
2970 /* Current line and column are already on the screen -- webb */
2971 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002972 vim_snprintf((char *)buffer + len, IOSIZE - len,
2973 _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002975 vim_snprintf((char *)buffer + len, IOSIZE - len,
2976 _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 (long)curbuf->b_ml.ml_line_count, n);
2978 }
2979#endif
2980 else
2981 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002982 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 _("line %ld of %ld --%d%%-- col "),
2984 (long)curwin->w_cursor.lnum,
2985 (long)curbuf->b_ml.ml_line_count,
2986 n);
2987 validate_virtcol();
2988 col_print(buffer + STRLEN(buffer),
2989 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
2990 }
2991
2992 (void)append_arg_number(curwin, buffer, !shortmess(SHM_FILE), IOSIZE);
2993
2994 if (dont_truncate)
2995 {
2996 /* Temporarily set msg_scroll to avoid the message being truncated.
2997 * First call msg_start() to get the message in the right place. */
2998 msg_start();
2999 n = msg_scroll;
3000 msg_scroll = TRUE;
3001 msg(buffer);
3002 msg_scroll = n;
3003 }
3004 else
3005 {
3006 p = msg_trunc_attr(buffer, FALSE, 0);
3007 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 /* Need to repeat the message after redrawing when:
3009 * - When restart_edit is set (otherwise there will be a delay
3010 * before redrawing).
3011 * - When the screen was scrolled but there is no wait-return
3012 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003013 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 }
3015
3016 vim_free(buffer);
3017}
3018
3019 void
3020col_print(buf, col, vcol)
3021 char_u *buf;
3022 int col;
3023 int vcol;
3024{
3025 if (col == vcol)
3026 sprintf((char *)buf, "%d", col);
3027 else
3028 sprintf((char *)buf, "%d-%d", col, vcol);
3029}
3030
3031#if defined(FEAT_TITLE) || defined(PROTO)
3032/*
3033 * put file name in title bar of window and in icon title
3034 */
3035
3036static char_u *lasttitle = NULL;
3037static char_u *lasticon = NULL;
3038
3039 void
3040maketitle()
3041{
3042 char_u *p;
3043 char_u *t_str = NULL;
3044 char_u *i_name;
3045 char_u *i_str = NULL;
3046 int maxlen = 0;
3047 int len;
3048 int mustset;
3049 char_u buf[IOSIZE];
3050 int off;
3051
3052 if (!redrawing())
3053 {
3054 /* Postpone updating the title when 'lazyredraw' is set. */
3055 need_maketitle = TRUE;
3056 return;
3057 }
3058
3059 need_maketitle = FALSE;
3060 if (!p_title && !p_icon)
3061 return;
3062
3063 if (p_title)
3064 {
3065 if (p_titlelen > 0)
3066 {
3067 maxlen = p_titlelen * Columns / 100;
3068 if (maxlen < 10)
3069 maxlen = 10;
3070 }
3071
3072 t_str = buf;
3073 if (*p_titlestring != NUL)
3074 {
3075#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003076 if (stl_syntax & STL_IN_TITLE)
3077 {
3078 int use_sandbox = FALSE;
3079 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003080
3081# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003082 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003083# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003084 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003086 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003087 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003088 if (called_emsg)
3089 set_string_option_direct((char_u *)"titlestring", -1,
3090 (char_u *)"", OPT_FREE, SID_ERROR);
3091 called_emsg |= save_called_emsg;
3092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 else
3094#endif
3095 t_str = p_titlestring;
3096 }
3097 else
3098 {
3099 /* format: "fname + (path) (1 of 2) - VIM" */
3100
3101 if (curbuf->b_fname == NULL)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003102 STRCPY(buf, _("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 else
3104 {
3105 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003106 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 }
3109
3110 switch (bufIsChanged(curbuf)
3111 + (curbuf->b_p_ro * 2)
3112 + (!curbuf->b_p_ma * 4))
3113 {
3114 case 1: STRCAT(buf, " +"); break;
3115 case 2: STRCAT(buf, " ="); break;
3116 case 3: STRCAT(buf, " =+"); break;
3117 case 4:
3118 case 6: STRCAT(buf, " -"); break;
3119 case 5:
3120 case 7: STRCAT(buf, " -+"); break;
3121 }
3122
3123 if (curbuf->b_fname != NULL)
3124 {
3125 /* Get path of file, replace home dir with ~ */
3126 off = (int)STRLEN(buf);
3127 buf[off++] = ' ';
3128 buf[off++] = '(';
3129 home_replace(curbuf, curbuf->b_ffname,
3130 buf + off, IOSIZE - off, TRUE);
3131#ifdef BACKSLASH_IN_FILENAME
3132 /* avoid "c:/name" to be reduced to "c" */
3133 if (isalpha(buf[off]) && buf[off + 1] == ':')
3134 off += 2;
3135#endif
3136 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003137 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003140 vim_strncpy(buf + off, (char_u *)_("help"),
3141 IOSIZE - off - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 /* translate unprintable chars */
3146 p = transstr(buf + off);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003147 vim_strncpy(buf + off, p, IOSIZE - off - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 STRCAT(buf, ")");
3150 }
3151
3152 append_arg_number(curwin, buf, FALSE, IOSIZE);
3153
3154#if defined(FEAT_CLIENTSERVER)
3155 if (serverName != NULL)
3156 {
3157 STRCAT(buf, " - ");
3158 STRCAT(buf, serverName);
3159 }
3160 else
3161#endif
3162 STRCAT(buf, " - VIM");
3163
3164 if (maxlen > 0)
3165 {
3166 /* make it shorter by removing a bit in the middle */
3167 len = vim_strsize(buf);
3168 if (len > maxlen)
3169 trunc_string(buf, buf, maxlen);
3170 }
3171 }
3172 }
3173 mustset = ti_change(t_str, &lasttitle);
3174
3175 if (p_icon)
3176 {
3177 i_str = buf;
3178 if (*p_iconstring != NUL)
3179 {
3180#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003181 if (stl_syntax & STL_IN_ICON)
3182 {
3183 int use_sandbox = FALSE;
3184 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003185
3186# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003187 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003188# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003189 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003191 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003192 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003193 if (called_emsg)
3194 set_string_option_direct((char_u *)"iconstring", -1,
3195 (char_u *)"", OPT_FREE, SID_ERROR);
3196 called_emsg |= save_called_emsg;
3197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 else
3199#endif
3200 i_str = p_iconstring;
3201 }
3202 else
3203 {
3204 if (buf_spname(curbuf) != NULL)
3205 i_name = (char_u *)buf_spname(curbuf);
3206 else /* use file name only in icon */
3207 i_name = gettail(curbuf->b_ffname);
3208 *i_str = NUL;
3209 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003210 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 if (len > 100)
3212 {
3213 len -= 100;
3214#ifdef FEAT_MBYTE
3215 if (has_mbyte)
3216 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3217#endif
3218 i_name += len;
3219 }
3220 STRCPY(i_str, i_name);
3221 trans_characters(i_str, IOSIZE);
3222 }
3223 }
3224
3225 mustset |= ti_change(i_str, &lasticon);
3226
3227 if (mustset)
3228 resettitle();
3229}
3230
3231/*
3232 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3233 * from "str" if it does.
3234 * Return TRUE when "*last" changed.
3235 */
3236 static int
3237ti_change(str, last)
3238 char_u *str;
3239 char_u **last;
3240{
3241 if ((str == NULL) != (*last == NULL)
3242 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3243 {
3244 vim_free(*last);
3245 if (str == NULL)
3246 *last = NULL;
3247 else
3248 *last = vim_strsave(str);
3249 return TRUE;
3250 }
3251 return FALSE;
3252}
3253
3254/*
3255 * Put current window title back (used after calling a shell)
3256 */
3257 void
3258resettitle()
3259{
3260 mch_settitle(lasttitle, lasticon);
3261}
Bram Moolenaarea408852005-06-25 22:49:46 +00003262
3263# if defined(EXITFREE) || defined(PROTO)
3264 void
3265free_titles()
3266{
3267 vim_free(lasttitle);
3268 vim_free(lasticon);
3269}
3270# endif
3271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272#endif /* FEAT_TITLE */
3273
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003274#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003276 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 * Return length of string in screen cells.
3278 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003279 * Normally works for window "wp", except when working for 'tabline' then it
3280 * is "curwin".
3281 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 * Items are drawn interspersed with the text that surrounds it
3283 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3284 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3285 *
3286 * If maxwidth is not zero, the string will be filled at any middle marker
3287 * or truncated if too long, fillchar is used for all whitespace.
3288 */
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003289/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003291build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003293 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 size_t outlen; /* length of out[] */
3295 char_u *fmt;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003296 int use_sandbox; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 int fillchar;
3298 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003299 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3300 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
3302 char_u *p;
3303 char_u *s;
3304 char_u *t;
3305 char_u *linecont;
3306#ifdef FEAT_EVAL
3307 win_T *o_curwin;
3308 buf_T *o_curbuf;
3309#endif
3310 int empty_line;
3311 colnr_T virtcol;
3312 long l;
3313 long n;
3314 int prevchar_isflag;
3315 int prevchar_isitem;
3316 int itemisflag;
3317 int fillable;
3318 char_u *str;
3319 long num;
3320 int width;
3321 int itemcnt;
3322 int curitem;
3323 int groupitem[STL_MAX_ITEM];
3324 int groupdepth;
3325 struct stl_item
3326 {
3327 char_u *start;
3328 int minwid;
3329 int maxwid;
3330 enum
3331 {
3332 Normal,
3333 Empty,
3334 Group,
3335 Middle,
3336 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003337 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 Trunc
3339 } type;
3340 } item[STL_MAX_ITEM];
3341 int minwid;
3342 int maxwid;
3343 int zeropad;
3344 char_u base;
3345 char_u opt;
3346#define TMPLEN 70
3347 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003348 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003349 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003350
3351#ifdef FEAT_EVAL
3352 /*
3353 * When the format starts with "%!" then evaluate it as an expression and
3354 * use the result as the actual format string.
3355 */
3356 if (fmt[0] == '%' && fmt[1] == '!')
3357 {
3358 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3359 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003360 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003361 }
3362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
3364 if (fillchar == 0)
3365 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003366#ifdef FEAT_MBYTE
3367 /* Can't handle a multi-byte fill character yet. */
3368 else if (mb_char2len(fillchar) > 1)
3369 fillchar = '-';
3370#endif
3371
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 /*
3373 * Get line & check if empty (cursorpos will show "0-1").
3374 * If inversion is possible we use it. Else '=' characters are used.
3375 */
3376 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3377 empty_line = (*linecont == NUL);
3378
3379 groupdepth = 0;
3380 p = out;
3381 curitem = 0;
3382 prevchar_isflag = TRUE;
3383 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003384 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
3386 if (*s != NUL && *s != '%')
3387 prevchar_isflag = prevchar_isitem = FALSE;
3388
3389 /*
3390 * Handle up to the next '%' or the end.
3391 */
3392 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3393 *p++ = *s++;
3394 if (*s == NUL || p + 1 >= out + outlen)
3395 break;
3396
3397 /*
3398 * Handle one '%' item.
3399 */
3400 s++;
3401 if (*s == '%')
3402 {
3403 if (p + 1 >= out + outlen)
3404 break;
3405 *p++ = *s++;
3406 prevchar_isflag = prevchar_isitem = FALSE;
3407 continue;
3408 }
3409 if (*s == STL_MIDDLEMARK)
3410 {
3411 s++;
3412 if (groupdepth > 0)
3413 continue;
3414 item[curitem].type = Middle;
3415 item[curitem++].start = p;
3416 continue;
3417 }
3418 if (*s == STL_TRUNCMARK)
3419 {
3420 s++;
3421 item[curitem].type = Trunc;
3422 item[curitem++].start = p;
3423 continue;
3424 }
3425 if (*s == ')')
3426 {
3427 s++;
3428 if (groupdepth < 1)
3429 continue;
3430 groupdepth--;
3431
3432 t = item[groupitem[groupdepth]].start;
3433 *p = NUL;
3434 l = vim_strsize(t);
3435 if (curitem > groupitem[groupdepth] + 1
3436 && item[groupitem[groupdepth]].minwid == 0)
3437 {
3438 /* remove group if all items are empty */
3439 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3440 if (item[n].type == Normal)
3441 break;
3442 if (n == curitem)
3443 {
3444 p = t;
3445 l = 0;
3446 }
3447 }
3448 if (l > item[groupitem[groupdepth]].maxwid)
3449 {
3450 /* truncate, remove n bytes of text at the start */
3451#ifdef FEAT_MBYTE
3452 if (has_mbyte)
3453 {
3454 /* Find the first character that should be included. */
3455 n = 0;
3456 while (l >= item[groupitem[groupdepth]].maxwid)
3457 {
3458 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003459 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 }
3461 }
3462 else
3463#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003464 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
3466 *t = '<';
3467 mch_memmove(t + 1, t + n, p - (t + n));
3468 p = p - n + 1;
3469#ifdef FEAT_MBYTE
3470 /* Fill up space left over by half a double-wide char. */
3471 while (++l < item[groupitem[groupdepth]].minwid)
3472 *p++ = fillchar;
3473#endif
3474
3475 /* correct the start of the items for the truncation */
3476 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3477 {
3478 item[l].start -= n;
3479 if (item[l].start < t)
3480 item[l].start = t;
3481 }
3482 }
3483 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3484 {
3485 /* fill */
3486 n = item[groupitem[groupdepth]].minwid;
3487 if (n < 0)
3488 {
3489 /* fill by appending characters */
3490 n = 0 - n;
3491 while (l++ < n && p + 1 < out + outlen)
3492 *p++ = fillchar;
3493 }
3494 else
3495 {
3496 /* fill by inserting characters */
3497 mch_memmove(t + n - l, t, p - t);
3498 l = n - l;
3499 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003500 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 p += l;
3502 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3503 item[n].start += l;
3504 for ( ; l > 0; l--)
3505 *t++ = fillchar;
3506 }
3507 }
3508 continue;
3509 }
3510 minwid = 0;
3511 maxwid = 9999;
3512 zeropad = FALSE;
3513 l = 1;
3514 if (*s == '0')
3515 {
3516 s++;
3517 zeropad = TRUE;
3518 }
3519 if (*s == '-')
3520 {
3521 s++;
3522 l = -1;
3523 }
3524 if (VIM_ISDIGIT(*s))
3525 {
3526 minwid = (int)getdigits(&s);
3527 if (minwid < 0) /* overflow */
3528 minwid = 0;
3529 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003530 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
3532 item[curitem].type = Highlight;
3533 item[curitem].start = p;
3534 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3535 s++;
3536 curitem++;
3537 continue;
3538 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003539 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3540 {
3541 if (*s == STL_TABCLOSENR)
3542 {
3543 if (minwid == 0)
3544 {
3545 /* %X ends the close label, go back to the previously
3546 * define tab label nr. */
3547 for (n = curitem - 1; n >= 0; --n)
3548 if (item[n].type == TabPage && item[n].minwid >= 0)
3549 {
3550 minwid = item[n].minwid;
3551 break;
3552 }
3553 }
3554 else
3555 /* close nrs are stored as negative values */
3556 minwid = - minwid;
3557 }
3558 item[curitem].type = TabPage;
3559 item[curitem].start = p;
3560 item[curitem].minwid = minwid;
3561 s++;
3562 curitem++;
3563 continue;
3564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 if (*s == '.')
3566 {
3567 s++;
3568 if (VIM_ISDIGIT(*s))
3569 {
3570 maxwid = (int)getdigits(&s);
3571 if (maxwid <= 0) /* overflow */
3572 maxwid = 50;
3573 }
3574 }
3575 minwid = (minwid > 50 ? 50 : minwid) * l;
3576 if (*s == '(')
3577 {
3578 groupitem[groupdepth++] = curitem;
3579 item[curitem].type = Group;
3580 item[curitem].start = p;
3581 item[curitem].minwid = minwid;
3582 item[curitem].maxwid = maxwid;
3583 s++;
3584 curitem++;
3585 continue;
3586 }
3587 if (vim_strchr(STL_ALL, *s) == NULL)
3588 {
3589 s++;
3590 continue;
3591 }
3592 opt = *s++;
3593
3594 /* OK - now for the real work */
3595 base = 'D';
3596 itemisflag = FALSE;
3597 fillable = TRUE;
3598 num = -1;
3599 str = NULL;
3600 switch (opt)
3601 {
3602 case STL_FILEPATH:
3603 case STL_FULLPATH:
3604 case STL_FILENAME:
3605 fillable = FALSE; /* don't change ' ' to fillchar */
3606 if (buf_spname(wp->w_buffer) != NULL)
3607 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3608 else
3609 {
3610 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003611 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3613 }
3614 trans_characters(NameBuff, MAXPATHL);
3615 if (opt != STL_FILENAME)
3616 str = NameBuff;
3617 else
3618 str = gettail(NameBuff);
3619 break;
3620
3621 case STL_VIM_EXPR: /* '{' */
3622 itemisflag = TRUE;
3623 t = p;
3624 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3625 *p++ = *s++;
3626 if (*s != '}') /* missing '}' or out of space */
3627 break;
3628 s++;
3629 *p = 0;
3630 p = t;
3631
3632#ifdef FEAT_EVAL
3633 sprintf((char *)tmp, "%d", curbuf->b_fnum);
3634 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3635
3636 o_curbuf = curbuf;
3637 o_curwin = curwin;
3638 curwin = wp;
3639 curbuf = wp->w_buffer;
3640
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003641 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642
3643 curwin = o_curwin;
3644 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003645 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
3647 if (str != NULL && *str != 0)
3648 {
3649 if (*skipdigits(str) == NUL)
3650 {
3651 num = atoi((char *)str);
3652 vim_free(str);
3653 str = NULL;
3654 itemisflag = FALSE;
3655 }
3656 }
3657#endif
3658 break;
3659
3660 case STL_LINE:
3661 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3662 ? 0L : (long)(wp->w_cursor.lnum);
3663 break;
3664
3665 case STL_NUMLINES:
3666 num = wp->w_buffer->b_ml.ml_line_count;
3667 break;
3668
3669 case STL_COLUMN:
3670 num = !(State & INSERT) && empty_line
3671 ? 0 : (int)wp->w_cursor.col + 1;
3672 break;
3673
3674 case STL_VIRTCOL:
3675 case STL_VIRTCOL_ALT:
3676 /* In list mode virtcol needs to be recomputed */
3677 virtcol = wp->w_virtcol;
3678 if (wp->w_p_list && lcs_tab1 == NUL)
3679 {
3680 wp->w_p_list = FALSE;
3681 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3682 wp->w_p_list = TRUE;
3683 }
3684 ++virtcol;
3685 /* Don't display %V if it's the same as %c. */
3686 if (opt == STL_VIRTCOL_ALT
3687 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3688 ? 0 : (int)wp->w_cursor.col + 1)))
3689 break;
3690 num = (long)virtcol;
3691 break;
3692
3693 case STL_PERCENTAGE:
3694 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3695 (long)wp->w_buffer->b_ml.ml_line_count);
3696 break;
3697
3698 case STL_ALTPERCENT:
3699 str = tmp;
3700 get_rel_pos(wp, str);
3701 break;
3702
3703 case STL_ARGLISTSTAT:
3704 fillable = FALSE;
3705 tmp[0] = 0;
3706 if (append_arg_number(wp, tmp, FALSE, (int)sizeof(tmp)))
3707 str = tmp;
3708 break;
3709
3710 case STL_KEYMAP:
3711 fillable = FALSE;
3712 if (get_keymap_str(wp, tmp, TMPLEN))
3713 str = tmp;
3714 break;
3715 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003716#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003717 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718#else
3719 num = 0;
3720#endif
3721 break;
3722
3723 case STL_BUFNO:
3724 num = wp->w_buffer->b_fnum;
3725 break;
3726
3727 case STL_OFFSET_X:
3728 base = 'X';
3729 case STL_OFFSET:
3730#ifdef FEAT_BYTEOFF
3731 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3732 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3733 0L : l + 1 + (!(State & INSERT) && empty_line ?
3734 0 : (int)wp->w_cursor.col);
3735#endif
3736 break;
3737
3738 case STL_BYTEVAL_X:
3739 base = 'X';
3740 case STL_BYTEVAL:
Bram Moolenaar49104e42007-03-15 21:54:01 +00003741 if (wp->w_cursor.col > STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 num = 0;
3743 else
3744 {
3745#ifdef FEAT_MBYTE
3746 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3747#else
3748 num = linecont[wp->w_cursor.col];
3749#endif
3750 }
3751 if (num == NL)
3752 num = 0;
3753 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3754 num = NL;
3755 break;
3756
3757 case STL_ROFLAG:
3758 case STL_ROFLAG_ALT:
3759 itemisflag = TRUE;
3760 if (wp->w_buffer->b_p_ro)
3761 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3762 break;
3763
3764 case STL_HELPFLAG:
3765 case STL_HELPFLAG_ALT:
3766 itemisflag = TRUE;
3767 if (wp->w_buffer->b_help)
3768 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003769 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 break;
3771
3772#ifdef FEAT_AUTOCMD
3773 case STL_FILETYPE:
3774 if (*wp->w_buffer->b_p_ft != NUL
3775 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3776 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003777 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3778 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 str = tmp;
3780 }
3781 break;
3782
3783 case STL_FILETYPE_ALT:
3784 itemisflag = TRUE;
3785 if (*wp->w_buffer->b_p_ft != NUL
3786 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3787 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003788 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3789 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 for (t = tmp; *t != 0; t++)
3791 *t = TOUPPER_LOC(*t);
3792 str = tmp;
3793 }
3794 break;
3795#endif
3796
3797#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3798 case STL_PREVIEWFLAG:
3799 case STL_PREVIEWFLAG_ALT:
3800 itemisflag = TRUE;
3801 if (wp->w_p_pvw)
3802 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3803 : _("[Preview]"));
3804 break;
3805#endif
3806
3807 case STL_MODIFIED:
3808 case STL_MODIFIED_ALT:
3809 itemisflag = TRUE;
3810 switch ((opt == STL_MODIFIED_ALT)
3811 + bufIsChanged(wp->w_buffer) * 2
3812 + (!wp->w_buffer->b_p_ma) * 4)
3813 {
3814 case 2: str = (char_u *)"[+]"; break;
3815 case 3: str = (char_u *)",+"; break;
3816 case 4: str = (char_u *)"[-]"; break;
3817 case 5: str = (char_u *)",-"; break;
3818 case 6: str = (char_u *)"[+-]"; break;
3819 case 7: str = (char_u *)",+-"; break;
3820 }
3821 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003822
3823 case STL_HIGHLIGHT:
3824 t = s;
3825 while (*s != '#' && *s != NUL)
3826 ++s;
3827 if (*s == '#')
3828 {
3829 item[curitem].type = Highlight;
3830 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003831 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003832 curitem++;
3833 }
3834 ++s;
3835 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837
3838 item[curitem].start = p;
3839 item[curitem].type = Normal;
3840 if (str != NULL && *str)
3841 {
3842 t = str;
3843 if (itemisflag)
3844 {
3845 if ((t[0] && t[1])
3846 && ((!prevchar_isitem && *t == ',')
3847 || (prevchar_isflag && *t == ' ')))
3848 t++;
3849 prevchar_isflag = TRUE;
3850 }
3851 l = vim_strsize(t);
3852 if (l > 0)
3853 prevchar_isitem = TRUE;
3854 if (l > maxwid)
3855 {
3856 while (l >= maxwid)
3857#ifdef FEAT_MBYTE
3858 if (has_mbyte)
3859 {
3860 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003861 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 }
3863 else
3864#endif
3865 l -= byte2cells(*t++);
3866 if (p + 1 >= out + outlen)
3867 break;
3868 *p++ = '<';
3869 }
3870 if (minwid > 0)
3871 {
3872 for (; l < minwid && p + 1 < out + outlen; l++)
3873 {
3874 /* Don't put a "-" in front of a digit. */
3875 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3876 *p++ = ' ';
3877 else
3878 *p++ = fillchar;
3879 }
3880 minwid = 0;
3881 }
3882 else
3883 minwid *= -1;
3884 while (*t && p + 1 < out + outlen)
3885 {
3886 *p++ = *t++;
3887 /* Change a space by fillchar, unless fillchar is '-' and a
3888 * digit follows. */
3889 if (fillable && p[-1] == ' '
3890 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3891 p[-1] = fillchar;
3892 }
3893 for (; l < minwid && p + 1 < out + outlen; l++)
3894 *p++ = fillchar;
3895 }
3896 else if (num >= 0)
3897 {
3898 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3899 char_u nstr[20];
3900
3901 if (p + 20 >= out + outlen)
3902 break; /* not sufficient space */
3903 prevchar_isitem = TRUE;
3904 t = nstr;
3905 if (opt == STL_VIRTCOL_ALT)
3906 {
3907 *t++ = '-';
3908 minwid--;
3909 }
3910 *t++ = '%';
3911 if (zeropad)
3912 *t++ = '0';
3913 *t++ = '*';
3914 *t++ = nbase == 16 ? base : (nbase == 8 ? 'o' : 'd');
3915 *t = 0;
3916
3917 for (n = num, l = 1; n >= nbase; n /= nbase)
3918 l++;
3919 if (opt == STL_VIRTCOL_ALT)
3920 l++;
3921 if (l > maxwid)
3922 {
3923 l += 2;
3924 n = l - maxwid;
3925 while (l-- > maxwid)
3926 num /= nbase;
3927 *t++ = '>';
3928 *t++ = '%';
3929 *t = t[-3];
3930 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003931 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3932 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
3934 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003935 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3936 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 p += STRLEN(p);
3938 }
3939 else
3940 item[curitem].type = Empty;
3941
3942 if (opt == STL_VIM_EXPR)
3943 vim_free(str);
3944
3945 if (num >= 0 || (!itemisflag && str && *str))
3946 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
3947 curitem++;
3948 }
3949 *p = NUL;
3950 itemcnt = curitem;
3951
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003952#ifdef FEAT_EVAL
3953 if (usefmt != fmt)
3954 vim_free(usefmt);
3955#endif
3956
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 width = vim_strsize(out);
3958 if (maxwidth > 0 && width > maxwidth)
3959 {
3960 /* Result is too long, must trunctate somewhere. */
3961 l = 0;
3962 if (itemcnt == 0)
3963 s = out;
3964 else
3965 {
3966 for ( ; l < itemcnt; l++)
3967 if (item[l].type == Trunc)
3968 {
3969 /* Truncate at %< item. */
3970 s = item[l].start;
3971 break;
3972 }
3973 if (l == itemcnt)
3974 {
3975 /* No %< item, truncate first item. */
3976 s = item[0].start;
3977 l = 0;
3978 }
3979 }
3980
3981 if (width - vim_strsize(s) >= maxwidth)
3982 {
3983 /* Truncation mark is beyond max length */
3984#ifdef FEAT_MBYTE
3985 if (has_mbyte)
3986 {
3987 s = out;
3988 width = 0;
3989 for (;;)
3990 {
3991 width += ptr2cells(s);
3992 if (width >= maxwidth)
3993 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003994 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 }
3996 /* Fill up for half a double-wide character. */
3997 while (++width < maxwidth)
3998 *s++ = fillchar;
3999 }
4000 else
4001#endif
4002 s = out + maxwidth - 1;
4003 for (l = 0; l < itemcnt; l++)
4004 if (item[l].start > s)
4005 break;
4006 itemcnt = l;
4007 *s++ = '>';
4008 *s = 0;
4009 }
4010 else
4011 {
4012#ifdef FEAT_MBYTE
4013 if (has_mbyte)
4014 {
4015 n = 0;
4016 while (width >= maxwidth)
4017 {
4018 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004019 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
4021 }
4022 else
4023#endif
4024 n = width - maxwidth + 1;
4025 p = s + n;
4026 mch_memmove(s + 1, p, STRLEN(p) + 1);
4027 *s = '<';
4028
4029 /* Fill up for half a double-wide character. */
4030 while (++width < maxwidth)
4031 {
4032 s = s + STRLEN(s);
4033 *s++ = fillchar;
4034 *s = NUL;
4035 }
4036
4037 --n; /* count the '<' */
4038 for (; l < itemcnt; l++)
4039 {
4040 if (item[l].start - n >= s)
4041 item[l].start -= n;
4042 else
4043 item[l].start = s;
4044 }
4045 }
4046 width = maxwidth;
4047 }
4048 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4049 {
4050 /* Apply STL_MIDDLE if any */
4051 for (l = 0; l < itemcnt; l++)
4052 if (item[l].type == Middle)
4053 break;
4054 if (l < itemcnt)
4055 {
4056 p = item[l].start + maxwidth - width;
4057 mch_memmove(p, item[l].start, STRLEN(item[l].start) + 1);
4058 for (s = item[l].start; s < p; s++)
4059 *s = fillchar;
4060 for (l++; l < itemcnt; l++)
4061 item[l].start += maxwidth - width;
4062 width = maxwidth;
4063 }
4064 }
4065
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004066 /* Store the info about highlighting. */
4067 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004069 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 for (l = 0; l < itemcnt; l++)
4071 {
4072 if (item[l].type == Highlight)
4073 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004074 sp->start = item[l].start;
4075 sp->userhl = item[l].minwid;
4076 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
4078 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004079 sp->start = NULL;
4080 sp->userhl = 0;
4081 }
4082
4083 /* Store the info about tab pages labels. */
4084 if (tabtab != NULL)
4085 {
4086 sp = tabtab;
4087 for (l = 0; l < itemcnt; l++)
4088 {
4089 if (item[l].type == TabPage)
4090 {
4091 sp->start = item[l].start;
4092 sp->userhl = item[l].minwid;
4093 sp++;
4094 }
4095 }
4096 sp->start = NULL;
4097 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 }
4099
4100 return width;
4101}
4102#endif /* FEAT_STL_OPT */
4103
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004104#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4105 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106/*
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004107 * Get relative cursor position in window into "str[]", in the form 99%, using
4108 * "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 */
4110 void
4111get_rel_pos(wp, str)
4112 win_T *wp;
4113 char_u *str;
4114{
4115 long above; /* number of lines above window */
4116 long below; /* number of lines below window */
4117
4118 above = wp->w_topline - 1;
4119#ifdef FEAT_DIFF
4120 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4121#endif
4122 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4123 if (below <= 0)
4124 STRCPY(str, above == 0 ? _("All") : _("Bot"));
4125 else if (above <= 0)
4126 STRCPY(str, _("Top"));
4127 else
4128 sprintf((char *)str, "%2d%%", above > 1000000L
4129 ? (int)(above / ((above + below) / 100L))
4130 : (int)(above * 100L / (above + below)));
4131}
4132#endif
4133
4134/*
4135 * Append (file 2 of 8) to 'buf', if editing more than one file.
4136 * Return TRUE if it was appended.
4137 */
4138 int
4139append_arg_number(wp, buf, add_file, maxlen)
4140 win_T *wp;
4141 char_u *buf;
4142 int add_file; /* Add "file" before the arg number */
4143 int maxlen; /* maximum nr of chars in buf or zero*/
4144{
4145 char_u *p;
4146
4147 if (ARGCOUNT <= 1) /* nothing to do */
4148 return FALSE;
4149
4150 p = buf + STRLEN(buf); /* go to the end of the buffer */
4151 if (maxlen && p - buf + 35 >= maxlen) /* getting too long */
4152 return FALSE;
4153 *p++ = ' ';
4154 *p++ = '(';
4155 if (add_file)
4156 {
4157 STRCPY(p, "file ");
4158 p += 5;
4159 }
4160 sprintf((char *)p, wp->w_arg_idx_invalid ? "(%d) of %d)"
4161 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4162 return TRUE;
4163}
4164
4165/*
4166 * If fname is not a full path, make it a full path.
4167 * Returns pointer to allocated memory (NULL for failure).
4168 */
4169 char_u *
4170fix_fname(fname)
4171 char_u *fname;
4172{
4173 /*
4174 * Force expanding the path always for Unix, because symbolic links may
4175 * mess up the full path name, even though it starts with a '/'.
4176 * Also expand when there is ".." in the file name, try to remove it,
4177 * because "c:/src/../README" is equal to "c:/README".
4178 * For MS-Windows also expand names like "longna~1" to "longname".
4179 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004180#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 return FullName_save(fname, TRUE);
4182#else
4183 if (!vim_isAbsName(fname) || strstr((char *)fname, "..") != NULL
4184#if defined(MSWIN) || defined(DJGPP)
4185 || vim_strchr(fname, '~') != NULL
4186#endif
4187 )
4188 return FullName_save(fname, FALSE);
4189
4190 fname = vim_strsave(fname);
4191
4192#ifdef USE_FNAME_CASE
4193# ifdef USE_LONG_FNAME
4194 if (USE_LONG_FNAME)
4195# endif
4196 {
4197 if (fname != NULL)
4198 fname_case(fname, 0); /* set correct case for file name */
4199 }
4200#endif
4201
4202 return fname;
4203#endif
4204}
4205
4206/*
4207 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4208 * "ffname" becomes a pointer to allocated memory (or NULL).
4209 */
4210/*ARGSUSED*/
4211 void
4212fname_expand(buf, ffname, sfname)
4213 buf_T *buf;
4214 char_u **ffname;
4215 char_u **sfname;
4216{
4217 if (*ffname == NULL) /* if no file name given, nothing to do */
4218 return;
4219 if (*sfname == NULL) /* if no short file name given, use ffname */
4220 *sfname = *ffname;
4221 *ffname = fix_fname(*ffname); /* expand to full path */
4222
4223#ifdef FEAT_SHORTCUT
4224 if (!buf->b_p_bin)
4225 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004226 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
4228 /* If the file name is a shortcut file, use the file it links to. */
4229 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004230 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 {
4232 vim_free(*ffname);
4233 *ffname = rfname;
4234 *sfname = rfname;
4235 }
4236 }
4237#endif
4238}
4239
4240/*
4241 * Get the file name for an argument list entry.
4242 */
4243 char_u *
4244alist_name(aep)
4245 aentry_T *aep;
4246{
4247 buf_T *bp;
4248
4249 /* Use the name from the associated buffer if it exists. */
4250 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004251 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 return aep->ae_fname;
4253 return bp->b_fname;
4254}
4255
4256#if defined(FEAT_WINDOWS) || defined(PROTO)
4257/*
4258 * do_arg_all(): Open up to 'count' windows, one for each argument.
4259 */
4260 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004261do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 int count;
4263 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004264 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265{
4266 int i;
4267 win_T *wp, *wpnext;
4268 char_u *opened; /* array of flags for which args are open */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004269 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 int use_firstwin = FALSE; /* use first window for arglist */
4271 int split_ret = OK;
4272 int p_ea_save;
4273 alist_T *alist; /* argument list to be used */
4274 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004275 tabpage_T *tpnext;
4276 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004277 win_T *new_curwin = NULL;
4278 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
4280 if (ARGCOUNT <= 0)
4281 {
4282 /* Don't give an error message. We don't want it when the ":all"
4283 * command is in the .vimrc. */
4284 return;
4285 }
4286 setpcmark();
4287
4288 opened_len = ARGCOUNT;
4289 opened = alloc_clear((unsigned)opened_len);
4290 if (opened == NULL)
4291 return;
4292
4293#ifdef FEAT_GUI
4294 need_mouse_correct = TRUE;
4295#endif
4296
4297 /*
4298 * Try closing all windows that are not in the argument list.
4299 * Also close windows that are not full width;
4300 * When 'hidden' or "forceit" set the buffer becomes hidden.
4301 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004302 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004304 if (had_tab > 0)
4305 goto_tabpage_tp(first_tabpage);
4306 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004308 tpnext = curtab->tp_next;
4309 for (wp = firstwin; wp != NULL; wp = wpnext)
4310 {
4311 wpnext = wp->w_next;
4312 buf = wp->w_buffer;
4313 if (buf->b_ffname == NULL
4314 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004316 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004318 )
4319 i = ARGCOUNT;
4320 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004322 /* check if the buffer in this window is in the arglist */
4323 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004325 if (ARGLIST[i].ae_fnum == buf->b_fnum
4326 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004327 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004329 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004330 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004331 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004332 if (i == 0)
4333 {
4334 new_curwin = wp;
4335 new_curtab = curtab;
4336 }
4337 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004338 if (wp->w_alist != curwin->w_alist)
4339 {
4340 /* Use the current argument list for all windows
4341 * containing a file from it. */
4342 alist_unlink(wp->w_alist);
4343 wp->w_alist = curwin->w_alist;
4344 ++wp->w_alist->al_refcount;
4345 }
4346 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004350 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004352 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004354 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004355 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004357 /* If the buffer was changed, and we would like to hide it,
4358 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004359 if (!P_HID(buf) && buf->b_nwindows <= 1
4360 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004362 (void)autowrite(buf, FALSE);
4363#ifdef FEAT_AUTOCMD
4364 /* check if autocommands removed the window */
4365 if (!win_valid(wp) || !buf_valid(buf))
4366 {
4367 wpnext = firstwin; /* start all over... */
4368 continue;
4369 }
4370#endif
4371 }
4372#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004373 /* don't close last window */
4374 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004375#endif
4376 use_firstwin = TRUE;
4377#ifdef FEAT_WINDOWS
4378 else
4379 {
4380 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4381# ifdef FEAT_AUTOCMD
4382 /* check if autocommands removed the next window */
4383 if (!win_valid(wpnext))
4384 wpnext = firstwin; /* start all over... */
4385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 }
4387#endif
4388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
4390 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004391
4392 /* Without the ":tab" modifier only do the current tab page. */
4393 if (had_tab == 0 || tpnext == NULL)
4394 break;
4395
4396# ifdef FEAT_AUTOCMD
4397 /* check if autocommands removed the next tab page */
4398 if (!valid_tabpage(tpnext))
4399 tpnext = first_tabpage; /* start all over...*/
4400# endif
4401 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 }
4403
4404 /*
4405 * Open a window for files in the argument list that don't have one.
4406 * ARGCOUNT may change while doing this, because of autocommands.
4407 */
4408 if (count > ARGCOUNT || count <= 0)
4409 count = ARGCOUNT;
4410
4411 /* Autocommands may do anything to the argument list. Make sure it's not
4412 * freed while we are working here by "locking" it. We still have to
4413 * watch out for its size to be changed. */
4414 alist = curwin->w_alist;
4415 ++alist->al_refcount;
4416
4417#ifdef FEAT_AUTOCMD
4418 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4419 ++autocmd_no_enter;
4420 ++autocmd_no_leave;
4421#endif
4422 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004423#ifdef FEAT_WINDOWS
4424 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4425 * leaving an empty tab page when executed locally. */
4426 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4427 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4428 use_firstwin = TRUE;
4429#endif
4430
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4432 {
4433 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4434 arg_had_last = TRUE;
4435 if (i < opened_len && opened[i])
4436 {
4437 /* Move the already present window to below the current window */
4438 if (curwin->w_arg_idx != i)
4439 {
4440 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4441 {
4442 if (wpnext->w_arg_idx == i)
4443 {
4444 win_move_after(wpnext, curwin);
4445 break;
4446 }
4447 }
4448 }
4449 }
4450 else if (split_ret == OK)
4451 {
4452 if (!use_firstwin) /* split current window */
4453 {
4454 p_ea_save = p_ea;
4455 p_ea = TRUE; /* use space from all windows */
4456 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4457 p_ea = p_ea_save;
4458 if (split_ret == FAIL)
4459 continue;
4460 }
4461#ifdef FEAT_AUTOCMD
4462 else /* first window: do autocmd for leaving this buffer */
4463 --autocmd_no_leave;
4464#endif
4465
4466 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004467 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 */
4469 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004470 if (i == 0)
4471 {
4472 new_curwin = curwin;
4473 new_curtab = curtab;
4474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4476 ECMD_ONE,
4477 ((P_HID(curwin->w_buffer)
4478 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4479 + ECMD_OLDBUF);
4480#ifdef FEAT_AUTOCMD
4481 if (use_firstwin)
4482 ++autocmd_no_leave;
4483#endif
4484 use_firstwin = FALSE;
4485 }
4486 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004487
4488 /* When ":tab" was used open a new tab for a new window repeatedly. */
4489 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4490 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
4492
4493 /* Remove the "lock" on the argument list. */
4494 alist_unlink(alist);
4495
4496#ifdef FEAT_AUTOCMD
4497 --autocmd_no_enter;
4498#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004499 /* to window with first arg */
4500 if (valid_tabpage(new_curtab))
4501 goto_tabpage_tp(new_curtab);
4502 if (win_valid(new_curwin))
4503 win_enter(new_curwin, FALSE);
4504
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505#ifdef FEAT_AUTOCMD
4506 --autocmd_no_leave;
4507#endif
4508 vim_free(opened);
4509}
4510
4511# if defined(FEAT_LISTCMDS) || defined(PROTO)
4512/*
4513 * Open a window for a number of buffers.
4514 */
4515 void
4516ex_buffer_all(eap)
4517 exarg_T *eap;
4518{
4519 buf_T *buf;
4520 win_T *wp, *wpnext;
4521 int split_ret = OK;
4522 int p_ea_save;
4523 int open_wins = 0;
4524 int r;
4525 int count; /* Maximum number of windows to open. */
4526 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004527#ifdef FEAT_WINDOWS
4528 int had_tab = cmdmod.tab;
4529 tabpage_T *tpnext;
4530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
4532 if (eap->addr_count == 0) /* make as many windows as possible */
4533 count = 9999;
4534 else
4535 count = eap->line2; /* make as many windows as specified */
4536 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4537 all = FALSE;
4538 else
4539 all = TRUE;
4540
4541 setpcmark();
4542
4543#ifdef FEAT_GUI
4544 need_mouse_correct = TRUE;
4545#endif
4546
4547 /*
4548 * Close superfluous windows (two windows for the same buffer).
4549 * Also close windows that are not full-width.
4550 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004551#ifdef FEAT_WINDOWS
4552 if (had_tab > 0)
4553 goto_tabpage_tp(first_tabpage);
4554 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004557 tpnext = curtab->tp_next;
4558 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004560 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004561 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004562#ifdef FEAT_VERTSPLIT
4563 || ((cmdmod.split & WSP_VERT)
4564 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004565 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004566 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004568#ifdef FEAT_WINDOWS
4569 || (had_tab > 0 && wp != firstwin)
4570#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004571 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004572 {
4573 win_close(wp, FALSE);
4574#ifdef FEAT_AUTOCMD
4575 wpnext = firstwin; /* just in case an autocommand does
4576 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004577 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004578 open_wins = 0;
4579#endif
4580 }
4581 else
4582 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004584
4585#ifdef FEAT_WINDOWS
4586 /* Without the ":tab" modifier only do the current tab page. */
4587 if (had_tab == 0 || tpnext == NULL)
4588 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004589 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593 /*
4594 * Go through the buffer list. When a buffer doesn't have a window yet,
4595 * open one. Otherwise move the window to the right position.
4596 * Watch out for autocommands that delete buffers or windows!
4597 */
4598#ifdef FEAT_AUTOCMD
4599 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4600 ++autocmd_no_enter;
4601#endif
4602 win_enter(lastwin, FALSE);
4603#ifdef FEAT_AUTOCMD
4604 ++autocmd_no_leave;
4605#endif
4606 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4607 {
4608 /* Check if this buffer needs a window */
4609 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4610 continue;
4611
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004612#ifdef FEAT_WINDOWS
4613 if (had_tab != 0)
4614 {
4615 /* With the ":tab" modifier don't move the window. */
4616 if (buf->b_nwindows > 0)
4617 wp = lastwin; /* buffer has a window, skip it */
4618 else
4619 wp = NULL;
4620 }
4621 else
4622#endif
4623 {
4624 /* Check if this buffer already has a window */
4625 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4626 if (wp->w_buffer == buf)
4627 break;
4628 /* If the buffer already has a window, move it */
4629 if (wp != NULL)
4630 win_move_after(wp, curwin);
4631 }
4632
4633 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
4635 /* Split the window and put the buffer in it */
4636 p_ea_save = p_ea;
4637 p_ea = TRUE; /* use space from all windows */
4638 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4639 ++open_wins;
4640 p_ea = p_ea_save;
4641 if (split_ret == FAIL)
4642 continue;
4643
4644 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004645#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 swap_exists_action = SEA_DIALOG;
4647#endif
4648 set_curbuf(buf, DOBUF_GOTO);
4649#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004652#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004654# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 break;
4656 }
4657#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004658#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (swap_exists_action == SEA_QUIT)
4660 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004661# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4662 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004663
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004664 /* Reset the error/interrupt/exception state here so that
4665 * aborting() returns FALSE when closing a window. */
4666 enter_cleanup(&cs);
4667# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004668
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004669 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 win_close(curwin, TRUE);
4671 --open_wins;
4672 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004673 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004674
4675# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4676 /* Restore the error/interrupt/exception state if not
4677 * discarded by a new aborting error, interrupt, or uncaught
4678 * exception. */
4679 leave_cleanup(&cs);
4680# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
4682 else
4683 handle_swap_exists(NULL);
4684#endif
4685 }
4686
4687 ui_breakcheck();
4688 if (got_int)
4689 {
4690 (void)vgetc(); /* only break the file loading, not the rest */
4691 break;
4692 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004693#ifdef FEAT_EVAL
4694 /* Autocommands deleted the buffer or aborted script processing!!! */
4695 if (aborting())
4696 break;
4697#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004698#ifdef FEAT_WINDOWS
4699 /* When ":tab" was used open a new tab for a new window repeatedly. */
4700 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4701 cmdmod.tab = 9999;
4702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704#ifdef FEAT_AUTOCMD
4705 --autocmd_no_enter;
4706#endif
4707 win_enter(firstwin, FALSE); /* back to first window */
4708#ifdef FEAT_AUTOCMD
4709 --autocmd_no_leave;
4710#endif
4711
4712 /*
4713 * Close superfluous windows.
4714 */
4715 for (wp = lastwin; open_wins > count; )
4716 {
4717 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4718 || autowrite(wp->w_buffer, FALSE) == OK);
4719#ifdef FEAT_AUTOCMD
4720 if (!win_valid(wp))
4721 {
4722 /* BufWrite Autocommands made the window invalid, start over */
4723 wp = lastwin;
4724 }
4725 else
4726#endif
4727 if (r)
4728 {
4729 win_close(wp, !P_HID(wp->w_buffer));
4730 --open_wins;
4731 wp = lastwin;
4732 }
4733 else
4734 {
4735 wp = wp->w_prev;
4736 if (wp == NULL)
4737 break;
4738 }
4739 }
4740}
4741# endif /* FEAT_LISTCMDS */
4742
4743#endif /* FEAT_WINDOWS */
4744
Bram Moolenaara3227e22006-03-08 21:32:40 +00004745static int chk_modeline __ARGS((linenr_T, int));
4746
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747/*
4748 * do_modelines() - process mode lines for the current file
4749 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004750 * "flags" can be:
4751 * OPT_WINONLY only set options local to window
4752 * OPT_NOWIN don't set options local to window
4753 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 * Returns immediately if the "ml" option isn't set.
4755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004757do_modelines(flags)
4758 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004760 linenr_T lnum;
4761 int nmlines;
4762 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
4764 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4765 return;
4766
4767 /* Disallow recursive entry here. Can happen when executing a modeline
4768 * triggers an autocommand, which reloads modelines with a ":do". */
4769 if (entered)
4770 return;
4771
4772 ++entered;
4773 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4774 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004775 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 nmlines = 0;
4777
4778 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4779 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004780 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 nmlines = 0;
4782 --entered;
4783}
4784
4785#include "version.h" /* for version number */
4786
4787/*
4788 * chk_modeline() - check a single line for a mode string
4789 * Return FAIL if an error encountered.
4790 */
4791 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004792chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004794 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795{
4796 char_u *s;
4797 char_u *e;
4798 char_u *linecopy; /* local copy of any modeline found */
4799 int prev;
4800 int vers;
4801 int end;
4802 int retval = OK;
4803 char_u *save_sourcing_name;
4804 linenr_T save_sourcing_lnum;
4805#ifdef FEAT_EVAL
4806 scid_T save_SID;
4807#endif
4808
4809 prev = -1;
4810 for (s = ml_get(lnum); *s != NUL; ++s)
4811 {
4812 if (prev == -1 || vim_isspace(prev))
4813 {
4814 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4815 || STRNCMP(s, "vi:", (size_t)3) == 0)
4816 break;
4817 if (STRNCMP(s, "vim", 3) == 0)
4818 {
4819 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4820 e = s + 4;
4821 else
4822 e = s + 3;
4823 vers = getdigits(&e);
4824 if (*e == ':'
4825 && (s[3] == ':'
4826 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4827 || (VIM_VERSION_100 < vers && s[3] == '<')
4828 || (VIM_VERSION_100 > vers && s[3] == '>')
4829 || (VIM_VERSION_100 == vers && s[3] == '=')))
4830 break;
4831 }
4832 }
4833 prev = *s;
4834 }
4835
4836 if (*s)
4837 {
4838 do /* skip over "ex:", "vi:" or "vim:" */
4839 ++s;
4840 while (s[-1] != ':');
4841
4842 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4843 if (linecopy == NULL)
4844 return FAIL;
4845
4846 save_sourcing_lnum = sourcing_lnum;
4847 save_sourcing_name = sourcing_name;
4848 sourcing_lnum = lnum; /* prepare for emsg() */
4849 sourcing_name = (char_u *)"modelines";
4850
4851 end = FALSE;
4852 while (end == FALSE)
4853 {
4854 s = skipwhite(s);
4855 if (*s == NUL)
4856 break;
4857
4858 /*
4859 * Find end of set command: ':' or end of line.
4860 * Skip over "\:", replacing it with ":".
4861 */
4862 for (e = s; *e != ':' && *e != NUL; ++e)
4863 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaar452a81b2007-08-06 20:28:43 +00004864 mch_memmove(e, e + 1, STRLEN(e));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 if (*e == NUL)
4866 end = TRUE;
4867
4868 /*
4869 * If there is a "set" command, require a terminating ':' and
4870 * ignore the stuff after the ':'.
4871 * "vi:set opt opt opt: foo" -- foo not interpreted
4872 * "vi:opt opt opt: foo" -- foo interpreted
4873 * Accept "se" for compatibility with Elvis.
4874 */
4875 if (STRNCMP(s, "set ", (size_t)4) == 0
4876 || STRNCMP(s, "se ", (size_t)3) == 0)
4877 {
4878 if (*e != ':') /* no terminating ':'? */
4879 break;
4880 end = TRUE;
4881 s = vim_strchr(s, ' ') + 1;
4882 }
4883 *e = NUL; /* truncate the set command */
4884
4885 if (*s != NUL) /* skip over an empty "::" */
4886 {
4887#ifdef FEAT_EVAL
4888 save_SID = current_SID;
4889 current_SID = SID_MODELINE;
4890#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004891 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892#ifdef FEAT_EVAL
4893 current_SID = save_SID;
4894#endif
4895 if (retval == FAIL) /* stop if error found */
4896 break;
4897 }
4898 s = e + 1; /* advance to next part */
4899 }
4900
4901 sourcing_lnum = save_sourcing_lnum;
4902 sourcing_name = save_sourcing_name;
4903
4904 vim_free(linecopy);
4905 }
4906 return retval;
4907}
4908
4909#ifdef FEAT_VIMINFO
4910 int
4911read_viminfo_bufferlist(virp, writing)
4912 vir_T *virp;
4913 int writing;
4914{
4915 char_u *tab;
4916 linenr_T lnum;
4917 colnr_T col;
4918 buf_T *buf;
4919 char_u *sfname;
4920 char_u *xline;
4921
4922 /* Handle long line and escaped characters. */
4923 xline = viminfo_readstring(virp, 1, FALSE);
4924
4925 /* don't read in if there are files on the command-line or if writing: */
4926 if (xline != NULL && !writing && ARGCOUNT == 0
4927 && find_viminfo_parameter('%') != NULL)
4928 {
4929 /* Format is: <fname> Tab <lnum> Tab <col>.
4930 * Watch out for a Tab in the file name, work from the end. */
4931 lnum = 0;
4932 col = 0;
4933 tab = vim_strrchr(xline, '\t');
4934 if (tab != NULL)
4935 {
4936 *tab++ = '\0';
4937 col = atoi((char *)tab);
4938 tab = vim_strrchr(xline, '\t');
4939 if (tab != NULL)
4940 {
4941 *tab++ = '\0';
4942 lnum = atol((char *)tab);
4943 }
4944 }
4945
4946 /* Expand "~/" in the file name at "line + 1" to a full path.
4947 * Then try shortening it by comparing with the current directory */
4948 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004949 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
4951 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
4952 if (buf != NULL) /* just in case... */
4953 {
4954 buf->b_last_cursor.lnum = lnum;
4955 buf->b_last_cursor.col = col;
4956 buflist_setfpos(buf, curwin, lnum, col, FALSE);
4957 }
4958 }
4959 vim_free(xline);
4960
4961 return viminfo_readline(virp);
4962}
4963
4964 void
4965write_viminfo_bufferlist(fp)
4966 FILE *fp;
4967{
4968 buf_T *buf;
4969#ifdef FEAT_WINDOWS
4970 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004971 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972#endif
4973 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004974 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975
4976 if (find_viminfo_parameter('%') == NULL)
4977 return;
4978
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004979 /* Without a number -1 is returned: do all buffers. */
4980 max_buffers = get_viminfo_parameter('%');
4981
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004983 line = alloc(MAXPATHL + 40);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 if (line == NULL)
4985 return;
4986
4987#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00004988 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 set_last_cursor(win);
4990#else
4991 set_last_cursor(curwin);
4992#endif
4993
4994 fprintf(fp, _("\n# Buffer list:\n"));
4995 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
4996 {
4997 if (buf->b_fname == NULL
4998 || !buf->b_p_bl
4999#ifdef FEAT_QUICKFIX
5000 || bt_quickfix(buf)
5001#endif
5002 || removable(buf->b_ffname))
5003 continue;
5004
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005005 if (max_buffers-- == 0)
5006 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 putc('%', fp);
5008 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
5009 sprintf((char *)line + STRLEN(line), "\t%ld\t%d",
5010 (long)buf->b_last_cursor.lnum,
5011 buf->b_last_cursor.col);
5012 viminfo_writestring(fp, line);
5013 }
5014 vim_free(line);
5015}
5016#endif
5017
5018
5019/*
5020 * Return special buffer name.
5021 * Returns NULL when the buffer has a normal file name.
5022 */
5023 char *
5024buf_spname(buf)
5025 buf_T *buf;
5026{
5027#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5028 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005029 {
5030 win_T *win;
5031
5032 /*
5033 * For location list window, w_llist_ref points to the location list.
5034 * For quickfix window, w_llist_ref is NULL.
5035 */
5036 FOR_ALL_WINDOWS(win)
5037 if (win->w_buffer == buf)
5038 break;
5039 if (win != NULL && win->w_llist_ref != NULL)
5040 return _("[Location List]");
5041 else
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005042 return _("[Quickfix List]");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044#endif
5045#ifdef FEAT_QUICKFIX
5046 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5047 * contains the name as specified by the user */
5048 if (bt_nofile(buf))
5049 {
5050 if (buf->b_sfname != NULL)
5051 return (char *)buf->b_sfname;
5052 return "[Scratch]";
5053 }
5054#endif
5055 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005056 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 return NULL;
5058}
5059
5060
5061#if defined(FEAT_SIGNS) || defined(PROTO)
5062/*
5063 * Insert the sign into the signlist.
5064 */
5065 static void
5066insert_sign(buf, prev, next, id, lnum, typenr)
5067 buf_T *buf; /* buffer to store sign in */
5068 signlist_T *prev; /* previous sign entry */
5069 signlist_T *next; /* next sign entry */
5070 int id; /* sign ID */
5071 linenr_T lnum; /* line number which gets the mark */
5072 int typenr; /* typenr of sign we are adding */
5073{
5074 signlist_T *newsign;
5075
5076 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5077 if (newsign != NULL)
5078 {
5079 newsign->id = id;
5080 newsign->lnum = lnum;
5081 newsign->typenr = typenr;
5082 newsign->next = next;
5083#ifdef FEAT_NETBEANS_INTG
5084 newsign->prev = prev;
5085 if (next != NULL)
5086 next->prev = newsign;
5087#endif
5088
5089 if (prev == NULL)
5090 {
5091 /* When adding first sign need to redraw the windows to create the
5092 * column for signs. */
5093 if (buf->b_signlist == NULL)
5094 {
5095 redraw_buf_later(buf, NOT_VALID);
5096 changed_cline_bef_curs();
5097 }
5098
5099 /* first sign in signlist */
5100 buf->b_signlist = newsign;
5101 }
5102 else
5103 prev->next = newsign;
5104 }
5105}
5106
5107/*
5108 * Add the sign into the signlist. Find the right spot to do it though.
5109 */
5110 void
5111buf_addsign(buf, id, lnum, typenr)
5112 buf_T *buf; /* buffer to store sign in */
5113 int id; /* sign ID */
5114 linenr_T lnum; /* line number which gets the mark */
5115 int typenr; /* typenr of sign we are adding */
5116{
5117 signlist_T *sign; /* a sign in the signlist */
5118 signlist_T *prev; /* the previous sign */
5119
5120 prev = NULL;
5121 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5122 {
5123 if (lnum == sign->lnum && id == sign->id)
5124 {
5125 sign->typenr = typenr;
5126 return;
5127 }
5128 else if (
5129#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5130 id < 0 &&
5131#endif
5132 lnum < sign->lnum)
5133 {
5134#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5135 /* XXX - GRP: Is this because of sign slide problem? Or is it
5136 * really needed? Or is it because we allow multiple signs per
5137 * line? If so, should I add that feature to FEAT_SIGNS?
5138 */
5139 while (prev != NULL && prev->lnum == lnum)
5140 prev = prev->prev;
5141 if (prev == NULL)
5142 sign = buf->b_signlist;
5143 else
5144 sign = prev->next;
5145#endif
5146 insert_sign(buf, prev, sign, id, lnum, typenr);
5147 return;
5148 }
5149 prev = sign;
5150 }
5151#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5152 /* XXX - GRP: See previous comment */
5153 while (prev != NULL && prev->lnum == lnum)
5154 prev = prev->prev;
5155 if (prev == NULL)
5156 sign = buf->b_signlist;
5157 else
5158 sign = prev->next;
5159#endif
5160 insert_sign(buf, prev, sign, id, lnum, typenr);
5161
5162 return;
5163}
5164
5165 int
5166buf_change_sign_type(buf, markId, typenr)
5167 buf_T *buf; /* buffer to store sign in */
5168 int markId; /* sign ID */
5169 int typenr; /* typenr of sign we are adding */
5170{
5171 signlist_T *sign; /* a sign in the signlist */
5172
5173 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5174 {
5175 if (sign->id == markId)
5176 {
5177 sign->typenr = typenr;
5178 return sign->lnum;
5179 }
5180 }
5181
5182 return 0;
5183}
5184
5185 int_u
5186buf_getsigntype(buf, lnum, type)
5187 buf_T *buf;
5188 linenr_T lnum;
5189 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5190{
5191 signlist_T *sign; /* a sign in a b_signlist */
5192
5193 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5194 if (sign->lnum == lnum
5195 && (type == SIGN_ANY
5196# ifdef FEAT_SIGN_ICONS
5197 || (type == SIGN_ICON
5198 && sign_get_image(sign->typenr) != NULL)
5199# endif
5200 || (type == SIGN_TEXT
5201 && sign_get_text(sign->typenr) != NULL)
5202 || (type == SIGN_LINEHL
5203 && sign_get_attr(sign->typenr, TRUE) != 0)))
5204 return sign->typenr;
5205 return 0;
5206}
5207
5208
5209 linenr_T
5210buf_delsign(buf, id)
5211 buf_T *buf; /* buffer sign is stored in */
5212 int id; /* sign id */
5213{
5214 signlist_T **lastp; /* pointer to pointer to current sign */
5215 signlist_T *sign; /* a sign in a b_signlist */
5216 signlist_T *next; /* the next sign in a b_signlist */
5217 linenr_T lnum; /* line number whose sign was deleted */
5218
5219 lastp = &buf->b_signlist;
5220 lnum = 0;
5221 for (sign = buf->b_signlist; sign != NULL; sign = next)
5222 {
5223 next = sign->next;
5224 if (sign->id == id)
5225 {
5226 *lastp = next;
5227#ifdef FEAT_NETBEANS_INTG
5228 if (next != NULL)
5229 next->prev = sign->prev;
5230#endif
5231 lnum = sign->lnum;
5232 vim_free(sign);
5233 break;
5234 }
5235 else
5236 lastp = &sign->next;
5237 }
5238
5239 /* When deleted the last sign need to redraw the windows to remove the
5240 * sign column. */
5241 if (buf->b_signlist == NULL)
5242 {
5243 redraw_buf_later(buf, NOT_VALID);
5244 changed_cline_bef_curs();
5245 }
5246
5247 return lnum;
5248}
5249
5250
5251/*
5252 * Find the line number of the sign with the requested id. If the sign does
5253 * not exist, return 0 as the line number. This will still let the correct file
5254 * get loaded.
5255 */
5256 int
5257buf_findsign(buf, id)
5258 buf_T *buf; /* buffer to store sign in */
5259 int id; /* sign ID */
5260{
5261 signlist_T *sign; /* a sign in the signlist */
5262
5263 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5264 if (sign->id == id)
5265 return sign->lnum;
5266
5267 return 0;
5268}
5269
5270 int
5271buf_findsign_id(buf, lnum)
5272 buf_T *buf; /* buffer whose sign we are searching for */
5273 linenr_T lnum; /* line number of sign */
5274{
5275 signlist_T *sign; /* a sign in the signlist */
5276
5277 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5278 if (sign->lnum == lnum)
5279 return sign->id;
5280
5281 return 0;
5282}
5283
5284
5285# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5286/* see if a given type of sign exists on a specific line */
5287 int
5288buf_findsigntype_id(buf, lnum, typenr)
5289 buf_T *buf; /* buffer whose sign we are searching for */
5290 linenr_T lnum; /* line number of sign */
5291 int typenr; /* sign type number */
5292{
5293 signlist_T *sign; /* a sign in the signlist */
5294
5295 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5296 if (sign->lnum == lnum && sign->typenr == typenr)
5297 return sign->id;
5298
5299 return 0;
5300}
5301
5302
5303# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5304/* return the number of icons on the given line */
5305 int
5306buf_signcount(buf, lnum)
5307 buf_T *buf;
5308 linenr_T lnum;
5309{
5310 signlist_T *sign; /* a sign in the signlist */
5311 int count = 0;
5312
5313 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5314 if (sign->lnum == lnum)
5315 if (sign_get_image(sign->typenr) != NULL)
5316 count++;
5317
5318 return count;
5319}
5320# endif /* FEAT_SIGN_ICONS */
5321# endif /* FEAT_NETBEANS_INTG */
5322
5323
5324/*
5325 * Delete signs in buffer "buf".
5326 */
5327 static void
5328buf_delete_signs(buf)
5329 buf_T *buf;
5330{
5331 signlist_T *next;
5332
5333 while (buf->b_signlist != NULL)
5334 {
5335 next = buf->b_signlist->next;
5336 vim_free(buf->b_signlist);
5337 buf->b_signlist = next;
5338 }
5339}
5340
5341/*
5342 * Delete all signs in all buffers.
5343 */
5344 void
5345buf_delete_all_signs()
5346{
5347 buf_T *buf; /* buffer we are checking for signs */
5348
5349 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5350 if (buf->b_signlist != NULL)
5351 {
5352 /* Need to redraw the windows to remove the sign column. */
5353 redraw_buf_later(buf, NOT_VALID);
5354 buf_delete_signs(buf);
5355 }
5356}
5357
5358/*
5359 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5360 */
5361 void
5362sign_list_placed(rbuf)
5363 buf_T *rbuf;
5364{
5365 buf_T *buf;
5366 signlist_T *p;
5367 char lbuf[BUFSIZ];
5368
5369 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5370 msg_putchar('\n');
5371 if (rbuf == NULL)
5372 buf = firstbuf;
5373 else
5374 buf = rbuf;
5375 while (buf != NULL)
5376 {
5377 if (buf->b_signlist != NULL)
5378 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005379 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5381 msg_putchar('\n');
5382 }
5383 for (p = buf->b_signlist; p != NULL; p = p->next)
5384 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005385 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5387 MSG_PUTS(lbuf);
5388 msg_putchar('\n');
5389 }
5390 if (rbuf != NULL)
5391 break;
5392 buf = buf->b_next;
5393 }
5394}
5395
5396/*
5397 * Adjust a placed sign for inserted/deleted lines.
5398 */
5399 void
5400sign_mark_adjust(line1, line2, amount, amount_after)
5401 linenr_T line1;
5402 linenr_T line2;
5403 long amount;
5404 long amount_after;
5405{
5406 signlist_T *sign; /* a sign in a b_signlist */
5407
5408 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5409 {
5410 if (sign->lnum >= line1 && sign->lnum <= line2)
5411 {
5412 if (amount == MAXLNUM)
5413 sign->lnum = line1;
5414 else
5415 sign->lnum += amount;
5416 }
5417 else if (sign->lnum > line2)
5418 sign->lnum += amount_after;
5419 }
5420}
5421#endif /* FEAT_SIGNS */
5422
5423/*
5424 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5425 */
5426 void
5427set_buflisted(on)
5428 int on;
5429{
5430 if (on != curbuf->b_p_bl)
5431 {
5432 curbuf->b_p_bl = on;
5433#ifdef FEAT_AUTOCMD
5434 if (on)
5435 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5436 else
5437 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5438#endif
5439 }
5440}
5441
5442/*
5443 * Read the file for "buf" again and check if the contents changed.
5444 * Return TRUE if it changed or this could not be checked.
5445 */
5446 int
5447buf_contents_changed(buf)
5448 buf_T *buf;
5449{
5450 buf_T *newbuf;
5451 int differ = TRUE;
5452 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 exarg_T ea;
5455
5456 /* Allocate a buffer without putting it in the buffer list. */
5457 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5458 if (newbuf == NULL)
5459 return TRUE;
5460
5461 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5462 if (prep_exarg(&ea, buf) == FAIL)
5463 {
5464 wipe_buffer(newbuf, FALSE);
5465 return TRUE;
5466 }
5467
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 /* set curwin/curbuf to buf and save a few things */
5469 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470
Bram Moolenaar4770d092006-01-12 23:22:24 +00005471 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 && readfile(buf->b_ffname, buf->b_fname,
5473 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5474 &ea, READ_NEW | READ_DUMMY) == OK)
5475 {
5476 /* compare the two files line by line */
5477 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5478 {
5479 differ = FALSE;
5480 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5481 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5482 {
5483 differ = TRUE;
5484 break;
5485 }
5486 }
5487 }
5488 vim_free(ea.cmd);
5489
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 /* restore curwin/curbuf and a few other things */
5491 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
5493 if (curbuf != newbuf) /* safety check */
5494 wipe_buffer(newbuf, FALSE);
5495
5496 return differ;
5497}
5498
5499/*
5500 * Wipe out a buffer and decrement the last buffer number if it was used for
5501 * this buffer. Call this to wipe out a temp buffer that does not contain any
5502 * marks.
5503 */
5504/*ARGSUSED*/
5505 void
5506wipe_buffer(buf, aucmd)
5507 buf_T *buf;
5508 int aucmd; /* When TRUE trigger autocommands. */
5509{
5510 if (buf->b_fnum == top_file_num - 1)
5511 --top_file_num;
5512
5513#ifdef FEAT_AUTOCMD
5514 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005515 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516#endif
5517 close_buffer(NULL, buf, DOBUF_WIPE);
5518#ifdef FEAT_AUTOCMD
5519 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005520 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521#endif
5522}