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