comparison src/buffer.c @ 7:3fc0f57ecb91 v7.0001

updated for version 7.0001
author vimboss
date Sun, 13 Jun 2004 20:20:40 +0000
parents
children 4102fb4ea781
comparison
equal deleted inserted replaced
6:c2daee826b8f 7:3fc0f57ecb91
1 /* 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
28
29 #include "vim.h"
30
31 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
32 static char_u *buflist_match __ARGS((regprog_T *prog, buf_T *buf));
33 # define HAVE_BUFLIST_MATCH
34 static char_u *fname_match __ARGS((regprog_T *prog, char_u *name));
35 #endif
36 static void buflist_setfpos __ARGS((buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options));
37 static wininfo_T *find_wininfo __ARGS((buf_T *buf));
38 #ifdef UNIX
39 static buf_T *buflist_findname_stat __ARGS((char_u *ffname, struct stat *st));
40 static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname, struct stat *stp));
41 static int buf_same_ino __ARGS((buf_T *buf, struct stat *stp));
42 #else
43 static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname));
44 #endif
45 #ifdef FEAT_TITLE
46 static int ti_change __ARGS((char_u *str, char_u **last));
47 #endif
48 static void free_buffer __ARGS((buf_T *));
49 static void free_buffer_stuff __ARGS((buf_T *buf, int free_options));
50 static 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)
59 static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr));
60 static 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
68 open_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
86 if (ml_open() == FAIL)
87 {
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
98 * This is OK, since there are no changes to loose.
99 */
100 if (curbuf == NULL)
101 {
102 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
103 getout(2);
104 }
105 EMSG(_("E83: Cannot allocate buffer, using other one..."));
106 enter_buffer(curbuf);
107 return FAIL;
108 }
109
110 #ifdef FEAT_AUTOCMD
111 /* The autocommands in readfile() may change the buffer, but only AFTER
112 * reading the file. */
113 old_curbuf = curbuf;
114 modified_was_set = FALSE;
115 #endif
116
117 /* mark cursor position as being invalid */
118 changed_line_abv_curs();
119
120 if (curbuf->b_ffname != NULL
121 #ifdef FEAT_NETBEANS_INTG
122 && netbeansReadFile
123 #endif
124 )
125 {
126 #ifdef FEAT_NETBEANS_INTG
127 int oldFire = netbeansFireChanges;
128
129 netbeansFireChanges = 0;
130 #endif
131 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
132 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_NEW);
133 #ifdef FEAT_NETBEANS_INTG
134 netbeansFireChanges = oldFire;
135 #endif
136 /* Help buffer is filtered. */
137 if (curbuf->b_help)
138 fix_help_buffer();
139 }
140 else if (read_stdin)
141 {
142 int save_bin = curbuf->b_p_bin;
143 linenr_T line_count;
144
145 /*
146 * First read the text in binary mode into the buffer.
147 * Then read from that same buffer and append at the end. This makes
148 * it possible to retry when 'fileformat' or 'fileencoding' was
149 * guessed wrong.
150 */
151 curbuf->b_p_bin = TRUE;
152 retval = readfile(NULL, NULL, (linenr_T)0,
153 (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW + READ_STDIN);
154 curbuf->b_p_bin = save_bin;
155 if (retval == OK)
156 {
157 line_count = curbuf->b_ml.ml_line_count;
158 retval = readfile(NULL, NULL, (linenr_T)line_count,
159 (linenr_T)0, (linenr_T)MAXLNUM, eap, READ_BUFFER);
160 if (retval == OK)
161 {
162 /* Delete the binary lines. */
163 while (--line_count >= 0)
164 ml_delete((linenr_T)1, FALSE);
165 }
166 else
167 {
168 /* Delete the converted lines. */
169 while (curbuf->b_ml.ml_line_count > line_count)
170 ml_delete(line_count, FALSE);
171 }
172 /* Put the cursor on the first line. */
173 curwin->w_cursor.lnum = 1;
174 curwin->w_cursor.col = 0;
175 #ifdef FEAT_AUTOCMD
176 # ifdef FEAT_EVAL
177 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
178 curbuf, &retval);
179 # else
180 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
181 # endif
182 #endif
183 }
184 }
185
186 /* if first time loading this buffer, init b_chartab[] */
187 if (curbuf->b_flags & BF_NEVERLOADED)
188 (void)buf_init_chartab(curbuf, FALSE);
189
190 /*
191 * Set/reset the Changed flag first, autocmds may change the buffer.
192 * Apply the automatic commands, before processing the modelines.
193 * So the modelines have priority over auto commands.
194 */
195 /* When reading stdin, the buffer contents always needs writing, so set
196 * the changed flag. Unless in readonly mode: "ls | gview -".
197 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
198 if ((read_stdin && !readonlymode && !bufempty())
199 #ifdef FEAT_AUTOCMD
200 || modified_was_set /* ":set modified" used in autocmd */
201 # ifdef FEAT_EVAL
202 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
203 # endif
204 #endif
205 || (got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL))
206 changed();
207 else if (retval != FAIL)
208 unchanged(curbuf, FALSE);
209 save_file_ff(curbuf); /* keep this fileformat */
210
211 /* require "!" to overwrite the file, because it wasn't read completely */
212 #ifdef FEAT_EVAL
213 if (aborting())
214 #else
215 if (got_int)
216 #endif
217 curbuf->b_flags |= BF_READERR;
218
219 #ifdef FEAT_AUTOCMD
220 /* need to set w_topline, unless some autocommand already did that. */
221 if (!(curwin->w_valid & VALID_TOPLINE))
222 {
223 curwin->w_topline = 1;
224 # ifdef FEAT_DIFF
225 curwin->w_topfill = 0;
226 # endif
227 }
228 # ifdef FEAT_EVAL
229 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
230 # else
231 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
232 # endif
233 #endif
234
235 if (retval != FAIL)
236 {
237 #ifdef FEAT_AUTOCMD
238 /*
239 * The autocommands may have changed the current buffer. Apply the
240 * modelines to the correct buffer, if it still exists and is loaded.
241 */
242 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
243 {
244 aco_save_T aco;
245
246 /* Go to the buffer that was opened. */
247 aucmd_prepbuf(&aco, old_curbuf);
248 #endif
249 do_modelines();
250 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
251
252 #ifdef FEAT_AUTOCMD
253 # ifdef FEAT_EVAL
254 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
255 &retval);
256 # else
257 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
258 # endif
259
260 /* restore curwin/curbuf and a few other things */
261 aucmd_restbuf(&aco);
262 }
263 #endif
264 }
265
266 #ifdef FEAT_FOLDING
267 /* Need to update automatic folding. */
268 foldUpdateAll(curwin);
269 #endif
270
271 return retval;
272 }
273
274 /*
275 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
276 */
277 int
278 buf_valid(buf)
279 buf_T *buf;
280 {
281 buf_T *bp;
282
283 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
284 if (bp == buf)
285 return TRUE;
286 return FALSE;
287 }
288
289 /*
290 * Close the link to a buffer.
291 * "action" is used when there is no longer a window for the buffer.
292 * It can be:
293 * 0 buffer becomes hidden
294 * DOBUF_UNLOAD buffer is unloaded
295 * DOBUF_DELETE buffer is unloaded and removed from buffer list
296 * DOBUF_WIPE buffer is unloaded and really deleted
297 * When doing all but the first one on the current buffer, the caller should
298 * get a new buffer very soon!
299 *
300 * The 'bufhidden' option can force freeing and deleting.
301 */
302 void
303 close_buffer(win, buf, action)
304 win_T *win; /* if not NULL, set b_last_cursor */
305 buf_T *buf;
306 int action;
307 {
308 #ifdef FEAT_AUTOCMD
309 int is_curbuf;
310 int nwindows = buf->b_nwindows;
311 #endif
312 int unload_buf = (action != 0);
313 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
314 int wipe_buf = (action == DOBUF_WIPE);
315
316 #ifdef FEAT_QUICKFIX
317 /*
318 * Force unloading or deleting when 'bufhidden' says so.
319 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
320 * "hide" (otherwise we could never free or delete a buffer).
321 */
322 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
323 {
324 del_buf = TRUE;
325 unload_buf = TRUE;
326 }
327 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
328 {
329 del_buf = TRUE;
330 unload_buf = TRUE;
331 wipe_buf = TRUE;
332 }
333 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
334 unload_buf = TRUE;
335 #endif
336
337 if (win != NULL)
338 {
339 /* Set b_last_cursor when closing the last window for the buffer.
340 * Remember the last cursor position and window options of the buffer.
341 * This used to be only for the current window, but then options like
342 * 'foldmethod' may be lost with a ":only" command. */
343 if (buf->b_nwindows == 1)
344 set_last_cursor(win);
345 buflist_setfpos(buf, win,
346 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
347 win->w_cursor.col, TRUE);
348 }
349
350 #ifdef FEAT_AUTOCMD
351 /* When the buffer is no longer in a window, trigger BufWinLeave */
352 if (buf->b_nwindows == 1)
353 {
354 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
355 FALSE, buf);
356 if (!buf_valid(buf)) /* autocommands may delete the buffer */
357 return;
358
359 /* When the buffer becomes hidden, but is not unloaded, trigger
360 * BufHidden */
361 if (!unload_buf)
362 {
363 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
364 FALSE, buf);
365 if (!buf_valid(buf)) /* autocmds may delete the buffer */
366 return;
367 }
368 # ifdef FEAT_EVAL
369 if (aborting()) /* autocmds may abort script processing */
370 return;
371 # endif
372 }
373 nwindows = buf->b_nwindows;
374 #endif
375
376 /* decrease the link count from windows (unless not in any window) */
377 if (buf->b_nwindows > 0)
378 --buf->b_nwindows;
379
380 /* Return when a window is displaying the buffer or when it's not
381 * unloaded. */
382 if (buf->b_nwindows > 0 || !unload_buf)
383 {
384 if (buf == curbuf)
385 u_sync(); /* sync undo before going to another buffer */
386 return;
387 }
388
389 /* Always remove the buffer when there is no file name. */
390 if (buf->b_ffname == NULL)
391 del_buf = TRUE;
392
393 /*
394 * Free all things allocated for this buffer.
395 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
396 */
397 #ifdef FEAT_AUTOCMD
398 /* Remember if we are closing the current buffer. Restore the number of
399 * windows, so that autocommands in buf_freeall() don't get confused. */
400 is_curbuf = (buf == curbuf);
401 buf->b_nwindows = nwindows;
402 #endif
403
404 buf_freeall(buf, del_buf, wipe_buf);
405
406 #ifdef FEAT_AUTOCMD
407 /* Autocommands may have deleted the buffer. */
408 if (!buf_valid(buf))
409 return;
410 # ifdef FEAT_EVAL
411 /* Autocommands may abort script processing. */
412 if (aborting())
413 return;
414 # endif
415
416 /* Autocommands may have opened or closed windows for this buffer.
417 * Decrement the count for the close we do here. */
418 if (buf->b_nwindows > 0)
419 --buf->b_nwindows;
420
421 /*
422 * It's possible that autocommands change curbuf to the one being deleted.
423 * This might cause the previous curbuf to be deleted unexpectedly. But
424 * in some cases it's OK to delete the curbuf, because a new one is
425 * obtained anyway. Therefore only return if curbuf changed to the
426 * deleted buffer.
427 */
428 if (buf == curbuf && !is_curbuf)
429 return;
430 #endif
431
432 #ifdef FEAT_NETBEANS_INTG
433 if (usingNetbeans)
434 netbeans_file_closed(buf);
435 #endif
436 #if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
437 /* Change directories when the acd option is set on. */
438 if (p_acd && curbuf->b_ffname != NULL
439 && vim_chdirfile(curbuf->b_ffname) == OK)
440 shorten_fnames(TRUE);
441 #endif
442
443 /*
444 * Remove the buffer from the list.
445 */
446 if (wipe_buf)
447 {
448 #ifdef FEAT_SUN_WORKSHOP
449 if (usingSunWorkShop)
450 workshop_file_closed_lineno((char *)buf->b_ffname,
451 (int)buf->b_last_cursor.lnum);
452 #endif
453 vim_free(buf->b_ffname);
454 vim_free(buf->b_sfname);
455 if (buf->b_prev == NULL)
456 firstbuf = buf->b_next;
457 else
458 buf->b_prev->b_next = buf->b_next;
459 if (buf->b_next == NULL)
460 lastbuf = buf->b_prev;
461 else
462 buf->b_next->b_prev = buf->b_prev;
463 free_buffer(buf);
464 }
465 else
466 {
467 if (del_buf)
468 {
469 /* Free all internal variables and reset option values, to make
470 * ":bdel" compatible with Vim 5.7. */
471 free_buffer_stuff(buf, TRUE);
472
473 /* Make it look like a new buffer. */
474 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
475
476 /* Init the options when loaded again. */
477 buf->b_p_initialized = FALSE;
478 }
479 buf_clear_file(buf);
480 if (del_buf)
481 buf->b_p_bl = FALSE;
482 }
483 }
484
485 /*
486 * Make buffer not contain a file.
487 */
488 void
489 buf_clear_file(buf)
490 buf_T *buf;
491 {
492 buf->b_ml.ml_line_count = 1;
493 unchanged(buf, TRUE);
494 #ifndef SHORT_FNAME
495 buf->b_shortname = FALSE;
496 #endif
497 buf->b_p_eol = TRUE;
498 buf->b_start_eol = TRUE;
499 #ifdef FEAT_MBYTE
500 buf->b_p_bomb = FALSE;
501 #endif
502 buf->b_ml.ml_mfp = NULL;
503 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
504 #ifdef FEAT_NETBEANS_INTG
505 netbeans_deleted_all_lines(buf);
506 netbeansOpenFile = 0; /* reset in netbeans_file_opened() */
507 #endif
508 }
509
510 /*
511 * buf_freeall() - free all things allocated for a buffer that are related to
512 * the file.
513 */
514 /*ARGSUSED*/
515 void
516 buf_freeall(buf, del_buf, wipe_buf)
517 buf_T *buf;
518 int del_buf; /* buffer is going to be deleted */
519 int wipe_buf; /* buffer is going to be wiped out */
520 {
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
541 if (aborting()) /* autocmds may abort script processing */
542 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
557 #ifdef FEAT_TCL
558 tcl_buffer_free(buf);
559 #endif
560 u_blockfree(buf); /* free the memory allocated for undo */
561 ml_close(buf, TRUE); /* close and delete the memline/memfile */
562 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
563 u_clearall(buf); /* reset all undo information */
564 #ifdef FEAT_SYN_HL
565 syntax_clear(buf); /* reset syntax info */
566 #endif
567 }
568
569 /*
570 * Free a buffer structure and the things it contains related to the buffer
571 * itself (not the file, that must have been done already).
572 */
573 static void
574 free_buffer(buf)
575 buf_T *buf;
576 {
577 free_buffer_stuff(buf, TRUE);
578 #ifdef FEAT_PERL
579 perl_buf_free(buf);
580 #endif
581 #ifdef FEAT_PYTHON
582 python_buffer_free(buf);
583 #endif
584 #ifdef FEAT_RUBY
585 ruby_buffer_free(buf);
586 #endif
587 vim_free(buf);
588 }
589
590 /*
591 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
592 */
593 static void
594 free_buffer_stuff(buf, free_options)
595 buf_T *buf;
596 int free_options; /* free options as well */
597 {
598 if (free_options)
599 {
600 clear_wininfo(buf); /* including window-local options */
601 free_buf_options(buf, TRUE);
602 }
603 #ifdef FEAT_EVAL
604 var_clear(&buf->b_vars); /* free all internal variables */
605 #endif
606 #ifdef FEAT_USR_CMDS
607 uc_clear(&buf->b_ucmds); /* clear local user commands */
608 #endif
609 #ifdef FEAT_SIGNS
610 buf_delete_signs(buf); /* delete any signs */
611 #endif
612 #ifdef FEAT_LOCALMAP
613 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
614 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
615 #endif
616 #ifdef FEAT_MBYTE
617 vim_free(buf->b_start_fenc);
618 buf->b_start_fenc = NULL;
619 #endif
620 }
621
622 /*
623 * Free the b_wininfo list for buffer "buf".
624 */
625 static void
626 clear_wininfo(buf)
627 buf_T *buf;
628 {
629 wininfo_T *wip;
630
631 while (buf->b_wininfo != NULL)
632 {
633 wip = buf->b_wininfo;
634 buf->b_wininfo = wip->wi_next;
635 if (wip->wi_optset)
636 {
637 clear_winopt(&wip->wi_opt);
638 #ifdef FEAT_FOLDING
639 deleteFoldRecurse(&wip->wi_folds);
640 #endif
641 }
642 vim_free(wip);
643 }
644 }
645
646 #if defined(FEAT_LISTCMDS) || defined(PROTO)
647 /*
648 * Go to another buffer. Handles the result of the ATTENTION dialog.
649 */
650 void
651 goto_buffer(eap, start, dir, count)
652 exarg_T *eap;
653 int start;
654 int dir;
655 int count;
656 {
657 # if defined(FEAT_WINDOWS) \
658 && (defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG))
659 buf_T *old_curbuf = curbuf;
660
661 swap_exists_action = SEA_DIALOG;
662 # endif
663 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
664 start, dir, count, eap->forceit);
665 # if defined(FEAT_WINDOWS) \
666 && (defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG))
667 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
668 {
669 /* Quitting means closing the split window, nothing else. */
670 win_close(curwin, TRUE);
671 swap_exists_action = SEA_NONE;
672 }
673 else
674 handle_swap_exists(old_curbuf);
675 # endif
676 }
677 #endif
678
679 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
680 /*
681 * Handle the situation of swap_exists_action being set.
682 * It is allowed for "old_curbuf" to be NULL or invalid.
683 */
684 void
685 handle_swap_exists(old_curbuf)
686 buf_T *old_curbuf;
687 {
688 if (swap_exists_action == SEA_QUIT)
689 {
690 /* User selected Quit at ATTENTION prompt. Go back to previous
691 * buffer. If that buffer is gone or the same as the current one,
692 * open a new, empty buffer. */
693 swap_exists_action = SEA_NONE; /* don't want it again */
694 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
695 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
696 old_curbuf = buflist_new(NULL, NULL, 1L,
697 BLN_CURBUF | BLN_LISTED | BLN_FORCE);
698 if (old_curbuf != NULL)
699 enter_buffer(old_curbuf);
700 /* If "old_curbuf" is NULL we are in big trouble here... */
701 }
702 else if (swap_exists_action == SEA_RECOVER)
703 {
704 /* User selected Recover at ATTENTION prompt. */
705 msg_scroll = TRUE;
706 ml_recover();
707 MSG_PUTS("\n"); /* don't overwrite the last message */
708 cmdline_row = msg_row;
709 do_modelines();
710 }
711 swap_exists_action = SEA_NONE;
712 }
713 #endif
714
715 #if defined(FEAT_LISTCMDS) || defined(PROTO)
716 /*
717 * do_bufdel() - delete or unload buffer(s)
718 *
719 * addr_count == 0: ":bdel" - delete current buffer
720 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
721 * buffer "end_bnr", then any other arguments.
722 * addr_count == 2: ":N,N bdel" - delete buffers in range
723 *
724 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
725 * DOBUF_DEL (":bdel")
726 *
727 * Returns error message or NULL
728 */
729 char_u *
730 do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
731 int command;
732 char_u *arg; /* pointer to extra arguments */
733 int addr_count;
734 int start_bnr; /* first buffer number in a range */
735 int end_bnr; /* buffer nr or last buffer nr in a range */
736 int forceit;
737 {
738 int do_current = 0; /* delete current buffer? */
739 int deleted = 0; /* number of buffers deleted */
740 char_u *errormsg = NULL; /* return value */
741 int bnr; /* buffer number */
742 char_u *p;
743
744 #ifdef FEAT_NETBEANS_INTG
745 netbeansCloseFile = 1;
746 #endif
747 if (addr_count == 0)
748 {
749 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
750 }
751 else
752 {
753 if (addr_count == 2)
754 {
755 if (*arg) /* both range and argument is not allowed */
756 return (char_u *)_(e_trailing);
757 bnr = start_bnr;
758 }
759 else /* addr_count == 1 */
760 bnr = end_bnr;
761
762 for ( ;!got_int; ui_breakcheck())
763 {
764 /*
765 * delete the current buffer last, otherwise when the
766 * current buffer is deleted, the next buffer becomes
767 * the current one and will be loaded, which may then
768 * also be deleted, etc.
769 */
770 if (bnr == curbuf->b_fnum)
771 do_current = bnr;
772 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
773 forceit) == OK)
774 ++deleted;
775
776 /*
777 * find next buffer number to delete/unload
778 */
779 if (addr_count == 2)
780 {
781 if (++bnr > end_bnr)
782 break;
783 }
784 else /* addr_count == 1 */
785 {
786 arg = skipwhite(arg);
787 if (*arg == NUL)
788 break;
789 if (!VIM_ISDIGIT(*arg))
790 {
791 p = skiptowhite_esc(arg);
792 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
793 if (bnr < 0) /* failed */
794 break;
795 arg = p;
796 }
797 else
798 bnr = getdigits(&arg);
799 }
800 }
801 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
802 FORWARD, do_current, forceit) == OK)
803 ++deleted;
804
805 if (deleted == 0)
806 {
807 if (command == DOBUF_UNLOAD)
808 sprintf((char *)IObuff, _("E515: No buffers were unloaded"));
809 else if (command == DOBUF_DEL)
810 sprintf((char *)IObuff, _("E516: No buffers were deleted"));
811 else
812 sprintf((char *)IObuff, _("E517: No buffers were wiped out"));
813 errormsg = IObuff;
814 }
815 else if (deleted >= p_report)
816 {
817 if (command == DOBUF_UNLOAD)
818 {
819 if (deleted == 1)
820 MSG(_("1 buffer unloaded"));
821 else
822 smsg((char_u *)_("%d buffers unloaded"), deleted);
823 }
824 else if (command == DOBUF_DEL)
825 {
826 if (deleted == 1)
827 MSG(_("1 buffer deleted"));
828 else
829 smsg((char_u *)_("%d buffers deleted"), deleted);
830 }
831 else
832 {
833 if (deleted == 1)
834 MSG(_("1 buffer wiped out"));
835 else
836 smsg((char_u *)_("%d buffers wiped out"), deleted);
837 }
838 }
839 }
840
841 #ifdef FEAT_NETBEANS_INTG
842 netbeansCloseFile = 0;
843 #endif
844
845 return errormsg;
846 }
847
848 /*
849 * Implementation of the commands for the buffer list.
850 *
851 * action == DOBUF_GOTO go to specified buffer
852 * action == DOBUF_SPLIT split window and go to specified buffer
853 * action == DOBUF_UNLOAD unload specified buffer(s)
854 * action == DOBUF_DEL delete specified buffer(s) from buffer list
855 * action == DOBUF_WIPE delete specified buffer(s) really
856 *
857 * start == DOBUF_CURRENT go to "count" buffer from current buffer
858 * start == DOBUF_FIRST go to "count" buffer from first buffer
859 * start == DOBUF_LAST go to "count" buffer from last buffer
860 * start == DOBUF_MOD go to "count" modified buffer from current buffer
861 *
862 * Return FAIL or OK.
863 */
864 int
865 do_buffer(action, start, dir, count, forceit)
866 int action;
867 int start;
868 int dir; /* FORWARD or BACKWARD */
869 int count; /* buffer number or number of buffers */
870 int forceit; /* TRUE for :...! */
871 {
872 buf_T *buf;
873 buf_T *bp;
874 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
875 || action == DOBUF_WIPE);
876
877 switch (start)
878 {
879 case DOBUF_FIRST: buf = firstbuf; break;
880 case DOBUF_LAST: buf = lastbuf; break;
881 default: buf = curbuf; break;
882 }
883 if (start == DOBUF_MOD) /* find next modified buffer */
884 {
885 while (count-- > 0)
886 {
887 do
888 {
889 buf = buf->b_next;
890 if (buf == NULL)
891 buf = firstbuf;
892 }
893 while (buf != curbuf && !bufIsChanged(buf));
894 }
895 if (!bufIsChanged(buf))
896 {
897 EMSG(_("E84: No modified buffer found"));
898 return FAIL;
899 }
900 }
901 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
902 {
903 while (buf != NULL && buf->b_fnum != count)
904 buf = buf->b_next;
905 }
906 else
907 {
908 bp = NULL;
909 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
910 {
911 /* remember the buffer where we start, we come back there when all
912 * buffers are unlisted. */
913 if (bp == NULL)
914 bp = buf;
915 if (dir == FORWARD)
916 {
917 buf = buf->b_next;
918 if (buf == NULL)
919 buf = firstbuf;
920 }
921 else
922 {
923 buf = buf->b_prev;
924 if (buf == NULL)
925 buf = lastbuf;
926 }
927 /* don't count unlisted buffers */
928 if (unload || buf->b_p_bl)
929 {
930 --count;
931 bp = NULL; /* use this buffer as new starting point */
932 }
933 if (bp == buf)
934 {
935 /* back where we started, didn't find anything. */
936 EMSG(_("E85: There is no listed buffer"));
937 return FAIL;
938 }
939 }
940 }
941
942 if (buf == NULL) /* could not find it */
943 {
944 if (start == DOBUF_FIRST)
945 {
946 /* don't warn when deleting */
947 if (!unload)
948 EMSGN(_("E86: Buffer %ld does not exist"), count);
949 }
950 else if (dir == FORWARD)
951 EMSG(_("E87: Cannot go beyond last buffer"));
952 else
953 EMSG(_("E88: Cannot go before first buffer"));
954 return FAIL;
955 }
956
957 #ifdef FEAT_GUI
958 need_mouse_correct = TRUE;
959 #endif
960
961 #ifdef FEAT_LISTCMDS
962 /*
963 * delete buffer buf from memory and/or the list
964 */
965 if (unload)
966 {
967 int forward;
968 int retval;
969
970 /* When unloading or deleting a buffer that's already unloaded and
971 * unlisted: fail silently. */
972 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
973 return FAIL;
974
975 if (!forceit && bufIsChanged(buf))
976 {
977 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
978 if ((p_confirm || cmdmod.confirm) && p_write)
979 {
980 dialog_changed(buf, FALSE);
981 # ifdef FEAT_AUTOCMD
982 if (!buf_valid(buf))
983 /* Autocommand deleted buffer, oops! It's not changed
984 * now. */
985 return FAIL;
986 # endif
987 }
988 if (bufIsChanged(buf))
989 #endif
990 {
991 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
992 buf->b_fnum);
993 return FAIL;
994 }
995 }
996
997 /*
998 * If deleting the last (listed) buffer, make it empty.
999 * The last (listed) buffer cannot be unloaded.
1000 */
1001 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1002 if (bp->b_p_bl && bp != buf)
1003 break;
1004 if (bp == NULL && buf == curbuf)
1005 {
1006 if (action == DOBUF_UNLOAD)
1007 {
1008 EMSG(_("E90: Cannot unload last buffer"));
1009 return FAIL;
1010 }
1011
1012 /* Close any other windows on this buffer, then make it empty. */
1013 #ifdef FEAT_WINDOWS
1014 {
1015 win_T *wp, *nextwp;
1016
1017 for (wp = firstwin; wp != NULL; wp = nextwp)
1018 {
1019 nextwp = wp->w_next;
1020 if (wp != curwin && wp->w_buffer == buf)
1021 {
1022 /* Start all over, autocommands may change the window
1023 * layout. */
1024 nextwp = firstwin;
1025 win_close(wp, FALSE);
1026 }
1027 }
1028 }
1029 #endif
1030 setpcmark();
1031 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1032 forceit ? ECMD_FORCEIT : 0);
1033
1034 /*
1035 * do_ecmd() may create a new buffer, then we have to delete
1036 * the old one. But do_ecmd() may have done that already, check
1037 * if the buffer still exists.
1038 */
1039 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1040 close_buffer(NULL, buf, action);
1041 return retval;
1042 }
1043
1044 #ifdef FEAT_WINDOWS
1045 /*
1046 * If the deleted buffer is the current one, close the current window
1047 * (unless it's the only window).
1048 */
1049 while (buf == curbuf && firstwin != lastwin)
1050 win_close(curwin, FALSE);
1051 #endif
1052
1053 /*
1054 * If the buffer to be deleted is not the current one, delete it here.
1055 */
1056 if (buf != curbuf)
1057 {
1058 #ifdef FEAT_WINDOWS
1059 close_windows(buf);
1060 #endif
1061 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1062 close_buffer(NULL, buf, action);
1063 return OK;
1064 }
1065
1066 /*
1067 * Deleting the current buffer: Need to find another buffer to go to.
1068 * There must be another, otherwise it would have been handled above.
1069 * First use au_new_curbuf, if it is valid.
1070 * Then prefer the buffer we most recently visited.
1071 * Else try to find one that is loaded, after the current buffer,
1072 * then before the current buffer.
1073 * Finally use any buffer.
1074 */
1075 buf = NULL; /* selected buffer */
1076 bp = NULL; /* used when no loaded buffer found */
1077 #ifdef FEAT_AUTOCMD
1078 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1079 buf = au_new_curbuf;
1080 # ifdef FEAT_JUMPLIST
1081 else
1082 # endif
1083 #endif
1084 #ifdef FEAT_JUMPLIST
1085 if (curwin->w_jumplistlen > 0)
1086 {
1087 int jumpidx;
1088
1089 jumpidx = curwin->w_jumplistidx - 1;
1090 if (jumpidx < 0)
1091 jumpidx = curwin->w_jumplistlen - 1;
1092
1093 forward = jumpidx;
1094 while (jumpidx != curwin->w_jumplistidx)
1095 {
1096 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1097 if (buf != NULL)
1098 {
1099 if (buf == curbuf || !buf->b_p_bl)
1100 buf = NULL; /* skip current and unlisted bufs */
1101 else if (buf->b_ml.ml_mfp == NULL)
1102 {
1103 /* skip unloaded buf, but may keep it for later */
1104 if (bp == NULL)
1105 bp = buf;
1106 buf = NULL;
1107 }
1108 }
1109 if (buf != NULL) /* found a valid buffer: stop searching */
1110 break;
1111 /* advance to older entry in jump list */
1112 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1113 break;
1114 if (--jumpidx < 0)
1115 jumpidx = curwin->w_jumplistlen - 1;
1116 if (jumpidx == forward) /* List exhausted for sure */
1117 break;
1118 }
1119 }
1120 #endif
1121
1122 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1123 {
1124 forward = TRUE;
1125 buf = curbuf->b_next;
1126 for (;;)
1127 {
1128 if (buf == NULL)
1129 {
1130 if (!forward) /* tried both directions */
1131 break;
1132 buf = curbuf->b_prev;
1133 forward = FALSE;
1134 continue;
1135 }
1136 /* in non-help buffer, try to skip help buffers, and vv */
1137 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1138 {
1139 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1140 break;
1141 if (bp == NULL) /* remember unloaded buf for later */
1142 bp = buf;
1143 }
1144 if (forward)
1145 buf = buf->b_next;
1146 else
1147 buf = buf->b_prev;
1148 }
1149 }
1150 if (buf == NULL) /* No loaded buffer, use unloaded one */
1151 buf = bp;
1152 if (buf == NULL) /* No loaded buffer, find listed one */
1153 {
1154 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1155 if (buf->b_p_bl && buf != curbuf)
1156 break;
1157 }
1158 if (buf == NULL) /* Still no buffer, just take one */
1159 {
1160 if (curbuf->b_next != NULL)
1161 buf = curbuf->b_next;
1162 else
1163 buf = curbuf->b_prev;
1164 }
1165 }
1166
1167 /*
1168 * make buf current buffer
1169 */
1170 if (action == DOBUF_SPLIT) /* split window first */
1171 {
1172 # ifdef FEAT_WINDOWS
1173 /* jump to first window containing buf if one exists ("useopen") */
1174 if (vim_strchr(p_swb, 'u') && buf_jump_open_win(buf))
1175 return OK;
1176 if (win_split(0, 0) == FAIL)
1177 # endif
1178 return FAIL;
1179 }
1180 #endif
1181
1182 /* go to current buffer - nothing to do */
1183 if (buf == curbuf)
1184 return OK;
1185
1186 /*
1187 * Check if the current buffer may be abandoned.
1188 */
1189 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1190 {
1191 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1192 if ((p_confirm || cmdmod.confirm) && p_write)
1193 {
1194 dialog_changed(curbuf, FALSE);
1195 # ifdef FEAT_AUTOCMD
1196 if (!buf_valid(buf))
1197 /* Autocommand deleted buffer, oops! */
1198 return FAIL;
1199 # endif
1200 }
1201 if (bufIsChanged(curbuf))
1202 #endif
1203 {
1204 EMSG(_(e_nowrtmsg));
1205 return FAIL;
1206 }
1207 }
1208
1209 /* Go to the other buffer. */
1210 set_curbuf(buf, action);
1211
1212 #if defined(FEAT_LISTCMDS) && defined(FEAT_SCROLLBIND)
1213 if (action == DOBUF_SPLIT)
1214 curwin->w_p_scb = FALSE; /* reset 'scrollbind' */
1215 #endif
1216
1217 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1218 if (aborting()) /* autocmds may abort script processing */
1219 return FAIL;
1220 #endif
1221
1222 return OK;
1223 }
1224
1225 #endif /* FEAT_LISTCMDS */
1226
1227 /*
1228 * Set current buffer to "buf". Executes autocommands and closes current
1229 * buffer. "action" tells how to close the current buffer:
1230 * DOBUF_GOTO free or hide it
1231 * DOBUF_SPLIT nothing
1232 * DOBUF_UNLOAD unload it
1233 * DOBUF_DEL delete it
1234 * DOBUF_WIPE wipe it out
1235 */
1236 void
1237 set_curbuf(buf, action)
1238 buf_T *buf;
1239 int action;
1240 {
1241 buf_T *prevbuf;
1242 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1243 || action == DOBUF_WIPE);
1244
1245 setpcmark();
1246 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
1247 buflist_altfpos(); /* remember curpos */
1248
1249 #ifdef FEAT_VISUAL
1250 /* Don't restart Select mode after switching to another buffer. */
1251 VIsual_reselect = FALSE;
1252 #endif
1253
1254 /* close_windows() or apply_autocmds() may change curbuf */
1255 prevbuf = curbuf;
1256
1257 #ifdef FEAT_AUTOCMD
1258 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1259 # ifdef FEAT_EVAL
1260 if (buf_valid(prevbuf) && !aborting())
1261 # else
1262 if (buf_valid(prevbuf))
1263 # endif
1264 #endif
1265 {
1266 #ifdef FEAT_WINDOWS
1267 if (unload)
1268 close_windows(prevbuf);
1269 #endif
1270 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1271 if (buf_valid(prevbuf) && !aborting())
1272 #else
1273 if (buf_valid(prevbuf))
1274 #endif
1275 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1276 unload ? action : (action == DOBUF_GOTO
1277 && !P_HID(prevbuf)
1278 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
1279 }
1280 #ifdef FEAT_AUTOCMD
1281 # ifdef FEAT_EVAL
1282 /* An autocommand may have deleted buf or aborted the script processing! */
1283 if (buf_valid(buf) && !aborting())
1284 # else
1285 if (buf_valid(buf)) /* an autocommand may have deleted buf! */
1286 # endif
1287 #endif
1288 enter_buffer(buf);
1289 }
1290
1291 /*
1292 * Enter a new current buffer.
1293 * Old curbuf must have been abandoned already!
1294 */
1295 void
1296 enter_buffer(buf)
1297 buf_T *buf;
1298 {
1299 /* Copy buffer and window local option values. Not for a help buffer. */
1300 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1301 if (!buf->b_help)
1302 get_winopts(buf);
1303 #ifdef FEAT_FOLDING
1304 else
1305 /* Remove all folds in the window. */
1306 clearFolding(curwin);
1307 foldUpdateAll(curwin); /* update folds (later). */
1308 #endif
1309
1310 /* Get the buffer in the current window. */
1311 curwin->w_buffer = buf;
1312 curbuf = buf;
1313 ++curbuf->b_nwindows;
1314
1315 #ifdef FEAT_DIFF
1316 diff_new_buffer();
1317 #endif
1318
1319 /* Cursor on first line by default. */
1320 curwin->w_cursor.lnum = 1;
1321 curwin->w_cursor.col = 0;
1322 #ifdef FEAT_VIRTUALEDIT
1323 curwin->w_cursor.coladd = 0;
1324 #endif
1325 curwin->w_set_curswant = TRUE;
1326
1327 /* Make sure the buffer is loaded. */
1328 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
1329 open_buffer(FALSE, NULL);
1330 else
1331 {
1332 need_fileinfo = TRUE; /* display file info after redraw */
1333 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1334 #ifdef FEAT_AUTOCMD
1335 curwin->w_topline = 1;
1336 # ifdef FEAT_DIFF
1337 curwin->w_topfill = 0;
1338 # endif
1339 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1340 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1341 #endif
1342 }
1343
1344 /* If autocommands did not change the cursor position, restore cursor lnum
1345 * and possibly cursor col. */
1346 if (curwin->w_cursor.lnum == 1 && inindent(0))
1347 buflist_getfpos();
1348
1349 check_arg_idx(curwin); /* check for valid arg_idx */
1350 #ifdef FEAT_TITLE
1351 maketitle();
1352 #endif
1353 #ifdef FEAT_AUTOCMD
1354 if (curwin->w_topline == 1) /* when autocmds didn't change it */
1355 #endif
1356 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1357
1358 #if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
1359 /* Change directories when the acd option is set on. */
1360 if (p_acd && curbuf->b_ffname != NULL
1361 && vim_chdirfile(curbuf->b_ffname) == OK)
1362 shorten_fnames(TRUE);
1363 #endif
1364
1365 #ifdef FEAT_KEYMAP
1366 if (curbuf->b_kmap_state & KEYMAP_INIT)
1367 keymap_init();
1368 #endif
1369 redraw_later(NOT_VALID);
1370 }
1371
1372 /*
1373 * functions for dealing with the buffer list
1374 */
1375
1376 /*
1377 * Add a file name to the buffer list. Return a pointer to the buffer.
1378 * If the same file name already exists return a pointer to that buffer.
1379 * If it does not exist, or if fname == NULL, a new entry is created.
1380 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1381 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1382 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
1383 * If (flags & BLN_FORCE) is TRUE, don't abort on an error.
1384 * This is the ONLY way to create a new buffer.
1385 */
1386 static int top_file_num = 1; /* highest file number */
1387
1388 buf_T *
1389 buflist_new(ffname, sfname, lnum, flags)
1390 char_u *ffname; /* full path of fname or relative */
1391 char_u *sfname; /* short fname or NULL */
1392 linenr_T lnum; /* preferred cursor line */
1393 int flags; /* BLN_ defines */
1394 {
1395 buf_T *buf;
1396 #ifdef UNIX
1397 struct stat st;
1398 #endif
1399
1400 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1401
1402 /*
1403 * If file name already exists in the list, update the entry.
1404 */
1405 #ifdef UNIX
1406 /* On Unix we can use inode numbers when the file exists. Works better
1407 * for hard links. */
1408 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1409 st.st_dev = (dev_T)-1;
1410 #endif
1411 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1412 #ifdef UNIX
1413 buflist_findname_stat(ffname, &st)
1414 #else
1415 buflist_findname(ffname)
1416 #endif
1417 ) != NULL)
1418 {
1419 vim_free(ffname);
1420 if (lnum != 0)
1421 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1422 /* copy the options now, if 'cpo' doesn't have 's' and not done
1423 * already */
1424 buf_copy_options(buf, 0);
1425 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1426 {
1427 buf->b_p_bl = TRUE;
1428 #ifdef FEAT_AUTOCMD
1429 if (!(flags & BLN_DUMMY))
1430 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1431 #endif
1432 }
1433 return buf;
1434 }
1435
1436 /*
1437 * If the current buffer has no name and no contents, use the current
1438 * buffer. Otherwise: Need to allocate a new buffer structure.
1439 *
1440 * This is the ONLY place where a new buffer structure is allocated!
1441 */
1442 buf = NULL;
1443 if ((flags & BLN_CURBUF)
1444 && curbuf != NULL
1445 && curbuf->b_ffname == NULL
1446 && curbuf->b_nwindows <= 1
1447 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1448 {
1449 buf = curbuf;
1450 #ifdef FEAT_AUTOCMD
1451 /* It's like this buffer is deleted. Watch out for autocommands that
1452 * change curbuf! If that happens, allocate a new buffer anyway. */
1453 if (curbuf->b_p_bl)
1454 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1455 if (buf == curbuf)
1456 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1457 # ifdef FEAT_EVAL
1458 /* autocmds may abort script processing */
1459 if (!(flags & BLN_FORCE) && aborting())
1460 return NULL;
1461 # endif
1462 #endif
1463 #ifdef FEAT_QUICKFIX
1464 # ifdef FEAT_AUTOCMD
1465 if (buf == curbuf)
1466 # endif
1467 {
1468 /* Make sure 'bufhidden' and 'buftype' are empty */
1469 clear_string_option(&buf->b_p_bh);
1470 clear_string_option(&buf->b_p_bt);
1471 }
1472 #endif
1473 }
1474 if (buf != curbuf || curbuf == NULL)
1475 {
1476 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1477 if (buf == NULL)
1478 {
1479 vim_free(ffname);
1480 return NULL;
1481 }
1482 }
1483
1484 if (ffname != NULL)
1485 {
1486 buf->b_ffname = ffname;
1487 buf->b_sfname = vim_strsave(sfname);
1488 }
1489
1490 clear_wininfo(buf);
1491 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1492
1493 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1494 || buf->b_wininfo == NULL)
1495 {
1496 vim_free(buf->b_ffname);
1497 buf->b_ffname = NULL;
1498 vim_free(buf->b_sfname);
1499 buf->b_sfname = NULL;
1500 if (buf != curbuf)
1501 free_buffer(buf);
1502 return NULL;
1503 }
1504
1505 if (buf == curbuf)
1506 {
1507 /* free all things allocated for this buffer */
1508 buf_freeall(buf, FALSE, FALSE);
1509 if (buf != curbuf) /* autocommands deleted the buffer! */
1510 return NULL;
1511 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1512 /* autocmds may abort script processing */
1513 if (!(flags & BLN_FORCE) && aborting())
1514 return NULL;
1515 #endif
1516 /* buf->b_nwindows = 0; why was this here? */
1517 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1518 #ifdef FEAT_KEYMAP
1519 /* need to reload lmaps and set b:keymap_name */
1520 curbuf->b_kmap_state |= KEYMAP_INIT;
1521 #endif
1522 }
1523 else
1524 {
1525 /*
1526 * put new buffer at the end of the buffer list
1527 */
1528 buf->b_next = NULL;
1529 if (firstbuf == NULL) /* buffer list is empty */
1530 {
1531 buf->b_prev = NULL;
1532 firstbuf = buf;
1533 }
1534 else /* append new buffer at end of list */
1535 {
1536 lastbuf->b_next = buf;
1537 buf->b_prev = lastbuf;
1538 }
1539 lastbuf = buf;
1540
1541 buf->b_fnum = top_file_num++;
1542 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1543 {
1544 EMSG(_("W14: Warning: List of file names overflow"));
1545 if (emsg_silent == 0)
1546 {
1547 out_flush();
1548 ui_delay(3000L, TRUE); /* make sure it is noticed */
1549 }
1550 top_file_num = 1;
1551 }
1552
1553 /*
1554 * Always copy the options from the current buffer.
1555 */
1556 buf_copy_options(buf, BCO_ALWAYS);
1557 }
1558
1559 buf->b_wininfo->wi_fpos.lnum = lnum;
1560 buf->b_wininfo->wi_win = curwin;
1561
1562 #ifdef FEAT_EVAL
1563 var_init(&buf->b_vars); /* init internal variables */
1564 #endif
1565
1566 buf->b_fname = buf->b_sfname;
1567 #ifdef UNIX
1568 if (st.st_dev == (dev_T)-1)
1569 buf->b_dev = -1;
1570 else
1571 {
1572 buf->b_dev = st.st_dev;
1573 buf->b_ino = st.st_ino;
1574 }
1575 #endif
1576 buf->b_u_synced = TRUE;
1577 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
1578 buf_clear_file(buf);
1579 clrallmarks(buf); /* clear marks */
1580 fmarks_check_names(buf); /* check file marks for this file */
1581 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1582 #ifdef FEAT_AUTOCMD
1583 if (!(flags & BLN_DUMMY))
1584 {
1585 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1586 if (flags & BLN_LISTED)
1587 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1588 # ifdef FEAT_EVAL
1589 /* autocmds may abort script processing */
1590 if (!(flags & BLN_FORCE) && aborting())
1591 return NULL;
1592 # endif
1593 }
1594 #endif
1595
1596 return buf;
1597 }
1598
1599 /*
1600 * Free the memory for the options of a buffer.
1601 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1602 * 'fileencoding'.
1603 */
1604 void
1605 free_buf_options(buf, free_p_ff)
1606 buf_T *buf;
1607 int free_p_ff;
1608 {
1609 if (free_p_ff)
1610 {
1611 #ifdef FEAT_MBYTE
1612 clear_string_option(&buf->b_p_fenc);
1613 #endif
1614 clear_string_option(&buf->b_p_ff);
1615 #ifdef FEAT_QUICKFIX
1616 clear_string_option(&buf->b_p_bh);
1617 clear_string_option(&buf->b_p_bt);
1618 #endif
1619 }
1620 #ifdef FEAT_FIND_ID
1621 clear_string_option(&buf->b_p_def);
1622 clear_string_option(&buf->b_p_inc);
1623 # ifdef FEAT_EVAL
1624 clear_string_option(&buf->b_p_inex);
1625 # endif
1626 #endif
1627 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1628 clear_string_option(&buf->b_p_inde);
1629 clear_string_option(&buf->b_p_indk);
1630 #endif
1631 #ifdef FEAT_CRYPT
1632 clear_string_option(&buf->b_p_key);
1633 #endif
1634 clear_string_option(&buf->b_p_kp);
1635 clear_string_option(&buf->b_p_mps);
1636 clear_string_option(&buf->b_p_fo);
1637 clear_string_option(&buf->b_p_isk);
1638 #ifdef FEAT_KEYMAP
1639 clear_string_option(&buf->b_p_keymap);
1640 ga_clear(&buf->b_kmap_ga);
1641 #endif
1642 #ifdef FEAT_COMMENTS
1643 clear_string_option(&buf->b_p_com);
1644 #endif
1645 #ifdef FEAT_FOLDING
1646 clear_string_option(&buf->b_p_cms);
1647 #endif
1648 clear_string_option(&buf->b_p_nf);
1649 #ifdef FEAT_SYN_HL
1650 clear_string_option(&buf->b_p_syn);
1651 #endif
1652 #ifdef FEAT_SEARCHPATH
1653 clear_string_option(&buf->b_p_sua);
1654 #endif
1655 #ifdef FEAT_AUTOCMD
1656 clear_string_option(&buf->b_p_ft);
1657 #endif
1658 #ifdef FEAT_OSFILETYPE
1659 clear_string_option(&buf->b_p_oft);
1660 #endif
1661 #ifdef FEAT_CINDENT
1662 clear_string_option(&buf->b_p_cink);
1663 clear_string_option(&buf->b_p_cino);
1664 #endif
1665 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1666 clear_string_option(&buf->b_p_cinw);
1667 #endif
1668 #ifdef FEAT_INS_EXPAND
1669 clear_string_option(&buf->b_p_cpt);
1670 #endif
1671 #ifdef FEAT_QUICKFIX
1672 clear_string_option(&buf->b_p_gp);
1673 clear_string_option(&buf->b_p_mp);
1674 clear_string_option(&buf->b_p_efm);
1675 #endif
1676 clear_string_option(&buf->b_p_ep);
1677 clear_string_option(&buf->b_p_path);
1678 clear_string_option(&buf->b_p_tags);
1679 #ifdef FEAT_INS_EXPAND
1680 clear_string_option(&buf->b_p_dict);
1681 clear_string_option(&buf->b_p_tsr);
1682 #endif
1683 buf->b_p_ar = -1;
1684 }
1685
1686 /*
1687 * get alternate file n
1688 * set linenr to lnum or altfpos.lnum if lnum == 0
1689 * also set cursor column to altfpos.col if 'startofline' is not set.
1690 * if (options & GETF_SETMARK) call setpcmark()
1691 * if (options & GETF_ALT) we are jumping to an alternate file.
1692 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1693 *
1694 * return FAIL for failure, OK for success
1695 */
1696 int
1697 buflist_getfile(n, lnum, options, forceit)
1698 int n;
1699 linenr_T lnum;
1700 int options;
1701 int forceit;
1702 {
1703 buf_T *buf;
1704 #ifdef FEAT_WINDOWS
1705 win_T *wp = NULL;
1706 #endif
1707 pos_T *fpos;
1708 colnr_T col;
1709
1710 buf = buflist_findnr(n);
1711 if (buf == NULL)
1712 {
1713 if ((options & GETF_ALT) && n == 0)
1714 EMSG(_(e_noalt));
1715 else
1716 EMSGN(_("E92: Buffer %ld not found"), n);
1717 return FAIL;
1718 }
1719
1720 /* if alternate file is the current buffer, nothing to do */
1721 if (buf == curbuf)
1722 return OK;
1723
1724 #ifdef FEAT_CMDWIN
1725 if (cmdwin_type != 0)
1726 return FAIL;
1727 #endif
1728
1729 /* altfpos may be changed by getfile(), get it now */
1730 if (lnum == 0)
1731 {
1732 fpos = buflist_findfpos(buf);
1733 lnum = fpos->lnum;
1734 col = fpos->col;
1735 }
1736 else
1737 col = 0;
1738
1739 #ifdef FEAT_WINDOWS
1740 if (options & GETF_SWITCH)
1741 {
1742 /* use existing open window for buffer if wanted */
1743 if (vim_strchr(p_swb, 'u')) /* useopen */
1744 wp = buf_jump_open_win(buf);
1745 /* split window if wanted ("split") */
1746 if (wp == NULL && vim_strchr(p_swb, 't') && !bufempty())
1747 {
1748 if (win_split(0, 0) == FAIL)
1749 return FAIL;
1750 # ifdef FEAT_SCROLLBIND
1751 curwin->w_p_scb = FALSE;
1752 # endif
1753 }
1754 }
1755 #endif
1756
1757 ++RedrawingDisabled;
1758 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1759 lnum, forceit) <= 0)
1760 {
1761 --RedrawingDisabled;
1762
1763 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1764 if (!p_sol && col != 0)
1765 {
1766 curwin->w_cursor.col = col;
1767 check_cursor_col();
1768 #ifdef FEAT_VIRTUALEDIT
1769 curwin->w_cursor.coladd = 0;
1770 #endif
1771 curwin->w_set_curswant = TRUE;
1772 }
1773 return OK;
1774 }
1775 --RedrawingDisabled;
1776 return FAIL;
1777 }
1778
1779 /*
1780 * go to the last know line number for the current buffer
1781 */
1782 void
1783 buflist_getfpos()
1784 {
1785 pos_T *fpos;
1786
1787 fpos = buflist_findfpos(curbuf);
1788
1789 curwin->w_cursor.lnum = fpos->lnum;
1790 check_cursor_lnum();
1791
1792 if (p_sol)
1793 curwin->w_cursor.col = 0;
1794 else
1795 {
1796 curwin->w_cursor.col = fpos->col;
1797 check_cursor_col();
1798 #ifdef FEAT_VIRTUALEDIT
1799 curwin->w_cursor.coladd = 0;
1800 #endif
1801 curwin->w_set_curswant = TRUE;
1802 }
1803 }
1804
1805 /*
1806 * Find file in buffer list by name (it has to be for the current window).
1807 * "ffname" must have a full path.
1808 */
1809 buf_T *
1810 buflist_findname(ffname)
1811 char_u *ffname;
1812 {
1813 #ifdef UNIX
1814 struct stat st;
1815
1816 if (mch_stat((char *)ffname, &st) < 0)
1817 st.st_dev = (dev_T)-1;
1818 return buflist_findname_stat(ffname, &st);
1819 }
1820
1821 /*
1822 * Same as buflist_findname(), but pass the stat structure to avoid getting it
1823 * twice for the same file.
1824 */
1825 static buf_T *
1826 buflist_findname_stat(ffname, stp)
1827 char_u *ffname;
1828 struct stat *stp;
1829 {
1830 #endif
1831 buf_T *buf;
1832
1833 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1834 if (!otherfile_buf(buf, ffname
1835 #ifdef UNIX
1836 , stp
1837 #endif
1838 ))
1839 return buf;
1840 return NULL;
1841 }
1842
1843 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
1844 /*
1845 * Find file in buffer list by a regexp pattern.
1846 * Return fnum of the found buffer.
1847 * Return < 0 for error.
1848 */
1849 /*ARGSUSED*/
1850 int
1851 buflist_findpat(pattern, pattern_end, unlisted, diffmode)
1852 char_u *pattern;
1853 char_u *pattern_end; /* pointer to first char after pattern */
1854 int unlisted; /* find unlisted buffers */
1855 int diffmode; /* find diff-mode buffers only */
1856 {
1857 buf_T *buf;
1858 regprog_T *prog;
1859 int match = -1;
1860 int find_listed;
1861 char_u *pat;
1862 char_u *patend;
1863 int attempt;
1864 char_u *p;
1865 int toggledollar;
1866
1867 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
1868 {
1869 if (*pattern == '%')
1870 match = curbuf->b_fnum;
1871 else
1872 match = curwin->w_alt_fnum;
1873 #ifdef FEAT_DIFF
1874 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
1875 match = -1;
1876 #endif
1877 }
1878
1879 /*
1880 * Try four ways of matching a listed buffer:
1881 * attempt == 0: without '^' or '$' (at any position)
1882 * attempt == 1: with '^' at start (only at postion 0)
1883 * attempt == 2: with '$' at end (only match at end)
1884 * attempt == 3: with '^' at start and '$' at end (only full match)
1885 * Repeat this for finding an unlisted buffer if there was no matching
1886 * listed buffer.
1887 */
1888 else
1889 {
1890 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
1891 if (pat == NULL)
1892 return -1;
1893 patend = pat + STRLEN(pat) - 1;
1894 toggledollar = (patend > pat && *patend == '$');
1895
1896 /* First try finding a listed buffer. If not found and "unlisted"
1897 * is TRUE, try finding an unlisted buffer. */
1898 find_listed = TRUE;
1899 for (;;)
1900 {
1901 for (attempt = 0; attempt <= 3; ++attempt)
1902 {
1903 /* may add '^' and '$' */
1904 if (toggledollar)
1905 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
1906 p = pat;
1907 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
1908 ++p;
1909 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
1910 if (prog == NULL)
1911 {
1912 vim_free(pat);
1913 return -1;
1914 }
1915
1916 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1917 if (buf->b_p_bl == find_listed
1918 #ifdef FEAT_DIFF
1919 && (!diffmode || diff_mode_buf(buf))
1920 #endif
1921 && buflist_match(prog, buf) != NULL)
1922 {
1923 if (match >= 0) /* already found a match */
1924 {
1925 match = -2;
1926 break;
1927 }
1928 match = buf->b_fnum; /* remember first match */
1929 }
1930
1931 vim_free(prog);
1932 if (match >= 0) /* found one match */
1933 break;
1934 }
1935
1936 /* Only search for unlisted buffers if there was no match with
1937 * a listed buffer. */
1938 if (!unlisted || !find_listed || match != -1)
1939 break;
1940 find_listed = FALSE;
1941 }
1942
1943 vim_free(pat);
1944 }
1945
1946 if (match == -2)
1947 EMSG2(_("E93: More than one match for %s"), pattern);
1948 else if (match < 0)
1949 EMSG2(_("E94: No matching buffer for %s"), pattern);
1950 return match;
1951 }
1952 #endif
1953
1954 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1955
1956 /*
1957 * Find all buffer names that match.
1958 * For command line expansion of ":buf" and ":sbuf".
1959 * Return OK if matches found, FAIL otherwise.
1960 */
1961 int
1962 ExpandBufnames(pat, num_file, file, options)
1963 char_u *pat;
1964 int *num_file;
1965 char_u ***file;
1966 int options;
1967 {
1968 int count = 0;
1969 buf_T *buf;
1970 int round;
1971 char_u *p;
1972 int attempt;
1973 regprog_T *prog;
1974
1975 *num_file = 0; /* return values in case of FAIL */
1976 *file = NULL;
1977
1978 /*
1979 * attempt == 1: try match with '^', match at start
1980 * attempt == 2: try match without '^', match anywhere
1981 */
1982 for (attempt = 1; attempt <= 2; ++attempt)
1983 {
1984 if (attempt == 2)
1985 {
1986 if (*pat != '^') /* there's no '^', no need to try again */
1987 break;
1988 ++pat; /* skip the '^' */
1989 }
1990 prog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
1991 if (prog == NULL)
1992 return FAIL;
1993
1994 /*
1995 * round == 1: Count the matches.
1996 * round == 2: Build the array to keep the matches.
1997 */
1998 for (round = 1; round <= 2; ++round)
1999 {
2000 count = 0;
2001 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2002 {
2003 if (!buf->b_p_bl) /* skip unlisted buffers */
2004 continue;
2005 p = buflist_match(prog, buf);
2006 if (p != NULL)
2007 {
2008 if (round == 1)
2009 ++count;
2010 else
2011 {
2012 if (options & WILD_HOME_REPLACE)
2013 p = home_replace_save(buf, p);
2014 else
2015 p = vim_strsave(p);
2016 (*file)[count++] = p;
2017 }
2018 }
2019 }
2020 if (count == 0) /* no match found, break here */
2021 break;
2022 if (round == 1)
2023 {
2024 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2025 if (*file == NULL)
2026 {
2027 vim_free(prog);
2028 return FAIL;
2029 }
2030 }
2031 }
2032 vim_free(prog);
2033 if (count) /* match(es) found, break here */
2034 break;
2035 }
2036
2037 *num_file = count;
2038 return (count == 0 ? FAIL : OK);
2039 }
2040
2041 #endif /* FEAT_CMDL_COMPL */
2042
2043 #ifdef HAVE_BUFLIST_MATCH
2044 /*
2045 * Check for a match on the file name for buffer "buf" with regprog "prog".
2046 */
2047 static char_u *
2048 buflist_match(prog, buf)
2049 regprog_T *prog;
2050 buf_T *buf;
2051 {
2052 char_u *match;
2053
2054 /* First try the short file name, then the long file name. */
2055 match = fname_match(prog, buf->b_sfname);
2056 if (match == NULL)
2057 match = fname_match(prog, buf->b_ffname);
2058
2059 return match;
2060 }
2061
2062 /*
2063 * Try matching the regexp in "prog" with file name "name".
2064 * Return "name" when there is a match, NULL when not.
2065 */
2066 static char_u *
2067 fname_match(prog, name)
2068 regprog_T *prog;
2069 char_u *name;
2070 {
2071 char_u *match = NULL;
2072 char_u *p;
2073 regmatch_T regmatch;
2074
2075 if (name != NULL)
2076 {
2077 regmatch.regprog = prog;
2078 #ifdef CASE_INSENSITIVE_FILENAME
2079 regmatch.rm_ic = TRUE; /* Always ignore case */
2080 #else
2081 regmatch.rm_ic = FALSE; /* Never ignore case */
2082 #endif
2083
2084 if (vim_regexec(&regmatch, name, (colnr_T)0))
2085 match = name;
2086 else
2087 {
2088 /* Replace $(HOME) with '~' and try matching again. */
2089 p = home_replace_save(NULL, name);
2090 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2091 match = name;
2092 vim_free(p);
2093 }
2094 }
2095
2096 return match;
2097 }
2098 #endif
2099
2100 /*
2101 * find file in buffer list by number
2102 */
2103 buf_T *
2104 buflist_findnr(nr)
2105 int nr;
2106 {
2107 buf_T *buf;
2108
2109 if (nr == 0)
2110 nr = curwin->w_alt_fnum;
2111 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2112 if (buf->b_fnum == nr)
2113 return (buf);
2114 return NULL;
2115 }
2116
2117 /*
2118 * Get name of file 'n' in the buffer list.
2119 * When the file has no name an empty string is returned.
2120 * home_replace() is used to shorten the file name (used for marks).
2121 * Returns a pointer to allocated memory, of NULL when failed.
2122 */
2123 char_u *
2124 buflist_nr2name(n, fullname, helptail)
2125 int n;
2126 int fullname;
2127 int helptail; /* for help buffers return tail only */
2128 {
2129 buf_T *buf;
2130
2131 buf = buflist_findnr(n);
2132 if (buf == NULL)
2133 return NULL;
2134 return home_replace_save(helptail ? buf : NULL,
2135 fullname ? buf->b_ffname : buf->b_fname);
2136 }
2137
2138 /*
2139 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2140 * When "copy_options" is TRUE save the local window option values.
2141 * When "lnum" is 0 only do the options.
2142 */
2143 static void
2144 buflist_setfpos(buf, win, lnum, col, copy_options)
2145 buf_T *buf;
2146 win_T *win;
2147 linenr_T lnum;
2148 colnr_T col;
2149 int copy_options;
2150 {
2151 wininfo_T *wip;
2152
2153 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2154 if (wip->wi_win == win)
2155 break;
2156 if (wip == NULL)
2157 {
2158 /* allocate a new entry */
2159 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2160 if (wip == NULL)
2161 return;
2162 wip->wi_win = win;
2163 if (lnum == 0) /* set lnum even when it's 0 */
2164 lnum = 1;
2165 }
2166 else
2167 {
2168 /* remove the entry from the list */
2169 if (wip->wi_prev)
2170 wip->wi_prev->wi_next = wip->wi_next;
2171 else
2172 buf->b_wininfo = wip->wi_next;
2173 if (wip->wi_next)
2174 wip->wi_next->wi_prev = wip->wi_prev;
2175 if (copy_options && wip->wi_optset)
2176 {
2177 clear_winopt(&wip->wi_opt);
2178 #ifdef FEAT_FOLDING
2179 deleteFoldRecurse(&wip->wi_folds);
2180 #endif
2181 }
2182 }
2183 if (lnum != 0)
2184 {
2185 wip->wi_fpos.lnum = lnum;
2186 wip->wi_fpos.col = col;
2187 }
2188 if (copy_options)
2189 {
2190 /* Save the window-specific option values. */
2191 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2192 #ifdef FEAT_FOLDING
2193 wip->wi_fold_manual = win->w_fold_manual;
2194 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2195 #endif
2196 wip->wi_optset = TRUE;
2197 }
2198
2199 /* insert the entry in front of the list */
2200 wip->wi_next = buf->b_wininfo;
2201 buf->b_wininfo = wip;
2202 wip->wi_prev = NULL;
2203 if (wip->wi_next)
2204 wip->wi_next->wi_prev = wip;
2205
2206 return;
2207 }
2208
2209 /*
2210 * Find info for the current window in buffer "buf".
2211 * If not found, return the info for the most recently used window.
2212 * Returns NULL when there isn't any info.
2213 */
2214 static wininfo_T *
2215 find_wininfo(buf)
2216 buf_T *buf;
2217 {
2218 wininfo_T *wip;
2219
2220 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2221 if (wip->wi_win == curwin)
2222 break;
2223 if (wip == NULL) /* if no fpos for curwin, use the first in the list */
2224 wip = buf->b_wininfo;
2225 return wip;
2226 }
2227
2228 /*
2229 * Reset the local window options to the values last used in this window.
2230 * If the buffer wasn't used in this window before, use the values from
2231 * the most recently used window. If the values were never set, use the
2232 * global values for the window.
2233 */
2234 void
2235 get_winopts(buf)
2236 buf_T *buf;
2237 {
2238 wininfo_T *wip;
2239
2240 clear_winopt(&curwin->w_onebuf_opt);
2241 #ifdef FEAT_FOLDING
2242 clearFolding(curwin);
2243 #endif
2244
2245 wip = find_wininfo(buf);
2246 if (wip != NULL && wip->wi_optset)
2247 {
2248 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2249 #ifdef FEAT_FOLDING
2250 curwin->w_fold_manual = wip->wi_fold_manual;
2251 curwin->w_foldinvalid = TRUE;
2252 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2253 #endif
2254 }
2255 else
2256 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2257
2258 #ifdef FEAT_FOLDING
2259 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2260 if (p_fdls >= 0)
2261 curwin->w_p_fdl = p_fdls;
2262 #endif
2263 }
2264
2265 /*
2266 * Find the position (lnum and col) for the buffer 'buf' for the current
2267 * window.
2268 * Returns a pointer to no_position if no position is found.
2269 */
2270 pos_T *
2271 buflist_findfpos(buf)
2272 buf_T *buf;
2273 {
2274 wininfo_T *wip;
2275 static pos_T no_position = {1, 0};
2276
2277 wip = find_wininfo(buf);
2278 if (wip != NULL)
2279 return &(wip->wi_fpos);
2280 else
2281 return &no_position;
2282 }
2283
2284 /*
2285 * Find the lnum for the buffer 'buf' for the current window.
2286 */
2287 linenr_T
2288 buflist_findlnum(buf)
2289 buf_T *buf;
2290 {
2291 return buflist_findfpos(buf)->lnum;
2292 }
2293
2294 #if defined(FEAT_LISTCMDS) || defined(PROTO)
2295 /*
2296 * List all know file names (for :files and :buffers command).
2297 */
2298 /*ARGSUSED*/
2299 void
2300 buflist_list(eap)
2301 exarg_T *eap;
2302 {
2303 buf_T *buf;
2304 int len;
2305 int i;
2306
2307 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2308 {
2309 /* skip unlisted buffers, unless ! was used */
2310 if (!buf->b_p_bl && !eap->forceit)
2311 continue;
2312 msg_putchar('\n');
2313 if (buf_spname(buf) != NULL)
2314 STRCPY(NameBuff, buf_spname(buf));
2315 else
2316 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2317
2318 sprintf((char *)IObuff, "%3d%c%c%c%c%c \"",
2319 buf->b_fnum,
2320 buf->b_p_bl ? ' ' : 'u',
2321 buf == curbuf ? '%' :
2322 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2323 buf->b_ml.ml_mfp == NULL ? ' ' :
2324 (buf->b_nwindows == 0 ? 'h' : 'a'),
2325 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2326 (buf->b_flags & BF_READERR) ? 'x'
2327 : (bufIsChanged(buf) ? '+' : ' ')
2328 );
2329
2330 len = (int)STRLEN(IObuff);
2331 STRNCPY(IObuff + len, NameBuff, IOSIZE - 20 - len);
2332 IObuff[IOSIZE - 20 - len] = NUL; /* make sure it's terminated */
2333
2334 len = (int)STRLEN(IObuff);
2335 IObuff[len++] = '"';
2336
2337 /* put "line 999" in column 40 or after the file name */
2338 IObuff[len] = NUL;
2339 i = 40 - vim_strsize(IObuff);
2340 do
2341 {
2342 IObuff[len++] = ' ';
2343 } while (--i > 0 && len < IOSIZE - 18);
2344 sprintf((char *)IObuff + len, _("line %ld"),
2345 buf == curbuf ? curwin->w_cursor.lnum :
2346 (long)buflist_findlnum(buf));
2347 msg_outtrans(IObuff);
2348 out_flush(); /* output one line at a time */
2349 ui_breakcheck();
2350 }
2351 }
2352 #endif
2353
2354 /*
2355 * Get file name and line number for file 'fnum'.
2356 * Used by DoOneCmd() for translating '%' and '#'.
2357 * Used by insert_reg() and cmdline_paste() for '#' register.
2358 * Return FAIL if not found, OK for success.
2359 */
2360 int
2361 buflist_name_nr(fnum, fname, lnum)
2362 int fnum;
2363 char_u **fname;
2364 linenr_T *lnum;
2365 {
2366 buf_T *buf;
2367
2368 buf = buflist_findnr(fnum);
2369 if (buf == NULL || buf->b_fname == NULL)
2370 return FAIL;
2371
2372 *fname = buf->b_fname;
2373 *lnum = buflist_findlnum(buf);
2374
2375 return OK;
2376 }
2377
2378 /*
2379 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2380 * The file name with the full path is also remembered, for when :cd is used.
2381 * Returns FAIL for failure (file name already in use by other buffer)
2382 * OK otherwise.
2383 */
2384 int
2385 setfname(buf, ffname, sfname, message)
2386 buf_T *buf;
2387 char_u *ffname, *sfname;
2388 int message;
2389 {
2390 buf_T *obuf;
2391 #ifdef UNIX
2392 struct stat st;
2393 #endif
2394
2395 if (ffname == NULL || *ffname == NUL)
2396 {
2397 /* Removing the name. */
2398 vim_free(buf->b_ffname);
2399 vim_free(buf->b_sfname);
2400 buf->b_ffname = NULL;
2401 buf->b_sfname = NULL;
2402 #ifdef UNIX
2403 st.st_dev = (dev_T)-1;
2404 #endif
2405 }
2406 else
2407 {
2408 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2409 if (ffname == NULL) /* out of memory */
2410 return FAIL;
2411
2412 /*
2413 * if the file name is already used in another buffer:
2414 * - if the buffer is loaded, fail
2415 * - if the buffer is not loaded, delete it from the list
2416 */
2417 #ifdef UNIX
2418 if (mch_stat((char *)ffname, &st) < 0)
2419 st.st_dev = (dev_T)-1;
2420 obuf = buflist_findname_stat(ffname, &st);
2421 #else
2422 obuf = buflist_findname(ffname);
2423 #endif
2424 if (obuf != NULL && obuf != buf)
2425 {
2426 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2427 {
2428 if (message)
2429 EMSG(_("E95: Buffer with this name already exists"));
2430 vim_free(ffname);
2431 return FAIL;
2432 }
2433 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2434 }
2435 sfname = vim_strsave(sfname);
2436 if (ffname == NULL || sfname == NULL)
2437 {
2438 vim_free(sfname);
2439 vim_free(ffname);
2440 return FAIL;
2441 }
2442 #ifdef USE_FNAME_CASE
2443 # ifdef USE_LONG_FNAME
2444 if (USE_LONG_FNAME)
2445 # endif
2446 fname_case(sfname, 0); /* set correct case for short file name */
2447 #endif
2448 vim_free(buf->b_ffname);
2449 vim_free(buf->b_sfname);
2450 buf->b_ffname = ffname;
2451 buf->b_sfname = sfname;
2452 }
2453 buf->b_fname = buf->b_sfname;
2454 #ifdef UNIX
2455 if (st.st_dev == (dev_T)-1)
2456 buf->b_dev = -1;
2457 else
2458 {
2459 buf->b_dev = st.st_dev;
2460 buf->b_ino = st.st_ino;
2461 }
2462 #endif
2463
2464 #ifndef SHORT_FNAME
2465 buf->b_shortname = FALSE;
2466 #endif
2467
2468 buf_name_changed(buf);
2469 return OK;
2470 }
2471
2472 /*
2473 * Take care of what needs to be done when the name of buffer "buf" has
2474 * changed.
2475 */
2476 void
2477 buf_name_changed(buf)
2478 buf_T *buf;
2479 {
2480 /*
2481 * If the file name changed, also change the name of the swapfile
2482 */
2483 if (buf->b_ml.ml_mfp != NULL)
2484 ml_setname(buf);
2485
2486 if (curwin->w_buffer == buf)
2487 check_arg_idx(curwin); /* check file name for arg list */
2488 #ifdef FEAT_TITLE
2489 maketitle(); /* set window title */
2490 #endif
2491 #ifdef FEAT_WINDOWS
2492 status_redraw_all(); /* status lines need to be redrawn */
2493 #endif
2494 fmarks_check_names(buf); /* check named file marks */
2495 ml_timestamp(buf); /* reset timestamp */
2496 }
2497
2498 /*
2499 * set alternate file name for current window
2500 *
2501 * Used by do_one_cmd(), do_write() and do_ecmd().
2502 * Return the buffer.
2503 */
2504 buf_T *
2505 setaltfname(ffname, sfname, lnum)
2506 char_u *ffname;
2507 char_u *sfname;
2508 linenr_T lnum;
2509 {
2510 buf_T *buf;
2511
2512 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2513 buf = buflist_new(ffname, sfname, lnum, 0);
2514 if (buf != NULL)
2515 curwin->w_alt_fnum = buf->b_fnum;
2516 return buf;
2517 }
2518
2519 /*
2520 * Get alternate file name for current window.
2521 * Return NULL if there isn't any, and give error message if requested.
2522 */
2523 char_u *
2524 getaltfname(errmsg)
2525 int errmsg; /* give error message */
2526 {
2527 char_u *fname;
2528 linenr_T dummy;
2529
2530 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2531 {
2532 if (errmsg)
2533 EMSG(_(e_noalt));
2534 return NULL;
2535 }
2536 return fname;
2537 }
2538
2539 /*
2540 * Add a file name to the buflist and return its number.
2541 * Uses same flags as buflist_new(), except BLN_DUMMY.
2542 *
2543 * used by qf_init(), main() and doarglist()
2544 */
2545 int
2546 buflist_add(fname, flags)
2547 char_u *fname;
2548 int flags;
2549 {
2550 buf_T *buf;
2551
2552 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2553 if (buf != NULL)
2554 return buf->b_fnum;
2555 return 0;
2556 }
2557
2558 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2559 /*
2560 * Adjust slashes in file names. Called after 'shellslash' was set.
2561 */
2562 void
2563 buflist_slash_adjust()
2564 {
2565 buf_T *bp;
2566
2567 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2568 {
2569 if (bp->b_ffname != NULL)
2570 slash_adjust(bp->b_ffname);
2571 if (bp->b_sfname != NULL)
2572 slash_adjust(bp->b_sfname);
2573 }
2574 }
2575 #endif
2576
2577 /*
2578 * Set alternate cursor position for current window.
2579 * Also save the local window option values.
2580 */
2581 void
2582 buflist_altfpos()
2583 {
2584 buflist_setfpos(curbuf, curwin, curwin->w_cursor.lnum,
2585 curwin->w_cursor.col, TRUE);
2586 }
2587
2588 /*
2589 * Return TRUE if 'ffname' is not the same file as current file.
2590 * Fname must have a full path (expanded by mch_FullName()).
2591 */
2592 int
2593 otherfile(ffname)
2594 char_u *ffname;
2595 {
2596 return otherfile_buf(curbuf, ffname
2597 #ifdef UNIX
2598 , NULL
2599 #endif
2600 );
2601 }
2602
2603 static int
2604 otherfile_buf(buf, ffname
2605 #ifdef UNIX
2606 , stp
2607 #endif
2608 )
2609 buf_T *buf;
2610 char_u *ffname;
2611 #ifdef UNIX
2612 struct stat *stp;
2613 #endif
2614 {
2615 /* no name is different */
2616 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2617 return TRUE;
2618 if (fnamecmp(ffname, buf->b_ffname) == 0)
2619 return FALSE;
2620 #ifdef UNIX
2621 {
2622 struct stat st;
2623
2624 /* If no struct stat given, get it now */
2625 if (stp == NULL)
2626 {
2627 if (buf->b_dev < 0 || mch_stat((char *)ffname, &st) < 0)
2628 st.st_dev = (dev_T)-1;
2629 stp = &st;
2630 }
2631 /* Use dev/ino to check if the files are the same, even when the names
2632 * are different (possible with links). Still need to compare the
2633 * name above, for when the file doesn't exist yet.
2634 * Problem: The dev/ino changes when a file is deleted (and created
2635 * again) and remains the same when renamed/moved. We don't want to
2636 * mch_stat() each buffer each time, that would be too slow. Get the
2637 * dev/ino again when they appear to match, but not when they appear
2638 * to be different: Could skip a buffer when it's actually the same
2639 * file. */
2640 if (buf_same_ino(buf, stp))
2641 {
2642 buf_setino(buf);
2643 if (buf_same_ino(buf, stp))
2644 return FALSE;
2645 }
2646 }
2647 #endif
2648 return TRUE;
2649 }
2650
2651 #if defined(UNIX) || defined(PROTO)
2652 /*
2653 * Set inode and device number for a buffer.
2654 * Must always be called when b_fname is changed!.
2655 */
2656 void
2657 buf_setino(buf)
2658 buf_T *buf;
2659 {
2660 struct stat st;
2661
2662 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2663 {
2664 buf->b_dev = st.st_dev;
2665 buf->b_ino = st.st_ino;
2666 }
2667 else
2668 buf->b_dev = -1;
2669 }
2670
2671 /*
2672 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2673 */
2674 static int
2675 buf_same_ino(buf, stp)
2676 buf_T *buf;
2677 struct stat *stp;
2678 {
2679 return (buf->b_dev >= 0
2680 && stp->st_dev == buf->b_dev
2681 && stp->st_ino == buf->b_ino);
2682 }
2683 #endif
2684
2685 void
2686 fileinfo(fullname, shorthelp, dont_truncate)
2687 int fullname;
2688 int shorthelp;
2689 int dont_truncate;
2690 {
2691 char_u *name;
2692 int n;
2693 char_u *p;
2694 char_u *buffer;
2695
2696 buffer = alloc(IOSIZE);
2697 if (buffer == NULL)
2698 return;
2699
2700 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2701 {
2702 sprintf((char *)buffer, "buf %d: ", curbuf->b_fnum);
2703 p = buffer + STRLEN(buffer);
2704 }
2705 else
2706 p = buffer;
2707
2708 *p++ = '"';
2709 if (buf_spname(curbuf) != NULL)
2710 STRCPY(p, buf_spname(curbuf));
2711 else
2712 {
2713 if (!fullname && curbuf->b_fname != NULL)
2714 name = curbuf->b_fname;
2715 else
2716 name = curbuf->b_ffname;
2717 home_replace(shorthelp ? curbuf : NULL, name, p,
2718 (int)(IOSIZE - (p - buffer)), TRUE);
2719 }
2720
2721 sprintf((char *)buffer + STRLEN(buffer),
2722 "\"%s%s%s%s%s%s",
2723 curbufIsChanged() ? (shortmess(SHM_MOD)
2724 ? " [+]" : _(" [Modified]")) : " ",
2725 (curbuf->b_flags & BF_NOTEDITED)
2726 #ifdef FEAT_QUICKFIX
2727 && !bt_dontwrite(curbuf)
2728 #endif
2729 ? _("[Not edited]") : "",
2730 (curbuf->b_flags & BF_NEW)
2731 #ifdef FEAT_QUICKFIX
2732 && !bt_dontwrite(curbuf)
2733 #endif
2734 ? _("[New file]") : "",
2735 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
2736 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
2737 : _("[readonly]")) : "",
2738 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
2739 || curbuf->b_p_ro) ?
2740 " " : "");
2741 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
2742 * causes an overflow, thus for large numbers divide instead. */
2743 if (curwin->w_cursor.lnum > 1000000L)
2744 n = (int)(((long)curwin->w_cursor.lnum) /
2745 ((long)curbuf->b_ml.ml_line_count / 100L));
2746 else
2747 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
2748 (long)curbuf->b_ml.ml_line_count);
2749 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2750 {
2751 STRCPY(buffer + STRLEN(buffer), _(no_lines_msg));
2752 }
2753 #ifdef FEAT_CMDL_INFO
2754 else if (p_ru)
2755 {
2756 /* Current line and column are already on the screen -- webb */
2757 if (curbuf->b_ml.ml_line_count == 1)
2758 sprintf((char *)buffer + STRLEN(buffer), _("1 line --%d%%--"), n);
2759 else
2760 sprintf((char *)buffer + STRLEN(buffer), _("%ld lines --%d%%--"),
2761 (long)curbuf->b_ml.ml_line_count, n);
2762 }
2763 #endif
2764 else
2765 {
2766 sprintf((char *)buffer + STRLEN(buffer),
2767 _("line %ld of %ld --%d%%-- col "),
2768 (long)curwin->w_cursor.lnum,
2769 (long)curbuf->b_ml.ml_line_count,
2770 n);
2771 validate_virtcol();
2772 col_print(buffer + STRLEN(buffer),
2773 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
2774 }
2775
2776 (void)append_arg_number(curwin, buffer, !shortmess(SHM_FILE), IOSIZE);
2777
2778 if (dont_truncate)
2779 {
2780 /* Temporarily set msg_scroll to avoid the message being truncated.
2781 * First call msg_start() to get the message in the right place. */
2782 msg_start();
2783 n = msg_scroll;
2784 msg_scroll = TRUE;
2785 msg(buffer);
2786 msg_scroll = n;
2787 }
2788 else
2789 {
2790 p = msg_trunc_attr(buffer, FALSE, 0);
2791 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
2792 {
2793 /* Need to repeat the message after redrawing when:
2794 * - When restart_edit is set (otherwise there will be a delay
2795 * before redrawing).
2796 * - When the screen was scrolled but there is no wait-return
2797 * prompt. */
2798 set_keep_msg(p);
2799 keep_msg_attr = 0;
2800 }
2801 }
2802
2803 vim_free(buffer);
2804 }
2805
2806 void
2807 col_print(buf, col, vcol)
2808 char_u *buf;
2809 int col;
2810 int vcol;
2811 {
2812 if (col == vcol)
2813 sprintf((char *)buf, "%d", col);
2814 else
2815 sprintf((char *)buf, "%d-%d", col, vcol);
2816 }
2817
2818 #if defined(FEAT_TITLE) || defined(PROTO)
2819 /*
2820 * put file name in title bar of window and in icon title
2821 */
2822
2823 static char_u *lasttitle = NULL;
2824 static char_u *lasticon = NULL;
2825
2826 void
2827 maketitle()
2828 {
2829 char_u *p;
2830 char_u *t_str = NULL;
2831 char_u *i_name;
2832 char_u *i_str = NULL;
2833 int maxlen = 0;
2834 int len;
2835 int mustset;
2836 char_u buf[IOSIZE];
2837 int off;
2838
2839 if (!redrawing())
2840 {
2841 /* Postpone updating the title when 'lazyredraw' is set. */
2842 need_maketitle = TRUE;
2843 return;
2844 }
2845
2846 need_maketitle = FALSE;
2847 if (!p_title && !p_icon)
2848 return;
2849
2850 if (p_title)
2851 {
2852 if (p_titlelen > 0)
2853 {
2854 maxlen = p_titlelen * Columns / 100;
2855 if (maxlen < 10)
2856 maxlen = 10;
2857 }
2858
2859 t_str = buf;
2860 if (*p_titlestring != NUL)
2861 {
2862 #ifdef FEAT_STL_OPT
2863 if (stl_syntax & STL_IN_TITLE)
2864 build_stl_str_hl(curwin, t_str, sizeof(buf),
2865 p_titlestring, 0, maxlen, NULL);
2866 else
2867 #endif
2868 t_str = p_titlestring;
2869 }
2870 else
2871 {
2872 /* format: "fname + (path) (1 of 2) - VIM" */
2873
2874 if (curbuf->b_fname == NULL)
2875 STRCPY(buf, _("[No file]"));
2876 else
2877 {
2878 p = transstr(gettail(curbuf->b_fname));
2879 STRNCPY(buf, p, IOSIZE - 100);
2880 vim_free(p);
2881 buf[IOSIZE - 100] = NUL; /* in case it was too long */
2882 }
2883
2884 switch (bufIsChanged(curbuf)
2885 + (curbuf->b_p_ro * 2)
2886 + (!curbuf->b_p_ma * 4))
2887 {
2888 case 1: STRCAT(buf, " +"); break;
2889 case 2: STRCAT(buf, " ="); break;
2890 case 3: STRCAT(buf, " =+"); break;
2891 case 4:
2892 case 6: STRCAT(buf, " -"); break;
2893 case 5:
2894 case 7: STRCAT(buf, " -+"); break;
2895 }
2896
2897 if (curbuf->b_fname != NULL)
2898 {
2899 /* Get path of file, replace home dir with ~ */
2900 off = (int)STRLEN(buf);
2901 buf[off++] = ' ';
2902 buf[off++] = '(';
2903 home_replace(curbuf, curbuf->b_ffname,
2904 buf + off, IOSIZE - off, TRUE);
2905 #ifdef BACKSLASH_IN_FILENAME
2906 /* avoid "c:/name" to be reduced to "c" */
2907 if (isalpha(buf[off]) && buf[off + 1] == ':')
2908 off += 2;
2909 #endif
2910 /* remove the file name */
2911 p = gettail(buf + off);
2912 if (p == buf + off)
2913 {
2914 /* must be a help buffer */
2915 STRCPY(buf + off, _("help"));
2916 }
2917 else
2918 {
2919 while (p > buf + off + 1 && vim_ispathsep(p[-1]))
2920 --p;
2921 #ifdef VMS
2922 /* path separator is part of the path */
2923 ++p;
2924 #endif
2925 *p = NUL;
2926 }
2927 /* translate unprintable chars */
2928 p = transstr(buf + off);
2929 STRNCPY(buf + off, p, IOSIZE - off);
2930 vim_free(p);
2931 buf[IOSIZE - 1] = NUL; /* in case it was too long */
2932 STRCAT(buf, ")");
2933 }
2934
2935 append_arg_number(curwin, buf, FALSE, IOSIZE);
2936
2937 #if defined(FEAT_CLIENTSERVER)
2938 if (serverName != NULL)
2939 {
2940 STRCAT(buf, " - ");
2941 STRCAT(buf, serverName);
2942 }
2943 else
2944 #endif
2945 STRCAT(buf, " - VIM");
2946
2947 if (maxlen > 0)
2948 {
2949 /* make it shorter by removing a bit in the middle */
2950 len = vim_strsize(buf);
2951 if (len > maxlen)
2952 trunc_string(buf, buf, maxlen);
2953 }
2954 }
2955 }
2956 mustset = ti_change(t_str, &lasttitle);
2957
2958 if (p_icon)
2959 {
2960 i_str = buf;
2961 if (*p_iconstring != NUL)
2962 {
2963 #ifdef FEAT_STL_OPT
2964 if (stl_syntax & STL_IN_ICON)
2965 build_stl_str_hl(curwin, i_str, sizeof(buf),
2966 p_iconstring, 0, 0, NULL);
2967 else
2968 #endif
2969 i_str = p_iconstring;
2970 }
2971 else
2972 {
2973 if (buf_spname(curbuf) != NULL)
2974 i_name = (char_u *)buf_spname(curbuf);
2975 else /* use file name only in icon */
2976 i_name = gettail(curbuf->b_ffname);
2977 *i_str = NUL;
2978 /* Truncate name at 100 bytes. */
2979 len = STRLEN(i_name);
2980 if (len > 100)
2981 {
2982 len -= 100;
2983 #ifdef FEAT_MBYTE
2984 if (has_mbyte)
2985 len += (*mb_tail_off)(i_name, i_name + len) + 1;
2986 #endif
2987 i_name += len;
2988 }
2989 STRCPY(i_str, i_name);
2990 trans_characters(i_str, IOSIZE);
2991 }
2992 }
2993
2994 mustset |= ti_change(i_str, &lasticon);
2995
2996 if (mustset)
2997 resettitle();
2998 }
2999
3000 /*
3001 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3002 * from "str" if it does.
3003 * Return TRUE when "*last" changed.
3004 */
3005 static int
3006 ti_change(str, last)
3007 char_u *str;
3008 char_u **last;
3009 {
3010 if ((str == NULL) != (*last == NULL)
3011 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3012 {
3013 vim_free(*last);
3014 if (str == NULL)
3015 *last = NULL;
3016 else
3017 *last = vim_strsave(str);
3018 return TRUE;
3019 }
3020 return FALSE;
3021 }
3022
3023 /*
3024 * Put current window title back (used after calling a shell)
3025 */
3026 void
3027 resettitle()
3028 {
3029 mch_settitle(lasttitle, lasticon);
3030 }
3031 #endif /* FEAT_TITLE */
3032
3033 #if defined(FEAT_STL_OPT) || defined(PROTO)
3034 /*
3035 * Build a string from the status line items in fmt.
3036 * Return length of string in screen cells.
3037 *
3038 * Items are drawn interspersed with the text that surrounds it
3039 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3040 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3041 *
3042 * If maxwidth is not zero, the string will be filled at any middle marker
3043 * or truncated if too long, fillchar is used for all whitespace.
3044 */
3045 int
3046 build_stl_str_hl(wp, out, outlen, fmt, fillchar, maxwidth, hl)
3047 win_T *wp;
3048 char_u *out; /* buffer to write into */
3049 size_t outlen; /* length of out[] */
3050 char_u *fmt;
3051 int fillchar;
3052 int maxwidth;
3053 struct stl_hlrec *hl;
3054 {
3055 char_u *p;
3056 char_u *s;
3057 char_u *t;
3058 char_u *linecont;
3059 #ifdef FEAT_EVAL
3060 win_T *o_curwin;
3061 buf_T *o_curbuf;
3062 #endif
3063 int empty_line;
3064 colnr_T virtcol;
3065 long l;
3066 long n;
3067 int prevchar_isflag;
3068 int prevchar_isitem;
3069 int itemisflag;
3070 int fillable;
3071 char_u *str;
3072 long num;
3073 int width;
3074 int itemcnt;
3075 int curitem;
3076 int groupitem[STL_MAX_ITEM];
3077 int groupdepth;
3078 struct stl_item
3079 {
3080 char_u *start;
3081 int minwid;
3082 int maxwid;
3083 enum
3084 {
3085 Normal,
3086 Empty,
3087 Group,
3088 Middle,
3089 Highlight,
3090 Trunc
3091 } type;
3092 } item[STL_MAX_ITEM];
3093 int minwid;
3094 int maxwid;
3095 int zeropad;
3096 char_u base;
3097 char_u opt;
3098 #define TMPLEN 70
3099 char_u tmp[TMPLEN];
3100
3101 if (fillchar == 0)
3102 fillchar = ' ';
3103 /*
3104 * Get line & check if empty (cursorpos will show "0-1").
3105 * If inversion is possible we use it. Else '=' characters are used.
3106 */
3107 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3108 empty_line = (*linecont == NUL);
3109
3110 groupdepth = 0;
3111 p = out;
3112 curitem = 0;
3113 prevchar_isflag = TRUE;
3114 prevchar_isitem = FALSE;
3115 for (s = fmt; *s;)
3116 {
3117 if (*s != NUL && *s != '%')
3118 prevchar_isflag = prevchar_isitem = FALSE;
3119
3120 /*
3121 * Handle up to the next '%' or the end.
3122 */
3123 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3124 *p++ = *s++;
3125 if (*s == NUL || p + 1 >= out + outlen)
3126 break;
3127
3128 /*
3129 * Handle one '%' item.
3130 */
3131 s++;
3132 if (*s == '%')
3133 {
3134 if (p + 1 >= out + outlen)
3135 break;
3136 *p++ = *s++;
3137 prevchar_isflag = prevchar_isitem = FALSE;
3138 continue;
3139 }
3140 if (*s == STL_MIDDLEMARK)
3141 {
3142 s++;
3143 if (groupdepth > 0)
3144 continue;
3145 item[curitem].type = Middle;
3146 item[curitem++].start = p;
3147 continue;
3148 }
3149 if (*s == STL_TRUNCMARK)
3150 {
3151 s++;
3152 item[curitem].type = Trunc;
3153 item[curitem++].start = p;
3154 continue;
3155 }
3156 if (*s == ')')
3157 {
3158 s++;
3159 if (groupdepth < 1)
3160 continue;
3161 groupdepth--;
3162
3163 t = item[groupitem[groupdepth]].start;
3164 *p = NUL;
3165 l = vim_strsize(t);
3166 if (curitem > groupitem[groupdepth] + 1
3167 && item[groupitem[groupdepth]].minwid == 0)
3168 {
3169 /* remove group if all items are empty */
3170 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3171 if (item[n].type == Normal)
3172 break;
3173 if (n == curitem)
3174 {
3175 p = t;
3176 l = 0;
3177 }
3178 }
3179 if (l > item[groupitem[groupdepth]].maxwid)
3180 {
3181 /* truncate, remove n bytes of text at the start */
3182 #ifdef FEAT_MBYTE
3183 if (has_mbyte)
3184 {
3185 /* Find the first character that should be included. */
3186 n = 0;
3187 while (l >= item[groupitem[groupdepth]].maxwid)
3188 {
3189 l -= ptr2cells(t + n);
3190 n += (*mb_ptr2len_check)(t + n);
3191 }
3192 }
3193 else
3194 #endif
3195 n = (p - t) - item[groupitem[groupdepth]].maxwid + 1;
3196
3197 *t = '<';
3198 mch_memmove(t + 1, t + n, p - (t + n));
3199 p = p - n + 1;
3200 #ifdef FEAT_MBYTE
3201 /* Fill up space left over by half a double-wide char. */
3202 while (++l < item[groupitem[groupdepth]].minwid)
3203 *p++ = fillchar;
3204 #endif
3205
3206 /* correct the start of the items for the truncation */
3207 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3208 {
3209 item[l].start -= n;
3210 if (item[l].start < t)
3211 item[l].start = t;
3212 }
3213 }
3214 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3215 {
3216 /* fill */
3217 n = item[groupitem[groupdepth]].minwid;
3218 if (n < 0)
3219 {
3220 /* fill by appending characters */
3221 n = 0 - n;
3222 while (l++ < n && p + 1 < out + outlen)
3223 *p++ = fillchar;
3224 }
3225 else
3226 {
3227 /* fill by inserting characters */
3228 mch_memmove(t + n - l, t, p - t);
3229 l = n - l;
3230 if (p + l >= out + outlen)
3231 l = (out + outlen) - p - 1;
3232 p += l;
3233 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3234 item[n].start += l;
3235 for ( ; l > 0; l--)
3236 *t++ = fillchar;
3237 }
3238 }
3239 continue;
3240 }
3241 minwid = 0;
3242 maxwid = 9999;
3243 zeropad = FALSE;
3244 l = 1;
3245 if (*s == '0')
3246 {
3247 s++;
3248 zeropad = TRUE;
3249 }
3250 if (*s == '-')
3251 {
3252 s++;
3253 l = -1;
3254 }
3255 if (VIM_ISDIGIT(*s))
3256 {
3257 minwid = (int)getdigits(&s);
3258 if (minwid < 0) /* overflow */
3259 minwid = 0;
3260 }
3261 if (*s == STL_HIGHLIGHT)
3262 {
3263 item[curitem].type = Highlight;
3264 item[curitem].start = p;
3265 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3266 s++;
3267 curitem++;
3268 continue;
3269 }
3270 if (*s == '.')
3271 {
3272 s++;
3273 if (VIM_ISDIGIT(*s))
3274 {
3275 maxwid = (int)getdigits(&s);
3276 if (maxwid <= 0) /* overflow */
3277 maxwid = 50;
3278 }
3279 }
3280 minwid = (minwid > 50 ? 50 : minwid) * l;
3281 if (*s == '(')
3282 {
3283 groupitem[groupdepth++] = curitem;
3284 item[curitem].type = Group;
3285 item[curitem].start = p;
3286 item[curitem].minwid = minwid;
3287 item[curitem].maxwid = maxwid;
3288 s++;
3289 curitem++;
3290 continue;
3291 }
3292 if (vim_strchr(STL_ALL, *s) == NULL)
3293 {
3294 s++;
3295 continue;
3296 }
3297 opt = *s++;
3298
3299 /* OK - now for the real work */
3300 base = 'D';
3301 itemisflag = FALSE;
3302 fillable = TRUE;
3303 num = -1;
3304 str = NULL;
3305 switch (opt)
3306 {
3307 case STL_FILEPATH:
3308 case STL_FULLPATH:
3309 case STL_FILENAME:
3310 fillable = FALSE; /* don't change ' ' to fillchar */
3311 if (buf_spname(wp->w_buffer) != NULL)
3312 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3313 else
3314 {
3315 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
3316 : wp->w_buffer->b_fname;
3317 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3318 }
3319 trans_characters(NameBuff, MAXPATHL);
3320 if (opt != STL_FILENAME)
3321 str = NameBuff;
3322 else
3323 str = gettail(NameBuff);
3324 break;
3325
3326 case STL_VIM_EXPR: /* '{' */
3327 itemisflag = TRUE;
3328 t = p;
3329 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3330 *p++ = *s++;
3331 if (*s != '}') /* missing '}' or out of space */
3332 break;
3333 s++;
3334 *p = 0;
3335 p = t;
3336
3337 #ifdef FEAT_EVAL
3338 sprintf((char *)tmp, "%d", curbuf->b_fnum);
3339 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3340
3341 o_curbuf = curbuf;
3342 o_curwin = curwin;
3343 curwin = wp;
3344 curbuf = wp->w_buffer;
3345
3346 str = eval_to_string_safe(p, &t);
3347
3348 curwin = o_curwin;
3349 curbuf = o_curbuf;
3350 do_unlet((char_u *)"g:actual_curbuf");
3351
3352 if (str != NULL && *str != 0)
3353 {
3354 if (*skipdigits(str) == NUL)
3355 {
3356 num = atoi((char *)str);
3357 vim_free(str);
3358 str = NULL;
3359 itemisflag = FALSE;
3360 }
3361 }
3362 #endif
3363 break;
3364
3365 case STL_LINE:
3366 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3367 ? 0L : (long)(wp->w_cursor.lnum);
3368 break;
3369
3370 case STL_NUMLINES:
3371 num = wp->w_buffer->b_ml.ml_line_count;
3372 break;
3373
3374 case STL_COLUMN:
3375 num = !(State & INSERT) && empty_line
3376 ? 0 : (int)wp->w_cursor.col + 1;
3377 break;
3378
3379 case STL_VIRTCOL:
3380 case STL_VIRTCOL_ALT:
3381 /* In list mode virtcol needs to be recomputed */
3382 virtcol = wp->w_virtcol;
3383 if (wp->w_p_list && lcs_tab1 == NUL)
3384 {
3385 wp->w_p_list = FALSE;
3386 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3387 wp->w_p_list = TRUE;
3388 }
3389 ++virtcol;
3390 /* Don't display %V if it's the same as %c. */
3391 if (opt == STL_VIRTCOL_ALT
3392 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3393 ? 0 : (int)wp->w_cursor.col + 1)))
3394 break;
3395 num = (long)virtcol;
3396 break;
3397
3398 case STL_PERCENTAGE:
3399 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3400 (long)wp->w_buffer->b_ml.ml_line_count);
3401 break;
3402
3403 case STL_ALTPERCENT:
3404 str = tmp;
3405 get_rel_pos(wp, str);
3406 break;
3407
3408 case STL_ARGLISTSTAT:
3409 fillable = FALSE;
3410 tmp[0] = 0;
3411 if (append_arg_number(wp, tmp, FALSE, (int)sizeof(tmp)))
3412 str = tmp;
3413 break;
3414
3415 case STL_KEYMAP:
3416 fillable = FALSE;
3417 if (get_keymap_str(wp, tmp, TMPLEN))
3418 str = tmp;
3419 break;
3420 case STL_PAGENUM:
3421 #ifdef FEAT_PRINTER
3422 num = get_printer_page_num();
3423 #else
3424 num = 0;
3425 #endif
3426 break;
3427
3428 case STL_BUFNO:
3429 num = wp->w_buffer->b_fnum;
3430 break;
3431
3432 case STL_OFFSET_X:
3433 base = 'X';
3434 case STL_OFFSET:
3435 #ifdef FEAT_BYTEOFF
3436 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3437 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3438 0L : l + 1 + (!(State & INSERT) && empty_line ?
3439 0 : (int)wp->w_cursor.col);
3440 #endif
3441 break;
3442
3443 case STL_BYTEVAL_X:
3444 base = 'X';
3445 case STL_BYTEVAL:
3446 if (((State & INSERT) && wp == curwin) || empty_line)
3447 num = 0;
3448 else
3449 {
3450 #ifdef FEAT_MBYTE
3451 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3452 #else
3453 num = linecont[wp->w_cursor.col];
3454 #endif
3455 }
3456 if (num == NL)
3457 num = 0;
3458 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3459 num = NL;
3460 break;
3461
3462 case STL_ROFLAG:
3463 case STL_ROFLAG_ALT:
3464 itemisflag = TRUE;
3465 if (wp->w_buffer->b_p_ro)
3466 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3467 break;
3468
3469 case STL_HELPFLAG:
3470 case STL_HELPFLAG_ALT:
3471 itemisflag = TRUE;
3472 if (wp->w_buffer->b_help)
3473 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
3474 : _("[help]"));
3475 break;
3476
3477 #ifdef FEAT_AUTOCMD
3478 case STL_FILETYPE:
3479 if (*wp->w_buffer->b_p_ft != NUL
3480 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3481 {
3482 sprintf((char *)tmp, "[%s]", wp->w_buffer->b_p_ft);
3483 str = tmp;
3484 }
3485 break;
3486
3487 case STL_FILETYPE_ALT:
3488 itemisflag = TRUE;
3489 if (*wp->w_buffer->b_p_ft != NUL
3490 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3491 {
3492 sprintf((char *)tmp, ",%s", wp->w_buffer->b_p_ft);
3493 for (t = tmp; *t != 0; t++)
3494 *t = TOUPPER_LOC(*t);
3495 str = tmp;
3496 }
3497 break;
3498 #endif
3499
3500 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3501 case STL_PREVIEWFLAG:
3502 case STL_PREVIEWFLAG_ALT:
3503 itemisflag = TRUE;
3504 if (wp->w_p_pvw)
3505 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3506 : _("[Preview]"));
3507 break;
3508 #endif
3509
3510 case STL_MODIFIED:
3511 case STL_MODIFIED_ALT:
3512 itemisflag = TRUE;
3513 switch ((opt == STL_MODIFIED_ALT)
3514 + bufIsChanged(wp->w_buffer) * 2
3515 + (!wp->w_buffer->b_p_ma) * 4)
3516 {
3517 case 2: str = (char_u *)"[+]"; break;
3518 case 3: str = (char_u *)",+"; break;
3519 case 4: str = (char_u *)"[-]"; break;
3520 case 5: str = (char_u *)",-"; break;
3521 case 6: str = (char_u *)"[+-]"; break;
3522 case 7: str = (char_u *)",+-"; break;
3523 }
3524 break;
3525 }
3526
3527 item[curitem].start = p;
3528 item[curitem].type = Normal;
3529 if (str != NULL && *str)
3530 {
3531 t = str;
3532 if (itemisflag)
3533 {
3534 if ((t[0] && t[1])
3535 && ((!prevchar_isitem && *t == ',')
3536 || (prevchar_isflag && *t == ' ')))
3537 t++;
3538 prevchar_isflag = TRUE;
3539 }
3540 l = vim_strsize(t);
3541 if (l > 0)
3542 prevchar_isitem = TRUE;
3543 if (l > maxwid)
3544 {
3545 while (l >= maxwid)
3546 #ifdef FEAT_MBYTE
3547 if (has_mbyte)
3548 {
3549 l -= ptr2cells(t);
3550 t += (*mb_ptr2len_check)(t);
3551 }
3552 else
3553 #endif
3554 l -= byte2cells(*t++);
3555 if (p + 1 >= out + outlen)
3556 break;
3557 *p++ = '<';
3558 }
3559 if (minwid > 0)
3560 {
3561 for (; l < minwid && p + 1 < out + outlen; l++)
3562 {
3563 /* Don't put a "-" in front of a digit. */
3564 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3565 *p++ = ' ';
3566 else
3567 *p++ = fillchar;
3568 }
3569 minwid = 0;
3570 }
3571 else
3572 minwid *= -1;
3573 while (*t && p + 1 < out + outlen)
3574 {
3575 *p++ = *t++;
3576 /* Change a space by fillchar, unless fillchar is '-' and a
3577 * digit follows. */
3578 if (fillable && p[-1] == ' '
3579 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3580 p[-1] = fillchar;
3581 }
3582 for (; l < minwid && p + 1 < out + outlen; l++)
3583 *p++ = fillchar;
3584 }
3585 else if (num >= 0)
3586 {
3587 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
3588 char_u nstr[20];
3589
3590 if (p + 20 >= out + outlen)
3591 break; /* not sufficient space */
3592 prevchar_isitem = TRUE;
3593 t = nstr;
3594 if (opt == STL_VIRTCOL_ALT)
3595 {
3596 *t++ = '-';
3597 minwid--;
3598 }
3599 *t++ = '%';
3600 if (zeropad)
3601 *t++ = '0';
3602 *t++ = '*';
3603 *t++ = nbase == 16 ? base : (nbase == 8 ? 'o' : 'd');
3604 *t = 0;
3605
3606 for (n = num, l = 1; n >= nbase; n /= nbase)
3607 l++;
3608 if (opt == STL_VIRTCOL_ALT)
3609 l++;
3610 if (l > maxwid)
3611 {
3612 l += 2;
3613 n = l - maxwid;
3614 while (l-- > maxwid)
3615 num /= nbase;
3616 *t++ = '>';
3617 *t++ = '%';
3618 *t = t[-3];
3619 *++t = 0;
3620 sprintf((char *)p, (char *)nstr, 0, num, n);
3621 }
3622 else
3623 sprintf((char *)p, (char *)nstr, minwid, num);
3624 p += STRLEN(p);
3625 }
3626 else
3627 item[curitem].type = Empty;
3628
3629 if (opt == STL_VIM_EXPR)
3630 vim_free(str);
3631
3632 if (num >= 0 || (!itemisflag && str && *str))
3633 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
3634 curitem++;
3635 }
3636 *p = NUL;
3637 itemcnt = curitem;
3638
3639 width = vim_strsize(out);
3640 if (maxwidth > 0 && width > maxwidth)
3641 {
3642 /* Result is too long, must trunctate somewhere. */
3643 l = 0;
3644 if (itemcnt == 0)
3645 s = out;
3646 else
3647 {
3648 for ( ; l < itemcnt; l++)
3649 if (item[l].type == Trunc)
3650 {
3651 /* Truncate at %< item. */
3652 s = item[l].start;
3653 break;
3654 }
3655 if (l == itemcnt)
3656 {
3657 /* No %< item, truncate first item. */
3658 s = item[0].start;
3659 l = 0;
3660 }
3661 }
3662
3663 if (width - vim_strsize(s) >= maxwidth)
3664 {
3665 /* Truncation mark is beyond max length */
3666 #ifdef FEAT_MBYTE
3667 if (has_mbyte)
3668 {
3669 s = out;
3670 width = 0;
3671 for (;;)
3672 {
3673 width += ptr2cells(s);
3674 if (width >= maxwidth)
3675 break;
3676 s += (*mb_ptr2len_check)(s);
3677 }
3678 /* Fill up for half a double-wide character. */
3679 while (++width < maxwidth)
3680 *s++ = fillchar;
3681 }
3682 else
3683 #endif
3684 s = out + maxwidth - 1;
3685 for (l = 0; l < itemcnt; l++)
3686 if (item[l].start > s)
3687 break;
3688 itemcnt = l;
3689 *s++ = '>';
3690 *s = 0;
3691 }
3692 else
3693 {
3694 #ifdef FEAT_MBYTE
3695 if (has_mbyte)
3696 {
3697 n = 0;
3698 while (width >= maxwidth)
3699 {
3700 width -= ptr2cells(s + n);
3701 n += (*mb_ptr2len_check)(s + n);
3702 }
3703 }
3704 else
3705 #endif
3706 n = width - maxwidth + 1;
3707 p = s + n;
3708 mch_memmove(s + 1, p, STRLEN(p) + 1);
3709 *s = '<';
3710
3711 /* Fill up for half a double-wide character. */
3712 while (++width < maxwidth)
3713 {
3714 s = s + STRLEN(s);
3715 *s++ = fillchar;
3716 *s = NUL;
3717 }
3718
3719 --n; /* count the '<' */
3720 for (; l < itemcnt; l++)
3721 {
3722 if (item[l].start - n >= s)
3723 item[l].start -= n;
3724 else
3725 item[l].start = s;
3726 }
3727 }
3728 width = maxwidth;
3729 }
3730 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
3731 {
3732 /* Apply STL_MIDDLE if any */
3733 for (l = 0; l < itemcnt; l++)
3734 if (item[l].type == Middle)
3735 break;
3736 if (l < itemcnt)
3737 {
3738 p = item[l].start + maxwidth - width;
3739 mch_memmove(p, item[l].start, STRLEN(item[l].start) + 1);
3740 for (s = item[l].start; s < p; s++)
3741 *s = fillchar;
3742 for (l++; l < itemcnt; l++)
3743 item[l].start += maxwidth - width;
3744 width = maxwidth;
3745 }
3746 }
3747
3748 if (hl != NULL)
3749 {
3750 for (l = 0; l < itemcnt; l++)
3751 {
3752 if (item[l].type == Highlight)
3753 {
3754 hl->start = item[l].start;
3755 hl->userhl = item[l].minwid;
3756 hl++;
3757 }
3758 }
3759 hl->start = NULL;
3760 hl->userhl = 0;
3761 }
3762
3763 return width;
3764 }
3765 #endif /* FEAT_STL_OPT */
3766
3767 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) || defined(PROTO)
3768 /*
3769 * Get relative cursor position in window, in the form 99%, using "Top", "Bot"
3770 * or "All" when appropriate.
3771 */
3772 void
3773 get_rel_pos(wp, str)
3774 win_T *wp;
3775 char_u *str;
3776 {
3777 long above; /* number of lines above window */
3778 long below; /* number of lines below window */
3779
3780 above = wp->w_topline - 1;
3781 #ifdef FEAT_DIFF
3782 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
3783 #endif
3784 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
3785 if (below <= 0)
3786 STRCPY(str, above == 0 ? _("All") : _("Bot"));
3787 else if (above <= 0)
3788 STRCPY(str, _("Top"));
3789 else
3790 sprintf((char *)str, "%2d%%", above > 1000000L
3791 ? (int)(above / ((above + below) / 100L))
3792 : (int)(above * 100L / (above + below)));
3793 }
3794 #endif
3795
3796 /*
3797 * Append (file 2 of 8) to 'buf', if editing more than one file.
3798 * Return TRUE if it was appended.
3799 */
3800 int
3801 append_arg_number(wp, buf, add_file, maxlen)
3802 win_T *wp;
3803 char_u *buf;
3804 int add_file; /* Add "file" before the arg number */
3805 int maxlen; /* maximum nr of chars in buf or zero*/
3806 {
3807 char_u *p;
3808
3809 if (ARGCOUNT <= 1) /* nothing to do */
3810 return FALSE;
3811
3812 p = buf + STRLEN(buf); /* go to the end of the buffer */
3813 if (maxlen && p - buf + 35 >= maxlen) /* getting too long */
3814 return FALSE;
3815 *p++ = ' ';
3816 *p++ = '(';
3817 if (add_file)
3818 {
3819 STRCPY(p, "file ");
3820 p += 5;
3821 }
3822 sprintf((char *)p, wp->w_arg_idx_invalid ? "(%d) of %d)"
3823 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
3824 return TRUE;
3825 }
3826
3827 /*
3828 * If fname is not a full path, make it a full path.
3829 * Returns pointer to allocated memory (NULL for failure).
3830 */
3831 char_u *
3832 fix_fname(fname)
3833 char_u *fname;
3834 {
3835 /*
3836 * Force expanding the path always for Unix, because symbolic links may
3837 * mess up the full path name, even though it starts with a '/'.
3838 * Also expand when there is ".." in the file name, try to remove it,
3839 * because "c:/src/../README" is equal to "c:/README".
3840 * For MS-Windows also expand names like "longna~1" to "longname".
3841 */
3842 #ifdef UNIX
3843 return FullName_save(fname, TRUE);
3844 #else
3845 if (!vim_isAbsName(fname) || strstr((char *)fname, "..") != NULL
3846 #if defined(MSWIN) || defined(DJGPP)
3847 || vim_strchr(fname, '~') != NULL
3848 #endif
3849 )
3850 return FullName_save(fname, FALSE);
3851
3852 fname = vim_strsave(fname);
3853
3854 #ifdef USE_FNAME_CASE
3855 # ifdef USE_LONG_FNAME
3856 if (USE_LONG_FNAME)
3857 # endif
3858 {
3859 if (fname != NULL)
3860 fname_case(fname, 0); /* set correct case for file name */
3861 }
3862 #endif
3863
3864 return fname;
3865 #endif
3866 }
3867
3868 /*
3869 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
3870 * "ffname" becomes a pointer to allocated memory (or NULL).
3871 */
3872 /*ARGSUSED*/
3873 void
3874 fname_expand(buf, ffname, sfname)
3875 buf_T *buf;
3876 char_u **ffname;
3877 char_u **sfname;
3878 {
3879 if (*ffname == NULL) /* if no file name given, nothing to do */
3880 return;
3881 if (*sfname == NULL) /* if no short file name given, use ffname */
3882 *sfname = *ffname;
3883 *ffname = fix_fname(*ffname); /* expand to full path */
3884
3885 #ifdef FEAT_SHORTCUT
3886 if (!buf->b_p_bin)
3887 {
3888 char_u *rfname = NULL;
3889
3890 /* If the file name is a shortcut file, use the file it links to. */
3891 rfname = mch_resolve_shortcut(*ffname);
3892 if (rfname)
3893 {
3894 vim_free(*ffname);
3895 *ffname = rfname;
3896 *sfname = rfname;
3897 }
3898 }
3899 #endif
3900 }
3901
3902 /*
3903 * Get the file name for an argument list entry.
3904 */
3905 char_u *
3906 alist_name(aep)
3907 aentry_T *aep;
3908 {
3909 buf_T *bp;
3910
3911 /* Use the name from the associated buffer if it exists. */
3912 bp = buflist_findnr(aep->ae_fnum);
3913 if (bp == NULL)
3914 return aep->ae_fname;
3915 return bp->b_fname;
3916 }
3917
3918 #if defined(FEAT_WINDOWS) || defined(PROTO)
3919 /*
3920 * do_arg_all(): Open up to 'count' windows, one for each argument.
3921 */
3922 void
3923 do_arg_all(count, forceit)
3924 int count;
3925 int forceit; /* hide buffers in current windows */
3926 {
3927 int i;
3928 win_T *wp, *wpnext;
3929 char_u *opened; /* array of flags for which args are open */
3930 int opened_len; /* lenght of opened[] */
3931 int use_firstwin = FALSE; /* use first window for arglist */
3932 int split_ret = OK;
3933 int p_ea_save;
3934 alist_T *alist; /* argument list to be used */
3935 buf_T *buf;
3936
3937 if (ARGCOUNT <= 0)
3938 {
3939 /* Don't give an error message. We don't want it when the ":all"
3940 * command is in the .vimrc. */
3941 return;
3942 }
3943 setpcmark();
3944
3945 opened_len = ARGCOUNT;
3946 opened = alloc_clear((unsigned)opened_len);
3947 if (opened == NULL)
3948 return;
3949
3950 #ifdef FEAT_GUI
3951 need_mouse_correct = TRUE;
3952 #endif
3953
3954 /*
3955 * Try closing all windows that are not in the argument list.
3956 * Also close windows that are not full width;
3957 * When 'hidden' or "forceit" set the buffer becomes hidden.
3958 * Windows that have a changed buffer and can't be hidden won't be closed.
3959 */
3960 for (wp = firstwin; wp != NULL; wp = wpnext)
3961 {
3962 wpnext = wp->w_next;
3963 buf = wp->w_buffer;
3964 if (buf->b_ffname == NULL
3965 || buf->b_nwindows > 1
3966 #ifdef FEAT_VERTSPLIT
3967 || wp->w_width != Columns
3968 #endif
3969 )
3970 i = ARGCOUNT;
3971 else
3972 {
3973 /* check if the buffer in this window is in the arglist */
3974 for (i = 0; i < ARGCOUNT; ++i)
3975 {
3976 if (ARGLIST[i].ae_fnum == buf->b_fnum
3977 || fullpathcmp(alist_name(&ARGLIST[i]),
3978 buf->b_ffname, TRUE) & FPC_SAME)
3979 {
3980 if (i < opened_len)
3981 opened[i] = TRUE;
3982 if (wp->w_alist != curwin->w_alist)
3983 {
3984 /* Use the current argument list for all windows
3985 * containing a file from it. */
3986 alist_unlink(wp->w_alist);
3987 wp->w_alist = curwin->w_alist;
3988 ++wp->w_alist->al_refcount;
3989 }
3990 break;
3991 }
3992 }
3993 }
3994 wp->w_arg_idx = i;
3995
3996 if (i == ARGCOUNT) /* close this window */
3997 {
3998 if (P_HID(buf) || forceit || buf->b_nwindows > 1
3999 || !bufIsChanged(buf))
4000 {
4001 /* If the buffer was changed, and we would like to hide it,
4002 * try autowriting. */
4003 if (!P_HID(buf) && buf->b_nwindows <= 1 && bufIsChanged(buf))
4004 {
4005 (void)autowrite(buf, FALSE);
4006 #ifdef FEAT_AUTOCMD
4007 /* check if autocommands removed the window */
4008 if (!win_valid(wp) || !buf_valid(buf))
4009 {
4010 wpnext = firstwin; /* start all over... */
4011 continue;
4012 }
4013 #endif
4014 }
4015 #ifdef FEAT_WINDOWS
4016 if (firstwin == lastwin) /* can't close last window */
4017 #endif
4018 use_firstwin = TRUE;
4019 #ifdef FEAT_WINDOWS
4020 else
4021 {
4022 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4023 # ifdef FEAT_AUTOCMD
4024 /* check if autocommands removed the next window */
4025 if (!win_valid(wpnext))
4026 wpnext = firstwin; /* start all over... */
4027 # endif
4028 }
4029 #endif
4030 }
4031 }
4032 }
4033
4034 /*
4035 * Open a window for files in the argument list that don't have one.
4036 * ARGCOUNT may change while doing this, because of autocommands.
4037 */
4038 if (count > ARGCOUNT || count <= 0)
4039 count = ARGCOUNT;
4040
4041 /* Autocommands may do anything to the argument list. Make sure it's not
4042 * freed while we are working here by "locking" it. We still have to
4043 * watch out for its size to be changed. */
4044 alist = curwin->w_alist;
4045 ++alist->al_refcount;
4046
4047 #ifdef FEAT_AUTOCMD
4048 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4049 ++autocmd_no_enter;
4050 ++autocmd_no_leave;
4051 #endif
4052 win_enter(lastwin, FALSE);
4053 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4054 {
4055 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4056 arg_had_last = TRUE;
4057 if (i < opened_len && opened[i])
4058 {
4059 /* Move the already present window to below the current window */
4060 if (curwin->w_arg_idx != i)
4061 {
4062 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4063 {
4064 if (wpnext->w_arg_idx == i)
4065 {
4066 win_move_after(wpnext, curwin);
4067 break;
4068 }
4069 }
4070 }
4071 }
4072 else if (split_ret == OK)
4073 {
4074 if (!use_firstwin) /* split current window */
4075 {
4076 p_ea_save = p_ea;
4077 p_ea = TRUE; /* use space from all windows */
4078 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4079 p_ea = p_ea_save;
4080 if (split_ret == FAIL)
4081 continue;
4082 }
4083 #ifdef FEAT_AUTOCMD
4084 else /* first window: do autocmd for leaving this buffer */
4085 --autocmd_no_leave;
4086 #endif
4087
4088 /*
4089 * edit file i
4090 */
4091 curwin->w_arg_idx = i;
4092 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4093 ECMD_ONE,
4094 ((P_HID(curwin->w_buffer)
4095 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
4096 + ECMD_OLDBUF);
4097 #ifdef FEAT_AUTOCMD
4098 if (use_firstwin)
4099 ++autocmd_no_leave;
4100 #endif
4101 use_firstwin = FALSE;
4102 }
4103 ui_breakcheck();
4104 }
4105
4106 /* Remove the "lock" on the argument list. */
4107 alist_unlink(alist);
4108
4109 #ifdef FEAT_AUTOCMD
4110 --autocmd_no_enter;
4111 #endif
4112 win_enter(firstwin, FALSE); /* back to first window */
4113 #ifdef FEAT_AUTOCMD
4114 --autocmd_no_leave;
4115 #endif
4116 vim_free(opened);
4117 }
4118
4119 # if defined(FEAT_LISTCMDS) || defined(PROTO)
4120 /*
4121 * Open a window for a number of buffers.
4122 */
4123 void
4124 ex_buffer_all(eap)
4125 exarg_T *eap;
4126 {
4127 buf_T *buf;
4128 win_T *wp, *wpnext;
4129 int split_ret = OK;
4130 int p_ea_save;
4131 int open_wins = 0;
4132 int r;
4133 int count; /* Maximum number of windows to open. */
4134 int all; /* When TRUE also load inactive buffers. */
4135
4136 if (eap->addr_count == 0) /* make as many windows as possible */
4137 count = 9999;
4138 else
4139 count = eap->line2; /* make as many windows as specified */
4140 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4141 all = FALSE;
4142 else
4143 all = TRUE;
4144
4145 setpcmark();
4146
4147 #ifdef FEAT_GUI
4148 need_mouse_correct = TRUE;
4149 #endif
4150
4151 /*
4152 * Close superfluous windows (two windows for the same buffer).
4153 * Also close windows that are not full-width.
4154 */
4155 for (wp = firstwin; wp != NULL; wp = wpnext)
4156 {
4157 wpnext = wp->w_next;
4158 if (wp->w_buffer->b_nwindows > 1
4159 #ifdef FEAT_VERTSPLIT
4160 || ((cmdmod.split & WSP_VERT)
4161 ? wp->w_height + wp->w_status_height < Rows - p_ch
4162 : wp->w_width != Columns)
4163 #endif
4164 )
4165 {
4166 win_close(wp, FALSE);
4167 #ifdef FEAT_AUTOCMD
4168 wpnext = firstwin; /* just in case an autocommand does something
4169 strange with windows */
4170 open_wins = 0;
4171 #endif
4172 }
4173 else
4174 ++open_wins;
4175 }
4176
4177 /*
4178 * Go through the buffer list. When a buffer doesn't have a window yet,
4179 * open one. Otherwise move the window to the right position.
4180 * Watch out for autocommands that delete buffers or windows!
4181 */
4182 #ifdef FEAT_AUTOCMD
4183 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4184 ++autocmd_no_enter;
4185 #endif
4186 win_enter(lastwin, FALSE);
4187 #ifdef FEAT_AUTOCMD
4188 ++autocmd_no_leave;
4189 #endif
4190 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4191 {
4192 /* Check if this buffer needs a window */
4193 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4194 continue;
4195
4196 /* Check if this buffer already has a window */
4197 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4198 if (wp->w_buffer == buf)
4199 break;
4200 /* If the buffer already has a window, move it */
4201 if (wp != NULL)
4202 win_move_after(wp, curwin);
4203 else if (split_ret == OK)
4204 {
4205 /* Split the window and put the buffer in it */
4206 p_ea_save = p_ea;
4207 p_ea = TRUE; /* use space from all windows */
4208 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4209 ++open_wins;
4210 p_ea = p_ea_save;
4211 if (split_ret == FAIL)
4212 continue;
4213
4214 /* Open the buffer in this window. */
4215 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4216 swap_exists_action = SEA_DIALOG;
4217 #endif
4218 set_curbuf(buf, DOBUF_GOTO);
4219 #ifdef FEAT_AUTOCMD
4220 # ifdef FEAT_EVAL
4221 /* Autocommands deleted the buffer or aborted script
4222 * processing!!! */
4223 if (!buf_valid(buf) || aborting())
4224 # else
4225 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
4226 # endif
4227 {
4228 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4229 swap_exists_action = SEA_NONE;
4230 #endif
4231 break;
4232 }
4233 #endif
4234 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4235 if (swap_exists_action == SEA_QUIT)
4236 {
4237 /* User selected Quit at ATTENTION prompt; close this window. */
4238 win_close(curwin, TRUE);
4239 --open_wins;
4240 swap_exists_action = SEA_NONE;
4241 }
4242 else
4243 handle_swap_exists(NULL);
4244 #endif
4245 }
4246
4247 ui_breakcheck();
4248 if (got_int)
4249 {
4250 (void)vgetc(); /* only break the file loading, not the rest */
4251 break;
4252 }
4253 }
4254 #ifdef FEAT_AUTOCMD
4255 --autocmd_no_enter;
4256 #endif
4257 win_enter(firstwin, FALSE); /* back to first window */
4258 #ifdef FEAT_AUTOCMD
4259 --autocmd_no_leave;
4260 #endif
4261
4262 /*
4263 * Close superfluous windows.
4264 */
4265 for (wp = lastwin; open_wins > count; )
4266 {
4267 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4268 || autowrite(wp->w_buffer, FALSE) == OK);
4269 #ifdef FEAT_AUTOCMD
4270 if (!win_valid(wp))
4271 {
4272 /* BufWrite Autocommands made the window invalid, start over */
4273 wp = lastwin;
4274 }
4275 else
4276 #endif
4277 if (r)
4278 {
4279 win_close(wp, !P_HID(wp->w_buffer));
4280 --open_wins;
4281 wp = lastwin;
4282 }
4283 else
4284 {
4285 wp = wp->w_prev;
4286 if (wp == NULL)
4287 break;
4288 }
4289 }
4290 }
4291 # endif /* FEAT_LISTCMDS */
4292
4293 #endif /* FEAT_WINDOWS */
4294
4295 /*
4296 * do_modelines() - process mode lines for the current file
4297 *
4298 * Returns immediately if the "ml" option isn't set.
4299 */
4300 static int chk_modeline __ARGS((linenr_T));
4301
4302 void
4303 do_modelines()
4304 {
4305 linenr_T lnum;
4306 int nmlines;
4307 static int entered = 0;
4308
4309 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4310 return;
4311
4312 /* Disallow recursive entry here. Can happen when executing a modeline
4313 * triggers an autocommand, which reloads modelines with a ":do". */
4314 if (entered)
4315 return;
4316
4317 ++entered;
4318 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4319 ++lnum)
4320 if (chk_modeline(lnum) == FAIL)
4321 nmlines = 0;
4322
4323 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4324 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
4325 if (chk_modeline(lnum) == FAIL)
4326 nmlines = 0;
4327 --entered;
4328 }
4329
4330 #include "version.h" /* for version number */
4331
4332 /*
4333 * chk_modeline() - check a single line for a mode string
4334 * Return FAIL if an error encountered.
4335 */
4336 static int
4337 chk_modeline(lnum)
4338 linenr_T lnum;
4339 {
4340 char_u *s;
4341 char_u *e;
4342 char_u *linecopy; /* local copy of any modeline found */
4343 int prev;
4344 int vers;
4345 int end;
4346 int retval = OK;
4347 char_u *save_sourcing_name;
4348 linenr_T save_sourcing_lnum;
4349 #ifdef FEAT_EVAL
4350 scid_T save_SID;
4351 #endif
4352
4353 prev = -1;
4354 for (s = ml_get(lnum); *s != NUL; ++s)
4355 {
4356 if (prev == -1 || vim_isspace(prev))
4357 {
4358 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4359 || STRNCMP(s, "vi:", (size_t)3) == 0)
4360 break;
4361 if (STRNCMP(s, "vim", 3) == 0)
4362 {
4363 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4364 e = s + 4;
4365 else
4366 e = s + 3;
4367 vers = getdigits(&e);
4368 if (*e == ':'
4369 && (s[3] == ':'
4370 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4371 || (VIM_VERSION_100 < vers && s[3] == '<')
4372 || (VIM_VERSION_100 > vers && s[3] == '>')
4373 || (VIM_VERSION_100 == vers && s[3] == '=')))
4374 break;
4375 }
4376 }
4377 prev = *s;
4378 }
4379
4380 if (*s)
4381 {
4382 do /* skip over "ex:", "vi:" or "vim:" */
4383 ++s;
4384 while (s[-1] != ':');
4385
4386 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4387 if (linecopy == NULL)
4388 return FAIL;
4389
4390 save_sourcing_lnum = sourcing_lnum;
4391 save_sourcing_name = sourcing_name;
4392 sourcing_lnum = lnum; /* prepare for emsg() */
4393 sourcing_name = (char_u *)"modelines";
4394
4395 end = FALSE;
4396 while (end == FALSE)
4397 {
4398 s = skipwhite(s);
4399 if (*s == NUL)
4400 break;
4401
4402 /*
4403 * Find end of set command: ':' or end of line.
4404 * Skip over "\:", replacing it with ":".
4405 */
4406 for (e = s; *e != ':' && *e != NUL; ++e)
4407 if (e[0] == '\\' && e[1] == ':')
4408 STRCPY(e, e + 1);
4409 if (*e == NUL)
4410 end = TRUE;
4411
4412 /*
4413 * If there is a "set" command, require a terminating ':' and
4414 * ignore the stuff after the ':'.
4415 * "vi:set opt opt opt: foo" -- foo not interpreted
4416 * "vi:opt opt opt: foo" -- foo interpreted
4417 * Accept "se" for compatibility with Elvis.
4418 */
4419 if (STRNCMP(s, "set ", (size_t)4) == 0
4420 || STRNCMP(s, "se ", (size_t)3) == 0)
4421 {
4422 if (*e != ':') /* no terminating ':'? */
4423 break;
4424 end = TRUE;
4425 s = vim_strchr(s, ' ') + 1;
4426 }
4427 *e = NUL; /* truncate the set command */
4428
4429 if (*s != NUL) /* skip over an empty "::" */
4430 {
4431 #ifdef FEAT_EVAL
4432 save_SID = current_SID;
4433 current_SID = SID_MODELINE;
4434 #endif
4435 retval = do_set(s, OPT_MODELINE | OPT_LOCAL);
4436 #ifdef FEAT_EVAL
4437 current_SID = save_SID;
4438 #endif
4439 if (retval == FAIL) /* stop if error found */
4440 break;
4441 }
4442 s = e + 1; /* advance to next part */
4443 }
4444
4445 sourcing_lnum = save_sourcing_lnum;
4446 sourcing_name = save_sourcing_name;
4447
4448 vim_free(linecopy);
4449 }
4450 return retval;
4451 }
4452
4453 #ifdef FEAT_VIMINFO
4454 int
4455 read_viminfo_bufferlist(virp, writing)
4456 vir_T *virp;
4457 int writing;
4458 {
4459 char_u *tab;
4460 linenr_T lnum;
4461 colnr_T col;
4462 buf_T *buf;
4463 char_u *sfname;
4464 char_u *xline;
4465
4466 /* Handle long line and escaped characters. */
4467 xline = viminfo_readstring(virp, 1, FALSE);
4468
4469 /* don't read in if there are files on the command-line or if writing: */
4470 if (xline != NULL && !writing && ARGCOUNT == 0
4471 && find_viminfo_parameter('%') != NULL)
4472 {
4473 /* Format is: <fname> Tab <lnum> Tab <col>.
4474 * Watch out for a Tab in the file name, work from the end. */
4475 lnum = 0;
4476 col = 0;
4477 tab = vim_strrchr(xline, '\t');
4478 if (tab != NULL)
4479 {
4480 *tab++ = '\0';
4481 col = atoi((char *)tab);
4482 tab = vim_strrchr(xline, '\t');
4483 if (tab != NULL)
4484 {
4485 *tab++ = '\0';
4486 lnum = atol((char *)tab);
4487 }
4488 }
4489
4490 /* Expand "~/" in the file name at "line + 1" to a full path.
4491 * Then try shortening it by comparing with the current directory */
4492 expand_env(xline, NameBuff, MAXPATHL);
4493 mch_dirname(IObuff, IOSIZE);
4494 sfname = shorten_fname(NameBuff, IObuff);
4495 if (sfname == NULL)
4496 sfname = NameBuff;
4497
4498 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
4499 if (buf != NULL) /* just in case... */
4500 {
4501 buf->b_last_cursor.lnum = lnum;
4502 buf->b_last_cursor.col = col;
4503 buflist_setfpos(buf, curwin, lnum, col, FALSE);
4504 }
4505 }
4506 vim_free(xline);
4507
4508 return viminfo_readline(virp);
4509 }
4510
4511 void
4512 write_viminfo_bufferlist(fp)
4513 FILE *fp;
4514 {
4515 buf_T *buf;
4516 #ifdef FEAT_WINDOWS
4517 win_T *win;
4518 #endif
4519 char_u *line;
4520
4521 if (find_viminfo_parameter('%') == NULL)
4522 return;
4523
4524 /* Allocate room for the file name, lnum and col. */
4525 line = alloc(MAXPATHL + 30);
4526 if (line == NULL)
4527 return;
4528
4529 #ifdef FEAT_WINDOWS
4530 for (win = firstwin; win != NULL; win = win->w_next)
4531 set_last_cursor(win);
4532 #else
4533 set_last_cursor(curwin);
4534 #endif
4535
4536 fprintf(fp, _("\n# Buffer list:\n"));
4537 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
4538 {
4539 if (buf->b_fname == NULL
4540 || !buf->b_p_bl
4541 #ifdef FEAT_QUICKFIX
4542 || bt_quickfix(buf)
4543 #endif
4544 || removable(buf->b_ffname))
4545 continue;
4546
4547 putc('%', fp);
4548 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
4549 sprintf((char *)line + STRLEN(line), "\t%ld\t%d",
4550 (long)buf->b_last_cursor.lnum,
4551 buf->b_last_cursor.col);
4552 viminfo_writestring(fp, line);
4553 }
4554 vim_free(line);
4555 }
4556 #endif
4557
4558
4559 /*
4560 * Return special buffer name.
4561 * Returns NULL when the buffer has a normal file name.
4562 */
4563 char *
4564 buf_spname(buf)
4565 buf_T *buf;
4566 {
4567 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
4568 if (bt_quickfix(buf))
4569 return _("[Error List]");
4570 #endif
4571 #ifdef FEAT_QUICKFIX
4572 /* There is no _file_ when 'buftype' is "nofile", b_sfname
4573 * contains the name as specified by the user */
4574 if (bt_nofile(buf))
4575 {
4576 if (buf->b_sfname != NULL)
4577 return (char *)buf->b_sfname;
4578 return "[Scratch]";
4579 }
4580 #endif
4581 if (buf->b_fname == NULL)
4582 return _("[No File]");
4583 return NULL;
4584 }
4585
4586
4587 #if defined(FEAT_SIGNS) || defined(PROTO)
4588 /*
4589 * Insert the sign into the signlist.
4590 */
4591 static void
4592 insert_sign(buf, prev, next, id, lnum, typenr)
4593 buf_T *buf; /* buffer to store sign in */
4594 signlist_T *prev; /* previous sign entry */
4595 signlist_T *next; /* next sign entry */
4596 int id; /* sign ID */
4597 linenr_T lnum; /* line number which gets the mark */
4598 int typenr; /* typenr of sign we are adding */
4599 {
4600 signlist_T *newsign;
4601
4602 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
4603 if (newsign != NULL)
4604 {
4605 newsign->id = id;
4606 newsign->lnum = lnum;
4607 newsign->typenr = typenr;
4608 newsign->next = next;
4609 #ifdef FEAT_NETBEANS_INTG
4610 newsign->prev = prev;
4611 if (next != NULL)
4612 next->prev = newsign;
4613 #endif
4614
4615 if (prev == NULL)
4616 {
4617 /* When adding first sign need to redraw the windows to create the
4618 * column for signs. */
4619 if (buf->b_signlist == NULL)
4620 {
4621 redraw_buf_later(buf, NOT_VALID);
4622 changed_cline_bef_curs();
4623 }
4624
4625 /* first sign in signlist */
4626 buf->b_signlist = newsign;
4627 }
4628 else
4629 prev->next = newsign;
4630 }
4631 }
4632
4633 /*
4634 * Add the sign into the signlist. Find the right spot to do it though.
4635 */
4636 void
4637 buf_addsign(buf, id, lnum, typenr)
4638 buf_T *buf; /* buffer to store sign in */
4639 int id; /* sign ID */
4640 linenr_T lnum; /* line number which gets the mark */
4641 int typenr; /* typenr of sign we are adding */
4642 {
4643 signlist_T *sign; /* a sign in the signlist */
4644 signlist_T *prev; /* the previous sign */
4645
4646 prev = NULL;
4647 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
4648 {
4649 if (lnum == sign->lnum && id == sign->id)
4650 {
4651 sign->typenr = typenr;
4652 return;
4653 }
4654 else if (
4655 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
4656 id < 0 &&
4657 #endif
4658 lnum < sign->lnum)
4659 {
4660 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
4661 /* XXX - GRP: Is this because of sign slide problem? Or is it
4662 * really needed? Or is it because we allow multiple signs per
4663 * line? If so, should I add that feature to FEAT_SIGNS?
4664 */
4665 while (prev != NULL && prev->lnum == lnum)
4666 prev = prev->prev;
4667 if (prev == NULL)
4668 sign = buf->b_signlist;
4669 else
4670 sign = prev->next;
4671 #endif
4672 insert_sign(buf, prev, sign, id, lnum, typenr);
4673 return;
4674 }
4675 prev = sign;
4676 }
4677 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
4678 /* XXX - GRP: See previous comment */
4679 while (prev != NULL && prev->lnum == lnum)
4680 prev = prev->prev;
4681 if (prev == NULL)
4682 sign = buf->b_signlist;
4683 else
4684 sign = prev->next;
4685 #endif
4686 insert_sign(buf, prev, sign, id, lnum, typenr);
4687
4688 return;
4689 }
4690
4691 int
4692 buf_change_sign_type(buf, markId, typenr)
4693 buf_T *buf; /* buffer to store sign in */
4694 int markId; /* sign ID */
4695 int typenr; /* typenr of sign we are adding */
4696 {
4697 signlist_T *sign; /* a sign in the signlist */
4698
4699 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
4700 {
4701 if (sign->id == markId)
4702 {
4703 sign->typenr = typenr;
4704 return sign->lnum;
4705 }
4706 }
4707
4708 return 0;
4709 }
4710
4711 int_u
4712 buf_getsigntype(buf, lnum, type)
4713 buf_T *buf;
4714 linenr_T lnum;
4715 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
4716 {
4717 signlist_T *sign; /* a sign in a b_signlist */
4718
4719 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
4720 if (sign->lnum == lnum
4721 && (type == SIGN_ANY
4722 # ifdef FEAT_SIGN_ICONS
4723 || (type == SIGN_ICON
4724 && sign_get_image(sign->typenr) != NULL)
4725 # endif
4726 || (type == SIGN_TEXT
4727 && sign_get_text(sign->typenr) != NULL)
4728 || (type == SIGN_LINEHL
4729 && sign_get_attr(sign->typenr, TRUE) != 0)))
4730 return sign->typenr;
4731 return 0;
4732 }
4733
4734
4735 linenr_T
4736 buf_delsign(buf, id)
4737 buf_T *buf; /* buffer sign is stored in */
4738 int id; /* sign id */
4739 {
4740 signlist_T **lastp; /* pointer to pointer to current sign */
4741 signlist_T *sign; /* a sign in a b_signlist */
4742 signlist_T *next; /* the next sign in a b_signlist */
4743 linenr_T lnum; /* line number whose sign was deleted */
4744
4745 lastp = &buf->b_signlist;
4746 lnum = 0;
4747 for (sign = buf->b_signlist; sign != NULL; sign = next)
4748 {
4749 next = sign->next;
4750 if (sign->id == id)
4751 {
4752 *lastp = next;
4753 #ifdef FEAT_NETBEANS_INTG
4754 if (next != NULL)
4755 next->prev = sign->prev;
4756 #endif
4757 lnum = sign->lnum;
4758 vim_free(sign);
4759 break;
4760 }
4761 else
4762 lastp = &sign->next;
4763 }
4764
4765 /* When deleted the last sign need to redraw the windows to remove the
4766 * sign column. */
4767 if (buf->b_signlist == NULL)
4768 {
4769 redraw_buf_later(buf, NOT_VALID);
4770 changed_cline_bef_curs();
4771 }
4772
4773 return lnum;
4774 }
4775
4776
4777 /*
4778 * Find the line number of the sign with the requested id. If the sign does
4779 * not exist, return 0 as the line number. This will still let the correct file
4780 * get loaded.
4781 */
4782 int
4783 buf_findsign(buf, id)
4784 buf_T *buf; /* buffer to store sign in */
4785 int id; /* sign ID */
4786 {
4787 signlist_T *sign; /* a sign in the signlist */
4788
4789 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
4790 if (sign->id == id)
4791 return sign->lnum;
4792
4793 return 0;
4794 }
4795
4796 int
4797 buf_findsign_id(buf, lnum)
4798 buf_T *buf; /* buffer whose sign we are searching for */
4799 linenr_T lnum; /* line number of sign */
4800 {
4801 signlist_T *sign; /* a sign in the signlist */
4802
4803 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
4804 if (sign->lnum == lnum)
4805 return sign->id;
4806
4807 return 0;
4808 }
4809
4810
4811 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
4812 /* see if a given type of sign exists on a specific line */
4813 int
4814 buf_findsigntype_id(buf, lnum, typenr)
4815 buf_T *buf; /* buffer whose sign we are searching for */
4816 linenr_T lnum; /* line number of sign */
4817 int typenr; /* sign type number */
4818 {
4819 signlist_T *sign; /* a sign in the signlist */
4820
4821 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
4822 if (sign->lnum == lnum && sign->typenr == typenr)
4823 return sign->id;
4824
4825 return 0;
4826 }
4827
4828
4829 # if defined(FEAT_SIGN_ICONS) || defined(PROTO)
4830 /* return the number of icons on the given line */
4831 int
4832 buf_signcount(buf, lnum)
4833 buf_T *buf;
4834 linenr_T lnum;
4835 {
4836 signlist_T *sign; /* a sign in the signlist */
4837 int count = 0;
4838
4839 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
4840 if (sign->lnum == lnum)
4841 if (sign_get_image(sign->typenr) != NULL)
4842 count++;
4843
4844 return count;
4845 }
4846 # endif /* FEAT_SIGN_ICONS */
4847 # endif /* FEAT_NETBEANS_INTG */
4848
4849
4850 /*
4851 * Delete signs in buffer "buf".
4852 */
4853 static void
4854 buf_delete_signs(buf)
4855 buf_T *buf;
4856 {
4857 signlist_T *next;
4858
4859 while (buf->b_signlist != NULL)
4860 {
4861 next = buf->b_signlist->next;
4862 vim_free(buf->b_signlist);
4863 buf->b_signlist = next;
4864 }
4865 }
4866
4867 /*
4868 * Delete all signs in all buffers.
4869 */
4870 void
4871 buf_delete_all_signs()
4872 {
4873 buf_T *buf; /* buffer we are checking for signs */
4874
4875 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4876 if (buf->b_signlist != NULL)
4877 {
4878 /* Need to redraw the windows to remove the sign column. */
4879 redraw_buf_later(buf, NOT_VALID);
4880 buf_delete_signs(buf);
4881 }
4882 }
4883
4884 /*
4885 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
4886 */
4887 void
4888 sign_list_placed(rbuf)
4889 buf_T *rbuf;
4890 {
4891 buf_T *buf;
4892 signlist_T *p;
4893 char lbuf[BUFSIZ];
4894
4895 MSG_PUTS_TITLE(_("\n--- Signs ---"));
4896 msg_putchar('\n');
4897 if (rbuf == NULL)
4898 buf = firstbuf;
4899 else
4900 buf = rbuf;
4901 while (buf != NULL)
4902 {
4903 if (buf->b_signlist != NULL)
4904 {
4905 #ifdef HAVE_SNPRINTF
4906 snprintf
4907 #else
4908 sprintf
4909 #endif
4910 (lbuf,
4911 #ifdef HAVE_SNPRINTF
4912 BUFSIZ,
4913 #endif
4914 _("Signs for %s:"), buf->b_fname);
4915 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
4916 msg_putchar('\n');
4917 }
4918 for (p = buf->b_signlist; p != NULL; p = p->next)
4919 {
4920 sprintf(lbuf, _(" line=%ld id=%d name=%s"),
4921 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
4922 MSG_PUTS(lbuf);
4923 msg_putchar('\n');
4924 }
4925 if (rbuf != NULL)
4926 break;
4927 buf = buf->b_next;
4928 }
4929 }
4930
4931 /*
4932 * Adjust a placed sign for inserted/deleted lines.
4933 */
4934 void
4935 sign_mark_adjust(line1, line2, amount, amount_after)
4936 linenr_T line1;
4937 linenr_T line2;
4938 long amount;
4939 long amount_after;
4940 {
4941 signlist_T *sign; /* a sign in a b_signlist */
4942
4943 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
4944 {
4945 if (sign->lnum >= line1 && sign->lnum <= line2)
4946 {
4947 if (amount == MAXLNUM)
4948 sign->lnum = line1;
4949 else
4950 sign->lnum += amount;
4951 }
4952 else if (sign->lnum > line2)
4953 sign->lnum += amount_after;
4954 }
4955 }
4956 #endif /* FEAT_SIGNS */
4957
4958 /*
4959 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
4960 */
4961 void
4962 set_buflisted(on)
4963 int on;
4964 {
4965 if (on != curbuf->b_p_bl)
4966 {
4967 curbuf->b_p_bl = on;
4968 #ifdef FEAT_AUTOCMD
4969 if (on)
4970 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
4971 else
4972 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
4973 #endif
4974 }
4975 }
4976
4977 /*
4978 * Read the file for "buf" again and check if the contents changed.
4979 * Return TRUE if it changed or this could not be checked.
4980 */
4981 int
4982 buf_contents_changed(buf)
4983 buf_T *buf;
4984 {
4985 buf_T *newbuf;
4986 int differ = TRUE;
4987 linenr_T lnum;
4988 #ifdef FEAT_AUTOCMD
4989 aco_save_T aco;
4990 #else
4991 buf_T *old_curbuf = curbuf;
4992 #endif
4993 exarg_T ea;
4994
4995 /* Allocate a buffer without putting it in the buffer list. */
4996 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
4997 if (newbuf == NULL)
4998 return TRUE;
4999
5000 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5001 if (prep_exarg(&ea, buf) == FAIL)
5002 {
5003 wipe_buffer(newbuf, FALSE);
5004 return TRUE;
5005 }
5006
5007 #ifdef FEAT_AUTOCMD
5008 /* set curwin/curbuf to buf and save a few things */
5009 aucmd_prepbuf(&aco, newbuf);
5010 #else
5011 curbuf = newbuf;
5012 curwin->w_buffer = newbuf;
5013 #endif
5014
5015 if (ml_open() == OK
5016 && readfile(buf->b_ffname, buf->b_fname,
5017 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5018 &ea, READ_NEW | READ_DUMMY) == OK)
5019 {
5020 /* compare the two files line by line */
5021 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5022 {
5023 differ = FALSE;
5024 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5025 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5026 {
5027 differ = TRUE;
5028 break;
5029 }
5030 }
5031 }
5032 vim_free(ea.cmd);
5033
5034 #ifdef FEAT_AUTOCMD
5035 /* restore curwin/curbuf and a few other things */
5036 aucmd_restbuf(&aco);
5037 #else
5038 curbuf = old_curbuf;
5039 curwin->w_buffer = old_curbuf;
5040 #endif
5041
5042 if (curbuf != newbuf) /* safety check */
5043 wipe_buffer(newbuf, FALSE);
5044
5045 return differ;
5046 }
5047
5048 /*
5049 * Wipe out a buffer and decrement the last buffer number if it was used for
5050 * this buffer. Call this to wipe out a temp buffer that does not contain any
5051 * marks.
5052 */
5053 /*ARGSUSED*/
5054 void
5055 wipe_buffer(buf, aucmd)
5056 buf_T *buf;
5057 int aucmd; /* When TRUE trigger autocommands. */
5058 {
5059 if (buf->b_fnum == top_file_num - 1)
5060 --top_file_num;
5061
5062 #ifdef FEAT_AUTOCMD
5063 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
5064 ++autocmd_block;
5065 #endif
5066 close_buffer(NULL, buf, DOBUF_WIPE);
5067 #ifdef FEAT_AUTOCMD
5068 if (!aucmd)
5069 --autocmd_block;
5070 #endif
5071 }