blob: 0569f166d18bad3c4b3cb677c58eece90a36e0d9 [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 */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100118 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119
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;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100318 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#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 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 void
516buf_freeall(buf, del_buf, wipe_buf)
517 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +0000518 int del_buf UNUSED; /* buffer is going to be deleted */
519 int wipe_buf UNUSED; /* buffer is going to be wiped out */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520{
521#ifdef FEAT_AUTOCMD
522 int is_curbuf = (buf == curbuf);
523
524 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
525 if (!buf_valid(buf)) /* autocommands may delete the buffer */
526 return;
527 if (del_buf && buf->b_p_bl)
528 {
529 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
530 if (!buf_valid(buf)) /* autocommands may delete the buffer */
531 return;
532 }
533 if (wipe_buf)
534 {
535 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
536 FALSE, buf);
537 if (!buf_valid(buf)) /* autocommands may delete the buffer */
538 return;
539 }
540# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000541 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 return;
543# endif
544
545 /*
546 * It's possible that autocommands change curbuf to the one being deleted.
547 * This might cause curbuf to be deleted unexpectedly. But in some cases
548 * it's OK to delete the curbuf, because a new one is obtained anyway.
549 * Therefore only return if curbuf changed to the deleted buffer.
550 */
551 if (buf == curbuf && !is_curbuf)
552 return;
553#endif
554#ifdef FEAT_DIFF
555 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
556#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000557
558#ifdef FEAT_FOLDING
559 /* No folds in an empty buffer. */
560# ifdef FEAT_WINDOWS
561 {
562 win_T *win;
563 tabpage_T *tp;
564
565 FOR_ALL_TAB_WINDOWS(tp, win)
566 if (win->w_buffer == buf)
567 clearFolding(win);
568 }
569# else
570 if (curwin->w_buffer == buf)
571 clearFolding(curwin);
572# endif
573#endif
574
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575#ifdef FEAT_TCL
576 tcl_buffer_free(buf);
577#endif
578 u_blockfree(buf); /* free the memory allocated for undo */
579 ml_close(buf, TRUE); /* close and delete the memline/memfile */
580 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
581 u_clearall(buf); /* reset all undo information */
582#ifdef FEAT_SYN_HL
583 syntax_clear(buf); /* reset syntax info */
584#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000585 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586}
587
588/*
589 * Free a buffer structure and the things it contains related to the buffer
590 * itself (not the file, that must have been done already).
591 */
592 static void
593free_buffer(buf)
594 buf_T *buf;
595{
596 free_buffer_stuff(buf, TRUE);
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000597#ifdef FEAT_MZSCHEME
598 mzscheme_buffer_free(buf);
599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600#ifdef FEAT_PERL
601 perl_buf_free(buf);
602#endif
603#ifdef FEAT_PYTHON
604 python_buffer_free(buf);
605#endif
606#ifdef FEAT_RUBY
607 ruby_buffer_free(buf);
608#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000609#ifdef FEAT_AUTOCMD
610 aubuflocal_remove(buf);
611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 vim_free(buf);
613}
614
615/*
616 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
617 */
618 static void
619free_buffer_stuff(buf, free_options)
620 buf_T *buf;
621 int free_options; /* free options as well */
622{
623 if (free_options)
624 {
625 clear_wininfo(buf); /* including window-local options */
626 free_buf_options(buf, TRUE);
627 }
628#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000629 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
630 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631#endif
632#ifdef FEAT_USR_CMDS
633 uc_clear(&buf->b_ucmds); /* clear local user commands */
634#endif
635#ifdef FEAT_SIGNS
636 buf_delete_signs(buf); /* delete any signs */
637#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000638#ifdef FEAT_NETBEANS_INTG
639 if (usingNetbeans)
640 netbeans_file_killed(buf);
641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642#ifdef FEAT_LOCALMAP
643 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
644 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
645#endif
646#ifdef FEAT_MBYTE
647 vim_free(buf->b_start_fenc);
648 buf->b_start_fenc = NULL;
649#endif
Bram Moolenaar9381ab72008-11-12 11:52:19 +0000650#ifdef FEAT_SPELL
651 ga_clear(&buf->b_langp);
652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653}
654
655/*
656 * Free the b_wininfo list for buffer "buf".
657 */
658 static void
659clear_wininfo(buf)
660 buf_T *buf;
661{
662 wininfo_T *wip;
663
664 while (buf->b_wininfo != NULL)
665 {
666 wip = buf->b_wininfo;
667 buf->b_wininfo = wip->wi_next;
668 if (wip->wi_optset)
669 {
670 clear_winopt(&wip->wi_opt);
671#ifdef FEAT_FOLDING
672 deleteFoldRecurse(&wip->wi_folds);
673#endif
674 }
675 vim_free(wip);
676 }
677}
678
679#if defined(FEAT_LISTCMDS) || defined(PROTO)
680/*
681 * Go to another buffer. Handles the result of the ATTENTION dialog.
682 */
683 void
684goto_buffer(eap, start, dir, count)
685 exarg_T *eap;
686 int start;
687 int dir;
688 int count;
689{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000690# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 buf_T *old_curbuf = curbuf;
692
693 swap_exists_action = SEA_DIALOG;
694# endif
695 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
696 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000697# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
699 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000700# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
701 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000702
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000703 /* Reset the error/interrupt/exception state here so that
704 * aborting() returns FALSE when closing a window. */
705 enter_cleanup(&cs);
706# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000707
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000708 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 win_close(curwin, TRUE);
710 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000711 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000712
713# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
714 /* Restore the error/interrupt/exception state if not discarded by a
715 * new aborting error, interrupt, or uncaught exception. */
716 leave_cleanup(&cs);
717# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719 else
720 handle_swap_exists(old_curbuf);
721# endif
722}
723#endif
724
Bram Moolenaare64ac772005-12-07 20:54:59 +0000725#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726/*
727 * Handle the situation of swap_exists_action being set.
728 * It is allowed for "old_curbuf" to be NULL or invalid.
729 */
730 void
731handle_swap_exists(old_curbuf)
732 buf_T *old_curbuf;
733{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000734# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
735 cleanup_T cs;
736# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 if (swap_exists_action == SEA_QUIT)
739 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000740# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
741 /* Reset the error/interrupt/exception state here so that
742 * aborting() returns FALSE when closing a buffer. */
743 enter_cleanup(&cs);
744# endif
745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 /* User selected Quit at ATTENTION prompt. Go back to previous
747 * buffer. If that buffer is gone or the same as the current one,
748 * open a new, empty buffer. */
749 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000750 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
752 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000753 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 if (old_curbuf != NULL)
755 enter_buffer(old_curbuf);
756 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000757
758# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
759 /* Restore the error/interrupt/exception state if not discarded by a
760 * new aborting error, interrupt, or uncaught exception. */
761 leave_cleanup(&cs);
762# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 }
764 else if (swap_exists_action == SEA_RECOVER)
765 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000766# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
767 /* Reset the error/interrupt/exception state here so that
768 * aborting() returns FALSE when closing a buffer. */
769 enter_cleanup(&cs);
770# endif
771
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 /* User selected Recover at ATTENTION prompt. */
773 msg_scroll = TRUE;
774 ml_recover();
775 MSG_PUTS("\n"); /* don't overwrite the last message */
776 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000777 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000778
779# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
780 /* Restore the error/interrupt/exception state if not discarded by a
781 * new aborting error, interrupt, or uncaught exception. */
782 leave_cleanup(&cs);
783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785 swap_exists_action = SEA_NONE;
786}
787#endif
788
789#if defined(FEAT_LISTCMDS) || defined(PROTO)
790/*
791 * do_bufdel() - delete or unload buffer(s)
792 *
793 * addr_count == 0: ":bdel" - delete current buffer
794 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
795 * buffer "end_bnr", then any other arguments.
796 * addr_count == 2: ":N,N bdel" - delete buffers in range
797 *
798 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
799 * DOBUF_DEL (":bdel")
800 *
801 * Returns error message or NULL
802 */
803 char_u *
804do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
805 int command;
806 char_u *arg; /* pointer to extra arguments */
807 int addr_count;
808 int start_bnr; /* first buffer number in a range */
809 int end_bnr; /* buffer nr or last buffer nr in a range */
810 int forceit;
811{
812 int do_current = 0; /* delete current buffer? */
813 int deleted = 0; /* number of buffers deleted */
814 char_u *errormsg = NULL; /* return value */
815 int bnr; /* buffer number */
816 char_u *p;
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 if (addr_count == 0)
819 {
820 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
821 }
822 else
823 {
824 if (addr_count == 2)
825 {
826 if (*arg) /* both range and argument is not allowed */
827 return (char_u *)_(e_trailing);
828 bnr = start_bnr;
829 }
830 else /* addr_count == 1 */
831 bnr = end_bnr;
832
833 for ( ;!got_int; ui_breakcheck())
834 {
835 /*
836 * delete the current buffer last, otherwise when the
837 * current buffer is deleted, the next buffer becomes
838 * the current one and will be loaded, which may then
839 * also be deleted, etc.
840 */
841 if (bnr == curbuf->b_fnum)
842 do_current = bnr;
843 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
844 forceit) == OK)
845 ++deleted;
846
847 /*
848 * find next buffer number to delete/unload
849 */
850 if (addr_count == 2)
851 {
852 if (++bnr > end_bnr)
853 break;
854 }
855 else /* addr_count == 1 */
856 {
857 arg = skipwhite(arg);
858 if (*arg == NUL)
859 break;
860 if (!VIM_ISDIGIT(*arg))
861 {
862 p = skiptowhite_esc(arg);
863 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
864 if (bnr < 0) /* failed */
865 break;
866 arg = p;
867 }
868 else
869 bnr = getdigits(&arg);
870 }
871 }
872 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
873 FORWARD, do_current, forceit) == OK)
874 ++deleted;
875
876 if (deleted == 0)
877 {
878 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000879 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000881 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000883 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 errormsg = IObuff;
885 }
886 else if (deleted >= p_report)
887 {
888 if (command == DOBUF_UNLOAD)
889 {
890 if (deleted == 1)
891 MSG(_("1 buffer unloaded"));
892 else
893 smsg((char_u *)_("%d buffers unloaded"), deleted);
894 }
895 else if (command == DOBUF_DEL)
896 {
897 if (deleted == 1)
898 MSG(_("1 buffer deleted"));
899 else
900 smsg((char_u *)_("%d buffers deleted"), deleted);
901 }
902 else
903 {
904 if (deleted == 1)
905 MSG(_("1 buffer wiped out"));
906 else
907 smsg((char_u *)_("%d buffers wiped out"), deleted);
908 }
909 }
910 }
911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
913 return errormsg;
914}
915
916/*
917 * Implementation of the commands for the buffer list.
918 *
919 * action == DOBUF_GOTO go to specified buffer
920 * action == DOBUF_SPLIT split window and go to specified buffer
921 * action == DOBUF_UNLOAD unload specified buffer(s)
922 * action == DOBUF_DEL delete specified buffer(s) from buffer list
923 * action == DOBUF_WIPE delete specified buffer(s) really
924 *
925 * start == DOBUF_CURRENT go to "count" buffer from current buffer
926 * start == DOBUF_FIRST go to "count" buffer from first buffer
927 * start == DOBUF_LAST go to "count" buffer from last buffer
928 * start == DOBUF_MOD go to "count" modified buffer from current buffer
929 *
930 * Return FAIL or OK.
931 */
932 int
933do_buffer(action, start, dir, count, forceit)
934 int action;
935 int start;
936 int dir; /* FORWARD or BACKWARD */
937 int count; /* buffer number or number of buffers */
938 int forceit; /* TRUE for :...! */
939{
940 buf_T *buf;
941 buf_T *bp;
942 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
943 || action == DOBUF_WIPE);
944
945 switch (start)
946 {
947 case DOBUF_FIRST: buf = firstbuf; break;
948 case DOBUF_LAST: buf = lastbuf; break;
949 default: buf = curbuf; break;
950 }
951 if (start == DOBUF_MOD) /* find next modified buffer */
952 {
953 while (count-- > 0)
954 {
955 do
956 {
957 buf = buf->b_next;
958 if (buf == NULL)
959 buf = firstbuf;
960 }
961 while (buf != curbuf && !bufIsChanged(buf));
962 }
963 if (!bufIsChanged(buf))
964 {
965 EMSG(_("E84: No modified buffer found"));
966 return FAIL;
967 }
968 }
969 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
970 {
971 while (buf != NULL && buf->b_fnum != count)
972 buf = buf->b_next;
973 }
974 else
975 {
976 bp = NULL;
977 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
978 {
979 /* remember the buffer where we start, we come back there when all
980 * buffers are unlisted. */
981 if (bp == NULL)
982 bp = buf;
983 if (dir == FORWARD)
984 {
985 buf = buf->b_next;
986 if (buf == NULL)
987 buf = firstbuf;
988 }
989 else
990 {
991 buf = buf->b_prev;
992 if (buf == NULL)
993 buf = lastbuf;
994 }
995 /* don't count unlisted buffers */
996 if (unload || buf->b_p_bl)
997 {
998 --count;
999 bp = NULL; /* use this buffer as new starting point */
1000 }
1001 if (bp == buf)
1002 {
1003 /* back where we started, didn't find anything. */
1004 EMSG(_("E85: There is no listed buffer"));
1005 return FAIL;
1006 }
1007 }
1008 }
1009
1010 if (buf == NULL) /* could not find it */
1011 {
1012 if (start == DOBUF_FIRST)
1013 {
1014 /* don't warn when deleting */
1015 if (!unload)
1016 EMSGN(_("E86: Buffer %ld does not exist"), count);
1017 }
1018 else if (dir == FORWARD)
1019 EMSG(_("E87: Cannot go beyond last buffer"));
1020 else
1021 EMSG(_("E88: Cannot go before first buffer"));
1022 return FAIL;
1023 }
1024
1025#ifdef FEAT_GUI
1026 need_mouse_correct = TRUE;
1027#endif
1028
1029#ifdef FEAT_LISTCMDS
1030 /*
1031 * delete buffer buf from memory and/or the list
1032 */
1033 if (unload)
1034 {
1035 int forward;
1036 int retval;
1037
1038 /* When unloading or deleting a buffer that's already unloaded and
1039 * unlisted: fail silently. */
1040 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1041 return FAIL;
1042
1043 if (!forceit && bufIsChanged(buf))
1044 {
1045#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1046 if ((p_confirm || cmdmod.confirm) && p_write)
1047 {
1048 dialog_changed(buf, FALSE);
1049# ifdef FEAT_AUTOCMD
1050 if (!buf_valid(buf))
1051 /* Autocommand deleted buffer, oops! It's not changed
1052 * now. */
1053 return FAIL;
1054# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001055 /* If it's still changed fail silently, the dialog already
1056 * mentioned why it fails. */
1057 if (bufIsChanged(buf))
1058 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001060 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061#endif
1062 {
1063 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1064 buf->b_fnum);
1065 return FAIL;
1066 }
1067 }
1068
1069 /*
1070 * If deleting the last (listed) buffer, make it empty.
1071 * The last (listed) buffer cannot be unloaded.
1072 */
1073 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1074 if (bp->b_p_bl && bp != buf)
1075 break;
1076 if (bp == NULL && buf == curbuf)
1077 {
1078 if (action == DOBUF_UNLOAD)
1079 {
1080 EMSG(_("E90: Cannot unload last buffer"));
1081 return FAIL;
1082 }
1083
1084 /* Close any other windows on this buffer, then make it empty. */
1085#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001086 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087#endif
1088 setpcmark();
1089 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001090 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091
1092 /*
1093 * do_ecmd() may create a new buffer, then we have to delete
1094 * the old one. But do_ecmd() may have done that already, check
1095 * if the buffer still exists.
1096 */
1097 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1098 close_buffer(NULL, buf, action);
1099 return retval;
1100 }
1101
1102#ifdef FEAT_WINDOWS
1103 /*
1104 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001105 * (unless it's the only window). Repeat this so long as we end up in
1106 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001108 while (buf == curbuf
1109 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 win_close(curwin, FALSE);
1111#endif
1112
1113 /*
1114 * If the buffer to be deleted is not the current one, delete it here.
1115 */
1116 if (buf != curbuf)
1117 {
1118#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001119 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120#endif
1121 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1122 close_buffer(NULL, buf, action);
1123 return OK;
1124 }
1125
1126 /*
1127 * Deleting the current buffer: Need to find another buffer to go to.
1128 * There must be another, otherwise it would have been handled above.
1129 * First use au_new_curbuf, if it is valid.
1130 * Then prefer the buffer we most recently visited.
1131 * Else try to find one that is loaded, after the current buffer,
1132 * then before the current buffer.
1133 * Finally use any buffer.
1134 */
1135 buf = NULL; /* selected buffer */
1136 bp = NULL; /* used when no loaded buffer found */
1137#ifdef FEAT_AUTOCMD
1138 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1139 buf = au_new_curbuf;
1140# ifdef FEAT_JUMPLIST
1141 else
1142# endif
1143#endif
1144#ifdef FEAT_JUMPLIST
1145 if (curwin->w_jumplistlen > 0)
1146 {
1147 int jumpidx;
1148
1149 jumpidx = curwin->w_jumplistidx - 1;
1150 if (jumpidx < 0)
1151 jumpidx = curwin->w_jumplistlen - 1;
1152
1153 forward = jumpidx;
1154 while (jumpidx != curwin->w_jumplistidx)
1155 {
1156 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1157 if (buf != NULL)
1158 {
1159 if (buf == curbuf || !buf->b_p_bl)
1160 buf = NULL; /* skip current and unlisted bufs */
1161 else if (buf->b_ml.ml_mfp == NULL)
1162 {
1163 /* skip unloaded buf, but may keep it for later */
1164 if (bp == NULL)
1165 bp = buf;
1166 buf = NULL;
1167 }
1168 }
1169 if (buf != NULL) /* found a valid buffer: stop searching */
1170 break;
1171 /* advance to older entry in jump list */
1172 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1173 break;
1174 if (--jumpidx < 0)
1175 jumpidx = curwin->w_jumplistlen - 1;
1176 if (jumpidx == forward) /* List exhausted for sure */
1177 break;
1178 }
1179 }
1180#endif
1181
1182 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1183 {
1184 forward = TRUE;
1185 buf = curbuf->b_next;
1186 for (;;)
1187 {
1188 if (buf == NULL)
1189 {
1190 if (!forward) /* tried both directions */
1191 break;
1192 buf = curbuf->b_prev;
1193 forward = FALSE;
1194 continue;
1195 }
1196 /* in non-help buffer, try to skip help buffers, and vv */
1197 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1198 {
1199 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1200 break;
1201 if (bp == NULL) /* remember unloaded buf for later */
1202 bp = buf;
1203 }
1204 if (forward)
1205 buf = buf->b_next;
1206 else
1207 buf = buf->b_prev;
1208 }
1209 }
1210 if (buf == NULL) /* No loaded buffer, use unloaded one */
1211 buf = bp;
1212 if (buf == NULL) /* No loaded buffer, find listed one */
1213 {
1214 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1215 if (buf->b_p_bl && buf != curbuf)
1216 break;
1217 }
1218 if (buf == NULL) /* Still no buffer, just take one */
1219 {
1220 if (curbuf->b_next != NULL)
1221 buf = curbuf->b_next;
1222 else
1223 buf = curbuf->b_prev;
1224 }
1225 }
1226
1227 /*
1228 * make buf current buffer
1229 */
1230 if (action == DOBUF_SPLIT) /* split window first */
1231 {
1232# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001233 /* If 'switchbuf' contains "useopen": jump to first window containing
1234 * "buf" if one exists */
1235 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001236 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001237 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001238 * page containing "buf" if one exists */
1239 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 return OK;
1241 if (win_split(0, 0) == FAIL)
1242# endif
1243 return FAIL;
1244 }
1245#endif
1246
1247 /* go to current buffer - nothing to do */
1248 if (buf == curbuf)
1249 return OK;
1250
1251 /*
1252 * Check if the current buffer may be abandoned.
1253 */
1254 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1255 {
1256#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1257 if ((p_confirm || cmdmod.confirm) && p_write)
1258 {
1259 dialog_changed(curbuf, FALSE);
1260# ifdef FEAT_AUTOCMD
1261 if (!buf_valid(buf))
1262 /* Autocommand deleted buffer, oops! */
1263 return FAIL;
1264# endif
1265 }
1266 if (bufIsChanged(curbuf))
1267#endif
1268 {
1269 EMSG(_(e_nowrtmsg));
1270 return FAIL;
1271 }
1272 }
1273
1274 /* Go to the other buffer. */
1275 set_curbuf(buf, action);
1276
1277#if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1278 if (action == DOBUF_SPLIT)
1279 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1280#endif
1281
1282#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1283 if (aborting()) /* autocmds may abort script processing */
1284 return FAIL;
1285#endif
1286
1287 return OK;
1288}
1289
1290#endif /* FEAT_LISTCMDS */
1291
1292/*
1293 * Set current buffer to "buf". Executes autocommands and closes current
1294 * buffer. "action" tells how to close the current buffer:
1295 * DOBUF_GOTO free or hide it
1296 * DOBUF_SPLIT nothing
1297 * DOBUF_UNLOAD unload it
1298 * DOBUF_DEL delete it
1299 * DOBUF_WIPE wipe it out
1300 */
1301 void
1302set_curbuf(buf, action)
1303 buf_T *buf;
1304 int action;
1305{
1306 buf_T *prevbuf;
1307 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1308 || action == DOBUF_WIPE);
1309
1310 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001311 if (!cmdmod.keepalt)
1312 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001313 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314
1315#ifdef FEAT_VISUAL
1316 /* Don't restart Select mode after switching to another buffer. */
1317 VIsual_reselect = FALSE;
1318#endif
1319
1320 /* close_windows() or apply_autocmds() may change curbuf */
1321 prevbuf = curbuf;
1322
1323#ifdef FEAT_AUTOCMD
1324 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1325# ifdef FEAT_EVAL
1326 if (buf_valid(prevbuf) && !aborting())
1327# else
1328 if (buf_valid(prevbuf))
1329# endif
1330#endif
1331 {
1332#ifdef FEAT_WINDOWS
1333 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001334 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335#endif
1336#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1337 if (buf_valid(prevbuf) && !aborting())
1338#else
1339 if (buf_valid(prevbuf))
1340#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001341 {
1342 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001343 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1345 unload ? action : (action == DOBUF_GOTO
1346 && !P_HID(prevbuf)
1347 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001351 /* An autocommand may have deleted "buf", already entered it (e.g., when
1352 * it did ":bunload") or aborted the script processing! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353# ifdef FEAT_EVAL
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001354 if (buf_valid(buf) && buf != curbuf && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355# else
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001356 if (buf_valid(buf) && buf != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357# endif
1358#endif
1359 enter_buffer(buf);
1360}
1361
1362/*
1363 * Enter a new current buffer.
1364 * Old curbuf must have been abandoned already!
1365 */
1366 void
1367enter_buffer(buf)
1368 buf_T *buf;
1369{
1370 /* Copy buffer and window local option values. Not for a help buffer. */
1371 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1372 if (!buf->b_help)
1373 get_winopts(buf);
1374#ifdef FEAT_FOLDING
1375 else
1376 /* Remove all folds in the window. */
1377 clearFolding(curwin);
1378 foldUpdateAll(curwin); /* update folds (later). */
1379#endif
1380
1381 /* Get the buffer in the current window. */
1382 curwin->w_buffer = buf;
1383 curbuf = buf;
1384 ++curbuf->b_nwindows;
1385
1386#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001387 if (curwin->w_p_diff)
1388 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389#endif
1390
1391 /* Cursor on first line by default. */
1392 curwin->w_cursor.lnum = 1;
1393 curwin->w_cursor.col = 0;
1394#ifdef FEAT_VIRTUALEDIT
1395 curwin->w_cursor.coladd = 0;
1396#endif
1397 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001398#ifdef FEAT_AUTOCMD
1399 curwin->w_topline_was_set = FALSE;
1400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001402 /* mark cursor position as being invalid */
1403 curwin->w_valid = 0;
1404
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 /* Make sure the buffer is loaded. */
1406 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001407 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001408#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001409 /* If there is no filetype, allow for detecting one. Esp. useful for
1410 * ":ball" used in a autocommand. If there already is a filetype we
1411 * might prefer to keep it. */
1412 if (*curbuf->b_p_ft == NUL)
1413 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001414#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 open_buffer(FALSE, NULL);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 else
1419 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001420 if (!msg_silent)
1421 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1423#ifdef FEAT_AUTOCMD
1424 curwin->w_topline = 1;
1425# ifdef FEAT_DIFF
1426 curwin->w_topfill = 0;
1427# endif
1428 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1429 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1430#endif
1431 }
1432
1433 /* If autocommands did not change the cursor position, restore cursor lnum
1434 * and possibly cursor col. */
1435 if (curwin->w_cursor.lnum == 1 && inindent(0))
1436 buflist_getfpos();
1437
1438 check_arg_idx(curwin); /* check for valid arg_idx */
1439#ifdef FEAT_TITLE
1440 maketitle();
1441#endif
1442#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001443 /* when autocmds didn't change it */
1444 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445#endif
1446 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1447
Bram Moolenaar009b2592004-10-24 19:18:58 +00001448#ifdef FEAT_NETBEANS_INTG
1449 /* Send fileOpened event because we've changed buffers. */
1450 if (usingNetbeans && isNetbeansBuffer(curbuf))
1451 netbeans_file_activated(curbuf);
1452#endif
1453
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001454 /* Change directories when the 'acd' option is set. */
1455 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
1457#ifdef FEAT_KEYMAP
1458 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001459 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001461#ifdef FEAT_SPELL
1462 /* May need to set the spell language. Can only do this after the buffer
1463 * has been properly setup. */
1464 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001465 (void)did_set_spelllang(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001466#endif
1467
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 redraw_later(NOT_VALID);
1469}
1470
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001471#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1472/*
1473 * Change to the directory of the current buffer.
1474 */
1475 void
1476do_autochdir()
1477{
1478 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1479 shorten_fnames(TRUE);
1480}
1481#endif
1482
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483/*
1484 * functions for dealing with the buffer list
1485 */
1486
1487/*
1488 * Add a file name to the buffer list. Return a pointer to the buffer.
1489 * If the same file name already exists return a pointer to that buffer.
1490 * If it does not exist, or if fname == NULL, a new entry is created.
1491 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1492 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1493 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 * This is the ONLY way to create a new buffer.
1495 */
1496static int top_file_num = 1; /* highest file number */
1497
1498 buf_T *
1499buflist_new(ffname, sfname, lnum, flags)
1500 char_u *ffname; /* full path of fname or relative */
1501 char_u *sfname; /* short fname or NULL */
1502 linenr_T lnum; /* preferred cursor line */
1503 int flags; /* BLN_ defines */
1504{
1505 buf_T *buf;
1506#ifdef UNIX
1507 struct stat st;
1508#endif
1509
1510 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1511
1512 /*
1513 * If file name already exists in the list, update the entry.
1514 */
1515#ifdef UNIX
1516 /* On Unix we can use inode numbers when the file exists. Works better
1517 * for hard links. */
1518 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1519 st.st_dev = (dev_T)-1;
1520#endif
1521 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1522#ifdef UNIX
1523 buflist_findname_stat(ffname, &st)
1524#else
1525 buflist_findname(ffname)
1526#endif
1527 ) != NULL)
1528 {
1529 vim_free(ffname);
1530 if (lnum != 0)
1531 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1532 /* copy the options now, if 'cpo' doesn't have 's' and not done
1533 * already */
1534 buf_copy_options(buf, 0);
1535 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1536 {
1537 buf->b_p_bl = TRUE;
1538#ifdef FEAT_AUTOCMD
1539 if (!(flags & BLN_DUMMY))
1540 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1541#endif
1542 }
1543 return buf;
1544 }
1545
1546 /*
1547 * If the current buffer has no name and no contents, use the current
1548 * buffer. Otherwise: Need to allocate a new buffer structure.
1549 *
1550 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001551 * (A spell file buffer is allocated in spell.c, but that's not a normal
1552 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 */
1554 buf = NULL;
1555 if ((flags & BLN_CURBUF)
1556 && curbuf != NULL
1557 && curbuf->b_ffname == NULL
1558 && curbuf->b_nwindows <= 1
1559 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1560 {
1561 buf = curbuf;
1562#ifdef FEAT_AUTOCMD
1563 /* It's like this buffer is deleted. Watch out for autocommands that
1564 * change curbuf! If that happens, allocate a new buffer anyway. */
1565 if (curbuf->b_p_bl)
1566 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1567 if (buf == curbuf)
1568 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1569# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001570 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 return NULL;
1572# endif
1573#endif
1574#ifdef FEAT_QUICKFIX
1575# ifdef FEAT_AUTOCMD
1576 if (buf == curbuf)
1577# endif
1578 {
1579 /* Make sure 'bufhidden' and 'buftype' are empty */
1580 clear_string_option(&buf->b_p_bh);
1581 clear_string_option(&buf->b_p_bt);
1582 }
1583#endif
1584 }
1585 if (buf != curbuf || curbuf == NULL)
1586 {
1587 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1588 if (buf == NULL)
1589 {
1590 vim_free(ffname);
1591 return NULL;
1592 }
1593 }
1594
1595 if (ffname != NULL)
1596 {
1597 buf->b_ffname = ffname;
1598 buf->b_sfname = vim_strsave(sfname);
1599 }
1600
1601 clear_wininfo(buf);
1602 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1603
1604 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1605 || buf->b_wininfo == NULL)
1606 {
1607 vim_free(buf->b_ffname);
1608 buf->b_ffname = NULL;
1609 vim_free(buf->b_sfname);
1610 buf->b_sfname = NULL;
1611 if (buf != curbuf)
1612 free_buffer(buf);
1613 return NULL;
1614 }
1615
1616 if (buf == curbuf)
1617 {
1618 /* free all things allocated for this buffer */
1619 buf_freeall(buf, FALSE, FALSE);
1620 if (buf != curbuf) /* autocommands deleted the buffer! */
1621 return NULL;
1622#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001623 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 return NULL;
1625#endif
1626 /* buf->b_nwindows = 0; why was this here? */
1627 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1628#ifdef FEAT_KEYMAP
1629 /* need to reload lmaps and set b:keymap_name */
1630 curbuf->b_kmap_state |= KEYMAP_INIT;
1631#endif
1632 }
1633 else
1634 {
1635 /*
1636 * put new buffer at the end of the buffer list
1637 */
1638 buf->b_next = NULL;
1639 if (firstbuf == NULL) /* buffer list is empty */
1640 {
1641 buf->b_prev = NULL;
1642 firstbuf = buf;
1643 }
1644 else /* append new buffer at end of list */
1645 {
1646 lastbuf->b_next = buf;
1647 buf->b_prev = lastbuf;
1648 }
1649 lastbuf = buf;
1650
1651 buf->b_fnum = top_file_num++;
1652 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1653 {
1654 EMSG(_("W14: Warning: List of file names overflow"));
1655 if (emsg_silent == 0)
1656 {
1657 out_flush();
1658 ui_delay(3000L, TRUE); /* make sure it is noticed */
1659 }
1660 top_file_num = 1;
1661 }
1662
1663 /*
1664 * Always copy the options from the current buffer.
1665 */
1666 buf_copy_options(buf, BCO_ALWAYS);
1667 }
1668
1669 buf->b_wininfo->wi_fpos.lnum = lnum;
1670 buf->b_wininfo->wi_win = curwin;
1671
1672#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001673 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1674#endif
1675#ifdef FEAT_SYN_HL
1676 hash_init(&buf->b_keywtab);
1677 hash_init(&buf->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678#endif
1679
1680 buf->b_fname = buf->b_sfname;
1681#ifdef UNIX
1682 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001683 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 else
1685 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001686 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 buf->b_dev = st.st_dev;
1688 buf->b_ino = st.st_ino;
1689 }
1690#endif
1691 buf->b_u_synced = TRUE;
1692 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001693 if (flags & BLN_DUMMY)
1694 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 buf_clear_file(buf);
1696 clrallmarks(buf); /* clear marks */
1697 fmarks_check_names(buf); /* check file marks for this file */
1698 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1699#ifdef FEAT_AUTOCMD
1700 if (!(flags & BLN_DUMMY))
1701 {
1702 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1703 if (flags & BLN_LISTED)
1704 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1705# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001706 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 return NULL;
1708# endif
1709 }
1710#endif
1711
1712 return buf;
1713}
1714
1715/*
1716 * Free the memory for the options of a buffer.
1717 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1718 * 'fileencoding'.
1719 */
1720 void
1721free_buf_options(buf, free_p_ff)
1722 buf_T *buf;
1723 int free_p_ff;
1724{
1725 if (free_p_ff)
1726 {
1727#ifdef FEAT_MBYTE
1728 clear_string_option(&buf->b_p_fenc);
1729#endif
1730 clear_string_option(&buf->b_p_ff);
1731#ifdef FEAT_QUICKFIX
1732 clear_string_option(&buf->b_p_bh);
1733 clear_string_option(&buf->b_p_bt);
1734#endif
1735 }
1736#ifdef FEAT_FIND_ID
1737 clear_string_option(&buf->b_p_def);
1738 clear_string_option(&buf->b_p_inc);
1739# ifdef FEAT_EVAL
1740 clear_string_option(&buf->b_p_inex);
1741# endif
1742#endif
1743#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1744 clear_string_option(&buf->b_p_inde);
1745 clear_string_option(&buf->b_p_indk);
1746#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001747#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1748 clear_string_option(&buf->b_p_bexpr);
1749#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001750#if defined(FEAT_EVAL)
1751 clear_string_option(&buf->b_p_fex);
1752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753#ifdef FEAT_CRYPT
1754 clear_string_option(&buf->b_p_key);
1755#endif
1756 clear_string_option(&buf->b_p_kp);
1757 clear_string_option(&buf->b_p_mps);
1758 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001759 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 clear_string_option(&buf->b_p_isk);
1761#ifdef FEAT_KEYMAP
1762 clear_string_option(&buf->b_p_keymap);
1763 ga_clear(&buf->b_kmap_ga);
1764#endif
1765#ifdef FEAT_COMMENTS
1766 clear_string_option(&buf->b_p_com);
1767#endif
1768#ifdef FEAT_FOLDING
1769 clear_string_option(&buf->b_p_cms);
1770#endif
1771 clear_string_option(&buf->b_p_nf);
1772#ifdef FEAT_SYN_HL
1773 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001774#endif
1775#ifdef FEAT_SPELL
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001776 clear_string_option(&buf->b_p_spc);
Bram Moolenaar86bc1fb2005-06-07 20:58:01 +00001777 clear_string_option(&buf->b_p_spf);
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001778 vim_free(buf->b_cap_prog);
1779 buf->b_cap_prog = NULL;
Bram Moolenaara0dea672005-03-20 22:23:10 +00001780 clear_string_option(&buf->b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781#endif
1782#ifdef FEAT_SEARCHPATH
1783 clear_string_option(&buf->b_p_sua);
1784#endif
1785#ifdef FEAT_AUTOCMD
1786 clear_string_option(&buf->b_p_ft);
1787#endif
1788#ifdef FEAT_OSFILETYPE
1789 clear_string_option(&buf->b_p_oft);
1790#endif
1791#ifdef FEAT_CINDENT
1792 clear_string_option(&buf->b_p_cink);
1793 clear_string_option(&buf->b_p_cino);
1794#endif
1795#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1796 clear_string_option(&buf->b_p_cinw);
1797#endif
1798#ifdef FEAT_INS_EXPAND
1799 clear_string_option(&buf->b_p_cpt);
1800#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001801#ifdef FEAT_COMPL_FUNC
1802 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001803 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805#ifdef FEAT_QUICKFIX
1806 clear_string_option(&buf->b_p_gp);
1807 clear_string_option(&buf->b_p_mp);
1808 clear_string_option(&buf->b_p_efm);
1809#endif
1810 clear_string_option(&buf->b_p_ep);
1811 clear_string_option(&buf->b_p_path);
1812 clear_string_option(&buf->b_p_tags);
1813#ifdef FEAT_INS_EXPAND
1814 clear_string_option(&buf->b_p_dict);
1815 clear_string_option(&buf->b_p_tsr);
1816#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001817#ifdef FEAT_TEXTOBJ
1818 clear_string_option(&buf->b_p_qe);
1819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 buf->b_p_ar = -1;
1821}
1822
1823/*
1824 * get alternate file n
1825 * set linenr to lnum or altfpos.lnum if lnum == 0
1826 * also set cursor column to altfpos.col if 'startofline' is not set.
1827 * if (options & GETF_SETMARK) call setpcmark()
1828 * if (options & GETF_ALT) we are jumping to an alternate file.
1829 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1830 *
1831 * return FAIL for failure, OK for success
1832 */
1833 int
1834buflist_getfile(n, lnum, options, forceit)
1835 int n;
1836 linenr_T lnum;
1837 int options;
1838 int forceit;
1839{
1840 buf_T *buf;
1841#ifdef FEAT_WINDOWS
1842 win_T *wp = NULL;
1843#endif
1844 pos_T *fpos;
1845 colnr_T col;
1846
1847 buf = buflist_findnr(n);
1848 if (buf == NULL)
1849 {
1850 if ((options & GETF_ALT) && n == 0)
1851 EMSG(_(e_noalt));
1852 else
1853 EMSGN(_("E92: Buffer %ld not found"), n);
1854 return FAIL;
1855 }
1856
1857 /* if alternate file is the current buffer, nothing to do */
1858 if (buf == curbuf)
1859 return OK;
1860
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001861 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001862 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001863 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001865 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001866#ifdef FEAT_AUTOCMD
1867 if (curbuf_locked())
1868 return FAIL;
1869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870
1871 /* altfpos may be changed by getfile(), get it now */
1872 if (lnum == 0)
1873 {
1874 fpos = buflist_findfpos(buf);
1875 lnum = fpos->lnum;
1876 col = fpos->col;
1877 }
1878 else
1879 col = 0;
1880
1881#ifdef FEAT_WINDOWS
1882 if (options & GETF_SWITCH)
1883 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001884 /* If 'switchbuf' contains "useopen": jump to first window containing
1885 * "buf" if one exists */
1886 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001888 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1889 * page containing "buf" if one exists */
1890 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001891 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001892 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1893 * isn't empty: open new window */
1894 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001896 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1897 tabpage_new();
1898 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 return FAIL;
1900# ifdef FEAT_SCROLLBIND
1901 curwin->w_p_scb = FALSE;
1902# endif
1903 }
1904 }
1905#endif
1906
1907 ++RedrawingDisabled;
1908 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1909 lnum, forceit) <= 0)
1910 {
1911 --RedrawingDisabled;
1912
1913 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1914 if (!p_sol && col != 0)
1915 {
1916 curwin->w_cursor.col = col;
1917 check_cursor_col();
1918#ifdef FEAT_VIRTUALEDIT
1919 curwin->w_cursor.coladd = 0;
1920#endif
1921 curwin->w_set_curswant = TRUE;
1922 }
1923 return OK;
1924 }
1925 --RedrawingDisabled;
1926 return FAIL;
1927}
1928
1929/*
1930 * go to the last know line number for the current buffer
1931 */
1932 void
1933buflist_getfpos()
1934{
1935 pos_T *fpos;
1936
1937 fpos = buflist_findfpos(curbuf);
1938
1939 curwin->w_cursor.lnum = fpos->lnum;
1940 check_cursor_lnum();
1941
1942 if (p_sol)
1943 curwin->w_cursor.col = 0;
1944 else
1945 {
1946 curwin->w_cursor.col = fpos->col;
1947 check_cursor_col();
1948#ifdef FEAT_VIRTUALEDIT
1949 curwin->w_cursor.coladd = 0;
1950#endif
1951 curwin->w_set_curswant = TRUE;
1952 }
1953}
1954
Bram Moolenaar81695252004-12-29 20:58:21 +00001955#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1956/*
1957 * Find file in buffer list by name (it has to be for the current window).
1958 * Returns NULL if not found.
1959 */
1960 buf_T *
1961buflist_findname_exp(fname)
1962 char_u *fname;
1963{
1964 char_u *ffname;
1965 buf_T *buf = NULL;
1966
1967 /* First make the name into a full path name */
1968 ffname = FullName_save(fname,
1969#ifdef UNIX
1970 TRUE /* force expansion, get rid of symbolic links */
1971#else
1972 FALSE
1973#endif
1974 );
1975 if (ffname != NULL)
1976 {
1977 buf = buflist_findname(ffname);
1978 vim_free(ffname);
1979 }
1980 return buf;
1981}
1982#endif
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984/*
1985 * Find file in buffer list by name (it has to be for the current window).
1986 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00001987 * Skips dummy buffers.
1988 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 */
1990 buf_T *
1991buflist_findname(ffname)
1992 char_u *ffname;
1993{
1994#ifdef UNIX
1995 struct stat st;
1996
1997 if (mch_stat((char *)ffname, &st) < 0)
1998 st.st_dev = (dev_T)-1;
1999 return buflist_findname_stat(ffname, &st);
2000}
2001
2002/*
2003 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2004 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002005 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 */
2007 static buf_T *
2008buflist_findname_stat(ffname, stp)
2009 char_u *ffname;
2010 struct stat *stp;
2011{
2012#endif
2013 buf_T *buf;
2014
2015 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002016 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017#ifdef UNIX
2018 , stp
2019#endif
2020 ))
2021 return buf;
2022 return NULL;
2023}
2024
2025#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2026/*
2027 * Find file in buffer list by a regexp pattern.
2028 * Return fnum of the found buffer.
2029 * Return < 0 for error.
2030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 int
2032buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2033 char_u *pattern;
2034 char_u *pattern_end; /* pointer to first char after pattern */
2035 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002036 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037{
2038 buf_T *buf;
2039 regprog_T *prog;
2040 int match = -1;
2041 int find_listed;
2042 char_u *pat;
2043 char_u *patend;
2044 int attempt;
2045 char_u *p;
2046 int toggledollar;
2047
2048 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2049 {
2050 if (*pattern == '%')
2051 match = curbuf->b_fnum;
2052 else
2053 match = curwin->w_alt_fnum;
2054#ifdef FEAT_DIFF
2055 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2056 match = -1;
2057#endif
2058 }
2059
2060 /*
2061 * Try four ways of matching a listed buffer:
2062 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002063 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 * attempt == 2: with '$' at end (only match at end)
2065 * attempt == 3: with '^' at start and '$' at end (only full match)
2066 * Repeat this for finding an unlisted buffer if there was no matching
2067 * listed buffer.
2068 */
2069 else
2070 {
2071 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2072 if (pat == NULL)
2073 return -1;
2074 patend = pat + STRLEN(pat) - 1;
2075 toggledollar = (patend > pat && *patend == '$');
2076
2077 /* First try finding a listed buffer. If not found and "unlisted"
2078 * is TRUE, try finding an unlisted buffer. */
2079 find_listed = TRUE;
2080 for (;;)
2081 {
2082 for (attempt = 0; attempt <= 3; ++attempt)
2083 {
2084 /* may add '^' and '$' */
2085 if (toggledollar)
2086 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2087 p = pat;
2088 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2089 ++p;
2090 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2091 if (prog == NULL)
2092 {
2093 vim_free(pat);
2094 return -1;
2095 }
2096
2097 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2098 if (buf->b_p_bl == find_listed
2099#ifdef FEAT_DIFF
2100 && (!diffmode || diff_mode_buf(buf))
2101#endif
2102 && buflist_match(prog, buf) != NULL)
2103 {
2104 if (match >= 0) /* already found a match */
2105 {
2106 match = -2;
2107 break;
2108 }
2109 match = buf->b_fnum; /* remember first match */
2110 }
2111
2112 vim_free(prog);
2113 if (match >= 0) /* found one match */
2114 break;
2115 }
2116
2117 /* Only search for unlisted buffers if there was no match with
2118 * a listed buffer. */
2119 if (!unlisted || !find_listed || match != -1)
2120 break;
2121 find_listed = FALSE;
2122 }
2123
2124 vim_free(pat);
2125 }
2126
2127 if (match == -2)
2128 EMSG2(_("E93: More than one match for %s"), pattern);
2129 else if (match < 0)
2130 EMSG2(_("E94: No matching buffer for %s"), pattern);
2131 return match;
2132}
2133#endif
2134
2135#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2136
2137/*
2138 * Find all buffer names that match.
2139 * For command line expansion of ":buf" and ":sbuf".
2140 * Return OK if matches found, FAIL otherwise.
2141 */
2142 int
2143ExpandBufnames(pat, num_file, file, options)
2144 char_u *pat;
2145 int *num_file;
2146 char_u ***file;
2147 int options;
2148{
2149 int count = 0;
2150 buf_T *buf;
2151 int round;
2152 char_u *p;
2153 int attempt;
2154 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002155 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
2157 *num_file = 0; /* return values in case of FAIL */
2158 *file = NULL;
2159
Bram Moolenaar05159a02005-02-26 23:04:13 +00002160 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2161 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002163 patc = alloc((unsigned)STRLEN(pat) + 11);
2164 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002166 STRCPY(patc, "\\(^\\|[\\/]\\)");
2167 STRCPY(patc + 11, pat + 1);
2168 }
2169 else
2170 patc = pat;
2171
2172 /*
2173 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002174 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002175 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002176 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002177 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002178 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002179 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002180 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002181 if (prog == NULL)
2182 {
2183 if (patc != pat)
2184 vim_free(patc);
2185 return FAIL;
2186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
2188 /*
2189 * round == 1: Count the matches.
2190 * round == 2: Build the array to keep the matches.
2191 */
2192 for (round = 1; round <= 2; ++round)
2193 {
2194 count = 0;
2195 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2196 {
2197 if (!buf->b_p_bl) /* skip unlisted buffers */
2198 continue;
2199 p = buflist_match(prog, buf);
2200 if (p != NULL)
2201 {
2202 if (round == 1)
2203 ++count;
2204 else
2205 {
2206 if (options & WILD_HOME_REPLACE)
2207 p = home_replace_save(buf, p);
2208 else
2209 p = vim_strsave(p);
2210 (*file)[count++] = p;
2211 }
2212 }
2213 }
2214 if (count == 0) /* no match found, break here */
2215 break;
2216 if (round == 1)
2217 {
2218 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2219 if (*file == NULL)
2220 {
2221 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002222 if (patc != pat)
2223 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 return FAIL;
2225 }
2226 }
2227 }
2228 vim_free(prog);
2229 if (count) /* match(es) found, break here */
2230 break;
2231 }
2232
Bram Moolenaar05159a02005-02-26 23:04:13 +00002233 if (patc != pat)
2234 vim_free(patc);
2235
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 *num_file = count;
2237 return (count == 0 ? FAIL : OK);
2238}
2239
2240#endif /* FEAT_CMDL_COMPL */
2241
2242#ifdef HAVE_BUFLIST_MATCH
2243/*
2244 * Check for a match on the file name for buffer "buf" with regprog "prog".
2245 */
2246 static char_u *
2247buflist_match(prog, buf)
2248 regprog_T *prog;
2249 buf_T *buf;
2250{
2251 char_u *match;
2252
2253 /* First try the short file name, then the long file name. */
2254 match = fname_match(prog, buf->b_sfname);
2255 if (match == NULL)
2256 match = fname_match(prog, buf->b_ffname);
2257
2258 return match;
2259}
2260
2261/*
2262 * Try matching the regexp in "prog" with file name "name".
2263 * Return "name" when there is a match, NULL when not.
2264 */
2265 static char_u *
2266fname_match(prog, name)
2267 regprog_T *prog;
2268 char_u *name;
2269{
2270 char_u *match = NULL;
2271 char_u *p;
2272 regmatch_T regmatch;
2273
2274 if (name != NULL)
2275 {
2276 regmatch.regprog = prog;
2277#ifdef CASE_INSENSITIVE_FILENAME
2278 regmatch.rm_ic = TRUE; /* Always ignore case */
2279#else
2280 regmatch.rm_ic = FALSE; /* Never ignore case */
2281#endif
2282
2283 if (vim_regexec(&regmatch, name, (colnr_T)0))
2284 match = name;
2285 else
2286 {
2287 /* Replace $(HOME) with '~' and try matching again. */
2288 p = home_replace_save(NULL, name);
2289 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2290 match = name;
2291 vim_free(p);
2292 }
2293 }
2294
2295 return match;
2296}
2297#endif
2298
2299/*
2300 * find file in buffer list by number
2301 */
2302 buf_T *
2303buflist_findnr(nr)
2304 int nr;
2305{
2306 buf_T *buf;
2307
2308 if (nr == 0)
2309 nr = curwin->w_alt_fnum;
2310 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2311 if (buf->b_fnum == nr)
2312 return (buf);
2313 return NULL;
2314}
2315
2316/*
2317 * Get name of file 'n' in the buffer list.
2318 * When the file has no name an empty string is returned.
2319 * home_replace() is used to shorten the file name (used for marks).
2320 * Returns a pointer to allocated memory, of NULL when failed.
2321 */
2322 char_u *
2323buflist_nr2name(n, fullname, helptail)
2324 int n;
2325 int fullname;
2326 int helptail; /* for help buffers return tail only */
2327{
2328 buf_T *buf;
2329
2330 buf = buflist_findnr(n);
2331 if (buf == NULL)
2332 return NULL;
2333 return home_replace_save(helptail ? buf : NULL,
2334 fullname ? buf->b_ffname : buf->b_fname);
2335}
2336
2337/*
2338 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2339 * When "copy_options" is TRUE save the local window option values.
2340 * When "lnum" is 0 only do the options.
2341 */
2342 static void
2343buflist_setfpos(buf, win, lnum, col, copy_options)
2344 buf_T *buf;
2345 win_T *win;
2346 linenr_T lnum;
2347 colnr_T col;
2348 int copy_options;
2349{
2350 wininfo_T *wip;
2351
2352 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2353 if (wip->wi_win == win)
2354 break;
2355 if (wip == NULL)
2356 {
2357 /* allocate a new entry */
2358 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2359 if (wip == NULL)
2360 return;
2361 wip->wi_win = win;
2362 if (lnum == 0) /* set lnum even when it's 0 */
2363 lnum = 1;
2364 }
2365 else
2366 {
2367 /* remove the entry from the list */
2368 if (wip->wi_prev)
2369 wip->wi_prev->wi_next = wip->wi_next;
2370 else
2371 buf->b_wininfo = wip->wi_next;
2372 if (wip->wi_next)
2373 wip->wi_next->wi_prev = wip->wi_prev;
2374 if (copy_options && wip->wi_optset)
2375 {
2376 clear_winopt(&wip->wi_opt);
2377#ifdef FEAT_FOLDING
2378 deleteFoldRecurse(&wip->wi_folds);
2379#endif
2380 }
2381 }
2382 if (lnum != 0)
2383 {
2384 wip->wi_fpos.lnum = lnum;
2385 wip->wi_fpos.col = col;
2386 }
2387 if (copy_options)
2388 {
2389 /* Save the window-specific option values. */
2390 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2391#ifdef FEAT_FOLDING
2392 wip->wi_fold_manual = win->w_fold_manual;
2393 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2394#endif
2395 wip->wi_optset = TRUE;
2396 }
2397
2398 /* insert the entry in front of the list */
2399 wip->wi_next = buf->b_wininfo;
2400 buf->b_wininfo = wip;
2401 wip->wi_prev = NULL;
2402 if (wip->wi_next)
2403 wip->wi_next->wi_prev = wip;
2404
2405 return;
2406}
2407
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002408#ifdef FEAT_DIFF
2409static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2410
2411/*
2412 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2413 * page. That's because a diff is local to a tab page.
2414 */
2415 static int
2416wininfo_other_tab_diff(wip)
2417 wininfo_T *wip;
2418{
2419 win_T *wp;
2420
2421 if (wip->wi_opt.wo_diff)
2422 {
2423 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2424 /* return FALSE when it's a window in the current tab page, thus
2425 * the buffer was in diff mode here */
2426 if (wip->wi_win == wp)
2427 return FALSE;
2428 return TRUE;
2429 }
2430 return FALSE;
2431}
2432#endif
2433
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434/*
2435 * Find info for the current window in buffer "buf".
2436 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002437 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2438 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 * Returns NULL when there isn't any info.
2440 */
2441 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 Moolenaar0c094b92009-05-14 20:20:33 +00002444 int skip_diff_buffer UNUSED;
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 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 void
2545buflist_list(eap)
2546 exarg_T *eap;
2547{
2548 buf_T *buf;
2549 int len;
2550 int i;
2551
2552 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2553 {
2554 /* skip unlisted buffers, unless ! was used */
2555 if (!buf->b_p_bl && !eap->forceit)
2556 continue;
2557 msg_putchar('\n');
2558 if (buf_spname(buf) != NULL)
2559 STRCPY(NameBuff, buf_spname(buf));
2560 else
2561 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2562
Bram Moolenaar50cde822005-06-05 21:54:54 +00002563 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 buf->b_fnum,
2565 buf->b_p_bl ? ' ' : 'u',
2566 buf == curbuf ? '%' :
2567 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2568 buf->b_ml.ml_mfp == NULL ? ' ' :
2569 (buf->b_nwindows == 0 ? 'h' : 'a'),
2570 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2571 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002572 : (bufIsChanged(buf) ? '+' : ' '),
2573 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
2575 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 i = 40 - vim_strsize(IObuff);
2577 do
2578 {
2579 IObuff[len++] = ' ';
2580 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002581 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2582 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002583 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 msg_outtrans(IObuff);
2585 out_flush(); /* output one line at a time */
2586 ui_breakcheck();
2587 }
2588}
2589#endif
2590
2591/*
2592 * Get file name and line number for file 'fnum'.
2593 * Used by DoOneCmd() for translating '%' and '#'.
2594 * Used by insert_reg() and cmdline_paste() for '#' register.
2595 * Return FAIL if not found, OK for success.
2596 */
2597 int
2598buflist_name_nr(fnum, fname, lnum)
2599 int fnum;
2600 char_u **fname;
2601 linenr_T *lnum;
2602{
2603 buf_T *buf;
2604
2605 buf = buflist_findnr(fnum);
2606 if (buf == NULL || buf->b_fname == NULL)
2607 return FAIL;
2608
2609 *fname = buf->b_fname;
2610 *lnum = buflist_findlnum(buf);
2611
2612 return OK;
2613}
2614
2615/*
2616 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2617 * The file name with the full path is also remembered, for when :cd is used.
2618 * Returns FAIL for failure (file name already in use by other buffer)
2619 * OK otherwise.
2620 */
2621 int
2622setfname(buf, ffname, sfname, message)
2623 buf_T *buf;
2624 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002625 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626{
Bram Moolenaar81695252004-12-29 20:58:21 +00002627 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628#ifdef UNIX
2629 struct stat st;
2630#endif
2631
2632 if (ffname == NULL || *ffname == NUL)
2633 {
2634 /* Removing the name. */
2635 vim_free(buf->b_ffname);
2636 vim_free(buf->b_sfname);
2637 buf->b_ffname = NULL;
2638 buf->b_sfname = NULL;
2639#ifdef UNIX
2640 st.st_dev = (dev_T)-1;
2641#endif
2642 }
2643 else
2644 {
2645 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2646 if (ffname == NULL) /* out of memory */
2647 return FAIL;
2648
2649 /*
2650 * if the file name is already used in another buffer:
2651 * - if the buffer is loaded, fail
2652 * - if the buffer is not loaded, delete it from the list
2653 */
2654#ifdef UNIX
2655 if (mch_stat((char *)ffname, &st) < 0)
2656 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002657#endif
2658 if (!(buf->b_flags & BF_DUMMY))
2659#ifdef UNIX
2660 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002662 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663#endif
2664 if (obuf != NULL && obuf != buf)
2665 {
2666 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2667 {
2668 if (message)
2669 EMSG(_("E95: Buffer with this name already exists"));
2670 vim_free(ffname);
2671 return FAIL;
2672 }
2673 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2674 }
2675 sfname = vim_strsave(sfname);
2676 if (ffname == NULL || sfname == NULL)
2677 {
2678 vim_free(sfname);
2679 vim_free(ffname);
2680 return FAIL;
2681 }
2682#ifdef USE_FNAME_CASE
2683# ifdef USE_LONG_FNAME
2684 if (USE_LONG_FNAME)
2685# endif
2686 fname_case(sfname, 0); /* set correct case for short file name */
2687#endif
2688 vim_free(buf->b_ffname);
2689 vim_free(buf->b_sfname);
2690 buf->b_ffname = ffname;
2691 buf->b_sfname = sfname;
2692 }
2693 buf->b_fname = buf->b_sfname;
2694#ifdef UNIX
2695 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002696 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 else
2698 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002699 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 buf->b_dev = st.st_dev;
2701 buf->b_ino = st.st_ino;
2702 }
2703#endif
2704
2705#ifndef SHORT_FNAME
2706 buf->b_shortname = FALSE;
2707#endif
2708
2709 buf_name_changed(buf);
2710 return OK;
2711}
2712
2713/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002714 * Crude way of changing the name of a buffer. Use with care!
2715 * The name should be relative to the current directory.
2716 */
2717 void
2718buf_set_name(fnum, name)
2719 int fnum;
2720 char_u *name;
2721{
2722 buf_T *buf;
2723
2724 buf = buflist_findnr(fnum);
2725 if (buf != NULL)
2726 {
2727 vim_free(buf->b_sfname);
2728 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002729 buf->b_ffname = vim_strsave(name);
2730 buf->b_sfname = NULL;
2731 /* Allocate ffname and expand into full path. Also resolves .lnk
2732 * files on Win32. */
2733 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002734 buf->b_fname = buf->b_sfname;
2735 }
2736}
2737
2738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 * Take care of what needs to be done when the name of buffer "buf" has
2740 * changed.
2741 */
2742 void
2743buf_name_changed(buf)
2744 buf_T *buf;
2745{
2746 /*
2747 * If the file name changed, also change the name of the swapfile
2748 */
2749 if (buf->b_ml.ml_mfp != NULL)
2750 ml_setname(buf);
2751
2752 if (curwin->w_buffer == buf)
2753 check_arg_idx(curwin); /* check file name for arg list */
2754#ifdef FEAT_TITLE
2755 maketitle(); /* set window title */
2756#endif
2757#ifdef FEAT_WINDOWS
2758 status_redraw_all(); /* status lines need to be redrawn */
2759#endif
2760 fmarks_check_names(buf); /* check named file marks */
2761 ml_timestamp(buf); /* reset timestamp */
2762}
2763
2764/*
2765 * set alternate file name for current window
2766 *
2767 * Used by do_one_cmd(), do_write() and do_ecmd().
2768 * Return the buffer.
2769 */
2770 buf_T *
2771setaltfname(ffname, sfname, lnum)
2772 char_u *ffname;
2773 char_u *sfname;
2774 linenr_T lnum;
2775{
2776 buf_T *buf;
2777
2778 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2779 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002780 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 curwin->w_alt_fnum = buf->b_fnum;
2782 return buf;
2783}
2784
2785/*
2786 * Get alternate file name for current window.
2787 * Return NULL if there isn't any, and give error message if requested.
2788 */
2789 char_u *
2790getaltfname(errmsg)
2791 int errmsg; /* give error message */
2792{
2793 char_u *fname;
2794 linenr_T dummy;
2795
2796 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2797 {
2798 if (errmsg)
2799 EMSG(_(e_noalt));
2800 return NULL;
2801 }
2802 return fname;
2803}
2804
2805/*
2806 * Add a file name to the buflist and return its number.
2807 * Uses same flags as buflist_new(), except BLN_DUMMY.
2808 *
2809 * used by qf_init(), main() and doarglist()
2810 */
2811 int
2812buflist_add(fname, flags)
2813 char_u *fname;
2814 int flags;
2815{
2816 buf_T *buf;
2817
2818 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2819 if (buf != NULL)
2820 return buf->b_fnum;
2821 return 0;
2822}
2823
2824#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2825/*
2826 * Adjust slashes in file names. Called after 'shellslash' was set.
2827 */
2828 void
2829buflist_slash_adjust()
2830{
2831 buf_T *bp;
2832
2833 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2834 {
2835 if (bp->b_ffname != NULL)
2836 slash_adjust(bp->b_ffname);
2837 if (bp->b_sfname != NULL)
2838 slash_adjust(bp->b_sfname);
2839 }
2840}
2841#endif
2842
2843/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002844 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 * Also save the local window option values.
2846 */
2847 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002848buflist_altfpos(win)
2849 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002851 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852}
2853
2854/*
2855 * Return TRUE if 'ffname' is not the same file as current file.
2856 * Fname must have a full path (expanded by mch_FullName()).
2857 */
2858 int
2859otherfile(ffname)
2860 char_u *ffname;
2861{
2862 return otherfile_buf(curbuf, ffname
2863#ifdef UNIX
2864 , NULL
2865#endif
2866 );
2867}
2868
2869 static int
2870otherfile_buf(buf, ffname
2871#ifdef UNIX
2872 , stp
2873#endif
2874 )
2875 buf_T *buf;
2876 char_u *ffname;
2877#ifdef UNIX
2878 struct stat *stp;
2879#endif
2880{
2881 /* no name is different */
2882 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2883 return TRUE;
2884 if (fnamecmp(ffname, buf->b_ffname) == 0)
2885 return FALSE;
2886#ifdef UNIX
2887 {
2888 struct stat st;
2889
2890 /* If no struct stat given, get it now */
2891 if (stp == NULL)
2892 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002893 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 st.st_dev = (dev_T)-1;
2895 stp = &st;
2896 }
2897 /* Use dev/ino to check if the files are the same, even when the names
2898 * are different (possible with links). Still need to compare the
2899 * name above, for when the file doesn't exist yet.
2900 * Problem: The dev/ino changes when a file is deleted (and created
2901 * again) and remains the same when renamed/moved. We don't want to
2902 * mch_stat() each buffer each time, that would be too slow. Get the
2903 * dev/ino again when they appear to match, but not when they appear
2904 * to be different: Could skip a buffer when it's actually the same
2905 * file. */
2906 if (buf_same_ino(buf, stp))
2907 {
2908 buf_setino(buf);
2909 if (buf_same_ino(buf, stp))
2910 return FALSE;
2911 }
2912 }
2913#endif
2914 return TRUE;
2915}
2916
2917#if defined(UNIX) || defined(PROTO)
2918/*
2919 * Set inode and device number for a buffer.
2920 * Must always be called when b_fname is changed!.
2921 */
2922 void
2923buf_setino(buf)
2924 buf_T *buf;
2925{
2926 struct stat st;
2927
2928 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2929 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002930 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 buf->b_dev = st.st_dev;
2932 buf->b_ino = st.st_ino;
2933 }
2934 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002935 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936}
2937
2938/*
2939 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2940 */
2941 static int
2942buf_same_ino(buf, stp)
2943 buf_T *buf;
2944 struct stat *stp;
2945{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002946 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 && stp->st_dev == buf->b_dev
2948 && stp->st_ino == buf->b_ino);
2949}
2950#endif
2951
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002952/*
2953 * Print info about the current buffer.
2954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 void
2956fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002957 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 int shorthelp;
2959 int dont_truncate;
2960{
2961 char_u *name;
2962 int n;
2963 char_u *p;
2964 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002965 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966
2967 buffer = alloc(IOSIZE);
2968 if (buffer == NULL)
2969 return;
2970
2971 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2972 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002973 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 p = buffer + STRLEN(buffer);
2975 }
2976 else
2977 p = buffer;
2978
2979 *p++ = '"';
2980 if (buf_spname(curbuf) != NULL)
2981 STRCPY(p, buf_spname(curbuf));
2982 else
2983 {
2984 if (!fullname && curbuf->b_fname != NULL)
2985 name = curbuf->b_fname;
2986 else
2987 name = curbuf->b_ffname;
2988 home_replace(shorthelp ? curbuf : NULL, name, p,
2989 (int)(IOSIZE - (p - buffer)), TRUE);
2990 }
2991
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002992 len = STRLEN(buffer);
2993 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 "\"%s%s%s%s%s%s",
2995 curbufIsChanged() ? (shortmess(SHM_MOD)
2996 ? " [+]" : _(" [Modified]")) : " ",
2997 (curbuf->b_flags & BF_NOTEDITED)
2998#ifdef FEAT_QUICKFIX
2999 && !bt_dontwrite(curbuf)
3000#endif
3001 ? _("[Not edited]") : "",
3002 (curbuf->b_flags & BF_NEW)
3003#ifdef FEAT_QUICKFIX
3004 && !bt_dontwrite(curbuf)
3005#endif
3006 ? _("[New file]") : "",
3007 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3008 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3009 : _("[readonly]")) : "",
3010 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3011 || curbuf->b_p_ro) ?
3012 " " : "");
3013 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3014 * causes an overflow, thus for large numbers divide instead. */
3015 if (curwin->w_cursor.lnum > 1000000L)
3016 n = (int)(((long)curwin->w_cursor.lnum) /
3017 ((long)curbuf->b_ml.ml_line_count / 100L));
3018 else
3019 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3020 (long)curbuf->b_ml.ml_line_count);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003021 len = STRLEN(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3023 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003024 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 }
3026#ifdef FEAT_CMDL_INFO
3027 else if (p_ru)
3028 {
3029 /* Current line and column are already on the screen -- webb */
3030 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003031 vim_snprintf((char *)buffer + len, IOSIZE - len,
3032 _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003034 vim_snprintf((char *)buffer + len, IOSIZE - len,
3035 _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 (long)curbuf->b_ml.ml_line_count, n);
3037 }
3038#endif
3039 else
3040 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003041 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 _("line %ld of %ld --%d%%-- col "),
3043 (long)curwin->w_cursor.lnum,
3044 (long)curbuf->b_ml.ml_line_count,
3045 n);
3046 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003047 len = STRLEN(buffer);
3048 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3050 }
3051
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003052 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
3054 if (dont_truncate)
3055 {
3056 /* Temporarily set msg_scroll to avoid the message being truncated.
3057 * First call msg_start() to get the message in the right place. */
3058 msg_start();
3059 n = msg_scroll;
3060 msg_scroll = TRUE;
3061 msg(buffer);
3062 msg_scroll = n;
3063 }
3064 else
3065 {
3066 p = msg_trunc_attr(buffer, FALSE, 0);
3067 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 /* Need to repeat the message after redrawing when:
3069 * - When restart_edit is set (otherwise there will be a delay
3070 * before redrawing).
3071 * - When the screen was scrolled but there is no wait-return
3072 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003073 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 }
3075
3076 vim_free(buffer);
3077}
3078
3079 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003080col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003082 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 int col;
3084 int vcol;
3085{
3086 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003087 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003089 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090}
3091
3092#if defined(FEAT_TITLE) || defined(PROTO)
3093/*
3094 * put file name in title bar of window and in icon title
3095 */
3096
3097static char_u *lasttitle = NULL;
3098static char_u *lasticon = NULL;
3099
3100 void
3101maketitle()
3102{
3103 char_u *p;
3104 char_u *t_str = NULL;
3105 char_u *i_name;
3106 char_u *i_str = NULL;
3107 int maxlen = 0;
3108 int len;
3109 int mustset;
3110 char_u buf[IOSIZE];
3111 int off;
3112
3113 if (!redrawing())
3114 {
3115 /* Postpone updating the title when 'lazyredraw' is set. */
3116 need_maketitle = TRUE;
3117 return;
3118 }
3119
3120 need_maketitle = FALSE;
3121 if (!p_title && !p_icon)
3122 return;
3123
3124 if (p_title)
3125 {
3126 if (p_titlelen > 0)
3127 {
3128 maxlen = p_titlelen * Columns / 100;
3129 if (maxlen < 10)
3130 maxlen = 10;
3131 }
3132
3133 t_str = buf;
3134 if (*p_titlestring != NUL)
3135 {
3136#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003137 if (stl_syntax & STL_IN_TITLE)
3138 {
3139 int use_sandbox = FALSE;
3140 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003141
3142# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003143 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003144# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003145 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003147 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003148 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003149 if (called_emsg)
3150 set_string_option_direct((char_u *)"titlestring", -1,
3151 (char_u *)"", OPT_FREE, SID_ERROR);
3152 called_emsg |= save_called_emsg;
3153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 else
3155#endif
3156 t_str = p_titlestring;
3157 }
3158 else
3159 {
3160 /* format: "fname + (path) (1 of 2) - VIM" */
3161
3162 if (curbuf->b_fname == NULL)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003163 STRCPY(buf, _("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 else
3165 {
3166 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003167 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 }
3170
3171 switch (bufIsChanged(curbuf)
3172 + (curbuf->b_p_ro * 2)
3173 + (!curbuf->b_p_ma * 4))
3174 {
3175 case 1: STRCAT(buf, " +"); break;
3176 case 2: STRCAT(buf, " ="); break;
3177 case 3: STRCAT(buf, " =+"); break;
3178 case 4:
3179 case 6: STRCAT(buf, " -"); break;
3180 case 5:
3181 case 7: STRCAT(buf, " -+"); break;
3182 }
3183
3184 if (curbuf->b_fname != NULL)
3185 {
3186 /* Get path of file, replace home dir with ~ */
3187 off = (int)STRLEN(buf);
3188 buf[off++] = ' ';
3189 buf[off++] = '(';
3190 home_replace(curbuf, curbuf->b_ffname,
3191 buf + off, IOSIZE - off, TRUE);
3192#ifdef BACKSLASH_IN_FILENAME
3193 /* avoid "c:/name" to be reduced to "c" */
3194 if (isalpha(buf[off]) && buf[off + 1] == ':')
3195 off += 2;
3196#endif
3197 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003198 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003201 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003202 (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003205
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 /* translate unprintable chars */
3207 p = transstr(buf + off);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003208 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 STRCAT(buf, ")");
3211 }
3212
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003213 append_arg_number(curwin, buf, IOSIZE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
3215#if defined(FEAT_CLIENTSERVER)
3216 if (serverName != NULL)
3217 {
3218 STRCAT(buf, " - ");
3219 STRCAT(buf, serverName);
3220 }
3221 else
3222#endif
3223 STRCAT(buf, " - VIM");
3224
3225 if (maxlen > 0)
3226 {
3227 /* make it shorter by removing a bit in the middle */
3228 len = vim_strsize(buf);
3229 if (len > maxlen)
3230 trunc_string(buf, buf, maxlen);
3231 }
3232 }
3233 }
3234 mustset = ti_change(t_str, &lasttitle);
3235
3236 if (p_icon)
3237 {
3238 i_str = buf;
3239 if (*p_iconstring != NUL)
3240 {
3241#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003242 if (stl_syntax & STL_IN_ICON)
3243 {
3244 int use_sandbox = FALSE;
3245 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003246
3247# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003248 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003249# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003250 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003252 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003253 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003254 if (called_emsg)
3255 set_string_option_direct((char_u *)"iconstring", -1,
3256 (char_u *)"", OPT_FREE, SID_ERROR);
3257 called_emsg |= save_called_emsg;
3258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 else
3260#endif
3261 i_str = p_iconstring;
3262 }
3263 else
3264 {
3265 if (buf_spname(curbuf) != NULL)
3266 i_name = (char_u *)buf_spname(curbuf);
3267 else /* use file name only in icon */
3268 i_name = gettail(curbuf->b_ffname);
3269 *i_str = NUL;
3270 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003271 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 if (len > 100)
3273 {
3274 len -= 100;
3275#ifdef FEAT_MBYTE
3276 if (has_mbyte)
3277 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3278#endif
3279 i_name += len;
3280 }
3281 STRCPY(i_str, i_name);
3282 trans_characters(i_str, IOSIZE);
3283 }
3284 }
3285
3286 mustset |= ti_change(i_str, &lasticon);
3287
3288 if (mustset)
3289 resettitle();
3290}
3291
3292/*
3293 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3294 * from "str" if it does.
3295 * Return TRUE when "*last" changed.
3296 */
3297 static int
3298ti_change(str, last)
3299 char_u *str;
3300 char_u **last;
3301{
3302 if ((str == NULL) != (*last == NULL)
3303 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3304 {
3305 vim_free(*last);
3306 if (str == NULL)
3307 *last = NULL;
3308 else
3309 *last = vim_strsave(str);
3310 return TRUE;
3311 }
3312 return FALSE;
3313}
3314
3315/*
3316 * Put current window title back (used after calling a shell)
3317 */
3318 void
3319resettitle()
3320{
3321 mch_settitle(lasttitle, lasticon);
3322}
Bram Moolenaarea408852005-06-25 22:49:46 +00003323
3324# if defined(EXITFREE) || defined(PROTO)
3325 void
3326free_titles()
3327{
3328 vim_free(lasttitle);
3329 vim_free(lasticon);
3330}
3331# endif
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#endif /* FEAT_TITLE */
3334
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003335#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003337 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 * Return length of string in screen cells.
3339 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003340 * Normally works for window "wp", except when working for 'tabline' then it
3341 * is "curwin".
3342 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 * Items are drawn interspersed with the text that surrounds it
3344 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3345 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3346 *
3347 * If maxwidth is not zero, the string will be filled at any middle marker
3348 * or truncated if too long, fillchar is used for all whitespace.
3349 */
3350 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003351build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003353 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 size_t outlen; /* length of out[] */
3355 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003356 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 int fillchar;
3358 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003359 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3360 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
3362 char_u *p;
3363 char_u *s;
3364 char_u *t;
3365 char_u *linecont;
3366#ifdef FEAT_EVAL
3367 win_T *o_curwin;
3368 buf_T *o_curbuf;
3369#endif
3370 int empty_line;
3371 colnr_T virtcol;
3372 long l;
3373 long n;
3374 int prevchar_isflag;
3375 int prevchar_isitem;
3376 int itemisflag;
3377 int fillable;
3378 char_u *str;
3379 long num;
3380 int width;
3381 int itemcnt;
3382 int curitem;
3383 int groupitem[STL_MAX_ITEM];
3384 int groupdepth;
3385 struct stl_item
3386 {
3387 char_u *start;
3388 int minwid;
3389 int maxwid;
3390 enum
3391 {
3392 Normal,
3393 Empty,
3394 Group,
3395 Middle,
3396 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003397 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 Trunc
3399 } type;
3400 } item[STL_MAX_ITEM];
3401 int minwid;
3402 int maxwid;
3403 int zeropad;
3404 char_u base;
3405 char_u opt;
3406#define TMPLEN 70
3407 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003408 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003409 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003410
3411#ifdef FEAT_EVAL
3412 /*
3413 * When the format starts with "%!" then evaluate it as an expression and
3414 * use the result as the actual format string.
3415 */
3416 if (fmt[0] == '%' && fmt[1] == '!')
3417 {
3418 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3419 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003420 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003421 }
3422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
3424 if (fillchar == 0)
3425 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003426#ifdef FEAT_MBYTE
3427 /* Can't handle a multi-byte fill character yet. */
3428 else if (mb_char2len(fillchar) > 1)
3429 fillchar = '-';
3430#endif
3431
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 /*
3433 * Get line & check if empty (cursorpos will show "0-1").
3434 * If inversion is possible we use it. Else '=' characters are used.
3435 */
3436 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3437 empty_line = (*linecont == NUL);
3438
3439 groupdepth = 0;
3440 p = out;
3441 curitem = 0;
3442 prevchar_isflag = TRUE;
3443 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003444 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
3446 if (*s != NUL && *s != '%')
3447 prevchar_isflag = prevchar_isitem = FALSE;
3448
3449 /*
3450 * Handle up to the next '%' or the end.
3451 */
3452 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3453 *p++ = *s++;
3454 if (*s == NUL || p + 1 >= out + outlen)
3455 break;
3456
3457 /*
3458 * Handle one '%' item.
3459 */
3460 s++;
3461 if (*s == '%')
3462 {
3463 if (p + 1 >= out + outlen)
3464 break;
3465 *p++ = *s++;
3466 prevchar_isflag = prevchar_isitem = FALSE;
3467 continue;
3468 }
3469 if (*s == STL_MIDDLEMARK)
3470 {
3471 s++;
3472 if (groupdepth > 0)
3473 continue;
3474 item[curitem].type = Middle;
3475 item[curitem++].start = p;
3476 continue;
3477 }
3478 if (*s == STL_TRUNCMARK)
3479 {
3480 s++;
3481 item[curitem].type = Trunc;
3482 item[curitem++].start = p;
3483 continue;
3484 }
3485 if (*s == ')')
3486 {
3487 s++;
3488 if (groupdepth < 1)
3489 continue;
3490 groupdepth--;
3491
3492 t = item[groupitem[groupdepth]].start;
3493 *p = NUL;
3494 l = vim_strsize(t);
3495 if (curitem > groupitem[groupdepth] + 1
3496 && item[groupitem[groupdepth]].minwid == 0)
3497 {
3498 /* remove group if all items are empty */
3499 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3500 if (item[n].type == Normal)
3501 break;
3502 if (n == curitem)
3503 {
3504 p = t;
3505 l = 0;
3506 }
3507 }
3508 if (l > item[groupitem[groupdepth]].maxwid)
3509 {
3510 /* truncate, remove n bytes of text at the start */
3511#ifdef FEAT_MBYTE
3512 if (has_mbyte)
3513 {
3514 /* Find the first character that should be included. */
3515 n = 0;
3516 while (l >= item[groupitem[groupdepth]].maxwid)
3517 {
3518 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003519 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521 }
3522 else
3523#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003524 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
3526 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003527 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 p = p - n + 1;
3529#ifdef FEAT_MBYTE
3530 /* Fill up space left over by half a double-wide char. */
3531 while (++l < item[groupitem[groupdepth]].minwid)
3532 *p++ = fillchar;
3533#endif
3534
3535 /* correct the start of the items for the truncation */
3536 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3537 {
3538 item[l].start -= n;
3539 if (item[l].start < t)
3540 item[l].start = t;
3541 }
3542 }
3543 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3544 {
3545 /* fill */
3546 n = item[groupitem[groupdepth]].minwid;
3547 if (n < 0)
3548 {
3549 /* fill by appending characters */
3550 n = 0 - n;
3551 while (l++ < n && p + 1 < out + outlen)
3552 *p++ = fillchar;
3553 }
3554 else
3555 {
3556 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003557 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 l = n - l;
3559 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003560 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 p += l;
3562 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3563 item[n].start += l;
3564 for ( ; l > 0; l--)
3565 *t++ = fillchar;
3566 }
3567 }
3568 continue;
3569 }
3570 minwid = 0;
3571 maxwid = 9999;
3572 zeropad = FALSE;
3573 l = 1;
3574 if (*s == '0')
3575 {
3576 s++;
3577 zeropad = TRUE;
3578 }
3579 if (*s == '-')
3580 {
3581 s++;
3582 l = -1;
3583 }
3584 if (VIM_ISDIGIT(*s))
3585 {
3586 minwid = (int)getdigits(&s);
3587 if (minwid < 0) /* overflow */
3588 minwid = 0;
3589 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003590 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
3592 item[curitem].type = Highlight;
3593 item[curitem].start = p;
3594 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3595 s++;
3596 curitem++;
3597 continue;
3598 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003599 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3600 {
3601 if (*s == STL_TABCLOSENR)
3602 {
3603 if (minwid == 0)
3604 {
3605 /* %X ends the close label, go back to the previously
3606 * define tab label nr. */
3607 for (n = curitem - 1; n >= 0; --n)
3608 if (item[n].type == TabPage && item[n].minwid >= 0)
3609 {
3610 minwid = item[n].minwid;
3611 break;
3612 }
3613 }
3614 else
3615 /* close nrs are stored as negative values */
3616 minwid = - minwid;
3617 }
3618 item[curitem].type = TabPage;
3619 item[curitem].start = p;
3620 item[curitem].minwid = minwid;
3621 s++;
3622 curitem++;
3623 continue;
3624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 if (*s == '.')
3626 {
3627 s++;
3628 if (VIM_ISDIGIT(*s))
3629 {
3630 maxwid = (int)getdigits(&s);
3631 if (maxwid <= 0) /* overflow */
3632 maxwid = 50;
3633 }
3634 }
3635 minwid = (minwid > 50 ? 50 : minwid) * l;
3636 if (*s == '(')
3637 {
3638 groupitem[groupdepth++] = curitem;
3639 item[curitem].type = Group;
3640 item[curitem].start = p;
3641 item[curitem].minwid = minwid;
3642 item[curitem].maxwid = maxwid;
3643 s++;
3644 curitem++;
3645 continue;
3646 }
3647 if (vim_strchr(STL_ALL, *s) == NULL)
3648 {
3649 s++;
3650 continue;
3651 }
3652 opt = *s++;
3653
3654 /* OK - now for the real work */
3655 base = 'D';
3656 itemisflag = FALSE;
3657 fillable = TRUE;
3658 num = -1;
3659 str = NULL;
3660 switch (opt)
3661 {
3662 case STL_FILEPATH:
3663 case STL_FULLPATH:
3664 case STL_FILENAME:
3665 fillable = FALSE; /* don't change ' ' to fillchar */
3666 if (buf_spname(wp->w_buffer) != NULL)
3667 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3668 else
3669 {
3670 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003671 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3673 }
3674 trans_characters(NameBuff, MAXPATHL);
3675 if (opt != STL_FILENAME)
3676 str = NameBuff;
3677 else
3678 str = gettail(NameBuff);
3679 break;
3680
3681 case STL_VIM_EXPR: /* '{' */
3682 itemisflag = TRUE;
3683 t = p;
3684 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3685 *p++ = *s++;
3686 if (*s != '}') /* missing '}' or out of space */
3687 break;
3688 s++;
3689 *p = 0;
3690 p = t;
3691
3692#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003693 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3695
3696 o_curbuf = curbuf;
3697 o_curwin = curwin;
3698 curwin = wp;
3699 curbuf = wp->w_buffer;
3700
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003701 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702
3703 curwin = o_curwin;
3704 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003705 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
3707 if (str != NULL && *str != 0)
3708 {
3709 if (*skipdigits(str) == NUL)
3710 {
3711 num = atoi((char *)str);
3712 vim_free(str);
3713 str = NULL;
3714 itemisflag = FALSE;
3715 }
3716 }
3717#endif
3718 break;
3719
3720 case STL_LINE:
3721 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3722 ? 0L : (long)(wp->w_cursor.lnum);
3723 break;
3724
3725 case STL_NUMLINES:
3726 num = wp->w_buffer->b_ml.ml_line_count;
3727 break;
3728
3729 case STL_COLUMN:
3730 num = !(State & INSERT) && empty_line
3731 ? 0 : (int)wp->w_cursor.col + 1;
3732 break;
3733
3734 case STL_VIRTCOL:
3735 case STL_VIRTCOL_ALT:
3736 /* In list mode virtcol needs to be recomputed */
3737 virtcol = wp->w_virtcol;
3738 if (wp->w_p_list && lcs_tab1 == NUL)
3739 {
3740 wp->w_p_list = FALSE;
3741 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3742 wp->w_p_list = TRUE;
3743 }
3744 ++virtcol;
3745 /* Don't display %V if it's the same as %c. */
3746 if (opt == STL_VIRTCOL_ALT
3747 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3748 ? 0 : (int)wp->w_cursor.col + 1)))
3749 break;
3750 num = (long)virtcol;
3751 break;
3752
3753 case STL_PERCENTAGE:
3754 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3755 (long)wp->w_buffer->b_ml.ml_line_count);
3756 break;
3757
3758 case STL_ALTPERCENT:
3759 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003760 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 break;
3762
3763 case STL_ARGLISTSTAT:
3764 fillable = FALSE;
3765 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003766 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 str = tmp;
3768 break;
3769
3770 case STL_KEYMAP:
3771 fillable = FALSE;
3772 if (get_keymap_str(wp, tmp, TMPLEN))
3773 str = tmp;
3774 break;
3775 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003776#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003777 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778#else
3779 num = 0;
3780#endif
3781 break;
3782
3783 case STL_BUFNO:
3784 num = wp->w_buffer->b_fnum;
3785 break;
3786
3787 case STL_OFFSET_X:
3788 base = 'X';
3789 case STL_OFFSET:
3790#ifdef FEAT_BYTEOFF
3791 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3792 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3793 0L : l + 1 + (!(State & INSERT) && empty_line ?
3794 0 : (int)wp->w_cursor.col);
3795#endif
3796 break;
3797
3798 case STL_BYTEVAL_X:
3799 base = 'X';
3800 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003801 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 num = 0;
3803 else
3804 {
3805#ifdef FEAT_MBYTE
3806 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3807#else
3808 num = linecont[wp->w_cursor.col];
3809#endif
3810 }
3811 if (num == NL)
3812 num = 0;
3813 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3814 num = NL;
3815 break;
3816
3817 case STL_ROFLAG:
3818 case STL_ROFLAG_ALT:
3819 itemisflag = TRUE;
3820 if (wp->w_buffer->b_p_ro)
3821 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3822 break;
3823
3824 case STL_HELPFLAG:
3825 case STL_HELPFLAG_ALT:
3826 itemisflag = TRUE;
3827 if (wp->w_buffer->b_help)
3828 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003829 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 break;
3831
3832#ifdef FEAT_AUTOCMD
3833 case STL_FILETYPE:
3834 if (*wp->w_buffer->b_p_ft != NUL
3835 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3836 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003837 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3838 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 str = tmp;
3840 }
3841 break;
3842
3843 case STL_FILETYPE_ALT:
3844 itemisflag = TRUE;
3845 if (*wp->w_buffer->b_p_ft != NUL
3846 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3847 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003848 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3849 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 for (t = tmp; *t != 0; t++)
3851 *t = TOUPPER_LOC(*t);
3852 str = tmp;
3853 }
3854 break;
3855#endif
3856
3857#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3858 case STL_PREVIEWFLAG:
3859 case STL_PREVIEWFLAG_ALT:
3860 itemisflag = TRUE;
3861 if (wp->w_p_pvw)
3862 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3863 : _("[Preview]"));
3864 break;
3865#endif
3866
3867 case STL_MODIFIED:
3868 case STL_MODIFIED_ALT:
3869 itemisflag = TRUE;
3870 switch ((opt == STL_MODIFIED_ALT)
3871 + bufIsChanged(wp->w_buffer) * 2
3872 + (!wp->w_buffer->b_p_ma) * 4)
3873 {
3874 case 2: str = (char_u *)"[+]"; break;
3875 case 3: str = (char_u *)",+"; break;
3876 case 4: str = (char_u *)"[-]"; break;
3877 case 5: str = (char_u *)",-"; break;
3878 case 6: str = (char_u *)"[+-]"; break;
3879 case 7: str = (char_u *)",+-"; break;
3880 }
3881 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003882
3883 case STL_HIGHLIGHT:
3884 t = s;
3885 while (*s != '#' && *s != NUL)
3886 ++s;
3887 if (*s == '#')
3888 {
3889 item[curitem].type = Highlight;
3890 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003891 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003892 curitem++;
3893 }
3894 ++s;
3895 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
3897
3898 item[curitem].start = p;
3899 item[curitem].type = Normal;
3900 if (str != NULL && *str)
3901 {
3902 t = str;
3903 if (itemisflag)
3904 {
3905 if ((t[0] && t[1])
3906 && ((!prevchar_isitem && *t == ',')
3907 || (prevchar_isflag && *t == ' ')))
3908 t++;
3909 prevchar_isflag = TRUE;
3910 }
3911 l = vim_strsize(t);
3912 if (l > 0)
3913 prevchar_isitem = TRUE;
3914 if (l > maxwid)
3915 {
3916 while (l >= maxwid)
3917#ifdef FEAT_MBYTE
3918 if (has_mbyte)
3919 {
3920 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003921 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
3923 else
3924#endif
3925 l -= byte2cells(*t++);
3926 if (p + 1 >= out + outlen)
3927 break;
3928 *p++ = '<';
3929 }
3930 if (minwid > 0)
3931 {
3932 for (; l < minwid && p + 1 < out + outlen; l++)
3933 {
3934 /* Don't put a "-" in front of a digit. */
3935 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3936 *p++ = ' ';
3937 else
3938 *p++ = fillchar;
3939 }
3940 minwid = 0;
3941 }
3942 else
3943 minwid *= -1;
3944 while (*t && p + 1 < out + outlen)
3945 {
3946 *p++ = *t++;
3947 /* Change a space by fillchar, unless fillchar is '-' and a
3948 * digit follows. */
3949 if (fillable && p[-1] == ' '
3950 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3951 p[-1] = fillchar;
3952 }
3953 for (; l < minwid && p + 1 < out + outlen; l++)
3954 *p++ = fillchar;
3955 }
3956 else if (num >= 0)
3957 {
3958 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3959 char_u nstr[20];
3960
3961 if (p + 20 >= out + outlen)
3962 break; /* not sufficient space */
3963 prevchar_isitem = TRUE;
3964 t = nstr;
3965 if (opt == STL_VIRTCOL_ALT)
3966 {
3967 *t++ = '-';
3968 minwid--;
3969 }
3970 *t++ = '%';
3971 if (zeropad)
3972 *t++ = '0';
3973 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003974 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 *t = 0;
3976
3977 for (n = num, l = 1; n >= nbase; n /= nbase)
3978 l++;
3979 if (opt == STL_VIRTCOL_ALT)
3980 l++;
3981 if (l > maxwid)
3982 {
3983 l += 2;
3984 n = l - maxwid;
3985 while (l-- > maxwid)
3986 num /= nbase;
3987 *t++ = '>';
3988 *t++ = '%';
3989 *t = t[-3];
3990 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003991 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3992 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003995 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3996 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 p += STRLEN(p);
3998 }
3999 else
4000 item[curitem].type = Empty;
4001
4002 if (opt == STL_VIM_EXPR)
4003 vim_free(str);
4004
4005 if (num >= 0 || (!itemisflag && str && *str))
4006 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4007 curitem++;
4008 }
4009 *p = NUL;
4010 itemcnt = curitem;
4011
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004012#ifdef FEAT_EVAL
4013 if (usefmt != fmt)
4014 vim_free(usefmt);
4015#endif
4016
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 width = vim_strsize(out);
4018 if (maxwidth > 0 && width > maxwidth)
4019 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004020 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 l = 0;
4022 if (itemcnt == 0)
4023 s = out;
4024 else
4025 {
4026 for ( ; l < itemcnt; l++)
4027 if (item[l].type == Trunc)
4028 {
4029 /* Truncate at %< item. */
4030 s = item[l].start;
4031 break;
4032 }
4033 if (l == itemcnt)
4034 {
4035 /* No %< item, truncate first item. */
4036 s = item[0].start;
4037 l = 0;
4038 }
4039 }
4040
4041 if (width - vim_strsize(s) >= maxwidth)
4042 {
4043 /* Truncation mark is beyond max length */
4044#ifdef FEAT_MBYTE
4045 if (has_mbyte)
4046 {
4047 s = out;
4048 width = 0;
4049 for (;;)
4050 {
4051 width += ptr2cells(s);
4052 if (width >= maxwidth)
4053 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004054 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 }
4056 /* Fill up for half a double-wide character. */
4057 while (++width < maxwidth)
4058 *s++ = fillchar;
4059 }
4060 else
4061#endif
4062 s = out + maxwidth - 1;
4063 for (l = 0; l < itemcnt; l++)
4064 if (item[l].start > s)
4065 break;
4066 itemcnt = l;
4067 *s++ = '>';
4068 *s = 0;
4069 }
4070 else
4071 {
4072#ifdef FEAT_MBYTE
4073 if (has_mbyte)
4074 {
4075 n = 0;
4076 while (width >= maxwidth)
4077 {
4078 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004079 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 }
4081 }
4082 else
4083#endif
4084 n = width - maxwidth + 1;
4085 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004086 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 *s = '<';
4088
4089 /* Fill up for half a double-wide character. */
4090 while (++width < maxwidth)
4091 {
4092 s = s + STRLEN(s);
4093 *s++ = fillchar;
4094 *s = NUL;
4095 }
4096
4097 --n; /* count the '<' */
4098 for (; l < itemcnt; l++)
4099 {
4100 if (item[l].start - n >= s)
4101 item[l].start -= n;
4102 else
4103 item[l].start = s;
4104 }
4105 }
4106 width = maxwidth;
4107 }
4108 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4109 {
4110 /* Apply STL_MIDDLE if any */
4111 for (l = 0; l < itemcnt; l++)
4112 if (item[l].type == Middle)
4113 break;
4114 if (l < itemcnt)
4115 {
4116 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004117 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 for (s = item[l].start; s < p; s++)
4119 *s = fillchar;
4120 for (l++; l < itemcnt; l++)
4121 item[l].start += maxwidth - width;
4122 width = maxwidth;
4123 }
4124 }
4125
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004126 /* Store the info about highlighting. */
4127 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004129 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 for (l = 0; l < itemcnt; l++)
4131 {
4132 if (item[l].type == Highlight)
4133 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004134 sp->start = item[l].start;
4135 sp->userhl = item[l].minwid;
4136 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
4138 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004139 sp->start = NULL;
4140 sp->userhl = 0;
4141 }
4142
4143 /* Store the info about tab pages labels. */
4144 if (tabtab != NULL)
4145 {
4146 sp = tabtab;
4147 for (l = 0; l < itemcnt; l++)
4148 {
4149 if (item[l].type == TabPage)
4150 {
4151 sp->start = item[l].start;
4152 sp->userhl = item[l].minwid;
4153 sp++;
4154 }
4155 }
4156 sp->start = NULL;
4157 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
4159
4160 return width;
4161}
4162#endif /* FEAT_STL_OPT */
4163
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004164#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4165 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004167 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4168 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 */
4170 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004171get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004173 char_u *buf;
4174 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
4176 long above; /* number of lines above window */
4177 long below; /* number of lines below window */
4178
4179 above = wp->w_topline - 1;
4180#ifdef FEAT_DIFF
4181 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4182#endif
4183 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4184 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004185 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4186 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004188 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004190 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 ? (int)(above / ((above + below) / 100L))
4192 : (int)(above * 100L / (above + below)));
4193}
4194#endif
4195
4196/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004197 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 * Return TRUE if it was appended.
4199 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004200 static int
4201append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 win_T *wp;
4203 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004204 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206{
4207 char_u *p;
4208
4209 if (ARGCOUNT <= 1) /* nothing to do */
4210 return FALSE;
4211
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004212 p = buf + STRLEN(buf); /* go to the end of the buffer */
4213 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 return FALSE;
4215 *p++ = ' ';
4216 *p++ = '(';
4217 if (add_file)
4218 {
4219 STRCPY(p, "file ");
4220 p += 5;
4221 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004222 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4223 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4225 return TRUE;
4226}
4227
4228/*
4229 * If fname is not a full path, make it a full path.
4230 * Returns pointer to allocated memory (NULL for failure).
4231 */
4232 char_u *
4233fix_fname(fname)
4234 char_u *fname;
4235{
4236 /*
4237 * Force expanding the path always for Unix, because symbolic links may
4238 * mess up the full path name, even though it starts with a '/'.
4239 * Also expand when there is ".." in the file name, try to remove it,
4240 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004241 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 * For MS-Windows also expand names like "longna~1" to "longname".
4243 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004244#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 return FullName_save(fname, TRUE);
4246#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004247 if (!vim_isAbsName(fname)
4248 || strstr((char *)fname, "..") != NULL
4249 || strstr((char *)fname, "//") != NULL
4250# ifdef BACKSLASH_IN_FILENAME
4251 || strstr((char *)fname, "\\\\") != NULL
4252# endif
4253# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 )
4257 return FullName_save(fname, FALSE);
4258
4259 fname = vim_strsave(fname);
4260
Bram Moolenaar9b942202007-10-03 12:31:33 +00004261# ifdef USE_FNAME_CASE
4262# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 {
4266 if (fname != NULL)
4267 fname_case(fname, 0); /* set correct case for file name */
4268 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004269# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
4271 return fname;
4272#endif
4273}
4274
4275/*
4276 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4277 * "ffname" becomes a pointer to allocated memory (or NULL).
4278 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 void
4280fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004281 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 char_u **ffname;
4283 char_u **sfname;
4284{
4285 if (*ffname == NULL) /* if no file name given, nothing to do */
4286 return;
4287 if (*sfname == NULL) /* if no short file name given, use ffname */
4288 *sfname = *ffname;
4289 *ffname = fix_fname(*ffname); /* expand to full path */
4290
4291#ifdef FEAT_SHORTCUT
4292 if (!buf->b_p_bin)
4293 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004294 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
4296 /* If the file name is a shortcut file, use the file it links to. */
4297 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004298 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 {
4300 vim_free(*ffname);
4301 *ffname = rfname;
4302 *sfname = rfname;
4303 }
4304 }
4305#endif
4306}
4307
4308/*
4309 * Get the file name for an argument list entry.
4310 */
4311 char_u *
4312alist_name(aep)
4313 aentry_T *aep;
4314{
4315 buf_T *bp;
4316
4317 /* Use the name from the associated buffer if it exists. */
4318 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004319 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 return aep->ae_fname;
4321 return bp->b_fname;
4322}
4323
4324#if defined(FEAT_WINDOWS) || defined(PROTO)
4325/*
4326 * do_arg_all(): Open up to 'count' windows, one for each argument.
4327 */
4328 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004329do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 int count;
4331 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004332 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333{
4334 int i;
4335 win_T *wp, *wpnext;
4336 char_u *opened; /* array of flags for which args are open */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004337 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 int use_firstwin = FALSE; /* use first window for arglist */
4339 int split_ret = OK;
4340 int p_ea_save;
4341 alist_T *alist; /* argument list to be used */
4342 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004343 tabpage_T *tpnext;
4344 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004345 win_T *new_curwin = NULL;
4346 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
4348 if (ARGCOUNT <= 0)
4349 {
4350 /* Don't give an error message. We don't want it when the ":all"
4351 * command is in the .vimrc. */
4352 return;
4353 }
4354 setpcmark();
4355
4356 opened_len = ARGCOUNT;
4357 opened = alloc_clear((unsigned)opened_len);
4358 if (opened == NULL)
4359 return;
4360
4361#ifdef FEAT_GUI
4362 need_mouse_correct = TRUE;
4363#endif
4364
4365 /*
4366 * Try closing all windows that are not in the argument list.
4367 * Also close windows that are not full width;
4368 * When 'hidden' or "forceit" set the buffer becomes hidden.
4369 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004370 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004372 if (had_tab > 0)
4373 goto_tabpage_tp(first_tabpage);
4374 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004376 tpnext = curtab->tp_next;
4377 for (wp = firstwin; wp != NULL; wp = wpnext)
4378 {
4379 wpnext = wp->w_next;
4380 buf = wp->w_buffer;
4381 if (buf->b_ffname == NULL
4382 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004384 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004386 )
4387 i = ARGCOUNT;
4388 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004390 /* check if the buffer in this window is in the arglist */
4391 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004393 if (ARGLIST[i].ae_fnum == buf->b_fnum
4394 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004395 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004397 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004398 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004399 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004400 if (i == 0)
4401 {
4402 new_curwin = wp;
4403 new_curtab = curtab;
4404 }
4405 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004406 if (wp->w_alist != curwin->w_alist)
4407 {
4408 /* Use the current argument list for all windows
4409 * containing a file from it. */
4410 alist_unlink(wp->w_alist);
4411 wp->w_alist = curwin->w_alist;
4412 ++wp->w_alist->al_refcount;
4413 }
4414 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
4417 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004418 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004420 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004422 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004423 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004425 /* If the buffer was changed, and we would like to hide it,
4426 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004427 if (!P_HID(buf) && buf->b_nwindows <= 1
4428 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004430 (void)autowrite(buf, FALSE);
4431#ifdef FEAT_AUTOCMD
4432 /* check if autocommands removed the window */
4433 if (!win_valid(wp) || !buf_valid(buf))
4434 {
4435 wpnext = firstwin; /* start all over... */
4436 continue;
4437 }
4438#endif
4439 }
4440#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004441 /* don't close last window */
4442 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004443#endif
4444 use_firstwin = TRUE;
4445#ifdef FEAT_WINDOWS
4446 else
4447 {
4448 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4449# ifdef FEAT_AUTOCMD
4450 /* check if autocommands removed the next window */
4451 if (!win_valid(wpnext))
4452 wpnext = firstwin; /* start all over... */
4453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
4455#endif
4456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004459
4460 /* Without the ":tab" modifier only do the current tab page. */
4461 if (had_tab == 0 || tpnext == NULL)
4462 break;
4463
4464# ifdef FEAT_AUTOCMD
4465 /* check if autocommands removed the next tab page */
4466 if (!valid_tabpage(tpnext))
4467 tpnext = first_tabpage; /* start all over...*/
4468# endif
4469 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471
4472 /*
4473 * Open a window for files in the argument list that don't have one.
4474 * ARGCOUNT may change while doing this, because of autocommands.
4475 */
4476 if (count > ARGCOUNT || count <= 0)
4477 count = ARGCOUNT;
4478
4479 /* Autocommands may do anything to the argument list. Make sure it's not
4480 * freed while we are working here by "locking" it. We still have to
4481 * watch out for its size to be changed. */
4482 alist = curwin->w_alist;
4483 ++alist->al_refcount;
4484
4485#ifdef FEAT_AUTOCMD
4486 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4487 ++autocmd_no_enter;
4488 ++autocmd_no_leave;
4489#endif
4490 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004491#ifdef FEAT_WINDOWS
4492 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4493 * leaving an empty tab page when executed locally. */
4494 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4495 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4496 use_firstwin = TRUE;
4497#endif
4498
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4500 {
4501 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4502 arg_had_last = TRUE;
4503 if (i < opened_len && opened[i])
4504 {
4505 /* Move the already present window to below the current window */
4506 if (curwin->w_arg_idx != i)
4507 {
4508 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4509 {
4510 if (wpnext->w_arg_idx == i)
4511 {
4512 win_move_after(wpnext, curwin);
4513 break;
4514 }
4515 }
4516 }
4517 }
4518 else if (split_ret == OK)
4519 {
4520 if (!use_firstwin) /* split current window */
4521 {
4522 p_ea_save = p_ea;
4523 p_ea = TRUE; /* use space from all windows */
4524 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4525 p_ea = p_ea_save;
4526 if (split_ret == FAIL)
4527 continue;
4528 }
4529#ifdef FEAT_AUTOCMD
4530 else /* first window: do autocmd for leaving this buffer */
4531 --autocmd_no_leave;
4532#endif
4533
4534 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004535 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 */
4537 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004538 if (i == 0)
4539 {
4540 new_curwin = curwin;
4541 new_curtab = curtab;
4542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4544 ECMD_ONE,
4545 ((P_HID(curwin->w_buffer)
4546 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004547 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548#ifdef FEAT_AUTOCMD
4549 if (use_firstwin)
4550 ++autocmd_no_leave;
4551#endif
4552 use_firstwin = FALSE;
4553 }
4554 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004555
4556 /* When ":tab" was used open a new tab for a new window repeatedly. */
4557 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4558 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 }
4560
4561 /* Remove the "lock" on the argument list. */
4562 alist_unlink(alist);
4563
4564#ifdef FEAT_AUTOCMD
4565 --autocmd_no_enter;
4566#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004567 /* to window with first arg */
4568 if (valid_tabpage(new_curtab))
4569 goto_tabpage_tp(new_curtab);
4570 if (win_valid(new_curwin))
4571 win_enter(new_curwin, FALSE);
4572
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573#ifdef FEAT_AUTOCMD
4574 --autocmd_no_leave;
4575#endif
4576 vim_free(opened);
4577}
4578
4579# if defined(FEAT_LISTCMDS) || defined(PROTO)
4580/*
4581 * Open a window for a number of buffers.
4582 */
4583 void
4584ex_buffer_all(eap)
4585 exarg_T *eap;
4586{
4587 buf_T *buf;
4588 win_T *wp, *wpnext;
4589 int split_ret = OK;
4590 int p_ea_save;
4591 int open_wins = 0;
4592 int r;
4593 int count; /* Maximum number of windows to open. */
4594 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004595#ifdef FEAT_WINDOWS
4596 int had_tab = cmdmod.tab;
4597 tabpage_T *tpnext;
4598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599
4600 if (eap->addr_count == 0) /* make as many windows as possible */
4601 count = 9999;
4602 else
4603 count = eap->line2; /* make as many windows as specified */
4604 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4605 all = FALSE;
4606 else
4607 all = TRUE;
4608
4609 setpcmark();
4610
4611#ifdef FEAT_GUI
4612 need_mouse_correct = TRUE;
4613#endif
4614
4615 /*
4616 * Close superfluous windows (two windows for the same buffer).
4617 * Also close windows that are not full-width.
4618 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004619#ifdef FEAT_WINDOWS
4620 if (had_tab > 0)
4621 goto_tabpage_tp(first_tabpage);
4622 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004625 tpnext = curtab->tp_next;
4626 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004628 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004629 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004630#ifdef FEAT_VERTSPLIT
4631 || ((cmdmod.split & WSP_VERT)
4632 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004633 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004634 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004636#ifdef FEAT_WINDOWS
4637 || (had_tab > 0 && wp != firstwin)
4638#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004639 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004640 {
4641 win_close(wp, FALSE);
4642#ifdef FEAT_AUTOCMD
4643 wpnext = firstwin; /* just in case an autocommand does
4644 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004645 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004646 open_wins = 0;
4647#endif
4648 }
4649 else
4650 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004652
4653#ifdef FEAT_WINDOWS
4654 /* Without the ":tab" modifier only do the current tab page. */
4655 if (had_tab == 0 || tpnext == NULL)
4656 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004657 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
4661 /*
4662 * Go through the buffer list. When a buffer doesn't have a window yet,
4663 * open one. Otherwise move the window to the right position.
4664 * Watch out for autocommands that delete buffers or windows!
4665 */
4666#ifdef FEAT_AUTOCMD
4667 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4668 ++autocmd_no_enter;
4669#endif
4670 win_enter(lastwin, FALSE);
4671#ifdef FEAT_AUTOCMD
4672 ++autocmd_no_leave;
4673#endif
4674 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4675 {
4676 /* Check if this buffer needs a window */
4677 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4678 continue;
4679
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004680#ifdef FEAT_WINDOWS
4681 if (had_tab != 0)
4682 {
4683 /* With the ":tab" modifier don't move the window. */
4684 if (buf->b_nwindows > 0)
4685 wp = lastwin; /* buffer has a window, skip it */
4686 else
4687 wp = NULL;
4688 }
4689 else
4690#endif
4691 {
4692 /* Check if this buffer already has a window */
4693 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4694 if (wp->w_buffer == buf)
4695 break;
4696 /* If the buffer already has a window, move it */
4697 if (wp != NULL)
4698 win_move_after(wp, curwin);
4699 }
4700
4701 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 {
4703 /* Split the window and put the buffer in it */
4704 p_ea_save = p_ea;
4705 p_ea = TRUE; /* use space from all windows */
4706 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4707 ++open_wins;
4708 p_ea = p_ea_save;
4709 if (split_ret == FAIL)
4710 continue;
4711
4712 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004713#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 swap_exists_action = SEA_DIALOG;
4715#endif
4716 set_curbuf(buf, DOBUF_GOTO);
4717#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004720#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 break;
4724 }
4725#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004726#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 if (swap_exists_action == SEA_QUIT)
4728 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004729# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4730 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004731
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004732 /* Reset the error/interrupt/exception state here so that
4733 * aborting() returns FALSE when closing a window. */
4734 enter_cleanup(&cs);
4735# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004736
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004737 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 win_close(curwin, TRUE);
4739 --open_wins;
4740 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004741 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004742
4743# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4744 /* Restore the error/interrupt/exception state if not
4745 * discarded by a new aborting error, interrupt, or uncaught
4746 * exception. */
4747 leave_cleanup(&cs);
4748# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 }
4750 else
4751 handle_swap_exists(NULL);
4752#endif
4753 }
4754
4755 ui_breakcheck();
4756 if (got_int)
4757 {
4758 (void)vgetc(); /* only break the file loading, not the rest */
4759 break;
4760 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004761#ifdef FEAT_EVAL
4762 /* Autocommands deleted the buffer or aborted script processing!!! */
4763 if (aborting())
4764 break;
4765#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004766#ifdef FEAT_WINDOWS
4767 /* When ":tab" was used open a new tab for a new window repeatedly. */
4768 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4769 cmdmod.tab = 9999;
4770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 }
4772#ifdef FEAT_AUTOCMD
4773 --autocmd_no_enter;
4774#endif
4775 win_enter(firstwin, FALSE); /* back to first window */
4776#ifdef FEAT_AUTOCMD
4777 --autocmd_no_leave;
4778#endif
4779
4780 /*
4781 * Close superfluous windows.
4782 */
4783 for (wp = lastwin; open_wins > count; )
4784 {
4785 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4786 || autowrite(wp->w_buffer, FALSE) == OK);
4787#ifdef FEAT_AUTOCMD
4788 if (!win_valid(wp))
4789 {
4790 /* BufWrite Autocommands made the window invalid, start over */
4791 wp = lastwin;
4792 }
4793 else
4794#endif
4795 if (r)
4796 {
4797 win_close(wp, !P_HID(wp->w_buffer));
4798 --open_wins;
4799 wp = lastwin;
4800 }
4801 else
4802 {
4803 wp = wp->w_prev;
4804 if (wp == NULL)
4805 break;
4806 }
4807 }
4808}
4809# endif /* FEAT_LISTCMDS */
4810
4811#endif /* FEAT_WINDOWS */
4812
Bram Moolenaara3227e22006-03-08 21:32:40 +00004813static int chk_modeline __ARGS((linenr_T, int));
4814
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815/*
4816 * do_modelines() - process mode lines for the current file
4817 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004818 * "flags" can be:
4819 * OPT_WINONLY only set options local to window
4820 * OPT_NOWIN don't set options local to window
4821 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 * Returns immediately if the "ml" option isn't set.
4823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004825do_modelines(flags)
4826 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004828 linenr_T lnum;
4829 int nmlines;
4830 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
4832 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4833 return;
4834
4835 /* Disallow recursive entry here. Can happen when executing a modeline
4836 * triggers an autocommand, which reloads modelines with a ":do". */
4837 if (entered)
4838 return;
4839
4840 ++entered;
4841 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4842 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004843 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 nmlines = 0;
4845
4846 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4847 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004848 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 nmlines = 0;
4850 --entered;
4851}
4852
4853#include "version.h" /* for version number */
4854
4855/*
4856 * chk_modeline() - check a single line for a mode string
4857 * Return FAIL if an error encountered.
4858 */
4859 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004860chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004862 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
4864 char_u *s;
4865 char_u *e;
4866 char_u *linecopy; /* local copy of any modeline found */
4867 int prev;
4868 int vers;
4869 int end;
4870 int retval = OK;
4871 char_u *save_sourcing_name;
4872 linenr_T save_sourcing_lnum;
4873#ifdef FEAT_EVAL
4874 scid_T save_SID;
4875#endif
4876
4877 prev = -1;
4878 for (s = ml_get(lnum); *s != NUL; ++s)
4879 {
4880 if (prev == -1 || vim_isspace(prev))
4881 {
4882 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4883 || STRNCMP(s, "vi:", (size_t)3) == 0)
4884 break;
4885 if (STRNCMP(s, "vim", 3) == 0)
4886 {
4887 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4888 e = s + 4;
4889 else
4890 e = s + 3;
4891 vers = getdigits(&e);
4892 if (*e == ':'
4893 && (s[3] == ':'
4894 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4895 || (VIM_VERSION_100 < vers && s[3] == '<')
4896 || (VIM_VERSION_100 > vers && s[3] == '>')
4897 || (VIM_VERSION_100 == vers && s[3] == '=')))
4898 break;
4899 }
4900 }
4901 prev = *s;
4902 }
4903
4904 if (*s)
4905 {
4906 do /* skip over "ex:", "vi:" or "vim:" */
4907 ++s;
4908 while (s[-1] != ':');
4909
4910 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4911 if (linecopy == NULL)
4912 return FAIL;
4913
4914 save_sourcing_lnum = sourcing_lnum;
4915 save_sourcing_name = sourcing_name;
4916 sourcing_lnum = lnum; /* prepare for emsg() */
4917 sourcing_name = (char_u *)"modelines";
4918
4919 end = FALSE;
4920 while (end == FALSE)
4921 {
4922 s = skipwhite(s);
4923 if (*s == NUL)
4924 break;
4925
4926 /*
4927 * Find end of set command: ':' or end of line.
4928 * Skip over "\:", replacing it with ":".
4929 */
4930 for (e = s; *e != ':' && *e != NUL; ++e)
4931 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00004932 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 if (*e == NUL)
4934 end = TRUE;
4935
4936 /*
4937 * If there is a "set" command, require a terminating ':' and
4938 * ignore the stuff after the ':'.
4939 * "vi:set opt opt opt: foo" -- foo not interpreted
4940 * "vi:opt opt opt: foo" -- foo interpreted
4941 * Accept "se" for compatibility with Elvis.
4942 */
4943 if (STRNCMP(s, "set ", (size_t)4) == 0
4944 || STRNCMP(s, "se ", (size_t)3) == 0)
4945 {
4946 if (*e != ':') /* no terminating ':'? */
4947 break;
4948 end = TRUE;
4949 s = vim_strchr(s, ' ') + 1;
4950 }
4951 *e = NUL; /* truncate the set command */
4952
4953 if (*s != NUL) /* skip over an empty "::" */
4954 {
4955#ifdef FEAT_EVAL
4956 save_SID = current_SID;
4957 current_SID = SID_MODELINE;
4958#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004959 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960#ifdef FEAT_EVAL
4961 current_SID = save_SID;
4962#endif
4963 if (retval == FAIL) /* stop if error found */
4964 break;
4965 }
4966 s = e + 1; /* advance to next part */
4967 }
4968
4969 sourcing_lnum = save_sourcing_lnum;
4970 sourcing_name = save_sourcing_name;
4971
4972 vim_free(linecopy);
4973 }
4974 return retval;
4975}
4976
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00004977#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 int
4979read_viminfo_bufferlist(virp, writing)
4980 vir_T *virp;
4981 int writing;
4982{
4983 char_u *tab;
4984 linenr_T lnum;
4985 colnr_T col;
4986 buf_T *buf;
4987 char_u *sfname;
4988 char_u *xline;
4989
4990 /* Handle long line and escaped characters. */
4991 xline = viminfo_readstring(virp, 1, FALSE);
4992
4993 /* don't read in if there are files on the command-line or if writing: */
4994 if (xline != NULL && !writing && ARGCOUNT == 0
4995 && find_viminfo_parameter('%') != NULL)
4996 {
4997 /* Format is: <fname> Tab <lnum> Tab <col>.
4998 * Watch out for a Tab in the file name, work from the end. */
4999 lnum = 0;
5000 col = 0;
5001 tab = vim_strrchr(xline, '\t');
5002 if (tab != NULL)
5003 {
5004 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005005 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 tab = vim_strrchr(xline, '\t');
5007 if (tab != NULL)
5008 {
5009 *tab++ = '\0';
5010 lnum = atol((char *)tab);
5011 }
5012 }
5013
5014 /* Expand "~/" in the file name at "line + 1" to a full path.
5015 * Then try shortening it by comparing with the current directory */
5016 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005017 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
5019 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5020 if (buf != NULL) /* just in case... */
5021 {
5022 buf->b_last_cursor.lnum = lnum;
5023 buf->b_last_cursor.col = col;
5024 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5025 }
5026 }
5027 vim_free(xline);
5028
5029 return viminfo_readline(virp);
5030}
5031
5032 void
5033write_viminfo_bufferlist(fp)
5034 FILE *fp;
5035{
5036 buf_T *buf;
5037#ifdef FEAT_WINDOWS
5038 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005039 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040#endif
5041 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005042 int max_buffers;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005043 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044
5045 if (find_viminfo_parameter('%') == NULL)
5046 return;
5047
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005048 /* Without a number -1 is returned: do all buffers. */
5049 max_buffers = get_viminfo_parameter('%');
5050
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005052#define LINE_BUF_LEN (MAXPATHL + 40)
5053 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 if (line == NULL)
5055 return;
5056
5057#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005058 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 set_last_cursor(win);
5060#else
5061 set_last_cursor(curwin);
5062#endif
5063
5064 fprintf(fp, _("\n# Buffer list:\n"));
5065 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5066 {
5067 if (buf->b_fname == NULL
5068 || !buf->b_p_bl
5069#ifdef FEAT_QUICKFIX
5070 || bt_quickfix(buf)
5071#endif
5072 || removable(buf->b_ffname))
5073 continue;
5074
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005075 if (max_buffers-- == 0)
5076 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 putc('%', fp);
5078 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005079 len = STRLEN(line);
5080 vim_snprintf((char *)line + len, len - LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 (long)buf->b_last_cursor.lnum,
5082 buf->b_last_cursor.col);
5083 viminfo_writestring(fp, line);
5084 }
5085 vim_free(line);
5086}
5087#endif
5088
5089
5090/*
5091 * Return special buffer name.
5092 * Returns NULL when the buffer has a normal file name.
5093 */
5094 char *
5095buf_spname(buf)
5096 buf_T *buf;
5097{
5098#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5099 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005100 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005101 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005102 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005103
5104 /*
5105 * For location list window, w_llist_ref points to the location list.
5106 * For quickfix window, w_llist_ref is NULL.
5107 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005108 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005109 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005110 goto win_found;
5111win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005112 if (win != NULL && win->w_llist_ref != NULL)
5113 return _("[Location List]");
5114 else
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005115 return _("[Quickfix List]");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117#endif
5118#ifdef FEAT_QUICKFIX
5119 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5120 * contains the name as specified by the user */
5121 if (bt_nofile(buf))
5122 {
5123 if (buf->b_sfname != NULL)
5124 return (char *)buf->b_sfname;
Bram Moolenaarf4f664c2008-12-03 10:21:57 +00005125 return _("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127#endif
5128 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005129 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 return NULL;
5131}
5132
5133
5134#if defined(FEAT_SIGNS) || defined(PROTO)
5135/*
5136 * Insert the sign into the signlist.
5137 */
5138 static void
5139insert_sign(buf, prev, next, id, lnum, typenr)
5140 buf_T *buf; /* buffer to store sign in */
5141 signlist_T *prev; /* previous sign entry */
5142 signlist_T *next; /* next sign entry */
5143 int id; /* sign ID */
5144 linenr_T lnum; /* line number which gets the mark */
5145 int typenr; /* typenr of sign we are adding */
5146{
5147 signlist_T *newsign;
5148
5149 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5150 if (newsign != NULL)
5151 {
5152 newsign->id = id;
5153 newsign->lnum = lnum;
5154 newsign->typenr = typenr;
5155 newsign->next = next;
5156#ifdef FEAT_NETBEANS_INTG
5157 newsign->prev = prev;
5158 if (next != NULL)
5159 next->prev = newsign;
5160#endif
5161
5162 if (prev == NULL)
5163 {
5164 /* When adding first sign need to redraw the windows to create the
5165 * column for signs. */
5166 if (buf->b_signlist == NULL)
5167 {
5168 redraw_buf_later(buf, NOT_VALID);
5169 changed_cline_bef_curs();
5170 }
5171
5172 /* first sign in signlist */
5173 buf->b_signlist = newsign;
5174 }
5175 else
5176 prev->next = newsign;
5177 }
5178}
5179
5180/*
5181 * Add the sign into the signlist. Find the right spot to do it though.
5182 */
5183 void
5184buf_addsign(buf, id, lnum, typenr)
5185 buf_T *buf; /* buffer to store sign in */
5186 int id; /* sign ID */
5187 linenr_T lnum; /* line number which gets the mark */
5188 int typenr; /* typenr of sign we are adding */
5189{
5190 signlist_T *sign; /* a sign in the signlist */
5191 signlist_T *prev; /* the previous sign */
5192
5193 prev = NULL;
5194 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5195 {
5196 if (lnum == sign->lnum && id == sign->id)
5197 {
5198 sign->typenr = typenr;
5199 return;
5200 }
5201 else if (
5202#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5203 id < 0 &&
5204#endif
5205 lnum < sign->lnum)
5206 {
5207#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5208 /* XXX - GRP: Is this because of sign slide problem? Or is it
5209 * really needed? Or is it because we allow multiple signs per
5210 * line? If so, should I add that feature to FEAT_SIGNS?
5211 */
5212 while (prev != NULL && prev->lnum == lnum)
5213 prev = prev->prev;
5214 if (prev == NULL)
5215 sign = buf->b_signlist;
5216 else
5217 sign = prev->next;
5218#endif
5219 insert_sign(buf, prev, sign, id, lnum, typenr);
5220 return;
5221 }
5222 prev = sign;
5223 }
5224#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5225 /* XXX - GRP: See previous comment */
5226 while (prev != NULL && prev->lnum == lnum)
5227 prev = prev->prev;
5228 if (prev == NULL)
5229 sign = buf->b_signlist;
5230 else
5231 sign = prev->next;
5232#endif
5233 insert_sign(buf, prev, sign, id, lnum, typenr);
5234
5235 return;
5236}
5237
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005238 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239buf_change_sign_type(buf, markId, typenr)
5240 buf_T *buf; /* buffer to store sign in */
5241 int markId; /* sign ID */
5242 int typenr; /* typenr of sign we are adding */
5243{
5244 signlist_T *sign; /* a sign in the signlist */
5245
5246 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5247 {
5248 if (sign->id == markId)
5249 {
5250 sign->typenr = typenr;
5251 return sign->lnum;
5252 }
5253 }
5254
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005255 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256}
5257
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005258 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259buf_getsigntype(buf, lnum, type)
5260 buf_T *buf;
5261 linenr_T lnum;
5262 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5263{
5264 signlist_T *sign; /* a sign in a b_signlist */
5265
5266 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5267 if (sign->lnum == lnum
5268 && (type == SIGN_ANY
5269# ifdef FEAT_SIGN_ICONS
5270 || (type == SIGN_ICON
5271 && sign_get_image(sign->typenr) != NULL)
5272# endif
5273 || (type == SIGN_TEXT
5274 && sign_get_text(sign->typenr) != NULL)
5275 || (type == SIGN_LINEHL
5276 && sign_get_attr(sign->typenr, TRUE) != 0)))
5277 return sign->typenr;
5278 return 0;
5279}
5280
5281
5282 linenr_T
5283buf_delsign(buf, id)
5284 buf_T *buf; /* buffer sign is stored in */
5285 int id; /* sign id */
5286{
5287 signlist_T **lastp; /* pointer to pointer to current sign */
5288 signlist_T *sign; /* a sign in a b_signlist */
5289 signlist_T *next; /* the next sign in a b_signlist */
5290 linenr_T lnum; /* line number whose sign was deleted */
5291
5292 lastp = &buf->b_signlist;
5293 lnum = 0;
5294 for (sign = buf->b_signlist; sign != NULL; sign = next)
5295 {
5296 next = sign->next;
5297 if (sign->id == id)
5298 {
5299 *lastp = next;
5300#ifdef FEAT_NETBEANS_INTG
5301 if (next != NULL)
5302 next->prev = sign->prev;
5303#endif
5304 lnum = sign->lnum;
5305 vim_free(sign);
5306 break;
5307 }
5308 else
5309 lastp = &sign->next;
5310 }
5311
5312 /* When deleted the last sign need to redraw the windows to remove the
5313 * sign column. */
5314 if (buf->b_signlist == NULL)
5315 {
5316 redraw_buf_later(buf, NOT_VALID);
5317 changed_cline_bef_curs();
5318 }
5319
5320 return lnum;
5321}
5322
5323
5324/*
5325 * Find the line number of the sign with the requested id. If the sign does
5326 * not exist, return 0 as the line number. This will still let the correct file
5327 * get loaded.
5328 */
5329 int
5330buf_findsign(buf, id)
5331 buf_T *buf; /* buffer to store sign in */
5332 int id; /* sign ID */
5333{
5334 signlist_T *sign; /* a sign in the signlist */
5335
5336 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5337 if (sign->id == id)
5338 return sign->lnum;
5339
5340 return 0;
5341}
5342
5343 int
5344buf_findsign_id(buf, lnum)
5345 buf_T *buf; /* buffer whose sign we are searching for */
5346 linenr_T lnum; /* line number of sign */
5347{
5348 signlist_T *sign; /* a sign in the signlist */
5349
5350 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5351 if (sign->lnum == lnum)
5352 return sign->id;
5353
5354 return 0;
5355}
5356
5357
5358# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5359/* see if a given type of sign exists on a specific line */
5360 int
5361buf_findsigntype_id(buf, lnum, typenr)
5362 buf_T *buf; /* buffer whose sign we are searching for */
5363 linenr_T lnum; /* line number of sign */
5364 int typenr; /* sign type number */
5365{
5366 signlist_T *sign; /* a sign in the signlist */
5367
5368 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5369 if (sign->lnum == lnum && sign->typenr == typenr)
5370 return sign->id;
5371
5372 return 0;
5373}
5374
5375
5376# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5377/* return the number of icons on the given line */
5378 int
5379buf_signcount(buf, lnum)
5380 buf_T *buf;
5381 linenr_T lnum;
5382{
5383 signlist_T *sign; /* a sign in the signlist */
5384 int count = 0;
5385
5386 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5387 if (sign->lnum == lnum)
5388 if (sign_get_image(sign->typenr) != NULL)
5389 count++;
5390
5391 return count;
5392}
5393# endif /* FEAT_SIGN_ICONS */
5394# endif /* FEAT_NETBEANS_INTG */
5395
5396
5397/*
5398 * Delete signs in buffer "buf".
5399 */
5400 static void
5401buf_delete_signs(buf)
5402 buf_T *buf;
5403{
5404 signlist_T *next;
5405
5406 while (buf->b_signlist != NULL)
5407 {
5408 next = buf->b_signlist->next;
5409 vim_free(buf->b_signlist);
5410 buf->b_signlist = next;
5411 }
5412}
5413
5414/*
5415 * Delete all signs in all buffers.
5416 */
5417 void
5418buf_delete_all_signs()
5419{
5420 buf_T *buf; /* buffer we are checking for signs */
5421
5422 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5423 if (buf->b_signlist != NULL)
5424 {
5425 /* Need to redraw the windows to remove the sign column. */
5426 redraw_buf_later(buf, NOT_VALID);
5427 buf_delete_signs(buf);
5428 }
5429}
5430
5431/*
5432 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5433 */
5434 void
5435sign_list_placed(rbuf)
5436 buf_T *rbuf;
5437{
5438 buf_T *buf;
5439 signlist_T *p;
5440 char lbuf[BUFSIZ];
5441
5442 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5443 msg_putchar('\n');
5444 if (rbuf == NULL)
5445 buf = firstbuf;
5446 else
5447 buf = rbuf;
5448 while (buf != NULL)
5449 {
5450 if (buf->b_signlist != NULL)
5451 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005452 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5454 msg_putchar('\n');
5455 }
5456 for (p = buf->b_signlist; p != NULL; p = p->next)
5457 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005458 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5460 MSG_PUTS(lbuf);
5461 msg_putchar('\n');
5462 }
5463 if (rbuf != NULL)
5464 break;
5465 buf = buf->b_next;
5466 }
5467}
5468
5469/*
5470 * Adjust a placed sign for inserted/deleted lines.
5471 */
5472 void
5473sign_mark_adjust(line1, line2, amount, amount_after)
5474 linenr_T line1;
5475 linenr_T line2;
5476 long amount;
5477 long amount_after;
5478{
5479 signlist_T *sign; /* a sign in a b_signlist */
5480
5481 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5482 {
5483 if (sign->lnum >= line1 && sign->lnum <= line2)
5484 {
5485 if (amount == MAXLNUM)
5486 sign->lnum = line1;
5487 else
5488 sign->lnum += amount;
5489 }
5490 else if (sign->lnum > line2)
5491 sign->lnum += amount_after;
5492 }
5493}
5494#endif /* FEAT_SIGNS */
5495
5496/*
5497 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5498 */
5499 void
5500set_buflisted(on)
5501 int on;
5502{
5503 if (on != curbuf->b_p_bl)
5504 {
5505 curbuf->b_p_bl = on;
5506#ifdef FEAT_AUTOCMD
5507 if (on)
5508 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5509 else
5510 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5511#endif
5512 }
5513}
5514
5515/*
5516 * Read the file for "buf" again and check if the contents changed.
5517 * Return TRUE if it changed or this could not be checked.
5518 */
5519 int
5520buf_contents_changed(buf)
5521 buf_T *buf;
5522{
5523 buf_T *newbuf;
5524 int differ = TRUE;
5525 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 exarg_T ea;
5528
5529 /* Allocate a buffer without putting it in the buffer list. */
5530 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5531 if (newbuf == NULL)
5532 return TRUE;
5533
5534 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5535 if (prep_exarg(&ea, buf) == FAIL)
5536 {
5537 wipe_buffer(newbuf, FALSE);
5538 return TRUE;
5539 }
5540
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 /* set curwin/curbuf to buf and save a few things */
5542 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
Bram Moolenaar4770d092006-01-12 23:22:24 +00005544 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 && readfile(buf->b_ffname, buf->b_fname,
5546 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5547 &ea, READ_NEW | READ_DUMMY) == OK)
5548 {
5549 /* compare the two files line by line */
5550 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5551 {
5552 differ = FALSE;
5553 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5554 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5555 {
5556 differ = TRUE;
5557 break;
5558 }
5559 }
5560 }
5561 vim_free(ea.cmd);
5562
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 /* restore curwin/curbuf and a few other things */
5564 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
5566 if (curbuf != newbuf) /* safety check */
5567 wipe_buffer(newbuf, FALSE);
5568
5569 return differ;
5570}
5571
5572/*
5573 * Wipe out a buffer and decrement the last buffer number if it was used for
5574 * this buffer. Call this to wipe out a temp buffer that does not contain any
5575 * marks.
5576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 void
5578wipe_buffer(buf, aucmd)
5579 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005580 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 if (buf->b_fnum == top_file_num - 1)
5583 --top_file_num;
5584
5585#ifdef FEAT_AUTOCMD
5586 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005587 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588#endif
5589 close_buffer(NULL, buf, DOBUF_WIPE);
5590#ifdef FEAT_AUTOCMD
5591 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005592 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593#endif
5594}