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