blob: 531e4b9a29f53c32c649d3b884b4a4d43aea2943 [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
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200639 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641#ifdef FEAT_LOCALMAP
642 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
643 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
644#endif
645#ifdef FEAT_MBYTE
646 vim_free(buf->b_start_fenc);
647 buf->b_start_fenc = NULL;
648#endif
Bram Moolenaar9381ab72008-11-12 11:52:19 +0000649#ifdef FEAT_SPELL
650 ga_clear(&buf->b_langp);
651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652}
653
654/*
655 * Free the b_wininfo list for buffer "buf".
656 */
657 static void
658clear_wininfo(buf)
659 buf_T *buf;
660{
661 wininfo_T *wip;
662
663 while (buf->b_wininfo != NULL)
664 {
665 wip = buf->b_wininfo;
666 buf->b_wininfo = wip->wi_next;
667 if (wip->wi_optset)
668 {
669 clear_winopt(&wip->wi_opt);
670#ifdef FEAT_FOLDING
671 deleteFoldRecurse(&wip->wi_folds);
672#endif
673 }
674 vim_free(wip);
675 }
676}
677
678#if defined(FEAT_LISTCMDS) || defined(PROTO)
679/*
680 * Go to another buffer. Handles the result of the ATTENTION dialog.
681 */
682 void
683goto_buffer(eap, start, dir, count)
684 exarg_T *eap;
685 int start;
686 int dir;
687 int count;
688{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000689# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 buf_T *old_curbuf = curbuf;
691
692 swap_exists_action = SEA_DIALOG;
693# endif
694 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
695 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000696# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
698 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000699# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
700 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000701
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000702 /* Reset the error/interrupt/exception state here so that
703 * aborting() returns FALSE when closing a window. */
704 enter_cleanup(&cs);
705# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000706
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000707 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 win_close(curwin, TRUE);
709 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000710 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000711
712# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
713 /* Restore the error/interrupt/exception state if not discarded by a
714 * new aborting error, interrupt, or uncaught exception. */
715 leave_cleanup(&cs);
716# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 }
718 else
719 handle_swap_exists(old_curbuf);
720# endif
721}
722#endif
723
Bram Moolenaare64ac772005-12-07 20:54:59 +0000724#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725/*
726 * Handle the situation of swap_exists_action being set.
727 * It is allowed for "old_curbuf" to be NULL or invalid.
728 */
729 void
730handle_swap_exists(old_curbuf)
731 buf_T *old_curbuf;
732{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000733# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
734 cleanup_T cs;
735# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 if (swap_exists_action == SEA_QUIT)
738 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000739# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
740 /* Reset the error/interrupt/exception state here so that
741 * aborting() returns FALSE when closing a buffer. */
742 enter_cleanup(&cs);
743# endif
744
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 /* User selected Quit at ATTENTION prompt. Go back to previous
746 * buffer. If that buffer is gone or the same as the current one,
747 * open a new, empty buffer. */
748 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000749 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
751 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000752 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 if (old_curbuf != NULL)
754 enter_buffer(old_curbuf);
755 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000756
757# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
758 /* Restore the error/interrupt/exception state if not discarded by a
759 * new aborting error, interrupt, or uncaught exception. */
760 leave_cleanup(&cs);
761# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 }
763 else if (swap_exists_action == SEA_RECOVER)
764 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000765# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
766 /* Reset the error/interrupt/exception state here so that
767 * aborting() returns FALSE when closing a buffer. */
768 enter_cleanup(&cs);
769# endif
770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 /* User selected Recover at ATTENTION prompt. */
772 msg_scroll = TRUE;
773 ml_recover();
774 MSG_PUTS("\n"); /* don't overwrite the last message */
775 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000776 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000777
778# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
779 /* Restore the error/interrupt/exception state if not discarded by a
780 * new aborting error, interrupt, or uncaught exception. */
781 leave_cleanup(&cs);
782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784 swap_exists_action = SEA_NONE;
785}
786#endif
787
788#if defined(FEAT_LISTCMDS) || defined(PROTO)
789/*
790 * do_bufdel() - delete or unload buffer(s)
791 *
792 * addr_count == 0: ":bdel" - delete current buffer
793 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
794 * buffer "end_bnr", then any other arguments.
795 * addr_count == 2: ":N,N bdel" - delete buffers in range
796 *
797 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
798 * DOBUF_DEL (":bdel")
799 *
800 * Returns error message or NULL
801 */
802 char_u *
803do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
804 int command;
805 char_u *arg; /* pointer to extra arguments */
806 int addr_count;
807 int start_bnr; /* first buffer number in a range */
808 int end_bnr; /* buffer nr or last buffer nr in a range */
809 int forceit;
810{
811 int do_current = 0; /* delete current buffer? */
812 int deleted = 0; /* number of buffers deleted */
813 char_u *errormsg = NULL; /* return value */
814 int bnr; /* buffer number */
815 char_u *p;
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 if (addr_count == 0)
818 {
819 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
820 }
821 else
822 {
823 if (addr_count == 2)
824 {
825 if (*arg) /* both range and argument is not allowed */
826 return (char_u *)_(e_trailing);
827 bnr = start_bnr;
828 }
829 else /* addr_count == 1 */
830 bnr = end_bnr;
831
832 for ( ;!got_int; ui_breakcheck())
833 {
834 /*
835 * delete the current buffer last, otherwise when the
836 * current buffer is deleted, the next buffer becomes
837 * the current one and will be loaded, which may then
838 * also be deleted, etc.
839 */
840 if (bnr == curbuf->b_fnum)
841 do_current = bnr;
842 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
843 forceit) == OK)
844 ++deleted;
845
846 /*
847 * find next buffer number to delete/unload
848 */
849 if (addr_count == 2)
850 {
851 if (++bnr > end_bnr)
852 break;
853 }
854 else /* addr_count == 1 */
855 {
856 arg = skipwhite(arg);
857 if (*arg == NUL)
858 break;
859 if (!VIM_ISDIGIT(*arg))
860 {
861 p = skiptowhite_esc(arg);
862 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
863 if (bnr < 0) /* failed */
864 break;
865 arg = p;
866 }
867 else
868 bnr = getdigits(&arg);
869 }
870 }
871 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
872 FORWARD, do_current, forceit) == OK)
873 ++deleted;
874
875 if (deleted == 0)
876 {
877 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000878 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000880 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000882 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 errormsg = IObuff;
884 }
885 else if (deleted >= p_report)
886 {
887 if (command == DOBUF_UNLOAD)
888 {
889 if (deleted == 1)
890 MSG(_("1 buffer unloaded"));
891 else
892 smsg((char_u *)_("%d buffers unloaded"), deleted);
893 }
894 else if (command == DOBUF_DEL)
895 {
896 if (deleted == 1)
897 MSG(_("1 buffer deleted"));
898 else
899 smsg((char_u *)_("%d buffers deleted"), deleted);
900 }
901 else
902 {
903 if (deleted == 1)
904 MSG(_("1 buffer wiped out"));
905 else
906 smsg((char_u *)_("%d buffers wiped out"), deleted);
907 }
908 }
909 }
910
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
912 return errormsg;
913}
914
915/*
916 * Implementation of the commands for the buffer list.
917 *
918 * action == DOBUF_GOTO go to specified buffer
919 * action == DOBUF_SPLIT split window and go to specified buffer
920 * action == DOBUF_UNLOAD unload specified buffer(s)
921 * action == DOBUF_DEL delete specified buffer(s) from buffer list
922 * action == DOBUF_WIPE delete specified buffer(s) really
923 *
924 * start == DOBUF_CURRENT go to "count" buffer from current buffer
925 * start == DOBUF_FIRST go to "count" buffer from first buffer
926 * start == DOBUF_LAST go to "count" buffer from last buffer
927 * start == DOBUF_MOD go to "count" modified buffer from current buffer
928 *
929 * Return FAIL or OK.
930 */
931 int
932do_buffer(action, start, dir, count, forceit)
933 int action;
934 int start;
935 int dir; /* FORWARD or BACKWARD */
936 int count; /* buffer number or number of buffers */
937 int forceit; /* TRUE for :...! */
938{
939 buf_T *buf;
940 buf_T *bp;
941 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
942 || action == DOBUF_WIPE);
943
944 switch (start)
945 {
946 case DOBUF_FIRST: buf = firstbuf; break;
947 case DOBUF_LAST: buf = lastbuf; break;
948 default: buf = curbuf; break;
949 }
950 if (start == DOBUF_MOD) /* find next modified buffer */
951 {
952 while (count-- > 0)
953 {
954 do
955 {
956 buf = buf->b_next;
957 if (buf == NULL)
958 buf = firstbuf;
959 }
960 while (buf != curbuf && !bufIsChanged(buf));
961 }
962 if (!bufIsChanged(buf))
963 {
964 EMSG(_("E84: No modified buffer found"));
965 return FAIL;
966 }
967 }
968 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
969 {
970 while (buf != NULL && buf->b_fnum != count)
971 buf = buf->b_next;
972 }
973 else
974 {
975 bp = NULL;
976 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
977 {
978 /* remember the buffer where we start, we come back there when all
979 * buffers are unlisted. */
980 if (bp == NULL)
981 bp = buf;
982 if (dir == FORWARD)
983 {
984 buf = buf->b_next;
985 if (buf == NULL)
986 buf = firstbuf;
987 }
988 else
989 {
990 buf = buf->b_prev;
991 if (buf == NULL)
992 buf = lastbuf;
993 }
994 /* don't count unlisted buffers */
995 if (unload || buf->b_p_bl)
996 {
997 --count;
998 bp = NULL; /* use this buffer as new starting point */
999 }
1000 if (bp == buf)
1001 {
1002 /* back where we started, didn't find anything. */
1003 EMSG(_("E85: There is no listed buffer"));
1004 return FAIL;
1005 }
1006 }
1007 }
1008
1009 if (buf == NULL) /* could not find it */
1010 {
1011 if (start == DOBUF_FIRST)
1012 {
1013 /* don't warn when deleting */
1014 if (!unload)
1015 EMSGN(_("E86: Buffer %ld does not exist"), count);
1016 }
1017 else if (dir == FORWARD)
1018 EMSG(_("E87: Cannot go beyond last buffer"));
1019 else
1020 EMSG(_("E88: Cannot go before first buffer"));
1021 return FAIL;
1022 }
1023
1024#ifdef FEAT_GUI
1025 need_mouse_correct = TRUE;
1026#endif
1027
1028#ifdef FEAT_LISTCMDS
1029 /*
1030 * delete buffer buf from memory and/or the list
1031 */
1032 if (unload)
1033 {
1034 int forward;
1035 int retval;
1036
1037 /* When unloading or deleting a buffer that's already unloaded and
1038 * unlisted: fail silently. */
1039 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1040 return FAIL;
1041
1042 if (!forceit && bufIsChanged(buf))
1043 {
1044#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1045 if ((p_confirm || cmdmod.confirm) && p_write)
1046 {
1047 dialog_changed(buf, FALSE);
1048# ifdef FEAT_AUTOCMD
1049 if (!buf_valid(buf))
1050 /* Autocommand deleted buffer, oops! It's not changed
1051 * now. */
1052 return FAIL;
1053# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001054 /* If it's still changed fail silently, the dialog already
1055 * mentioned why it fails. */
1056 if (bufIsChanged(buf))
1057 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001059 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060#endif
1061 {
1062 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1063 buf->b_fnum);
1064 return FAIL;
1065 }
1066 }
1067
1068 /*
1069 * If deleting the last (listed) buffer, make it empty.
1070 * The last (listed) buffer cannot be unloaded.
1071 */
1072 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1073 if (bp->b_p_bl && bp != buf)
1074 break;
1075 if (bp == NULL && buf == curbuf)
1076 {
1077 if (action == DOBUF_UNLOAD)
1078 {
1079 EMSG(_("E90: Cannot unload last buffer"));
1080 return FAIL;
1081 }
1082
1083 /* Close any other windows on this buffer, then make it empty. */
1084#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001085 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086#endif
1087 setpcmark();
1088 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001089 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090
1091 /*
1092 * do_ecmd() may create a new buffer, then we have to delete
1093 * the old one. But do_ecmd() may have done that already, check
1094 * if the buffer still exists.
1095 */
1096 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1097 close_buffer(NULL, buf, action);
1098 return retval;
1099 }
1100
1101#ifdef FEAT_WINDOWS
1102 /*
1103 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001104 * (unless it's the only window). Repeat this so long as we end up in
1105 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001107 while (buf == curbuf
1108 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 win_close(curwin, FALSE);
1110#endif
1111
1112 /*
1113 * If the buffer to be deleted is not the current one, delete it here.
1114 */
1115 if (buf != curbuf)
1116 {
1117#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001118 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119#endif
1120 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1121 close_buffer(NULL, buf, action);
1122 return OK;
1123 }
1124
1125 /*
1126 * Deleting the current buffer: Need to find another buffer to go to.
1127 * There must be another, otherwise it would have been handled above.
1128 * First use au_new_curbuf, if it is valid.
1129 * Then prefer the buffer we most recently visited.
1130 * Else try to find one that is loaded, after the current buffer,
1131 * then before the current buffer.
1132 * Finally use any buffer.
1133 */
1134 buf = NULL; /* selected buffer */
1135 bp = NULL; /* used when no loaded buffer found */
1136#ifdef FEAT_AUTOCMD
1137 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1138 buf = au_new_curbuf;
1139# ifdef FEAT_JUMPLIST
1140 else
1141# endif
1142#endif
1143#ifdef FEAT_JUMPLIST
1144 if (curwin->w_jumplistlen > 0)
1145 {
1146 int jumpidx;
1147
1148 jumpidx = curwin->w_jumplistidx - 1;
1149 if (jumpidx < 0)
1150 jumpidx = curwin->w_jumplistlen - 1;
1151
1152 forward = jumpidx;
1153 while (jumpidx != curwin->w_jumplistidx)
1154 {
1155 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1156 if (buf != NULL)
1157 {
1158 if (buf == curbuf || !buf->b_p_bl)
1159 buf = NULL; /* skip current and unlisted bufs */
1160 else if (buf->b_ml.ml_mfp == NULL)
1161 {
1162 /* skip unloaded buf, but may keep it for later */
1163 if (bp == NULL)
1164 bp = buf;
1165 buf = NULL;
1166 }
1167 }
1168 if (buf != NULL) /* found a valid buffer: stop searching */
1169 break;
1170 /* advance to older entry in jump list */
1171 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1172 break;
1173 if (--jumpidx < 0)
1174 jumpidx = curwin->w_jumplistlen - 1;
1175 if (jumpidx == forward) /* List exhausted for sure */
1176 break;
1177 }
1178 }
1179#endif
1180
1181 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1182 {
1183 forward = TRUE;
1184 buf = curbuf->b_next;
1185 for (;;)
1186 {
1187 if (buf == NULL)
1188 {
1189 if (!forward) /* tried both directions */
1190 break;
1191 buf = curbuf->b_prev;
1192 forward = FALSE;
1193 continue;
1194 }
1195 /* in non-help buffer, try to skip help buffers, and vv */
1196 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1197 {
1198 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1199 break;
1200 if (bp == NULL) /* remember unloaded buf for later */
1201 bp = buf;
1202 }
1203 if (forward)
1204 buf = buf->b_next;
1205 else
1206 buf = buf->b_prev;
1207 }
1208 }
1209 if (buf == NULL) /* No loaded buffer, use unloaded one */
1210 buf = bp;
1211 if (buf == NULL) /* No loaded buffer, find listed one */
1212 {
1213 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1214 if (buf->b_p_bl && buf != curbuf)
1215 break;
1216 }
1217 if (buf == NULL) /* Still no buffer, just take one */
1218 {
1219 if (curbuf->b_next != NULL)
1220 buf = curbuf->b_next;
1221 else
1222 buf = curbuf->b_prev;
1223 }
1224 }
1225
1226 /*
1227 * make buf current buffer
1228 */
1229 if (action == DOBUF_SPLIT) /* split window first */
1230 {
1231# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001232 /* If 'switchbuf' contains "useopen": jump to first window containing
1233 * "buf" if one exists */
1234 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001235 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001236 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001237 * page containing "buf" if one exists */
1238 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 return OK;
1240 if (win_split(0, 0) == FAIL)
1241# endif
1242 return FAIL;
1243 }
1244#endif
1245
1246 /* go to current buffer - nothing to do */
1247 if (buf == curbuf)
1248 return OK;
1249
1250 /*
1251 * Check if the current buffer may be abandoned.
1252 */
1253 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1254 {
1255#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1256 if ((p_confirm || cmdmod.confirm) && p_write)
1257 {
1258 dialog_changed(curbuf, FALSE);
1259# ifdef FEAT_AUTOCMD
1260 if (!buf_valid(buf))
1261 /* Autocommand deleted buffer, oops! */
1262 return FAIL;
1263# endif
1264 }
1265 if (bufIsChanged(curbuf))
1266#endif
1267 {
1268 EMSG(_(e_nowrtmsg));
1269 return FAIL;
1270 }
1271 }
1272
1273 /* Go to the other buffer. */
1274 set_curbuf(buf, action);
1275
1276#if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1277 if (action == DOBUF_SPLIT)
1278 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1279#endif
1280
1281#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1282 if (aborting()) /* autocmds may abort script processing */
1283 return FAIL;
1284#endif
1285
1286 return OK;
1287}
1288
1289#endif /* FEAT_LISTCMDS */
1290
1291/*
1292 * Set current buffer to "buf". Executes autocommands and closes current
1293 * buffer. "action" tells how to close the current buffer:
1294 * DOBUF_GOTO free or hide it
1295 * DOBUF_SPLIT nothing
1296 * DOBUF_UNLOAD unload it
1297 * DOBUF_DEL delete it
1298 * DOBUF_WIPE wipe it out
1299 */
1300 void
1301set_curbuf(buf, action)
1302 buf_T *buf;
1303 int action;
1304{
1305 buf_T *prevbuf;
1306 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1307 || action == DOBUF_WIPE);
1308
1309 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001310 if (!cmdmod.keepalt)
1311 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001312 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
1314#ifdef FEAT_VISUAL
1315 /* Don't restart Select mode after switching to another buffer. */
1316 VIsual_reselect = FALSE;
1317#endif
1318
1319 /* close_windows() or apply_autocmds() may change curbuf */
1320 prevbuf = curbuf;
1321
1322#ifdef FEAT_AUTOCMD
1323 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1324# ifdef FEAT_EVAL
1325 if (buf_valid(prevbuf) && !aborting())
1326# else
1327 if (buf_valid(prevbuf))
1328# endif
1329#endif
1330 {
1331#ifdef FEAT_WINDOWS
1332 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001333 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334#endif
1335#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1336 if (buf_valid(prevbuf) && !aborting())
1337#else
1338 if (buf_valid(prevbuf))
1339#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001340 {
1341 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001342 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1344 unload ? action : (action == DOBUF_GOTO
1345 && !P_HID(prevbuf)
1346 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001350 /* An autocommand may have deleted "buf", already entered it (e.g., when
1351 * it did ":bunload") or aborted the script processing! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352# ifdef FEAT_EVAL
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001353 if (buf_valid(buf) && buf != curbuf && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354# else
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001355 if (buf_valid(buf) && buf != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356# endif
1357#endif
1358 enter_buffer(buf);
1359}
1360
1361/*
1362 * Enter a new current buffer.
1363 * Old curbuf must have been abandoned already!
1364 */
1365 void
1366enter_buffer(buf)
1367 buf_T *buf;
1368{
1369 /* Copy buffer and window local option values. Not for a help buffer. */
1370 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1371 if (!buf->b_help)
1372 get_winopts(buf);
1373#ifdef FEAT_FOLDING
1374 else
1375 /* Remove all folds in the window. */
1376 clearFolding(curwin);
1377 foldUpdateAll(curwin); /* update folds (later). */
1378#endif
1379
1380 /* Get the buffer in the current window. */
1381 curwin->w_buffer = buf;
1382 curbuf = buf;
1383 ++curbuf->b_nwindows;
1384
1385#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001386 if (curwin->w_p_diff)
1387 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388#endif
1389
1390 /* Cursor on first line by default. */
1391 curwin->w_cursor.lnum = 1;
1392 curwin->w_cursor.col = 0;
1393#ifdef FEAT_VIRTUALEDIT
1394 curwin->w_cursor.coladd = 0;
1395#endif
1396 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001397#ifdef FEAT_AUTOCMD
1398 curwin->w_topline_was_set = FALSE;
1399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001401 /* mark cursor position as being invalid */
1402 curwin->w_valid = 0;
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 /* Make sure the buffer is loaded. */
1405 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001406 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001407#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001408 /* If there is no filetype, allow for detecting one. Esp. useful for
1409 * ":ball" used in a autocommand. If there already is a filetype we
1410 * might prefer to keep it. */
1411 if (*curbuf->b_p_ft == NUL)
1412 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001413#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 open_buffer(FALSE, NULL);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 else
1418 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001419 if (!msg_silent)
1420 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1422#ifdef FEAT_AUTOCMD
1423 curwin->w_topline = 1;
1424# ifdef FEAT_DIFF
1425 curwin->w_topfill = 0;
1426# endif
1427 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1428 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1429#endif
1430 }
1431
1432 /* If autocommands did not change the cursor position, restore cursor lnum
1433 * and possibly cursor col. */
1434 if (curwin->w_cursor.lnum == 1 && inindent(0))
1435 buflist_getfpos();
1436
1437 check_arg_idx(curwin); /* check for valid arg_idx */
1438#ifdef FEAT_TITLE
1439 maketitle();
1440#endif
1441#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001442 /* when autocmds didn't change it */
1443 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444#endif
1445 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1446
Bram Moolenaar009b2592004-10-24 19:18:58 +00001447#ifdef FEAT_NETBEANS_INTG
1448 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001449 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001450#endif
1451
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001452 /* Change directories when the 'acd' option is set. */
1453 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454
1455#ifdef FEAT_KEYMAP
1456 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001457 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001459#ifdef FEAT_SPELL
1460 /* May need to set the spell language. Can only do this after the buffer
1461 * has been properly setup. */
1462 if (!curbuf->b_help && curwin->w_p_spell && *curbuf->b_p_spl != NUL)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001463 (void)did_set_spelllang(curbuf);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001464#endif
1465
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 redraw_later(NOT_VALID);
1467}
1468
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001469#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1470/*
1471 * Change to the directory of the current buffer.
1472 */
1473 void
1474do_autochdir()
1475{
1476 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1477 shorten_fnames(TRUE);
1478}
1479#endif
1480
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481/*
1482 * functions for dealing with the buffer list
1483 */
1484
1485/*
1486 * Add a file name to the buffer list. Return a pointer to the buffer.
1487 * If the same file name already exists return a pointer to that buffer.
1488 * If it does not exist, or if fname == NULL, a new entry is created.
1489 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1490 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1491 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 * This is the ONLY way to create a new buffer.
1493 */
1494static int top_file_num = 1; /* highest file number */
1495
1496 buf_T *
1497buflist_new(ffname, sfname, lnum, flags)
1498 char_u *ffname; /* full path of fname or relative */
1499 char_u *sfname; /* short fname or NULL */
1500 linenr_T lnum; /* preferred cursor line */
1501 int flags; /* BLN_ defines */
1502{
1503 buf_T *buf;
1504#ifdef UNIX
1505 struct stat st;
1506#endif
1507
1508 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1509
1510 /*
1511 * If file name already exists in the list, update the entry.
1512 */
1513#ifdef UNIX
1514 /* On Unix we can use inode numbers when the file exists. Works better
1515 * for hard links. */
1516 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1517 st.st_dev = (dev_T)-1;
1518#endif
1519 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1520#ifdef UNIX
1521 buflist_findname_stat(ffname, &st)
1522#else
1523 buflist_findname(ffname)
1524#endif
1525 ) != NULL)
1526 {
1527 vim_free(ffname);
1528 if (lnum != 0)
1529 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1530 /* copy the options now, if 'cpo' doesn't have 's' and not done
1531 * already */
1532 buf_copy_options(buf, 0);
1533 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1534 {
1535 buf->b_p_bl = TRUE;
1536#ifdef FEAT_AUTOCMD
1537 if (!(flags & BLN_DUMMY))
1538 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1539#endif
1540 }
1541 return buf;
1542 }
1543
1544 /*
1545 * If the current buffer has no name and no contents, use the current
1546 * buffer. Otherwise: Need to allocate a new buffer structure.
1547 *
1548 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001549 * (A spell file buffer is allocated in spell.c, but that's not a normal
1550 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 */
1552 buf = NULL;
1553 if ((flags & BLN_CURBUF)
1554 && curbuf != NULL
1555 && curbuf->b_ffname == NULL
1556 && curbuf->b_nwindows <= 1
1557 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1558 {
1559 buf = curbuf;
1560#ifdef FEAT_AUTOCMD
1561 /* It's like this buffer is deleted. Watch out for autocommands that
1562 * change curbuf! If that happens, allocate a new buffer anyway. */
1563 if (curbuf->b_p_bl)
1564 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1565 if (buf == curbuf)
1566 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1567# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001568 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 return NULL;
1570# endif
1571#endif
1572#ifdef FEAT_QUICKFIX
1573# ifdef FEAT_AUTOCMD
1574 if (buf == curbuf)
1575# endif
1576 {
1577 /* Make sure 'bufhidden' and 'buftype' are empty */
1578 clear_string_option(&buf->b_p_bh);
1579 clear_string_option(&buf->b_p_bt);
1580 }
1581#endif
1582 }
1583 if (buf != curbuf || curbuf == NULL)
1584 {
1585 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1586 if (buf == NULL)
1587 {
1588 vim_free(ffname);
1589 return NULL;
1590 }
1591 }
1592
1593 if (ffname != NULL)
1594 {
1595 buf->b_ffname = ffname;
1596 buf->b_sfname = vim_strsave(sfname);
1597 }
1598
1599 clear_wininfo(buf);
1600 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1601
1602 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1603 || buf->b_wininfo == NULL)
1604 {
1605 vim_free(buf->b_ffname);
1606 buf->b_ffname = NULL;
1607 vim_free(buf->b_sfname);
1608 buf->b_sfname = NULL;
1609 if (buf != curbuf)
1610 free_buffer(buf);
1611 return NULL;
1612 }
1613
1614 if (buf == curbuf)
1615 {
1616 /* free all things allocated for this buffer */
1617 buf_freeall(buf, FALSE, FALSE);
1618 if (buf != curbuf) /* autocommands deleted the buffer! */
1619 return NULL;
1620#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001621 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 return NULL;
1623#endif
1624 /* buf->b_nwindows = 0; why was this here? */
1625 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1626#ifdef FEAT_KEYMAP
1627 /* need to reload lmaps and set b:keymap_name */
1628 curbuf->b_kmap_state |= KEYMAP_INIT;
1629#endif
1630 }
1631 else
1632 {
1633 /*
1634 * put new buffer at the end of the buffer list
1635 */
1636 buf->b_next = NULL;
1637 if (firstbuf == NULL) /* buffer list is empty */
1638 {
1639 buf->b_prev = NULL;
1640 firstbuf = buf;
1641 }
1642 else /* append new buffer at end of list */
1643 {
1644 lastbuf->b_next = buf;
1645 buf->b_prev = lastbuf;
1646 }
1647 lastbuf = buf;
1648
1649 buf->b_fnum = top_file_num++;
1650 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1651 {
1652 EMSG(_("W14: Warning: List of file names overflow"));
1653 if (emsg_silent == 0)
1654 {
1655 out_flush();
1656 ui_delay(3000L, TRUE); /* make sure it is noticed */
1657 }
1658 top_file_num = 1;
1659 }
1660
1661 /*
1662 * Always copy the options from the current buffer.
1663 */
1664 buf_copy_options(buf, BCO_ALWAYS);
1665 }
1666
1667 buf->b_wininfo->wi_fpos.lnum = lnum;
1668 buf->b_wininfo->wi_win = curwin;
1669
1670#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001671 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1672#endif
1673#ifdef FEAT_SYN_HL
1674 hash_init(&buf->b_keywtab);
1675 hash_init(&buf->b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676#endif
1677
1678 buf->b_fname = buf->b_sfname;
1679#ifdef UNIX
1680 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001681 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 else
1683 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001684 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 buf->b_dev = st.st_dev;
1686 buf->b_ino = st.st_ino;
1687 }
1688#endif
1689 buf->b_u_synced = TRUE;
1690 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001691 if (flags & BLN_DUMMY)
1692 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 buf_clear_file(buf);
1694 clrallmarks(buf); /* clear marks */
1695 fmarks_check_names(buf); /* check file marks for this file */
1696 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1697#ifdef FEAT_AUTOCMD
1698 if (!(flags & BLN_DUMMY))
1699 {
1700 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1701 if (flags & BLN_LISTED)
1702 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1703# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001704 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 return NULL;
1706# endif
1707 }
1708#endif
1709
1710 return buf;
1711}
1712
1713/*
1714 * Free the memory for the options of a buffer.
1715 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1716 * 'fileencoding'.
1717 */
1718 void
1719free_buf_options(buf, free_p_ff)
1720 buf_T *buf;
1721 int free_p_ff;
1722{
1723 if (free_p_ff)
1724 {
1725#ifdef FEAT_MBYTE
1726 clear_string_option(&buf->b_p_fenc);
1727#endif
1728 clear_string_option(&buf->b_p_ff);
1729#ifdef FEAT_QUICKFIX
1730 clear_string_option(&buf->b_p_bh);
1731 clear_string_option(&buf->b_p_bt);
1732#endif
1733 }
1734#ifdef FEAT_FIND_ID
1735 clear_string_option(&buf->b_p_def);
1736 clear_string_option(&buf->b_p_inc);
1737# ifdef FEAT_EVAL
1738 clear_string_option(&buf->b_p_inex);
1739# endif
1740#endif
1741#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1742 clear_string_option(&buf->b_p_inde);
1743 clear_string_option(&buf->b_p_indk);
1744#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001745#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1746 clear_string_option(&buf->b_p_bexpr);
1747#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001748#if defined(FEAT_EVAL)
1749 clear_string_option(&buf->b_p_fex);
1750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751#ifdef FEAT_CRYPT
1752 clear_string_option(&buf->b_p_key);
1753#endif
1754 clear_string_option(&buf->b_p_kp);
1755 clear_string_option(&buf->b_p_mps);
1756 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001757 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 clear_string_option(&buf->b_p_isk);
1759#ifdef FEAT_KEYMAP
1760 clear_string_option(&buf->b_p_keymap);
1761 ga_clear(&buf->b_kmap_ga);
1762#endif
1763#ifdef FEAT_COMMENTS
1764 clear_string_option(&buf->b_p_com);
1765#endif
1766#ifdef FEAT_FOLDING
1767 clear_string_option(&buf->b_p_cms);
1768#endif
1769 clear_string_option(&buf->b_p_nf);
1770#ifdef FEAT_SYN_HL
1771 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001772#endif
1773#ifdef FEAT_SPELL
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001774 clear_string_option(&buf->b_p_spc);
Bram Moolenaar86bc1fb2005-06-07 20:58:01 +00001775 clear_string_option(&buf->b_p_spf);
Bram Moolenaar0f7d31a2005-07-02 23:09:03 +00001776 vim_free(buf->b_cap_prog);
1777 buf->b_cap_prog = NULL;
Bram Moolenaara0dea672005-03-20 22:23:10 +00001778 clear_string_option(&buf->b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779#endif
1780#ifdef FEAT_SEARCHPATH
1781 clear_string_option(&buf->b_p_sua);
1782#endif
1783#ifdef FEAT_AUTOCMD
1784 clear_string_option(&buf->b_p_ft);
1785#endif
1786#ifdef FEAT_OSFILETYPE
1787 clear_string_option(&buf->b_p_oft);
1788#endif
1789#ifdef FEAT_CINDENT
1790 clear_string_option(&buf->b_p_cink);
1791 clear_string_option(&buf->b_p_cino);
1792#endif
1793#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1794 clear_string_option(&buf->b_p_cinw);
1795#endif
1796#ifdef FEAT_INS_EXPAND
1797 clear_string_option(&buf->b_p_cpt);
1798#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001799#ifdef FEAT_COMPL_FUNC
1800 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001801 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803#ifdef FEAT_QUICKFIX
1804 clear_string_option(&buf->b_p_gp);
1805 clear_string_option(&buf->b_p_mp);
1806 clear_string_option(&buf->b_p_efm);
1807#endif
1808 clear_string_option(&buf->b_p_ep);
1809 clear_string_option(&buf->b_p_path);
1810 clear_string_option(&buf->b_p_tags);
1811#ifdef FEAT_INS_EXPAND
1812 clear_string_option(&buf->b_p_dict);
1813 clear_string_option(&buf->b_p_tsr);
1814#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001815#ifdef FEAT_TEXTOBJ
1816 clear_string_option(&buf->b_p_qe);
1817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 buf->b_p_ar = -1;
1819}
1820
1821/*
1822 * get alternate file n
1823 * set linenr to lnum or altfpos.lnum if lnum == 0
1824 * also set cursor column to altfpos.col if 'startofline' is not set.
1825 * if (options & GETF_SETMARK) call setpcmark()
1826 * if (options & GETF_ALT) we are jumping to an alternate file.
1827 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1828 *
1829 * return FAIL for failure, OK for success
1830 */
1831 int
1832buflist_getfile(n, lnum, options, forceit)
1833 int n;
1834 linenr_T lnum;
1835 int options;
1836 int forceit;
1837{
1838 buf_T *buf;
1839#ifdef FEAT_WINDOWS
1840 win_T *wp = NULL;
1841#endif
1842 pos_T *fpos;
1843 colnr_T col;
1844
1845 buf = buflist_findnr(n);
1846 if (buf == NULL)
1847 {
1848 if ((options & GETF_ALT) && n == 0)
1849 EMSG(_(e_noalt));
1850 else
1851 EMSGN(_("E92: Buffer %ld not found"), n);
1852 return FAIL;
1853 }
1854
1855 /* if alternate file is the current buffer, nothing to do */
1856 if (buf == curbuf)
1857 return OK;
1858
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001859 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001860 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001861 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001863 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001864#ifdef FEAT_AUTOCMD
1865 if (curbuf_locked())
1866 return FAIL;
1867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
1869 /* altfpos may be changed by getfile(), get it now */
1870 if (lnum == 0)
1871 {
1872 fpos = buflist_findfpos(buf);
1873 lnum = fpos->lnum;
1874 col = fpos->col;
1875 }
1876 else
1877 col = 0;
1878
1879#ifdef FEAT_WINDOWS
1880 if (options & GETF_SWITCH)
1881 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001882 /* If 'switchbuf' contains "useopen": jump to first window containing
1883 * "buf" if one exists */
1884 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001886 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1887 * page containing "buf" if one exists */
1888 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001889 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001890 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1891 * isn't empty: open new window */
1892 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001894 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1895 tabpage_new();
1896 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 return FAIL;
1898# ifdef FEAT_SCROLLBIND
1899 curwin->w_p_scb = FALSE;
1900# endif
1901 }
1902 }
1903#endif
1904
1905 ++RedrawingDisabled;
1906 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1907 lnum, forceit) <= 0)
1908 {
1909 --RedrawingDisabled;
1910
1911 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1912 if (!p_sol && col != 0)
1913 {
1914 curwin->w_cursor.col = col;
1915 check_cursor_col();
1916#ifdef FEAT_VIRTUALEDIT
1917 curwin->w_cursor.coladd = 0;
1918#endif
1919 curwin->w_set_curswant = TRUE;
1920 }
1921 return OK;
1922 }
1923 --RedrawingDisabled;
1924 return FAIL;
1925}
1926
1927/*
1928 * go to the last know line number for the current buffer
1929 */
1930 void
1931buflist_getfpos()
1932{
1933 pos_T *fpos;
1934
1935 fpos = buflist_findfpos(curbuf);
1936
1937 curwin->w_cursor.lnum = fpos->lnum;
1938 check_cursor_lnum();
1939
1940 if (p_sol)
1941 curwin->w_cursor.col = 0;
1942 else
1943 {
1944 curwin->w_cursor.col = fpos->col;
1945 check_cursor_col();
1946#ifdef FEAT_VIRTUALEDIT
1947 curwin->w_cursor.coladd = 0;
1948#endif
1949 curwin->w_set_curswant = TRUE;
1950 }
1951}
1952
Bram Moolenaar81695252004-12-29 20:58:21 +00001953#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1954/*
1955 * Find file in buffer list by name (it has to be for the current window).
1956 * Returns NULL if not found.
1957 */
1958 buf_T *
1959buflist_findname_exp(fname)
1960 char_u *fname;
1961{
1962 char_u *ffname;
1963 buf_T *buf = NULL;
1964
1965 /* First make the name into a full path name */
1966 ffname = FullName_save(fname,
1967#ifdef UNIX
1968 TRUE /* force expansion, get rid of symbolic links */
1969#else
1970 FALSE
1971#endif
1972 );
1973 if (ffname != NULL)
1974 {
1975 buf = buflist_findname(ffname);
1976 vim_free(ffname);
1977 }
1978 return buf;
1979}
1980#endif
1981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982/*
1983 * Find file in buffer list by name (it has to be for the current window).
1984 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00001985 * Skips dummy buffers.
1986 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 */
1988 buf_T *
1989buflist_findname(ffname)
1990 char_u *ffname;
1991{
1992#ifdef UNIX
1993 struct stat st;
1994
1995 if (mch_stat((char *)ffname, &st) < 0)
1996 st.st_dev = (dev_T)-1;
1997 return buflist_findname_stat(ffname, &st);
1998}
1999
2000/*
2001 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2002 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002003 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 */
2005 static buf_T *
2006buflist_findname_stat(ffname, stp)
2007 char_u *ffname;
2008 struct stat *stp;
2009{
2010#endif
2011 buf_T *buf;
2012
2013 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002014 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015#ifdef UNIX
2016 , stp
2017#endif
2018 ))
2019 return buf;
2020 return NULL;
2021}
2022
2023#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2024/*
2025 * Find file in buffer list by a regexp pattern.
2026 * Return fnum of the found buffer.
2027 * Return < 0 for error.
2028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 int
2030buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2031 char_u *pattern;
2032 char_u *pattern_end; /* pointer to first char after pattern */
2033 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002034 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
2036 buf_T *buf;
2037 regprog_T *prog;
2038 int match = -1;
2039 int find_listed;
2040 char_u *pat;
2041 char_u *patend;
2042 int attempt;
2043 char_u *p;
2044 int toggledollar;
2045
2046 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2047 {
2048 if (*pattern == '%')
2049 match = curbuf->b_fnum;
2050 else
2051 match = curwin->w_alt_fnum;
2052#ifdef FEAT_DIFF
2053 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2054 match = -1;
2055#endif
2056 }
2057
2058 /*
2059 * Try four ways of matching a listed buffer:
2060 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002061 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 * attempt == 2: with '$' at end (only match at end)
2063 * attempt == 3: with '^' at start and '$' at end (only full match)
2064 * Repeat this for finding an unlisted buffer if there was no matching
2065 * listed buffer.
2066 */
2067 else
2068 {
2069 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2070 if (pat == NULL)
2071 return -1;
2072 patend = pat + STRLEN(pat) - 1;
2073 toggledollar = (patend > pat && *patend == '$');
2074
2075 /* First try finding a listed buffer. If not found and "unlisted"
2076 * is TRUE, try finding an unlisted buffer. */
2077 find_listed = TRUE;
2078 for (;;)
2079 {
2080 for (attempt = 0; attempt <= 3; ++attempt)
2081 {
2082 /* may add '^' and '$' */
2083 if (toggledollar)
2084 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2085 p = pat;
2086 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2087 ++p;
2088 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2089 if (prog == NULL)
2090 {
2091 vim_free(pat);
2092 return -1;
2093 }
2094
2095 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2096 if (buf->b_p_bl == find_listed
2097#ifdef FEAT_DIFF
2098 && (!diffmode || diff_mode_buf(buf))
2099#endif
2100 && buflist_match(prog, buf) != NULL)
2101 {
2102 if (match >= 0) /* already found a match */
2103 {
2104 match = -2;
2105 break;
2106 }
2107 match = buf->b_fnum; /* remember first match */
2108 }
2109
2110 vim_free(prog);
2111 if (match >= 0) /* found one match */
2112 break;
2113 }
2114
2115 /* Only search for unlisted buffers if there was no match with
2116 * a listed buffer. */
2117 if (!unlisted || !find_listed || match != -1)
2118 break;
2119 find_listed = FALSE;
2120 }
2121
2122 vim_free(pat);
2123 }
2124
2125 if (match == -2)
2126 EMSG2(_("E93: More than one match for %s"), pattern);
2127 else if (match < 0)
2128 EMSG2(_("E94: No matching buffer for %s"), pattern);
2129 return match;
2130}
2131#endif
2132
2133#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2134
2135/*
2136 * Find all buffer names that match.
2137 * For command line expansion of ":buf" and ":sbuf".
2138 * Return OK if matches found, FAIL otherwise.
2139 */
2140 int
2141ExpandBufnames(pat, num_file, file, options)
2142 char_u *pat;
2143 int *num_file;
2144 char_u ***file;
2145 int options;
2146{
2147 int count = 0;
2148 buf_T *buf;
2149 int round;
2150 char_u *p;
2151 int attempt;
2152 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002153 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
2155 *num_file = 0; /* return values in case of FAIL */
2156 *file = NULL;
2157
Bram Moolenaar05159a02005-02-26 23:04:13 +00002158 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2159 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002161 patc = alloc((unsigned)STRLEN(pat) + 11);
2162 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002164 STRCPY(patc, "\\(^\\|[\\/]\\)");
2165 STRCPY(patc + 11, pat + 1);
2166 }
2167 else
2168 patc = pat;
2169
2170 /*
2171 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002172 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002173 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002174 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002175 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002176 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002177 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002178 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002179 if (prog == NULL)
2180 {
2181 if (patc != pat)
2182 vim_free(patc);
2183 return FAIL;
2184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185
2186 /*
2187 * round == 1: Count the matches.
2188 * round == 2: Build the array to keep the matches.
2189 */
2190 for (round = 1; round <= 2; ++round)
2191 {
2192 count = 0;
2193 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2194 {
2195 if (!buf->b_p_bl) /* skip unlisted buffers */
2196 continue;
2197 p = buflist_match(prog, buf);
2198 if (p != NULL)
2199 {
2200 if (round == 1)
2201 ++count;
2202 else
2203 {
2204 if (options & WILD_HOME_REPLACE)
2205 p = home_replace_save(buf, p);
2206 else
2207 p = vim_strsave(p);
2208 (*file)[count++] = p;
2209 }
2210 }
2211 }
2212 if (count == 0) /* no match found, break here */
2213 break;
2214 if (round == 1)
2215 {
2216 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2217 if (*file == NULL)
2218 {
2219 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002220 if (patc != pat)
2221 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 return FAIL;
2223 }
2224 }
2225 }
2226 vim_free(prog);
2227 if (count) /* match(es) found, break here */
2228 break;
2229 }
2230
Bram Moolenaar05159a02005-02-26 23:04:13 +00002231 if (patc != pat)
2232 vim_free(patc);
2233
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 *num_file = count;
2235 return (count == 0 ? FAIL : OK);
2236}
2237
2238#endif /* FEAT_CMDL_COMPL */
2239
2240#ifdef HAVE_BUFLIST_MATCH
2241/*
2242 * Check for a match on the file name for buffer "buf" with regprog "prog".
2243 */
2244 static char_u *
2245buflist_match(prog, buf)
2246 regprog_T *prog;
2247 buf_T *buf;
2248{
2249 char_u *match;
2250
2251 /* First try the short file name, then the long file name. */
2252 match = fname_match(prog, buf->b_sfname);
2253 if (match == NULL)
2254 match = fname_match(prog, buf->b_ffname);
2255
2256 return match;
2257}
2258
2259/*
2260 * Try matching the regexp in "prog" with file name "name".
2261 * Return "name" when there is a match, NULL when not.
2262 */
2263 static char_u *
2264fname_match(prog, name)
2265 regprog_T *prog;
2266 char_u *name;
2267{
2268 char_u *match = NULL;
2269 char_u *p;
2270 regmatch_T regmatch;
2271
2272 if (name != NULL)
2273 {
2274 regmatch.regprog = prog;
2275#ifdef CASE_INSENSITIVE_FILENAME
2276 regmatch.rm_ic = TRUE; /* Always ignore case */
2277#else
2278 regmatch.rm_ic = FALSE; /* Never ignore case */
2279#endif
2280
2281 if (vim_regexec(&regmatch, name, (colnr_T)0))
2282 match = name;
2283 else
2284 {
2285 /* Replace $(HOME) with '~' and try matching again. */
2286 p = home_replace_save(NULL, name);
2287 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2288 match = name;
2289 vim_free(p);
2290 }
2291 }
2292
2293 return match;
2294}
2295#endif
2296
2297/*
2298 * find file in buffer list by number
2299 */
2300 buf_T *
2301buflist_findnr(nr)
2302 int nr;
2303{
2304 buf_T *buf;
2305
2306 if (nr == 0)
2307 nr = curwin->w_alt_fnum;
2308 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2309 if (buf->b_fnum == nr)
2310 return (buf);
2311 return NULL;
2312}
2313
2314/*
2315 * Get name of file 'n' in the buffer list.
2316 * When the file has no name an empty string is returned.
2317 * home_replace() is used to shorten the file name (used for marks).
2318 * Returns a pointer to allocated memory, of NULL when failed.
2319 */
2320 char_u *
2321buflist_nr2name(n, fullname, helptail)
2322 int n;
2323 int fullname;
2324 int helptail; /* for help buffers return tail only */
2325{
2326 buf_T *buf;
2327
2328 buf = buflist_findnr(n);
2329 if (buf == NULL)
2330 return NULL;
2331 return home_replace_save(helptail ? buf : NULL,
2332 fullname ? buf->b_ffname : buf->b_fname);
2333}
2334
2335/*
2336 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2337 * When "copy_options" is TRUE save the local window option values.
2338 * When "lnum" is 0 only do the options.
2339 */
2340 static void
2341buflist_setfpos(buf, win, lnum, col, copy_options)
2342 buf_T *buf;
2343 win_T *win;
2344 linenr_T lnum;
2345 colnr_T col;
2346 int copy_options;
2347{
2348 wininfo_T *wip;
2349
2350 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2351 if (wip->wi_win == win)
2352 break;
2353 if (wip == NULL)
2354 {
2355 /* allocate a new entry */
2356 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2357 if (wip == NULL)
2358 return;
2359 wip->wi_win = win;
2360 if (lnum == 0) /* set lnum even when it's 0 */
2361 lnum = 1;
2362 }
2363 else
2364 {
2365 /* remove the entry from the list */
2366 if (wip->wi_prev)
2367 wip->wi_prev->wi_next = wip->wi_next;
2368 else
2369 buf->b_wininfo = wip->wi_next;
2370 if (wip->wi_next)
2371 wip->wi_next->wi_prev = wip->wi_prev;
2372 if (copy_options && wip->wi_optset)
2373 {
2374 clear_winopt(&wip->wi_opt);
2375#ifdef FEAT_FOLDING
2376 deleteFoldRecurse(&wip->wi_folds);
2377#endif
2378 }
2379 }
2380 if (lnum != 0)
2381 {
2382 wip->wi_fpos.lnum = lnum;
2383 wip->wi_fpos.col = col;
2384 }
2385 if (copy_options)
2386 {
2387 /* Save the window-specific option values. */
2388 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2389#ifdef FEAT_FOLDING
2390 wip->wi_fold_manual = win->w_fold_manual;
2391 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2392#endif
2393 wip->wi_optset = TRUE;
2394 }
2395
2396 /* insert the entry in front of the list */
2397 wip->wi_next = buf->b_wininfo;
2398 buf->b_wininfo = wip;
2399 wip->wi_prev = NULL;
2400 if (wip->wi_next)
2401 wip->wi_next->wi_prev = wip;
2402
2403 return;
2404}
2405
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002406#ifdef FEAT_DIFF
2407static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2408
2409/*
2410 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2411 * page. That's because a diff is local to a tab page.
2412 */
2413 static int
2414wininfo_other_tab_diff(wip)
2415 wininfo_T *wip;
2416{
2417 win_T *wp;
2418
2419 if (wip->wi_opt.wo_diff)
2420 {
2421 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2422 /* return FALSE when it's a window in the current tab page, thus
2423 * the buffer was in diff mode here */
2424 if (wip->wi_win == wp)
2425 return FALSE;
2426 return TRUE;
2427 }
2428 return FALSE;
2429}
2430#endif
2431
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432/*
2433 * Find info for the current window in buffer "buf".
2434 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002435 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2436 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 * Returns NULL when there isn't any info.
2438 */
2439 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002440find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002442 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443{
2444 wininfo_T *wip;
2445
2446 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002447 if (wip->wi_win == curwin
2448#ifdef FEAT_DIFF
2449 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2450#endif
2451 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002453
2454 /* If no wininfo for curwin, use the first in the list (that doesn't have
2455 * 'diff' set and is in another tab page). */
2456 if (wip == NULL)
2457 {
2458#ifdef FEAT_DIFF
2459 if (skip_diff_buffer)
2460 {
2461 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2462 if (!wininfo_other_tab_diff(wip))
2463 break;
2464 }
2465 else
2466#endif
2467 wip = buf->b_wininfo;
2468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 return wip;
2470}
2471
2472/*
2473 * Reset the local window options to the values last used in this window.
2474 * If the buffer wasn't used in this window before, use the values from
2475 * the most recently used window. If the values were never set, use the
2476 * global values for the window.
2477 */
2478 void
2479get_winopts(buf)
2480 buf_T *buf;
2481{
2482 wininfo_T *wip;
2483
2484 clear_winopt(&curwin->w_onebuf_opt);
2485#ifdef FEAT_FOLDING
2486 clearFolding(curwin);
2487#endif
2488
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002489 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 if (wip != NULL && wip->wi_optset)
2491 {
2492 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2493#ifdef FEAT_FOLDING
2494 curwin->w_fold_manual = wip->wi_fold_manual;
2495 curwin->w_foldinvalid = TRUE;
2496 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2497#endif
2498 }
2499 else
2500 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2501
2502#ifdef FEAT_FOLDING
2503 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2504 if (p_fdls >= 0)
2505 curwin->w_p_fdl = p_fdls;
2506#endif
2507}
2508
2509/*
2510 * Find the position (lnum and col) for the buffer 'buf' for the current
2511 * window.
2512 * Returns a pointer to no_position if no position is found.
2513 */
2514 pos_T *
2515buflist_findfpos(buf)
2516 buf_T *buf;
2517{
2518 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002519 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002521 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (wip != NULL)
2523 return &(wip->wi_fpos);
2524 else
2525 return &no_position;
2526}
2527
2528/*
2529 * Find the lnum for the buffer 'buf' for the current window.
2530 */
2531 linenr_T
2532buflist_findlnum(buf)
2533 buf_T *buf;
2534{
2535 return buflist_findfpos(buf)->lnum;
2536}
2537
2538#if defined(FEAT_LISTCMDS) || defined(PROTO)
2539/*
2540 * List all know file names (for :files and :buffers command).
2541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 void
2543buflist_list(eap)
2544 exarg_T *eap;
2545{
2546 buf_T *buf;
2547 int len;
2548 int i;
2549
2550 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2551 {
2552 /* skip unlisted buffers, unless ! was used */
2553 if (!buf->b_p_bl && !eap->forceit)
2554 continue;
2555 msg_putchar('\n');
2556 if (buf_spname(buf) != NULL)
2557 STRCPY(NameBuff, buf_spname(buf));
2558 else
2559 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2560
Bram Moolenaar50cde822005-06-05 21:54:54 +00002561 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 buf->b_fnum,
2563 buf->b_p_bl ? ' ' : 'u',
2564 buf == curbuf ? '%' :
2565 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2566 buf->b_ml.ml_mfp == NULL ? ' ' :
2567 (buf->b_nwindows == 0 ? 'h' : 'a'),
2568 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2569 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002570 : (bufIsChanged(buf) ? '+' : ' '),
2571 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
2573 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 i = 40 - vim_strsize(IObuff);
2575 do
2576 {
2577 IObuff[len++] = ' ';
2578 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002579 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2580 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002581 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 msg_outtrans(IObuff);
2583 out_flush(); /* output one line at a time */
2584 ui_breakcheck();
2585 }
2586}
2587#endif
2588
2589/*
2590 * Get file name and line number for file 'fnum'.
2591 * Used by DoOneCmd() for translating '%' and '#'.
2592 * Used by insert_reg() and cmdline_paste() for '#' register.
2593 * Return FAIL if not found, OK for success.
2594 */
2595 int
2596buflist_name_nr(fnum, fname, lnum)
2597 int fnum;
2598 char_u **fname;
2599 linenr_T *lnum;
2600{
2601 buf_T *buf;
2602
2603 buf = buflist_findnr(fnum);
2604 if (buf == NULL || buf->b_fname == NULL)
2605 return FAIL;
2606
2607 *fname = buf->b_fname;
2608 *lnum = buflist_findlnum(buf);
2609
2610 return OK;
2611}
2612
2613/*
2614 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2615 * The file name with the full path is also remembered, for when :cd is used.
2616 * Returns FAIL for failure (file name already in use by other buffer)
2617 * OK otherwise.
2618 */
2619 int
2620setfname(buf, ffname, sfname, message)
2621 buf_T *buf;
2622 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002623 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624{
Bram Moolenaar81695252004-12-29 20:58:21 +00002625 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626#ifdef UNIX
2627 struct stat st;
2628#endif
2629
2630 if (ffname == NULL || *ffname == NUL)
2631 {
2632 /* Removing the name. */
2633 vim_free(buf->b_ffname);
2634 vim_free(buf->b_sfname);
2635 buf->b_ffname = NULL;
2636 buf->b_sfname = NULL;
2637#ifdef UNIX
2638 st.st_dev = (dev_T)-1;
2639#endif
2640 }
2641 else
2642 {
2643 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2644 if (ffname == NULL) /* out of memory */
2645 return FAIL;
2646
2647 /*
2648 * if the file name is already used in another buffer:
2649 * - if the buffer is loaded, fail
2650 * - if the buffer is not loaded, delete it from the list
2651 */
2652#ifdef UNIX
2653 if (mch_stat((char *)ffname, &st) < 0)
2654 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002655#endif
2656 if (!(buf->b_flags & BF_DUMMY))
2657#ifdef UNIX
2658 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002660 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661#endif
2662 if (obuf != NULL && obuf != buf)
2663 {
2664 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2665 {
2666 if (message)
2667 EMSG(_("E95: Buffer with this name already exists"));
2668 vim_free(ffname);
2669 return FAIL;
2670 }
2671 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2672 }
2673 sfname = vim_strsave(sfname);
2674 if (ffname == NULL || sfname == NULL)
2675 {
2676 vim_free(sfname);
2677 vim_free(ffname);
2678 return FAIL;
2679 }
2680#ifdef USE_FNAME_CASE
2681# ifdef USE_LONG_FNAME
2682 if (USE_LONG_FNAME)
2683# endif
2684 fname_case(sfname, 0); /* set correct case for short file name */
2685#endif
2686 vim_free(buf->b_ffname);
2687 vim_free(buf->b_sfname);
2688 buf->b_ffname = ffname;
2689 buf->b_sfname = sfname;
2690 }
2691 buf->b_fname = buf->b_sfname;
2692#ifdef UNIX
2693 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002694 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 else
2696 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002697 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 buf->b_dev = st.st_dev;
2699 buf->b_ino = st.st_ino;
2700 }
2701#endif
2702
2703#ifndef SHORT_FNAME
2704 buf->b_shortname = FALSE;
2705#endif
2706
2707 buf_name_changed(buf);
2708 return OK;
2709}
2710
2711/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002712 * Crude way of changing the name of a buffer. Use with care!
2713 * The name should be relative to the current directory.
2714 */
2715 void
2716buf_set_name(fnum, name)
2717 int fnum;
2718 char_u *name;
2719{
2720 buf_T *buf;
2721
2722 buf = buflist_findnr(fnum);
2723 if (buf != NULL)
2724 {
2725 vim_free(buf->b_sfname);
2726 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002727 buf->b_ffname = vim_strsave(name);
2728 buf->b_sfname = NULL;
2729 /* Allocate ffname and expand into full path. Also resolves .lnk
2730 * files on Win32. */
2731 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002732 buf->b_fname = buf->b_sfname;
2733 }
2734}
2735
2736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 * Take care of what needs to be done when the name of buffer "buf" has
2738 * changed.
2739 */
2740 void
2741buf_name_changed(buf)
2742 buf_T *buf;
2743{
2744 /*
2745 * If the file name changed, also change the name of the swapfile
2746 */
2747 if (buf->b_ml.ml_mfp != NULL)
2748 ml_setname(buf);
2749
2750 if (curwin->w_buffer == buf)
2751 check_arg_idx(curwin); /* check file name for arg list */
2752#ifdef FEAT_TITLE
2753 maketitle(); /* set window title */
2754#endif
2755#ifdef FEAT_WINDOWS
2756 status_redraw_all(); /* status lines need to be redrawn */
2757#endif
2758 fmarks_check_names(buf); /* check named file marks */
2759 ml_timestamp(buf); /* reset timestamp */
2760}
2761
2762/*
2763 * set alternate file name for current window
2764 *
2765 * Used by do_one_cmd(), do_write() and do_ecmd().
2766 * Return the buffer.
2767 */
2768 buf_T *
2769setaltfname(ffname, sfname, lnum)
2770 char_u *ffname;
2771 char_u *sfname;
2772 linenr_T lnum;
2773{
2774 buf_T *buf;
2775
2776 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2777 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002778 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 curwin->w_alt_fnum = buf->b_fnum;
2780 return buf;
2781}
2782
2783/*
2784 * Get alternate file name for current window.
2785 * Return NULL if there isn't any, and give error message if requested.
2786 */
2787 char_u *
2788getaltfname(errmsg)
2789 int errmsg; /* give error message */
2790{
2791 char_u *fname;
2792 linenr_T dummy;
2793
2794 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2795 {
2796 if (errmsg)
2797 EMSG(_(e_noalt));
2798 return NULL;
2799 }
2800 return fname;
2801}
2802
2803/*
2804 * Add a file name to the buflist and return its number.
2805 * Uses same flags as buflist_new(), except BLN_DUMMY.
2806 *
2807 * used by qf_init(), main() and doarglist()
2808 */
2809 int
2810buflist_add(fname, flags)
2811 char_u *fname;
2812 int flags;
2813{
2814 buf_T *buf;
2815
2816 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2817 if (buf != NULL)
2818 return buf->b_fnum;
2819 return 0;
2820}
2821
2822#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2823/*
2824 * Adjust slashes in file names. Called after 'shellslash' was set.
2825 */
2826 void
2827buflist_slash_adjust()
2828{
2829 buf_T *bp;
2830
2831 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2832 {
2833 if (bp->b_ffname != NULL)
2834 slash_adjust(bp->b_ffname);
2835 if (bp->b_sfname != NULL)
2836 slash_adjust(bp->b_sfname);
2837 }
2838}
2839#endif
2840
2841/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002842 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 * Also save the local window option values.
2844 */
2845 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002846buflist_altfpos(win)
2847 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002849 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850}
2851
2852/*
2853 * Return TRUE if 'ffname' is not the same file as current file.
2854 * Fname must have a full path (expanded by mch_FullName()).
2855 */
2856 int
2857otherfile(ffname)
2858 char_u *ffname;
2859{
2860 return otherfile_buf(curbuf, ffname
2861#ifdef UNIX
2862 , NULL
2863#endif
2864 );
2865}
2866
2867 static int
2868otherfile_buf(buf, ffname
2869#ifdef UNIX
2870 , stp
2871#endif
2872 )
2873 buf_T *buf;
2874 char_u *ffname;
2875#ifdef UNIX
2876 struct stat *stp;
2877#endif
2878{
2879 /* no name is different */
2880 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2881 return TRUE;
2882 if (fnamecmp(ffname, buf->b_ffname) == 0)
2883 return FALSE;
2884#ifdef UNIX
2885 {
2886 struct stat st;
2887
2888 /* If no struct stat given, get it now */
2889 if (stp == NULL)
2890 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002891 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 st.st_dev = (dev_T)-1;
2893 stp = &st;
2894 }
2895 /* Use dev/ino to check if the files are the same, even when the names
2896 * are different (possible with links). Still need to compare the
2897 * name above, for when the file doesn't exist yet.
2898 * Problem: The dev/ino changes when a file is deleted (and created
2899 * again) and remains the same when renamed/moved. We don't want to
2900 * mch_stat() each buffer each time, that would be too slow. Get the
2901 * dev/ino again when they appear to match, but not when they appear
2902 * to be different: Could skip a buffer when it's actually the same
2903 * file. */
2904 if (buf_same_ino(buf, stp))
2905 {
2906 buf_setino(buf);
2907 if (buf_same_ino(buf, stp))
2908 return FALSE;
2909 }
2910 }
2911#endif
2912 return TRUE;
2913}
2914
2915#if defined(UNIX) || defined(PROTO)
2916/*
2917 * Set inode and device number for a buffer.
2918 * Must always be called when b_fname is changed!.
2919 */
2920 void
2921buf_setino(buf)
2922 buf_T *buf;
2923{
2924 struct stat st;
2925
2926 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2927 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002928 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 buf->b_dev = st.st_dev;
2930 buf->b_ino = st.st_ino;
2931 }
2932 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002933 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934}
2935
2936/*
2937 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2938 */
2939 static int
2940buf_same_ino(buf, stp)
2941 buf_T *buf;
2942 struct stat *stp;
2943{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002944 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 && stp->st_dev == buf->b_dev
2946 && stp->st_ino == buf->b_ino);
2947}
2948#endif
2949
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002950/*
2951 * Print info about the current buffer.
2952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 void
2954fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002955 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 int shorthelp;
2957 int dont_truncate;
2958{
2959 char_u *name;
2960 int n;
2961 char_u *p;
2962 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002963 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
2965 buffer = alloc(IOSIZE);
2966 if (buffer == NULL)
2967 return;
2968
2969 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2970 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002971 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 p = buffer + STRLEN(buffer);
2973 }
2974 else
2975 p = buffer;
2976
2977 *p++ = '"';
2978 if (buf_spname(curbuf) != NULL)
2979 STRCPY(p, buf_spname(curbuf));
2980 else
2981 {
2982 if (!fullname && curbuf->b_fname != NULL)
2983 name = curbuf->b_fname;
2984 else
2985 name = curbuf->b_ffname;
2986 home_replace(shorthelp ? curbuf : NULL, name, p,
2987 (int)(IOSIZE - (p - buffer)), TRUE);
2988 }
2989
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002990 len = STRLEN(buffer);
2991 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 "\"%s%s%s%s%s%s",
2993 curbufIsChanged() ? (shortmess(SHM_MOD)
2994 ? " [+]" : _(" [Modified]")) : " ",
2995 (curbuf->b_flags & BF_NOTEDITED)
2996#ifdef FEAT_QUICKFIX
2997 && !bt_dontwrite(curbuf)
2998#endif
2999 ? _("[Not edited]") : "",
3000 (curbuf->b_flags & BF_NEW)
3001#ifdef FEAT_QUICKFIX
3002 && !bt_dontwrite(curbuf)
3003#endif
3004 ? _("[New file]") : "",
3005 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3006 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3007 : _("[readonly]")) : "",
3008 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3009 || curbuf->b_p_ro) ?
3010 " " : "");
3011 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3012 * causes an overflow, thus for large numbers divide instead. */
3013 if (curwin->w_cursor.lnum > 1000000L)
3014 n = (int)(((long)curwin->w_cursor.lnum) /
3015 ((long)curbuf->b_ml.ml_line_count / 100L));
3016 else
3017 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3018 (long)curbuf->b_ml.ml_line_count);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003019 len = STRLEN(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3021 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003022 vim_snprintf((char *)buffer + len, IOSIZE - len, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 }
3024#ifdef FEAT_CMDL_INFO
3025 else if (p_ru)
3026 {
3027 /* Current line and column are already on the screen -- webb */
3028 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003029 vim_snprintf((char *)buffer + len, IOSIZE - len,
3030 _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003032 vim_snprintf((char *)buffer + len, IOSIZE - len,
3033 _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 (long)curbuf->b_ml.ml_line_count, n);
3035 }
3036#endif
3037 else
3038 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003039 vim_snprintf((char *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 _("line %ld of %ld --%d%%-- col "),
3041 (long)curwin->w_cursor.lnum,
3042 (long)curbuf->b_ml.ml_line_count,
3043 n);
3044 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003045 len = STRLEN(buffer);
3046 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3048 }
3049
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003050 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051
3052 if (dont_truncate)
3053 {
3054 /* Temporarily set msg_scroll to avoid the message being truncated.
3055 * First call msg_start() to get the message in the right place. */
3056 msg_start();
3057 n = msg_scroll;
3058 msg_scroll = TRUE;
3059 msg(buffer);
3060 msg_scroll = n;
3061 }
3062 else
3063 {
3064 p = msg_trunc_attr(buffer, FALSE, 0);
3065 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 /* Need to repeat the message after redrawing when:
3067 * - When restart_edit is set (otherwise there will be a delay
3068 * before redrawing).
3069 * - When the screen was scrolled but there is no wait-return
3070 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003071 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 }
3073
3074 vim_free(buffer);
3075}
3076
3077 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003078col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003080 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 int col;
3082 int vcol;
3083{
3084 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003085 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003087 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088}
3089
3090#if defined(FEAT_TITLE) || defined(PROTO)
3091/*
3092 * put file name in title bar of window and in icon title
3093 */
3094
3095static char_u *lasttitle = NULL;
3096static char_u *lasticon = NULL;
3097
3098 void
3099maketitle()
3100{
3101 char_u *p;
3102 char_u *t_str = NULL;
3103 char_u *i_name;
3104 char_u *i_str = NULL;
3105 int maxlen = 0;
3106 int len;
3107 int mustset;
3108 char_u buf[IOSIZE];
3109 int off;
3110
3111 if (!redrawing())
3112 {
3113 /* Postpone updating the title when 'lazyredraw' is set. */
3114 need_maketitle = TRUE;
3115 return;
3116 }
3117
3118 need_maketitle = FALSE;
3119 if (!p_title && !p_icon)
3120 return;
3121
3122 if (p_title)
3123 {
3124 if (p_titlelen > 0)
3125 {
3126 maxlen = p_titlelen * Columns / 100;
3127 if (maxlen < 10)
3128 maxlen = 10;
3129 }
3130
3131 t_str = buf;
3132 if (*p_titlestring != NUL)
3133 {
3134#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003135 if (stl_syntax & STL_IN_TITLE)
3136 {
3137 int use_sandbox = FALSE;
3138 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003139
3140# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003141 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003142# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003143 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003145 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003146 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003147 if (called_emsg)
3148 set_string_option_direct((char_u *)"titlestring", -1,
3149 (char_u *)"", OPT_FREE, SID_ERROR);
3150 called_emsg |= save_called_emsg;
3151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 else
3153#endif
3154 t_str = p_titlestring;
3155 }
3156 else
3157 {
3158 /* format: "fname + (path) (1 of 2) - VIM" */
3159
3160 if (curbuf->b_fname == NULL)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003161 STRCPY(buf, _("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 else
3163 {
3164 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003165 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 }
3168
3169 switch (bufIsChanged(curbuf)
3170 + (curbuf->b_p_ro * 2)
3171 + (!curbuf->b_p_ma * 4))
3172 {
3173 case 1: STRCAT(buf, " +"); break;
3174 case 2: STRCAT(buf, " ="); break;
3175 case 3: STRCAT(buf, " =+"); break;
3176 case 4:
3177 case 6: STRCAT(buf, " -"); break;
3178 case 5:
3179 case 7: STRCAT(buf, " -+"); break;
3180 }
3181
3182 if (curbuf->b_fname != NULL)
3183 {
3184 /* Get path of file, replace home dir with ~ */
3185 off = (int)STRLEN(buf);
3186 buf[off++] = ' ';
3187 buf[off++] = '(';
3188 home_replace(curbuf, curbuf->b_ffname,
3189 buf + off, IOSIZE - off, TRUE);
3190#ifdef BACKSLASH_IN_FILENAME
3191 /* avoid "c:/name" to be reduced to "c" */
3192 if (isalpha(buf[off]) && buf[off + 1] == ':')
3193 off += 2;
3194#endif
3195 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003196 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003199 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003200 (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003203
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 /* translate unprintable chars */
3205 p = transstr(buf + off);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003206 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 STRCAT(buf, ")");
3209 }
3210
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003211 append_arg_number(curwin, buf, IOSIZE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
3213#if defined(FEAT_CLIENTSERVER)
3214 if (serverName != NULL)
3215 {
3216 STRCAT(buf, " - ");
3217 STRCAT(buf, serverName);
3218 }
3219 else
3220#endif
3221 STRCAT(buf, " - VIM");
3222
3223 if (maxlen > 0)
3224 {
3225 /* make it shorter by removing a bit in the middle */
3226 len = vim_strsize(buf);
3227 if (len > maxlen)
3228 trunc_string(buf, buf, maxlen);
3229 }
3230 }
3231 }
3232 mustset = ti_change(t_str, &lasttitle);
3233
3234 if (p_icon)
3235 {
3236 i_str = buf;
3237 if (*p_iconstring != NUL)
3238 {
3239#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003240 if (stl_syntax & STL_IN_ICON)
3241 {
3242 int use_sandbox = FALSE;
3243 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003244
3245# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003246 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003247# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003248 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003250 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003251 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003252 if (called_emsg)
3253 set_string_option_direct((char_u *)"iconstring", -1,
3254 (char_u *)"", OPT_FREE, SID_ERROR);
3255 called_emsg |= save_called_emsg;
3256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 else
3258#endif
3259 i_str = p_iconstring;
3260 }
3261 else
3262 {
3263 if (buf_spname(curbuf) != NULL)
3264 i_name = (char_u *)buf_spname(curbuf);
3265 else /* use file name only in icon */
3266 i_name = gettail(curbuf->b_ffname);
3267 *i_str = NUL;
3268 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003269 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 if (len > 100)
3271 {
3272 len -= 100;
3273#ifdef FEAT_MBYTE
3274 if (has_mbyte)
3275 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3276#endif
3277 i_name += len;
3278 }
3279 STRCPY(i_str, i_name);
3280 trans_characters(i_str, IOSIZE);
3281 }
3282 }
3283
3284 mustset |= ti_change(i_str, &lasticon);
3285
3286 if (mustset)
3287 resettitle();
3288}
3289
3290/*
3291 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3292 * from "str" if it does.
3293 * Return TRUE when "*last" changed.
3294 */
3295 static int
3296ti_change(str, last)
3297 char_u *str;
3298 char_u **last;
3299{
3300 if ((str == NULL) != (*last == NULL)
3301 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3302 {
3303 vim_free(*last);
3304 if (str == NULL)
3305 *last = NULL;
3306 else
3307 *last = vim_strsave(str);
3308 return TRUE;
3309 }
3310 return FALSE;
3311}
3312
3313/*
3314 * Put current window title back (used after calling a shell)
3315 */
3316 void
3317resettitle()
3318{
3319 mch_settitle(lasttitle, lasticon);
3320}
Bram Moolenaarea408852005-06-25 22:49:46 +00003321
3322# if defined(EXITFREE) || defined(PROTO)
3323 void
3324free_titles()
3325{
3326 vim_free(lasttitle);
3327 vim_free(lasticon);
3328}
3329# endif
3330
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331#endif /* FEAT_TITLE */
3332
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003333#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003335 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 * Return length of string in screen cells.
3337 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003338 * Normally works for window "wp", except when working for 'tabline' then it
3339 * is "curwin".
3340 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 * Items are drawn interspersed with the text that surrounds it
3342 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3343 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3344 *
3345 * If maxwidth is not zero, the string will be filled at any middle marker
3346 * or truncated if too long, fillchar is used for all whitespace.
3347 */
3348 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003349build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003351 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 size_t outlen; /* length of out[] */
3353 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003354 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 int fillchar;
3356 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003357 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3358 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360 char_u *p;
3361 char_u *s;
3362 char_u *t;
3363 char_u *linecont;
3364#ifdef FEAT_EVAL
3365 win_T *o_curwin;
3366 buf_T *o_curbuf;
3367#endif
3368 int empty_line;
3369 colnr_T virtcol;
3370 long l;
3371 long n;
3372 int prevchar_isflag;
3373 int prevchar_isitem;
3374 int itemisflag;
3375 int fillable;
3376 char_u *str;
3377 long num;
3378 int width;
3379 int itemcnt;
3380 int curitem;
3381 int groupitem[STL_MAX_ITEM];
3382 int groupdepth;
3383 struct stl_item
3384 {
3385 char_u *start;
3386 int minwid;
3387 int maxwid;
3388 enum
3389 {
3390 Normal,
3391 Empty,
3392 Group,
3393 Middle,
3394 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003395 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 Trunc
3397 } type;
3398 } item[STL_MAX_ITEM];
3399 int minwid;
3400 int maxwid;
3401 int zeropad;
3402 char_u base;
3403 char_u opt;
3404#define TMPLEN 70
3405 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003406 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003407 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003408
3409#ifdef FEAT_EVAL
3410 /*
3411 * When the format starts with "%!" then evaluate it as an expression and
3412 * use the result as the actual format string.
3413 */
3414 if (fmt[0] == '%' && fmt[1] == '!')
3415 {
3416 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3417 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003418 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003419 }
3420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
3422 if (fillchar == 0)
3423 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003424#ifdef FEAT_MBYTE
3425 /* Can't handle a multi-byte fill character yet. */
3426 else if (mb_char2len(fillchar) > 1)
3427 fillchar = '-';
3428#endif
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 /*
3431 * Get line & check if empty (cursorpos will show "0-1").
3432 * If inversion is possible we use it. Else '=' characters are used.
3433 */
3434 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3435 empty_line = (*linecont == NUL);
3436
3437 groupdepth = 0;
3438 p = out;
3439 curitem = 0;
3440 prevchar_isflag = TRUE;
3441 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003442 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 {
3444 if (*s != NUL && *s != '%')
3445 prevchar_isflag = prevchar_isitem = FALSE;
3446
3447 /*
3448 * Handle up to the next '%' or the end.
3449 */
3450 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3451 *p++ = *s++;
3452 if (*s == NUL || p + 1 >= out + outlen)
3453 break;
3454
3455 /*
3456 * Handle one '%' item.
3457 */
3458 s++;
3459 if (*s == '%')
3460 {
3461 if (p + 1 >= out + outlen)
3462 break;
3463 *p++ = *s++;
3464 prevchar_isflag = prevchar_isitem = FALSE;
3465 continue;
3466 }
3467 if (*s == STL_MIDDLEMARK)
3468 {
3469 s++;
3470 if (groupdepth > 0)
3471 continue;
3472 item[curitem].type = Middle;
3473 item[curitem++].start = p;
3474 continue;
3475 }
3476 if (*s == STL_TRUNCMARK)
3477 {
3478 s++;
3479 item[curitem].type = Trunc;
3480 item[curitem++].start = p;
3481 continue;
3482 }
3483 if (*s == ')')
3484 {
3485 s++;
3486 if (groupdepth < 1)
3487 continue;
3488 groupdepth--;
3489
3490 t = item[groupitem[groupdepth]].start;
3491 *p = NUL;
3492 l = vim_strsize(t);
3493 if (curitem > groupitem[groupdepth] + 1
3494 && item[groupitem[groupdepth]].minwid == 0)
3495 {
3496 /* remove group if all items are empty */
3497 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3498 if (item[n].type == Normal)
3499 break;
3500 if (n == curitem)
3501 {
3502 p = t;
3503 l = 0;
3504 }
3505 }
3506 if (l > item[groupitem[groupdepth]].maxwid)
3507 {
3508 /* truncate, remove n bytes of text at the start */
3509#ifdef FEAT_MBYTE
3510 if (has_mbyte)
3511 {
3512 /* Find the first character that should be included. */
3513 n = 0;
3514 while (l >= item[groupitem[groupdepth]].maxwid)
3515 {
3516 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003517 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519 }
3520 else
3521#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003522 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
3524 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003525 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 p = p - n + 1;
3527#ifdef FEAT_MBYTE
3528 /* Fill up space left over by half a double-wide char. */
3529 while (++l < item[groupitem[groupdepth]].minwid)
3530 *p++ = fillchar;
3531#endif
3532
3533 /* correct the start of the items for the truncation */
3534 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3535 {
3536 item[l].start -= n;
3537 if (item[l].start < t)
3538 item[l].start = t;
3539 }
3540 }
3541 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3542 {
3543 /* fill */
3544 n = item[groupitem[groupdepth]].minwid;
3545 if (n < 0)
3546 {
3547 /* fill by appending characters */
3548 n = 0 - n;
3549 while (l++ < n && p + 1 < out + outlen)
3550 *p++ = fillchar;
3551 }
3552 else
3553 {
3554 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003555 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 l = n - l;
3557 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003558 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 p += l;
3560 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3561 item[n].start += l;
3562 for ( ; l > 0; l--)
3563 *t++ = fillchar;
3564 }
3565 }
3566 continue;
3567 }
3568 minwid = 0;
3569 maxwid = 9999;
3570 zeropad = FALSE;
3571 l = 1;
3572 if (*s == '0')
3573 {
3574 s++;
3575 zeropad = TRUE;
3576 }
3577 if (*s == '-')
3578 {
3579 s++;
3580 l = -1;
3581 }
3582 if (VIM_ISDIGIT(*s))
3583 {
3584 minwid = (int)getdigits(&s);
3585 if (minwid < 0) /* overflow */
3586 minwid = 0;
3587 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003588 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
3590 item[curitem].type = Highlight;
3591 item[curitem].start = p;
3592 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3593 s++;
3594 curitem++;
3595 continue;
3596 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003597 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3598 {
3599 if (*s == STL_TABCLOSENR)
3600 {
3601 if (minwid == 0)
3602 {
3603 /* %X ends the close label, go back to the previously
3604 * define tab label nr. */
3605 for (n = curitem - 1; n >= 0; --n)
3606 if (item[n].type == TabPage && item[n].minwid >= 0)
3607 {
3608 minwid = item[n].minwid;
3609 break;
3610 }
3611 }
3612 else
3613 /* close nrs are stored as negative values */
3614 minwid = - minwid;
3615 }
3616 item[curitem].type = TabPage;
3617 item[curitem].start = p;
3618 item[curitem].minwid = minwid;
3619 s++;
3620 curitem++;
3621 continue;
3622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 if (*s == '.')
3624 {
3625 s++;
3626 if (VIM_ISDIGIT(*s))
3627 {
3628 maxwid = (int)getdigits(&s);
3629 if (maxwid <= 0) /* overflow */
3630 maxwid = 50;
3631 }
3632 }
3633 minwid = (minwid > 50 ? 50 : minwid) * l;
3634 if (*s == '(')
3635 {
3636 groupitem[groupdepth++] = curitem;
3637 item[curitem].type = Group;
3638 item[curitem].start = p;
3639 item[curitem].minwid = minwid;
3640 item[curitem].maxwid = maxwid;
3641 s++;
3642 curitem++;
3643 continue;
3644 }
3645 if (vim_strchr(STL_ALL, *s) == NULL)
3646 {
3647 s++;
3648 continue;
3649 }
3650 opt = *s++;
3651
3652 /* OK - now for the real work */
3653 base = 'D';
3654 itemisflag = FALSE;
3655 fillable = TRUE;
3656 num = -1;
3657 str = NULL;
3658 switch (opt)
3659 {
3660 case STL_FILEPATH:
3661 case STL_FULLPATH:
3662 case STL_FILENAME:
3663 fillable = FALSE; /* don't change ' ' to fillchar */
3664 if (buf_spname(wp->w_buffer) != NULL)
3665 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3666 else
3667 {
3668 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003669 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3671 }
3672 trans_characters(NameBuff, MAXPATHL);
3673 if (opt != STL_FILENAME)
3674 str = NameBuff;
3675 else
3676 str = gettail(NameBuff);
3677 break;
3678
3679 case STL_VIM_EXPR: /* '{' */
3680 itemisflag = TRUE;
3681 t = p;
3682 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3683 *p++ = *s++;
3684 if (*s != '}') /* missing '}' or out of space */
3685 break;
3686 s++;
3687 *p = 0;
3688 p = t;
3689
3690#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003691 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3693
3694 o_curbuf = curbuf;
3695 o_curwin = curwin;
3696 curwin = wp;
3697 curbuf = wp->w_buffer;
3698
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003699 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
3701 curwin = o_curwin;
3702 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003703 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705 if (str != NULL && *str != 0)
3706 {
3707 if (*skipdigits(str) == NUL)
3708 {
3709 num = atoi((char *)str);
3710 vim_free(str);
3711 str = NULL;
3712 itemisflag = FALSE;
3713 }
3714 }
3715#endif
3716 break;
3717
3718 case STL_LINE:
3719 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3720 ? 0L : (long)(wp->w_cursor.lnum);
3721 break;
3722
3723 case STL_NUMLINES:
3724 num = wp->w_buffer->b_ml.ml_line_count;
3725 break;
3726
3727 case STL_COLUMN:
3728 num = !(State & INSERT) && empty_line
3729 ? 0 : (int)wp->w_cursor.col + 1;
3730 break;
3731
3732 case STL_VIRTCOL:
3733 case STL_VIRTCOL_ALT:
3734 /* In list mode virtcol needs to be recomputed */
3735 virtcol = wp->w_virtcol;
3736 if (wp->w_p_list && lcs_tab1 == NUL)
3737 {
3738 wp->w_p_list = FALSE;
3739 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3740 wp->w_p_list = TRUE;
3741 }
3742 ++virtcol;
3743 /* Don't display %V if it's the same as %c. */
3744 if (opt == STL_VIRTCOL_ALT
3745 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3746 ? 0 : (int)wp->w_cursor.col + 1)))
3747 break;
3748 num = (long)virtcol;
3749 break;
3750
3751 case STL_PERCENTAGE:
3752 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3753 (long)wp->w_buffer->b_ml.ml_line_count);
3754 break;
3755
3756 case STL_ALTPERCENT:
3757 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003758 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 break;
3760
3761 case STL_ARGLISTSTAT:
3762 fillable = FALSE;
3763 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003764 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 str = tmp;
3766 break;
3767
3768 case STL_KEYMAP:
3769 fillable = FALSE;
3770 if (get_keymap_str(wp, tmp, TMPLEN))
3771 str = tmp;
3772 break;
3773 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003774#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003775 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776#else
3777 num = 0;
3778#endif
3779 break;
3780
3781 case STL_BUFNO:
3782 num = wp->w_buffer->b_fnum;
3783 break;
3784
3785 case STL_OFFSET_X:
3786 base = 'X';
3787 case STL_OFFSET:
3788#ifdef FEAT_BYTEOFF
3789 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3790 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3791 0L : l + 1 + (!(State & INSERT) && empty_line ?
3792 0 : (int)wp->w_cursor.col);
3793#endif
3794 break;
3795
3796 case STL_BYTEVAL_X:
3797 base = 'X';
3798 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003799 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 num = 0;
3801 else
3802 {
3803#ifdef FEAT_MBYTE
3804 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3805#else
3806 num = linecont[wp->w_cursor.col];
3807#endif
3808 }
3809 if (num == NL)
3810 num = 0;
3811 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3812 num = NL;
3813 break;
3814
3815 case STL_ROFLAG:
3816 case STL_ROFLAG_ALT:
3817 itemisflag = TRUE;
3818 if (wp->w_buffer->b_p_ro)
3819 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3820 break;
3821
3822 case STL_HELPFLAG:
3823 case STL_HELPFLAG_ALT:
3824 itemisflag = TRUE;
3825 if (wp->w_buffer->b_help)
3826 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003827 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 break;
3829
3830#ifdef FEAT_AUTOCMD
3831 case STL_FILETYPE:
3832 if (*wp->w_buffer->b_p_ft != NUL
3833 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3834 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003835 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3836 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 str = tmp;
3838 }
3839 break;
3840
3841 case STL_FILETYPE_ALT:
3842 itemisflag = TRUE;
3843 if (*wp->w_buffer->b_p_ft != NUL
3844 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3845 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003846 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3847 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 for (t = tmp; *t != 0; t++)
3849 *t = TOUPPER_LOC(*t);
3850 str = tmp;
3851 }
3852 break;
3853#endif
3854
3855#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3856 case STL_PREVIEWFLAG:
3857 case STL_PREVIEWFLAG_ALT:
3858 itemisflag = TRUE;
3859 if (wp->w_p_pvw)
3860 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3861 : _("[Preview]"));
3862 break;
3863#endif
3864
3865 case STL_MODIFIED:
3866 case STL_MODIFIED_ALT:
3867 itemisflag = TRUE;
3868 switch ((opt == STL_MODIFIED_ALT)
3869 + bufIsChanged(wp->w_buffer) * 2
3870 + (!wp->w_buffer->b_p_ma) * 4)
3871 {
3872 case 2: str = (char_u *)"[+]"; break;
3873 case 3: str = (char_u *)",+"; break;
3874 case 4: str = (char_u *)"[-]"; break;
3875 case 5: str = (char_u *)",-"; break;
3876 case 6: str = (char_u *)"[+-]"; break;
3877 case 7: str = (char_u *)",+-"; break;
3878 }
3879 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003880
3881 case STL_HIGHLIGHT:
3882 t = s;
3883 while (*s != '#' && *s != NUL)
3884 ++s;
3885 if (*s == '#')
3886 {
3887 item[curitem].type = Highlight;
3888 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003889 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003890 curitem++;
3891 }
3892 ++s;
3893 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
3895
3896 item[curitem].start = p;
3897 item[curitem].type = Normal;
3898 if (str != NULL && *str)
3899 {
3900 t = str;
3901 if (itemisflag)
3902 {
3903 if ((t[0] && t[1])
3904 && ((!prevchar_isitem && *t == ',')
3905 || (prevchar_isflag && *t == ' ')))
3906 t++;
3907 prevchar_isflag = TRUE;
3908 }
3909 l = vim_strsize(t);
3910 if (l > 0)
3911 prevchar_isitem = TRUE;
3912 if (l > maxwid)
3913 {
3914 while (l >= maxwid)
3915#ifdef FEAT_MBYTE
3916 if (has_mbyte)
3917 {
3918 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003919 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921 else
3922#endif
3923 l -= byte2cells(*t++);
3924 if (p + 1 >= out + outlen)
3925 break;
3926 *p++ = '<';
3927 }
3928 if (minwid > 0)
3929 {
3930 for (; l < minwid && p + 1 < out + outlen; l++)
3931 {
3932 /* Don't put a "-" in front of a digit. */
3933 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3934 *p++ = ' ';
3935 else
3936 *p++ = fillchar;
3937 }
3938 minwid = 0;
3939 }
3940 else
3941 minwid *= -1;
3942 while (*t && p + 1 < out + outlen)
3943 {
3944 *p++ = *t++;
3945 /* Change a space by fillchar, unless fillchar is '-' and a
3946 * digit follows. */
3947 if (fillable && p[-1] == ' '
3948 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3949 p[-1] = fillchar;
3950 }
3951 for (; l < minwid && p + 1 < out + outlen; l++)
3952 *p++ = fillchar;
3953 }
3954 else if (num >= 0)
3955 {
3956 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3957 char_u nstr[20];
3958
3959 if (p + 20 >= out + outlen)
3960 break; /* not sufficient space */
3961 prevchar_isitem = TRUE;
3962 t = nstr;
3963 if (opt == STL_VIRTCOL_ALT)
3964 {
3965 *t++ = '-';
3966 minwid--;
3967 }
3968 *t++ = '%';
3969 if (zeropad)
3970 *t++ = '0';
3971 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003972 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 *t = 0;
3974
3975 for (n = num, l = 1; n >= nbase; n /= nbase)
3976 l++;
3977 if (opt == STL_VIRTCOL_ALT)
3978 l++;
3979 if (l > maxwid)
3980 {
3981 l += 2;
3982 n = l - maxwid;
3983 while (l-- > maxwid)
3984 num /= nbase;
3985 *t++ = '>';
3986 *t++ = '%';
3987 *t = t[-3];
3988 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003989 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3990 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 }
3992 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003993 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
3994 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 p += STRLEN(p);
3996 }
3997 else
3998 item[curitem].type = Empty;
3999
4000 if (opt == STL_VIM_EXPR)
4001 vim_free(str);
4002
4003 if (num >= 0 || (!itemisflag && str && *str))
4004 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4005 curitem++;
4006 }
4007 *p = NUL;
4008 itemcnt = curitem;
4009
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004010#ifdef FEAT_EVAL
4011 if (usefmt != fmt)
4012 vim_free(usefmt);
4013#endif
4014
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 width = vim_strsize(out);
4016 if (maxwidth > 0 && width > maxwidth)
4017 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004018 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 l = 0;
4020 if (itemcnt == 0)
4021 s = out;
4022 else
4023 {
4024 for ( ; l < itemcnt; l++)
4025 if (item[l].type == Trunc)
4026 {
4027 /* Truncate at %< item. */
4028 s = item[l].start;
4029 break;
4030 }
4031 if (l == itemcnt)
4032 {
4033 /* No %< item, truncate first item. */
4034 s = item[0].start;
4035 l = 0;
4036 }
4037 }
4038
4039 if (width - vim_strsize(s) >= maxwidth)
4040 {
4041 /* Truncation mark is beyond max length */
4042#ifdef FEAT_MBYTE
4043 if (has_mbyte)
4044 {
4045 s = out;
4046 width = 0;
4047 for (;;)
4048 {
4049 width += ptr2cells(s);
4050 if (width >= maxwidth)
4051 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004052 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 }
4054 /* Fill up for half a double-wide character. */
4055 while (++width < maxwidth)
4056 *s++ = fillchar;
4057 }
4058 else
4059#endif
4060 s = out + maxwidth - 1;
4061 for (l = 0; l < itemcnt; l++)
4062 if (item[l].start > s)
4063 break;
4064 itemcnt = l;
4065 *s++ = '>';
4066 *s = 0;
4067 }
4068 else
4069 {
4070#ifdef FEAT_MBYTE
4071 if (has_mbyte)
4072 {
4073 n = 0;
4074 while (width >= maxwidth)
4075 {
4076 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004077 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079 }
4080 else
4081#endif
4082 n = width - maxwidth + 1;
4083 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004084 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 *s = '<';
4086
4087 /* Fill up for half a double-wide character. */
4088 while (++width < maxwidth)
4089 {
4090 s = s + STRLEN(s);
4091 *s++ = fillchar;
4092 *s = NUL;
4093 }
4094
4095 --n; /* count the '<' */
4096 for (; l < itemcnt; l++)
4097 {
4098 if (item[l].start - n >= s)
4099 item[l].start -= n;
4100 else
4101 item[l].start = s;
4102 }
4103 }
4104 width = maxwidth;
4105 }
4106 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4107 {
4108 /* Apply STL_MIDDLE if any */
4109 for (l = 0; l < itemcnt; l++)
4110 if (item[l].type == Middle)
4111 break;
4112 if (l < itemcnt)
4113 {
4114 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004115 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 for (s = item[l].start; s < p; s++)
4117 *s = fillchar;
4118 for (l++; l < itemcnt; l++)
4119 item[l].start += maxwidth - width;
4120 width = maxwidth;
4121 }
4122 }
4123
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004124 /* Store the info about highlighting. */
4125 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004127 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 for (l = 0; l < itemcnt; l++)
4129 {
4130 if (item[l].type == Highlight)
4131 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004132 sp->start = item[l].start;
4133 sp->userhl = item[l].minwid;
4134 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
4136 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004137 sp->start = NULL;
4138 sp->userhl = 0;
4139 }
4140
4141 /* Store the info about tab pages labels. */
4142 if (tabtab != NULL)
4143 {
4144 sp = tabtab;
4145 for (l = 0; l < itemcnt; l++)
4146 {
4147 if (item[l].type == TabPage)
4148 {
4149 sp->start = item[l].start;
4150 sp->userhl = item[l].minwid;
4151 sp++;
4152 }
4153 }
4154 sp->start = NULL;
4155 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 }
4157
4158 return width;
4159}
4160#endif /* FEAT_STL_OPT */
4161
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004162#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4163 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004165 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4166 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 */
4168 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004169get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004171 char_u *buf;
4172 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173{
4174 long above; /* number of lines above window */
4175 long below; /* number of lines below window */
4176
4177 above = wp->w_topline - 1;
4178#ifdef FEAT_DIFF
4179 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4180#endif
4181 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4182 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004183 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4184 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004186 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004188 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 ? (int)(above / ((above + below) / 100L))
4190 : (int)(above * 100L / (above + below)));
4191}
4192#endif
4193
4194/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004195 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 * Return TRUE if it was appended.
4197 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004198 static int
4199append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 win_T *wp;
4201 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004202 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204{
4205 char_u *p;
4206
4207 if (ARGCOUNT <= 1) /* nothing to do */
4208 return FALSE;
4209
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004210 p = buf + STRLEN(buf); /* go to the end of the buffer */
4211 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FALSE;
4213 *p++ = ' ';
4214 *p++ = '(';
4215 if (add_file)
4216 {
4217 STRCPY(p, "file ");
4218 p += 5;
4219 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004220 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4221 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4223 return TRUE;
4224}
4225
4226/*
4227 * If fname is not a full path, make it a full path.
4228 * Returns pointer to allocated memory (NULL for failure).
4229 */
4230 char_u *
4231fix_fname(fname)
4232 char_u *fname;
4233{
4234 /*
4235 * Force expanding the path always for Unix, because symbolic links may
4236 * mess up the full path name, even though it starts with a '/'.
4237 * Also expand when there is ".." in the file name, try to remove it,
4238 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004239 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 * For MS-Windows also expand names like "longna~1" to "longname".
4241 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004242#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 return FullName_save(fname, TRUE);
4244#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004245 if (!vim_isAbsName(fname)
4246 || strstr((char *)fname, "..") != NULL
4247 || strstr((char *)fname, "//") != NULL
4248# ifdef BACKSLASH_IN_FILENAME
4249 || strstr((char *)fname, "\\\\") != NULL
4250# endif
4251# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 )
4255 return FullName_save(fname, FALSE);
4256
4257 fname = vim_strsave(fname);
4258
Bram Moolenaar9b942202007-10-03 12:31:33 +00004259# ifdef USE_FNAME_CASE
4260# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004262# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 {
4264 if (fname != NULL)
4265 fname_case(fname, 0); /* set correct case for file name */
4266 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004267# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268
4269 return fname;
4270#endif
4271}
4272
4273/*
4274 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4275 * "ffname" becomes a pointer to allocated memory (or NULL).
4276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 void
4278fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004279 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 char_u **ffname;
4281 char_u **sfname;
4282{
4283 if (*ffname == NULL) /* if no file name given, nothing to do */
4284 return;
4285 if (*sfname == NULL) /* if no short file name given, use ffname */
4286 *sfname = *ffname;
4287 *ffname = fix_fname(*ffname); /* expand to full path */
4288
4289#ifdef FEAT_SHORTCUT
4290 if (!buf->b_p_bin)
4291 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004292 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293
4294 /* If the file name is a shortcut file, use the file it links to. */
4295 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004296 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 {
4298 vim_free(*ffname);
4299 *ffname = rfname;
4300 *sfname = rfname;
4301 }
4302 }
4303#endif
4304}
4305
4306/*
4307 * Get the file name for an argument list entry.
4308 */
4309 char_u *
4310alist_name(aep)
4311 aentry_T *aep;
4312{
4313 buf_T *bp;
4314
4315 /* Use the name from the associated buffer if it exists. */
4316 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004317 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 return aep->ae_fname;
4319 return bp->b_fname;
4320}
4321
4322#if defined(FEAT_WINDOWS) || defined(PROTO)
4323/*
4324 * do_arg_all(): Open up to 'count' windows, one for each argument.
4325 */
4326 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004327do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 int count;
4329 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004330 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331{
4332 int i;
4333 win_T *wp, *wpnext;
4334 char_u *opened; /* array of flags for which args are open */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004335 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 int use_firstwin = FALSE; /* use first window for arglist */
4337 int split_ret = OK;
4338 int p_ea_save;
4339 alist_T *alist; /* argument list to be used */
4340 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004341 tabpage_T *tpnext;
4342 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004343 win_T *new_curwin = NULL;
4344 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
4346 if (ARGCOUNT <= 0)
4347 {
4348 /* Don't give an error message. We don't want it when the ":all"
4349 * command is in the .vimrc. */
4350 return;
4351 }
4352 setpcmark();
4353
4354 opened_len = ARGCOUNT;
4355 opened = alloc_clear((unsigned)opened_len);
4356 if (opened == NULL)
4357 return;
4358
4359#ifdef FEAT_GUI
4360 need_mouse_correct = TRUE;
4361#endif
4362
4363 /*
4364 * Try closing all windows that are not in the argument list.
4365 * Also close windows that are not full width;
4366 * When 'hidden' or "forceit" set the buffer becomes hidden.
4367 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004368 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004370 if (had_tab > 0)
4371 goto_tabpage_tp(first_tabpage);
4372 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004374 tpnext = curtab->tp_next;
4375 for (wp = firstwin; wp != NULL; wp = wpnext)
4376 {
4377 wpnext = wp->w_next;
4378 buf = wp->w_buffer;
4379 if (buf->b_ffname == NULL
4380 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004382 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004384 )
4385 i = ARGCOUNT;
4386 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004388 /* check if the buffer in this window is in the arglist */
4389 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004391 if (ARGLIST[i].ae_fnum == buf->b_fnum
4392 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004393 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004395 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004396 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004397 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004398 if (i == 0)
4399 {
4400 new_curwin = wp;
4401 new_curtab = curtab;
4402 }
4403 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004404 if (wp->w_alist != curwin->w_alist)
4405 {
4406 /* Use the current argument list for all windows
4407 * containing a file from it. */
4408 alist_unlink(wp->w_alist);
4409 wp->w_alist = curwin->w_alist;
4410 ++wp->w_alist->al_refcount;
4411 }
4412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004416 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004418 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004420 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004421 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004423 /* If the buffer was changed, and we would like to hide it,
4424 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004425 if (!P_HID(buf) && buf->b_nwindows <= 1
4426 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004428 (void)autowrite(buf, FALSE);
4429#ifdef FEAT_AUTOCMD
4430 /* check if autocommands removed the window */
4431 if (!win_valid(wp) || !buf_valid(buf))
4432 {
4433 wpnext = firstwin; /* start all over... */
4434 continue;
4435 }
4436#endif
4437 }
4438#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004439 /* don't close last window */
4440 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004441#endif
4442 use_firstwin = TRUE;
4443#ifdef FEAT_WINDOWS
4444 else
4445 {
4446 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4447# ifdef FEAT_AUTOCMD
4448 /* check if autocommands removed the next window */
4449 if (!win_valid(wpnext))
4450 wpnext = firstwin; /* start all over... */
4451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
4453#endif
4454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 }
4456 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004457
4458 /* Without the ":tab" modifier only do the current tab page. */
4459 if (had_tab == 0 || tpnext == NULL)
4460 break;
4461
4462# ifdef FEAT_AUTOCMD
4463 /* check if autocommands removed the next tab page */
4464 if (!valid_tabpage(tpnext))
4465 tpnext = first_tabpage; /* start all over...*/
4466# endif
4467 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469
4470 /*
4471 * Open a window for files in the argument list that don't have one.
4472 * ARGCOUNT may change while doing this, because of autocommands.
4473 */
4474 if (count > ARGCOUNT || count <= 0)
4475 count = ARGCOUNT;
4476
4477 /* Autocommands may do anything to the argument list. Make sure it's not
4478 * freed while we are working here by "locking" it. We still have to
4479 * watch out for its size to be changed. */
4480 alist = curwin->w_alist;
4481 ++alist->al_refcount;
4482
4483#ifdef FEAT_AUTOCMD
4484 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4485 ++autocmd_no_enter;
4486 ++autocmd_no_leave;
4487#endif
4488 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004489#ifdef FEAT_WINDOWS
4490 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4491 * leaving an empty tab page when executed locally. */
4492 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4493 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4494 use_firstwin = TRUE;
4495#endif
4496
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4498 {
4499 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4500 arg_had_last = TRUE;
4501 if (i < opened_len && opened[i])
4502 {
4503 /* Move the already present window to below the current window */
4504 if (curwin->w_arg_idx != i)
4505 {
4506 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4507 {
4508 if (wpnext->w_arg_idx == i)
4509 {
4510 win_move_after(wpnext, curwin);
4511 break;
4512 }
4513 }
4514 }
4515 }
4516 else if (split_ret == OK)
4517 {
4518 if (!use_firstwin) /* split current window */
4519 {
4520 p_ea_save = p_ea;
4521 p_ea = TRUE; /* use space from all windows */
4522 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4523 p_ea = p_ea_save;
4524 if (split_ret == FAIL)
4525 continue;
4526 }
4527#ifdef FEAT_AUTOCMD
4528 else /* first window: do autocmd for leaving this buffer */
4529 --autocmd_no_leave;
4530#endif
4531
4532 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004533 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 */
4535 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004536 if (i == 0)
4537 {
4538 new_curwin = curwin;
4539 new_curtab = curtab;
4540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4542 ECMD_ONE,
4543 ((P_HID(curwin->w_buffer)
4544 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004545 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546#ifdef FEAT_AUTOCMD
4547 if (use_firstwin)
4548 ++autocmd_no_leave;
4549#endif
4550 use_firstwin = FALSE;
4551 }
4552 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004553
4554 /* When ":tab" was used open a new tab for a new window repeatedly. */
4555 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4556 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
4558
4559 /* Remove the "lock" on the argument list. */
4560 alist_unlink(alist);
4561
4562#ifdef FEAT_AUTOCMD
4563 --autocmd_no_enter;
4564#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004565 /* to window with first arg */
4566 if (valid_tabpage(new_curtab))
4567 goto_tabpage_tp(new_curtab);
4568 if (win_valid(new_curwin))
4569 win_enter(new_curwin, FALSE);
4570
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571#ifdef FEAT_AUTOCMD
4572 --autocmd_no_leave;
4573#endif
4574 vim_free(opened);
4575}
4576
4577# if defined(FEAT_LISTCMDS) || defined(PROTO)
4578/*
4579 * Open a window for a number of buffers.
4580 */
4581 void
4582ex_buffer_all(eap)
4583 exarg_T *eap;
4584{
4585 buf_T *buf;
4586 win_T *wp, *wpnext;
4587 int split_ret = OK;
4588 int p_ea_save;
4589 int open_wins = 0;
4590 int r;
4591 int count; /* Maximum number of windows to open. */
4592 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004593#ifdef FEAT_WINDOWS
4594 int had_tab = cmdmod.tab;
4595 tabpage_T *tpnext;
4596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
4598 if (eap->addr_count == 0) /* make as many windows as possible */
4599 count = 9999;
4600 else
4601 count = eap->line2; /* make as many windows as specified */
4602 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4603 all = FALSE;
4604 else
4605 all = TRUE;
4606
4607 setpcmark();
4608
4609#ifdef FEAT_GUI
4610 need_mouse_correct = TRUE;
4611#endif
4612
4613 /*
4614 * Close superfluous windows (two windows for the same buffer).
4615 * Also close windows that are not full-width.
4616 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004617#ifdef FEAT_WINDOWS
4618 if (had_tab > 0)
4619 goto_tabpage_tp(first_tabpage);
4620 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004623 tpnext = curtab->tp_next;
4624 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004626 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004627 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004628#ifdef FEAT_VERTSPLIT
4629 || ((cmdmod.split & WSP_VERT)
4630 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004631 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004632 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004634#ifdef FEAT_WINDOWS
4635 || (had_tab > 0 && wp != firstwin)
4636#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004637 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004638 {
4639 win_close(wp, FALSE);
4640#ifdef FEAT_AUTOCMD
4641 wpnext = firstwin; /* just in case an autocommand does
4642 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004643 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004644 open_wins = 0;
4645#endif
4646 }
4647 else
4648 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004650
4651#ifdef FEAT_WINDOWS
4652 /* Without the ":tab" modifier only do the current tab page. */
4653 if (had_tab == 0 || tpnext == NULL)
4654 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004655 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
4659 /*
4660 * Go through the buffer list. When a buffer doesn't have a window yet,
4661 * open one. Otherwise move the window to the right position.
4662 * Watch out for autocommands that delete buffers or windows!
4663 */
4664#ifdef FEAT_AUTOCMD
4665 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4666 ++autocmd_no_enter;
4667#endif
4668 win_enter(lastwin, FALSE);
4669#ifdef FEAT_AUTOCMD
4670 ++autocmd_no_leave;
4671#endif
4672 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4673 {
4674 /* Check if this buffer needs a window */
4675 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4676 continue;
4677
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004678#ifdef FEAT_WINDOWS
4679 if (had_tab != 0)
4680 {
4681 /* With the ":tab" modifier don't move the window. */
4682 if (buf->b_nwindows > 0)
4683 wp = lastwin; /* buffer has a window, skip it */
4684 else
4685 wp = NULL;
4686 }
4687 else
4688#endif
4689 {
4690 /* Check if this buffer already has a window */
4691 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4692 if (wp->w_buffer == buf)
4693 break;
4694 /* If the buffer already has a window, move it */
4695 if (wp != NULL)
4696 win_move_after(wp, curwin);
4697 }
4698
4699 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
4701 /* Split the window and put the buffer in it */
4702 p_ea_save = p_ea;
4703 p_ea = TRUE; /* use space from all windows */
4704 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4705 ++open_wins;
4706 p_ea = p_ea_save;
4707 if (split_ret == FAIL)
4708 continue;
4709
4710 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004711#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 swap_exists_action = SEA_DIALOG;
4713#endif
4714 set_curbuf(buf, DOBUF_GOTO);
4715#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004718#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 break;
4722 }
4723#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004724#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 if (swap_exists_action == SEA_QUIT)
4726 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004727# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4728 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004729
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004730 /* Reset the error/interrupt/exception state here so that
4731 * aborting() returns FALSE when closing a window. */
4732 enter_cleanup(&cs);
4733# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004734
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004735 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 win_close(curwin, TRUE);
4737 --open_wins;
4738 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004739 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004740
4741# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4742 /* Restore the error/interrupt/exception state if not
4743 * discarded by a new aborting error, interrupt, or uncaught
4744 * exception. */
4745 leave_cleanup(&cs);
4746# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 }
4748 else
4749 handle_swap_exists(NULL);
4750#endif
4751 }
4752
4753 ui_breakcheck();
4754 if (got_int)
4755 {
4756 (void)vgetc(); /* only break the file loading, not the rest */
4757 break;
4758 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004759#ifdef FEAT_EVAL
4760 /* Autocommands deleted the buffer or aborted script processing!!! */
4761 if (aborting())
4762 break;
4763#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004764#ifdef FEAT_WINDOWS
4765 /* When ":tab" was used open a new tab for a new window repeatedly. */
4766 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4767 cmdmod.tab = 9999;
4768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 }
4770#ifdef FEAT_AUTOCMD
4771 --autocmd_no_enter;
4772#endif
4773 win_enter(firstwin, FALSE); /* back to first window */
4774#ifdef FEAT_AUTOCMD
4775 --autocmd_no_leave;
4776#endif
4777
4778 /*
4779 * Close superfluous windows.
4780 */
4781 for (wp = lastwin; open_wins > count; )
4782 {
4783 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4784 || autowrite(wp->w_buffer, FALSE) == OK);
4785#ifdef FEAT_AUTOCMD
4786 if (!win_valid(wp))
4787 {
4788 /* BufWrite Autocommands made the window invalid, start over */
4789 wp = lastwin;
4790 }
4791 else
4792#endif
4793 if (r)
4794 {
4795 win_close(wp, !P_HID(wp->w_buffer));
4796 --open_wins;
4797 wp = lastwin;
4798 }
4799 else
4800 {
4801 wp = wp->w_prev;
4802 if (wp == NULL)
4803 break;
4804 }
4805 }
4806}
4807# endif /* FEAT_LISTCMDS */
4808
4809#endif /* FEAT_WINDOWS */
4810
Bram Moolenaara3227e22006-03-08 21:32:40 +00004811static int chk_modeline __ARGS((linenr_T, int));
4812
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813/*
4814 * do_modelines() - process mode lines for the current file
4815 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004816 * "flags" can be:
4817 * OPT_WINONLY only set options local to window
4818 * OPT_NOWIN don't set options local to window
4819 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 * Returns immediately if the "ml" option isn't set.
4821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004823do_modelines(flags)
4824 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004826 linenr_T lnum;
4827 int nmlines;
4828 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829
4830 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4831 return;
4832
4833 /* Disallow recursive entry here. Can happen when executing a modeline
4834 * triggers an autocommand, which reloads modelines with a ":do". */
4835 if (entered)
4836 return;
4837
4838 ++entered;
4839 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4840 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004841 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 nmlines = 0;
4843
4844 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4845 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004846 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 nmlines = 0;
4848 --entered;
4849}
4850
4851#include "version.h" /* for version number */
4852
4853/*
4854 * chk_modeline() - check a single line for a mode string
4855 * Return FAIL if an error encountered.
4856 */
4857 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004858chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004860 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
4862 char_u *s;
4863 char_u *e;
4864 char_u *linecopy; /* local copy of any modeline found */
4865 int prev;
4866 int vers;
4867 int end;
4868 int retval = OK;
4869 char_u *save_sourcing_name;
4870 linenr_T save_sourcing_lnum;
4871#ifdef FEAT_EVAL
4872 scid_T save_SID;
4873#endif
4874
4875 prev = -1;
4876 for (s = ml_get(lnum); *s != NUL; ++s)
4877 {
4878 if (prev == -1 || vim_isspace(prev))
4879 {
4880 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4881 || STRNCMP(s, "vi:", (size_t)3) == 0)
4882 break;
4883 if (STRNCMP(s, "vim", 3) == 0)
4884 {
4885 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4886 e = s + 4;
4887 else
4888 e = s + 3;
4889 vers = getdigits(&e);
4890 if (*e == ':'
4891 && (s[3] == ':'
4892 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4893 || (VIM_VERSION_100 < vers && s[3] == '<')
4894 || (VIM_VERSION_100 > vers && s[3] == '>')
4895 || (VIM_VERSION_100 == vers && s[3] == '=')))
4896 break;
4897 }
4898 }
4899 prev = *s;
4900 }
4901
4902 if (*s)
4903 {
4904 do /* skip over "ex:", "vi:" or "vim:" */
4905 ++s;
4906 while (s[-1] != ':');
4907
4908 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4909 if (linecopy == NULL)
4910 return FAIL;
4911
4912 save_sourcing_lnum = sourcing_lnum;
4913 save_sourcing_name = sourcing_name;
4914 sourcing_lnum = lnum; /* prepare for emsg() */
4915 sourcing_name = (char_u *)"modelines";
4916
4917 end = FALSE;
4918 while (end == FALSE)
4919 {
4920 s = skipwhite(s);
4921 if (*s == NUL)
4922 break;
4923
4924 /*
4925 * Find end of set command: ':' or end of line.
4926 * Skip over "\:", replacing it with ":".
4927 */
4928 for (e = s; *e != ':' && *e != NUL; ++e)
4929 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00004930 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 if (*e == NUL)
4932 end = TRUE;
4933
4934 /*
4935 * If there is a "set" command, require a terminating ':' and
4936 * ignore the stuff after the ':'.
4937 * "vi:set opt opt opt: foo" -- foo not interpreted
4938 * "vi:opt opt opt: foo" -- foo interpreted
4939 * Accept "se" for compatibility with Elvis.
4940 */
4941 if (STRNCMP(s, "set ", (size_t)4) == 0
4942 || STRNCMP(s, "se ", (size_t)3) == 0)
4943 {
4944 if (*e != ':') /* no terminating ':'? */
4945 break;
4946 end = TRUE;
4947 s = vim_strchr(s, ' ') + 1;
4948 }
4949 *e = NUL; /* truncate the set command */
4950
4951 if (*s != NUL) /* skip over an empty "::" */
4952 {
4953#ifdef FEAT_EVAL
4954 save_SID = current_SID;
4955 current_SID = SID_MODELINE;
4956#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00004957 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958#ifdef FEAT_EVAL
4959 current_SID = save_SID;
4960#endif
4961 if (retval == FAIL) /* stop if error found */
4962 break;
4963 }
4964 s = e + 1; /* advance to next part */
4965 }
4966
4967 sourcing_lnum = save_sourcing_lnum;
4968 sourcing_name = save_sourcing_name;
4969
4970 vim_free(linecopy);
4971 }
4972 return retval;
4973}
4974
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00004975#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 int
4977read_viminfo_bufferlist(virp, writing)
4978 vir_T *virp;
4979 int writing;
4980{
4981 char_u *tab;
4982 linenr_T lnum;
4983 colnr_T col;
4984 buf_T *buf;
4985 char_u *sfname;
4986 char_u *xline;
4987
4988 /* Handle long line and escaped characters. */
4989 xline = viminfo_readstring(virp, 1, FALSE);
4990
4991 /* don't read in if there are files on the command-line or if writing: */
4992 if (xline != NULL && !writing && ARGCOUNT == 0
4993 && find_viminfo_parameter('%') != NULL)
4994 {
4995 /* Format is: <fname> Tab <lnum> Tab <col>.
4996 * Watch out for a Tab in the file name, work from the end. */
4997 lnum = 0;
4998 col = 0;
4999 tab = vim_strrchr(xline, '\t');
5000 if (tab != NULL)
5001 {
5002 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005003 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 tab = vim_strrchr(xline, '\t');
5005 if (tab != NULL)
5006 {
5007 *tab++ = '\0';
5008 lnum = atol((char *)tab);
5009 }
5010 }
5011
5012 /* Expand "~/" in the file name at "line + 1" to a full path.
5013 * Then try shortening it by comparing with the current directory */
5014 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005015 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016
5017 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5018 if (buf != NULL) /* just in case... */
5019 {
5020 buf->b_last_cursor.lnum = lnum;
5021 buf->b_last_cursor.col = col;
5022 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5023 }
5024 }
5025 vim_free(xline);
5026
5027 return viminfo_readline(virp);
5028}
5029
5030 void
5031write_viminfo_bufferlist(fp)
5032 FILE *fp;
5033{
5034 buf_T *buf;
5035#ifdef FEAT_WINDOWS
5036 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005037 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038#endif
5039 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005040 int max_buffers;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005041 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
5043 if (find_viminfo_parameter('%') == NULL)
5044 return;
5045
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005046 /* Without a number -1 is returned: do all buffers. */
5047 max_buffers = get_viminfo_parameter('%');
5048
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005050#define LINE_BUF_LEN (MAXPATHL + 40)
5051 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (line == NULL)
5053 return;
5054
5055#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005056 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 set_last_cursor(win);
5058#else
5059 set_last_cursor(curwin);
5060#endif
5061
5062 fprintf(fp, _("\n# Buffer list:\n"));
5063 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5064 {
5065 if (buf->b_fname == NULL
5066 || !buf->b_p_bl
5067#ifdef FEAT_QUICKFIX
5068 || bt_quickfix(buf)
5069#endif
5070 || removable(buf->b_ffname))
5071 continue;
5072
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005073 if (max_buffers-- == 0)
5074 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 putc('%', fp);
5076 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005077 len = STRLEN(line);
5078 vim_snprintf((char *)line + len, len - LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 (long)buf->b_last_cursor.lnum,
5080 buf->b_last_cursor.col);
5081 viminfo_writestring(fp, line);
5082 }
5083 vim_free(line);
5084}
5085#endif
5086
5087
5088/*
5089 * Return special buffer name.
5090 * Returns NULL when the buffer has a normal file name.
5091 */
5092 char *
5093buf_spname(buf)
5094 buf_T *buf;
5095{
5096#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5097 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005098 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005099 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005100 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005101
5102 /*
5103 * For location list window, w_llist_ref points to the location list.
5104 * For quickfix window, w_llist_ref is NULL.
5105 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005106 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005107 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005108 goto win_found;
5109win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005110 if (win != NULL && win->w_llist_ref != NULL)
5111 return _("[Location List]");
5112 else
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005113 return _("[Quickfix List]");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115#endif
5116#ifdef FEAT_QUICKFIX
5117 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5118 * contains the name as specified by the user */
5119 if (bt_nofile(buf))
5120 {
5121 if (buf->b_sfname != NULL)
5122 return (char *)buf->b_sfname;
Bram Moolenaarf4f664c2008-12-03 10:21:57 +00005123 return _("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125#endif
5126 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005127 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 return NULL;
5129}
5130
5131
5132#if defined(FEAT_SIGNS) || defined(PROTO)
5133/*
5134 * Insert the sign into the signlist.
5135 */
5136 static void
5137insert_sign(buf, prev, next, id, lnum, typenr)
5138 buf_T *buf; /* buffer to store sign in */
5139 signlist_T *prev; /* previous sign entry */
5140 signlist_T *next; /* next sign entry */
5141 int id; /* sign ID */
5142 linenr_T lnum; /* line number which gets the mark */
5143 int typenr; /* typenr of sign we are adding */
5144{
5145 signlist_T *newsign;
5146
5147 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5148 if (newsign != NULL)
5149 {
5150 newsign->id = id;
5151 newsign->lnum = lnum;
5152 newsign->typenr = typenr;
5153 newsign->next = next;
5154#ifdef FEAT_NETBEANS_INTG
5155 newsign->prev = prev;
5156 if (next != NULL)
5157 next->prev = newsign;
5158#endif
5159
5160 if (prev == NULL)
5161 {
5162 /* When adding first sign need to redraw the windows to create the
5163 * column for signs. */
5164 if (buf->b_signlist == NULL)
5165 {
5166 redraw_buf_later(buf, NOT_VALID);
5167 changed_cline_bef_curs();
5168 }
5169
5170 /* first sign in signlist */
5171 buf->b_signlist = newsign;
5172 }
5173 else
5174 prev->next = newsign;
5175 }
5176}
5177
5178/*
5179 * Add the sign into the signlist. Find the right spot to do it though.
5180 */
5181 void
5182buf_addsign(buf, id, lnum, typenr)
5183 buf_T *buf; /* buffer to store sign in */
5184 int id; /* sign ID */
5185 linenr_T lnum; /* line number which gets the mark */
5186 int typenr; /* typenr of sign we are adding */
5187{
5188 signlist_T *sign; /* a sign in the signlist */
5189 signlist_T *prev; /* the previous sign */
5190
5191 prev = NULL;
5192 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5193 {
5194 if (lnum == sign->lnum && id == sign->id)
5195 {
5196 sign->typenr = typenr;
5197 return;
5198 }
5199 else if (
5200#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5201 id < 0 &&
5202#endif
5203 lnum < sign->lnum)
5204 {
5205#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5206 /* XXX - GRP: Is this because of sign slide problem? Or is it
5207 * really needed? Or is it because we allow multiple signs per
5208 * line? If so, should I add that feature to FEAT_SIGNS?
5209 */
5210 while (prev != NULL && prev->lnum == lnum)
5211 prev = prev->prev;
5212 if (prev == NULL)
5213 sign = buf->b_signlist;
5214 else
5215 sign = prev->next;
5216#endif
5217 insert_sign(buf, prev, sign, id, lnum, typenr);
5218 return;
5219 }
5220 prev = sign;
5221 }
5222#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5223 /* XXX - GRP: See previous comment */
5224 while (prev != NULL && prev->lnum == lnum)
5225 prev = prev->prev;
5226 if (prev == NULL)
5227 sign = buf->b_signlist;
5228 else
5229 sign = prev->next;
5230#endif
5231 insert_sign(buf, prev, sign, id, lnum, typenr);
5232
5233 return;
5234}
5235
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005236 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237buf_change_sign_type(buf, markId, typenr)
5238 buf_T *buf; /* buffer to store sign in */
5239 int markId; /* sign ID */
5240 int typenr; /* typenr of sign we are adding */
5241{
5242 signlist_T *sign; /* a sign in the signlist */
5243
5244 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5245 {
5246 if (sign->id == markId)
5247 {
5248 sign->typenr = typenr;
5249 return sign->lnum;
5250 }
5251 }
5252
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005253 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254}
5255
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005256 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257buf_getsigntype(buf, lnum, type)
5258 buf_T *buf;
5259 linenr_T lnum;
5260 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5261{
5262 signlist_T *sign; /* a sign in a b_signlist */
5263
5264 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5265 if (sign->lnum == lnum
5266 && (type == SIGN_ANY
5267# ifdef FEAT_SIGN_ICONS
5268 || (type == SIGN_ICON
5269 && sign_get_image(sign->typenr) != NULL)
5270# endif
5271 || (type == SIGN_TEXT
5272 && sign_get_text(sign->typenr) != NULL)
5273 || (type == SIGN_LINEHL
5274 && sign_get_attr(sign->typenr, TRUE) != 0)))
5275 return sign->typenr;
5276 return 0;
5277}
5278
5279
5280 linenr_T
5281buf_delsign(buf, id)
5282 buf_T *buf; /* buffer sign is stored in */
5283 int id; /* sign id */
5284{
5285 signlist_T **lastp; /* pointer to pointer to current sign */
5286 signlist_T *sign; /* a sign in a b_signlist */
5287 signlist_T *next; /* the next sign in a b_signlist */
5288 linenr_T lnum; /* line number whose sign was deleted */
5289
5290 lastp = &buf->b_signlist;
5291 lnum = 0;
5292 for (sign = buf->b_signlist; sign != NULL; sign = next)
5293 {
5294 next = sign->next;
5295 if (sign->id == id)
5296 {
5297 *lastp = next;
5298#ifdef FEAT_NETBEANS_INTG
5299 if (next != NULL)
5300 next->prev = sign->prev;
5301#endif
5302 lnum = sign->lnum;
5303 vim_free(sign);
5304 break;
5305 }
5306 else
5307 lastp = &sign->next;
5308 }
5309
5310 /* When deleted the last sign need to redraw the windows to remove the
5311 * sign column. */
5312 if (buf->b_signlist == NULL)
5313 {
5314 redraw_buf_later(buf, NOT_VALID);
5315 changed_cline_bef_curs();
5316 }
5317
5318 return lnum;
5319}
5320
5321
5322/*
5323 * Find the line number of the sign with the requested id. If the sign does
5324 * not exist, return 0 as the line number. This will still let the correct file
5325 * get loaded.
5326 */
5327 int
5328buf_findsign(buf, id)
5329 buf_T *buf; /* buffer to store sign in */
5330 int id; /* sign ID */
5331{
5332 signlist_T *sign; /* a sign in the signlist */
5333
5334 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5335 if (sign->id == id)
5336 return sign->lnum;
5337
5338 return 0;
5339}
5340
5341 int
5342buf_findsign_id(buf, lnum)
5343 buf_T *buf; /* buffer whose sign we are searching for */
5344 linenr_T lnum; /* line number of sign */
5345{
5346 signlist_T *sign; /* a sign in the signlist */
5347
5348 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5349 if (sign->lnum == lnum)
5350 return sign->id;
5351
5352 return 0;
5353}
5354
5355
5356# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5357/* see if a given type of sign exists on a specific line */
5358 int
5359buf_findsigntype_id(buf, lnum, typenr)
5360 buf_T *buf; /* buffer whose sign we are searching for */
5361 linenr_T lnum; /* line number of sign */
5362 int typenr; /* sign type number */
5363{
5364 signlist_T *sign; /* a sign in the signlist */
5365
5366 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5367 if (sign->lnum == lnum && sign->typenr == typenr)
5368 return sign->id;
5369
5370 return 0;
5371}
5372
5373
5374# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5375/* return the number of icons on the given line */
5376 int
5377buf_signcount(buf, lnum)
5378 buf_T *buf;
5379 linenr_T lnum;
5380{
5381 signlist_T *sign; /* a sign in the signlist */
5382 int count = 0;
5383
5384 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5385 if (sign->lnum == lnum)
5386 if (sign_get_image(sign->typenr) != NULL)
5387 count++;
5388
5389 return count;
5390}
5391# endif /* FEAT_SIGN_ICONS */
5392# endif /* FEAT_NETBEANS_INTG */
5393
5394
5395/*
5396 * Delete signs in buffer "buf".
5397 */
5398 static void
5399buf_delete_signs(buf)
5400 buf_T *buf;
5401{
5402 signlist_T *next;
5403
5404 while (buf->b_signlist != NULL)
5405 {
5406 next = buf->b_signlist->next;
5407 vim_free(buf->b_signlist);
5408 buf->b_signlist = next;
5409 }
5410}
5411
5412/*
5413 * Delete all signs in all buffers.
5414 */
5415 void
5416buf_delete_all_signs()
5417{
5418 buf_T *buf; /* buffer we are checking for signs */
5419
5420 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5421 if (buf->b_signlist != NULL)
5422 {
5423 /* Need to redraw the windows to remove the sign column. */
5424 redraw_buf_later(buf, NOT_VALID);
5425 buf_delete_signs(buf);
5426 }
5427}
5428
5429/*
5430 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5431 */
5432 void
5433sign_list_placed(rbuf)
5434 buf_T *rbuf;
5435{
5436 buf_T *buf;
5437 signlist_T *p;
5438 char lbuf[BUFSIZ];
5439
5440 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5441 msg_putchar('\n');
5442 if (rbuf == NULL)
5443 buf = firstbuf;
5444 else
5445 buf = rbuf;
5446 while (buf != NULL)
5447 {
5448 if (buf->b_signlist != NULL)
5449 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005450 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5452 msg_putchar('\n');
5453 }
5454 for (p = buf->b_signlist; p != NULL; p = p->next)
5455 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005456 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5458 MSG_PUTS(lbuf);
5459 msg_putchar('\n');
5460 }
5461 if (rbuf != NULL)
5462 break;
5463 buf = buf->b_next;
5464 }
5465}
5466
5467/*
5468 * Adjust a placed sign for inserted/deleted lines.
5469 */
5470 void
5471sign_mark_adjust(line1, line2, amount, amount_after)
5472 linenr_T line1;
5473 linenr_T line2;
5474 long amount;
5475 long amount_after;
5476{
5477 signlist_T *sign; /* a sign in a b_signlist */
5478
5479 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5480 {
5481 if (sign->lnum >= line1 && sign->lnum <= line2)
5482 {
5483 if (amount == MAXLNUM)
5484 sign->lnum = line1;
5485 else
5486 sign->lnum += amount;
5487 }
5488 else if (sign->lnum > line2)
5489 sign->lnum += amount_after;
5490 }
5491}
5492#endif /* FEAT_SIGNS */
5493
5494/*
5495 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5496 */
5497 void
5498set_buflisted(on)
5499 int on;
5500{
5501 if (on != curbuf->b_p_bl)
5502 {
5503 curbuf->b_p_bl = on;
5504#ifdef FEAT_AUTOCMD
5505 if (on)
5506 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5507 else
5508 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5509#endif
5510 }
5511}
5512
5513/*
5514 * Read the file for "buf" again and check if the contents changed.
5515 * Return TRUE if it changed or this could not be checked.
5516 */
5517 int
5518buf_contents_changed(buf)
5519 buf_T *buf;
5520{
5521 buf_T *newbuf;
5522 int differ = TRUE;
5523 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 exarg_T ea;
5526
5527 /* Allocate a buffer without putting it in the buffer list. */
5528 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5529 if (newbuf == NULL)
5530 return TRUE;
5531
5532 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5533 if (prep_exarg(&ea, buf) == FAIL)
5534 {
5535 wipe_buffer(newbuf, FALSE);
5536 return TRUE;
5537 }
5538
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 /* set curwin/curbuf to buf and save a few things */
5540 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541
Bram Moolenaar4770d092006-01-12 23:22:24 +00005542 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 && readfile(buf->b_ffname, buf->b_fname,
5544 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5545 &ea, READ_NEW | READ_DUMMY) == OK)
5546 {
5547 /* compare the two files line by line */
5548 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5549 {
5550 differ = FALSE;
5551 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5552 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5553 {
5554 differ = TRUE;
5555 break;
5556 }
5557 }
5558 }
5559 vim_free(ea.cmd);
5560
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 /* restore curwin/curbuf and a few other things */
5562 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563
5564 if (curbuf != newbuf) /* safety check */
5565 wipe_buffer(newbuf, FALSE);
5566
5567 return differ;
5568}
5569
5570/*
5571 * Wipe out a buffer and decrement the last buffer number if it was used for
5572 * this buffer. Call this to wipe out a temp buffer that does not contain any
5573 * marks.
5574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 void
5576wipe_buffer(buf, aucmd)
5577 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005578 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 if (buf->b_fnum == top_file_num - 1)
5581 --top_file_num;
5582
5583#ifdef FEAT_AUTOCMD
5584 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005585 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586#endif
5587 close_buffer(NULL, buf, DOBUF_WIPE);
5588#ifdef FEAT_AUTOCMD
5589 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005590 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591#endif
5592}