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