blob: 32d1eb402054e5517a007d3e9ea76fa09668145c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010030static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010032static void buflist_setfpos(buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020034static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
35static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
36static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#endif
40#ifdef FEAT_TITLE
Bram Moolenaar84a93082018-06-16 22:58:15 +020041static int value_changed(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010043static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
44static void free_buffer(buf_T *);
45static void free_buffer_stuff(buf_T *buf, int free_options);
46static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047
48#ifdef UNIX
49# define dev_T dev_t
50#else
51# define dev_T unsigned
52#endif
53
54#if defined(FEAT_SIGNS)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010055static void insert_sign(buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#endif
57
Bram Moolenaar4033c552017-09-16 20:54:51 +020058#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020059static char *msg_loclist = N_("[Location List]");
60static char *msg_qflist = N_("[Quickfix List]");
61#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010062static char *e_auabort = N_("E855: Autocommands caused command to abort");
Bram Moolenaar7fd73202010-07-25 16:58:46 +020063
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020064/* Number of times free_buffer() was called. */
65static int buf_free_count = 0;
66
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020067/* Read data from buffer for retrying. */
68 static int
69read_buffer(
70 int read_stdin, /* read file from stdin, otherwise fifo */
71 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
72 int flags) /* extra flags for readfile() */
73{
74 int retval = OK;
75 linenr_T line_count;
76
77 /*
78 * Read from the buffer which the text is already filled in and append at
79 * the end. This makes it possible to retry when 'fileformat' or
80 * 'fileencoding' was guessed wrong.
81 */
82 line_count = curbuf->b_ml.ml_line_count;
83 retval = readfile(
84 read_stdin ? NULL : curbuf->b_ffname,
85 read_stdin ? NULL : curbuf->b_fname,
86 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
87 flags | READ_BUFFER);
88 if (retval == OK)
89 {
90 /* Delete the binary lines. */
91 while (--line_count >= 0)
92 ml_delete((linenr_T)1, FALSE);
93 }
94 else
95 {
96 /* Delete the converted lines. */
97 while (curbuf->b_ml.ml_line_count > line_count)
98 ml_delete(line_count, FALSE);
99 }
100 /* Put the cursor on the first line. */
101 curwin->w_cursor.lnum = 1;
102 curwin->w_cursor.col = 0;
103
104 if (read_stdin)
105 {
106 /* Set or reset 'modified' before executing autocommands, so that
107 * it can be changed there. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100108 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200109 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100110 else if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200111 unchanged(curbuf, FALSE);
112
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100113 if (retval == OK)
114 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100115#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100116 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100117 curbuf, &retval);
118#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100119 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200120#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100121 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200122 }
123 return retval;
124}
125
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200127 * Open current buffer, that is: open the memfile and read the file into
128 * memory.
129 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 */
131 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100132open_buffer(
133 int read_stdin, /* read file from stdin */
134 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
135 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136{
137 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200138 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100139#ifdef FEAT_SYN_HL
140 long old_tw = curbuf->b_p_tw;
141#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200142 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143
144 /*
145 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
146 * When re-entering the same buffer, it should not change, because the
147 * user may have reset the flag by hand.
148 */
149 if (readonlymode && curbuf->b_ffname != NULL
150 && (curbuf->b_flags & BF_NEVERLOADED))
151 curbuf->b_p_ro = TRUE;
152
Bram Moolenaar4770d092006-01-12 23:22:24 +0000153 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 {
155 /*
156 * There MUST be a memfile, otherwise we can't do anything
157 * If we can't create one for the current buffer, take another buffer
158 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100159 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200160 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (curbuf->b_ml.ml_mfp != NULL)
162 break;
163 /*
164 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000165 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 */
167 if (curbuf == NULL)
168 {
169 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
170 getout(2);
171 }
172 EMSG(_("E83: Cannot allocate buffer, using other one..."));
173 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100174#ifdef FEAT_SYN_HL
175 if (old_tw != curbuf->b_p_tw)
176 check_colorcolumn(curwin);
177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 return FAIL;
179 }
180
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 /* The autocommands in readfile() may change the buffer, but only AFTER
182 * reading the file. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200183 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
186 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100187 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
189 if (curbuf->b_ffname != NULL
190#ifdef FEAT_NETBEANS_INTG
191 && netbeansReadFile
192#endif
193 )
194 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100195 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200196#ifdef UNIX
197 int save_bin = curbuf->b_p_bin;
198 int perm;
199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200#ifdef FEAT_NETBEANS_INTG
201 int oldFire = netbeansFireChanges;
202
203 netbeansFireChanges = 0;
204#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200205#ifdef UNIX
206 perm = mch_getperm(curbuf->b_ffname);
207 if (perm >= 0 && (0
208# ifdef S_ISFIFO
209 || S_ISFIFO(perm)
210# endif
211# ifdef S_ISSOCK
212 || S_ISSOCK(perm)
213# endif
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200214# ifdef OPEN_CHR_FILES
215 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
216# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200217 ))
218 read_fifo = TRUE;
219 if (read_fifo)
220 curbuf->b_p_bin = TRUE;
221#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100222 if (shortmess(SHM_FILEINFO))
223 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200225 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200226 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
227#ifdef UNIX
228 if (read_fifo)
229 {
230 curbuf->b_p_bin = save_bin;
231 if (retval == OK)
232 retval = read_buffer(FALSE, eap, flags);
233 }
234#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100235 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236#ifdef FEAT_NETBEANS_INTG
237 netbeansFireChanges = oldFire;
238#endif
239 /* Help buffer is filtered. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200240 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 fix_help_buffer();
242 }
243 else if (read_stdin)
244 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200245 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247 /*
248 * First read the text in binary mode into the buffer.
249 * Then read from that same buffer and append at the end. This makes
250 * it possible to retry when 'fileformat' or 'fileencoding' was
251 * guessed wrong.
252 */
253 curbuf->b_p_bin = TRUE;
254 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200255 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
256 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 curbuf->b_p_bin = save_bin;
258 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200259 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 }
261
262 /* if first time loading this buffer, init b_chartab[] */
263 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100266#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100267 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100268#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270
271 /*
272 * Set/reset the Changed flag first, autocmds may change the buffer.
273 * Apply the automatic commands, before processing the modelines.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200274 * So the modelines have priority over autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 */
276 /* When reading stdin, the buffer contents always needs writing, so set
277 * the changed flag. Unless in readonly mode: "ls | gview -".
278 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000279 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 || modified_was_set /* ":set modified" used in autocmd */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100281#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000284 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100286 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 unchanged(curbuf, FALSE);
288 save_file_ff(curbuf); /* keep this fileformat */
289
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100290 /* Set last_changedtick to avoid triggering a TextChanged autocommand right
291 * after it was added. */
292 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
293#ifdef FEAT_INS_EXPAND
294 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
295#endif
296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 /* require "!" to overwrite the file, because it wasn't read completely */
298#ifdef FEAT_EVAL
299 if (aborting())
300#else
301 if (got_int)
302#endif
303 curbuf->b_flags |= BF_READERR;
304
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000305#ifdef FEAT_FOLDING
306 /* Need to update automatic folding. Do this before the autocommands,
307 * they may use the fold info. */
308 foldUpdateAll(curwin);
309#endif
310
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 /* need to set w_topline, unless some autocommand already did that. */
312 if (!(curwin->w_valid & VALID_TOPLINE))
313 {
314 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100315#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100319#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100321#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#endif
324
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100325 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 /*
328 * The autocommands may have changed the current buffer. Apply the
329 * modelines to the correct buffer, if it still exists and is loaded.
330 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200331 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 {
333 aco_save_T aco;
334
335 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200336 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000337 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
339
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100340#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100342 &retval);
343#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
347 /* restore curwin/curbuf and a few other things */
348 aucmd_restbuf(&aco);
349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 }
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 return retval;
353}
354
355/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200356 * Store "buf" in "bufref" and set the free count.
357 */
358 void
359set_bufref(bufref_T *bufref, buf_T *buf)
360{
361 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200362 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200363 bufref->br_buf_free_count = buf_free_count;
364}
365
366/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200367 * Return TRUE if "bufref->br_buf" points to the same buffer as when
368 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200369 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200370 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
371 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200372 */
373 int
374bufref_valid(bufref_T *bufref)
375{
376 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200377 ? TRUE : buf_valid(bufref->br_buf)
378 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200379}
380
381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200383 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 */
385 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100386buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
388 buf_T *bp;
389
Bram Moolenaar82404332016-07-10 17:00:38 +0200390 /* Assume that we more often have a recent buffer, start with the last
391 * one. */
392 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 if (bp == buf)
394 return TRUE;
395 return FALSE;
396}
397
398/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200399 * A hash table used to quickly lookup a buffer by its number.
400 */
401static hashtab_T buf_hashtab;
402
403 static void
404buf_hashtab_add(buf_T *buf)
405{
406 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
407 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
408 EMSG(_("E931: Buffer cannot be registered"));
409}
410
411 static void
412buf_hashtab_remove(buf_T *buf)
413{
414 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
415
416 if (!HASHITEM_EMPTY(hi))
417 hash_remove(&buf_hashtab, hi);
418}
419
Bram Moolenaara997b452018-04-17 23:24:06 +0200420static char *e_buflocked = N_("E937: Attempt to delete a buffer that is in use");
421
Bram Moolenaar480778b2016-07-14 22:09:39 +0200422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 * Close the link to a buffer.
424 * "action" is used when there is no longer a window for the buffer.
425 * It can be:
426 * 0 buffer becomes hidden
427 * DOBUF_UNLOAD buffer is unloaded
428 * DOBUF_DELETE buffer is unloaded and removed from buffer list
429 * DOBUF_WIPE buffer is unloaded and really deleted
430 * When doing all but the first one on the current buffer, the caller should
431 * get a new buffer very soon!
432 *
433 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100434 *
435 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
436 * cause there to be only one window with this buffer. e.g. when ":quit" is
437 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 */
439 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100440close_buffer(
441 win_T *win, /* if not NULL, set b_last_cursor */
442 buf_T *buf,
443 int action,
444 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100447 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200448 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100449 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200450 win_T *the_curwin = curwin;
451 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 int unload_buf = (action != 0);
453 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
454 int wipe_buf = (action == DOBUF_WIPE);
455
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200456 /*
457 * Force unloading or deleting when 'bufhidden' says so.
458 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
459 * "hide" (otherwise we could never free or delete a buffer).
460 */
461 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
462 {
463 del_buf = TRUE;
464 unload_buf = TRUE;
465 }
466 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
467 {
468 del_buf = TRUE;
469 unload_buf = TRUE;
470 wipe_buf = TRUE;
471 }
472 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
473 unload_buf = TRUE;
474
Bram Moolenaar94053a52017-08-01 21:44:33 +0200475#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200476 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200477 {
478 if (term_job_running(buf->b_term))
479 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200480 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200481 {
482 if (buf->b_locked)
483 {
484 EMSG(_(e_buflocked));
485 return;
486 }
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200487 /* Wiping out or unloading a terminal buffer kills the job. */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200488 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200489 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200490 else
491 {
492 /* The job keeps running, hide the buffer. */
493 del_buf = FALSE;
494 unload_buf = FALSE;
495 }
496 }
497 else
498 {
499 /* A terminal buffer is wiped out if the job has finished. */
500 del_buf = TRUE;
501 unload_buf = TRUE;
502 wipe_buf = TRUE;
503 }
504 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200507 /* Disallow deleting the buffer when it is locked (already being closed or
508 * halfway a command that relies on it). Unloading is allowed. */
509 if (buf->b_locked > 0 && (del_buf || wipe_buf))
510 {
Bram Moolenaara997b452018-04-17 23:24:06 +0200511 EMSG(_(e_buflocked));
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200512 return;
513 }
514
Bram Moolenaar4033c552017-09-16 20:54:51 +0200515 /* check no autocommands closed the window */
516 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 {
518 /* Set b_last_cursor when closing the last window for the buffer.
519 * Remember the last cursor position and window options of the buffer.
520 * This used to be only for the current window, but then options like
521 * 'foldmethod' may be lost with a ":only" command. */
522 if (buf->b_nwindows == 1)
523 set_last_cursor(win);
524 buflist_setfpos(buf, win,
525 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
526 win->w_cursor.col, TRUE);
527 }
528
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200529 set_bufref(&bufref, buf);
530
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 /* When the buffer is no longer in a window, trigger BufWinLeave */
532 if (buf->b_nwindows == 1)
533 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200534 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200535 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
536 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200537 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100538 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200539 /* Autocommands deleted the buffer. */
540aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100541 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100543 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200544 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200545 if (abort_if_last && one_window())
546 /* Autocommands made this the only window. */
547 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
549 /* When the buffer becomes hidden, but is not unloaded, trigger
550 * BufHidden */
551 if (!unload_buf)
552 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200553 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200554 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
555 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200556 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200557 /* Autocommands deleted the buffer. */
558 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200559 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200560 if (abort_if_last && one_window())
561 /* Autocommands made this the only window. */
562 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100564#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 if (aborting()) /* autocmds may abort script processing */
566 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200569
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200570 /* If the buffer was in curwin and the window has changed, go back to that
571 * window, if it still exists. This avoids that ":edit x" triggering a
572 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
573 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
574 {
575 block_autocmds();
576 goto_tabpage_win(the_curtab, the_curwin);
577 unblock_autocmds();
578 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
582 /* decrease the link count from windows (unless not in any window) */
583 if (buf->b_nwindows > 0)
584 --buf->b_nwindows;
585
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100586#ifdef FEAT_DIFF
587 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100588 diff_buf_delete(buf); /* Clear 'diff' for hidden buffer. */
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100589#endif
590
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 /* Return when a window is displaying the buffer or when it's not
592 * unloaded. */
593 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
596 /* Always remove the buffer when there is no file name. */
597 if (buf->b_ffname == NULL)
598 del_buf = TRUE;
599
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200600 /* When closing the current buffer stop Visual mode before freeing
601 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200602 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200603#if defined(EXITFREE)
604 && !entered_free_all_mem
605#endif
606 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200607 end_visual_mode();
608
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 /*
610 * Free all things allocated for this buffer.
611 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 /* Remember if we are closing the current buffer. Restore the number of
614 * windows, so that autocommands in buf_freeall() don't get confused. */
615 is_curbuf = (buf == curbuf);
616 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200618 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200621 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100623#ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000624 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 /*
629 * It's possible that autocommands change curbuf to the one being deleted.
630 * This might cause the previous curbuf to be deleted unexpectedly. But
631 * in some cases it's OK to delete the curbuf, because a new one is
632 * obtained anyway. Therefore only return if curbuf changed to the
633 * deleted buffer.
634 */
635 if (buf == curbuf && !is_curbuf)
636 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200637
Bram Moolenaar4033c552017-09-16 20:54:51 +0200638 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200639 win->w_buffer = NULL; /* make sure we don't use the buffer now */
640
641 /* Autocommands may have opened or closed windows for this buffer.
642 * Decrement the count for the close we do here. */
643 if (buf->b_nwindows > 0)
644 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 /*
647 * Remove the buffer from the list.
648 */
649 if (wipe_buf)
650 {
651#ifdef FEAT_SUN_WORKSHOP
652 if (usingSunWorkShop)
653 workshop_file_closed_lineno((char *)buf->b_ffname,
654 (int)buf->b_last_cursor.lnum);
655#endif
656 vim_free(buf->b_ffname);
657 vim_free(buf->b_sfname);
658 if (buf->b_prev == NULL)
659 firstbuf = buf->b_next;
660 else
661 buf->b_prev->b_next = buf->b_next;
662 if (buf->b_next == NULL)
663 lastbuf = buf->b_prev;
664 else
665 buf->b_next->b_prev = buf->b_prev;
666 free_buffer(buf);
667 }
668 else
669 {
670 if (del_buf)
671 {
672 /* Free all internal variables and reset option values, to make
673 * ":bdel" compatible with Vim 5.7. */
674 free_buffer_stuff(buf, TRUE);
675
676 /* Make it look like a new buffer. */
677 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
678
679 /* Init the options when loaded again. */
680 buf->b_p_initialized = FALSE;
681 }
682 buf_clear_file(buf);
683 if (del_buf)
684 buf->b_p_bl = FALSE;
685 }
686}
687
688/*
689 * Make buffer not contain a file.
690 */
691 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100692buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693{
694 buf->b_ml.ml_line_count = 1;
695 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 buf->b_p_eol = TRUE;
698 buf->b_start_eol = TRUE;
699#ifdef FEAT_MBYTE
700 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000701 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702#endif
703 buf->b_ml.ml_mfp = NULL;
704 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
705#ifdef FEAT_NETBEANS_INTG
706 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#endif
708}
709
710/*
711 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200712 * the file. Careful: get here with "curwin" NULL when exiting.
713 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200714 * BFA_DEL buffer is going to be deleted
715 * BFA_WIPE buffer is going to be wiped out
716 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100719buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200722 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200723 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200724 win_T *the_curwin = curwin;
725 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726
Bram Moolenaar5a497892016-09-03 16:29:04 +0200727 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200728 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200729 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200730 if (buf->b_ml.ml_mfp != NULL)
731 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200732 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
733 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200734 && !bufref_valid(&bufref))
735 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200736 return;
737 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200738 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200740 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
741 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200742 && !bufref_valid(&bufref))
743 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 return;
745 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200746 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200748 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
749 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200750 && !bufref_valid(&bufref))
751 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 return;
753 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200754 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200755
Bram Moolenaar5a497892016-09-03 16:29:04 +0200756 /* If the buffer was in curwin and the window has changed, go back to that
757 * window, if it still exists. This avoids that ":edit x" triggering a
758 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
759 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
760 {
761 block_autocmds();
762 goto_tabpage_win(the_curtab, the_curwin);
763 unblock_autocmds();
764 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200765
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100766#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000767 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770
771 /*
772 * It's possible that autocommands change curbuf to the one being deleted.
773 * This might cause curbuf to be deleted unexpectedly. But in some cases
774 * it's OK to delete the curbuf, because a new one is obtained anyway.
775 * Therefore only return if curbuf changed to the deleted buffer.
776 */
777 if (buf == curbuf && !is_curbuf)
778 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779#ifdef FEAT_DIFF
780 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
781#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200782#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100783 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200784 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100785 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200786#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000787
788#ifdef FEAT_FOLDING
789 /* No folds in an empty buffer. */
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000790 {
791 win_T *win;
792 tabpage_T *tp;
793
794 FOR_ALL_TAB_WINDOWS(tp, win)
795 if (win->w_buffer == buf)
796 clearFolding(win);
797 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000798#endif
799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800#ifdef FEAT_TCL
801 tcl_buffer_free(buf);
802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 ml_close(buf, TRUE); /* close and delete the memline/memfile */
804 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200805 if ((flags & BFA_KEEP_UNDO) == 0)
806 {
807 u_blockfree(buf); /* free the memory allocated for undo */
808 u_clearall(buf); /* reset all undo information */
809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200811 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000813 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814}
815
816/*
817 * Free a buffer structure and the things it contains related to the buffer
818 * itself (not the file, that must have been done already).
819 */
820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100821free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200823 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200825#ifdef FEAT_EVAL
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200826 /* b:changedtick uses an item in buf_T, remove it now */
827 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200828 unref_var_dict(buf->b_vars);
829#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200830#ifdef FEAT_LUA
831 lua_buffer_free(buf);
832#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000833#ifdef FEAT_MZSCHEME
834 mzscheme_buffer_free(buf);
835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836#ifdef FEAT_PERL
837 perl_buf_free(buf);
838#endif
839#ifdef FEAT_PYTHON
840 python_buffer_free(buf);
841#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200842#ifdef FEAT_PYTHON3
843 python3_buffer_free(buf);
844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#ifdef FEAT_RUBY
846 ruby_buffer_free(buf);
847#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200848#ifdef FEAT_JOB_CHANNEL
849 channel_buffer_free(buf);
850#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200851#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200852 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200853#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200854#ifdef FEAT_JOB_CHANNEL
855 vim_free(buf->b_prompt_text);
856 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
857#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200858
859 buf_hashtab_remove(buf);
860
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200861 aubuflocal_remove(buf);
862
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200863 if (autocmd_busy)
864 {
865 /* Do not free the buffer structure while autocommands are executing,
866 * it's still needed. Free it when autocmd_busy is reset. */
867 buf->b_next = au_pending_free_buf;
868 au_pending_free_buf = buf;
869 }
870 else
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200871 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872}
873
874/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100875 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100876 */
877 static void
878init_changedtick(buf_T *buf)
879{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100880 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100881
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100882 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
883 di->di_tv.v_type = VAR_NUMBER;
884 di->di_tv.v_lock = VAR_FIXED;
885 di->di_tv.vval.v_number = 0;
886
Bram Moolenaar92769c32017-02-25 15:41:37 +0100887#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100888 STRCPY(buf->b_ct_di.di_key, "changedtick");
889 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100890#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100891}
892
893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
895 */
896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100897free_buffer_stuff(
898 buf_T *buf,
899 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900{
901 if (free_options)
902 {
903 clear_wininfo(buf); /* including window-local options */
904 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200905#ifdef FEAT_SPELL
906 ga_clear(&buf->b_s.b_langp);
907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 }
909#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100910 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100911 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100912
913 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
914 hash_init(&buf->b_vars->dv_hashtab);
915 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100916 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918#endif
919#ifdef FEAT_USR_CMDS
920 uc_clear(&buf->b_ucmds); /* clear local user commands */
921#endif
922#ifdef FEAT_SIGNS
923 buf_delete_signs(buf); /* delete any signs */
924#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000925#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200926 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928#ifdef FEAT_LOCALMAP
929 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
930 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
931#endif
932#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +0100933 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934#endif
935}
936
937/*
938 * Free the b_wininfo list for buffer "buf".
939 */
940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100941clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942{
943 wininfo_T *wip;
944
945 while (buf->b_wininfo != NULL)
946 {
947 wip = buf->b_wininfo;
948 buf->b_wininfo = wip->wi_next;
949 if (wip->wi_optset)
950 {
951 clear_winopt(&wip->wi_opt);
952#ifdef FEAT_FOLDING
953 deleteFoldRecurse(&wip->wi_folds);
954#endif
955 }
956 vim_free(wip);
957 }
958}
959
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960/*
961 * Go to another buffer. Handles the result of the ATTENTION dialog.
962 */
963 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100964goto_buffer(
965 exarg_T *eap,
966 int start,
967 int dir,
968 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200970#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200971 bufref_T old_curbuf;
972
973 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
975 swap_exists_action = SEA_DIALOG;
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
978 start, dir, count, eap->forceit);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200979#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
981 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200982# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000983 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000984
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000985 /* Reset the error/interrupt/exception state here so that
986 * aborting() returns FALSE when closing a window. */
987 enter_cleanup(&cs);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200988# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000989
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000990 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 win_close(curwin, TRUE);
992 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000993 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000994
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200995# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000996 /* Restore the error/interrupt/exception state if not discarded by a
997 * new aborting error, interrupt, or uncaught exception. */
998 leave_cleanup(&cs);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 }
1001 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001002 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003#endif
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001004}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
Bram Moolenaare64ac772005-12-07 20:54:59 +00001006#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007/*
1008 * Handle the situation of swap_exists_action being set.
1009 * It is allowed for "old_curbuf" to be NULL or invalid.
1010 */
1011 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001012handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001014# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001015 cleanup_T cs;
1016# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001017# ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001018 long old_tw = curbuf->b_p_tw;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001019# endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001020 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001021
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (swap_exists_action == SEA_QUIT)
1023 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001024# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001025 /* Reset the error/interrupt/exception state here so that
1026 * aborting() returns FALSE when closing a buffer. */
1027 enter_cleanup(&cs);
1028# endif
1029
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 /* User selected Quit at ATTENTION prompt. Go back to previous
1031 * buffer. If that buffer is gone or the same as the current one,
1032 * open a new, empty buffer. */
1033 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001034 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001035 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001036 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1037 || old_curbuf->br_buf == curbuf)
1038 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1039 else
1040 buf = old_curbuf->br_buf;
1041 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001042 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001043 enter_buffer(buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001044# ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001045 if (old_tw != curbuf->b_p_tw)
1046 check_colorcolumn(curwin);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001047# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001050
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001051# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001052 /* Restore the error/interrupt/exception state if not discarded by a
1053 * new aborting error, interrupt, or uncaught exception. */
1054 leave_cleanup(&cs);
1055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 }
1057 else if (swap_exists_action == SEA_RECOVER)
1058 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001059# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001060 /* Reset the error/interrupt/exception state here so that
1061 * aborting() returns FALSE when closing a buffer. */
1062 enter_cleanup(&cs);
1063# endif
1064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 /* User selected Recover at ATTENTION prompt. */
1066 msg_scroll = TRUE;
1067 ml_recover();
1068 MSG_PUTS("\n"); /* don't overwrite the last message */
1069 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001070 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001071
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001072# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001073 /* Restore the error/interrupt/exception state if not discarded by a
1074 * new aborting error, interrupt, or uncaught exception. */
1075 leave_cleanup(&cs);
1076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 }
1078 swap_exists_action = SEA_NONE;
1079}
1080#endif
1081
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082/*
1083 * do_bufdel() - delete or unload buffer(s)
1084 *
1085 * addr_count == 0: ":bdel" - delete current buffer
1086 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1087 * buffer "end_bnr", then any other arguments.
1088 * addr_count == 2: ":N,N bdel" - delete buffers in range
1089 *
1090 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1091 * DOBUF_DEL (":bdel")
1092 *
1093 * Returns error message or NULL
1094 */
1095 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001096do_bufdel(
1097 int command,
1098 char_u *arg, /* pointer to extra arguments */
1099 int addr_count,
1100 int start_bnr, /* first buffer number in a range */
1101 int end_bnr, /* buffer nr or last buffer nr in a range */
1102 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103{
1104 int do_current = 0; /* delete current buffer? */
1105 int deleted = 0; /* number of buffers deleted */
1106 char_u *errormsg = NULL; /* return value */
1107 int bnr; /* buffer number */
1108 char_u *p;
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 if (addr_count == 0)
1111 {
1112 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1113 }
1114 else
1115 {
1116 if (addr_count == 2)
1117 {
1118 if (*arg) /* both range and argument is not allowed */
1119 return (char_u *)_(e_trailing);
1120 bnr = start_bnr;
1121 }
1122 else /* addr_count == 1 */
1123 bnr = end_bnr;
1124
1125 for ( ;!got_int; ui_breakcheck())
1126 {
1127 /*
1128 * delete the current buffer last, otherwise when the
1129 * current buffer is deleted, the next buffer becomes
1130 * the current one and will be loaded, which may then
1131 * also be deleted, etc.
1132 */
1133 if (bnr == curbuf->b_fnum)
1134 do_current = bnr;
1135 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1136 forceit) == OK)
1137 ++deleted;
1138
1139 /*
1140 * find next buffer number to delete/unload
1141 */
1142 if (addr_count == 2)
1143 {
1144 if (++bnr > end_bnr)
1145 break;
1146 }
1147 else /* addr_count == 1 */
1148 {
1149 arg = skipwhite(arg);
1150 if (*arg == NUL)
1151 break;
1152 if (!VIM_ISDIGIT(*arg))
1153 {
1154 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001155 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1156 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if (bnr < 0) /* failed */
1158 break;
1159 arg = p;
1160 }
1161 else
1162 bnr = getdigits(&arg);
1163 }
1164 }
1165 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1166 FORWARD, do_current, forceit) == OK)
1167 ++deleted;
1168
1169 if (deleted == 0)
1170 {
1171 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001172 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001174 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001176 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 errormsg = IObuff;
1178 }
1179 else if (deleted >= p_report)
1180 {
1181 if (command == DOBUF_UNLOAD)
1182 {
1183 if (deleted == 1)
1184 MSG(_("1 buffer unloaded"));
1185 else
1186 smsg((char_u *)_("%d buffers unloaded"), deleted);
1187 }
1188 else if (command == DOBUF_DEL)
1189 {
1190 if (deleted == 1)
1191 MSG(_("1 buffer deleted"));
1192 else
1193 smsg((char_u *)_("%d buffers deleted"), deleted);
1194 }
1195 else
1196 {
1197 if (deleted == 1)
1198 MSG(_("1 buffer wiped out"));
1199 else
1200 smsg((char_u *)_("%d buffers wiped out"), deleted);
1201 }
1202 }
1203 }
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205
1206 return errormsg;
1207}
1208
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001209static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001210
1211/*
1212 * Make the current buffer empty.
1213 * Used when it is wiped out and it's the last buffer.
1214 */
1215 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001216empty_curbuf(
1217 int close_others,
1218 int forceit,
1219 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001220{
1221 int retval;
1222 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001223 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001224
1225 if (action == DOBUF_UNLOAD)
1226 {
1227 EMSG(_("E90: Cannot unload last buffer"));
1228 return FAIL;
1229 }
1230
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001231 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001232 if (close_others)
1233 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001234 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001235
1236 setpcmark();
1237 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1238 forceit ? ECMD_FORCEIT : 0, curwin);
1239
1240 /*
1241 * do_ecmd() may create a new buffer, then we have to delete
1242 * the old one. But do_ecmd() may have done that already, check
1243 * if the buffer still exists.
1244 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001245 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001246 close_buffer(NULL, buf, action, FALSE);
1247 if (!close_others)
1248 need_fileinfo = FALSE;
1249 return retval;
1250}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251/*
1252 * Implementation of the commands for the buffer list.
1253 *
1254 * action == DOBUF_GOTO go to specified buffer
1255 * action == DOBUF_SPLIT split window and go to specified buffer
1256 * action == DOBUF_UNLOAD unload specified buffer(s)
1257 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1258 * action == DOBUF_WIPE delete specified buffer(s) really
1259 *
1260 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1261 * start == DOBUF_FIRST go to "count" buffer from first buffer
1262 * start == DOBUF_LAST go to "count" buffer from last buffer
1263 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1264 *
1265 * Return FAIL or OK.
1266 */
1267 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001268do_buffer(
1269 int action,
1270 int start,
1271 int dir, /* FORWARD or BACKWARD */
1272 int count, /* buffer number or number of buffers */
1273 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275 buf_T *buf;
1276 buf_T *bp;
1277 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1278 || action == DOBUF_WIPE);
1279
1280 switch (start)
1281 {
1282 case DOBUF_FIRST: buf = firstbuf; break;
1283 case DOBUF_LAST: buf = lastbuf; break;
1284 default: buf = curbuf; break;
1285 }
1286 if (start == DOBUF_MOD) /* find next modified buffer */
1287 {
1288 while (count-- > 0)
1289 {
1290 do
1291 {
1292 buf = buf->b_next;
1293 if (buf == NULL)
1294 buf = firstbuf;
1295 }
1296 while (buf != curbuf && !bufIsChanged(buf));
1297 }
1298 if (!bufIsChanged(buf))
1299 {
1300 EMSG(_("E84: No modified buffer found"));
1301 return FAIL;
1302 }
1303 }
1304 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1305 {
1306 while (buf != NULL && buf->b_fnum != count)
1307 buf = buf->b_next;
1308 }
1309 else
1310 {
1311 bp = NULL;
1312 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1313 {
1314 /* remember the buffer where we start, we come back there when all
1315 * buffers are unlisted. */
1316 if (bp == NULL)
1317 bp = buf;
1318 if (dir == FORWARD)
1319 {
1320 buf = buf->b_next;
1321 if (buf == NULL)
1322 buf = firstbuf;
1323 }
1324 else
1325 {
1326 buf = buf->b_prev;
1327 if (buf == NULL)
1328 buf = lastbuf;
1329 }
1330 /* don't count unlisted buffers */
1331 if (unload || buf->b_p_bl)
1332 {
1333 --count;
1334 bp = NULL; /* use this buffer as new starting point */
1335 }
1336 if (bp == buf)
1337 {
1338 /* back where we started, didn't find anything. */
1339 EMSG(_("E85: There is no listed buffer"));
1340 return FAIL;
1341 }
1342 }
1343 }
1344
1345 if (buf == NULL) /* could not find it */
1346 {
1347 if (start == DOBUF_FIRST)
1348 {
1349 /* don't warn when deleting */
1350 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001351 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353 else if (dir == FORWARD)
1354 EMSG(_("E87: Cannot go beyond last buffer"));
1355 else
1356 EMSG(_("E88: Cannot go before first buffer"));
1357 return FAIL;
1358 }
1359
1360#ifdef FEAT_GUI
1361 need_mouse_correct = TRUE;
1362#endif
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 /*
1365 * delete buffer buf from memory and/or the list
1366 */
1367 if (unload)
1368 {
1369 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001370 bufref_T bufref;
1371
Bram Moolenaara997b452018-04-17 23:24:06 +02001372 if (buf->b_locked)
1373 {
1374 EMSG(_(e_buflocked));
1375 return FAIL;
1376 }
1377
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001378 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379
1380 /* When unloading or deleting a buffer that's already unloaded and
1381 * unlisted: fail silently. */
1382 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1383 return FAIL;
1384
1385 if (!forceit && bufIsChanged(buf))
1386 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001387#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 if ((p_confirm || cmdmod.confirm) && p_write)
1389 {
1390 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001391 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 /* Autocommand deleted buffer, oops! It's not changed
1393 * now. */
1394 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001395 /* If it's still changed fail silently, the dialog already
1396 * mentioned why it fails. */
1397 if (bufIsChanged(buf))
1398 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001400 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 {
1403 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1404 buf->b_fnum);
1405 return FAIL;
1406 }
1407 }
1408
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001409 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001410 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001411 end_visual_mode();
1412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 /*
1414 * If deleting the last (listed) buffer, make it empty.
1415 * The last (listed) buffer cannot be unloaded.
1416 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001417 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 if (bp->b_p_bl && bp != buf)
1419 break;
1420 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001421 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 /*
1424 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001425 * (unless it's the only window). Repeat this so long as we end up in
1426 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001428 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001429 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001430 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001431 {
1432 if (win_close(curwin, FALSE) == FAIL)
1433 break;
1434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
1436 /*
1437 * If the buffer to be deleted is not the current one, delete it here.
1438 */
1439 if (buf != curbuf)
1440 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001441 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001442 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001443 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 return OK;
1445 }
1446
1447 /*
1448 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001449 * There should be another, otherwise it would have been handled
1450 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001451 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 * Then prefer the buffer we most recently visited.
1453 * Else try to find one that is loaded, after the current buffer,
1454 * then before the current buffer.
1455 * Finally use any buffer.
1456 */
1457 buf = NULL; /* selected buffer */
1458 bp = NULL; /* used when no loaded buffer found */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001459 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1460 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001462 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 {
1464 int jumpidx;
1465
1466 jumpidx = curwin->w_jumplistidx - 1;
1467 if (jumpidx < 0)
1468 jumpidx = curwin->w_jumplistlen - 1;
1469
1470 forward = jumpidx;
1471 while (jumpidx != curwin->w_jumplistidx)
1472 {
1473 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1474 if (buf != NULL)
1475 {
1476 if (buf == curbuf || !buf->b_p_bl)
1477 buf = NULL; /* skip current and unlisted bufs */
1478 else if (buf->b_ml.ml_mfp == NULL)
1479 {
1480 /* skip unloaded buf, but may keep it for later */
1481 if (bp == NULL)
1482 bp = buf;
1483 buf = NULL;
1484 }
1485 }
1486 if (buf != NULL) /* found a valid buffer: stop searching */
1487 break;
1488 /* advance to older entry in jump list */
1489 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1490 break;
1491 if (--jumpidx < 0)
1492 jumpidx = curwin->w_jumplistlen - 1;
1493 if (jumpidx == forward) /* List exhausted for sure */
1494 break;
1495 }
1496 }
1497#endif
1498
1499 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1500 {
1501 forward = TRUE;
1502 buf = curbuf->b_next;
1503 for (;;)
1504 {
1505 if (buf == NULL)
1506 {
1507 if (!forward) /* tried both directions */
1508 break;
1509 buf = curbuf->b_prev;
1510 forward = FALSE;
1511 continue;
1512 }
1513 /* in non-help buffer, try to skip help buffers, and vv */
1514 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1515 {
1516 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1517 break;
1518 if (bp == NULL) /* remember unloaded buf for later */
1519 bp = buf;
1520 }
1521 if (forward)
1522 buf = buf->b_next;
1523 else
1524 buf = buf->b_prev;
1525 }
1526 }
1527 if (buf == NULL) /* No loaded buffer, use unloaded one */
1528 buf = bp;
1529 if (buf == NULL) /* No loaded buffer, find listed one */
1530 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001531 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 if (buf->b_p_bl && buf != curbuf)
1533 break;
1534 }
1535 if (buf == NULL) /* Still no buffer, just take one */
1536 {
1537 if (curbuf->b_next != NULL)
1538 buf = curbuf->b_next;
1539 else
1540 buf = curbuf->b_prev;
1541 }
1542 }
1543
Bram Moolenaara02471e2014-01-10 16:43:14 +01001544 if (buf == NULL)
1545 {
1546 /* Autocommands must have wiped out all other buffers. Only option
1547 * now is to make the current buffer empty. */
1548 return empty_curbuf(FALSE, forceit, action);
1549 }
1550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 /*
1552 * make buf current buffer
1553 */
1554 if (action == DOBUF_SPLIT) /* split window first */
1555 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001556 /* If 'switchbuf' contains "useopen": jump to first window containing
1557 * "buf" if one exists */
1558 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001559 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001560 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001561 * page containing "buf" if one exists */
1562 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 return OK;
1564 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 return FAIL;
1566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
1568 /* go to current buffer - nothing to do */
1569 if (buf == curbuf)
1570 return OK;
1571
1572 /*
1573 * Check if the current buffer may be abandoned.
1574 */
1575 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1576 {
1577#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1578 if ((p_confirm || cmdmod.confirm) && p_write)
1579 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001580 bufref_T bufref;
1581
1582 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001584 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 /* Autocommand deleted buffer, oops! */
1586 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 }
1588 if (bufIsChanged(curbuf))
1589#endif
1590 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001591 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 return FAIL;
1593 }
1594 }
1595
1596 /* Go to the other buffer. */
1597 set_curbuf(buf, action);
1598
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001600 {
1601 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001604#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 if (aborting()) /* autocmds may abort script processing */
1606 return FAIL;
1607#endif
1608
1609 return OK;
1610}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
1612/*
1613 * Set current buffer to "buf". Executes autocommands and closes current
1614 * buffer. "action" tells how to close the current buffer:
1615 * DOBUF_GOTO free or hide it
1616 * DOBUF_SPLIT nothing
1617 * DOBUF_UNLOAD unload it
1618 * DOBUF_DEL delete it
1619 * DOBUF_WIPE wipe it out
1620 */
1621 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001622set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
1624 buf_T *prevbuf;
1625 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1626 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001627#ifdef FEAT_SYN_HL
1628 long old_tw = curbuf->b_p_tw;
1629#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001630 bufref_T newbufref;
1631 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
1633 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001634 if (!cmdmod.keepalt)
1635 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001636 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 /* Don't restart Select mode after switching to another buffer. */
1639 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001641 /* close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
1642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001644 set_bufref(&prevbufref, prevbuf);
1645 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001647 /* Autocommands may delete the curren buffer and/or the buffer we wan to go
1648 * to. In those cases don't close the buffer. */
Bram Moolenaar82404332016-07-10 17:00:38 +02001649 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001650 || (bufref_valid(&prevbufref)
1651 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001652#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001653 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001655 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001657#ifdef FEAT_SYN_HL
1658 if (prevbuf == curwin->w_buffer)
1659 reset_synblock(curwin);
1660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001662 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001663#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001664 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001666 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001668 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001669 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001670 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001671 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1673 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001674 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001675 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001676 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001677 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001678 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001681 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001682 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001683 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1684 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001685#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001686 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001688 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001689 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001691#ifdef FEAT_SYN_HL
1692 if (old_tw != curbuf->b_p_tw)
1693 check_colorcolumn(curwin);
1694#endif
1695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696}
1697
1698/*
1699 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001700 * Old curbuf must have been abandoned already! This also means "curbuf" may
1701 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 */
1703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001704enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705{
1706 /* Copy buffer and window local option values. Not for a help buffer. */
1707 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1708 if (!buf->b_help)
1709 get_winopts(buf);
1710#ifdef FEAT_FOLDING
1711 else
1712 /* Remove all folds in the window. */
1713 clearFolding(curwin);
1714 foldUpdateAll(curwin); /* update folds (later). */
1715#endif
1716
1717 /* Get the buffer in the current window. */
1718 curwin->w_buffer = buf;
1719 curbuf = buf;
1720 ++curbuf->b_nwindows;
1721
1722#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001723 if (curwin->w_p_diff)
1724 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#endif
1726
Bram Moolenaara971b822011-09-14 14:43:25 +02001727#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001728 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001729#endif
1730
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 /* Cursor on first line by default. */
1732 curwin->w_cursor.lnum = 1;
1733 curwin->w_cursor.col = 0;
1734#ifdef FEAT_VIRTUALEDIT
1735 curwin->w_cursor.coladd = 0;
1736#endif
1737 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001738 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001740 /* mark cursor position as being invalid */
1741 curwin->w_valid = 0;
1742
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 /* Make sure the buffer is loaded. */
1744 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001745 {
1746 /* If there is no filetype, allow for detecting one. Esp. useful for
1747 * ":ball" used in a autocommand. If there already is a filetype we
1748 * might prefer to keep it. */
1749 if (*curbuf->b_p_ft == NUL)
1750 did_filetype = FALSE;
1751
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001752 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 else
1755 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001756 if (!msg_silent)
1757 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001760#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1764 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 }
1766
1767 /* If autocommands did not change the cursor position, restore cursor lnum
1768 * and possibly cursor col. */
1769 if (curwin->w_cursor.lnum == 1 && inindent(0))
1770 buflist_getfpos();
1771
1772 check_arg_idx(curwin); /* check for valid arg_idx */
1773#ifdef FEAT_TITLE
1774 maketitle();
1775#endif
Bram Moolenaard4153d42008-11-15 15:06:17 +00001776 /* when autocmds didn't change it */
1777 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1779
Bram Moolenaar009b2592004-10-24 19:18:58 +00001780#ifdef FEAT_NETBEANS_INTG
1781 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001782 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001783#endif
1784
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001785 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02001786 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
1788#ifdef FEAT_KEYMAP
1789 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001790 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001792#ifdef FEAT_SPELL
1793 /* May need to set the spell language. Can only do this after the buffer
1794 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001795 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1796 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001797#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001798#ifdef FEAT_VIMINFO
1799 curbuf->b_last_used = vim_time();
1800#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 redraw_later(NOT_VALID);
1803}
1804
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001805#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1806/*
1807 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001808 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001809 */
1810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001811do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001812{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001813 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001814 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001815 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001816 shorten_fnames(TRUE);
1817}
1818#endif
1819
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001820 void
1821no_write_message(void)
1822{
1823#ifdef FEAT_TERMINAL
1824 if (term_job_running(curbuf->b_term))
1825 EMSG(_("E948: Job still running (add ! to end the job)"));
1826 else
1827#endif
1828 EMSG(_("E37: No write since last change (add ! to override)"));
1829}
1830
1831 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001832no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001833{
1834#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001835 if (term_job_running(buf->b_term))
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001836 EMSG(_("E948: Job still running"));
1837 else
1838#endif
1839 EMSG(_("E37: No write since last change"));
1840}
1841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842/*
1843 * functions for dealing with the buffer list
1844 */
1845
Bram Moolenaar480778b2016-07-14 22:09:39 +02001846static int top_file_num = 1; /* highest file number */
1847
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001849 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1850 * only one window. That means it can be re-used.
1851 */
1852 int
1853curbuf_reusable(void)
1854{
1855 return (curbuf != NULL
1856 && curbuf->b_ffname == NULL
1857 && curbuf->b_nwindows <= 1
1858 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
1859 && !curbufIsChanged());
1860}
1861
1862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 * Add a file name to the buffer list. Return a pointer to the buffer.
1864 * If the same file name already exists return a pointer to that buffer.
1865 * If it does not exist, or if fname == NULL, a new entry is created.
1866 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1867 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1868 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001869 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001870 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1871 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 * This is the ONLY way to create a new buffer.
1873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001875buflist_new(
1876 char_u *ffname, /* full path of fname or relative */
1877 char_u *sfname, /* short fname or NULL */
1878 linenr_T lnum, /* preferred cursor line */
1879 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880{
1881 buf_T *buf;
1882#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001883 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884#endif
1885
Bram Moolenaar480778b2016-07-14 22:09:39 +02001886 if (top_file_num == 1)
1887 hash_init(&buf_hashtab);
1888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1890
1891 /*
1892 * If file name already exists in the list, update the entry.
1893 */
1894#ifdef UNIX
1895 /* On Unix we can use inode numbers when the file exists. Works better
1896 * for hard links. */
1897 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1898 st.st_dev = (dev_T)-1;
1899#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001900 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901#ifdef UNIX
1902 buflist_findname_stat(ffname, &st)
1903#else
1904 buflist_findname(ffname)
1905#endif
1906 ) != NULL)
1907 {
1908 vim_free(ffname);
1909 if (lnum != 0)
1910 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001911
1912 if ((flags & BLN_NOOPT) == 0)
1913 /* copy the options now, if 'cpo' doesn't have 's' and not done
1914 * already */
1915 buf_copy_options(buf, 0);
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1918 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001919 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001920
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001922 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001924 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001925 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001926 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001927 return NULL;
1928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 }
1930 return buf;
1931 }
1932
1933 /*
1934 * If the current buffer has no name and no contents, use the current
1935 * buffer. Otherwise: Need to allocate a new buffer structure.
1936 *
1937 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001938 * (A spell file buffer is allocated in spell.c, but that's not a normal
1939 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 */
1941 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001942 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 {
1944 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 /* It's like this buffer is deleted. Watch out for autocommands that
1946 * change curbuf! If that happens, allocate a new buffer anyway. */
1947 if (curbuf->b_p_bl)
1948 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1949 if (buf == curbuf)
1950 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001951#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001952 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 /* Make sure 'bufhidden' and 'buftype' are empty */
1958 clear_string_option(&buf->b_p_bh);
1959 clear_string_option(&buf->b_p_bt);
1960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 }
1962 if (buf != curbuf || curbuf == NULL)
1963 {
1964 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1965 if (buf == NULL)
1966 {
1967 vim_free(ffname);
1968 return NULL;
1969 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001970#ifdef FEAT_EVAL
1971 /* init b: variables */
1972 buf->b_vars = dict_alloc();
1973 if (buf->b_vars == NULL)
1974 {
1975 vim_free(ffname);
1976 vim_free(buf);
1977 return NULL;
1978 }
1979 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1980#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001981 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 }
1983
1984 if (ffname != NULL)
1985 {
1986 buf->b_ffname = ffname;
1987 buf->b_sfname = vim_strsave(sfname);
1988 }
1989
1990 clear_wininfo(buf);
1991 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1992
1993 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1994 || buf->b_wininfo == NULL)
1995 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001996 VIM_CLEAR(buf->b_ffname);
1997 VIM_CLEAR(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 if (buf != curbuf)
1999 free_buffer(buf);
2000 return NULL;
2001 }
2002
2003 if (buf == curbuf)
2004 {
2005 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002006 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 if (buf != curbuf) /* autocommands deleted the buffer! */
2008 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002009#if defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002010 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 return NULL;
2012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002014
2015 /* Init the options. */
2016 buf->b_p_initialized = FALSE;
2017 buf_copy_options(buf, BCO_ENTER);
2018
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019#ifdef FEAT_KEYMAP
2020 /* need to reload lmaps and set b:keymap_name */
2021 curbuf->b_kmap_state |= KEYMAP_INIT;
2022#endif
2023 }
2024 else
2025 {
2026 /*
2027 * put new buffer at the end of the buffer list
2028 */
2029 buf->b_next = NULL;
2030 if (firstbuf == NULL) /* buffer list is empty */
2031 {
2032 buf->b_prev = NULL;
2033 firstbuf = buf;
2034 }
2035 else /* append new buffer at end of list */
2036 {
2037 lastbuf->b_next = buf;
2038 buf->b_prev = lastbuf;
2039 }
2040 lastbuf = buf;
2041
2042 buf->b_fnum = top_file_num++;
2043 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2044 {
2045 EMSG(_("W14: Warning: List of file names overflow"));
2046 if (emsg_silent == 0)
2047 {
2048 out_flush();
2049 ui_delay(3000L, TRUE); /* make sure it is noticed */
2050 }
2051 top_file_num = 1;
2052 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002053 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054
2055 /*
2056 * Always copy the options from the current buffer.
2057 */
2058 buf_copy_options(buf, BCO_ALWAYS);
2059 }
2060
2061 buf->b_wininfo->wi_fpos.lnum = lnum;
2062 buf->b_wininfo->wi_win = curwin;
2063
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002064#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002065 hash_init(&buf->b_s.b_keywtab);
2066 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067#endif
2068
2069 buf->b_fname = buf->b_sfname;
2070#ifdef UNIX
2071 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002072 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 else
2074 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002075 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 buf->b_dev = st.st_dev;
2077 buf->b_ino = st.st_ino;
2078 }
2079#endif
2080 buf->b_u_synced = TRUE;
2081 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002082 if (flags & BLN_DUMMY)
2083 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 buf_clear_file(buf);
2085 clrallmarks(buf); /* clear marks */
2086 fmarks_check_names(buf); /* check file marks for this file */
2087 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 if (!(flags & BLN_DUMMY))
2089 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002090 bufref_T bufref;
2091
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002092 /* Tricky: these autocommands may change the buffer list. They could
2093 * also split the window with re-using the one empty buffer. This may
2094 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002095 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002096 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002097 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002098 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002100 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002101 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002102 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002103 return NULL;
2104 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002105#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002106 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
2111 return buf;
2112}
2113
2114/*
2115 * Free the memory for the options of a buffer.
2116 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2117 * 'fileencoding'.
2118 */
2119 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002120free_buf_options(
2121 buf_T *buf,
2122 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123{
2124 if (free_p_ff)
2125 {
2126#ifdef FEAT_MBYTE
2127 clear_string_option(&buf->b_p_fenc);
2128#endif
2129 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 clear_string_option(&buf->b_p_bh);
2131 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
2133#ifdef FEAT_FIND_ID
2134 clear_string_option(&buf->b_p_def);
2135 clear_string_option(&buf->b_p_inc);
2136# ifdef FEAT_EVAL
2137 clear_string_option(&buf->b_p_inex);
2138# endif
2139#endif
2140#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2141 clear_string_option(&buf->b_p_inde);
2142 clear_string_option(&buf->b_p_indk);
2143#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002144#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2145 clear_string_option(&buf->b_p_bexpr);
2146#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002147#if defined(FEAT_CRYPT)
2148 clear_string_option(&buf->b_p_cm);
2149#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002150 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002151#if defined(FEAT_EVAL)
2152 clear_string_option(&buf->b_p_fex);
2153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154#ifdef FEAT_CRYPT
2155 clear_string_option(&buf->b_p_key);
2156#endif
2157 clear_string_option(&buf->b_p_kp);
2158 clear_string_option(&buf->b_p_mps);
2159 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002160 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002162#ifdef FEAT_VARTABS
2163 clear_string_option(&buf->b_p_vsts);
2164 if (buf->b_p_vsts_nopaste)
2165 vim_free(buf->b_p_vsts_nopaste);
2166 buf->b_p_vsts_nopaste = NULL;
2167 if (buf->b_p_vsts_array)
2168 vim_free(buf->b_p_vsts_array);
2169 buf->b_p_vsts_array = NULL;
2170 clear_string_option(&buf->b_p_vts);
2171 if (buf->b_p_vts_array)
2172 vim_free(buf->b_p_vts_array);
2173 buf->b_p_vts_array = NULL;
2174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175#ifdef FEAT_KEYMAP
2176 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002177 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 ga_clear(&buf->b_kmap_ga);
2179#endif
2180#ifdef FEAT_COMMENTS
2181 clear_string_option(&buf->b_p_com);
2182#endif
2183#ifdef FEAT_FOLDING
2184 clear_string_option(&buf->b_p_cms);
2185#endif
2186 clear_string_option(&buf->b_p_nf);
2187#ifdef FEAT_SYN_HL
2188 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002189 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002190#endif
2191#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002192 clear_string_option(&buf->b_s.b_p_spc);
2193 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002194 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002195 buf->b_s.b_cap_prog = NULL;
2196 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197#endif
2198#ifdef FEAT_SEARCHPATH
2199 clear_string_option(&buf->b_p_sua);
2200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202#ifdef FEAT_CINDENT
2203 clear_string_option(&buf->b_p_cink);
2204 clear_string_option(&buf->b_p_cino);
2205#endif
2206#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2207 clear_string_option(&buf->b_p_cinw);
2208#endif
2209#ifdef FEAT_INS_EXPAND
2210 clear_string_option(&buf->b_p_cpt);
2211#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002212#ifdef FEAT_COMPL_FUNC
2213 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002214 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216#ifdef FEAT_QUICKFIX
2217 clear_string_option(&buf->b_p_gp);
2218 clear_string_option(&buf->b_p_mp);
2219 clear_string_option(&buf->b_p_efm);
2220#endif
2221 clear_string_option(&buf->b_p_ep);
2222 clear_string_option(&buf->b_p_path);
2223 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002224 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225#ifdef FEAT_INS_EXPAND
2226 clear_string_option(&buf->b_p_dict);
2227 clear_string_option(&buf->b_p_tsr);
2228#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002229#ifdef FEAT_TEXTOBJ
2230 clear_string_option(&buf->b_p_qe);
2231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002233 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002234#ifdef FEAT_LISP
2235 clear_string_option(&buf->b_p_lw);
2236#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002237 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002238#ifdef FEAT_MBYTE
2239 clear_string_option(&buf->b_p_menc);
2240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241}
2242
2243/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002244 * Get alternate file "n".
2245 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2246 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 * if (options & GETF_SETMARK) call setpcmark()
2248 * if (options & GETF_ALT) we are jumping to an alternate file.
2249 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2250 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002251 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 */
2253 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002254buflist_getfile(
2255 int n,
2256 linenr_T lnum,
2257 int options,
2258 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259{
2260 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 pos_T *fpos;
2263 colnr_T col;
2264
2265 buf = buflist_findnr(n);
2266 if (buf == NULL)
2267 {
2268 if ((options & GETF_ALT) && n == 0)
2269 EMSG(_(e_noalt));
2270 else
2271 EMSGN(_("E92: Buffer %ld not found"), n);
2272 return FAIL;
2273 }
2274
2275 /* if alternate file is the current buffer, nothing to do */
2276 if (buf == curbuf)
2277 return OK;
2278
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002279 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002280 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002281 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002283 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002284 if (curbuf_locked())
2285 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287 /* altfpos may be changed by getfile(), get it now */
2288 if (lnum == 0)
2289 {
2290 fpos = buflist_findfpos(buf);
2291 lnum = fpos->lnum;
2292 col = fpos->col;
2293 }
2294 else
2295 col = 0;
2296
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 if (options & GETF_SWITCH)
2298 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002299 /* If 'switchbuf' contains "useopen": jump to first window containing
2300 * "buf" if one exists */
2301 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002303
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002304 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002305 * page containing "buf" if one exists */
2306 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002307 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002308
2309 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2310 * current buffer isn't empty: open new tab or window */
2311 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002312 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002314 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002315 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002316 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2317 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002319 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
2323 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002324 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2325 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
2327 --RedrawingDisabled;
2328
2329 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2330 if (!p_sol && col != 0)
2331 {
2332 curwin->w_cursor.col = col;
2333 check_cursor_col();
2334#ifdef FEAT_VIRTUALEDIT
2335 curwin->w_cursor.coladd = 0;
2336#endif
2337 curwin->w_set_curswant = TRUE;
2338 }
2339 return OK;
2340 }
2341 --RedrawingDisabled;
2342 return FAIL;
2343}
2344
2345/*
2346 * go to the last know line number for the current buffer
2347 */
2348 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002349buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350{
2351 pos_T *fpos;
2352
2353 fpos = buflist_findfpos(curbuf);
2354
2355 curwin->w_cursor.lnum = fpos->lnum;
2356 check_cursor_lnum();
2357
2358 if (p_sol)
2359 curwin->w_cursor.col = 0;
2360 else
2361 {
2362 curwin->w_cursor.col = fpos->col;
2363 check_cursor_col();
2364#ifdef FEAT_VIRTUALEDIT
2365 curwin->w_cursor.coladd = 0;
2366#endif
2367 curwin->w_set_curswant = TRUE;
2368 }
2369}
2370
Bram Moolenaar81695252004-12-29 20:58:21 +00002371#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2372/*
2373 * Find file in buffer list by name (it has to be for the current window).
2374 * Returns NULL if not found.
2375 */
2376 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002377buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002378{
2379 char_u *ffname;
2380 buf_T *buf = NULL;
2381
2382 /* First make the name into a full path name */
2383 ffname = FullName_save(fname,
2384#ifdef UNIX
2385 TRUE /* force expansion, get rid of symbolic links */
2386#else
2387 FALSE
2388#endif
2389 );
2390 if (ffname != NULL)
2391 {
2392 buf = buflist_findname(ffname);
2393 vim_free(ffname);
2394 }
2395 return buf;
2396}
2397#endif
2398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399/*
2400 * Find file in buffer list by name (it has to be for the current window).
2401 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002402 * Skips dummy buffers.
2403 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 */
2405 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002406buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
2408#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002409 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
2411 if (mch_stat((char *)ffname, &st) < 0)
2412 st.st_dev = (dev_T)-1;
2413 return buflist_findname_stat(ffname, &st);
2414}
2415
2416/*
2417 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2418 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002419 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 */
2421 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002422buflist_findname_stat(
2423 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002424 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425{
2426#endif
2427 buf_T *buf;
2428
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002429 /* Start at the last buffer, expect to find a match sooner. */
2430 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002431 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432#ifdef UNIX
2433 , stp
2434#endif
2435 ))
2436 return buf;
2437 return NULL;
2438}
2439
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440/*
2441 * Find file in buffer list by a regexp pattern.
2442 * Return fnum of the found buffer.
2443 * Return < 0 for error.
2444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002446buflist_findpat(
2447 char_u *pattern,
2448 char_u *pattern_end, /* pointer to first char after pattern */
2449 int unlisted, /* find unlisted buffers */
2450 int diffmode UNUSED, /* find diff-mode buffers only */
2451 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452{
2453 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 int match = -1;
2455 int find_listed;
2456 char_u *pat;
2457 char_u *patend;
2458 int attempt;
2459 char_u *p;
2460 int toggledollar;
2461
2462 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2463 {
2464 if (*pattern == '%')
2465 match = curbuf->b_fnum;
2466 else
2467 match = curwin->w_alt_fnum;
2468#ifdef FEAT_DIFF
2469 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2470 match = -1;
2471#endif
2472 }
2473
2474 /*
2475 * Try four ways of matching a listed buffer:
2476 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002477 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 * attempt == 2: with '$' at end (only match at end)
2479 * attempt == 3: with '^' at start and '$' at end (only full match)
2480 * Repeat this for finding an unlisted buffer if there was no matching
2481 * listed buffer.
2482 */
2483 else
2484 {
2485 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2486 if (pat == NULL)
2487 return -1;
2488 patend = pat + STRLEN(pat) - 1;
2489 toggledollar = (patend > pat && *patend == '$');
2490
2491 /* First try finding a listed buffer. If not found and "unlisted"
2492 * is TRUE, try finding an unlisted buffer. */
2493 find_listed = TRUE;
2494 for (;;)
2495 {
2496 for (attempt = 0; attempt <= 3; ++attempt)
2497 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002498 regmatch_T regmatch;
2499
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 /* may add '^' and '$' */
2501 if (toggledollar)
2502 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2503 p = pat;
2504 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2505 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002506 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2507 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 {
2509 vim_free(pat);
2510 return -1;
2511 }
2512
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002513 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 if (buf->b_p_bl == find_listed
2515#ifdef FEAT_DIFF
2516 && (!diffmode || diff_mode_buf(buf))
2517#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002518 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002520 if (curtab_only)
2521 {
2522 /* Ignore the match if the buffer is not open in
2523 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002524 win_T *wp;
2525
Bram Moolenaar29323592016-07-24 22:04:11 +02002526 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002527 if (wp->w_buffer == buf)
2528 break;
2529 if (wp == NULL)
2530 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (match >= 0) /* already found a match */
2533 {
2534 match = -2;
2535 break;
2536 }
2537 match = buf->b_fnum; /* remember first match */
2538 }
2539
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002540 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 if (match >= 0) /* found one match */
2542 break;
2543 }
2544
2545 /* Only search for unlisted buffers if there was no match with
2546 * a listed buffer. */
2547 if (!unlisted || !find_listed || match != -1)
2548 break;
2549 find_listed = FALSE;
2550 }
2551
2552 vim_free(pat);
2553 }
2554
2555 if (match == -2)
2556 EMSG2(_("E93: More than one match for %s"), pattern);
2557 else if (match < 0)
2558 EMSG2(_("E94: No matching buffer for %s"), pattern);
2559 return match;
2560}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
2562#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2563
2564/*
2565 * Find all buffer names that match.
2566 * For command line expansion of ":buf" and ":sbuf".
2567 * Return OK if matches found, FAIL otherwise.
2568 */
2569 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002570ExpandBufnames(
2571 char_u *pat,
2572 int *num_file,
2573 char_u ***file,
2574 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576 int count = 0;
2577 buf_T *buf;
2578 int round;
2579 char_u *p;
2580 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002581 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
2583 *num_file = 0; /* return values in case of FAIL */
2584 *file = NULL;
2585
Bram Moolenaar05159a02005-02-26 23:04:13 +00002586 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2587 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002589 patc = alloc((unsigned)STRLEN(pat) + 11);
2590 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002592 STRCPY(patc, "\\(^\\|[\\/]\\)");
2593 STRCPY(patc + 11, pat + 1);
2594 }
2595 else
2596 patc = pat;
2597
2598 /*
2599 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002600 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002601 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002602 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002603 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002604 regmatch_T regmatch;
2605
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002606 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002607 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002608 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2609 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002610 {
2611 if (patc != pat)
2612 vim_free(patc);
2613 return FAIL;
2614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615
2616 /*
2617 * round == 1: Count the matches.
2618 * round == 2: Build the array to keep the matches.
2619 */
2620 for (round = 1; round <= 2; ++round)
2621 {
2622 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002623 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 {
2625 if (!buf->b_p_bl) /* skip unlisted buffers */
2626 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002627 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 if (p != NULL)
2629 {
2630 if (round == 1)
2631 ++count;
2632 else
2633 {
2634 if (options & WILD_HOME_REPLACE)
2635 p = home_replace_save(buf, p);
2636 else
2637 p = vim_strsave(p);
2638 (*file)[count++] = p;
2639 }
2640 }
2641 }
2642 if (count == 0) /* no match found, break here */
2643 break;
2644 if (round == 1)
2645 {
2646 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2647 if (*file == NULL)
2648 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002649 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002650 if (patc != pat)
2651 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 return FAIL;
2653 }
2654 }
2655 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002656 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (count) /* match(es) found, break here */
2658 break;
2659 }
2660
Bram Moolenaar05159a02005-02-26 23:04:13 +00002661 if (patc != pat)
2662 vim_free(patc);
2663
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 *num_file = count;
2665 return (count == 0 ? FAIL : OK);
2666}
2667
2668#endif /* FEAT_CMDL_COMPL */
2669
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670/*
2671 * Check for a match on the file name for buffer "buf" with regprog "prog".
2672 */
2673 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002674buflist_match(
2675 regmatch_T *rmp,
2676 buf_T *buf,
2677 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678{
2679 char_u *match;
2680
2681 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002682 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002684 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
2686 return match;
2687}
2688
2689/*
2690 * Try matching the regexp in "prog" with file name "name".
2691 * Return "name" when there is a match, NULL when not.
2692 */
2693 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002694fname_match(
2695 regmatch_T *rmp,
2696 char_u *name,
2697 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698{
2699 char_u *match = NULL;
2700 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
2702 if (name != NULL)
2703 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002704 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002705 rmp->rm_ic = p_fic || ignore_case;
2706 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 match = name;
2708 else
2709 {
2710 /* Replace $(HOME) with '~' and try matching again. */
2711 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002712 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 match = name;
2714 vim_free(p);
2715 }
2716 }
2717
2718 return match;
2719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002722 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 */
2724 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002725buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002727 char_u key[VIM_SIZEOF_INT * 2 + 1];
2728 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
2730 if (nr == 0)
2731 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002732 sprintf((char *)key, "%x", nr);
2733 hi = hash_find(&buf_hashtab, key);
2734
2735 if (!HASHITEM_EMPTY(hi))
2736 return (buf_T *)(hi->hi_key
2737 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 return NULL;
2739}
2740
2741/*
2742 * Get name of file 'n' in the buffer list.
2743 * When the file has no name an empty string is returned.
2744 * home_replace() is used to shorten the file name (used for marks).
2745 * Returns a pointer to allocated memory, of NULL when failed.
2746 */
2747 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002748buflist_nr2name(
2749 int n,
2750 int fullname,
2751 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752{
2753 buf_T *buf;
2754
2755 buf = buflist_findnr(n);
2756 if (buf == NULL)
2757 return NULL;
2758 return home_replace_save(helptail ? buf : NULL,
2759 fullname ? buf->b_ffname : buf->b_fname);
2760}
2761
2762/*
2763 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2764 * When "copy_options" is TRUE save the local window option values.
2765 * When "lnum" is 0 only do the options.
2766 */
2767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002768buflist_setfpos(
2769 buf_T *buf,
2770 win_T *win,
2771 linenr_T lnum,
2772 colnr_T col,
2773 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774{
2775 wininfo_T *wip;
2776
2777 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2778 if (wip->wi_win == win)
2779 break;
2780 if (wip == NULL)
2781 {
2782 /* allocate a new entry */
2783 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2784 if (wip == NULL)
2785 return;
2786 wip->wi_win = win;
2787 if (lnum == 0) /* set lnum even when it's 0 */
2788 lnum = 1;
2789 }
2790 else
2791 {
2792 /* remove the entry from the list */
2793 if (wip->wi_prev)
2794 wip->wi_prev->wi_next = wip->wi_next;
2795 else
2796 buf->b_wininfo = wip->wi_next;
2797 if (wip->wi_next)
2798 wip->wi_next->wi_prev = wip->wi_prev;
2799 if (copy_options && wip->wi_optset)
2800 {
2801 clear_winopt(&wip->wi_opt);
2802#ifdef FEAT_FOLDING
2803 deleteFoldRecurse(&wip->wi_folds);
2804#endif
2805 }
2806 }
2807 if (lnum != 0)
2808 {
2809 wip->wi_fpos.lnum = lnum;
2810 wip->wi_fpos.col = col;
2811 }
2812 if (copy_options)
2813 {
2814 /* Save the window-specific option values. */
2815 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2816#ifdef FEAT_FOLDING
2817 wip->wi_fold_manual = win->w_fold_manual;
2818 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2819#endif
2820 wip->wi_optset = TRUE;
2821 }
2822
2823 /* insert the entry in front of the list */
2824 wip->wi_next = buf->b_wininfo;
2825 buf->b_wininfo = wip;
2826 wip->wi_prev = NULL;
2827 if (wip->wi_next)
2828 wip->wi_next->wi_prev = wip;
2829
2830 return;
2831}
2832
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002833#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002834static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002835
2836/*
2837 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2838 * page. That's because a diff is local to a tab page.
2839 */
2840 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002841wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002842{
2843 win_T *wp;
2844
2845 if (wip->wi_opt.wo_diff)
2846 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002847 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002848 /* return FALSE when it's a window in the current tab page, thus
2849 * the buffer was in diff mode here */
2850 if (wip->wi_win == wp)
2851 return FALSE;
2852 return TRUE;
2853 }
2854 return FALSE;
2855}
2856#endif
2857
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858/*
2859 * Find info for the current window in buffer "buf".
2860 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002861 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2862 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 * Returns NULL when there isn't any info.
2864 */
2865 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002866find_wininfo(
2867 buf_T *buf,
2868 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
2870 wininfo_T *wip;
2871
2872 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002873 if (wip->wi_win == curwin
2874#ifdef FEAT_DIFF
2875 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2876#endif
2877 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002879
2880 /* If no wininfo for curwin, use the first in the list (that doesn't have
2881 * 'diff' set and is in another tab page). */
2882 if (wip == NULL)
2883 {
2884#ifdef FEAT_DIFF
2885 if (skip_diff_buffer)
2886 {
2887 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2888 if (!wininfo_other_tab_diff(wip))
2889 break;
2890 }
2891 else
2892#endif
2893 wip = buf->b_wininfo;
2894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 return wip;
2896}
2897
2898/*
2899 * Reset the local window options to the values last used in this window.
2900 * If the buffer wasn't used in this window before, use the values from
2901 * the most recently used window. If the values were never set, use the
2902 * global values for the window.
2903 */
2904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002905get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
2907 wininfo_T *wip;
2908
2909 clear_winopt(&curwin->w_onebuf_opt);
2910#ifdef FEAT_FOLDING
2911 clearFolding(curwin);
2912#endif
2913
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002914 wip = find_wininfo(buf, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02002915 if (wip != NULL && wip->wi_win != NULL
2916 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 {
Bram Moolenaar25782a72018-05-13 18:05:33 +02002918 /* The buffer is currently displayed in the window: use the actual
2919 * option values instead of the saved (possibly outdated) values. */
2920 win_T *wp = wip->wi_win;
2921
2922 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
2923#ifdef FEAT_FOLDING
2924 curwin->w_fold_manual = wp->w_fold_manual;
2925 curwin->w_foldinvalid = TRUE;
2926 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
2927#endif
2928 }
2929 else if (wip != NULL && wip->wi_optset)
2930 {
2931 /* the buffer was displayed in the current window earlier */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2933#ifdef FEAT_FOLDING
2934 curwin->w_fold_manual = wip->wi_fold_manual;
2935 curwin->w_foldinvalid = TRUE;
2936 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2937#endif
2938 }
2939 else
2940 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2941
2942#ifdef FEAT_FOLDING
2943 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2944 if (p_fdls >= 0)
2945 curwin->w_p_fdl = p_fdls;
2946#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002947#ifdef FEAT_SYN_HL
2948 check_colorcolumn(curwin);
2949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950}
2951
2952/*
2953 * Find the position (lnum and col) for the buffer 'buf' for the current
2954 * window.
2955 * Returns a pointer to no_position if no position is found.
2956 */
2957 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002958buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
2960 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002961 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002963 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 if (wip != NULL)
2965 return &(wip->wi_fpos);
2966 else
2967 return &no_position;
2968}
2969
2970/*
2971 * Find the lnum for the buffer 'buf' for the current window.
2972 */
2973 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002974buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
2976 return buflist_findfpos(buf)->lnum;
2977}
2978
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002980 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002983buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
2985 buf_T *buf;
2986 int len;
2987 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02002988 int ro_char;
2989 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02002990#ifdef FEAT_TERMINAL
2991 int job_running;
2992 int job_none_open;
2993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
2995 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2996 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02002997#ifdef FEAT_TERMINAL
2998 job_running = term_job_running(buf->b_term);
2999 job_none_open = job_running && term_none_open(buf->b_term);
3000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02003002 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3003 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3004 || (vim_strchr(eap->arg, '+')
3005 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3006 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003007 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003008 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003009 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3010#ifdef FEAT_TERMINAL
3011 || (vim_strchr(eap->arg, 'R')
3012 && (!job_running || (job_running && job_none_open)))
3013 || (vim_strchr(eap->arg, '?')
3014 && (!job_running || (job_running && !job_none_open)))
3015 || (vim_strchr(eap->arg, 'F')
3016 && (job_running || buf->b_term == NULL))
3017#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003018 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3019 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3020 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3021 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3022 || (vim_strchr(eap->arg, '#')
3023 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003026 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 else
3028 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003029 if (message_filtered(NameBuff))
3030 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003032 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3033 : (bufIsChanged(buf) ? '+' : ' ');
3034#ifdef FEAT_TERMINAL
3035 if (term_job_running(buf->b_term))
3036 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003037 if (term_none_open(buf->b_term))
3038 ro_char = '?';
3039 else
3040 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003041 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
3042 * closing, but it's not actually changed. */
3043 }
3044 else if (buf->b_term != NULL)
3045 ro_char = 'F';
3046 else
3047#endif
3048 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3049
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003050 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003051 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 buf->b_fnum,
3053 buf->b_p_bl ? ' ' : 'u',
3054 buf == curbuf ? '%' :
3055 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3056 buf->b_ml.ml_mfp == NULL ? ' ' :
3057 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003058 ro_char,
3059 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003060 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003061 if (len > IOSIZE - 20)
3062 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
3064 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 i = 40 - vim_strsize(IObuff);
3066 do
3067 {
3068 IObuff[len++] = ' ';
3069 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003070 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3071 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003072 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 msg_outtrans(IObuff);
3074 out_flush(); /* output one line at a time */
3075 ui_breakcheck();
3076 }
3077}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
3079/*
3080 * Get file name and line number for file 'fnum'.
3081 * Used by DoOneCmd() for translating '%' and '#'.
3082 * Used by insert_reg() and cmdline_paste() for '#' register.
3083 * Return FAIL if not found, OK for success.
3084 */
3085 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003086buflist_name_nr(
3087 int fnum,
3088 char_u **fname,
3089 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
3091 buf_T *buf;
3092
3093 buf = buflist_findnr(fnum);
3094 if (buf == NULL || buf->b_fname == NULL)
3095 return FAIL;
3096
3097 *fname = buf->b_fname;
3098 *lnum = buflist_findlnum(buf);
3099
3100 return OK;
3101}
3102
3103/*
3104 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3105 * The file name with the full path is also remembered, for when :cd is used.
3106 * Returns FAIL for failure (file name already in use by other buffer)
3107 * OK otherwise.
3108 */
3109 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003110setfname(
3111 buf_T *buf,
3112 char_u *ffname,
3113 char_u *sfname,
3114 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
Bram Moolenaar81695252004-12-29 20:58:21 +00003116 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003118 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119#endif
3120
3121 if (ffname == NULL || *ffname == NUL)
3122 {
3123 /* Removing the name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003124 VIM_CLEAR(buf->b_ffname);
3125 VIM_CLEAR(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126#ifdef UNIX
3127 st.st_dev = (dev_T)-1;
3128#endif
3129 }
3130 else
3131 {
3132 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3133 if (ffname == NULL) /* out of memory */
3134 return FAIL;
3135
3136 /*
3137 * if the file name is already used in another buffer:
3138 * - if the buffer is loaded, fail
3139 * - if the buffer is not loaded, delete it from the list
3140 */
3141#ifdef UNIX
3142 if (mch_stat((char *)ffname, &st) < 0)
3143 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003144#endif
3145 if (!(buf->b_flags & BF_DUMMY))
3146#ifdef UNIX
3147 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003149 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150#endif
3151 if (obuf != NULL && obuf != buf)
3152 {
3153 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3154 {
3155 if (message)
3156 EMSG(_("E95: Buffer with this name already exists"));
3157 vim_free(ffname);
3158 return FAIL;
3159 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003160 /* delete from the list */
3161 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 }
3163 sfname = vim_strsave(sfname);
3164 if (ffname == NULL || sfname == NULL)
3165 {
3166 vim_free(sfname);
3167 vim_free(ffname);
3168 return FAIL;
3169 }
3170#ifdef USE_FNAME_CASE
3171# ifdef USE_LONG_FNAME
3172 if (USE_LONG_FNAME)
3173# endif
3174 fname_case(sfname, 0); /* set correct case for short file name */
3175#endif
3176 vim_free(buf->b_ffname);
3177 vim_free(buf->b_sfname);
3178 buf->b_ffname = ffname;
3179 buf->b_sfname = sfname;
3180 }
3181 buf->b_fname = buf->b_sfname;
3182#ifdef UNIX
3183 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003184 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 else
3186 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003187 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 buf->b_dev = st.st_dev;
3189 buf->b_ino = st.st_ino;
3190 }
3191#endif
3192
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195 buf_name_changed(buf);
3196 return OK;
3197}
3198
3199/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003200 * Crude way of changing the name of a buffer. Use with care!
3201 * The name should be relative to the current directory.
3202 */
3203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003204buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003205{
3206 buf_T *buf;
3207
3208 buf = buflist_findnr(fnum);
3209 if (buf != NULL)
3210 {
3211 vim_free(buf->b_sfname);
3212 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003213 buf->b_ffname = vim_strsave(name);
3214 buf->b_sfname = NULL;
3215 /* Allocate ffname and expand into full path. Also resolves .lnk
3216 * files on Win32. */
3217 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003218 buf->b_fname = buf->b_sfname;
3219 }
3220}
3221
3222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 * Take care of what needs to be done when the name of buffer "buf" has
3224 * changed.
3225 */
3226 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003227buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228{
3229 /*
3230 * If the file name changed, also change the name of the swapfile
3231 */
3232 if (buf->b_ml.ml_mfp != NULL)
3233 ml_setname(buf);
3234
3235 if (curwin->w_buffer == buf)
3236 check_arg_idx(curwin); /* check file name for arg list */
3237#ifdef FEAT_TITLE
3238 maketitle(); /* set window title */
3239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 fmarks_check_names(buf); /* check named file marks */
3242 ml_timestamp(buf); /* reset timestamp */
3243}
3244
3245/*
3246 * set alternate file name for current window
3247 *
3248 * Used by do_one_cmd(), do_write() and do_ecmd().
3249 * Return the buffer.
3250 */
3251 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003252setaltfname(
3253 char_u *ffname,
3254 char_u *sfname,
3255 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256{
3257 buf_T *buf;
3258
3259 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3260 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003261 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 curwin->w_alt_fnum = buf->b_fnum;
3263 return buf;
3264}
3265
3266/*
3267 * Get alternate file name for current window.
3268 * Return NULL if there isn't any, and give error message if requested.
3269 */
3270 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003271getaltfname(
3272 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
3274 char_u *fname;
3275 linenr_T dummy;
3276
3277 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3278 {
3279 if (errmsg)
3280 EMSG(_(e_noalt));
3281 return NULL;
3282 }
3283 return fname;
3284}
3285
3286/*
3287 * Add a file name to the buflist and return its number.
3288 * Uses same flags as buflist_new(), except BLN_DUMMY.
3289 *
3290 * used by qf_init(), main() and doarglist()
3291 */
3292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003293buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295 buf_T *buf;
3296
3297 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3298 if (buf != NULL)
3299 return buf->b_fnum;
3300 return 0;
3301}
3302
3303#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3304/*
3305 * Adjust slashes in file names. Called after 'shellslash' was set.
3306 */
3307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003308buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
3310 buf_T *bp;
3311
Bram Moolenaar29323592016-07-24 22:04:11 +02003312 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
3314 if (bp->b_ffname != NULL)
3315 slash_adjust(bp->b_ffname);
3316 if (bp->b_sfname != NULL)
3317 slash_adjust(bp->b_sfname);
3318 }
3319}
3320#endif
3321
3322/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003323 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 * Also save the local window option values.
3325 */
3326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003327buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003329 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330}
3331
3332/*
3333 * Return TRUE if 'ffname' is not the same file as current file.
3334 * Fname must have a full path (expanded by mch_FullName()).
3335 */
3336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003337otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 return otherfile_buf(curbuf, ffname
3340#ifdef UNIX
3341 , NULL
3342#endif
3343 );
3344}
3345
3346 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003347otherfile_buf(
3348 buf_T *buf,
3349 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003351 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003353 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 /* no name is different */
3356 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3357 return TRUE;
3358 if (fnamecmp(ffname, buf->b_ffname) == 0)
3359 return FALSE;
3360#ifdef UNIX
3361 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003362 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
Bram Moolenaar8767f522016-07-01 17:17:39 +02003364 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (stp == NULL)
3366 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003367 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 st.st_dev = (dev_T)-1;
3369 stp = &st;
3370 }
3371 /* Use dev/ino to check if the files are the same, even when the names
3372 * are different (possible with links). Still need to compare the
3373 * name above, for when the file doesn't exist yet.
3374 * Problem: The dev/ino changes when a file is deleted (and created
3375 * again) and remains the same when renamed/moved. We don't want to
3376 * mch_stat() each buffer each time, that would be too slow. Get the
3377 * dev/ino again when they appear to match, but not when they appear
3378 * to be different: Could skip a buffer when it's actually the same
3379 * file. */
3380 if (buf_same_ino(buf, stp))
3381 {
3382 buf_setino(buf);
3383 if (buf_same_ino(buf, stp))
3384 return FALSE;
3385 }
3386 }
3387#endif
3388 return TRUE;
3389}
3390
3391#if defined(UNIX) || defined(PROTO)
3392/*
3393 * Set inode and device number for a buffer.
3394 * Must always be called when b_fname is changed!.
3395 */
3396 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003397buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003399 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
3401 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3402 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003403 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 buf->b_dev = st.st_dev;
3405 buf->b_ino = st.st_ino;
3406 }
3407 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003408 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409}
3410
3411/*
3412 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3413 */
3414 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003415buf_same_ino(
3416 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003417 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003419 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 && stp->st_dev == buf->b_dev
3421 && stp->st_ino == buf->b_ino);
3422}
3423#endif
3424
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003425/*
3426 * Print info about the current buffer.
3427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003429fileinfo(
3430 int fullname, /* when non-zero print full path */
3431 int shorthelp,
3432 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434 char_u *name;
3435 int n;
3436 char_u *p;
3437 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003438 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 buffer = alloc(IOSIZE);
3441 if (buffer == NULL)
3442 return;
3443
3444 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3445 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003446 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 p = buffer + STRLEN(buffer);
3448 }
3449 else
3450 p = buffer;
3451
3452 *p++ = '"';
3453 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003454 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 else
3456 {
3457 if (!fullname && curbuf->b_fname != NULL)
3458 name = curbuf->b_fname;
3459 else
3460 name = curbuf->b_ffname;
3461 home_replace(shorthelp ? curbuf : NULL, name, p,
3462 (int)(IOSIZE - (p - buffer)), TRUE);
3463 }
3464
Bram Moolenaara800b422010-06-27 01:15:55 +02003465 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 curbufIsChanged() ? (shortmess(SHM_MOD)
3467 ? " [+]" : _(" [Modified]")) : " ",
3468 (curbuf->b_flags & BF_NOTEDITED)
3469#ifdef FEAT_QUICKFIX
3470 && !bt_dontwrite(curbuf)
3471#endif
3472 ? _("[Not edited]") : "",
3473 (curbuf->b_flags & BF_NEW)
3474#ifdef FEAT_QUICKFIX
3475 && !bt_dontwrite(curbuf)
3476#endif
3477 ? _("[New file]") : "",
3478 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003479 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 : _("[readonly]")) : "",
3481 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3482 || curbuf->b_p_ro) ?
3483 " " : "");
3484 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3485 * causes an overflow, thus for large numbers divide instead. */
3486 if (curwin->w_cursor.lnum > 1000000L)
3487 n = (int)(((long)curwin->w_cursor.lnum) /
3488 ((long)curbuf->b_ml.ml_line_count / 100L));
3489 else
3490 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3491 (long)curbuf->b_ml.ml_line_count);
3492 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3493 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003494 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
3496#ifdef FEAT_CMDL_INFO
3497 else if (p_ru)
3498 {
3499 /* Current line and column are already on the screen -- webb */
3500 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003501 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003503 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 (long)curbuf->b_ml.ml_line_count, n);
3505 }
3506#endif
3507 else
3508 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003509 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 _("line %ld of %ld --%d%%-- col "),
3511 (long)curwin->w_cursor.lnum,
3512 (long)curbuf->b_ml.ml_line_count,
3513 n);
3514 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003515 len = STRLEN(buffer);
3516 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3518 }
3519
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003520 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
3522 if (dont_truncate)
3523 {
3524 /* Temporarily set msg_scroll to avoid the message being truncated.
3525 * First call msg_start() to get the message in the right place. */
3526 msg_start();
3527 n = msg_scroll;
3528 msg_scroll = TRUE;
3529 msg(buffer);
3530 msg_scroll = n;
3531 }
3532 else
3533 {
3534 p = msg_trunc_attr(buffer, FALSE, 0);
3535 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 /* Need to repeat the message after redrawing when:
3537 * - When restart_edit is set (otherwise there will be a delay
3538 * before redrawing).
3539 * - When the screen was scrolled but there is no wait-return
3540 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003541 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
3543
3544 vim_free(buffer);
3545}
3546
3547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003548col_print(
3549 char_u *buf,
3550 size_t buflen,
3551 int col,
3552 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553{
3554 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003555 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003557 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558}
3559
3560#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561static char_u *lasttitle = NULL;
3562static char_u *lasticon = NULL;
3563
Bram Moolenaar84a93082018-06-16 22:58:15 +02003564/*
3565 * Put the file name in the title bar and icon of the window.
3566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003568maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
3570 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003571 char_u *title_str = NULL;
3572 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 int maxlen = 0;
3574 int len;
3575 int mustset;
3576 char_u buf[IOSIZE];
3577 int off;
3578
3579 if (!redrawing())
3580 {
3581 /* Postpone updating the title when 'lazyredraw' is set. */
3582 need_maketitle = TRUE;
3583 return;
3584 }
3585
3586 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003587 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003588 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
3590 if (p_title)
3591 {
3592 if (p_titlelen > 0)
3593 {
3594 maxlen = p_titlelen * Columns / 100;
3595 if (maxlen < 10)
3596 maxlen = 10;
3597 }
3598
Bram Moolenaar84a93082018-06-16 22:58:15 +02003599 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (*p_titlestring != NUL)
3601 {
3602#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003603 if (stl_syntax & STL_IN_TITLE)
3604 {
3605 int use_sandbox = FALSE;
3606 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003607
3608# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003609 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003610# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003611 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003612 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003613 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003614 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003615 if (called_emsg)
3616 set_string_option_direct((char_u *)"titlestring", -1,
3617 (char_u *)"", OPT_FREE, SID_ERROR);
3618 called_emsg |= save_called_emsg;
3619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 else
3621#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003622 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624 else
3625 {
3626 /* format: "fname + (path) (1 of 2) - VIM" */
3627
Bram Moolenaar2c666692012-09-05 13:30:40 +02003628#define SPACE_FOR_FNAME (IOSIZE - 100)
3629#define SPACE_FOR_DIR (IOSIZE - 20)
3630#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003632 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003633#ifdef FEAT_TERMINAL
3634 else if (curbuf->b_term != NULL)
3635 {
3636 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3637 SPACE_FOR_FNAME);
3638 }
3639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 else
3641 {
3642 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003643 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646
Bram Moolenaar21554412017-07-24 21:44:43 +02003647#ifdef FEAT_TERMINAL
3648 if (curbuf->b_term == NULL)
3649#endif
3650 switch (bufIsChanged(curbuf)
3651 + (curbuf->b_p_ro * 2)
3652 + (!curbuf->b_p_ma * 4))
3653 {
3654 case 1: STRCAT(buf, " +"); break;
3655 case 2: STRCAT(buf, " ="); break;
3656 case 3: STRCAT(buf, " =+"); break;
3657 case 4:
3658 case 6: STRCAT(buf, " -"); break;
3659 case 5:
3660 case 7: STRCAT(buf, " -+"); break;
3661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
Bram Moolenaar21554412017-07-24 21:44:43 +02003663 if (curbuf->b_fname != NULL
3664#ifdef FEAT_TERMINAL
3665 && curbuf->b_term == NULL
3666#endif
3667 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
3669 /* Get path of file, replace home dir with ~ */
3670 off = (int)STRLEN(buf);
3671 buf[off++] = ' ';
3672 buf[off++] = '(';
3673 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003674 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675#ifdef BACKSLASH_IN_FILENAME
3676 /* avoid "c:/name" to be reduced to "c" */
3677 if (isalpha(buf[off]) && buf[off + 1] == ':')
3678 off += 2;
3679#endif
3680 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003681 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003683 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003684 /* must be a help buffer */
3685 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003686 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003690
Bram Moolenaar2c666692012-09-05 13:30:40 +02003691 /* Translate unprintable chars and concatenate. Keep some
3692 * room for the server name. When there is no room (very long
3693 * file name) use (...). */
3694 if (off < SPACE_FOR_DIR)
3695 {
3696 p = transstr(buf + off);
3697 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3698 vim_free(p);
3699 }
3700 else
3701 {
3702 vim_strncpy(buf + off, (char_u *)"...",
3703 (size_t)(SPACE_FOR_ARGNR - off));
3704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 STRCAT(buf, ")");
3706 }
3707
Bram Moolenaar2c666692012-09-05 13:30:40 +02003708 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
3710#if defined(FEAT_CLIENTSERVER)
3711 if (serverName != NULL)
3712 {
3713 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003714 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
3716 else
3717#endif
3718 STRCAT(buf, " - VIM");
3719
3720 if (maxlen > 0)
3721 {
3722 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003723 if (vim_strsize(buf) > maxlen)
3724 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
3726 }
3727 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003728 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729
3730 if (p_icon)
3731 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003732 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 if (*p_iconstring != NUL)
3734 {
3735#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003736 if (stl_syntax & STL_IN_ICON)
3737 {
3738 int use_sandbox = FALSE;
3739 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003740
3741# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003742 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003743# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003744 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003745 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003746 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003747 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003748 if (called_emsg)
3749 set_string_option_direct((char_u *)"iconstring", -1,
3750 (char_u *)"", OPT_FREE, SID_ERROR);
3751 called_emsg |= save_called_emsg;
3752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 else
3754#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003755 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
3757 else
3758 {
3759 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003760 p = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 else /* use file name only in icon */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003762 p = gettail(curbuf->b_ffname);
3763 *icon_str = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 /* Truncate name at 100 bytes. */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003765 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 if (len > 100)
3767 {
3768 len -= 100;
3769#ifdef FEAT_MBYTE
3770 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003771 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003773 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003775 STRCPY(icon_str, p);
3776 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 }
3779
Bram Moolenaar84a93082018-06-16 22:58:15 +02003780 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
3782 if (mustset)
3783 resettitle();
3784}
3785
3786/*
3787 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3788 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003789 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 */
3791 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003792value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793{
3794 if ((str == NULL) != (*last == NULL)
3795 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3796 {
3797 vim_free(*last);
3798 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02003801 mch_restore_title(
3802 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003807 return TRUE;
3808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810 return FALSE;
3811}
3812
3813/*
3814 * Put current window title back (used after calling a shell)
3815 */
3816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003817resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818{
3819 mch_settitle(lasttitle, lasticon);
3820}
Bram Moolenaarea408852005-06-25 22:49:46 +00003821
3822# if defined(EXITFREE) || defined(PROTO)
3823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003824free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003825{
3826 vim_free(lasttitle);
3827 vim_free(lasticon);
3828}
3829# endif
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831#endif /* FEAT_TITLE */
3832
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003833#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003835 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 * Return length of string in screen cells.
3837 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003838 * Normally works for window "wp", except when working for 'tabline' then it
3839 * is "curwin".
3840 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 * Items are drawn interspersed with the text that surrounds it
3842 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3843 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3844 *
3845 * If maxwidth is not zero, the string will be filled at any middle marker
3846 * or truncated if too long, fillchar is used for all whitespace.
3847 */
3848 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003849build_stl_str_hl(
3850 win_T *wp,
3851 char_u *out, /* buffer to write into != NameBuff */
3852 size_t outlen, /* length of out[] */
3853 char_u *fmt,
3854 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3855 int fillchar,
3856 int maxwidth,
3857 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3858 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859{
3860 char_u *p;
3861 char_u *s;
3862 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003863 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003865 win_T *save_curwin;
3866 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867#endif
3868 int empty_line;
3869 colnr_T virtcol;
3870 long l;
3871 long n;
3872 int prevchar_isflag;
3873 int prevchar_isitem;
3874 int itemisflag;
3875 int fillable;
3876 char_u *str;
3877 long num;
3878 int width;
3879 int itemcnt;
3880 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003881 int group_end_userhl;
3882 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 int groupitem[STL_MAX_ITEM];
3884 int groupdepth;
3885 struct stl_item
3886 {
3887 char_u *start;
3888 int minwid;
3889 int maxwid;
3890 enum
3891 {
3892 Normal,
3893 Empty,
3894 Group,
3895 Middle,
3896 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003897 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 Trunc
3899 } type;
3900 } item[STL_MAX_ITEM];
3901 int minwid;
3902 int maxwid;
3903 int zeropad;
3904 char_u base;
3905 char_u opt;
3906#define TMPLEN 70
3907 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003908 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003909 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003910 int save_must_redraw = must_redraw;
3911 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003912
3913#ifdef FEAT_EVAL
3914 /*
3915 * When the format starts with "%!" then evaluate it as an expression and
3916 * use the result as the actual format string.
3917 */
3918 if (fmt[0] == '%' && fmt[1] == '!')
3919 {
3920 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3921 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003922 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003923 }
3924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
3926 if (fillchar == 0)
3927 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003928#ifdef FEAT_MBYTE
3929 /* Can't handle a multi-byte fill character yet. */
3930 else if (mb_char2len(fillchar) > 1)
3931 fillchar = '-';
3932#endif
3933
Bram Moolenaar567199b2013-04-24 16:52:36 +02003934 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3935 * p will become invalid when getting another buffer line. */
3936 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3937 empty_line = (*p == NUL);
3938
3939 /* Get the byte value now, in case we need it below. This is more
3940 * efficient than making a copy of the line. */
3941 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3942 byteval = 0;
3943 else
3944#ifdef FEAT_MBYTE
3945 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3946#else
3947 byteval = p[wp->w_cursor.col];
3948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949
3950 groupdepth = 0;
3951 p = out;
3952 curitem = 0;
3953 prevchar_isflag = TRUE;
3954 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003955 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003957 if (curitem == STL_MAX_ITEM)
3958 {
3959 /* There are too many items. Add the error code to the statusline
3960 * to give the user a hint about what went wrong. */
3961 if (p + 6 < out + outlen)
3962 {
3963 mch_memmove(p, " E541", (size_t)5);
3964 p += 5;
3965 }
3966 break;
3967 }
3968
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 if (*s != NUL && *s != '%')
3970 prevchar_isflag = prevchar_isitem = FALSE;
3971
3972 /*
3973 * Handle up to the next '%' or the end.
3974 */
3975 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3976 *p++ = *s++;
3977 if (*s == NUL || p + 1 >= out + outlen)
3978 break;
3979
3980 /*
3981 * Handle one '%' item.
3982 */
3983 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003984 if (*s == NUL) /* ignore trailing % */
3985 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 if (*s == '%')
3987 {
3988 if (p + 1 >= out + outlen)
3989 break;
3990 *p++ = *s++;
3991 prevchar_isflag = prevchar_isitem = FALSE;
3992 continue;
3993 }
3994 if (*s == STL_MIDDLEMARK)
3995 {
3996 s++;
3997 if (groupdepth > 0)
3998 continue;
3999 item[curitem].type = Middle;
4000 item[curitem++].start = p;
4001 continue;
4002 }
4003 if (*s == STL_TRUNCMARK)
4004 {
4005 s++;
4006 item[curitem].type = Trunc;
4007 item[curitem++].start = p;
4008 continue;
4009 }
4010 if (*s == ')')
4011 {
4012 s++;
4013 if (groupdepth < 1)
4014 continue;
4015 groupdepth--;
4016
4017 t = item[groupitem[groupdepth]].start;
4018 *p = NUL;
4019 l = vim_strsize(t);
4020 if (curitem > groupitem[groupdepth] + 1
4021 && item[groupitem[groupdepth]].minwid == 0)
4022 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004023 /* remove group if all items are empty and highlight group
4024 * doesn't change */
4025 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004026 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4027 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004028 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004029 {
4030 group_start_userhl = group_end_userhl = item[n].minwid;
4031 break;
4032 }
4033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004035 {
4036 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004038 if (item[n].type == Highlight)
4039 group_end_userhl = item[n].minwid;
4040 }
4041 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
4043 p = t;
4044 l = 0;
4045 }
4046 }
4047 if (l > item[groupitem[groupdepth]].maxwid)
4048 {
4049 /* truncate, remove n bytes of text at the start */
4050#ifdef FEAT_MBYTE
4051 if (has_mbyte)
4052 {
4053 /* Find the first character that should be included. */
4054 n = 0;
4055 while (l >= item[groupitem[groupdepth]].maxwid)
4056 {
4057 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004058 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060 }
4061 else
4062#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004063 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064
4065 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004066 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 p = p - n + 1;
4068#ifdef FEAT_MBYTE
4069 /* Fill up space left over by half a double-wide char. */
4070 while (++l < item[groupitem[groupdepth]].minwid)
4071 *p++ = fillchar;
4072#endif
4073
4074 /* correct the start of the items for the truncation */
4075 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4076 {
4077 item[l].start -= n;
4078 if (item[l].start < t)
4079 item[l].start = t;
4080 }
4081 }
4082 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4083 {
4084 /* fill */
4085 n = item[groupitem[groupdepth]].minwid;
4086 if (n < 0)
4087 {
4088 /* fill by appending characters */
4089 n = 0 - n;
4090 while (l++ < n && p + 1 < out + outlen)
4091 *p++ = fillchar;
4092 }
4093 else
4094 {
4095 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004096 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 l = n - l;
4098 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004099 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 p += l;
4101 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4102 item[n].start += l;
4103 for ( ; l > 0; l--)
4104 *t++ = fillchar;
4105 }
4106 }
4107 continue;
4108 }
4109 minwid = 0;
4110 maxwid = 9999;
4111 zeropad = FALSE;
4112 l = 1;
4113 if (*s == '0')
4114 {
4115 s++;
4116 zeropad = TRUE;
4117 }
4118 if (*s == '-')
4119 {
4120 s++;
4121 l = -1;
4122 }
4123 if (VIM_ISDIGIT(*s))
4124 {
4125 minwid = (int)getdigits(&s);
4126 if (minwid < 0) /* overflow */
4127 minwid = 0;
4128 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004129 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 {
4131 item[curitem].type = Highlight;
4132 item[curitem].start = p;
4133 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4134 s++;
4135 curitem++;
4136 continue;
4137 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004138 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4139 {
4140 if (*s == STL_TABCLOSENR)
4141 {
4142 if (minwid == 0)
4143 {
4144 /* %X ends the close label, go back to the previously
4145 * define tab label nr. */
4146 for (n = curitem - 1; n >= 0; --n)
4147 if (item[n].type == TabPage && item[n].minwid >= 0)
4148 {
4149 minwid = item[n].minwid;
4150 break;
4151 }
4152 }
4153 else
4154 /* close nrs are stored as negative values */
4155 minwid = - minwid;
4156 }
4157 item[curitem].type = TabPage;
4158 item[curitem].start = p;
4159 item[curitem].minwid = minwid;
4160 s++;
4161 curitem++;
4162 continue;
4163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 if (*s == '.')
4165 {
4166 s++;
4167 if (VIM_ISDIGIT(*s))
4168 {
4169 maxwid = (int)getdigits(&s);
4170 if (maxwid <= 0) /* overflow */
4171 maxwid = 50;
4172 }
4173 }
4174 minwid = (minwid > 50 ? 50 : minwid) * l;
4175 if (*s == '(')
4176 {
4177 groupitem[groupdepth++] = curitem;
4178 item[curitem].type = Group;
4179 item[curitem].start = p;
4180 item[curitem].minwid = minwid;
4181 item[curitem].maxwid = maxwid;
4182 s++;
4183 curitem++;
4184 continue;
4185 }
4186 if (vim_strchr(STL_ALL, *s) == NULL)
4187 {
4188 s++;
4189 continue;
4190 }
4191 opt = *s++;
4192
4193 /* OK - now for the real work */
4194 base = 'D';
4195 itemisflag = FALSE;
4196 fillable = TRUE;
4197 num = -1;
4198 str = NULL;
4199 switch (opt)
4200 {
4201 case STL_FILEPATH:
4202 case STL_FULLPATH:
4203 case STL_FILENAME:
4204 fillable = FALSE; /* don't change ' ' to fillchar */
4205 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004206 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 else
4208 {
4209 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004210 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4212 }
4213 trans_characters(NameBuff, MAXPATHL);
4214 if (opt != STL_FILENAME)
4215 str = NameBuff;
4216 else
4217 str = gettail(NameBuff);
4218 break;
4219
4220 case STL_VIM_EXPR: /* '{' */
4221 itemisflag = TRUE;
4222 t = p;
4223 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4224 *p++ = *s++;
4225 if (*s != '}') /* missing '}' or out of space */
4226 break;
4227 s++;
4228 *p = 0;
4229 p = t;
4230
4231#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004232 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar3cb44482018-08-05 13:22:26 +02004233 set_internal_string_var((char_u *)"g:actual_curbuf", tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004235 save_curbuf = curbuf;
4236 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 curwin = wp;
4238 curbuf = wp->w_buffer;
4239
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004240 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004242 curwin = save_curwin;
4243 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004244 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
4246 if (str != NULL && *str != 0)
4247 {
4248 if (*skipdigits(str) == NUL)
4249 {
4250 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004251 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 itemisflag = FALSE;
4253 }
4254 }
4255#endif
4256 break;
4257
4258 case STL_LINE:
4259 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4260 ? 0L : (long)(wp->w_cursor.lnum);
4261 break;
4262
4263 case STL_NUMLINES:
4264 num = wp->w_buffer->b_ml.ml_line_count;
4265 break;
4266
4267 case STL_COLUMN:
4268 num = !(State & INSERT) && empty_line
4269 ? 0 : (int)wp->w_cursor.col + 1;
4270 break;
4271
4272 case STL_VIRTCOL:
4273 case STL_VIRTCOL_ALT:
4274 /* In list mode virtcol needs to be recomputed */
4275 virtcol = wp->w_virtcol;
4276 if (wp->w_p_list && lcs_tab1 == NUL)
4277 {
4278 wp->w_p_list = FALSE;
4279 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4280 wp->w_p_list = TRUE;
4281 }
4282 ++virtcol;
4283 /* Don't display %V if it's the same as %c. */
4284 if (opt == STL_VIRTCOL_ALT
4285 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4286 ? 0 : (int)wp->w_cursor.col + 1)))
4287 break;
4288 num = (long)virtcol;
4289 break;
4290
4291 case STL_PERCENTAGE:
4292 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4293 (long)wp->w_buffer->b_ml.ml_line_count);
4294 break;
4295
4296 case STL_ALTPERCENT:
4297 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004298 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 break;
4300
4301 case STL_ARGLISTSTAT:
4302 fillable = FALSE;
4303 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004304 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 str = tmp;
4306 break;
4307
4308 case STL_KEYMAP:
4309 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004310 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 str = tmp;
4312 break;
4313 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004314#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004315 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316#else
4317 num = 0;
4318#endif
4319 break;
4320
4321 case STL_BUFNO:
4322 num = wp->w_buffer->b_fnum;
4323 break;
4324
4325 case STL_OFFSET_X:
4326 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004327 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 case STL_OFFSET:
4329#ifdef FEAT_BYTEOFF
4330 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4331 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4332 0L : l + 1 + (!(State & INSERT) && empty_line ?
4333 0 : (int)wp->w_cursor.col);
4334#endif
4335 break;
4336
4337 case STL_BYTEVAL_X:
4338 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004339 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004341 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 if (num == NL)
4343 num = 0;
4344 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4345 num = NL;
4346 break;
4347
4348 case STL_ROFLAG:
4349 case STL_ROFLAG_ALT:
4350 itemisflag = TRUE;
4351 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004352 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 break;
4354
4355 case STL_HELPFLAG:
4356 case STL_HELPFLAG_ALT:
4357 itemisflag = TRUE;
4358 if (wp->w_buffer->b_help)
4359 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004360 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 break;
4362
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 case STL_FILETYPE:
4364 if (*wp->w_buffer->b_p_ft != NUL
4365 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4366 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004367 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4368 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 str = tmp;
4370 }
4371 break;
4372
4373 case STL_FILETYPE_ALT:
4374 itemisflag = TRUE;
4375 if (*wp->w_buffer->b_p_ft != NUL
4376 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4377 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004378 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4379 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 for (t = tmp; *t != 0; t++)
4381 *t = TOUPPER_LOC(*t);
4382 str = tmp;
4383 }
4384 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
Bram Moolenaar4033c552017-09-16 20:54:51 +02004386#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 case STL_PREVIEWFLAG:
4388 case STL_PREVIEWFLAG_ALT:
4389 itemisflag = TRUE;
4390 if (wp->w_p_pvw)
4391 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4392 : _("[Preview]"));
4393 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004394
4395 case STL_QUICKFIX:
4396 if (bt_quickfix(wp->w_buffer))
4397 str = (char_u *)(wp->w_llist_ref
4398 ? _(msg_loclist)
4399 : _(msg_qflist));
4400 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401#endif
4402
4403 case STL_MODIFIED:
4404 case STL_MODIFIED_ALT:
4405 itemisflag = TRUE;
4406 switch ((opt == STL_MODIFIED_ALT)
4407 + bufIsChanged(wp->w_buffer) * 2
4408 + (!wp->w_buffer->b_p_ma) * 4)
4409 {
4410 case 2: str = (char_u *)"[+]"; break;
4411 case 3: str = (char_u *)",+"; break;
4412 case 4: str = (char_u *)"[-]"; break;
4413 case 5: str = (char_u *)",-"; break;
4414 case 6: str = (char_u *)"[+-]"; break;
4415 case 7: str = (char_u *)",+-"; break;
4416 }
4417 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004418
4419 case STL_HIGHLIGHT:
4420 t = s;
4421 while (*s != '#' && *s != NUL)
4422 ++s;
4423 if (*s == '#')
4424 {
4425 item[curitem].type = Highlight;
4426 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004427 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004428 curitem++;
4429 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004430 if (*s != NUL)
4431 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004432 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434
4435 item[curitem].start = p;
4436 item[curitem].type = Normal;
4437 if (str != NULL && *str)
4438 {
4439 t = str;
4440 if (itemisflag)
4441 {
4442 if ((t[0] && t[1])
4443 && ((!prevchar_isitem && *t == ',')
4444 || (prevchar_isflag && *t == ' ')))
4445 t++;
4446 prevchar_isflag = TRUE;
4447 }
4448 l = vim_strsize(t);
4449 if (l > 0)
4450 prevchar_isitem = TRUE;
4451 if (l > maxwid)
4452 {
4453 while (l >= maxwid)
4454#ifdef FEAT_MBYTE
4455 if (has_mbyte)
4456 {
4457 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004458 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
4460 else
4461#endif
4462 l -= byte2cells(*t++);
4463 if (p + 1 >= out + outlen)
4464 break;
4465 *p++ = '<';
4466 }
4467 if (minwid > 0)
4468 {
4469 for (; l < minwid && p + 1 < out + outlen; l++)
4470 {
4471 /* Don't put a "-" in front of a digit. */
4472 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4473 *p++ = ' ';
4474 else
4475 *p++ = fillchar;
4476 }
4477 minwid = 0;
4478 }
4479 else
4480 minwid *= -1;
4481 while (*t && p + 1 < out + outlen)
4482 {
4483 *p++ = *t++;
4484 /* Change a space by fillchar, unless fillchar is '-' and a
4485 * digit follows. */
4486 if (fillable && p[-1] == ' '
4487 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4488 p[-1] = fillchar;
4489 }
4490 for (; l < minwid && p + 1 < out + outlen; l++)
4491 *p++ = fillchar;
4492 }
4493 else if (num >= 0)
4494 {
4495 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4496 char_u nstr[20];
4497
4498 if (p + 20 >= out + outlen)
4499 break; /* not sufficient space */
4500 prevchar_isitem = TRUE;
4501 t = nstr;
4502 if (opt == STL_VIRTCOL_ALT)
4503 {
4504 *t++ = '-';
4505 minwid--;
4506 }
4507 *t++ = '%';
4508 if (zeropad)
4509 *t++ = '0';
4510 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004511 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 *t = 0;
4513
4514 for (n = num, l = 1; n >= nbase; n /= nbase)
4515 l++;
4516 if (opt == STL_VIRTCOL_ALT)
4517 l++;
4518 if (l > maxwid)
4519 {
4520 l += 2;
4521 n = l - maxwid;
4522 while (l-- > maxwid)
4523 num /= nbase;
4524 *t++ = '>';
4525 *t++ = '%';
4526 *t = t[-3];
4527 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004528 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4529 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 }
4531 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004532 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4533 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 p += STRLEN(p);
4535 }
4536 else
4537 item[curitem].type = Empty;
4538
4539 if (opt == STL_VIM_EXPR)
4540 vim_free(str);
4541
4542 if (num >= 0 || (!itemisflag && str && *str))
4543 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4544 curitem++;
4545 }
4546 *p = NUL;
4547 itemcnt = curitem;
4548
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004549#ifdef FEAT_EVAL
4550 if (usefmt != fmt)
4551 vim_free(usefmt);
4552#endif
4553
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 width = vim_strsize(out);
4555 if (maxwidth > 0 && width > maxwidth)
4556 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004557 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 l = 0;
4559 if (itemcnt == 0)
4560 s = out;
4561 else
4562 {
4563 for ( ; l < itemcnt; l++)
4564 if (item[l].type == Trunc)
4565 {
4566 /* Truncate at %< item. */
4567 s = item[l].start;
4568 break;
4569 }
4570 if (l == itemcnt)
4571 {
4572 /* No %< item, truncate first item. */
4573 s = item[0].start;
4574 l = 0;
4575 }
4576 }
4577
4578 if (width - vim_strsize(s) >= maxwidth)
4579 {
4580 /* Truncation mark is beyond max length */
4581#ifdef FEAT_MBYTE
4582 if (has_mbyte)
4583 {
4584 s = out;
4585 width = 0;
4586 for (;;)
4587 {
4588 width += ptr2cells(s);
4589 if (width >= maxwidth)
4590 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004591 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593 /* Fill up for half a double-wide character. */
4594 while (++width < maxwidth)
4595 *s++ = fillchar;
4596 }
4597 else
4598#endif
4599 s = out + maxwidth - 1;
4600 for (l = 0; l < itemcnt; l++)
4601 if (item[l].start > s)
4602 break;
4603 itemcnt = l;
4604 *s++ = '>';
4605 *s = 0;
4606 }
4607 else
4608 {
4609#ifdef FEAT_MBYTE
4610 if (has_mbyte)
4611 {
4612 n = 0;
4613 while (width >= maxwidth)
4614 {
4615 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004616 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
4618 }
4619 else
4620#endif
4621 n = width - maxwidth + 1;
4622 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004623 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 *s = '<';
4625
4626 /* Fill up for half a double-wide character. */
4627 while (++width < maxwidth)
4628 {
4629 s = s + STRLEN(s);
4630 *s++ = fillchar;
4631 *s = NUL;
4632 }
4633
4634 --n; /* count the '<' */
4635 for (; l < itemcnt; l++)
4636 {
4637 if (item[l].start - n >= s)
4638 item[l].start -= n;
4639 else
4640 item[l].start = s;
4641 }
4642 }
4643 width = maxwidth;
4644 }
4645 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4646 {
4647 /* Apply STL_MIDDLE if any */
4648 for (l = 0; l < itemcnt; l++)
4649 if (item[l].type == Middle)
4650 break;
4651 if (l < itemcnt)
4652 {
4653 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004654 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 for (s = item[l].start; s < p; s++)
4656 *s = fillchar;
4657 for (l++; l < itemcnt; l++)
4658 item[l].start += maxwidth - width;
4659 width = maxwidth;
4660 }
4661 }
4662
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004663 /* Store the info about highlighting. */
4664 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004666 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 for (l = 0; l < itemcnt; l++)
4668 {
4669 if (item[l].type == Highlight)
4670 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004671 sp->start = item[l].start;
4672 sp->userhl = item[l].minwid;
4673 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
4675 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004676 sp->start = NULL;
4677 sp->userhl = 0;
4678 }
4679
4680 /* Store the info about tab pages labels. */
4681 if (tabtab != NULL)
4682 {
4683 sp = tabtab;
4684 for (l = 0; l < itemcnt; l++)
4685 {
4686 if (item[l].type == TabPage)
4687 {
4688 sp->start = item[l].start;
4689 sp->userhl = item[l].minwid;
4690 sp++;
4691 }
4692 }
4693 sp->start = NULL;
4694 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
4696
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004697 /* When inside update_screen we do not want redrawing a stausline, ruler,
4698 * title, etc. to trigger another redraw, it may cause an endless loop. */
4699 if (updating_screen)
4700 {
4701 must_redraw = save_must_redraw;
4702 curwin->w_redr_type = save_redr_type;
4703 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004704
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 return width;
4706}
4707#endif /* FEAT_STL_OPT */
4708
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004709#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4710 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004712 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4713 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 */
4715 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004716get_rel_pos(
4717 win_T *wp,
4718 char_u *buf,
4719 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720{
4721 long above; /* number of lines above window */
4722 long below; /* number of lines below window */
4723
Bram Moolenaar0027c212015-01-07 13:31:52 +01004724 if (buflen < 3) /* need at least 3 chars for writing */
4725 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 above = wp->w_topline - 1;
4727#ifdef FEAT_DIFF
4728 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004729 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4730 above = 0; /* All buffer lines are displayed and there is an
4731 * indication of filler lines, that can be considered
4732 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733#endif
4734 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4735 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004736 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4737 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004739 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004741 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 ? (int)(above / ((above + below) / 100L))
4743 : (int)(above * 100L / (above + below)));
4744}
4745#endif
4746
4747/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004748 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 * Return TRUE if it was appended.
4750 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004751 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004752append_arg_number(
4753 win_T *wp,
4754 char_u *buf,
4755 int buflen,
4756 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
4758 char_u *p;
4759
4760 if (ARGCOUNT <= 1) /* nothing to do */
4761 return FALSE;
4762
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004763 p = buf + STRLEN(buf); /* go to the end of the buffer */
4764 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 return FALSE;
4766 *p++ = ' ';
4767 *p++ = '(';
4768 if (add_file)
4769 {
4770 STRCPY(p, "file ");
4771 p += 5;
4772 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004773 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4774 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4776 return TRUE;
4777}
4778
4779/*
4780 * If fname is not a full path, make it a full path.
4781 * Returns pointer to allocated memory (NULL for failure).
4782 */
4783 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004784fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785{
4786 /*
4787 * Force expanding the path always for Unix, because symbolic links may
4788 * mess up the full path name, even though it starts with a '/'.
4789 * Also expand when there is ".." in the file name, try to remove it,
4790 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004791 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 * For MS-Windows also expand names like "longna~1" to "longname".
4793 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004794#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 return FullName_save(fname, TRUE);
4796#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004797 if (!vim_isAbsName(fname)
4798 || strstr((char *)fname, "..") != NULL
4799 || strstr((char *)fname, "//") != NULL
4800# ifdef BACKSLASH_IN_FILENAME
4801 || strstr((char *)fname, "\\\\") != NULL
4802# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004803# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004805# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 )
4807 return FullName_save(fname, FALSE);
4808
4809 fname = vim_strsave(fname);
4810
Bram Moolenaar9b942202007-10-03 12:31:33 +00004811# ifdef USE_FNAME_CASE
4812# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004814# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 {
4816 if (fname != NULL)
4817 fname_case(fname, 0); /* set correct case for file name */
4818 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004819# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
4821 return fname;
4822#endif
4823}
4824
4825/*
4826 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4827 * "ffname" becomes a pointer to allocated memory (or NULL).
4828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004830fname_expand(
4831 buf_T *buf UNUSED,
4832 char_u **ffname,
4833 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834{
4835 if (*ffname == NULL) /* if no file name given, nothing to do */
4836 return;
4837 if (*sfname == NULL) /* if no short file name given, use ffname */
4838 *sfname = *ffname;
4839 *ffname = fix_fname(*ffname); /* expand to full path */
4840
4841#ifdef FEAT_SHORTCUT
4842 if (!buf->b_p_bin)
4843 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004844 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845
4846 /* If the file name is a shortcut file, use the file it links to. */
4847 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004848 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 {
4850 vim_free(*ffname);
4851 *ffname = rfname;
4852 *sfname = rfname;
4853 }
4854 }
4855#endif
4856}
4857
4858/*
4859 * Get the file name for an argument list entry.
4860 */
4861 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004862alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
4864 buf_T *bp;
4865
4866 /* Use the name from the associated buffer if it exists. */
4867 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004868 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 return aep->ae_fname;
4870 return bp->b_fname;
4871}
4872
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873/*
4874 * do_arg_all(): Open up to 'count' windows, one for each argument.
4875 */
4876 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004877do_arg_all(
4878 int count,
4879 int forceit, /* hide buffers in current windows */
4880 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881{
4882 int i;
4883 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004884 char_u *opened; /* Array of weight for which args are open:
4885 * 0: not opened
4886 * 1: opened in other tab
4887 * 2: opened in curtab
4888 * 3: opened in curtab and curwin
4889 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004890 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 int use_firstwin = FALSE; /* use first window for arglist */
4892 int split_ret = OK;
4893 int p_ea_save;
4894 alist_T *alist; /* argument list to be used */
4895 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004896 tabpage_T *tpnext;
4897 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004898 win_T *old_curwin, *last_curwin;
4899 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004900 win_T *new_curwin = NULL;
4901 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
4903 if (ARGCOUNT <= 0)
4904 {
4905 /* Don't give an error message. We don't want it when the ":all"
4906 * command is in the .vimrc. */
4907 return;
4908 }
4909 setpcmark();
4910
4911 opened_len = ARGCOUNT;
4912 opened = alloc_clear((unsigned)opened_len);
4913 if (opened == NULL)
4914 return;
4915
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004916 /* Autocommands may do anything to the argument list. Make sure it's not
4917 * freed while we are working here by "locking" it. We still have to
4918 * watch out for its size to be changed. */
4919 alist = curwin->w_alist;
4920 ++alist->al_refcount;
4921
4922 old_curwin = curwin;
4923 old_curtab = curtab;
4924
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004925# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928
4929 /*
4930 * Try closing all windows that are not in the argument list.
4931 * Also close windows that are not full width;
4932 * When 'hidden' or "forceit" set the buffer becomes hidden.
4933 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004934 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004936 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004937 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004938 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004940 tpnext = curtab->tp_next;
4941 for (wp = firstwin; wp != NULL; wp = wpnext)
4942 {
4943 wpnext = wp->w_next;
4944 buf = wp->w_buffer;
4945 if (buf->b_ffname == NULL
Bram Moolenaar5a030a52016-12-01 17:48:29 +01004946 || (!keep_tabs && (buf->b_nwindows > 1
4947 || wp->w_width != Columns)))
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004948 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004949 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004951 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004952 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004954 if (i < alist->al_ga.ga_len
4955 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4956 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4957 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004959 int weight = 1;
4960
4961 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004962 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004963 ++weight;
4964 if (old_curwin == wp)
4965 ++weight;
4966 }
4967
4968 if (weight > (int)opened[i])
4969 {
4970 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004971 if (i == 0)
4972 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004973 if (new_curwin != NULL)
4974 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004975 new_curwin = wp;
4976 new_curtab = curtab;
4977 }
4978 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004979 else if (keep_tabs)
4980 i = opened_len;
4981
4982 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004983 {
4984 /* Use the current argument list for all windows
4985 * containing a file from it. */
4986 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004987 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004988 ++wp->w_alist->al_refcount;
4989 }
4990 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
4993 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004994 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004996 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02004998 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004999 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005001 /* If the buffer was changed, and we would like to hide it,
5002 * try autowriting. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005003 if (!buf_hide(buf) && buf->b_nwindows <= 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005004 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005006 bufref_T bufref;
5007
5008 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005009
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005010 (void)autowrite(buf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005011
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005012 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005013 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005014 {
5015 wpnext = firstwin; /* start all over... */
5016 continue;
5017 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005018 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005019 /* don't close last window */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005020 if (ONE_WINDOW
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005021 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005022 use_firstwin = TRUE;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005023 else
5024 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005025 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005026
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005027 /* check if autocommands removed the next window */
5028 if (!win_valid(wpnext))
5029 wpnext = firstwin; /* start all over... */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005034
5035 /* Without the ":tab" modifier only do the current tab page. */
5036 if (had_tab == 0 || tpnext == NULL)
5037 break;
5038
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005039 /* check if autocommands removed the next tab page */
5040 if (!valid_tabpage(tpnext))
5041 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005042
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005043 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 }
5045
5046 /*
5047 * Open a window for files in the argument list that don't have one.
5048 * ARGCOUNT may change while doing this, because of autocommands.
5049 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005050 if (count > opened_len || count <= 0)
5051 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5054 ++autocmd_no_enter;
5055 ++autocmd_no_leave;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005056 last_curwin = curwin;
5057 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005059 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
5060 * leaving an empty tab page when executed locally. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005061 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005062 && curbuf->b_ffname == NULL && !curbuf->b_changed)
5063 use_firstwin = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005064
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005065 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
5068 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005069 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
5071 /* Move the already present window to below the current window */
5072 if (curwin->w_arg_idx != i)
5073 {
5074 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
5075 {
5076 if (wpnext->w_arg_idx == i)
5077 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005078 if (keep_tabs)
5079 {
5080 new_curwin = wpnext;
5081 new_curtab = curtab;
5082 }
5083 else
5084 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 break;
5086 }
5087 }
5088 }
5089 }
5090 else if (split_ret == OK)
5091 {
5092 if (!use_firstwin) /* split current window */
5093 {
5094 p_ea_save = p_ea;
5095 p_ea = TRUE; /* use space from all windows */
5096 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5097 p_ea = p_ea_save;
5098 if (split_ret == FAIL)
5099 continue;
5100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 else /* first window: do autocmd for leaving this buffer */
5102 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103
5104 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005105 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 */
5107 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005108 if (i == 0)
5109 {
5110 new_curwin = curwin;
5111 new_curtab = curtab;
5112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5114 ECMD_ONE,
Bram Moolenaareb44a682017-08-03 22:44:55 +02005115 ((buf_hide(curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005117 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if (use_firstwin)
5119 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 use_firstwin = FALSE;
5121 }
5122 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005123
5124 /* When ":tab" was used open a new tab for a new window repeatedly. */
5125 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5126 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128
5129 /* Remove the "lock" on the argument list. */
5130 alist_unlink(alist);
5131
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 --autocmd_no_enter;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005133
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005134 /* restore last referenced tabpage's curwin */
5135 if (last_curtab != new_curtab)
5136 {
5137 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005138 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005139 if (win_valid(last_curwin))
5140 win_enter(last_curwin, FALSE);
5141 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005142 /* to window with first arg */
5143 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005144 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005145 if (win_valid(new_curwin))
5146 win_enter(new_curwin, FALSE);
5147
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 vim_free(opened);
5150}
5151
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152/*
5153 * Open a window for a number of buffers.
5154 */
5155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005156ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157{
5158 buf_T *buf;
5159 win_T *wp, *wpnext;
5160 int split_ret = OK;
5161 int p_ea_save;
5162 int open_wins = 0;
5163 int r;
5164 int count; /* Maximum number of windows to open. */
5165 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005166 int had_tab = cmdmod.tab;
5167 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168
5169 if (eap->addr_count == 0) /* make as many windows as possible */
5170 count = 9999;
5171 else
5172 count = eap->line2; /* make as many windows as specified */
5173 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5174 all = FALSE;
5175 else
5176 all = TRUE;
5177
5178 setpcmark();
5179
5180#ifdef FEAT_GUI
5181 need_mouse_correct = TRUE;
5182#endif
5183
5184 /*
5185 * Close superfluous windows (two windows for the same buffer).
5186 * Also close windows that are not full-width.
5187 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005188 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005189 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005190 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005192 tpnext = curtab->tp_next;
5193 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005195 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005196 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005197 || ((cmdmod.split & WSP_VERT)
5198 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005199 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005200 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005201 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005202 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005203 {
5204 win_close(wp, FALSE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005205 wpnext = firstwin; /* just in case an autocommand does
5206 something strange with windows */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005207 tpnext = first_tabpage; /* start all over... */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005208 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005209 }
5210 else
5211 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005213
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005214 /* Without the ":tab" modifier only do the current tab page. */
5215 if (had_tab == 0 || tpnext == NULL)
5216 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005217 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219
5220 /*
5221 * Go through the buffer list. When a buffer doesn't have a window yet,
5222 * open one. Otherwise move the window to the right position.
5223 * Watch out for autocommands that delete buffers or windows!
5224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5226 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5230 {
5231 /* Check if this buffer needs a window */
5232 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5233 continue;
5234
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005235 if (had_tab != 0)
5236 {
5237 /* With the ":tab" modifier don't move the window. */
5238 if (buf->b_nwindows > 0)
5239 wp = lastwin; /* buffer has a window, skip it */
5240 else
5241 wp = NULL;
5242 }
5243 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005244 {
5245 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005246 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005247 if (wp->w_buffer == buf)
5248 break;
5249 /* If the buffer already has a window, move it */
5250 if (wp != NULL)
5251 win_move_after(wp, curwin);
5252 }
5253
5254 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005256 bufref_T bufref;
5257
5258 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005259
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 /* Split the window and put the buffer in it */
5261 p_ea_save = p_ea;
5262 p_ea = TRUE; /* use space from all windows */
5263 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5264 ++open_wins;
5265 p_ea = p_ea_save;
5266 if (split_ret == FAIL)
5267 continue;
5268
5269 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005270#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 swap_exists_action = SEA_DIALOG;
5272#endif
5273 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005274 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005276 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005277#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 swap_exists_action = SEA_NONE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 break;
5281 }
Bram Moolenaare64ac772005-12-07 20:54:59 +00005282#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 if (swap_exists_action == SEA_QUIT)
5284 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005285# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005286 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005287
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005288 /* Reset the error/interrupt/exception state here so that
5289 * aborting() returns FALSE when closing a window. */
5290 enter_cleanup(&cs);
5291# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005292
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005293 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 win_close(curwin, TRUE);
5295 --open_wins;
5296 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005297 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005298
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005299# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005300 /* Restore the error/interrupt/exception state if not
5301 * discarded by a new aborting error, interrupt, or uncaught
5302 * exception. */
5303 leave_cleanup(&cs);
5304# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
5306 else
5307 handle_swap_exists(NULL);
5308#endif
5309 }
5310
5311 ui_breakcheck();
5312 if (got_int)
5313 {
5314 (void)vgetc(); /* only break the file loading, not the rest */
5315 break;
5316 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005317#ifdef FEAT_EVAL
5318 /* Autocommands deleted the buffer or aborted script processing!!! */
5319 if (aborting())
5320 break;
5321#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005322 /* When ":tab" was used open a new tab for a new window repeatedly. */
5323 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5324 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 --autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 win_enter(firstwin, FALSE); /* back to first window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329
5330 /*
5331 * Close superfluous windows.
5332 */
5333 for (wp = lastwin; open_wins > count; )
5334 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005335 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 if (!win_valid(wp))
5338 {
5339 /* BufWrite Autocommands made the window invalid, start over */
5340 wp = lastwin;
5341 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005342 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005344 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 --open_wins;
5346 wp = lastwin;
5347 }
5348 else
5349 {
5350 wp = wp->w_prev;
5351 if (wp == NULL)
5352 break;
5353 }
5354 }
5355}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005358static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005359
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360/*
5361 * do_modelines() - process mode lines for the current file
5362 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005363 * "flags" can be:
5364 * OPT_WINONLY only set options local to window
5365 * OPT_NOWIN don't set options local to window
5366 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 * Returns immediately if the "ml" option isn't set.
5368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005370do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005372 linenr_T lnum;
5373 int nmlines;
5374 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
5376 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5377 return;
5378
5379 /* Disallow recursive entry here. Can happen when executing a modeline
5380 * triggers an autocommand, which reloads modelines with a ":do". */
5381 if (entered)
5382 return;
5383
5384 ++entered;
5385 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5386 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005387 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 nmlines = 0;
5389
5390 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5391 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005392 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 nmlines = 0;
5394 --entered;
5395}
5396
5397#include "version.h" /* for version number */
5398
5399/*
5400 * chk_modeline() - check a single line for a mode string
5401 * Return FAIL if an error encountered.
5402 */
5403 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005404chk_modeline(
5405 linenr_T lnum,
5406 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407{
5408 char_u *s;
5409 char_u *e;
5410 char_u *linecopy; /* local copy of any modeline found */
5411 int prev;
5412 int vers;
5413 int end;
5414 int retval = OK;
5415 char_u *save_sourcing_name;
5416 linenr_T save_sourcing_lnum;
5417#ifdef FEAT_EVAL
5418 scid_T save_SID;
5419#endif
5420
5421 prev = -1;
5422 for (s = ml_get(lnum); *s != NUL; ++s)
5423 {
5424 if (prev == -1 || vim_isspace(prev))
5425 {
5426 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5427 || STRNCMP(s, "vi:", (size_t)3) == 0)
5428 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005429 /* Accept both "vim" and "Vim". */
5430 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {
5432 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5433 e = s + 4;
5434 else
5435 e = s + 3;
5436 vers = getdigits(&e);
5437 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005438 && (s[0] != 'V'
5439 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 && (s[3] == ':'
5441 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5442 || (VIM_VERSION_100 < vers && s[3] == '<')
5443 || (VIM_VERSION_100 > vers && s[3] == '>')
5444 || (VIM_VERSION_100 == vers && s[3] == '=')))
5445 break;
5446 }
5447 }
5448 prev = *s;
5449 }
5450
5451 if (*s)
5452 {
5453 do /* skip over "ex:", "vi:" or "vim:" */
5454 ++s;
5455 while (s[-1] != ':');
5456
5457 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5458 if (linecopy == NULL)
5459 return FAIL;
5460
5461 save_sourcing_lnum = sourcing_lnum;
5462 save_sourcing_name = sourcing_name;
5463 sourcing_lnum = lnum; /* prepare for emsg() */
5464 sourcing_name = (char_u *)"modelines";
5465
5466 end = FALSE;
5467 while (end == FALSE)
5468 {
5469 s = skipwhite(s);
5470 if (*s == NUL)
5471 break;
5472
5473 /*
5474 * Find end of set command: ':' or end of line.
5475 * Skip over "\:", replacing it with ":".
5476 */
5477 for (e = s; *e != ':' && *e != NUL; ++e)
5478 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005479 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 if (*e == NUL)
5481 end = TRUE;
5482
5483 /*
5484 * If there is a "set" command, require a terminating ':' and
5485 * ignore the stuff after the ':'.
5486 * "vi:set opt opt opt: foo" -- foo not interpreted
5487 * "vi:opt opt opt: foo" -- foo interpreted
5488 * Accept "se" for compatibility with Elvis.
5489 */
5490 if (STRNCMP(s, "set ", (size_t)4) == 0
5491 || STRNCMP(s, "se ", (size_t)3) == 0)
5492 {
5493 if (*e != ':') /* no terminating ':'? */
5494 break;
5495 end = TRUE;
5496 s = vim_strchr(s, ' ') + 1;
5497 }
5498 *e = NUL; /* truncate the set command */
5499
5500 if (*s != NUL) /* skip over an empty "::" */
5501 {
5502#ifdef FEAT_EVAL
5503 save_SID = current_SID;
5504 current_SID = SID_MODELINE;
5505#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005506 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507#ifdef FEAT_EVAL
5508 current_SID = save_SID;
5509#endif
5510 if (retval == FAIL) /* stop if error found */
5511 break;
5512 }
5513 s = e + 1; /* advance to next part */
5514 }
5515
5516 sourcing_lnum = save_sourcing_lnum;
5517 sourcing_name = save_sourcing_name;
5518
5519 vim_free(linecopy);
5520 }
5521 return retval;
5522}
5523
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005524#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005526read_viminfo_bufferlist(
5527 vir_T *virp,
5528 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
5530 char_u *tab;
5531 linenr_T lnum;
5532 colnr_T col;
5533 buf_T *buf;
5534 char_u *sfname;
5535 char_u *xline;
5536
5537 /* Handle long line and escaped characters. */
5538 xline = viminfo_readstring(virp, 1, FALSE);
5539
5540 /* don't read in if there are files on the command-line or if writing: */
5541 if (xline != NULL && !writing && ARGCOUNT == 0
5542 && find_viminfo_parameter('%') != NULL)
5543 {
5544 /* Format is: <fname> Tab <lnum> Tab <col>.
5545 * Watch out for a Tab in the file name, work from the end. */
5546 lnum = 0;
5547 col = 0;
5548 tab = vim_strrchr(xline, '\t');
5549 if (tab != NULL)
5550 {
5551 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005552 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 tab = vim_strrchr(xline, '\t');
5554 if (tab != NULL)
5555 {
5556 *tab++ = '\0';
5557 lnum = atol((char *)tab);
5558 }
5559 }
5560
5561 /* Expand "~/" in the file name at "line + 1" to a full path.
5562 * Then try shortening it by comparing with the current directory */
5563 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005564 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
5566 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5567 if (buf != NULL) /* just in case... */
5568 {
5569 buf->b_last_cursor.lnum = lnum;
5570 buf->b_last_cursor.col = col;
5571 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5572 }
5573 }
5574 vim_free(xline);
5575
5576 return viminfo_readline(virp);
5577}
5578
5579 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005580write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005584 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005586 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
5588 if (find_viminfo_parameter('%') == NULL)
5589 return;
5590
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005591 /* Without a number -1 is returned: do all buffers. */
5592 max_buffers = get_viminfo_parameter('%');
5593
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005595#define LINE_BUF_LEN (MAXPATHL + 40)
5596 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 if (line == NULL)
5598 return;
5599
Bram Moolenaarf740b292006-02-16 22:11:02 +00005600 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 set_last_cursor(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005603 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005604 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 {
5606 if (buf->b_fname == NULL
5607 || !buf->b_p_bl
5608#ifdef FEAT_QUICKFIX
5609 || bt_quickfix(buf)
5610#endif
Bram Moolenaare6278052017-08-13 18:11:17 +02005611#ifdef FEAT_TERMINAL
5612 || bt_terminal(buf)
5613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 || removable(buf->b_ffname))
5615 continue;
5616
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005617 if (max_buffers-- == 0)
5618 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 putc('%', fp);
5620 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005621 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 (long)buf->b_last_cursor.lnum,
5623 buf->b_last_cursor.col);
5624 viminfo_writestring(fp, line);
5625 }
5626 vim_free(line);
5627}
5628#endif
5629
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005630/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005631 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5632 */
5633 int
5634bt_normal(buf_T *buf)
5635{
5636 return buf != NULL && buf->b_p_bt[0] == NUL;
5637}
5638
5639/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005640 * Return TRUE if "buf" is the quickfix buffer.
5641 */
5642 int
5643bt_quickfix(buf_T *buf)
5644{
5645 return buf != NULL && buf->b_p_bt[0] == 'q';
5646}
5647
5648/*
5649 * Return TRUE if "buf" is a terminal buffer.
5650 */
5651 int
5652bt_terminal(buf_T *buf)
5653{
5654 return buf != NULL && buf->b_p_bt[0] == 't';
5655}
5656
5657/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005658 * Return TRUE if "buf" is a help buffer.
5659 */
5660 int
5661bt_help(buf_T *buf)
5662{
5663 return buf != NULL && buf->b_help;
5664}
5665
5666/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005667 * Return TRUE if "buf" is a prompt buffer.
5668 */
5669 int
5670bt_prompt(buf_T *buf)
5671{
5672 return buf != NULL && buf->b_p_bt[0] == 'p';
5673}
5674
5675/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005676 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5677 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005678 */
5679 int
5680bt_nofile(buf_T *buf)
5681{
5682 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5683 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005684 || buf->b_p_bt[0] == 't'
5685 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005686}
5687
5688/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005689 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5690 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005691 */
5692 int
5693bt_dontwrite(buf_T *buf)
5694{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005695 return buf != NULL && (buf->b_p_bt[0] == 'n'
5696 || buf->b_p_bt[0] == 't'
5697 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005698}
5699
5700 int
5701bt_dontwrite_msg(buf_T *buf)
5702{
5703 if (bt_dontwrite(buf))
5704 {
5705 EMSG(_("E382: Cannot write, 'buftype' option is set"));
5706 return TRUE;
5707 }
5708 return FALSE;
5709}
5710
5711/*
5712 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5713 * and 'bufhidden'.
5714 */
5715 int
5716buf_hide(buf_T *buf)
5717{
5718 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5719 switch (buf->b_p_bh[0])
5720 {
5721 case 'u': /* "unload" */
5722 case 'w': /* "wipe" */
5723 case 'd': return FALSE; /* "delete" */
5724 case 'h': return TRUE; /* "hide" */
5725 }
5726 return (p_hid || cmdmod.hide);
5727}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
5729/*
5730 * Return special buffer name.
5731 * Returns NULL when the buffer has a normal file name.
5732 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005733 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005734buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005736#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005738 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005739 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005740 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005741
5742 /*
5743 * For location list window, w_llist_ref points to the location list.
5744 * For quickfix window, w_llist_ref is NULL.
5745 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005746 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005747 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005748 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005749 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005752
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005754 * contains the name as specified by the user. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 if (bt_nofile(buf))
5756 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005757#ifdef FEAT_TERMINAL
5758 if (buf->b_term != NULL)
5759 return term_get_status_text(buf->b_term);
5760#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005761 if (buf->b_fname != NULL)
5762 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005763#ifdef FEAT_JOB_CHANNEL
5764 if (bt_prompt(buf))
5765 return (char_u *)_("[Prompt]");
5766#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005767 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005769
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005771 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 return NULL;
5773}
5774
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005775#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005776 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5777 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005778# define SWITCH_TO_WIN
5779
5780/*
5781 * Find a window that contains "buf" and switch to it.
5782 * If there is no such window, use the current window and change "curbuf".
5783 * Caller must initialize save_curbuf to NULL.
5784 * restore_win_for_buf() MUST be called later!
5785 */
5786 void
5787switch_to_win_for_buf(
5788 buf_T *buf,
5789 win_T **save_curwinp,
5790 tabpage_T **save_curtabp,
5791 bufref_T *save_curbuf)
5792{
5793 win_T *wp;
5794 tabpage_T *tp;
5795
5796 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5797 switch_buffer(save_curbuf, buf);
5798 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5799 {
5800 restore_win(*save_curwinp, *save_curtabp, TRUE);
5801 switch_buffer(save_curbuf, buf);
5802 }
5803}
5804
5805 void
5806restore_win_for_buf(
5807 win_T *save_curwin,
5808 tabpage_T *save_curtab,
5809 bufref_T *save_curbuf)
5810{
5811 if (save_curbuf->br_buf == NULL)
5812 restore_win(save_curwin, save_curtab, TRUE);
5813 else
5814 restore_buffer(save_curbuf);
5815}
5816#endif
5817
Bram Moolenaar4033c552017-09-16 20:54:51 +02005818#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005819/*
5820 * Find a window for buffer "buf".
5821 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5822 * If not found FAIL is returned.
5823 */
5824 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005825find_win_for_buf(
5826 buf_T *buf,
5827 win_T **wp,
5828 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005829{
5830 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5831 if ((*wp)->w_buffer == buf)
5832 goto win_found;
5833 return FAIL;
5834win_found:
5835 return OK;
5836}
5837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
5839#if defined(FEAT_SIGNS) || defined(PROTO)
5840/*
5841 * Insert the sign into the signlist.
5842 */
5843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005844insert_sign(
5845 buf_T *buf, /* buffer to store sign in */
5846 signlist_T *prev, /* previous sign entry */
5847 signlist_T *next, /* next sign entry */
5848 int id, /* sign ID */
5849 linenr_T lnum, /* line number which gets the mark */
5850 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851{
5852 signlist_T *newsign;
5853
5854 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5855 if (newsign != NULL)
5856 {
5857 newsign->id = id;
5858 newsign->lnum = lnum;
5859 newsign->typenr = typenr;
5860 newsign->next = next;
5861#ifdef FEAT_NETBEANS_INTG
5862 newsign->prev = prev;
5863 if (next != NULL)
5864 next->prev = newsign;
5865#endif
5866
5867 if (prev == NULL)
5868 {
5869 /* When adding first sign need to redraw the windows to create the
5870 * column for signs. */
5871 if (buf->b_signlist == NULL)
5872 {
5873 redraw_buf_later(buf, NOT_VALID);
5874 changed_cline_bef_curs();
5875 }
5876
5877 /* first sign in signlist */
5878 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005879#ifdef FEAT_NETBEANS_INTG
5880 if (netbeans_active())
5881 buf->b_has_sign_column = TRUE;
5882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 }
5884 else
5885 prev->next = newsign;
5886 }
5887}
5888
5889/*
5890 * Add the sign into the signlist. Find the right spot to do it though.
5891 */
5892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005893buf_addsign(
5894 buf_T *buf, /* buffer to store sign in */
5895 int id, /* sign ID */
5896 linenr_T lnum, /* line number which gets the mark */
5897 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
5899 signlist_T *sign; /* a sign in the signlist */
5900 signlist_T *prev; /* the previous sign */
5901
5902 prev = NULL;
5903 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5904 {
5905 if (lnum == sign->lnum && id == sign->id)
5906 {
5907 sign->typenr = typenr;
5908 return;
5909 }
5910 else if (
5911#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5912 id < 0 &&
5913#endif
5914 lnum < sign->lnum)
5915 {
5916#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5917 /* XXX - GRP: Is this because of sign slide problem? Or is it
5918 * really needed? Or is it because we allow multiple signs per
5919 * line? If so, should I add that feature to FEAT_SIGNS?
5920 */
5921 while (prev != NULL && prev->lnum == lnum)
5922 prev = prev->prev;
5923 if (prev == NULL)
5924 sign = buf->b_signlist;
5925 else
5926 sign = prev->next;
5927#endif
5928 insert_sign(buf, prev, sign, id, lnum, typenr);
5929 return;
5930 }
5931 prev = sign;
5932 }
5933#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5934 /* XXX - GRP: See previous comment */
5935 while (prev != NULL && prev->lnum == lnum)
5936 prev = prev->prev;
5937 if (prev == NULL)
5938 sign = buf->b_signlist;
5939 else
5940 sign = prev->next;
5941#endif
5942 insert_sign(buf, prev, sign, id, lnum, typenr);
5943
5944 return;
5945}
5946
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005947/*
5948 * For an existing, placed sign "markId" change the type to "typenr".
5949 * Returns the line number of the sign, or zero if the sign is not found.
5950 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005951 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005952buf_change_sign_type(
5953 buf_T *buf, /* buffer to store sign in */
5954 int markId, /* sign ID */
5955 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956{
5957 signlist_T *sign; /* a sign in the signlist */
5958
5959 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5960 {
5961 if (sign->id == markId)
5962 {
5963 sign->typenr = typenr;
5964 return sign->lnum;
5965 }
5966 }
5967
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005968 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969}
5970
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005971 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005972buf_getsigntype(
5973 buf_T *buf,
5974 linenr_T lnum,
5975 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
5977 signlist_T *sign; /* a sign in a b_signlist */
5978
5979 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5980 if (sign->lnum == lnum
5981 && (type == SIGN_ANY
5982# ifdef FEAT_SIGN_ICONS
5983 || (type == SIGN_ICON
5984 && sign_get_image(sign->typenr) != NULL)
5985# endif
5986 || (type == SIGN_TEXT
5987 && sign_get_text(sign->typenr) != NULL)
5988 || (type == SIGN_LINEHL
5989 && sign_get_attr(sign->typenr, TRUE) != 0)))
5990 return sign->typenr;
5991 return 0;
5992}
5993
5994
5995 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005996buf_delsign(
5997 buf_T *buf, /* buffer sign is stored in */
5998 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999{
6000 signlist_T **lastp; /* pointer to pointer to current sign */
6001 signlist_T *sign; /* a sign in a b_signlist */
6002 signlist_T *next; /* the next sign in a b_signlist */
6003 linenr_T lnum; /* line number whose sign was deleted */
6004
6005 lastp = &buf->b_signlist;
6006 lnum = 0;
6007 for (sign = buf->b_signlist; sign != NULL; sign = next)
6008 {
6009 next = sign->next;
6010 if (sign->id == id)
6011 {
6012 *lastp = next;
6013#ifdef FEAT_NETBEANS_INTG
6014 if (next != NULL)
6015 next->prev = sign->prev;
6016#endif
6017 lnum = sign->lnum;
6018 vim_free(sign);
6019 break;
6020 }
6021 else
6022 lastp = &sign->next;
6023 }
6024
6025 /* When deleted the last sign need to redraw the windows to remove the
6026 * sign column. */
6027 if (buf->b_signlist == NULL)
6028 {
6029 redraw_buf_later(buf, NOT_VALID);
6030 changed_cline_bef_curs();
6031 }
6032
6033 return lnum;
6034}
6035
6036
6037/*
6038 * Find the line number of the sign with the requested id. If the sign does
6039 * not exist, return 0 as the line number. This will still let the correct file
6040 * get loaded.
6041 */
6042 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006043buf_findsign(
6044 buf_T *buf, /* buffer to store sign in */
6045 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
6047 signlist_T *sign; /* a sign in the signlist */
6048
6049 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6050 if (sign->id == id)
6051 return sign->lnum;
6052
6053 return 0;
6054}
6055
6056 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006057buf_findsign_id(
6058 buf_T *buf, /* buffer whose sign we are searching for */
6059 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060{
6061 signlist_T *sign; /* a sign in the signlist */
6062
6063 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6064 if (sign->lnum == lnum)
6065 return sign->id;
6066
6067 return 0;
6068}
6069
6070
6071# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
6072/* see if a given type of sign exists on a specific line */
6073 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006074buf_findsigntype_id(
6075 buf_T *buf, /* buffer whose sign we are searching for */
6076 linenr_T lnum, /* line number of sign */
6077 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078{
6079 signlist_T *sign; /* a sign in the signlist */
6080
6081 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6082 if (sign->lnum == lnum && sign->typenr == typenr)
6083 return sign->id;
6084
6085 return 0;
6086}
6087
6088
6089# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6090/* return the number of icons on the given line */
6091 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006092buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093{
6094 signlist_T *sign; /* a sign in the signlist */
6095 int count = 0;
6096
6097 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6098 if (sign->lnum == lnum)
6099 if (sign_get_image(sign->typenr) != NULL)
6100 count++;
6101
6102 return count;
6103}
6104# endif /* FEAT_SIGN_ICONS */
6105# endif /* FEAT_NETBEANS_INTG */
6106
6107
6108/*
6109 * Delete signs in buffer "buf".
6110 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02006111 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006112buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113{
6114 signlist_T *next;
6115
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006116 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02006117 * sign column. Not when curwin is NULL (this means we're exiting). */
6118 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006119 {
6120 redraw_buf_later(buf, NOT_VALID);
6121 changed_cline_bef_curs();
6122 }
6123
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 while (buf->b_signlist != NULL)
6125 {
6126 next = buf->b_signlist->next;
6127 vim_free(buf->b_signlist);
6128 buf->b_signlist = next;
6129 }
6130}
6131
6132/*
6133 * Delete all signs in all buffers.
6134 */
6135 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006136buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137{
6138 buf_T *buf; /* buffer we are checking for signs */
6139
Bram Moolenaar29323592016-07-24 22:04:11 +02006140 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143}
6144
6145/*
6146 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
6147 */
6148 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006149sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150{
6151 buf_T *buf;
6152 signlist_T *p;
6153 char lbuf[BUFSIZ];
6154
6155 MSG_PUTS_TITLE(_("\n--- Signs ---"));
6156 msg_putchar('\n');
6157 if (rbuf == NULL)
6158 buf = firstbuf;
6159 else
6160 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006161 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 {
6163 if (buf->b_signlist != NULL)
6164 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006165 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006166 MSG_PUTS_ATTR(lbuf, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 msg_putchar('\n');
6168 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006169 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006171 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
6173 MSG_PUTS(lbuf);
6174 msg_putchar('\n');
6175 }
6176 if (rbuf != NULL)
6177 break;
6178 buf = buf->b_next;
6179 }
6180}
6181
6182/*
6183 * Adjust a placed sign for inserted/deleted lines.
6184 */
6185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006186sign_mark_adjust(
6187 linenr_T line1,
6188 linenr_T line2,
6189 long amount,
6190 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191{
6192 signlist_T *sign; /* a sign in a b_signlist */
6193
6194 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
6195 {
6196 if (sign->lnum >= line1 && sign->lnum <= line2)
6197 {
6198 if (amount == MAXLNUM)
6199 sign->lnum = line1;
6200 else
6201 sign->lnum += amount;
6202 }
6203 else if (sign->lnum > line2)
6204 sign->lnum += amount_after;
6205 }
6206}
6207#endif /* FEAT_SIGNS */
6208
6209/*
6210 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6211 */
6212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006213set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214{
6215 if (on != curbuf->b_p_bl)
6216 {
6217 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 if (on)
6219 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6220 else
6221 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 }
6223}
6224
6225/*
6226 * Read the file for "buf" again and check if the contents changed.
6227 * Return TRUE if it changed or this could not be checked.
6228 */
6229 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006230buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
6232 buf_T *newbuf;
6233 int differ = TRUE;
6234 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 exarg_T ea;
6237
6238 /* Allocate a buffer without putting it in the buffer list. */
6239 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6240 if (newbuf == NULL)
6241 return TRUE;
6242
6243 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6244 if (prep_exarg(&ea, buf) == FAIL)
6245 {
6246 wipe_buffer(newbuf, FALSE);
6247 return TRUE;
6248 }
6249
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 /* set curwin/curbuf to buf and save a few things */
6251 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252
Bram Moolenaar4770d092006-01-12 23:22:24 +00006253 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 && readfile(buf->b_ffname, buf->b_fname,
6255 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6256 &ea, READ_NEW | READ_DUMMY) == OK)
6257 {
6258 /* compare the two files line by line */
6259 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6260 {
6261 differ = FALSE;
6262 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6263 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6264 {
6265 differ = TRUE;
6266 break;
6267 }
6268 }
6269 }
6270 vim_free(ea.cmd);
6271
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 /* restore curwin/curbuf and a few other things */
6273 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274
6275 if (curbuf != newbuf) /* safety check */
6276 wipe_buffer(newbuf, FALSE);
6277
6278 return differ;
6279}
6280
6281/*
6282 * Wipe out a buffer and decrement the last buffer number if it was used for
6283 * this buffer. Call this to wipe out a temp buffer that does not contain any
6284 * marks.
6285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006287wipe_buffer(
6288 buf_T *buf,
6289 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291 if (buf->b_fnum == top_file_num - 1)
6292 --top_file_num;
6293
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006295 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006296
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006297 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006298
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006300 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301}