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