blob: 4da9d638926c9a0e3c44fd5ede0a2790c425f676 [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));
Bram Moolenaar701f7af2008-11-15 13:12:07 +000036static wininfo_T *find_wininfo __ARGS((buf_T *buf, int skip_diff_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#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
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000047static int append_arg_number __ARGS((win_T *wp, char_u *buf, int buflen, int add_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static void free_buffer __ARGS((buf_T *));
49static void free_buffer_stuff __ARGS((buf_T *buf, int free_options));
50static void clear_wininfo __ARGS((buf_T *buf));
51
52#ifdef UNIX
53# define dev_T dev_t
54#else
55# define dev_T unsigned
56#endif
57
58#if defined(FEAT_SIGNS)
59static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr));
60static void buf_delete_signs __ARGS((buf_T *buf));
61#endif
62
63/*
64 * Open current buffer, that is: open the memfile and read the file into memory
65 * return FAIL for failure, OK otherwise
66 */
67 int
68open_buffer(read_stdin, eap)
69 int read_stdin; /* read file from stdin */
70 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
71{
72 int retval = OK;
73#ifdef FEAT_AUTOCMD
74 buf_T *old_curbuf;
75#endif
76
77 /*
78 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
79 * When re-entering the same buffer, it should not change, because the
80 * user may have reset the flag by hand.
81 */
82 if (readonlymode && curbuf->b_ffname != NULL
83 && (curbuf->b_flags & BF_NEVERLOADED))
84 curbuf->b_p_ro = TRUE;
85
Bram Moolenaar4770d092006-01-12 23:22:24 +000086 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 {
88 /*
89 * There MUST be a memfile, otherwise we can't do anything
90 * If we can't create one for the current buffer, take another buffer
91 */
92 close_buffer(NULL, curbuf, 0);
93 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
94 if (curbuf->b_ml.ml_mfp != NULL)
95 break;
96 /*
97 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +000098 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +000099 */
100 if (curbuf == NULL)
101 {
102 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
103 getout(2);
104 }
105 EMSG(_("E83: Cannot allocate buffer, using other one..."));
106 enter_buffer(curbuf);
107 return FAIL;
108 }
109
110#ifdef FEAT_AUTOCMD
111 /* The autocommands in readfile() may change the buffer, but only AFTER
112 * reading the file. */
113 old_curbuf = curbuf;
114 modified_was_set = FALSE;
115#endif
116
117 /* mark cursor position as being invalid */
118 changed_line_abv_curs();
119
120 if (curbuf->b_ffname != NULL
121#ifdef FEAT_NETBEANS_INTG
122 && netbeansReadFile
123#endif
124 )
125 {
126#ifdef FEAT_NETBEANS_INTG
127 int oldFire = netbeansFireChanges;
128
129 netbeansFireChanges = 0;
130#endif
131 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
132 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_NEW);
133#ifdef FEAT_NETBEANS_INTG
134 netbeansFireChanges = oldFire;
135#endif
136 /* Help buffer is filtered. */
137 if (curbuf->b_help)
138 fix_help_buffer();
139 }
140 else if (read_stdin)
141 {
142 int save_bin = curbuf->b_p_bin;
143 linenr_T line_count;
144
145 /*
146 * First read the text in binary mode into the buffer.
147 * Then read from that same buffer and append at the end. This makes
148 * it possible to retry when 'fileformat' or 'fileencoding' was
149 * guessed wrong.
150 */
151 curbuf->b_p_bin = TRUE;
152 retval = readfile(NULL, NULL, (linenr_T)0,
153 (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW + READ_STDIN);
154 curbuf->b_p_bin = save_bin;
155 if (retval == OK)
156 {
157 line_count = curbuf->b_ml.ml_line_count;
158 retval = readfile(NULL, NULL, (linenr_T)line_count,
159 (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_BUFFER);
160 if (retval == OK)
161 {
162 /* Delete the binary lines. */
163 while (--line_count >= 0)
164 ml_delete((linenr_T)1, FALSE);
165 }
166 else
167 {
168 /* Delete the converted lines. */
169 while (curbuf->b_ml.ml_line_count > line_count)
170 ml_delete(line_count, FALSE);
171 }
172 /* Put the cursor on the first line. */
173 curwin->w_cursor.lnum = 1;
174 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000175
176 /* Set or reset 'modified' before executing autocommands, so that
177 * it can be changed there. */
178 if (!readonlymode && !bufempty())
179 changed();
180 else if (retval != FAIL)
181 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#ifdef FEAT_AUTOCMD
183# ifdef FEAT_EVAL
184 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
185 curbuf, &retval);
186# else
187 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
188# endif
189#endif
190 }
191 }
192
193 /* if first time loading this buffer, init b_chartab[] */
194 if (curbuf->b_flags & BF_NEVERLOADED)
195 (void)buf_init_chartab(curbuf, FALSE);
196
197 /*
198 * Set/reset the Changed flag first, autocmds may change the buffer.
199 * Apply the automatic commands, before processing the modelines.
200 * So the modelines have priority over auto commands.
201 */
202 /* When reading stdin, the buffer contents always needs writing, so set
203 * the changed flag. Unless in readonly mode: "ls | gview -".
204 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000205 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#ifdef FEAT_AUTOCMD
207 || modified_was_set /* ":set modified" used in autocmd */
208# ifdef FEAT_EVAL
209 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
210# endif
211#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000212 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000214 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 unchanged(curbuf, FALSE);
216 save_file_ff(curbuf); /* keep this fileformat */
217
218 /* require "!" to overwrite the file, because it wasn't read completely */
219#ifdef FEAT_EVAL
220 if (aborting())
221#else
222 if (got_int)
223#endif
224 curbuf->b_flags |= BF_READERR;
225
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000226#ifdef FEAT_FOLDING
227 /* Need to update automatic folding. Do this before the autocommands,
228 * they may use the fold info. */
229 foldUpdateAll(curwin);
230#endif
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#ifdef FEAT_AUTOCMD
233 /* need to set w_topline, unless some autocommand already did that. */
234 if (!(curwin->w_valid & VALID_TOPLINE))
235 {
236 curwin->w_topline = 1;
237# ifdef FEAT_DIFF
238 curwin->w_topfill = 0;
239# endif
240 }
241# ifdef FEAT_EVAL
242 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
243# else
244 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
245# endif
246#endif
247
248 if (retval != FAIL)
249 {
250#ifdef FEAT_AUTOCMD
251 /*
252 * The autocommands may have changed the current buffer. Apply the
253 * modelines to the correct buffer, if it still exists and is loaded.
254 */
255 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
256 {
257 aco_save_T aco;
258
259 /* Go to the buffer that was opened. */
260 aucmd_prepbuf(&aco, old_curbuf);
261#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000262 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
264
265#ifdef FEAT_AUTOCMD
266# ifdef FEAT_EVAL
267 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
268 &retval);
269# else
270 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
271# endif
272
273 /* restore curwin/curbuf and a few other things */
274 aucmd_restbuf(&aco);
275 }
276#endif
277 }
278
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 return retval;
280}
281
282/*
283 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
284 */
285 int
286buf_valid(buf)
287 buf_T *buf;
288{
289 buf_T *bp;
290
291 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
292 if (bp == buf)
293 return TRUE;
294 return FALSE;
295}
296
297/*
298 * Close the link to a buffer.
299 * "action" is used when there is no longer a window for the buffer.
300 * It can be:
301 * 0 buffer becomes hidden
302 * DOBUF_UNLOAD buffer is unloaded
303 * DOBUF_DELETE buffer is unloaded and removed from buffer list
304 * DOBUF_WIPE buffer is unloaded and really deleted
305 * When doing all but the first one on the current buffer, the caller should
306 * get a new buffer very soon!
307 *
308 * The 'bufhidden' option can force freeing and deleting.
309 */
310 void
311close_buffer(win, buf, action)
312 win_T *win; /* if not NULL, set b_last_cursor */
313 buf_T *buf;
314 int action;
315{
316#ifdef FEAT_AUTOCMD
317 int is_curbuf;
318 int nwindows = buf->b_nwindows;
319#endif
320 int unload_buf = (action != 0);
321 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
322 int wipe_buf = (action == DOBUF_WIPE);
323
324#ifdef FEAT_QUICKFIX
325 /*
326 * Force unloading or deleting when 'bufhidden' says so.
327 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
328 * "hide" (otherwise we could never free or delete a buffer).
329 */
330 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
331 {
332 del_buf = TRUE;
333 unload_buf = TRUE;
334 }
335 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
336 {
337 del_buf = TRUE;
338 unload_buf = TRUE;
339 wipe_buf = TRUE;
340 }
341 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
342 unload_buf = TRUE;
343#endif
344
345 if (win != NULL)
346 {
347 /* Set b_last_cursor when closing the last window for the buffer.
348 * Remember the last cursor position and window options of the buffer.
349 * This used to be only for the current window, but then options like
350 * 'foldmethod' may be lost with a ":only" command. */
351 if (buf->b_nwindows == 1)
352 set_last_cursor(win);
353 buflist_setfpos(buf, win,
354 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
355 win->w_cursor.col, TRUE);
356 }
357
358#ifdef FEAT_AUTOCMD
359 /* When the buffer is no longer in a window, trigger BufWinLeave */
360 if (buf->b_nwindows == 1)
361 {
362 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
363 FALSE, buf);
364 if (!buf_valid(buf)) /* autocommands may delete the buffer */
365 return;
366
367 /* When the buffer becomes hidden, but is not unloaded, trigger
368 * BufHidden */
369 if (!unload_buf)
370 {
371 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
372 FALSE, buf);
373 if (!buf_valid(buf)) /* autocmds may delete the buffer */
374 return;
375 }
376# ifdef FEAT_EVAL
377 if (aborting()) /* autocmds may abort script processing */
378 return;
379# endif
380 }
381 nwindows = buf->b_nwindows;
382#endif
383
384 /* decrease the link count from windows (unless not in any window) */
385 if (buf->b_nwindows > 0)
386 --buf->b_nwindows;
387
388 /* Return when a window is displaying the buffer or when it's not
389 * unloaded. */
390 if (buf->b_nwindows > 0 || !unload_buf)
391 {
Bram Moolenaar607a95ed2006-03-28 20:57:42 +0000392#if 0 /* why was this here? */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 if (buf == curbuf)
394 u_sync(); /* sync undo before going to another buffer */
Bram Moolenaar607a95ed2006-03-28 20:57:42 +0000395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 return;
397 }
398
399 /* Always remove the buffer when there is no file name. */
400 if (buf->b_ffname == NULL)
401 del_buf = TRUE;
402
403 /*
404 * Free all things allocated for this buffer.
405 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
406 */
407#ifdef FEAT_AUTOCMD
408 /* Remember if we are closing the current buffer. Restore the number of
409 * windows, so that autocommands in buf_freeall() don't get confused. */
410 is_curbuf = (buf == curbuf);
411 buf->b_nwindows = nwindows;
412#endif
413
414 buf_freeall(buf, del_buf, wipe_buf);
415
416#ifdef FEAT_AUTOCMD
417 /* Autocommands may have deleted the buffer. */
418 if (!buf_valid(buf))
419 return;
420# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000421 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 return;
423# endif
424
425 /* Autocommands may have opened or closed windows for this buffer.
426 * Decrement the count for the close we do here. */
427 if (buf->b_nwindows > 0)
428 --buf->b_nwindows;
429
430 /*
431 * It's possible that autocommands change curbuf to the one being deleted.
432 * This might cause the previous curbuf to be deleted unexpectedly. But
433 * in some cases it's OK to delete the curbuf, because a new one is
434 * obtained anyway. Therefore only return if curbuf changed to the
435 * deleted buffer.
436 */
437 if (buf == curbuf && !is_curbuf)
438 return;
439#endif
440
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000441 /* Change directories when the 'acd' option is set. */
442 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443
444 /*
445 * Remove the buffer from the list.
446 */
447 if (wipe_buf)
448 {
449#ifdef FEAT_SUN_WORKSHOP
450 if (usingSunWorkShop)
451 workshop_file_closed_lineno((char *)buf->b_ffname,
452 (int)buf->b_last_cursor.lnum);
453#endif
454 vim_free(buf->b_ffname);
455 vim_free(buf->b_sfname);
456 if (buf->b_prev == NULL)
457 firstbuf = buf->b_next;
458 else
459 buf->b_prev->b_next = buf->b_next;
460 if (buf->b_next == NULL)
461 lastbuf = buf->b_prev;
462 else
463 buf->b_next->b_prev = buf->b_prev;
464 free_buffer(buf);
465 }
466 else
467 {
468 if (del_buf)
469 {
470 /* Free all internal variables and reset option values, to make
471 * ":bdel" compatible with Vim 5.7. */
472 free_buffer_stuff(buf, TRUE);
473
474 /* Make it look like a new buffer. */
475 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
476
477 /* Init the options when loaded again. */
478 buf->b_p_initialized = FALSE;
479 }
480 buf_clear_file(buf);
481 if (del_buf)
482 buf->b_p_bl = FALSE;
483 }
484}
485
486/*
487 * Make buffer not contain a file.
488 */
489 void
490buf_clear_file(buf)
491 buf_T *buf;
492{
493 buf->b_ml.ml_line_count = 1;
494 unchanged(buf, TRUE);
495#ifndef SHORT_FNAME
496 buf->b_shortname = FALSE;
497#endif
498 buf->b_p_eol = TRUE;
499 buf->b_start_eol = TRUE;
500#ifdef FEAT_MBYTE
501 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000502 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503#endif
504 buf->b_ml.ml_mfp = NULL;
505 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
506#ifdef FEAT_NETBEANS_INTG
507 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508#endif
509}
510
511/*
512 * buf_freeall() - free all things allocated for a buffer that are related to
513 * the file.
514 */
515/*ARGSUSED*/
516 void
517buf_freeall(buf, del_buf, wipe_buf)
518 buf_T *buf;
519 int del_buf; /* buffer is going to be deleted */
520 int wipe_buf; /* buffer is going to be wiped out */
521{
522#ifdef FEAT_AUTOCMD
523 int is_curbuf = (buf == curbuf);
524
525 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
526 if (!buf_valid(buf)) /* autocommands may delete the buffer */
527 return;
528 if (del_buf && buf->b_p_bl)
529 {
530 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
531 if (!buf_valid(buf)) /* autocommands may delete the buffer */
532 return;
533 }
534 if (wipe_buf)
535 {
536 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
537 FALSE, buf);
538 if (!buf_valid(buf)) /* autocommands may delete the buffer */
539 return;
540 }
541# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000542 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 return;
544# endif
545
546 /*
547 * It's possible that autocommands change curbuf to the one being deleted.
548 * This might cause curbuf to be deleted unexpectedly. But in some cases
549 * it's OK to delete the curbuf, because a new one is obtained anyway.
550 * Therefore only return if curbuf changed to the deleted buffer.
551 */
552 if (buf == curbuf && !is_curbuf)
553 return;
554#endif
555#ifdef FEAT_DIFF
556 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
557#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000558
559#ifdef FEAT_FOLDING
560 /* No folds in an empty buffer. */
561# ifdef FEAT_WINDOWS
562 {
563 win_T *win;
564 tabpage_T *tp;
565
566 FOR_ALL_TAB_WINDOWS(tp, win)
567 if (win->w_buffer == buf)
568 clearFolding(win);
569 }
570# else
571 if (curwin->w_buffer == buf)
572 clearFolding(curwin);
573# endif
574#endif
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576#ifdef FEAT_TCL
577 tcl_buffer_free(buf);
578#endif
579 u_blockfree(buf); /* free the memory allocated for undo */
580 ml_close(buf, TRUE); /* close and delete the memline/memfile */
581 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
582 u_clearall(buf); /* reset all undo information */
583#ifdef FEAT_SYN_HL
584 syntax_clear(buf); /* reset syntax info */
585#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000586 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587}
588
589/*
590 * Free a buffer structure and the things it contains related to the buffer
591 * itself (not the file, that must have been done already).
592 */
593 static void
594free_buffer(buf)
595 buf_T *buf;
596{
597 free_buffer_stuff(buf, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000598#ifdef FEAT_MZSCHEME
599 mzscheme_buffer_free(buf);
600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601#ifdef FEAT_PERL
602 perl_buf_free(buf);
603#endif
604#ifdef FEAT_PYTHON
605 python_buffer_free(buf);
606#endif
607#ifdef FEAT_RUBY
608 ruby_buffer_free(buf);
609#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000610#ifdef FEAT_AUTOCMD
611 aubuflocal_remove(buf);
612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 vim_free(buf);
614}
615
616/*
617 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
618 */
619 static void
620free_buffer_stuff(buf, free_options)
621 buf_T *buf;
622 int free_options; /* free options as well */
623{
624 if (free_options)
625 {
626 clear_wininfo(buf); /* including window-local options */
627 free_buf_options(buf, TRUE);
628 }
629#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000630 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
631 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632#endif
633#ifdef FEAT_USR_CMDS
634 uc_clear(&buf->b_ucmds); /* clear local user commands */
635#endif
636#ifdef FEAT_SIGNS
637 buf_delete_signs(buf); /* delete any signs */
638#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000639#ifdef FEAT_NETBEANS_INTG
640 if (usingNetbeans)
641 netbeans_file_killed(buf);
642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#ifdef FEAT_LOCALMAP
644 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
645 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
646#endif
647#ifdef FEAT_MBYTE
648 vim_free(buf->b_start_fenc);
649 buf->b_start_fenc = NULL;
650#endif
Bram Moolenaar9381ab72008-11-12 11:52:19 +0000651#ifdef FEAT_SPELL
652 ga_clear(&buf->b_langp);
653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654}
655
656/*
657 * Free the b_wininfo list for buffer "buf".
658 */
659 static void
660clear_wininfo(buf)
661 buf_T *buf;
662{
663 wininfo_T *wip;
664
665 while (buf->b_wininfo != NULL)
666 {
667 wip = buf->b_wininfo;
668 buf->b_wininfo = wip->wi_next;
669 if (wip->wi_optset)
670 {
671 clear_winopt(&wip->wi_opt);
672#ifdef FEAT_FOLDING
673 deleteFoldRecurse(&wip->wi_folds);
674#endif
675 }
676 vim_free(wip);
677 }
678}
679
680#if defined(FEAT_LISTCMDS) || defined(PROTO)
681/*
682 * Go to another buffer. Handles the result of the ATTENTION dialog.
683 */
684 void
685goto_buffer(eap, start, dir, count)
686 exarg_T *eap;
687 int start;
688 int dir;
689 int count;
690{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000691# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 buf_T *old_curbuf = curbuf;
693
694 swap_exists_action = SEA_DIALOG;
695# endif
696 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
697 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000698# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
700 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000701# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
702 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000703
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000704 /* Reset the error/interrupt/exception state here so that
705 * aborting() returns FALSE when closing a window. */
706 enter_cleanup(&cs);
707# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000708
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000709 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 win_close(curwin, TRUE);
711 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000712 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000713
714# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
715 /* Restore the error/interrupt/exception state if not discarded by a
716 * new aborting error, interrupt, or uncaught exception. */
717 leave_cleanup(&cs);
718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 }
720 else
721 handle_swap_exists(old_curbuf);
722# endif
723}
724#endif
725
Bram Moolenaare64ac772005-12-07 20:54:59 +0000726#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727/*
728 * Handle the situation of swap_exists_action being set.
729 * It is allowed for "old_curbuf" to be NULL or invalid.
730 */
731 void
732handle_swap_exists(old_curbuf)
733 buf_T *old_curbuf;
734{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000735# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
736 cleanup_T cs;
737# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000738
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 if (swap_exists_action == SEA_QUIT)
740 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000741# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
742 /* Reset the error/interrupt/exception state here so that
743 * aborting() returns FALSE when closing a buffer. */
744 enter_cleanup(&cs);
745# endif
746
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 /* User selected Quit at ATTENTION prompt. Go back to previous
748 * buffer. If that buffer is gone or the same as the current one,
749 * open a new, empty buffer. */
750 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000751 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
753 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000754 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 if (old_curbuf != NULL)
756 enter_buffer(old_curbuf);
757 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000758
759# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
760 /* Restore the error/interrupt/exception state if not discarded by a
761 * new aborting error, interrupt, or uncaught exception. */
762 leave_cleanup(&cs);
763# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 }
765 else if (swap_exists_action == SEA_RECOVER)
766 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000767# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
768 /* Reset the error/interrupt/exception state here so that
769 * aborting() returns FALSE when closing a buffer. */
770 enter_cleanup(&cs);
771# endif
772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 /* User selected Recover at ATTENTION prompt. */
774 msg_scroll = TRUE;
775 ml_recover();
776 MSG_PUTS("\n"); /* don't overwrite the last message */
777 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000778 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000779
780# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
781 /* Restore the error/interrupt/exception state if not discarded by a
782 * new aborting error, interrupt, or uncaught exception. */
783 leave_cleanup(&cs);
784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786 swap_exists_action = SEA_NONE;
787}
788#endif
789
790#if defined(FEAT_LISTCMDS) || defined(PROTO)
791/*
792 * do_bufdel() - delete or unload buffer(s)
793 *
794 * addr_count == 0: ":bdel" - delete current buffer
795 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
796 * buffer "end_bnr", then any other arguments.
797 * addr_count == 2: ":N,N bdel" - delete buffers in range
798 *
799 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
800 * DOBUF_DEL (":bdel")
801 *
802 * Returns error message or NULL
803 */
804 char_u *
805do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
806 int command;
807 char_u *arg; /* pointer to extra arguments */
808 int addr_count;
809 int start_bnr; /* first buffer number in a range */
810 int end_bnr; /* buffer nr or last buffer nr in a range */
811 int forceit;
812{
813 int do_current = 0; /* delete current buffer? */
814 int deleted = 0; /* number of buffers deleted */
815 char_u *errormsg = NULL; /* return value */
816 int bnr; /* buffer number */
817 char_u *p;
818
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (addr_count == 0)
820 {
821 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
822 }
823 else
824 {
825 if (addr_count == 2)
826 {
827 if (*arg) /* both range and argument is not allowed */
828 return (char_u *)_(e_trailing);
829 bnr = start_bnr;
830 }
831 else /* addr_count == 1 */
832 bnr = end_bnr;
833
834 for ( ;!got_int; ui_breakcheck())
835 {
836 /*
837 * delete the current buffer last, otherwise when the
838 * current buffer is deleted, the next buffer becomes
839 * the current one and will be loaded, which may then
840 * also be deleted, etc.
841 */
842 if (bnr == curbuf->b_fnum)
843 do_current = bnr;
844 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
845 forceit) == OK)
846 ++deleted;
847
848 /*
849 * find next buffer number to delete/unload
850 */
851 if (addr_count == 2)
852 {
853 if (++bnr > end_bnr)
854 break;
855 }
856 else /* addr_count == 1 */
857 {
858 arg = skipwhite(arg);
859 if (*arg == NUL)
860 break;
861 if (!VIM_ISDIGIT(*arg))
862 {
863 p = skiptowhite_esc(arg);
864 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
865 if (bnr < 0) /* failed */
866 break;
867 arg = p;
868 }
869 else
870 bnr = getdigits(&arg);
871 }
872 }
873 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
874 FORWARD, do_current, forceit) == OK)
875 ++deleted;
876
877 if (deleted == 0)
878 {
879 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000880 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000882 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000884 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 errormsg = IObuff;
886 }
887 else if (deleted >= p_report)
888 {
889 if (command == DOBUF_UNLOAD)
890 {
891 if (deleted == 1)
892 MSG(_("1 buffer unloaded"));
893 else
894 smsg((char_u *)_("%d buffers unloaded"), deleted);
895 }
896 else if (command == DOBUF_DEL)
897 {
898 if (deleted == 1)
899 MSG(_("1 buffer deleted"));
900 else
901 smsg((char_u *)_("%d buffers deleted"), deleted);
902 }
903 else
904 {
905 if (deleted == 1)
906 MSG(_("1 buffer wiped out"));
907 else
908 smsg((char_u *)_("%d buffers wiped out"), deleted);
909 }
910 }
911 }
912
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913
914 return errormsg;
915}
916
917/*
918 * Implementation of the commands for the buffer list.
919 *
920 * action == DOBUF_GOTO go to specified buffer
921 * action == DOBUF_SPLIT split window and go to specified buffer
922 * action == DOBUF_UNLOAD unload specified buffer(s)
923 * action == DOBUF_DEL delete specified buffer(s) from buffer list
924 * action == DOBUF_WIPE delete specified buffer(s) really
925 *
926 * start == DOBUF_CURRENT go to "count" buffer from current buffer
927 * start == DOBUF_FIRST go to "count" buffer from first buffer
928 * start == DOBUF_LAST go to "count" buffer from last buffer
929 * start == DOBUF_MOD go to "count" modified buffer from current buffer
930 *
931 * Return FAIL or OK.
932 */
933 int
934do_buffer(action, start, dir, count, forceit)
935 int action;
936 int start;
937 int dir; /* FORWARD or BACKWARD */
938 int count; /* buffer number or number of buffers */
939 int forceit; /* TRUE for :...! */
940{
941 buf_T *buf;
942 buf_T *bp;
943 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
944 || action == DOBUF_WIPE);
945
946 switch (start)
947 {
948 case DOBUF_FIRST: buf = firstbuf; break;
949 case DOBUF_LAST: buf = lastbuf; break;
950 default: buf = curbuf; break;
951 }
952 if (start == DOBUF_MOD) /* find next modified buffer */
953 {
954 while (count-- > 0)
955 {
956 do
957 {
958 buf = buf->b_next;
959 if (buf == NULL)
960 buf = firstbuf;
961 }
962 while (buf != curbuf && !bufIsChanged(buf));
963 }
964 if (!bufIsChanged(buf))
965 {
966 EMSG(_("E84: No modified buffer found"));
967 return FAIL;
968 }
969 }
970 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
971 {
972 while (buf != NULL && buf->b_fnum != count)
973 buf = buf->b_next;
974 }
975 else
976 {
977 bp = NULL;
978 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
979 {
980 /* remember the buffer where we start, we come back there when all
981 * buffers are unlisted. */
982 if (bp == NULL)
983 bp = buf;
984 if (dir == FORWARD)
985 {
986 buf = buf->b_next;
987 if (buf == NULL)
988 buf = firstbuf;
989 }
990 else
991 {
992 buf = buf->b_prev;
993 if (buf == NULL)
994 buf = lastbuf;
995 }
996 /* don't count unlisted buffers */
997 if (unload || buf->b_p_bl)
998 {
999 --count;
1000 bp = NULL; /* use this buffer as new starting point */
1001 }
1002 if (bp == buf)
1003 {
1004 /* back where we started, didn't find anything. */
1005 EMSG(_("E85: There is no listed buffer"));
1006 return FAIL;
1007 }
1008 }
1009 }
1010
1011 if (buf == NULL) /* could not find it */
1012 {
1013 if (start == DOBUF_FIRST)
1014 {
1015 /* don't warn when deleting */
1016 if (!unload)
1017 EMSGN(_("E86: Buffer %ld does not exist"), count);
1018 }
1019 else if (dir == FORWARD)
1020 EMSG(_("E87: Cannot go beyond last buffer"));
1021 else
1022 EMSG(_("E88: Cannot go before first buffer"));
1023 return FAIL;
1024 }
1025
1026#ifdef FEAT_GUI
1027 need_mouse_correct = TRUE;
1028#endif
1029
1030#ifdef FEAT_LISTCMDS
1031 /*
1032 * delete buffer buf from memory and/or the list
1033 */
1034 if (unload)
1035 {
1036 int forward;
1037 int retval;
1038
1039 /* When unloading or deleting a buffer that's already unloaded and
1040 * unlisted: fail silently. */
1041 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1042 return FAIL;
1043
1044 if (!forceit && bufIsChanged(buf))
1045 {
1046#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1047 if ((p_confirm || cmdmod.confirm) && p_write)
1048 {
1049 dialog_changed(buf, FALSE);
1050# ifdef FEAT_AUTOCMD
1051 if (!buf_valid(buf))
1052 /* Autocommand deleted buffer, oops! It's not changed
1053 * now. */
1054 return FAIL;
1055# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001056 /* If it's still changed fail silently, the dialog already
1057 * mentioned why it fails. */
1058 if (bufIsChanged(buf))
1059 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001061 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062#endif
1063 {
1064 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1065 buf->b_fnum);
1066 return FAIL;
1067 }
1068 }
1069
1070 /*
1071 * If deleting the last (listed) buffer, make it empty.
1072 * The last (listed) buffer cannot be unloaded.
1073 */
1074 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1075 if (bp->b_p_bl && bp != buf)
1076 break;
1077 if (bp == NULL && buf == curbuf)
1078 {
1079 if (action == DOBUF_UNLOAD)
1080 {
1081 EMSG(_("E90: Cannot unload last buffer"));
1082 return FAIL;
1083 }
1084
1085 /* Close any other windows on this buffer, then make it empty. */
1086#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001087 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088#endif
1089 setpcmark();
1090 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001091 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092
1093 /*
1094 * do_ecmd() may create a new buffer, then we have to delete
1095 * the old one. But do_ecmd() may have done that already, check
1096 * if the buffer still exists.
1097 */
1098 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1099 close_buffer(NULL, buf, action);
1100 return retval;
1101 }
1102
1103#ifdef FEAT_WINDOWS
1104 /*
1105 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001106 * (unless it's the only window). Repeat this so long as we end up in
1107 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001109 while (buf == curbuf
1110 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 win_close(curwin, FALSE);
1112#endif
1113
1114 /*
1115 * If the buffer to be deleted is not the current one, delete it here.
1116 */
1117 if (buf != curbuf)
1118 {
1119#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001120 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121#endif
1122 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1123 close_buffer(NULL, buf, action);
1124 return OK;
1125 }
1126
1127 /*
1128 * Deleting the current buffer: Need to find another buffer to go to.
1129 * There must be another, otherwise it would have been handled above.
1130 * First use au_new_curbuf, if it is valid.
1131 * Then prefer the buffer we most recently visited.
1132 * Else try to find one that is loaded, after the current buffer,
1133 * then before the current buffer.
1134 * Finally use any buffer.
1135 */
1136 buf = NULL; /* selected buffer */
1137 bp = NULL; /* used when no loaded buffer found */
1138#ifdef FEAT_AUTOCMD
1139 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1140 buf = au_new_curbuf;
1141# ifdef FEAT_JUMPLIST
1142 else
1143# endif
1144#endif
1145#ifdef FEAT_JUMPLIST
1146 if (curwin->w_jumplistlen > 0)
1147 {
1148 int jumpidx;
1149
1150 jumpidx = curwin->w_jumplistidx - 1;
1151 if (jumpidx < 0)
1152 jumpidx = curwin->w_jumplistlen - 1;
1153
1154 forward = jumpidx;
1155 while (jumpidx != curwin->w_jumplistidx)
1156 {
1157 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1158 if (buf != NULL)
1159 {
1160 if (buf == curbuf || !buf->b_p_bl)
1161 buf = NULL; /* skip current and unlisted bufs */
1162 else if (buf->b_ml.ml_mfp == NULL)
1163 {
1164 /* skip unloaded buf, but may keep it for later */
1165 if (bp == NULL)
1166 bp = buf;
1167 buf = NULL;
1168 }
1169 }
1170 if (buf != NULL) /* found a valid buffer: stop searching */
1171 break;
1172 /* advance to older entry in jump list */
1173 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1174 break;
1175 if (--jumpidx < 0)
1176 jumpidx = curwin->w_jumplistlen - 1;
1177 if (jumpidx == forward) /* List exhausted for sure */
1178 break;
1179 }
1180 }
1181#endif
1182
1183 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1184 {
1185 forward = TRUE;
1186 buf = curbuf->b_next;
1187 for (;;)
1188 {
1189 if (buf == NULL)
1190 {
1191 if (!forward) /* tried both directions */
1192 break;
1193 buf = curbuf->b_prev;
1194 forward = FALSE;
1195 continue;
1196 }
1197 /* in non-help buffer, try to skip help buffers, and vv */
1198 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1199 {
1200 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1201 break;
1202 if (bp == NULL) /* remember unloaded buf for later */
1203 bp = buf;
1204 }
1205 if (forward)
1206 buf = buf->b_next;
1207 else
1208 buf = buf->b_prev;
1209 }
1210 }
1211 if (buf == NULL) /* No loaded buffer, use unloaded one */
1212 buf = bp;
1213 if (buf == NULL) /* No loaded buffer, find listed one */
1214 {
1215 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1216 if (buf->b_p_bl && buf != curbuf)
1217 break;
1218 }
1219 if (buf == NULL) /* Still no buffer, just take one */
1220 {
1221 if (curbuf->b_next != NULL)
1222 buf = curbuf->b_next;
1223 else
1224 buf = curbuf->b_prev;
1225 }
1226 }
1227
1228 /*
1229 * make buf current buffer
1230 */
1231 if (action == DOBUF_SPLIT) /* split window first */
1232 {
1233# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001234 /* If 'switchbuf' contains "useopen": jump to first window containing
1235 * "buf" if one exists */
1236 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001237 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001238 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001239 * page containing "buf" if one exists */
1240 if ((swb_flags & SWB_USETAB) && 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 Moolenaar701f7af2008-11-15 13:12:07 +00001314 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
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
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001352 /* An autocommand may have deleted "buf", already entered it (e.g., when
1353 * it did ":bunload") or aborted the script processing! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354# ifdef FEAT_EVAL
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001355 if (buf_valid(buf) && buf != curbuf && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356# else
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001357 if (buf_valid(buf) && buf != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358# endif
1359#endif
1360 enter_buffer(buf);
1361}
1362
1363/*
1364 * Enter a new current buffer.
1365 * Old curbuf must have been abandoned already!
1366 */
1367 void
1368enter_buffer(buf)
1369 buf_T *buf;
1370{
1371 /* Copy buffer and window local option values. Not for a help buffer. */
1372 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1373 if (!buf->b_help)
1374 get_winopts(buf);
1375#ifdef FEAT_FOLDING
1376 else
1377 /* Remove all folds in the window. */
1378 clearFolding(curwin);
1379 foldUpdateAll(curwin); /* update folds (later). */
1380#endif
1381
1382 /* Get the buffer in the current window. */
1383 curwin->w_buffer = buf;
1384 curbuf = buf;
1385 ++curbuf->b_nwindows;
1386
1387#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001388 if (curwin->w_p_diff)
1389 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390#endif
1391
1392 /* Cursor on first line by default. */
1393 curwin->w_cursor.lnum = 1;
1394 curwin->w_cursor.col = 0;
1395#ifdef FEAT_VIRTUALEDIT
1396 curwin->w_cursor.coladd = 0;
1397#endif
1398 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001399#ifdef FEAT_AUTOCMD
1400 curwin->w_topline_was_set = FALSE;
1401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402
1403 /* Make sure the buffer is loaded. */
1404 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001405 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001406#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001407 /* If there is no filetype, allow for detecting one. Esp. useful for
1408 * ":ball" used in a autocommand. If there already is a filetype we
1409 * might prefer to keep it. */
1410 if (*curbuf->b_p_ft == NUL)
1411 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001412#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 open_buffer(FALSE, NULL);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 else
1417 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001418 if (!msg_silent)
1419 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1421#ifdef FEAT_AUTOCMD
1422 curwin->w_topline = 1;
1423# ifdef FEAT_DIFF
1424 curwin->w_topfill = 0;
1425# endif
1426 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1427 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1428#endif
1429 }
1430
1431 /* If autocommands did not change the cursor position, restore cursor lnum
1432 * and possibly cursor col. */
1433 if (curwin->w_cursor.lnum == 1 && inindent(0))
1434 buflist_getfpos();
1435
1436 check_arg_idx(curwin); /* check for valid arg_idx */
1437#ifdef FEAT_TITLE
1438 maketitle();
1439#endif
1440#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001441 /* when autocmds didn't change it */
1442 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443#endif
1444 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1445
Bram Moolenaar009b2592004-10-24 19:18:58 +00001446#ifdef FEAT_NETBEANS_INTG
1447 /* Send fileOpened event because we've changed buffers. */
1448 if (usingNetbeans && isNetbeansBuffer(curbuf))
1449 netbeans_file_activated(curbuf);
1450#endif
1451
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001452 /* Change directories when the 'acd' option is set. */
1453 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454
1455#ifdef FEAT_KEYMAP
1456 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001457 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001459#ifdef FEAT_SPELL
1460 /* May need to set the spell language. Can only do this after the buffer
1461 * has been properly setup. */
1462 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001463 (void)did_set_spelllang(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001464#endif
1465
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 redraw_later(NOT_VALID);
1467}
1468
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001469#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1470/*
1471 * Change to the directory of the current buffer.
1472 */
1473 void
1474do_autochdir()
1475{
1476 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1477 shorten_fnames(TRUE);
1478}
1479#endif
1480
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481/*
1482 * functions for dealing with the buffer list
1483 */
1484
1485/*
1486 * Add a file name to the buffer list. Return a pointer to the buffer.
1487 * If the same file name already exists return a pointer to that buffer.
1488 * If it does not exist, or if fname == NULL, a new entry is created.
1489 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1490 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1491 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 * This is the ONLY way to create a new buffer.
1493 */
1494static int top_file_num = 1; /* highest file number */
1495
1496 buf_T *
1497buflist_new(ffname, sfname, lnum, flags)
1498 char_u *ffname; /* full path of fname or relative */
1499 char_u *sfname; /* short fname or NULL */
1500 linenr_T lnum; /* preferred cursor line */
1501 int flags; /* BLN_ defines */
1502{
1503 buf_T *buf;
1504#ifdef UNIX
1505 struct stat st;
1506#endif
1507
1508 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1509
1510 /*
1511 * If file name already exists in the list, update the entry.
1512 */
1513#ifdef UNIX
1514 /* On Unix we can use inode numbers when the file exists. Works better
1515 * for hard links. */
1516 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1517 st.st_dev = (dev_T)-1;
1518#endif
1519 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1520#ifdef UNIX
1521 buflist_findname_stat(ffname, &st)
1522#else
1523 buflist_findname(ffname)
1524#endif
1525 ) != NULL)
1526 {
1527 vim_free(ffname);
1528 if (lnum != 0)
1529 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1530 /* copy the options now, if 'cpo' doesn't have 's' and not done
1531 * already */
1532 buf_copy_options(buf, 0);
1533 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1534 {
1535 buf->b_p_bl = TRUE;
1536#ifdef FEAT_AUTOCMD
1537 if (!(flags & BLN_DUMMY))
1538 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1539#endif
1540 }
1541 return buf;
1542 }
1543
1544 /*
1545 * If the current buffer has no name and no contents, use the current
1546 * buffer. Otherwise: Need to allocate a new buffer structure.
1547 *
1548 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001549 * (A spell file buffer is allocated in spell.c, but that's not a normal
1550 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 */
1552 buf = NULL;
1553 if ((flags & BLN_CURBUF)
1554 && curbuf != NULL
1555 && curbuf->b_ffname == NULL
1556 && curbuf->b_nwindows <= 1
1557 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1558 {
1559 buf = curbuf;
1560#ifdef FEAT_AUTOCMD
1561 /* It's like this buffer is deleted. Watch out for autocommands that
1562 * change curbuf! If that happens, allocate a new buffer anyway. */
1563 if (curbuf->b_p_bl)
1564 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1565 if (buf == curbuf)
1566 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1567# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001568 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 return NULL;
1570# endif
1571#endif
1572#ifdef FEAT_QUICKFIX
1573# ifdef FEAT_AUTOCMD
1574 if (buf == curbuf)
1575# endif
1576 {
1577 /* Make sure 'bufhidden' and 'buftype' are empty */
1578 clear_string_option(&buf->b_p_bh);
1579 clear_string_option(&buf->b_p_bt);
1580 }
1581#endif
1582 }
1583 if (buf != curbuf || curbuf == NULL)
1584 {
1585 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1586 if (buf == NULL)
1587 {
1588 vim_free(ffname);
1589 return NULL;
1590 }
1591 }
1592
1593 if (ffname != NULL)
1594 {
1595 buf->b_ffname = ffname;
1596 buf->b_sfname = vim_strsave(sfname);
1597 }
1598
1599 clear_wininfo(buf);
1600 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1601
1602 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1603 || buf->b_wininfo == NULL)
1604 {
1605 vim_free(buf->b_ffname);
1606 buf->b_ffname = NULL;
1607 vim_free(buf->b_sfname);
1608 buf->b_sfname = NULL;
1609 if (buf != curbuf)
1610 free_buffer(buf);
1611 return NULL;
1612 }
1613
1614 if (buf == curbuf)
1615 {
1616 /* free all things allocated for this buffer */
1617 buf_freeall(buf, FALSE, FALSE);
1618 if (buf != curbuf) /* autocommands deleted the buffer! */
1619 return NULL;
1620#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001621 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 return NULL;
1623#endif
1624 /* buf->b_nwindows = 0; why was this here? */
1625 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1626#ifdef FEAT_KEYMAP
1627 /* need to reload lmaps and set b:keymap_name */
1628 curbuf->b_kmap_state |= KEYMAP_INIT;
1629#endif
1630 }
1631 else
1632 {
1633 /*
1634 * put new buffer at the end of the buffer list
1635 */
1636 buf->b_next = NULL;
1637 if (firstbuf == NULL) /* buffer list is empty */
1638 {
1639 buf->b_prev = NULL;
1640 firstbuf = buf;
1641 }
1642 else /* append new buffer at end of list */
1643 {
1644 lastbuf->b_next = buf;
1645 buf->b_prev = lastbuf;
1646 }
1647 lastbuf = buf;
1648
1649 buf->b_fnum = top_file_num++;
1650 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1651 {
1652 EMSG(_("W14: Warning: List of file names overflow"));
1653 if (emsg_silent == 0)
1654 {
1655 out_flush();
1656 ui_delay(3000L, TRUE); /* make sure it is noticed */
1657 }
1658 top_file_num = 1;
1659 }
1660
1661 /*
1662 * Always copy the options from the current buffer.
1663 */
1664 buf_copy_options(buf, BCO_ALWAYS);
1665 }
1666
1667 buf->b_wininfo->wi_fpos.lnum = lnum;
1668 buf->b_wininfo->wi_win = curwin;
1669
1670#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001671 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1672#endif
1673#ifdef FEAT_SYN_HL
1674 hash_init(&buf->b_keywtab);
1675 hash_init(&buf->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676#endif
1677
1678 buf->b_fname = buf->b_sfname;
1679#ifdef UNIX
1680 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001681 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 else
1683 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001684 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 buf->b_dev = st.st_dev;
1686 buf->b_ino = st.st_ino;
1687 }
1688#endif
1689 buf->b_u_synced = TRUE;
1690 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001691 if (flags & BLN_DUMMY)
1692 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 buf_clear_file(buf);
1694 clrallmarks(buf); /* clear marks */
1695 fmarks_check_names(buf); /* check file marks for this file */
1696 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1697#ifdef FEAT_AUTOCMD
1698 if (!(flags & BLN_DUMMY))
1699 {
1700 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1701 if (flags & BLN_LISTED)
1702 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1703# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001704 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 return NULL;
1706# endif
1707 }
1708#endif
1709
1710 return buf;
1711}
1712
1713/*
1714 * Free the memory for the options of a buffer.
1715 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1716 * 'fileencoding'.
1717 */
1718 void
1719free_buf_options(buf, free_p_ff)
1720 buf_T *buf;
1721 int free_p_ff;
1722{
1723 if (free_p_ff)
1724 {
1725#ifdef FEAT_MBYTE
1726 clear_string_option(&buf->b_p_fenc);
1727#endif
1728 clear_string_option(&buf->b_p_ff);
1729#ifdef FEAT_QUICKFIX
1730 clear_string_option(&buf->b_p_bh);
1731 clear_string_option(&buf->b_p_bt);
1732#endif
1733 }
1734#ifdef FEAT_FIND_ID
1735 clear_string_option(&buf->b_p_def);
1736 clear_string_option(&buf->b_p_inc);
1737# ifdef FEAT_EVAL
1738 clear_string_option(&buf->b_p_inex);
1739# endif
1740#endif
1741#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1742 clear_string_option(&buf->b_p_inde);
1743 clear_string_option(&buf->b_p_indk);
1744#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001745#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1746 clear_string_option(&buf->b_p_bexpr);
1747#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001748#if defined(FEAT_EVAL)
1749 clear_string_option(&buf->b_p_fex);
1750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751#ifdef FEAT_CRYPT
1752 clear_string_option(&buf->b_p_key);
1753#endif
1754 clear_string_option(&buf->b_p_kp);
1755 clear_string_option(&buf->b_p_mps);
1756 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001757 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 clear_string_option(&buf->b_p_isk);
1759#ifdef FEAT_KEYMAP
1760 clear_string_option(&buf->b_p_keymap);
1761 ga_clear(&buf->b_kmap_ga);
1762#endif
1763#ifdef FEAT_COMMENTS
1764 clear_string_option(&buf->b_p_com);
1765#endif
1766#ifdef FEAT_FOLDING
1767 clear_string_option(&buf->b_p_cms);
1768#endif
1769 clear_string_option(&buf->b_p_nf);
1770#ifdef FEAT_SYN_HL
1771 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001772#endif
1773#ifdef FEAT_SPELL
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001774 clear_string_option(&buf->b_p_spc);
Bram Moolenaar86bc1fb2005-06-07 20:58:01 +00001775 clear_string_option(&buf->b_p_spf);
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001776 vim_free(buf->b_cap_prog);
1777 buf->b_cap_prog = NULL;
Bram Moolenaara0dea672005-03-20 22:23:10 +00001778 clear_string_option(&buf->b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779#endif
1780#ifdef FEAT_SEARCHPATH
1781 clear_string_option(&buf->b_p_sua);
1782#endif
1783#ifdef FEAT_AUTOCMD
1784 clear_string_option(&buf->b_p_ft);
1785#endif
1786#ifdef FEAT_OSFILETYPE
1787 clear_string_option(&buf->b_p_oft);
1788#endif
1789#ifdef FEAT_CINDENT
1790 clear_string_option(&buf->b_p_cink);
1791 clear_string_option(&buf->b_p_cino);
1792#endif
1793#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1794 clear_string_option(&buf->b_p_cinw);
1795#endif
1796#ifdef FEAT_INS_EXPAND
1797 clear_string_option(&buf->b_p_cpt);
1798#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001799#ifdef FEAT_COMPL_FUNC
1800 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001801 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803#ifdef FEAT_QUICKFIX
1804 clear_string_option(&buf->b_p_gp);
1805 clear_string_option(&buf->b_p_mp);
1806 clear_string_option(&buf->b_p_efm);
1807#endif
1808 clear_string_option(&buf->b_p_ep);
1809 clear_string_option(&buf->b_p_path);
1810 clear_string_option(&buf->b_p_tags);
1811#ifdef FEAT_INS_EXPAND
1812 clear_string_option(&buf->b_p_dict);
1813 clear_string_option(&buf->b_p_tsr);
1814#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001815#ifdef FEAT_TEXTOBJ
1816 clear_string_option(&buf->b_p_qe);
1817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 buf->b_p_ar = -1;
1819}
1820
1821/*
1822 * get alternate file n
1823 * set linenr to lnum or altfpos.lnum if lnum == 0
1824 * also set cursor column to altfpos.col if 'startofline' is not set.
1825 * if (options & GETF_SETMARK) call setpcmark()
1826 * if (options & GETF_ALT) we are jumping to an alternate file.
1827 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1828 *
1829 * return FAIL for failure, OK for success
1830 */
1831 int
1832buflist_getfile(n, lnum, options, forceit)
1833 int n;
1834 linenr_T lnum;
1835 int options;
1836 int forceit;
1837{
1838 buf_T *buf;
1839#ifdef FEAT_WINDOWS
1840 win_T *wp = NULL;
1841#endif
1842 pos_T *fpos;
1843 colnr_T col;
1844
1845 buf = buflist_findnr(n);
1846 if (buf == NULL)
1847 {
1848 if ((options & GETF_ALT) && n == 0)
1849 EMSG(_(e_noalt));
1850 else
1851 EMSGN(_("E92: Buffer %ld not found"), n);
1852 return FAIL;
1853 }
1854
1855 /* if alternate file is the current buffer, nothing to do */
1856 if (buf == curbuf)
1857 return OK;
1858
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001859 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001860 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001861 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001863 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001864#ifdef FEAT_AUTOCMD
1865 if (curbuf_locked())
1866 return FAIL;
1867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
1869 /* altfpos may be changed by getfile(), get it now */
1870 if (lnum == 0)
1871 {
1872 fpos = buflist_findfpos(buf);
1873 lnum = fpos->lnum;
1874 col = fpos->col;
1875 }
1876 else
1877 col = 0;
1878
1879#ifdef FEAT_WINDOWS
1880 if (options & GETF_SWITCH)
1881 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001882 /* If 'switchbuf' contains "useopen": jump to first window containing
1883 * "buf" if one exists */
1884 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001886 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1887 * page containing "buf" if one exists */
1888 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001889 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001890 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1891 * isn't empty: open new window */
1892 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001894 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1895 tabpage_new();
1896 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 return FAIL;
1898# ifdef FEAT_SCROLLBIND
1899 curwin->w_p_scb = FALSE;
1900# endif
1901 }
1902 }
1903#endif
1904
1905 ++RedrawingDisabled;
1906 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1907 lnum, forceit) <= 0)
1908 {
1909 --RedrawingDisabled;
1910
1911 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1912 if (!p_sol && col != 0)
1913 {
1914 curwin->w_cursor.col = col;
1915 check_cursor_col();
1916#ifdef FEAT_VIRTUALEDIT
1917 curwin->w_cursor.coladd = 0;
1918#endif
1919 curwin->w_set_curswant = TRUE;
1920 }
1921 return OK;
1922 }
1923 --RedrawingDisabled;
1924 return FAIL;
1925}
1926
1927/*
1928 * go to the last know line number for the current buffer
1929 */
1930 void
1931buflist_getfpos()
1932{
1933 pos_T *fpos;
1934
1935 fpos = buflist_findfpos(curbuf);
1936
1937 curwin->w_cursor.lnum = fpos->lnum;
1938 check_cursor_lnum();
1939
1940 if (p_sol)
1941 curwin->w_cursor.col = 0;
1942 else
1943 {
1944 curwin->w_cursor.col = fpos->col;
1945 check_cursor_col();
1946#ifdef FEAT_VIRTUALEDIT
1947 curwin->w_cursor.coladd = 0;
1948#endif
1949 curwin->w_set_curswant = TRUE;
1950 }
1951}
1952
Bram Moolenaar81695252004-12-29 20:58:21 +00001953#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1954/*
1955 * Find file in buffer list by name (it has to be for the current window).
1956 * Returns NULL if not found.
1957 */
1958 buf_T *
1959buflist_findname_exp(fname)
1960 char_u *fname;
1961{
1962 char_u *ffname;
1963 buf_T *buf = NULL;
1964
1965 /* First make the name into a full path name */
1966 ffname = FullName_save(fname,
1967#ifdef UNIX
1968 TRUE /* force expansion, get rid of symbolic links */
1969#else
1970 FALSE
1971#endif
1972 );
1973 if (ffname != NULL)
1974 {
1975 buf = buflist_findname(ffname);
1976 vim_free(ffname);
1977 }
1978 return buf;
1979}
1980#endif
1981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982/*
1983 * Find file in buffer list by name (it has to be for the current window).
1984 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00001985 * Skips dummy buffers.
1986 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 */
1988 buf_T *
1989buflist_findname(ffname)
1990 char_u *ffname;
1991{
1992#ifdef UNIX
1993 struct stat st;
1994
1995 if (mch_stat((char *)ffname, &st) < 0)
1996 st.st_dev = (dev_T)-1;
1997 return buflist_findname_stat(ffname, &st);
1998}
1999
2000/*
2001 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2002 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002003 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 */
2005 static buf_T *
2006buflist_findname_stat(ffname, stp)
2007 char_u *ffname;
2008 struct stat *stp;
2009{
2010#endif
2011 buf_T *buf;
2012
2013 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002014 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015#ifdef UNIX
2016 , stp
2017#endif
2018 ))
2019 return buf;
2020 return NULL;
2021}
2022
2023#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2024/*
2025 * Find file in buffer list by a regexp pattern.
2026 * Return fnum of the found buffer.
2027 * Return < 0 for error.
2028 */
2029/*ARGSUSED*/
2030 int
2031buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2032 char_u *pattern;
2033 char_u *pattern_end; /* pointer to first char after pattern */
2034 int unlisted; /* find unlisted buffers */
2035 int diffmode; /* find diff-mode buffers only */
2036{
2037 buf_T *buf;
2038 regprog_T *prog;
2039 int match = -1;
2040 int find_listed;
2041 char_u *pat;
2042 char_u *patend;
2043 int attempt;
2044 char_u *p;
2045 int toggledollar;
2046
2047 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2048 {
2049 if (*pattern == '%')
2050 match = curbuf->b_fnum;
2051 else
2052 match = curwin->w_alt_fnum;
2053#ifdef FEAT_DIFF
2054 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2055 match = -1;
2056#endif
2057 }
2058
2059 /*
2060 * Try four ways of matching a listed buffer:
2061 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002062 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 * attempt == 2: with '$' at end (only match at end)
2064 * attempt == 3: with '^' at start and '$' at end (only full match)
2065 * Repeat this for finding an unlisted buffer if there was no matching
2066 * listed buffer.
2067 */
2068 else
2069 {
2070 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2071 if (pat == NULL)
2072 return -1;
2073 patend = pat + STRLEN(pat) - 1;
2074 toggledollar = (patend > pat && *patend == '$');
2075
2076 /* First try finding a listed buffer. If not found and "unlisted"
2077 * is TRUE, try finding an unlisted buffer. */
2078 find_listed = TRUE;
2079 for (;;)
2080 {
2081 for (attempt = 0; attempt <= 3; ++attempt)
2082 {
2083 /* may add '^' and '$' */
2084 if (toggledollar)
2085 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2086 p = pat;
2087 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2088 ++p;
2089 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2090 if (prog == NULL)
2091 {
2092 vim_free(pat);
2093 return -1;
2094 }
2095
2096 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2097 if (buf->b_p_bl == find_listed
2098#ifdef FEAT_DIFF
2099 && (!diffmode || diff_mode_buf(buf))
2100#endif
2101 && buflist_match(prog, buf) != NULL)
2102 {
2103 if (match >= 0) /* already found a match */
2104 {
2105 match = -2;
2106 break;
2107 }
2108 match = buf->b_fnum; /* remember first match */
2109 }
2110
2111 vim_free(prog);
2112 if (match >= 0) /* found one match */
2113 break;
2114 }
2115
2116 /* Only search for unlisted buffers if there was no match with
2117 * a listed buffer. */
2118 if (!unlisted || !find_listed || match != -1)
2119 break;
2120 find_listed = FALSE;
2121 }
2122
2123 vim_free(pat);
2124 }
2125
2126 if (match == -2)
2127 EMSG2(_("E93: More than one match for %s"), pattern);
2128 else if (match < 0)
2129 EMSG2(_("E94: No matching buffer for %s"), pattern);
2130 return match;
2131}
2132#endif
2133
2134#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2135
2136/*
2137 * Find all buffer names that match.
2138 * For command line expansion of ":buf" and ":sbuf".
2139 * Return OK if matches found, FAIL otherwise.
2140 */
2141 int
2142ExpandBufnames(pat, num_file, file, options)
2143 char_u *pat;
2144 int *num_file;
2145 char_u ***file;
2146 int options;
2147{
2148 int count = 0;
2149 buf_T *buf;
2150 int round;
2151 char_u *p;
2152 int attempt;
2153 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002154 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156 *num_file = 0; /* return values in case of FAIL */
2157 *file = NULL;
2158
Bram Moolenaar05159a02005-02-26 23:04:13 +00002159 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2160 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002162 patc = alloc((unsigned)STRLEN(pat) + 11);
2163 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002165 STRCPY(patc, "\\(^\\|[\\/]\\)");
2166 STRCPY(patc + 11, pat + 1);
2167 }
2168 else
2169 patc = pat;
2170
2171 /*
2172 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002173 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002174 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002175 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002176 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002177 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002178 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002179 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002180 if (prog == NULL)
2181 {
2182 if (patc != pat)
2183 vim_free(patc);
2184 return FAIL;
2185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
2187 /*
2188 * round == 1: Count the matches.
2189 * round == 2: Build the array to keep the matches.
2190 */
2191 for (round = 1; round <= 2; ++round)
2192 {
2193 count = 0;
2194 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2195 {
2196 if (!buf->b_p_bl) /* skip unlisted buffers */
2197 continue;
2198 p = buflist_match(prog, buf);
2199 if (p != NULL)
2200 {
2201 if (round == 1)
2202 ++count;
2203 else
2204 {
2205 if (options & WILD_HOME_REPLACE)
2206 p = home_replace_save(buf, p);
2207 else
2208 p = vim_strsave(p);
2209 (*file)[count++] = p;
2210 }
2211 }
2212 }
2213 if (count == 0) /* no match found, break here */
2214 break;
2215 if (round == 1)
2216 {
2217 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2218 if (*file == NULL)
2219 {
2220 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002221 if (patc != pat)
2222 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 return FAIL;
2224 }
2225 }
2226 }
2227 vim_free(prog);
2228 if (count) /* match(es) found, break here */
2229 break;
2230 }
2231
Bram Moolenaar05159a02005-02-26 23:04:13 +00002232 if (patc != pat)
2233 vim_free(patc);
2234
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 *num_file = count;
2236 return (count == 0 ? FAIL : OK);
2237}
2238
2239#endif /* FEAT_CMDL_COMPL */
2240
2241#ifdef HAVE_BUFLIST_MATCH
2242/*
2243 * Check for a match on the file name for buffer "buf" with regprog "prog".
2244 */
2245 static char_u *
2246buflist_match(prog, buf)
2247 regprog_T *prog;
2248 buf_T *buf;
2249{
2250 char_u *match;
2251
2252 /* First try the short file name, then the long file name. */
2253 match = fname_match(prog, buf->b_sfname);
2254 if (match == NULL)
2255 match = fname_match(prog, buf->b_ffname);
2256
2257 return match;
2258}
2259
2260/*
2261 * Try matching the regexp in "prog" with file name "name".
2262 * Return "name" when there is a match, NULL when not.
2263 */
2264 static char_u *
2265fname_match(prog, name)
2266 regprog_T *prog;
2267 char_u *name;
2268{
2269 char_u *match = NULL;
2270 char_u *p;
2271 regmatch_T regmatch;
2272
2273 if (name != NULL)
2274 {
2275 regmatch.regprog = prog;
2276#ifdef CASE_INSENSITIVE_FILENAME
2277 regmatch.rm_ic = TRUE; /* Always ignore case */
2278#else
2279 regmatch.rm_ic = FALSE; /* Never ignore case */
2280#endif
2281
2282 if (vim_regexec(&regmatch, name, (colnr_T)0))
2283 match = name;
2284 else
2285 {
2286 /* Replace $(HOME) with '~' and try matching again. */
2287 p = home_replace_save(NULL, name);
2288 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2289 match = name;
2290 vim_free(p);
2291 }
2292 }
2293
2294 return match;
2295}
2296#endif
2297
2298/*
2299 * find file in buffer list by number
2300 */
2301 buf_T *
2302buflist_findnr(nr)
2303 int nr;
2304{
2305 buf_T *buf;
2306
2307 if (nr == 0)
2308 nr = curwin->w_alt_fnum;
2309 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2310 if (buf->b_fnum == nr)
2311 return (buf);
2312 return NULL;
2313}
2314
2315/*
2316 * Get name of file 'n' in the buffer list.
2317 * When the file has no name an empty string is returned.
2318 * home_replace() is used to shorten the file name (used for marks).
2319 * Returns a pointer to allocated memory, of NULL when failed.
2320 */
2321 char_u *
2322buflist_nr2name(n, fullname, helptail)
2323 int n;
2324 int fullname;
2325 int helptail; /* for help buffers return tail only */
2326{
2327 buf_T *buf;
2328
2329 buf = buflist_findnr(n);
2330 if (buf == NULL)
2331 return NULL;
2332 return home_replace_save(helptail ? buf : NULL,
2333 fullname ? buf->b_ffname : buf->b_fname);
2334}
2335
2336/*
2337 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2338 * When "copy_options" is TRUE save the local window option values.
2339 * When "lnum" is 0 only do the options.
2340 */
2341 static void
2342buflist_setfpos(buf, win, lnum, col, copy_options)
2343 buf_T *buf;
2344 win_T *win;
2345 linenr_T lnum;
2346 colnr_T col;
2347 int copy_options;
2348{
2349 wininfo_T *wip;
2350
2351 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2352 if (wip->wi_win == win)
2353 break;
2354 if (wip == NULL)
2355 {
2356 /* allocate a new entry */
2357 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2358 if (wip == NULL)
2359 return;
2360 wip->wi_win = win;
2361 if (lnum == 0) /* set lnum even when it's 0 */
2362 lnum = 1;
2363 }
2364 else
2365 {
2366 /* remove the entry from the list */
2367 if (wip->wi_prev)
2368 wip->wi_prev->wi_next = wip->wi_next;
2369 else
2370 buf->b_wininfo = wip->wi_next;
2371 if (wip->wi_next)
2372 wip->wi_next->wi_prev = wip->wi_prev;
2373 if (copy_options && wip->wi_optset)
2374 {
2375 clear_winopt(&wip->wi_opt);
2376#ifdef FEAT_FOLDING
2377 deleteFoldRecurse(&wip->wi_folds);
2378#endif
2379 }
2380 }
2381 if (lnum != 0)
2382 {
2383 wip->wi_fpos.lnum = lnum;
2384 wip->wi_fpos.col = col;
2385 }
2386 if (copy_options)
2387 {
2388 /* Save the window-specific option values. */
2389 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2390#ifdef FEAT_FOLDING
2391 wip->wi_fold_manual = win->w_fold_manual;
2392 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2393#endif
2394 wip->wi_optset = TRUE;
2395 }
2396
2397 /* insert the entry in front of the list */
2398 wip->wi_next = buf->b_wininfo;
2399 buf->b_wininfo = wip;
2400 wip->wi_prev = NULL;
2401 if (wip->wi_next)
2402 wip->wi_next->wi_prev = wip;
2403
2404 return;
2405}
2406
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002407#ifdef FEAT_DIFF
2408static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2409
2410/*
2411 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2412 * page. That's because a diff is local to a tab page.
2413 */
2414 static int
2415wininfo_other_tab_diff(wip)
2416 wininfo_T *wip;
2417{
2418 win_T *wp;
2419
2420 if (wip->wi_opt.wo_diff)
2421 {
2422 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2423 /* return FALSE when it's a window in the current tab page, thus
2424 * the buffer was in diff mode here */
2425 if (wip->wi_win == wp)
2426 return FALSE;
2427 return TRUE;
2428 }
2429 return FALSE;
2430}
2431#endif
2432
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433/*
2434 * Find info for the current window in buffer "buf".
2435 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002436 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2437 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 * Returns NULL when there isn't any info.
2439 */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002440/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002442find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 buf_T *buf;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002444 int skip_diff_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445{
2446 wininfo_T *wip;
2447
2448 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002449 if (wip->wi_win == curwin
2450#ifdef FEAT_DIFF
2451 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2452#endif
2453 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002455
2456 /* If no wininfo for curwin, use the first in the list (that doesn't have
2457 * 'diff' set and is in another tab page). */
2458 if (wip == NULL)
2459 {
2460#ifdef FEAT_DIFF
2461 if (skip_diff_buffer)
2462 {
2463 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2464 if (!wininfo_other_tab_diff(wip))
2465 break;
2466 }
2467 else
2468#endif
2469 wip = buf->b_wininfo;
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 return wip;
2472}
2473
2474/*
2475 * Reset the local window options to the values last used in this window.
2476 * If the buffer wasn't used in this window before, use the values from
2477 * the most recently used window. If the values were never set, use the
2478 * global values for the window.
2479 */
2480 void
2481get_winopts(buf)
2482 buf_T *buf;
2483{
2484 wininfo_T *wip;
2485
2486 clear_winopt(&curwin->w_onebuf_opt);
2487#ifdef FEAT_FOLDING
2488 clearFolding(curwin);
2489#endif
2490
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002491 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 if (wip != NULL && wip->wi_optset)
2493 {
2494 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2495#ifdef FEAT_FOLDING
2496 curwin->w_fold_manual = wip->wi_fold_manual;
2497 curwin->w_foldinvalid = TRUE;
2498 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2499#endif
2500 }
2501 else
2502 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2503
2504#ifdef FEAT_FOLDING
2505 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2506 if (p_fdls >= 0)
2507 curwin->w_p_fdl = p_fdls;
2508#endif
2509}
2510
2511/*
2512 * Find the position (lnum and col) for the buffer 'buf' for the current
2513 * window.
2514 * Returns a pointer to no_position if no position is found.
2515 */
2516 pos_T *
2517buflist_findfpos(buf)
2518 buf_T *buf;
2519{
2520 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002521 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002523 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 if (wip != NULL)
2525 return &(wip->wi_fpos);
2526 else
2527 return &no_position;
2528}
2529
2530/*
2531 * Find the lnum for the buffer 'buf' for the current window.
2532 */
2533 linenr_T
2534buflist_findlnum(buf)
2535 buf_T *buf;
2536{
2537 return buflist_findfpos(buf)->lnum;
2538}
2539
2540#if defined(FEAT_LISTCMDS) || defined(PROTO)
2541/*
2542 * List all know file names (for :files and :buffers command).
2543 */
2544/*ARGSUSED*/
2545 void
2546buflist_list(eap)
2547 exarg_T *eap;
2548{
2549 buf_T *buf;
2550 int len;
2551 int i;
2552
2553 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2554 {
2555 /* skip unlisted buffers, unless ! was used */
2556 if (!buf->b_p_bl && !eap->forceit)
2557 continue;
2558 msg_putchar('\n');
2559 if (buf_spname(buf) != NULL)
2560 STRCPY(NameBuff, buf_spname(buf));
2561 else
2562 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2563
Bram Moolenaar50cde822005-06-05 21:54:54 +00002564 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 buf->b_fnum,
2566 buf->b_p_bl ? ' ' : 'u',
2567 buf == curbuf ? '%' :
2568 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2569 buf->b_ml.ml_mfp == NULL ? ' ' :
2570 (buf->b_nwindows == 0 ? 'h' : 'a'),
2571 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2572 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002573 : (bufIsChanged(buf) ? '+' : ' '),
2574 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
2576 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 i = 40 - vim_strsize(IObuff);
2578 do
2579 {
2580 IObuff[len++] = ' ';
2581 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002582 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2583 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002584 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 msg_outtrans(IObuff);
2586 out_flush(); /* output one line at a time */
2587 ui_breakcheck();
2588 }
2589}
2590#endif
2591
2592/*
2593 * Get file name and line number for file 'fnum'.
2594 * Used by DoOneCmd() for translating '%' and '#'.
2595 * Used by insert_reg() and cmdline_paste() for '#' register.
2596 * Return FAIL if not found, OK for success.
2597 */
2598 int
2599buflist_name_nr(fnum, fname, lnum)
2600 int fnum;
2601 char_u **fname;
2602 linenr_T *lnum;
2603{
2604 buf_T *buf;
2605
2606 buf = buflist_findnr(fnum);
2607 if (buf == NULL || buf->b_fname == NULL)
2608 return FAIL;
2609
2610 *fname = buf->b_fname;
2611 *lnum = buflist_findlnum(buf);
2612
2613 return OK;
2614}
2615
2616/*
2617 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2618 * The file name with the full path is also remembered, for when :cd is used.
2619 * Returns FAIL for failure (file name already in use by other buffer)
2620 * OK otherwise.
2621 */
2622 int
2623setfname(buf, ffname, sfname, message)
2624 buf_T *buf;
2625 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002626 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627{
Bram Moolenaar81695252004-12-29 20:58:21 +00002628 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629#ifdef UNIX
2630 struct stat st;
2631#endif
2632
2633 if (ffname == NULL || *ffname == NUL)
2634 {
2635 /* Removing the name. */
2636 vim_free(buf->b_ffname);
2637 vim_free(buf->b_sfname);
2638 buf->b_ffname = NULL;
2639 buf->b_sfname = NULL;
2640#ifdef UNIX
2641 st.st_dev = (dev_T)-1;
2642#endif
2643 }
2644 else
2645 {
2646 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2647 if (ffname == NULL) /* out of memory */
2648 return FAIL;
2649
2650 /*
2651 * if the file name is already used in another buffer:
2652 * - if the buffer is loaded, fail
2653 * - if the buffer is not loaded, delete it from the list
2654 */
2655#ifdef UNIX
2656 if (mch_stat((char *)ffname, &st) < 0)
2657 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002658#endif
2659 if (!(buf->b_flags & BF_DUMMY))
2660#ifdef UNIX
2661 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002663 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664#endif
2665 if (obuf != NULL && obuf != buf)
2666 {
2667 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2668 {
2669 if (message)
2670 EMSG(_("E95: Buffer with this name already exists"));
2671 vim_free(ffname);
2672 return FAIL;
2673 }
2674 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2675 }
2676 sfname = vim_strsave(sfname);
2677 if (ffname == NULL || sfname == NULL)
2678 {
2679 vim_free(sfname);
2680 vim_free(ffname);
2681 return FAIL;
2682 }
2683#ifdef USE_FNAME_CASE
2684# ifdef USE_LONG_FNAME
2685 if (USE_LONG_FNAME)
2686# endif
2687 fname_case(sfname, 0); /* set correct case for short file name */
2688#endif
2689 vim_free(buf->b_ffname);
2690 vim_free(buf->b_sfname);
2691 buf->b_ffname = ffname;
2692 buf->b_sfname = sfname;
2693 }
2694 buf->b_fname = buf->b_sfname;
2695#ifdef UNIX
2696 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002697 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 else
2699 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002700 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 buf->b_dev = st.st_dev;
2702 buf->b_ino = st.st_ino;
2703 }
2704#endif
2705
2706#ifndef SHORT_FNAME
2707 buf->b_shortname = FALSE;
2708#endif
2709
2710 buf_name_changed(buf);
2711 return OK;
2712}
2713
2714/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002715 * Crude way of changing the name of a buffer. Use with care!
2716 * The name should be relative to the current directory.
2717 */
2718 void
2719buf_set_name(fnum, name)
2720 int fnum;
2721 char_u *name;
2722{
2723 buf_T *buf;
2724
2725 buf = buflist_findnr(fnum);
2726 if (buf != NULL)
2727 {
2728 vim_free(buf->b_sfname);
2729 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002730 buf->b_ffname = vim_strsave(name);
2731 buf->b_sfname = NULL;
2732 /* Allocate ffname and expand into full path. Also resolves .lnk
2733 * files on Win32. */
2734 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002735 buf->b_fname = buf->b_sfname;
2736 }
2737}
2738
2739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 * Take care of what needs to be done when the name of buffer "buf" has
2741 * changed.
2742 */
2743 void
2744buf_name_changed(buf)
2745 buf_T *buf;
2746{
2747 /*
2748 * If the file name changed, also change the name of the swapfile
2749 */
2750 if (buf->b_ml.ml_mfp != NULL)
2751 ml_setname(buf);
2752
2753 if (curwin->w_buffer == buf)
2754 check_arg_idx(curwin); /* check file name for arg list */
2755#ifdef FEAT_TITLE
2756 maketitle(); /* set window title */
2757#endif
2758#ifdef FEAT_WINDOWS
2759 status_redraw_all(); /* status lines need to be redrawn */
2760#endif
2761 fmarks_check_names(buf); /* check named file marks */
2762 ml_timestamp(buf); /* reset timestamp */
2763}
2764
2765/*
2766 * set alternate file name for current window
2767 *
2768 * Used by do_one_cmd(), do_write() and do_ecmd().
2769 * Return the buffer.
2770 */
2771 buf_T *
2772setaltfname(ffname, sfname, lnum)
2773 char_u *ffname;
2774 char_u *sfname;
2775 linenr_T lnum;
2776{
2777 buf_T *buf;
2778
2779 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2780 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002781 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 curwin->w_alt_fnum = buf->b_fnum;
2783 return buf;
2784}
2785
2786/*
2787 * Get alternate file name for current window.
2788 * Return NULL if there isn't any, and give error message if requested.
2789 */
2790 char_u *
2791getaltfname(errmsg)
2792 int errmsg; /* give error message */
2793{
2794 char_u *fname;
2795 linenr_T dummy;
2796
2797 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2798 {
2799 if (errmsg)
2800 EMSG(_(e_noalt));
2801 return NULL;
2802 }
2803 return fname;
2804}
2805
2806/*
2807 * Add a file name to the buflist and return its number.
2808 * Uses same flags as buflist_new(), except BLN_DUMMY.
2809 *
2810 * used by qf_init(), main() and doarglist()
2811 */
2812 int
2813buflist_add(fname, flags)
2814 char_u *fname;
2815 int flags;
2816{
2817 buf_T *buf;
2818
2819 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2820 if (buf != NULL)
2821 return buf->b_fnum;
2822 return 0;
2823}
2824
2825#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2826/*
2827 * Adjust slashes in file names. Called after 'shellslash' was set.
2828 */
2829 void
2830buflist_slash_adjust()
2831{
2832 buf_T *bp;
2833
2834 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2835 {
2836 if (bp->b_ffname != NULL)
2837 slash_adjust(bp->b_ffname);
2838 if (bp->b_sfname != NULL)
2839 slash_adjust(bp->b_sfname);
2840 }
2841}
2842#endif
2843
2844/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002845 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 * Also save the local window option values.
2847 */
2848 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002849buflist_altfpos(win)
2850 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002852 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853}
2854
2855/*
2856 * Return TRUE if 'ffname' is not the same file as current file.
2857 * Fname must have a full path (expanded by mch_FullName()).
2858 */
2859 int
2860otherfile(ffname)
2861 char_u *ffname;
2862{
2863 return otherfile_buf(curbuf, ffname
2864#ifdef UNIX
2865 , NULL
2866#endif
2867 );
2868}
2869
2870 static int
2871otherfile_buf(buf, ffname
2872#ifdef UNIX
2873 , stp
2874#endif
2875 )
2876 buf_T *buf;
2877 char_u *ffname;
2878#ifdef UNIX
2879 struct stat *stp;
2880#endif
2881{
2882 /* no name is different */
2883 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2884 return TRUE;
2885 if (fnamecmp(ffname, buf->b_ffname) == 0)
2886 return FALSE;
2887#ifdef UNIX
2888 {
2889 struct stat st;
2890
2891 /* If no struct stat given, get it now */
2892 if (stp == NULL)
2893 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002894 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 st.st_dev = (dev_T)-1;
2896 stp = &st;
2897 }
2898 /* Use dev/ino to check if the files are the same, even when the names
2899 * are different (possible with links). Still need to compare the
2900 * name above, for when the file doesn't exist yet.
2901 * Problem: The dev/ino changes when a file is deleted (and created
2902 * again) and remains the same when renamed/moved. We don't want to
2903 * mch_stat() each buffer each time, that would be too slow. Get the
2904 * dev/ino again when they appear to match, but not when they appear
2905 * to be different: Could skip a buffer when it's actually the same
2906 * file. */
2907 if (buf_same_ino(buf, stp))
2908 {
2909 buf_setino(buf);
2910 if (buf_same_ino(buf, stp))
2911 return FALSE;
2912 }
2913 }
2914#endif
2915 return TRUE;
2916}
2917
2918#if defined(UNIX) || defined(PROTO)
2919/*
2920 * Set inode and device number for a buffer.
2921 * Must always be called when b_fname is changed!.
2922 */
2923 void
2924buf_setino(buf)
2925 buf_T *buf;
2926{
2927 struct stat st;
2928
2929 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2930 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002931 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 buf->b_dev = st.st_dev;
2933 buf->b_ino = st.st_ino;
2934 }
2935 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002936 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937}
2938
2939/*
2940 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2941 */
2942 static int
2943buf_same_ino(buf, stp)
2944 buf_T *buf;
2945 struct stat *stp;
2946{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002947 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 && stp->st_dev == buf->b_dev
2949 && stp->st_ino == buf->b_ino);
2950}
2951#endif
2952
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002953/*
2954 * Print info about the current buffer.
2955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 void
2957fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002958 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 int shorthelp;
2960 int dont_truncate;
2961{
2962 char_u *name;
2963 int n;
2964 char_u *p;
2965 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002966 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967
2968 buffer = alloc(IOSIZE);
2969 if (buffer == NULL)
2970 return;
2971
2972 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2973 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002974 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 p = buffer + STRLEN(buffer);
2976 }
2977 else
2978 p = buffer;
2979
2980 *p++ = '"';
2981 if (buf_spname(curbuf) != NULL)
2982 STRCPY(p, buf_spname(curbuf));
2983 else
2984 {
2985 if (!fullname && curbuf->b_fname != NULL)
2986 name = curbuf->b_fname;
2987 else
2988 name = curbuf->b_ffname;
2989 home_replace(shorthelp ? curbuf : NULL, name, p,
2990 (int)(IOSIZE - (p - buffer)), TRUE);
2991 }
2992
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002993 len = STRLEN(buffer);
2994 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 "\"%s%s%s%s%s%s",
2996 curbufIsChanged() ? (shortmess(SHM_MOD)
2997 ? " [+]" : _(" [Modified]")) : " ",
2998 (curbuf->b_flags & BF_NOTEDITED)
2999#ifdef FEAT_QUICKFIX
3000 && !bt_dontwrite(curbuf)
3001#endif
3002 ? _("[Not edited]") : "",
3003 (curbuf->b_flags & BF_NEW)
3004#ifdef FEAT_QUICKFIX
3005 && !bt_dontwrite(curbuf)
3006#endif
3007 ? _("[New file]") : "",
3008 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3009 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3010 : _("[readonly]")) : "",
3011 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3012 || curbuf->b_p_ro) ?
3013 " " : "");
3014 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3015 * causes an overflow, thus for large numbers divide instead. */
3016 if (curwin->w_cursor.lnum > 1000000L)
3017 n = (int)(((long)curwin->w_cursor.lnum) /
3018 ((long)curbuf->b_ml.ml_line_count / 100L));
3019 else
3020 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3021 (long)curbuf->b_ml.ml_line_count);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003022 len = STRLEN(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3024 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003025 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 }
3027#ifdef FEAT_CMDL_INFO
3028 else if (p_ru)
3029 {
3030 /* Current line and column are already on the screen -- webb */
3031 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003032 vim_snprintf((char *)buffer + len, IOSIZE - len,
3033 _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003035 vim_snprintf((char *)buffer + len, IOSIZE - len,
3036 _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 (long)curbuf->b_ml.ml_line_count, n);
3038 }
3039#endif
3040 else
3041 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003042 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 _("line %ld of %ld --%d%%-- col "),
3044 (long)curwin->w_cursor.lnum,
3045 (long)curbuf->b_ml.ml_line_count,
3046 n);
3047 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003048 len = STRLEN(buffer);
3049 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3051 }
3052
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003053 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
3055 if (dont_truncate)
3056 {
3057 /* Temporarily set msg_scroll to avoid the message being truncated.
3058 * First call msg_start() to get the message in the right place. */
3059 msg_start();
3060 n = msg_scroll;
3061 msg_scroll = TRUE;
3062 msg(buffer);
3063 msg_scroll = n;
3064 }
3065 else
3066 {
3067 p = msg_trunc_attr(buffer, FALSE, 0);
3068 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 /* Need to repeat the message after redrawing when:
3070 * - When restart_edit is set (otherwise there will be a delay
3071 * before redrawing).
3072 * - When the screen was scrolled but there is no wait-return
3073 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003074 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 }
3076
3077 vim_free(buffer);
3078}
3079
3080 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003081col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003083 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 int col;
3085 int vcol;
3086{
3087 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003088 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003090 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091}
3092
3093#if defined(FEAT_TITLE) || defined(PROTO)
3094/*
3095 * put file name in title bar of window and in icon title
3096 */
3097
3098static char_u *lasttitle = NULL;
3099static char_u *lasticon = NULL;
3100
3101 void
3102maketitle()
3103{
3104 char_u *p;
3105 char_u *t_str = NULL;
3106 char_u *i_name;
3107 char_u *i_str = NULL;
3108 int maxlen = 0;
3109 int len;
3110 int mustset;
3111 char_u buf[IOSIZE];
3112 int off;
3113
3114 if (!redrawing())
3115 {
3116 /* Postpone updating the title when 'lazyredraw' is set. */
3117 need_maketitle = TRUE;
3118 return;
3119 }
3120
3121 need_maketitle = FALSE;
3122 if (!p_title && !p_icon)
3123 return;
3124
3125 if (p_title)
3126 {
3127 if (p_titlelen > 0)
3128 {
3129 maxlen = p_titlelen * Columns / 100;
3130 if (maxlen < 10)
3131 maxlen = 10;
3132 }
3133
3134 t_str = buf;
3135 if (*p_titlestring != NUL)
3136 {
3137#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003138 if (stl_syntax & STL_IN_TITLE)
3139 {
3140 int use_sandbox = FALSE;
3141 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003142
3143# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003144 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003145# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003146 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003148 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003149 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003150 if (called_emsg)
3151 set_string_option_direct((char_u *)"titlestring", -1,
3152 (char_u *)"", OPT_FREE, SID_ERROR);
3153 called_emsg |= save_called_emsg;
3154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 else
3156#endif
3157 t_str = p_titlestring;
3158 }
3159 else
3160 {
3161 /* format: "fname + (path) (1 of 2) - VIM" */
3162
3163 if (curbuf->b_fname == NULL)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003164 STRCPY(buf, _("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 else
3166 {
3167 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003168 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 }
3171
3172 switch (bufIsChanged(curbuf)
3173 + (curbuf->b_p_ro * 2)
3174 + (!curbuf->b_p_ma * 4))
3175 {
3176 case 1: STRCAT(buf, " +"); break;
3177 case 2: STRCAT(buf, " ="); break;
3178 case 3: STRCAT(buf, " =+"); break;
3179 case 4:
3180 case 6: STRCAT(buf, " -"); break;
3181 case 5:
3182 case 7: STRCAT(buf, " -+"); break;
3183 }
3184
3185 if (curbuf->b_fname != NULL)
3186 {
3187 /* Get path of file, replace home dir with ~ */
3188 off = (int)STRLEN(buf);
3189 buf[off++] = ' ';
3190 buf[off++] = '(';
3191 home_replace(curbuf, curbuf->b_ffname,
3192 buf + off, IOSIZE - off, TRUE);
3193#ifdef BACKSLASH_IN_FILENAME
3194 /* avoid "c:/name" to be reduced to "c" */
3195 if (isalpha(buf[off]) && buf[off + 1] == ':')
3196 off += 2;
3197#endif
3198 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003199 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003202 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003203 (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003206
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 /* translate unprintable chars */
3208 p = transstr(buf + off);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003209 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 STRCAT(buf, ")");
3212 }
3213
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003214 append_arg_number(curwin, buf, IOSIZE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
3216#if defined(FEAT_CLIENTSERVER)
3217 if (serverName != NULL)
3218 {
3219 STRCAT(buf, " - ");
3220 STRCAT(buf, serverName);
3221 }
3222 else
3223#endif
3224 STRCAT(buf, " - VIM");
3225
3226 if (maxlen > 0)
3227 {
3228 /* make it shorter by removing a bit in the middle */
3229 len = vim_strsize(buf);
3230 if (len > maxlen)
3231 trunc_string(buf, buf, maxlen);
3232 }
3233 }
3234 }
3235 mustset = ti_change(t_str, &lasttitle);
3236
3237 if (p_icon)
3238 {
3239 i_str = buf;
3240 if (*p_iconstring != NUL)
3241 {
3242#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003243 if (stl_syntax & STL_IN_ICON)
3244 {
3245 int use_sandbox = FALSE;
3246 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003247
3248# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003249 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003250# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003251 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003253 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003254 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003255 if (called_emsg)
3256 set_string_option_direct((char_u *)"iconstring", -1,
3257 (char_u *)"", OPT_FREE, SID_ERROR);
3258 called_emsg |= save_called_emsg;
3259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 else
3261#endif
3262 i_str = p_iconstring;
3263 }
3264 else
3265 {
3266 if (buf_spname(curbuf) != NULL)
3267 i_name = (char_u *)buf_spname(curbuf);
3268 else /* use file name only in icon */
3269 i_name = gettail(curbuf->b_ffname);
3270 *i_str = NUL;
3271 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003272 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 if (len > 100)
3274 {
3275 len -= 100;
3276#ifdef FEAT_MBYTE
3277 if (has_mbyte)
3278 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3279#endif
3280 i_name += len;
3281 }
3282 STRCPY(i_str, i_name);
3283 trans_characters(i_str, IOSIZE);
3284 }
3285 }
3286
3287 mustset |= ti_change(i_str, &lasticon);
3288
3289 if (mustset)
3290 resettitle();
3291}
3292
3293/*
3294 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3295 * from "str" if it does.
3296 * Return TRUE when "*last" changed.
3297 */
3298 static int
3299ti_change(str, last)
3300 char_u *str;
3301 char_u **last;
3302{
3303 if ((str == NULL) != (*last == NULL)
3304 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3305 {
3306 vim_free(*last);
3307 if (str == NULL)
3308 *last = NULL;
3309 else
3310 *last = vim_strsave(str);
3311 return TRUE;
3312 }
3313 return FALSE;
3314}
3315
3316/*
3317 * Put current window title back (used after calling a shell)
3318 */
3319 void
3320resettitle()
3321{
3322 mch_settitle(lasttitle, lasticon);
3323}
Bram Moolenaarea408852005-06-25 22:49:46 +00003324
3325# if defined(EXITFREE) || defined(PROTO)
3326 void
3327free_titles()
3328{
3329 vim_free(lasttitle);
3330 vim_free(lasticon);
3331}
3332# endif
3333
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334#endif /* FEAT_TITLE */
3335
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003336#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003338 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 * Return length of string in screen cells.
3340 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003341 * Normally works for window "wp", except when working for 'tabline' then it
3342 * is "curwin".
3343 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 * Items are drawn interspersed with the text that surrounds it
3345 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3346 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3347 *
3348 * If maxwidth is not zero, the string will be filled at any middle marker
3349 * or truncated if too long, fillchar is used for all whitespace.
3350 */
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003351/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003353build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003355 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 size_t outlen; /* length of out[] */
3357 char_u *fmt;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003358 int use_sandbox; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 int fillchar;
3360 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003361 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3362 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
3364 char_u *p;
3365 char_u *s;
3366 char_u *t;
3367 char_u *linecont;
3368#ifdef FEAT_EVAL
3369 win_T *o_curwin;
3370 buf_T *o_curbuf;
3371#endif
3372 int empty_line;
3373 colnr_T virtcol;
3374 long l;
3375 long n;
3376 int prevchar_isflag;
3377 int prevchar_isitem;
3378 int itemisflag;
3379 int fillable;
3380 char_u *str;
3381 long num;
3382 int width;
3383 int itemcnt;
3384 int curitem;
3385 int groupitem[STL_MAX_ITEM];
3386 int groupdepth;
3387 struct stl_item
3388 {
3389 char_u *start;
3390 int minwid;
3391 int maxwid;
3392 enum
3393 {
3394 Normal,
3395 Empty,
3396 Group,
3397 Middle,
3398 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003399 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 Trunc
3401 } type;
3402 } item[STL_MAX_ITEM];
3403 int minwid;
3404 int maxwid;
3405 int zeropad;
3406 char_u base;
3407 char_u opt;
3408#define TMPLEN 70
3409 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003410 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003411 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003412
3413#ifdef FEAT_EVAL
3414 /*
3415 * When the format starts with "%!" then evaluate it as an expression and
3416 * use the result as the actual format string.
3417 */
3418 if (fmt[0] == '%' && fmt[1] == '!')
3419 {
3420 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3421 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003422 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003423 }
3424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
3426 if (fillchar == 0)
3427 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003428#ifdef FEAT_MBYTE
3429 /* Can't handle a multi-byte fill character yet. */
3430 else if (mb_char2len(fillchar) > 1)
3431 fillchar = '-';
3432#endif
3433
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 /*
3435 * Get line & check if empty (cursorpos will show "0-1").
3436 * If inversion is possible we use it. Else '=' characters are used.
3437 */
3438 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3439 empty_line = (*linecont == NUL);
3440
3441 groupdepth = 0;
3442 p = out;
3443 curitem = 0;
3444 prevchar_isflag = TRUE;
3445 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003446 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
3448 if (*s != NUL && *s != '%')
3449 prevchar_isflag = prevchar_isitem = FALSE;
3450
3451 /*
3452 * Handle up to the next '%' or the end.
3453 */
3454 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3455 *p++ = *s++;
3456 if (*s == NUL || p + 1 >= out + outlen)
3457 break;
3458
3459 /*
3460 * Handle one '%' item.
3461 */
3462 s++;
3463 if (*s == '%')
3464 {
3465 if (p + 1 >= out + outlen)
3466 break;
3467 *p++ = *s++;
3468 prevchar_isflag = prevchar_isitem = FALSE;
3469 continue;
3470 }
3471 if (*s == STL_MIDDLEMARK)
3472 {
3473 s++;
3474 if (groupdepth > 0)
3475 continue;
3476 item[curitem].type = Middle;
3477 item[curitem++].start = p;
3478 continue;
3479 }
3480 if (*s == STL_TRUNCMARK)
3481 {
3482 s++;
3483 item[curitem].type = Trunc;
3484 item[curitem++].start = p;
3485 continue;
3486 }
3487 if (*s == ')')
3488 {
3489 s++;
3490 if (groupdepth < 1)
3491 continue;
3492 groupdepth--;
3493
3494 t = item[groupitem[groupdepth]].start;
3495 *p = NUL;
3496 l = vim_strsize(t);
3497 if (curitem > groupitem[groupdepth] + 1
3498 && item[groupitem[groupdepth]].minwid == 0)
3499 {
3500 /* remove group if all items are empty */
3501 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3502 if (item[n].type == Normal)
3503 break;
3504 if (n == curitem)
3505 {
3506 p = t;
3507 l = 0;
3508 }
3509 }
3510 if (l > item[groupitem[groupdepth]].maxwid)
3511 {
3512 /* truncate, remove n bytes of text at the start */
3513#ifdef FEAT_MBYTE
3514 if (has_mbyte)
3515 {
3516 /* Find the first character that should be included. */
3517 n = 0;
3518 while (l >= item[groupitem[groupdepth]].maxwid)
3519 {
3520 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003521 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523 }
3524 else
3525#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003526 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
3528 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003529 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 p = p - n + 1;
3531#ifdef FEAT_MBYTE
3532 /* Fill up space left over by half a double-wide char. */
3533 while (++l < item[groupitem[groupdepth]].minwid)
3534 *p++ = fillchar;
3535#endif
3536
3537 /* correct the start of the items for the truncation */
3538 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3539 {
3540 item[l].start -= n;
3541 if (item[l].start < t)
3542 item[l].start = t;
3543 }
3544 }
3545 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3546 {
3547 /* fill */
3548 n = item[groupitem[groupdepth]].minwid;
3549 if (n < 0)
3550 {
3551 /* fill by appending characters */
3552 n = 0 - n;
3553 while (l++ < n && p + 1 < out + outlen)
3554 *p++ = fillchar;
3555 }
3556 else
3557 {
3558 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003559 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 l = n - l;
3561 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003562 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 p += l;
3564 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3565 item[n].start += l;
3566 for ( ; l > 0; l--)
3567 *t++ = fillchar;
3568 }
3569 }
3570 continue;
3571 }
3572 minwid = 0;
3573 maxwid = 9999;
3574 zeropad = FALSE;
3575 l = 1;
3576 if (*s == '0')
3577 {
3578 s++;
3579 zeropad = TRUE;
3580 }
3581 if (*s == '-')
3582 {
3583 s++;
3584 l = -1;
3585 }
3586 if (VIM_ISDIGIT(*s))
3587 {
3588 minwid = (int)getdigits(&s);
3589 if (minwid < 0) /* overflow */
3590 minwid = 0;
3591 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003592 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
3594 item[curitem].type = Highlight;
3595 item[curitem].start = p;
3596 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3597 s++;
3598 curitem++;
3599 continue;
3600 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003601 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3602 {
3603 if (*s == STL_TABCLOSENR)
3604 {
3605 if (minwid == 0)
3606 {
3607 /* %X ends the close label, go back to the previously
3608 * define tab label nr. */
3609 for (n = curitem - 1; n >= 0; --n)
3610 if (item[n].type == TabPage && item[n].minwid >= 0)
3611 {
3612 minwid = item[n].minwid;
3613 break;
3614 }
3615 }
3616 else
3617 /* close nrs are stored as negative values */
3618 minwid = - minwid;
3619 }
3620 item[curitem].type = TabPage;
3621 item[curitem].start = p;
3622 item[curitem].minwid = minwid;
3623 s++;
3624 curitem++;
3625 continue;
3626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 if (*s == '.')
3628 {
3629 s++;
3630 if (VIM_ISDIGIT(*s))
3631 {
3632 maxwid = (int)getdigits(&s);
3633 if (maxwid <= 0) /* overflow */
3634 maxwid = 50;
3635 }
3636 }
3637 minwid = (minwid > 50 ? 50 : minwid) * l;
3638 if (*s == '(')
3639 {
3640 groupitem[groupdepth++] = curitem;
3641 item[curitem].type = Group;
3642 item[curitem].start = p;
3643 item[curitem].minwid = minwid;
3644 item[curitem].maxwid = maxwid;
3645 s++;
3646 curitem++;
3647 continue;
3648 }
3649 if (vim_strchr(STL_ALL, *s) == NULL)
3650 {
3651 s++;
3652 continue;
3653 }
3654 opt = *s++;
3655
3656 /* OK - now for the real work */
3657 base = 'D';
3658 itemisflag = FALSE;
3659 fillable = TRUE;
3660 num = -1;
3661 str = NULL;
3662 switch (opt)
3663 {
3664 case STL_FILEPATH:
3665 case STL_FULLPATH:
3666 case STL_FILENAME:
3667 fillable = FALSE; /* don't change ' ' to fillchar */
3668 if (buf_spname(wp->w_buffer) != NULL)
3669 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3670 else
3671 {
3672 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003673 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3675 }
3676 trans_characters(NameBuff, MAXPATHL);
3677 if (opt != STL_FILENAME)
3678 str = NameBuff;
3679 else
3680 str = gettail(NameBuff);
3681 break;
3682
3683 case STL_VIM_EXPR: /* '{' */
3684 itemisflag = TRUE;
3685 t = p;
3686 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3687 *p++ = *s++;
3688 if (*s != '}') /* missing '}' or out of space */
3689 break;
3690 s++;
3691 *p = 0;
3692 p = t;
3693
3694#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003695 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3697
3698 o_curbuf = curbuf;
3699 o_curwin = curwin;
3700 curwin = wp;
3701 curbuf = wp->w_buffer;
3702
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003703 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705 curwin = o_curwin;
3706 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003707 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
3709 if (str != NULL && *str != 0)
3710 {
3711 if (*skipdigits(str) == NUL)
3712 {
3713 num = atoi((char *)str);
3714 vim_free(str);
3715 str = NULL;
3716 itemisflag = FALSE;
3717 }
3718 }
3719#endif
3720 break;
3721
3722 case STL_LINE:
3723 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3724 ? 0L : (long)(wp->w_cursor.lnum);
3725 break;
3726
3727 case STL_NUMLINES:
3728 num = wp->w_buffer->b_ml.ml_line_count;
3729 break;
3730
3731 case STL_COLUMN:
3732 num = !(State & INSERT) && empty_line
3733 ? 0 : (int)wp->w_cursor.col + 1;
3734 break;
3735
3736 case STL_VIRTCOL:
3737 case STL_VIRTCOL_ALT:
3738 /* In list mode virtcol needs to be recomputed */
3739 virtcol = wp->w_virtcol;
3740 if (wp->w_p_list && lcs_tab1 == NUL)
3741 {
3742 wp->w_p_list = FALSE;
3743 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3744 wp->w_p_list = TRUE;
3745 }
3746 ++virtcol;
3747 /* Don't display %V if it's the same as %c. */
3748 if (opt == STL_VIRTCOL_ALT
3749 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3750 ? 0 : (int)wp->w_cursor.col + 1)))
3751 break;
3752 num = (long)virtcol;
3753 break;
3754
3755 case STL_PERCENTAGE:
3756 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3757 (long)wp->w_buffer->b_ml.ml_line_count);
3758 break;
3759
3760 case STL_ALTPERCENT:
3761 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003762 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 break;
3764
3765 case STL_ARGLISTSTAT:
3766 fillable = FALSE;
3767 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003768 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 str = tmp;
3770 break;
3771
3772 case STL_KEYMAP:
3773 fillable = FALSE;
3774 if (get_keymap_str(wp, tmp, TMPLEN))
3775 str = tmp;
3776 break;
3777 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003778#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003779 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780#else
3781 num = 0;
3782#endif
3783 break;
3784
3785 case STL_BUFNO:
3786 num = wp->w_buffer->b_fnum;
3787 break;
3788
3789 case STL_OFFSET_X:
3790 base = 'X';
3791 case STL_OFFSET:
3792#ifdef FEAT_BYTEOFF
3793 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3794 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3795 0L : l + 1 + (!(State & INSERT) && empty_line ?
3796 0 : (int)wp->w_cursor.col);
3797#endif
3798 break;
3799
3800 case STL_BYTEVAL_X:
3801 base = 'X';
3802 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003803 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 num = 0;
3805 else
3806 {
3807#ifdef FEAT_MBYTE
3808 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3809#else
3810 num = linecont[wp->w_cursor.col];
3811#endif
3812 }
3813 if (num == NL)
3814 num = 0;
3815 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3816 num = NL;
3817 break;
3818
3819 case STL_ROFLAG:
3820 case STL_ROFLAG_ALT:
3821 itemisflag = TRUE;
3822 if (wp->w_buffer->b_p_ro)
3823 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3824 break;
3825
3826 case STL_HELPFLAG:
3827 case STL_HELPFLAG_ALT:
3828 itemisflag = TRUE;
3829 if (wp->w_buffer->b_help)
3830 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003831 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 break;
3833
3834#ifdef FEAT_AUTOCMD
3835 case STL_FILETYPE:
3836 if (*wp->w_buffer->b_p_ft != NUL
3837 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3838 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003839 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3840 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 str = tmp;
3842 }
3843 break;
3844
3845 case STL_FILETYPE_ALT:
3846 itemisflag = TRUE;
3847 if (*wp->w_buffer->b_p_ft != NUL
3848 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3849 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003850 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3851 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 for (t = tmp; *t != 0; t++)
3853 *t = TOUPPER_LOC(*t);
3854 str = tmp;
3855 }
3856 break;
3857#endif
3858
3859#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3860 case STL_PREVIEWFLAG:
3861 case STL_PREVIEWFLAG_ALT:
3862 itemisflag = TRUE;
3863 if (wp->w_p_pvw)
3864 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3865 : _("[Preview]"));
3866 break;
3867#endif
3868
3869 case STL_MODIFIED:
3870 case STL_MODIFIED_ALT:
3871 itemisflag = TRUE;
3872 switch ((opt == STL_MODIFIED_ALT)
3873 + bufIsChanged(wp->w_buffer) * 2
3874 + (!wp->w_buffer->b_p_ma) * 4)
3875 {
3876 case 2: str = (char_u *)"[+]"; break;
3877 case 3: str = (char_u *)",+"; break;
3878 case 4: str = (char_u *)"[-]"; break;
3879 case 5: str = (char_u *)",-"; break;
3880 case 6: str = (char_u *)"[+-]"; break;
3881 case 7: str = (char_u *)",+-"; break;
3882 }
3883 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003884
3885 case STL_HIGHLIGHT:
3886 t = s;
3887 while (*s != '#' && *s != NUL)
3888 ++s;
3889 if (*s == '#')
3890 {
3891 item[curitem].type = Highlight;
3892 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003893 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003894 curitem++;
3895 }
3896 ++s;
3897 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
3899
3900 item[curitem].start = p;
3901 item[curitem].type = Normal;
3902 if (str != NULL && *str)
3903 {
3904 t = str;
3905 if (itemisflag)
3906 {
3907 if ((t[0] && t[1])
3908 && ((!prevchar_isitem && *t == ',')
3909 || (prevchar_isflag && *t == ' ')))
3910 t++;
3911 prevchar_isflag = TRUE;
3912 }
3913 l = vim_strsize(t);
3914 if (l > 0)
3915 prevchar_isitem = TRUE;
3916 if (l > maxwid)
3917 {
3918 while (l >= maxwid)
3919#ifdef FEAT_MBYTE
3920 if (has_mbyte)
3921 {
3922 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003923 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 }
3925 else
3926#endif
3927 l -= byte2cells(*t++);
3928 if (p + 1 >= out + outlen)
3929 break;
3930 *p++ = '<';
3931 }
3932 if (minwid > 0)
3933 {
3934 for (; l < minwid && p + 1 < out + outlen; l++)
3935 {
3936 /* Don't put a "-" in front of a digit. */
3937 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3938 *p++ = ' ';
3939 else
3940 *p++ = fillchar;
3941 }
3942 minwid = 0;
3943 }
3944 else
3945 minwid *= -1;
3946 while (*t && p + 1 < out + outlen)
3947 {
3948 *p++ = *t++;
3949 /* Change a space by fillchar, unless fillchar is '-' and a
3950 * digit follows. */
3951 if (fillable && p[-1] == ' '
3952 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3953 p[-1] = fillchar;
3954 }
3955 for (; l < minwid && p + 1 < out + outlen; l++)
3956 *p++ = fillchar;
3957 }
3958 else if (num >= 0)
3959 {
3960 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3961 char_u nstr[20];
3962
3963 if (p + 20 >= out + outlen)
3964 break; /* not sufficient space */
3965 prevchar_isitem = TRUE;
3966 t = nstr;
3967 if (opt == STL_VIRTCOL_ALT)
3968 {
3969 *t++ = '-';
3970 minwid--;
3971 }
3972 *t++ = '%';
3973 if (zeropad)
3974 *t++ = '0';
3975 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003976 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 *t = 0;
3978
3979 for (n = num, l = 1; n >= nbase; n /= nbase)
3980 l++;
3981 if (opt == STL_VIRTCOL_ALT)
3982 l++;
3983 if (l > maxwid)
3984 {
3985 l += 2;
3986 n = l - maxwid;
3987 while (l-- > maxwid)
3988 num /= nbase;
3989 *t++ = '>';
3990 *t++ = '%';
3991 *t = t[-3];
3992 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003993 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3994 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 }
3996 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003997 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3998 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 p += STRLEN(p);
4000 }
4001 else
4002 item[curitem].type = Empty;
4003
4004 if (opt == STL_VIM_EXPR)
4005 vim_free(str);
4006
4007 if (num >= 0 || (!itemisflag && str && *str))
4008 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4009 curitem++;
4010 }
4011 *p = NUL;
4012 itemcnt = curitem;
4013
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004014#ifdef FEAT_EVAL
4015 if (usefmt != fmt)
4016 vim_free(usefmt);
4017#endif
4018
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 width = vim_strsize(out);
4020 if (maxwidth > 0 && width > maxwidth)
4021 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004022 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 l = 0;
4024 if (itemcnt == 0)
4025 s = out;
4026 else
4027 {
4028 for ( ; l < itemcnt; l++)
4029 if (item[l].type == Trunc)
4030 {
4031 /* Truncate at %< item. */
4032 s = item[l].start;
4033 break;
4034 }
4035 if (l == itemcnt)
4036 {
4037 /* No %< item, truncate first item. */
4038 s = item[0].start;
4039 l = 0;
4040 }
4041 }
4042
4043 if (width - vim_strsize(s) >= maxwidth)
4044 {
4045 /* Truncation mark is beyond max length */
4046#ifdef FEAT_MBYTE
4047 if (has_mbyte)
4048 {
4049 s = out;
4050 width = 0;
4051 for (;;)
4052 {
4053 width += ptr2cells(s);
4054 if (width >= maxwidth)
4055 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004056 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
4058 /* Fill up for half a double-wide character. */
4059 while (++width < maxwidth)
4060 *s++ = fillchar;
4061 }
4062 else
4063#endif
4064 s = out + maxwidth - 1;
4065 for (l = 0; l < itemcnt; l++)
4066 if (item[l].start > s)
4067 break;
4068 itemcnt = l;
4069 *s++ = '>';
4070 *s = 0;
4071 }
4072 else
4073 {
4074#ifdef FEAT_MBYTE
4075 if (has_mbyte)
4076 {
4077 n = 0;
4078 while (width >= maxwidth)
4079 {
4080 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004081 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
4083 }
4084 else
4085#endif
4086 n = width - maxwidth + 1;
4087 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004088 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 *s = '<';
4090
4091 /* Fill up for half a double-wide character. */
4092 while (++width < maxwidth)
4093 {
4094 s = s + STRLEN(s);
4095 *s++ = fillchar;
4096 *s = NUL;
4097 }
4098
4099 --n; /* count the '<' */
4100 for (; l < itemcnt; l++)
4101 {
4102 if (item[l].start - n >= s)
4103 item[l].start -= n;
4104 else
4105 item[l].start = s;
4106 }
4107 }
4108 width = maxwidth;
4109 }
4110 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4111 {
4112 /* Apply STL_MIDDLE if any */
4113 for (l = 0; l < itemcnt; l++)
4114 if (item[l].type == Middle)
4115 break;
4116 if (l < itemcnt)
4117 {
4118 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004119 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 for (s = item[l].start; s < p; s++)
4121 *s = fillchar;
4122 for (l++; l < itemcnt; l++)
4123 item[l].start += maxwidth - width;
4124 width = maxwidth;
4125 }
4126 }
4127
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004128 /* Store the info about highlighting. */
4129 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004131 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 for (l = 0; l < itemcnt; l++)
4133 {
4134 if (item[l].type == Highlight)
4135 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004136 sp->start = item[l].start;
4137 sp->userhl = item[l].minwid;
4138 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004141 sp->start = NULL;
4142 sp->userhl = 0;
4143 }
4144
4145 /* Store the info about tab pages labels. */
4146 if (tabtab != NULL)
4147 {
4148 sp = tabtab;
4149 for (l = 0; l < itemcnt; l++)
4150 {
4151 if (item[l].type == TabPage)
4152 {
4153 sp->start = item[l].start;
4154 sp->userhl = item[l].minwid;
4155 sp++;
4156 }
4157 }
4158 sp->start = NULL;
4159 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 }
4161
4162 return width;
4163}
4164#endif /* FEAT_STL_OPT */
4165
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004166#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4167 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004169 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4170 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 */
4172 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004173get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004175 char_u *buf;
4176 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177{
4178 long above; /* number of lines above window */
4179 long below; /* number of lines below window */
4180
4181 above = wp->w_topline - 1;
4182#ifdef FEAT_DIFF
4183 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4184#endif
4185 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4186 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004187 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4188 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004190 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004192 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 ? (int)(above / ((above + below) / 100L))
4194 : (int)(above * 100L / (above + below)));
4195}
4196#endif
4197
4198/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004199 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 * Return TRUE if it was appended.
4201 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004202 static int
4203append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 win_T *wp;
4205 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004206 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208{
4209 char_u *p;
4210
4211 if (ARGCOUNT <= 1) /* nothing to do */
4212 return FALSE;
4213
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004214 p = buf + STRLEN(buf); /* go to the end of the buffer */
4215 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 return FALSE;
4217 *p++ = ' ';
4218 *p++ = '(';
4219 if (add_file)
4220 {
4221 STRCPY(p, "file ");
4222 p += 5;
4223 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004224 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4225 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4227 return TRUE;
4228}
4229
4230/*
4231 * If fname is not a full path, make it a full path.
4232 * Returns pointer to allocated memory (NULL for failure).
4233 */
4234 char_u *
4235fix_fname(fname)
4236 char_u *fname;
4237{
4238 /*
4239 * Force expanding the path always for Unix, because symbolic links may
4240 * mess up the full path name, even though it starts with a '/'.
4241 * Also expand when there is ".." in the file name, try to remove it,
4242 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004243 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 * For MS-Windows also expand names like "longna~1" to "longname".
4245 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004246#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 return FullName_save(fname, TRUE);
4248#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004249 if (!vim_isAbsName(fname)
4250 || strstr((char *)fname, "..") != NULL
4251 || strstr((char *)fname, "//") != NULL
4252# ifdef BACKSLASH_IN_FILENAME
4253 || strstr((char *)fname, "\\\\") != NULL
4254# endif
4255# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 )
4259 return FullName_save(fname, FALSE);
4260
4261 fname = vim_strsave(fname);
4262
Bram Moolenaar9b942202007-10-03 12:31:33 +00004263# ifdef USE_FNAME_CASE
4264# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 {
4268 if (fname != NULL)
4269 fname_case(fname, 0); /* set correct case for file name */
4270 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
4273 return fname;
4274#endif
4275}
4276
4277/*
4278 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4279 * "ffname" becomes a pointer to allocated memory (or NULL).
4280 */
4281/*ARGSUSED*/
4282 void
4283fname_expand(buf, ffname, sfname)
4284 buf_T *buf;
4285 char_u **ffname;
4286 char_u **sfname;
4287{
4288 if (*ffname == NULL) /* if no file name given, nothing to do */
4289 return;
4290 if (*sfname == NULL) /* if no short file name given, use ffname */
4291 *sfname = *ffname;
4292 *ffname = fix_fname(*ffname); /* expand to full path */
4293
4294#ifdef FEAT_SHORTCUT
4295 if (!buf->b_p_bin)
4296 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004297 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
4299 /* If the file name is a shortcut file, use the file it links to. */
4300 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004301 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 {
4303 vim_free(*ffname);
4304 *ffname = rfname;
4305 *sfname = rfname;
4306 }
4307 }
4308#endif
4309}
4310
4311/*
4312 * Get the file name for an argument list entry.
4313 */
4314 char_u *
4315alist_name(aep)
4316 aentry_T *aep;
4317{
4318 buf_T *bp;
4319
4320 /* Use the name from the associated buffer if it exists. */
4321 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004322 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 return aep->ae_fname;
4324 return bp->b_fname;
4325}
4326
4327#if defined(FEAT_WINDOWS) || defined(PROTO)
4328/*
4329 * do_arg_all(): Open up to 'count' windows, one for each argument.
4330 */
4331 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004332do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 int count;
4334 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004335 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336{
4337 int i;
4338 win_T *wp, *wpnext;
4339 char_u *opened; /* array of flags for which args are open */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004340 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 int use_firstwin = FALSE; /* use first window for arglist */
4342 int split_ret = OK;
4343 int p_ea_save;
4344 alist_T *alist; /* argument list to be used */
4345 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004346 tabpage_T *tpnext;
4347 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004348 win_T *new_curwin = NULL;
4349 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350
4351 if (ARGCOUNT <= 0)
4352 {
4353 /* Don't give an error message. We don't want it when the ":all"
4354 * command is in the .vimrc. */
4355 return;
4356 }
4357 setpcmark();
4358
4359 opened_len = ARGCOUNT;
4360 opened = alloc_clear((unsigned)opened_len);
4361 if (opened == NULL)
4362 return;
4363
4364#ifdef FEAT_GUI
4365 need_mouse_correct = TRUE;
4366#endif
4367
4368 /*
4369 * Try closing all windows that are not in the argument list.
4370 * Also close windows that are not full width;
4371 * When 'hidden' or "forceit" set the buffer becomes hidden.
4372 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004373 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004375 if (had_tab > 0)
4376 goto_tabpage_tp(first_tabpage);
4377 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004379 tpnext = curtab->tp_next;
4380 for (wp = firstwin; wp != NULL; wp = wpnext)
4381 {
4382 wpnext = wp->w_next;
4383 buf = wp->w_buffer;
4384 if (buf->b_ffname == NULL
4385 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004387 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004389 )
4390 i = ARGCOUNT;
4391 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004393 /* check if the buffer in this window is in the arglist */
4394 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004396 if (ARGLIST[i].ae_fnum == buf->b_fnum
4397 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004398 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004400 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004401 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004402 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004403 if (i == 0)
4404 {
4405 new_curwin = wp;
4406 new_curtab = curtab;
4407 }
4408 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004409 if (wp->w_alist != curwin->w_alist)
4410 {
4411 /* Use the current argument list for all windows
4412 * containing a file from it. */
4413 alist_unlink(wp->w_alist);
4414 wp->w_alist = curwin->w_alist;
4415 ++wp->w_alist->al_refcount;
4416 }
4417 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
4420 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004421 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004423 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004425 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004426 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004428 /* If the buffer was changed, and we would like to hide it,
4429 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004430 if (!P_HID(buf) && buf->b_nwindows <= 1
4431 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004433 (void)autowrite(buf, FALSE);
4434#ifdef FEAT_AUTOCMD
4435 /* check if autocommands removed the window */
4436 if (!win_valid(wp) || !buf_valid(buf))
4437 {
4438 wpnext = firstwin; /* start all over... */
4439 continue;
4440 }
4441#endif
4442 }
4443#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004444 /* don't close last window */
4445 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004446#endif
4447 use_firstwin = TRUE;
4448#ifdef FEAT_WINDOWS
4449 else
4450 {
4451 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4452# ifdef FEAT_AUTOCMD
4453 /* check if autocommands removed the next window */
4454 if (!win_valid(wpnext))
4455 wpnext = firstwin; /* start all over... */
4456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458#endif
4459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 }
4461 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004462
4463 /* Without the ":tab" modifier only do the current tab page. */
4464 if (had_tab == 0 || tpnext == NULL)
4465 break;
4466
4467# ifdef FEAT_AUTOCMD
4468 /* check if autocommands removed the next tab page */
4469 if (!valid_tabpage(tpnext))
4470 tpnext = first_tabpage; /* start all over...*/
4471# endif
4472 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474
4475 /*
4476 * Open a window for files in the argument list that don't have one.
4477 * ARGCOUNT may change while doing this, because of autocommands.
4478 */
4479 if (count > ARGCOUNT || count <= 0)
4480 count = ARGCOUNT;
4481
4482 /* Autocommands may do anything to the argument list. Make sure it's not
4483 * freed while we are working here by "locking" it. We still have to
4484 * watch out for its size to be changed. */
4485 alist = curwin->w_alist;
4486 ++alist->al_refcount;
4487
4488#ifdef FEAT_AUTOCMD
4489 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4490 ++autocmd_no_enter;
4491 ++autocmd_no_leave;
4492#endif
4493 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004494#ifdef FEAT_WINDOWS
4495 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4496 * leaving an empty tab page when executed locally. */
4497 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4498 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4499 use_firstwin = TRUE;
4500#endif
4501
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4503 {
4504 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4505 arg_had_last = TRUE;
4506 if (i < opened_len && opened[i])
4507 {
4508 /* Move the already present window to below the current window */
4509 if (curwin->w_arg_idx != i)
4510 {
4511 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4512 {
4513 if (wpnext->w_arg_idx == i)
4514 {
4515 win_move_after(wpnext, curwin);
4516 break;
4517 }
4518 }
4519 }
4520 }
4521 else if (split_ret == OK)
4522 {
4523 if (!use_firstwin) /* split current window */
4524 {
4525 p_ea_save = p_ea;
4526 p_ea = TRUE; /* use space from all windows */
4527 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4528 p_ea = p_ea_save;
4529 if (split_ret == FAIL)
4530 continue;
4531 }
4532#ifdef FEAT_AUTOCMD
4533 else /* first window: do autocmd for leaving this buffer */
4534 --autocmd_no_leave;
4535#endif
4536
4537 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004538 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 */
4540 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004541 if (i == 0)
4542 {
4543 new_curwin = curwin;
4544 new_curtab = curtab;
4545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4547 ECMD_ONE,
4548 ((P_HID(curwin->w_buffer)
4549 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004550 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551#ifdef FEAT_AUTOCMD
4552 if (use_firstwin)
4553 ++autocmd_no_leave;
4554#endif
4555 use_firstwin = FALSE;
4556 }
4557 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004558
4559 /* When ":tab" was used open a new tab for a new window repeatedly. */
4560 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4561 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 }
4563
4564 /* Remove the "lock" on the argument list. */
4565 alist_unlink(alist);
4566
4567#ifdef FEAT_AUTOCMD
4568 --autocmd_no_enter;
4569#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004570 /* to window with first arg */
4571 if (valid_tabpage(new_curtab))
4572 goto_tabpage_tp(new_curtab);
4573 if (win_valid(new_curwin))
4574 win_enter(new_curwin, FALSE);
4575
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576#ifdef FEAT_AUTOCMD
4577 --autocmd_no_leave;
4578#endif
4579 vim_free(opened);
4580}
4581
4582# if defined(FEAT_LISTCMDS) || defined(PROTO)
4583/*
4584 * Open a window for a number of buffers.
4585 */
4586 void
4587ex_buffer_all(eap)
4588 exarg_T *eap;
4589{
4590 buf_T *buf;
4591 win_T *wp, *wpnext;
4592 int split_ret = OK;
4593 int p_ea_save;
4594 int open_wins = 0;
4595 int r;
4596 int count; /* Maximum number of windows to open. */
4597 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004598#ifdef FEAT_WINDOWS
4599 int had_tab = cmdmod.tab;
4600 tabpage_T *tpnext;
4601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
4603 if (eap->addr_count == 0) /* make as many windows as possible */
4604 count = 9999;
4605 else
4606 count = eap->line2; /* make as many windows as specified */
4607 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4608 all = FALSE;
4609 else
4610 all = TRUE;
4611
4612 setpcmark();
4613
4614#ifdef FEAT_GUI
4615 need_mouse_correct = TRUE;
4616#endif
4617
4618 /*
4619 * Close superfluous windows (two windows for the same buffer).
4620 * Also close windows that are not full-width.
4621 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004622#ifdef FEAT_WINDOWS
4623 if (had_tab > 0)
4624 goto_tabpage_tp(first_tabpage);
4625 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004628 tpnext = curtab->tp_next;
4629 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004631 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004632 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004633#ifdef FEAT_VERTSPLIT
4634 || ((cmdmod.split & WSP_VERT)
4635 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004636 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004637 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004639#ifdef FEAT_WINDOWS
4640 || (had_tab > 0 && wp != firstwin)
4641#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004642 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004643 {
4644 win_close(wp, FALSE);
4645#ifdef FEAT_AUTOCMD
4646 wpnext = firstwin; /* just in case an autocommand does
4647 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004648 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004649 open_wins = 0;
4650#endif
4651 }
4652 else
4653 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004655
4656#ifdef FEAT_WINDOWS
4657 /* Without the ":tab" modifier only do the current tab page. */
4658 if (had_tab == 0 || tpnext == NULL)
4659 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004660 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
4664 /*
4665 * Go through the buffer list. When a buffer doesn't have a window yet,
4666 * open one. Otherwise move the window to the right position.
4667 * Watch out for autocommands that delete buffers or windows!
4668 */
4669#ifdef FEAT_AUTOCMD
4670 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4671 ++autocmd_no_enter;
4672#endif
4673 win_enter(lastwin, FALSE);
4674#ifdef FEAT_AUTOCMD
4675 ++autocmd_no_leave;
4676#endif
4677 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4678 {
4679 /* Check if this buffer needs a window */
4680 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4681 continue;
4682
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004683#ifdef FEAT_WINDOWS
4684 if (had_tab != 0)
4685 {
4686 /* With the ":tab" modifier don't move the window. */
4687 if (buf->b_nwindows > 0)
4688 wp = lastwin; /* buffer has a window, skip it */
4689 else
4690 wp = NULL;
4691 }
4692 else
4693#endif
4694 {
4695 /* Check if this buffer already has a window */
4696 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4697 if (wp->w_buffer == buf)
4698 break;
4699 /* If the buffer already has a window, move it */
4700 if (wp != NULL)
4701 win_move_after(wp, curwin);
4702 }
4703
4704 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 {
4706 /* Split the window and put the buffer in it */
4707 p_ea_save = p_ea;
4708 p_ea = TRUE; /* use space from all windows */
4709 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4710 ++open_wins;
4711 p_ea = p_ea_save;
4712 if (split_ret == FAIL)
4713 continue;
4714
4715 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004716#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 swap_exists_action = SEA_DIALOG;
4718#endif
4719 set_curbuf(buf, DOBUF_GOTO);
4720#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004723#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004725# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 break;
4727 }
4728#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004729#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (swap_exists_action == SEA_QUIT)
4731 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004732# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4733 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004734
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004735 /* Reset the error/interrupt/exception state here so that
4736 * aborting() returns FALSE when closing a window. */
4737 enter_cleanup(&cs);
4738# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004739
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004740 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 win_close(curwin, TRUE);
4742 --open_wins;
4743 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004744 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004745
4746# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4747 /* Restore the error/interrupt/exception state if not
4748 * discarded by a new aborting error, interrupt, or uncaught
4749 * exception. */
4750 leave_cleanup(&cs);
4751# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 }
4753 else
4754 handle_swap_exists(NULL);
4755#endif
4756 }
4757
4758 ui_breakcheck();
4759 if (got_int)
4760 {
4761 (void)vgetc(); /* only break the file loading, not the rest */
4762 break;
4763 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004764#ifdef FEAT_EVAL
4765 /* Autocommands deleted the buffer or aborted script processing!!! */
4766 if (aborting())
4767 break;
4768#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004769#ifdef FEAT_WINDOWS
4770 /* When ":tab" was used open a new tab for a new window repeatedly. */
4771 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4772 cmdmod.tab = 9999;
4773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 }
4775#ifdef FEAT_AUTOCMD
4776 --autocmd_no_enter;
4777#endif
4778 win_enter(firstwin, FALSE); /* back to first window */
4779#ifdef FEAT_AUTOCMD
4780 --autocmd_no_leave;
4781#endif
4782
4783 /*
4784 * Close superfluous windows.
4785 */
4786 for (wp = lastwin; open_wins > count; )
4787 {
4788 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4789 || autowrite(wp->w_buffer, FALSE) == OK);
4790#ifdef FEAT_AUTOCMD
4791 if (!win_valid(wp))
4792 {
4793 /* BufWrite Autocommands made the window invalid, start over */
4794 wp = lastwin;
4795 }
4796 else
4797#endif
4798 if (r)
4799 {
4800 win_close(wp, !P_HID(wp->w_buffer));
4801 --open_wins;
4802 wp = lastwin;
4803 }
4804 else
4805 {
4806 wp = wp->w_prev;
4807 if (wp == NULL)
4808 break;
4809 }
4810 }
4811}
4812# endif /* FEAT_LISTCMDS */
4813
4814#endif /* FEAT_WINDOWS */
4815
Bram Moolenaara3227e22006-03-08 21:32:40 +00004816static int chk_modeline __ARGS((linenr_T, int));
4817
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818/*
4819 * do_modelines() - process mode lines for the current file
4820 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004821 * "flags" can be:
4822 * OPT_WINONLY only set options local to window
4823 * OPT_NOWIN don't set options local to window
4824 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 * Returns immediately if the "ml" option isn't set.
4826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004828do_modelines(flags)
4829 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004831 linenr_T lnum;
4832 int nmlines;
4833 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
4835 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4836 return;
4837
4838 /* Disallow recursive entry here. Can happen when executing a modeline
4839 * triggers an autocommand, which reloads modelines with a ":do". */
4840 if (entered)
4841 return;
4842
4843 ++entered;
4844 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4845 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004846 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 nmlines = 0;
4848
4849 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4850 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004851 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 nmlines = 0;
4853 --entered;
4854}
4855
4856#include "version.h" /* for version number */
4857
4858/*
4859 * chk_modeline() - check a single line for a mode string
4860 * Return FAIL if an error encountered.
4861 */
4862 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004863chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004865 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866{
4867 char_u *s;
4868 char_u *e;
4869 char_u *linecopy; /* local copy of any modeline found */
4870 int prev;
4871 int vers;
4872 int end;
4873 int retval = OK;
4874 char_u *save_sourcing_name;
4875 linenr_T save_sourcing_lnum;
4876#ifdef FEAT_EVAL
4877 scid_T save_SID;
4878#endif
4879
4880 prev = -1;
4881 for (s = ml_get(lnum); *s != NUL; ++s)
4882 {
4883 if (prev == -1 || vim_isspace(prev))
4884 {
4885 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4886 || STRNCMP(s, "vi:", (size_t)3) == 0)
4887 break;
4888 if (STRNCMP(s, "vim", 3) == 0)
4889 {
4890 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4891 e = s + 4;
4892 else
4893 e = s + 3;
4894 vers = getdigits(&e);
4895 if (*e == ':'
4896 && (s[3] == ':'
4897 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4898 || (VIM_VERSION_100 < vers && s[3] == '<')
4899 || (VIM_VERSION_100 > vers && s[3] == '>')
4900 || (VIM_VERSION_100 == vers && s[3] == '=')))
4901 break;
4902 }
4903 }
4904 prev = *s;
4905 }
4906
4907 if (*s)
4908 {
4909 do /* skip over "ex:", "vi:" or "vim:" */
4910 ++s;
4911 while (s[-1] != ':');
4912
4913 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4914 if (linecopy == NULL)
4915 return FAIL;
4916
4917 save_sourcing_lnum = sourcing_lnum;
4918 save_sourcing_name = sourcing_name;
4919 sourcing_lnum = lnum; /* prepare for emsg() */
4920 sourcing_name = (char_u *)"modelines";
4921
4922 end = FALSE;
4923 while (end == FALSE)
4924 {
4925 s = skipwhite(s);
4926 if (*s == NUL)
4927 break;
4928
4929 /*
4930 * Find end of set command: ':' or end of line.
4931 * Skip over "\:", replacing it with ":".
4932 */
4933 for (e = s; *e != ':' && *e != NUL; ++e)
4934 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00004935 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 if (*e == NUL)
4937 end = TRUE;
4938
4939 /*
4940 * If there is a "set" command, require a terminating ':' and
4941 * ignore the stuff after the ':'.
4942 * "vi:set opt opt opt: foo" -- foo not interpreted
4943 * "vi:opt opt opt: foo" -- foo interpreted
4944 * Accept "se" for compatibility with Elvis.
4945 */
4946 if (STRNCMP(s, "set ", (size_t)4) == 0
4947 || STRNCMP(s, "se ", (size_t)3) == 0)
4948 {
4949 if (*e != ':') /* no terminating ':'? */
4950 break;
4951 end = TRUE;
4952 s = vim_strchr(s, ' ') + 1;
4953 }
4954 *e = NUL; /* truncate the set command */
4955
4956 if (*s != NUL) /* skip over an empty "::" */
4957 {
4958#ifdef FEAT_EVAL
4959 save_SID = current_SID;
4960 current_SID = SID_MODELINE;
4961#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004962 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963#ifdef FEAT_EVAL
4964 current_SID = save_SID;
4965#endif
4966 if (retval == FAIL) /* stop if error found */
4967 break;
4968 }
4969 s = e + 1; /* advance to next part */
4970 }
4971
4972 sourcing_lnum = save_sourcing_lnum;
4973 sourcing_name = save_sourcing_name;
4974
4975 vim_free(linecopy);
4976 }
4977 return retval;
4978}
4979
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00004980#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 int
4982read_viminfo_bufferlist(virp, writing)
4983 vir_T *virp;
4984 int writing;
4985{
4986 char_u *tab;
4987 linenr_T lnum;
4988 colnr_T col;
4989 buf_T *buf;
4990 char_u *sfname;
4991 char_u *xline;
4992
4993 /* Handle long line and escaped characters. */
4994 xline = viminfo_readstring(virp, 1, FALSE);
4995
4996 /* don't read in if there are files on the command-line or if writing: */
4997 if (xline != NULL && !writing && ARGCOUNT == 0
4998 && find_viminfo_parameter('%') != NULL)
4999 {
5000 /* Format is: <fname> Tab <lnum> Tab <col>.
5001 * Watch out for a Tab in the file name, work from the end. */
5002 lnum = 0;
5003 col = 0;
5004 tab = vim_strrchr(xline, '\t');
5005 if (tab != NULL)
5006 {
5007 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005008 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 tab = vim_strrchr(xline, '\t');
5010 if (tab != NULL)
5011 {
5012 *tab++ = '\0';
5013 lnum = atol((char *)tab);
5014 }
5015 }
5016
5017 /* Expand "~/" in the file name at "line + 1" to a full path.
5018 * Then try shortening it by comparing with the current directory */
5019 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005020 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021
5022 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5023 if (buf != NULL) /* just in case... */
5024 {
5025 buf->b_last_cursor.lnum = lnum;
5026 buf->b_last_cursor.col = col;
5027 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5028 }
5029 }
5030 vim_free(xline);
5031
5032 return viminfo_readline(virp);
5033}
5034
5035 void
5036write_viminfo_bufferlist(fp)
5037 FILE *fp;
5038{
5039 buf_T *buf;
5040#ifdef FEAT_WINDOWS
5041 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005042 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043#endif
5044 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005045 int max_buffers;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005046 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048 if (find_viminfo_parameter('%') == NULL)
5049 return;
5050
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005051 /* Without a number -1 is returned: do all buffers. */
5052 max_buffers = get_viminfo_parameter('%');
5053
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005055#define LINE_BUF_LEN (MAXPATHL + 40)
5056 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 if (line == NULL)
5058 return;
5059
5060#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005061 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 set_last_cursor(win);
5063#else
5064 set_last_cursor(curwin);
5065#endif
5066
5067 fprintf(fp, _("\n# Buffer list:\n"));
5068 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5069 {
5070 if (buf->b_fname == NULL
5071 || !buf->b_p_bl
5072#ifdef FEAT_QUICKFIX
5073 || bt_quickfix(buf)
5074#endif
5075 || removable(buf->b_ffname))
5076 continue;
5077
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005078 if (max_buffers-- == 0)
5079 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 putc('%', fp);
5081 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005082 len = STRLEN(line);
5083 vim_snprintf((char *)line + len, len - LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 (long)buf->b_last_cursor.lnum,
5085 buf->b_last_cursor.col);
5086 viminfo_writestring(fp, line);
5087 }
5088 vim_free(line);
5089}
5090#endif
5091
5092
5093/*
5094 * Return special buffer name.
5095 * Returns NULL when the buffer has a normal file name.
5096 */
5097 char *
5098buf_spname(buf)
5099 buf_T *buf;
5100{
5101#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5102 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005103 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005104 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005105 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005106
5107 /*
5108 * For location list window, w_llist_ref points to the location list.
5109 * For quickfix window, w_llist_ref is NULL.
5110 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005111 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005112 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005113 goto win_found;
5114win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005115 if (win != NULL && win->w_llist_ref != NULL)
5116 return _("[Location List]");
5117 else
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005118 return _("[Quickfix List]");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120#endif
5121#ifdef FEAT_QUICKFIX
5122 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5123 * contains the name as specified by the user */
5124 if (bt_nofile(buf))
5125 {
5126 if (buf->b_sfname != NULL)
5127 return (char *)buf->b_sfname;
Bram Moolenaarf4f664c2008-12-03 10:21:57 +00005128 return _("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130#endif
5131 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005132 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 return NULL;
5134}
5135
5136
5137#if defined(FEAT_SIGNS) || defined(PROTO)
5138/*
5139 * Insert the sign into the signlist.
5140 */
5141 static void
5142insert_sign(buf, prev, next, id, lnum, typenr)
5143 buf_T *buf; /* buffer to store sign in */
5144 signlist_T *prev; /* previous sign entry */
5145 signlist_T *next; /* next sign entry */
5146 int id; /* sign ID */
5147 linenr_T lnum; /* line number which gets the mark */
5148 int typenr; /* typenr of sign we are adding */
5149{
5150 signlist_T *newsign;
5151
5152 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5153 if (newsign != NULL)
5154 {
5155 newsign->id = id;
5156 newsign->lnum = lnum;
5157 newsign->typenr = typenr;
5158 newsign->next = next;
5159#ifdef FEAT_NETBEANS_INTG
5160 newsign->prev = prev;
5161 if (next != NULL)
5162 next->prev = newsign;
5163#endif
5164
5165 if (prev == NULL)
5166 {
5167 /* When adding first sign need to redraw the windows to create the
5168 * column for signs. */
5169 if (buf->b_signlist == NULL)
5170 {
5171 redraw_buf_later(buf, NOT_VALID);
5172 changed_cline_bef_curs();
5173 }
5174
5175 /* first sign in signlist */
5176 buf->b_signlist = newsign;
5177 }
5178 else
5179 prev->next = newsign;
5180 }
5181}
5182
5183/*
5184 * Add the sign into the signlist. Find the right spot to do it though.
5185 */
5186 void
5187buf_addsign(buf, id, lnum, typenr)
5188 buf_T *buf; /* buffer to store sign in */
5189 int id; /* sign ID */
5190 linenr_T lnum; /* line number which gets the mark */
5191 int typenr; /* typenr of sign we are adding */
5192{
5193 signlist_T *sign; /* a sign in the signlist */
5194 signlist_T *prev; /* the previous sign */
5195
5196 prev = NULL;
5197 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5198 {
5199 if (lnum == sign->lnum && id == sign->id)
5200 {
5201 sign->typenr = typenr;
5202 return;
5203 }
5204 else if (
5205#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5206 id < 0 &&
5207#endif
5208 lnum < sign->lnum)
5209 {
5210#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5211 /* XXX - GRP: Is this because of sign slide problem? Or is it
5212 * really needed? Or is it because we allow multiple signs per
5213 * line? If so, should I add that feature to FEAT_SIGNS?
5214 */
5215 while (prev != NULL && prev->lnum == lnum)
5216 prev = prev->prev;
5217 if (prev == NULL)
5218 sign = buf->b_signlist;
5219 else
5220 sign = prev->next;
5221#endif
5222 insert_sign(buf, prev, sign, id, lnum, typenr);
5223 return;
5224 }
5225 prev = sign;
5226 }
5227#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5228 /* XXX - GRP: See previous comment */
5229 while (prev != NULL && prev->lnum == lnum)
5230 prev = prev->prev;
5231 if (prev == NULL)
5232 sign = buf->b_signlist;
5233 else
5234 sign = prev->next;
5235#endif
5236 insert_sign(buf, prev, sign, id, lnum, typenr);
5237
5238 return;
5239}
5240
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005241 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242buf_change_sign_type(buf, markId, typenr)
5243 buf_T *buf; /* buffer to store sign in */
5244 int markId; /* sign ID */
5245 int typenr; /* typenr of sign we are adding */
5246{
5247 signlist_T *sign; /* a sign in the signlist */
5248
5249 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5250 {
5251 if (sign->id == markId)
5252 {
5253 sign->typenr = typenr;
5254 return sign->lnum;
5255 }
5256 }
5257
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005258 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259}
5260
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005261 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262buf_getsigntype(buf, lnum, type)
5263 buf_T *buf;
5264 linenr_T lnum;
5265 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5266{
5267 signlist_T *sign; /* a sign in a b_signlist */
5268
5269 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5270 if (sign->lnum == lnum
5271 && (type == SIGN_ANY
5272# ifdef FEAT_SIGN_ICONS
5273 || (type == SIGN_ICON
5274 && sign_get_image(sign->typenr) != NULL)
5275# endif
5276 || (type == SIGN_TEXT
5277 && sign_get_text(sign->typenr) != NULL)
5278 || (type == SIGN_LINEHL
5279 && sign_get_attr(sign->typenr, TRUE) != 0)))
5280 return sign->typenr;
5281 return 0;
5282}
5283
5284
5285 linenr_T
5286buf_delsign(buf, id)
5287 buf_T *buf; /* buffer sign is stored in */
5288 int id; /* sign id */
5289{
5290 signlist_T **lastp; /* pointer to pointer to current sign */
5291 signlist_T *sign; /* a sign in a b_signlist */
5292 signlist_T *next; /* the next sign in a b_signlist */
5293 linenr_T lnum; /* line number whose sign was deleted */
5294
5295 lastp = &buf->b_signlist;
5296 lnum = 0;
5297 for (sign = buf->b_signlist; sign != NULL; sign = next)
5298 {
5299 next = sign->next;
5300 if (sign->id == id)
5301 {
5302 *lastp = next;
5303#ifdef FEAT_NETBEANS_INTG
5304 if (next != NULL)
5305 next->prev = sign->prev;
5306#endif
5307 lnum = sign->lnum;
5308 vim_free(sign);
5309 break;
5310 }
5311 else
5312 lastp = &sign->next;
5313 }
5314
5315 /* When deleted the last sign need to redraw the windows to remove the
5316 * sign column. */
5317 if (buf->b_signlist == NULL)
5318 {
5319 redraw_buf_later(buf, NOT_VALID);
5320 changed_cline_bef_curs();
5321 }
5322
5323 return lnum;
5324}
5325
5326
5327/*
5328 * Find the line number of the sign with the requested id. If the sign does
5329 * not exist, return 0 as the line number. This will still let the correct file
5330 * get loaded.
5331 */
5332 int
5333buf_findsign(buf, id)
5334 buf_T *buf; /* buffer to store sign in */
5335 int id; /* sign ID */
5336{
5337 signlist_T *sign; /* a sign in the signlist */
5338
5339 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5340 if (sign->id == id)
5341 return sign->lnum;
5342
5343 return 0;
5344}
5345
5346 int
5347buf_findsign_id(buf, lnum)
5348 buf_T *buf; /* buffer whose sign we are searching for */
5349 linenr_T lnum; /* line number of sign */
5350{
5351 signlist_T *sign; /* a sign in the signlist */
5352
5353 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5354 if (sign->lnum == lnum)
5355 return sign->id;
5356
5357 return 0;
5358}
5359
5360
5361# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5362/* see if a given type of sign exists on a specific line */
5363 int
5364buf_findsigntype_id(buf, lnum, typenr)
5365 buf_T *buf; /* buffer whose sign we are searching for */
5366 linenr_T lnum; /* line number of sign */
5367 int typenr; /* sign type number */
5368{
5369 signlist_T *sign; /* a sign in the signlist */
5370
5371 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5372 if (sign->lnum == lnum && sign->typenr == typenr)
5373 return sign->id;
5374
5375 return 0;
5376}
5377
5378
5379# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5380/* return the number of icons on the given line */
5381 int
5382buf_signcount(buf, lnum)
5383 buf_T *buf;
5384 linenr_T lnum;
5385{
5386 signlist_T *sign; /* a sign in the signlist */
5387 int count = 0;
5388
5389 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5390 if (sign->lnum == lnum)
5391 if (sign_get_image(sign->typenr) != NULL)
5392 count++;
5393
5394 return count;
5395}
5396# endif /* FEAT_SIGN_ICONS */
5397# endif /* FEAT_NETBEANS_INTG */
5398
5399
5400/*
5401 * Delete signs in buffer "buf".
5402 */
5403 static void
5404buf_delete_signs(buf)
5405 buf_T *buf;
5406{
5407 signlist_T *next;
5408
5409 while (buf->b_signlist != NULL)
5410 {
5411 next = buf->b_signlist->next;
5412 vim_free(buf->b_signlist);
5413 buf->b_signlist = next;
5414 }
5415}
5416
5417/*
5418 * Delete all signs in all buffers.
5419 */
5420 void
5421buf_delete_all_signs()
5422{
5423 buf_T *buf; /* buffer we are checking for signs */
5424
5425 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5426 if (buf->b_signlist != NULL)
5427 {
5428 /* Need to redraw the windows to remove the sign column. */
5429 redraw_buf_later(buf, NOT_VALID);
5430 buf_delete_signs(buf);
5431 }
5432}
5433
5434/*
5435 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5436 */
5437 void
5438sign_list_placed(rbuf)
5439 buf_T *rbuf;
5440{
5441 buf_T *buf;
5442 signlist_T *p;
5443 char lbuf[BUFSIZ];
5444
5445 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5446 msg_putchar('\n');
5447 if (rbuf == NULL)
5448 buf = firstbuf;
5449 else
5450 buf = rbuf;
5451 while (buf != NULL)
5452 {
5453 if (buf->b_signlist != NULL)
5454 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005455 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5457 msg_putchar('\n');
5458 }
5459 for (p = buf->b_signlist; p != NULL; p = p->next)
5460 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005461 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5463 MSG_PUTS(lbuf);
5464 msg_putchar('\n');
5465 }
5466 if (rbuf != NULL)
5467 break;
5468 buf = buf->b_next;
5469 }
5470}
5471
5472/*
5473 * Adjust a placed sign for inserted/deleted lines.
5474 */
5475 void
5476sign_mark_adjust(line1, line2, amount, amount_after)
5477 linenr_T line1;
5478 linenr_T line2;
5479 long amount;
5480 long amount_after;
5481{
5482 signlist_T *sign; /* a sign in a b_signlist */
5483
5484 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5485 {
5486 if (sign->lnum >= line1 && sign->lnum <= line2)
5487 {
5488 if (amount == MAXLNUM)
5489 sign->lnum = line1;
5490 else
5491 sign->lnum += amount;
5492 }
5493 else if (sign->lnum > line2)
5494 sign->lnum += amount_after;
5495 }
5496}
5497#endif /* FEAT_SIGNS */
5498
5499/*
5500 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5501 */
5502 void
5503set_buflisted(on)
5504 int on;
5505{
5506 if (on != curbuf->b_p_bl)
5507 {
5508 curbuf->b_p_bl = on;
5509#ifdef FEAT_AUTOCMD
5510 if (on)
5511 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5512 else
5513 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5514#endif
5515 }
5516}
5517
5518/*
5519 * Read the file for "buf" again and check if the contents changed.
5520 * Return TRUE if it changed or this could not be checked.
5521 */
5522 int
5523buf_contents_changed(buf)
5524 buf_T *buf;
5525{
5526 buf_T *newbuf;
5527 int differ = TRUE;
5528 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 exarg_T ea;
5531
5532 /* Allocate a buffer without putting it in the buffer list. */
5533 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5534 if (newbuf == NULL)
5535 return TRUE;
5536
5537 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5538 if (prep_exarg(&ea, buf) == FAIL)
5539 {
5540 wipe_buffer(newbuf, FALSE);
5541 return TRUE;
5542 }
5543
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 /* set curwin/curbuf to buf and save a few things */
5545 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
Bram Moolenaar4770d092006-01-12 23:22:24 +00005547 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 && readfile(buf->b_ffname, buf->b_fname,
5549 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5550 &ea, READ_NEW | READ_DUMMY) == OK)
5551 {
5552 /* compare the two files line by line */
5553 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5554 {
5555 differ = FALSE;
5556 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5557 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5558 {
5559 differ = TRUE;
5560 break;
5561 }
5562 }
5563 }
5564 vim_free(ea.cmd);
5565
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 /* restore curwin/curbuf and a few other things */
5567 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
5569 if (curbuf != newbuf) /* safety check */
5570 wipe_buffer(newbuf, FALSE);
5571
5572 return differ;
5573}
5574
5575/*
5576 * Wipe out a buffer and decrement the last buffer number if it was used for
5577 * this buffer. Call this to wipe out a temp buffer that does not contain any
5578 * marks.
5579 */
5580/*ARGSUSED*/
5581 void
5582wipe_buffer(buf, aucmd)
5583 buf_T *buf;
5584 int aucmd; /* When TRUE trigger autocommands. */
5585{
5586 if (buf->b_fnum == top_file_num - 1)
5587 --top_file_num;
5588
5589#ifdef FEAT_AUTOCMD
5590 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005591 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592#endif
5593 close_buffer(NULL, buf, DOBUF_WIPE);
5594#ifdef FEAT_AUTOCMD
5595 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005596 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597#endif
5598}