Mercurial > vim
annotate src/ex_cmds.c @ 7689:20dc2763a3b9 v7.4.1143
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Author: Bram Moolenaar <Bram@vim.org>
Date: Tue Jan 19 23:36:15 2016 +0100
patch 7.4.1143
Problem: Can't sort on floating point numbers.
Solution: Add the "f" flag to ":sort". (Alex Jakushev) Also add the "f"
flag to sort().
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Tue, 19 Jan 2016 23:45:05 +0100 |
parents | 1fded31d9e04 |
children | fd4175b669e2 |
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 * ex_cmds.c: some functions for command line commands | |
12 */ | |
13 | |
14 #include "vim.h" | |
15 #include "version.h" | |
16 | |
17 #ifdef FEAT_EX_EXTRA | |
18 static int linelen __ARGS((int *has_tab)); | |
19 #endif | |
20 static void do_filter __ARGS((linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, int do_in, int do_out)); | |
21 #ifdef FEAT_VIMINFO | |
22 static char_u *viminfo_filename __ARGS((char_u *)); | |
1733 | 23 static void do_viminfo __ARGS((FILE *fp_in, FILE *fp_out, int flags)); |
7 | 24 static int viminfo_encoding __ARGS((vir_T *virp)); |
25 static int read_viminfo_up_to_marks __ARGS((vir_T *virp, int forceit, int writing)); | |
26 #endif | |
27 | |
28 static int check_readonly __ARGS((int *forceit, buf_T *buf)); | |
29 #ifdef FEAT_AUTOCMD | |
30 static void delbuf_msg __ARGS((char_u *name)); | |
31 #endif | |
32 static int | |
33 #ifdef __BORLANDC__ | |
34 _RTLENTRYF | |
35 #endif | |
36 help_compare __ARGS((const void *s1, const void *s2)); | |
6365 | 37 static void prepare_help_buffer __ARGS((void)); |
7 | 38 |
39 /* | |
40 * ":ascii" and "ga". | |
41 */ | |
42 void | |
43 do_ascii(eap) | |
1876 | 44 exarg_T *eap UNUSED; |
7 | 45 { |
46 int c; | |
1784 | 47 int cval; |
7 | 48 char buf1[20]; |
49 char buf2[20]; | |
50 char_u buf3[7]; | |
51 #ifdef FEAT_MBYTE | |
714 | 52 int cc[MAX_MCO]; |
53 int ci = 0; | |
7 | 54 int len; |
55 | |
56 if (enc_utf8) | |
714 | 57 c = utfc_ptr2char(ml_get_cursor(), cc); |
7 | 58 else |
59 #endif | |
60 c = gchar_cursor(); | |
61 if (c == NUL) | |
62 { | |
63 MSG("NUL"); | |
64 return; | |
65 } | |
66 | |
67 #ifdef FEAT_MBYTE | |
68 IObuff[0] = NUL; | |
69 if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80) | |
70 #endif | |
71 { | |
72 if (c == NL) /* NUL is stored as NL */ | |
73 c = NUL; | |
1784 | 74 if (c == CAR && get_fileformat(curbuf) == EOL_MAC) |
75 cval = NL; /* NL is stored as CR */ | |
76 else | |
77 cval = c; | |
7 | 78 if (vim_isprintc_strict(c) && (c < ' ' |
79 #ifndef EBCDIC | |
80 || c > '~' | |
81 #endif | |
82 )) | |
83 { | |
84 transchar_nonprint(buf3, c); | |
1872 | 85 vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3); |
7 | 86 } |
87 else | |
88 buf1[0] = NUL; | |
89 #ifndef EBCDIC | |
90 if (c >= 0x80) | |
1872 | 91 vim_snprintf(buf2, sizeof(buf2), " <M-%s>", |
92 (char *)transchar(c & 0x7f)); | |
7 | 93 else |
94 #endif | |
95 buf2[0] = NUL; | |
274 | 96 vim_snprintf((char *)IObuff, IOSIZE, |
97 _("<%s>%s%s %d, Hex %02x, Octal %03o"), | |
1784 | 98 transchar(c), buf1, buf2, cval, cval, cval); |
7 | 99 #ifdef FEAT_MBYTE |
963 | 100 if (enc_utf8) |
101 c = cc[ci++]; | |
102 else | |
103 c = 0; | |
7 | 104 #endif |
105 } | |
106 | |
107 #ifdef FEAT_MBYTE | |
108 /* Repeat for combining characters. */ | |
109 while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80))) | |
110 { | |
111 len = (int)STRLEN(IObuff); | |
112 /* This assumes every multi-byte char is printable... */ | |
113 if (len > 0) | |
114 IObuff[len++] = ' '; | |
115 IObuff[len++] = '<'; | |
963 | 116 if (enc_utf8 && utf_iscomposing(c) |
625 | 117 # ifdef USE_GUI |
7 | 118 && !gui.in_use |
625 | 119 # endif |
7 | 120 ) |
121 IObuff[len++] = ' '; /* draw composing char on top of a space */ | |
274 | 122 len += (*mb_char2bytes)(c, IObuff + len); |
123 vim_snprintf((char *)IObuff + len, IOSIZE - len, | |
7 | 124 c < 0x10000 ? _("> %d, Hex %04x, Octal %o") |
125 : _("> %d, Hex %08x, Octal %o"), c, c, c); | |
714 | 126 if (ci == MAX_MCO) |
127 break; | |
963 | 128 if (enc_utf8) |
129 c = cc[ci++]; | |
130 else | |
131 c = 0; | |
7 | 132 } |
133 #endif | |
134 | |
135 msg(IObuff); | |
136 } | |
137 | |
138 #if defined(FEAT_EX_EXTRA) || defined(PROTO) | |
139 /* | |
140 * ":left", ":center" and ":right": align text. | |
141 */ | |
142 void | |
143 ex_align(eap) | |
144 exarg_T *eap; | |
145 { | |
146 pos_T save_curpos; | |
147 int len; | |
148 int indent = 0; | |
149 int new_indent; | |
150 int has_tab; | |
151 int width; | |
152 | |
153 #ifdef FEAT_RIGHTLEFT | |
154 if (curwin->w_p_rl) | |
155 { | |
156 /* switch left and right aligning */ | |
157 if (eap->cmdidx == CMD_right) | |
158 eap->cmdidx = CMD_left; | |
159 else if (eap->cmdidx == CMD_left) | |
160 eap->cmdidx = CMD_right; | |
161 } | |
162 #endif | |
163 | |
164 width = atoi((char *)eap->arg); | |
165 save_curpos = curwin->w_cursor; | |
166 if (eap->cmdidx == CMD_left) /* width is used for new indent */ | |
167 { | |
168 if (width >= 0) | |
169 indent = width; | |
170 } | |
171 else | |
172 { | |
173 /* | |
174 * if 'textwidth' set, use it | |
175 * else if 'wrapmargin' set, use it | |
176 * if invalid value, use 80 | |
177 */ | |
178 if (width <= 0) | |
179 width = curbuf->b_p_tw; | |
180 if (width == 0 && curbuf->b_p_wm > 0) | |
181 width = W_WIDTH(curwin) - curbuf->b_p_wm; | |
182 if (width <= 0) | |
183 width = 80; | |
184 } | |
185 | |
186 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL) | |
187 return; | |
188 | |
189 for (curwin->w_cursor.lnum = eap->line1; | |
190 curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum) | |
191 { | |
192 if (eap->cmdidx == CMD_left) /* left align */ | |
193 new_indent = indent; | |
194 else | |
195 { | |
944 | 196 has_tab = FALSE; /* avoid uninit warnings */ |
7 | 197 len = linelen(eap->cmdidx == CMD_right ? &has_tab |
198 : NULL) - get_indent(); | |
199 | |
200 if (len <= 0) /* skip blank lines */ | |
201 continue; | |
202 | |
203 if (eap->cmdidx == CMD_center) | |
204 new_indent = (width - len) / 2; | |
205 else | |
206 { | |
207 new_indent = width - len; /* right align */ | |
208 | |
209 /* | |
210 * Make sure that embedded TABs don't make the text go too far | |
211 * to the right. | |
212 */ | |
213 if (has_tab) | |
214 while (new_indent > 0) | |
215 { | |
216 (void)set_indent(new_indent, 0); | |
217 if (linelen(NULL) <= width) | |
218 { | |
219 /* | |
220 * Now try to move the line as much as possible to | |
221 * the right. Stop when it moves too far. | |
222 */ | |
223 do | |
224 (void)set_indent(++new_indent, 0); | |
225 while (linelen(NULL) <= width); | |
226 --new_indent; | |
227 break; | |
228 } | |
229 --new_indent; | |
230 } | |
231 } | |
232 } | |
233 if (new_indent < 0) | |
234 new_indent = 0; | |
235 (void)set_indent(new_indent, 0); /* set indent */ | |
236 } | |
237 changed_lines(eap->line1, 0, eap->line2 + 1, 0L); | |
238 curwin->w_cursor = save_curpos; | |
239 beginline(BL_WHITE | BL_FIX); | |
240 } | |
241 | |
242 /* | |
243 * Get the length of the current line, excluding trailing white space. | |
244 */ | |
245 static int | |
246 linelen(has_tab) | |
247 int *has_tab; | |
248 { | |
249 char_u *line; | |
250 char_u *first; | |
251 char_u *last; | |
252 int save; | |
253 int len; | |
254 | |
255 /* find the first non-blank character */ | |
256 line = ml_get_curline(); | |
257 first = skipwhite(line); | |
258 | |
259 /* find the character after the last non-blank character */ | |
260 for (last = first + STRLEN(first); | |
261 last > first && vim_iswhite(last[-1]); --last) | |
262 ; | |
263 save = *last; | |
264 *last = NUL; | |
265 len = linetabsize(line); /* get line length */ | |
266 if (has_tab != NULL) /* check for embedded TAB */ | |
267 *has_tab = (vim_strrchr(first, TAB) != NULL); | |
268 *last = save; | |
269 | |
270 return len; | |
271 } | |
272 | |
826 | 273 /* Buffer for two lines used during sorting. They are allocated to |
274 * contain the longest line being sorted. */ | |
275 static char_u *sortbuf1; | |
276 static char_u *sortbuf2; | |
284 | 277 |
7689
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
278 static int sort_ic; /* ignore case */ |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
279 static int sort_nr; /* sort on number */ |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
280 static int sort_rx; /* sort on regex instead of skipping it */ |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
281 #ifdef FEAT_FLOAT |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
282 static int sort_flt; /* sort on floating number */ |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
283 #endif |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
284 |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
285 static int sort_abort; /* flag to indicate if sorting has been interrupted */ |
294 | 286 |
287 /* Struct to store info to be sorted. */ | |
288 typedef struct | |
289 { | |
290 linenr_T lnum; /* line number */ | |
7689
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
291 union { |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
292 struct |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
293 { |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
294 long start_col_nr; /* starting column number */ |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
295 long end_col_nr; /* ending column number */ |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
296 } line; |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
297 long value; /* value if sorting by integer */ |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
298 #ifdef FEAT_FLOAT |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
299 float_T value_flt; /* value if sorting by float */ |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
300 #endif |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
301 } st_u; |
294 | 302 } sorti_T; |
284 | 303 |
304 static int | |
305 #ifdef __BORLANDC__ | |
306 _RTLENTRYF | |
307 #endif | |
308 sort_compare __ARGS((const void *s1, const void *s2)); | |
309 | |
310 static int | |
311 #ifdef __BORLANDC__ | |
312 _RTLENTRYF | |
313 #endif | |
314 sort_compare(s1, s2) | |
315 const void *s1; | |
316 const void *s2; | |
317 { | |
294 | 318 sorti_T l1 = *(sorti_T *)s1; |
319 sorti_T l2 = *(sorti_T *)s2; | |
826 | 320 int result = 0; |
321 | |
322 /* If the user interrupts, there's no way to stop qsort() immediately, but | |
834 | 323 * if we return 0 every time, qsort will assume it's done sorting and |
324 * exit. */ | |
826 | 325 if (sort_abort) |
326 return 0; | |
327 fast_breakcheck(); | |
328 if (got_int) | |
329 sort_abort = TRUE; | |
330 | |
834 | 331 /* When sorting numbers "start_col_nr" is the number, not the column |
332 * number. */ | |
294 | 333 if (sort_nr) |
7689
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
334 result = l1.st_u.value == l2.st_u.value ? 0 |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
335 : l1.st_u.value > l2.st_u.value ? 1 : -1; |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
336 #ifdef FEAT_FLOAT |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
337 else if (sort_flt) |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
338 result = l1.st_u.value_flt == l2.st_u.value_flt ? 0 |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
339 : l1.st_u.value_flt > l2.st_u.value_flt ? 1 : -1; |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
340 #endif |
826 | 341 else |
342 { | |
834 | 343 /* We need to copy one line into "sortbuf1", because there is no |
344 * guarantee that the first pointer becomes invalid when obtaining the | |
345 * second one. */ | |
7689
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
346 STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr, |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
347 l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr + 1); |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
348 sortbuf1[l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr] = 0; |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
349 STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr, |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
350 l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr + 1); |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
351 sortbuf2[l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr] = 0; |
826 | 352 |
834 | 353 result = sort_ic ? STRICMP(sortbuf1, sortbuf2) |
354 : STRCMP(sortbuf1, sortbuf2); | |
355 } | |
356 | |
357 /* If two lines have the same value, preserve the original line order. */ | |
826 | 358 if (result == 0) |
834 | 359 return (int)(l1.lnum - l2.lnum); |
360 return result; | |
284 | 361 } |
362 | |
363 /* | |
364 * ":sort". | |
365 */ | |
366 void | |
367 ex_sort(eap) | |
368 exarg_T *eap; | |
369 { | |
370 regmatch_T regmatch; | |
371 int len; | |
372 linenr_T lnum; | |
373 long maxlen = 0; | |
294 | 374 sorti_T *nrs; |
1872 | 375 size_t count = (size_t)(eap->line2 - eap->line1 + 1); |
294 | 376 size_t i; |
284 | 377 char_u *p; |
378 char_u *s; | |
826 | 379 char_u *s2; |
380 char_u c; /* temporary character storage */ | |
284 | 381 int unique = FALSE; |
382 long deleted; | |
826 | 383 colnr_T start_col; |
384 colnr_T end_col; | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
385 int sort_what = 0; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
386 int format_found = 0; |
284 | 387 |
1538 | 388 /* Sorting one line is really quick! */ |
389 if (count <= 1) | |
390 return; | |
391 | |
284 | 392 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL) |
393 return; | |
826 | 394 sortbuf1 = NULL; |
395 sortbuf2 = NULL; | |
284 | 396 regmatch.regprog = NULL; |
294 | 397 nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE); |
284 | 398 if (nrs == NULL) |
826 | 399 goto sortend; |
400 | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
401 sort_abort = sort_ic = sort_rx = sort_nr = 0; |
7689
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
402 #ifdef FEAT_FLOAT |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
403 sort_flt = 0; |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
404 #endif |
294 | 405 |
284 | 406 for (p = eap->arg; *p != NUL; ++p) |
407 { | |
408 if (vim_iswhite(*p)) | |
409 ; | |
410 else if (*p == 'i') | |
411 sort_ic = TRUE; | |
826 | 412 else if (*p == 'r') |
413 sort_rx = TRUE; | |
294 | 414 else if (*p == 'n') |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
415 { |
7689
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
416 sort_nr = 1; |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
417 ++format_found; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
418 } |
7689
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
419 #ifdef FEAT_FLOAT |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
420 else if (*p == 'f') |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
421 { |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
422 sort_flt = 1; |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
423 ++format_found; |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
424 } |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
425 #endif |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
426 else if (*p == 'b') |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
427 { |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
428 sort_what = STR2NR_BIN + STR2NR_FORCE; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
429 ++format_found; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
430 } |
294 | 431 else if (*p == 'o') |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
432 { |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
433 sort_what = STR2NR_OCT + STR2NR_FORCE; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
434 ++format_found; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
435 } |
294 | 436 else if (*p == 'x') |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
437 { |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
438 sort_what = STR2NR_HEX + STR2NR_FORCE; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
439 ++format_found; |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
440 } |
284 | 441 else if (*p == 'u') |
442 unique = TRUE; | |
289 | 443 else if (*p == '"') /* comment start */ |
444 break; | |
445 else if (check_nextcmd(p) != NULL) | |
446 { | |
447 eap->nextcmd = check_nextcmd(p); | |
448 break; | |
449 } | |
450 else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL) | |
284 | 451 { |
452 s = skip_regexp(p + 1, *p, TRUE, NULL); | |
453 if (*s != *p) | |
454 { | |
455 EMSG(_(e_invalpat)); | |
826 | 456 goto sortend; |
284 | 457 } |
458 *s = NUL; | |
1314 | 459 /* Use last search pattern if sort pattern is empty. */ |
4199 | 460 if (s == p + 1) |
461 { | |
462 if (last_search_pat() == NULL) | |
463 { | |
464 EMSG(_(e_noprevre)); | |
465 goto sortend; | |
466 } | |
1314 | 467 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC); |
4199 | 468 } |
1314 | 469 else |
470 regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC); | |
284 | 471 if (regmatch.regprog == NULL) |
826 | 472 goto sortend; |
289 | 473 p = s; /* continue after the regexp */ |
284 | 474 regmatch.rm_ic = p_ic; |
475 } | |
476 else | |
477 { | |
478 EMSG2(_(e_invarg2), p); | |
826 | 479 goto sortend; |
284 | 480 } |
481 } | |
482 | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
483 /* Can only have one of 'n', 'b', 'o' and 'x'. */ |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
484 if (format_found > 1) |
294 | 485 { |
486 EMSG(_(e_invarg)); | |
826 | 487 goto sortend; |
294 | 488 } |
489 | |
7689
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
490 /* From here on "sort_nr" is used as a flag for any integer number |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
491 * sorting. */ |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
7412
diff
changeset
|
492 sort_nr += sort_what; |
294 | 493 |
284 | 494 /* |
294 | 495 * Make an array with all line numbers. This avoids having to copy all |
284 | 496 * the lines into allocated memory. |
826 | 497 * When sorting on strings "start_col_nr" is the offset in the line, for |
498 * numbers sorting it's the number to sort on. This means the pattern | |
499 * matching and number conversion only has to be done once per line. | |
294 | 500 * Also get the longest line length for allocating "sortbuf". |
284 | 501 */ |
502 for (lnum = eap->line1; lnum <= eap->line2; ++lnum) | |
503 { | |
504 s = ml_get(lnum); | |
835 | 505 len = (int)STRLEN(s); |
284 | 506 if (maxlen < len) |
507 maxlen = len; | |
294 | 508 |
826 | 509 start_col = 0; |
510 end_col = len; | |
294 | 511 if (regmatch.regprog != NULL && vim_regexec(®match, s, 0)) |
826 | 512 { |
513 if (sort_rx) | |
514 { | |
835 | 515 start_col = (colnr_T)(regmatch.startp[0] - s); |
516 end_col = (colnr_T)(regmatch.endp[0] - s); | |
826 | 517 } |
518 else | |
835 | 519 start_col = (colnr_T)(regmatch.endp[0] - s); |
826 | 520 } |
294 | 521 else |
826 | 522 if (regmatch.regprog != NULL) |
523 end_col = 0; | |
294 | 524 |
7689
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
525 if (sort_nr || sort_flt) |
294 | 526 { |
826 | 527 /* Make sure vim_str2nr doesn't read any digits past the end |
528 * of the match, by temporarily terminating the string there */ | |
529 s2 = s + end_col; | |
530 c = *s2; | |
2606 | 531 *s2 = NUL; |
294 | 532 /* Sorting on number: Store the number itself. */ |
1687 | 533 p = s + start_col; |
7689
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
534 if (sort_nr) |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
535 { |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
536 if (sort_what & STR2NR_HEX) |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
537 s = skiptohex(p); |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
538 else if (sort_what & STR2NR_BIN) |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
539 s = skiptobin(p); |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
540 else |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
541 s = skiptodigit(p); |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
542 if (s > p && s[-1] == '-') |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
543 --s; /* include preceding negative sign */ |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
544 if (*s == NUL) |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
545 /* empty line should sort before any number */ |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
546 nrs[lnum - eap->line1].st_u.value = -MAXLNUM; |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
547 else |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
548 vim_str2nr(s, NULL, NULL, sort_what, |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
549 &nrs[lnum - eap->line1].st_u.value, NULL, 0); |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
550 } |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
551 #ifdef FEAT_FLOAT |
294 | 552 else |
7689
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
553 { |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
554 s = skipwhite(p); |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
555 if (*s == '+') |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
556 s = skipwhite(s + 1); |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
557 |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
558 if (*s == NUL) |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
559 /* empty line should sort before any number */ |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
560 nrs[lnum - eap->line1].st_u.value_flt = -DBL_MAX; |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
561 else |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
562 nrs[lnum - eap->line1].st_u.value_flt = |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
563 strtod((char *)s, NULL); |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
564 } |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
565 #endif |
2606 | 566 *s2 = c; |
294 | 567 } |
568 else | |
826 | 569 { |
294 | 570 /* Store the column to sort at. */ |
7689
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
571 nrs[lnum - eap->line1].st_u.line.start_col_nr = start_col; |
20dc2763a3b9
commit https://github.com/vim/vim/commit/f7edf40448a09e04eec3bd05e043f7fea93b07c9
Christian Brabandt <cb@256bit.org>
parents:
7664
diff
changeset
|
572 nrs[lnum - eap->line1].st_u.line.end_col_nr = end_col; |
826 | 573 } |
294 | 574 |
575 nrs[lnum - eap->line1].lnum = lnum; | |
481 | 576 |
577 if (regmatch.regprog != NULL) | |
578 fast_breakcheck(); | |
579 if (got_int) | |
826 | 580 goto sortend; |
284 | 581 } |
582 | |
294 | 583 /* Allocate a buffer that can hold the longest line. */ |
826 | 584 sortbuf1 = alloc((unsigned)maxlen + 1); |
585 if (sortbuf1 == NULL) | |
586 goto sortend; | |
587 sortbuf2 = alloc((unsigned)maxlen + 1); | |
588 if (sortbuf2 == NULL) | |
589 goto sortend; | |
284 | 590 |
481 | 591 /* Sort the array of line numbers. Note: can't be interrupted! */ |
294 | 592 qsort((void *)nrs, count, sizeof(sorti_T), sort_compare); |
284 | 593 |
826 | 594 if (sort_abort) |
595 goto sortend; | |
596 | |
284 | 597 /* Insert the lines in the sorted order below the last one. */ |
598 lnum = eap->line2; | |
599 for (i = 0; i < count; ++i) | |
600 { | |
601 s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum); | |
602 if (!unique || i == 0 | |
826 | 603 || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0) |
284 | 604 { |
7340
645abb8d8daf
commit https://github.com/vim/vim/commit/75e3ad019933f4879137775549261bf51985ab7d
Christian Brabandt <cb@256bit.org>
parents:
7226
diff
changeset
|
605 /* Copy the line into a buffer, it may become invalid in |
645abb8d8daf
commit https://github.com/vim/vim/commit/75e3ad019933f4879137775549261bf51985ab7d
Christian Brabandt <cb@256bit.org>
parents:
7226
diff
changeset
|
606 * ml_append(). And it's needed for "unique". */ |
645abb8d8daf
commit https://github.com/vim/vim/commit/75e3ad019933f4879137775549261bf51985ab7d
Christian Brabandt <cb@256bit.org>
parents:
7226
diff
changeset
|
607 STRCPY(sortbuf1, s); |
645abb8d8daf
commit https://github.com/vim/vim/commit/75e3ad019933f4879137775549261bf51985ab7d
Christian Brabandt <cb@256bit.org>
parents:
7226
diff
changeset
|
608 if (ml_append(lnum++, sortbuf1, (colnr_T)0, FALSE) == FAIL) |
284 | 609 break; |
610 } | |
481 | 611 fast_breakcheck(); |
612 if (got_int) | |
826 | 613 goto sortend; |
284 | 614 } |
615 | |
616 /* delete the original lines if appending worked */ | |
617 if (i == count) | |
618 for (i = 0; i < count; ++i) | |
619 ml_delete(eap->line1, FALSE); | |
620 else | |
621 count = 0; | |
622 | |
294 | 623 /* Adjust marks for deleted (or added) lines and prepare for displaying. */ |
835 | 624 deleted = (long)(count - (lnum - eap->line2)); |
284 | 625 if (deleted > 0) |
626 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted); | |
627 else if (deleted < 0) | |
628 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L); | |
629 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted); | |
294 | 630 |
284 | 631 curwin->w_cursor.lnum = eap->line1; |
632 beginline(BL_WHITE | BL_FIX); | |
633 | |
826 | 634 sortend: |
284 | 635 vim_free(nrs); |
826 | 636 vim_free(sortbuf1); |
637 vim_free(sortbuf2); | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4613
diff
changeset
|
638 vim_regfree(regmatch.regprog); |
481 | 639 if (got_int) |
640 EMSG(_(e_interr)); | |
284 | 641 } |
642 | |
7 | 643 /* |
644 * ":retab". | |
645 */ | |
646 void | |
647 ex_retab(eap) | |
648 exarg_T *eap; | |
649 { | |
650 linenr_T lnum; | |
651 int got_tab = FALSE; | |
652 long num_spaces = 0; | |
653 long num_tabs; | |
654 long len; | |
655 long col; | |
656 long vcol; | |
657 long start_col = 0; /* For start of white-space string */ | |
658 long start_vcol = 0; /* For start of white-space string */ | |
659 int temp; | |
660 long old_len; | |
661 char_u *ptr; | |
662 char_u *new_line = (char_u *)1; /* init to non-NULL */ | |
663 int did_undo; /* called u_save for current line */ | |
664 int new_ts; | |
665 int save_list; | |
666 linenr_T first_line = 0; /* first changed line */ | |
667 linenr_T last_line = 0; /* last changed line */ | |
668 | |
669 save_list = curwin->w_p_list; | |
670 curwin->w_p_list = 0; /* don't want list mode here */ | |
671 | |
672 new_ts = getdigits(&(eap->arg)); | |
673 if (new_ts < 0) | |
674 { | |
675 EMSG(_(e_positive)); | |
676 return; | |
677 } | |
678 if (new_ts == 0) | |
679 new_ts = curbuf->b_p_ts; | |
680 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum) | |
681 { | |
682 ptr = ml_get(lnum); | |
683 col = 0; | |
684 vcol = 0; | |
685 did_undo = FALSE; | |
686 for (;;) | |
687 { | |
688 if (vim_iswhite(ptr[col])) | |
689 { | |
690 if (!got_tab && num_spaces == 0) | |
691 { | |
692 /* First consecutive white-space */ | |
693 start_vcol = vcol; | |
694 start_col = col; | |
695 } | |
696 if (ptr[col] == ' ') | |
697 num_spaces++; | |
698 else | |
699 got_tab = TRUE; | |
700 } | |
701 else | |
702 { | |
703 if (got_tab || (eap->forceit && num_spaces > 1)) | |
704 { | |
705 /* Retabulate this string of white-space */ | |
706 | |
707 /* len is virtual length of white string */ | |
708 len = num_spaces = vcol - start_vcol; | |
709 num_tabs = 0; | |
710 if (!curbuf->b_p_et) | |
711 { | |
712 temp = new_ts - (start_vcol % new_ts); | |
713 if (num_spaces >= temp) | |
714 { | |
715 num_spaces -= temp; | |
716 num_tabs++; | |
717 } | |
718 num_tabs += num_spaces / new_ts; | |
719 num_spaces -= (num_spaces / new_ts) * new_ts; | |
720 } | |
721 if (curbuf->b_p_et || got_tab || | |
722 (num_spaces + num_tabs < len)) | |
723 { | |
724 if (did_undo == FALSE) | |
725 { | |
726 did_undo = TRUE; | |
727 if (u_save((linenr_T)(lnum - 1), | |
728 (linenr_T)(lnum + 1)) == FAIL) | |
729 { | |
730 new_line = NULL; /* flag out-of-memory */ | |
731 break; | |
732 } | |
733 } | |
734 | |
735 /* len is actual number of white characters used */ | |
736 len = num_spaces + num_tabs; | |
737 old_len = (long)STRLEN(ptr); | |
738 new_line = lalloc(old_len - col + start_col + len + 1, | |
739 TRUE); | |
740 if (new_line == NULL) | |
741 break; | |
742 if (start_col > 0) | |
743 mch_memmove(new_line, ptr, (size_t)start_col); | |
744 mch_memmove(new_line + start_col + len, | |
745 ptr + col, (size_t)(old_len - col + 1)); | |
746 ptr = new_line + start_col; | |
747 for (col = 0; col < len; col++) | |
748 ptr[col] = (col < num_tabs) ? '\t' : ' '; | |
749 ml_replace(lnum, new_line, FALSE); | |
750 if (first_line == 0) | |
751 first_line = lnum; | |
752 last_line = lnum; | |
753 ptr = new_line; | |
754 col = start_col + len; | |
755 } | |
756 } | |
757 got_tab = FALSE; | |
758 num_spaces = 0; | |
759 } | |
760 if (ptr[col] == NUL) | |
761 break; | |
762 vcol += chartabsize(ptr + col, (colnr_T)vcol); | |
763 #ifdef FEAT_MBYTE | |
764 if (has_mbyte) | |
474 | 765 col += (*mb_ptr2len)(ptr + col); |
7 | 766 else |
767 #endif | |
768 ++col; | |
769 } | |
770 if (new_line == NULL) /* out of memory */ | |
771 break; | |
772 line_breakcheck(); | |
773 } | |
774 if (got_int) | |
775 EMSG(_(e_interr)); | |
776 | |
777 if (curbuf->b_p_ts != new_ts) | |
778 redraw_curbuf_later(NOT_VALID); | |
779 if (first_line != 0) | |
780 changed_lines(first_line, 0, last_line + 1, 0L); | |
781 | |
782 curwin->w_p_list = save_list; /* restore 'list' */ | |
783 | |
784 curbuf->b_p_ts = new_ts; | |
785 coladvance(curwin->w_curswant); | |
786 | |
787 u_clearline(); | |
788 } | |
789 #endif | |
790 | |
791 /* | |
792 * :move command - move lines line1-line2 to line dest | |
793 * | |
794 * return FAIL for failure, OK otherwise | |
795 */ | |
796 int | |
797 do_move(line1, line2, dest) | |
798 linenr_T line1; | |
799 linenr_T line2; | |
800 linenr_T dest; | |
801 { | |
802 char_u *str; | |
803 linenr_T l; | |
804 linenr_T extra; /* Num lines added before line1 */ | |
805 linenr_T num_lines; /* Num lines moved */ | |
806 linenr_T last_line; /* Last line in file after adding new text */ | |
6755 | 807 #ifdef FEAT_FOLDING |
808 int isFolded; | |
809 | |
810 /* Moving lines seems to corrupt the folds, delete folding info now | |
811 * and recreate it when finished. Don't do this for manual folding, it | |
812 * would delete all folds. */ | |
813 isFolded = hasAnyFolding(curwin) && !foldmethodIsManual(curwin); | |
814 if (isFolded) | |
815 deleteFoldRecurse(&curwin->w_folds); | |
816 #endif | |
7 | 817 |
818 if (dest >= line1 && dest < line2) | |
819 { | |
820 EMSG(_("E134: Move lines into themselves")); | |
821 return FAIL; | |
822 } | |
823 | |
824 num_lines = line2 - line1 + 1; | |
825 | |
826 /* | |
827 * First we copy the old text to its new location -- webb | |
828 * Also copy the flag that ":global" command uses. | |
829 */ | |
830 if (u_save(dest, dest + 1) == FAIL) | |
831 return FAIL; | |
832 for (extra = 0, l = line1; l <= line2; l++) | |
833 { | |
834 str = vim_strsave(ml_get(l + extra)); | |
835 if (str != NULL) | |
836 { | |
837 ml_append(dest + l - line1, str, (colnr_T)0, FALSE); | |
838 vim_free(str); | |
839 if (dest < line1) | |
840 extra++; | |
841 } | |
842 } | |
843 | |
844 /* | |
845 * Now we must be careful adjusting our marks so that we don't overlap our | |
846 * mark_adjust() calls. | |
847 * | |
848 * We adjust the marks within the old text so that they refer to the | |
849 * last lines of the file (temporarily), because we know no other marks | |
850 * will be set there since these line numbers did not exist until we added | |
851 * our new lines. | |
852 * | |
853 * Then we adjust the marks on lines between the old and new text positions | |
854 * (either forwards or backwards). | |
855 * | |
856 * And Finally we adjust the marks we put at the end of the file back to | |
857 * their final destination at the new text position -- webb | |
858 */ | |
859 last_line = curbuf->b_ml.ml_line_count; | |
860 mark_adjust(line1, line2, last_line - line2, 0L); | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
861 changed_lines(last_line - num_lines + 1, 0, last_line + 1, num_lines); |
7 | 862 if (dest >= line2) |
863 { | |
864 mark_adjust(line2 + 1, dest, -num_lines, 0L); | |
865 curbuf->b_op_start.lnum = dest - num_lines + 1; | |
866 curbuf->b_op_end.lnum = dest; | |
867 } | |
868 else | |
869 { | |
870 mark_adjust(dest + 1, line1 - 1, num_lines, 0L); | |
871 curbuf->b_op_start.lnum = dest + 1; | |
872 curbuf->b_op_end.lnum = dest + num_lines; | |
873 } | |
874 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; | |
875 mark_adjust(last_line - num_lines + 1, last_line, | |
876 -(last_line - dest - extra), 0L); | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
877 changed_lines(last_line - num_lines + 1, 0, last_line + 1, -extra); |
7 | 878 |
879 /* | |
880 * Now we delete the original text -- webb | |
881 */ | |
882 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL) | |
883 return FAIL; | |
884 | |
885 for (l = line1; l <= line2; l++) | |
886 ml_delete(line1 + extra, TRUE); | |
887 | |
888 if (!global_busy && num_lines > p_report) | |
889 { | |
890 if (num_lines == 1) | |
891 MSG(_("1 line moved")); | |
892 else | |
893 smsg((char_u *)_("%ld lines moved"), num_lines); | |
894 } | |
895 | |
896 /* | |
897 * Leave the cursor on the last of the moved lines. | |
898 */ | |
899 if (dest >= line1) | |
900 curwin->w_cursor.lnum = dest; | |
901 else | |
902 curwin->w_cursor.lnum = dest + (line2 - line1) + 1; | |
903 | |
904 if (line1 < dest) | |
3184 | 905 { |
906 dest += num_lines + 1; | |
907 last_line = curbuf->b_ml.ml_line_count; | |
908 if (dest > last_line + 1) | |
909 dest = last_line + 1; | |
910 changed_lines(line1, 0, dest, 0L); | |
911 } | |
7 | 912 else |
913 changed_lines(dest + 1, 0, line1 + num_lines, 0L); | |
914 | |
6755 | 915 #ifdef FEAT_FOLDING |
916 /* recreate folds */ | |
917 if (isFolded) | |
918 foldUpdateAll(curwin); | |
919 #endif | |
920 | |
7 | 921 return OK; |
922 } | |
923 | |
924 /* | |
925 * ":copy" | |
926 */ | |
927 void | |
928 ex_copy(line1, line2, n) | |
929 linenr_T line1; | |
930 linenr_T line2; | |
931 linenr_T n; | |
932 { | |
933 linenr_T count; | |
934 char_u *p; | |
935 | |
936 count = line2 - line1 + 1; | |
937 curbuf->b_op_start.lnum = n + 1; | |
938 curbuf->b_op_end.lnum = n + count; | |
939 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; | |
940 | |
941 /* | |
942 * there are three situations: | |
943 * 1. destination is above line1 | |
944 * 2. destination is between line1 and line2 | |
945 * 3. destination is below line2 | |
946 * | |
947 * n = destination (when starting) | |
948 * curwin->w_cursor.lnum = destination (while copying) | |
949 * line1 = start of source (while copying) | |
950 * line2 = end of source (while copying) | |
951 */ | |
952 if (u_save(n, n + 1) == FAIL) | |
953 return; | |
954 | |
955 curwin->w_cursor.lnum = n; | |
956 while (line1 <= line2) | |
957 { | |
958 /* need to use vim_strsave() because the line will be unlocked within | |
959 * ml_append() */ | |
960 p = vim_strsave(ml_get(line1)); | |
961 if (p != NULL) | |
962 { | |
963 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE); | |
964 vim_free(p); | |
965 } | |
966 /* situation 2: skip already copied lines */ | |
967 if (line1 == n) | |
968 line1 = curwin->w_cursor.lnum; | |
969 ++line1; | |
970 if (curwin->w_cursor.lnum < line1) | |
971 ++line1; | |
972 if (curwin->w_cursor.lnum < line2) | |
973 ++line2; | |
974 ++curwin->w_cursor.lnum; | |
975 } | |
976 | |
977 appended_lines_mark(n, count); | |
978 | |
979 msgmore((long)count); | |
980 } | |
981 | |
359 | 982 static char_u *prevcmd = NULL; /* the previous command */ |
983 | |
984 #if defined(EXITFREE) || defined(PROTO) | |
985 void | |
986 free_prev_shellcmd() | |
987 { | |
988 vim_free(prevcmd); | |
989 } | |
990 #endif | |
991 | |
7 | 992 /* |
993 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd" | |
994 * Bangs in the argument are replaced with the previously entered command. | |
995 * Remember the argument. | |
996 */ | |
997 void | |
998 do_bang(addr_count, eap, forceit, do_in, do_out) | |
999 int addr_count; | |
1000 exarg_T *eap; | |
1001 int forceit; | |
1002 int do_in, do_out; | |
1003 { | |
1004 char_u *arg = eap->arg; /* command */ | |
1005 linenr_T line1 = eap->line1; /* start of range */ | |
1006 linenr_T line2 = eap->line2; /* end of range */ | |
1007 char_u *newcmd = NULL; /* the new command */ | |
1008 int free_newcmd = FALSE; /* need to free() newcmd */ | |
1009 int ins_prevcmd; | |
1010 char_u *t; | |
1011 char_u *p; | |
1012 char_u *trailarg; | |
1013 int len; | |
1014 int scroll_save = msg_scroll; | |
1015 | |
1016 /* | |
1017 * Disallow shell commands for "rvim". | |
1018 * Disallow shell commands from .exrc and .vimrc in current directory for | |
1019 * security reasons. | |
1020 */ | |
1021 if (check_restricted() || check_secure()) | |
1022 return; | |
1023 | |
1024 if (addr_count == 0) /* :! */ | |
1025 { | |
1026 msg_scroll = FALSE; /* don't scroll here */ | |
1027 autowrite_all(); | |
1028 msg_scroll = scroll_save; | |
1029 } | |
1030 | |
1031 /* | |
1032 * Try to find an embedded bang, like in :!<cmd> ! [args] | |
1033 * (:!! is indicated by the 'forceit' variable) | |
1034 */ | |
1035 ins_prevcmd = forceit; | |
1036 trailarg = arg; | |
1037 do | |
1038 { | |
1039 len = (int)STRLEN(trailarg) + 1; | |
1040 if (newcmd != NULL) | |
1041 len += (int)STRLEN(newcmd); | |
1042 if (ins_prevcmd) | |
1043 { | |
1044 if (prevcmd == NULL) | |
1045 { | |
1046 EMSG(_(e_noprev)); | |
1047 vim_free(newcmd); | |
1048 return; | |
1049 } | |
1050 len += (int)STRLEN(prevcmd); | |
1051 } | |
1872 | 1052 if ((t = alloc((unsigned)len)) == NULL) |
7 | 1053 { |
1054 vim_free(newcmd); | |
1055 return; | |
1056 } | |
1057 *t = NUL; | |
1058 if (newcmd != NULL) | |
1059 STRCAT(t, newcmd); | |
1060 if (ins_prevcmd) | |
1061 STRCAT(t, prevcmd); | |
1062 p = t + STRLEN(t); | |
1063 STRCAT(t, trailarg); | |
1064 vim_free(newcmd); | |
1065 newcmd = t; | |
1066 | |
1067 /* | |
1068 * Scan the rest of the argument for '!', which is replaced by the | |
1069 * previous command. "\!" is replaced by "!" (this is vi compatible). | |
1070 */ | |
1071 trailarg = NULL; | |
1072 while (*p) | |
1073 { | |
2823 | 1074 if (*p == '!') |
7 | 1075 { |
1076 if (p > newcmd && p[-1] == '\\') | |
1624 | 1077 STRMOVE(p - 1, p); |
7 | 1078 else |
1079 { | |
1080 trailarg = p; | |
1081 *trailarg++ = NUL; | |
1082 ins_prevcmd = TRUE; | |
1083 break; | |
1084 } | |
1085 } | |
1086 ++p; | |
1087 } | |
1088 } while (trailarg != NULL); | |
1089 | |
1090 vim_free(prevcmd); | |
1091 prevcmd = newcmd; | |
1092 | |
1093 if (bangredo) /* put cmd in redo buffer for ! command */ | |
1094 { | |
5728 | 1095 /* If % or # appears in the command, it must have been escaped. |
1096 * Reescape them, so that redoing them does not substitute them by the | |
1097 * buffername. */ | |
1098 char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#"); | |
1099 | |
1100 if (cmd != NULL) | |
1101 { | |
1102 AppendToRedobuffLit(cmd, -1); | |
1103 vim_free(cmd); | |
1104 } | |
1105 else | |
1106 AppendToRedobuffLit(prevcmd, -1); | |
7 | 1107 AppendToRedobuff((char_u *)"\n"); |
1108 bangredo = FALSE; | |
1109 } | |
1110 /* | |
1111 * Add quotes around the command, for shells that need them. | |
1112 */ | |
1113 if (*p_shq != NUL) | |
1114 { | |
1115 newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1)); | |
1116 if (newcmd == NULL) | |
1117 return; | |
1118 STRCPY(newcmd, p_shq); | |
1119 STRCAT(newcmd, prevcmd); | |
1120 STRCAT(newcmd, p_shq); | |
1121 free_newcmd = TRUE; | |
1122 } | |
1123 if (addr_count == 0) /* :! */ | |
1124 { | |
1125 /* echo the command */ | |
1126 msg_start(); | |
1127 msg_putchar(':'); | |
1128 msg_putchar('!'); | |
1129 msg_outtrans(newcmd); | |
1130 msg_clr_eos(); | |
1131 windgoto(msg_row, msg_col); | |
1132 | |
1133 do_shell(newcmd, 0); | |
1134 } | |
1135 else /* :range! */ | |
725 | 1136 { |
7 | 1137 /* Careful: This may recursively call do_bang() again! (because of |
1138 * autocommands) */ | |
1139 do_filter(line1, line2, eap, newcmd, do_in, do_out); | |
725 | 1140 #ifdef FEAT_AUTOCMD |
1141 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf); | |
1142 #endif | |
1143 } | |
7 | 1144 if (free_newcmd) |
1145 vim_free(newcmd); | |
1146 } | |
1147 | |
1148 /* | |
1149 * do_filter: filter lines through a command given by the user | |
1150 * | |
169 | 1151 * We mostly use temp files and the call_shell() routine here. This would |
1152 * normally be done using pipes on a UNIX machine, but this is more portable | |
1153 * to non-unix machines. The call_shell() routine needs to be able | |
7 | 1154 * to deal with redirection somehow, and should handle things like looking |
1155 * at the PATH env. variable, and adding reasonable extensions to the | |
1156 * command name given by the user. All reasonable versions of call_shell() | |
1157 * do this. | |
169 | 1158 * Alternatively, if on Unix and redirecting input or output, but not both, |
1159 * and the 'shelltemp' option isn't set, use pipes. | |
7 | 1160 * We use input redirection if do_in is TRUE. |
1161 * We use output redirection if do_out is TRUE. | |
1162 */ | |
1163 static void | |
1164 do_filter(line1, line2, eap, cmd, do_in, do_out) | |
1165 linenr_T line1, line2; | |
1166 exarg_T *eap; /* for forced 'ff' and 'fenc' */ | |
1167 char_u *cmd; | |
1168 int do_in, do_out; | |
1169 { | |
1170 char_u *itmp = NULL; | |
1171 char_u *otmp = NULL; | |
1172 linenr_T linecount; | |
1173 linenr_T read_linecount; | |
1174 pos_T cursor_save; | |
1175 char_u *cmd_buf; | |
1176 #ifdef FEAT_AUTOCMD | |
1177 buf_T *old_curbuf = curbuf; | |
1178 #endif | |
169 | 1179 int shell_flags = 0; |
7 | 1180 |
1181 if (*cmd == NUL) /* no filter command */ | |
1182 return; | |
1183 | |
1184 #ifdef WIN3264 | |
1185 /* | |
1186 * Check if external commands are allowed now. | |
1187 */ | |
1188 if (can_end_termcap_mode(TRUE) == FALSE) | |
1189 return; | |
1190 #endif | |
1191 | |
1192 cursor_save = curwin->w_cursor; | |
1193 linecount = line2 - line1 + 1; | |
1194 curwin->w_cursor.lnum = line1; | |
1195 curwin->w_cursor.col = 0; | |
1196 changed_line_abv_curs(); | |
1197 invalidate_botline(); | |
1198 | |
1199 /* | |
169 | 1200 * When using temp files: |
1201 * 1. * Form temp file names | |
1202 * 2. * Write the lines to a temp file | |
1203 * 3. Run the filter command on the temp file | |
1204 * 4. * Read the output of the command into the buffer | |
1205 * 5. * Delete the original lines to be filtered | |
1206 * 6. * Remove the temp files | |
1207 * | |
1208 * When writing the input with a pipe or when catching the output with a | |
1209 * pipe only need to do 3. | |
7 | 1210 */ |
1211 | |
169 | 1212 if (do_out) |
1213 shell_flags |= SHELL_DOOUT; | |
1214 | |
3482 | 1215 #ifdef FEAT_FILTERPIPE |
169 | 1216 if (!do_in && do_out && !p_stmp) |
1217 { | |
1218 /* Use a pipe to fetch stdout of the command, do not use a temp file. */ | |
1219 shell_flags |= SHELL_READ; | |
1220 curwin->w_cursor.lnum = line2; | |
1221 } | |
1222 else if (do_in && !do_out && !p_stmp) | |
7 | 1223 { |
169 | 1224 /* Use a pipe to write stdin of the command, do not use a temp file. */ |
1225 shell_flags |= SHELL_WRITE; | |
1226 curbuf->b_op_start.lnum = line1; | |
1227 curbuf->b_op_end.lnum = line2; | |
7 | 1228 } |
169 | 1229 else if (do_in && do_out && !p_stmp) |
1230 { | |
1231 /* Use a pipe to write stdin and fetch stdout of the command, do not | |
1232 * use a temp file. */ | |
1233 shell_flags |= SHELL_READ|SHELL_WRITE; | |
1234 curbuf->b_op_start.lnum = line1; | |
1235 curbuf->b_op_end.lnum = line2; | |
1236 curwin->w_cursor.lnum = line2; | |
1237 } | |
1238 else | |
1239 #endif | |
6721 | 1240 if ((do_in && (itmp = vim_tempname('i', FALSE)) == NULL) |
1241 || (do_out && (otmp = vim_tempname('o', FALSE)) == NULL)) | |
169 | 1242 { |
1243 EMSG(_(e_notmp)); | |
1244 goto filterend; | |
1245 } | |
7 | 1246 |
1247 /* | |
1248 * The writing and reading of temp files will not be shown. | |
1249 * Vi also doesn't do this and the messages are not very informative. | |
1250 */ | |
1251 ++no_wait_return; /* don't call wait_return() while busy */ | |
169 | 1252 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap, |
7 | 1253 FALSE, FALSE, FALSE, TRUE) == FAIL) |
1254 { | |
1255 msg_putchar('\n'); /* keep message from buf_write() */ | |
1256 --no_wait_return; | |
1257 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
1258 if (!aborting()) | |
1259 #endif | |
1260 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */ | |
1261 goto filterend; | |
1262 } | |
1263 #ifdef FEAT_AUTOCMD | |
1264 if (curbuf != old_curbuf) | |
1265 goto filterend; | |
1266 #endif | |
1267 | |
1268 if (!do_out) | |
1269 msg_putchar('\n'); | |
1270 | |
1581 | 1271 /* Create the shell command in allocated memory. */ |
7 | 1272 cmd_buf = make_filter_cmd(cmd, itmp, otmp); |
1273 if (cmd_buf == NULL) | |
1274 goto filterend; | |
1275 | |
1276 windgoto((int)Rows - 1, 0); | |
1277 cursor_on(); | |
1278 | |
1279 /* | |
1280 * When not redirecting the output the command can write anything to the | |
1281 * screen. If 'shellredir' is equal to ">", screen may be messed up by | |
1282 * stderr output of external command. Clear the screen later. | |
1283 * If do_in is FALSE, this could be something like ":r !cat", which may | |
1284 * also mess up the screen, clear it later. | |
1285 */ | |
1286 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in) | |
1287 redraw_later_clear(); | |
1288 | |
169 | 1289 if (do_out) |
1290 { | |
1291 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL) | |
1581 | 1292 { |
1293 vim_free(cmd_buf); | |
169 | 1294 goto error; |
1581 | 1295 } |
169 | 1296 redraw_curbuf_later(VALID); |
1297 } | |
1298 read_linecount = curbuf->b_ml.ml_line_count; | |
1299 | |
7 | 1300 /* |
1301 * When call_shell() fails wait_return() is called to give the user a | |
1302 * chance to read the error messages. Otherwise errors are ignored, so you | |
1303 * can see the error messages from the command that appear on stdout; use | |
1304 * 'u' to fix the text | |
1305 * Switch to cooked mode when not redirecting stdin, avoids that something | |
1306 * like ":r !cat" hangs. | |
1307 * Pass on the SHELL_DOOUT flag when the output is being redirected. | |
1308 */ | |
169 | 1309 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags)) |
7 | 1310 { |
1311 redraw_later_clear(); | |
1312 wait_return(FALSE); | |
1313 } | |
1314 vim_free(cmd_buf); | |
1315 | |
1316 did_check_timestamps = FALSE; | |
1317 need_check_timestamps = TRUE; | |
1318 | |
1319 /* When interrupting the shell command, it may still have produced some | |
1320 * useful output. Reset got_int here, so that readfile() won't cancel | |
1321 * reading. */ | |
1322 ui_breakcheck(); | |
1323 got_int = FALSE; | |
1324 | |
1325 if (do_out) | |
1326 { | |
169 | 1327 if (otmp != NULL) |
7 | 1328 { |
169 | 1329 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM, |
1330 eap, READ_FILTER) == FAIL) | |
1331 { | |
7 | 1332 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
169 | 1333 if (!aborting()) |
1334 #endif | |
1335 { | |
1336 msg_putchar('\n'); | |
1337 EMSG2(_(e_notread), otmp); | |
1338 } | |
1339 goto error; | |
7 | 1340 } |
169 | 1341 #ifdef FEAT_AUTOCMD |
1342 if (curbuf != old_curbuf) | |
1343 goto filterend; | |
1344 #endif | |
7 | 1345 } |
169 | 1346 |
1347 read_linecount = curbuf->b_ml.ml_line_count - read_linecount; | |
1348 | |
1349 if (shell_flags & SHELL_READ) | |
1350 { | |
1351 curbuf->b_op_start.lnum = line2 + 1; | |
1352 curbuf->b_op_end.lnum = curwin->w_cursor.lnum; | |
1353 appended_lines_mark(line2, read_linecount); | |
1354 } | |
7 | 1355 |
1356 if (do_in) | |
1357 { | |
1358 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL) | |
1359 { | |
1360 if (read_linecount >= linecount) | |
1361 /* move all marks from old lines to new lines */ | |
1362 mark_adjust(line1, line2, linecount, 0L); | |
1363 else | |
1364 { | |
1365 /* move marks from old lines to new lines, delete marks | |
1366 * that are in deleted lines */ | |
1367 mark_adjust(line1, line1 + read_linecount - 1, | |
1368 linecount, 0L); | |
1369 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L); | |
1370 } | |
1371 } | |
1372 | |
1373 /* | |
1374 * Put cursor on first filtered line for ":range!cmd". | |
1375 * Adjust '[ and '] (set by buf_write()). | |
1376 */ | |
1377 curwin->w_cursor.lnum = line1; | |
1378 del_lines(linecount, TRUE); | |
1379 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */ | |
1380 curbuf->b_op_end.lnum -= linecount; /* adjust '] */ | |
1381 write_lnum_adjust(-linecount); /* adjust last line | |
1382 for next write */ | |
100 | 1383 #ifdef FEAT_FOLDING |
1384 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum); | |
1385 #endif | |
7 | 1386 } |
1387 else | |
1388 { | |
1389 /* | |
1390 * Put cursor on last new line for ":r !cmd". | |
1391 */ | |
169 | 1392 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1; |
7 | 1393 curwin->w_cursor.lnum = curbuf->b_op_end.lnum; |
1394 } | |
100 | 1395 |
7 | 1396 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */ |
1397 --no_wait_return; | |
1398 | |
1399 if (linecount > p_report) | |
1400 { | |
1401 if (do_in) | |
1402 { | |
274 | 1403 vim_snprintf((char *)msg_buf, sizeof(msg_buf), |
1404 _("%ld lines filtered"), (long)linecount); | |
7 | 1405 if (msg(msg_buf) && !msg_scroll) |
1406 /* save message to display it after redraw */ | |
680 | 1407 set_keep_msg(msg_buf, 0); |
7 | 1408 } |
1409 else | |
1410 msgmore((long)linecount); | |
1411 } | |
1412 } | |
1413 else | |
1414 { | |
1415 error: | |
1416 /* put cursor back in same position for ":w !cmd" */ | |
1417 curwin->w_cursor = cursor_save; | |
1418 --no_wait_return; | |
1419 wait_return(FALSE); | |
1420 } | |
1421 | |
1422 filterend: | |
1423 | |
1424 #ifdef FEAT_AUTOCMD | |
1425 if (curbuf != old_curbuf) | |
1426 { | |
1427 --no_wait_return; | |
1428 EMSG(_("E135: *Filter* Autocommands must not change current buffer")); | |
1429 } | |
1430 #endif | |
1431 if (itmp != NULL) | |
1432 mch_remove(itmp); | |
1433 if (otmp != NULL) | |
1434 mch_remove(otmp); | |
1435 vim_free(itmp); | |
1436 vim_free(otmp); | |
1437 } | |
1438 | |
1439 /* | |
1440 * Call a shell to execute a command. | |
1441 * When "cmd" is NULL start an interactive shell. | |
1442 */ | |
1443 void | |
1444 do_shell(cmd, flags) | |
1445 char_u *cmd; | |
1446 int flags; /* may be SHELL_DOOUT when output is redirected */ | |
1447 { | |
1448 buf_T *buf; | |
1449 #ifndef FEAT_GUI_MSWIN | |
1450 int save_nwr; | |
1451 #endif | |
1452 #ifdef MSWIN | |
1453 int winstart = FALSE; | |
1454 #endif | |
1455 | |
1456 /* | |
1457 * Disallow shell commands for "rvim". | |
1458 * Disallow shell commands from .exrc and .vimrc in current directory for | |
1459 * security reasons. | |
1460 */ | |
1461 if (check_restricted() || check_secure()) | |
1462 { | |
1463 msg_end(); | |
1464 return; | |
1465 } | |
1466 | |
1467 #ifdef MSWIN | |
1468 /* | |
1469 * Check if external commands are allowed now. | |
1470 */ | |
1471 if (can_end_termcap_mode(TRUE) == FALSE) | |
1472 return; | |
1473 | |
1474 /* | |
1475 * Check if ":!start" is used. | |
1476 */ | |
1477 if (cmd != NULL) | |
1478 winstart = (STRNICMP(cmd, "start ", 6) == 0); | |
1479 #endif | |
1480 | |
1481 /* | |
1482 * For autocommands we want to get the output on the current screen, to | |
1483 * avoid having to type return below. | |
1484 */ | |
1485 msg_putchar('\r'); /* put cursor at start of line */ | |
1486 #ifdef FEAT_AUTOCMD | |
1487 if (!autocmd_busy) | |
1488 #endif | |
1489 { | |
1490 #ifdef MSWIN | |
1491 if (!winstart) | |
1492 #endif | |
1493 stoptermcap(); | |
1494 } | |
1495 #ifdef MSWIN | |
1496 if (!winstart) | |
1497 #endif | |
1498 msg_putchar('\n'); /* may shift screen one line up */ | |
1499 | |
1500 /* warning message before calling the shell */ | |
1501 if (p_warn | |
1502 #ifdef FEAT_AUTOCMD | |
1503 && !autocmd_busy | |
1504 #endif | |
1505 && msg_silent == 0) | |
1506 for (buf = firstbuf; buf; buf = buf->b_next) | |
1507 if (bufIsChanged(buf)) | |
1508 { | |
1509 #ifdef FEAT_GUI_MSWIN | |
1510 if (!winstart) | |
1511 starttermcap(); /* don't want a message box here */ | |
1512 #endif | |
1513 MSG_PUTS(_("[No write since last change]\n")); | |
1514 #ifdef FEAT_GUI_MSWIN | |
1515 if (!winstart) | |
1516 stoptermcap(); | |
1517 #endif | |
1518 break; | |
1519 } | |
1520 | |
1521 /* This windgoto is required for when the '\n' resulted in a "delete line | |
1522 * 1" command to the terminal. */ | |
1523 if (!swapping_screen()) | |
1524 windgoto(msg_row, msg_col); | |
1525 cursor_on(); | |
1526 (void)call_shell(cmd, SHELL_COOKED | flags); | |
1527 did_check_timestamps = FALSE; | |
1528 need_check_timestamps = TRUE; | |
1529 | |
1530 /* | |
1531 * put the message cursor at the end of the screen, avoids wait_return() | |
1532 * to overwrite the text that the external command showed | |
1533 */ | |
1534 if (!swapping_screen()) | |
1535 { | |
1536 msg_row = Rows - 1; | |
1537 msg_col = 0; | |
1538 } | |
1539 | |
1540 #ifdef FEAT_AUTOCMD | |
1541 if (autocmd_busy) | |
1542 { | |
1543 if (msg_silent == 0) | |
1544 redraw_later_clear(); | |
1545 } | |
1546 else | |
1547 #endif | |
1548 { | |
1549 /* | |
1550 * For ":sh" there is no need to call wait_return(), just redraw. | |
1551 * Also for the Win32 GUI (the output is in a console window). | |
1552 * Otherwise there is probably text on the screen that the user wants | |
1553 * to read before redrawing, so call wait_return(). | |
1554 */ | |
1555 #ifndef FEAT_GUI_MSWIN | |
1556 if (cmd == NULL | |
1557 # ifdef WIN3264 | |
1558 || (winstart && !need_wait_return) | |
1559 # endif | |
1560 ) | |
1561 { | |
1562 if (msg_silent == 0) | |
1563 redraw_later_clear(); | |
1564 need_wait_return = FALSE; | |
1565 } | |
1566 else | |
1567 { | |
1568 /* | |
1569 * If we switch screens when starttermcap() is called, we really | |
1570 * want to wait for "hit return to continue". | |
1571 */ | |
1572 save_nwr = no_wait_return; | |
1573 if (swapping_screen()) | |
1574 no_wait_return = FALSE; | |
1575 # ifdef AMIGA | |
1576 wait_return(term_console ? -1 : msg_silent == 0); /* see below */ | |
1577 # else | |
1578 wait_return(msg_silent == 0); | |
1579 # endif | |
1580 no_wait_return = save_nwr; | |
1581 } | |
1582 #endif /* FEAT_GUI_W32 */ | |
1583 | |
1584 #ifdef MSWIN | |
1585 if (!winstart) /* if winstart==TRUE, never stopped termcap! */ | |
1586 #endif | |
1587 starttermcap(); /* start termcap if not done by wait_return() */ | |
1588 | |
1589 /* | |
1590 * In an Amiga window redrawing is caused by asking the window size. | |
1591 * If we got an interrupt this will not work. The chance that the | |
1592 * window size is wrong is very small, but we need to redraw the | |
1593 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY | |
1594 * but it saves an extra redraw. | |
1595 */ | |
1596 #ifdef AMIGA | |
1597 if (skip_redraw) /* ':' hit in wait_return() */ | |
1598 { | |
1599 if (msg_silent == 0) | |
1600 redraw_later_clear(); | |
1601 } | |
1602 else if (term_console) | |
1603 { | |
1604 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */ | |
1605 if (got_int && msg_silent == 0) | |
1606 redraw_later_clear(); /* if got_int is TRUE, redraw needed */ | |
1607 else | |
1608 must_redraw = 0; /* no extra redraw needed */ | |
1609 } | |
1610 #endif | |
1611 } | |
1612 | |
1613 /* display any error messages now */ | |
1614 display_errors(); | |
725 | 1615 |
1616 #ifdef FEAT_AUTOCMD | |
1617 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf); | |
1618 #endif | |
7 | 1619 } |
1620 | |
1621 /* | |
1622 * Create a shell command from a command string, input redirection file and | |
1623 * output redirection file. | |
1624 * Returns an allocated string with the shell command, or NULL for failure. | |
1625 */ | |
1626 char_u * | |
1627 make_filter_cmd(cmd, itmp, otmp) | |
1628 char_u *cmd; /* command */ | |
1629 char_u *itmp; /* NULL or name of input file */ | |
1630 char_u *otmp; /* NULL or name of output file */ | |
1631 { | |
1632 char_u *buf; | |
1633 long_u len; | |
5867 | 1634 |
7410
08e62c4fc17d
commit https://github.com/vim/vim/commit/53076830fea6df737455523f7e235bfe4f79864d
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
1635 #if defined(UNIX) |
5881 | 1636 int is_fish_shell; |
5911 | 1637 char_u *shell_name = get_isolated_shell_name(); |
5881 | 1638 |
5867 | 1639 /* Account for fish's different syntax for subshells */ |
5911 | 1640 is_fish_shell = (fnamecmp(shell_name, "fish") == 0); |
1641 vim_free(shell_name); | |
5867 | 1642 if (is_fish_shell) |
1643 len = (long_u)STRLEN(cmd) + 13; /* "begin; " + "; end" + NUL */ | |
1644 else | |
1645 #endif | |
1646 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */ | |
7 | 1647 if (itmp != NULL) |
1648 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */ | |
1649 if (otmp != NULL) | |
1650 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */ | |
1651 buf = lalloc(len, TRUE); | |
1652 if (buf == NULL) | |
1653 return NULL; | |
1654 | |
7410
08e62c4fc17d
commit https://github.com/vim/vim/commit/53076830fea6df737455523f7e235bfe4f79864d
Christian Brabandt <cb@256bit.org>
parents:
7408
diff
changeset
|
1655 #if defined(UNIX) |
7 | 1656 /* |
169 | 1657 * Put braces around the command (for concatenated commands) when |
1658 * redirecting input and/or output. | |
7 | 1659 */ |
169 | 1660 if (itmp != NULL || otmp != NULL) |
5867 | 1661 { |
1662 if (is_fish_shell) | |
1663 vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd); | |
1664 else | |
1665 vim_snprintf((char *)buf, len, "(%s)", (char *)cmd); | |
1666 } | |
169 | 1667 else |
1668 STRCPY(buf, cmd); | |
7 | 1669 if (itmp != NULL) |
1670 { | |
1671 STRCAT(buf, " < "); | |
1672 STRCAT(buf, itmp); | |
1673 } | |
1674 #else | |
1675 /* | |
5867 | 1676 * For shells that don't understand braces around commands, at least allow |
7 | 1677 * the use of commands in a pipe. |
1678 */ | |
1679 STRCPY(buf, cmd); | |
1680 if (itmp != NULL) | |
1681 { | |
1682 char_u *p; | |
1683 | |
1684 /* | |
1685 * If there is a pipe, we have to put the '<' in front of it. | |
1686 * Don't do this when 'shellquote' is not empty, otherwise the | |
1687 * redirection would be inside the quotes. | |
1688 */ | |
1689 if (*p_shq == NUL) | |
1690 { | |
1691 p = vim_strchr(buf, '|'); | |
1692 if (p != NULL) | |
1693 *p = NUL; | |
1694 } | |
1695 STRCAT(buf, " <"); /* " < " causes problems on Amiga */ | |
1696 STRCAT(buf, itmp); | |
1697 if (*p_shq == NUL) | |
1698 { | |
1699 p = vim_strchr(cmd, '|'); | |
1700 if (p != NULL) | |
1701 { | |
1702 STRCAT(buf, " "); /* insert a space before the '|' for DOS */ | |
1703 STRCAT(buf, p); | |
1704 } | |
1705 } | |
1706 } | |
1707 #endif | |
1708 if (otmp != NULL) | |
1872 | 1709 append_redir(buf, (int)len, p_srr, otmp); |
7 | 1710 |
1711 return buf; | |
1712 } | |
1713 | |
1714 /* | |
1872 | 1715 * Append output redirection for file "fname" to the end of string buffer |
1716 * "buf[buflen]" | |
7 | 1717 * Works with the 'shellredir' and 'shellpipe' options. |
1718 * The caller should make sure that there is enough room: | |
1719 * STRLEN(opt) + STRLEN(fname) + 3 | |
1720 */ | |
1721 void | |
1872 | 1722 append_redir(buf, buflen, opt, fname) |
7 | 1723 char_u *buf; |
1872 | 1724 int buflen; |
7 | 1725 char_u *opt; |
1726 char_u *fname; | |
1727 { | |
1728 char_u *p; | |
1872 | 1729 char_u *end; |
1730 | |
1731 end = buf + STRLEN(buf); | |
5257
e63e4b4be923
updated for version 7.4b.005
Bram Moolenaar <bram@vim.org>
parents:
5242
diff
changeset
|
1732 /* find "%s" */ |
7 | 1733 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p) |
5257
e63e4b4be923
updated for version 7.4b.005
Bram Moolenaar <bram@vim.org>
parents:
5242
diff
changeset
|
1734 { |
e63e4b4be923
updated for version 7.4b.005
Bram Moolenaar <bram@vim.org>
parents:
5242
diff
changeset
|
1735 if (p[1] == 's') /* found %s */ |
7 | 1736 break; |
5257
e63e4b4be923
updated for version 7.4b.005
Bram Moolenaar <bram@vim.org>
parents:
5242
diff
changeset
|
1737 if (p[1] == '%') /* skip %% */ |
e63e4b4be923
updated for version 7.4b.005
Bram Moolenaar <bram@vim.org>
parents:
5242
diff
changeset
|
1738 ++p; |
e63e4b4be923
updated for version 7.4b.005
Bram Moolenaar <bram@vim.org>
parents:
5242
diff
changeset
|
1739 } |
7 | 1740 if (p != NULL) |
1741 { | |
1872 | 1742 *end = ' '; /* not really needed? Not with sh, ksh or bash */ |
1743 vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)), | |
1744 (char *)opt, (char *)fname); | |
7 | 1745 } |
1746 else | |
1872 | 1747 vim_snprintf((char *)end, (size_t)(buflen - (end - buf)), |
7 | 1748 #ifdef FEAT_QUICKFIX |
1749 " %s %s", | |
1750 #else | |
1751 " %s%s", /* " > %s" causes problems on Amiga */ | |
1752 #endif | |
1753 (char *)opt, (char *)fname); | |
1754 } | |
1755 | |
7664
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1756 #if defined(FEAT_VIMINFO) || defined(PROTO) |
7 | 1757 |
1758 static int no_viminfo __ARGS((void)); | |
7664
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
1759 static void write_viminfo_barlines(vir_T *virp, FILE *fp_out); |
7 | 1760 static int viminfo_errcnt; |
1761 | |
1762 static int | |
1763 no_viminfo() | |
1764 { | |
1765 /* "vim -i NONE" does not read or write a viminfo file */ | |
1766 return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0); | |
1767 } | |
1768 | |
1769 /* | |
1770 * Report an error for reading a viminfo file. | |
1771 * Count the number of errors. When there are more than 10, return TRUE. | |
1772 */ | |
1773 int | |
1774 viminfo_error(errnum, message, line) | |
1775 char *errnum; | |
1776 char *message; | |
1777 char_u *line; | |
1778 { | |
274 | 1779 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "), |
1780 errnum, message); | |
1459 | 1781 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1); |
156 | 1782 if (IObuff[STRLEN(IObuff) - 1] == '\n') |
1783 IObuff[STRLEN(IObuff) - 1] = NUL; | |
7 | 1784 emsg(IObuff); |
1785 if (++viminfo_errcnt >= 10) | |
1786 { | |
1787 EMSG(_("E136: viminfo: Too many errors, skipping rest of file")); | |
1788 return TRUE; | |
1789 } | |
1790 return FALSE; | |
1791 } | |
1792 | |
1793 /* | |
1794 * read_viminfo() -- Read the viminfo file. Registers etc. which are already | |
1733 | 1795 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb |
7 | 1796 */ |
1797 int | |
1733 | 1798 read_viminfo(file, flags) |
1799 char_u *file; /* file name or NULL to use default name */ | |
1800 int flags; /* VIF_WANT_INFO et al. */ | |
7 | 1801 { |
1802 FILE *fp; | |
1803 char_u *fname; | |
1804 | |
1805 if (no_viminfo()) | |
1806 return FAIL; | |
1807 | |
1733 | 1808 fname = viminfo_filename(file); /* get file name in allocated buffer */ |
7 | 1809 if (fname == NULL) |
1810 return FAIL; | |
1811 fp = mch_fopen((char *)fname, READBIN); | |
1812 | |
1813 if (p_verbose > 0) | |
294 | 1814 { |
1815 verbose_enter(); | |
274 | 1816 smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"), |
1817 fname, | |
1733 | 1818 (flags & VIF_WANT_INFO) ? _(" info") : "", |
1819 (flags & VIF_WANT_MARKS) ? _(" marks") : "", | |
1820 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "", | |
274 | 1821 fp == NULL ? _(" FAILED") : ""); |
294 | 1822 verbose_leave(); |
1823 } | |
7 | 1824 |
1825 vim_free(fname); | |
1826 if (fp == NULL) | |
1827 return FAIL; | |
1828 | |
1829 viminfo_errcnt = 0; | |
1733 | 1830 do_viminfo(fp, NULL, flags); |
7 | 1831 |
1832 fclose(fp); | |
1833 return OK; | |
1834 } | |
1835 | |
1836 /* | |
4903
2fc1f3346bfb
updated for version 7.3.1197
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
1837 * Write the viminfo file. The old one is read in first so that effectively a |
2fc1f3346bfb
updated for version 7.3.1197
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
1838 * merge of current info and old info is done. This allows multiple vims to |
2fc1f3346bfb
updated for version 7.3.1197
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
1839 * run simultaneously, without losing any marks etc. |
2fc1f3346bfb
updated for version 7.3.1197
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
1840 * If "forceit" is TRUE, then the old file is not read in, and only internal |
2fc1f3346bfb
updated for version 7.3.1197
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
1841 * info is written to the file. |
7 | 1842 */ |
1843 void | |
1844 write_viminfo(file, forceit) | |
1845 char_u *file; | |
1846 int forceit; | |
1847 { | |
1848 char_u *fname; | |
1849 FILE *fp_in = NULL; /* input viminfo file, if any */ | |
1850 FILE *fp_out = NULL; /* output viminfo file */ | |
1851 char_u *tempname = NULL; /* name of temp viminfo file */ | |
1852 struct stat st_new; /* mch_stat() of potential new file */ | |
1853 char_u *wp; | |
1854 #if defined(UNIX) || defined(VMS) | |
1855 mode_t umask_save; | |
1856 #endif | |
1857 #ifdef UNIX | |
1858 int shortname = FALSE; /* use 8.3 file name */ | |
1859 struct stat st_old; /* mch_stat() of existing viminfo file */ | |
1860 #endif | |
601 | 1861 #ifdef WIN3264 |
7194
272f04b41f51
commit https://github.com/vim/vim/commit/8a52ba791893fd55c5bdf98825c5b3e8892eaa62
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1862 int hidden = FALSE; |
601 | 1863 #endif |
7 | 1864 |
1865 if (no_viminfo()) | |
1866 return; | |
1867 | |
1868 fname = viminfo_filename(file); /* may set to default if NULL */ | |
1869 if (fname == NULL) | |
1870 return; | |
1871 | |
1872 fp_in = mch_fopen((char *)fname, READBIN); | |
1873 if (fp_in == NULL) | |
1874 { | |
1875 /* if it does exist, but we can't read it, don't try writing */ | |
1876 if (mch_stat((char *)fname, &st_new) == 0) | |
1877 goto end; | |
1878 #if defined(UNIX) || defined(VMS) | |
1879 /* | |
1880 * For Unix we create the .viminfo non-accessible for others, | |
1881 * because it may contain text from non-accessible documents. | |
1882 */ | |
1883 umask_save = umask(077); | |
1884 #endif | |
1885 fp_out = mch_fopen((char *)fname, WRITEBIN); | |
1886 #if defined(UNIX) || defined(VMS) | |
1887 (void)umask(umask_save); | |
1888 #endif | |
1889 } | |
1890 else | |
1891 { | |
1892 /* | |
1893 * There is an existing viminfo file. Create a temporary file to | |
1894 * write the new viminfo into, in the same directory as the | |
1895 * existing viminfo file, which will be renamed later. | |
1896 */ | |
1897 #ifdef UNIX | |
1898 /* | |
1899 * For Unix we check the owner of the file. It's not very nice to | |
1900 * overwrite a user's viminfo file after a "su root", with a | |
1901 * viminfo file that the user can't read. | |
1902 */ | |
1869 | 1903 st_old.st_dev = (dev_t)0; |
1438 | 1904 st_old.st_ino = 0; |
7 | 1905 st_old.st_mode = 0600; |
1076 | 1906 if (mch_stat((char *)fname, &st_old) == 0 |
1907 && getuid() != ROOT_UID | |
560 | 1908 && !(st_old.st_uid == getuid() |
7 | 1909 ? (st_old.st_mode & 0200) |
1910 : (st_old.st_gid == getgid() | |
1911 ? (st_old.st_mode & 0020) | |
1912 : (st_old.st_mode & 0002)))) | |
1913 { | |
944 | 1914 int tt = msg_didany; |
7 | 1915 |
1916 /* avoid a wait_return for this message, it's annoying */ | |
1917 EMSG2(_("E137: Viminfo file is not writable: %s"), fname); | |
1918 msg_didany = tt; | |
840 | 1919 fclose(fp_in); |
7 | 1920 goto end; |
1921 } | |
1922 #endif | |
601 | 1923 #ifdef WIN3264 |
1924 /* Get the file attributes of the existing viminfo file. */ | |
7194
272f04b41f51
commit https://github.com/vim/vim/commit/8a52ba791893fd55c5bdf98825c5b3e8892eaa62
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1925 hidden = mch_ishidden(fname); |
601 | 1926 #endif |
7 | 1927 |
1928 /* | |
1929 * Make tempname. | |
1930 * May try twice: Once normal and once with shortname set, just in | |
1931 * case somebody puts his viminfo file in an 8.3 filesystem. | |
1932 */ | |
1933 for (;;) | |
1934 { | |
1935 tempname = buf_modname( | |
1936 #ifdef UNIX | |
1937 shortname, | |
1938 #else | |
1939 # ifdef SHORT_FNAME | |
1940 TRUE, | |
1941 # else | |
1942 # ifdef FEAT_GUI_W32 | |
1943 gui_is_win32s(), | |
1944 # else | |
1945 FALSE, | |
1946 # endif | |
1947 # endif | |
1948 #endif | |
1949 fname, | |
1950 #ifdef VMS | |
1951 (char_u *)"-tmp", | |
1952 #else | |
1953 (char_u *)".tmp", | |
1954 #endif | |
1955 FALSE); | |
1956 if (tempname == NULL) /* out of memory */ | |
1957 break; | |
1958 | |
1959 /* | |
1960 * Check if tempfile already exists. Never overwrite an | |
1961 * existing file! | |
1962 */ | |
1963 if (mch_stat((char *)tempname, &st_new) == 0) | |
1964 { | |
1965 #ifdef UNIX | |
1966 /* | |
1967 * Check if tempfile is same as original file. May happen | |
1968 * when modname() gave the same file back. E.g. silly | |
1969 * link, or file name-length reached. Try again with | |
1970 * shortname set. | |
1971 */ | |
560 | 1972 if (!shortname && st_new.st_dev == st_old.st_dev |
1973 && st_new.st_ino == st_old.st_ino) | |
7 | 1974 { |
1975 vim_free(tempname); | |
1976 tempname = NULL; | |
1977 shortname = TRUE; | |
1978 continue; | |
1979 } | |
1980 #endif | |
1981 /* | |
1982 * Try another name. Change one character, just before | |
1983 * the extension. This should also work for an 8.3 | |
1984 * file name, when after adding the extension it still is | |
1985 * the same file as the original. | |
1986 */ | |
1987 wp = tempname + STRLEN(tempname) - 5; | |
1988 if (wp < gettail(tempname)) /* empty file name? */ | |
1989 wp = gettail(tempname); | |
1990 for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0; | |
1991 --*wp) | |
1992 { | |
1993 /* | |
1994 * They all exist? Must be something wrong! Don't | |
1995 * write the viminfo file then. | |
1996 */ | |
1997 if (*wp == 'a') | |
1998 { | |
1999 vim_free(tempname); | |
2000 tempname = NULL; | |
2001 break; | |
2002 } | |
2003 } | |
2004 } | |
2005 break; | |
2006 } | |
2007 | |
2008 if (tempname != NULL) | |
2009 { | |
762 | 2010 #ifdef VMS |
2011 /* fdopen() fails for some reason */ | |
770 | 2012 umask_save = umask(077); |
2013 fp_out = mch_fopen((char *)tempname, WRITEBIN); | |
2014 (void)umask(umask_save); | |
762 | 2015 #else |
557 | 2016 int fd; |
2017 | |
2018 /* Use mch_open() to be able to use O_NOFOLLOW and set file | |
601 | 2019 * protection: |
673 | 2020 * Unix: same as original file, but strip s-bit. Reset umask to |
2021 * avoid it getting in the way. | |
601 | 2022 * Others: r&w for user only. */ |
762 | 2023 # ifdef UNIX |
673 | 2024 umask_save = umask(0); |
557 | 2025 fd = mch_open((char *)tempname, |
2026 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, | |
569 | 2027 (int)((st_old.st_mode & 0777) | 0600)); |
673 | 2028 (void)umask(umask_save); |
762 | 2029 # else |
569 | 2030 fd = mch_open((char *)tempname, |
673 | 2031 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600); |
762 | 2032 # endif |
557 | 2033 if (fd < 0) |
2034 fp_out = NULL; | |
2035 else | |
2036 fp_out = fdopen(fd, WRITEBIN); | |
762 | 2037 #endif /* VMS */ |
7 | 2038 |
2039 /* | |
2040 * If we can't create in the same directory, try creating a | |
2041 * "normal" temp file. | |
2042 */ | |
2043 if (fp_out == NULL) | |
2044 { | |
2045 vim_free(tempname); | |
6721 | 2046 if ((tempname = vim_tempname('o', TRUE)) != NULL) |
7 | 2047 fp_out = mch_fopen((char *)tempname, WRITEBIN); |
2048 } | |
557 | 2049 |
2050 #if defined(UNIX) && defined(HAVE_FCHOWN) | |
7 | 2051 /* |
557 | 2052 * Make sure the owner can read/write it. This only works for |
2053 * root. | |
7 | 2054 */ |
2055 if (fp_out != NULL) | |
1757 | 2056 ignored = fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid); |
7 | 2057 #endif |
2058 } | |
2059 } | |
2060 | |
2061 /* | |
2062 * Check if the new viminfo file can be written to. | |
2063 */ | |
2064 if (fp_out == NULL) | |
2065 { | |
2066 EMSG2(_("E138: Can't write viminfo file %s!"), | |
301 | 2067 (fp_in == NULL || tempname == NULL) ? fname : tempname); |
7 | 2068 if (fp_in != NULL) |
2069 fclose(fp_in); | |
2070 goto end; | |
2071 } | |
2072 | |
2073 if (p_verbose > 0) | |
294 | 2074 { |
2075 verbose_enter(); | |
274 | 2076 smsg((char_u *)_("Writing viminfo file \"%s\""), fname); |
294 | 2077 verbose_leave(); |
2078 } | |
7 | 2079 |
2080 viminfo_errcnt = 0; | |
1733 | 2081 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS)); |
7 | 2082 |
2083 fclose(fp_out); /* errors are ignored !? */ | |
2084 if (fp_in != NULL) | |
2085 { | |
2086 fclose(fp_in); | |
601 | 2087 |
6049 | 2088 /* In case of an error keep the original viminfo file. Otherwise |
2089 * rename the newly written file. Give an error if that fails. */ | |
2090 if (viminfo_errcnt == 0 && vim_rename(tempname, fname) == -1) | |
2091 { | |
2092 ++viminfo_errcnt; | |
2093 EMSG2(_("E886: Can't rename viminfo file to %s!"), fname); | |
2094 } | |
2095 if (viminfo_errcnt > 0) | |
7 | 2096 mch_remove(tempname); |
601 | 2097 |
2098 #ifdef WIN3264 | |
2099 /* If the viminfo file was hidden then also hide the new file. */ | |
7194
272f04b41f51
commit https://github.com/vim/vim/commit/8a52ba791893fd55c5bdf98825c5b3e8892eaa62
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
2100 if (hidden) |
601 | 2101 mch_hide(fname); |
2102 #endif | |
2103 } | |
2104 | |
7 | 2105 end: |
2106 vim_free(fname); | |
2107 vim_free(tempname); | |
2108 } | |
2109 | |
2110 /* | |
2111 * Get the viminfo file name to use. | |
2112 * If "file" is given and not empty, use it (has already been expanded by | |
2113 * cmdline functions). | |
2114 * Otherwise use "-i file_name", value from 'viminfo' or the default, and | |
2115 * expand environment variables. | |
2116 * Returns an allocated string. NULL when out of memory. | |
2117 */ | |
2118 static char_u * | |
2119 viminfo_filename(file) | |
2120 char_u *file; | |
2121 { | |
2122 if (file == NULL || *file == NUL) | |
2123 { | |
2124 if (use_viminfo != NULL) | |
2125 file = use_viminfo; | |
2126 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL) | |
2127 { | |
2128 #ifdef VIMINFO_FILE2 | |
2129 /* don't use $HOME when not defined (turned into "c:/"!). */ | |
2130 # ifdef VMS | |
2131 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL) | |
2132 # else | |
2133 if (mch_getenv((char_u *)"HOME") == NULL) | |
2134 # endif | |
2135 { | |
2136 /* don't use $VIM when not available. */ | |
2137 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL); | |
2138 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */ | |
2139 file = (char_u *)VIMINFO_FILE2; | |
2140 else | |
2141 file = (char_u *)VIMINFO_FILE; | |
2142 } | |
2143 else | |
2144 #endif | |
2145 file = (char_u *)VIMINFO_FILE; | |
2146 } | |
2147 expand_env(file, NameBuff, MAXPATHL); | |
2148 file = NameBuff; | |
2149 } | |
2150 return vim_strsave(file); | |
2151 } | |
2152 | |
2153 /* | |
2154 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo(). | |
2155 */ | |
2156 static void | |
1733 | 2157 do_viminfo(fp_in, fp_out, flags) |
7 | 2158 FILE *fp_in; |
2159 FILE *fp_out; | |
1733 | 2160 int flags; |
7 | 2161 { |
2162 int count = 0; | |
2163 int eof = FALSE; | |
2164 vir_T vir; | |
4903
2fc1f3346bfb
updated for version 7.3.1197
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
2165 int merge = FALSE; |
7 | 2166 |
2167 if ((vir.vir_line = alloc(LSIZE)) == NULL) | |
2168 return; | |
2169 vir.vir_fd = fp_in; | |
2170 #ifdef FEAT_MBYTE | |
2171 vir.vir_conv.vc_type = CONV_NONE; | |
2172 #endif | |
7664
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2173 ga_init2(&vir.vir_barlines, (int)sizeof(char_u *), 100); |
7 | 2174 |
2175 if (fp_in != NULL) | |
2176 { | |
1733 | 2177 if (flags & VIF_WANT_INFO) |
4903
2fc1f3346bfb
updated for version 7.3.1197
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
2178 { |
1733 | 2179 eof = read_viminfo_up_to_marks(&vir, |
2180 flags & VIF_FORCEIT, fp_out != NULL); | |
4903
2fc1f3346bfb
updated for version 7.3.1197
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
2181 merge = TRUE; |
2fc1f3346bfb
updated for version 7.3.1197
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
2182 } |
2fc1f3346bfb
updated for version 7.3.1197
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
2183 else if (flags != 0) |
7 | 2184 /* Skip info, find start of marks */ |
2185 while (!(eof = viminfo_readline(&vir)) | |
2186 && vir.vir_line[0] != '>') | |
2187 ; | |
2188 } | |
2189 if (fp_out != NULL) | |
2190 { | |
2191 /* Write the info: */ | |
2192 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"), | |
2193 VIM_VERSION_MEDIUM); | |
2545
298d8d6e69be
Avoid warnings from the clang compiler. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2394
diff
changeset
|
2194 fputs(_("# You may edit it if you're careful!\n\n"), fp_out); |
7 | 2195 #ifdef FEAT_MBYTE |
2545
298d8d6e69be
Avoid warnings from the clang compiler. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2394
diff
changeset
|
2196 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out); |
7 | 2197 fprintf(fp_out, "*encoding=%s\n\n", p_enc); |
2198 #endif | |
2199 write_viminfo_search_pattern(fp_out); | |
2200 write_viminfo_sub_string(fp_out); | |
2201 #ifdef FEAT_CMDHIST | |
4903
2fc1f3346bfb
updated for version 7.3.1197
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
2202 write_viminfo_history(fp_out, merge); |
7 | 2203 #endif |
2204 write_viminfo_registers(fp_out); | |
2205 #ifdef FEAT_EVAL | |
2206 write_viminfo_varlist(fp_out); | |
2207 #endif | |
2208 write_viminfo_filemarks(fp_out); | |
2209 write_viminfo_bufferlist(fp_out); | |
7664
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2210 write_viminfo_barlines(&vir, fp_out); |
7 | 2211 count = write_viminfo_marks(fp_out); |
2212 } | |
1733 | 2213 if (fp_in != NULL |
2214 && (flags & (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT))) | |
2215 copy_viminfo_marks(&vir, fp_out, count, eof, flags); | |
7 | 2216 |
2217 vim_free(vir.vir_line); | |
2218 #ifdef FEAT_MBYTE | |
2219 if (vir.vir_conv.vc_type != CONV_NONE) | |
2220 convert_setup(&vir.vir_conv, NULL, NULL); | |
2221 #endif | |
7664
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2222 ga_clear_strings(&vir.vir_barlines); |
7 | 2223 } |
2224 | |
2225 /* | |
2226 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the | |
2227 * first part of the viminfo file which contains everything but the marks that | |
2228 * are local to a file. Returns TRUE when end-of-file is reached. -- webb | |
2229 */ | |
2230 static int | |
2231 read_viminfo_up_to_marks(virp, forceit, writing) | |
2232 vir_T *virp; | |
2233 int forceit; | |
2234 int writing; | |
2235 { | |
2236 int eof; | |
2237 buf_T *buf; | |
2238 | |
2239 #ifdef FEAT_CMDHIST | |
4285 | 2240 prepare_viminfo_history(forceit ? 9999 : 0, writing); |
7 | 2241 #endif |
2242 eof = viminfo_readline(virp); | |
2243 while (!eof && virp->vir_line[0] != '>') | |
2244 { | |
2245 switch (virp->vir_line[0]) | |
2246 { | |
2247 /* Characters reserved for future expansion, ignored now */ | |
2248 case '+': /* "+40 /path/dir file", for running vim without args */ | |
2249 case '^': /* to be defined */ | |
2250 case '<': /* long line - ignored */ | |
2251 /* A comment or empty line. */ | |
2252 case NUL: | |
2253 case '\r': | |
2254 case '\n': | |
2255 case '#': | |
2256 eof = viminfo_readline(virp); | |
2257 break; | |
7664
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2258 case '|': /* copy line (for future use) */ |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2259 if (writing) |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2260 ga_add_string(&virp->vir_barlines, virp->vir_line); |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2261 eof = viminfo_readline(virp); |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2262 break; |
7 | 2263 case '*': /* "*encoding=value" */ |
2264 eof = viminfo_encoding(virp); | |
2265 break; | |
2266 case '!': /* global variable */ | |
2267 #ifdef FEAT_EVAL | |
2268 eof = read_viminfo_varlist(virp, writing); | |
2269 #else | |
2270 eof = viminfo_readline(virp); | |
2271 #endif | |
2272 break; | |
2273 case '%': /* entry for buffer list */ | |
2274 eof = read_viminfo_bufferlist(virp, writing); | |
2275 break; | |
2276 case '"': | |
2277 eof = read_viminfo_register(virp, forceit); | |
2278 break; | |
2279 case '/': /* Search string */ | |
2280 case '&': /* Substitute search string */ | |
2281 case '~': /* Last search string, followed by '/' or '&' */ | |
2282 eof = read_viminfo_search_pattern(virp, forceit); | |
2283 break; | |
2284 case '$': | |
2285 eof = read_viminfo_sub_string(virp, forceit); | |
2286 break; | |
2287 case ':': | |
2288 case '?': | |
2289 case '=': | |
2290 case '@': | |
2291 #ifdef FEAT_CMDHIST | |
4285 | 2292 eof = read_viminfo_history(virp, writing); |
7 | 2293 #else |
2294 eof = viminfo_readline(virp); | |
2295 #endif | |
2296 break; | |
2297 case '-': | |
2298 case '\'': | |
2299 eof = read_viminfo_filemark(virp, forceit); | |
2300 break; | |
2301 default: | |
2302 if (viminfo_error("E575: ", _("Illegal starting char"), | |
2303 virp->vir_line)) | |
2304 eof = TRUE; | |
2305 else | |
2306 eof = viminfo_readline(virp); | |
2307 break; | |
2308 } | |
2309 } | |
2310 | |
2311 #ifdef FEAT_CMDHIST | |
2312 /* Finish reading history items. */ | |
4285 | 2313 if (!writing) |
2314 finish_viminfo_history(); | |
7 | 2315 #endif |
2316 | |
2317 /* Change file names to buffer numbers for fmarks. */ | |
2318 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
2319 fmarks_check_names(buf); | |
2320 | |
2321 return eof; | |
2322 } | |
2323 | |
2324 /* | |
2325 * Compare the 'encoding' value in the viminfo file with the current value of | |
2326 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for | |
2327 * conversion of text with iconv() in viminfo_readstring(). | |
2328 */ | |
2329 static int | |
2330 viminfo_encoding(virp) | |
2331 vir_T *virp; | |
2332 { | |
2333 #ifdef FEAT_MBYTE | |
2334 char_u *p; | |
2335 int i; | |
2336 | |
2337 if (get_viminfo_parameter('c') != 0) | |
2338 { | |
2339 p = vim_strchr(virp->vir_line, '='); | |
2340 if (p != NULL) | |
2341 { | |
2342 /* remove trailing newline */ | |
2343 ++p; | |
2344 for (i = 0; vim_isprintc(p[i]); ++i) | |
2345 ; | |
2346 p[i] = NUL; | |
2347 | |
2348 convert_setup(&virp->vir_conv, p, p_enc); | |
2349 } | |
2350 } | |
2351 #endif | |
2352 return viminfo_readline(virp); | |
2353 } | |
2354 | |
2355 /* | |
2356 * Read a line from the viminfo file. | |
2357 * Returns TRUE for end-of-file; | |
2358 */ | |
2359 int | |
2360 viminfo_readline(virp) | |
2361 vir_T *virp; | |
2362 { | |
2363 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd); | |
2364 } | |
2365 | |
2366 /* | |
2367 * check string read from viminfo file | |
2368 * remove '\n' at the end of the line | |
2369 * - replace CTRL-V CTRL-V with CTRL-V | |
2370 * - replace CTRL-V 'n' with '\n' | |
2371 * | |
2372 * Check for a long line as written by viminfo_writestring(). | |
2373 * | |
2374 * Return the string in allocated memory (NULL when out of memory). | |
2375 */ | |
2376 char_u * | |
2377 viminfo_readstring(virp, off, convert) | |
2378 vir_T *virp; | |
2379 int off; /* offset for virp->vir_line */ | |
1883 | 2380 int convert UNUSED; /* convert the string */ |
7 | 2381 { |
2382 char_u *retval; | |
2383 char_u *s, *d; | |
2384 long len; | |
2385 | |
2386 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1])) | |
2387 { | |
2388 len = atol((char *)virp->vir_line + off + 1); | |
2389 retval = lalloc(len, TRUE); | |
2390 if (retval == NULL) | |
2391 { | |
2392 /* Line too long? File messed up? Skip next line. */ | |
2393 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd); | |
2394 return NULL; | |
2395 } | |
2396 (void)vim_fgets(retval, (int)len, virp->vir_fd); | |
2397 s = retval + 1; /* Skip the leading '<' */ | |
2398 } | |
2399 else | |
2400 { | |
2401 retval = vim_strsave(virp->vir_line + off); | |
2402 if (retval == NULL) | |
2403 return NULL; | |
2404 s = retval; | |
2405 } | |
2406 | |
2407 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */ | |
2408 d = retval; | |
2409 while (*s != NUL && *s != '\n') | |
2410 { | |
2411 if (s[0] == Ctrl_V && s[1] != NUL) | |
2412 { | |
2413 if (s[1] == 'n') | |
2414 *d++ = '\n'; | |
2415 else | |
2416 *d++ = Ctrl_V; | |
2417 s += 2; | |
2418 } | |
2419 else | |
2420 *d++ = *s++; | |
2421 } | |
2422 *d = NUL; | |
2423 | |
2424 #ifdef FEAT_MBYTE | |
2425 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL) | |
2426 { | |
2427 d = string_convert(&virp->vir_conv, retval, NULL); | |
2428 if (d != NULL) | |
2429 { | |
2430 vim_free(retval); | |
2431 retval = d; | |
2432 } | |
2433 } | |
2434 #endif | |
2435 | |
2436 return retval; | |
2437 } | |
2438 | |
2439 /* | |
2440 * write string to viminfo file | |
2441 * - replace CTRL-V with CTRL-V CTRL-V | |
2442 * - replace '\n' with CTRL-V 'n' | |
2443 * - add a '\n' at the end | |
2444 * | |
2445 * For a long line: | |
2446 * - write " CTRL-V <length> \n " in first line | |
2447 * - write " < <string> \n " in second line | |
2448 */ | |
2449 void | |
2450 viminfo_writestring(fd, p) | |
2451 FILE *fd; | |
2452 char_u *p; | |
2453 { | |
2454 int c; | |
2455 char_u *s; | |
2456 int len = 0; | |
2457 | |
2458 for (s = p; *s != NUL; ++s) | |
2459 { | |
2460 if (*s == Ctrl_V || *s == '\n') | |
2461 ++len; | |
2462 ++len; | |
2463 } | |
2464 | |
2465 /* If the string will be too long, write its length and put it in the next | |
2466 * line. Take into account that some room is needed for what comes before | |
2467 * the string (e.g., variable name). Add something to the length for the | |
2468 * '<', NL and trailing NUL. */ | |
2469 if (len > LSIZE / 2) | |
2470 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3); | |
2471 | |
2472 while ((c = *p++) != NUL) | |
2473 { | |
2474 if (c == Ctrl_V || c == '\n') | |
2475 { | |
2476 putc(Ctrl_V, fd); | |
2477 if (c == '\n') | |
2478 c = 'n'; | |
2479 } | |
2480 putc(c, fd); | |
2481 } | |
2482 putc('\n', fd); | |
2483 } | |
7664
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2484 |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2485 static void |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2486 write_viminfo_barlines(vir_T *virp, FILE *fp_out) |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2487 { |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2488 int i; |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2489 garray_T *gap = &virp->vir_barlines; |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2490 |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2491 if (gap->ga_len > 0) |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2492 { |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2493 fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out); |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2494 |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2495 for (i = 0; i < gap->ga_len; ++i) |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2496 fputs(((char **)(gap->ga_data))[i], fp_out); |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2497 } |
1fded31d9e04
commit https://github.com/vim/vim/commit/b20e334859334be35de4b295023a2b49bdabbfa9
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
2498 } |
7 | 2499 #endif /* FEAT_VIMINFO */ |
2500 | |
2501 /* | |
2502 * Implementation of ":fixdel", also used by get_stty(). | |
2503 * <BS> resulting <Del> | |
2504 * ^? ^H | |
2505 * not ^? ^? | |
2506 */ | |
2507 void | |
2508 do_fixdel(eap) | |
1876 | 2509 exarg_T *eap UNUSED; |
7 | 2510 { |
2511 char_u *p; | |
2512 | |
2513 p = find_termcode((char_u *)"kb"); | |
2514 add_termcode((char_u *)"kD", p != NULL | |
2515 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE); | |
2516 } | |
2517 | |
2518 void | |
169 | 2519 print_line_no_prefix(lnum, use_number, list) |
7 | 2520 linenr_T lnum; |
2521 int use_number; | |
169 | 2522 int list; |
7 | 2523 { |
13 | 2524 char_u numbuf[30]; |
7 | 2525 |
2526 if (curwin->w_p_nu || use_number) | |
2527 { | |
1872 | 2528 vim_snprintf((char *)numbuf, sizeof(numbuf), |
2529 "%*ld ", number_width(curwin), (long)lnum); | |
7 | 2530 msg_puts_attr(numbuf, hl_attr(HLF_N)); /* Highlight line nrs */ |
2531 } | |
169 | 2532 msg_prt_line(ml_get(lnum), list); |
7 | 2533 } |
2534 | |
2535 /* | |
2536 * Print a text line. Also in silent mode ("ex -s"). | |
2537 */ | |
2538 void | |
169 | 2539 print_line(lnum, use_number, list) |
7 | 2540 linenr_T lnum; |
2541 int use_number; | |
169 | 2542 int list; |
7 | 2543 { |
2544 int save_silent = silent_mode; | |
2545 | |
169 | 2546 msg_start(); |
7 | 2547 silent_mode = FALSE; |
169 | 2548 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */ |
2549 print_line_no_prefix(lnum, use_number, list); | |
7 | 2550 if (save_silent) |
2551 { | |
2552 msg_putchar('\n'); | |
2553 cursor_on(); /* msg_start() switches it off */ | |
2554 out_flush(); | |
2555 silent_mode = save_silent; | |
1798 | 2556 } |
2557 info_message = FALSE; | |
7 | 2558 } |
2559 | |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2560 int |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2561 rename_buffer(new_fname) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2562 char_u *new_fname; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2563 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2564 char_u *fname, *sfname, *xfname; |
4613
219b2fcad60d
updated for version 7.3.1054
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2565 buf_T *buf; |
219b2fcad60d
updated for version 7.3.1054
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2566 |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2567 #ifdef FEAT_AUTOCMD |
4613
219b2fcad60d
updated for version 7.3.1054
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2568 buf = curbuf; |
219b2fcad60d
updated for version 7.3.1054
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2569 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2570 /* buffer changed, don't change name now */ |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2571 if (buf != curbuf) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2572 return FAIL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2573 # ifdef FEAT_EVAL |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2574 if (aborting()) /* autocmds may abort script processing */ |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2575 return FAIL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2576 # endif |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2577 #endif |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2578 /* |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2579 * The name of the current buffer will be changed. |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2580 * A new (unlisted) buffer entry needs to be made to hold the old file |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2581 * name, which will become the alternate file name. |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2582 * But don't set the alternate file name if the buffer didn't have a |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2583 * name. |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2584 */ |
4613
219b2fcad60d
updated for version 7.3.1054
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2585 fname = curbuf->b_ffname; |
219b2fcad60d
updated for version 7.3.1054
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2586 sfname = curbuf->b_sfname; |
219b2fcad60d
updated for version 7.3.1054
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2587 xfname = curbuf->b_fname; |
219b2fcad60d
updated for version 7.3.1054
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2588 curbuf->b_ffname = NULL; |
219b2fcad60d
updated for version 7.3.1054
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2589 curbuf->b_sfname = NULL; |
219b2fcad60d
updated for version 7.3.1054
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2590 if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL) |
219b2fcad60d
updated for version 7.3.1054
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2591 { |
219b2fcad60d
updated for version 7.3.1054
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2592 curbuf->b_ffname = fname; |
219b2fcad60d
updated for version 7.3.1054
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2593 curbuf->b_sfname = sfname; |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2594 return FAIL; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2595 } |
4613
219b2fcad60d
updated for version 7.3.1054
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2596 curbuf->b_flags |= BF_NOTEDITED; |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2597 if (xfname != NULL && *xfname != NUL) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2598 { |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2599 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2600 if (buf != NULL && !cmdmod.keepalt) |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2601 curwin->w_alt_fnum = buf->b_fnum; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2602 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2603 vim_free(fname); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2604 vim_free(sfname); |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2605 #ifdef FEAT_AUTOCMD |
4613
219b2fcad60d
updated for version 7.3.1054
Bram Moolenaar <bram@vim.org>
parents:
4589
diff
changeset
|
2606 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2607 #endif |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2608 /* Change directories when the 'acd' option is set. */ |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2609 DO_AUTOCHDIR |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2610 return OK; |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2611 } |
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2612 |
7 | 2613 /* |
2614 * ":file[!] [fname]". | |
2615 */ | |
2616 void | |
2617 ex_file(eap) | |
2618 exarg_T *eap; | |
2619 { | |
14 | 2620 /* ":0file" removes the file name. Check for illegal uses ":3file", |
2621 * "0file name", etc. */ | |
2622 if (eap->addr_count > 0 | |
2623 && (*eap->arg != NUL | |
2624 || eap->line2 > 0 | |
2625 || eap->addr_count > 1)) | |
2626 { | |
2627 EMSG(_(e_invarg)); | |
2628 return; | |
2629 } | |
2630 | |
2631 if (*eap->arg != NUL || eap->addr_count == 1) | |
7 | 2632 { |
4589
fa39483a1363
updated for version 7.3.1042
Bram Moolenaar <bram@vim.org>
parents:
4285
diff
changeset
|
2633 if (rename_buffer(eap->arg) == FAIL) |
7 | 2634 return; |
2635 } | |
2636 /* print full file name if :cd used */ | |
2637 fileinfo(FALSE, FALSE, eap->forceit); | |
2638 } | |
2639 | |
2640 /* | |
2641 * ":update". | |
2642 */ | |
2643 void | |
2644 ex_update(eap) | |
2645 exarg_T *eap; | |
2646 { | |
2647 if (curbufIsChanged()) | |
2648 (void)do_write(eap); | |
2649 } | |
2650 | |
2651 /* | |
2652 * ":write" and ":saveas". | |
2653 */ | |
2654 void | |
2655 ex_write(eap) | |
2656 exarg_T *eap; | |
2657 { | |
2658 if (eap->usefilter) /* input lines to shell command */ | |
2659 do_bang(1, eap, FALSE, TRUE, FALSE); | |
2660 else | |
2661 (void)do_write(eap); | |
2662 } | |
2663 | |
2664 /* | |
2665 * write current buffer to file 'eap->arg' | |
2666 * if 'eap->append' is TRUE, append to the file | |
2667 * | |
2668 * if *eap->arg == NUL write to current file | |
2669 * | |
2670 * return FAIL for failure, OK otherwise | |
2671 */ | |
2672 int | |
2673 do_write(eap) | |
2674 exarg_T *eap; | |
2675 { | |
2676 int other; | |
2677 char_u *fname = NULL; /* init to shut up gcc */ | |
2678 char_u *ffname; | |
2679 int retval = FAIL; | |
2680 char_u *free_fname = NULL; | |
2681 #ifdef FEAT_BROWSE | |
2682 char_u *browse_file = NULL; | |
2683 #endif | |
2684 buf_T *alt_buf = NULL; | |
2685 | |
2686 if (not_writing()) /* check 'write' option */ | |
2687 return FAIL; | |
2688 | |
2689 ffname = eap->arg; | |
2690 #ifdef FEAT_BROWSE | |
2691 if (cmdmod.browse) | |
2692 { | |
29 | 2693 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname, |
7 | 2694 NULL, NULL, NULL, curbuf); |
2695 if (browse_file == NULL) | |
2696 goto theend; | |
2697 ffname = browse_file; | |
2698 } | |
2699 #endif | |
2700 if (*ffname == NUL) | |
2701 { | |
2702 if (eap->cmdidx == CMD_saveas) | |
2703 { | |
2704 EMSG(_(e_argreq)); | |
2705 goto theend; | |
2706 } | |
2707 other = FALSE; | |
2708 } | |
2709 else | |
2710 { | |
2711 fname = ffname; | |
2712 free_fname = fix_fname(ffname); | |
2713 /* | |
2714 * When out-of-memory, keep unexpanded file name, because we MUST be | |
2715 * able to write the file in this situation. | |
2716 */ | |
2717 if (free_fname != NULL) | |
2718 ffname = free_fname; | |
2719 other = otherfile(ffname); | |
2720 } | |
2721 | |
2722 /* | |
2723 * If we have a new file, put its name in the list of alternate file names. | |
2724 */ | |
2725 if (other) | |
2726 { | |
2727 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL | |
2728 || eap->cmdidx == CMD_saveas) | |
2729 alt_buf = setaltfname(ffname, fname, (linenr_T)1); | |
2730 else | |
2731 alt_buf = buflist_findname(ffname); | |
2732 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL) | |
2733 { | |
2734 /* Overwriting a file that is loaded in another buffer is not a | |
2735 * good idea. */ | |
315 | 2736 EMSG(_(e_bufloaded)); |
7 | 2737 goto theend; |
2738 } | |
2739 } | |
2740 | |
2741 /* | |
2742 * Writing to the current file is not allowed in readonly mode | |
2743 * and a file name is required. | |
2744 * "nofile" and "nowrite" buffers cannot be written implicitly either. | |
2745 */ | |
2746 if (!other && ( | |
2747 #ifdef FEAT_QUICKFIX | |
2748 bt_dontwrite_msg(curbuf) || | |
2749 #endif | |
2750 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf))) | |
2751 goto theend; | |
2752 | |
2753 if (!other) | |
2754 { | |
2755 ffname = curbuf->b_ffname; | |
2756 fname = curbuf->b_fname; | |
2757 /* | |
2758 * Not writing the whole file is only allowed with '!'. | |
2759 */ | |
2760 if ( (eap->line1 != 1 | |
2761 || eap->line2 != curbuf->b_ml.ml_line_count) | |
2762 && !eap->forceit | |
2763 && !eap->append | |
2764 && !p_wa) | |
2765 { | |
2766 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
2767 if (p_confirm || cmdmod.confirm) | |
2768 { | |
2769 if (vim_dialog_yesno(VIM_QUESTION, NULL, | |
2770 (char_u *)_("Write partial file?"), 2) != VIM_YES) | |
2771 goto theend; | |
2772 eap->forceit = TRUE; | |
2773 } | |
2774 else | |
2775 #endif | |
2776 { | |
2777 EMSG(_("E140: Use ! to write partial buffer")); | |
2778 goto theend; | |
2779 } | |
2780 } | |
2781 } | |
2782 | |
2783 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK) | |
2784 { | |
2785 if (eap->cmdidx == CMD_saveas && alt_buf != NULL) | |
2786 { | |
2787 #ifdef FEAT_AUTOCMD | |
2788 buf_T *was_curbuf = curbuf; | |
2789 | |
2790 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); | |
21 | 2791 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf); |
7 | 2792 # ifdef FEAT_EVAL |
2793 if (curbuf != was_curbuf || aborting()) | |
2794 # else | |
2795 if (curbuf != was_curbuf) | |
2796 # endif | |
2797 { | |
2798 /* buffer changed, don't change name now */ | |
2799 retval = FAIL; | |
2800 goto theend; | |
2801 } | |
2802 #endif | |
2803 /* Exchange the file names for the current and the alternate | |
2804 * buffer. This makes it look like we are now editing the buffer | |
2805 * under the new name. Must be done before buf_write(), because | |
2806 * if there is no file name and 'cpo' contains 'F', it will set | |
2807 * the file name. */ | |
2808 fname = alt_buf->b_fname; | |
2809 alt_buf->b_fname = curbuf->b_fname; | |
2810 curbuf->b_fname = fname; | |
2811 fname = alt_buf->b_ffname; | |
2812 alt_buf->b_ffname = curbuf->b_ffname; | |
2813 curbuf->b_ffname = fname; | |
2814 fname = alt_buf->b_sfname; | |
2815 alt_buf->b_sfname = curbuf->b_sfname; | |
2816 curbuf->b_sfname = fname; | |
2817 buf_name_changed(curbuf); | |
2818 #ifdef FEAT_AUTOCMD | |
2819 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); | |
21 | 2820 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf); |
7 | 2821 if (!alt_buf->b_p_bl) |
2822 { | |
2823 alt_buf->b_p_bl = TRUE; | |
2824 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf); | |
2825 } | |
2826 # ifdef FEAT_EVAL | |
2827 if (curbuf != was_curbuf || aborting()) | |
2828 # else | |
2829 if (curbuf != was_curbuf) | |
2830 # endif | |
2831 { | |
2832 /* buffer changed, don't write the file */ | |
2833 retval = FAIL; | |
2834 goto theend; | |
2835 } | |
634 | 2836 |
2837 /* If 'filetype' was empty try detecting it now. */ | |
2838 if (*curbuf->b_p_ft == NUL) | |
2839 { | |
819 | 2840 if (au_has_group((char_u *)"filetypedetect")) |
2841 (void)do_doautocmd((char_u *)"filetypedetect BufRead", | |
2842 TRUE); | |
717 | 2843 do_modelines(0); |
634 | 2844 } |
2648 | 2845 |
2846 /* Autocommands may have changed buffer names, esp. when | |
2847 * 'autochdir' is set. */ | |
2848 fname = curbuf->b_sfname; | |
7 | 2849 #endif |
2850 } | |
2851 | |
2852 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2, | |
2853 eap, eap->append, eap->forceit, TRUE, FALSE); | |
634 | 2854 |
819 | 2855 /* After ":saveas fname" reset 'readonly'. */ |
961 | 2856 if (eap->cmdidx == CMD_saveas) |
2857 { | |
2858 if (retval == OK) | |
1806 | 2859 { |
961 | 2860 curbuf->b_p_ro = FALSE; |
1806 | 2861 #ifdef FEAT_WINDOWS |
2862 redraw_tabline = TRUE; | |
2863 #endif | |
2864 } | |
961 | 2865 /* Change directories when the 'acd' option is set. */ |
2866 DO_AUTOCHDIR | |
2867 } | |
7 | 2868 } |
2869 | |
2870 theend: | |
2871 #ifdef FEAT_BROWSE | |
2872 vim_free(browse_file); | |
2873 #endif | |
2874 vim_free(free_fname); | |
2875 return retval; | |
2876 } | |
2877 | |
2878 /* | |
2879 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED, | |
2880 * BF_NEW or BF_READERR, check for overwriting current file. | |
2881 * May set eap->forceit if a dialog says it's OK to overwrite. | |
2882 * Return OK if it's OK, FAIL if it is not. | |
2883 */ | |
3486 | 2884 int |
7 | 2885 check_overwrite(eap, buf, fname, ffname, other) |
2886 exarg_T *eap; | |
2887 buf_T *buf; | |
2888 char_u *fname; /* file name to be used (can differ from | |
2889 buf->ffname) */ | |
2890 char_u *ffname; /* full path version of fname */ | |
2891 int other; /* writing under other name */ | |
2892 { | |
2893 /* | |
2894 * write to other file or b_flags set or not writing the whole file: | |
2895 * overwriting only allowed with '!' | |
2896 */ | |
2897 if ( (other | |
2898 || (buf->b_flags & BF_NOTEDITED) | |
2899 || ((buf->b_flags & BF_NEW) | |
2900 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL) | |
2901 || (buf->b_flags & BF_READERR)) | |
2902 && !p_wa | |
1457 | 2903 #ifdef FEAT_QUICKFIX |
2904 && !bt_nofile(buf) | |
2905 #endif | |
7 | 2906 && vim_fexists(ffname)) |
2907 { | |
460 | 2908 if (!eap->forceit && !eap->append) |
7 | 2909 { |
460 | 2910 #ifdef UNIX |
637 | 2911 /* with UNIX it is possible to open a directory */ |
460 | 2912 if (mch_isdir(ffname)) |
2913 { | |
2914 EMSG2(_(e_isadir2), ffname); | |
2915 return FAIL; | |
2916 } | |
7 | 2917 #endif |
2918 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
460 | 2919 if (p_confirm || cmdmod.confirm) |
2920 { | |
2770 | 2921 char_u buff[DIALOG_MSG_SIZE]; |
460 | 2922 |
2923 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname); | |
2924 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES) | |
2925 return FAIL; | |
2926 eap->forceit = TRUE; | |
2927 } | |
2928 else | |
2929 #endif | |
2930 { | |
2931 EMSG(_(e_exists)); | |
7 | 2932 return FAIL; |
460 | 2933 } |
7 | 2934 } |
460 | 2935 |
2936 /* For ":w! filename" check that no swap file exists for "filename". */ | |
2937 if (other && !emsg_silent) | |
7 | 2938 { |
2770 | 2939 char_u *dir; |
460 | 2940 char_u *p; |
2941 int r; | |
2942 char_u *swapname; | |
2943 | |
2944 /* We only try the first entry in 'directory', without checking if | |
2945 * it's writable. If the "." directory is not writable the write | |
2946 * will probably fail anyway. | |
2947 * Use 'shortname' of the current buffer, since there is no buffer | |
2948 * for the written file. */ | |
2949 if (*p_dir == NUL) | |
2770 | 2950 { |
2951 dir = alloc(5); | |
2952 if (dir == NULL) | |
2953 return FAIL; | |
460 | 2954 STRCPY(dir, "."); |
2770 | 2955 } |
460 | 2956 else |
2957 { | |
2770 | 2958 dir = alloc(MAXPATHL); |
2959 if (dir == NULL) | |
2960 return FAIL; | |
460 | 2961 p = p_dir; |
2962 copy_option_part(&p, dir, MAXPATHL, ","); | |
2963 } | |
2964 swapname = makeswapname(fname, ffname, curbuf, dir); | |
2770 | 2965 vim_free(dir); |
460 | 2966 r = vim_fexists(swapname); |
2967 if (r) | |
2968 { | |
2969 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
2970 if (p_confirm || cmdmod.confirm) | |
2971 { | |
2770 | 2972 char_u buff[DIALOG_MSG_SIZE]; |
460 | 2973 |
2974 dialog_msg(buff, | |
2975 _("Swap file \"%s\" exists, overwrite anyway?"), | |
2976 swapname); | |
2977 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) | |
2978 != VIM_YES) | |
819 | 2979 { |
2980 vim_free(swapname); | |
460 | 2981 return FAIL; |
819 | 2982 } |
460 | 2983 eap->forceit = TRUE; |
2984 } | |
2985 else | |
2986 #endif | |
2987 { | |
2988 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"), | |
2989 swapname); | |
819 | 2990 vim_free(swapname); |
460 | 2991 return FAIL; |
2992 } | |
2993 } | |
819 | 2994 vim_free(swapname); |
7 | 2995 } |
2996 } | |
2997 return OK; | |
2998 } | |
2999 | |
3000 /* | |
3001 * Handle ":wnext", ":wNext" and ":wprevious" commands. | |
3002 */ | |
3003 void | |
3004 ex_wnext(eap) | |
3005 exarg_T *eap; | |
3006 { | |
3007 int i; | |
3008 | |
3009 if (eap->cmd[1] == 'n') | |
3010 i = curwin->w_arg_idx + (int)eap->line2; | |
3011 else | |
3012 i = curwin->w_arg_idx - (int)eap->line2; | |
3013 eap->line1 = 1; | |
3014 eap->line2 = curbuf->b_ml.ml_line_count; | |
3015 if (do_write(eap) != FAIL) | |
3016 do_argfile(eap, i); | |
3017 } | |
3018 | |
3019 /* | |
3020 * ":wall", ":wqall" and ":xall": Write all changed files (and exit). | |
3021 */ | |
3022 void | |
3023 do_wqall(eap) | |
3024 exarg_T *eap; | |
3025 { | |
3026 buf_T *buf; | |
3027 int error = 0; | |
3028 int save_forceit = eap->forceit; | |
3029 | |
3030 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall) | |
3031 exiting = TRUE; | |
3032 | |
3033 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
3034 { | |
3035 if (bufIsChanged(buf)) | |
3036 { | |
3037 /* | |
3038 * Check if there is a reason the buffer cannot be written: | |
3039 * 1. if the 'write' option is set | |
3040 * 2. if there is no file name (even after browsing) | |
3041 * 3. if the 'readonly' is set (even after a dialog) | |
3042 * 4. if overwriting is allowed (even after a dialog) | |
3043 */ | |
3044 if (not_writing()) | |
3045 { | |
3046 ++error; | |
3047 break; | |
3048 } | |
3049 #ifdef FEAT_BROWSE | |
3050 /* ":browse wall": ask for file name if there isn't one */ | |
3051 if (buf->b_ffname == NULL && cmdmod.browse) | |
3052 browse_save_fname(buf); | |
3053 #endif | |
3054 if (buf->b_ffname == NULL) | |
3055 { | |
3056 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum); | |
3057 ++error; | |
3058 } | |
3059 else if (check_readonly(&eap->forceit, buf) | |
3060 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname, | |
3061 FALSE) == FAIL) | |
3062 { | |
3063 ++error; | |
3064 } | |
3065 else | |
3066 { | |
3067 if (buf_write_all(buf, eap->forceit) == FAIL) | |
3068 ++error; | |
3069 #ifdef FEAT_AUTOCMD | |
3070 /* an autocommand may have deleted the buffer */ | |
3071 if (!buf_valid(buf)) | |
3072 buf = firstbuf; | |
3073 #endif | |
3074 } | |
3075 eap->forceit = save_forceit; /* check_overwrite() may set it */ | |
3076 } | |
3077 } | |
3078 if (exiting) | |
3079 { | |
3080 if (!error) | |
3081 getout(0); /* exit Vim */ | |
3082 not_exiting(); | |
3083 } | |
3084 } | |
3085 | |
3086 /* | |
3087 * Check the 'write' option. | |
3088 * Return TRUE and give a message when it's not st. | |
3089 */ | |
3090 int | |
3091 not_writing() | |
3092 { | |
3093 if (p_write) | |
3094 return FALSE; | |
3095 EMSG(_("E142: File not written: Writing is disabled by 'write' option")); | |
3096 return TRUE; | |
3097 } | |
3098 | |
3099 /* | |
1303 | 3100 * Check if a buffer is read-only (either 'readonly' option is set or file is |
3101 * read-only). Ask for overruling in a dialog. Return TRUE and give an error | |
3102 * message when the buffer is readonly. | |
7 | 3103 */ |
3104 static int | |
3105 check_readonly(forceit, buf) | |
3106 int *forceit; | |
3107 buf_T *buf; | |
3108 { | |
1303 | 3109 struct stat st; |
3110 | |
3111 /* Handle a file being readonly when the 'readonly' option is set or when | |
3112 * the file exists and permissions are read-only. | |
3113 * We will send 0777 to check_file_readonly(), as the "perm" variable is | |
3114 * important for device checks but not here. */ | |
3115 if (!*forceit && (buf->b_p_ro | |
3116 || (mch_stat((char *)buf->b_ffname, &st) >= 0 | |
3117 && check_file_readonly(buf->b_ffname, 0777)))) | |
7 | 3118 { |
3119 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
3120 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL) | |
3121 { | |
2770 | 3122 char_u buff[DIALOG_MSG_SIZE]; |
7 | 3123 |
1303 | 3124 if (buf->b_p_ro) |
3125 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"), | |
3126 buf->b_fname); | |
3127 else | |
3128 dialog_msg(buff, _("File permissions of \"%s\" are read-only.\nIt may still be possible to write it.\nDo you wish to try?"), | |
7 | 3129 buf->b_fname); |
3130 | |
3131 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES) | |
3132 { | |
3133 /* Set forceit, to force the writing of a readonly file */ | |
3134 *forceit = TRUE; | |
3135 return FALSE; | |
3136 } | |
3137 else | |
3138 return TRUE; | |
3139 } | |
3140 else | |
3141 #endif | |
1303 | 3142 if (buf->b_p_ro) |
7 | 3143 EMSG(_(e_readonly)); |
1303 | 3144 else |
3145 EMSG2(_("E505: \"%s\" is read-only (add ! to override)"), | |
3146 buf->b_fname); | |
7 | 3147 return TRUE; |
3148 } | |
1303 | 3149 |
7 | 3150 return FALSE; |
3151 } | |
3152 | |
3153 /* | |
632 | 3154 * Try to abandon current file and edit a new or existing file. |
3155 * 'fnum' is the number of the file, if zero use ffname/sfname. | |
7 | 3156 * |
632 | 3157 * Return 1 for "normal" error, 2 for "not written" error, 0 for success |
1370 | 3158 * -1 for successfully opening another file. |
7 | 3159 * 'lnum' is the line number for the cursor in the new file (if non-zero). |
3160 */ | |
3161 int | |
3162 getfile(fnum, ffname, sfname, setpm, lnum, forceit) | |
3163 int fnum; | |
3164 char_u *ffname; | |
3165 char_u *sfname; | |
3166 int setpm; | |
3167 linenr_T lnum; | |
3168 int forceit; | |
3169 { | |
3170 int other; | |
3171 int retval; | |
3172 char_u *free_me = NULL; | |
3173 | |
634 | 3174 if (text_locked()) |
7 | 3175 return 1; |
819 | 3176 #ifdef FEAT_AUTOCMD |
3177 if (curbuf_locked()) | |
3178 return 1; | |
3179 #endif | |
7 | 3180 |
3181 if (fnum == 0) | |
3182 { | |
3183 /* make ffname full path, set sfname */ | |
3184 fname_expand(curbuf, &ffname, &sfname); | |
3185 other = otherfile(ffname); | |
3186 free_me = ffname; /* has been allocated, free() later */ | |
3187 } | |
3188 else | |
3189 other = (fnum != curbuf->b_fnum); | |
3190 | |
3191 if (other) | |
3192 ++no_wait_return; /* don't wait for autowrite message */ | |
3193 if (other && !forceit && curbuf->b_nwindows == 1 && !P_HID(curbuf) | |
3194 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL) | |
3195 { | |
3196 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
3197 if (p_confirm && p_write) | |
3198 dialog_changed(curbuf, FALSE); | |
3199 if (curbufIsChanged()) | |
3200 #endif | |
3201 { | |
3202 if (other) | |
3203 --no_wait_return; | |
3204 EMSG(_(e_nowrtmsg)); | |
3205 retval = 2; /* file has been changed */ | |
3206 goto theend; | |
3207 } | |
3208 } | |
3209 if (other) | |
3210 --no_wait_return; | |
3211 if (setpm) | |
3212 setpcmark(); | |
3213 if (!other) | |
3214 { | |
3215 if (lnum != 0) | |
3216 curwin->w_cursor.lnum = lnum; | |
3217 check_cursor_lnum(); | |
3218 beginline(BL_SOL | BL_FIX); | |
3219 retval = 0; /* it's in the same file */ | |
3220 } | |
3221 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum, | |
1743 | 3222 (P_HID(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0), |
3223 curwin) == OK) | |
7 | 3224 retval = -1; /* opened another file */ |
3225 else | |
3226 retval = 1; /* error encountered */ | |
3227 | |
3228 theend: | |
3229 vim_free(free_me); | |
3230 return retval; | |
3231 } | |
3232 | |
3233 /* | |
3234 * start editing a new file | |
3235 * | |
3236 * fnum: file number; if zero use ffname/sfname | |
3237 * ffname: the file name | |
3238 * - full path if sfname used, | |
3239 * - any file name if sfname is NULL | |
3240 * - empty string to re-edit with the same file name (but may be | |
3241 * in a different directory) | |
3242 * - NULL to start an empty buffer | |
3243 * sfname: the short file name (or NULL) | |
3244 * eap: contains the command to be executed after loading the file and | |
3245 * forced 'ff' and 'fenc' | |
3246 * newlnum: if > 0: put cursor on this line number (if possible) | |
3247 * if ECMD_LASTL: use last position in loaded file | |
3248 * if ECMD_LAST: use last position in all files | |
3249 * if ECMD_ONE: use first line | |
3250 * flags: | |
3251 * ECMD_HIDE: if TRUE don't free the current buffer | |
3252 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file | |
3253 * ECMD_OLDBUF: use existing buffer if it exists | |
3254 * ECMD_FORCEIT: ! used for Ex command | |
3255 * ECMD_ADDBUF: don't edit, just add to buffer list | |
1743 | 3256 * oldwin: Should be "curwin" when editing a new buffer in the current |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
3257 * window, NULL when splitting the window first. When not NULL info |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
3258 * of the previous buffer for "oldwin" is stored. |
7 | 3259 * |
3260 * return FAIL for failure, OK otherwise | |
3261 */ | |
3262 int | |
1743 | 3263 do_ecmd(fnum, ffname, sfname, eap, newlnum, flags, oldwin) |
7 | 3264 int fnum; |
3265 char_u *ffname; | |
3266 char_u *sfname; | |
3267 exarg_T *eap; /* can be NULL! */ | |
3268 linenr_T newlnum; | |
3269 int flags; | |
1743 | 3270 win_T *oldwin; |
7 | 3271 { |
3272 int other_file; /* TRUE if editing another file */ | |
3273 int oldbuf; /* TRUE if using existing buffer */ | |
3274 #ifdef FEAT_AUTOCMD | |
3275 int auto_buf = FALSE; /* TRUE if autocommands brought us | |
3276 into the buffer unexpectedly */ | |
3277 char_u *new_name = NULL; | |
716 | 3278 int did_set_swapcommand = FALSE; |
7 | 3279 #endif |
3280 buf_T *buf; | |
3281 #if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
3282 buf_T *old_curbuf = curbuf; | |
3283 #endif | |
3284 char_u *free_fname = NULL; | |
3285 #ifdef FEAT_BROWSE | |
3286 char_u *browse_file = NULL; | |
3287 #endif | |
3288 int retval = FAIL; | |
3289 long n; | |
6702 | 3290 pos_T orig_pos; |
7 | 3291 linenr_T topline = 0; |
3292 int newcol = -1; | |
3293 int solcol = -1; | |
3294 pos_T *pos; | |
3295 #ifdef FEAT_SUN_WORKSHOP | |
3296 char_u *cp; | |
3297 #endif | |
3298 char_u *command = NULL; | |
1185 | 3299 #ifdef FEAT_SPELL |
3300 int did_get_winopts = FALSE; | |
3301 #endif | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3302 int readfile_flags = 0; |
7 | 3303 |
3304 if (eap != NULL) | |
3305 command = eap->do_ecmd_cmd; | |
3306 | |
3307 if (fnum != 0) | |
3308 { | |
3309 if (fnum == curbuf->b_fnum) /* file is already being edited */ | |
3310 return OK; /* nothing to do */ | |
3311 other_file = TRUE; | |
3312 } | |
3313 else | |
3314 { | |
3315 #ifdef FEAT_BROWSE | |
3316 if (cmdmod.browse) | |
3317 { | |
1683 | 3318 # ifdef FEAT_AUTOCMD |
462 | 3319 if ( |
1683 | 3320 # ifdef FEAT_GUI |
462 | 3321 !gui.in_use && |
1683 | 3322 # endif |
462 | 3323 au_has_group((char_u *)"FileExplorer")) |
3324 { | |
3325 /* No browsing supported but we do have the file explorer: | |
3326 * Edit the directory. */ | |
3327 if (ffname == NULL || !mch_isdir(ffname)) | |
3328 ffname = (char_u *)"."; | |
3329 } | |
3330 else | |
1683 | 3331 # endif |
462 | 3332 { |
3333 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname, | |
7 | 3334 NULL, NULL, NULL, curbuf); |
462 | 3335 if (browse_file == NULL) |
3336 goto theend; | |
3337 ffname = browse_file; | |
3338 } | |
7 | 3339 } |
3340 #endif | |
3341 /* if no short name given, use ffname for short name */ | |
3342 if (sfname == NULL) | |
3343 sfname = ffname; | |
3344 #ifdef USE_FNAME_CASE | |
3345 # ifdef USE_LONG_FNAME | |
3346 if (USE_LONG_FNAME) | |
3347 # endif | |
436 | 3348 if (sfname != NULL) |
3349 fname_case(sfname, 0); /* set correct case for sfname */ | |
7 | 3350 #endif |
3351 | |
3352 #ifdef FEAT_LISTCMDS | |
3353 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL)) | |
3354 goto theend; | |
3355 #endif | |
3356 | |
3357 if (ffname == NULL) | |
3358 other_file = TRUE; | |
3359 /* there is no file name */ | |
3360 else if (*ffname == NUL && curbuf->b_ffname == NULL) | |
3361 other_file = FALSE; | |
3362 else | |
3363 { | |
3364 if (*ffname == NUL) /* re-edit with same file name */ | |
3365 { | |
3366 ffname = curbuf->b_ffname; | |
3367 sfname = curbuf->b_fname; | |
3368 } | |
3369 free_fname = fix_fname(ffname); /* may expand to full path name */ | |
3370 if (free_fname != NULL) | |
3371 ffname = free_fname; | |
3372 other_file = otherfile(ffname); | |
3373 #ifdef FEAT_SUN_WORKSHOP | |
3374 if (usingSunWorkShop && p_acd | |
3375 && (cp = vim_strrchr(sfname, '/')) != NULL) | |
3376 sfname = ++cp; | |
3377 #endif | |
3378 } | |
3379 } | |
3380 | |
3381 /* | |
3382 * if the file was changed we may not be allowed to abandon it | |
3383 * - if we are going to re-edit the same file | |
3384 * - or if we are the only window on this file and if ECMD_HIDE is FALSE | |
3385 */ | |
3386 if ( ((!other_file && !(flags & ECMD_OLDBUF)) | |
3387 || (curbuf->b_nwindows == 1 | |
3388 && !(flags & (ECMD_HIDE | ECMD_ADDBUF)))) | |
5464 | 3389 && check_changed(curbuf, (p_awa ? CCGD_AW : 0) |
3390 | (other_file ? 0 : CCGD_MULTWIN) | |
3391 | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0) | |
3392 | (eap == NULL ? 0 : CCGD_EXCMD))) | |
7 | 3393 { |
3394 if (fnum == 0 && other_file && ffname != NULL) | |
3395 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum); | |
3396 goto theend; | |
3397 } | |
3398 | |
3399 /* | |
3400 * End Visual mode before switching to another buffer, so the text can be | |
3401 * copied into the GUI selection buffer. | |
3402 */ | |
3403 reset_VIsual(); | |
3404 | |
716 | 3405 #ifdef FEAT_AUTOCMD |
3406 if ((command != NULL || newlnum > (linenr_T)0) | |
3407 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL) | |
3408 { | |
3409 int len; | |
3410 char_u *p; | |
3411 | |
3412 /* Set v:swapcommand for the SwapExists autocommands. */ | |
3413 if (command != NULL) | |
835 | 3414 len = (int)STRLEN(command) + 3; |
716 | 3415 else |
3416 len = 30; | |
3417 p = alloc((unsigned)len); | |
3418 if (p != NULL) | |
3419 { | |
3420 if (command != NULL) | |
3421 vim_snprintf((char *)p, len, ":%s\r", command); | |
3422 else | |
3423 vim_snprintf((char *)p, len, "%ldG", (long)newlnum); | |
3424 set_vim_var_string(VV_SWAPCOMMAND, p, -1); | |
3425 did_set_swapcommand = TRUE; | |
3426 vim_free(p); | |
3427 } | |
3428 } | |
3429 #endif | |
3430 | |
7 | 3431 /* |
3432 * If we are starting to edit another file, open a (new) buffer. | |
3433 * Otherwise we re-use the current buffer. | |
3434 */ | |
3435 if (other_file) | |
3436 { | |
3437 #ifdef FEAT_LISTCMDS | |
3438 if (!(flags & ECMD_ADDBUF)) | |
3439 #endif | |
3440 { | |
22 | 3441 if (!cmdmod.keepalt) |
3442 curwin->w_alt_fnum = curbuf->b_fnum; | |
1743 | 3443 if (oldwin != NULL) |
3444 buflist_altfpos(oldwin); | |
7 | 3445 } |
3446 | |
3447 if (fnum) | |
3448 buf = buflist_findnr(fnum); | |
3449 else | |
3450 { | |
3451 #ifdef FEAT_LISTCMDS | |
3452 if (flags & ECMD_ADDBUF) | |
3453 { | |
3454 linenr_T tlnum = 1L; | |
3455 | |
3456 if (command != NULL) | |
3457 { | |
3458 tlnum = atol((char *)command); | |
3459 if (tlnum <= 0) | |
3460 tlnum = 1L; | |
3461 } | |
3462 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED); | |
3463 goto theend; | |
3464 } | |
3465 #endif | |
3466 buf = buflist_new(ffname, sfname, 0L, | |
3467 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED)); | |
5816 | 3468 #ifdef FEAT_AUTOCMD |
3469 /* autocommands may change curwin and curbuf */ | |
3470 if (oldwin != NULL) | |
3471 oldwin = curwin; | |
3472 old_curbuf = curbuf; | |
3473 #endif | |
7 | 3474 } |
3475 if (buf == NULL) | |
3476 goto theend; | |
3477 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */ | |
3478 { | |
3479 oldbuf = FALSE; | |
3480 } | |
3481 else /* existing memfile */ | |
3482 { | |
3483 oldbuf = TRUE; | |
3484 (void)buf_check_timestamp(buf, FALSE); | |
3485 /* Check if autocommands made buffer invalid or changed the current | |
3486 * buffer. */ | |
3487 if (!buf_valid(buf) | |
3488 #ifdef FEAT_AUTOCMD | |
3489 || curbuf != old_curbuf | |
3490 #endif | |
3491 ) | |
3492 goto theend; | |
3493 #ifdef FEAT_EVAL | |
3494 if (aborting()) /* autocmds may abort script processing */ | |
3495 goto theend; | |
3496 #endif | |
3497 } | |
3498 | |
3499 /* May jump to last used line number for a loaded buffer or when asked | |
3500 * for explicitly */ | |
3501 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST) | |
3502 { | |
3503 pos = buflist_findfpos(buf); | |
3504 newlnum = pos->lnum; | |
3505 solcol = pos->col; | |
3506 } | |
3507 | |
3508 /* | |
3509 * Make the (new) buffer the one used by the current window. | |
3510 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE. | |
3511 * If the current buffer was empty and has no file name, curbuf | |
6639 | 3512 * is returned by buflist_new(), nothing to do here. |
7 | 3513 */ |
3514 if (buf != curbuf) | |
3515 { | |
3516 #ifdef FEAT_AUTOCMD | |
3517 /* | |
3518 * Be careful: The autocommands may delete any buffer and change | |
3519 * the current buffer. | |
3520 * - If the buffer we are going to edit is deleted, give up. | |
3521 * - If the current buffer is deleted, prefer to load the new | |
3522 * buffer when loading a buffer is required. This avoids | |
3523 * loading another buffer which then must be closed again. | |
3524 * - If we ended up in the new buffer already, need to skip a few | |
3525 * things, set auto_buf. | |
3526 */ | |
3527 if (buf->b_fname != NULL) | |
3528 new_name = vim_strsave(buf->b_fname); | |
3529 au_new_curbuf = buf; | |
3530 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); | |
3531 if (!buf_valid(buf)) /* new buffer has been deleted */ | |
3532 { | |
3533 delbuf_msg(new_name); /* frees new_name */ | |
3534 goto theend; | |
3535 } | |
3536 # ifdef FEAT_EVAL | |
3537 if (aborting()) /* autocmds may abort script processing */ | |
3538 { | |
3539 vim_free(new_name); | |
3540 goto theend; | |
3541 } | |
3542 # endif | |
3543 if (buf == curbuf) /* already in new buffer */ | |
3544 auto_buf = TRUE; | |
3545 else | |
3546 { | |
3547 if (curbuf == old_curbuf) | |
3548 #endif | |
3549 buf_copy_options(buf, BCO_ENTER); | |
3550 | |
3551 /* close the link to the current buffer */ | |
825 | 3552 u_sync(FALSE); |
1743 | 3553 close_buffer(oldwin, curbuf, |
3365 | 3554 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE); |
7 | 3555 |
3556 #ifdef FEAT_AUTOCMD | |
3242 | 3557 /* Autocommands may open a new window and leave oldwin open |
3558 * which leads to crashes since the above call sets | |
3559 * oldwin->w_buffer to NULL. */ | |
3560 if (curwin != oldwin && oldwin != aucmd_win | |
3561 && win_valid(oldwin) && oldwin->w_buffer == NULL) | |
3562 win_close(oldwin, FALSE); | |
3563 | |
7 | 3564 # ifdef FEAT_EVAL |
3565 if (aborting()) /* autocmds may abort script processing */ | |
3566 { | |
3567 vim_free(new_name); | |
3568 goto theend; | |
3569 } | |
3570 # endif | |
3571 /* Be careful again, like above. */ | |
3572 if (!buf_valid(buf)) /* new buffer has been deleted */ | |
3573 { | |
3574 delbuf_msg(new_name); /* frees new_name */ | |
3575 goto theend; | |
3576 } | |
3577 if (buf == curbuf) /* already in new buffer */ | |
3578 auto_buf = TRUE; | |
3579 else | |
3580 #endif | |
3581 { | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3582 #ifdef FEAT_SYN_HL |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3583 /* |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3584 * <VN> We could instead free the synblock |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3585 * and re-attach to buffer, perhaps. |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3586 */ |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3587 if (curwin->w_s == &(curwin->w_buffer->b_s)) |
3480 | 3588 curwin->w_s = &(buf->b_s); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3589 #endif |
7 | 3590 curwin->w_buffer = buf; |
3591 curbuf = buf; | |
3592 ++curbuf->b_nwindows; | |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
4903
diff
changeset
|
3593 |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
4903
diff
changeset
|
3594 /* Set 'fileformat', 'binary' and 'fenc' when forced. */ |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
4903
diff
changeset
|
3595 if (!oldbuf && eap != NULL) |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
4903
diff
changeset
|
3596 { |
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
4903
diff
changeset
|
3597 set_file_options(TRUE, eap); |
5242
f0361e297d9c
updated for version 7.4a.046
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
3598 #ifdef FEAT_MBYTE |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
4903
diff
changeset
|
3599 set_forced_fenc(eap); |
5242
f0361e297d9c
updated for version 7.4a.046
Bram Moolenaar <bram@vim.org>
parents:
5231
diff
changeset
|
3600 #endif |
5231
74d2f3188cd0
updated for version 7.4a.041
Bram Moolenaar <bram@vim.org>
parents:
4903
diff
changeset
|
3601 } |
7 | 3602 } |
3603 | |
3604 /* May get the window options from the last time this buffer | |
3605 * was in this window (or another window). If not used | |
3606 * before, reset the local window options to the global | |
3607 * values. Also restores old folding stuff. */ | |
1289 | 3608 get_winopts(curbuf); |
1185 | 3609 #ifdef FEAT_SPELL |
3610 did_get_winopts = TRUE; | |
3611 #endif | |
7 | 3612 |
3613 #ifdef FEAT_AUTOCMD | |
3614 } | |
3615 vim_free(new_name); | |
3616 au_new_curbuf = NULL; | |
3617 #endif | |
3618 } | |
3619 | |
3620 curwin->w_pcmark.lnum = 1; | |
3621 curwin->w_pcmark.col = 0; | |
3622 } | |
3623 else /* !other_file */ | |
3624 { | |
3625 if ( | |
3626 #ifdef FEAT_LISTCMDS | |
3627 (flags & ECMD_ADDBUF) || | |
3628 #endif | |
3629 check_fname() == FAIL) | |
3630 goto theend; | |
6531 | 3631 |
7 | 3632 oldbuf = (flags & ECMD_OLDBUF); |
3633 } | |
3634 | |
6365 | 3635 #ifdef FEAT_AUTOCMD |
3636 buf = curbuf; | |
3637 #endif | |
7 | 3638 if ((flags & ECMD_SET_HELP) || keep_help_flag) |
3639 { | |
6365 | 3640 prepare_help_buffer(); |
7 | 3641 } |
3642 else | |
3643 { | |
3644 /* Don't make a buffer listed if it's a help buffer. Useful when | |
3645 * using CTRL-O to go back to a help file. */ | |
3646 if (!curbuf->b_help) | |
3647 set_buflisted(TRUE); | |
3648 } | |
3649 | |
3650 #ifdef FEAT_AUTOCMD | |
3651 /* If autocommands change buffers under our fingers, forget about | |
3652 * editing the file. */ | |
3653 if (buf != curbuf) | |
3654 goto theend; | |
3655 # ifdef FEAT_EVAL | |
3656 if (aborting()) /* autocmds may abort script processing */ | |
3657 goto theend; | |
3658 # endif | |
3659 | |
3660 /* Since we are starting to edit a file, consider the filetype to be | |
3661 * unset. Helps for when an autocommand changes files and expects syntax | |
3662 * highlighting to work in the other file. */ | |
3663 did_filetype = FALSE; | |
3664 #endif | |
3665 | |
3666 /* | |
3667 * other_file oldbuf | |
3668 * FALSE FALSE re-edit same file, buffer is re-used | |
3669 * FALSE TRUE re-edit same file, nothing changes | |
3670 * TRUE FALSE start editing new file, new buffer | |
3671 * TRUE TRUE start editing in existing buffer (nothing to do) | |
3672 */ | |
3673 if (!other_file && !oldbuf) /* re-use the buffer */ | |
3674 { | |
3675 set_last_cursor(curwin); /* may set b_last_cursor */ | |
3676 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL) | |
3677 { | |
3678 newlnum = curwin->w_cursor.lnum; | |
3679 solcol = curwin->w_cursor.col; | |
3680 } | |
3681 #ifdef FEAT_AUTOCMD | |
3682 buf = curbuf; | |
3683 if (buf->b_fname != NULL) | |
3684 new_name = vim_strsave(buf->b_fname); | |
3685 else | |
3686 new_name = NULL; | |
3687 #endif | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3688 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur) |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3689 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3690 /* Save all the text, so that the reload can be undone. |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3691 * Sync first so that this is a separate undo-able action. */ |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3692 u_sync(FALSE); |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3693 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE) |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3694 == FAIL) |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3695 goto theend; |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3696 u_unchanged(curbuf); |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3697 buf_freeall(curbuf, BFA_KEEP_UNDO); |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3698 |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3699 /* tell readfile() not to clear or reload undo info */ |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3700 readfile_flags = READ_KEEP_UNDO; |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3701 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3702 else |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3703 buf_freeall(curbuf, 0); /* free all things for buffer */ |
7 | 3704 #ifdef FEAT_AUTOCMD |
3705 /* If autocommands deleted the buffer we were going to re-edit, give | |
3706 * up and jump to the end. */ | |
3707 if (!buf_valid(buf)) | |
3708 { | |
3709 delbuf_msg(new_name); /* frees new_name */ | |
3710 goto theend; | |
3711 } | |
3712 vim_free(new_name); | |
3713 | |
3714 /* If autocommands change buffers under our fingers, forget about | |
3715 * re-editing the file. Should do the buf_clear_file(), but perhaps | |
3716 * the autocommands changed the buffer... */ | |
3717 if (buf != curbuf) | |
3718 goto theend; | |
3719 # ifdef FEAT_EVAL | |
3720 if (aborting()) /* autocmds may abort script processing */ | |
3721 goto theend; | |
3722 # endif | |
3723 #endif | |
3724 buf_clear_file(curbuf); | |
3725 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */ | |
3726 curbuf->b_op_end.lnum = 0; | |
3727 } | |
3728 | |
3729 /* | |
3730 * If we get here we are sure to start editing | |
3731 */ | |
3732 /* don't redraw until the cursor is in the right line */ | |
3733 ++RedrawingDisabled; | |
3734 | |
3735 /* Assume success now */ | |
3736 retval = OK; | |
3737 | |
3738 /* | |
3739 * Reset cursor position, could be used by autocommands. | |
3740 */ | |
3741 check_cursor(); | |
3742 | |
3743 /* | |
3744 * Check if we are editing the w_arg_idx file in the argument list. | |
3745 */ | |
3746 check_arg_idx(curwin); | |
3747 | |
3748 #ifdef FEAT_AUTOCMD | |
3749 if (!auto_buf) | |
3750 #endif | |
3751 { | |
3752 /* | |
3753 * Set cursor and init window before reading the file and executing | |
3754 * autocommands. This allows for the autocommands to position the | |
3755 * cursor. | |
3756 */ | |
677 | 3757 curwin_init(); |
7 | 3758 |
3759 #ifdef FEAT_FOLDING | |
1370 | 3760 /* It's possible that all lines in the buffer changed. Need to update |
3761 * automatic folding for all windows where it's used. */ | |
3762 # ifdef FEAT_WINDOWS | |
3763 { | |
3764 win_T *win; | |
3765 tabpage_T *tp; | |
3766 | |
3767 FOR_ALL_TAB_WINDOWS(tp, win) | |
3768 if (win->w_buffer == curbuf) | |
3769 foldUpdateAll(win); | |
3770 } | |
3771 # else | |
7 | 3772 foldUpdateAll(curwin); |
1370 | 3773 # endif |
7 | 3774 #endif |
3775 | |
961 | 3776 /* Change directories when the 'acd' option is set. */ |
3777 DO_AUTOCHDIR | |
3778 | |
7 | 3779 /* |
3780 * Careful: open_buffer() and apply_autocmds() may change the current | |
3781 * buffer and window. | |
3782 */ | |
6702 | 3783 orig_pos = curwin->w_cursor; |
7 | 3784 topline = curwin->w_topline; |
3785 if (!oldbuf) /* need to read the file */ | |
3786 { | |
580 | 3787 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 3788 swap_exists_action = SEA_DIALOG; |
3789 #endif | |
3790 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */ | |
3791 | |
3792 /* | |
3793 * Open the buffer and read the file. | |
3794 */ | |
3795 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3796 if (should_abort(open_buffer(FALSE, eap, readfile_flags))) |
7 | 3797 retval = FAIL; |
3798 #else | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3799 (void)open_buffer(FALSE, eap, readfile_flags); |
7 | 3800 #endif |
3801 | |
580 | 3802 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 3803 if (swap_exists_action == SEA_QUIT) |
3804 retval = FAIL; | |
3805 handle_swap_exists(old_curbuf); | |
3806 #endif | |
3807 } | |
3808 #ifdef FEAT_AUTOCMD | |
3809 else | |
3810 { | |
23 | 3811 /* Read the modelines, but only to set window-local options. Any |
3812 * buffer-local options have already been set and may have been | |
3813 * changed by the user. */ | |
717 | 3814 do_modelines(OPT_WINONLY); |
23 | 3815 |
7 | 3816 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, |
3817 &retval); | |
3818 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf, | |
3819 &retval); | |
3820 } | |
3821 check_arg_idx(curwin); | |
3822 #endif | |
3823 | |
6702 | 3824 /* If autocommands change the cursor position or topline, we should |
3825 * keep it. Also when it moves within a line. */ | |
3826 if (!equalpos(curwin->w_cursor, orig_pos)) | |
7 | 3827 { |
3828 newlnum = curwin->w_cursor.lnum; | |
3829 newcol = curwin->w_cursor.col; | |
3830 } | |
3831 if (curwin->w_topline == topline) | |
3832 topline = 0; | |
3833 | |
3834 /* Even when cursor didn't move we need to recompute topline. */ | |
3835 changed_line_abv_curs(); | |
3836 | |
3837 #ifdef FEAT_TITLE | |
3838 maketitle(); | |
3839 #endif | |
3840 } | |
3841 | |
3842 #ifdef FEAT_DIFF | |
3843 /* Tell the diff stuff that this buffer is new and/or needs updating. | |
3844 * Also needed when re-editing the same buffer, because unloading will | |
3845 * have removed it as a diff buffer. */ | |
673 | 3846 if (curwin->w_p_diff) |
3847 { | |
3848 diff_buf_add(curbuf); | |
3849 diff_invalidate(curbuf); | |
3850 } | |
7 | 3851 #endif |
3852 | |
1185 | 3853 #ifdef FEAT_SPELL |
3854 /* If the window options were changed may need to set the spell language. | |
3855 * Can only do this after the buffer has been properly setup. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3856 if (did_get_winopts && 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:
2210
diff
changeset
|
3857 (void)did_set_spelllang(curwin); |
1185 | 3858 #endif |
3859 | |
7 | 3860 if (command == NULL) |
3861 { | |
3862 if (newcol >= 0) /* position set by autocommands */ | |
3863 { | |
3864 curwin->w_cursor.lnum = newlnum; | |
3865 curwin->w_cursor.col = newcol; | |
3866 check_cursor(); | |
3867 } | |
3868 else if (newlnum > 0) /* line number from caller or old position */ | |
3869 { | |
3870 curwin->w_cursor.lnum = newlnum; | |
3871 check_cursor_lnum(); | |
3872 if (solcol >= 0 && !p_sol) | |
3873 { | |
3874 /* 'sol' is off: Use last known column. */ | |
3875 curwin->w_cursor.col = solcol; | |
3876 check_cursor_col(); | |
3877 #ifdef FEAT_VIRTUALEDIT | |
3878 curwin->w_cursor.coladd = 0; | |
3879 #endif | |
3880 curwin->w_set_curswant = TRUE; | |
3881 } | |
3882 else | |
3883 beginline(BL_SOL | BL_FIX); | |
3884 } | |
3885 else /* no line number, go to last line in Ex mode */ | |
3886 { | |
3887 if (exmode_active) | |
3888 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; | |
3889 beginline(BL_WHITE | BL_FIX); | |
3890 } | |
3891 } | |
3892 | |
3893 #ifdef FEAT_WINDOWS | |
3894 /* Check if cursors in other windows on the same buffer are still valid */ | |
3895 check_lnums(FALSE); | |
3896 #endif | |
3897 | |
3898 /* | |
3899 * Did not read the file, need to show some info about the file. | |
3900 * Do this after setting the cursor. | |
3901 */ | |
3902 if (oldbuf | |
3903 #ifdef FEAT_AUTOCMD | |
3904 && !auto_buf | |
3905 #endif | |
3906 ) | |
3907 { | |
3908 int msg_scroll_save = msg_scroll; | |
3909 | |
3910 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file | |
3911 * message. */ | |
3912 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) | |
3913 msg_scroll = FALSE; | |
3914 if (!msg_scroll) /* wait a bit when overwriting an error msg */ | |
3915 check_for_delay(FALSE); | |
3916 msg_start(); | |
3917 msg_scroll = msg_scroll_save; | |
3918 msg_scrolled_ign = TRUE; | |
3919 | |
3920 fileinfo(FALSE, TRUE, FALSE); | |
3921 | |
3922 msg_scrolled_ign = FALSE; | |
3923 } | |
3924 | |
3925 if (command != NULL) | |
3926 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE); | |
3927 | |
3928 #ifdef FEAT_KEYMAP | |
3929 if (curbuf->b_kmap_state & KEYMAP_INIT) | |
1869 | 3930 (void)keymap_init(); |
7 | 3931 #endif |
3932 | |
3933 --RedrawingDisabled; | |
3934 if (!skip_redraw) | |
3935 { | |
3936 n = p_so; | |
3937 if (topline == 0 && command == NULL) | |
3938 p_so = 999; /* force cursor halfway the window */ | |
3939 update_topline(); | |
3940 #ifdef FEAT_SCROLLBIND | |
3941 curwin->w_scbind_pos = curwin->w_topline; | |
3942 #endif | |
3943 p_so = n; | |
3944 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */ | |
3945 } | |
3946 | |
3947 if (p_im) | |
3948 need_start_insertmode = TRUE; | |
3949 | |
961 | 3950 /* Change directories when the 'acd' option is set. */ |
3951 DO_AUTOCHDIR | |
821 | 3952 |
3953 #if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) | |
2209
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2178
diff
changeset
|
3954 if (curbuf->b_ffname != NULL) |
7 | 3955 { |
3956 # ifdef FEAT_SUN_WORKSHOP | |
2209
d0ddf7ba1630
Included the patch to support netbeans in a terminal.
Bram Moolenaar <bram@vim.org>
parents:
2178
diff
changeset
|
3957 if (gui.in_use && usingSunWorkShop) |
7 | 3958 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro); |
3959 # endif | |
3960 # ifdef FEAT_NETBEANS_INTG | |
2210 | 3961 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP) |
34 | 3962 netbeans_file_opened(curbuf); |
7 | 3963 # endif |
3964 } | |
3965 #endif | |
3966 | |
3967 theend: | |
716 | 3968 #ifdef FEAT_AUTOCMD |
3969 if (did_set_swapcommand) | |
3970 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1); | |
3971 #endif | |
7 | 3972 #ifdef FEAT_BROWSE |
3973 vim_free(browse_file); | |
3974 #endif | |
3975 vim_free(free_fname); | |
3976 return retval; | |
3977 } | |
3978 | |
3979 #ifdef FEAT_AUTOCMD | |
3980 static void | |
3981 delbuf_msg(name) | |
3982 char_u *name; | |
3983 { | |
3984 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"), | |
3985 name == NULL ? (char_u *)"" : name); | |
3986 vim_free(name); | |
3987 au_new_curbuf = NULL; | |
3988 } | |
3989 #endif | |
3990 | |
169 | 3991 static int append_indent = 0; /* autoindent for first line */ |
3992 | |
7 | 3993 /* |
3994 * ":insert" and ":append", also used by ":change" | |
3995 */ | |
3996 void | |
3997 ex_append(eap) | |
3998 exarg_T *eap; | |
3999 { | |
4000 char_u *theline; | |
4001 int did_undo = FALSE; | |
4002 linenr_T lnum = eap->line2; | |
165 | 4003 int indent = 0; |
4004 char_u *p; | |
4005 int vcol; | |
4006 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY); | |
7 | 4007 |
169 | 4008 /* the ! flag toggles autoindent */ |
4009 if (eap->forceit) | |
4010 curbuf->b_p_ai = !curbuf->b_p_ai; | |
4011 | |
4012 /* First autoindent comes from the line we start on */ | |
4013 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0) | |
4014 append_indent = get_indent_lnum(lnum); | |
4015 | |
7 | 4016 if (eap->cmdidx != CMD_append) |
4017 --lnum; | |
4018 | |
165 | 4019 /* when the buffer is empty append to line 0 and delete the dummy line */ |
4020 if (empty && lnum == 1) | |
4021 lnum = 0; | |
4022 | |
7 | 4023 State = INSERT; /* behave like in Insert mode */ |
4024 if (curbuf->b_p_iminsert == B_IMODE_LMAP) | |
4025 State |= LANGMAP; | |
165 | 4026 |
408 | 4027 for (;;) |
7 | 4028 { |
4029 msg_scroll = TRUE; | |
4030 need_wait_return = FALSE; | |
169 | 4031 if (curbuf->b_p_ai) |
4032 { | |
4033 if (append_indent >= 0) | |
4034 { | |
4035 indent = append_indent; | |
4036 append_indent = -1; | |
4037 } | |
4038 else if (lnum > 0) | |
4039 indent = get_indent_lnum(lnum); | |
4040 } | |
4041 ex_keep_indent = FALSE; | |
7 | 4042 if (eap->getline == NULL) |
169 | 4043 { |
4044 /* No getline() function, use the lines that follow. This ends | |
4045 * when there is no more. */ | |
4046 if (eap->nextcmd == NULL || *eap->nextcmd == NUL) | |
4047 break; | |
4048 p = vim_strchr(eap->nextcmd, NL); | |
4049 if (p == NULL) | |
4050 p = eap->nextcmd + STRLEN(eap->nextcmd); | |
4051 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd)); | |
4052 if (*p != NUL) | |
4053 ++p; | |
4054 eap->nextcmd = p; | |
4055 } | |
7 | 4056 else |
6164 | 4057 { |
4058 int save_State = State; | |
4059 | |
4060 /* Set State to avoid the cursor shape to be set to INSERT mode | |
4061 * when getline() returns. */ | |
4062 State = CMDLINE; | |
7 | 4063 theline = eap->getline( |
4064 #ifdef FEAT_EVAL | |
76 | 4065 eap->cstack->cs_looplevel > 0 ? -1 : |
7 | 4066 #endif |
165 | 4067 NUL, eap->cookie, indent); |
6164 | 4068 State = save_State; |
4069 } | |
7 | 4070 lines_left = Rows - 1; |
165 | 4071 if (theline == NULL) |
4072 break; | |
4073 | |
169 | 4074 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */ |
4075 if (ex_keep_indent) | |
4076 append_indent = indent; | |
4077 | |
165 | 4078 /* Look for the "." after automatic indent. */ |
4079 vcol = 0; | |
4080 for (p = theline; indent > vcol; ++p) | |
4081 { | |
4082 if (*p == ' ') | |
4083 ++vcol; | |
4084 else if (*p == TAB) | |
4085 vcol += 8 - vcol % 8; | |
4086 else | |
4087 break; | |
4088 } | |
4089 if ((p[0] == '.' && p[1] == NUL) | |
321 | 4090 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0)) |
4091 == FAIL)) | |
7 | 4092 { |
4093 vim_free(theline); | |
4094 break; | |
4095 } | |
4096 | |
169 | 4097 /* don't use autoindent if nothing was typed. */ |
4098 if (p[0] == NUL) | |
4099 theline[0] = NUL; | |
4100 | |
7 | 4101 did_undo = TRUE; |
4102 ml_append(lnum, theline, (colnr_T)0, FALSE); | |
4103 appended_lines_mark(lnum, 1L); | |
4104 | |
4105 vim_free(theline); | |
4106 ++lnum; | |
165 | 4107 |
4108 if (empty) | |
4109 { | |
4110 ml_delete(2L, FALSE); | |
4111 empty = FALSE; | |
4112 } | |
7 | 4113 } |
4114 State = NORMAL; | |
4115 | |
169 | 4116 if (eap->forceit) |
4117 curbuf->b_p_ai = !curbuf->b_p_ai; | |
4118 | |
7 | 4119 /* "start" is set to eap->line2+1 unless that position is invalid (when |
1227 | 4120 * eap->line2 pointed to the end of the buffer and nothing was appended) |
7 | 4121 * "end" is set to lnum when something has been appended, otherwise |
4122 * it is the same than "start" -- Acevedo */ | |
4123 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ? | |
4124 eap->line2 + 1 : curbuf->b_ml.ml_line_count; | |
4125 if (eap->cmdidx != CMD_append) | |
4126 --curbuf->b_op_start.lnum; | |
4127 curbuf->b_op_end.lnum = (eap->line2 < lnum) | |
4128 ? lnum : curbuf->b_op_start.lnum; | |
4129 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; | |
4130 curwin->w_cursor.lnum = lnum; | |
4131 check_cursor_lnum(); | |
4132 beginline(BL_SOL | BL_FIX); | |
4133 | |
4134 need_wait_return = FALSE; /* don't use wait_return() now */ | |
4135 ex_no_reprint = TRUE; | |
4136 } | |
4137 | |
4138 /* | |
4139 * ":change" | |
4140 */ | |
4141 void | |
4142 ex_change(eap) | |
4143 exarg_T *eap; | |
4144 { | |
4145 linenr_T lnum; | |
4146 | |
4147 if (eap->line2 >= eap->line1 | |
4148 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL) | |
4149 return; | |
4150 | |
169 | 4151 /* the ! flag toggles autoindent */ |
4152 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai) | |
4153 append_indent = get_indent_lnum(eap->line1); | |
4154 | |
7 | 4155 for (lnum = eap->line2; lnum >= eap->line1; --lnum) |
4156 { | |
4157 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ | |
4158 break; | |
4159 ml_delete(eap->line1, FALSE); | |
4160 } | |
1929 | 4161 |
4162 /* make sure the cursor is not beyond the end of the file now */ | |
4163 check_cursor_lnum(); | |
7 | 4164 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum)); |
4165 | |
4166 /* ":append" on the line above the deleted lines. */ | |
4167 eap->line2 = eap->line1; | |
4168 ex_append(eap); | |
4169 } | |
4170 | |
4171 void | |
4172 ex_z(eap) | |
4173 exarg_T *eap; | |
4174 { | |
4175 char_u *x; | |
165 | 4176 int bigness; |
4177 char_u *kind; | |
7 | 4178 int minus = 0; |
4179 linenr_T start, end, curs, i; | |
4180 int j; | |
4181 linenr_T lnum = eap->line2; | |
4182 | |
165 | 4183 /* Vi compatible: ":z!" uses display height, without a count uses |
4184 * 'scroll' */ | |
4185 if (eap->forceit) | |
4186 bigness = curwin->w_height; | |
5678 | 4187 #ifdef FEAT_WINDOWS |
4188 else if (firstwin != lastwin) | |
4189 bigness = curwin->w_height - 3; | |
4190 #endif | |
4191 else | |
165 | 4192 bigness = curwin->w_p_scr * 2; |
7 | 4193 if (bigness < 1) |
4194 bigness = 1; | |
4195 | |
4196 x = eap->arg; | |
165 | 4197 kind = x; |
4198 if (*kind == '-' || *kind == '+' || *kind == '=' | |
4199 || *kind == '^' || *kind == '.') | |
4200 ++x; | |
4201 while (*x == '-' || *x == '+') | |
7 | 4202 ++x; |
4203 | |
4204 if (*x != 0) | |
4205 { | |
4206 if (!VIM_ISDIGIT(*x)) | |
4207 { | |
4208 EMSG(_("E144: non-numeric argument to :z")); | |
4209 return; | |
4210 } | |
4211 else | |
165 | 4212 { |
7 | 4213 bigness = atoi((char *)x); |
165 | 4214 p_window = bigness; |
169 | 4215 if (*kind == '=') |
4216 bigness += 2; | |
165 | 4217 } |
7 | 4218 } |
4219 | |
165 | 4220 /* the number of '-' and '+' multiplies the distance */ |
4221 if (*kind == '-' || *kind == '+') | |
4222 for (x = kind + 1; *x == *kind; ++x) | |
4223 ; | |
4224 | |
4225 switch (*kind) | |
7 | 4226 { |
4227 case '-': | |
2881 | 4228 start = lnum - bigness * (linenr_T)(x - kind) + 1; |
4229 end = start + bigness - 1; | |
165 | 4230 curs = end; |
7 | 4231 break; |
4232 | |
4233 case '=': | |
159 | 4234 start = lnum - (bigness + 1) / 2 + 1; |
4235 end = lnum + (bigness + 1) / 2 - 1; | |
7 | 4236 curs = lnum; |
4237 minus = 1; | |
4238 break; | |
4239 | |
4240 case '^': | |
4241 start = lnum - bigness * 2; | |
4242 end = lnum - bigness; | |
4243 curs = lnum - bigness; | |
4244 break; | |
4245 | |
4246 case '.': | |
159 | 4247 start = lnum - (bigness + 1) / 2 + 1; |
4248 end = lnum + (bigness + 1) / 2 - 1; | |
7 | 4249 curs = end; |
4250 break; | |
4251 | |
4252 default: /* '+' */ | |
4253 start = lnum; | |
165 | 4254 if (*kind == '+') |
835 | 4255 start += bigness * (linenr_T)(x - kind - 1) + 1; |
169 | 4256 else if (eap->addr_count == 0) |
4257 ++start; | |
4258 end = start + bigness - 1; | |
7 | 4259 curs = end; |
4260 break; | |
4261 } | |
4262 | |
4263 if (start < 1) | |
4264 start = 1; | |
4265 | |
4266 if (end > curbuf->b_ml.ml_line_count) | |
4267 end = curbuf->b_ml.ml_line_count; | |
4268 | |
4269 if (curs > curbuf->b_ml.ml_line_count) | |
4270 curs = curbuf->b_ml.ml_line_count; | |
4271 | |
4272 for (i = start; i <= end; i++) | |
4273 { | |
4274 if (minus && i == lnum) | |
4275 { | |
4276 msg_putchar('\n'); | |
4277 | |
4278 for (j = 1; j < Columns; j++) | |
4279 msg_putchar('-'); | |
4280 } | |
4281 | |
169 | 4282 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST); |
7 | 4283 |
4284 if (minus && i == lnum) | |
4285 { | |
4286 msg_putchar('\n'); | |
4287 | |
4288 for (j = 1; j < Columns; j++) | |
4289 msg_putchar('-'); | |
4290 } | |
4291 } | |
4292 | |
4293 curwin->w_cursor.lnum = curs; | |
4294 ex_no_reprint = TRUE; | |
4295 } | |
4296 | |
4297 /* | |
4298 * Check if the restricted flag is set. | |
4299 * If so, give an error message and return TRUE. | |
4300 * Otherwise, return FALSE. | |
4301 */ | |
4302 int | |
4303 check_restricted() | |
4304 { | |
4305 if (restricted) | |
4306 { | |
4307 EMSG(_("E145: Shell commands not allowed in rvim")); | |
4308 return TRUE; | |
4309 } | |
4310 return FALSE; | |
4311 } | |
4312 | |
4313 /* | |
4314 * Check if the secure flag is set (.exrc or .vimrc in current directory). | |
4315 * If so, give an error message and return TRUE. | |
4316 * Otherwise, return FALSE. | |
4317 */ | |
4318 int | |
4319 check_secure() | |
4320 { | |
4321 if (secure) | |
4322 { | |
4323 secure = 2; | |
4324 EMSG(_(e_curdir)); | |
4325 return TRUE; | |
4326 } | |
4327 #ifdef HAVE_SANDBOX | |
4328 /* | |
4329 * In the sandbox more things are not allowed, including the things | |
4330 * disallowed in secure mode. | |
4331 */ | |
4332 if (sandbox != 0) | |
4333 { | |
4334 EMSG(_(e_sandbox)); | |
4335 return TRUE; | |
4336 } | |
4337 #endif | |
4338 return FALSE; | |
4339 } | |
4340 | |
4341 static char_u *old_sub = NULL; /* previous substitute pattern */ | |
4342 static int global_need_beginline; /* call beginline() after ":g" */ | |
4343 | |
4344 /* do_sub() | |
4345 * | |
4346 * Perform a substitution from line eap->line1 to line eap->line2 using the | |
4347 * command pointed to by eap->arg which should be of the form: | |
4348 * | |
4349 * /pattern/substitution/{flags} | |
4350 * | |
4351 * The usual escapes are supported as described in the regexp docs. | |
4352 */ | |
4353 void | |
4354 do_sub(eap) | |
4355 exarg_T *eap; | |
4356 { | |
4357 linenr_T lnum; | |
4358 long i = 0; | |
4359 regmmatch_T regmatch; | |
4360 static int do_all = FALSE; /* do multiple substitutions per line */ | |
4361 static int do_ask = FALSE; /* ask for confirmation */ | |
170 | 4362 static int do_count = FALSE; /* count only */ |
7 | 4363 static int do_error = TRUE; /* if false, ignore errors */ |
4364 static int do_print = FALSE; /* print last line with subs. */ | |
169 | 4365 static int do_list = FALSE; /* list last line with subs. */ |
4366 static int do_number = FALSE; /* list last line with line nr*/ | |
7 | 4367 static int do_ic = 0; /* ignore case flag */ |
6789 | 4368 int save_do_all; /* remember user specified 'g' flag */ |
4369 int save_do_ask; /* remember user specified 'c' flag */ | |
7 | 4370 char_u *pat = NULL, *sub = NULL; /* init for GCC */ |
4371 int delimiter; | |
4372 int sublen; | |
4373 int got_quit = FALSE; | |
4374 int got_match = FALSE; | |
4375 int temp; | |
4376 int which_pat; | |
4377 char_u *cmd; | |
4378 int save_State; | |
165 | 4379 linenr_T first_line = 0; /* first changed line */ |
4380 linenr_T last_line= 0; /* below last changed line AFTER the | |
7 | 4381 * change */ |
4382 linenr_T old_line_count = curbuf->b_ml.ml_line_count; | |
4383 linenr_T line2; | |
165 | 4384 long nmatch; /* number of lines in match */ |
4385 char_u *sub_firstline; /* allocated copy of first sub line */ | |
4386 int endcolumn = FALSE; /* cursor in last column when done */ | |
170 | 4387 pos_T old_cursor = curwin->w_cursor; |
2126
e038754d419a
updated for version 7.2.408
Bram Moolenaar <bram@zimbu.org>
parents:
1929
diff
changeset
|
4388 int start_nsubs; |
3736 | 4389 #ifdef FEAT_EVAL |
5867 | 4390 int save_ma = 0; |
3736 | 4391 #endif |
7 | 4392 |
4393 cmd = eap->arg; | |
4394 if (!global_busy) | |
4395 { | |
4396 sub_nsubs = 0; | |
4397 sub_nlines = 0; | |
4398 } | |
2126
e038754d419a
updated for version 7.2.408
Bram Moolenaar <bram@zimbu.org>
parents:
1929
diff
changeset
|
4399 start_nsubs = sub_nsubs; |
7 | 4400 |
4401 if (eap->cmdidx == CMD_tilde) | |
4402 which_pat = RE_LAST; /* use last used regexp */ | |
4403 else | |
4404 which_pat = RE_SUBST; /* use last substitute regexp */ | |
4405 | |
4406 /* new pattern and substitution */ | |
4407 if (eap->cmd[0] == 's' && *cmd != NUL && !vim_iswhite(*cmd) | |
4408 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL) | |
4409 { | |
4410 /* don't accept alphanumeric for separator */ | |
4411 if (isalpha(*cmd)) | |
4412 { | |
4413 EMSG(_("E146: Regular expressions can't be delimited by letters")); | |
4414 return; | |
4415 } | |
4416 /* | |
4417 * undocumented vi feature: | |
4418 * "\/sub/" and "\?sub?" use last used search pattern (almost like | |
4419 * //sub/r). "\&sub&" use last substitute pattern (like //sub/). | |
4420 */ | |
4421 if (*cmd == '\\') | |
4422 { | |
4423 ++cmd; | |
4424 if (vim_strchr((char_u *)"/?&", *cmd) == NULL) | |
4425 { | |
4426 EMSG(_(e_backslash)); | |
4427 return; | |
4428 } | |
4429 if (*cmd != '&') | |
4430 which_pat = RE_SEARCH; /* use last '/' pattern */ | |
4431 pat = (char_u *)""; /* empty search pattern */ | |
4432 delimiter = *cmd++; /* remember delimiter character */ | |
4433 } | |
4434 else /* find the end of the regexp */ | |
4435 { | |
1466 | 4436 #ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */ |
4437 if (p_altkeymap && curwin->w_p_rl) | |
4438 lrF_sub(cmd); | |
4439 #endif | |
7 | 4440 which_pat = RE_LAST; /* use last used regexp */ |
4441 delimiter = *cmd++; /* remember delimiter character */ | |
4442 pat = cmd; /* remember start of search pat */ | |
4443 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg); | |
4444 if (cmd[0] == delimiter) /* end delimiter found */ | |
4445 *cmd++ = NUL; /* replace it with a NUL */ | |
4446 } | |
4447 | |
4448 /* | |
4449 * Small incompatibility: vi sees '\n' as end of the command, but in | |
4450 * Vim we want to use '\n' to find/substitute a NUL. | |
4451 */ | |
4452 sub = cmd; /* remember the start of the substitution */ | |
4453 | |
4454 while (cmd[0]) | |
4455 { | |
4456 if (cmd[0] == delimiter) /* end delimiter found */ | |
4457 { | |
4458 *cmd++ = NUL; /* replace it with a NUL */ | |
4459 break; | |
4460 } | |
4461 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */ | |
4462 ++cmd; | |
39 | 4463 mb_ptr_adv(cmd); |
7 | 4464 } |
4465 | |
4466 if (!eap->skip) | |
4467 { | |
169 | 4468 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */ |
4469 if (STRCMP(sub, "%") == 0 | |
4470 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL) | |
4471 { | |
4472 if (old_sub == NULL) /* there is no previous command */ | |
4473 { | |
4474 EMSG(_(e_nopresub)); | |
4475 return; | |
4476 } | |
4477 sub = old_sub; | |
4478 } | |
4479 else | |
4480 { | |
4481 vim_free(old_sub); | |
4482 old_sub = vim_strsave(sub); | |
4483 } | |
7 | 4484 } |
4485 } | |
4486 else if (!eap->skip) /* use previous pattern and substitution */ | |
4487 { | |
4488 if (old_sub == NULL) /* there is no previous command */ | |
4489 { | |
4490 EMSG(_(e_nopresub)); | |
4491 return; | |
4492 } | |
4493 pat = NULL; /* search_regcomp() will use previous pattern */ | |
4494 sub = old_sub; | |
163 | 4495 |
4496 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the | |
4497 * last column after using "$". */ | |
4498 endcolumn = (curwin->w_curswant == MAXCOL); | |
7 | 4499 } |
4500 | |
5776 | 4501 /* Recognize ":%s/\n//" and turn it into a join command, which is much |
4502 * more efficient. | |
4503 * TODO: find a generic solution to make line-joining operations more | |
4504 * efficient, avoid allocating a string that grows in size. | |
4505 */ | |
5802 | 4506 if (pat != NULL && STRCMP(pat, "\\n") == 0 |
5776 | 4507 && *sub == NUL |
4508 && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l' | |
4509 || *cmd == 'p' || *cmd == '#')))) | |
4510 { | |
6426 | 4511 linenr_T joined_lines_count; |
4512 | |
5776 | 4513 curwin->w_cursor.lnum = eap->line1; |
4514 if (*cmd == 'l') | |
4515 eap->flags = EXFLAG_LIST; | |
4516 else if (*cmd == '#') | |
4517 eap->flags = EXFLAG_NR; | |
4518 else if (*cmd == 'p') | |
4519 eap->flags = EXFLAG_PRINT; | |
4520 | |
6426 | 4521 /* The number of lines joined is the number of lines in the range plus |
4522 * one. One less when the last line is included. */ | |
4523 joined_lines_count = eap->line2 - eap->line1 + 1; | |
4524 if (eap->line2 < curbuf->b_ml.ml_line_count) | |
4525 ++joined_lines_count; | |
4526 if (joined_lines_count > 1) | |
4527 { | |
4528 (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE); | |
4529 sub_nsubs = joined_lines_count - 1; | |
4530 sub_nlines = 1; | |
4531 (void)do_sub_msg(FALSE); | |
4532 ex_may_print(eap); | |
4533 } | |
4534 | |
4535 if (!cmdmod.keeppatterns) | |
4536 save_re_pat(RE_SUBST, pat, p_magic); | |
4537 #ifdef FEAT_CMDHIST | |
4538 /* put pattern in history */ | |
4539 add_to_history(HIST_SEARCH, pat, TRUE, NUL); | |
4540 #endif | |
4541 | |
5776 | 4542 return; |
4543 } | |
4544 | |
7 | 4545 /* |
4546 * Find trailing options. When '&' is used, keep old options. | |
4547 */ | |
4548 if (*cmd == '&') | |
4549 ++cmd; | |
4550 else | |
4551 { | |
4552 if (!p_ed) | |
4553 { | |
4554 if (p_gd) /* default is global on */ | |
4555 do_all = TRUE; | |
4556 else | |
4557 do_all = FALSE; | |
4558 do_ask = FALSE; | |
4559 } | |
4560 do_error = TRUE; | |
4561 do_print = FALSE; | |
587 | 4562 do_count = FALSE; |
1321 | 4563 do_number = FALSE; |
7 | 4564 do_ic = 0; |
4565 } | |
4566 while (*cmd) | |
4567 { | |
4568 /* | |
4569 * Note that 'g' and 'c' are always inverted, also when p_ed is off. | |
4570 * 'r' is never inverted. | |
4571 */ | |
4572 if (*cmd == 'g') | |
4573 do_all = !do_all; | |
4574 else if (*cmd == 'c') | |
4575 do_ask = !do_ask; | |
170 | 4576 else if (*cmd == 'n') |
4577 do_count = TRUE; | |
7 | 4578 else if (*cmd == 'e') |
4579 do_error = !do_error; | |
4580 else if (*cmd == 'r') /* use last used regexp */ | |
4581 which_pat = RE_LAST; | |
4582 else if (*cmd == 'p') | |
4583 do_print = TRUE; | |
169 | 4584 else if (*cmd == '#') |
4585 { | |
4586 do_print = TRUE; | |
4587 do_number = TRUE; | |
4588 } | |
4589 else if (*cmd == 'l') | |
4590 { | |
4591 do_print = TRUE; | |
4592 do_list = TRUE; | |
4593 } | |
7 | 4594 else if (*cmd == 'i') /* ignore case */ |
4595 do_ic = 'i'; | |
4596 else if (*cmd == 'I') /* don't ignore case */ | |
4597 do_ic = 'I'; | |
4598 else | |
4599 break; | |
4600 ++cmd; | |
4601 } | |
170 | 4602 if (do_count) |
4603 do_ask = FALSE; | |
7 | 4604 |
6789 | 4605 save_do_all = do_all; |
4606 save_do_ask = do_ask; | |
4607 | |
7 | 4608 /* |
4609 * check for a trailing count | |
4610 */ | |
4611 cmd = skipwhite(cmd); | |
4612 if (VIM_ISDIGIT(*cmd)) | |
4613 { | |
4614 i = getdigits(&cmd); | |
4615 if (i <= 0 && !eap->skip && do_error) | |
4616 { | |
4617 EMSG(_(e_zerocount)); | |
4618 return; | |
4619 } | |
4620 eap->line1 = eap->line2; | |
4621 eap->line2 += i - 1; | |
4622 if (eap->line2 > curbuf->b_ml.ml_line_count) | |
4623 eap->line2 = curbuf->b_ml.ml_line_count; | |
4624 } | |
4625 | |
4626 /* | |
4627 * check for trailing command or garbage | |
4628 */ | |
4629 cmd = skipwhite(cmd); | |
4630 if (*cmd && *cmd != '"') /* if not end-of-line or comment */ | |
4631 { | |
4632 eap->nextcmd = check_nextcmd(cmd); | |
4633 if (eap->nextcmd == NULL) | |
4634 { | |
4635 EMSG(_(e_trailing)); | |
4636 return; | |
4637 } | |
4638 } | |
4639 | |
4640 if (eap->skip) /* not executing commands, only parsing */ | |
4641 return; | |
4642 | |
823 | 4643 if (!do_count && !curbuf->b_p_ma) |
4644 { | |
825 | 4645 /* Substitution is not allowed in non-'modifiable' buffer */ |
823 | 4646 EMSG(_(e_modifiable)); |
4647 return; | |
4648 } | |
4649 | |
7 | 4650 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, ®match) == FAIL) |
4651 { | |
4652 if (do_error) | |
4653 EMSG(_(e_invcmd)); | |
4654 return; | |
4655 } | |
4656 | |
4657 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */ | |
4658 if (do_ic == 'i') | |
4659 regmatch.rmm_ic = TRUE; | |
4660 else if (do_ic == 'I') | |
4661 regmatch.rmm_ic = FALSE; | |
4662 | |
4663 sub_firstline = NULL; | |
4664 | |
4665 /* | |
4666 * ~ in the substitute pattern is replaced with the old pattern. | |
4667 * We do it here once to avoid it to be replaced over and over again. | |
4668 * But don't do it when it starts with "\=", then it's an expression. | |
4669 */ | |
4670 if (!(sub[0] == '\\' && sub[1] == '=')) | |
4671 sub = regtilde(sub, p_magic); | |
4672 | |
4673 /* | |
4674 * Check for a match on each line. | |
4675 */ | |
4676 line2 = eap->line2; | |
4677 for (lnum = eap->line1; lnum <= line2 && !(got_quit | |
4678 #if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) | |
4679 || aborting() | |
4680 #endif | |
4681 ); ++lnum) | |
4682 { | |
1521 | 4683 nmatch = vim_regexec_multi(®match, curwin, curbuf, lnum, |
4684 (colnr_T)0, NULL); | |
7 | 4685 if (nmatch) |
4686 { | |
4687 colnr_T copycol; | |
4688 colnr_T matchcol; | |
4689 colnr_T prev_matchcol = MAXCOL; | |
4690 char_u *new_end, *new_start = NULL; | |
4691 unsigned new_start_len = 0; | |
4692 char_u *p1; | |
4693 int did_sub = FALSE; | |
4694 int lastone; | |
1872 | 4695 int len, copy_len, needed_len; |
7 | 4696 long nmatch_tl = 0; /* nr of lines matched below lnum */ |
4697 int do_again; /* do it again after joining lines */ | |
15 | 4698 int skip_match = FALSE; |
1499 | 4699 linenr_T sub_firstlnum; /* nr of first sub line */ |
7 | 4700 |
4701 /* | |
4702 * The new text is build up step by step, to avoid too much | |
4703 * copying. There are these pieces: | |
1581 | 4704 * sub_firstline The old text, unmodified. |
7 | 4705 * copycol Column in the old text where we started |
4706 * looking for a match; from here old text still | |
4707 * needs to be copied to the new text. | |
4708 * matchcol Column number of the old text where to look | |
4709 * for the next match. It's just after the | |
4710 * previous match or one further. | |
4711 * prev_matchcol Column just after the previous match (if any). | |
4712 * Mostly equal to matchcol, except for the first | |
4713 * match and after skipping an empty match. | |
4714 * regmatch.*pos Where the pattern matched in the old text. | |
4715 * new_start The new text, all that has been produced so | |
4716 * far. | |
4717 * new_end The new text, where to append new text. | |
4718 * | |
1499 | 4719 * lnum The line number where we found the start of |
4720 * the match. Can be below the line we searched | |
4721 * when there is a \n before a \zs in the | |
4722 * pattern. | |
7 | 4723 * sub_firstlnum The line number in the buffer where to look |
4724 * for a match. Can be different from "lnum" | |
4725 * when the pattern or substitute string contains | |
4726 * line breaks. | |
4727 * | |
4728 * Special situations: | |
4729 * - When the substitute string contains a line break, the part up | |
4730 * to the line break is inserted in the text, but the copy of | |
4731 * the original line is kept. "sub_firstlnum" is adjusted for | |
4732 * the inserted lines. | |
4733 * - When the matched pattern contains a line break, the old line | |
4734 * is taken from the line at the end of the pattern. The lines | |
4735 * in the match are deleted later, "sub_firstlnum" is adjusted | |
4736 * accordingly. | |
4737 * | |
4738 * The new text is built up in new_start[]. It has some extra | |
4739 * room to avoid using alloc()/free() too often. new_start_len is | |
1389 | 4740 * the length of the allocated memory at new_start. |
7 | 4741 * |
4742 * Make a copy of the old line, so it won't be taken away when | |
4743 * updating the screen or handling a multi-line match. The "old_" | |
4744 * pointers point into this copy. | |
4745 */ | |
1499 | 4746 sub_firstlnum = lnum; |
7 | 4747 copycol = 0; |
4748 matchcol = 0; | |
4749 | |
4750 /* At first match, remember current cursor position. */ | |
4751 if (!got_match) | |
4752 { | |
4753 setpcmark(); | |
4754 got_match = TRUE; | |
4755 } | |
4756 | |
4757 /* | |
4758 * Loop until nothing more to replace in this line. | |
4759 * 1. Handle match with empty string. | |
4760 * 2. If do_ask is set, ask for confirmation. | |
4761 * 3. substitute the string. | |
4762 * 4. if do_all is set, find next match | |
4763 * 5. break if there isn't another match in this line | |
4764 */ | |
4765 for (;;) | |
4766 { | |
1499 | 4767 /* Advance "lnum" to the line where the match starts. The |
4768 * match does not start in the first line when there is a line | |
4769 * break before \zs. */ | |
4770 if (regmatch.startpos[0].lnum > 0) | |
4771 { | |
4772 lnum += regmatch.startpos[0].lnum; | |
4773 sub_firstlnum += regmatch.startpos[0].lnum; | |
4774 nmatch -= regmatch.startpos[0].lnum; | |
4775 vim_free(sub_firstline); | |
4776 sub_firstline = NULL; | |
4777 } | |
4778 | |
4779 if (sub_firstline == NULL) | |
4780 { | |
4781 sub_firstline = vim_strsave(ml_get(sub_firstlnum)); | |
4782 if (sub_firstline == NULL) | |
4783 { | |
4784 vim_free(new_start); | |
4785 goto outofmem; | |
4786 } | |
4787 } | |
4788 | |
7 | 4789 /* Save the line number of the last change for the final |
4790 * cursor position (just like Vi). */ | |
4791 curwin->w_cursor.lnum = lnum; | |
4792 do_again = FALSE; | |
4793 | |
4794 /* | |
4795 * 1. Match empty string does not count, except for first | |
4796 * match. This reproduces the strange vi behaviour. | |
4797 * This also catches endless loops. | |
4798 */ | |
4799 if (matchcol == prev_matchcol | |
4800 && regmatch.endpos[0].lnum == 0 | |
4801 && matchcol == regmatch.endpos[0].col) | |
4802 { | |
15 | 4803 if (sub_firstline[matchcol] == NUL) |
4804 /* We already were at the end of the line. Don't look | |
4805 * for a match in this line again. */ | |
4806 skip_match = TRUE; | |
4807 else | |
2837 | 4808 { |
4809 /* search for a match at next column */ | |
4810 #ifdef FEAT_MBYTE | |
4811 if (has_mbyte) | |
4812 matchcol += mb_ptr2len(sub_firstline + matchcol); | |
4813 else | |
4814 #endif | |
4815 ++matchcol; | |
4816 } | |
7 | 4817 goto skip; |
4818 } | |
4819 | |
4820 /* Normally we continue searching for a match just after the | |
4821 * previous match. */ | |
4822 matchcol = regmatch.endpos[0].col; | |
4823 prev_matchcol = matchcol; | |
4824 | |
4825 /* | |
170 | 4826 * 2. If do_count is set only increase the counter. |
4827 * If do_ask is set, ask for confirmation. | |
7 | 4828 */ |
170 | 4829 if (do_count) |
4830 { | |
4831 /* For a multi-line match, put matchcol at the NUL at | |
4832 * the end of the line and set nmatch to one, so that | |
4833 * we continue looking for a match on the next line. | |
4834 * Avoids that ":s/\nB\@=//gc" get stuck. */ | |
4835 if (nmatch > 1) | |
4836 { | |
835 | 4837 matchcol = (colnr_T)STRLEN(sub_firstline); |
170 | 4838 nmatch = 1; |
1483 | 4839 skip_match = TRUE; |
170 | 4840 } |
4841 sub_nsubs++; | |
4842 did_sub = TRUE; | |
3736 | 4843 #ifdef FEAT_EVAL |
4844 /* Skip the substitution, unless an expression is used, | |
4845 * then it is evaluated in the sandbox. */ | |
4846 if (!(sub[0] == '\\' && sub[1] == '=')) | |
4847 #endif | |
4848 goto skip; | |
170 | 4849 } |
4850 | |
7 | 4851 if (do_ask) |
4852 { | |
1874 | 4853 int typed = 0; |
1872 | 4854 |
7 | 4855 /* change State to CONFIRM, so that the mouse works |
4856 * properly */ | |
4857 save_State = State; | |
4858 State = CONFIRM; | |
4859 #ifdef FEAT_MOUSE | |
4860 setmouse(); /* disable mouse in xterm */ | |
4861 #endif | |
4862 curwin->w_cursor.col = regmatch.startpos[0].col; | |
4863 | |
4864 /* When 'cpoptions' contains "u" don't sync undo when | |
4865 * asking for confirmation. */ | |
4866 if (vim_strchr(p_cpo, CPO_UNDO) != NULL) | |
4867 ++no_u_sync; | |
4868 | |
4869 /* | |
4870 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed. | |
4871 */ | |
4872 while (do_ask) | |
4873 { | |
169 | 4874 if (exmode_active) |
4875 { | |
4876 char_u *resp; | |
4877 colnr_T sc, ec; | |
4878 | |
5396 | 4879 print_line_no_prefix(lnum, do_number, do_list); |
169 | 4880 |
4881 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL); | |
4882 curwin->w_cursor.col = regmatch.endpos[0].col - 1; | |
4883 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec); | |
5396 | 4884 if (do_number || curwin->w_p_nu) |
4885 { | |
4886 int numw = number_width(curwin) + 1; | |
4887 sc += numw; | |
4888 ec += numw; | |
4889 } | |
169 | 4890 msg_start(); |
170 | 4891 for (i = 0; i < (long)sc; ++i) |
169 | 4892 msg_putchar(' '); |
170 | 4893 for ( ; i <= (long)ec; ++i) |
169 | 4894 msg_putchar('^'); |
4895 | |
4896 resp = getexmodeline('?', NULL, 0); | |
4897 if (resp != NULL) | |
4898 { | |
1872 | 4899 typed = *resp; |
169 | 4900 vim_free(resp); |
4901 } | |
4902 } | |
4903 else | |
4904 { | |
4076 | 4905 char_u *orig_line = NULL; |
4906 int len_change = 0; | |
7 | 4907 #ifdef FEAT_FOLDING |
169 | 4908 int save_p_fen = curwin->w_p_fen; |
4909 | |
4910 curwin->w_p_fen = FALSE; | |
4911 #endif | |
4912 /* Invert the matched string. | |
4913 * Remove the inversion afterwards. */ | |
4914 temp = RedrawingDisabled; | |
4915 RedrawingDisabled = 0; | |
4916 | |
4076 | 4917 if (new_start != NULL) |
4918 { | |
4919 /* There already was a substitution, we would | |
4920 * like to show this to the user. We cannot | |
4921 * really update the line, it would change | |
4922 * what matches. Temporarily replace the line | |
4923 * and change it back afterwards. */ | |
4924 orig_line = vim_strsave(ml_get(lnum)); | |
4925 if (orig_line != NULL) | |
4926 { | |
4927 char_u *new_line = concat_str(new_start, | |
4928 sub_firstline + copycol); | |
4929 | |
4930 if (new_line == NULL) | |
4931 { | |
4932 vim_free(orig_line); | |
4933 orig_line = NULL; | |
4934 } | |
4935 else | |
4936 { | |
4937 /* Position the cursor relative to the | |
4938 * end of the line, the previous | |
4939 * substitute may have inserted or | |
4940 * deleted characters before the | |
4941 * cursor. */ | |
4086 | 4942 len_change = (int)STRLEN(new_line) |
4943 - (int)STRLEN(orig_line); | |
4076 | 4944 curwin->w_cursor.col += len_change; |
4945 ml_replace(lnum, new_line, FALSE); | |
4946 } | |
4947 } | |
4948 } | |
4949 | |
1499 | 4950 search_match_lines = regmatch.endpos[0].lnum |
4951 - regmatch.startpos[0].lnum; | |
4076 | 4952 search_match_endcol = regmatch.endpos[0].col |
4953 + len_change; | |
169 | 4954 highlight_match = TRUE; |
4955 | |
4956 update_topline(); | |
4957 validate_cursor(); | |
748 | 4958 update_screen(SOME_VALID); |
169 | 4959 highlight_match = FALSE; |
748 | 4960 redraw_later(SOME_VALID); |
7 | 4961 |
4962 #ifdef FEAT_FOLDING | |
169 | 4963 curwin->w_p_fen = save_p_fen; |
4964 #endif | |
4965 if (msg_row == Rows - 1) | |
4966 msg_didout = FALSE; /* avoid a scroll-up */ | |
4967 msg_starthere(); | |
4968 i = msg_scroll; | |
4969 msg_scroll = 0; /* truncate msg when | |
4970 needed */ | |
4971 msg_no_more = TRUE; | |
4972 /* write message same highlighting as for | |
4973 * wait_return */ | |
4974 smsg_attr(hl_attr(HLF_R), | |
4975 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub); | |
4976 msg_no_more = FALSE; | |
4977 msg_scroll = i; | |
4978 showruler(TRUE); | |
4979 windgoto(msg_row, msg_col); | |
4980 RedrawingDisabled = temp; | |
7 | 4981 |
4982 #ifdef USE_ON_FLY_SCROLL | |
169 | 4983 dont_scroll = FALSE; /* allow scrolling here */ |
4984 #endif | |
4985 ++no_mapping; /* don't map this key */ | |
4986 ++allow_keys; /* allow special keys */ | |
1872 | 4987 typed = plain_vgetc(); |
169 | 4988 --allow_keys; |
4989 --no_mapping; | |
4990 | |
4991 /* clear the question */ | |
4992 msg_didout = FALSE; /* don't scroll up */ | |
4993 msg_col = 0; | |
4994 gotocmdline(TRUE); | |
4076 | 4995 |
4996 /* restore the line */ | |
4997 if (orig_line != NULL) | |
4998 ml_replace(lnum, orig_line, FALSE); | |
169 | 4999 } |
5000 | |
7 | 5001 need_wait_return = FALSE; /* no hit-return prompt */ |
1872 | 5002 if (typed == 'q' || typed == ESC || typed == Ctrl_C |
7 | 5003 #ifdef UNIX |
1872 | 5004 || typed == intr_char |
7 | 5005 #endif |
5006 ) | |
5007 { | |
5008 got_quit = TRUE; | |
5009 break; | |
5010 } | |
1872 | 5011 if (typed == 'n') |
7 | 5012 break; |
1872 | 5013 if (typed == 'y') |
7 | 5014 break; |
1872 | 5015 if (typed == 'l') |
7 | 5016 { |
5017 /* last: replace and then stop */ | |
5018 do_all = FALSE; | |
5019 line2 = lnum; | |
5020 break; | |
5021 } | |
1872 | 5022 if (typed == 'a') |
7 | 5023 { |
5024 do_ask = FALSE; | |
5025 break; | |
5026 } | |
5027 #ifdef FEAT_INS_EXPAND | |
1872 | 5028 if (typed == Ctrl_E) |
7 | 5029 scrollup_clamp(); |
1872 | 5030 else if (typed == Ctrl_Y) |
7 | 5031 scrolldown_clamp(); |
5032 #endif | |
5033 } | |
5034 State = save_State; | |
5035 #ifdef FEAT_MOUSE | |
5036 setmouse(); | |
5037 #endif | |
5038 if (vim_strchr(p_cpo, CPO_UNDO) != NULL) | |
5039 --no_u_sync; | |
5040 | |
1872 | 5041 if (typed == 'n') |
7 | 5042 { |
5043 /* For a multi-line match, put matchcol at the NUL at | |
5044 * the end of the line and set nmatch to one, so that | |
5045 * we continue looking for a match on the next line. | |
1091 | 5046 * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc" |
5047 * get stuck when pressing 'n'. */ | |
7 | 5048 if (nmatch > 1) |
5049 { | |
835 | 5050 matchcol = (colnr_T)STRLEN(sub_firstline); |
1091 | 5051 skip_match = TRUE; |
7 | 5052 } |
5053 goto skip; | |
5054 } | |
5055 if (got_quit) | |
4099 | 5056 goto skip; |
7 | 5057 } |
5058 | |
5059 /* Move the cursor to the start of the match, so that we can | |
5060 * use "\=col("."). */ | |
5061 curwin->w_cursor.col = regmatch.startpos[0].col; | |
5062 | |
5063 /* | |
5064 * 3. substitute the string. | |
5065 */ | |
3736 | 5066 #ifdef FEAT_EVAL |
5067 if (do_count) | |
5068 { | |
3786 | 5069 /* prevent accidentally changing the buffer by a function */ |
3736 | 5070 save_ma = curbuf->b_p_ma; |
5071 curbuf->b_p_ma = FALSE; | |
5072 sandbox++; | |
5073 } | |
5074 #endif | |
7 | 5075 /* get length of substitution part */ |
1499 | 5076 sublen = vim_regsub_multi(®match, |
5077 sub_firstlnum - regmatch.startpos[0].lnum, | |
7 | 5078 sub, sub_firstline, FALSE, p_magic, TRUE); |
3736 | 5079 #ifdef FEAT_EVAL |
5080 if (do_count) | |
5081 { | |
5082 curbuf->b_p_ma = save_ma; | |
5083 sandbox--; | |
5084 goto skip; | |
5085 } | |
5086 #endif | |
7 | 5087 |
533 | 5088 /* When the match included the "$" of the last line it may |
819 | 5089 * go beyond the last line of the buffer. */ |
533 | 5090 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1) |
5091 { | |
5092 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1; | |
5093 skip_match = TRUE; | |
5094 } | |
5095 | |
7 | 5096 /* Need room for: |
5097 * - result so far in new_start (not for first sub in line) | |
5098 * - original text up to match | |
5099 * - length of substituted part | |
5100 * - original text after match | |
5101 */ | |
5102 if (nmatch == 1) | |
5103 p1 = sub_firstline; | |
5104 else | |
5105 { | |
5106 p1 = ml_get(sub_firstlnum + nmatch - 1); | |
5107 nmatch_tl += nmatch - 1; | |
5108 } | |
1872 | 5109 copy_len = regmatch.startpos[0].col - copycol; |
5110 needed_len = copy_len + ((unsigned)STRLEN(p1) | |
5111 - regmatch.endpos[0].col) + sublen + 1; | |
7 | 5112 if (new_start == NULL) |
5113 { | |
5114 /* | |
5115 * Get some space for a temporary buffer to do the | |
5116 * substitution into (and some extra space to avoid | |
5117 * too many calls to alloc()/free()). | |
5118 */ | |
5119 new_start_len = needed_len + 50; | |
5120 if ((new_start = alloc_check(new_start_len)) == NULL) | |
5121 goto outofmem; | |
5122 *new_start = NUL; | |
5123 new_end = new_start; | |
5124 } | |
5125 else | |
5126 { | |
5127 /* | |
5128 * Check if the temporary buffer is long enough to do the | |
5129 * substitution into. If not, make it larger (with a bit | |
5130 * extra to avoid too many calls to alloc()/free()). | |
5131 */ | |
5132 len = (unsigned)STRLEN(new_start); | |
5133 needed_len += len; | |
1872 | 5134 if (needed_len > (int)new_start_len) |
7 | 5135 { |
5136 new_start_len = needed_len + 50; | |
5137 if ((p1 = alloc_check(new_start_len)) == NULL) | |
5138 { | |
5139 vim_free(new_start); | |
5140 goto outofmem; | |
5141 } | |
5142 mch_memmove(p1, new_start, (size_t)(len + 1)); | |
5143 vim_free(new_start); | |
5144 new_start = p1; | |
5145 } | |
5146 new_end = new_start + len; | |
5147 } | |
5148 | |
5149 /* | |
5150 * copy the text up to the part that matched | |
5151 */ | |
1872 | 5152 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len); |
5153 new_end += copy_len; | |
7 | 5154 |
1499 | 5155 (void)vim_regsub_multi(®match, |
5156 sub_firstlnum - regmatch.startpos[0].lnum, | |
7 | 5157 sub, new_end, TRUE, p_magic, TRUE); |
5158 sub_nsubs++; | |
5159 did_sub = TRUE; | |
5160 | |
5161 /* Move the cursor to the start of the line, to avoid that it | |
5162 * is beyond the end of the line after the substitution. */ | |
5163 curwin->w_cursor.col = 0; | |
5164 | |
5165 /* For a multi-line match, make a copy of the last matched | |
5166 * line and continue in that one. */ | |
5167 if (nmatch > 1) | |
5168 { | |
5169 sub_firstlnum += nmatch - 1; | |
5170 vim_free(sub_firstline); | |
5171 sub_firstline = vim_strsave(ml_get(sub_firstlnum)); | |
5172 /* When going beyond the last line, stop substituting. */ | |
5173 if (sub_firstlnum <= line2) | |
5174 do_again = TRUE; | |
5175 else | |
5176 do_all = FALSE; | |
5177 } | |
5178 | |
5179 /* Remember next character to be copied. */ | |
5180 copycol = regmatch.endpos[0].col; | |
5181 | |
819 | 5182 if (skip_match) |
5183 { | |
5184 /* Already hit end of the buffer, sub_firstlnum is one | |
5185 * less than what it ought to be. */ | |
5186 vim_free(sub_firstline); | |
5187 sub_firstline = vim_strsave((char_u *)""); | |
5188 copycol = 0; | |
5189 } | |
5190 | |
7 | 5191 /* |
5192 * Now the trick is to replace CTRL-M chars with a real line | |
5193 * break. This would make it impossible to insert a CTRL-M in | |
5194 * the text. The line break can be avoided by preceding the | |
5195 * CTRL-M with a backslash. To be able to insert a backslash, | |
5196 * they must be doubled in the string and are halved here. | |
5197 * That is Vi compatible. | |
5198 */ | |
5199 for (p1 = new_end; *p1; ++p1) | |
5200 { | |
5201 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */ | |
1624 | 5202 STRMOVE(p1, p1 + 1); |
7 | 5203 else if (*p1 == CAR) |
5204 { | |
5205 if (u_inssub(lnum) == OK) /* prepare for undo */ | |
5206 { | |
5207 *p1 = NUL; /* truncate up to the CR */ | |
5208 ml_append(lnum - 1, new_start, | |
5209 (colnr_T)(p1 - new_start + 1), FALSE); | |
5210 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); | |
5211 if (do_ask) | |
5212 appended_lines(lnum - 1, 1L); | |
5213 else | |
5214 { | |
5215 if (first_line == 0) | |
5216 first_line = lnum; | |
5217 last_line = lnum + 1; | |
5218 } | |
5219 /* All line numbers increase. */ | |
5220 ++sub_firstlnum; | |
5221 ++lnum; | |
5222 ++line2; | |
5223 /* move the cursor to the new line, like Vi */ | |
5224 ++curwin->w_cursor.lnum; | |
1444 | 5225 /* copy the rest */ |
1624 | 5226 STRMOVE(new_start, p1 + 1); |
7 | 5227 p1 = new_start - 1; |
5228 } | |
5229 } | |
5230 #ifdef FEAT_MBYTE | |
5231 else if (has_mbyte) | |
474 | 5232 p1 += (*mb_ptr2len)(p1) - 1; |
7 | 5233 #endif |
5234 } | |
5235 | |
5236 /* | |
5237 * 4. If do_all is set, find next match. | |
5238 * Prevent endless loop with patterns that match empty | |
5239 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g. | |
5240 * But ":s/\n/#/" is OK. | |
5241 */ | |
5242 skip: | |
5243 /* We already know that we did the last subst when we are at | |
5244 * the end of the line, except that a pattern like | |
1499 | 5245 * "bar\|\nfoo" may match at the NUL. "lnum" can be below |
5246 * "line2" when there is a \zs in the pattern after a line | |
5247 * break. */ | |
15 | 5248 lastone = (skip_match |
5249 || got_int | |
5250 || got_quit | |
1499 | 5251 || lnum > line2 |
15 | 5252 || !(do_all || do_again) |
5253 || (sub_firstline[matchcol] == NUL && nmatch <= 1 | |
5254 && !re_multiline(regmatch.regprog))); | |
7 | 5255 nmatch = -1; |
5256 | |
5257 /* | |
5258 * Replace the line in the buffer when needed. This is | |
5259 * skipped when there are more matches. | |
5260 * The check for nmatch_tl is needed for when multi-line | |
5261 * matching must replace the lines before trying to do another | |
5262 * match, otherwise "\@<=" won't work. | |
1499 | 5263 * When the match starts below where we start searching also |
5264 * need to replace the line first (using \zs after \n). | |
7 | 5265 */ |
5266 if (lastone | |
5267 || nmatch_tl > 0 | |
5268 || (nmatch = vim_regexec_multi(®match, curwin, | |
1521 | 5269 curbuf, sub_firstlnum, |
5270 matchcol, NULL)) == 0 | |
1499 | 5271 || regmatch.startpos[0].lnum > 0) |
7 | 5272 { |
5273 if (new_start != NULL) | |
5274 { | |
5275 /* | |
5276 * Copy the rest of the line, that didn't match. | |
5277 * "matchcol" has to be adjusted, we use the end of | |
5278 * the line as reference, because the substitute may | |
5279 * have changed the number of characters. Same for | |
5280 * "prev_matchcol". | |
5281 */ | |
5282 STRCAT(new_start, sub_firstline + copycol); | |
5283 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol; | |
5284 prev_matchcol = (colnr_T)STRLEN(sub_firstline) | |
5285 - prev_matchcol; | |
5286 | |
5287 if (u_savesub(lnum) != OK) | |
5288 break; | |
5289 ml_replace(lnum, new_start, TRUE); | |
5290 | |
5291 if (nmatch_tl > 0) | |
5292 { | |
5293 /* | |
5294 * Matched lines have now been substituted and are | |
5295 * useless, delete them. The part after the match | |
5296 * has been appended to new_start, we don't need | |
5297 * it in the buffer. | |
5298 */ | |
5299 ++lnum; | |
5300 if (u_savedel(lnum, nmatch_tl) != OK) | |
5301 break; | |
5302 for (i = 0; i < nmatch_tl; ++i) | |
5303 ml_delete(lnum, (int)FALSE); | |
5304 mark_adjust(lnum, lnum + nmatch_tl - 1, | |
5305 (long)MAXLNUM, -nmatch_tl); | |
5306 if (do_ask) | |
5307 deleted_lines(lnum, nmatch_tl); | |
5308 --lnum; | |
5309 line2 -= nmatch_tl; /* nr of lines decreases */ | |
5310 nmatch_tl = 0; | |
5311 } | |
5312 | |
5313 /* When asking, undo is saved each time, must also set | |
5314 * changed flag each time. */ | |
5315 if (do_ask) | |
5316 changed_bytes(lnum, 0); | |
5317 else | |
5318 { | |
5319 if (first_line == 0) | |
5320 first_line = lnum; | |
5321 last_line = lnum + 1; | |
5322 } | |
5323 | |
5324 sub_firstlnum = lnum; | |
5325 vim_free(sub_firstline); /* free the temp buffer */ | |
5326 sub_firstline = new_start; | |
5327 new_start = NULL; | |
5328 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol; | |
5329 prev_matchcol = (colnr_T)STRLEN(sub_firstline) | |
5330 - prev_matchcol; | |
5331 copycol = 0; | |
5332 } | |
5333 if (nmatch == -1 && !lastone) | |
5334 nmatch = vim_regexec_multi(®match, curwin, curbuf, | |
1521 | 5335 sub_firstlnum, matchcol, NULL); |
7 | 5336 |
5337 /* | |
5338 * 5. break if there isn't another match in this line | |
5339 */ | |
5340 if (nmatch <= 0) | |
1499 | 5341 { |
5342 /* If the match found didn't start where we were | |
5343 * searching, do the next search in the line where we | |
5344 * found the match. */ | |
5345 if (nmatch == -1) | |
5346 lnum -= regmatch.startpos[0].lnum; | |
7 | 5347 break; |
1499 | 5348 } |
7 | 5349 } |
5350 | |
5351 line_breakcheck(); | |
5352 } | |
5353 | |
5354 if (did_sub) | |
5355 ++sub_nlines; | |
1720 | 5356 vim_free(new_start); /* for when substitute was cancelled */ |
7 | 5357 vim_free(sub_firstline); /* free the copy of the original line */ |
5358 sub_firstline = NULL; | |
5359 } | |
5360 | |
5361 line_breakcheck(); | |
5362 } | |
5363 | |
5364 if (first_line != 0) | |
5365 { | |
5366 /* Need to subtract the number of added lines from "last_line" to get | |
5367 * the line number before the change (same as adding the number of | |
5368 * deleted lines). */ | |
5369 i = curbuf->b_ml.ml_line_count - old_line_count; | |
5370 changed_lines(first_line, 0, last_line - i, i); | |
5371 } | |
5372 | |
5373 outofmem: | |
5374 vim_free(sub_firstline); /* may have to free allocated copy of the line */ | |
170 | 5375 |
5376 /* ":s/pat//n" doesn't move the cursor */ | |
5377 if (do_count) | |
5378 curwin->w_cursor = old_cursor; | |
5379 | |
2126
e038754d419a
updated for version 7.2.408
Bram Moolenaar <bram@zimbu.org>
parents:
1929
diff
changeset
|
5380 if (sub_nsubs > start_nsubs) |
7 | 5381 { |
5382 /* Set the '[ and '] marks. */ | |
5383 curbuf->b_op_start.lnum = eap->line1; | |
5384 curbuf->b_op_end.lnum = line2; | |
5385 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; | |
5386 | |
5387 if (!global_busy) | |
5388 { | |
3394 | 5389 if (!do_ask) /* when interactive leave cursor on the match */ |
5390 { | |
5391 if (endcolumn) | |
5392 coladvance((colnr_T)MAXCOL); | |
5393 else | |
5394 beginline(BL_WHITE | BL_FIX); | |
5395 } | |
170 | 5396 if (!do_sub_msg(do_count) && do_ask) |
7 | 5397 MSG(""); |
5398 } | |
5399 else | |
5400 global_need_beginline = TRUE; | |
5401 if (do_print) | |
169 | 5402 print_line(curwin->w_cursor.lnum, do_number, do_list); |
7 | 5403 } |
5404 else if (!global_busy) | |
5405 { | |
5406 if (got_int) /* interrupted */ | |
5407 EMSG(_(e_interr)); | |
5408 else if (got_match) /* did find something but nothing substituted */ | |
5409 MSG(""); | |
5410 else if (do_error) /* nothing found */ | |
5411 EMSG2(_(e_patnotf2), get_search_pat()); | |
5412 } | |
5413 | |
4035 | 5414 #ifdef FEAT_FOLDING |
5415 if (do_ask && hasAnyFolding(curwin)) | |
5416 /* Cursor position may require updating */ | |
5417 changed_window_setting(); | |
5418 #endif | |
5419 | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4613
diff
changeset
|
5420 vim_regfree(regmatch.regprog); |
6789 | 5421 |
5422 /* Restore the flag values, they can be used for ":&&". */ | |
5423 do_all = save_do_all; | |
5424 do_ask = save_do_ask; | |
7 | 5425 } |
5426 | |
5427 /* | |
5428 * Give message for number of substitutions. | |
5429 * Can also be used after a ":global" command. | |
5430 * Return TRUE if a message was given. | |
5431 */ | |
484 | 5432 int |
170 | 5433 do_sub_msg(count_only) |
5434 int count_only; /* used 'n' flag for ":s" */ | |
7 | 5435 { |
5436 /* | |
5437 * Only report substitutions when: | |
5438 * - more than 'report' substitutions | |
5439 * - command was typed by user, or number of changed lines > 'report' | |
5440 * - giving messages is not disabled by 'lazyredraw' | |
5441 */ | |
170 | 5442 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1)) |
5443 || count_only) | |
7 | 5444 && messaging()) |
5445 { | |
5446 if (got_int) | |
5447 STRCPY(msg_buf, _("(Interrupted) ")); | |
2290
22529abcd646
Fixed ":s" message. Docs updates.
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
5448 else |
22529abcd646
Fixed ":s" message. Docs updates.
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
5449 *msg_buf = NUL; |
7 | 5450 if (sub_nsubs == 1) |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2254
diff
changeset
|
5451 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), |
274 | 5452 "%s", count_only ? _("1 match") : _("1 substitution")); |
7 | 5453 else |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2254
diff
changeset
|
5454 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), |
170 | 5455 count_only ? _("%ld matches") : _("%ld substitutions"), |
7 | 5456 sub_nsubs); |
5457 if (sub_nlines == 1) | |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2254
diff
changeset
|
5458 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), |
274 | 5459 "%s", _(" on 1 line")); |
7 | 5460 else |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2254
diff
changeset
|
5461 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), |
274 | 5462 _(" on %ld lines"), (long)sub_nlines); |
7 | 5463 if (msg(msg_buf)) |
5464 /* save message to display it after redraw */ | |
680 | 5465 set_keep_msg(msg_buf, 0); |
7 | 5466 return TRUE; |
5467 } | |
5468 if (got_int) | |
5469 { | |
5470 EMSG(_(e_interr)); | |
5471 return TRUE; | |
5472 } | |
5473 return FALSE; | |
5474 } | |
5475 | |
5476 /* | |
5477 * Execute a global command of the form: | |
5478 * | |
5479 * g/pattern/X : execute X on all lines where pattern matches | |
5480 * v/pattern/X : execute X on all lines where pattern does not match | |
5481 * | |
5482 * where 'X' is an EX command | |
5483 * | |
5484 * The command character (as well as the trailing slash) is optional, and | |
5485 * is assumed to be 'p' if missing. | |
5486 * | |
5487 * This is implemented in two passes: first we scan the file for the pattern and | |
3786 | 5488 * set a mark for each line that (not) matches. Secondly we execute the command |
7 | 5489 * for each line that has a mark. This is required because after deleting |
5490 * lines we do not know where to search for the next match. | |
5491 */ | |
5492 void | |
5493 ex_global(eap) | |
5494 exarg_T *eap; | |
5495 { | |
5496 linenr_T lnum; /* line number according to old situation */ | |
170 | 5497 int ndone = 0; |
7 | 5498 int type; /* first char of cmd: 'v' or 'g' */ |
5499 char_u *cmd; /* command argument */ | |
5500 | |
5501 char_u delim; /* delimiter, normally '/' */ | |
5502 char_u *pat; | |
5503 regmmatch_T regmatch; | |
5504 int match; | |
5505 int which_pat; | |
5506 | |
5507 if (global_busy) | |
5508 { | |
5509 EMSG(_("E147: Cannot do :global recursive")); /* will increment global_busy */ | |
5510 return; | |
5511 } | |
5512 | |
5513 if (eap->forceit) /* ":global!" is like ":vglobal" */ | |
5514 type = 'v'; | |
5515 else | |
5516 type = *eap->cmd; | |
5517 cmd = eap->arg; | |
5518 which_pat = RE_LAST; /* default: use last used regexp */ | |
5519 | |
5520 /* | |
5521 * undocumented vi feature: | |
5522 * "\/" and "\?": use previous search pattern. | |
5523 * "\&": use previous substitute pattern. | |
5524 */ | |
5525 if (*cmd == '\\') | |
5526 { | |
5527 ++cmd; | |
5528 if (vim_strchr((char_u *)"/?&", *cmd) == NULL) | |
5529 { | |
5530 EMSG(_(e_backslash)); | |
5531 return; | |
5532 } | |
5533 if (*cmd == '&') | |
5534 which_pat = RE_SUBST; /* use previous substitute pattern */ | |
5535 else | |
5536 which_pat = RE_SEARCH; /* use previous search pattern */ | |
5537 ++cmd; | |
5538 pat = (char_u *)""; | |
5539 } | |
5540 else if (*cmd == NUL) | |
5541 { | |
5542 EMSG(_("E148: Regular expression missing from global")); | |
5543 return; | |
5544 } | |
5545 else | |
5546 { | |
5547 delim = *cmd; /* get the delimiter */ | |
5548 if (delim) | |
5549 ++cmd; /* skip delimiter if there is one */ | |
5550 pat = cmd; /* remember start of pattern */ | |
5551 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg); | |
5552 if (cmd[0] == delim) /* end delimiter found */ | |
5553 *cmd++ = NUL; /* replace it with a NUL */ | |
5554 } | |
5555 | |
5556 #ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */ | |
5557 if (p_altkeymap && curwin->w_p_rl) | |
5558 lrFswap(pat,0); | |
5559 #endif | |
5560 | |
5561 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, ®match) == FAIL) | |
5562 { | |
5563 EMSG(_(e_invcmd)); | |
5564 return; | |
5565 } | |
5566 | |
5567 /* | |
5568 * pass 1: set marks for each (not) matching line | |
5569 */ | |
5570 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum) | |
5571 { | |
5572 /* a match on this line? */ | |
1521 | 5573 match = vim_regexec_multi(®match, curwin, curbuf, lnum, |
5574 (colnr_T)0, NULL); | |
7 | 5575 if ((type == 'g' && match) || (type == 'v' && !match)) |
5576 { | |
5577 ml_setmarked(lnum); | |
5578 ndone++; | |
5579 } | |
5580 line_breakcheck(); | |
5581 } | |
5582 | |
5583 /* | |
5584 * pass 2: execute the command for each line that has been marked | |
5585 */ | |
5586 if (got_int) | |
5587 MSG(_(e_interr)); | |
5588 else if (ndone == 0) | |
5589 { | |
5590 if (type == 'v') | |
274 | 5591 smsg((char_u *)_("Pattern found in every line: %s"), pat); |
7 | 5592 else |
4195 | 5593 smsg((char_u *)_("Pattern not found: %s"), pat); |
7 | 5594 } |
5595 else | |
6116 | 5596 { |
5597 #ifdef FEAT_CLIPBOARD | |
5598 start_global_changes(); | |
5599 #endif | |
7 | 5600 global_exe(cmd); |
6116 | 5601 #ifdef FEAT_CLIPBOARD |
5602 end_global_changes(); | |
5603 #endif | |
5604 } | |
7 | 5605 |
5606 ml_clearmarked(); /* clear rest of the marks */ | |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4613
diff
changeset
|
5607 vim_regfree(regmatch.regprog); |
7 | 5608 } |
5609 | |
5610 /* | |
5611 * Execute "cmd" on lines marked with ml_setmarked(). | |
5612 */ | |
5613 void | |
5614 global_exe(cmd) | |
5615 char_u *cmd; | |
5616 { | |
2819 | 5617 linenr_T old_lcount; /* b_ml.ml_line_count before the command */ |
5618 buf_T *old_buf = curbuf; /* remember what buffer we started in */ | |
5619 linenr_T lnum; /* line number according to old situation */ | |
7 | 5620 |
5621 /* | |
5622 * Set current position only once for a global command. | |
5623 * If global_busy is set, setpcmark() will not do anything. | |
5624 * If there is an error, global_busy will be incremented. | |
5625 */ | |
5626 setpcmark(); | |
5627 | |
5628 /* When the command writes a message, don't overwrite the command. */ | |
5629 msg_didout = TRUE; | |
5630 | |
2127
4e22214f8464
updated for version 7.2.409
Bram Moolenaar <bram@zimbu.org>
parents:
2126
diff
changeset
|
5631 sub_nsubs = 0; |
4e22214f8464
updated for version 7.2.409
Bram Moolenaar <bram@zimbu.org>
parents:
2126
diff
changeset
|
5632 sub_nlines = 0; |
7 | 5633 global_need_beginline = FALSE; |
5634 global_busy = 1; | |
5635 old_lcount = curbuf->b_ml.ml_line_count; | |
5636 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1) | |
5637 { | |
5638 curwin->w_cursor.lnum = lnum; | |
5639 curwin->w_cursor.col = 0; | |
5640 if (*cmd == NUL || *cmd == '\n') | |
5641 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT); | |
5642 else | |
5643 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT); | |
5644 ui_breakcheck(); | |
5645 } | |
5646 | |
5647 global_busy = 0; | |
5648 if (global_need_beginline) | |
5649 beginline(BL_WHITE | BL_FIX); | |
5650 else | |
5651 check_cursor(); /* cursor may be beyond the end of the line */ | |
5652 | |
39 | 5653 /* the cursor may not have moved in the text but a change in a previous |
5654 * line may move it on the screen */ | |
5655 changed_line_abv_curs(); | |
5656 | |
7 | 5657 /* If it looks like no message was written, allow overwriting the |
5658 * command with the report for number of changes. */ | |
5659 if (msg_col == 0 && msg_scrolled == 0) | |
5660 msg_didout = FALSE; | |
5661 | |
1227 | 5662 /* If substitutes done, report number of substitutes, otherwise report |
2819 | 5663 * number of extra or deleted lines. |
5664 * Don't report extra or deleted lines in the edge case where the buffer | |
5665 * we are in after execution is different from the buffer we started in. */ | |
5666 if (!do_sub_msg(FALSE) && curbuf == old_buf) | |
7 | 5667 msgmore(curbuf->b_ml.ml_line_count - old_lcount); |
5668 } | |
5669 | |
5670 #ifdef FEAT_VIMINFO | |
5671 int | |
5672 read_viminfo_sub_string(virp, force) | |
5673 vir_T *virp; | |
5674 int force; | |
5675 { | |
2690 | 5676 if (force) |
7 | 5677 vim_free(old_sub); |
5678 if (force || old_sub == NULL) | |
5679 old_sub = viminfo_readstring(virp, 1, TRUE); | |
5680 return viminfo_readline(virp); | |
5681 } | |
5682 | |
5683 void | |
5684 write_viminfo_sub_string(fp) | |
5685 FILE *fp; | |
5686 { | |
5687 if (get_viminfo_parameter('/') != 0 && old_sub != NULL) | |
5688 { | |
2545
298d8d6e69be
Avoid warnings from the clang compiler. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2394
diff
changeset
|
5689 fputs(_("\n# Last Substitute String:\n$"), fp); |
7 | 5690 viminfo_writestring(fp, old_sub); |
5691 } | |
5692 } | |
5693 #endif /* FEAT_VIMINFO */ | |
5694 | |
359 | 5695 #if defined(EXITFREE) || defined(PROTO) |
5696 void | |
5697 free_old_sub() | |
5698 { | |
5699 vim_free(old_sub); | |
5700 } | |
5701 #endif | |
5702 | |
7 | 5703 #if (defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)) || defined(PROTO) |
5704 /* | |
5705 * Set up for a tagpreview. | |
735 | 5706 * Return TRUE when it was created. |
7 | 5707 */ |
735 | 5708 int |
816 | 5709 prepare_tagpreview(undo_sync) |
5710 int undo_sync; /* sync undo when leaving the window */ | |
7 | 5711 { |
5712 win_T *wp; | |
5713 | |
5714 # ifdef FEAT_GUI | |
5715 need_mouse_correct = TRUE; | |
5716 # endif | |
5717 | |
5718 /* | |
5719 * If there is already a preview window open, use that one. | |
5720 */ | |
5721 if (!curwin->w_p_pvw) | |
5722 { | |
5723 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
5724 if (wp->w_p_pvw) | |
5725 break; | |
5726 if (wp != NULL) | |
816 | 5727 win_enter(wp, undo_sync); |
7 | 5728 else |
5729 { | |
5730 /* | |
5731 * There is no preview window open yet. Create one. | |
5732 */ | |
5733 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0) | |
5734 == FAIL) | |
735 | 5735 return FALSE; |
7 | 5736 curwin->w_p_pvw = TRUE; |
5737 curwin->w_p_wfh = TRUE; | |
2583 | 5738 RESET_BINDING(curwin); /* don't take over 'scrollbind' |
5739 and 'cursorbind' */ | |
7 | 5740 # ifdef FEAT_DIFF |
5741 curwin->w_p_diff = FALSE; /* no 'diff' */ | |
5742 # endif | |
5743 # ifdef FEAT_FOLDING | |
5744 curwin->w_p_fdc = 0; /* no 'foldcolumn' */ | |
5745 # endif | |
735 | 5746 return TRUE; |
7 | 5747 } |
5748 } | |
735 | 5749 return FALSE; |
7 | 5750 } |
5751 | |
5752 #endif | |
5753 | |
5754 | |
5755 /* | |
5756 * ":help": open a read-only window on a help file | |
5757 */ | |
5758 void | |
5759 ex_help(eap) | |
5760 exarg_T *eap; | |
5761 { | |
5762 char_u *arg; | |
5763 char_u *tag; | |
5764 FILE *helpfd; /* file descriptor of help file */ | |
5765 int n; | |
5766 int i; | |
5767 #ifdef FEAT_WINDOWS | |
5768 win_T *wp; | |
5769 #endif | |
5770 int num_matches; | |
5771 char_u **matches; | |
5772 char_u *p; | |
5773 int empty_fnum = 0; | |
5774 int alt_fnum = 0; | |
5775 buf_T *buf; | |
5776 #ifdef FEAT_MULTI_LANG | |
5777 int len; | |
9 | 5778 char_u *lang; |
7 | 5779 #endif |
3112 | 5780 #ifdef FEAT_FOLDING |
5781 int old_KeyTyped = KeyTyped; | |
5782 #endif | |
7 | 5783 |
5784 if (eap != NULL) | |
5785 { | |
5786 /* | |
5787 * A ":help" command ends at the first LF, or at a '|' that is | |
5788 * followed by some text. Set nextcmd to the following command. | |
5789 */ | |
5790 for (arg = eap->arg; *arg; ++arg) | |
5791 { | |
5792 if (*arg == '\n' || *arg == '\r' | |
5793 || (*arg == '|' && arg[1] != NUL && arg[1] != '|')) | |
5794 { | |
5795 *arg++ = NUL; | |
5796 eap->nextcmd = arg; | |
5797 break; | |
5798 } | |
5799 } | |
5800 arg = eap->arg; | |
5801 | |
3446 | 5802 if (eap->forceit && *arg == NUL && !curbuf->b_help) |
7 | 5803 { |
5804 EMSG(_("E478: Don't panic!")); | |
5805 return; | |
5806 } | |
5807 | |
5808 if (eap->skip) /* not executing commands */ | |
5809 return; | |
5810 } | |
5811 else | |
5812 arg = (char_u *)""; | |
5813 | |
5814 /* remove trailing blanks */ | |
5815 p = arg + STRLEN(arg) - 1; | |
5816 while (p > arg && vim_iswhite(*p) && p[-1] != '\\') | |
5817 *p-- = NUL; | |
5818 | |
5819 #ifdef FEAT_MULTI_LANG | |
5820 /* Check for a specified language */ | |
9 | 5821 lang = check_help_lang(arg); |
7 | 5822 #endif |
5823 | |
5824 /* When no argument given go to the index. */ | |
5825 if (*arg == NUL) | |
5826 arg = (char_u *)"help.txt"; | |
5827 | |
5828 /* | |
5829 * Check if there is a match for the argument. | |
5830 */ | |
5831 n = find_help_tags(arg, &num_matches, &matches, | |
5832 eap != NULL && eap->forceit); | |
5833 | |
5834 i = 0; | |
5835 #ifdef FEAT_MULTI_LANG | |
5836 if (n != FAIL && lang != NULL) | |
5837 /* Find first item with the requested language. */ | |
5838 for (i = 0; i < num_matches; ++i) | |
5839 { | |
835 | 5840 len = (int)STRLEN(matches[i]); |
7 | 5841 if (len > 3 && matches[i][len - 3] == '@' |
5842 && STRICMP(matches[i] + len - 2, lang) == 0) | |
5843 break; | |
5844 } | |
5845 #endif | |
5846 if (i >= num_matches || n == FAIL) | |
5847 { | |
5848 #ifdef FEAT_MULTI_LANG | |
5849 if (lang != NULL) | |
5850 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg); | |
5851 else | |
5852 #endif | |
5853 EMSG2(_("E149: Sorry, no help for %s"), arg); | |
5854 if (n != FAIL) | |
5855 FreeWild(num_matches, matches); | |
5856 return; | |
5857 } | |
5858 | |
5859 /* The first match (in the requested language) is the best match. */ | |
5860 tag = vim_strsave(matches[i]); | |
5861 FreeWild(num_matches, matches); | |
5862 | |
5863 #ifdef FEAT_GUI | |
5864 need_mouse_correct = TRUE; | |
5865 #endif | |
5866 | |
5867 /* | |
5868 * Re-use an existing help window or open a new one. | |
684 | 5869 * Always open a new one for ":tab help". |
7 | 5870 */ |
684 | 5871 if (!curwin->w_buffer->b_help |
5872 #ifdef FEAT_WINDOWS | |
5873 || cmdmod.tab != 0 | |
5874 #endif | |
5875 ) | |
7 | 5876 { |
5877 #ifdef FEAT_WINDOWS | |
684 | 5878 if (cmdmod.tab != 0) |
5879 wp = NULL; | |
5880 else | |
5881 for (wp = firstwin; wp != NULL; wp = wp->w_next) | |
5882 if (wp->w_buffer != NULL && wp->w_buffer->b_help) | |
5883 break; | |
7 | 5884 if (wp != NULL && wp->w_buffer->b_nwindows > 0) |
5885 win_enter(wp, TRUE); | |
5886 else | |
5887 #endif | |
5888 { | |
5889 /* | |
5890 * There is no help window yet. | |
5891 * Try to open the file specified by the "helpfile" option. | |
5892 */ | |
5893 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL) | |
5894 { | |
274 | 5895 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf); |
7 | 5896 goto erret; |
5897 } | |
5898 fclose(helpfd); | |
5899 | |
5900 #ifdef FEAT_WINDOWS | |
5901 /* Split off help window; put it at far top if no position | |
684 | 5902 * specified, the current window is vertically split and |
5903 * narrow. */ | |
7 | 5904 n = WSP_HELP; |
5905 # ifdef FEAT_VERTSPLIT | |
5906 if (cmdmod.split == 0 && curwin->w_width != Columns | |
684 | 5907 && curwin->w_width < 80) |
7 | 5908 n |= WSP_TOP; |
5909 # endif | |
5910 if (win_split(0, n) == FAIL) | |
684 | 5911 goto erret; |
7 | 5912 #else |
5913 /* use current window */ | |
5914 if (!can_abandon(curbuf, FALSE)) | |
5915 goto erret; | |
684 | 5916 #endif |
7 | 5917 |
5918 #ifdef FEAT_WINDOWS | |
5919 if (curwin->w_height < p_hh) | |
5920 win_setheight((int)p_hh); | |
5921 #endif | |
5922 | |
5923 /* | |
5924 * Open help file (do_ecmd() will set b_help flag, readfile() will | |
5925 * set b_p_ro flag). | |
5926 * Set the alternate file to the previously edited file. | |
5927 */ | |
5928 alt_fnum = curbuf->b_fnum; | |
5929 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL, | |
1743 | 5930 ECMD_HIDE + ECMD_SET_HELP, |
5931 #ifdef FEAT_WINDOWS | |
5932 NULL /* buffer is still open, don't store info */ | |
5933 #else | |
5934 curwin | |
5935 #endif | |
5936 ); | |
22 | 5937 if (!cmdmod.keepalt) |
5938 curwin->w_alt_fnum = alt_fnum; | |
7 | 5939 empty_fnum = curbuf->b_fnum; |
5940 } | |
5941 } | |
5942 | |
5943 if (!p_im) | |
5944 restart_edit = 0; /* don't want insert mode in help file */ | |
5945 | |
3112 | 5946 #ifdef FEAT_FOLDING |
5947 /* Restore KeyTyped, setting 'filetype=help' may reset it. | |
5948 * It is needed for do_tag top open folds under the cursor. */ | |
5949 KeyTyped = old_KeyTyped; | |
5950 #endif | |
5951 | |
7 | 5952 if (tag != NULL) |
5953 do_tag(tag, DT_HELP, 1, FALSE, TRUE); | |
5954 | |
819 | 5955 /* Delete the empty buffer if we're not using it. Careful: autocommands |
5956 * may have jumped to another window, check that the buffer is not in a | |
5957 * window. */ | |
7 | 5958 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum) |
5959 { | |
5960 buf = buflist_findnr(empty_fnum); | |
819 | 5961 if (buf != NULL && buf->b_nwindows == 0) |
7 | 5962 wipe_buffer(buf, TRUE); |
5963 } | |
5964 | |
5965 /* keep the previous alternate file */ | |
22 | 5966 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt) |
7 | 5967 curwin->w_alt_fnum = alt_fnum; |
5968 | |
5969 erret: | |
5970 vim_free(tag); | |
5971 } | |
5972 | |
6228 | 5973 /* |
6234 | 5974 * ":helpclose": Close one help window |
6228 | 5975 */ |
5976 void | |
5977 ex_helpclose(eap) | |
5978 exarg_T *eap UNUSED; | |
5979 { | |
6236 | 5980 #if defined(FEAT_WINDOWS) |
6228 | 5981 win_T *win; |
5982 | |
5983 FOR_ALL_WINDOWS(win) | |
5984 { | |
5985 if (win->w_buffer->b_help) | |
5986 { | |
5987 win_close(win, FALSE); | |
6234 | 5988 return; |
6228 | 5989 } |
5990 } | |
6236 | 5991 #endif |
6228 | 5992 } |
7 | 5993 |
9 | 5994 #if defined(FEAT_MULTI_LANG) || defined(PROTO) |
5995 /* | |
5996 * In an argument search for a language specifiers in the form "@xx". | |
5997 * Changes the "@" to NUL if found, and returns a pointer to "xx". | |
5998 * Returns NULL if not found. | |
5999 */ | |
6000 char_u * | |
6001 check_help_lang(arg) | |
6002 char_u *arg; | |
6003 { | |
835 | 6004 int len = (int)STRLEN(arg); |
9 | 6005 |
6006 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2]) | |
6007 && ASCII_ISALPHA(arg[len - 1])) | |
6008 { | |
6009 arg[len - 3] = NUL; /* remove the '@' */ | |
6010 return arg + len - 2; | |
6011 } | |
6012 return NULL; | |
6013 } | |
6014 #endif | |
6015 | |
7 | 6016 /* |
6017 * Return a heuristic indicating how well the given string matches. The | |
6018 * smaller the number, the better the match. This is the order of priorities, | |
6019 * from best match to worst match: | |
6020 * - Match with least alpha-numeric characters is better. | |
6021 * - Match with least total characters is better. | |
6022 * - Match towards the start is better. | |
6023 * - Match starting with "+" is worse (feature instead of command) | |
6024 * Assumption is made that the matched_string passed has already been found to | |
6025 * match some string for which help is requested. webb. | |
6026 */ | |
6027 int | |
6028 help_heuristic(matched_string, offset, wrong_case) | |
6029 char_u *matched_string; | |
6030 int offset; /* offset for match */ | |
6031 int wrong_case; /* no matching case */ | |
6032 { | |
6033 int num_letters; | |
6034 char_u *p; | |
6035 | |
6036 num_letters = 0; | |
6037 for (p = matched_string; *p; p++) | |
6038 if (ASCII_ISALNUM(*p)) | |
6039 num_letters++; | |
6040 | |
6041 /* | |
6042 * Multiply the number of letters by 100 to give it a much bigger | |
6043 * weighting than the number of characters. | |
6044 * If there only is a match while ignoring case, add 5000. | |
6045 * If the match starts in the middle of a word, add 10000 to put it | |
6046 * somewhere in the last half. | |
6047 * If the match is more than 2 chars from the start, multiply by 200 to | |
6048 * put it after matches at the start. | |
6049 */ | |
6050 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0 | |
6051 && ASCII_ISALNUM(matched_string[offset - 1])) | |
6052 offset += 10000; | |
6053 else if (offset > 2) | |
6054 offset *= 200; | |
6055 if (wrong_case) | |
6056 offset += 5000; | |
6057 /* Features are less interesting than the subjects themselves, but "+" | |
6058 * alone is not a feature. */ | |
6059 if (matched_string[0] == '+' && matched_string[1] != NUL) | |
6060 offset += 100; | |
6061 return (int)(100 * num_letters + STRLEN(matched_string) + offset); | |
6062 } | |
6063 | |
6064 /* | |
6065 * Compare functions for qsort() below, that checks the help heuristics number | |
6066 * that has been put after the tagname by find_tags(). | |
6067 */ | |
6068 static int | |
6069 #ifdef __BORLANDC__ | |
6070 _RTLENTRYF | |
6071 #endif | |
6072 help_compare(s1, s2) | |
6073 const void *s1; | |
6074 const void *s2; | |
6075 { | |
6076 char *p1; | |
6077 char *p2; | |
6078 | |
6079 p1 = *(char **)s1 + strlen(*(char **)s1) + 1; | |
6080 p2 = *(char **)s2 + strlen(*(char **)s2) + 1; | |
6081 return strcmp(p1, p2); | |
6082 } | |
6083 | |
6084 /* | |
6085 * Find all help tags matching "arg", sort them and return in matches[], with | |
6086 * the number of matches in num_matches. | |
6087 * The matches will be sorted with a "best" match algorithm. | |
6088 * When "keep_lang" is TRUE try keeping the language of the current buffer. | |
6089 */ | |
6090 int | |
6091 find_help_tags(arg, num_matches, matches, keep_lang) | |
6092 char_u *arg; | |
6093 int *num_matches; | |
6094 char_u ***matches; | |
6095 int keep_lang; | |
6096 { | |
6097 char_u *s, *d; | |
6098 int i; | |
6099 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*", | |
445 | 6100 "/*", "/\\*", "\"*", "**", |
5269
7d1f89b27103
updated for version 7.4b.011
Bram Moolenaar <bram@vim.org>
parents:
5257
diff
changeset
|
6101 "cpo-*", "/\\(\\)", "/\\%(\\)", |
323 | 6102 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?", |
279 | 6103 "/\\?", "/\\z(\\)", "\\=", ":s\\=", |
7 | 6104 "[count]", "[quotex]", "[range]", |
5647 | 6105 "[pattern]", "\\|", "\\%$", |
6106 "s/\\~", "s/\\U", "s/\\L", | |
6107 "s/\\1", "s/\\2", "s/\\3", "s/\\9"}; | |
7 | 6108 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star", |
445 | 6109 "/star", "/\\\\star", "quotestar", "starstar", |
5269
7d1f89b27103
updated for version 7.4b.011
Bram Moolenaar <bram@vim.org>
parents:
5257
diff
changeset
|
6110 "cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)", |
323 | 6111 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?", |
279 | 6112 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=", |
7 | 6113 "\\[count]", "\\[quotex]", "\\[range]", |
5647 | 6114 "\\[pattern]", "\\\\bar", "/\\\\%\\$", |
5867 | 6115 "s/\\\\\\~", "s/\\\\U", "s/\\\\L", |
5647 | 6116 "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"}; |
7 | 6117 int flags; |
6118 | |
6119 d = IObuff; /* assume IObuff is long enough! */ | |
6120 | |
6121 /* | |
6122 * Recognize a few exceptions to the rule. Some strings that contain '*' | |
6123 * with "star". Otherwise '*' is recognized as a wildcard. | |
6124 */ | |
1872 | 6125 for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; ) |
7 | 6126 if (STRCMP(arg, mtable[i]) == 0) |
6127 { | |
6128 STRCPY(d, rtable[i]); | |
6129 break; | |
6130 } | |
6131 | |
6132 if (i < 0) /* no match in table */ | |
6133 { | |
6134 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched. | |
6135 * Also replace "\%^" and "\%(", they match every tag too. | |
6136 * Also "\zs", "\z1", etc. | |
6137 * Also "\@<", "\@=", "\@<=", etc. | |
6138 * And also "\_$" and "\_^". */ | |
6139 if (arg[0] == '\\' | |
6140 && ((arg[1] != NUL && arg[2] == NUL) | |
6141 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL | |
6142 && arg[2] != NUL))) | |
6143 { | |
6144 STRCPY(d, "/\\\\"); | |
6145 STRCPY(d + 3, arg + 1); | |
6146 /* Check for "/\\_$", should be "/\\_\$" */ | |
6147 if (d[3] == '_' && d[4] == '$') | |
6148 STRCPY(d + 4, "\\$"); | |
6149 } | |
6150 else | |
6151 { | |
3786 | 6152 /* Replace: |
6153 * "[:...:]" with "\[:...:]" | |
6154 * "[++...]" with "\[++...]" | |
5867 | 6155 * "\{" with "\\{" -- matching "} \}" |
3786 | 6156 */ |
6157 if ((arg[0] == '[' && (arg[1] == ':' | |
6158 || (arg[1] == '+' && arg[2] == '+'))) | |
6159 || (arg[0] == '\\' && arg[1] == '{')) | |
7 | 6160 *d++ = '\\'; |
6161 | |
6162 for (s = arg; *s; ++s) | |
6163 { | |
6164 /* | |
6165 * Replace "|" with "bar" and '"' with "quote" to match the name of | |
6166 * the tags for these commands. | |
6167 * Replace "*" with ".*" and "?" with "." to match command line | |
6168 * completion. | |
6169 * Insert a backslash before '~', '$' and '.' to avoid their | |
6170 * special meaning. | |
6171 */ | |
6172 if (d - IObuff > IOSIZE - 10) /* getting too long!? */ | |
6173 break; | |
6174 switch (*s) | |
6175 { | |
6176 case '|': STRCPY(d, "bar"); | |
6177 d += 3; | |
6178 continue; | |
6179 case '"': STRCPY(d, "quote"); | |
6180 d += 5; | |
6181 continue; | |
6182 case '*': *d++ = '.'; | |
6183 break; | |
6184 case '?': *d++ = '.'; | |
6185 continue; | |
6186 case '$': | |
6187 case '.': | |
6188 case '~': *d++ = '\\'; | |
6189 break; | |
6190 } | |
6191 | |
6192 /* | |
6193 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make | |
6194 * ":help i_^_CTRL-D" work. | |
6195 * Insert '-' before and after "CTRL-X" when applicable. | |
6196 */ | |
6197 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1]) | |
6198 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL))) | |
6199 { | |
5282
8c42772f0543
updated for version 7.4b.017
Bram Moolenaar <bram@vim.org>
parents:
5269
diff
changeset
|
6200 if (d > IObuff && d[-1] != '_' && d[-1] != '\\') |
8c42772f0543
updated for version 7.4b.017
Bram Moolenaar <bram@vim.org>
parents:
5269
diff
changeset
|
6201 *d++ = '_'; /* prepend a '_' to make x_CTRL-x */ |
7 | 6202 STRCPY(d, "CTRL-"); |
6203 d += 5; | |
6204 if (*s < ' ') | |
6205 { | |
6206 #ifdef EBCDIC | |
6207 *d++ = CtrlChar(*s); | |
6208 #else | |
6209 *d++ = *s + '@'; | |
6210 #endif | |
6211 if (d[-1] == '\\') | |
6212 *d++ = '\\'; /* double a backslash */ | |
6213 } | |
6214 else | |
6215 *d++ = *++s; | |
6216 if (s[1] != NUL && s[1] != '_') | |
6217 *d++ = '_'; /* append a '_' */ | |
6218 continue; | |
6219 } | |
6220 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */ | |
6221 *d++ = '\\'; | |
6222 | |
6223 /* | |
6224 * Insert a backslash before a backslash after a slash, for search | |
6225 * pattern tags: "/\|" --> "/\\|". | |
6226 */ | |
6227 else if (s[0] == '\\' && s[1] != '\\' | |
6228 && *arg == '/' && s == arg + 1) | |
6229 *d++ = '\\'; | |
6230 | |
6231 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in | |
6232 * "CTRL-\_CTRL-N" */ | |
6233 if (STRNICMP(s, "CTRL-\\_", 7) == 0) | |
6234 { | |
6235 STRCPY(d, "CTRL-\\\\"); | |
6236 d += 7; | |
6237 s += 6; | |
6238 } | |
6239 | |
6240 *d++ = *s; | |
6241 | |
6242 /* | |
6243 * If tag starts with ', toss everything after a second '. Fixes | |
6244 * CTRL-] on 'option'. (would include the trailing '.'). | |
6245 */ | |
6246 if (*s == '\'' && s > arg && *arg == '\'') | |
6247 break; | |
6248 } | |
6249 *d = NUL; | |
3480 | 6250 |
6251 if (*IObuff == '`') | |
6252 { | |
6253 if (d > IObuff + 2 && d[-1] == '`') | |
6254 { | |
6255 /* remove the backticks from `command` */ | |
6256 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff)); | |
6257 d[-2] = NUL; | |
6258 } | |
6259 else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',') | |
6260 { | |
6261 /* remove the backticks and comma from `command`, */ | |
6262 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff)); | |
6263 d[-3] = NUL; | |
6264 } | |
6265 else if (d > IObuff + 4 && d[-3] == '`' | |
6266 && d[-2] == '\\' && d[-1] == '.') | |
6267 { | |
6268 /* remove the backticks and dot from `command`\. */ | |
6269 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff)); | |
6270 d[-4] = NUL; | |
6271 } | |
6272 } | |
7 | 6273 } |
6274 } | |
6275 | |
6276 *matches = (char_u **)""; | |
6277 *num_matches = 0; | |
6278 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE; | |
6279 if (keep_lang) | |
6280 flags |= TAG_KEEP_LANG; | |
1696 | 6281 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK |
7 | 6282 && *num_matches > 0) |
1696 | 6283 { |
7 | 6284 /* Sort the matches found on the heuristic number that is after the |
6285 * tag name. */ | |
6286 qsort((void *)*matches, (size_t)*num_matches, | |
6287 sizeof(char_u *), help_compare); | |
1696 | 6288 /* Delete more than TAG_MANY to reduce the size of the listing. */ |
6289 while (*num_matches > TAG_MANY) | |
6290 vim_free((*matches)[--*num_matches]); | |
6291 } | |
7 | 6292 return OK; |
6293 } | |
6294 | |
6295 /* | |
6365 | 6296 * Called when starting to edit a buffer for a help file. |
6297 */ | |
6298 static void | |
6299 prepare_help_buffer() | |
6300 { | |
6301 char_u *p; | |
6302 | |
6303 curbuf->b_help = TRUE; | |
6304 #ifdef FEAT_QUICKFIX | |
6305 set_string_option_direct((char_u *)"buftype", -1, | |
6306 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0); | |
6307 #endif | |
6308 | |
6309 /* | |
6310 * Always set these options after jumping to a help tag, because the | |
6311 * user may have an autocommand that gets in the way. | |
6312 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and | |
6313 * latin1 word characters (for translated help files). | |
6314 * Only set it when needed, buf_init_chartab() is some work. | |
6315 */ | |
6316 p = | |
6317 #ifdef EBCDIC | |
6318 (char_u *)"65-255,^*,^|,^\""; | |
6319 #else | |
6320 (char_u *)"!-~,^*,^|,^\",192-255"; | |
6321 #endif | |
6322 if (STRCMP(curbuf->b_p_isk, p) != 0) | |
6323 { | |
6324 set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0); | |
6325 check_buf_options(curbuf); | |
6326 (void)buf_init_chartab(curbuf, FALSE); | |
6327 } | |
6328 | |
6415 | 6329 #ifdef FEAT_FOLDING |
6365 | 6330 /* Don't use the global foldmethod.*/ |
6331 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual", | |
6332 OPT_FREE|OPT_LOCAL, 0); | |
6415 | 6333 #endif |
6365 | 6334 |
6335 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */ | |
6336 curwin->w_p_list = FALSE; /* no list mode */ | |
6337 | |
6338 curbuf->b_p_ma = FALSE; /* not modifiable */ | |
6339 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */ | |
6340 curwin->w_p_nu = 0; /* no line numbers */ | |
6341 curwin->w_p_rnu = 0; /* no relative line numbers */ | |
6342 RESET_BINDING(curwin); /* no scroll or cursor binding */ | |
6343 #ifdef FEAT_ARABIC | |
6344 curwin->w_p_arab = FALSE; /* no arabic mode */ | |
6345 #endif | |
6346 #ifdef FEAT_RIGHTLEFT | |
6347 curwin->w_p_rl = FALSE; /* help window is left-to-right */ | |
6348 #endif | |
6349 #ifdef FEAT_FOLDING | |
6350 curwin->w_p_fen = FALSE; /* No folding in the help window */ | |
6351 #endif | |
6352 #ifdef FEAT_DIFF | |
6353 curwin->w_p_diff = FALSE; /* No 'diff' */ | |
6354 #endif | |
6355 #ifdef FEAT_SPELL | |
6356 curwin->w_p_spell = FALSE; /* No spell checking */ | |
6357 #endif | |
6358 | |
6359 set_buflisted(FALSE); | |
6360 } | |
6361 | |
6362 /* | |
7 | 6363 * After reading a help file: May cleanup a help buffer when syntax |
6364 * highlighting is not used. | |
6365 */ | |
6366 void | |
6367 fix_help_buffer() | |
6368 { | |
6369 linenr_T lnum; | |
6370 char_u *line; | |
6371 int in_example = FALSE; | |
6372 int len; | |
3141 | 6373 char_u *fname; |
7 | 6374 char_u *p; |
6375 char_u *rt; | |
6376 int mustfree; | |
6377 | |
6378 /* set filetype to "help". */ | |
6379 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL); | |
6380 | |
6381 #ifdef FEAT_SYN_HL | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
6382 if (!syntax_present(curwin)) |
7 | 6383 #endif |
6384 { | |
6385 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) | |
6386 { | |
6387 line = ml_get_buf(curbuf, lnum, FALSE); | |
6388 len = (int)STRLEN(line); | |
6389 if (in_example && len > 0 && !vim_iswhite(line[0])) | |
6390 { | |
6391 /* End of example: non-white or '<' in first column. */ | |
6392 if (line[0] == '<') | |
6393 { | |
6394 /* blank-out a '<' in the first column */ | |
6395 line = ml_get_buf(curbuf, lnum, TRUE); | |
6396 line[0] = ' '; | |
6397 } | |
6398 in_example = FALSE; | |
6399 } | |
6400 if (!in_example && len > 0) | |
6401 { | |
6402 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' ')) | |
6403 { | |
6404 /* blank-out a '>' in the last column (start of example) */ | |
6405 line = ml_get_buf(curbuf, lnum, TRUE); | |
6406 line[len - 1] = ' '; | |
6407 in_example = TRUE; | |
6408 } | |
6409 else if (line[len - 1] == '~') | |
6410 { | |
6411 /* blank-out a '~' at the end of line (header marker) */ | |
6412 line = ml_get_buf(curbuf, lnum, TRUE); | |
6413 line[len - 1] = ' '; | |
6414 } | |
6415 } | |
6416 } | |
6417 } | |
6418 | |
6419 /* | |
3141 | 6420 * In the "help.txt" and "help.abx" file, add the locally added help |
6421 * files. This uses the very first line in the help file. | |
7 | 6422 */ |
3141 | 6423 fname = gettail(curbuf->b_fname); |
6424 if (fnamecmp(fname, "help.txt") == 0 | |
6425 #ifdef FEAT_MULTI_LANG | |
6426 || (fnamencmp(fname, "help.", 5) == 0 | |
6427 && ASCII_ISALPHA(fname[5]) | |
6428 && ASCII_ISALPHA(fname[6]) | |
6429 && TOLOWER_ASC(fname[7]) == 'x' | |
6430 && fname[8] == NUL) | |
6431 #endif | |
6432 ) | |
7 | 6433 { |
6434 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum) | |
6435 { | |
6436 line = ml_get_buf(curbuf, lnum, FALSE); | |
3141 | 6437 if (strstr((char *)line, "*local-additions*") == NULL) |
6438 continue; | |
6439 | |
6440 /* Go through all directories in 'runtimepath', skipping | |
6441 * $VIMRUNTIME. */ | |
6442 p = p_rtp; | |
6443 while (*p != NUL) | |
7 | 6444 { |
3141 | 6445 copy_option_part(&p, NameBuff, MAXPATHL, ","); |
6446 mustfree = FALSE; | |
6447 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree); | |
6448 if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME) | |
7 | 6449 { |
3141 | 6450 int fcount; |
6451 char_u **fnames; | |
6452 FILE *fd; | |
6453 char_u *s; | |
6454 int fi; | |
7 | 6455 #ifdef FEAT_MBYTE |
3141 | 6456 vimconv_T vc; |
6457 char_u *cp; | |
6458 #endif | |
6459 | |
6460 /* Find all "doc/ *.txt" files in this directory. */ | |
6461 add_pathsep(NameBuff); | |
6462 #ifdef FEAT_MULTI_LANG | |
6463 STRCAT(NameBuff, "doc/*.??[tx]"); | |
7 | 6464 #else |
3141 | 6465 STRCAT(NameBuff, "doc/*.txt"); |
6466 #endif | |
6467 if (gen_expand_wildcards(1, &NameBuff, &fcount, | |
6468 &fnames, EW_FILE|EW_SILENT) == OK | |
6469 && fcount > 0) | |
6470 { | |
6471 #ifdef FEAT_MULTI_LANG | |
6472 int i1; | |
6473 int i2; | |
6474 char_u *f1; | |
6475 char_u *f2; | |
6476 char_u *t1; | |
6477 char_u *e1; | |
6478 char_u *e2; | |
6479 | |
6480 /* If foo.abx is found use it instead of foo.txt in | |
6481 * the same directory. */ | |
6482 for (i1 = 0; i1 < fcount; ++i1) | |
6483 { | |
6484 for (i2 = 0; i2 < fcount; ++i2) | |
6485 { | |
6486 if (i1 == i2) | |
6487 continue; | |
6488 if (fnames[i1] == NULL || fnames[i2] == NULL) | |
6489 continue; | |
6490 f1 = fnames[i1]; | |
6491 f2 = fnames[i2]; | |
6492 t1 = gettail(f1); | |
6493 if (fnamencmp(f1, f2, t1 - f1) != 0) | |
6494 continue; | |
6495 e1 = vim_strrchr(t1, '.'); | |
6496 e2 = vim_strrchr(gettail(f2), '.'); | |
6497 if (e1 == NUL || e2 == NUL) | |
6498 continue; | |
6499 if (fnamecmp(e1, ".txt") != 0 | |
6500 && fnamecmp(e1, fname + 4) != 0) | |
6501 { | |
6502 /* Not .txt and not .abx, remove it. */ | |
6503 vim_free(fnames[i1]); | |
6504 fnames[i1] = NULL; | |
6505 continue; | |
6506 } | |
6507 if (fnamencmp(f1, f2, e1 - f1) != 0) | |
6508 continue; | |
6509 if (fnamecmp(e1, ".txt") == 0 | |
6510 && fnamecmp(e2, fname + 4) == 0) | |
6511 { | |
6512 /* use .abx instead of .txt */ | |
6513 vim_free(fnames[i1]); | |
6514 fnames[i1] = NULL; | |
7 | 6515 } |
6516 } | |
6517 } | |
3141 | 6518 #endif |
6519 for (fi = 0; fi < fcount; ++fi) | |
6520 { | |
6521 if (fnames[fi] == NULL) | |
6522 continue; | |
6523 fd = mch_fopen((char *)fnames[fi], "r"); | |
6524 if (fd != NULL) | |
6525 { | |
6526 vim_fgets(IObuff, IOSIZE, fd); | |
6527 if (IObuff[0] == '*' | |
6528 && (s = vim_strchr(IObuff + 1, '*')) | |
6529 != NULL) | |
6530 { | |
6531 #ifdef FEAT_MBYTE | |
6532 int this_utf = MAYBE; | |
6533 #endif | |
6534 /* Change tag definition to a | |
6535 * reference and remove <CR>/<NL>. */ | |
6536 IObuff[0] = '|'; | |
6537 *s = '|'; | |
6538 while (*s != NUL) | |
6539 { | |
6540 if (*s == '\r' || *s == '\n') | |
6541 *s = NUL; | |
6542 #ifdef FEAT_MBYTE | |
6543 /* The text is utf-8 when a byte | |
6544 * above 127 is found and no | |
6545 * illegal byte sequence is found. | |
6546 */ | |
6547 if (*s >= 0x80 && this_utf != FALSE) | |
6548 { | |
6549 int l; | |
6550 | |
6551 this_utf = TRUE; | |
6552 l = utf_ptr2len(s); | |
6553 if (l == 1) | |
6554 this_utf = FALSE; | |
6555 s += l - 1; | |
6556 } | |
6557 #endif | |
6558 ++s; | |
6559 } | |
6560 #ifdef FEAT_MBYTE | |
6561 /* The help file is latin1 or utf-8; | |
6562 * conversion to the current | |
6563 * 'encoding' may be required. */ | |
6564 vc.vc_type = CONV_NONE; | |
6565 convert_setup(&vc, (char_u *)( | |
6566 this_utf == TRUE ? "utf-8" | |
6567 : "latin1"), p_enc); | |
6568 if (vc.vc_type == CONV_NONE) | |
6569 /* No conversion needed. */ | |
6570 cp = IObuff; | |
6571 else | |
6572 { | |
6573 /* Do the conversion. If it fails | |
6574 * use the unconverted text. */ | |
6575 cp = string_convert(&vc, IObuff, | |
6576 NULL); | |
6577 if (cp == NULL) | |
6578 cp = IObuff; | |
6579 } | |
6580 convert_setup(&vc, NULL, NULL); | |
6581 | |
6582 ml_append(lnum, cp, (colnr_T)0, FALSE); | |
6583 if (cp != IObuff) | |
6584 vim_free(cp); | |
6585 #else | |
6586 ml_append(lnum, IObuff, (colnr_T)0, | |
6587 FALSE); | |
6588 #endif | |
6589 ++lnum; | |
6590 } | |
6591 fclose(fd); | |
6592 } | |
6593 } | |
6594 FreeWild(fcount, fnames); | |
7 | 6595 } |
6596 } | |
3141 | 6597 if (mustfree) |
6598 vim_free(rt); | |
7 | 6599 } |
3141 | 6600 break; |
7 | 6601 } |
6602 } | |
6603 } | |
6604 | |
40 | 6605 /* |
6606 * ":exusage" | |
6607 */ | |
6608 void | |
6609 ex_exusage(eap) | |
1876 | 6610 exarg_T *eap UNUSED; |
40 | 6611 { |
6612 do_cmdline_cmd((char_u *)"help ex-cmd-index"); | |
6613 } | |
6614 | |
6615 /* | |
6616 * ":viusage" | |
6617 */ | |
6618 void | |
6619 ex_viusage(eap) | |
1876 | 6620 exarg_T *eap UNUSED; |
40 | 6621 { |
6622 do_cmdline_cmd((char_u *)"help normal-index"); | |
6623 } | |
6624 | |
7 | 6625 #if defined(FEAT_EX_EXTRA) || defined(PROTO) |
1502 | 6626 static void helptags_one __ARGS((char_u *dir, char_u *ext, char_u *lang, int add_help_tags)); |
7 | 6627 |
6628 /* | |
6629 * ":helptags" | |
6630 */ | |
6631 void | |
6632 ex_helptags(eap) | |
6633 exarg_T *eap; | |
6634 { | |
6635 garray_T ga; | |
6636 int i, j; | |
6637 int len; | |
9 | 6638 #ifdef FEAT_MULTI_LANG |
7 | 6639 char_u lang[2]; |
9 | 6640 #endif |
1507 | 6641 expand_T xpc; |
6642 char_u *dirname; | |
7 | 6643 char_u ext[5]; |
6644 char_u fname[8]; | |
6645 int filecount; | |
6646 char_u **files; | |
1502 | 6647 int add_help_tags = FALSE; |
6648 | |
6649 /* Check for ":helptags ++t {dir}". */ | |
6650 if (STRNCMP(eap->arg, "++t", 3) == 0 && vim_iswhite(eap->arg[3])) | |
6651 { | |
6652 add_help_tags = TRUE; | |
6653 eap->arg = skipwhite(eap->arg + 3); | |
6654 } | |
7 | 6655 |
1507 | 6656 ExpandInit(&xpc); |
6657 xpc.xp_context = EXPAND_DIRECTORIES; | |
6658 dirname = ExpandOne(&xpc, eap->arg, NULL, | |
6659 WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE); | |
6660 if (dirname == NULL || !mch_isdir(dirname)) | |
7 | 6661 { |
6662 EMSG2(_("E150: Not a directory: %s"), eap->arg); | |
7226
9b350a65138a
commit https://github.com/vim/vim/commit/1c2836e268ce930bca9ea1287d0d83e92ce1b3ff
Christian Brabandt <cb@256bit.org>
parents:
7194
diff
changeset
|
6663 vim_free(dirname); |
7 | 6664 return; |
6665 } | |
6666 | |
6667 #ifdef FEAT_MULTI_LANG | |
3957 | 6668 /* Get a list of all files in the help directory and in subdirectories. */ |
1507 | 6669 STRCPY(NameBuff, dirname); |
7 | 6670 add_pathsep(NameBuff); |
3957 | 6671 STRCAT(NameBuff, "**"); |
7 | 6672 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files, |
6673 EW_FILE|EW_SILENT) == FAIL | |
6674 || filecount == 0) | |
6675 { | |
6676 EMSG2("E151: No match: %s", NameBuff); | |
1507 | 6677 vim_free(dirname); |
7 | 6678 return; |
6679 } | |
6680 | |
6681 /* Go over all files in the directory to find out what languages are | |
6682 * present. */ | |
6683 ga_init2(&ga, 1, 10); | |
6684 for (i = 0; i < filecount; ++i) | |
6685 { | |
835 | 6686 len = (int)STRLEN(files[i]); |
7 | 6687 if (len > 4) |
6688 { | |
6689 if (STRICMP(files[i] + len - 4, ".txt") == 0) | |
6690 { | |
6691 /* ".txt" -> language "en" */ | |
6692 lang[0] = 'e'; | |
6693 lang[1] = 'n'; | |
6694 } | |
6695 else if (files[i][len - 4] == '.' | |
6696 && ASCII_ISALPHA(files[i][len - 3]) | |
6697 && ASCII_ISALPHA(files[i][len - 2]) | |
6698 && TOLOWER_ASC(files[i][len - 1]) == 'x') | |
6699 { | |
6700 /* ".abx" -> language "ab" */ | |
6701 lang[0] = TOLOWER_ASC(files[i][len - 3]); | |
6702 lang[1] = TOLOWER_ASC(files[i][len - 2]); | |
6703 } | |
6704 else | |
6705 continue; | |
6706 | |
6707 /* Did we find this language already? */ | |
6708 for (j = 0; j < ga.ga_len; j += 2) | |
6709 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0) | |
6710 break; | |
6711 if (j == ga.ga_len) | |
6712 { | |
6713 /* New language, add it. */ | |
6714 if (ga_grow(&ga, 2) == FAIL) | |
6715 break; | |
6716 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0]; | |
6717 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1]; | |
6718 } | |
6719 } | |
6720 } | |
6721 | |
6722 /* | |
6723 * Loop over the found languages to generate a tags file for each one. | |
6724 */ | |
6725 for (j = 0; j < ga.ga_len; j += 2) | |
6726 { | |
6727 STRCPY(fname, "tags-xx"); | |
6728 fname[5] = ((char_u *)ga.ga_data)[j]; | |
6729 fname[6] = ((char_u *)ga.ga_data)[j + 1]; | |
6730 if (fname[5] == 'e' && fname[6] == 'n') | |
6731 { | |
6732 /* English is an exception: use ".txt" and "tags". */ | |
6733 fname[4] = NUL; | |
6734 STRCPY(ext, ".txt"); | |
6735 } | |
6736 else | |
6737 { | |
6738 /* Language "ab" uses ".abx" and "tags-ab". */ | |
6739 STRCPY(ext, ".xxx"); | |
6740 ext[1] = fname[5]; | |
6741 ext[2] = fname[6]; | |
6742 } | |
1507 | 6743 helptags_one(dirname, ext, fname, add_help_tags); |
7 | 6744 } |
6745 | |
6746 ga_clear(&ga); | |
6747 FreeWild(filecount, files); | |
6748 | |
6749 #else | |
6750 /* No language support, just use "*.txt" and "tags". */ | |
1507 | 6751 helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags); |
6752 #endif | |
6753 vim_free(dirname); | |
7 | 6754 } |
6755 | |
6756 static void | |
1502 | 6757 helptags_one(dir, ext, tagfname, add_help_tags) |
6758 char_u *dir; /* doc directory */ | |
6759 char_u *ext; /* suffix, ".txt", ".itx", ".frx", etc. */ | |
3957 | 6760 char_u *tagfname; /* "tags" for English, "tags-fr" for French. */ |
6761 int add_help_tags; /* add "help-tags" tag */ | |
7 | 6762 { |
6763 FILE *fd_tags; | |
6764 FILE *fd; | |
6765 garray_T ga; | |
6766 int filecount; | |
6767 char_u **files; | |
6768 char_u *p1, *p2; | |
6769 int fi; | |
6770 char_u *s; | |
6771 int i; | |
6772 char_u *fname; | |
3957 | 6773 int dirlen; |
7 | 6774 # ifdef FEAT_MBYTE |
6775 int utf8 = MAYBE; | |
6776 int this_utf8; | |
6777 int firstline; | |
6778 int mix = FALSE; /* detected mixed encodings */ | |
6779 # endif | |
6780 | |
6781 /* | |
6782 * Find all *.txt files. | |
6783 */ | |
3978 | 6784 dirlen = (int)STRLEN(dir); |
7 | 6785 STRCPY(NameBuff, dir); |
3957 | 6786 STRCAT(NameBuff, "/**/*"); |
7 | 6787 STRCAT(NameBuff, ext); |
6788 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files, | |
6789 EW_FILE|EW_SILENT) == FAIL | |
6790 || filecount == 0) | |
6791 { | |
6792 if (!got_int) | |
6793 EMSG2("E151: No match: %s", NameBuff); | |
6794 return; | |
6795 } | |
6796 | |
6797 /* | |
6798 * Open the tags file for writing. | |
6799 * Do this before scanning through all the files. | |
6800 */ | |
6801 STRCPY(NameBuff, dir); | |
6802 add_pathsep(NameBuff); | |
6803 STRCAT(NameBuff, tagfname); | |
531 | 6804 fd_tags = mch_fopen((char *)NameBuff, "w"); |
7 | 6805 if (fd_tags == NULL) |
6806 { | |
6807 EMSG2(_("E152: Cannot open %s for writing"), NameBuff); | |
6808 FreeWild(filecount, files); | |
6809 return; | |
6810 } | |
6811 | |
6812 /* | |
1502 | 6813 * If using the "++t" argument or generating tags for "$VIMRUNTIME/doc" |
6814 * add the "help-tags" tag. | |
7 | 6815 */ |
6816 ga_init2(&ga, (int)sizeof(char_u *), 100); | |
1502 | 6817 if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc", |
6818 dir, FALSE) == FPC_SAME) | |
7 | 6819 { |
6820 if (ga_grow(&ga, 1) == FAIL) | |
6821 got_int = TRUE; | |
6822 else | |
6823 { | |
835 | 6824 s = alloc(18 + (unsigned)STRLEN(tagfname)); |
7 | 6825 if (s == NULL) |
6826 got_int = TRUE; | |
6827 else | |
6828 { | |
6829 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname); | |
6830 ((char_u **)ga.ga_data)[ga.ga_len] = s; | |
6831 ++ga.ga_len; | |
6832 } | |
6833 } | |
6834 } | |
6835 | |
6836 /* | |
6837 * Go over all the files and extract the tags. | |
6838 */ | |
6839 for (fi = 0; fi < filecount && !got_int; ++fi) | |
6840 { | |
531 | 6841 fd = mch_fopen((char *)files[fi], "r"); |
7 | 6842 if (fd == NULL) |
6843 { | |
6844 EMSG2(_("E153: Unable to open %s for reading"), files[fi]); | |
6845 continue; | |
6846 } | |
3957 | 6847 fname = files[fi] + dirlen + 1; |
7 | 6848 |
6849 # ifdef FEAT_MBYTE | |
6850 firstline = TRUE; | |
6851 # endif | |
6852 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int) | |
6853 { | |
6854 # ifdef FEAT_MBYTE | |
6855 if (firstline) | |
6856 { | |
6857 /* Detect utf-8 file by a non-ASCII char in the first line. */ | |
6858 this_utf8 = MAYBE; | |
6859 for (s = IObuff; *s != NUL; ++s) | |
6860 if (*s >= 0x80) | |
6861 { | |
6862 int l; | |
6863 | |
6864 this_utf8 = TRUE; | |
474 | 6865 l = utf_ptr2len(s); |
7 | 6866 if (l == 1) |
6867 { | |
6868 /* Illegal UTF-8 byte sequence. */ | |
6869 this_utf8 = FALSE; | |
6870 break; | |
6871 } | |
6872 s += l - 1; | |
6873 } | |
6874 if (this_utf8 == MAYBE) /* only ASCII characters found */ | |
6875 this_utf8 = FALSE; | |
6876 if (utf8 == MAYBE) /* first file */ | |
6877 utf8 = this_utf8; | |
6878 else if (utf8 != this_utf8) | |
6879 { | |
6880 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]); | |
6881 mix = !got_int; | |
6882 got_int = TRUE; | |
6883 } | |
6884 firstline = FALSE; | |
6885 } | |
6886 # endif | |
6887 p1 = vim_strchr(IObuff, '*'); /* find first '*' */ | |
6888 while (p1 != NULL) | |
6889 { | |
3514 | 6890 /* Use vim_strbyte() instead of vim_strchr() so that when |
6891 * 'encoding' is dbcs it still works, don't find '*' in the | |
6892 * second byte. */ | |
6893 p2 = vim_strbyte(p1 + 1, '*'); /* find second '*' */ | |
7 | 6894 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */ |
6895 { | |
6896 for (s = p1 + 1; s < p2; ++s) | |
6897 if (*s == ' ' || *s == '\t' || *s == '|') | |
6898 break; | |
6899 | |
6900 /* | |
6901 * Only accept a *tag* when it consists of valid | |
6902 * characters, there is white space before it and is | |
6903 * followed by a white character or end-of-line. | |
6904 */ | |
6905 if (s == p2 | |
6906 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t') | |
6907 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL | |
6908 || s[1] == '\0')) | |
6909 { | |
6910 *p2 = '\0'; | |
6911 ++p1; | |
6912 if (ga_grow(&ga, 1) == FAIL) | |
6913 { | |
6914 got_int = TRUE; | |
6915 break; | |
6916 } | |
6917 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2)); | |
6918 if (s == NULL) | |
6919 { | |
6920 got_int = TRUE; | |
6921 break; | |
6922 } | |
6923 ((char_u **)ga.ga_data)[ga.ga_len] = s; | |
6924 ++ga.ga_len; | |
6925 sprintf((char *)s, "%s\t%s", p1, fname); | |
6926 | |
6927 /* find next '*' */ | |
6928 p2 = vim_strchr(p2 + 1, '*'); | |
6929 } | |
6930 } | |
6931 p1 = p2; | |
6932 } | |
6933 line_breakcheck(); | |
6934 } | |
6935 | |
6936 fclose(fd); | |
6937 } | |
6938 | |
6939 FreeWild(filecount, files); | |
6940 | |
6941 if (!got_int) | |
6942 { | |
6943 /* | |
6944 * Sort the tags. | |
6945 */ | |
7009 | 6946 if (ga.ga_data != NULL) |
6947 sort_strings((char_u **)ga.ga_data, ga.ga_len); | |
7 | 6948 |
6949 /* | |
6950 * Check for duplicates. | |
6951 */ | |
6952 for (i = 1; i < ga.ga_len; ++i) | |
6953 { | |
6954 p1 = ((char_u **)ga.ga_data)[i - 1]; | |
6955 p2 = ((char_u **)ga.ga_data)[i]; | |
6956 while (*p1 == *p2) | |
6957 { | |
6958 if (*p2 == '\t') | |
6959 { | |
6960 *p2 = NUL; | |
274 | 6961 vim_snprintf((char *)NameBuff, MAXPATHL, |
7 | 6962 _("E154: Duplicate tag \"%s\" in file %s/%s"), |
6963 ((char_u **)ga.ga_data)[i], dir, p2 + 1); | |
6964 EMSG(NameBuff); | |
6965 *p2 = '\t'; | |
6966 break; | |
6967 } | |
6968 ++p1; | |
6969 ++p2; | |
6970 } | |
6971 } | |
6972 | |
6973 # ifdef FEAT_MBYTE | |
6974 if (utf8 == TRUE) | |
6975 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n"); | |
6976 # endif | |
6977 | |
6978 /* | |
6979 * Write the tags into the file. | |
6980 */ | |
6981 for (i = 0; i < ga.ga_len; ++i) | |
6982 { | |
6983 s = ((char_u **)ga.ga_data)[i]; | |
1325 | 6984 if (STRNCMP(s, "help-tags\t", 10) == 0) |
7 | 6985 /* help-tags entry was added in formatted form */ |
1325 | 6986 fputs((char *)s, fd_tags); |
7 | 6987 else |
6988 { | |
6989 fprintf(fd_tags, "%s\t/*", s); | |
6990 for (p1 = s; *p1 != '\t'; ++p1) | |
6991 { | |
6992 /* insert backslash before '\\' and '/' */ | |
6993 if (*p1 == '\\' || *p1 == '/') | |
6994 putc('\\', fd_tags); | |
6995 putc(*p1, fd_tags); | |
6996 } | |
6997 fprintf(fd_tags, "*\n"); | |
6998 } | |
6999 } | |
7000 } | |
7001 #ifdef FEAT_MBYTE | |
7002 if (mix) | |
7003 got_int = FALSE; /* continue with other languages */ | |
7004 #endif | |
7005 | |
7006 for (i = 0; i < ga.ga_len; ++i) | |
7007 vim_free(((char_u **)ga.ga_data)[i]); | |
7008 ga_clear(&ga); | |
7009 fclose(fd_tags); /* there is no check for an error... */ | |
7010 } | |
7011 #endif | |
7012 | |
7013 #if defined(FEAT_SIGNS) || defined(PROTO) | |
7014 | |
7015 /* | |
7016 * Struct to hold the sign properties. | |
7017 */ | |
7018 typedef struct sign sign_T; | |
7019 | |
7020 struct sign | |
7021 { | |
7022 sign_T *sn_next; /* next sign in list */ | |
2606 | 7023 int sn_typenr; /* type number of sign */ |
7 | 7024 char_u *sn_name; /* name of sign */ |
7025 char_u *sn_icon; /* name of pixmap */ | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7026 # ifdef FEAT_SIGN_ICONS |
7 | 7027 void *sn_image; /* icon image */ |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7028 # endif |
7 | 7029 char_u *sn_text; /* text used instead of pixmap */ |
7030 int sn_line_hl; /* highlight ID for line */ | |
7031 int sn_text_hl; /* highlight ID for text */ | |
7032 }; | |
7033 | |
7034 static sign_T *first_sign = NULL; | |
2605 | 7035 static int next_sign_typenr = 1; |
7 | 7036 |
1874 | 7037 static int sign_cmd_idx __ARGS((char_u *begin_cmd, char_u *end_cmd)); |
7 | 7038 static void sign_list_defined __ARGS((sign_T *sp)); |
1828 | 7039 static void sign_undefine __ARGS((sign_T *sp, sign_T *sp_prev)); |
7 | 7040 |
1868 | 7041 static char *cmds[] = { |
7042 "define", | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7043 # define SIGNCMD_DEFINE 0 |
1868 | 7044 "undefine", |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7045 # define SIGNCMD_UNDEFINE 1 |
1868 | 7046 "list", |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7047 # define SIGNCMD_LIST 2 |
1868 | 7048 "place", |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7049 # define SIGNCMD_PLACE 3 |
1868 | 7050 "unplace", |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7051 # define SIGNCMD_UNPLACE 4 |
1868 | 7052 "jump", |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7053 # define SIGNCMD_JUMP 5 |
1868 | 7054 NULL |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7055 # define SIGNCMD_LAST 6 |
1868 | 7056 }; |
7057 | |
7058 /* | |
7059 * Find index of a ":sign" subcmd from its name. | |
7060 * "*end_cmd" must be writable. | |
7061 */ | |
7062 static int | |
7063 sign_cmd_idx(begin_cmd, end_cmd) | |
1874 | 7064 char_u *begin_cmd; /* begin of sign subcmd */ |
7065 char_u *end_cmd; /* just after sign subcmd */ | |
1868 | 7066 { |
7067 int idx; | |
7068 char save = *end_cmd; | |
7069 | |
7070 *end_cmd = NUL; | |
7071 for (idx = 0; ; ++idx) | |
7072 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0) | |
7073 break; | |
7074 *end_cmd = save; | |
7075 return idx; | |
7076 } | |
7077 | |
7 | 7078 /* |
7079 * ":sign" command | |
7080 */ | |
7081 void | |
7082 ex_sign(eap) | |
7083 exarg_T *eap; | |
7084 { | |
7085 char_u *arg = eap->arg; | |
7086 char_u *p; | |
7087 int idx; | |
7088 sign_T *sp; | |
7089 sign_T *sp_prev; | |
7090 buf_T *buf; | |
7091 | |
7092 /* Parse the subcommand. */ | |
7093 p = skiptowhite(arg); | |
1868 | 7094 idx = sign_cmd_idx(arg, p); |
7095 if (idx == SIGNCMD_LAST) | |
7096 { | |
7097 EMSG2(_("E160: Unknown sign command: %s"), arg); | |
7098 return; | |
7 | 7099 } |
7100 arg = skipwhite(p); | |
7101 | |
7102 if (idx <= SIGNCMD_LIST) | |
7103 { | |
7104 /* | |
7105 * Define, undefine or list signs. | |
7106 */ | |
7107 if (idx == SIGNCMD_LIST && *arg == NUL) | |
7108 { | |
7109 /* ":sign list": list all defined signs */ | |
3411 | 7110 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next) |
7 | 7111 sign_list_defined(sp); |
7112 } | |
7113 else if (*arg == NUL) | |
7114 EMSG(_("E156: Missing sign name")); | |
7115 else | |
7116 { | |
2605 | 7117 /* Isolate the sign name. If it's a number skip leading zeroes, |
7118 * so that "099" and "99" are the same sign. But keep "0". */ | |
7 | 7119 p = skiptowhite(arg); |
7120 if (*p != NUL) | |
7121 *p++ = NUL; | |
2605 | 7122 while (arg[0] == '0' && arg[1] != NUL) |
7123 ++arg; | |
7124 | |
7 | 7125 sp_prev = NULL; |
7126 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
7127 { | |
7128 if (STRCMP(sp->sn_name, arg) == 0) | |
7129 break; | |
7130 sp_prev = sp; | |
7131 } | |
7132 if (idx == SIGNCMD_DEFINE) | |
7133 { | |
7134 /* ":sign define {name} ...": define a sign */ | |
7135 if (sp == NULL) | |
7136 { | |
2605 | 7137 sign_T *lp; |
7138 int start = next_sign_typenr; | |
7139 | |
7 | 7140 /* Allocate a new sign. */ |
7141 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T)); | |
7142 if (sp == NULL) | |
7143 return; | |
7144 | |
2605 | 7145 /* Check that next_sign_typenr is not already being used. |
7146 * This only happens after wrapping around. Hopefully | |
7147 * another one got deleted and we can use its number. */ | |
7148 for (lp = first_sign; lp != NULL; ) | |
7 | 7149 { |
2605 | 7150 if (lp->sn_typenr == next_sign_typenr) |
7 | 7151 { |
2605 | 7152 ++next_sign_typenr; |
7153 if (next_sign_typenr == MAX_TYPENR) | |
7154 next_sign_typenr = 1; | |
7155 if (next_sign_typenr == start) | |
7 | 7156 { |
2605 | 7157 vim_free(sp); |
7158 EMSG(_("E612: Too many signs defined")); | |
7159 return; | |
7 | 7160 } |
2605 | 7161 lp = first_sign; /* start all over */ |
7162 continue; | |
7 | 7163 } |
2605 | 7164 lp = lp->sn_next; |
7165 } | |
7166 | |
7167 sp->sn_typenr = next_sign_typenr; | |
7168 if (++next_sign_typenr == MAX_TYPENR) | |
7169 next_sign_typenr = 1; /* wrap around */ | |
7170 | |
7171 sp->sn_name = vim_strsave(arg); | |
7172 if (sp->sn_name == NULL) /* out of memory */ | |
7173 { | |
7174 vim_free(sp); | |
7175 return; | |
7 | 7176 } |
2601 | 7177 |
7178 /* add the new sign to the list of signs */ | |
7179 if (sp_prev == NULL) | |
7180 first_sign = sp; | |
7181 else | |
7182 sp_prev->sn_next = sp; | |
7 | 7183 } |
7184 | |
7185 /* set values for a defined sign. */ | |
7186 for (;;) | |
7187 { | |
7188 arg = skipwhite(p); | |
7189 if (*arg == NUL) | |
7190 break; | |
7191 p = skiptowhite_esc(arg); | |
7192 if (STRNCMP(arg, "icon=", 5) == 0) | |
7193 { | |
7194 arg += 5; | |
7195 vim_free(sp->sn_icon); | |
7196 sp->sn_icon = vim_strnsave(arg, (int)(p - arg)); | |
7197 backslash_halve(sp->sn_icon); | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7198 # ifdef FEAT_SIGN_ICONS |
7 | 7199 if (gui.in_use) |
7200 { | |
7201 out_flush(); | |
7202 if (sp->sn_image != NULL) | |
7203 gui_mch_destroy_sign(sp->sn_image); | |
7204 sp->sn_image = gui_mch_register_sign(sp->sn_icon); | |
7205 } | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7206 # endif |
7 | 7207 } |
7208 else if (STRNCMP(arg, "text=", 5) == 0) | |
7209 { | |
7210 char_u *s; | |
7211 int cells; | |
7212 int len; | |
7213 | |
7214 arg += 5; | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7215 # ifdef FEAT_MBYTE |
7 | 7216 /* Count cells and check for non-printable chars */ |
7217 if (has_mbyte) | |
7218 { | |
7219 cells = 0; | |
474 | 7220 for (s = arg; s < p; s += (*mb_ptr2len)(s)) |
7 | 7221 { |
7222 if (!vim_isprintc((*mb_ptr2char)(s))) | |
7223 break; | |
7224 cells += (*mb_ptr2cells)(s); | |
7225 } | |
7226 } | |
7227 else | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7228 # endif |
7 | 7229 { |
7230 for (s = arg; s < p; ++s) | |
7231 if (!vim_isprintc(*s)) | |
7232 break; | |
835 | 7233 cells = (int)(s - arg); |
7 | 7234 } |
7235 /* Currently must be one or two display cells */ | |
7236 if (s != p || cells < 1 || cells > 2) | |
7237 { | |
7238 *p = NUL; | |
7239 EMSG2(_("E239: Invalid sign text: %s"), arg); | |
7240 return; | |
7241 } | |
7242 | |
7243 vim_free(sp->sn_text); | |
7244 /* Allocate one byte more if we need to pad up | |
7245 * with a space. */ | |
835 | 7246 len = (int)(p - arg + ((cells == 1) ? 1 : 0)); |
7 | 7247 sp->sn_text = vim_strnsave(arg, len); |
7248 | |
7249 if (sp->sn_text != NULL && cells == 1) | |
7250 STRCPY(sp->sn_text + len - 1, " "); | |
7251 } | |
7252 else if (STRNCMP(arg, "linehl=", 7) == 0) | |
7253 { | |
7254 arg += 7; | |
7255 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg)); | |
7256 } | |
7257 else if (STRNCMP(arg, "texthl=", 7) == 0) | |
7258 { | |
7259 arg += 7; | |
7260 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg)); | |
7261 } | |
7262 else | |
7263 { | |
7264 EMSG2(_(e_invarg2), arg); | |
7265 return; | |
7266 } | |
7267 } | |
7268 } | |
7269 else if (sp == NULL) | |
7270 EMSG2(_("E155: Unknown sign: %s"), arg); | |
7271 else if (idx == SIGNCMD_LIST) | |
7272 /* ":sign list {name}" */ | |
7273 sign_list_defined(sp); | |
7274 else | |
7275 /* ":sign undefine {name}" */ | |
1828 | 7276 sign_undefine(sp, sp_prev); |
7 | 7277 } |
7278 } | |
7279 else | |
7280 { | |
7281 int id = -1; | |
7282 linenr_T lnum = -1; | |
7283 char_u *sign_name = NULL; | |
7284 char_u *arg1; | |
7285 | |
7286 if (*arg == NUL) | |
7287 { | |
7288 if (idx == SIGNCMD_PLACE) | |
7289 { | |
7290 /* ":sign place": list placed signs in all buffers */ | |
7291 sign_list_placed(NULL); | |
7292 } | |
7293 else if (idx == SIGNCMD_UNPLACE) | |
7294 { | |
7295 /* ":sign unplace": remove placed sign at cursor */ | |
7296 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum); | |
7297 if (id > 0) | |
7298 { | |
7299 buf_delsign(curwin->w_buffer, id); | |
7300 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum); | |
7301 } | |
7302 else | |
7303 EMSG(_("E159: Missing sign number")); | |
7304 } | |
7305 else | |
7306 EMSG(_(e_argreq)); | |
7307 return; | |
7308 } | |
7309 | |
7310 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL) | |
7311 { | |
7312 /* ":sign unplace *": remove all placed signs */ | |
7313 buf_delete_all_signs(); | |
7314 return; | |
7315 } | |
7316 | |
7317 /* first arg could be placed sign id */ | |
7318 arg1 = arg; | |
7319 if (VIM_ISDIGIT(*arg)) | |
7320 { | |
7321 id = getdigits(&arg); | |
7322 if (!vim_iswhite(*arg) && *arg != NUL) | |
7323 { | |
7324 id = -1; | |
7325 arg = arg1; | |
7326 } | |
7327 else | |
7328 { | |
7329 arg = skipwhite(arg); | |
7330 if (idx == SIGNCMD_UNPLACE && *arg == NUL) | |
7331 { | |
7332 /* ":sign unplace {id}": remove placed sign by number */ | |
7333 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
7334 if ((lnum = buf_delsign(buf, id)) != 0) | |
7335 update_debug_sign(buf, lnum); | |
7336 return; | |
7337 } | |
7338 } | |
7339 } | |
7340 | |
7341 /* | |
7342 * Check for line={lnum} name={name} and file={fname} or buffer={nr}. | |
7343 * Leave "arg" pointing to {fname}. | |
7344 */ | |
7345 for (;;) | |
7346 { | |
7347 if (STRNCMP(arg, "line=", 5) == 0) | |
7348 { | |
7349 arg += 5; | |
7350 lnum = atoi((char *)arg); | |
7351 arg = skiptowhite(arg); | |
7352 } | |
3672 | 7353 else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE) |
7354 { | |
7355 if (id != -1) | |
7356 { | |
7357 EMSG(_(e_invarg)); | |
7358 return; | |
7359 } | |
7360 id = -2; | |
7361 arg = skiptowhite(arg + 1); | |
7362 } | |
7 | 7363 else if (STRNCMP(arg, "name=", 5) == 0) |
7364 { | |
7365 arg += 5; | |
7366 sign_name = arg; | |
7367 arg = skiptowhite(arg); | |
7368 if (*arg != NUL) | |
7369 *arg++ = NUL; | |
2605 | 7370 while (sign_name[0] == '0' && sign_name[1] != NUL) |
7371 ++sign_name; | |
7 | 7372 } |
7373 else if (STRNCMP(arg, "file=", 5) == 0) | |
7374 { | |
7375 arg += 5; | |
7376 buf = buflist_findname(arg); | |
7377 break; | |
7378 } | |
7379 else if (STRNCMP(arg, "buffer=", 7) == 0) | |
7380 { | |
7381 arg += 7; | |
7382 buf = buflist_findnr((int)getdigits(&arg)); | |
7383 if (*skipwhite(arg) != NUL) | |
7384 EMSG(_(e_trailing)); | |
7385 break; | |
7386 } | |
7387 else | |
7388 { | |
7389 EMSG(_(e_invarg)); | |
7390 return; | |
7391 } | |
7392 arg = skipwhite(arg); | |
7393 } | |
7394 | |
7395 if (buf == NULL) | |
7396 { | |
7397 EMSG2(_("E158: Invalid buffer name: %s"), arg); | |
7398 } | |
3672 | 7399 else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2)) |
7 | 7400 { |
7401 if (lnum >= 0 || sign_name != NULL) | |
7402 EMSG(_(e_invarg)); | |
7403 else | |
7404 /* ":sign place file={fname}": list placed signs in one file */ | |
7405 sign_list_placed(buf); | |
7406 } | |
7407 else if (idx == SIGNCMD_JUMP) | |
7408 { | |
7409 /* ":sign jump {id} file={fname}" */ | |
7410 if (lnum >= 0 || sign_name != NULL) | |
7411 EMSG(_(e_invarg)); | |
7412 else if ((lnum = buf_findsign(buf, id)) > 0) | |
7413 { /* goto a sign ... */ | |
7414 if (buf_jump_open_win(buf) != NULL) | |
7415 { /* ... in a current window */ | |
7416 curwin->w_cursor.lnum = lnum; | |
7417 check_cursor_lnum(); | |
7418 beginline(BL_WHITE); | |
7419 } | |
7420 else | |
7421 { /* ... not currently in a window */ | |
7422 char_u *cmd; | |
7423 | |
7424 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25); | |
7425 if (cmd == NULL) | |
7426 return; | |
7427 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname); | |
7428 do_cmdline_cmd(cmd); | |
7429 vim_free(cmd); | |
7430 } | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7431 # ifdef FEAT_FOLDING |
7 | 7432 foldOpenCursor(); |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7433 # endif |
7 | 7434 } |
7435 else | |
7436 EMSGN(_("E157: Invalid sign ID: %ld"), id); | |
7437 } | |
7438 else if (idx == SIGNCMD_UNPLACE) | |
7439 { | |
7440 if (lnum >= 0 || sign_name != NULL) | |
7441 EMSG(_(e_invarg)); | |
3672 | 7442 else if (id == -2) |
7443 { | |
7444 /* ":sign unplace * file={fname}" */ | |
7445 redraw_buf_later(buf, NOT_VALID); | |
7446 buf_delete_signs(buf); | |
7447 } | |
7 | 7448 else |
7449 { | |
3672 | 7450 /* ":sign unplace {id} file={fname}" */ |
7 | 7451 lnum = buf_delsign(buf, id); |
7452 update_debug_sign(buf, lnum); | |
7453 } | |
7454 } | |
7455 /* idx == SIGNCMD_PLACE */ | |
7456 else if (sign_name != NULL) | |
7457 { | |
7458 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
7459 if (STRCMP(sp->sn_name, sign_name) == 0) | |
7460 break; | |
7461 if (sp == NULL) | |
7462 { | |
7463 EMSG2(_("E155: Unknown sign: %s"), sign_name); | |
7464 return; | |
7465 } | |
7466 if (lnum > 0) | |
7467 /* ":sign place {id} line={lnum} name={name} file={fname}": | |
7468 * place a sign */ | |
7469 buf_addsign(buf, id, lnum, sp->sn_typenr); | |
7470 else | |
7471 /* ":sign place {id} file={fname}": change sign type */ | |
7472 lnum = buf_change_sign_type(buf, id, sp->sn_typenr); | |
5865 | 7473 if (lnum > 0) |
7474 update_debug_sign(buf, lnum); | |
7475 else | |
7476 EMSG2(_("E885: Not possible to change sign %s"), sign_name); | |
7 | 7477 } |
7478 else | |
7479 EMSG(_(e_invarg)); | |
7480 } | |
7481 } | |
7482 | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7483 # if defined(FEAT_SIGN_ICONS) || defined(PROTO) |
7 | 7484 /* |
7485 * Allocate the icons. Called when the GUI has started. Allows defining | |
7486 * signs before it starts. | |
7487 */ | |
7488 void | |
7489 sign_gui_started() | |
7490 { | |
7491 sign_T *sp; | |
7492 | |
7493 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
7494 if (sp->sn_icon != NULL) | |
7495 sp->sn_image = gui_mch_register_sign(sp->sn_icon); | |
7496 } | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7497 # endif |
7 | 7498 |
7499 /* | |
7500 * List one sign. | |
7501 */ | |
7502 static void | |
7503 sign_list_defined(sp) | |
7504 sign_T *sp; | |
7505 { | |
7506 char_u *p; | |
7507 | |
274 | 7508 smsg((char_u *)"sign %s", sp->sn_name); |
7 | 7509 if (sp->sn_icon != NULL) |
7510 { | |
7511 MSG_PUTS(" icon="); | |
7512 msg_outtrans(sp->sn_icon); | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7513 # ifdef FEAT_SIGN_ICONS |
7 | 7514 if (sp->sn_image == NULL) |
7515 MSG_PUTS(_(" (NOT FOUND)")); | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7516 # else |
7 | 7517 MSG_PUTS(_(" (not supported)")); |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7518 # endif |
7 | 7519 } |
7520 if (sp->sn_text != NULL) | |
7521 { | |
7522 MSG_PUTS(" text="); | |
7523 msg_outtrans(sp->sn_text); | |
7524 } | |
7525 if (sp->sn_line_hl > 0) | |
7526 { | |
7527 MSG_PUTS(" linehl="); | |
7528 p = get_highlight_name(NULL, sp->sn_line_hl - 1); | |
7529 if (p == NULL) | |
7530 MSG_PUTS("NONE"); | |
7531 else | |
7532 msg_puts(p); | |
7533 } | |
7534 if (sp->sn_text_hl > 0) | |
7535 { | |
7536 MSG_PUTS(" texthl="); | |
7537 p = get_highlight_name(NULL, sp->sn_text_hl - 1); | |
7538 if (p == NULL) | |
7539 MSG_PUTS("NONE"); | |
7540 else | |
7541 msg_puts(p); | |
7542 } | |
7543 } | |
7544 | |
7545 /* | |
1828 | 7546 * Undefine a sign and free its memory. |
7547 */ | |
7548 static void | |
7549 sign_undefine(sp, sp_prev) | |
7550 sign_T *sp; | |
7551 sign_T *sp_prev; | |
7552 { | |
7553 vim_free(sp->sn_name); | |
7554 vim_free(sp->sn_icon); | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7555 # ifdef FEAT_SIGN_ICONS |
1828 | 7556 if (sp->sn_image != NULL) |
7557 { | |
7558 out_flush(); | |
7559 gui_mch_destroy_sign(sp->sn_image); | |
7560 } | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7561 # endif |
1828 | 7562 vim_free(sp->sn_text); |
7563 if (sp_prev == NULL) | |
7564 first_sign = sp->sn_next; | |
7565 else | |
7566 sp_prev->sn_next = sp->sn_next; | |
7567 vim_free(sp); | |
7568 } | |
7569 | |
7570 /* | |
7 | 7571 * Get highlighting attribute for sign "typenr". |
7572 * If "line" is TRUE: line highl, if FALSE: text highl. | |
7573 */ | |
7574 int | |
7575 sign_get_attr(typenr, line) | |
7576 int typenr; | |
7577 int line; | |
7578 { | |
7579 sign_T *sp; | |
7580 | |
7581 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
7582 if (sp->sn_typenr == typenr) | |
7583 { | |
7584 if (line) | |
7585 { | |
7586 if (sp->sn_line_hl > 0) | |
7587 return syn_id2attr(sp->sn_line_hl); | |
7588 } | |
7589 else | |
7590 { | |
7591 if (sp->sn_text_hl > 0) | |
7592 return syn_id2attr(sp->sn_text_hl); | |
7593 } | |
7594 break; | |
7595 } | |
7596 return 0; | |
7597 } | |
7598 | |
7599 /* | |
7600 * Get text mark for sign "typenr". | |
7601 * Returns NULL if there isn't one. | |
7602 */ | |
7603 char_u * | |
7604 sign_get_text(typenr) | |
7605 int typenr; | |
7606 { | |
7607 sign_T *sp; | |
7608 | |
7609 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
7610 if (sp->sn_typenr == typenr) | |
7611 return sp->sn_text; | |
7612 return NULL; | |
7613 } | |
7614 | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7615 # if defined(FEAT_SIGN_ICONS) || defined(PROTO) |
7 | 7616 void * |
7617 sign_get_image(typenr) | |
7618 int typenr; /* the attribute which may have a sign */ | |
7619 { | |
7620 sign_T *sp; | |
7621 | |
7622 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
7623 if (sp->sn_typenr == typenr) | |
7624 return sp->sn_image; | |
7625 return NULL; | |
7626 } | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7627 # endif |
7 | 7628 |
7629 /* | |
7630 * Get the name of a sign by its typenr. | |
7631 */ | |
7632 char_u * | |
7633 sign_typenr2name(typenr) | |
7634 int typenr; | |
7635 { | |
7636 sign_T *sp; | |
7637 | |
7638 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
7639 if (sp->sn_typenr == typenr) | |
7640 return sp->sn_name; | |
7641 return (char_u *)_("[Deleted]"); | |
7642 } | |
7643 | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7644 # if defined(EXITFREE) || defined(PROTO) |
1828 | 7645 /* |
7646 * Undefine/free all signs. | |
7647 */ | |
7648 void | |
7649 free_signs() | |
7650 { | |
7651 while (first_sign != NULL) | |
7652 sign_undefine(first_sign, NULL); | |
7653 } | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7654 # endif |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7655 |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7656 # if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
1868 | 7657 static enum |
7658 { | |
7659 EXP_SUBCMD, /* expand :sign sub-commands */ | |
7660 EXP_DEFINE, /* expand :sign define {name} args */ | |
7661 EXP_PLACE, /* expand :sign place {id} args */ | |
7662 EXP_UNPLACE, /* expand :sign unplace" */ | |
7663 EXP_SIGN_NAMES /* expand with name of placed signs */ | |
7664 } expand_what; | |
7665 | |
7666 /* | |
7667 * Function given to ExpandGeneric() to obtain the sign command | |
7668 * expansion. | |
7669 */ | |
7670 char_u * | |
7671 get_sign_name(xp, idx) | |
1876 | 7672 expand_T *xp UNUSED; |
1868 | 7673 int idx; |
7674 { | |
7675 sign_T *sp; | |
7676 int current_idx; | |
7677 | |
7678 switch (expand_what) | |
7679 { | |
7680 case EXP_SUBCMD: | |
7681 return (char_u *)cmds[idx]; | |
7682 case EXP_DEFINE: | |
7683 { | |
7684 char *define_arg[] = | |
7685 { | |
7686 "icon=", "linehl=", "text=", "texthl=", NULL | |
7687 }; | |
7688 return (char_u *)define_arg[idx]; | |
7689 } | |
7690 case EXP_PLACE: | |
7691 { | |
7692 char *place_arg[] = | |
7693 { | |
7694 "line=", "name=", "file=", "buffer=", NULL | |
7695 }; | |
7696 return (char_u *)place_arg[idx]; | |
7697 } | |
7698 case EXP_UNPLACE: | |
7699 { | |
7700 char *unplace_arg[] = { "file=", "buffer=", NULL }; | |
7701 return (char_u *)unplace_arg[idx]; | |
7702 } | |
7703 case EXP_SIGN_NAMES: | |
7704 /* Complete with name of signs already defined */ | |
7705 current_idx = 0; | |
7706 for (sp = first_sign; sp != NULL; sp = sp->sn_next) | |
7707 if (current_idx++ == idx) | |
7708 return sp->sn_name; | |
7709 return NULL; | |
7710 default: | |
7711 return NULL; | |
7712 } | |
7713 } | |
7714 | |
7715 /* | |
7716 * Handle command line completion for :sign command. | |
7717 */ | |
7718 void | |
7719 set_context_in_sign_cmd(xp, arg) | |
7720 expand_T *xp; | |
7721 char_u *arg; | |
7722 { | |
7723 char_u *p; | |
7724 char_u *end_subcmd; | |
7725 char_u *last; | |
7726 int cmd_idx; | |
7727 char_u *begin_subcmd_args; | |
7728 | |
7729 /* Default: expand subcommands. */ | |
7730 xp->xp_context = EXPAND_SIGN; | |
7731 expand_what = EXP_SUBCMD; | |
7732 xp->xp_pattern = arg; | |
7733 | |
7734 end_subcmd = skiptowhite(arg); | |
7735 if (*end_subcmd == NUL) | |
7736 /* expand subcmd name | |
7737 * :sign {subcmd}<CTRL-D>*/ | |
7738 return; | |
7739 | |
7740 cmd_idx = sign_cmd_idx(arg, end_subcmd); | |
7741 | |
7742 /* :sign {subcmd} {subcmd_args} | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
7743 * | |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
7744 * begin_subcmd_args */ |
1868 | 7745 begin_subcmd_args = skipwhite(end_subcmd); |
7746 p = skiptowhite(begin_subcmd_args); | |
7747 if (*p == NUL) | |
7748 { | |
7749 /* | |
7750 * Expand first argument of subcmd when possible. | |
7751 * For ":jump {id}" and ":unplace {id}", we could | |
7752 * possibly expand the ids of all signs already placed. | |
7753 */ | |
7754 xp->xp_pattern = begin_subcmd_args; | |
7755 switch (cmd_idx) | |
7756 { | |
7757 case SIGNCMD_LIST: | |
7758 case SIGNCMD_UNDEFINE: | |
7759 /* :sign list <CTRL-D> | |
7760 * :sign undefine <CTRL-D> */ | |
7761 expand_what = EXP_SIGN_NAMES; | |
7762 break; | |
7763 default: | |
7764 xp->xp_context = EXPAND_NOTHING; | |
7765 } | |
7766 return; | |
7767 } | |
7768 | |
7769 /* expand last argument of subcmd */ | |
7770 | |
7771 /* :sign define {name} {args}... | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
7772 * | |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
7773 * p */ |
1868 | 7774 |
7775 /* Loop until reaching last argument. */ | |
7776 do | |
7777 { | |
7778 p = skipwhite(p); | |
7779 last = p; | |
7780 p = skiptowhite(p); | |
7781 } while (*p != NUL); | |
7782 | |
7783 p = vim_strchr(last, '='); | |
7784 | |
7785 /* :sign define {name} {args}... {last}= | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
7786 * | | |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2290
diff
changeset
|
7787 * last p */ |
1868 | 7788 if (p == NUL) |
7789 { | |
7790 /* Expand last argument name (before equal sign). */ | |
7791 xp->xp_pattern = last; | |
7792 switch (cmd_idx) | |
7793 { | |
7794 case SIGNCMD_DEFINE: | |
7795 expand_what = EXP_DEFINE; | |
7796 break; | |
7797 case SIGNCMD_PLACE: | |
7798 expand_what = EXP_PLACE; | |
7799 break; | |
7800 case SIGNCMD_JUMP: | |
7801 case SIGNCMD_UNPLACE: | |
7802 expand_what = EXP_UNPLACE; | |
7803 break; | |
7804 default: | |
7805 xp->xp_context = EXPAND_NOTHING; | |
7806 } | |
7807 } | |
7808 else | |
7809 { | |
7810 /* Expand last argument value (after equal sign). */ | |
7811 xp->xp_pattern = p + 1; | |
7812 switch (cmd_idx) | |
7813 { | |
7814 case SIGNCMD_DEFINE: | |
7815 if (STRNCMP(last, "texthl", p - last) == 0 || | |
7816 STRNCMP(last, "linehl", p - last) == 0) | |
7817 xp->xp_context = EXPAND_HIGHLIGHT; | |
7818 else if (STRNCMP(last, "icon", p - last) == 0) | |
7819 xp->xp_context = EXPAND_FILES; | |
7820 else | |
7821 xp->xp_context = EXPAND_NOTHING; | |
7822 break; | |
7823 case SIGNCMD_PLACE: | |
7824 if (STRNCMP(last, "name", p - last) == 0) | |
7825 expand_what = EXP_SIGN_NAMES; | |
7826 else | |
7827 xp->xp_context = EXPAND_NOTHING; | |
7828 break; | |
7829 default: | |
7830 xp->xp_context = EXPAND_NOTHING; | |
7831 } | |
7832 } | |
7833 } | |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7834 # endif |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7835 #endif |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7836 |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7837 /* |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7838 * Make the user happy. |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7839 */ |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7840 void |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7841 ex_smile(eap) |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7842 exarg_T *eap UNUSED; |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7843 { |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7844 static char *code = "\34 \4o\14$\4ox\30 \2o\30$\1ox\25 \2o\36$\1o\11 \1o\1$\3 \2$\1 \1o\1$x\5 \1o\1 \1$\1 \2o\10 \1o\44$\1o\7 \2$\1 \2$\1 \2$\1o\1$x\2 \2o\1 \1$\1 \1$\1 \1\"\1$\6 \1o\11$\4 \15$\4 \11$\1o\7 \3$\1o\2$\1o\1$x\2 \1\"\6$\1o\1$\5 \1o\11$\6 \13$\6 \12$\1o\4 \10$x\4 \7$\4 \13$\6 \13$\6 \27$x\4 \27$\4 \15$\4 \16$\2 \3\"\3$x\5 \1\"\3$\4\"\61$\5 \1\"\3$x\6 \3$\3 \1o\62$\5 \1\"\3$\1ox\5 \1o\2$\1\"\3 \63$\7 \3$\1ox\5 \3$\4 \55$\1\"\1 \1\"\6$\5o\4$\1ox\4 \1o\3$\4o\5$\2 \45$\3 \1o\21$x\4 \10$\1\"\4$\3 \42$\5 \4$\10\"x\3 \4\"\7 \4$\4 \1\"\34$\1\"\6 \1o\3$x\16 \1\"\3$\1o\5 \3\"\22$\1\"\2$\1\"\11 \3$x\20 \3$\1o\12 \1\"\2$\2\"\6$\4\"\13 \1o\3$x\21 \4$\1o\40 \1o\3$\1\"x\22 \1\"\4$\1o\6 \1o\6$\1o\1\"\4$\1o\10 \1o\4$x\24 \1\"\5$\2o\5 \2\"\4$\1o\5$\1o\3 \1o\4$\2\"x\27 \2\"\5$\4o\2 \1\"\3$\1o\11$\3\"x\32 \2\"\7$\2o\1 \12$x\42 \4\"\13$x\46 \14$x\47 \12$\1\"x\50 \1\"\3$\4\"x"; |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7845 char *p; |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7846 int n; |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7847 |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7848 msg_start(); |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7849 msg_putchar('\n'); |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7850 for (p = code; *p != NUL; ++p) |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7851 if (*p == 'x') |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7852 msg_putchar('\n'); |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7853 else |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7854 for (n = *p++; n > 0; --n) |
7412
bc5de65e499a
commit https://github.com/vim/vim/commit/2d820808cda15b3ad9fe674393d1f1e997453d9e
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7855 if (*p == 'o' || *p == '$') |
bc5de65e499a
commit https://github.com/vim/vim/commit/2d820808cda15b3ad9fe674393d1f1e997453d9e
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7856 msg_putchar_attr(*p, hl_attr(HLF_L)); |
bc5de65e499a
commit https://github.com/vim/vim/commit/2d820808cda15b3ad9fe674393d1f1e997453d9e
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7857 else |
bc5de65e499a
commit https://github.com/vim/vim/commit/2d820808cda15b3ad9fe674393d1f1e997453d9e
Christian Brabandt <cb@256bit.org>
parents:
7410
diff
changeset
|
7858 msg_putchar(*p); |
7402
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7859 msg_clr_eos(); |
2c63e9ecf29d
commit https://github.com/vim/vim/commit/86e179dbe75010e9545e1a2fcc92a15d57bf27fd
Christian Brabandt <cb@256bit.org>
parents:
7340
diff
changeset
|
7860 } |
7 | 7861 |
7862 #if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO) | |
7863 /* | |
7864 * ":drop" | |
7865 * Opens the first argument in a window. When there are two or more arguments | |
7866 * the argument list is redefined. | |
7867 */ | |
7868 void | |
7869 ex_drop(eap) | |
7870 exarg_T *eap; | |
7871 { | |
7872 int split = FALSE; | |
7873 win_T *wp; | |
7874 buf_T *buf; | |
735 | 7875 # ifdef FEAT_WINDOWS |
7876 tabpage_T *tp; | |
7877 # endif | |
7 | 7878 |
7879 /* | |
7880 * Check if the first argument is already being edited in a window. If | |
7881 * so, jump to that window. | |
7882 * We would actually need to check all arguments, but that's complicated | |
7883 * and mostly only one file is dropped. | |
7884 * This also ignores wildcards, since it is very unlikely the user is | |
7885 * editing a file name with a wildcard character. | |
7886 */ | |
735 | 7887 set_arglist(eap->arg); |
7888 | |
1067 | 7889 /* |
7890 * Expanding wildcards may result in an empty argument list. E.g. when | |
7891 * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we | |
7892 * already did an error message for this. | |
7893 */ | |
7894 if (ARGCOUNT == 0) | |
7895 return; | |
7896 | |
7 | 7897 # ifdef FEAT_WINDOWS |
735 | 7898 if (cmdmod.tab) |
7899 { | |
7900 /* ":tab drop file ...": open a tab for each argument that isn't | |
7901 * edited in a window yet. It's like ":tab all" but without closing | |
7902 * windows or tabs. */ | |
7903 ex_all(eap); | |
7904 } | |
7905 else | |
7 | 7906 # endif |
735 | 7907 { |
7908 /* ":drop file ...": Edit the first argument. Jump to an existing | |
7909 * window if possible, edit in current window if the current buffer | |
7910 * can be abandoned, otherwise open a new window. */ | |
7911 buf = buflist_findnr(ARGLIST[0].ae_fnum); | |
7912 | |
7913 FOR_ALL_TAB_WINDOWS(tp, wp) | |
7914 { | |
7915 if (wp->w_buffer == buf) | |
7 | 7916 { |
735 | 7917 # ifdef FEAT_WINDOWS |
825 | 7918 goto_tabpage_win(tp, wp); |
735 | 7919 # endif |
7 | 7920 curwin->w_arg_idx = 0; |
7921 return; | |
7922 } | |
7923 } | |
735 | 7924 |
7925 /* | |
7926 * Check whether the current buffer is changed. If so, we will need | |
7927 * to split the current window or data could be lost. | |
7928 * Skip the check if the 'hidden' option is set, as in this case the | |
7929 * buffer won't be lost. | |
7930 */ | |
7931 if (!P_HID(curbuf)) | |
7932 { | |
7 | 7933 # ifdef FEAT_WINDOWS |
735 | 7934 ++emsg_off; |
7 | 7935 # endif |
5464 | 7936 split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD); |
7 | 7937 # ifdef FEAT_WINDOWS |
735 | 7938 --emsg_off; |
7 | 7939 # else |
735 | 7940 if (split) |
7941 return; | |
7 | 7942 # endif |
735 | 7943 } |
7944 | |
7945 /* Fake a ":sfirst" or ":first" command edit the first argument. */ | |
7946 if (split) | |
7947 { | |
7948 eap->cmdidx = CMD_sfirst; | |
7949 eap->cmd[0] = 's'; | |
7950 } | |
7951 else | |
7952 eap->cmdidx = CMD_first; | |
7953 ex_rewind(eap); | |
7954 } | |
7 | 7955 } |
7956 #endif |