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