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