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