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