blob: 6af71783b979407a58c6bee761007727ae33a1ba [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 * 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 */
27typedef 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 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +010046static void newFoldLevelWin(win_T *wp);
47static int checkCloseRec(garray_T *gap, linenr_T lnum, int level);
48static int foldFind(garray_T *gap, linenr_T lnum, fold_T **fpp);
49static int foldLevelWin(win_T *wp, linenr_T lnum);
50static void checkupdate(win_T *wp);
51static void setFoldRepeat(linenr_T lnum, long count, int do_open);
52static linenr_T setManualFold(linenr_T lnum, int opening, int recurse, int *donep);
53static linenr_T setManualFoldWin(win_T *wp, linenr_T lnum, int opening, int recurse, int *donep);
54static void foldOpenNested(fold_T *fpr);
55static void deleteFoldEntry(garray_T *gap, int idx, int recursive);
56static void foldMarkAdjustRecurse(garray_T *gap, linenr_T line1, linenr_T line2, long amount, long amount_after);
57static int getDeepestNestingRecurse(garray_T *gap);
58static int check_closed(win_T *win, fold_T *fp, int *use_levelp, int level, int *maybe_smallp, linenr_T lnum_off);
59static void checkSmall(win_T *wp, fold_T *fp, linenr_T lnum_off);
60static void setSmallMaybe(garray_T *gap);
61static void foldCreateMarkers(linenr_T start, linenr_T end);
62static void foldAddMarker(linenr_T lnum, char_u *marker, int markerlen);
63static void deleteFoldMarkers(fold_T *fp, int recursive, linenr_T lnum_off);
64static void foldDelMarker(linenr_T lnum, char_u *marker, int markerlen);
65static void foldUpdateIEMS(win_T *wp, linenr_T top, linenr_T bot);
66static void parseMarker(win_T *wp);
Bram Moolenaar88d298a2017-03-14 21:53:58 +010067static void foldMoveRange_int(garray_T *gap, linenr_T line1, linenr_T line2, linenr_T dest);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
69static 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 */
75static linenr_T invalid_top = (linenr_T)0;
76static 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 */
85static linenr_T prev_lnum = 0;
86static 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
93static int foldstartmarkerlen;
94static char_u *foldendmarker;
95static 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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100104copyFoldingState(win_T *wp_from, win_T *wp_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100117hasAnyFolding(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100132hasFolding(linenr_T lnum, linenr_T *firstp, linenr_T *lastp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133{
134 return hasFoldingWin(curwin, lnum, firstp, lastp, TRUE, NULL);
135}
136
137/* hasFoldingWin() {{{2 */
138 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100139hasFoldingWin(
140 win_T *win,
141 linenr_T lnum,
142 linenr_T *firstp,
143 linenr_T *lastp,
144 int cache, /* when TRUE: use cached values of window */
145 foldinfo_T *infop) /* where to store fold info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146{
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;
Bram Moolenaar945ec092016-06-08 21:17:43 +0200157 int low_level = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158
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
Bram Moolenaar05b20fb2015-04-13 20:52:36 +0200232 if (last > win->w_buffer->b_ml.ml_line_count)
233 last = win->w_buffer->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100252foldLevel(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100278lineFolded(win_T *win, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100293foldedCount(win_T *win, linenr_T lnum, foldinfo_T *infop)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100307foldmethodIsManual(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308{
309 return (wp->w_p_fdm[3] == 'u');
310}
311
312/* foldmethodIsIndent() {{{2 */
313/*
314 * Return TRUE if 'foldmethod' is "indent"
315 */
316 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100317foldmethodIsIndent(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318{
319 return (wp->w_p_fdm[0] == 'i');
320}
321
322/* foldmethodIsExpr() {{{2 */
323/*
324 * Return TRUE if 'foldmethod' is "expr"
325 */
326 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100327foldmethodIsExpr(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328{
329 return (wp->w_p_fdm[1] == 'x');
330}
331
332/* foldmethodIsMarker() {{{2 */
333/*
334 * Return TRUE if 'foldmethod' is "marker"
335 */
336 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100337foldmethodIsMarker(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338{
339 return (wp->w_p_fdm[2] == 'r');
340}
341
342/* foldmethodIsSyntax() {{{2 */
343/*
344 * Return TRUE if 'foldmethod' is "syntax"
345 */
346 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100347foldmethodIsSyntax(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348{
349 return (wp->w_p_fdm[0] == 's');
350}
351
352/* foldmethodIsDiff() {{{2 */
353/*
354 * Return TRUE if 'foldmethod' is "diff"
355 */
356 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100357foldmethodIsDiff(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100368closeFold(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369{
370 setFoldRepeat(lnum, count, FALSE);
371}
372
373/* closeFoldRecurse() {{{2 */
374/*
375 * Close fold for current window at line "lnum" recursively.
376 */
377 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100378closeFoldRecurse(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100389opFoldRange(
390 linenr_T first,
391 linenr_T last,
392 int opening, /* TRUE to open, FALSE to close */
393 int recurse, /* TRUE to do it recursively */
394 int had_visual) /* TRUE when Visual selection used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395{
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));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 /* Force a redraw to remove the Visual highlighting. */
416 if (had_visual)
417 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418}
419
420/* openFold() {{{2 */
421/*
422 * Open fold for current window at line "lnum".
423 * Repeat "count" times.
424 */
425 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100426openFold(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427{
428 setFoldRepeat(lnum, count, TRUE);
429}
430
431/* openFoldRecurse() {{{2 */
432/*
433 * Open fold for current window at line "lnum" recursively.
434 */
435 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100436openFoldRecurse(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100446foldOpenCursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100466newFoldLevel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100491newFoldLevelWin(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100515foldCheckClose(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100528checkCloseRec(garray_T *gap, linenr_T lnum, int level)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100560foldManualAllowed(int create)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100577foldCreate(linenr_T start, linenr_T end)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578{
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;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 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;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100702deleteFold(
703 linenr_T start,
704 linenr_T end,
705 int recursive,
706 int had_visual) /* TRUE when Visual selection used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707{
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;
Bram Moolenaar238b8e22009-01-06 14:02:45 +0000713 int use_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 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;
Bram Moolenaar238b8e22009-01-06 14:02:45 +0000730 use_level = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 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;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758 if (foldmethodIsManual(curwin))
759 deleteFoldEntry(found_ga,
760 (int)(found_fp - (fold_T *)found_ga->ga_data), recursive);
761 else
762 {
Bram Moolenaar238b8e22009-01-06 14:02:45 +0000763 if (first_lnum > found_fp->fd_top + found_off)
764 first_lnum = found_fp->fd_top + found_off;
765 if (last_lnum < lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 last_lnum = lnum;
Bram Moolenaar238b8e22009-01-06 14:02:45 +0000767 if (!did_one)
768 parseMarker(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 deleteFoldMarkers(found_fp, recursive, found_off);
770 }
Bram Moolenaar238b8e22009-01-06 14:02:45 +0000771 did_one = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
773 /* redraw window */
774 changed_window_setting();
775 }
776 }
777 if (!did_one)
778 {
779 EMSG(_(e_nofold));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 /* Force a redraw to remove the Visual highlighting. */
781 if (had_visual)
782 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
Bram Moolenaar238b8e22009-01-06 14:02:45 +0000784 else
785 /* Deleting markers may make cursor column invalid. */
786 check_cursor_col();
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100797clearFolding(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100811foldUpdate(win_T *wp, linenr_T top, linenr_T bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
813 fold_T *fp;
814
Bram Moolenaar429fcfb2016-04-14 16:22:04 +0200815 if (disable_fold_update > 0)
816 return;
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 /* Mark all folds from top to bot as maybe-small. */
Bram Moolenaard5f69332015-04-15 12:43:50 +0200819 (void)foldFind(&wp->w_folds, top, &fp);
820 while (fp < (fold_T *)wp->w_folds.ga_data + wp->w_folds.ga_len
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 && 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))
Bram Moolenaarfa6d5af2007-10-14 13:33:20 +0000834 {
835 int save_got_int = got_int;
836
837 /* reset got_int here, otherwise it won't work */
838 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 foldUpdateIEMS(wp, top, bot);
Bram Moolenaarfa6d5af2007-10-14 13:33:20 +0000840 got_int |= save_got_int;
841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100852foldUpdateAll(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100865foldMoveTo(
866 int updown,
867 int dir, /* FORWARD or BACKWARD */
868 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869{
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
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000882 checkupdate(curwin);
883
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 /* 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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001002foldInitWin(win_T *new_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001004 ga_init2(&new_win->w_folds, (int)sizeof(fold_T), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005}
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001015find_wl_entry(win_T *win, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016{
1017 int i;
1018
Bram Moolenaar08bb82e2010-03-17 16:45:12 +01001019 for (i = 0; i < win->w_lines_valid; ++i)
1020 if (win->w_lines[i].wl_valid)
1021 {
1022 if (lnum < win->w_lines[i].wl_lnum)
1023 return -1;
1024 if (lnum <= win->w_lines[i].wl_lastlnum)
1025 return i;
1026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 return -1;
1028}
1029
1030/* foldAdjustVisual() {{{2 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031/*
1032 * Adjust the Visual area to include any fold at the start or end completely.
1033 */
1034 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001035foldAdjustVisual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
1037 pos_T *start, *end;
1038 char_u *ptr;
1039
1040 if (!VIsual_active || !hasAnyFolding(curwin))
1041 return;
1042
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001043 if (LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
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}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
1069/* cursor_foldstart() {{{2 */
1070/*
1071 * Move the cursor to the first line of a closed fold.
1072 */
1073 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001074foldAdjustCursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075{
1076 (void)hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL);
1077}
1078
Bram Moolenaar88d298a2017-03-14 21:53:58 +01001079/* foldMoveRange() {{{2 */
1080 void
1081foldMoveRange(garray_T *gap, linenr_T line1, linenr_T line2, linenr_T dest)
1082{
1083 foldMoveRange_int(gap, line1, line2, dest);
1084}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085/* 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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001093cloneFoldGrowArray(garray_T *from, garray_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 ++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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001127foldFind(garray_T *gap, linenr_T lnum, fold_T **fpp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001166foldLevelWin(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001193checkupdate(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001208setFoldRepeat(linenr_T lnum, long count, int do_open)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209{
1210 int done;
1211 long n;
1212
1213 for (n = 0; n < count; ++n)
1214 {
1215 done = DONE_NOTHING;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001216 (void)setManualFold(lnum, do_open, FALSE, &done);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001233setManualFold(
1234 linenr_T lnum,
1235 int opening, /* TRUE when opening, FALSE when closing */
1236 int recurse, /* TRUE when closing/opening recursive */
1237 int *donep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001275setManualFoldWin(
1276 win_T *wp,
1277 linenr_T lnum,
1278 int opening, /* TRUE when opening, FALSE when closing */
1279 int recurse, /* TRUE when closing/opening recursive */
1280 int *donep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001386foldOpenNested(fold_T *fpr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001406deleteFoldEntry(garray_T *gap, int idx, int recursive)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 if (idx < gap->ga_len)
1420 mch_memmove(fp, fp + 1, sizeof(fold_T) * (gap->ga_len - idx));
1421 }
1422 else
1423 {
Bram Moolenaarc9927c12011-08-26 16:13:00 +02001424 /* Move nested folds one level up, to overwrite the fold that is
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 * deleted. */
1426 moved = fp->fd_nested.ga_len;
1427 if (ga_grow(gap, (int)(moved - 1)) == OK)
1428 {
Bram Moolenaarc9927c12011-08-26 16:13:00 +02001429 /* Get "fp" again, the array may have been reallocated. */
1430 fp = (fold_T *)gap->ga_data + idx;
1431
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 /* 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 */
Bram Moolenaarc9927c12011-08-26 16:13:00 +02001444 if (idx + 1 < gap->ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 mch_memmove(fp + moved, fp + 1,
Bram Moolenaarc9927c12011-08-26 16:13:00 +02001446 sizeof(fold_T) * (gap->ga_len - (idx + 1)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 /* 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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452 }
1453}
1454
1455/* deleteFoldRecurse() {{{2 */
1456/*
1457 * Delete nested folds in a fold.
1458 */
1459 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001460deleteFoldRecurse(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001474foldMarkAdjust(
1475 win_T *wp,
1476 linenr_T line1,
1477 linenr_T line2,
1478 long amount,
1479 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001494foldMarkAdjustRecurse(
1495 garray_T *gap,
1496 linenr_T line1,
1497 linenr_T line2,
1498 long amount,
1499 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500{
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 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 if (fp->fd_top < top)
1564 {
Bram Moolenaar194b94c2009-09-18 13:17:09 +00001565 /* 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);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 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 {
Bram Moolenaar194b94c2009-09-18 13:17:09 +00001584 /* 5. fold is below line1 and contains line2; need to
1585 * correct nested folds too */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 if (amount == MAXLNUM)
1587 {
Bram Moolenaar1159b162017-02-28 21:53:56 +01001588 foldMarkAdjustRecurse(&fp->fd_nested,
1589 line1 - fp->fd_top,
1590 line2 - fp->fd_top,
1591 amount,
1592 amount_after + (fp->fd_top - top));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 fp->fd_len -= line2 - fp->fd_top + 1;
1594 fp->fd_top = line1;
1595 }
1596 else
1597 {
Bram Moolenaar1159b162017-02-28 21:53:56 +01001598 foldMarkAdjustRecurse(&fp->fd_nested,
1599 line1 - fp->fd_top,
1600 line2 - fp->fd_top,
1601 amount,
1602 amount_after - amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001618getDeepestNesting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
1620 checkupdate(curwin);
1621 return getDeepestNestingRecurse(&curwin->w_folds);
1622}
1623
1624 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001625getDeepestNestingRecurse(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001648check_closed(
1649 win_T *win,
1650 fold_T *fp,
1651 int *use_levelp, /* TRUE: outer fold had FD_LEVEL */
1652 int level, /* folding depth */
1653 int *maybe_smallp, /* TRUE: outer this had fd_small == MAYBE */
1654 linenr_T lnum_off) /* line number offset for fp->fd_top */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001688checkSmall(
1689 win_T *wp,
1690 fold_T *fp,
1691 linenr_T lnum_off) /* offset for fp->fd_top */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001725setSmallMaybe(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001741foldCreateMarkers(linenr_T start, linenr_T end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001763foldAddMarker(linenr_T lnum, char_u *marker, int markerlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
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");
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001770 int line_is_comment = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
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 {
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001778#if defined(FEAT_COMMENTS)
1779 /* Check if the line ends with an unclosed comment */
1780 (void)skip_comment(line, FALSE, FALSE, &line_is_comment);
1781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 newline = alloc((unsigned)(line_len + markerlen + STRLEN(cms) + 1));
1783 if (newline == NULL)
1784 return;
1785 STRCPY(newline, line);
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001786 /* Append the marker to the end of the line */
1787 if (p == NULL || line_is_comment)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001788 vim_strncpy(newline + line_len, marker, markerlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001805deleteFoldMarkers(
1806 fold_T *fp,
1807 int recursive,
1808 linenr_T lnum_off) /* offset for fp->fd_top */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001829foldDelMarker(linenr_T lnum, char_u *marker, int markerlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830{
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
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001873/* get_foldtext() {{{2 */
1874/*
1875 * Return the text for a closed fold at line "lnum", with last line "lnume".
Bram Moolenaaree695f72016-08-03 22:08:45 +02001876 * When 'foldtext' isn't set puts the result in "buf[FOLD_TEXT_LEN]".
1877 * Otherwise the result is in allocated memory.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001878 */
1879 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001880get_foldtext(
1881 win_T *wp,
1882 linenr_T lnum,
1883 linenr_T lnume,
1884 foldinfo_T *foldinfo,
1885 char_u *buf)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001886{
1887 char_u *text = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001888#ifdef FEAT_EVAL
Bram Moolenaardab38d52013-06-15 17:06:36 +02001889 /* an error occurred when evaluating 'fdt' setting */
1890 static int got_fdt_error = FALSE;
1891 int save_did_emsg = did_emsg;
1892 static win_T *last_wp = NULL;
1893 static linenr_T last_lnum = 0;
1894
1895 if (last_wp != wp || last_wp == NULL
1896 || last_lnum > lnum || last_lnum == 0)
1897 /* window changed, try evaluating foldtext setting once again */
1898 got_fdt_error = FALSE;
1899
1900 if (!got_fdt_error)
1901 /* a previous error should not abort evaluating 'foldexpr' */
1902 did_emsg = FALSE;
1903
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001904 if (*wp->w_p_fdt != NUL)
1905 {
Bram Moolenaar5b88ba42009-11-03 15:30:12 +00001906 char_u dashes[MAX_LEVEL + 2];
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001907 win_T *save_curwin;
1908 int level;
1909 char_u *p;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001910
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;
Bram Moolenaar5b88ba42009-11-03 15:30:12 +00001918 if (level > (int)sizeof(dashes) - 1)
1919 level = (int)sizeof(dashes) - 1;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001920 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);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001924
Bram Moolenaardab38d52013-06-15 17:06:36 +02001925 /* skip evaluating foldtext on errors */
1926 if (!got_fdt_error)
1927 {
1928 save_curwin = curwin;
1929 curwin = wp;
1930 curbuf = wp->w_buffer;
1931
1932 ++emsg_silent; /* handle exceptions, but don't display errors */
1933 text = eval_to_string_safe(wp->w_p_fdt, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001934 was_set_insecurely((char_u *)"foldtext", OPT_LOCAL));
Bram Moolenaardab38d52013-06-15 17:06:36 +02001935 --emsg_silent;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001936
Bram Moolenaardab38d52013-06-15 17:06:36 +02001937 if (text == NULL || did_emsg)
1938 got_fdt_error = TRUE;
1939
1940 curwin = save_curwin;
1941 curbuf = curwin->w_buffer;
1942 }
1943 last_lnum = lnum;
1944 last_wp = wp;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001945 set_vim_var_string(VV_FOLDDASHES, NULL, -1);
1946
Bram Moolenaardab38d52013-06-15 17:06:36 +02001947 if (!did_emsg && save_did_emsg)
1948 did_emsg = save_did_emsg;
1949
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001950 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
Bram Moolenaar009b2592004-10-24 19:18:58 +00001957 int len;
1958
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001959 if (has_mbyte && (len = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001960 {
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 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02001983 long count = (long)(lnume - lnum + 1);
1984
1985 vim_snprintf((char *)buf, FOLD_TEXT_LEN,
Bram Moolenaar1c465442017-03-12 20:10:05 +01001986 NGETTEXT("+--%3ld line folded ",
Bram Moolenaaree695f72016-08-03 22:08:45 +02001987 "+--%3ld lines folded ", count),
1988 count);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001989 text = buf;
1990 }
1991 return text;
1992}
1993
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994/* foldtext_cleanup() {{{2 */
1995/*
1996 * Remove 'foldmarker' and 'commentstring' from "str" (in-place).
1997 */
1998 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001999foldtext_cleanup(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000{
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;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002006 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 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);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002013 cms_slen = (int)STRLEN(cms_start);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002014 while (cms_slen > 0 && VIM_ISWHITE(cms_start[cms_slen - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 --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 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002021 cms_elen = cms_slen - (int)(cms_end - cms_start);
2022 cms_slen = (int)(cms_end - cms_start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023
2024 /* exclude white space before "%s" */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002025 while (cms_slen > 0 && VIM_ISWHITE(cms_start[cms_slen - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 --cms_slen;
2027
2028 /* skip "%s" and white space after it */
2029 s = skipwhite(cms_end + 2);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002030 cms_elen -= (int)(s - cms_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 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)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 len = foldstartmarkerlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 else if (STRNCMP(s, foldendmarker, foldendmarkerlen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 len = foldendmarkerlen;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002042 if (len > 0)
2043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 if (VIM_ISDIGIT(s[len]))
2045 ++len;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002046
2047 /* May remove 'commentstring' start. Useful when it's a double
2048 * quote and we already removed a double quote. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002049 for (p = s; p > str && VIM_ISWHITE(p[-1]); --p)
Bram Moolenaardef9e822004-12-31 20:58:58 +00002050 ;
2051 if (p >= str + cms_slen
2052 && STRNCMP(p - cms_slen, cms_start, cms_slen) == 0)
2053 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002054 len += (int)(s - p) + cms_slen;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002055 s = p - cms_slen;
2056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
2058 else if (cms_end != NULL)
2059 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002060 if (!did1 && cms_slen > 0 && STRNCMP(s, cms_start, cms_slen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 {
2062 len = cms_slen;
2063 did1 = TRUE;
2064 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00002065 else if (!did2 && cms_elen > 0
2066 && STRNCMP(s, cms_end, cms_elen) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 {
2068 len = cms_elen;
2069 did2 = TRUE;
2070 }
2071 }
2072 if (len != 0)
2073 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002074 while (VIM_ISWHITE(s[len]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 ++len;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002076 STRMOVE(s, s + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 }
2078 else
2079 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002080 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
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 */
2087typedef 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. */
2104static int fold_changed;
2105
2106/* Function declarations. {{{2 */
Bram Moolenaard99df422016-01-29 23:20:40 +01002107static linenr_T foldUpdateIEMSRecurse(garray_T *gap, int level, linenr_T startlnum, fline_T *flp, void (*getlevel)(fline_T *), linenr_T bot, int topflags);
Bram Moolenaard25c16e2016-01-29 22:13:30 +01002108static int foldInsert(garray_T *gap, int i);
2109static void foldSplit(garray_T *gap, int i, linenr_T top, linenr_T bot);
2110static void foldRemove(garray_T *gap, linenr_T top, linenr_T bot);
2111static void foldMerge(fold_T *fp1, garray_T *gap, fold_T *fp2);
2112static void foldlevelIndent(fline_T *flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113#ifdef FEAT_DIFF
Bram Moolenaard25c16e2016-01-29 22:13:30 +01002114static void foldlevelDiff(fline_T *flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +01002116static void foldlevelExpr(fline_T *flp);
2117static void foldlevelMarker(fline_T *flp);
2118static void foldlevelSyntax(fline_T *flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002126foldUpdateIEMS(win_T *wp, linenr_T top, linenr_T bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
2128 linenr_T start;
2129 linenr_T end;
2130 fline_T fline;
Bram Moolenaard99df422016-01-29 23:20:40 +01002131 void (*getlevel)(fline_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 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
Bram Moolenaarec986472009-11-03 13:46:54 +00002243 /*
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;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002253 fold_T *fpn = NULL;
Bram Moolenaarec986472009-11-03 13:46:54 +00002254 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 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002260 if (!foldFind(gap, lnum_rel, &fpn))
Bram Moolenaarec986472009-11-03 13:46:54 +00002261 break;
2262 ++current_fdl;
2263
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002264 fold_start_lnum += fpn->fd_top;
2265 gap = &fpn->fd_nested;
2266 lnum_rel -= fpn->fd_top;
Bram Moolenaarec986472009-11-03 13:46:54 +00002267 }
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002268 if (fpn != NULL && current_fdl == fline.lvl)
Bram Moolenaarec986472009-11-03 13:46:54 +00002269 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002270 linenr_T fold_end_lnum = fold_start_lnum + fpn->fd_len;
Bram Moolenaarec986472009-11-03 13:46:54 +00002271
2272 if (fold_end_lnum > bot)
2273 bot = fold_end_lnum;
2274 }
2275 }
2276
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 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)
Bram Moolenaare3b3f282008-03-06 21:45:49 +00002338 changed_window_setting_win(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
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
Bram Moolenaare37d50a2008-08-06 17:06:04 +00002372 * and then created again. But we would lose all information about the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 * 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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002379foldUpdateIEMSRecurse(
2380 garray_T *gap,
2381 int level,
2382 linenr_T startlnum,
2383 fline_T *flp,
2384 void (*getlevel)(fline_T *),
2385 linenr_T bot,
2386 int topflags) /* flags used by containing fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387{
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 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002408 (void)foldFind(gap, startlnum - 1, &fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 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 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002470 (void)foldFind(&fp->fd_nested, flp->lnum - fp->fd_top, &fp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 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. */
Bram Moolenaareadbc2b2017-03-04 18:42:39 +01002521 if (fp->fd_top == firstlnum)
2522 {
2523 /* have found a fold beginning where we want */
2524 }
2525 else if (fp->fd_top >= startlnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
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 }
Bram Moolenaareadbc2b2017-03-04 18:42:39 +01002543 else if ((flp->start != 0 && lvl == level)
2544 || firstlnum != startlnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
Bram Moolenaareadbc2b2017-03-04 18:42:39 +01002546 linenr_T breakstart;
2547 linenr_T breakend;
2548
2549 /*
2550 * Before there was a fold spanning from above
2551 * startlnum to below firstlnum. This fold is valid
2552 * above startlnum (because we are not updating
2553 * that range), but there should now be a break in
2554 * it.
2555 * If the break is because we are now forced to
2556 * start a new fold at the level "level" at line
2557 * fline->lnum, then we need to split the fold at
2558 * fline->lnum.
2559 * If the break is because the range
2560 * [startlnum, firstlnum) is now at a lower indent
2561 * than "level", we need to split the fold in this
2562 * range.
2563 * Any splits have to be done recursively.
2564 */
2565 if (firstlnum != startlnum)
2566 {
2567 breakstart = startlnum;
2568 breakend = firstlnum;
2569 }
2570 else
2571 {
2572 breakstart = flp->lnum;
2573 breakend = flp->lnum;
2574 }
2575 foldRemove(&fp->fd_nested, breakstart - fp->fd_top,
2576 breakend - fp->fd_top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 i = (int)(fp - (fold_T *)gap->ga_data);
Bram Moolenaareadbc2b2017-03-04 18:42:39 +01002578 foldSplit(gap, i, breakstart, breakend - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 fp = (fold_T *)gap->ga_data + i + 1;
Bram Moolenaareadbc2b2017-03-04 18:42:39 +01002580
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 /* 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 }
Bram Moolenaareadbc2b2017-03-04 18:42:39 +01002589
2590 if (fp->fd_top == startlnum && concat)
2591 {
2592 i = (int)(fp - (fold_T *)gap->ga_data);
2593 if (i != 0)
2594 {
2595 fp2 = fp - 1;
2596 if (fp2->fd_top + fp2->fd_len == fp->fd_top)
2597 {
2598 foldMerge(fp2, gap, fp);
2599 fp = fp2;
2600 }
2601 }
2602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 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".
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002676 * Check "fp" for safety.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002678 if (lvl > level && fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 {
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;
Bram Moolenaaref6fc092008-01-13 20:58:13 +00002751 fp->fd_small = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 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 {
Bram Moolenaar25394022007-05-10 19:06:20 +00002767 /* fold continued below bot */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002837foldInsert(garray_T *gap, int i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838{
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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002860foldSplit(
2861 garray_T *gap,
2862 int i,
2863 linenr_T top,
2864 linenr_T bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865{
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;
Bram Moolenaareda6eb92009-11-03 17:04:43 +00002880 fp[1].fd_small = MAYBE;
2881 fp->fd_small = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 gap1->ga_len -= len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
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
Bram Moolenaar25394022007-05-10 19:06:20 +00002917 * 2: truncate to stop above "top"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 * 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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002924foldRemove(garray_T *gap, linenr_T top, linenr_T bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925{
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;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002961 if (fp->fd_top + fp->fd_len - 1 > bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 {
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
Bram Moolenaar88d298a2017-03-14 21:53:58 +01002978/* foldReverseOrder() {{{2 */
2979 static void
2980foldReverseOrder(garray_T *gap, linenr_T start, linenr_T end)
2981{
2982 fold_T *left, *right;
2983 fold_T tmp;
2984
2985 for (; start < end; start++, end--)
2986 {
2987 left = (fold_T *)gap->ga_data + start;
2988 right = (fold_T *)gap->ga_data + end;
2989 tmp = *left;
2990 *left = *right;
2991 *right = tmp;
2992 }
2993}
2994
2995/* foldMoveRange_int() {{{2 */
2996/*
2997 * Move folds within the inclusive range "line1" to "line2" to after "dest"
2998 * requires "line1" <= "line2" <= "dest"
2999 *
3000 * There are the following situations for the first fold at or below line1 - 1.
3001 * 1 2 3 4
3002 * 1 2 3 4
3003 * line1 2 3 4
3004 * 2 3 4 5 6 7
3005 * line2 3 4 5 6 7
3006 * 3 4 6 7 8 9
3007 * dest 4 7 8 9
3008 * 4 7 8 10
3009 * 4 7 8 10
3010 *
3011 * In the following descriptions, "moved" means moving in the buffer, *and* in
3012 * the fold array.
3013 * Meanwhile, "shifted" just means moving in the buffer.
3014 * 1. not changed
3015 * 2. truncated above line1
3016 * 3. length reduced by line2 - line1, folds starting between the end of 3 and
3017 * dest are truncated and shifted up
3018 * 4. internal folds moved (from [line1, line2] to dest)
3019 * 5. moved to dest.
3020 * 6. truncated below line2 and moved.
3021 * 7. length reduced by line2 - dest, folds starting between line2 and dest are
3022 * removed, top is moved down by move_len.
3023 * 8. truncated below dest and shifted up.
3024 * 9. shifted up
3025 * 10. not changed
3026 */
3027
3028 static void
3029truncate_fold(fold_T *fp, linenr_T end)
3030{
3031 foldRemove(&fp->fd_nested, end - fp->fd_top, MAXLNUM);
3032 fp->fd_len = end - fp->fd_top + 1;
3033}
3034
3035#define fold_end(fp) ((fp)->fd_top + (fp)->fd_len - 1)
3036#define valid_fold(fp, gap) ((fp) < ((fold_T *)(gap)->ga_data + (gap)->ga_len))
3037#define fold_index(fp, gap) ((size_t)(fp - ((fold_T *)(gap)->ga_data)))
3038
3039 static void
3040foldMoveRange_int(garray_T *gap, linenr_T line1, linenr_T line2, linenr_T dest)
3041{
3042 fold_T *fp;
3043 linenr_T range_len = line2 - line1 + 1;
3044 linenr_T move_len = dest - line2;
3045 int at_start = foldFind(gap, line1 - 1, &fp);
3046 size_t move_start = 0, move_end = 0, dest_index = 0;
3047
3048 if (at_start)
3049 {
3050 if (fold_end(fp) > dest)
3051 {
3052 /* Case 4
3053 * don't have to change this fold, but have to move nested folds.
3054 */
3055 foldMoveRange(&fp->fd_nested, line1 - fp->fd_top, line2 -
3056 fp->fd_top, dest - fp->fd_top);
3057 return;
3058 }
3059 else if (fold_end(fp) > line2)
3060 {
3061 /* Case 3
3062 * Remove nested folds between line1 and line2 & reduce the
3063 * length of fold by "range_len".
3064 * Folds after this one must be dealt with.
3065 */
3066 foldMarkAdjustRecurse(&fp->fd_nested, line1 - fp->fd_top, line2 -
3067 fp->fd_top, MAXLNUM, -range_len);
3068 fp->fd_len -= range_len;
3069 }
3070 else
3071 /* Case 2 truncate fold, folds after this one must be dealt with. */
3072 truncate_fold(fp, line1);
3073
3074 /* Look at the next fold, and treat that one as if it were the first
3075 * after "line1" (because now it is). */
3076 fp = fp + 1;
3077 }
3078
3079 if (!valid_fold(fp, gap) || fp->fd_top > dest)
3080 {
3081 /* Case 10
3082 * No folds after "line1" and before "dest"
3083 */
3084 return;
3085 }
3086 else if (fp->fd_top > line2)
3087 {
3088 for (; valid_fold(fp, gap) && fold_end(fp) < dest; fp++)
3089 /* Case 9. (for all case 9's) -- shift up. */
3090 fp->fd_top -= range_len;
3091
3092 if (valid_fold(fp, gap) && fp->fd_top < dest)
3093 {
3094 /* Case 8. -- ensure truncated at dest, shift up */
3095 truncate_fold(fp, dest);
3096 fp->fd_top -= range_len;
3097 }
3098 return;
3099 }
3100 else if (fold_end(fp) > dest)
3101 {
3102 /* Case 7 -- remove nested folds and shrink */
3103 foldMarkAdjustRecurse(&fp->fd_nested, line2 + 1 - fp->fd_top, dest -
3104 fp->fd_top, MAXLNUM, -move_len);
3105 fp->fd_len -= move_len;
3106 fp->fd_top += move_len;
3107 return;
3108 }
3109
3110 /* Case 5 or 6
3111 * changes rely on whether there are folds between the end of
3112 * this fold and "dest".
3113 */
3114 move_start = fold_index(fp, gap);
3115
3116 for (; valid_fold(fp, gap) && fp->fd_top <= dest; fp++)
3117 {
3118 if (fp->fd_top <= line2)
3119 {
3120 /* 1. 2. or 3. */
3121 if (fold_end(fp) > line2)
3122 /* 2. or 3., truncate before moving */
3123 truncate_fold(fp, line2);
3124
3125 fp->fd_top += move_len;
3126 continue;
3127 }
3128
3129 /* Record index of the first fold after the moved range. */
3130 if (move_end == 0)
3131 move_end = fold_index(fp, gap);
3132
3133 if (fold_end(fp) > dest)
3134 truncate_fold(fp, dest);
3135
3136 fp->fd_top -= range_len;
3137 }
3138
3139 dest_index = fold_index(fp, gap);
3140
3141 /*
3142 * All folds are now correct, but they are not necessarily in the correct
3143 * order. We have to swap folds in the range [move_end, dest_index) with
3144 * those in the range [move_start, move_end).
3145 */
3146 foldReverseOrder(gap, move_start, dest_index - 1);
3147 foldReverseOrder(gap, move_start, move_start + dest_index - move_end - 1);
3148 foldReverseOrder(gap, move_start + dest_index - move_end, dest_index - 1);
3149}
3150#undef fold_end
3151#undef valid_fold
3152#undef fold_index
3153
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154/* foldMerge() {{{2 */
3155/*
Bram Moolenaar25394022007-05-10 19:06:20 +00003156 * Merge two adjacent folds (and the nested ones in them).
3157 * This only works correctly when the folds are really adjacent! Thus "fp1"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 * 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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003163foldMerge(fold_T *fp1, garray_T *gap, fold_T *fp2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164{
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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 }
3186 gap2->ga_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 }
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003201foldlevelIndent(fline_T *flp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202{
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
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003221 flp->lvl = get_indent_buf(buf, lnum) / get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (flp->lvl > flp->wp->w_p_fdn)
Bram Moolenaar74c596b2006-11-01 11:44:31 +00003223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 flp->lvl = flp->wp->w_p_fdn;
Bram Moolenaar74c596b2006-11-01 11:44:31 +00003225 if (flp->lvl < 0)
3226 flp->lvl = 0;
3227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228}
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003237foldlevelDiff(fline_T *flp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003253foldlevelExpr(fline_T *flp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
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;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003279 n = (int)eval_foldexpr(flp->wp->w_p_fde, &c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003355parseMarker(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003373foldlevelMarker(fline_T *flp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374{
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 */
Bram Moolenaar3ee02292010-01-19 17:24:25 +01003433 if (flp->lvl_next > start_lvl)
3434 flp->lvl_next = start_lvl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
3436 }
3437 else
3438 --flp->lvl_next;
3439 }
3440 else
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003441 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 }
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003455foldlevelSyntax(fline_T *flp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456{
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)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01003482static int put_folds_recurse(FILE *fd, garray_T *gap, linenr_T off);
3483static int put_foldopen_recurse(FILE *fd, win_T *wp, garray_T *gap, linenr_T off);
3484static int put_fold_open_close(FILE *fd, fold_T *fp, linenr_T off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
3486/*
3487 * Write commands to "fd" to restore the manual folds in window "wp".
3488 * Return FAIL if writing fails.
3489 */
3490 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003491put_folds(FILE *fd, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492{
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)
Bram Moolenaard25ad652012-02-29 19:20:02 +01003502 return put_foldopen_recurse(fd, wp, &wp->w_folds, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003513put_folds_recurse(FILE *fd, garray_T *gap, linenr_T off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514{
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003539put_foldopen_recurse(
3540 FILE *fd,
3541 win_T *wp,
3542 garray_T *gap,
3543 linenr_T off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544{
3545 int i;
Bram Moolenaard25ad652012-02-29 19:20:02 +01003546 int level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 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 {
Bram Moolenaard25ad652012-02-29 19:20:02 +01003556 /* open nested folds while this fold is open */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 if (fprintf(fd, "%ld", fp->fd_top + off) < 0
3558 || put_eol(fd) == FAIL
Bram Moolenaar002a4ed2012-08-29 15:22:25 +02003559 || put_line(fd, "normal! zo") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 return FAIL;
Bram Moolenaard25ad652012-02-29 19:20:02 +01003561 if (put_foldopen_recurse(fd, wp, &fp->fd_nested,
3562 off + fp->fd_top)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 == FAIL)
3564 return FAIL;
Bram Moolenaard25ad652012-02-29 19:20:02 +01003565 /* 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 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
Bram Moolenaard25ad652012-02-29 19:20:02 +01003572 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;
3582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
3584 ++fp;
3585 }
3586
3587 return OK;
3588}
Bram Moolenaard25ad652012-02-29 19:20:02 +01003589
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
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003596put_fold_open_close(FILE *fd, fold_T *fp, linenr_T off)
Bram Moolenaard25ad652012-02-29 19:20:02 +01003597{
3598 if (fprintf(fd, "%ld", fp->fd_top + off) < 0
3599 || put_eol(fd) == FAIL
Bram Moolenaar002a4ed2012-08-29 15:22:25 +02003600 || fprintf(fd, "normal! z%c",
Bram Moolenaard25ad652012-02-29 19:20:02 +01003601 fp->fd_flags == FD_CLOSED ? 'c' : 'o') < 0
3602 || put_eol(fd) == FAIL)
3603 return FAIL;
3604
3605 return OK;
3606}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607#endif /* FEAT_SESSION */
3608
3609/* }}}1 */
3610#endif /* defined(FEAT_FOLDING) || defined(PROTO) */