Mercurial > vim
annotate src/fold.c @ 11140:6b26e044b6f5 v8.0.0457
patch 8.0.0457: using :move messes up manual folds
commit https://github.com/vim/vim/commit/88d298aed8682eac872ebfe40df3112a6acd83e8
Author: Bram Moolenaar <Bram@vim.org>
Date: Tue Mar 14 21:53:58 2017 +0100
patch 8.0.0457: using :move messes up manual folds
Problem: Using :move messes up manual folds.
Solution: Split adjusting marks and folds. Add foldMoveRange(). (neovim
patch #6221)
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Tue, 14 Mar 2017 22:00:05 +0100 |
parents | 8d9ecf09183a |
children | 87779062d706 |
rev | line source |
---|---|
10042
4aead6a9b7a9
commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents:
9754
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4 noet: |
7 | 2 * vim600:fdm=marker fdl=1 fdc=3: |
3 * | |
4 * VIM - Vi IMproved by Bram Moolenaar | |
5 * | |
6 * Do ":help uganda" in Vim to read copying and usage conditions. | |
7 * Do ":help credits" in Vim to see a list of people who contributed. | |
8 * See README.txt for an overview of the Vim source code. | |
9 */ | |
10 | |
11 /* | |
12 * fold.c: code for folding | |
13 */ | |
14 | |
15 #include "vim.h" | |
16 | |
17 #if defined(FEAT_FOLDING) || defined(PROTO) | |
18 | |
19 /* local declarations. {{{1 */ | |
20 /* typedef fold_T {{{2 */ | |
21 /* | |
22 * The toplevel folds for each window are stored in the w_folds growarray. | |
23 * Each toplevel fold can contain an array of second level folds in the | |
24 * fd_nested growarray. | |
25 * The info stored in both growarrays is the same: An array of fold_T. | |
26 */ | |
27 typedef struct | |
28 { | |
29 linenr_T fd_top; /* first line of fold; for nested fold | |
30 * relative to parent */ | |
31 linenr_T fd_len; /* number of lines in the fold */ | |
32 garray_T fd_nested; /* array of nested folds */ | |
33 char fd_flags; /* see below */ | |
34 char fd_small; /* TRUE, FALSE or MAYBE: fold smaller than | |
35 'foldminlines'; MAYBE applies to nested | |
36 folds too */ | |
37 } fold_T; | |
38 | |
39 #define FD_OPEN 0 /* fold is open (nested ones can be closed) */ | |
40 #define FD_CLOSED 1 /* fold is closed */ | |
41 #define FD_LEVEL 2 /* depends on 'foldlevel' (nested folds too) */ | |
42 | |
43 #define MAX_LEVEL 20 /* maximum fold depth */ | |
44 | |
45 /* static functions {{{2 */ | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
46 static void newFoldLevelWin(win_T *wp); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
47 static int checkCloseRec(garray_T *gap, linenr_T lnum, int level); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
48 static int foldFind(garray_T *gap, linenr_T lnum, fold_T **fpp); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
49 static int foldLevelWin(win_T *wp, linenr_T lnum); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
50 static void checkupdate(win_T *wp); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
51 static void setFoldRepeat(linenr_T lnum, long count, int do_open); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
52 static linenr_T setManualFold(linenr_T lnum, int opening, int recurse, int *donep); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
53 static linenr_T setManualFoldWin(win_T *wp, linenr_T lnum, int opening, int recurse, int *donep); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
54 static void foldOpenNested(fold_T *fpr); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
55 static void deleteFoldEntry(garray_T *gap, int idx, int recursive); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
56 static void foldMarkAdjustRecurse(garray_T *gap, linenr_T line1, linenr_T line2, long amount, long amount_after); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
57 static int getDeepestNestingRecurse(garray_T *gap); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
58 static int check_closed(win_T *win, fold_T *fp, int *use_levelp, int level, int *maybe_smallp, linenr_T lnum_off); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
59 static void checkSmall(win_T *wp, fold_T *fp, linenr_T lnum_off); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
60 static void setSmallMaybe(garray_T *gap); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
61 static void foldCreateMarkers(linenr_T start, linenr_T end); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
62 static void foldAddMarker(linenr_T lnum, char_u *marker, int markerlen); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
63 static void deleteFoldMarkers(fold_T *fp, int recursive, linenr_T lnum_off); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
64 static void foldDelMarker(linenr_T lnum, char_u *marker, int markerlen); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
65 static void foldUpdateIEMS(win_T *wp, linenr_T top, linenr_T bot); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
66 static void parseMarker(win_T *wp); |
11140
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
67 static void foldMoveRange_int(garray_T *gap, linenr_T line1, linenr_T line2, linenr_T dest); |
7 | 68 |
69 static char *e_nofold = N_("E490: No fold found"); | |
70 | |
71 /* | |
72 * While updating the folds lines between invalid_top and invalid_bot have an | |
73 * undefined fold level. Only used for the window currently being updated. | |
74 */ | |
75 static linenr_T invalid_top = (linenr_T)0; | |
76 static linenr_T invalid_bot = (linenr_T)0; | |
77 | |
78 /* | |
79 * When using 'foldexpr' we sometimes get the level of the next line, which | |
80 * calls foldlevel() to get the level of the current line, which hasn't been | |
81 * stored yet. To get around this chicken-egg problem the level of the | |
82 * previous line is stored here when available. prev_lnum is zero when the | |
83 * level is not available. | |
84 */ | |
85 static linenr_T prev_lnum = 0; | |
86 static int prev_lnum_lvl = -1; | |
87 | |
88 /* Flags used for "done" argument of setManualFold. */ | |
89 #define DONE_NOTHING 0 | |
90 #define DONE_ACTION 1 /* did close or open a fold */ | |
91 #define DONE_FOLD 2 /* did find a fold */ | |
92 | |
93 static int foldstartmarkerlen; | |
94 static char_u *foldendmarker; | |
95 static int foldendmarkerlen; | |
96 | |
97 /* Exported folding functions. {{{1 */ | |
98 /* copyFoldingState() {{{2 */ | |
99 #if defined(FEAT_WINDOWS) || defined(PROTO) | |
100 /* | |
101 * Copy that folding state from window "wp_from" to window "wp_to". | |
102 */ | |
103 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
104 copyFoldingState(win_T *wp_from, win_T *wp_to) |
7 | 105 { |
106 wp_to->w_fold_manual = wp_from->w_fold_manual; | |
107 wp_to->w_foldinvalid = wp_from->w_foldinvalid; | |
108 cloneFoldGrowArray(&wp_from->w_folds, &wp_to->w_folds); | |
109 } | |
110 #endif | |
111 | |
112 /* hasAnyFolding() {{{2 */ | |
113 /* | |
114 * Return TRUE if there may be folded lines in the current window. | |
115 */ | |
116 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
117 hasAnyFolding(win_T *win) |
7 | 118 { |
119 /* very simple now, but can become more complex later */ | |
120 return (win->w_p_fen | |
121 && (!foldmethodIsManual(win) || win->w_folds.ga_len > 0)); | |
122 } | |
123 | |
124 /* hasFolding() {{{2 */ | |
125 /* | |
126 * Return TRUE if line "lnum" in the current window is part of a closed | |
127 * fold. | |
128 * When returning TRUE, *firstp and *lastp are set to the first and last | |
129 * lnum of the sequence of folded lines (skipped when NULL). | |
130 */ | |
131 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
132 hasFolding(linenr_T lnum, linenr_T *firstp, linenr_T *lastp) |
7 | 133 { |
134 return hasFoldingWin(curwin, lnum, firstp, lastp, TRUE, NULL); | |
135 } | |
136 | |
137 /* hasFoldingWin() {{{2 */ | |
138 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
139 hasFoldingWin( |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
140 win_T *win, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
141 linenr_T lnum, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
142 linenr_T *firstp, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
143 linenr_T *lastp, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
144 int cache, /* when TRUE: use cached values of window */ |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
145 foldinfo_T *infop) /* where to store fold info */ |
7 | 146 { |
147 int had_folded = FALSE; | |
148 linenr_T first = 0; | |
149 linenr_T last = 0; | |
150 linenr_T lnum_rel = lnum; | |
151 int x; | |
152 fold_T *fp; | |
153 int level = 0; | |
154 int use_level = FALSE; | |
155 int maybe_small = FALSE; | |
156 garray_T *gap; | |
9252
c25898cc99c1
commit https://github.com/vim/vim/commit/945ec093cd4ddefab930239990564b12eb232153
Christian Brabandt <cb@256bit.org>
parents:
8891
diff
changeset
|
157 int low_level = 0; |
7 | 158 |
159 checkupdate(win); | |
160 /* | |
161 * Return quickly when there is no folding at all in this window. | |
162 */ | |
163 if (!hasAnyFolding(win)) | |
164 { | |
165 if (infop != NULL) | |
166 infop->fi_level = 0; | |
167 return FALSE; | |
168 } | |
169 | |
170 if (cache) | |
171 { | |
172 /* | |
173 * First look in cached info for displayed lines. This is probably | |
174 * the fastest, but it can only be used if the entry is still valid. | |
175 */ | |
176 x = find_wl_entry(win, lnum); | |
177 if (x >= 0) | |
178 { | |
179 first = win->w_lines[x].wl_lnum; | |
180 last = win->w_lines[x].wl_lastlnum; | |
181 had_folded = win->w_lines[x].wl_folded; | |
182 } | |
183 } | |
184 | |
185 if (first == 0) | |
186 { | |
187 /* | |
188 * Recursively search for a fold that contains "lnum". | |
189 */ | |
190 gap = &win->w_folds; | |
191 for (;;) | |
192 { | |
193 if (!foldFind(gap, lnum_rel, &fp)) | |
194 break; | |
195 | |
196 /* Remember lowest level of fold that starts in "lnum". */ | |
197 if (lnum_rel == fp->fd_top && low_level == 0) | |
198 low_level = level + 1; | |
199 | |
200 first += fp->fd_top; | |
201 last += fp->fd_top; | |
202 | |
203 /* is this fold closed? */ | |
204 had_folded = check_closed(win, fp, &use_level, level, | |
205 &maybe_small, lnum - lnum_rel); | |
206 if (had_folded) | |
207 { | |
208 /* Fold closed: Set last and quit loop. */ | |
209 last += fp->fd_len - 1; | |
210 break; | |
211 } | |
212 | |
213 /* Fold found, but it's open: Check nested folds. Line number is | |
214 * relative to containing fold. */ | |
215 gap = &fp->fd_nested; | |
216 lnum_rel -= fp->fd_top; | |
217 ++level; | |
218 } | |
219 } | |
220 | |
221 if (!had_folded) | |
222 { | |
223 if (infop != NULL) | |
224 { | |
225 infop->fi_level = level; | |
226 infop->fi_lnum = lnum - lnum_rel; | |
227 infop->fi_low_level = low_level == 0 ? level : low_level; | |
228 } | |
229 return FALSE; | |
230 } | |
231 | |
6753 | 232 if (last > win->w_buffer->b_ml.ml_line_count) |
233 last = win->w_buffer->b_ml.ml_line_count; | |
7 | 234 if (lastp != NULL) |
235 *lastp = last; | |
236 if (firstp != NULL) | |
237 *firstp = first; | |
238 if (infop != NULL) | |
239 { | |
240 infop->fi_level = level + 1; | |
241 infop->fi_lnum = first; | |
242 infop->fi_low_level = low_level == 0 ? level + 1 : low_level; | |
243 } | |
244 return TRUE; | |
245 } | |
246 | |
247 /* foldLevel() {{{2 */ | |
248 /* | |
249 * Return fold level at line number "lnum" in the current window. | |
250 */ | |
251 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
252 foldLevel(linenr_T lnum) |
7 | 253 { |
254 /* While updating the folds lines between invalid_top and invalid_bot have | |
255 * an undefined fold level. Otherwise update the folds first. */ | |
256 if (invalid_top == (linenr_T)0) | |
257 checkupdate(curwin); | |
258 else if (lnum == prev_lnum && prev_lnum_lvl >= 0) | |
259 return prev_lnum_lvl; | |
260 else if (lnum >= invalid_top && lnum <= invalid_bot) | |
261 return -1; | |
262 | |
263 /* Return quickly when there is no folding at all in this window. */ | |
264 if (!hasAnyFolding(curwin)) | |
265 return 0; | |
266 | |
267 return foldLevelWin(curwin, lnum); | |
268 } | |
269 | |
270 /* lineFolded() {{{2 */ | |
271 /* | |
272 * Low level function to check if a line is folded. Doesn't use any caching. | |
273 * Return TRUE if line is folded. | |
274 * Return FALSE if line is not folded. | |
275 * Return MAYBE if the line is folded when next to a folded line. | |
276 */ | |
277 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
278 lineFolded(win_T *win, linenr_T lnum) |
7 | 279 { |
280 return foldedCount(win, lnum, NULL) != 0; | |
281 } | |
282 | |
283 /* foldedCount() {{{2 */ | |
284 /* | |
285 * Count the number of lines that are folded at line number "lnum". | |
286 * Normally "lnum" is the first line of a possible fold, and the returned | |
287 * number is the number of lines in the fold. | |
288 * Doesn't use caching from the displayed window. | |
289 * Returns number of folded lines from "lnum", or 0 if line is not folded. | |
290 * When "infop" is not NULL, fills *infop with the fold level info. | |
291 */ | |
292 long | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
293 foldedCount(win_T *win, linenr_T lnum, foldinfo_T *infop) |
7 | 294 { |
295 linenr_T last; | |
296 | |
297 if (hasFoldingWin(win, lnum, NULL, &last, FALSE, infop)) | |
298 return (long)(last - lnum + 1); | |
299 return 0; | |
300 } | |
301 | |
302 /* foldmethodIsManual() {{{2 */ | |
303 /* | |
304 * Return TRUE if 'foldmethod' is "manual" | |
305 */ | |
306 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
307 foldmethodIsManual(win_T *wp) |
7 | 308 { |
309 return (wp->w_p_fdm[3] == 'u'); | |
310 } | |
311 | |
312 /* foldmethodIsIndent() {{{2 */ | |
313 /* | |
314 * Return TRUE if 'foldmethod' is "indent" | |
315 */ | |
316 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
317 foldmethodIsIndent(win_T *wp) |
7 | 318 { |
319 return (wp->w_p_fdm[0] == 'i'); | |
320 } | |
321 | |
322 /* foldmethodIsExpr() {{{2 */ | |
323 /* | |
324 * Return TRUE if 'foldmethod' is "expr" | |
325 */ | |
326 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
327 foldmethodIsExpr(win_T *wp) |
7 | 328 { |
329 return (wp->w_p_fdm[1] == 'x'); | |
330 } | |
331 | |
332 /* foldmethodIsMarker() {{{2 */ | |
333 /* | |
334 * Return TRUE if 'foldmethod' is "marker" | |
335 */ | |
336 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
337 foldmethodIsMarker(win_T *wp) |
7 | 338 { |
339 return (wp->w_p_fdm[2] == 'r'); | |
340 } | |
341 | |
342 /* foldmethodIsSyntax() {{{2 */ | |
343 /* | |
344 * Return TRUE if 'foldmethod' is "syntax" | |
345 */ | |
346 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
347 foldmethodIsSyntax(win_T *wp) |
7 | 348 { |
349 return (wp->w_p_fdm[0] == 's'); | |
350 } | |
351 | |
352 /* foldmethodIsDiff() {{{2 */ | |
353 /* | |
354 * Return TRUE if 'foldmethod' is "diff" | |
355 */ | |
356 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
357 foldmethodIsDiff(win_T *wp) |
7 | 358 { |
359 return (wp->w_p_fdm[0] == 'd'); | |
360 } | |
361 | |
362 /* closeFold() {{{2 */ | |
363 /* | |
364 * Close fold for current window at line "lnum". | |
365 * Repeat "count" times. | |
366 */ | |
367 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
368 closeFold(linenr_T lnum, long count) |
7 | 369 { |
370 setFoldRepeat(lnum, count, FALSE); | |
371 } | |
372 | |
373 /* closeFoldRecurse() {{{2 */ | |
374 /* | |
375 * Close fold for current window at line "lnum" recursively. | |
376 */ | |
377 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
378 closeFoldRecurse(linenr_T lnum) |
7 | 379 { |
380 (void)setManualFold(lnum, FALSE, TRUE, NULL); | |
381 } | |
382 | |
383 /* opFoldRange() {{{2 */ | |
384 /* | |
385 * Open or Close folds for current window in lines "first" to "last". | |
386 * Used for "zo", "zO", "zc" and "zC" in Visual mode. | |
387 */ | |
388 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
389 opFoldRange( |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
390 linenr_T first, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
391 linenr_T last, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
392 int opening, /* TRUE to open, FALSE to close */ |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
393 int recurse, /* TRUE to do it recursively */ |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
394 int had_visual) /* TRUE when Visual selection used */ |
7 | 395 { |
396 int done = DONE_NOTHING; /* avoid error messages */ | |
397 linenr_T lnum; | |
398 linenr_T lnum_next; | |
399 | |
400 for (lnum = first; lnum <= last; lnum = lnum_next + 1) | |
401 { | |
402 lnum_next = lnum; | |
403 /* Opening one level only: next fold to open is after the one going to | |
404 * be opened. */ | |
405 if (opening && !recurse) | |
406 (void)hasFolding(lnum, NULL, &lnum_next); | |
407 (void)setManualFold(lnum, opening, recurse, &done); | |
408 /* Closing one level only: next line to close a fold is after just | |
409 * closed fold. */ | |
410 if (!opening && !recurse) | |
411 (void)hasFolding(lnum, NULL, &lnum_next); | |
412 } | |
413 if (done == DONE_NOTHING) | |
414 EMSG(_(e_nofold)); | |
415 /* Force a redraw to remove the Visual highlighting. */ | |
416 if (had_visual) | |
417 redraw_curbuf_later(INVERTED); | |
418 } | |
419 | |
420 /* openFold() {{{2 */ | |
421 /* | |
422 * Open fold for current window at line "lnum". | |
423 * Repeat "count" times. | |
424 */ | |
425 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
426 openFold(linenr_T lnum, long count) |
7 | 427 { |
428 setFoldRepeat(lnum, count, TRUE); | |
429 } | |
430 | |
431 /* openFoldRecurse() {{{2 */ | |
432 /* | |
433 * Open fold for current window at line "lnum" recursively. | |
434 */ | |
435 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
436 openFoldRecurse(linenr_T lnum) |
7 | 437 { |
438 (void)setManualFold(lnum, TRUE, TRUE, NULL); | |
439 } | |
440 | |
441 /* foldOpenCursor() {{{2 */ | |
442 /* | |
443 * Open folds until the cursor line is not in a closed fold. | |
444 */ | |
445 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
446 foldOpenCursor(void) |
7 | 447 { |
448 int done; | |
449 | |
450 checkupdate(curwin); | |
451 if (hasAnyFolding(curwin)) | |
452 for (;;) | |
453 { | |
454 done = DONE_NOTHING; | |
455 (void)setManualFold(curwin->w_cursor.lnum, TRUE, FALSE, &done); | |
456 if (!(done & DONE_ACTION)) | |
457 break; | |
458 } | |
459 } | |
460 | |
461 /* newFoldLevel() {{{2 */ | |
462 /* | |
463 * Set new foldlevel for current window. | |
464 */ | |
465 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
466 newFoldLevel(void) |
7 | 467 { |
468 newFoldLevelWin(curwin); | |
469 | |
470 #ifdef FEAT_DIFF | |
471 if (foldmethodIsDiff(curwin) && curwin->w_p_scb) | |
472 { | |
473 win_T *wp; | |
474 | |
475 /* | |
476 * Set the same foldlevel in other windows in diff mode. | |
477 */ | |
478 FOR_ALL_WINDOWS(wp) | |
479 { | |
480 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb) | |
481 { | |
482 wp->w_p_fdl = curwin->w_p_fdl; | |
483 newFoldLevelWin(wp); | |
484 } | |
485 } | |
486 } | |
487 #endif | |
488 } | |
489 | |
490 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
491 newFoldLevelWin(win_T *wp) |
7 | 492 { |
493 fold_T *fp; | |
494 int i; | |
495 | |
496 checkupdate(wp); | |
497 if (wp->w_fold_manual) | |
498 { | |
499 /* Set all flags for the first level of folds to FD_LEVEL. Following | |
500 * manual open/close will then change the flags to FD_OPEN or | |
501 * FD_CLOSED for those folds that don't use 'foldlevel'. */ | |
502 fp = (fold_T *)wp->w_folds.ga_data; | |
503 for (i = 0; i < wp->w_folds.ga_len; ++i) | |
504 fp[i].fd_flags = FD_LEVEL; | |
505 wp->w_fold_manual = FALSE; | |
506 } | |
507 changed_window_setting_win(wp); | |
508 } | |
509 | |
510 /* foldCheckClose() {{{2 */ | |
511 /* | |
512 * Apply 'foldlevel' to all folds that don't contain the cursor. | |
513 */ | |
514 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
515 foldCheckClose(void) |
7 | 516 { |
517 if (*p_fcl != NUL) /* can only be "all" right now */ | |
518 { | |
519 checkupdate(curwin); | |
520 if (checkCloseRec(&curwin->w_folds, curwin->w_cursor.lnum, | |
521 (int)curwin->w_p_fdl)) | |
522 changed_window_setting(); | |
523 } | |
524 } | |
525 | |
526 /* checkCloseRec() {{{2 */ | |
527 static int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
528 checkCloseRec(garray_T *gap, linenr_T lnum, int level) |
7 | 529 { |
530 fold_T *fp; | |
531 int retval = FALSE; | |
532 int i; | |
533 | |
534 fp = (fold_T *)gap->ga_data; | |
535 for (i = 0; i < gap->ga_len; ++i) | |
536 { | |
537 /* Only manually opened folds may need to be closed. */ | |
538 if (fp[i].fd_flags == FD_OPEN) | |
539 { | |
540 if (level <= 0 && (lnum < fp[i].fd_top | |
541 || lnum >= fp[i].fd_top + fp[i].fd_len)) | |
542 { | |
543 fp[i].fd_flags = FD_LEVEL; | |
544 retval = TRUE; | |
545 } | |
546 else | |
547 retval |= checkCloseRec(&fp[i].fd_nested, lnum - fp[i].fd_top, | |
548 level - 1); | |
549 } | |
550 } | |
551 return retval; | |
552 } | |
553 | |
554 /* foldCreateAllowed() {{{2 */ | |
555 /* | |
556 * Return TRUE if it's allowed to manually create or delete a fold. | |
557 * Give an error message and return FALSE if not. | |
558 */ | |
559 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
560 foldManualAllowed(int create) |
7 | 561 { |
562 if (foldmethodIsManual(curwin) || foldmethodIsMarker(curwin)) | |
563 return TRUE; | |
564 if (create) | |
565 EMSG(_("E350: Cannot create fold with current 'foldmethod'")); | |
566 else | |
567 EMSG(_("E351: Cannot delete fold with current 'foldmethod'")); | |
568 return FALSE; | |
569 } | |
570 | |
571 /* foldCreate() {{{2 */ | |
572 /* | |
573 * Create a fold from line "start" to line "end" (inclusive) in the current | |
574 * window. | |
575 */ | |
576 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
577 foldCreate(linenr_T start, linenr_T end) |
7 | 578 { |
579 fold_T *fp; | |
580 garray_T *gap; | |
581 garray_T fold_ga; | |
582 int i, j; | |
583 int cont; | |
584 int use_level = FALSE; | |
585 int closed = FALSE; | |
586 int level = 0; | |
587 linenr_T start_rel = start; | |
588 linenr_T end_rel = end; | |
589 | |
590 if (start > end) | |
591 { | |
592 /* reverse the range */ | |
593 end = start_rel; | |
594 start = end_rel; | |
595 start_rel = start; | |
596 end_rel = end; | |
597 } | |
598 | |
599 /* When 'foldmethod' is "marker" add markers, which creates the folds. */ | |
600 if (foldmethodIsMarker(curwin)) | |
601 { | |
602 foldCreateMarkers(start, end); | |
603 return; | |
604 } | |
605 | |
606 checkupdate(curwin); | |
607 | |
608 /* Find the place to insert the new fold. */ | |
609 gap = &curwin->w_folds; | |
610 for (;;) | |
611 { | |
612 if (!foldFind(gap, start_rel, &fp)) | |
613 break; | |
614 if (fp->fd_top + fp->fd_len > end_rel) | |
615 { | |
616 /* New fold is completely inside this fold: Go one level deeper. */ | |
617 gap = &fp->fd_nested; | |
618 start_rel -= fp->fd_top; | |
619 end_rel -= fp->fd_top; | |
620 if (use_level || fp->fd_flags == FD_LEVEL) | |
621 { | |
622 use_level = TRUE; | |
623 if (level >= curwin->w_p_fdl) | |
624 closed = TRUE; | |
625 } | |
626 else if (fp->fd_flags == FD_CLOSED) | |
627 closed = TRUE; | |
628 ++level; | |
629 } | |
630 else | |
631 { | |
632 /* This fold and new fold overlap: Insert here and move some folds | |
633 * inside the new fold. */ | |
634 break; | |
635 } | |
636 } | |
637 | |
638 i = (int)(fp - (fold_T *)gap->ga_data); | |
639 if (ga_grow(gap, 1) == OK) | |
640 { | |
641 fp = (fold_T *)gap->ga_data + i; | |
642 ga_init2(&fold_ga, (int)sizeof(fold_T), 10); | |
643 | |
644 /* Count number of folds that will be contained in the new fold. */ | |
645 for (cont = 0; i + cont < gap->ga_len; ++cont) | |
646 if (fp[cont].fd_top > end_rel) | |
647 break; | |
648 if (cont > 0 && ga_grow(&fold_ga, cont) == OK) | |
649 { | |
650 /* If the first fold starts before the new fold, let the new fold | |
651 * start there. Otherwise the existing fold would change. */ | |
652 if (start_rel > fp->fd_top) | |
653 start_rel = fp->fd_top; | |
654 | |
655 /* When last contained fold isn't completely contained, adjust end | |
656 * of new fold. */ | |
657 if (end_rel < fp[cont - 1].fd_top + fp[cont - 1].fd_len - 1) | |
658 end_rel = fp[cont - 1].fd_top + fp[cont - 1].fd_len - 1; | |
659 /* Move contained folds to inside new fold. */ | |
660 mch_memmove(fold_ga.ga_data, fp, sizeof(fold_T) * cont); | |
661 fold_ga.ga_len += cont; | |
662 i += cont; | |
663 | |
664 /* Adjust line numbers in contained folds to be relative to the | |
665 * new fold. */ | |
666 for (j = 0; j < cont; ++j) | |
667 ((fold_T *)fold_ga.ga_data)[j].fd_top -= start_rel; | |
668 } | |
669 /* Move remaining entries to after the new fold. */ | |
670 if (i < gap->ga_len) | |
671 mch_memmove(fp + 1, (fold_T *)gap->ga_data + i, | |
672 sizeof(fold_T) * (gap->ga_len - i)); | |
673 gap->ga_len = gap->ga_len + 1 - cont; | |
674 | |
675 /* insert new fold */ | |
676 fp->fd_nested = fold_ga; | |
677 fp->fd_top = start_rel; | |
678 fp->fd_len = end_rel - start_rel + 1; | |
679 | |
680 /* We want the new fold to be closed. If it would remain open because | |
681 * of using 'foldlevel', need to adjust fd_flags of containing folds. | |
682 */ | |
683 if (use_level && !closed && level < curwin->w_p_fdl) | |
684 closeFold(start, 1L); | |
685 if (!use_level) | |
686 curwin->w_fold_manual = TRUE; | |
687 fp->fd_flags = FD_CLOSED; | |
688 fp->fd_small = MAYBE; | |
689 | |
690 /* redraw */ | |
691 changed_window_setting(); | |
692 } | |
693 } | |
694 | |
695 /* deleteFold() {{{2 */ | |
696 /* | |
697 * Delete a fold at line "start" in the current window. | |
698 * When "end" is not 0, delete all folds from "start" to "end". | |
699 * When "recursive" is TRUE delete recursively. | |
700 */ | |
701 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
702 deleteFold( |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
703 linenr_T start, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
704 linenr_T end, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
705 int recursive, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
706 int had_visual) /* TRUE when Visual selection used */ |
7 | 707 { |
708 garray_T *gap; | |
709 fold_T *fp; | |
710 garray_T *found_ga; | |
711 fold_T *found_fp = NULL; | |
712 linenr_T found_off = 0; | |
1780 | 713 int use_level; |
7 | 714 int maybe_small = FALSE; |
715 int level = 0; | |
716 linenr_T lnum = start; | |
717 linenr_T lnum_off; | |
718 int did_one = FALSE; | |
719 linenr_T first_lnum = MAXLNUM; | |
720 linenr_T last_lnum = 0; | |
721 | |
722 checkupdate(curwin); | |
723 | |
724 while (lnum <= end) | |
725 { | |
726 /* Find the deepest fold for "start". */ | |
727 gap = &curwin->w_folds; | |
728 found_ga = NULL; | |
729 lnum_off = 0; | |
1780 | 730 use_level = FALSE; |
7 | 731 for (;;) |
732 { | |
733 if (!foldFind(gap, lnum - lnum_off, &fp)) | |
734 break; | |
735 /* lnum is inside this fold, remember info */ | |
736 found_ga = gap; | |
737 found_fp = fp; | |
738 found_off = lnum_off; | |
739 | |
740 /* if "lnum" is folded, don't check nesting */ | |
741 if (check_closed(curwin, fp, &use_level, level, | |
742 &maybe_small, lnum_off)) | |
743 break; | |
744 | |
745 /* check nested folds */ | |
746 gap = &fp->fd_nested; | |
747 lnum_off += fp->fd_top; | |
748 ++level; | |
749 } | |
750 if (found_ga == NULL) | |
751 { | |
752 ++lnum; | |
753 } | |
754 else | |
755 { | |
756 lnum = found_fp->fd_top + found_fp->fd_len + found_off; | |
757 | |
758 if (foldmethodIsManual(curwin)) | |
759 deleteFoldEntry(found_ga, | |
760 (int)(found_fp - (fold_T *)found_ga->ga_data), recursive); | |
761 else | |
762 { | |
1780 | 763 if (first_lnum > found_fp->fd_top + found_off) |
764 first_lnum = found_fp->fd_top + found_off; | |
765 if (last_lnum < lnum) | |
7 | 766 last_lnum = lnum; |
1780 | 767 if (!did_one) |
768 parseMarker(curwin); | |
7 | 769 deleteFoldMarkers(found_fp, recursive, found_off); |
770 } | |
1780 | 771 did_one = TRUE; |
7 | 772 |
773 /* redraw window */ | |
774 changed_window_setting(); | |
775 } | |
776 } | |
777 if (!did_one) | |
778 { | |
779 EMSG(_(e_nofold)); | |
780 /* Force a redraw to remove the Visual highlighting. */ | |
781 if (had_visual) | |
782 redraw_curbuf_later(INVERTED); | |
783 } | |
1780 | 784 else |
785 /* Deleting markers may make cursor column invalid. */ | |
786 check_cursor_col(); | |
787 | |
7 | 788 if (last_lnum > 0) |
789 changed_lines(first_lnum, (colnr_T)0, last_lnum, 0L); | |
790 } | |
791 | |
792 /* clearFolding() {{{2 */ | |
793 /* | |
794 * Remove all folding for window "win". | |
795 */ | |
796 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
797 clearFolding(win_T *win) |
7 | 798 { |
799 deleteFoldRecurse(&win->w_folds); | |
800 win->w_foldinvalid = FALSE; | |
801 } | |
802 | |
803 /* foldUpdate() {{{2 */ | |
804 /* | |
805 * Update folds for changes in the buffer of a window. | |
806 * Note that inserted/deleted lines must have already been taken care of by | |
807 * calling foldMarkAdjust(). | |
808 * The changes in lines from top to bot (inclusive). | |
809 */ | |
810 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
811 foldUpdate(win_T *wp, linenr_T top, linenr_T bot) |
7 | 812 { |
813 fold_T *fp; | |
814 | |
8891
d7ba3f9b9ba6
commit https://github.com/vim/vim/commit/429fcfbf9a9275367fe9441a50a3dcd773497d84
Christian Brabandt <cb@256bit.org>
parents:
7821
diff
changeset
|
815 if (disable_fold_update > 0) |
d7ba3f9b9ba6
commit https://github.com/vim/vim/commit/429fcfbf9a9275367fe9441a50a3dcd773497d84
Christian Brabandt <cb@256bit.org>
parents:
7821
diff
changeset
|
816 return; |
d7ba3f9b9ba6
commit https://github.com/vim/vim/commit/429fcfbf9a9275367fe9441a50a3dcd773497d84
Christian Brabandt <cb@256bit.org>
parents:
7821
diff
changeset
|
817 |
7 | 818 /* Mark all folds from top to bot as maybe-small. */ |
6755 | 819 (void)foldFind(&wp->w_folds, top, &fp); |
820 while (fp < (fold_T *)wp->w_folds.ga_data + wp->w_folds.ga_len | |
7 | 821 && fp->fd_top < bot) |
822 { | |
823 fp->fd_small = MAYBE; | |
824 ++fp; | |
825 } | |
826 | |
827 if (foldmethodIsIndent(wp) | |
828 || foldmethodIsExpr(wp) | |
829 || foldmethodIsMarker(wp) | |
830 #ifdef FEAT_DIFF | |
831 || foldmethodIsDiff(wp) | |
832 #endif | |
833 || foldmethodIsSyntax(wp)) | |
1424 | 834 { |
835 int save_got_int = got_int; | |
836 | |
837 /* reset got_int here, otherwise it won't work */ | |
838 got_int = FALSE; | |
7 | 839 foldUpdateIEMS(wp, top, bot); |
1424 | 840 got_int |= save_got_int; |
841 } | |
7 | 842 } |
843 | |
844 /* foldUpdateAll() {{{2 */ | |
845 /* | |
846 * Update all lines in a window for folding. | |
847 * Used when a fold setting changes or after reloading the buffer. | |
848 * The actual updating is postponed until fold info is used, to avoid doing | |
849 * every time a setting is changed or a syntax item is added. | |
850 */ | |
851 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
852 foldUpdateAll(win_T *win) |
7 | 853 { |
854 win->w_foldinvalid = TRUE; | |
855 redraw_win_later(win, NOT_VALID); | |
856 } | |
857 | |
858 /* foldMoveTo() {{{2 */ | |
859 /* | |
860 * If "updown" is FALSE: Move to the start or end of the fold. | |
861 * If "updown" is TRUE: move to fold at the same level. | |
862 * If not moved return FAIL. | |
863 */ | |
864 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
865 foldMoveTo( |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
866 int updown, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
867 int dir, /* FORWARD or BACKWARD */ |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
868 long count) |
7 | 869 { |
870 long n; | |
871 int retval = FAIL; | |
872 linenr_T lnum_off; | |
873 linenr_T lnum_found; | |
874 linenr_T lnum; | |
875 int use_level; | |
876 int maybe_small; | |
877 garray_T *gap; | |
878 fold_T *fp; | |
879 int level; | |
880 int last; | |
881 | |
36 | 882 checkupdate(curwin); |
883 | |
7 | 884 /* Repeat "count" times. */ |
885 for (n = 0; n < count; ++n) | |
886 { | |
887 /* Find nested folds. Stop when a fold is closed. The deepest fold | |
888 * that moves the cursor is used. */ | |
889 lnum_off = 0; | |
890 gap = &curwin->w_folds; | |
891 use_level = FALSE; | |
892 maybe_small = FALSE; | |
893 lnum_found = curwin->w_cursor.lnum; | |
894 level = 0; | |
895 last = FALSE; | |
896 for (;;) | |
897 { | |
898 if (!foldFind(gap, curwin->w_cursor.lnum - lnum_off, &fp)) | |
899 { | |
900 if (!updown) | |
901 break; | |
902 | |
903 /* When moving up, consider a fold above the cursor; when | |
904 * moving down consider a fold below the cursor. */ | |
905 if (dir == FORWARD) | |
906 { | |
907 if (fp - (fold_T *)gap->ga_data >= gap->ga_len) | |
908 break; | |
909 --fp; | |
910 } | |
911 else | |
912 { | |
913 if (fp == (fold_T *)gap->ga_data) | |
914 break; | |
915 } | |
916 /* don't look for contained folds, they will always move | |
917 * the cursor too far. */ | |
918 last = TRUE; | |
919 } | |
920 | |
921 if (!last) | |
922 { | |
923 /* Check if this fold is closed. */ | |
924 if (check_closed(curwin, fp, &use_level, level, | |
925 &maybe_small, lnum_off)) | |
926 last = TRUE; | |
927 | |
928 /* "[z" and "]z" stop at closed fold */ | |
929 if (last && !updown) | |
930 break; | |
931 } | |
932 | |
933 if (updown) | |
934 { | |
935 if (dir == FORWARD) | |
936 { | |
937 /* to start of next fold if there is one */ | |
938 if (fp + 1 - (fold_T *)gap->ga_data < gap->ga_len) | |
939 { | |
940 lnum = fp[1].fd_top + lnum_off; | |
941 if (lnum > curwin->w_cursor.lnum) | |
942 lnum_found = lnum; | |
943 } | |
944 } | |
945 else | |
946 { | |
947 /* to end of previous fold if there is one */ | |
948 if (fp > (fold_T *)gap->ga_data) | |
949 { | |
950 lnum = fp[-1].fd_top + lnum_off + fp[-1].fd_len - 1; | |
951 if (lnum < curwin->w_cursor.lnum) | |
952 lnum_found = lnum; | |
953 } | |
954 } | |
955 } | |
956 else | |
957 { | |
958 /* Open fold found, set cursor to its start/end and then check | |
959 * nested folds. */ | |
960 if (dir == FORWARD) | |
961 { | |
962 lnum = fp->fd_top + lnum_off + fp->fd_len - 1; | |
963 if (lnum > curwin->w_cursor.lnum) | |
964 lnum_found = lnum; | |
965 } | |
966 else | |
967 { | |
968 lnum = fp->fd_top + lnum_off; | |
969 if (lnum < curwin->w_cursor.lnum) | |
970 lnum_found = lnum; | |
971 } | |
972 } | |
973 | |
974 if (last) | |
975 break; | |
976 | |
977 /* Check nested folds (if any). */ | |
978 gap = &fp->fd_nested; | |
979 lnum_off += fp->fd_top; | |
980 ++level; | |
981 } | |
982 if (lnum_found != curwin->w_cursor.lnum) | |
983 { | |
984 if (retval == FAIL) | |
985 setpcmark(); | |
986 curwin->w_cursor.lnum = lnum_found; | |
987 curwin->w_cursor.col = 0; | |
988 retval = OK; | |
989 } | |
990 else | |
991 break; | |
992 } | |
993 | |
994 return retval; | |
995 } | |
996 | |
997 /* foldInitWin() {{{2 */ | |
998 /* | |
999 * Init the fold info in a new window. | |
1000 */ | |
1001 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1002 foldInitWin(win_T *new_win) |
7 | 1003 { |
3263 | 1004 ga_init2(&new_win->w_folds, (int)sizeof(fold_T), 10); |
7 | 1005 } |
1006 | |
1007 /* find_wl_entry() {{{2 */ | |
1008 /* | |
1009 * Find an entry in the win->w_lines[] array for buffer line "lnum". | |
1010 * Only valid entries are considered (for entries where wl_valid is FALSE the | |
1011 * line number can be wrong). | |
1012 * Returns index of entry or -1 if not found. | |
1013 */ | |
1014 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1015 find_wl_entry(win_T *win, linenr_T lnum) |
7 | 1016 { |
1017 int i; | |
1018 | |
2114
5a97d0c03b59
updated for version 7.2.397
Bram Moolenaar <bram@zimbu.org>
parents:
2086
diff
changeset
|
1019 for (i = 0; i < win->w_lines_valid; ++i) |
5a97d0c03b59
updated for version 7.2.397
Bram Moolenaar <bram@zimbu.org>
parents:
2086
diff
changeset
|
1020 if (win->w_lines[i].wl_valid) |
5a97d0c03b59
updated for version 7.2.397
Bram Moolenaar <bram@zimbu.org>
parents:
2086
diff
changeset
|
1021 { |
5a97d0c03b59
updated for version 7.2.397
Bram Moolenaar <bram@zimbu.org>
parents:
2086
diff
changeset
|
1022 if (lnum < win->w_lines[i].wl_lnum) |
5a97d0c03b59
updated for version 7.2.397
Bram Moolenaar <bram@zimbu.org>
parents:
2086
diff
changeset
|
1023 return -1; |
5a97d0c03b59
updated for version 7.2.397
Bram Moolenaar <bram@zimbu.org>
parents:
2086
diff
changeset
|
1024 if (lnum <= win->w_lines[i].wl_lastlnum) |
5a97d0c03b59
updated for version 7.2.397
Bram Moolenaar <bram@zimbu.org>
parents:
2086
diff
changeset
|
1025 return i; |
5a97d0c03b59
updated for version 7.2.397
Bram Moolenaar <bram@zimbu.org>
parents:
2086
diff
changeset
|
1026 } |
7 | 1027 return -1; |
1028 } | |
1029 | |
1030 /* foldAdjustVisual() {{{2 */ | |
1031 /* | |
1032 * Adjust the Visual area to include any fold at the start or end completely. | |
1033 */ | |
1034 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1035 foldAdjustVisual(void) |
7 | 1036 { |
1037 pos_T *start, *end; | |
1038 char_u *ptr; | |
1039 | |
1040 if (!VIsual_active || !hasAnyFolding(curwin)) | |
1041 return; | |
1042 | |
11121
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11038
diff
changeset
|
1043 if (LTOREQ_POS(VIsual, curwin->w_cursor)) |
7 | 1044 { |
1045 start = &VIsual; | |
1046 end = &curwin->w_cursor; | |
1047 } | |
1048 else | |
1049 { | |
1050 start = &curwin->w_cursor; | |
1051 end = &VIsual; | |
1052 } | |
1053 if (hasFolding(start->lnum, &start->lnum, NULL)) | |
1054 start->col = 0; | |
1055 if (hasFolding(end->lnum, NULL, &end->lnum)) | |
1056 { | |
1057 ptr = ml_get(end->lnum); | |
1058 end->col = (colnr_T)STRLEN(ptr); | |
1059 if (end->col > 0 && *p_sel == 'o') | |
1060 --end->col; | |
1061 #ifdef FEAT_MBYTE | |
1062 /* prevent cursor from moving on the trail byte */ | |
1063 if (has_mbyte) | |
1064 mb_adjust_cursor(); | |
1065 #endif | |
1066 } | |
1067 } | |
1068 | |
1069 /* cursor_foldstart() {{{2 */ | |
1070 /* | |
1071 * Move the cursor to the first line of a closed fold. | |
1072 */ | |
1073 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1074 foldAdjustCursor(void) |
7 | 1075 { |
1076 (void)hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL); | |
1077 } | |
1078 | |
11140
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
1079 /* foldMoveRange() {{{2 */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
1080 void |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
1081 foldMoveRange(garray_T *gap, linenr_T line1, linenr_T line2, linenr_T dest) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
1082 { |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
1083 foldMoveRange_int(gap, line1, line2, dest); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
1084 } |
7 | 1085 /* Internal functions for "fold_T" {{{1 */ |
1086 /* cloneFoldGrowArray() {{{2 */ | |
1087 /* | |
1088 * Will "clone" (i.e deep copy) a garray_T of folds. | |
1089 * | |
1090 * Return FAIL if the operation cannot be completed, otherwise OK. | |
1091 */ | |
1092 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1093 cloneFoldGrowArray(garray_T *from, garray_T *to) |
7 | 1094 { |
1095 int i; | |
1096 fold_T *from_p; | |
1097 fold_T *to_p; | |
1098 | |
1099 ga_init2(to, from->ga_itemsize, from->ga_growsize); | |
1100 if (from->ga_len == 0 || ga_grow(to, from->ga_len) == FAIL) | |
1101 return; | |
1102 | |
1103 from_p = (fold_T *)from->ga_data; | |
1104 to_p = (fold_T *)to->ga_data; | |
1105 | |
1106 for (i = 0; i < from->ga_len; i++) | |
1107 { | |
1108 to_p->fd_top = from_p->fd_top; | |
1109 to_p->fd_len = from_p->fd_len; | |
1110 to_p->fd_flags = from_p->fd_flags; | |
1111 to_p->fd_small = from_p->fd_small; | |
1112 cloneFoldGrowArray(&from_p->fd_nested, &to_p->fd_nested); | |
1113 ++to->ga_len; | |
1114 ++from_p; | |
1115 ++to_p; | |
1116 } | |
1117 } | |
1118 | |
1119 /* foldFind() {{{2 */ | |
1120 /* | |
1121 * Search for line "lnum" in folds of growarray "gap". | |
1122 * Set *fpp to the fold struct for the fold that contains "lnum" or | |
1123 * the first fold below it (careful: it can be beyond the end of the array!). | |
1124 * Returns FALSE when there is no fold that contains "lnum". | |
1125 */ | |
1126 static int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1127 foldFind(garray_T *gap, linenr_T lnum, fold_T **fpp) |
7 | 1128 { |
1129 linenr_T low, high; | |
1130 fold_T *fp; | |
1131 int i; | |
1132 | |
1133 /* | |
1134 * Perform a binary search. | |
1135 * "low" is lowest index of possible match. | |
1136 * "high" is highest index of possible match. | |
1137 */ | |
1138 fp = (fold_T *)gap->ga_data; | |
1139 low = 0; | |
1140 high = gap->ga_len - 1; | |
1141 while (low <= high) | |
1142 { | |
1143 i = (low + high) / 2; | |
1144 if (fp[i].fd_top > lnum) | |
1145 /* fold below lnum, adjust high */ | |
1146 high = i - 1; | |
1147 else if (fp[i].fd_top + fp[i].fd_len <= lnum) | |
1148 /* fold above lnum, adjust low */ | |
1149 low = i + 1; | |
1150 else | |
1151 { | |
1152 /* lnum is inside this fold */ | |
1153 *fpp = fp + i; | |
1154 return TRUE; | |
1155 } | |
1156 } | |
1157 *fpp = fp + low; | |
1158 return FALSE; | |
1159 } | |
1160 | |
1161 /* foldLevelWin() {{{2 */ | |
1162 /* | |
1163 * Return fold level at line number "lnum" in window "wp". | |
1164 */ | |
1165 static int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1166 foldLevelWin(win_T *wp, linenr_T lnum) |
7 | 1167 { |
1168 fold_T *fp; | |
1169 linenr_T lnum_rel = lnum; | |
1170 int level = 0; | |
1171 garray_T *gap; | |
1172 | |
1173 /* Recursively search for a fold that contains "lnum". */ | |
1174 gap = &wp->w_folds; | |
1175 for (;;) | |
1176 { | |
1177 if (!foldFind(gap, lnum_rel, &fp)) | |
1178 break; | |
1179 /* Check nested folds. Line number is relative to containing fold. */ | |
1180 gap = &fp->fd_nested; | |
1181 lnum_rel -= fp->fd_top; | |
1182 ++level; | |
1183 } | |
1184 | |
1185 return level; | |
1186 } | |
1187 | |
1188 /* checkupdate() {{{2 */ | |
1189 /* | |
1190 * Check if the folds in window "wp" are invalid and update them if needed. | |
1191 */ | |
1192 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1193 checkupdate(win_T *wp) |
7 | 1194 { |
1195 if (wp->w_foldinvalid) | |
1196 { | |
1197 foldUpdate(wp, (linenr_T)1, (linenr_T)MAXLNUM); /* will update all */ | |
1198 wp->w_foldinvalid = FALSE; | |
1199 } | |
1200 } | |
1201 | |
1202 /* setFoldRepeat() {{{2 */ | |
1203 /* | |
1204 * Open or close fold for current window at line "lnum". | |
1205 * Repeat "count" times. | |
1206 */ | |
1207 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1208 setFoldRepeat(linenr_T lnum, long count, int do_open) |
7 | 1209 { |
1210 int done; | |
1211 long n; | |
1212 | |
1213 for (n = 0; n < count; ++n) | |
1214 { | |
1215 done = DONE_NOTHING; | |
1757 | 1216 (void)setManualFold(lnum, do_open, FALSE, &done); |
7 | 1217 if (!(done & DONE_ACTION)) |
1218 { | |
1219 /* Only give an error message when no fold could be opened. */ | |
1220 if (n == 0 && !(done & DONE_FOLD)) | |
1221 EMSG(_(e_nofold)); | |
1222 break; | |
1223 } | |
1224 } | |
1225 } | |
1226 | |
1227 /* setManualFold() {{{2 */ | |
1228 /* | |
1229 * Open or close the fold in the current window which contains "lnum". | |
1230 * Also does this for other windows in diff mode when needed. | |
1231 */ | |
1232 static linenr_T | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1233 setManualFold( |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1234 linenr_T lnum, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1235 int opening, /* TRUE when opening, FALSE when closing */ |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1236 int recurse, /* TRUE when closing/opening recursive */ |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1237 int *donep) |
7 | 1238 { |
1239 #ifdef FEAT_DIFF | |
1240 if (foldmethodIsDiff(curwin) && curwin->w_p_scb) | |
1241 { | |
1242 win_T *wp; | |
1243 linenr_T dlnum; | |
1244 | |
1245 /* | |
1246 * Do the same operation in other windows in diff mode. Calculate the | |
1247 * line number from the diffs. | |
1248 */ | |
1249 FOR_ALL_WINDOWS(wp) | |
1250 { | |
1251 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb) | |
1252 { | |
1253 dlnum = diff_lnum_win(curwin->w_cursor.lnum, wp); | |
1254 if (dlnum != 0) | |
1255 (void)setManualFoldWin(wp, dlnum, opening, recurse, NULL); | |
1256 } | |
1257 } | |
1258 } | |
1259 #endif | |
1260 | |
1261 return setManualFoldWin(curwin, lnum, opening, recurse, donep); | |
1262 } | |
1263 | |
1264 /* setManualFoldWin() {{{2 */ | |
1265 /* | |
1266 * Open or close the fold in window "wp" which contains "lnum". | |
1267 * "donep", when not NULL, points to flag that is set to DONE_FOLD when some | |
1268 * fold was found and to DONE_ACTION when some fold was opened or closed. | |
1269 * When "donep" is NULL give an error message when no fold was found for | |
1270 * "lnum", but only if "wp" is "curwin". | |
1271 * Return the line number of the next line that could be closed. | |
1272 * It's only valid when "opening" is TRUE! | |
1273 */ | |
1274 static linenr_T | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1275 setManualFoldWin( |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1276 win_T *wp, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1277 linenr_T lnum, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1278 int opening, /* TRUE when opening, FALSE when closing */ |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1279 int recurse, /* TRUE when closing/opening recursive */ |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1280 int *donep) |
7 | 1281 { |
1282 fold_T *fp; | |
1283 fold_T *fp2; | |
1284 fold_T *found = NULL; | |
1285 int j; | |
1286 int level = 0; | |
1287 int use_level = FALSE; | |
1288 int found_fold = FALSE; | |
1289 garray_T *gap; | |
1290 linenr_T next = MAXLNUM; | |
1291 linenr_T off = 0; | |
1292 int done = 0; | |
1293 | |
1294 checkupdate(wp); | |
1295 | |
1296 /* | |
1297 * Find the fold, open or close it. | |
1298 */ | |
1299 gap = &wp->w_folds; | |
1300 for (;;) | |
1301 { | |
1302 if (!foldFind(gap, lnum, &fp)) | |
1303 { | |
1304 /* If there is a following fold, continue there next time. */ | |
1305 if (fp < (fold_T *)gap->ga_data + gap->ga_len) | |
1306 next = fp->fd_top + off; | |
1307 break; | |
1308 } | |
1309 | |
1310 /* lnum is inside this fold */ | |
1311 found_fold = TRUE; | |
1312 | |
1313 /* If there is a following fold, continue there next time. */ | |
1314 if (fp + 1 < (fold_T *)gap->ga_data + gap->ga_len) | |
1315 next = fp[1].fd_top + off; | |
1316 | |
1317 /* Change from level-dependent folding to manual. */ | |
1318 if (use_level || fp->fd_flags == FD_LEVEL) | |
1319 { | |
1320 use_level = TRUE; | |
1321 if (level >= wp->w_p_fdl) | |
1322 fp->fd_flags = FD_CLOSED; | |
1323 else | |
1324 fp->fd_flags = FD_OPEN; | |
1325 fp2 = (fold_T *)fp->fd_nested.ga_data; | |
1326 for (j = 0; j < fp->fd_nested.ga_len; ++j) | |
1327 fp2[j].fd_flags = FD_LEVEL; | |
1328 } | |
1329 | |
1330 /* Simple case: Close recursively means closing the fold. */ | |
1331 if (!opening && recurse) | |
1332 { | |
1333 if (fp->fd_flags != FD_CLOSED) | |
1334 { | |
1335 done |= DONE_ACTION; | |
1336 fp->fd_flags = FD_CLOSED; | |
1337 } | |
1338 } | |
1339 else if (fp->fd_flags == FD_CLOSED) | |
1340 { | |
1341 /* When opening, open topmost closed fold. */ | |
1342 if (opening) | |
1343 { | |
1344 fp->fd_flags = FD_OPEN; | |
1345 done |= DONE_ACTION; | |
1346 if (recurse) | |
1347 foldOpenNested(fp); | |
1348 } | |
1349 break; | |
1350 } | |
1351 | |
1352 /* fold is open, check nested folds */ | |
1353 found = fp; | |
1354 gap = &fp->fd_nested; | |
1355 lnum -= fp->fd_top; | |
1356 off += fp->fd_top; | |
1357 ++level; | |
1358 } | |
1359 if (found_fold) | |
1360 { | |
1361 /* When closing and not recurse, close deepest open fold. */ | |
1362 if (!opening && found != NULL) | |
1363 { | |
1364 found->fd_flags = FD_CLOSED; | |
1365 done |= DONE_ACTION; | |
1366 } | |
1367 wp->w_fold_manual = TRUE; | |
1368 if (done & DONE_ACTION) | |
1369 changed_window_setting_win(wp); | |
1370 done |= DONE_FOLD; | |
1371 } | |
1372 else if (donep == NULL && wp == curwin) | |
1373 EMSG(_(e_nofold)); | |
1374 | |
1375 if (donep != NULL) | |
1376 *donep |= done; | |
1377 | |
1378 return next; | |
1379 } | |
1380 | |
1381 /* foldOpenNested() {{{2 */ | |
1382 /* | |
1383 * Open all nested folds in fold "fpr" recursively. | |
1384 */ | |
1385 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1386 foldOpenNested(fold_T *fpr) |
7 | 1387 { |
1388 int i; | |
1389 fold_T *fp; | |
1390 | |
1391 fp = (fold_T *)fpr->fd_nested.ga_data; | |
1392 for (i = 0; i < fpr->fd_nested.ga_len; ++i) | |
1393 { | |
1394 foldOpenNested(&fp[i]); | |
1395 fp[i].fd_flags = FD_OPEN; | |
1396 } | |
1397 } | |
1398 | |
1399 /* deleteFoldEntry() {{{2 */ | |
1400 /* | |
1401 * Delete fold "idx" from growarray "gap". | |
1402 * When "recursive" is TRUE also delete all the folds contained in it. | |
1403 * When "recursive" is FALSE contained folds are moved one level up. | |
1404 */ | |
1405 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1406 deleteFoldEntry(garray_T *gap, int idx, int recursive) |
7 | 1407 { |
1408 fold_T *fp; | |
1409 int i; | |
1410 long moved; | |
1411 fold_T *nfp; | |
1412 | |
1413 fp = (fold_T *)gap->ga_data + idx; | |
1414 if (recursive || fp->fd_nested.ga_len == 0) | |
1415 { | |
1416 /* recursively delete the contained folds */ | |
1417 deleteFoldRecurse(&fp->fd_nested); | |
1418 --gap->ga_len; | |
1419 if (idx < gap->ga_len) | |
1420 mch_memmove(fp, fp + 1, sizeof(fold_T) * (gap->ga_len - idx)); | |
1421 } | |
1422 else | |
1423 { | |
3028 | 1424 /* Move nested folds one level up, to overwrite the fold that is |
7 | 1425 * deleted. */ |
1426 moved = fp->fd_nested.ga_len; | |
1427 if (ga_grow(gap, (int)(moved - 1)) == OK) | |
1428 { | |
3028 | 1429 /* Get "fp" again, the array may have been reallocated. */ |
1430 fp = (fold_T *)gap->ga_data + idx; | |
1431 | |
7 | 1432 /* adjust fd_top and fd_flags for the moved folds */ |
1433 nfp = (fold_T *)fp->fd_nested.ga_data; | |
1434 for (i = 0; i < moved; ++i) | |
1435 { | |
1436 nfp[i].fd_top += fp->fd_top; | |
1437 if (fp->fd_flags == FD_LEVEL) | |
1438 nfp[i].fd_flags = FD_LEVEL; | |
1439 if (fp->fd_small == MAYBE) | |
1440 nfp[i].fd_small = MAYBE; | |
1441 } | |
1442 | |
1443 /* move the existing folds down to make room */ | |
3028 | 1444 if (idx + 1 < gap->ga_len) |
7 | 1445 mch_memmove(fp + moved, fp + 1, |
3028 | 1446 sizeof(fold_T) * (gap->ga_len - (idx + 1))); |
7 | 1447 /* move the contained folds one level up */ |
1448 mch_memmove(fp, nfp, (size_t)(sizeof(fold_T) * moved)); | |
1449 vim_free(nfp); | |
1450 gap->ga_len += moved - 1; | |
1451 } | |
1452 } | |
1453 } | |
1454 | |
1455 /* deleteFoldRecurse() {{{2 */ | |
1456 /* | |
1457 * Delete nested folds in a fold. | |
1458 */ | |
1459 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1460 deleteFoldRecurse(garray_T *gap) |
7 | 1461 { |
1462 int i; | |
1463 | |
1464 for (i = 0; i < gap->ga_len; ++i) | |
1465 deleteFoldRecurse(&(((fold_T *)(gap->ga_data))[i].fd_nested)); | |
1466 ga_clear(gap); | |
1467 } | |
1468 | |
1469 /* foldMarkAdjust() {{{2 */ | |
1470 /* | |
1471 * Update line numbers of folds for inserted/deleted lines. | |
1472 */ | |
1473 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1474 foldMarkAdjust( |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1475 win_T *wp, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1476 linenr_T line1, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1477 linenr_T line2, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1478 long amount, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1479 long amount_after) |
7 | 1480 { |
1481 /* If deleting marks from line1 to line2, but not deleting all those | |
1482 * lines, set line2 so that only deleted lines have their folds removed. */ | |
1483 if (amount == MAXLNUM && line2 >= line1 && line2 - line1 >= -amount_after) | |
1484 line2 = line1 - amount_after - 1; | |
1485 /* If appending a line in Insert mode, it should be included in the fold | |
1486 * just above the line. */ | |
1487 if ((State & INSERT) && amount == (linenr_T)1 && line2 == MAXLNUM) | |
1488 --line1; | |
1489 foldMarkAdjustRecurse(&wp->w_folds, line1, line2, amount, amount_after); | |
1490 } | |
1491 | |
1492 /* foldMarkAdjustRecurse() {{{2 */ | |
1493 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1494 foldMarkAdjustRecurse( |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1495 garray_T *gap, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1496 linenr_T line1, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1497 linenr_T line2, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1498 long amount, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1499 long amount_after) |
7 | 1500 { |
1501 fold_T *fp; | |
1502 int i; | |
1503 linenr_T last; | |
1504 linenr_T top; | |
1505 | |
1506 /* In Insert mode an inserted line at the top of a fold is considered part | |
1507 * of the fold, otherwise it isn't. */ | |
1508 if ((State & INSERT) && amount == (linenr_T)1 && line2 == MAXLNUM) | |
1509 top = line1 + 1; | |
1510 else | |
1511 top = line1; | |
1512 | |
1513 /* Find the fold containing or just below "line1". */ | |
1514 (void)foldFind(gap, line1, &fp); | |
1515 | |
1516 /* | |
1517 * Adjust all folds below "line1" that are affected. | |
1518 */ | |
1519 for (i = (int)(fp - (fold_T *)gap->ga_data); i < gap->ga_len; ++i, ++fp) | |
1520 { | |
1521 /* | |
1522 * Check for these situations: | |
1523 * 1 2 3 | |
1524 * 1 2 3 | |
1525 * line1 2 3 4 5 | |
1526 * 2 3 4 5 | |
1527 * 2 3 4 5 | |
1528 * line2 2 3 4 5 | |
1529 * 3 5 6 | |
1530 * 3 5 6 | |
1531 */ | |
1532 | |
1533 last = fp->fd_top + fp->fd_len - 1; /* last line of fold */ | |
1534 | |
1535 /* 1. fold completely above line1: nothing to do */ | |
1536 if (last < line1) | |
1537 continue; | |
1538 | |
1539 /* 6. fold below line2: only adjust for amount_after */ | |
1540 if (fp->fd_top > line2) | |
1541 { | |
1542 if (amount_after == 0) | |
1543 break; | |
1544 fp->fd_top += amount_after; | |
1545 } | |
1546 else | |
1547 { | |
1548 if (fp->fd_top >= top && last <= line2) | |
1549 { | |
1550 /* 4. fold completely contained in range */ | |
1551 if (amount == MAXLNUM) | |
1552 { | |
1553 /* Deleting lines: delete the fold completely */ | |
1554 deleteFoldEntry(gap, i, TRUE); | |
1555 --i; /* adjust index for deletion */ | |
1556 --fp; | |
1557 } | |
1558 else | |
1559 fp->fd_top += amount; | |
1560 } | |
1561 else | |
1562 { | |
1563 if (fp->fd_top < top) | |
1564 { | |
1964 | 1565 /* 2 or 3: need to correct nested folds too */ |
1566 foldMarkAdjustRecurse(&fp->fd_nested, line1 - fp->fd_top, | |
1567 line2 - fp->fd_top, amount, amount_after); | |
7 | 1568 if (last <= line2) |
1569 { | |
1570 /* 2. fold contains line1, line2 is below fold */ | |
1571 if (amount == MAXLNUM) | |
1572 fp->fd_len = line1 - fp->fd_top; | |
1573 else | |
1574 fp->fd_len += amount; | |
1575 } | |
1576 else | |
1577 { | |
1578 /* 3. fold contains line1 and line2 */ | |
1579 fp->fd_len += amount_after; | |
1580 } | |
1581 } | |
1582 else | |
1583 { | |
1964 | 1584 /* 5. fold is below line1 and contains line2; need to |
1585 * correct nested folds too */ | |
7 | 1586 if (amount == MAXLNUM) |
1587 { | |
10998
2645a98217fa
patch 8.0.0388: filtering lines changes folds
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1588 foldMarkAdjustRecurse(&fp->fd_nested, |
2645a98217fa
patch 8.0.0388: filtering lines changes folds
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1589 line1 - fp->fd_top, |
2645a98217fa
patch 8.0.0388: filtering lines changes folds
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1590 line2 - fp->fd_top, |
2645a98217fa
patch 8.0.0388: filtering lines changes folds
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1591 amount, |
2645a98217fa
patch 8.0.0388: filtering lines changes folds
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1592 amount_after + (fp->fd_top - top)); |
7 | 1593 fp->fd_len -= line2 - fp->fd_top + 1; |
1594 fp->fd_top = line1; | |
1595 } | |
1596 else | |
1597 { | |
10998
2645a98217fa
patch 8.0.0388: filtering lines changes folds
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1598 foldMarkAdjustRecurse(&fp->fd_nested, |
2645a98217fa
patch 8.0.0388: filtering lines changes folds
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1599 line1 - fp->fd_top, |
2645a98217fa
patch 8.0.0388: filtering lines changes folds
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1600 line2 - fp->fd_top, |
2645a98217fa
patch 8.0.0388: filtering lines changes folds
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1601 amount, |
2645a98217fa
patch 8.0.0388: filtering lines changes folds
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1602 amount_after - amount); |
7 | 1603 fp->fd_len += amount_after - amount; |
1604 fp->fd_top += amount; | |
1605 } | |
1606 } | |
1607 } | |
1608 } | |
1609 } | |
1610 } | |
1611 | |
1612 /* getDeepestNesting() {{{2 */ | |
1613 /* | |
1614 * Get the lowest 'foldlevel' value that makes the deepest nested fold in the | |
1615 * current window open. | |
1616 */ | |
1617 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1618 getDeepestNesting(void) |
7 | 1619 { |
1620 checkupdate(curwin); | |
1621 return getDeepestNestingRecurse(&curwin->w_folds); | |
1622 } | |
1623 | |
1624 static int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1625 getDeepestNestingRecurse(garray_T *gap) |
7 | 1626 { |
1627 int i; | |
1628 int level; | |
1629 int maxlevel = 0; | |
1630 fold_T *fp; | |
1631 | |
1632 fp = (fold_T *)gap->ga_data; | |
1633 for (i = 0; i < gap->ga_len; ++i) | |
1634 { | |
1635 level = getDeepestNestingRecurse(&fp[i].fd_nested) + 1; | |
1636 if (level > maxlevel) | |
1637 maxlevel = level; | |
1638 } | |
1639 | |
1640 return maxlevel; | |
1641 } | |
1642 | |
1643 /* check_closed() {{{2 */ | |
1644 /* | |
1645 * Check if a fold is closed and update the info needed to check nested folds. | |
1646 */ | |
1647 static int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1648 check_closed( |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1649 win_T *win, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1650 fold_T *fp, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1651 int *use_levelp, /* TRUE: outer fold had FD_LEVEL */ |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1652 int level, /* folding depth */ |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1653 int *maybe_smallp, /* TRUE: outer this had fd_small == MAYBE */ |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1654 linenr_T lnum_off) /* line number offset for fp->fd_top */ |
7 | 1655 { |
1656 int closed = FALSE; | |
1657 | |
1658 /* Check if this fold is closed. If the flag is FD_LEVEL this | |
1659 * fold and all folds it contains depend on 'foldlevel'. */ | |
1660 if (*use_levelp || fp->fd_flags == FD_LEVEL) | |
1661 { | |
1662 *use_levelp = TRUE; | |
1663 if (level >= win->w_p_fdl) | |
1664 closed = TRUE; | |
1665 } | |
1666 else if (fp->fd_flags == FD_CLOSED) | |
1667 closed = TRUE; | |
1668 | |
1669 /* Small fold isn't closed anyway. */ | |
1670 if (fp->fd_small == MAYBE) | |
1671 *maybe_smallp = TRUE; | |
1672 if (closed) | |
1673 { | |
1674 if (*maybe_smallp) | |
1675 fp->fd_small = MAYBE; | |
1676 checkSmall(win, fp, lnum_off); | |
1677 if (fp->fd_small == TRUE) | |
1678 closed = FALSE; | |
1679 } | |
1680 return closed; | |
1681 } | |
1682 | |
1683 /* checkSmall() {{{2 */ | |
1684 /* | |
1685 * Update fd_small field of fold "fp". | |
1686 */ | |
1687 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1688 checkSmall( |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1689 win_T *wp, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1690 fold_T *fp, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1691 linenr_T lnum_off) /* offset for fp->fd_top */ |
7 | 1692 { |
1693 int count; | |
1694 int n; | |
1695 | |
1696 if (fp->fd_small == MAYBE) | |
1697 { | |
1698 /* Mark any nested folds to maybe-small */ | |
1699 setSmallMaybe(&fp->fd_nested); | |
1700 | |
1701 if (fp->fd_len > curwin->w_p_fml) | |
1702 fp->fd_small = FALSE; | |
1703 else | |
1704 { | |
1705 count = 0; | |
1706 for (n = 0; n < fp->fd_len; ++n) | |
1707 { | |
1708 count += plines_win_nofold(wp, fp->fd_top + lnum_off + n); | |
1709 if (count > curwin->w_p_fml) | |
1710 { | |
1711 fp->fd_small = FALSE; | |
1712 return; | |
1713 } | |
1714 } | |
1715 fp->fd_small = TRUE; | |
1716 } | |
1717 } | |
1718 } | |
1719 | |
1720 /* setSmallMaybe() {{{2 */ | |
1721 /* | |
1722 * Set small flags in "gap" to MAYBE. | |
1723 */ | |
1724 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1725 setSmallMaybe(garray_T *gap) |
7 | 1726 { |
1727 int i; | |
1728 fold_T *fp; | |
1729 | |
1730 fp = (fold_T *)gap->ga_data; | |
1731 for (i = 0; i < gap->ga_len; ++i) | |
1732 fp[i].fd_small = MAYBE; | |
1733 } | |
1734 | |
1735 /* foldCreateMarkers() {{{2 */ | |
1736 /* | |
1737 * Create a fold from line "start" to line "end" (inclusive) in the current | |
1738 * window by adding markers. | |
1739 */ | |
1740 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1741 foldCreateMarkers(linenr_T start, linenr_T end) |
7 | 1742 { |
1743 if (!curbuf->b_p_ma) | |
1744 { | |
1745 EMSG(_(e_modifiable)); | |
1746 return; | |
1747 } | |
1748 parseMarker(curwin); | |
1749 | |
1750 foldAddMarker(start, curwin->w_p_fmr, foldstartmarkerlen); | |
1751 foldAddMarker(end, foldendmarker, foldendmarkerlen); | |
1752 | |
1753 /* Update both changes here, to avoid all folds after the start are | |
1754 * changed when the start marker is inserted and the end isn't. */ | |
1755 changed_lines(start, (colnr_T)0, end, 0L); | |
1756 } | |
1757 | |
1758 /* foldAddMarker() {{{2 */ | |
1759 /* | |
1760 * Add "marker[markerlen]" in 'commentstring' to line "lnum". | |
1761 */ | |
1762 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1763 foldAddMarker(linenr_T lnum, char_u *marker, int markerlen) |
7 | 1764 { |
1765 char_u *cms = curbuf->b_p_cms; | |
1766 char_u *line; | |
1767 int line_len; | |
1768 char_u *newline; | |
1769 char_u *p = (char_u *)strstr((char *)curbuf->b_p_cms, "%s"); | |
11131
8d9ecf09183a
patch 8.0.0453: adding fold marker creates new comment
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
1770 int line_is_comment = FALSE; |
7 | 1771 |
1772 /* Allocate a new line: old-line + 'cms'-start + marker + 'cms'-end */ | |
1773 line = ml_get(lnum); | |
1774 line_len = (int)STRLEN(line); | |
1775 | |
1776 if (u_save(lnum - 1, lnum + 1) == OK) | |
1777 { | |
11131
8d9ecf09183a
patch 8.0.0453: adding fold marker creates new comment
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
1778 #if defined(FEAT_COMMENTS) |
8d9ecf09183a
patch 8.0.0453: adding fold marker creates new comment
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
1779 /* Check if the line ends with an unclosed comment */ |
8d9ecf09183a
patch 8.0.0453: adding fold marker creates new comment
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
1780 (void)skip_comment(line, FALSE, FALSE, &line_is_comment); |
8d9ecf09183a
patch 8.0.0453: adding fold marker creates new comment
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
1781 #endif |
7 | 1782 newline = alloc((unsigned)(line_len + markerlen + STRLEN(cms) + 1)); |
1783 if (newline == NULL) | |
1784 return; | |
1785 STRCPY(newline, line); | |
11131
8d9ecf09183a
patch 8.0.0453: adding fold marker creates new comment
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
1786 /* Append the marker to the end of the line */ |
8d9ecf09183a
patch 8.0.0453: adding fold marker creates new comment
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
1787 if (p == NULL || line_is_comment) |
419 | 1788 vim_strncpy(newline + line_len, marker, markerlen); |
7 | 1789 else |
1790 { | |
1791 STRCPY(newline + line_len, cms); | |
1792 STRNCPY(newline + line_len + (p - cms), marker, markerlen); | |
1793 STRCPY(newline + line_len + (p - cms) + markerlen, p + 2); | |
1794 } | |
1795 | |
1796 ml_replace(lnum, newline, FALSE); | |
1797 } | |
1798 } | |
1799 | |
1800 /* deleteFoldMarkers() {{{2 */ | |
1801 /* | |
1802 * Delete the markers for a fold, causing it to be deleted. | |
1803 */ | |
1804 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1805 deleteFoldMarkers( |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1806 fold_T *fp, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1807 int recursive, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1808 linenr_T lnum_off) /* offset for fp->fd_top */ |
7 | 1809 { |
1810 int i; | |
1811 | |
1812 if (recursive) | |
1813 for (i = 0; i < fp->fd_nested.ga_len; ++i) | |
1814 deleteFoldMarkers((fold_T *)fp->fd_nested.ga_data + i, TRUE, | |
1815 lnum_off + fp->fd_top); | |
1816 foldDelMarker(fp->fd_top + lnum_off, curwin->w_p_fmr, foldstartmarkerlen); | |
1817 foldDelMarker(fp->fd_top + lnum_off + fp->fd_len - 1, | |
1818 foldendmarker, foldendmarkerlen); | |
1819 } | |
1820 | |
1821 /* foldDelMarker() {{{2 */ | |
1822 /* | |
1823 * Delete marker "marker[markerlen]" at the end of line "lnum". | |
1824 * Delete 'commentstring' if it matches. | |
1825 * If the marker is not found, there is no error message. Could a missing | |
1826 * close-marker. | |
1827 */ | |
1828 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1829 foldDelMarker(linenr_T lnum, char_u *marker, int markerlen) |
7 | 1830 { |
1831 char_u *line; | |
1832 char_u *newline; | |
1833 char_u *p; | |
1834 int len; | |
1835 char_u *cms = curbuf->b_p_cms; | |
1836 char_u *cms2; | |
1837 | |
1838 line = ml_get(lnum); | |
1839 for (p = line; *p != NUL; ++p) | |
1840 if (STRNCMP(p, marker, markerlen) == 0) | |
1841 { | |
1842 /* Found the marker, include a digit if it's there. */ | |
1843 len = markerlen; | |
1844 if (VIM_ISDIGIT(p[len])) | |
1845 ++len; | |
1846 if (*cms != NUL) | |
1847 { | |
1848 /* Also delete 'commentstring' if it matches. */ | |
1849 cms2 = (char_u *)strstr((char *)cms, "%s"); | |
1850 if (p - line >= cms2 - cms | |
1851 && STRNCMP(p - (cms2 - cms), cms, cms2 - cms) == 0 | |
1852 && STRNCMP(p + len, cms2 + 2, STRLEN(cms2 + 2)) == 0) | |
1853 { | |
1854 p -= cms2 - cms; | |
1855 len += (int)STRLEN(cms) - 2; | |
1856 } | |
1857 } | |
1858 if (u_save(lnum - 1, lnum + 1) == OK) | |
1859 { | |
1860 /* Make new line: text-before-marker + text-after-marker */ | |
1861 newline = alloc((unsigned)(STRLEN(line) - len + 1)); | |
1862 if (newline != NULL) | |
1863 { | |
1864 STRNCPY(newline, line, p - line); | |
1865 STRCPY(newline + (p - line), p + len); | |
1866 ml_replace(lnum, newline, FALSE); | |
1867 } | |
1868 } | |
1869 break; | |
1870 } | |
1871 } | |
1872 | |
29 | 1873 /* get_foldtext() {{{2 */ |
1874 /* | |
1875 * Return the text for a closed fold at line "lnum", with last line "lnume". | |
9754
a990e7ed260b
commit https://github.com/vim/vim/commit/ee695f787ade7fd88fc5f5497553d95c0c3645b5
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
1876 * When 'foldtext' isn't set puts the result in "buf[FOLD_TEXT_LEN]". |
a990e7ed260b
commit https://github.com/vim/vim/commit/ee695f787ade7fd88fc5f5497553d95c0c3645b5
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
1877 * Otherwise the result is in allocated memory. |
29 | 1878 */ |
1879 char_u * | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1880 get_foldtext( |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1881 win_T *wp, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1882 linenr_T lnum, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1883 linenr_T lnume, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1884 foldinfo_T *foldinfo, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1885 char_u *buf) |
29 | 1886 { |
1887 char_u *text = NULL; | |
4907
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1888 #ifdef FEAT_EVAL |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1889 /* an error occurred when evaluating 'fdt' setting */ |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1890 static int got_fdt_error = FALSE; |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1891 int save_did_emsg = did_emsg; |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1892 static win_T *last_wp = NULL; |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1893 static linenr_T last_lnum = 0; |
29 | 1894 |
4907
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1895 if (last_wp != wp || last_wp == NULL |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1896 || last_lnum > lnum || last_lnum == 0) |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1897 /* window changed, try evaluating foldtext setting once again */ |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1898 got_fdt_error = FALSE; |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1899 |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1900 if (!got_fdt_error) |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1901 /* a previous error should not abort evaluating 'foldexpr' */ |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1902 did_emsg = FALSE; |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1903 |
29 | 1904 if (*wp->w_p_fdt != NUL) |
1905 { | |
1981 | 1906 char_u dashes[MAX_LEVEL + 2]; |
29 | 1907 win_T *save_curwin; |
1908 int level; | |
1909 char_u *p; | |
1910 | |
1911 /* Set "v:foldstart" and "v:foldend". */ | |
1912 set_vim_var_nr(VV_FOLDSTART, lnum); | |
1913 set_vim_var_nr(VV_FOLDEND, lnume); | |
1914 | |
1915 /* Set "v:folddashes" to a string of "level" dashes. */ | |
1916 /* Set "v:foldlevel" to "level". */ | |
1917 level = foldinfo->fi_level; | |
1981 | 1918 if (level > (int)sizeof(dashes) - 1) |
1919 level = (int)sizeof(dashes) - 1; | |
29 | 1920 vim_memset(dashes, '-', (size_t)level); |
1921 dashes[level] = NUL; | |
1922 set_vim_var_string(VV_FOLDDASHES, dashes, -1); | |
1923 set_vim_var_nr(VV_FOLDLEVEL, (long)level); | |
4907
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1924 |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1925 /* skip evaluating foldtext on errors */ |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1926 if (!got_fdt_error) |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1927 { |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1928 save_curwin = curwin; |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1929 curwin = wp; |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1930 curbuf = wp->w_buffer; |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1931 |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1932 ++emsg_silent; /* handle exceptions, but don't display errors */ |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1933 text = eval_to_string_safe(wp->w_p_fdt, NULL, |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1934 was_set_insecurely((char_u *)"foldtext", OPT_LOCAL)); |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1935 --emsg_silent; |
29 | 1936 |
4907
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1937 if (text == NULL || did_emsg) |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1938 got_fdt_error = TRUE; |
29 | 1939 |
4907
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1940 curwin = save_curwin; |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1941 curbuf = curwin->w_buffer; |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1942 } |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1943 last_lnum = lnum; |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1944 last_wp = wp; |
29 | 1945 set_vim_var_string(VV_FOLDDASHES, NULL, -1); |
1946 | |
4907
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1947 if (!did_emsg && save_did_emsg) |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1948 did_emsg = save_did_emsg; |
be2973afe770
updated for version 7.3.1199
Bram Moolenaar <bram@vim.org>
parents:
3766
diff
changeset
|
1949 |
29 | 1950 if (text != NULL) |
1951 { | |
1952 /* Replace unprintable characters, if there are any. But | |
1953 * replace a TAB with a space. */ | |
1954 for (p = text; *p != NUL; ++p) | |
1955 { | |
1956 # ifdef FEAT_MBYTE | |
33 | 1957 int len; |
1958 | |
474 | 1959 if (has_mbyte && (len = (*mb_ptr2len)(p)) > 1) |
29 | 1960 { |
1961 if (!vim_isprintc((*mb_ptr2char)(p))) | |
1962 break; | |
1963 p += len - 1; | |
1964 } | |
1965 else | |
1966 # endif | |
1967 if (*p == TAB) | |
1968 *p = ' '; | |
1969 else if (ptr2cells(p) > 1) | |
1970 break; | |
1971 } | |
1972 if (*p != NUL) | |
1973 { | |
1974 p = transstr(text); | |
1975 vim_free(text); | |
1976 text = p; | |
1977 } | |
1978 } | |
1979 } | |
1980 if (text == NULL) | |
1981 #endif | |
1982 { | |
9754
a990e7ed260b
commit https://github.com/vim/vim/commit/ee695f787ade7fd88fc5f5497553d95c0c3645b5
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
1983 long count = (long)(lnume - lnum + 1); |
a990e7ed260b
commit https://github.com/vim/vim/commit/ee695f787ade7fd88fc5f5497553d95c0c3645b5
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
1984 |
a990e7ed260b
commit https://github.com/vim/vim/commit/ee695f787ade7fd88fc5f5497553d95c0c3645b5
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
1985 vim_snprintf((char *)buf, FOLD_TEXT_LEN, |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
1986 NGETTEXT("+--%3ld line folded ", |
9754
a990e7ed260b
commit https://github.com/vim/vim/commit/ee695f787ade7fd88fc5f5497553d95c0c3645b5
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
1987 "+--%3ld lines folded ", count), |
a990e7ed260b
commit https://github.com/vim/vim/commit/ee695f787ade7fd88fc5f5497553d95c0c3645b5
Christian Brabandt <cb@256bit.org>
parents:
9389
diff
changeset
|
1988 count); |
29 | 1989 text = buf; |
1990 } | |
1991 return text; | |
1992 } | |
1993 | |
7 | 1994 /* foldtext_cleanup() {{{2 */ |
1995 /* | |
1996 * Remove 'foldmarker' and 'commentstring' from "str" (in-place). | |
1997 */ | |
1998 void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
1999 foldtext_cleanup(char_u *str) |
7 | 2000 { |
2001 char_u *cms_start; /* first part or the whole comment */ | |
2002 int cms_slen = 0; /* length of cms_start */ | |
2003 char_u *cms_end; /* last part of the comment or NULL */ | |
2004 int cms_elen = 0; /* length of cms_end */ | |
2005 char_u *s; | |
45 | 2006 char_u *p; |
7 | 2007 int len; |
2008 int did1 = FALSE; | |
2009 int did2 = FALSE; | |
2010 | |
2011 /* Ignore leading and trailing white space in 'commentstring'. */ | |
2012 cms_start = skipwhite(curbuf->b_p_cms); | |
835 | 2013 cms_slen = (int)STRLEN(cms_start); |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
2014 while (cms_slen > 0 && VIM_ISWHITE(cms_start[cms_slen - 1])) |
7 | 2015 --cms_slen; |
2016 | |
2017 /* locate "%s" in 'commentstring', use the part before and after it. */ | |
2018 cms_end = (char_u *)strstr((char *)cms_start, "%s"); | |
2019 if (cms_end != NULL) | |
2020 { | |
835 | 2021 cms_elen = cms_slen - (int)(cms_end - cms_start); |
2022 cms_slen = (int)(cms_end - cms_start); | |
7 | 2023 |
2024 /* exclude white space before "%s" */ | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
2025 while (cms_slen > 0 && VIM_ISWHITE(cms_start[cms_slen - 1])) |
7 | 2026 --cms_slen; |
2027 | |
2028 /* skip "%s" and white space after it */ | |
2029 s = skipwhite(cms_end + 2); | |
835 | 2030 cms_elen -= (int)(s - cms_end); |
7 | 2031 cms_end = s; |
2032 } | |
2033 parseMarker(curwin); | |
2034 | |
2035 for (s = str; *s != NUL; ) | |
2036 { | |
2037 len = 0; | |
2038 if (STRNCMP(s, curwin->w_p_fmr, foldstartmarkerlen) == 0) | |
45 | 2039 len = foldstartmarkerlen; |
2040 else if (STRNCMP(s, foldendmarker, foldendmarkerlen) == 0) | |
2041 len = foldendmarkerlen; | |
2042 if (len > 0) | |
7 | 2043 { |
2044 if (VIM_ISDIGIT(s[len])) | |
2045 ++len; | |
45 | 2046 |
2047 /* May remove 'commentstring' start. Useful when it's a double | |
2048 * quote and we already removed a double quote. */ | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
2049 for (p = s; p > str && VIM_ISWHITE(p[-1]); --p) |
45 | 2050 ; |
2051 if (p >= str + cms_slen | |
2052 && STRNCMP(p - cms_slen, cms_start, cms_slen) == 0) | |
2053 { | |
835 | 2054 len += (int)(s - p) + cms_slen; |
45 | 2055 s = p - cms_slen; |
2056 } | |
7 | 2057 } |
2058 else if (cms_end != NULL) | |
2059 { | |
45 | 2060 if (!did1 && cms_slen > 0 && STRNCMP(s, cms_start, cms_slen) == 0) |
7 | 2061 { |
2062 len = cms_slen; | |
2063 did1 = TRUE; | |
2064 } | |
45 | 2065 else if (!did2 && cms_elen > 0 |
2066 && STRNCMP(s, cms_end, cms_elen) == 0) | |
7 | 2067 { |
2068 len = cms_elen; | |
2069 did2 = TRUE; | |
2070 } | |
2071 } | |
2072 if (len != 0) | |
2073 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
2074 while (VIM_ISWHITE(s[len])) |
7 | 2075 ++len; |
1624 | 2076 STRMOVE(s, s + len); |
7 | 2077 } |
2078 else | |
2079 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
2080 MB_PTR_ADV(s); |
7 | 2081 } |
2082 } | |
2083 } | |
2084 | |
2085 /* Folding by indent, expr, marker and syntax. {{{1 */ | |
2086 /* Define "fline_T", passed to get fold level for a line. {{{2 */ | |
2087 typedef struct | |
2088 { | |
2089 win_T *wp; /* window */ | |
2090 linenr_T lnum; /* current line number */ | |
2091 linenr_T off; /* offset between lnum and real line number */ | |
2092 linenr_T lnum_save; /* line nr used by foldUpdateIEMSRecurse() */ | |
2093 int lvl; /* current level (-1 for undefined) */ | |
2094 int lvl_next; /* level used for next line */ | |
2095 int start; /* number of folds that are forced to start at | |
2096 this line. */ | |
2097 int end; /* level of fold that is forced to end below | |
2098 this line */ | |
2099 int had_end; /* level of fold that is forced to end above | |
2100 this line (copy of "end" of prev. line) */ | |
2101 } fline_T; | |
2102 | |
2103 /* Flag is set when redrawing is needed. */ | |
2104 static int fold_changed; | |
2105 | |
2106 /* Function declarations. {{{2 */ | |
7807
1a5d34492798
commit https://github.com/vim/vim/commit/d99df423c559d85c17779b3685426c489554908c
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2107 static linenr_T foldUpdateIEMSRecurse(garray_T *gap, int level, linenr_T startlnum, fline_T *flp, void (*getlevel)(fline_T *), linenr_T bot, int topflags); |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
2108 static int foldInsert(garray_T *gap, int i); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
2109 static void foldSplit(garray_T *gap, int i, linenr_T top, linenr_T bot); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
2110 static void foldRemove(garray_T *gap, linenr_T top, linenr_T bot); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
2111 static void foldMerge(fold_T *fp1, garray_T *gap, fold_T *fp2); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
2112 static void foldlevelIndent(fline_T *flp); |
7 | 2113 #ifdef FEAT_DIFF |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
2114 static void foldlevelDiff(fline_T *flp); |
7 | 2115 #endif |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
2116 static void foldlevelExpr(fline_T *flp); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
2117 static void foldlevelMarker(fline_T *flp); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
2118 static void foldlevelSyntax(fline_T *flp); |
7 | 2119 |
2120 /* foldUpdateIEMS() {{{2 */ | |
2121 /* | |
2122 * Update the folding for window "wp", at least from lines "top" to "bot". | |
2123 * Return TRUE if any folds did change. | |
2124 */ | |
2125 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2126 foldUpdateIEMS(win_T *wp, linenr_T top, linenr_T bot) |
7 | 2127 { |
2128 linenr_T start; | |
2129 linenr_T end; | |
2130 fline_T fline; | |
7807
1a5d34492798
commit https://github.com/vim/vim/commit/d99df423c559d85c17779b3685426c489554908c
Christian Brabandt <cb@256bit.org>
parents:
7801
diff
changeset
|
2131 void (*getlevel)(fline_T *); |
7 | 2132 int level; |
2133 fold_T *fp; | |
2134 | |
2135 /* Avoid problems when being called recursively. */ | |
2136 if (invalid_top != (linenr_T)0) | |
2137 return; | |
2138 | |
2139 if (wp->w_foldinvalid) | |
2140 { | |
2141 /* Need to update all folds. */ | |
2142 top = 1; | |
2143 bot = wp->w_buffer->b_ml.ml_line_count; | |
2144 wp->w_foldinvalid = FALSE; | |
2145 | |
2146 /* Mark all folds a maybe-small. */ | |
2147 setSmallMaybe(&wp->w_folds); | |
2148 } | |
2149 | |
2150 #ifdef FEAT_DIFF | |
2151 /* add the context for "diff" folding */ | |
2152 if (foldmethodIsDiff(wp)) | |
2153 { | |
2154 if (top > diff_context) | |
2155 top -= diff_context; | |
2156 else | |
2157 top = 1; | |
2158 bot += diff_context; | |
2159 } | |
2160 #endif | |
2161 | |
2162 /* When deleting lines at the end of the buffer "top" can be past the end | |
2163 * of the buffer. */ | |
2164 if (top > wp->w_buffer->b_ml.ml_line_count) | |
2165 top = wp->w_buffer->b_ml.ml_line_count; | |
2166 | |
2167 fold_changed = FALSE; | |
2168 fline.wp = wp; | |
2169 fline.off = 0; | |
2170 fline.lvl = 0; | |
2171 fline.lvl_next = -1; | |
2172 fline.start = 0; | |
2173 fline.end = MAX_LEVEL + 1; | |
2174 fline.had_end = MAX_LEVEL + 1; | |
2175 | |
2176 invalid_top = top; | |
2177 invalid_bot = bot; | |
2178 | |
2179 if (foldmethodIsMarker(wp)) | |
2180 { | |
2181 getlevel = foldlevelMarker; | |
2182 | |
2183 /* Init marker variables to speed up foldlevelMarker(). */ | |
2184 parseMarker(wp); | |
2185 | |
2186 /* Need to get the level of the line above top, it is used if there is | |
2187 * no marker at the top. */ | |
2188 if (top > 1) | |
2189 { | |
2190 /* Get the fold level at top - 1. */ | |
2191 level = foldLevelWin(wp, top - 1); | |
2192 | |
2193 /* The fold may end just above the top, check for that. */ | |
2194 fline.lnum = top - 1; | |
2195 fline.lvl = level; | |
2196 getlevel(&fline); | |
2197 | |
2198 /* If a fold started here, we already had the level, if it stops | |
2199 * here, we need to use lvl_next. Could also start and end a fold | |
2200 * in the same line. */ | |
2201 if (fline.lvl > level) | |
2202 fline.lvl = level - (fline.lvl - fline.lvl_next); | |
2203 else | |
2204 fline.lvl = fline.lvl_next; | |
2205 } | |
2206 fline.lnum = top; | |
2207 getlevel(&fline); | |
2208 } | |
2209 else | |
2210 { | |
2211 fline.lnum = top; | |
2212 if (foldmethodIsExpr(wp)) | |
2213 { | |
2214 getlevel = foldlevelExpr; | |
2215 /* start one line back, because a "<1" may indicate the end of a | |
2216 * fold in the topline */ | |
2217 if (top > 1) | |
2218 --fline.lnum; | |
2219 } | |
2220 else if (foldmethodIsSyntax(wp)) | |
2221 getlevel = foldlevelSyntax; | |
2222 #ifdef FEAT_DIFF | |
2223 else if (foldmethodIsDiff(wp)) | |
2224 getlevel = foldlevelDiff; | |
2225 #endif | |
2226 else | |
2227 getlevel = foldlevelIndent; | |
2228 | |
2229 /* Backup to a line for which the fold level is defined. Since it's | |
2230 * always defined for line one, we will stop there. */ | |
2231 fline.lvl = -1; | |
2232 for ( ; !got_int; --fline.lnum) | |
2233 { | |
2234 /* Reset lvl_next each time, because it will be set to a value for | |
2235 * the next line, but we search backwards here. */ | |
2236 fline.lvl_next = -1; | |
2237 getlevel(&fline); | |
2238 if (fline.lvl >= 0) | |
2239 break; | |
2240 } | |
2241 } | |
2242 | |
1977 | 2243 /* |
2244 * If folding is defined by the syntax, it is possible that a change in | |
2245 * one line will cause all sub-folds of the current fold to change (e.g., | |
2246 * closing a C-style comment can cause folds in the subsequent lines to | |
2247 * appear). To take that into account we should adjust the value of "bot" | |
2248 * to point to the end of the current fold: | |
2249 */ | |
2250 if (foldlevelSyntax == getlevel) | |
2251 { | |
2252 garray_T *gap = &wp->w_folds; | |
2271
2b33a7678e7b
Fix compiler warnings for shadowed variables. Make 'conceal' a long instead
Bram Moolenaar <bram@vim.org>
parents:
2114
diff
changeset
|
2253 fold_T *fpn = NULL; |
1977 | 2254 int current_fdl = 0; |
2255 linenr_T fold_start_lnum = 0; | |
2256 linenr_T lnum_rel = fline.lnum; | |
2257 | |
2258 while (current_fdl < fline.lvl) | |
2259 { | |
2271
2b33a7678e7b
Fix compiler warnings for shadowed variables. Make 'conceal' a long instead
Bram Moolenaar <bram@vim.org>
parents:
2114
diff
changeset
|
2260 if (!foldFind(gap, lnum_rel, &fpn)) |
1977 | 2261 break; |
2262 ++current_fdl; | |
2263 | |
2271
2b33a7678e7b
Fix compiler warnings for shadowed variables. Make 'conceal' a long instead
Bram Moolenaar <bram@vim.org>
parents:
2114
diff
changeset
|
2264 fold_start_lnum += fpn->fd_top; |
2b33a7678e7b
Fix compiler warnings for shadowed variables. Make 'conceal' a long instead
Bram Moolenaar <bram@vim.org>
parents:
2114
diff
changeset
|
2265 gap = &fpn->fd_nested; |
2b33a7678e7b
Fix compiler warnings for shadowed variables. Make 'conceal' a long instead
Bram Moolenaar <bram@vim.org>
parents:
2114
diff
changeset
|
2266 lnum_rel -= fpn->fd_top; |
1977 | 2267 } |
2271
2b33a7678e7b
Fix compiler warnings for shadowed variables. Make 'conceal' a long instead
Bram Moolenaar <bram@vim.org>
parents:
2114
diff
changeset
|
2268 if (fpn != NULL && current_fdl == fline.lvl) |
1977 | 2269 { |
2271
2b33a7678e7b
Fix compiler warnings for shadowed variables. Make 'conceal' a long instead
Bram Moolenaar <bram@vim.org>
parents:
2114
diff
changeset
|
2270 linenr_T fold_end_lnum = fold_start_lnum + fpn->fd_len; |
1977 | 2271 |
2272 if (fold_end_lnum > bot) | |
2273 bot = fold_end_lnum; | |
2274 } | |
2275 } | |
2276 | |
7 | 2277 start = fline.lnum; |
2278 end = bot; | |
2279 /* Do at least one line. */ | |
2280 if (start > end && end < wp->w_buffer->b_ml.ml_line_count) | |
2281 end = start; | |
2282 while (!got_int) | |
2283 { | |
2284 /* Always stop at the end of the file ("end" can be past the end of | |
2285 * the file). */ | |
2286 if (fline.lnum > wp->w_buffer->b_ml.ml_line_count) | |
2287 break; | |
2288 if (fline.lnum > end) | |
2289 { | |
2290 /* For "marker", "expr" and "syntax" methods: If a change caused | |
2291 * a fold to be removed, we need to continue at least until where | |
2292 * it ended. */ | |
2293 if (getlevel != foldlevelMarker | |
2294 && getlevel != foldlevelSyntax | |
2295 && getlevel != foldlevelExpr) | |
2296 break; | |
2297 if ((start <= end | |
2298 && foldFind(&wp->w_folds, end, &fp) | |
2299 && fp->fd_top + fp->fd_len - 1 > end) | |
2300 || (fline.lvl == 0 | |
2301 && foldFind(&wp->w_folds, fline.lnum, &fp) | |
2302 && fp->fd_top < fline.lnum)) | |
2303 end = fp->fd_top + fp->fd_len - 1; | |
2304 else if (getlevel == foldlevelSyntax | |
2305 && foldLevelWin(wp, fline.lnum) != fline.lvl) | |
2306 /* For "syntax" method: Compare the foldlevel that the syntax | |
2307 * tells us to the foldlevel from the existing folds. If they | |
2308 * don't match continue updating folds. */ | |
2309 end = fline.lnum; | |
2310 else | |
2311 break; | |
2312 } | |
2313 | |
2314 /* A level 1 fold starts at a line with foldlevel > 0. */ | |
2315 if (fline.lvl > 0) | |
2316 { | |
2317 invalid_top = fline.lnum; | |
2318 invalid_bot = end; | |
2319 end = foldUpdateIEMSRecurse(&wp->w_folds, | |
2320 1, start, &fline, getlevel, end, FD_LEVEL); | |
2321 start = fline.lnum; | |
2322 } | |
2323 else | |
2324 { | |
2325 if (fline.lnum == wp->w_buffer->b_ml.ml_line_count) | |
2326 break; | |
2327 ++fline.lnum; | |
2328 fline.lvl = fline.lvl_next; | |
2329 getlevel(&fline); | |
2330 } | |
2331 } | |
2332 | |
2333 /* There can't be any folds from start until end now. */ | |
2334 foldRemove(&wp->w_folds, start, end); | |
2335 | |
2336 /* If some fold changed, need to redraw and position cursor. */ | |
2337 if (fold_changed && wp->w_p_fen) | |
1554 | 2338 changed_window_setting_win(wp); |
7 | 2339 |
2340 /* If we updated folds past "bot", need to redraw more lines. Don't do | |
2341 * this in other situations, the changed lines will be redrawn anyway and | |
2342 * this method can cause the whole window to be updated. */ | |
2343 if (end != bot) | |
2344 { | |
2345 if (wp->w_redraw_top == 0 || wp->w_redraw_top > top) | |
2346 wp->w_redraw_top = top; | |
2347 if (wp->w_redraw_bot < end) | |
2348 wp->w_redraw_bot = end; | |
2349 } | |
2350 | |
2351 invalid_top = (linenr_T)0; | |
2352 } | |
2353 | |
2354 /* foldUpdateIEMSRecurse() {{{2 */ | |
2355 /* | |
2356 * Update a fold that starts at "flp->lnum". At this line there is always a | |
2357 * valid foldlevel, and its level >= "level". | |
2358 * "flp" is valid for "flp->lnum" when called and it's valid when returning. | |
2359 * "flp->lnum" is set to the lnum just below the fold, if it ends before | |
2360 * "bot", it's "bot" plus one if the fold continues and it's bigger when using | |
2361 * the marker method and a text change made following folds to change. | |
2362 * When returning, "flp->lnum_save" is the line number that was used to get | |
2363 * the level when the level at "flp->lnum" is invalid. | |
2364 * Remove any folds from "startlnum" up to here at this level. | |
2365 * Recursively update nested folds. | |
2366 * Below line "bot" there are no changes in the text. | |
2367 * "flp->lnum", "flp->lnum_save" and "bot" are relative to the start of the | |
2368 * outer fold. | |
2369 * "flp->off" is the offset to the real line number in the buffer. | |
2370 * | |
2371 * All this would be a lot simpler if all folds in the range would be deleted | |
1698 | 2372 * and then created again. But we would lose all information about the |
7 | 2373 * folds, even when making changes that don't affect the folding (e.g. "vj~"). |
2374 * | |
2375 * Returns bot, which may have been increased for lines that also need to be | |
2376 * updated as a result of a detected change in the fold. | |
2377 */ | |
2378 static linenr_T | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2379 foldUpdateIEMSRecurse( |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2380 garray_T *gap, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2381 int level, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2382 linenr_T startlnum, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2383 fline_T *flp, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2384 void (*getlevel)(fline_T *), |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2385 linenr_T bot, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2386 int topflags) /* flags used by containing fold */ |
7 | 2387 { |
2388 linenr_T ll; | |
2389 fold_T *fp = NULL; | |
2390 fold_T *fp2; | |
2391 int lvl = level; | |
2392 linenr_T startlnum2 = startlnum; | |
2393 linenr_T firstlnum = flp->lnum; /* first lnum we got */ | |
2394 int i; | |
2395 int finish = FALSE; | |
2396 linenr_T linecount = flp->wp->w_buffer->b_ml.ml_line_count - flp->off; | |
2397 int concat; | |
2398 | |
2399 /* | |
2400 * If using the marker method, the start line is not the start of a fold | |
2401 * at the level we're dealing with and the level is non-zero, we must use | |
2402 * the previous fold. But ignore a fold that starts at or below | |
2403 * startlnum, it must be deleted. | |
2404 */ | |
2405 if (getlevel == foldlevelMarker && flp->start <= flp->lvl - level | |
2406 && flp->lvl > 0) | |
2407 { | |
7009 | 2408 (void)foldFind(gap, startlnum - 1, &fp); |
7 | 2409 if (fp >= ((fold_T *)gap->ga_data) + gap->ga_len |
2410 || fp->fd_top >= startlnum) | |
2411 fp = NULL; | |
2412 } | |
2413 | |
2414 /* | |
2415 * Loop over all lines in this fold, or until "bot" is hit. | |
2416 * Handle nested folds inside of this fold. | |
2417 * "flp->lnum" is the current line. When finding the end of the fold, it | |
2418 * is just below the end of the fold. | |
2419 * "*flp" contains the level of the line "flp->lnum" or a following one if | |
2420 * there are lines with an invalid fold level. "flp->lnum_save" is the | |
2421 * line number that was used to get the fold level (below "flp->lnum" when | |
2422 * it has an invalid fold level). When called the fold level is always | |
2423 * valid, thus "flp->lnum_save" is equal to "flp->lnum". | |
2424 */ | |
2425 flp->lnum_save = flp->lnum; | |
2426 while (!got_int) | |
2427 { | |
2428 /* Updating folds can be slow, check for CTRL-C. */ | |
2429 line_breakcheck(); | |
2430 | |
2431 /* Set "lvl" to the level of line "flp->lnum". When flp->start is set | |
2432 * and after the first line of the fold, set the level to zero to | |
2433 * force the fold to end. Do the same when had_end is set: Previous | |
2434 * line was marked as end of a fold. */ | |
2435 lvl = flp->lvl; | |
2436 if (lvl > MAX_LEVEL) | |
2437 lvl = MAX_LEVEL; | |
2438 if (flp->lnum > firstlnum | |
2439 && (level > lvl - flp->start || level >= flp->had_end)) | |
2440 lvl = 0; | |
2441 | |
2442 if (flp->lnum > bot && !finish && fp != NULL) | |
2443 { | |
2444 /* For "marker" and "syntax" methods: | |
2445 * - If a change caused a nested fold to be removed, we need to | |
2446 * delete it and continue at least until where it ended. | |
2447 * - If a change caused a nested fold to be created, or this fold | |
2448 * to continue below its original end, need to finish this fold. | |
2449 */ | |
2450 if (getlevel != foldlevelMarker | |
2451 && getlevel != foldlevelExpr | |
2452 && getlevel != foldlevelSyntax) | |
2453 break; | |
2454 i = 0; | |
2455 fp2 = fp; | |
2456 if (lvl >= level) | |
2457 { | |
2458 /* Compute how deep the folds currently are, if it's deeper | |
2459 * than "lvl" then some must be deleted, need to update | |
2460 * at least one nested fold. */ | |
2461 ll = flp->lnum - fp->fd_top; | |
2462 while (foldFind(&fp2->fd_nested, ll, &fp2)) | |
2463 { | |
2464 ++i; | |
2465 ll -= fp2->fd_top; | |
2466 } | |
2467 } | |
2468 if (lvl < level + i) | |
2469 { | |
7009 | 2470 (void)foldFind(&fp->fd_nested, flp->lnum - fp->fd_top, &fp2); |
7 | 2471 if (fp2 != NULL) |
2472 bot = fp2->fd_top + fp2->fd_len - 1 + fp->fd_top; | |
2473 } | |
2474 else if (fp->fd_top + fp->fd_len <= flp->lnum && lvl >= level) | |
2475 finish = TRUE; | |
2476 else | |
2477 break; | |
2478 } | |
2479 | |
2480 /* At the start of the first nested fold and at the end of the current | |
2481 * fold: check if existing folds at this level, before the current | |
2482 * one, need to be deleted or truncated. */ | |
2483 if (fp == NULL | |
2484 && (lvl != level | |
2485 || flp->lnum_save >= bot | |
2486 || flp->start != 0 | |
2487 || flp->had_end <= MAX_LEVEL | |
2488 || flp->lnum == linecount)) | |
2489 { | |
2490 /* | |
2491 * Remove or update folds that have lines between startlnum and | |
2492 * firstlnum. | |
2493 */ | |
2494 while (!got_int) | |
2495 { | |
2496 /* set concat to 1 if it's allowed to concatenated this fold | |
2497 * with a previous one that touches it. */ | |
2498 if (flp->start != 0 || flp->had_end <= MAX_LEVEL) | |
2499 concat = 0; | |
2500 else | |
2501 concat = 1; | |
2502 | |
2503 /* Find an existing fold to re-use. Preferably one that | |
2504 * includes startlnum, otherwise one that ends just before | |
2505 * startlnum or starts after it. */ | |
2506 if (foldFind(gap, startlnum, &fp) | |
2507 || (fp < ((fold_T *)gap->ga_data) + gap->ga_len | |
2508 && fp->fd_top <= firstlnum) | |
2509 || foldFind(gap, firstlnum - concat, &fp) | |
2510 || (fp < ((fold_T *)gap->ga_data) + gap->ga_len | |
2511 && ((lvl < level && fp->fd_top < flp->lnum) | |
2512 || (lvl >= level | |
2513 && fp->fd_top <= flp->lnum_save)))) | |
2514 { | |
2515 if (fp->fd_top + fp->fd_len + concat > firstlnum) | |
2516 { | |
2517 /* Use existing fold for the new fold. If it starts | |
2518 * before where we started looking, extend it. If it | |
2519 * starts at another line, update nested folds to keep | |
2520 * their position, compensating for the new fd_top. */ | |
11038
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2521 if (fp->fd_top == firstlnum) |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2522 { |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2523 /* have found a fold beginning where we want */ |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2524 } |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2525 else if (fp->fd_top >= startlnum) |
7 | 2526 { |
2527 if (fp->fd_top > firstlnum) | |
2528 /* like lines are inserted */ | |
2529 foldMarkAdjustRecurse(&fp->fd_nested, | |
2530 (linenr_T)0, (linenr_T)MAXLNUM, | |
2531 (long)(fp->fd_top - firstlnum), 0L); | |
2532 else | |
2533 /* like lines are deleted */ | |
2534 foldMarkAdjustRecurse(&fp->fd_nested, | |
2535 (linenr_T)0, | |
2536 (long)(firstlnum - fp->fd_top - 1), | |
2537 (linenr_T)MAXLNUM, | |
2538 (long)(fp->fd_top - firstlnum)); | |
2539 fp->fd_len += fp->fd_top - firstlnum; | |
2540 fp->fd_top = firstlnum; | |
2541 fold_changed = TRUE; | |
2542 } | |
11038
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2543 else if ((flp->start != 0 && lvl == level) |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2544 || firstlnum != startlnum) |
7 | 2545 { |
11038
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2546 linenr_T breakstart; |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2547 linenr_T breakend; |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2548 |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2549 /* |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2550 * Before there was a fold spanning from above |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2551 * startlnum to below firstlnum. This fold is valid |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2552 * above startlnum (because we are not updating |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2553 * that range), but there should now be a break in |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2554 * it. |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2555 * If the break is because we are now forced to |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2556 * start a new fold at the level "level" at line |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2557 * fline->lnum, then we need to split the fold at |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2558 * fline->lnum. |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2559 * If the break is because the range |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2560 * [startlnum, firstlnum) is now at a lower indent |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2561 * than "level", we need to split the fold in this |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2562 * range. |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2563 * Any splits have to be done recursively. |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2564 */ |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2565 if (firstlnum != startlnum) |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2566 { |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2567 breakstart = startlnum; |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2568 breakend = firstlnum; |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2569 } |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2570 else |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2571 { |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2572 breakstart = flp->lnum; |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2573 breakend = flp->lnum; |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2574 } |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2575 foldRemove(&fp->fd_nested, breakstart - fp->fd_top, |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2576 breakend - fp->fd_top); |
7 | 2577 i = (int)(fp - (fold_T *)gap->ga_data); |
11038
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2578 foldSplit(gap, i, breakstart, breakend - 1); |
7 | 2579 fp = (fold_T *)gap->ga_data + i + 1; |
11038
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2580 |
7 | 2581 /* If using the "marker" or "syntax" method, we |
2582 * need to continue until the end of the fold is | |
2583 * found. */ | |
2584 if (getlevel == foldlevelMarker | |
2585 || getlevel == foldlevelExpr | |
2586 || getlevel == foldlevelSyntax) | |
2587 finish = TRUE; | |
2588 } | |
11038
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2589 |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2590 if (fp->fd_top == startlnum && concat) |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2591 { |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2592 i = (int)(fp - (fold_T *)gap->ga_data); |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2593 if (i != 0) |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2594 { |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2595 fp2 = fp - 1; |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2596 if (fp2->fd_top + fp2->fd_len == fp->fd_top) |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2597 { |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2598 foldMerge(fp2, gap, fp); |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2599 fp = fp2; |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2600 } |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2601 } |
c8b49300c6b7
patch 8.0.0408: updating folds does not always work properly
Christian Brabandt <cb@256bit.org>
parents:
10998
diff
changeset
|
2602 } |
7 | 2603 break; |
2604 } | |
2605 if (fp->fd_top >= startlnum) | |
2606 { | |
2607 /* A fold that starts at or after startlnum and stops | |
2608 * before the new fold must be deleted. Continue | |
2609 * looking for the next one. */ | |
2610 deleteFoldEntry(gap, | |
2611 (int)(fp - (fold_T *)gap->ga_data), TRUE); | |
2612 } | |
2613 else | |
2614 { | |
2615 /* A fold has some lines above startlnum, truncate it | |
2616 * to stop just above startlnum. */ | |
2617 fp->fd_len = startlnum - fp->fd_top; | |
2618 foldMarkAdjustRecurse(&fp->fd_nested, | |
2619 (linenr_T)fp->fd_len, (linenr_T)MAXLNUM, | |
2620 (linenr_T)MAXLNUM, 0L); | |
2621 fold_changed = TRUE; | |
2622 } | |
2623 } | |
2624 else | |
2625 { | |
2626 /* Insert new fold. Careful: ga_data may be NULL and it | |
2627 * may change! */ | |
2628 i = (int)(fp - (fold_T *)gap->ga_data); | |
2629 if (foldInsert(gap, i) != OK) | |
2630 return bot; | |
2631 fp = (fold_T *)gap->ga_data + i; | |
2632 /* The new fold continues until bot, unless we find the | |
2633 * end earlier. */ | |
2634 fp->fd_top = firstlnum; | |
2635 fp->fd_len = bot - firstlnum + 1; | |
2636 /* When the containing fold is open, the new fold is open. | |
2637 * The new fold is closed if the fold above it is closed. | |
2638 * The first fold depends on the containing fold. */ | |
2639 if (topflags == FD_OPEN) | |
2640 { | |
2641 flp->wp->w_fold_manual = TRUE; | |
2642 fp->fd_flags = FD_OPEN; | |
2643 } | |
2644 else if (i <= 0) | |
2645 { | |
2646 fp->fd_flags = topflags; | |
2647 if (topflags != FD_LEVEL) | |
2648 flp->wp->w_fold_manual = TRUE; | |
2649 } | |
2650 else | |
2651 fp->fd_flags = (fp - 1)->fd_flags; | |
2652 fp->fd_small = MAYBE; | |
2653 /* If using the "marker", "expr" or "syntax" method, we | |
2654 * need to continue until the end of the fold is found. */ | |
2655 if (getlevel == foldlevelMarker | |
2656 || getlevel == foldlevelExpr | |
2657 || getlevel == foldlevelSyntax) | |
2658 finish = TRUE; | |
2659 fold_changed = TRUE; | |
2660 break; | |
2661 } | |
2662 } | |
2663 } | |
2664 | |
2665 if (lvl < level || flp->lnum > linecount) | |
2666 { | |
2667 /* | |
2668 * Found a line with a lower foldlevel, this fold ends just above | |
2669 * "flp->lnum". | |
2670 */ | |
2671 break; | |
2672 } | |
2673 | |
2674 /* | |
2675 * The fold includes the line "flp->lnum" and "flp->lnum_save". | |
840 | 2676 * Check "fp" for safety. |
7 | 2677 */ |
840 | 2678 if (lvl > level && fp != NULL) |
7 | 2679 { |
2680 /* | |
2681 * There is a nested fold, handle it recursively. | |
2682 */ | |
2683 /* At least do one line (can happen when finish is TRUE). */ | |
2684 if (bot < flp->lnum) | |
2685 bot = flp->lnum; | |
2686 | |
2687 /* Line numbers in the nested fold are relative to the start of | |
2688 * this fold. */ | |
2689 flp->lnum = flp->lnum_save - fp->fd_top; | |
2690 flp->off += fp->fd_top; | |
2691 i = (int)(fp - (fold_T *)gap->ga_data); | |
2692 bot = foldUpdateIEMSRecurse(&fp->fd_nested, level + 1, | |
2693 startlnum2 - fp->fd_top, flp, getlevel, | |
2694 bot - fp->fd_top, fp->fd_flags); | |
2695 fp = (fold_T *)gap->ga_data + i; | |
2696 flp->lnum += fp->fd_top; | |
2697 flp->lnum_save += fp->fd_top; | |
2698 flp->off -= fp->fd_top; | |
2699 bot += fp->fd_top; | |
2700 startlnum2 = flp->lnum; | |
2701 | |
2702 /* This fold may end at the same line, don't incr. flp->lnum. */ | |
2703 } | |
2704 else | |
2705 { | |
2706 /* | |
2707 * Get the level of the next line, then continue the loop to check | |
2708 * if it ends there. | |
2709 * Skip over undefined lines, to find the foldlevel after it. | |
2710 * For the last line in the file the foldlevel is always valid. | |
2711 */ | |
2712 flp->lnum = flp->lnum_save; | |
2713 ll = flp->lnum + 1; | |
2714 while (!got_int) | |
2715 { | |
2716 /* Make the previous level available to foldlevel(). */ | |
2717 prev_lnum = flp->lnum; | |
2718 prev_lnum_lvl = flp->lvl; | |
2719 | |
2720 if (++flp->lnum > linecount) | |
2721 break; | |
2722 flp->lvl = flp->lvl_next; | |
2723 getlevel(flp); | |
2724 if (flp->lvl >= 0 || flp->had_end <= MAX_LEVEL) | |
2725 break; | |
2726 } | |
2727 prev_lnum = 0; | |
2728 if (flp->lnum > linecount) | |
2729 break; | |
2730 | |
2731 /* leave flp->lnum_save to lnum of the line that was used to get | |
2732 * the level, flp->lnum to the lnum of the next line. */ | |
2733 flp->lnum_save = flp->lnum; | |
2734 flp->lnum = ll; | |
2735 } | |
2736 } | |
2737 | |
2738 if (fp == NULL) /* only happens when got_int is set */ | |
2739 return bot; | |
2740 | |
2741 /* | |
2742 * Get here when: | |
2743 * lvl < level: the folds ends just above "flp->lnum" | |
2744 * lvl >= level: fold continues below "bot" | |
2745 */ | |
2746 | |
2747 /* Current fold at least extends until lnum. */ | |
2748 if (fp->fd_len < flp->lnum - fp->fd_top) | |
2749 { | |
2750 fp->fd_len = flp->lnum - fp->fd_top; | |
1513 | 2751 fp->fd_small = MAYBE; |
7 | 2752 fold_changed = TRUE; |
2753 } | |
2754 | |
2755 /* Delete contained folds from the end of the last one found until where | |
2756 * we stopped looking. */ | |
2757 foldRemove(&fp->fd_nested, startlnum2 - fp->fd_top, | |
2758 flp->lnum - 1 - fp->fd_top); | |
2759 | |
2760 if (lvl < level) | |
2761 { | |
2762 /* End of fold found, update the length when it got shorter. */ | |
2763 if (fp->fd_len != flp->lnum - fp->fd_top) | |
2764 { | |
2765 if (fp->fd_top + fp->fd_len > bot + 1) | |
2766 { | |
1224 | 2767 /* fold continued below bot */ |
7 | 2768 if (getlevel == foldlevelMarker |
2769 || getlevel == foldlevelExpr | |
2770 || getlevel == foldlevelSyntax) | |
2771 { | |
2772 /* marker method: truncate the fold and make sure the | |
2773 * previously included lines are processed again */ | |
2774 bot = fp->fd_top + fp->fd_len - 1; | |
2775 fp->fd_len = flp->lnum - fp->fd_top; | |
2776 } | |
2777 else | |
2778 { | |
2779 /* indent or expr method: split fold to create a new one | |
2780 * below bot */ | |
2781 i = (int)(fp - (fold_T *)gap->ga_data); | |
2782 foldSplit(gap, i, flp->lnum, bot); | |
2783 fp = (fold_T *)gap->ga_data + i; | |
2784 } | |
2785 } | |
2786 else | |
2787 fp->fd_len = flp->lnum - fp->fd_top; | |
2788 fold_changed = TRUE; | |
2789 } | |
2790 } | |
2791 | |
2792 /* delete following folds that end before the current line */ | |
2793 for (;;) | |
2794 { | |
2795 fp2 = fp + 1; | |
2796 if (fp2 >= (fold_T *)gap->ga_data + gap->ga_len | |
2797 || fp2->fd_top > flp->lnum) | |
2798 break; | |
2799 if (fp2->fd_top + fp2->fd_len > flp->lnum) | |
2800 { | |
2801 if (fp2->fd_top < flp->lnum) | |
2802 { | |
2803 /* Make fold that includes lnum start at lnum. */ | |
2804 foldMarkAdjustRecurse(&fp2->fd_nested, | |
2805 (linenr_T)0, (long)(flp->lnum - fp2->fd_top - 1), | |
2806 (linenr_T)MAXLNUM, (long)(fp2->fd_top - flp->lnum)); | |
2807 fp2->fd_len -= flp->lnum - fp2->fd_top; | |
2808 fp2->fd_top = flp->lnum; | |
2809 fold_changed = TRUE; | |
2810 } | |
2811 | |
2812 if (lvl >= level) | |
2813 { | |
2814 /* merge new fold with existing fold that follows */ | |
2815 foldMerge(fp, gap, fp2); | |
2816 } | |
2817 break; | |
2818 } | |
2819 fold_changed = TRUE; | |
2820 deleteFoldEntry(gap, (int)(fp2 - (fold_T *)gap->ga_data), TRUE); | |
2821 } | |
2822 | |
2823 /* Need to redraw the lines we inspected, which might be further down than | |
2824 * was asked for. */ | |
2825 if (bot < flp->lnum - 1) | |
2826 bot = flp->lnum - 1; | |
2827 | |
2828 return bot; | |
2829 } | |
2830 | |
2831 /* foldInsert() {{{2 */ | |
2832 /* | |
2833 * Insert a new fold in "gap" at position "i". | |
2834 * Returns OK for success, FAIL for failure. | |
2835 */ | |
2836 static int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2837 foldInsert(garray_T *gap, int i) |
7 | 2838 { |
2839 fold_T *fp; | |
2840 | |
2841 if (ga_grow(gap, 1) != OK) | |
2842 return FAIL; | |
2843 fp = (fold_T *)gap->ga_data + i; | |
2844 if (i < gap->ga_len) | |
2845 mch_memmove(fp + 1, fp, sizeof(fold_T) * (gap->ga_len - i)); | |
2846 ++gap->ga_len; | |
2847 ga_init2(&fp->fd_nested, (int)sizeof(fold_T), 10); | |
2848 return OK; | |
2849 } | |
2850 | |
2851 /* foldSplit() {{{2 */ | |
2852 /* | |
2853 * Split the "i"th fold in "gap", which starts before "top" and ends below | |
2854 * "bot" in two pieces, one ending above "top" and the other starting below | |
2855 * "bot". | |
2856 * The caller must first have taken care of any nested folds from "top" to | |
2857 * "bot"! | |
2858 */ | |
2859 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2860 foldSplit( |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2861 garray_T *gap, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2862 int i, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2863 linenr_T top, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2864 linenr_T bot) |
7 | 2865 { |
2866 fold_T *fp; | |
2867 fold_T *fp2; | |
2868 garray_T *gap1; | |
2869 garray_T *gap2; | |
2870 int idx; | |
2871 int len; | |
2872 | |
2873 /* The fold continues below bot, need to split it. */ | |
2874 if (foldInsert(gap, i + 1) == FAIL) | |
2875 return; | |
2876 fp = (fold_T *)gap->ga_data + i; | |
2877 fp[1].fd_top = bot + 1; | |
2878 fp[1].fd_len = fp->fd_len - (fp[1].fd_top - fp->fd_top); | |
2879 fp[1].fd_flags = fp->fd_flags; | |
1985 | 2880 fp[1].fd_small = MAYBE; |
2881 fp->fd_small = MAYBE; | |
7 | 2882 |
2883 /* Move nested folds below bot to new fold. There can't be | |
2884 * any between top and bot, they have been removed by the caller. */ | |
2885 gap1 = &fp->fd_nested; | |
2886 gap2 = &fp[1].fd_nested; | |
2887 (void)(foldFind(gap1, bot + 1 - fp->fd_top, &fp2)); | |
2888 len = (int)((fold_T *)gap1->ga_data + gap1->ga_len - fp2); | |
2889 if (len > 0 && ga_grow(gap2, len) == OK) | |
2890 { | |
2891 for (idx = 0; idx < len; ++idx) | |
2892 { | |
2893 ((fold_T *)gap2->ga_data)[idx] = fp2[idx]; | |
2894 ((fold_T *)gap2->ga_data)[idx].fd_top | |
2895 -= fp[1].fd_top - fp->fd_top; | |
2896 } | |
2897 gap2->ga_len = len; | |
2898 gap1->ga_len -= len; | |
2899 } | |
2900 fp->fd_len = top - fp->fd_top; | |
2901 fold_changed = TRUE; | |
2902 } | |
2903 | |
2904 /* foldRemove() {{{2 */ | |
2905 /* | |
2906 * Remove folds within the range "top" to and including "bot". | |
2907 * Check for these situations: | |
2908 * 1 2 3 | |
2909 * 1 2 3 | |
2910 * top 2 3 4 5 | |
2911 * 2 3 4 5 | |
2912 * bot 2 3 4 5 | |
2913 * 3 5 6 | |
2914 * 3 5 6 | |
2915 * | |
2916 * 1: not changed | |
1224 | 2917 * 2: truncate to stop above "top" |
7 | 2918 * 3: split in two parts, one stops above "top", other starts below "bot". |
2919 * 4: deleted | |
2920 * 5: made to start below "bot". | |
2921 * 6: not changed | |
2922 */ | |
2923 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
2924 foldRemove(garray_T *gap, linenr_T top, linenr_T bot) |
7 | 2925 { |
2926 fold_T *fp = NULL; | |
2927 | |
2928 if (bot < top) | |
2929 return; /* nothing to do */ | |
2930 | |
2931 for (;;) | |
2932 { | |
2933 /* Find fold that includes top or a following one. */ | |
2934 if (foldFind(gap, top, &fp) && fp->fd_top < top) | |
2935 { | |
2936 /* 2: or 3: need to delete nested folds */ | |
2937 foldRemove(&fp->fd_nested, top - fp->fd_top, bot - fp->fd_top); | |
2938 if (fp->fd_top + fp->fd_len > bot + 1) | |
2939 { | |
2940 /* 3: need to split it. */ | |
2941 foldSplit(gap, (int)(fp - (fold_T *)gap->ga_data), top, bot); | |
2942 } | |
2943 else | |
2944 { | |
2945 /* 2: truncate fold at "top". */ | |
2946 fp->fd_len = top - fp->fd_top; | |
2947 } | |
2948 fold_changed = TRUE; | |
2949 continue; | |
2950 } | |
2951 if (fp >= (fold_T *)(gap->ga_data) + gap->ga_len | |
2952 || fp->fd_top > bot) | |
2953 { | |
2954 /* 6: Found a fold below bot, can stop looking. */ | |
2955 break; | |
2956 } | |
2957 if (fp->fd_top >= top) | |
2958 { | |
2959 /* Found an entry below top. */ | |
2960 fold_changed = TRUE; | |
36 | 2961 if (fp->fd_top + fp->fd_len - 1 > bot) |
7 | 2962 { |
2963 /* 5: Make fold that includes bot start below bot. */ | |
2964 foldMarkAdjustRecurse(&fp->fd_nested, | |
2965 (linenr_T)0, (long)(bot - fp->fd_top), | |
2966 (linenr_T)MAXLNUM, (long)(fp->fd_top - bot - 1)); | |
2967 fp->fd_len -= bot - fp->fd_top + 1; | |
2968 fp->fd_top = bot + 1; | |
2969 break; | |
2970 } | |
2971 | |
2972 /* 4: Delete completely contained fold. */ | |
2973 deleteFoldEntry(gap, (int)(fp - (fold_T *)gap->ga_data), TRUE); | |
2974 } | |
2975 } | |
2976 } | |
2977 | |
11140
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2978 /* foldReverseOrder() {{{2 */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2979 static void |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2980 foldReverseOrder(garray_T *gap, linenr_T start, linenr_T end) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2981 { |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2982 fold_T *left, *right; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2983 fold_T tmp; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2984 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2985 for (; start < end; start++, end--) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2986 { |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2987 left = (fold_T *)gap->ga_data + start; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2988 right = (fold_T *)gap->ga_data + end; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2989 tmp = *left; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2990 *left = *right; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2991 *right = tmp; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2992 } |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2993 } |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2994 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2995 /* foldMoveRange_int() {{{2 */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2996 /* |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2997 * Move folds within the inclusive range "line1" to "line2" to after "dest" |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2998 * requires "line1" <= "line2" <= "dest" |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
2999 * |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3000 * There are the following situations for the first fold at or below line1 - 1. |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3001 * 1 2 3 4 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3002 * 1 2 3 4 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3003 * line1 2 3 4 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3004 * 2 3 4 5 6 7 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3005 * line2 3 4 5 6 7 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3006 * 3 4 6 7 8 9 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3007 * dest 4 7 8 9 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3008 * 4 7 8 10 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3009 * 4 7 8 10 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3010 * |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3011 * In the following descriptions, "moved" means moving in the buffer, *and* in |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3012 * the fold array. |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3013 * Meanwhile, "shifted" just means moving in the buffer. |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3014 * 1. not changed |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3015 * 2. truncated above line1 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3016 * 3. length reduced by line2 - line1, folds starting between the end of 3 and |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3017 * dest are truncated and shifted up |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3018 * 4. internal folds moved (from [line1, line2] to dest) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3019 * 5. moved to dest. |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3020 * 6. truncated below line2 and moved. |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3021 * 7. length reduced by line2 - dest, folds starting between line2 and dest are |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3022 * removed, top is moved down by move_len. |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3023 * 8. truncated below dest and shifted up. |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3024 * 9. shifted up |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3025 * 10. not changed |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3026 */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3027 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3028 static void |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3029 truncate_fold(fold_T *fp, linenr_T end) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3030 { |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3031 foldRemove(&fp->fd_nested, end - fp->fd_top, MAXLNUM); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3032 fp->fd_len = end - fp->fd_top + 1; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3033 } |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3034 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3035 #define fold_end(fp) ((fp)->fd_top + (fp)->fd_len - 1) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3036 #define valid_fold(fp, gap) ((fp) < ((fold_T *)(gap)->ga_data + (gap)->ga_len)) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3037 #define fold_index(fp, gap) ((size_t)(fp - ((fold_T *)(gap)->ga_data))) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3038 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3039 static void |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3040 foldMoveRange_int(garray_T *gap, linenr_T line1, linenr_T line2, linenr_T dest) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3041 { |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3042 fold_T *fp; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3043 linenr_T range_len = line2 - line1 + 1; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3044 linenr_T move_len = dest - line2; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3045 int at_start = foldFind(gap, line1 - 1, &fp); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3046 size_t move_start = 0, move_end = 0, dest_index = 0; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3047 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3048 if (at_start) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3049 { |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3050 if (fold_end(fp) > dest) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3051 { |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3052 /* Case 4 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3053 * don't have to change this fold, but have to move nested folds. |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3054 */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3055 foldMoveRange(&fp->fd_nested, line1 - fp->fd_top, line2 - |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3056 fp->fd_top, dest - fp->fd_top); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3057 return; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3058 } |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3059 else if (fold_end(fp) > line2) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3060 { |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3061 /* Case 3 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3062 * Remove nested folds between line1 and line2 & reduce the |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3063 * length of fold by "range_len". |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3064 * Folds after this one must be dealt with. |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3065 */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3066 foldMarkAdjustRecurse(&fp->fd_nested, line1 - fp->fd_top, line2 - |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3067 fp->fd_top, MAXLNUM, -range_len); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3068 fp->fd_len -= range_len; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3069 } |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3070 else |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3071 /* Case 2 truncate fold, folds after this one must be dealt with. */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3072 truncate_fold(fp, line1); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3073 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3074 /* Look at the next fold, and treat that one as if it were the first |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3075 * after "line1" (because now it is). */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3076 fp = fp + 1; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3077 } |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3078 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3079 if (!valid_fold(fp, gap) || fp->fd_top > dest) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3080 { |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3081 /* Case 10 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3082 * No folds after "line1" and before "dest" |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3083 */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3084 return; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3085 } |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3086 else if (fp->fd_top > line2) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3087 { |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3088 for (; valid_fold(fp, gap) && fold_end(fp) < dest; fp++) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3089 /* Case 9. (for all case 9's) -- shift up. */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3090 fp->fd_top -= range_len; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3091 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3092 if (valid_fold(fp, gap) && fp->fd_top < dest) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3093 { |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3094 /* Case 8. -- ensure truncated at dest, shift up */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3095 truncate_fold(fp, dest); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3096 fp->fd_top -= range_len; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3097 } |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3098 return; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3099 } |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3100 else if (fold_end(fp) > dest) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3101 { |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3102 /* Case 7 -- remove nested folds and shrink */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3103 foldMarkAdjustRecurse(&fp->fd_nested, line2 + 1 - fp->fd_top, dest - |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3104 fp->fd_top, MAXLNUM, -move_len); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3105 fp->fd_len -= move_len; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3106 fp->fd_top += move_len; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3107 return; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3108 } |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3109 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3110 /* Case 5 or 6 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3111 * changes rely on whether there are folds between the end of |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3112 * this fold and "dest". |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3113 */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3114 move_start = fold_index(fp, gap); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3115 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3116 for (; valid_fold(fp, gap) && fp->fd_top <= dest; fp++) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3117 { |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3118 if (fp->fd_top <= line2) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3119 { |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3120 /* 1. 2. or 3. */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3121 if (fold_end(fp) > line2) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3122 /* 2. or 3., truncate before moving */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3123 truncate_fold(fp, line2); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3124 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3125 fp->fd_top += move_len; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3126 continue; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3127 } |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3128 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3129 /* Record index of the first fold after the moved range. */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3130 if (move_end == 0) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3131 move_end = fold_index(fp, gap); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3132 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3133 if (fold_end(fp) > dest) |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3134 truncate_fold(fp, dest); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3135 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3136 fp->fd_top -= range_len; |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3137 } |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3138 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3139 dest_index = fold_index(fp, gap); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3140 |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3141 /* |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3142 * All folds are now correct, but they are not necessarily in the correct |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3143 * order. We have to swap folds in the range [move_end, dest_index) with |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3144 * those in the range [move_start, move_end). |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3145 */ |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3146 foldReverseOrder(gap, move_start, dest_index - 1); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3147 foldReverseOrder(gap, move_start, move_start + dest_index - move_end - 1); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3148 foldReverseOrder(gap, move_start + dest_index - move_end, dest_index - 1); |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3149 } |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3150 #undef fold_end |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3151 #undef valid_fold |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3152 #undef fold_index |
6b26e044b6f5
patch 8.0.0457: using :move messes up manual folds
Christian Brabandt <cb@256bit.org>
parents:
11131
diff
changeset
|
3153 |
7 | 3154 /* foldMerge() {{{2 */ |
3155 /* | |
1224 | 3156 * Merge two adjacent folds (and the nested ones in them). |
3157 * This only works correctly when the folds are really adjacent! Thus "fp1" | |
7 | 3158 * must end just above "fp2". |
3159 * The resulting fold is "fp1", nested folds are moved from "fp2" to "fp1". | |
3160 * Fold entry "fp2" in "gap" is deleted. | |
3161 */ | |
3162 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3163 foldMerge(fold_T *fp1, garray_T *gap, fold_T *fp2) |
7 | 3164 { |
3165 fold_T *fp3; | |
3166 fold_T *fp4; | |
3167 int idx; | |
3168 garray_T *gap1 = &fp1->fd_nested; | |
3169 garray_T *gap2 = &fp2->fd_nested; | |
3170 | |
3171 /* If the last nested fold in fp1 touches the first nested fold in fp2, | |
3172 * merge them recursively. */ | |
3173 if (foldFind(gap1, fp1->fd_len - 1L, &fp3) && foldFind(gap2, 0L, &fp4)) | |
3174 foldMerge(fp3, gap2, fp4); | |
3175 | |
3176 /* Move nested folds in fp2 to the end of fp1. */ | |
3177 if (gap2->ga_len > 0 && ga_grow(gap1, gap2->ga_len) == OK) | |
3178 { | |
3179 for (idx = 0; idx < gap2->ga_len; ++idx) | |
3180 { | |
3181 ((fold_T *)gap1->ga_data)[gap1->ga_len] | |
3182 = ((fold_T *)gap2->ga_data)[idx]; | |
3183 ((fold_T *)gap1->ga_data)[gap1->ga_len].fd_top += fp1->fd_len; | |
3184 ++gap1->ga_len; | |
3185 } | |
3186 gap2->ga_len = 0; | |
3187 } | |
3188 | |
3189 fp1->fd_len += fp2->fd_len; | |
3190 deleteFoldEntry(gap, (int)(fp2 - (fold_T *)gap->ga_data), TRUE); | |
3191 fold_changed = TRUE; | |
3192 } | |
3193 | |
3194 /* foldlevelIndent() {{{2 */ | |
3195 /* | |
3196 * Low level function to get the foldlevel for the "indent" method. | |
3197 * Doesn't use any caching. | |
3198 * Returns a level of -1 if the foldlevel depends on surrounding lines. | |
3199 */ | |
3200 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3201 foldlevelIndent(fline_T *flp) |
7 | 3202 { |
3203 char_u *s; | |
3204 buf_T *buf; | |
3205 linenr_T lnum = flp->lnum + flp->off; | |
3206 | |
3207 buf = flp->wp->w_buffer; | |
3208 s = skipwhite(ml_get_buf(buf, lnum, FALSE)); | |
3209 | |
3210 /* empty line or lines starting with a character in 'foldignore': level | |
3211 * depends on surrounding lines */ | |
3212 if (*s == NUL || vim_strchr(flp->wp->w_p_fdi, *s) != NULL) | |
3213 { | |
3214 /* first and last line can't be undefined, use level 0 */ | |
3215 if (lnum == 1 || lnum == buf->b_ml.ml_line_count) | |
3216 flp->lvl = 0; | |
3217 else | |
3218 flp->lvl = -1; | |
3219 } | |
3220 else | |
5438 | 3221 flp->lvl = get_indent_buf(buf, lnum) / get_sw_value(curbuf); |
7 | 3222 if (flp->lvl > flp->wp->w_p_fdn) |
1028 | 3223 { |
7 | 3224 flp->lvl = flp->wp->w_p_fdn; |
1028 | 3225 if (flp->lvl < 0) |
3226 flp->lvl = 0; | |
3227 } | |
7 | 3228 } |
3229 | |
3230 /* foldlevelDiff() {{{2 */ | |
3231 #ifdef FEAT_DIFF | |
3232 /* | |
3233 * Low level function to get the foldlevel for the "diff" method. | |
3234 * Doesn't use any caching. | |
3235 */ | |
3236 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3237 foldlevelDiff(fline_T *flp) |
7 | 3238 { |
3239 if (diff_infold(flp->wp, flp->lnum + flp->off)) | |
3240 flp->lvl = 1; | |
3241 else | |
3242 flp->lvl = 0; | |
3243 } | |
3244 #endif | |
3245 | |
3246 /* foldlevelExpr() {{{2 */ | |
3247 /* | |
3248 * Low level function to get the foldlevel for the "expr" method. | |
3249 * Doesn't use any caching. | |
3250 * Returns a level of -1 if the foldlevel depends on surrounding lines. | |
3251 */ | |
3252 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3253 foldlevelExpr(fline_T *flp) |
7 | 3254 { |
3255 #ifndef FEAT_EVAL | |
3256 flp->start = FALSE; | |
3257 flp->lvl = 0; | |
3258 #else | |
3259 win_T *win; | |
3260 int n; | |
3261 int c; | |
3262 linenr_T lnum = flp->lnum + flp->off; | |
3263 int save_keytyped; | |
3264 | |
3265 win = curwin; | |
3266 curwin = flp->wp; | |
3267 curbuf = flp->wp->w_buffer; | |
3268 set_vim_var_nr(VV_LNUM, lnum); | |
3269 | |
3270 flp->start = 0; | |
3271 flp->had_end = flp->end; | |
3272 flp->end = MAX_LEVEL + 1; | |
3273 if (lnum <= 1) | |
3274 flp->lvl = 0; | |
3275 | |
3276 /* KeyTyped may be reset to 0 when calling a function which invokes | |
3277 * do_cmdline(). To make 'foldopen' work correctly restore KeyTyped. */ | |
3278 save_keytyped = KeyTyped; | |
9389
32e34e574716
commit https://github.com/vim/vim/commit/22fcfad29276bd5f317faf516637dcd491b96a12
Christian Brabandt <cb@256bit.org>
parents:
9252
diff
changeset
|
3279 n = (int)eval_foldexpr(flp->wp->w_p_fde, &c); |
7 | 3280 KeyTyped = save_keytyped; |
3281 | |
3282 switch (c) | |
3283 { | |
3284 /* "a1", "a2", .. : add to the fold level */ | |
3285 case 'a': if (flp->lvl >= 0) | |
3286 { | |
3287 flp->lvl += n; | |
3288 flp->lvl_next = flp->lvl; | |
3289 } | |
3290 flp->start = n; | |
3291 break; | |
3292 | |
3293 /* "s1", "s2", .. : subtract from the fold level */ | |
3294 case 's': if (flp->lvl >= 0) | |
3295 { | |
3296 if (n > flp->lvl) | |
3297 flp->lvl_next = 0; | |
3298 else | |
3299 flp->lvl_next = flp->lvl - n; | |
3300 flp->end = flp->lvl_next + 1; | |
3301 } | |
3302 break; | |
3303 | |
3304 /* ">1", ">2", .. : start a fold with a certain level */ | |
3305 case '>': flp->lvl = n; | |
3306 flp->lvl_next = n; | |
3307 flp->start = 1; | |
3308 break; | |
3309 | |
3310 /* "<1", "<2", .. : end a fold with a certain level */ | |
3311 case '<': flp->lvl_next = n - 1; | |
3312 flp->end = n; | |
3313 break; | |
3314 | |
3315 /* "=": No change in level */ | |
3316 case '=': flp->lvl_next = flp->lvl; | |
3317 break; | |
3318 | |
3319 /* "-1", "0", "1", ..: set fold level */ | |
3320 default: if (n < 0) | |
3321 /* Use the current level for the next line, so that "a1" | |
3322 * will work there. */ | |
3323 flp->lvl_next = flp->lvl; | |
3324 else | |
3325 flp->lvl_next = n; | |
3326 flp->lvl = n; | |
3327 break; | |
3328 } | |
3329 | |
3330 /* If the level is unknown for the first or the last line in the file, use | |
3331 * level 0. */ | |
3332 if (flp->lvl < 0) | |
3333 { | |
3334 if (lnum <= 1) | |
3335 { | |
3336 flp->lvl = 0; | |
3337 flp->lvl_next = 0; | |
3338 } | |
3339 if (lnum == curbuf->b_ml.ml_line_count) | |
3340 flp->lvl_next = 0; | |
3341 } | |
3342 | |
3343 curwin = win; | |
3344 curbuf = curwin->w_buffer; | |
3345 #endif | |
3346 } | |
3347 | |
3348 /* parseMarker() {{{2 */ | |
3349 /* | |
3350 * Parse 'foldmarker' and set "foldendmarker", "foldstartmarkerlen" and | |
3351 * "foldendmarkerlen". | |
3352 * Relies on the option value to have been checked for correctness already. | |
3353 */ | |
3354 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3355 parseMarker(win_T *wp) |
7 | 3356 { |
3357 foldendmarker = vim_strchr(wp->w_p_fmr, ','); | |
3358 foldstartmarkerlen = (int)(foldendmarker++ - wp->w_p_fmr); | |
3359 foldendmarkerlen = (int)STRLEN(foldendmarker); | |
3360 } | |
3361 | |
3362 /* foldlevelMarker() {{{2 */ | |
3363 /* | |
3364 * Low level function to get the foldlevel for the "marker" method. | |
3365 * "foldendmarker", "foldstartmarkerlen" and "foldendmarkerlen" must have been | |
3366 * set before calling this. | |
3367 * Requires that flp->lvl is set to the fold level of the previous line! | |
3368 * Careful: This means you can't call this function twice on the same line. | |
3369 * Doesn't use any caching. | |
3370 * Sets flp->start when a start marker was found. | |
3371 */ | |
3372 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3373 foldlevelMarker(fline_T *flp) |
7 | 3374 { |
3375 char_u *startmarker; | |
3376 int cstart; | |
3377 int cend; | |
3378 int start_lvl = flp->lvl; | |
3379 char_u *s; | |
3380 int n; | |
3381 | |
3382 /* cache a few values for speed */ | |
3383 startmarker = flp->wp->w_p_fmr; | |
3384 cstart = *startmarker; | |
3385 ++startmarker; | |
3386 cend = *foldendmarker; | |
3387 | |
3388 /* Default: no start found, next level is same as current level */ | |
3389 flp->start = 0; | |
3390 flp->lvl_next = flp->lvl; | |
3391 | |
3392 s = ml_get_buf(flp->wp->w_buffer, flp->lnum + flp->off, FALSE); | |
3393 while (*s) | |
3394 { | |
3395 if (*s == cstart | |
3396 && STRNCMP(s + 1, startmarker, foldstartmarkerlen - 1) == 0) | |
3397 { | |
3398 /* found startmarker: set flp->lvl */ | |
3399 s += foldstartmarkerlen; | |
3400 if (VIM_ISDIGIT(*s)) | |
3401 { | |
3402 n = atoi((char *)s); | |
3403 if (n > 0) | |
3404 { | |
3405 flp->lvl = n; | |
3406 flp->lvl_next = n; | |
3407 if (n <= start_lvl) | |
3408 flp->start = 1; | |
3409 else | |
3410 flp->start = n - start_lvl; | |
3411 } | |
3412 } | |
3413 else | |
3414 { | |
3415 ++flp->lvl; | |
3416 ++flp->lvl_next; | |
3417 ++flp->start; | |
3418 } | |
3419 } | |
3420 else if (*s == cend | |
3421 && STRNCMP(s + 1, foldendmarker + 1, foldendmarkerlen - 1) == 0) | |
3422 { | |
3423 /* found endmarker: set flp->lvl_next */ | |
3424 s += foldendmarkerlen; | |
3425 if (VIM_ISDIGIT(*s)) | |
3426 { | |
3427 n = atoi((char *)s); | |
3428 if (n > 0) | |
3429 { | |
3430 flp->lvl = n; | |
3431 flp->lvl_next = n - 1; | |
3432 /* never start a fold with an end marker */ | |
2054
79b8d69a4588
updated for version 7.2.340
Bram Moolenaar <bram@zimbu.org>
parents:
1985
diff
changeset
|
3433 if (flp->lvl_next > start_lvl) |
79b8d69a4588
updated for version 7.2.340
Bram Moolenaar <bram@zimbu.org>
parents:
1985
diff
changeset
|
3434 flp->lvl_next = start_lvl; |
7 | 3435 } |
3436 } | |
3437 else | |
3438 --flp->lvl_next; | |
3439 } | |
3440 else | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
3441 MB_PTR_ADV(s); |
7 | 3442 } |
3443 | |
3444 /* The level can't go negative, must be missing a start marker. */ | |
3445 if (flp->lvl_next < 0) | |
3446 flp->lvl_next = 0; | |
3447 } | |
3448 | |
3449 /* foldlevelSyntax() {{{2 */ | |
3450 /* | |
3451 * Low level function to get the foldlevel for the "syntax" method. | |
3452 * Doesn't use any caching. | |
3453 */ | |
3454 static void | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3455 foldlevelSyntax(fline_T *flp) |
7 | 3456 { |
3457 #ifndef FEAT_SYN_HL | |
3458 flp->start = 0; | |
3459 flp->lvl = 0; | |
3460 #else | |
3461 linenr_T lnum = flp->lnum + flp->off; | |
3462 int n; | |
3463 | |
3464 /* Use the maximum fold level at the start of this line and the next. */ | |
3465 flp->lvl = syn_get_foldlevel(flp->wp, lnum); | |
3466 flp->start = 0; | |
3467 if (lnum < flp->wp->w_buffer->b_ml.ml_line_count) | |
3468 { | |
3469 n = syn_get_foldlevel(flp->wp, lnum + 1); | |
3470 if (n > flp->lvl) | |
3471 { | |
3472 flp->start = n - flp->lvl; /* fold(s) start here */ | |
3473 flp->lvl = n; | |
3474 } | |
3475 } | |
3476 #endif | |
3477 } | |
3478 | |
3479 /* functions for storing the fold state in a View {{{1 */ | |
3480 /* put_folds() {{{2 */ | |
3481 #if defined(FEAT_SESSION) || defined(PROTO) | |
7801
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3482 static int put_folds_recurse(FILE *fd, garray_T *gap, linenr_T off); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3483 static int put_foldopen_recurse(FILE *fd, win_T *wp, garray_T *gap, linenr_T off); |
a1e71a01dbd6
commit https://github.com/vim/vim/commit/d25c16e2f2776d50245bf31d6e4d5364f12d188e
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
3484 static int put_fold_open_close(FILE *fd, fold_T *fp, linenr_T off); |
7 | 3485 |
3486 /* | |
3487 * Write commands to "fd" to restore the manual folds in window "wp". | |
3488 * Return FAIL if writing fails. | |
3489 */ | |
3490 int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3491 put_folds(FILE *fd, win_T *wp) |
7 | 3492 { |
3493 if (foldmethodIsManual(wp)) | |
3494 { | |
3495 if (put_line(fd, "silent! normal! zE") == FAIL | |
3496 || put_folds_recurse(fd, &wp->w_folds, (linenr_T)0) == FAIL) | |
3497 return FAIL; | |
3498 } | |
3499 | |
3500 /* If some folds are manually opened/closed, need to restore that. */ | |
3501 if (wp->w_fold_manual) | |
3392 | 3502 return put_foldopen_recurse(fd, wp, &wp->w_folds, (linenr_T)0); |
7 | 3503 |
3504 return OK; | |
3505 } | |
3506 | |
3507 /* put_folds_recurse() {{{2 */ | |
3508 /* | |
3509 * Write commands to "fd" to recreate manually created folds. | |
3510 * Returns FAIL when writing failed. | |
3511 */ | |
3512 static int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3513 put_folds_recurse(FILE *fd, garray_T *gap, linenr_T off) |
7 | 3514 { |
3515 int i; | |
3516 fold_T *fp; | |
3517 | |
3518 fp = (fold_T *)gap->ga_data; | |
3519 for (i = 0; i < gap->ga_len; i++) | |
3520 { | |
3521 /* Do nested folds first, they will be created closed. */ | |
3522 if (put_folds_recurse(fd, &fp->fd_nested, off + fp->fd_top) == FAIL) | |
3523 return FAIL; | |
3524 if (fprintf(fd, "%ld,%ldfold", fp->fd_top + off, | |
3525 fp->fd_top + off + fp->fd_len - 1) < 0 | |
3526 || put_eol(fd) == FAIL) | |
3527 return FAIL; | |
3528 ++fp; | |
3529 } | |
3530 return OK; | |
3531 } | |
3532 | |
3533 /* put_foldopen_recurse() {{{2 */ | |
3534 /* | |
3535 * Write commands to "fd" to open and close manually opened/closed folds. | |
3536 * Returns FAIL when writing failed. | |
3537 */ | |
3538 static int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3539 put_foldopen_recurse( |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3540 FILE *fd, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3541 win_T *wp, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3542 garray_T *gap, |
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3543 linenr_T off) |
7 | 3544 { |
3545 int i; | |
3392 | 3546 int level; |
7 | 3547 fold_T *fp; |
3548 | |
3549 fp = (fold_T *)gap->ga_data; | |
3550 for (i = 0; i < gap->ga_len; i++) | |
3551 { | |
3552 if (fp->fd_flags != FD_LEVEL) | |
3553 { | |
3554 if (fp->fd_nested.ga_len > 0) | |
3555 { | |
3392 | 3556 /* open nested folds while this fold is open */ |
7 | 3557 if (fprintf(fd, "%ld", fp->fd_top + off) < 0 |
3558 || put_eol(fd) == FAIL | |
3766 | 3559 || put_line(fd, "normal! zo") == FAIL) |
7 | 3560 return FAIL; |
3392 | 3561 if (put_foldopen_recurse(fd, wp, &fp->fd_nested, |
3562 off + fp->fd_top) | |
7 | 3563 == FAIL) |
3564 return FAIL; | |
3392 | 3565 /* close the parent when needed */ |
3566 if (fp->fd_flags == FD_CLOSED) | |
3567 { | |
3568 if (put_fold_open_close(fd, fp, off) == FAIL) | |
3569 return FAIL; | |
3570 } | |
3571 } | |
3572 else | |
3573 { | |
3574 /* Open or close the leaf according to the window foldlevel. | |
3575 * Do not close a leaf that is already closed, as it will close | |
3576 * the parent. */ | |
3577 level = foldLevelWin(wp, off + fp->fd_top); | |
3578 if ((fp->fd_flags == FD_CLOSED && wp->w_p_fdl >= level) | |
3579 || (fp->fd_flags != FD_CLOSED && wp->w_p_fdl < level)) | |
3580 if (put_fold_open_close(fd, fp, off) == FAIL) | |
3581 return FAIL; | |
7 | 3582 } |
3583 } | |
3584 ++fp; | |
3585 } | |
3586 | |
3587 return OK; | |
3588 } | |
3392 | 3589 |
3590 /* put_fold_open_close() {{{2 */ | |
3591 /* | |
3592 * Write the open or close command to "fd". | |
3593 * Returns FAIL when writing failed. | |
3594 */ | |
3595 static int | |
7821
81794242a275
commit https://github.com/vim/vim/commit/66f948e928d5e0cd3123af902aa8ac1613534c94
Christian Brabandt <cb@256bit.org>
parents:
7807
diff
changeset
|
3596 put_fold_open_close(FILE *fd, fold_T *fp, linenr_T off) |
3392 | 3597 { |
3598 if (fprintf(fd, "%ld", fp->fd_top + off) < 0 | |
3599 || put_eol(fd) == FAIL | |
3766 | 3600 || fprintf(fd, "normal! z%c", |
3392 | 3601 fp->fd_flags == FD_CLOSED ? 'c' : 'o') < 0 |
3602 || put_eol(fd) == FAIL) | |
3603 return FAIL; | |
3604 | |
3605 return OK; | |
3606 } | |
7 | 3607 #endif /* FEAT_SESSION */ |
3608 | |
3609 /* }}}1 */ | |
3610 #endif /* defined(FEAT_FOLDING) || defined(PROTO) */ |