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