Mercurial > vim
annotate src/buffer.c @ 9414:1003973c99df v7.4.1988
commit https://github.com/vim/vim/commit/ab9c89b68dcbdb3fbda8c5a50dd90caca64f1bfd
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Jul 3 17:47:26 2016 +0200
patch 7.4.1988
Problem: When updating viminfo with file marks there is no time order.
Solution: Remember the time when a buffer was last used, store marks for
the most recently used buffers.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 03 Jul 2016 18:00:05 +0200 |
parents | f094d4085014 |
children | 073aebdba121 |
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 |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9228
diff
changeset
|
38 static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st); |
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9228
diff
changeset
|
39 static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp); |
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9228
diff
changeset
|
40 static int buf_same_ino(buf_T *buf, stat_T *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 |
9414
1003973c99df
commit https://github.com/vim/vim/commit/ab9c89b68dcbdb3fbda8c5a50dd90caca64f1bfd
Christian Brabandt <cb@256bit.org>
parents:
9387
diff
changeset
|
1622 #ifdef FEAT_VIMINFO |
1003973c99df
commit https://github.com/vim/vim/commit/ab9c89b68dcbdb3fbda8c5a50dd90caca64f1bfd
Christian Brabandt <cb@256bit.org>
parents:
9387
diff
changeset
|
1623 curbuf->b_last_used = vim_time(); |
1003973c99df
commit https://github.com/vim/vim/commit/ab9c89b68dcbdb3fbda8c5a50dd90caca64f1bfd
Christian Brabandt <cb@256bit.org>
parents:
9387
diff
changeset
|
1624 #endif |
1185 | 1625 |
7 | 1626 redraw_later(NOT_VALID); |
1627 } | |
1628 | |
961 | 1629 #if defined(FEAT_AUTOCHDIR) || defined(PROTO) |
1630 /* | |
1631 * 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
|
1632 * Don't do this while still starting up. |
961 | 1633 */ |
1634 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1635 do_autochdir(void) |
961 | 1636 { |
8216
03af9acbefb0
commit https://github.com/vim/vim/commit/6bd364e08461159ad3c153ffba4def5b896486a1
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
1637 if (starting == 0 |
03af9acbefb0
commit https://github.com/vim/vim/commit/6bd364e08461159ad3c153ffba4def5b896486a1
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
1638 && curbuf->b_ffname != NULL |
03af9acbefb0
commit https://github.com/vim/vim/commit/6bd364e08461159ad3c153ffba4def5b896486a1
Christian Brabandt <cb@256bit.org>
parents:
8212
diff
changeset
|
1639 && vim_chdirfile(curbuf->b_ffname) == OK) |
961 | 1640 shorten_fnames(TRUE); |
1641 } | |
1642 #endif | |
1643 | |
7 | 1644 /* |
1645 * functions for dealing with the buffer list | |
1646 */ | |
1647 | |
1648 /* | |
1649 * Add a file name to the buffer list. Return a pointer to the buffer. | |
1650 * If the same file name already exists return a pointer to that buffer. | |
1651 * If it does not exist, or if fname == NULL, a new entry is created. | |
1652 * If (flags & BLN_CURBUF) is TRUE, may use current buffer. | |
1653 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list. | |
1654 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer. | |
9149
18bbf31015c2
commit https://github.com/vim/vim/commit/b127cfd75f59e82580df395b6e2c009774644b16
Christian Brabandt <cb@256bit.org>
parents:
9106
diff
changeset
|
1655 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer. |
7 | 1656 * This is the ONLY way to create a new buffer. |
1657 */ | |
1658 static int top_file_num = 1; /* highest file number */ | |
1659 | |
1660 buf_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1661 buflist_new( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1662 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
|
1663 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
|
1664 linenr_T lnum, /* preferred cursor line */ |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1665 int flags) /* BLN_ defines */ |
7 | 1666 { |
1667 buf_T *buf; | |
1668 #ifdef UNIX | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9228
diff
changeset
|
1669 stat_T st; |
7 | 1670 #endif |
1671 | |
1672 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */ | |
1673 | |
1674 /* | |
1675 * If file name already exists in the list, update the entry. | |
1676 */ | |
1677 #ifdef UNIX | |
1678 /* On Unix we can use inode numbers when the file exists. Works better | |
1679 * for hard links. */ | |
1680 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0) | |
1681 st.st_dev = (dev_T)-1; | |
1682 #endif | |
9149
18bbf31015c2
commit https://github.com/vim/vim/commit/b127cfd75f59e82580df395b6e2c009774644b16
Christian Brabandt <cb@256bit.org>
parents:
9106
diff
changeset
|
1683 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf = |
7 | 1684 #ifdef UNIX |
1685 buflist_findname_stat(ffname, &st) | |
1686 #else | |
1687 buflist_findname(ffname) | |
1688 #endif | |
1689 ) != NULL) | |
1690 { | |
1691 vim_free(ffname); | |
1692 if (lnum != 0) | |
1693 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE); | |
1694 /* copy the options now, if 'cpo' doesn't have 's' and not done | |
1695 * already */ | |
1696 buf_copy_options(buf, 0); | |
1697 if ((flags & BLN_LISTED) && !buf->b_p_bl) | |
1698 { | |
1699 buf->b_p_bl = TRUE; | |
1700 #ifdef FEAT_AUTOCMD | |
1701 if (!(flags & BLN_DUMMY)) | |
5816 | 1702 { |
7 | 1703 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf); |
5816 | 1704 if (!buf_valid(buf)) |
1705 return NULL; | |
1706 } | |
7 | 1707 #endif |
1708 } | |
1709 return buf; | |
1710 } | |
1711 | |
1712 /* | |
1713 * If the current buffer has no name and no contents, use the current | |
1714 * buffer. Otherwise: Need to allocate a new buffer structure. | |
1715 * | |
1716 * This is the ONLY place where a new buffer structure is allocated! | |
625 | 1717 * (A spell file buffer is allocated in spell.c, but that's not a normal |
1718 * buffer.) | |
7 | 1719 */ |
1720 buf = NULL; | |
1721 if ((flags & BLN_CURBUF) | |
1722 && curbuf != NULL | |
1723 && curbuf->b_ffname == NULL | |
1724 && curbuf->b_nwindows <= 1 | |
1725 && (curbuf->b_ml.ml_mfp == NULL || bufempty())) | |
1726 { | |
1727 buf = curbuf; | |
1728 #ifdef FEAT_AUTOCMD | |
1729 /* It's like this buffer is deleted. Watch out for autocommands that | |
1730 * change curbuf! If that happens, allocate a new buffer anyway. */ | |
1731 if (curbuf->b_p_bl) | |
1732 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
1733 if (buf == curbuf) | |
1734 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); | |
1735 # ifdef FEAT_EVAL | |
36 | 1736 if (aborting()) /* autocmds may abort script processing */ |
7 | 1737 return NULL; |
1738 # endif | |
1739 #endif | |
1740 #ifdef FEAT_QUICKFIX | |
1741 # ifdef FEAT_AUTOCMD | |
1742 if (buf == curbuf) | |
1743 # endif | |
1744 { | |
1745 /* Make sure 'bufhidden' and 'buftype' are empty */ | |
1746 clear_string_option(&buf->b_p_bh); | |
1747 clear_string_option(&buf->b_p_bt); | |
1748 } | |
1749 #endif | |
1750 } | |
1751 if (buf != curbuf || curbuf == NULL) | |
1752 { | |
1753 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T)); | |
1754 if (buf == NULL) | |
1755 { | |
1756 vim_free(ffname); | |
1757 return NULL; | |
1758 } | |
4287 | 1759 #ifdef FEAT_EVAL |
1760 /* init b: variables */ | |
1761 buf->b_vars = dict_alloc(); | |
1762 if (buf->b_vars == NULL) | |
1763 { | |
1764 vim_free(ffname); | |
1765 vim_free(buf); | |
1766 return NULL; | |
1767 } | |
1768 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE); | |
1769 #endif | |
7 | 1770 } |
1771 | |
1772 if (ffname != NULL) | |
1773 { | |
1774 buf->b_ffname = ffname; | |
1775 buf->b_sfname = vim_strsave(sfname); | |
1776 } | |
1777 | |
1778 clear_wininfo(buf); | |
1779 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T)); | |
1780 | |
1781 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL)) | |
1782 || buf->b_wininfo == NULL) | |
1783 { | |
1784 vim_free(buf->b_ffname); | |
1785 buf->b_ffname = NULL; | |
1786 vim_free(buf->b_sfname); | |
1787 buf->b_sfname = NULL; | |
1788 if (buf != curbuf) | |
1789 free_buffer(buf); | |
1790 return NULL; | |
1791 } | |
1792 | |
1793 if (buf == curbuf) | |
1794 { | |
1795 /* 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
|
1796 buf_freeall(buf, 0); |
7 | 1797 if (buf != curbuf) /* autocommands deleted the buffer! */ |
1798 return NULL; | |
1799 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
36 | 1800 if (aborting()) /* autocmds may abort script processing */ |
7 | 1801 return NULL; |
1802 #endif | |
1803 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */ | |
3925 | 1804 |
1805 /* Init the options. */ | |
1806 buf->b_p_initialized = FALSE; | |
1807 buf_copy_options(buf, BCO_ENTER); | |
1808 | |
7 | 1809 #ifdef FEAT_KEYMAP |
1810 /* need to reload lmaps and set b:keymap_name */ | |
1811 curbuf->b_kmap_state |= KEYMAP_INIT; | |
1812 #endif | |
1813 } | |
1814 else | |
1815 { | |
1816 /* | |
1817 * put new buffer at the end of the buffer list | |
1818 */ | |
1819 buf->b_next = NULL; | |
1820 if (firstbuf == NULL) /* buffer list is empty */ | |
1821 { | |
1822 buf->b_prev = NULL; | |
1823 firstbuf = buf; | |
1824 } | |
1825 else /* append new buffer at end of list */ | |
1826 { | |
1827 lastbuf->b_next = buf; | |
1828 buf->b_prev = lastbuf; | |
1829 } | |
1830 lastbuf = buf; | |
1831 | |
1832 buf->b_fnum = top_file_num++; | |
1833 if (top_file_num < 0) /* wrap around (may cause duplicates) */ | |
1834 { | |
1835 EMSG(_("W14: Warning: List of file names overflow")); | |
1836 if (emsg_silent == 0) | |
1837 { | |
1838 out_flush(); | |
1839 ui_delay(3000L, TRUE); /* make sure it is noticed */ | |
1840 } | |
1841 top_file_num = 1; | |
1842 } | |
1843 | |
1844 /* | |
1845 * Always copy the options from the current buffer. | |
1846 */ | |
1847 buf_copy_options(buf, BCO_ALWAYS); | |
1848 } | |
1849 | |
1850 buf->b_wininfo->wi_fpos.lnum = lnum; | |
1851 buf->b_wininfo->wi_win = curwin; | |
1852 | |
126 | 1853 #ifdef FEAT_SYN_HL |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1854 hash_init(&buf->b_s.b_keywtab); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1855 hash_init(&buf->b_s.b_keywtab_ic); |
7 | 1856 #endif |
1857 | |
1858 buf->b_fname = buf->b_sfname; | |
1859 #ifdef UNIX | |
1860 if (st.st_dev == (dev_T)-1) | |
1873 | 1861 buf->b_dev_valid = FALSE; |
7 | 1862 else |
1863 { | |
1873 | 1864 buf->b_dev_valid = TRUE; |
7 | 1865 buf->b_dev = st.st_dev; |
1866 buf->b_ino = st.st_ino; | |
1867 } | |
1868 #endif | |
1869 buf->b_u_synced = TRUE; | |
1870 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED; | |
42 | 1871 if (flags & BLN_DUMMY) |
1872 buf->b_flags |= BF_DUMMY; | |
7 | 1873 buf_clear_file(buf); |
1874 clrallmarks(buf); /* clear marks */ | |
1875 fmarks_check_names(buf); /* check file marks for this file */ | |
1876 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */ | |
1877 #ifdef FEAT_AUTOCMD | |
1878 if (!(flags & BLN_DUMMY)) | |
1879 { | |
6639 | 1880 /* Tricky: these autocommands may change the buffer list. They could |
1881 * also split the window with re-using the one empty buffer. This may | |
1882 * result in unexpectedly losing the empty buffer. */ | |
7 | 1883 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf); |
5816 | 1884 if (!buf_valid(buf)) |
1885 return NULL; | |
7 | 1886 if (flags & BLN_LISTED) |
5816 | 1887 { |
7 | 1888 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf); |
5816 | 1889 if (!buf_valid(buf)) |
1890 return NULL; | |
1891 } | |
7 | 1892 # ifdef FEAT_EVAL |
36 | 1893 if (aborting()) /* autocmds may abort script processing */ |
7 | 1894 return NULL; |
1895 # endif | |
1896 } | |
1897 #endif | |
1898 | |
1899 return buf; | |
1900 } | |
1901 | |
1902 /* | |
1903 * Free the memory for the options of a buffer. | |
1904 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and | |
1905 * 'fileencoding'. | |
1906 */ | |
1907 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1908 free_buf_options( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1909 buf_T *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
1910 int free_p_ff) |
7 | 1911 { |
1912 if (free_p_ff) | |
1913 { | |
1914 #ifdef FEAT_MBYTE | |
1915 clear_string_option(&buf->b_p_fenc); | |
1916 #endif | |
1917 clear_string_option(&buf->b_p_ff); | |
1918 #ifdef FEAT_QUICKFIX | |
1919 clear_string_option(&buf->b_p_bh); | |
1920 clear_string_option(&buf->b_p_bt); | |
1921 #endif | |
1922 } | |
1923 #ifdef FEAT_FIND_ID | |
1924 clear_string_option(&buf->b_p_def); | |
1925 clear_string_option(&buf->b_p_inc); | |
1926 # ifdef FEAT_EVAL | |
1927 clear_string_option(&buf->b_p_inex); | |
1928 # endif | |
1929 #endif | |
1930 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL) | |
1931 clear_string_option(&buf->b_p_inde); | |
1932 clear_string_option(&buf->b_p_indk); | |
1933 #endif | |
788 | 1934 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL) |
1935 clear_string_option(&buf->b_p_bexpr); | |
1936 #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
|
1937 #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
|
1938 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
|
1939 #endif |
667 | 1940 #if defined(FEAT_EVAL) |
1941 clear_string_option(&buf->b_p_fex); | |
1942 #endif | |
7 | 1943 #ifdef FEAT_CRYPT |
1944 clear_string_option(&buf->b_p_key); | |
1945 #endif | |
1946 clear_string_option(&buf->b_p_kp); | |
1947 clear_string_option(&buf->b_p_mps); | |
1948 clear_string_option(&buf->b_p_fo); | |
41 | 1949 clear_string_option(&buf->b_p_flp); |
7 | 1950 clear_string_option(&buf->b_p_isk); |
1951 #ifdef FEAT_KEYMAP | |
1952 clear_string_option(&buf->b_p_keymap); | |
1953 ga_clear(&buf->b_kmap_ga); | |
1954 #endif | |
1955 #ifdef FEAT_COMMENTS | |
1956 clear_string_option(&buf->b_p_com); | |
1957 #endif | |
1958 #ifdef FEAT_FOLDING | |
1959 clear_string_option(&buf->b_p_cms); | |
1960 #endif | |
1961 clear_string_option(&buf->b_p_nf); | |
1962 #ifdef FEAT_SYN_HL | |
1963 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
|
1964 clear_string_option(&buf->b_s.b_syn_isk); |
737 | 1965 #endif |
1966 #ifdef FEAT_SPELL | |
2250
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_spc); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1968 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
|
1969 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
|
1970 buf->b_s.b_cap_prog = NULL; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2214
diff
changeset
|
1971 clear_string_option(&buf->b_s.b_p_spl); |
7 | 1972 #endif |
1973 #ifdef FEAT_SEARCHPATH | |
1974 clear_string_option(&buf->b_p_sua); | |
1975 #endif | |
1976 #ifdef FEAT_AUTOCMD | |
1977 clear_string_option(&buf->b_p_ft); | |
1978 #endif | |
1979 #ifdef FEAT_CINDENT | |
1980 clear_string_option(&buf->b_p_cink); | |
1981 clear_string_option(&buf->b_p_cino); | |
1982 #endif | |
1983 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT) | |
1984 clear_string_option(&buf->b_p_cinw); | |
1985 #endif | |
1986 #ifdef FEAT_INS_EXPAND | |
1987 clear_string_option(&buf->b_p_cpt); | |
1988 #endif | |
12 | 1989 #ifdef FEAT_COMPL_FUNC |
1990 clear_string_option(&buf->b_p_cfu); | |
502 | 1991 clear_string_option(&buf->b_p_ofu); |
12 | 1992 #endif |
7 | 1993 #ifdef FEAT_QUICKFIX |
1994 clear_string_option(&buf->b_p_gp); | |
1995 clear_string_option(&buf->b_p_mp); | |
1996 clear_string_option(&buf->b_p_efm); | |
1997 #endif | |
1998 clear_string_option(&buf->b_p_ep); | |
1999 clear_string_option(&buf->b_p_path); | |
2000 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
|
2001 clear_string_option(&buf->b_p_tc); |
7 | 2002 #ifdef FEAT_INS_EXPAND |
2003 clear_string_option(&buf->b_p_dict); | |
2004 clear_string_option(&buf->b_p_tsr); | |
2005 #endif | |
12 | 2006 #ifdef FEAT_TEXTOBJ |
2007 clear_string_option(&buf->b_p_qe); | |
2008 #endif | |
7 | 2009 buf->b_p_ar = -1; |
5446 | 2010 buf->b_p_ul = NO_LOCAL_UNDOLEVEL; |
5712 | 2011 #ifdef FEAT_LISP |
2012 clear_string_option(&buf->b_p_lw); | |
2013 #endif | |
6243 | 2014 clear_string_option(&buf->b_p_bkc); |
7 | 2015 } |
2016 | |
2017 /* | |
2018 * get alternate file n | |
2019 * set linenr to lnum or altfpos.lnum if lnum == 0 | |
2020 * also set cursor column to altfpos.col if 'startofline' is not set. | |
2021 * if (options & GETF_SETMARK) call setpcmark() | |
2022 * if (options & GETF_ALT) we are jumping to an alternate file. | |
2023 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping | |
2024 * | |
2025 * return FAIL for failure, OK for success | |
2026 */ | |
2027 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2028 buflist_getfile( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2029 int n, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2030 linenr_T lnum, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2031 int options, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2032 int forceit) |
7 | 2033 { |
2034 buf_T *buf; | |
2035 #ifdef FEAT_WINDOWS | |
2036 win_T *wp = NULL; | |
2037 #endif | |
2038 pos_T *fpos; | |
2039 colnr_T col; | |
2040 | |
2041 buf = buflist_findnr(n); | |
2042 if (buf == NULL) | |
2043 { | |
2044 if ((options & GETF_ALT) && n == 0) | |
2045 EMSG(_(e_noalt)); | |
2046 else | |
2047 EMSGN(_("E92: Buffer %ld not found"), n); | |
2048 return FAIL; | |
2049 } | |
2050 | |
2051 /* if alternate file is the current buffer, nothing to do */ | |
2052 if (buf == curbuf) | |
2053 return OK; | |
2054 | |
633 | 2055 if (text_locked()) |
631 | 2056 { |
633 | 2057 text_locked_msg(); |
7 | 2058 return FAIL; |
631 | 2059 } |
819 | 2060 #ifdef FEAT_AUTOCMD |
2061 if (curbuf_locked()) | |
2062 return FAIL; | |
2063 #endif | |
7 | 2064 |
2065 /* altfpos may be changed by getfile(), get it now */ | |
2066 if (lnum == 0) | |
2067 { | |
2068 fpos = buflist_findfpos(buf); | |
2069 lnum = fpos->lnum; | |
2070 col = fpos->col; | |
2071 } | |
2072 else | |
2073 col = 0; | |
2074 | |
2075 #ifdef FEAT_WINDOWS | |
2076 if (options & GETF_SWITCH) | |
2077 { | |
1618 | 2078 /* If 'switchbuf' contains "useopen": jump to first window containing |
2079 * "buf" if one exists */ | |
2080 if (swb_flags & SWB_USEOPEN) | |
7 | 2081 wp = buf_jump_open_win(buf); |
6843 | 2082 |
4352 | 2083 /* If 'switchbuf' contains "usetab": jump to first window in any tab |
1618 | 2084 * page containing "buf" if one exists */ |
2085 if (wp == NULL && (swb_flags & SWB_USETAB)) | |
825 | 2086 wp = buf_jump_open_tab(buf); |
6843 | 2087 |
2088 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the | |
2089 * current buffer isn't empty: open new tab or window */ | |
2090 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB)) | |
2091 && !bufempty()) | |
7 | 2092 { |
6843 | 2093 if (swb_flags & SWB_NEWTAB) |
1618 | 2094 tabpage_new(); |
6843 | 2095 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0) |
2096 == FAIL) | |
7 | 2097 return FAIL; |
2583 | 2098 RESET_BINDING(curwin); |
7 | 2099 } |
2100 } | |
2101 #endif | |
2102 | |
2103 ++RedrawingDisabled; | |
2104 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK), | |
2105 lnum, forceit) <= 0) | |
2106 { | |
2107 --RedrawingDisabled; | |
2108 | |
2109 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */ | |
2110 if (!p_sol && col != 0) | |
2111 { | |
2112 curwin->w_cursor.col = col; | |
2113 check_cursor_col(); | |
2114 #ifdef FEAT_VIRTUALEDIT | |
2115 curwin->w_cursor.coladd = 0; | |
2116 #endif | |
2117 curwin->w_set_curswant = TRUE; | |
2118 } | |
2119 return OK; | |
2120 } | |
2121 --RedrawingDisabled; | |
2122 return FAIL; | |
2123 } | |
2124 | |
2125 /* | |
2126 * go to the last know line number for the current buffer | |
2127 */ | |
2128 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2129 buflist_getfpos(void) |
7 | 2130 { |
2131 pos_T *fpos; | |
2132 | |
2133 fpos = buflist_findfpos(curbuf); | |
2134 | |
2135 curwin->w_cursor.lnum = fpos->lnum; | |
2136 check_cursor_lnum(); | |
2137 | |
2138 if (p_sol) | |
2139 curwin->w_cursor.col = 0; | |
2140 else | |
2141 { | |
2142 curwin->w_cursor.col = fpos->col; | |
2143 check_cursor_col(); | |
2144 #ifdef FEAT_VIRTUALEDIT | |
2145 curwin->w_cursor.coladd = 0; | |
2146 #endif | |
2147 curwin->w_set_curswant = TRUE; | |
2148 } | |
2149 } | |
2150 | |
42 | 2151 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO) |
2152 /* | |
2153 * Find file in buffer list by name (it has to be for the current window). | |
2154 * Returns NULL if not found. | |
2155 */ | |
2156 buf_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2157 buflist_findname_exp(char_u *fname) |
42 | 2158 { |
2159 char_u *ffname; | |
2160 buf_T *buf = NULL; | |
2161 | |
2162 /* First make the name into a full path name */ | |
2163 ffname = FullName_save(fname, | |
2164 #ifdef UNIX | |
2165 TRUE /* force expansion, get rid of symbolic links */ | |
2166 #else | |
2167 FALSE | |
2168 #endif | |
2169 ); | |
2170 if (ffname != NULL) | |
2171 { | |
2172 buf = buflist_findname(ffname); | |
2173 vim_free(ffname); | |
2174 } | |
2175 return buf; | |
2176 } | |
2177 #endif | |
2178 | |
7 | 2179 /* |
2180 * Find file in buffer list by name (it has to be for the current window). | |
2181 * "ffname" must have a full path. | |
42 | 2182 * Skips dummy buffers. |
2183 * Returns NULL if not found. | |
7 | 2184 */ |
2185 buf_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2186 buflist_findname(char_u *ffname) |
7 | 2187 { |
2188 #ifdef UNIX | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9228
diff
changeset
|
2189 stat_T st; |
7 | 2190 |
2191 if (mch_stat((char *)ffname, &st) < 0) | |
2192 st.st_dev = (dev_T)-1; | |
2193 return buflist_findname_stat(ffname, &st); | |
2194 } | |
2195 | |
2196 /* | |
2197 * Same as buflist_findname(), but pass the stat structure to avoid getting it | |
2198 * twice for the same file. | |
42 | 2199 * Returns NULL if not found. |
7 | 2200 */ |
2201 static buf_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2202 buflist_findname_stat( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2203 char_u *ffname, |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9228
diff
changeset
|
2204 stat_T *stp) |
7 | 2205 { |
2206 #endif | |
2207 buf_T *buf; | |
2208 | |
2209 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
42 | 2210 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname |
7 | 2211 #ifdef UNIX |
2212 , stp | |
2213 #endif | |
2214 )) | |
2215 return buf; | |
2216 return NULL; | |
2217 } | |
2218 | |
4236 | 2219 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \ |
2220 || defined(PROTO) | |
7 | 2221 /* |
2222 * Find file in buffer list by a regexp pattern. | |
2223 * Return fnum of the found buffer. | |
2224 * Return < 0 for error. | |
2225 */ | |
2226 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2227 buflist_findpat( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2228 char_u *pattern, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2229 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
|
2230 int unlisted, /* find unlisted buffers */ |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2231 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
|
2232 int curtab_only) /* find buffers in current tab only */ |
7 | 2233 { |
2234 buf_T *buf; | |
2235 int match = -1; | |
2236 int find_listed; | |
2237 char_u *pat; | |
2238 char_u *patend; | |
2239 int attempt; | |
2240 char_u *p; | |
2241 int toggledollar; | |
2242 | |
2243 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#')) | |
2244 { | |
2245 if (*pattern == '%') | |
2246 match = curbuf->b_fnum; | |
2247 else | |
2248 match = curwin->w_alt_fnum; | |
2249 #ifdef FEAT_DIFF | |
2250 if (diffmode && !diff_mode_buf(buflist_findnr(match))) | |
2251 match = -1; | |
2252 #endif | |
2253 } | |
2254 | |
2255 /* | |
2256 * Try four ways of matching a listed buffer: | |
2257 * attempt == 0: without '^' or '$' (at any position) | |
1187 | 2258 * attempt == 1: with '^' at start (only at position 0) |
7 | 2259 * attempt == 2: with '$' at end (only match at end) |
2260 * attempt == 3: with '^' at start and '$' at end (only full match) | |
2261 * Repeat this for finding an unlisted buffer if there was no matching | |
2262 * listed buffer. | |
2263 */ | |
2264 else | |
2265 { | |
2266 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE); | |
2267 if (pat == NULL) | |
2268 return -1; | |
2269 patend = pat + STRLEN(pat) - 1; | |
2270 toggledollar = (patend > pat && *patend == '$'); | |
2271 | |
2272 /* First try finding a listed buffer. If not found and "unlisted" | |
2273 * is TRUE, try finding an unlisted buffer. */ | |
2274 find_listed = TRUE; | |
2275 for (;;) | |
2276 { | |
2277 for (attempt = 0; attempt <= 3; ++attempt) | |
2278 { | |
6375 | 2279 regmatch_T regmatch; |
2280 | |
7 | 2281 /* may add '^' and '$' */ |
2282 if (toggledollar) | |
2283 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */ | |
2284 p = pat; | |
2285 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */ | |
2286 ++p; | |
6375 | 2287 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0); |
2288 if (regmatch.regprog == NULL) | |
7 | 2289 { |
2290 vim_free(pat); | |
2291 return -1; | |
2292 } | |
2293 | |
2294 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
2295 if (buf->b_p_bl == find_listed | |
2296 #ifdef FEAT_DIFF | |
2297 && (!diffmode || diff_mode_buf(buf)) | |
2298 #endif | |
6375 | 2299 && buflist_match(®match, buf, FALSE) != NULL) |
7 | 2300 { |
4236 | 2301 if (curtab_only) |
2302 { | |
2303 /* Ignore the match if the buffer is not open in | |
2304 * the current tab. */ | |
2305 #ifdef FEAT_WINDOWS | |
2306 win_T *wp; | |
2307 | |
2308 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
2309 if (wp->w_buffer == buf) | |
2310 break; | |
2311 if (wp == NULL) | |
2312 continue; | |
2313 #else | |
2314 if (curwin->w_buffer != buf) | |
2315 continue; | |
2316 #endif | |
2317 } | |
7 | 2318 if (match >= 0) /* already found a match */ |
2319 { | |
2320 match = -2; | |
2321 break; | |
2322 } | |
2323 match = buf->b_fnum; /* remember first match */ | |
2324 } | |
2325 | |
6375 | 2326 vim_regfree(regmatch.regprog); |
7 | 2327 if (match >= 0) /* found one match */ |
2328 break; | |
2329 } | |
2330 | |
2331 /* Only search for unlisted buffers if there was no match with | |
2332 * a listed buffer. */ | |
2333 if (!unlisted || !find_listed || match != -1) | |
2334 break; | |
2335 find_listed = FALSE; | |
2336 } | |
2337 | |
2338 vim_free(pat); | |
2339 } | |
2340 | |
2341 if (match == -2) | |
2342 EMSG2(_("E93: More than one match for %s"), pattern); | |
2343 else if (match < 0) | |
2344 EMSG2(_("E94: No matching buffer for %s"), pattern); | |
2345 return match; | |
2346 } | |
2347 #endif | |
2348 | |
2349 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
2350 | |
2351 /* | |
2352 * Find all buffer names that match. | |
2353 * For command line expansion of ":buf" and ":sbuf". | |
2354 * Return OK if matches found, FAIL otherwise. | |
2355 */ | |
2356 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2357 ExpandBufnames( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2358 char_u *pat, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2359 int *num_file, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2360 char_u ***file, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2361 int options) |
7 | 2362 { |
2363 int count = 0; | |
2364 buf_T *buf; | |
2365 int round; | |
2366 char_u *p; | |
2367 int attempt; | |
170 | 2368 char_u *patc; |
7 | 2369 |
2370 *num_file = 0; /* return values in case of FAIL */ | |
2371 *file = NULL; | |
2372 | |
170 | 2373 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */ |
2374 if (*pat == '^') | |
2375 { | |
2376 patc = alloc((unsigned)STRLEN(pat) + 11); | |
2377 if (patc == NULL) | |
2378 return FAIL; | |
2379 STRCPY(patc, "\\(^\\|[\\/]\\)"); | |
2380 STRCPY(patc + 11, pat + 1); | |
2381 } | |
2382 else | |
2383 patc = pat; | |
2384 | |
7 | 2385 /* |
170 | 2386 * attempt == 0: try match with '\<', match at start of word |
267 | 2387 * attempt == 1: try match without '\<', match anywhere |
7 | 2388 */ |
267 | 2389 for (attempt = 0; attempt <= 1; ++attempt) |
7 | 2390 { |
6375 | 2391 regmatch_T regmatch; |
2392 | |
267 | 2393 if (attempt > 0 && patc == pat) |
170 | 2394 break; /* there was no anchor, no need to try again */ |
6375 | 2395 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC); |
2396 if (regmatch.regprog == NULL) | |
7 | 2397 { |
170 | 2398 if (patc != pat) |
2399 vim_free(patc); | |
2400 return FAIL; | |
7 | 2401 } |
2402 | |
2403 /* | |
2404 * round == 1: Count the matches. | |
2405 * round == 2: Build the array to keep the matches. | |
2406 */ | |
2407 for (round = 1; round <= 2; ++round) | |
2408 { | |
2409 count = 0; | |
2410 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
2411 { | |
2412 if (!buf->b_p_bl) /* skip unlisted buffers */ | |
2413 continue; | |
6375 | 2414 p = buflist_match(®match, buf, p_wic); |
7 | 2415 if (p != NULL) |
2416 { | |
2417 if (round == 1) | |
2418 ++count; | |
2419 else | |
2420 { | |
2421 if (options & WILD_HOME_REPLACE) | |
2422 p = home_replace_save(buf, p); | |
2423 else | |
2424 p = vim_strsave(p); | |
2425 (*file)[count++] = p; | |
2426 } | |
2427 } | |
2428 } | |
2429 if (count == 0) /* no match found, break here */ | |
2430 break; | |
2431 if (round == 1) | |
2432 { | |
2433 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); | |
2434 if (*file == NULL) | |
2435 { | |
6375 | 2436 vim_regfree(regmatch.regprog); |
170 | 2437 if (patc != pat) |
2438 vim_free(patc); | |
7 | 2439 return FAIL; |
2440 } | |
2441 } | |
2442 } | |
6375 | 2443 vim_regfree(regmatch.regprog); |
7 | 2444 if (count) /* match(es) found, break here */ |
2445 break; | |
2446 } | |
2447 | |
170 | 2448 if (patc != pat) |
2449 vim_free(patc); | |
2450 | |
7 | 2451 *num_file = count; |
2452 return (count == 0 ? FAIL : OK); | |
2453 } | |
2454 | |
2455 #endif /* FEAT_CMDL_COMPL */ | |
2456 | |
2457 #ifdef HAVE_BUFLIST_MATCH | |
2458 /* | |
2459 * Check for a match on the file name for buffer "buf" with regprog "prog". | |
2460 */ | |
2461 static char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2462 buflist_match( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2463 regmatch_T *rmp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2464 buf_T *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2465 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */ |
7 | 2466 { |
2467 char_u *match; | |
2468 | |
2469 /* First try the short file name, then the long file name. */ | |
6375 | 2470 match = fname_match(rmp, buf->b_sfname, ignore_case); |
7 | 2471 if (match == NULL) |
6375 | 2472 match = fname_match(rmp, buf->b_ffname, ignore_case); |
7 | 2473 |
2474 return match; | |
2475 } | |
2476 | |
2477 /* | |
2478 * Try matching the regexp in "prog" with file name "name". | |
2479 * Return "name" when there is a match, NULL when not. | |
2480 */ | |
2481 static char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2482 fname_match( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2483 regmatch_T *rmp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2484 char_u *name, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2485 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */ |
7 | 2486 { |
2487 char_u *match = NULL; | |
2488 char_u *p; | |
2489 | |
2490 if (name != NULL) | |
2491 { | |
6241 | 2492 /* Ignore case when 'fileignorecase' or the argument is set. */ |
6375 | 2493 rmp->rm_ic = p_fic || ignore_case; |
2494 if (vim_regexec(rmp, name, (colnr_T)0)) | |
7 | 2495 match = name; |
2496 else | |
2497 { | |
2498 /* Replace $(HOME) with '~' and try matching again. */ | |
2499 p = home_replace_save(NULL, name); | |
6375 | 2500 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0)) |
7 | 2501 match = name; |
2502 vim_free(p); | |
2503 } | |
2504 } | |
2505 | |
2506 return match; | |
2507 } | |
2508 #endif | |
2509 | |
2510 /* | |
2511 * find file in buffer list by number | |
2512 */ | |
2513 buf_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2514 buflist_findnr(int nr) |
7 | 2515 { |
2516 buf_T *buf; | |
2517 | |
2518 if (nr == 0) | |
2519 nr = curwin->w_alt_fnum; | |
2520 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
2521 if (buf->b_fnum == nr) | |
9228
ea504064c996
commit https://github.com/vim/vim/commit/fd89d7ea81b18d32363456b16258174dc9e095dc
Christian Brabandt <cb@256bit.org>
parents:
9149
diff
changeset
|
2522 return buf; |
7 | 2523 return NULL; |
2524 } | |
2525 | |
2526 /* | |
2527 * Get name of file 'n' in the buffer list. | |
2528 * When the file has no name an empty string is returned. | |
2529 * home_replace() is used to shorten the file name (used for marks). | |
2530 * Returns a pointer to allocated memory, of NULL when failed. | |
2531 */ | |
2532 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2533 buflist_nr2name( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2534 int n, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2535 int fullname, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2536 int helptail) /* for help buffers return tail only */ |
7 | 2537 { |
2538 buf_T *buf; | |
2539 | |
2540 buf = buflist_findnr(n); | |
2541 if (buf == NULL) | |
2542 return NULL; | |
2543 return home_replace_save(helptail ? buf : NULL, | |
2544 fullname ? buf->b_ffname : buf->b_fname); | |
2545 } | |
2546 | |
2547 /* | |
2548 * Set the "lnum" and "col" for the buffer "buf" and the current window. | |
2549 * When "copy_options" is TRUE save the local window option values. | |
2550 * When "lnum" is 0 only do the options. | |
2551 */ | |
2552 static void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2553 buflist_setfpos( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2554 buf_T *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2555 win_T *win, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2556 linenr_T lnum, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2557 colnr_T col, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2558 int copy_options) |
7 | 2559 { |
2560 wininfo_T *wip; | |
2561 | |
2562 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) | |
2563 if (wip->wi_win == win) | |
2564 break; | |
2565 if (wip == NULL) | |
2566 { | |
2567 /* allocate a new entry */ | |
2568 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T)); | |
2569 if (wip == NULL) | |
2570 return; | |
2571 wip->wi_win = win; | |
2572 if (lnum == 0) /* set lnum even when it's 0 */ | |
2573 lnum = 1; | |
2574 } | |
2575 else | |
2576 { | |
2577 /* remove the entry from the list */ | |
2578 if (wip->wi_prev) | |
2579 wip->wi_prev->wi_next = wip->wi_next; | |
2580 else | |
2581 buf->b_wininfo = wip->wi_next; | |
2582 if (wip->wi_next) | |
2583 wip->wi_next->wi_prev = wip->wi_prev; | |
2584 if (copy_options && wip->wi_optset) | |
2585 { | |
2586 clear_winopt(&wip->wi_opt); | |
2587 #ifdef FEAT_FOLDING | |
2588 deleteFoldRecurse(&wip->wi_folds); | |
2589 #endif | |
2590 } | |
2591 } | |
2592 if (lnum != 0) | |
2593 { | |
2594 wip->wi_fpos.lnum = lnum; | |
2595 wip->wi_fpos.col = col; | |
2596 } | |
2597 if (copy_options) | |
2598 { | |
2599 /* Save the window-specific option values. */ | |
2600 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt); | |
2601 #ifdef FEAT_FOLDING | |
2602 wip->wi_fold_manual = win->w_fold_manual; | |
2603 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds); | |
2604 #endif | |
2605 wip->wi_optset = TRUE; | |
2606 } | |
2607 | |
2608 /* insert the entry in front of the list */ | |
2609 wip->wi_next = buf->b_wininfo; | |
2610 buf->b_wininfo = wip; | |
2611 wip->wi_prev = NULL; | |
2612 if (wip->wi_next) | |
2613 wip->wi_next->wi_prev = wip; | |
2614 | |
2615 return; | |
2616 } | |
2617 | |
1743 | 2618 #ifdef FEAT_DIFF |
7799
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7687
diff
changeset
|
2619 static int wininfo_other_tab_diff(wininfo_T *wip); |
1743 | 2620 |
2621 /* | |
2622 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab | |
2623 * page. That's because a diff is local to a tab page. | |
2624 */ | |
2625 static int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2626 wininfo_other_tab_diff(wininfo_T *wip) |
1743 | 2627 { |
2628 win_T *wp; | |
2629 | |
2630 if (wip->wi_opt.wo_diff) | |
2631 { | |
2632 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
2633 /* return FALSE when it's a window in the current tab page, thus | |
2634 * the buffer was in diff mode here */ | |
2635 if (wip->wi_win == wp) | |
2636 return FALSE; | |
2637 return TRUE; | |
2638 } | |
2639 return FALSE; | |
2640 } | |
2641 #endif | |
2642 | |
7 | 2643 /* |
2644 * Find info for the current window in buffer "buf". | |
2645 * If not found, return the info for the most recently used window. | |
1743 | 2646 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in |
2647 * another tab page. | |
7 | 2648 * Returns NULL when there isn't any info. |
2649 */ | |
2650 static wininfo_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2651 find_wininfo( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2652 buf_T *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2653 int skip_diff_buffer UNUSED) |
7 | 2654 { |
2655 wininfo_T *wip; | |
2656 | |
2657 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) | |
1743 | 2658 if (wip->wi_win == curwin |
2659 #ifdef FEAT_DIFF | |
2660 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip)) | |
2661 #endif | |
2662 ) | |
7 | 2663 break; |
1743 | 2664 |
2665 /* If no wininfo for curwin, use the first in the list (that doesn't have | |
2666 * 'diff' set and is in another tab page). */ | |
2667 if (wip == NULL) | |
2668 { | |
2669 #ifdef FEAT_DIFF | |
2670 if (skip_diff_buffer) | |
2671 { | |
2672 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) | |
2673 if (!wininfo_other_tab_diff(wip)) | |
2674 break; | |
2675 } | |
2676 else | |
2677 #endif | |
2678 wip = buf->b_wininfo; | |
2679 } | |
7 | 2680 return wip; |
2681 } | |
2682 | |
2683 /* | |
2684 * Reset the local window options to the values last used in this window. | |
2685 * If the buffer wasn't used in this window before, use the values from | |
2686 * the most recently used window. If the values were never set, use the | |
2687 * global values for the window. | |
2688 */ | |
2689 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2690 get_winopts(buf_T *buf) |
7 | 2691 { |
2692 wininfo_T *wip; | |
2693 | |
2694 clear_winopt(&curwin->w_onebuf_opt); | |
2695 #ifdef FEAT_FOLDING | |
2696 clearFolding(curwin); | |
2697 #endif | |
2698 | |
1743 | 2699 wip = find_wininfo(buf, TRUE); |
7 | 2700 if (wip != NULL && wip->wi_optset) |
2701 { | |
2702 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt); | |
2703 #ifdef FEAT_FOLDING | |
2704 curwin->w_fold_manual = wip->wi_fold_manual; | |
2705 curwin->w_foldinvalid = TRUE; | |
2706 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds); | |
2707 #endif | |
2708 } | |
2709 else | |
2710 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt); | |
2711 | |
2712 #ifdef FEAT_FOLDING | |
2713 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */ | |
2714 if (p_fdls >= 0) | |
2715 curwin->w_p_fdl = p_fdls; | |
2716 #endif | |
2799 | 2717 #ifdef FEAT_SYN_HL |
2718 check_colorcolumn(curwin); | |
2719 #endif | |
7 | 2720 } |
2721 | |
2722 /* | |
2723 * Find the position (lnum and col) for the buffer 'buf' for the current | |
2724 * window. | |
2725 * Returns a pointer to no_position if no position is found. | |
2726 */ | |
2727 pos_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2728 buflist_findfpos(buf_T *buf) |
7 | 2729 { |
2730 wininfo_T *wip; | |
1869 | 2731 static pos_T no_position = INIT_POS_T(1, 0, 0); |
7 | 2732 |
1743 | 2733 wip = find_wininfo(buf, FALSE); |
7 | 2734 if (wip != NULL) |
2735 return &(wip->wi_fpos); | |
2736 else | |
2737 return &no_position; | |
2738 } | |
2739 | |
2740 /* | |
2741 * Find the lnum for the buffer 'buf' for the current window. | |
2742 */ | |
2743 linenr_T | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2744 buflist_findlnum(buf_T *buf) |
7 | 2745 { |
2746 return buflist_findfpos(buf)->lnum; | |
2747 } | |
2748 | |
2749 #if defined(FEAT_LISTCMDS) || defined(PROTO) | |
2750 /* | |
2751 * List all know file names (for :files and :buffers command). | |
2752 */ | |
2753 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2754 buflist_list(exarg_T *eap) |
7 | 2755 { |
2756 buf_T *buf; | |
2757 int len; | |
2758 int i; | |
2759 | |
2760 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next) | |
2761 { | |
2762 /* skip unlisted buffers, unless ! was used */ | |
6945 | 2763 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u')) |
2764 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl) | |
2765 || (vim_strchr(eap->arg, '+') | |
2766 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf))) | |
2767 || (vim_strchr(eap->arg, 'a') | |
2768 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0)) | |
2769 || (vim_strchr(eap->arg, 'h') | |
2770 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0)) | |
2771 || (vim_strchr(eap->arg, '-') && buf->b_p_ma) | |
2772 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro) | |
2773 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR)) | |
2774 || (vim_strchr(eap->arg, '%') && buf != curbuf) | |
2775 || (vim_strchr(eap->arg, '#') | |
2776 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum))) | |
7 | 2777 continue; |
2778 msg_putchar('\n'); | |
2779 if (buf_spname(buf) != NULL) | |
3839 | 2780 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1); |
7 | 2781 else |
2782 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE); | |
2783 | |
302 | 2784 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"", |
7 | 2785 buf->b_fnum, |
2786 buf->b_p_bl ? ' ' : 'u', | |
2787 buf == curbuf ? '%' : | |
2788 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '), | |
2789 buf->b_ml.ml_mfp == NULL ? ' ' : | |
2790 (buf->b_nwindows == 0 ? 'h' : 'a'), | |
2791 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '), | |
2792 (buf->b_flags & BF_READERR) ? 'x' | |
300 | 2793 : (bufIsChanged(buf) ? '+' : ' '), |
2794 NameBuff); | |
7572
992fe73d4ee6
commit https://github.com/vim/vim/commit/507edf63df75fe228e0f76b845b58d60266e65d8
Christian Brabandt <cb@256bit.org>
parents:
7266
diff
changeset
|
2795 if (len > IOSIZE - 20) |
992fe73d4ee6
commit https://github.com/vim/vim/commit/507edf63df75fe228e0f76b845b58d60266e65d8
Christian Brabandt <cb@256bit.org>
parents:
7266
diff
changeset
|
2796 len = IOSIZE - 20; |
7 | 2797 |
2798 /* put "line 999" in column 40 or after the file name */ | |
2799 i = 40 - vim_strsize(IObuff); | |
2800 do | |
2801 { | |
2802 IObuff[len++] = ' '; | |
2803 } while (--i > 0 && len < IOSIZE - 18); | |
1869 | 2804 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len), |
2805 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum | |
272 | 2806 : (long)buflist_findlnum(buf)); |
7 | 2807 msg_outtrans(IObuff); |
2808 out_flush(); /* output one line at a time */ | |
2809 ui_breakcheck(); | |
2810 } | |
2811 } | |
2812 #endif | |
2813 | |
2814 /* | |
2815 * Get file name and line number for file 'fnum'. | |
2816 * Used by DoOneCmd() for translating '%' and '#'. | |
2817 * Used by insert_reg() and cmdline_paste() for '#' register. | |
2818 * Return FAIL if not found, OK for success. | |
2819 */ | |
2820 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2821 buflist_name_nr( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2822 int fnum, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2823 char_u **fname, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2824 linenr_T *lnum) |
7 | 2825 { |
2826 buf_T *buf; | |
2827 | |
2828 buf = buflist_findnr(fnum); | |
2829 if (buf == NULL || buf->b_fname == NULL) | |
2830 return FAIL; | |
2831 | |
2832 *fname = buf->b_fname; | |
2833 *lnum = buflist_findlnum(buf); | |
2834 | |
2835 return OK; | |
2836 } | |
2837 | |
2838 /* | |
2839 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'. | |
2840 * The file name with the full path is also remembered, for when :cd is used. | |
2841 * Returns FAIL for failure (file name already in use by other buffer) | |
2842 * OK otherwise. | |
2843 */ | |
2844 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2845 setfname( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2846 buf_T *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2847 char_u *ffname, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2848 char_u *sfname, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2849 int message) /* give message when buffer already exists */ |
7 | 2850 { |
42 | 2851 buf_T *obuf = NULL; |
7 | 2852 #ifdef UNIX |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9228
diff
changeset
|
2853 stat_T st; |
7 | 2854 #endif |
2855 | |
2856 if (ffname == NULL || *ffname == NUL) | |
2857 { | |
2858 /* Removing the name. */ | |
2859 vim_free(buf->b_ffname); | |
2860 vim_free(buf->b_sfname); | |
2861 buf->b_ffname = NULL; | |
2862 buf->b_sfname = NULL; | |
2863 #ifdef UNIX | |
2864 st.st_dev = (dev_T)-1; | |
2865 #endif | |
2866 } | |
2867 else | |
2868 { | |
2869 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */ | |
2870 if (ffname == NULL) /* out of memory */ | |
2871 return FAIL; | |
2872 | |
2873 /* | |
2874 * if the file name is already used in another buffer: | |
2875 * - if the buffer is loaded, fail | |
2876 * - if the buffer is not loaded, delete it from the list | |
2877 */ | |
2878 #ifdef UNIX | |
2879 if (mch_stat((char *)ffname, &st) < 0) | |
2880 st.st_dev = (dev_T)-1; | |
42 | 2881 #endif |
2882 if (!(buf->b_flags & BF_DUMMY)) | |
2883 #ifdef UNIX | |
2884 obuf = buflist_findname_stat(ffname, &st); | |
7 | 2885 #else |
42 | 2886 obuf = buflist_findname(ffname); |
7 | 2887 #endif |
2888 if (obuf != NULL && obuf != buf) | |
2889 { | |
2890 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */ | |
2891 { | |
2892 if (message) | |
2893 EMSG(_("E95: Buffer with this name already exists")); | |
2894 vim_free(ffname); | |
2895 return FAIL; | |
2896 } | |
3365 | 2897 /* delete from the list */ |
2898 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE); | |
7 | 2899 } |
2900 sfname = vim_strsave(sfname); | |
2901 if (ffname == NULL || sfname == NULL) | |
2902 { | |
2903 vim_free(sfname); | |
2904 vim_free(ffname); | |
2905 return FAIL; | |
2906 } | |
2907 #ifdef USE_FNAME_CASE | |
2908 # ifdef USE_LONG_FNAME | |
2909 if (USE_LONG_FNAME) | |
2910 # endif | |
2911 fname_case(sfname, 0); /* set correct case for short file name */ | |
2912 #endif | |
2913 vim_free(buf->b_ffname); | |
2914 vim_free(buf->b_sfname); | |
2915 buf->b_ffname = ffname; | |
2916 buf->b_sfname = sfname; | |
2917 } | |
2918 buf->b_fname = buf->b_sfname; | |
2919 #ifdef UNIX | |
2920 if (st.st_dev == (dev_T)-1) | |
1873 | 2921 buf->b_dev_valid = FALSE; |
7 | 2922 else |
2923 { | |
1873 | 2924 buf->b_dev_valid = TRUE; |
7 | 2925 buf->b_dev = st.st_dev; |
2926 buf->b_ino = st.st_ino; | |
2927 } | |
2928 #endif | |
2929 | |
2930 buf->b_shortname = FALSE; | |
2931 | |
2932 buf_name_changed(buf); | |
2933 return OK; | |
2934 } | |
2935 | |
2936 /* | |
41 | 2937 * Crude way of changing the name of a buffer. Use with care! |
2938 * The name should be relative to the current directory. | |
2939 */ | |
2940 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2941 buf_set_name(int fnum, char_u *name) |
41 | 2942 { |
2943 buf_T *buf; | |
2944 | |
2945 buf = buflist_findnr(fnum); | |
2946 if (buf != NULL) | |
2947 { | |
2948 vim_free(buf->b_sfname); | |
2949 vim_free(buf->b_ffname); | |
844 | 2950 buf->b_ffname = vim_strsave(name); |
2951 buf->b_sfname = NULL; | |
2952 /* Allocate ffname and expand into full path. Also resolves .lnk | |
2953 * files on Win32. */ | |
2954 fname_expand(buf, &buf->b_ffname, &buf->b_sfname); | |
41 | 2955 buf->b_fname = buf->b_sfname; |
2956 } | |
2957 } | |
2958 | |
2959 /* | |
7 | 2960 * Take care of what needs to be done when the name of buffer "buf" has |
2961 * changed. | |
2962 */ | |
2963 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2964 buf_name_changed(buf_T *buf) |
7 | 2965 { |
2966 /* | |
2967 * If the file name changed, also change the name of the swapfile | |
2968 */ | |
2969 if (buf->b_ml.ml_mfp != NULL) | |
2970 ml_setname(buf); | |
2971 | |
2972 if (curwin->w_buffer == buf) | |
2973 check_arg_idx(curwin); /* check file name for arg list */ | |
2974 #ifdef FEAT_TITLE | |
2975 maketitle(); /* set window title */ | |
2976 #endif | |
2977 #ifdef FEAT_WINDOWS | |
2978 status_redraw_all(); /* status lines need to be redrawn */ | |
2979 #endif | |
2980 fmarks_check_names(buf); /* check named file marks */ | |
2981 ml_timestamp(buf); /* reset timestamp */ | |
2982 } | |
2983 | |
2984 /* | |
2985 * set alternate file name for current window | |
2986 * | |
2987 * Used by do_one_cmd(), do_write() and do_ecmd(). | |
2988 * Return the buffer. | |
2989 */ | |
2990 buf_T * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2991 setaltfname( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2992 char_u *ffname, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2993 char_u *sfname, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
2994 linenr_T lnum) |
7 | 2995 { |
2996 buf_T *buf; | |
2997 | |
2998 /* Create a buffer. 'buflisted' is not set if it's a new buffer */ | |
2999 buf = buflist_new(ffname, sfname, lnum, 0); | |
22 | 3000 if (buf != NULL && !cmdmod.keepalt) |
7 | 3001 curwin->w_alt_fnum = buf->b_fnum; |
3002 return buf; | |
3003 } | |
3004 | |
3005 /* | |
3006 * Get alternate file name for current window. | |
3007 * Return NULL if there isn't any, and give error message if requested. | |
3008 */ | |
3009 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3010 getaltfname( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3011 int errmsg) /* give error message */ |
7 | 3012 { |
3013 char_u *fname; | |
3014 linenr_T dummy; | |
3015 | |
3016 if (buflist_name_nr(0, &fname, &dummy) == FAIL) | |
3017 { | |
3018 if (errmsg) | |
3019 EMSG(_(e_noalt)); | |
3020 return NULL; | |
3021 } | |
3022 return fname; | |
3023 } | |
3024 | |
3025 /* | |
3026 * Add a file name to the buflist and return its number. | |
3027 * Uses same flags as buflist_new(), except BLN_DUMMY. | |
3028 * | |
3029 * used by qf_init(), main() and doarglist() | |
3030 */ | |
3031 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3032 buflist_add(char_u *fname, int flags) |
7 | 3033 { |
3034 buf_T *buf; | |
3035 | |
3036 buf = buflist_new(fname, NULL, (linenr_T)0, flags); | |
3037 if (buf != NULL) | |
3038 return buf->b_fnum; | |
3039 return 0; | |
3040 } | |
3041 | |
3042 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | |
3043 /* | |
3044 * Adjust slashes in file names. Called after 'shellslash' was set. | |
3045 */ | |
3046 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3047 buflist_slash_adjust(void) |
7 | 3048 { |
3049 buf_T *bp; | |
3050 | |
3051 for (bp = firstbuf; bp != NULL; bp = bp->b_next) | |
3052 { | |
3053 if (bp->b_ffname != NULL) | |
3054 slash_adjust(bp->b_ffname); | |
3055 if (bp->b_sfname != NULL) | |
3056 slash_adjust(bp->b_sfname); | |
3057 } | |
3058 } | |
3059 #endif | |
3060 | |
3061 /* | |
1743 | 3062 * Set alternate cursor position for the current buffer and window "win". |
7 | 3063 * Also save the local window option values. |
3064 */ | |
3065 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3066 buflist_altfpos(win_T *win) |
7 | 3067 { |
1743 | 3068 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE); |
7 | 3069 } |
3070 | |
3071 /* | |
3072 * Return TRUE if 'ffname' is not the same file as current file. | |
3073 * Fname must have a full path (expanded by mch_FullName()). | |
3074 */ | |
3075 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3076 otherfile(char_u *ffname) |
7 | 3077 { |
3078 return otherfile_buf(curbuf, ffname | |
3079 #ifdef UNIX | |
3080 , NULL | |
3081 #endif | |
3082 ); | |
3083 } | |
3084 | |
3085 static int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3086 otherfile_buf( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3087 buf_T *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3088 char_u *ffname |
7 | 3089 #ifdef UNIX |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9228
diff
changeset
|
3090 , stat_T *stp |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3091 #endif |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3092 ) |
7 | 3093 { |
3094 /* no name is different */ | |
3095 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL) | |
3096 return TRUE; | |
3097 if (fnamecmp(ffname, buf->b_ffname) == 0) | |
3098 return FALSE; | |
3099 #ifdef UNIX | |
3100 { | |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9228
diff
changeset
|
3101 stat_T st; |
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9228
diff
changeset
|
3102 |
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9228
diff
changeset
|
3103 /* If no stat_T given, get it now */ |
7 | 3104 if (stp == NULL) |
3105 { | |
1873 | 3106 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0) |
7 | 3107 st.st_dev = (dev_T)-1; |
3108 stp = &st; | |
3109 } | |
3110 /* Use dev/ino to check if the files are the same, even when the names | |
3111 * are different (possible with links). Still need to compare the | |
3112 * name above, for when the file doesn't exist yet. | |
3113 * Problem: The dev/ino changes when a file is deleted (and created | |
3114 * again) and remains the same when renamed/moved. We don't want to | |
3115 * mch_stat() each buffer each time, that would be too slow. Get the | |
3116 * dev/ino again when they appear to match, but not when they appear | |
3117 * to be different: Could skip a buffer when it's actually the same | |
3118 * file. */ | |
3119 if (buf_same_ino(buf, stp)) | |
3120 { | |
3121 buf_setino(buf); | |
3122 if (buf_same_ino(buf, stp)) | |
3123 return FALSE; | |
3124 } | |
3125 } | |
3126 #endif | |
3127 return TRUE; | |
3128 } | |
3129 | |
3130 #if defined(UNIX) || defined(PROTO) | |
3131 /* | |
3132 * Set inode and device number for a buffer. | |
3133 * Must always be called when b_fname is changed!. | |
3134 */ | |
3135 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3136 buf_setino(buf_T *buf) |
7 | 3137 { |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9228
diff
changeset
|
3138 stat_T st; |
7 | 3139 |
3140 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0) | |
3141 { | |
1873 | 3142 buf->b_dev_valid = TRUE; |
7 | 3143 buf->b_dev = st.st_dev; |
3144 buf->b_ino = st.st_ino; | |
3145 } | |
3146 else | |
1873 | 3147 buf->b_dev_valid = FALSE; |
7 | 3148 } |
3149 | |
3150 /* | |
3151 * Return TRUE if dev/ino in buffer "buf" matches with "stp". | |
3152 */ | |
3153 static int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3154 buf_same_ino( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3155 buf_T *buf, |
9387
f094d4085014
commit https://github.com/vim/vim/commit/8767f52fbfd4f053ce00a978227c95f1d7d323fe
Christian Brabandt <cb@256bit.org>
parents:
9228
diff
changeset
|
3156 stat_T *stp) |
7 | 3157 { |
1873 | 3158 return (buf->b_dev_valid |
7 | 3159 && stp->st_dev == buf->b_dev |
3160 && stp->st_ino == buf->b_ino); | |
3161 } | |
3162 #endif | |
3163 | |
667 | 3164 /* |
3165 * Print info about the current buffer. | |
3166 */ | |
7 | 3167 void |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3168 fileinfo( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3169 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
|
3170 int shorthelp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3171 int dont_truncate) |
7 | 3172 { |
3173 char_u *name; | |
3174 int n; | |
3175 char_u *p; | |
3176 char_u *buffer; | |
272 | 3177 size_t len; |
7 | 3178 |
3179 buffer = alloc(IOSIZE); | |
3180 if (buffer == NULL) | |
3181 return; | |
3182 | |
3183 if (fullname > 1) /* 2 CTRL-G: include buffer number */ | |
3184 { | |
1869 | 3185 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum); |
7 | 3186 p = buffer + STRLEN(buffer); |
3187 } | |
3188 else | |
3189 p = buffer; | |
3190 | |
3191 *p++ = '"'; | |
3192 if (buf_spname(curbuf) != NULL) | |
3835 | 3193 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1); |
7 | 3194 else |
3195 { | |
3196 if (!fullname && curbuf->b_fname != NULL) | |
3197 name = curbuf->b_fname; | |
3198 else | |
3199 name = curbuf->b_ffname; | |
3200 home_replace(shorthelp ? curbuf : NULL, name, p, | |
3201 (int)(IOSIZE - (p - buffer)), TRUE); | |
3202 } | |
3203 | |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3204 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s", |
7 | 3205 curbufIsChanged() ? (shortmess(SHM_MOD) |
3206 ? " [+]" : _(" [Modified]")) : " ", | |
3207 (curbuf->b_flags & BF_NOTEDITED) | |
3208 #ifdef FEAT_QUICKFIX | |
3209 && !bt_dontwrite(curbuf) | |
3210 #endif | |
3211 ? _("[Not edited]") : "", | |
3212 (curbuf->b_flags & BF_NEW) | |
3213 #ifdef FEAT_QUICKFIX | |
3214 && !bt_dontwrite(curbuf) | |
3215 #endif | |
3216 ? _("[New file]") : "", | |
3217 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "", | |
4795
8360a59aa04b
updated for version 7.3.1144
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
3218 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]") |
7 | 3219 : _("[readonly]")) : "", |
3220 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK) | |
3221 || curbuf->b_p_ro) ? | |
3222 " " : ""); | |
3223 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100 | |
3224 * causes an overflow, thus for large numbers divide instead. */ | |
3225 if (curwin->w_cursor.lnum > 1000000L) | |
3226 n = (int)(((long)curwin->w_cursor.lnum) / | |
3227 ((long)curbuf->b_ml.ml_line_count / 100L)); | |
3228 else | |
3229 n = (int)(((long)curwin->w_cursor.lnum * 100L) / | |
3230 (long)curbuf->b_ml.ml_line_count); | |
3231 if (curbuf->b_ml.ml_flags & ML_EMPTY) | |
3232 { | |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3233 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg)); |
7 | 3234 } |
3235 #ifdef FEAT_CMDL_INFO | |
3236 else if (p_ru) | |
3237 { | |
3238 /* Current line and column are already on the screen -- webb */ | |
3239 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
|
3240 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n); |
7 | 3241 else |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3242 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"), |
7 | 3243 (long)curbuf->b_ml.ml_line_count, n); |
3244 } | |
3245 #endif | |
3246 else | |
3247 { | |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2253
diff
changeset
|
3248 vim_snprintf_add((char *)buffer, IOSIZE, |
7 | 3249 _("line %ld of %ld --%d%%-- col "), |
3250 (long)curwin->w_cursor.lnum, | |
3251 (long)curbuf->b_ml.ml_line_count, | |
3252 n); | |
3253 validate_virtcol(); | |
1869 | 3254 len = STRLEN(buffer); |
3255 col_print(buffer + len, IOSIZE - len, | |
7 | 3256 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1); |
3257 } | |
3258 | |
1869 | 3259 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE)); |
7 | 3260 |
3261 if (dont_truncate) | |
3262 { | |
3263 /* Temporarily set msg_scroll to avoid the message being truncated. | |
3264 * First call msg_start() to get the message in the right place. */ | |
3265 msg_start(); | |
3266 n = msg_scroll; | |
3267 msg_scroll = TRUE; | |
3268 msg(buffer); | |
3269 msg_scroll = n; | |
3270 } | |
3271 else | |
3272 { | |
3273 p = msg_trunc_attr(buffer, FALSE, 0); | |
3274 if (restart_edit != 0 || (msg_scrolled && !need_wait_return)) | |
3275 /* Need to repeat the message after redrawing when: | |
3276 * - When restart_edit is set (otherwise there will be a delay | |
3277 * before redrawing). | |
3278 * - When the screen was scrolled but there is no wait-return | |
3279 * prompt. */ | |
678 | 3280 set_keep_msg(p, 0); |
7 | 3281 } |
3282 | |
3283 vim_free(buffer); | |
3284 } | |
3285 | |
3286 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3287 col_print( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3288 char_u *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3289 size_t buflen, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3290 int col, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3291 int vcol) |
7 | 3292 { |
3293 if (col == vcol) | |
1869 | 3294 vim_snprintf((char *)buf, buflen, "%d", col); |
7 | 3295 else |
1869 | 3296 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol); |
7 | 3297 } |
3298 | |
3299 #if defined(FEAT_TITLE) || defined(PROTO) | |
3300 /* | |
3301 * put file name in title bar of window and in icon title | |
3302 */ | |
3303 | |
3304 static char_u *lasttitle = NULL; | |
3305 static char_u *lasticon = NULL; | |
3306 | |
3307 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3308 maketitle(void) |
7 | 3309 { |
3310 char_u *p; | |
3311 char_u *t_str = NULL; | |
3312 char_u *i_name; | |
3313 char_u *i_str = NULL; | |
3314 int maxlen = 0; | |
3315 int len; | |
3316 int mustset; | |
3317 char_u buf[IOSIZE]; | |
3318 int off; | |
3319 | |
3320 if (!redrawing()) | |
3321 { | |
3322 /* Postpone updating the title when 'lazyredraw' is set. */ | |
3323 need_maketitle = TRUE; | |
3324 return; | |
3325 } | |
3326 | |
3327 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
|
3328 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL) |
7 | 3329 return; |
3330 | |
3331 if (p_title) | |
3332 { | |
3333 if (p_titlelen > 0) | |
3334 { | |
3335 maxlen = p_titlelen * Columns / 100; | |
3336 if (maxlen < 10) | |
3337 maxlen = 10; | |
3338 } | |
3339 | |
3340 t_str = buf; | |
3341 if (*p_titlestring != NUL) | |
3342 { | |
3343 #ifdef FEAT_STL_OPT | |
771 | 3344 if (stl_syntax & STL_IN_TITLE) |
3345 { | |
3346 int use_sandbox = FALSE; | |
3347 int save_called_emsg = called_emsg; | |
675 | 3348 |
3349 # ifdef FEAT_EVAL | |
771 | 3350 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0); |
675 | 3351 # endif |
771 | 3352 called_emsg = FALSE; |
7 | 3353 build_stl_str_hl(curwin, t_str, sizeof(buf), |
675 | 3354 p_titlestring, use_sandbox, |
681 | 3355 0, maxlen, NULL, NULL); |
771 | 3356 if (called_emsg) |
3357 set_string_option_direct((char_u *)"titlestring", -1, | |
3358 (char_u *)"", OPT_FREE, SID_ERROR); | |
3359 called_emsg |= save_called_emsg; | |
3360 } | |
7 | 3361 else |
3362 #endif | |
3363 t_str = p_titlestring; | |
3364 } | |
3365 else | |
3366 { | |
3367 /* format: "fname + (path) (1 of 2) - VIM" */ | |
3368 | |
3780 | 3369 #define SPACE_FOR_FNAME (IOSIZE - 100) |
3370 #define SPACE_FOR_DIR (IOSIZE - 20) | |
3371 #define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */ | |
7 | 3372 if (curbuf->b_fname == NULL) |
3780 | 3373 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME); |
7 | 3374 else |
3375 { | |
3376 p = transstr(gettail(curbuf->b_fname)); | |
3780 | 3377 vim_strncpy(buf, p, SPACE_FOR_FNAME); |
7 | 3378 vim_free(p); |
3379 } | |
3380 | |
3381 switch (bufIsChanged(curbuf) | |
3382 + (curbuf->b_p_ro * 2) | |
3383 + (!curbuf->b_p_ma * 4)) | |
3384 { | |
3385 case 1: STRCAT(buf, " +"); break; | |
3386 case 2: STRCAT(buf, " ="); break; | |
3387 case 3: STRCAT(buf, " =+"); break; | |
3388 case 4: | |
3389 case 6: STRCAT(buf, " -"); break; | |
3390 case 5: | |
3391 case 7: STRCAT(buf, " -+"); break; | |
3392 } | |
3393 | |
3394 if (curbuf->b_fname != NULL) | |
3395 { | |
3396 /* Get path of file, replace home dir with ~ */ | |
3397 off = (int)STRLEN(buf); | |
3398 buf[off++] = ' '; | |
3399 buf[off++] = '('; | |
3400 home_replace(curbuf, curbuf->b_ffname, | |
3780 | 3401 buf + off, SPACE_FOR_DIR - off, TRUE); |
7 | 3402 #ifdef BACKSLASH_IN_FILENAME |
3403 /* avoid "c:/name" to be reduced to "c" */ | |
3404 if (isalpha(buf[off]) && buf[off + 1] == ':') | |
3405 off += 2; | |
3406 #endif | |
3407 /* remove the file name */ | |
39 | 3408 p = gettail_sep(buf + off); |
7 | 3409 if (p == buf + off) |
3410 /* must be a help buffer */ | |
416 | 3411 vim_strncpy(buf + off, (char_u *)_("help"), |
3780 | 3412 (size_t)(SPACE_FOR_DIR - off - 1)); |
7 | 3413 else |
3414 *p = NUL; | |
39 | 3415 |
3780 | 3416 /* Translate unprintable chars and concatenate. Keep some |
3417 * room for the server name. When there is no room (very long | |
3418 * file name) use (...). */ | |
3419 if (off < SPACE_FOR_DIR) | |
3420 { | |
3421 p = transstr(buf + off); | |
3422 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off)); | |
3423 vim_free(p); | |
3424 } | |
3425 else | |
3426 { | |
3427 vim_strncpy(buf + off, (char_u *)"...", | |
3428 (size_t)(SPACE_FOR_ARGNR - off)); | |
3429 } | |
7 | 3430 STRCAT(buf, ")"); |
3431 } | |
3432 | |
3780 | 3433 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE); |
7 | 3434 |
3435 #if defined(FEAT_CLIENTSERVER) | |
3436 if (serverName != NULL) | |
3437 { | |
3438 STRCAT(buf, " - "); | |
2768 | 3439 vim_strcat(buf, serverName, IOSIZE); |
7 | 3440 } |
3441 else | |
3442 #endif | |
3443 STRCAT(buf, " - VIM"); | |
3444 | |
3445 if (maxlen > 0) | |
3446 { | |
3447 /* make it shorter by removing a bit in the middle */ | |
3277 | 3448 if (vim_strsize(buf) > maxlen) |
3449 trunc_string(buf, buf, maxlen, IOSIZE); | |
7 | 3450 } |
3451 } | |
3452 } | |
3453 mustset = ti_change(t_str, &lasttitle); | |
3454 | |
3455 if (p_icon) | |
3456 { | |
3457 i_str = buf; | |
3458 if (*p_iconstring != NUL) | |
3459 { | |
3460 #ifdef FEAT_STL_OPT | |
771 | 3461 if (stl_syntax & STL_IN_ICON) |
3462 { | |
3463 int use_sandbox = FALSE; | |
3464 int save_called_emsg = called_emsg; | |
675 | 3465 |
3466 # ifdef FEAT_EVAL | |
771 | 3467 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0); |
675 | 3468 # endif |
771 | 3469 called_emsg = FALSE; |
7 | 3470 build_stl_str_hl(curwin, i_str, sizeof(buf), |
675 | 3471 p_iconstring, use_sandbox, |
681 | 3472 0, 0, NULL, NULL); |
771 | 3473 if (called_emsg) |
3474 set_string_option_direct((char_u *)"iconstring", -1, | |
3475 (char_u *)"", OPT_FREE, SID_ERROR); | |
3476 called_emsg |= save_called_emsg; | |
3477 } | |
7 | 3478 else |
3479 #endif | |
3480 i_str = p_iconstring; | |
3481 } | |
3482 else | |
3483 { | |
3484 if (buf_spname(curbuf) != NULL) | |
3839 | 3485 i_name = buf_spname(curbuf); |
7 | 3486 else /* use file name only in icon */ |
3487 i_name = gettail(curbuf->b_ffname); | |
3488 *i_str = NUL; | |
3489 /* Truncate name at 100 bytes. */ | |
835 | 3490 len = (int)STRLEN(i_name); |
7 | 3491 if (len > 100) |
3492 { | |
3493 len -= 100; | |
3494 #ifdef FEAT_MBYTE | |
3495 if (has_mbyte) | |
3496 len += (*mb_tail_off)(i_name, i_name + len) + 1; | |
3497 #endif | |
3498 i_name += len; | |
3499 } | |
3500 STRCPY(i_str, i_name); | |
3501 trans_characters(i_str, IOSIZE); | |
3502 } | |
3503 } | |
3504 | |
3505 mustset |= ti_change(i_str, &lasticon); | |
3506 | |
3507 if (mustset) | |
3508 resettitle(); | |
3509 } | |
3510 | |
3511 /* | |
3512 * Used for title and icon: Check if "str" differs from "*last". Set "*last" | |
3513 * from "str" if it does. | |
3514 * Return TRUE when "*last" changed. | |
3515 */ | |
3516 static int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3517 ti_change(char_u *str, char_u **last) |
7 | 3518 { |
3519 if ((str == NULL) != (*last == NULL) | |
3520 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0)) | |
3521 { | |
3522 vim_free(*last); | |
3523 if (str == NULL) | |
3524 *last = NULL; | |
3525 else | |
3526 *last = vim_strsave(str); | |
3527 return TRUE; | |
3528 } | |
3529 return FALSE; | |
3530 } | |
3531 | |
3532 /* | |
3533 * Put current window title back (used after calling a shell) | |
3534 */ | |
3535 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3536 resettitle(void) |
7 | 3537 { |
3538 mch_settitle(lasttitle, lasticon); | |
3539 } | |
358 | 3540 |
3541 # if defined(EXITFREE) || defined(PROTO) | |
3542 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3543 free_titles(void) |
358 | 3544 { |
3545 vim_free(lasttitle); | |
3546 vim_free(lasticon); | |
3547 } | |
3548 # endif | |
3549 | |
7 | 3550 #endif /* FEAT_TITLE */ |
3551 | |
686 | 3552 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO) |
7 | 3553 /* |
675 | 3554 * Build a string from the status line items in "fmt". |
7 | 3555 * Return length of string in screen cells. |
3556 * | |
675 | 3557 * Normally works for window "wp", except when working for 'tabline' then it |
3558 * is "curwin". | |
3559 * | |
7 | 3560 * Items are drawn interspersed with the text that surrounds it |
3561 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation | |
3562 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional | |
3563 * | |
3564 * If maxwidth is not zero, the string will be filled at any middle marker | |
3565 * or truncated if too long, fillchar is used for all whitespace. | |
3566 */ | |
3567 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3568 build_stl_str_hl( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3569 win_T *wp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3570 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
|
3571 size_t outlen, /* length of out[] */ |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3572 char_u *fmt, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3573 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
|
3574 int fillchar, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3575 int maxwidth, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
3576 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
|
3577 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */ |
7 | 3578 { |
3579 char_u *p; | |
3580 char_u *s; | |
3581 char_u *t; | |
4333 | 3582 int byteval; |
7 | 3583 #ifdef FEAT_EVAL |
3584 win_T *o_curwin; | |
3585 buf_T *o_curbuf; | |
3586 #endif | |
3587 int empty_line; | |
3588 colnr_T virtcol; | |
3589 long l; | |
3590 long n; | |
3591 int prevchar_isflag; | |
3592 int prevchar_isitem; | |
3593 int itemisflag; | |
3594 int fillable; | |
3595 char_u *str; | |
3596 long num; | |
3597 int width; | |
3598 int itemcnt; | |
3599 int curitem; | |
3600 int groupitem[STL_MAX_ITEM]; | |
3601 int groupdepth; | |
3602 struct stl_item | |
3603 { | |
3604 char_u *start; | |
3605 int minwid; | |
3606 int maxwid; | |
3607 enum | |
3608 { | |
3609 Normal, | |
3610 Empty, | |
3611 Group, | |
3612 Middle, | |
3613 Highlight, | |
681 | 3614 TabPage, |
7 | 3615 Trunc |
3616 } type; | |
3617 } item[STL_MAX_ITEM]; | |
3618 int minwid; | |
3619 int maxwid; | |
3620 int zeropad; | |
3621 char_u base; | |
3622 char_u opt; | |
3623 #define TMPLEN 70 | |
3624 char_u tmp[TMPLEN]; | |
678 | 3625 char_u *usefmt = fmt; |
681 | 3626 struct stl_hlrec *sp; |
678 | 3627 |
3628 #ifdef FEAT_EVAL | |
3629 /* | |
3630 * When the format starts with "%!" then evaluate it as an expression and | |
3631 * use the result as the actual format string. | |
3632 */ | |
3633 if (fmt[0] == '%' && fmt[1] == '!') | |
3634 { | |
3635 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox); | |
3636 if (usefmt == NULL) | |
943 | 3637 usefmt = fmt; |
678 | 3638 } |
3639 #endif | |
7 | 3640 |
3641 if (fillchar == 0) | |
3642 fillchar = ' '; | |
819 | 3643 #ifdef FEAT_MBYTE |
3644 /* Can't handle a multi-byte fill character yet. */ | |
3645 else if (mb_char2len(fillchar) > 1) | |
3646 fillchar = '-'; | |
3647 #endif | |
3648 | |
4333 | 3649 /* Get line & check if empty (cursorpos will show "0-1"). Note that |
3650 * p will become invalid when getting another buffer line. */ | |
3651 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE); | |
3652 empty_line = (*p == NUL); | |
3653 | |
3654 /* Get the byte value now, in case we need it below. This is more | |
3655 * efficient than making a copy of the line. */ | |
3656 if (wp->w_cursor.col > (colnr_T)STRLEN(p)) | |
3657 byteval = 0; | |
3658 else | |
3659 #ifdef FEAT_MBYTE | |
3660 byteval = (*mb_ptr2char)(p + wp->w_cursor.col); | |
3661 #else | |
3662 byteval = p[wp->w_cursor.col]; | |
3663 #endif | |
7 | 3664 |
3665 groupdepth = 0; | |
3666 p = out; | |
3667 curitem = 0; | |
3668 prevchar_isflag = TRUE; | |
3669 prevchar_isitem = FALSE; | |
678 | 3670 for (s = usefmt; *s; ) |
7 | 3671 { |
2704 | 3672 if (curitem == STL_MAX_ITEM) |
3673 { | |
3674 /* There are too many items. Add the error code to the statusline | |
3675 * to give the user a hint about what went wrong. */ | |
3676 if (p + 6 < out + outlen) | |
3677 { | |
3678 mch_memmove(p, " E541", (size_t)5); | |
3679 p += 5; | |
3680 } | |
3681 break; | |
3682 } | |
3683 | |
7 | 3684 if (*s != NUL && *s != '%') |
3685 prevchar_isflag = prevchar_isitem = FALSE; | |
3686 | |
3687 /* | |
3688 * Handle up to the next '%' or the end. | |
3689 */ | |
3690 while (*s != NUL && *s != '%' && p + 1 < out + outlen) | |
3691 *p++ = *s++; | |
3692 if (*s == NUL || p + 1 >= out + outlen) | |
3693 break; | |
3694 | |
3695 /* | |
3696 * Handle one '%' item. | |
3697 */ | |
3698 s++; | |
2694 | 3699 if (*s == NUL) /* ignore trailing % */ |
3700 break; | |
7 | 3701 if (*s == '%') |
3702 { | |
3703 if (p + 1 >= out + outlen) | |
3704 break; | |
3705 *p++ = *s++; | |
3706 prevchar_isflag = prevchar_isitem = FALSE; | |
3707 continue; | |
3708 } | |
3709 if (*s == STL_MIDDLEMARK) | |
3710 { | |
3711 s++; | |
3712 if (groupdepth > 0) | |
3713 continue; | |
3714 item[curitem].type = Middle; | |
3715 item[curitem++].start = p; | |
3716 continue; | |
3717 } | |
3718 if (*s == STL_TRUNCMARK) | |
3719 { | |
3720 s++; | |
3721 item[curitem].type = Trunc; | |
3722 item[curitem++].start = p; | |
3723 continue; | |
3724 } | |
3725 if (*s == ')') | |
3726 { | |
3727 s++; | |
3728 if (groupdepth < 1) | |
3729 continue; | |
3730 groupdepth--; | |
3731 | |
3732 t = item[groupitem[groupdepth]].start; | |
3733 *p = NUL; | |
3734 l = vim_strsize(t); | |
3735 if (curitem > groupitem[groupdepth] + 1 | |
3736 && item[groupitem[groupdepth]].minwid == 0) | |
3737 { | |
3738 /* remove group if all items are empty */ | |
3739 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
|
3740 if (item[n].type == Normal || item[n].type == Highlight) |
7 | 3741 break; |
3742 if (n == curitem) | |
3743 { | |
3744 p = t; | |
3745 l = 0; | |
3746 } | |
3747 } | |
3748 if (l > item[groupitem[groupdepth]].maxwid) | |
3749 { | |
3750 /* truncate, remove n bytes of text at the start */ | |
3751 #ifdef FEAT_MBYTE | |
3752 if (has_mbyte) | |
3753 { | |
3754 /* Find the first character that should be included. */ | |
3755 n = 0; | |
3756 while (l >= item[groupitem[groupdepth]].maxwid) | |
3757 { | |
3758 l -= ptr2cells(t + n); | |
474 | 3759 n += (*mb_ptr2len)(t + n); |
7 | 3760 } |
3761 } | |
3762 else | |
3763 #endif | |
835 | 3764 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1; |
7 | 3765 |
3766 *t = '<'; | |
1869 | 3767 mch_memmove(t + 1, t + n, (size_t)(p - (t + n))); |
7 | 3768 p = p - n + 1; |
3769 #ifdef FEAT_MBYTE | |
3770 /* Fill up space left over by half a double-wide char. */ | |
3771 while (++l < item[groupitem[groupdepth]].minwid) | |
3772 *p++ = fillchar; | |
3773 #endif | |
3774 | |
3775 /* correct the start of the items for the truncation */ | |
3776 for (l = groupitem[groupdepth] + 1; l < curitem; l++) | |
3777 { | |
3778 item[l].start -= n; | |
3779 if (item[l].start < t) | |
3780 item[l].start = t; | |
3781 } | |
3782 } | |
3783 else if (abs(item[groupitem[groupdepth]].minwid) > l) | |
3784 { | |
3785 /* fill */ | |
3786 n = item[groupitem[groupdepth]].minwid; | |
3787 if (n < 0) | |
3788 { | |
3789 /* fill by appending characters */ | |
3790 n = 0 - n; | |
3791 while (l++ < n && p + 1 < out + outlen) | |
3792 *p++ = fillchar; | |
3793 } | |
3794 else | |
3795 { | |
3796 /* fill by inserting characters */ | |
1869 | 3797 mch_memmove(t + n - l, t, (size_t)(p - t)); |
7 | 3798 l = n - l; |
3799 if (p + l >= out + outlen) | |
835 | 3800 l = (long)((out + outlen) - p - 1); |
7 | 3801 p += l; |
3802 for (n = groupitem[groupdepth] + 1; n < curitem; n++) | |
3803 item[n].start += l; | |
3804 for ( ; l > 0; l--) | |
3805 *t++ = fillchar; | |
3806 } | |
3807 } | |
3808 continue; | |
3809 } | |
3810 minwid = 0; | |
3811 maxwid = 9999; | |
3812 zeropad = FALSE; | |
3813 l = 1; | |
3814 if (*s == '0') | |
3815 { | |
3816 s++; | |
3817 zeropad = TRUE; | |
3818 } | |
3819 if (*s == '-') | |
3820 { | |
3821 s++; | |
3822 l = -1; | |
3823 } | |
3824 if (VIM_ISDIGIT(*s)) | |
3825 { | |
3826 minwid = (int)getdigits(&s); | |
3827 if (minwid < 0) /* overflow */ | |
3828 minwid = 0; | |
3829 } | |
678 | 3830 if (*s == STL_USER_HL) |
7 | 3831 { |
3832 item[curitem].type = Highlight; | |
3833 item[curitem].start = p; | |
3834 item[curitem].minwid = minwid > 9 ? 1 : minwid; | |
3835 s++; | |
3836 curitem++; | |
3837 continue; | |
3838 } | |
681 | 3839 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR) |
3840 { | |
3841 if (*s == STL_TABCLOSENR) | |
3842 { | |
3843 if (minwid == 0) | |
3844 { | |
3845 /* %X ends the close label, go back to the previously | |
3846 * define tab label nr. */ | |
3847 for (n = curitem - 1; n >= 0; --n) | |
3848 if (item[n].type == TabPage && item[n].minwid >= 0) | |
3849 { | |
3850 minwid = item[n].minwid; | |
3851 break; | |
3852 } | |
3853 } | |
3854 else | |
3855 /* close nrs are stored as negative values */ | |
3856 minwid = - minwid; | |
3857 } | |
3858 item[curitem].type = TabPage; | |
3859 item[curitem].start = p; | |
3860 item[curitem].minwid = minwid; | |
3861 s++; | |
3862 curitem++; | |
3863 continue; | |
3864 } | |
7 | 3865 if (*s == '.') |
3866 { | |
3867 s++; | |
3868 if (VIM_ISDIGIT(*s)) | |
3869 { | |
3870 maxwid = (int)getdigits(&s); | |
3871 if (maxwid <= 0) /* overflow */ | |
3872 maxwid = 50; | |
3873 } | |
3874 } | |
3875 minwid = (minwid > 50 ? 50 : minwid) * l; | |
3876 if (*s == '(') | |
3877 { | |
3878 groupitem[groupdepth++] = curitem; | |
3879 item[curitem].type = Group; | |
3880 item[curitem].start = p; | |
3881 item[curitem].minwid = minwid; | |
3882 item[curitem].maxwid = maxwid; | |
3883 s++; | |
3884 curitem++; | |
3885 continue; | |
3886 } | |
3887 if (vim_strchr(STL_ALL, *s) == NULL) | |
3888 { | |
3889 s++; | |
3890 continue; | |
3891 } | |
3892 opt = *s++; | |
3893 | |
3894 /* OK - now for the real work */ | |
3895 base = 'D'; | |
3896 itemisflag = FALSE; | |
3897 fillable = TRUE; | |
3898 num = -1; | |
3899 str = NULL; | |
3900 switch (opt) | |
3901 { | |
3902 case STL_FILEPATH: | |
3903 case STL_FULLPATH: | |
3904 case STL_FILENAME: | |
3905 fillable = FALSE; /* don't change ' ' to fillchar */ | |
3906 if (buf_spname(wp->w_buffer) != NULL) | |
3839 | 3907 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1); |
7 | 3908 else |
3909 { | |
3910 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname | |
667 | 3911 : wp->w_buffer->b_fname; |
7 | 3912 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE); |
3913 } | |
3914 trans_characters(NameBuff, MAXPATHL); | |
3915 if (opt != STL_FILENAME) | |
3916 str = NameBuff; | |
3917 else | |
3918 str = gettail(NameBuff); | |
3919 break; | |
3920 | |
3921 case STL_VIM_EXPR: /* '{' */ | |
3922 itemisflag = TRUE; | |
3923 t = p; | |
3924 while (*s != '}' && *s != NUL && p + 1 < out + outlen) | |
3925 *p++ = *s++; | |
3926 if (*s != '}') /* missing '}' or out of space */ | |
3927 break; | |
3928 s++; | |
3929 *p = 0; | |
3930 p = t; | |
3931 | |
3932 #ifdef FEAT_EVAL | |
1869 | 3933 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum); |
7 | 3934 set_internal_string_var((char_u *)"actual_curbuf", tmp); |
3935 | |
3936 o_curbuf = curbuf; | |
3937 o_curwin = curwin; | |
3938 curwin = wp; | |
3939 curbuf = wp->w_buffer; | |
3940 | |
675 | 3941 str = eval_to_string_safe(p, &t, use_sandbox); |
7 | 3942 |
3943 curwin = o_curwin; | |
3944 curbuf = o_curbuf; | |
145 | 3945 do_unlet((char_u *)"g:actual_curbuf", TRUE); |
7 | 3946 |
3947 if (str != NULL && *str != 0) | |
3948 { | |
3949 if (*skipdigits(str) == NUL) | |
3950 { | |
3951 num = atoi((char *)str); | |
3952 vim_free(str); | |
3953 str = NULL; | |
3954 itemisflag = FALSE; | |
3955 } | |
3956 } | |
3957 #endif | |
3958 break; | |
3959 | |
3960 case STL_LINE: | |
3961 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) | |
3962 ? 0L : (long)(wp->w_cursor.lnum); | |
3963 break; | |
3964 | |
3965 case STL_NUMLINES: | |
3966 num = wp->w_buffer->b_ml.ml_line_count; | |
3967 break; | |
3968 | |
3969 case STL_COLUMN: | |
3970 num = !(State & INSERT) && empty_line | |
3971 ? 0 : (int)wp->w_cursor.col + 1; | |
3972 break; | |
3973 | |
3974 case STL_VIRTCOL: | |
3975 case STL_VIRTCOL_ALT: | |
3976 /* In list mode virtcol needs to be recomputed */ | |
3977 virtcol = wp->w_virtcol; | |
3978 if (wp->w_p_list && lcs_tab1 == NUL) | |
3979 { | |
3980 wp->w_p_list = FALSE; | |
3981 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL); | |
3982 wp->w_p_list = TRUE; | |
3983 } | |
3984 ++virtcol; | |
3985 /* Don't display %V if it's the same as %c. */ | |
3986 if (opt == STL_VIRTCOL_ALT | |
3987 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line | |
3988 ? 0 : (int)wp->w_cursor.col + 1))) | |
3989 break; | |
3990 num = (long)virtcol; | |
3991 break; | |
3992 | |
3993 case STL_PERCENTAGE: | |
3994 num = (int)(((long)wp->w_cursor.lnum * 100L) / | |
3995 (long)wp->w_buffer->b_ml.ml_line_count); | |
3996 break; | |
3997 | |
3998 case STL_ALTPERCENT: | |
3999 str = tmp; | |
1869 | 4000 get_rel_pos(wp, str, TMPLEN); |
7 | 4001 break; |
4002 | |
4003 case STL_ARGLISTSTAT: | |
4004 fillable = FALSE; | |
4005 tmp[0] = 0; | |
1869 | 4006 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE)) |
7 | 4007 str = tmp; |
4008 break; | |
4009 | |
4010 case STL_KEYMAP: | |
4011 fillable = FALSE; | |
4012 if (get_keymap_str(wp, tmp, TMPLEN)) | |
4013 str = tmp; | |
4014 break; | |
4015 case STL_PAGENUM: | |
706 | 4016 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE) |
686 | 4017 num = printer_page_num; |
7 | 4018 #else |
4019 num = 0; | |
4020 #endif | |
4021 break; | |
4022 | |
4023 case STL_BUFNO: | |
4024 num = wp->w_buffer->b_fnum; | |
4025 break; | |
4026 | |
4027 case STL_OFFSET_X: | |
4028 base = 'X'; | |
4029 case STL_OFFSET: | |
4030 #ifdef FEAT_BYTEOFF | |
4031 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL); | |
4032 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ? | |
4033 0L : l + 1 + (!(State & INSERT) && empty_line ? | |
4034 0 : (int)wp->w_cursor.col); | |
4035 #endif | |
4036 break; | |
4037 | |
4038 case STL_BYTEVAL_X: | |
4039 base = 'X'; | |
4040 case STL_BYTEVAL: | |
4333 | 4041 num = byteval; |
7 | 4042 if (num == NL) |
4043 num = 0; | |
4044 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC) | |
4045 num = NL; | |
4046 break; | |
4047 | |
4048 case STL_ROFLAG: | |
4049 case STL_ROFLAG_ALT: | |
4050 itemisflag = TRUE; | |
4051 if (wp->w_buffer->b_p_ro) | |
4795
8360a59aa04b
updated for version 7.3.1144
Bram Moolenaar <bram@vim.org>
parents:
4354
diff
changeset
|
4052 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]")); |
7 | 4053 break; |
4054 | |
4055 case STL_HELPFLAG: | |
4056 case STL_HELPFLAG_ALT: | |
4057 itemisflag = TRUE; | |
4058 if (wp->w_buffer->b_help) | |
4059 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP" | |
809 | 4060 : _("[Help]")); |
7 | 4061 break; |
4062 | |
4063 #ifdef FEAT_AUTOCMD | |
4064 case STL_FILETYPE: | |
4065 if (*wp->w_buffer->b_p_ft != NUL | |
4066 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3) | |
4067 { | |
272 | 4068 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]", |
4069 wp->w_buffer->b_p_ft); | |
7 | 4070 str = tmp; |
4071 } | |
4072 break; | |
4073 | |
4074 case STL_FILETYPE_ALT: | |
4075 itemisflag = TRUE; | |
4076 if (*wp->w_buffer->b_p_ft != NUL | |
4077 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2) | |
4078 { | |
272 | 4079 vim_snprintf((char *)tmp, sizeof(tmp), ",%s", |
4080 wp->w_buffer->b_p_ft); | |
7 | 4081 for (t = tmp; *t != 0; t++) |
4082 *t = TOUPPER_LOC(*t); | |
4083 str = tmp; | |
4084 } | |
4085 break; | |
4086 #endif | |
4087 | |
4088 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) | |
4089 case STL_PREVIEWFLAG: | |
4090 case STL_PREVIEWFLAG_ALT: | |
4091 itemisflag = TRUE; | |
4092 if (wp->w_p_pvw) | |
4093 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV" | |
4094 : _("[Preview]")); | |
4095 break; | |
2411
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
4096 |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
4097 case STL_QUICKFIX: |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
4098 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
|
4099 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
|
4100 ? _(msg_loclist) |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
4101 : _(msg_qflist)); |
68e394361ca3
Add "q" item for 'statusline'. Add w:quickfix_title. (Lech Lorens)
Bram Moolenaar <bram@vim.org>
parents:
2403
diff
changeset
|
4102 break; |
7 | 4103 #endif |
4104 | |
4105 case STL_MODIFIED: | |
4106 case STL_MODIFIED_ALT: | |
4107 itemisflag = TRUE; | |
4108 switch ((opt == STL_MODIFIED_ALT) | |
4109 + bufIsChanged(wp->w_buffer) * 2 | |
4110 + (!wp->w_buffer->b_p_ma) * 4) | |
4111 { | |
4112 case 2: str = (char_u *)"[+]"; break; | |
4113 case 3: str = (char_u *)",+"; break; | |
4114 case 4: str = (char_u *)"[-]"; break; | |
4115 case 5: str = (char_u *)",-"; break; | |
4116 case 6: str = (char_u *)"[+-]"; break; | |
4117 case 7: str = (char_u *)",+-"; break; | |
4118 } | |
4119 break; | |
678 | 4120 |
4121 case STL_HIGHLIGHT: | |
4122 t = s; | |
4123 while (*s != '#' && *s != NUL) | |
4124 ++s; | |
4125 if (*s == '#') | |
4126 { | |
4127 item[curitem].type = Highlight; | |
4128 item[curitem].start = p; | |
835 | 4129 item[curitem].minwid = -syn_namen2id(t, (int)(s - t)); |
678 | 4130 curitem++; |
4131 } | |
5407 | 4132 if (*s != NUL) |
4133 ++s; | |
678 | 4134 continue; |
7 | 4135 } |
4136 | |
4137 item[curitem].start = p; | |
4138 item[curitem].type = Normal; | |
4139 if (str != NULL && *str) | |
4140 { | |
4141 t = str; | |
4142 if (itemisflag) | |
4143 { | |
4144 if ((t[0] && t[1]) | |
4145 && ((!prevchar_isitem && *t == ',') | |
4146 || (prevchar_isflag && *t == ' '))) | |
4147 t++; | |
4148 prevchar_isflag = TRUE; | |
4149 } | |
4150 l = vim_strsize(t); | |
4151 if (l > 0) | |
4152 prevchar_isitem = TRUE; | |
4153 if (l > maxwid) | |
4154 { | |
4155 while (l >= maxwid) | |
4156 #ifdef FEAT_MBYTE | |
4157 if (has_mbyte) | |
4158 { | |
4159 l -= ptr2cells(t); | |
474 | 4160 t += (*mb_ptr2len)(t); |
7 | 4161 } |
4162 else | |
4163 #endif | |
4164 l -= byte2cells(*t++); | |
4165 if (p + 1 >= out + outlen) | |
4166 break; | |
4167 *p++ = '<'; | |
4168 } | |
4169 if (minwid > 0) | |
4170 { | |
4171 for (; l < minwid && p + 1 < out + outlen; l++) | |
4172 { | |
4173 /* Don't put a "-" in front of a digit. */ | |
4174 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t)) | |
4175 *p++ = ' '; | |
4176 else | |
4177 *p++ = fillchar; | |
4178 } | |
4179 minwid = 0; | |
4180 } | |
4181 else | |
4182 minwid *= -1; | |
4183 while (*t && p + 1 < out + outlen) | |
4184 { | |
4185 *p++ = *t++; | |
4186 /* Change a space by fillchar, unless fillchar is '-' and a | |
4187 * digit follows. */ | |
4188 if (fillable && p[-1] == ' ' | |
4189 && (!VIM_ISDIGIT(*t) || fillchar != '-')) | |
4190 p[-1] = fillchar; | |
4191 } | |
4192 for (; l < minwid && p + 1 < out + outlen; l++) | |
4193 *p++ = fillchar; | |
4194 } | |
4195 else if (num >= 0) | |
4196 { | |
4197 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16)); | |
4198 char_u nstr[20]; | |
4199 | |
4200 if (p + 20 >= out + outlen) | |
4201 break; /* not sufficient space */ | |
4202 prevchar_isitem = TRUE; | |
4203 t = nstr; | |
4204 if (opt == STL_VIRTCOL_ALT) | |
4205 { | |
4206 *t++ = '-'; | |
4207 minwid--; | |
4208 } | |
4209 *t++ = '%'; | |
4210 if (zeropad) | |
4211 *t++ = '0'; | |
4212 *t++ = '*'; | |
1869 | 4213 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd'); |
7 | 4214 *t = 0; |
4215 | |
4216 for (n = num, l = 1; n >= nbase; n /= nbase) | |
4217 l++; | |
4218 if (opt == STL_VIRTCOL_ALT) | |
4219 l++; | |
4220 if (l > maxwid) | |
4221 { | |
4222 l += 2; | |
4223 n = l - maxwid; | |
4224 while (l-- > maxwid) | |
4225 num /= nbase; | |
4226 *t++ = '>'; | |
4227 *t++ = '%'; | |
4228 *t = t[-3]; | |
4229 *++t = 0; | |
272 | 4230 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr, |
4231 0, num, n); | |
7 | 4232 } |
4233 else | |
272 | 4234 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr, |
4235 minwid, num); | |
7 | 4236 p += STRLEN(p); |
4237 } | |
4238 else | |
4239 item[curitem].type = Empty; | |
4240 | |
4241 if (opt == STL_VIM_EXPR) | |
4242 vim_free(str); | |
4243 | |
4244 if (num >= 0 || (!itemisflag && str && *str)) | |
4245 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */ | |
4246 curitem++; | |
4247 } | |
4248 *p = NUL; | |
4249 itemcnt = curitem; | |
4250 | |
678 | 4251 #ifdef FEAT_EVAL |
4252 if (usefmt != fmt) | |
4253 vim_free(usefmt); | |
4254 #endif | |
4255 | |
7 | 4256 width = vim_strsize(out); |
4257 if (maxwidth > 0 && width > maxwidth) | |
4258 { | |
1736 | 4259 /* Result is too long, must truncate somewhere. */ |
7 | 4260 l = 0; |
4261 if (itemcnt == 0) | |
4262 s = out; | |
4263 else | |
4264 { | |
4265 for ( ; l < itemcnt; l++) | |
4266 if (item[l].type == Trunc) | |
4267 { | |
4268 /* Truncate at %< item. */ | |
4269 s = item[l].start; | |
4270 break; | |
4271 } | |
4272 if (l == itemcnt) | |
4273 { | |
4274 /* No %< item, truncate first item. */ | |
4275 s = item[0].start; | |
4276 l = 0; | |
4277 } | |
4278 } | |
4279 | |
4280 if (width - vim_strsize(s) >= maxwidth) | |
4281 { | |
4282 /* Truncation mark is beyond max length */ | |
4283 #ifdef FEAT_MBYTE | |
4284 if (has_mbyte) | |
4285 { | |
4286 s = out; | |
4287 width = 0; | |
4288 for (;;) | |
4289 { | |
4290 width += ptr2cells(s); | |
4291 if (width >= maxwidth) | |
4292 break; | |
474 | 4293 s += (*mb_ptr2len)(s); |
7 | 4294 } |
4295 /* Fill up for half a double-wide character. */ | |
4296 while (++width < maxwidth) | |
4297 *s++ = fillchar; | |
4298 } | |
4299 else | |
4300 #endif | |
4301 s = out + maxwidth - 1; | |
4302 for (l = 0; l < itemcnt; l++) | |
4303 if (item[l].start > s) | |
4304 break; | |
4305 itemcnt = l; | |
4306 *s++ = '>'; | |
4307 *s = 0; | |
4308 } | |
4309 else | |
4310 { | |
4311 #ifdef FEAT_MBYTE | |
4312 if (has_mbyte) | |
4313 { | |
4314 n = 0; | |
4315 while (width >= maxwidth) | |
4316 { | |
4317 width -= ptr2cells(s + n); | |
474 | 4318 n += (*mb_ptr2len)(s + n); |
7 | 4319 } |
4320 } | |
4321 else | |
4322 #endif | |
4323 n = width - maxwidth + 1; | |
4324 p = s + n; | |
1618 | 4325 STRMOVE(s + 1, p); |
7 | 4326 *s = '<'; |
4327 | |
4328 /* Fill up for half a double-wide character. */ | |
4329 while (++width < maxwidth) | |
4330 { | |
4331 s = s + STRLEN(s); | |
4332 *s++ = fillchar; | |
4333 *s = NUL; | |
4334 } | |
4335 | |
4336 --n; /* count the '<' */ | |
4337 for (; l < itemcnt; l++) | |
4338 { | |
4339 if (item[l].start - n >= s) | |
4340 item[l].start -= n; | |
4341 else | |
4342 item[l].start = s; | |
4343 } | |
4344 } | |
4345 width = maxwidth; | |
4346 } | |
4347 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen) | |
4348 { | |
4349 /* Apply STL_MIDDLE if any */ | |
4350 for (l = 0; l < itemcnt; l++) | |
4351 if (item[l].type == Middle) | |
4352 break; | |
4353 if (l < itemcnt) | |
4354 { | |
4355 p = item[l].start + maxwidth - width; | |
1618 | 4356 STRMOVE(p, item[l].start); |
7 | 4357 for (s = item[l].start; s < p; s++) |
4358 *s = fillchar; | |
4359 for (l++; l < itemcnt; l++) | |
4360 item[l].start += maxwidth - width; | |
4361 width = maxwidth; | |
4362 } | |
4363 } | |
4364 | |
681 | 4365 /* Store the info about highlighting. */ |
4366 if (hltab != NULL) | |
7 | 4367 { |
681 | 4368 sp = hltab; |
7 | 4369 for (l = 0; l < itemcnt; l++) |
4370 { | |
4371 if (item[l].type == Highlight) | |
4372 { | |
681 | 4373 sp->start = item[l].start; |
4374 sp->userhl = item[l].minwid; | |
4375 sp++; | |
7 | 4376 } |
4377 } | |
681 | 4378 sp->start = NULL; |
4379 sp->userhl = 0; | |
4380 } | |
4381 | |
4382 /* Store the info about tab pages labels. */ | |
4383 if (tabtab != NULL) | |
4384 { | |
4385 sp = tabtab; | |
4386 for (l = 0; l < itemcnt; l++) | |
4387 { | |
4388 if (item[l].type == TabPage) | |
4389 { | |
4390 sp->start = item[l].start; | |
4391 sp->userhl = item[l].minwid; | |
4392 sp++; | |
4393 } | |
4394 } | |
4395 sp->start = NULL; | |
4396 sp->userhl = 0; | |
7 | 4397 } |
4398 | |
4399 return width; | |
4400 } | |
4401 #endif /* FEAT_STL_OPT */ | |
4402 | |
686 | 4403 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \ |
4404 || defined(FEAT_GUI_TABLINE) || defined(PROTO) | |
7 | 4405 /* |
1869 | 4406 * Get relative cursor position in window into "buf[buflen]", in the form 99%, |
4407 * using "Top", "Bot" or "All" when appropriate. | |
7 | 4408 */ |
4409 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4410 get_rel_pos( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4411 win_T *wp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4412 char_u *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4413 int buflen) |
7 | 4414 { |
4415 long above; /* number of lines above window */ | |
4416 long below; /* number of lines below window */ | |
4417 | |
6466 | 4418 if (buflen < 3) /* need at least 3 chars for writing */ |
4419 return; | |
7 | 4420 above = wp->w_topline - 1; |
4421 #ifdef FEAT_DIFF | |
4422 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill; | |
6975 | 4423 if (wp->w_topline == 1 && wp->w_topfill >= 1) |
4424 above = 0; /* All buffer lines are displayed and there is an | |
4425 * indication of filler lines, that can be considered | |
4426 * seeing all lines. */ | |
7 | 4427 #endif |
4428 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1; | |
4429 if (below <= 0) | |
1869 | 4430 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")), |
4431 (size_t)(buflen - 1)); | |
7 | 4432 else if (above <= 0) |
1869 | 4433 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1)); |
7 | 4434 else |
1869 | 4435 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L |
7 | 4436 ? (int)(above / ((above + below) / 100L)) |
4437 : (int)(above * 100L / (above + below))); | |
4438 } | |
4439 #endif | |
4440 | |
4441 /* | |
1869 | 4442 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file. |
7 | 4443 * Return TRUE if it was appended. |
4444 */ | |
1869 | 4445 static int |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4446 append_arg_number( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4447 win_T *wp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4448 char_u *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4449 int buflen, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4450 int add_file) /* Add "file" before the arg number */ |
7 | 4451 { |
4452 char_u *p; | |
4453 | |
4454 if (ARGCOUNT <= 1) /* nothing to do */ | |
4455 return FALSE; | |
4456 | |
1869 | 4457 p = buf + STRLEN(buf); /* go to the end of the buffer */ |
4458 if (p - buf + 35 >= buflen) /* getting too long */ | |
7 | 4459 return FALSE; |
4460 *p++ = ' '; | |
4461 *p++ = '('; | |
4462 if (add_file) | |
4463 { | |
4464 STRCPY(p, "file "); | |
4465 p += 5; | |
4466 } | |
1869 | 4467 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)), |
4468 wp->w_arg_idx_invalid ? "(%d) of %d)" | |
7 | 4469 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT); |
4470 return TRUE; | |
4471 } | |
4472 | |
4473 /* | |
4474 * If fname is not a full path, make it a full path. | |
4475 * Returns pointer to allocated memory (NULL for failure). | |
4476 */ | |
4477 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4478 fix_fname(char_u *fname) |
7 | 4479 { |
4480 /* | |
4481 * Force expanding the path always for Unix, because symbolic links may | |
4482 * mess up the full path name, even though it starts with a '/'. | |
4483 * Also expand when there is ".." in the file name, try to remove it, | |
4484 * because "c:/src/../README" is equal to "c:/README". | |
1420 | 4485 * Similarly "c:/src//file" is equal to "c:/src/file". |
7 | 4486 * For MS-Windows also expand names like "longna~1" to "longname". |
4487 */ | |
1082 | 4488 #ifdef UNIX |
7 | 4489 return FullName_save(fname, TRUE); |
4490 #else | |
1420 | 4491 if (!vim_isAbsName(fname) |
4492 || strstr((char *)fname, "..") != NULL | |
4493 || strstr((char *)fname, "//") != NULL | |
4494 # ifdef BACKSLASH_IN_FILENAME | |
4495 || strstr((char *)fname, "\\\\") != NULL | |
4496 # endif | |
8212
05b88224cea1
commit https://github.com/vim/vim/commit/48e330aff911be1c798c88a973af6437a8141fce
Christian Brabandt <cb@256bit.org>
parents:
7817
diff
changeset
|
4497 # if defined(MSWIN) |
7 | 4498 || vim_strchr(fname, '~') != NULL |
1420 | 4499 # endif |
7 | 4500 ) |
4501 return FullName_save(fname, FALSE); | |
4502 | |
4503 fname = vim_strsave(fname); | |
4504 | |
1420 | 4505 # ifdef USE_FNAME_CASE |
4506 # ifdef USE_LONG_FNAME | |
7 | 4507 if (USE_LONG_FNAME) |
1420 | 4508 # endif |
7 | 4509 { |
4510 if (fname != NULL) | |
4511 fname_case(fname, 0); /* set correct case for file name */ | |
4512 } | |
1420 | 4513 # endif |
7 | 4514 |
4515 return fname; | |
4516 #endif | |
4517 } | |
4518 | |
4519 /* | |
4520 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL. | |
4521 * "ffname" becomes a pointer to allocated memory (or NULL). | |
4522 */ | |
4523 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4524 fname_expand( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4525 buf_T *buf UNUSED, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4526 char_u **ffname, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4527 char_u **sfname) |
7 | 4528 { |
4529 if (*ffname == NULL) /* if no file name given, nothing to do */ | |
4530 return; | |
4531 if (*sfname == NULL) /* if no short file name given, use ffname */ | |
4532 *sfname = *ffname; | |
4533 *ffname = fix_fname(*ffname); /* expand to full path */ | |
4534 | |
4535 #ifdef FEAT_SHORTCUT | |
4536 if (!buf->b_p_bin) | |
4537 { | |
844 | 4538 char_u *rfname; |
7 | 4539 |
4540 /* If the file name is a shortcut file, use the file it links to. */ | |
4541 rfname = mch_resolve_shortcut(*ffname); | |
844 | 4542 if (rfname != NULL) |
7 | 4543 { |
4544 vim_free(*ffname); | |
4545 *ffname = rfname; | |
4546 *sfname = rfname; | |
4547 } | |
4548 } | |
4549 #endif | |
4550 } | |
4551 | |
4552 /* | |
4553 * Get the file name for an argument list entry. | |
4554 */ | |
4555 char_u * | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4556 alist_name(aentry_T *aep) |
7 | 4557 { |
4558 buf_T *bp; | |
4559 | |
4560 /* Use the name from the associated buffer if it exists. */ | |
4561 bp = buflist_findnr(aep->ae_fnum); | |
1036 | 4562 if (bp == NULL || bp->b_fname == NULL) |
7 | 4563 return aep->ae_fname; |
4564 return bp->b_fname; | |
4565 } | |
4566 | |
4567 #if defined(FEAT_WINDOWS) || defined(PROTO) | |
4568 /* | |
4569 * do_arg_all(): Open up to 'count' windows, one for each argument. | |
4570 */ | |
4571 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4572 do_arg_all( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4573 int count, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4574 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
|
4575 int keep_tabs) /* keep current tabs, for ":tab drop file" */ |
7 | 4576 { |
4577 int i; | |
4578 win_T *wp, *wpnext; | |
3380 | 4579 char_u *opened; /* Array of weight for which args are open: |
4580 * 0: not opened | |
4581 * 1: opened in other tab | |
4582 * 2: opened in curtab | |
4583 * 3: opened in curtab and curwin | |
4584 */ | |
1411 | 4585 int opened_len; /* length of opened[] */ |
7 | 4586 int use_firstwin = FALSE; /* use first window for arglist */ |
4587 int split_ret = OK; | |
4588 int p_ea_save; | |
4589 alist_T *alist; /* argument list to be used */ | |
4590 buf_T *buf; | |
698 | 4591 tabpage_T *tpnext; |
4592 int had_tab = cmdmod.tab; | |
3380 | 4593 win_T *old_curwin, *last_curwin; |
4594 tabpage_T *old_curtab, *last_curtab; | |
726 | 4595 win_T *new_curwin = NULL; |
4596 tabpage_T *new_curtab = NULL; | |
7 | 4597 |
4598 if (ARGCOUNT <= 0) | |
4599 { | |
4600 /* Don't give an error message. We don't want it when the ":all" | |
4601 * command is in the .vimrc. */ | |
4602 return; | |
4603 } | |
4604 setpcmark(); | |
4605 | |
4606 opened_len = ARGCOUNT; | |
4607 opened = alloc_clear((unsigned)opened_len); | |
4608 if (opened == NULL) | |
4609 return; | |
4610 | |
3380 | 4611 /* Autocommands may do anything to the argument list. Make sure it's not |
4612 * freed while we are working here by "locking" it. We still have to | |
4613 * watch out for its size to be changed. */ | |
4614 alist = curwin->w_alist; | |
4615 ++alist->al_refcount; | |
4616 | |
4617 old_curwin = curwin; | |
4618 old_curtab = curtab; | |
4619 | |
8643
24b43dd167eb
commit https://github.com/vim/vim/commit/44a2f923c00f1384c9ecde12fb5b4711bc20702e
Christian Brabandt <cb@256bit.org>
parents:
8560
diff
changeset
|
4620 # ifdef FEAT_GUI |
7 | 4621 need_mouse_correct = TRUE; |
8643
24b43dd167eb
commit https://github.com/vim/vim/commit/44a2f923c00f1384c9ecde12fb5b4711bc20702e
Christian Brabandt <cb@256bit.org>
parents:
8560
diff
changeset
|
4622 # endif |
7 | 4623 |
4624 /* | |
4625 * Try closing all windows that are not in the argument list. | |
4626 * Also close windows that are not full width; | |
4627 * When 'hidden' or "forceit" set the buffer becomes hidden. | |
4628 * Windows that have a changed buffer and can't be hidden won't be closed. | |
698 | 4629 * When the ":tab" modifier was used do this for all tab pages. |
7 | 4630 */ |
698 | 4631 if (had_tab > 0) |
4354 | 4632 goto_tabpage_tp(first_tabpage, TRUE, TRUE); |
698 | 4633 for (;;) |
7 | 4634 { |
698 | 4635 tpnext = curtab->tp_next; |
4636 for (wp = firstwin; wp != NULL; wp = wpnext) | |
7 | 4637 { |
698 | 4638 wpnext = wp->w_next; |
4639 buf = wp->w_buffer; | |
4640 if (buf->b_ffname == NULL | |
3380 | 4641 || (!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
|
4642 || wp->w_width != Columns) |
3380 | 4643 i = opened_len; |
698 | 4644 else |
7 | 4645 { |
698 | 4646 /* check if the buffer in this window is in the arglist */ |
3380 | 4647 for (i = 0; i < opened_len; ++i) |
7 | 4648 { |
3380 | 4649 if (i < alist->al_ga.ga_len |
4650 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum | |
4651 || fullpathcmp(alist_name(&AARGLIST(alist)[i]), | |
4652 buf->b_ffname, TRUE) & FPC_SAME)) | |
7 | 4653 { |
3380 | 4654 int weight = 1; |
4655 | |
4656 if (old_curtab == curtab) | |
726 | 4657 { |
3380 | 4658 ++weight; |
4659 if (old_curwin == wp) | |
4660 ++weight; | |
4661 } | |
4662 | |
4663 if (weight > (int)opened[i]) | |
4664 { | |
4665 opened[i] = (char_u)weight; | |
726 | 4666 if (i == 0) |
4667 { | |
3380 | 4668 if (new_curwin != NULL) |
4669 new_curwin->w_arg_idx = opened_len; | |
726 | 4670 new_curwin = wp; |
4671 new_curtab = curtab; | |
4672 } | |
4673 } | |
3380 | 4674 else if (keep_tabs) |
4675 i = opened_len; | |
4676 | |
4677 if (wp->w_alist != alist) | |
698 | 4678 { |
4679 /* Use the current argument list for all windows | |
4680 * containing a file from it. */ | |
4681 alist_unlink(wp->w_alist); | |
3380 | 4682 wp->w_alist = alist; |
698 | 4683 ++wp->w_alist->al_refcount; |
4684 } | |
4685 break; | |
7 | 4686 } |
698 | 4687 } |
4688 } | |
4689 wp->w_arg_idx = i; | |
4690 | |
3380 | 4691 if (i == opened_len && !keep_tabs)/* close this window */ |
698 | 4692 { |
4693 if (P_HID(buf) || forceit || buf->b_nwindows > 1 | |
726 | 4694 || !bufIsChanged(buf)) |
698 | 4695 { |
4696 /* If the buffer was changed, and we would like to hide it, | |
4697 * try autowriting. */ | |
726 | 4698 if (!P_HID(buf) && buf->b_nwindows <= 1 |
4699 && bufIsChanged(buf)) | |
698 | 4700 { |
4701 (void)autowrite(buf, FALSE); | |
4702 #ifdef FEAT_AUTOCMD | |
4703 /* check if autocommands removed the window */ | |
4704 if (!win_valid(wp) || !buf_valid(buf)) | |
4705 { | |
4706 wpnext = firstwin; /* start all over... */ | |
4707 continue; | |
4708 } | |
4709 #endif | |
4710 } | |
4711 #ifdef FEAT_WINDOWS | |
726 | 4712 /* don't close last window */ |
3380 | 4713 if (firstwin == lastwin |
4714 && (first_tabpage->tp_next == NULL || !had_tab)) | |
698 | 4715 #endif |
4716 use_firstwin = TRUE; | |
4717 #ifdef FEAT_WINDOWS | |
4718 else | |
4719 { | |
4720 win_close(wp, !P_HID(buf) && !bufIsChanged(buf)); | |
4721 # ifdef FEAT_AUTOCMD | |
4722 /* check if autocommands removed the next window */ | |
4723 if (!win_valid(wpnext)) | |
4724 wpnext = firstwin; /* start all over... */ | |
4725 # endif | |
4726 } | |
4727 #endif | |
7 | 4728 } |
4729 } | |
4730 } | |
698 | 4731 |
4732 /* Without the ":tab" modifier only do the current tab page. */ | |
4733 if (had_tab == 0 || tpnext == NULL) | |
4734 break; | |
4735 | |
7 | 4736 # ifdef FEAT_AUTOCMD |
698 | 4737 /* check if autocommands removed the next tab page */ |
4738 if (!valid_tabpage(tpnext)) | |
4739 tpnext = first_tabpage; /* start all over...*/ | |
7 | 4740 # endif |
4354 | 4741 goto_tabpage_tp(tpnext, TRUE, TRUE); |
7 | 4742 } |
4743 | |
4744 /* | |
4745 * Open a window for files in the argument list that don't have one. | |
4746 * ARGCOUNT may change while doing this, because of autocommands. | |
4747 */ | |
3380 | 4748 if (count > opened_len || count <= 0) |
4749 count = opened_len; | |
7 | 4750 |
4751 #ifdef FEAT_AUTOCMD | |
4752 /* Don't execute Win/Buf Enter/Leave autocommands here. */ | |
4753 ++autocmd_no_enter; | |
4754 ++autocmd_no_leave; | |
4755 #endif | |
3380 | 4756 last_curwin = curwin; |
4757 last_curtab = curtab; | |
7 | 4758 win_enter(lastwin, FALSE); |
819 | 4759 #ifdef FEAT_WINDOWS |
4760 /* ":drop all" should re-use an empty window to avoid "--remote-tab" | |
4761 * leaving an empty tab page when executed locally. */ | |
4762 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1 | |
4763 && curbuf->b_ffname == NULL && !curbuf->b_changed) | |
4764 use_firstwin = TRUE; | |
4765 #endif | |
4766 | |
3380 | 4767 for (i = 0; i < count && i < opened_len && !got_int; ++i) |
7 | 4768 { |
4769 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1) | |
4770 arg_had_last = TRUE; | |
3380 | 4771 if (opened[i] > 0) |
7 | 4772 { |
4773 /* Move the already present window to below the current window */ | |
4774 if (curwin->w_arg_idx != i) | |
4775 { | |
4776 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next) | |
4777 { | |
4778 if (wpnext->w_arg_idx == i) | |
4779 { | |
3380 | 4780 if (keep_tabs) |
4781 { | |
4782 new_curwin = wpnext; | |
4783 new_curtab = curtab; | |
4784 } | |
4785 else | |
4786 win_move_after(wpnext, curwin); | |
7 | 4787 break; |
4788 } | |
4789 } | |
4790 } | |
4791 } | |
4792 else if (split_ret == OK) | |
4793 { | |
4794 if (!use_firstwin) /* split current window */ | |
4795 { | |
4796 p_ea_save = p_ea; | |
4797 p_ea = TRUE; /* use space from all windows */ | |
4798 split_ret = win_split(0, WSP_ROOM | WSP_BELOW); | |
4799 p_ea = p_ea_save; | |
4800 if (split_ret == FAIL) | |
4801 continue; | |
4802 } | |
4803 #ifdef FEAT_AUTOCMD | |
4804 else /* first window: do autocmd for leaving this buffer */ | |
4805 --autocmd_no_leave; | |
4806 #endif | |
4807 | |
4808 /* | |
726 | 4809 * edit file "i" |
7 | 4810 */ |
4811 curwin->w_arg_idx = i; | |
726 | 4812 if (i == 0) |
4813 { | |
4814 new_curwin = curwin; | |
4815 new_curtab = curtab; | |
4816 } | |
7 | 4817 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL, |
4818 ECMD_ONE, | |
4819 ((P_HID(curwin->w_buffer) | |
4820 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0) | |
1743 | 4821 + ECMD_OLDBUF, curwin); |
7 | 4822 #ifdef FEAT_AUTOCMD |
4823 if (use_firstwin) | |
4824 ++autocmd_no_leave; | |
4825 #endif | |
4826 use_firstwin = FALSE; | |
4827 } | |
4828 ui_breakcheck(); | |
698 | 4829 |
4830 /* When ":tab" was used open a new tab for a new window repeatedly. */ | |
4831 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm) | |
4832 cmdmod.tab = 9999; | |
7 | 4833 } |
4834 | |
4835 /* Remove the "lock" on the argument list. */ | |
4836 alist_unlink(alist); | |
4837 | |
4838 #ifdef FEAT_AUTOCMD | |
4839 --autocmd_no_enter; | |
4840 #endif | |
3380 | 4841 /* restore last referenced tabpage's curwin */ |
4842 if (last_curtab != new_curtab) | |
4843 { | |
4844 if (valid_tabpage(last_curtab)) | |
4354 | 4845 goto_tabpage_tp(last_curtab, TRUE, TRUE); |
3380 | 4846 if (win_valid(last_curwin)) |
4847 win_enter(last_curwin, FALSE); | |
4848 } | |
726 | 4849 /* to window with first arg */ |
4850 if (valid_tabpage(new_curtab)) | |
4354 | 4851 goto_tabpage_tp(new_curtab, TRUE, TRUE); |
726 | 4852 if (win_valid(new_curwin)) |
4853 win_enter(new_curwin, FALSE); | |
4854 | |
7 | 4855 #ifdef FEAT_AUTOCMD |
4856 --autocmd_no_leave; | |
4857 #endif | |
4858 vim_free(opened); | |
4859 } | |
4860 | |
4861 # if defined(FEAT_LISTCMDS) || defined(PROTO) | |
4862 /* | |
4863 * Open a window for a number of buffers. | |
4864 */ | |
4865 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
4866 ex_buffer_all(exarg_T *eap) |
7 | 4867 { |
4868 buf_T *buf; | |
4869 win_T *wp, *wpnext; | |
4870 int split_ret = OK; | |
4871 int p_ea_save; | |
4872 int open_wins = 0; | |
4873 int r; | |
4874 int count; /* Maximum number of windows to open. */ | |
4875 int all; /* When TRUE also load inactive buffers. */ | |
698 | 4876 #ifdef FEAT_WINDOWS |
4877 int had_tab = cmdmod.tab; | |
4878 tabpage_T *tpnext; | |
4879 #endif | |
7 | 4880 |
4881 if (eap->addr_count == 0) /* make as many windows as possible */ | |
4882 count = 9999; | |
4883 else | |
4884 count = eap->line2; /* make as many windows as specified */ | |
4885 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide) | |
4886 all = FALSE; | |
4887 else | |
4888 all = TRUE; | |
4889 | |
4890 setpcmark(); | |
4891 | |
4892 #ifdef FEAT_GUI | |
4893 need_mouse_correct = TRUE; | |
4894 #endif | |
4895 | |
4896 /* | |
4897 * Close superfluous windows (two windows for the same buffer). | |
4898 * Also close windows that are not full-width. | |
4899 */ | |
698 | 4900 #ifdef FEAT_WINDOWS |
4901 if (had_tab > 0) | |
4354 | 4902 goto_tabpage_tp(first_tabpage, TRUE, TRUE); |
698 | 4903 for (;;) |
7 | 4904 { |
698 | 4905 #endif |
4906 tpnext = curtab->tp_next; | |
4907 for (wp = firstwin; wp != NULL; wp = wpnext) | |
7 | 4908 { |
698 | 4909 wpnext = wp->w_next; |
706 | 4910 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
|
4911 #ifdef FEAT_WINDOWS |
698 | 4912 || ((cmdmod.split & WSP_VERT) |
4913 ? wp->w_height + wp->w_status_height < Rows - p_ch | |
706 | 4914 - tabline_height() |
698 | 4915 : wp->w_width != Columns) |
701 | 4916 || (had_tab > 0 && wp != firstwin) |
4917 #endif | |
3570 | 4918 ) && firstwin != lastwin |
4919 #ifdef FEAT_AUTOCMD | |
4920 && !(wp->w_closing || wp->w_buffer->b_closing) | |
4921 #endif | |
4922 ) | |
698 | 4923 { |
4924 win_close(wp, FALSE); | |
7 | 4925 #ifdef FEAT_AUTOCMD |
698 | 4926 wpnext = firstwin; /* just in case an autocommand does |
4927 something strange with windows */ | |
701 | 4928 tpnext = first_tabpage; /* start all over...*/ |
698 | 4929 open_wins = 0; |
4930 #endif | |
4931 } | |
4932 else | |
4933 ++open_wins; | |
7 | 4934 } |
698 | 4935 |
4936 #ifdef FEAT_WINDOWS | |
4937 /* Without the ":tab" modifier only do the current tab page. */ | |
4938 if (had_tab == 0 || tpnext == NULL) | |
4939 break; | |
4354 | 4940 goto_tabpage_tp(tpnext, TRUE, TRUE); |
7 | 4941 } |
698 | 4942 #endif |
7 | 4943 |
4944 /* | |
4945 * Go through the buffer list. When a buffer doesn't have a window yet, | |
4946 * open one. Otherwise move the window to the right position. | |
4947 * Watch out for autocommands that delete buffers or windows! | |
4948 */ | |
4949 #ifdef FEAT_AUTOCMD | |
4950 /* Don't execute Win/Buf Enter/Leave autocommands here. */ | |
4951 ++autocmd_no_enter; | |
4952 #endif | |
4953 win_enter(lastwin, FALSE); | |
4954 #ifdef FEAT_AUTOCMD | |
4955 ++autocmd_no_leave; | |
4956 #endif | |
4957 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next) | |
4958 { | |
4959 /* Check if this buffer needs a window */ | |
4960 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl) | |
4961 continue; | |
4962 | |
701 | 4963 #ifdef FEAT_WINDOWS |
4964 if (had_tab != 0) | |
4965 { | |
4966 /* With the ":tab" modifier don't move the window. */ | |
4967 if (buf->b_nwindows > 0) | |
4968 wp = lastwin; /* buffer has a window, skip it */ | |
4969 else | |
4970 wp = NULL; | |
4971 } | |
4972 else | |
4973 #endif | |
4974 { | |
4975 /* Check if this buffer already has a window */ | |
4976 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
4977 if (wp->w_buffer == buf) | |
4978 break; | |
4979 /* If the buffer already has a window, move it */ | |
4980 if (wp != NULL) | |
4981 win_move_after(wp, curwin); | |
4982 } | |
4983 | |
4984 if (wp == NULL && split_ret == OK) | |
7 | 4985 { |
4986 /* Split the window and put the buffer in it */ | |
4987 p_ea_save = p_ea; | |
4988 p_ea = TRUE; /* use space from all windows */ | |
4989 split_ret = win_split(0, WSP_ROOM | WSP_BELOW); | |
4990 ++open_wins; | |
4991 p_ea = p_ea_save; | |
4992 if (split_ret == FAIL) | |
4993 continue; | |
4994 | |
4995 /* Open the buffer in this window. */ | |
576 | 4996 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 4997 swap_exists_action = SEA_DIALOG; |
4998 #endif | |
4999 set_curbuf(buf, DOBUF_GOTO); | |
5000 #ifdef FEAT_AUTOCMD | |
5001 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */ | |
20 | 5002 { |
576 | 5003 #if defined(HAS_SWAP_EXISTS_ACTION) |
20 | 5004 swap_exists_action = SEA_NONE; |
7 | 5005 # endif |
5006 break; | |
5007 } | |
5008 #endif | |
576 | 5009 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 5010 if (swap_exists_action == SEA_QUIT) |
5011 { | |
24 | 5012 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
5013 cleanup_T cs; | |
5014 | |
5015 /* Reset the error/interrupt/exception state here so that | |
5016 * aborting() returns FALSE when closing a window. */ | |
5017 enter_cleanup(&cs); | |
5018 # endif | |
5019 | |
5020 /* User selected Quit at ATTENTION prompt; close this window. */ | |
7 | 5021 win_close(curwin, TRUE); |
5022 --open_wins; | |
5023 swap_exists_action = SEA_NONE; | |
602 | 5024 swap_exists_did_quit = TRUE; |
24 | 5025 |
5026 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
5027 /* Restore the error/interrupt/exception state if not | |
5028 * discarded by a new aborting error, interrupt, or uncaught | |
5029 * exception. */ | |
5030 leave_cleanup(&cs); | |
5031 # endif | |
7 | 5032 } |
5033 else | |
5034 handle_swap_exists(NULL); | |
5035 #endif | |
5036 } | |
5037 | |
5038 ui_breakcheck(); | |
5039 if (got_int) | |
5040 { | |
5041 (void)vgetc(); /* only break the file loading, not the rest */ | |
5042 break; | |
5043 } | |
20 | 5044 #ifdef FEAT_EVAL |
5045 /* Autocommands deleted the buffer or aborted script processing!!! */ | |
5046 if (aborting()) | |
5047 break; | |
5048 #endif | |
698 | 5049 #ifdef FEAT_WINDOWS |
5050 /* When ":tab" was used open a new tab for a new window repeatedly. */ | |
5051 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm) | |
5052 cmdmod.tab = 9999; | |
5053 #endif | |
7 | 5054 } |
5055 #ifdef FEAT_AUTOCMD | |
5056 --autocmd_no_enter; | |
5057 #endif | |
5058 win_enter(firstwin, FALSE); /* back to first window */ | |
5059 #ifdef FEAT_AUTOCMD | |
5060 --autocmd_no_leave; | |
5061 #endif | |
5062 | |
5063 /* | |
5064 * Close superfluous windows. | |
5065 */ | |
5066 for (wp = lastwin; open_wins > count; ) | |
5067 { | |
5068 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer) | |
5069 || autowrite(wp->w_buffer, FALSE) == OK); | |
5070 #ifdef FEAT_AUTOCMD | |
5071 if (!win_valid(wp)) | |
5072 { | |
5073 /* BufWrite Autocommands made the window invalid, start over */ | |
5074 wp = lastwin; | |
5075 } | |
5076 else | |
5077 #endif | |
5078 if (r) | |
5079 { | |
5080 win_close(wp, !P_HID(wp->w_buffer)); | |
5081 --open_wins; | |
5082 wp = lastwin; | |
5083 } | |
5084 else | |
5085 { | |
5086 wp = wp->w_prev; | |
5087 if (wp == NULL) | |
5088 break; | |
5089 } | |
5090 } | |
5091 } | |
5092 # endif /* FEAT_LISTCMDS */ | |
5093 | |
5094 #endif /* FEAT_WINDOWS */ | |
5095 | |
7799
af3c41a3c53f
commit https://github.com/vim/vim/commit/f28dbcea371b3a35727d91afc90fb90e0527d78a
Christian Brabandt <cb@256bit.org>
parents:
7687
diff
changeset
|
5096 static int chk_modeline(linenr_T, int); |
717 | 5097 |
7 | 5098 /* |
5099 * do_modelines() - process mode lines for the current file | |
5100 * | |
717 | 5101 * "flags" can be: |
5102 * OPT_WINONLY only set options local to window | |
5103 * OPT_NOWIN don't set options local to window | |
5104 * | |
7 | 5105 * Returns immediately if the "ml" option isn't set. |
5106 */ | |
5107 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5108 do_modelines(int flags) |
7 | 5109 { |
23 | 5110 linenr_T lnum; |
5111 int nmlines; | |
5112 static int entered = 0; | |
7 | 5113 |
5114 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0) | |
5115 return; | |
5116 | |
5117 /* Disallow recursive entry here. Can happen when executing a modeline | |
5118 * triggers an autocommand, which reloads modelines with a ":do". */ | |
5119 if (entered) | |
5120 return; | |
5121 | |
5122 ++entered; | |
5123 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines; | |
5124 ++lnum) | |
717 | 5125 if (chk_modeline(lnum, flags) == FAIL) |
7 | 5126 nmlines = 0; |
5127 | |
5128 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines | |
5129 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum) | |
717 | 5130 if (chk_modeline(lnum, flags) == FAIL) |
7 | 5131 nmlines = 0; |
5132 --entered; | |
5133 } | |
5134 | |
5135 #include "version.h" /* for version number */ | |
5136 | |
5137 /* | |
5138 * chk_modeline() - check a single line for a mode string | |
5139 * Return FAIL if an error encountered. | |
5140 */ | |
5141 static int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5142 chk_modeline( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5143 linenr_T lnum, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5144 int flags) /* Same as for do_modelines(). */ |
7 | 5145 { |
5146 char_u *s; | |
5147 char_u *e; | |
5148 char_u *linecopy; /* local copy of any modeline found */ | |
5149 int prev; | |
5150 int vers; | |
5151 int end; | |
5152 int retval = OK; | |
5153 char_u *save_sourcing_name; | |
5154 linenr_T save_sourcing_lnum; | |
5155 #ifdef FEAT_EVAL | |
5156 scid_T save_SID; | |
5157 #endif | |
5158 | |
5159 prev = -1; | |
5160 for (s = ml_get(lnum); *s != NUL; ++s) | |
5161 { | |
5162 if (prev == -1 || vim_isspace(prev)) | |
5163 { | |
5164 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0) | |
5165 || STRNCMP(s, "vi:", (size_t)3) == 0) | |
5166 break; | |
5010
b614332f7df2
updated for version 7.3.1249
Bram Moolenaar <bram@vim.org>
parents:
4936
diff
changeset
|
5167 /* Accept both "vim" and "Vim". */ |
b614332f7df2
updated for version 7.3.1249
Bram Moolenaar <bram@vim.org>
parents:
4936
diff
changeset
|
5168 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm') |
7 | 5169 { |
5170 if (s[3] == '<' || s[3] == '=' || s[3] == '>') | |
5171 e = s + 4; | |
5172 else | |
5173 e = s + 3; | |
5174 vers = getdigits(&e); | |
5175 if (*e == ':' | |
5043
53c1b30632df
updated for version 7.3.1265
Bram Moolenaar <bram@vim.org>
parents:
5010
diff
changeset
|
5176 && (s[0] != 'V' |
53c1b30632df
updated for version 7.3.1265
Bram Moolenaar <bram@vim.org>
parents:
5010
diff
changeset
|
5177 || STRNCMP(skipwhite(e + 1), "set", 3) == 0) |
7 | 5178 && (s[3] == ':' |
5179 || (VIM_VERSION_100 >= vers && isdigit(s[3])) | |
5180 || (VIM_VERSION_100 < vers && s[3] == '<') | |
5181 || (VIM_VERSION_100 > vers && s[3] == '>') | |
5182 || (VIM_VERSION_100 == vers && s[3] == '='))) | |
5183 break; | |
5184 } | |
5185 } | |
5186 prev = *s; | |
5187 } | |
5188 | |
5189 if (*s) | |
5190 { | |
5191 do /* skip over "ex:", "vi:" or "vim:" */ | |
5192 ++s; | |
5193 while (s[-1] != ':'); | |
5194 | |
5195 s = linecopy = vim_strsave(s); /* copy the line, it will change */ | |
5196 if (linecopy == NULL) | |
5197 return FAIL; | |
5198 | |
5199 save_sourcing_lnum = sourcing_lnum; | |
5200 save_sourcing_name = sourcing_name; | |
5201 sourcing_lnum = lnum; /* prepare for emsg() */ | |
5202 sourcing_name = (char_u *)"modelines"; | |
5203 | |
5204 end = FALSE; | |
5205 while (end == FALSE) | |
5206 { | |
5207 s = skipwhite(s); | |
5208 if (*s == NUL) | |
5209 break; | |
5210 | |
5211 /* | |
5212 * Find end of set command: ':' or end of line. | |
5213 * Skip over "\:", replacing it with ":". | |
5214 */ | |
5215 for (e = s; *e != ':' && *e != NUL; ++e) | |
5216 if (e[0] == '\\' && e[1] == ':') | |
1618 | 5217 STRMOVE(e, e + 1); |
7 | 5218 if (*e == NUL) |
5219 end = TRUE; | |
5220 | |
5221 /* | |
5222 * If there is a "set" command, require a terminating ':' and | |
5223 * ignore the stuff after the ':'. | |
5224 * "vi:set opt opt opt: foo" -- foo not interpreted | |
5225 * "vi:opt opt opt: foo" -- foo interpreted | |
5226 * Accept "se" for compatibility with Elvis. | |
5227 */ | |
5228 if (STRNCMP(s, "set ", (size_t)4) == 0 | |
5229 || STRNCMP(s, "se ", (size_t)3) == 0) | |
5230 { | |
5231 if (*e != ':') /* no terminating ':'? */ | |
5232 break; | |
5233 end = TRUE; | |
5234 s = vim_strchr(s, ' ') + 1; | |
5235 } | |
5236 *e = NUL; /* truncate the set command */ | |
5237 | |
5238 if (*s != NUL) /* skip over an empty "::" */ | |
5239 { | |
5240 #ifdef FEAT_EVAL | |
5241 save_SID = current_SID; | |
5242 current_SID = SID_MODELINE; | |
5243 #endif | |
717 | 5244 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags); |
7 | 5245 #ifdef FEAT_EVAL |
5246 current_SID = save_SID; | |
5247 #endif | |
5248 if (retval == FAIL) /* stop if error found */ | |
5249 break; | |
5250 } | |
5251 s = e + 1; /* advance to next part */ | |
5252 } | |
5253 | |
5254 sourcing_lnum = save_sourcing_lnum; | |
5255 sourcing_name = save_sourcing_name; | |
5256 | |
5257 vim_free(linecopy); | |
5258 } | |
5259 return retval; | |
5260 } | |
5261 | |
1559 | 5262 #if defined(FEAT_VIMINFO) || defined(PROTO) |
7 | 5263 int |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5264 read_viminfo_bufferlist( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5265 vir_T *virp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5266 int writing) |
7 | 5267 { |
5268 char_u *tab; | |
5269 linenr_T lnum; | |
5270 colnr_T col; | |
5271 buf_T *buf; | |
5272 char_u *sfname; | |
5273 char_u *xline; | |
5274 | |
5275 /* Handle long line and escaped characters. */ | |
5276 xline = viminfo_readstring(virp, 1, FALSE); | |
5277 | |
5278 /* don't read in if there are files on the command-line or if writing: */ | |
5279 if (xline != NULL && !writing && ARGCOUNT == 0 | |
5280 && find_viminfo_parameter('%') != NULL) | |
5281 { | |
5282 /* Format is: <fname> Tab <lnum> Tab <col>. | |
5283 * Watch out for a Tab in the file name, work from the end. */ | |
5284 lnum = 0; | |
5285 col = 0; | |
5286 tab = vim_strrchr(xline, '\t'); | |
5287 if (tab != NULL) | |
5288 { | |
5289 *tab++ = '\0'; | |
1869 | 5290 col = (colnr_T)atoi((char *)tab); |
7 | 5291 tab = vim_strrchr(xline, '\t'); |
5292 if (tab != NULL) | |
5293 { | |
5294 *tab++ = '\0'; | |
5295 lnum = atol((char *)tab); | |
5296 } | |
5297 } | |
5298 | |
5299 /* Expand "~/" in the file name at "line + 1" to a full path. | |
5300 * Then try shortening it by comparing with the current directory */ | |
5301 expand_env(xline, NameBuff, MAXPATHL); | |
1411 | 5302 sfname = shorten_fname1(NameBuff); |
7 | 5303 |
5304 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED); | |
5305 if (buf != NULL) /* just in case... */ | |
5306 { | |
5307 buf->b_last_cursor.lnum = lnum; | |
5308 buf->b_last_cursor.col = col; | |
5309 buflist_setfpos(buf, curwin, lnum, col, FALSE); | |
5310 } | |
5311 } | |
5312 vim_free(xline); | |
5313 | |
5314 return viminfo_readline(virp); | |
5315 } | |
5316 | |
5317 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5318 write_viminfo_bufferlist(FILE *fp) |
7 | 5319 { |
5320 buf_T *buf; | |
5321 #ifdef FEAT_WINDOWS | |
5322 win_T *win; | |
671 | 5323 tabpage_T *tp; |
7 | 5324 #endif |
5325 char_u *line; | |
23 | 5326 int max_buffers; |
7 | 5327 |
5328 if (find_viminfo_parameter('%') == NULL) | |
5329 return; | |
5330 | |
23 | 5331 /* Without a number -1 is returned: do all buffers. */ |
5332 max_buffers = get_viminfo_parameter('%'); | |
5333 | |
7 | 5334 /* Allocate room for the file name, lnum and col. */ |
1869 | 5335 #define LINE_BUF_LEN (MAXPATHL + 40) |
5336 line = alloc(LINE_BUF_LEN); | |
7 | 5337 if (line == NULL) |
5338 return; | |
5339 | |
5340 #ifdef FEAT_WINDOWS | |
671 | 5341 FOR_ALL_TAB_WINDOWS(tp, win) |
7 | 5342 set_last_cursor(win); |
5343 #else | |
5344 set_last_cursor(curwin); | |
5345 #endif | |
5346 | |
2545
298d8d6e69be
Avoid warnings from the clang compiler. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2520
diff
changeset
|
5347 fputs(_("\n# Buffer list:\n"), fp); |
7 | 5348 for (buf = firstbuf; buf != NULL ; buf = buf->b_next) |
5349 { | |
5350 if (buf->b_fname == NULL | |
5351 || !buf->b_p_bl | |
5352 #ifdef FEAT_QUICKFIX | |
5353 || bt_quickfix(buf) | |
5354 #endif | |
5355 || removable(buf->b_ffname)) | |
5356 continue; | |
5357 | |
23 | 5358 if (max_buffers-- == 0) |
5359 break; | |
7 | 5360 putc('%', fp); |
5361 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
|
5362 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d", |
7 | 5363 (long)buf->b_last_cursor.lnum, |
5364 buf->b_last_cursor.col); | |
5365 viminfo_writestring(fp, line); | |
5366 } | |
5367 vim_free(line); | |
5368 } | |
5369 #endif | |
5370 | |
5371 | |
5372 /* | |
5373 * Return special buffer name. | |
5374 * Returns NULL when the buffer has a normal file name. | |
5375 */ | |
3839 | 5376 char_u * |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5377 buf_spname(buf_T *buf) |
7 | 5378 { |
5379 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS) | |
5380 if (bt_quickfix(buf)) | |
643 | 5381 { |
5208
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5382 win_T *win; |
1559 | 5383 tabpage_T *tp; |
643 | 5384 |
5385 /* | |
5386 * For location list window, w_llist_ref points to the location list. | |
5387 * For quickfix window, w_llist_ref is NULL. | |
5388 */ | |
5208
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5389 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL) |
3839 | 5390 return (char_u *)_(msg_loclist); |
643 | 5391 else |
3839 | 5392 return (char_u *)_(msg_qflist); |
643 | 5393 } |
7 | 5394 #endif |
5395 #ifdef FEAT_QUICKFIX | |
5396 /* There is no _file_ when 'buftype' is "nofile", b_sfname | |
5397 * contains the name as specified by the user */ | |
5398 if (bt_nofile(buf)) | |
5399 { | |
5400 if (buf->b_sfname != NULL) | |
3839 | 5401 return buf->b_sfname; |
5402 return (char_u *)_("[Scratch]"); | |
7 | 5403 } |
5404 #endif | |
5405 if (buf->b_fname == NULL) | |
3839 | 5406 return (char_u *)_("[No Name]"); |
7 | 5407 return NULL; |
5408 } | |
5409 | |
5208
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5410 #if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \ |
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5411 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \ |
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5412 || defined(PROTO) |
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 * Find a window for buffer "buf". |
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5415 * 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
|
5416 * If not found FAIL is returned. |
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5417 */ |
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5418 int |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5419 find_win_for_buf( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5420 buf_T *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5421 win_T **wp, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5422 tabpage_T **tp) |
5208
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5423 { |
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5424 FOR_ALL_TAB_WINDOWS(*tp, *wp) |
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5425 if ((*wp)->w_buffer == buf) |
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5426 goto win_found; |
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5427 return FAIL; |
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5428 win_found: |
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5429 return OK; |
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5430 } |
bc4fb0317465
updated for version 7.4a.030
Bram Moolenaar <bram@vim.org>
parents:
5043
diff
changeset
|
5431 #endif |
7 | 5432 |
5433 #if defined(FEAT_SIGNS) || defined(PROTO) | |
5434 /* | |
5435 * Insert the sign into the signlist. | |
5436 */ | |
5437 static void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5438 insert_sign( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5439 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
|
5440 signlist_T *prev, /* previous sign entry */ |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5441 signlist_T *next, /* next sign entry */ |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5442 int id, /* sign ID */ |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5443 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
|
5444 int typenr) /* typenr of sign we are adding */ |
7 | 5445 { |
5446 signlist_T *newsign; | |
5447 | |
5448 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE); | |
5449 if (newsign != NULL) | |
5450 { | |
5451 newsign->id = id; | |
5452 newsign->lnum = lnum; | |
5453 newsign->typenr = typenr; | |
5454 newsign->next = next; | |
5455 #ifdef FEAT_NETBEANS_INTG | |
5456 newsign->prev = prev; | |
5457 if (next != NULL) | |
5458 next->prev = newsign; | |
5459 #endif | |
5460 | |
5461 if (prev == NULL) | |
5462 { | |
5463 /* When adding first sign need to redraw the windows to create the | |
5464 * column for signs. */ | |
5465 if (buf->b_signlist == NULL) | |
5466 { | |
5467 redraw_buf_later(buf, NOT_VALID); | |
5468 changed_cline_bef_curs(); | |
5469 } | |
5470 | |
5471 /* first sign in signlist */ | |
5472 buf->b_signlist = newsign; | |
6689 | 5473 #ifdef FEAT_NETBEANS_INTG |
5474 if (netbeans_active()) | |
5475 buf->b_has_sign_column = TRUE; | |
5476 #endif | |
7 | 5477 } |
5478 else | |
5479 prev->next = newsign; | |
5480 } | |
5481 } | |
5482 | |
5483 /* | |
5484 * Add the sign into the signlist. Find the right spot to do it though. | |
5485 */ | |
5486 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5487 buf_addsign( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5488 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
|
5489 int id, /* sign ID */ |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5490 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
|
5491 int typenr) /* typenr of sign we are adding */ |
7 | 5492 { |
5493 signlist_T *sign; /* a sign in the signlist */ | |
5494 signlist_T *prev; /* the previous sign */ | |
5495 | |
5496 prev = NULL; | |
5497 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5498 { | |
5499 if (lnum == sign->lnum && id == sign->id) | |
5500 { | |
5501 sign->typenr = typenr; | |
5502 return; | |
5503 } | |
5504 else if ( | |
5505 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */ | |
5506 id < 0 && | |
5507 #endif | |
5508 lnum < sign->lnum) | |
5509 { | |
5510 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */ | |
5511 /* XXX - GRP: Is this because of sign slide problem? Or is it | |
5512 * really needed? Or is it because we allow multiple signs per | |
5513 * line? If so, should I add that feature to FEAT_SIGNS? | |
5514 */ | |
5515 while (prev != NULL && prev->lnum == lnum) | |
5516 prev = prev->prev; | |
5517 if (prev == NULL) | |
5518 sign = buf->b_signlist; | |
5519 else | |
5520 sign = prev->next; | |
5521 #endif | |
5522 insert_sign(buf, prev, sign, id, lnum, typenr); | |
5523 return; | |
5524 } | |
5525 prev = sign; | |
5526 } | |
5527 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */ | |
5528 /* XXX - GRP: See previous comment */ | |
5529 while (prev != NULL && prev->lnum == lnum) | |
5530 prev = prev->prev; | |
5531 if (prev == NULL) | |
5532 sign = buf->b_signlist; | |
5533 else | |
5534 sign = prev->next; | |
5535 #endif | |
5536 insert_sign(buf, prev, sign, id, lnum, typenr); | |
5537 | |
5538 return; | |
5539 } | |
5540 | |
5869 | 5541 /* |
5542 * For an existing, placed sign "markId" change the type to "typenr". | |
5543 * Returns the line number of the sign, or zero if the sign is not found. | |
5544 */ | |
1869 | 5545 linenr_T |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5546 buf_change_sign_type( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5547 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
|
5548 int markId, /* sign ID */ |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5549 int typenr) /* typenr of sign we are adding */ |
7 | 5550 { |
5551 signlist_T *sign; /* a sign in the signlist */ | |
5552 | |
5553 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5554 { | |
5555 if (sign->id == markId) | |
5556 { | |
5557 sign->typenr = typenr; | |
5558 return sign->lnum; | |
5559 } | |
5560 } | |
5561 | |
1869 | 5562 return (linenr_T)0; |
7 | 5563 } |
5564 | |
1869 | 5565 int |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5566 buf_getsigntype( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5567 buf_T *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5568 linenr_T lnum, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5569 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */ |
7 | 5570 { |
5571 signlist_T *sign; /* a sign in a b_signlist */ | |
5572 | |
5573 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5574 if (sign->lnum == lnum | |
5575 && (type == SIGN_ANY | |
5576 # ifdef FEAT_SIGN_ICONS | |
5577 || (type == SIGN_ICON | |
5578 && sign_get_image(sign->typenr) != NULL) | |
5579 # endif | |
5580 || (type == SIGN_TEXT | |
5581 && sign_get_text(sign->typenr) != NULL) | |
5582 || (type == SIGN_LINEHL | |
5583 && sign_get_attr(sign->typenr, TRUE) != 0))) | |
5584 return sign->typenr; | |
5585 return 0; | |
5586 } | |
5587 | |
5588 | |
5589 linenr_T | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5590 buf_delsign( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5591 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
|
5592 int id) /* sign id */ |
7 | 5593 { |
5594 signlist_T **lastp; /* pointer to pointer to current sign */ | |
5595 signlist_T *sign; /* a sign in a b_signlist */ | |
5596 signlist_T *next; /* the next sign in a b_signlist */ | |
5597 linenr_T lnum; /* line number whose sign was deleted */ | |
5598 | |
5599 lastp = &buf->b_signlist; | |
5600 lnum = 0; | |
5601 for (sign = buf->b_signlist; sign != NULL; sign = next) | |
5602 { | |
5603 next = sign->next; | |
5604 if (sign->id == id) | |
5605 { | |
5606 *lastp = next; | |
5607 #ifdef FEAT_NETBEANS_INTG | |
5608 if (next != NULL) | |
5609 next->prev = sign->prev; | |
5610 #endif | |
5611 lnum = sign->lnum; | |
5612 vim_free(sign); | |
5613 break; | |
5614 } | |
5615 else | |
5616 lastp = &sign->next; | |
5617 } | |
5618 | |
5619 /* When deleted the last sign need to redraw the windows to remove the | |
5620 * sign column. */ | |
5621 if (buf->b_signlist == NULL) | |
5622 { | |
5623 redraw_buf_later(buf, NOT_VALID); | |
5624 changed_cline_bef_curs(); | |
5625 } | |
5626 | |
5627 return lnum; | |
5628 } | |
5629 | |
5630 | |
5631 /* | |
5632 * Find the line number of the sign with the requested id. If the sign does | |
5633 * not exist, return 0 as the line number. This will still let the correct file | |
5634 * get loaded. | |
5635 */ | |
5636 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5637 buf_findsign( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5638 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
|
5639 int id) /* sign ID */ |
7 | 5640 { |
5641 signlist_T *sign; /* a sign in the signlist */ | |
5642 | |
5643 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5644 if (sign->id == id) | |
5645 return sign->lnum; | |
5646 | |
5647 return 0; | |
5648 } | |
5649 | |
5650 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5651 buf_findsign_id( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5652 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
|
5653 linenr_T lnum) /* line number of sign */ |
7 | 5654 { |
5655 signlist_T *sign; /* a sign in the signlist */ | |
5656 | |
5657 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5658 if (sign->lnum == lnum) | |
5659 return sign->id; | |
5660 | |
5661 return 0; | |
5662 } | |
5663 | |
5664 | |
5665 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO) | |
5666 /* see if a given type of sign exists on a specific line */ | |
5667 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5668 buf_findsigntype_id( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5669 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
|
5670 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
|
5671 int typenr) /* sign type number */ |
7 | 5672 { |
5673 signlist_T *sign; /* a sign in the signlist */ | |
5674 | |
5675 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5676 if (sign->lnum == lnum && sign->typenr == typenr) | |
5677 return sign->id; | |
5678 | |
5679 return 0; | |
5680 } | |
5681 | |
5682 | |
5683 # if defined(FEAT_SIGN_ICONS) || defined(PROTO) | |
5684 /* return the number of icons on the given line */ | |
5685 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5686 buf_signcount(buf_T *buf, linenr_T lnum) |
7 | 5687 { |
5688 signlist_T *sign; /* a sign in the signlist */ | |
5689 int count = 0; | |
5690 | |
5691 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) | |
5692 if (sign->lnum == lnum) | |
5693 if (sign_get_image(sign->typenr) != NULL) | |
5694 count++; | |
5695 | |
5696 return count; | |
5697 } | |
5698 # endif /* FEAT_SIGN_ICONS */ | |
5699 # endif /* FEAT_NETBEANS_INTG */ | |
5700 | |
5701 | |
5702 /* | |
5703 * Delete signs in buffer "buf". | |
5704 */ | |
3672 | 5705 void |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5706 buf_delete_signs(buf_T *buf) |
7 | 5707 { |
5708 signlist_T *next; | |
5709 | |
5869 | 5710 /* When deleting the last sign need to redraw the windows to remove the |
6060 | 5711 * sign column. Not when curwin is NULL (this means we're exiting). */ |
5712 if (buf->b_signlist != NULL && curwin != NULL) | |
5869 | 5713 { |
5714 redraw_buf_later(buf, NOT_VALID); | |
5715 changed_cline_bef_curs(); | |
5716 } | |
5717 | |
7 | 5718 while (buf->b_signlist != NULL) |
5719 { | |
5720 next = buf->b_signlist->next; | |
5721 vim_free(buf->b_signlist); | |
5722 buf->b_signlist = next; | |
5723 } | |
5724 } | |
5725 | |
5726 /* | |
5727 * Delete all signs in all buffers. | |
5728 */ | |
5729 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5730 buf_delete_all_signs(void) |
7 | 5731 { |
5732 buf_T *buf; /* buffer we are checking for signs */ | |
5733 | |
5734 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
5735 if (buf->b_signlist != NULL) | |
5736 buf_delete_signs(buf); | |
5737 } | |
5738 | |
5739 /* | |
5740 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers. | |
5741 */ | |
5742 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5743 sign_list_placed(buf_T *rbuf) |
7 | 5744 { |
5745 buf_T *buf; | |
5746 signlist_T *p; | |
5747 char lbuf[BUFSIZ]; | |
5748 | |
5749 MSG_PUTS_TITLE(_("\n--- Signs ---")); | |
5750 msg_putchar('\n'); | |
5751 if (rbuf == NULL) | |
5752 buf = firstbuf; | |
5753 else | |
5754 buf = rbuf; | |
3411 | 5755 while (buf != NULL && !got_int) |
7 | 5756 { |
5757 if (buf->b_signlist != NULL) | |
5758 { | |
272 | 5759 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname); |
7 | 5760 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D)); |
5761 msg_putchar('\n'); | |
5762 } | |
3411 | 5763 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next) |
7 | 5764 { |
272 | 5765 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"), |
7 | 5766 (long)p->lnum, p->id, sign_typenr2name(p->typenr)); |
5767 MSG_PUTS(lbuf); | |
5768 msg_putchar('\n'); | |
5769 } | |
5770 if (rbuf != NULL) | |
5771 break; | |
5772 buf = buf->b_next; | |
5773 } | |
5774 } | |
5775 | |
5776 /* | |
5777 * Adjust a placed sign for inserted/deleted lines. | |
5778 */ | |
5779 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5780 sign_mark_adjust( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5781 linenr_T line1, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5782 linenr_T line2, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5783 long amount, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5784 long amount_after) |
7 | 5785 { |
5786 signlist_T *sign; /* a sign in a b_signlist */ | |
5787 | |
5788 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next) | |
5789 { | |
5790 if (sign->lnum >= line1 && sign->lnum <= line2) | |
5791 { | |
5792 if (amount == MAXLNUM) | |
5793 sign->lnum = line1; | |
5794 else | |
5795 sign->lnum += amount; | |
5796 } | |
5797 else if (sign->lnum > line2) | |
5798 sign->lnum += amount_after; | |
5799 } | |
5800 } | |
5801 #endif /* FEAT_SIGNS */ | |
5802 | |
5803 /* | |
5804 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed. | |
5805 */ | |
5806 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5807 set_buflisted(int on) |
7 | 5808 { |
5809 if (on != curbuf->b_p_bl) | |
5810 { | |
5811 curbuf->b_p_bl = on; | |
5812 #ifdef FEAT_AUTOCMD | |
5813 if (on) | |
5814 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); | |
5815 else | |
5816 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
5817 #endif | |
5818 } | |
5819 } | |
5820 | |
5821 /* | |
5822 * Read the file for "buf" again and check if the contents changed. | |
5823 * Return TRUE if it changed or this could not be checked. | |
5824 */ | |
5825 int | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5826 buf_contents_changed(buf_T *buf) |
7 | 5827 { |
5828 buf_T *newbuf; | |
5829 int differ = TRUE; | |
5830 linenr_T lnum; | |
5831 aco_save_T aco; | |
5832 exarg_T ea; | |
5833 | |
5834 /* Allocate a buffer without putting it in the buffer list. */ | |
5835 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); | |
5836 if (newbuf == NULL) | |
5837 return TRUE; | |
5838 | |
5839 /* Force the 'fileencoding' and 'fileformat' to be equal. */ | |
5840 if (prep_exarg(&ea, buf) == FAIL) | |
5841 { | |
5842 wipe_buffer(newbuf, FALSE); | |
5843 return TRUE; | |
5844 } | |
5845 | |
5846 /* set curwin/curbuf to buf and save a few things */ | |
5847 aucmd_prepbuf(&aco, newbuf); | |
5848 | |
625 | 5849 if (ml_open(curbuf) == OK |
7 | 5850 && readfile(buf->b_ffname, buf->b_fname, |
5851 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, | |
5852 &ea, READ_NEW | READ_DUMMY) == OK) | |
5853 { | |
5854 /* compare the two files line by line */ | |
5855 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count) | |
5856 { | |
5857 differ = FALSE; | |
5858 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) | |
5859 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0) | |
5860 { | |
5861 differ = TRUE; | |
5862 break; | |
5863 } | |
5864 } | |
5865 } | |
5866 vim_free(ea.cmd); | |
5867 | |
5868 /* restore curwin/curbuf and a few other things */ | |
5869 aucmd_restbuf(&aco); | |
5870 | |
5871 if (curbuf != newbuf) /* safety check */ | |
5872 wipe_buffer(newbuf, FALSE); | |
5873 | |
5874 return differ; | |
5875 } | |
5876 | |
5877 /* | |
5878 * Wipe out a buffer and decrement the last buffer number if it was used for | |
5879 * this buffer. Call this to wipe out a temp buffer that does not contain any | |
5880 * marks. | |
5881 */ | |
5882 void | |
7817
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5883 wipe_buffer( |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5884 buf_T *buf, |
83861277e6a3
commit https://github.com/vim/vim/commit/7454a06e2642d2b37afad1c5e71cec68081ca4ff
Christian Brabandt <cb@256bit.org>
parents:
7799
diff
changeset
|
5885 int aucmd UNUSED) /* When TRUE trigger autocommands. */ |
7 | 5886 { |
5887 if (buf->b_fnum == top_file_num - 1) | |
5888 --top_file_num; | |
5889 | |
5890 #ifdef FEAT_AUTOCMD | |
5891 if (!aucmd) /* Don't trigger BufDelete autocommands here. */ | |
1410 | 5892 block_autocmds(); |
7 | 5893 #endif |
3365 | 5894 close_buffer(NULL, buf, DOBUF_WIPE, FALSE); |
7 | 5895 #ifdef FEAT_AUTOCMD |
5896 if (!aucmd) | |
1410 | 5897 unblock_autocmds(); |
7 | 5898 #endif |
5899 } |