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