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