aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/fold.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/fold.c')
-rw-r--r--src/nvim/fold.c999
1 files changed, 423 insertions, 576 deletions
diff --git a/src/nvim/fold.c b/src/nvim/fold.c
index 546345eeac..8f26e03a94 100644
--- a/src/nvim/fold.c
+++ b/src/nvim/fold.c
@@ -3,9 +3,7 @@
// vim: set fdm=marker fdl=1 fdc=3
-/*
- * fold.c: code for folding
- */
+// fold.c: code for folding
#include <inttypes.h>
#include <string.h>
@@ -41,12 +39,12 @@
// local declarations. {{{1
// typedef fold_T {{{2
-/*
- * The toplevel folds for each window are stored in the w_folds growarray.
- * Each toplevel fold can contain an array of second level folds in the
- * fd_nested growarray.
- * The info stored in both growarrays is the same: An array of fold_T.
- */
+
+// The toplevel folds for each window are stored in the w_folds growarray.
+// Each toplevel fold can contain an array of second level folds in the
+// fd_nested growarray.
+// The info stored in both growarrays is the same: An array of fold_T.
+
typedef struct {
linenr_T fd_top; // first line of fold; for nested fold
// relative to parent
@@ -93,20 +91,16 @@ typedef void (*LevelGetter)(fline_T *);
#endif
static char *e_nofold = N_("E490: No fold found");
-/*
- * While updating the folds lines between invalid_top and invalid_bot have an
- * undefined fold level. Only used for the window currently being updated.
- */
+// While updating the folds lines between invalid_top and invalid_bot have an
+// undefined fold level. Only used for the window currently being updated.
static linenr_T invalid_top = (linenr_T)0;
static linenr_T invalid_bot = (linenr_T)0;
-/*
- * When using 'foldexpr' we sometimes get the level of the next line, which
- * calls foldlevel() to get the level of the current line, which hasn't been
- * stored yet. To get around this chicken-egg problem the level of the
- * previous line is stored here when available. prev_lnum is zero when the
- * level is not available.
- */
+// When using 'foldexpr' we sometimes get the level of the next line, which
+// calls foldlevel() to get the level of the current line, which hasn't been
+// stored yet. To get around this chicken-egg problem the level of the
+// previous line is stored here when available. prev_lnum is zero when the
+// level is not available.
static linenr_T prev_lnum = 0;
static int prev_lnum_lvl = -1;
@@ -121,9 +115,7 @@ static size_t foldendmarkerlen;
// Exported folding functions. {{{1
// copyFoldingState() {{{2
-/*
- * Copy that folding state from window "wp_from" to window "wp_to".
- */
+/// Copy that folding state from window "wp_from" to window "wp_to".
void copyFoldingState(win_T *wp_from, win_T *wp_to)
{
wp_to->w_fold_manual = wp_from->w_fold_manual;
@@ -132,9 +124,7 @@ void copyFoldingState(win_T *wp_from, win_T *wp_to)
}
// hasAnyFolding() {{{2
-/*
- * Return TRUE if there may be folded lines in the current window.
- */
+/// @return true if there may be folded lines in the current window.
int hasAnyFolding(win_T *win)
{
// very simple now, but can become more complex later
@@ -164,16 +154,6 @@ bool hasFolding(linenr_T lnum, linenr_T *firstp, linenr_T *lastp)
bool hasFoldingWin(win_T *const win, const linenr_T lnum, linenr_T *const firstp,
linenr_T *const lastp, const bool cache, foldinfo_T *const infop)
{
- bool had_folded = false;
- linenr_T first = 0;
- linenr_T last = 0;
- linenr_T lnum_rel = lnum;
- fold_T *fp;
- int level = 0;
- bool use_level = false;
- bool maybe_small = false;
- int low_level = 0;
-
checkupdate(win);
// Return quickly when there is no folding at all in this window.
@@ -184,11 +164,13 @@ bool hasFoldingWin(win_T *const win, const linenr_T lnum, linenr_T *const firstp
return false;
}
+ bool had_folded = false;
+ linenr_T first = 0;
+ linenr_T last = 0;
+
if (cache) {
- /*
- * First look in cached info for displayed lines. This is probably
- * the fastest, but it can only be used if the entry is still valid.
- */
+ // First look in cached info for displayed lines. This is probably
+ // the fastest, but it can only be used if the entry is still valid.
const int x = find_wl_entry(win, lnum);
if (x >= 0) {
first = win->w_lines[x].wl_lnum;
@@ -197,10 +179,15 @@ bool hasFoldingWin(win_T *const win, const linenr_T lnum, linenr_T *const firstp
}
}
+ linenr_T lnum_rel = lnum;
+ int level = 0;
+ int low_level = 0;
+ fold_T *fp;
+ bool maybe_small = false;
+ bool use_level = false;
+
if (first == 0) {
- /*
- * Recursively search for a fold that contains "lnum".
- */
+ // Recursively search for a fold that contains "lnum".
garray_T *gap = &win->w_folds;
for (;;) {
if (!foldFind(gap, lnum_rel, &fp)) {
@@ -228,7 +215,7 @@ bool hasFoldingWin(win_T *const win, const linenr_T lnum, linenr_T *const firstp
// relative to containing fold.
gap = &fp->fd_nested;
lnum_rel -= fp->fd_top;
- ++level;
+ level++;
}
}
@@ -259,9 +246,7 @@ bool hasFoldingWin(win_T *const win, const linenr_T lnum, linenr_T *const firstp
}
// foldLevel() {{{2
-/*
- * Return fold level at line number "lnum" in the current window.
- */
+/// @return fold level at line number "lnum" in the current window.
int foldLevel(linenr_T lnum)
{
// While updating the folds lines between invalid_top and invalid_bot have
@@ -283,15 +268,16 @@ int foldLevel(linenr_T lnum)
}
// lineFolded() {{{2
-// Low level function to check if a line is folded. Doesn't use any caching.
-// Return true if line is folded.
-// Return false if line is not folded.
+/// Low level function to check if a line is folded. Doesn't use any caching.
+///
+/// @return true if line is folded or,
+/// false if line is not folded.
bool lineFolded(win_T *const win, const linenr_T lnum)
{
return fold_info(win, lnum).fi_lines != 0;
}
-/// fold_info() {{{2
+// fold_info() {{{2
///
/// Count the number of lines that are folded at line number "lnum".
/// Normally "lnum" is the first line of a possible fold, and the returned
@@ -316,61 +302,49 @@ foldinfo_T fold_info(win_T *win, linenr_T lnum)
}
// foldmethodIsManual() {{{2
-/*
- * Return TRUE if 'foldmethod' is "manual"
- */
+/// @return true if 'foldmethod' is "manual"
int foldmethodIsManual(win_T *wp)
{
return wp->w_p_fdm[3] == 'u';
}
// foldmethodIsIndent() {{{2
-/*
- * Return TRUE if 'foldmethod' is "indent"
- */
+/// @return true if 'foldmethod' is "indent"
int foldmethodIsIndent(win_T *wp)
{
return wp->w_p_fdm[0] == 'i';
}
// foldmethodIsExpr() {{{2
-/*
- * Return TRUE if 'foldmethod' is "expr"
- */
+/// @return true if 'foldmethod' is "expr"
int foldmethodIsExpr(win_T *wp)
{
return wp->w_p_fdm[1] == 'x';
}
// foldmethodIsMarker() {{{2
-/*
- * Return TRUE if 'foldmethod' is "marker"
- */
+/// @return true if 'foldmethod' is "marker"
int foldmethodIsMarker(win_T *wp)
{
return wp->w_p_fdm[2] == 'r';
}
// foldmethodIsSyntax() {{{2
-/*
- * Return TRUE if 'foldmethod' is "syntax"
- */
+/// @return true if 'foldmethod' is "syntax"
int foldmethodIsSyntax(win_T *wp)
{
return wp->w_p_fdm[0] == 's';
}
// foldmethodIsDiff() {{{2
-/*
- * Return TRUE if 'foldmethod' is "diff"
- */
+/// @return true if 'foldmethod' is "diff"
int foldmethodIsDiff(win_T *wp)
{
return wp->w_p_fdm[0] == 'd';
}
// closeFold() {{{2
-/// Close fold for current window at line "lnum".
+/// Close fold for current window at position "pos".
/// Repeat "count" times.
void closeFold(pos_T pos, long count)
{
@@ -378,9 +352,7 @@ void closeFold(pos_T pos, long count)
}
// closeFoldRecurse() {{{2
-/*
- * Close fold for current window at line "lnum" recursively.
- */
+/// Close fold for current window at position `pos` recursively.
void closeFoldRecurse(pos_T pos)
{
(void)setManualFold(pos, false, true, NULL);
@@ -391,18 +363,17 @@ void closeFoldRecurse(pos_T pos)
/// Open or Close folds for current window in lines "first" to "last".
/// Used for "zo", "zO", "zc" and "zC" in Visual mode.
///
-/// @param opening TRUE to open, FALSE to close
-/// @param recurse TRUE to do it recursively
-/// @param had_visual TRUE when Visual selection used
+/// @param opening true to open, false to close
+/// @param recurse true to do it recursively
+/// @param had_visual true when Visual selection used
void opFoldRange(pos_T firstpos, pos_T lastpos, int opening, int recurse, int had_visual)
{
int done = DONE_NOTHING; // avoid error messages
linenr_T first = firstpos.lnum;
linenr_T last = lastpos.lnum;
- linenr_T lnum;
linenr_T lnum_next;
- for (lnum = first; lnum <= last; lnum = lnum_next + 1) {
+ for (linenr_T lnum = first; lnum <= last; lnum = lnum_next + 1) {
pos_T temp = { lnum, 0, 0 };
lnum_next = lnum;
// Opening one level only: next fold to open is after the one going to
@@ -427,36 +398,28 @@ void opFoldRange(pos_T firstpos, pos_T lastpos, int opening, int recurse, int ha
}
// openFold() {{{2
-/*
- * Open fold for current window at line "lnum".
- * Repeat "count" times.
- */
+/// Open fold for current window at position "pos".
+/// Repeat "count" times.
void openFold(pos_T pos, long count)
{
setFoldRepeat(pos, count, true);
}
// openFoldRecurse() {{{2
-/*
- * Open fold for current window at line "lnum" recursively.
- */
+/// Open fold for current window at position `pos` recursively.
void openFoldRecurse(pos_T pos)
{
(void)setManualFold(pos, true, true, NULL);
}
// foldOpenCursor() {{{2
-/*
- * Open folds until the cursor line is not in a closed fold.
- */
+/// Open folds until the cursor line is not in a closed fold.
void foldOpenCursor(void)
{
- int done;
-
checkupdate(curwin);
if (hasAnyFolding(curwin)) {
for (;;) {
- done = DONE_NOTHING;
+ int done = DONE_NOTHING;
(void)setManualFold(curwin->w_cursor, true, false, &done);
if (!(done & DONE_ACTION)) {
break;
@@ -466,17 +429,13 @@ void foldOpenCursor(void)
}
// newFoldLevel() {{{2
-/*
- * Set new foldlevel for current window.
- */
+/// Set new foldlevel for current window.
void newFoldLevel(void)
{
newFoldLevelWin(curwin);
if (foldmethodIsDiff(curwin) && curwin->w_p_scb) {
- /*
- * Set the same foldlevel in other windows in diff mode.
- */
+ // Set the same foldlevel in other windows in diff mode.
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb) {
wp->w_p_fdl = curwin->w_p_fdl;
@@ -488,15 +447,13 @@ void newFoldLevel(void)
static void newFoldLevelWin(win_T *wp)
{
- fold_T *fp;
-
checkupdate(wp);
if (wp->w_fold_manual) {
// Set all flags for the first level of folds to FD_LEVEL. Following
// manual open/close will then change the flags to FD_OPEN or
// FD_CLOSED for those folds that don't use 'foldlevel'.
- fp = (fold_T *)wp->w_folds.ga_data;
- for (int i = 0; i < wp->w_folds.ga_len; ++i) {
+ fold_T *fp = (fold_T *)wp->w_folds.ga_data;
+ for (int i = 0; i < wp->w_folds.ga_len; i++) {
fp[i].fd_flags = FD_LEVEL;
}
wp->w_fold_manual = false;
@@ -505,9 +462,7 @@ static void newFoldLevelWin(win_T *wp)
}
// foldCheckClose() {{{2
-/*
- * Apply 'foldlevel' to all folds that don't contain the cursor.
- */
+/// Apply 'foldlevel' to all folds that don't contain the cursor.
void foldCheckClose(void)
{
if (*p_fcl != NUL) { // can only be "all" right now
@@ -520,19 +475,18 @@ void foldCheckClose(void)
}
// checkCloseRec() {{{2
-static int checkCloseRec(garray_T *gap, linenr_T lnum, int level)
+static bool checkCloseRec(garray_T *gap, linenr_T lnum, int level)
{
- fold_T *fp;
- int retval = FALSE;
+ bool retval = false;
- fp = (fold_T *)gap->ga_data;
- for (int i = 0; i < gap->ga_len; ++i) {
+ fold_T *fp = (fold_T *)gap->ga_data;
+ for (int i = 0; i < gap->ga_len; i++) {
// Only manually opened folds may need to be closed.
if (fp[i].fd_flags == FD_OPEN) {
if (level <= 0 && (lnum < fp[i].fd_top
|| lnum >= fp[i].fd_top + fp[i].fd_len)) {
fp[i].fd_flags = FD_LEVEL;
- retval = TRUE;
+ retval = true;
} else {
retval |= checkCloseRec(&fp[i].fd_nested, lnum - fp[i].fd_top,
level - 1);
@@ -543,19 +497,19 @@ static int checkCloseRec(garray_T *gap, linenr_T lnum, int level)
}
// foldCreateAllowed() {{{2
-/// Return TRUE if it's allowed to manually create or delete a fold.
-/// Give an error message and return FALSE if not.
+/// @return true if it's allowed to manually create or delete a fold or,
+/// give an error message and return false if not.
int foldManualAllowed(bool create)
{
if (foldmethodIsManual(curwin) || foldmethodIsMarker(curwin)) {
- return TRUE;
+ return true;
}
if (create) {
emsg(_("E350: Cannot create fold with current 'foldmethod'"));
} else {
emsg(_("E351: Cannot delete fold with current 'foldmethod'"));
}
- return FALSE;
+ return false;
}
// foldCreate() {{{2
@@ -563,13 +517,8 @@ int foldManualAllowed(bool create)
/// window.
void foldCreate(win_T *wp, pos_T start, pos_T end)
{
- fold_T *fp;
- garray_T *gap;
- garray_T fold_ga;
- int i;
- int cont;
- int use_level = FALSE;
- int closed = FALSE;
+ int use_level = false;
+ int closed = false;
int level = 0;
pos_T start_rel = start;
pos_T end_rel = end;
@@ -590,11 +539,14 @@ void foldCreate(win_T *wp, pos_T start, pos_T end)
checkupdate(wp);
+ int i;
+
// Find the place to insert the new fold
- gap = &wp->w_folds;
+ garray_T *gap = &wp->w_folds;
if (gap->ga_len == 0) {
i = 0;
} else {
+ fold_T *fp;
for (;;) {
if (!foldFind(gap, start_rel.lnum, &fp)) {
break;
@@ -628,10 +580,12 @@ void foldCreate(win_T *wp, pos_T start, pos_T end)
ga_grow(gap, 1);
{
- fp = (fold_T *)gap->ga_data + i;
+ fold_T *fp = (fold_T *)gap->ga_data + i;
+ garray_T fold_ga;
ga_init(&fold_ga, (int)sizeof(fold_T), 10);
// Count number of folds that will be contained in the new fold.
+ int cont;
for (cont = 0; i + cont < gap->ga_len; cont++) {
if (fp[cont].fd_top > end_rel.lnum) {
break;
@@ -689,7 +643,6 @@ void foldCreate(win_T *wp, pos_T start, pos_T end)
}
}
-
// deleteFold() {{{2
/// @param start delete all folds from start to end when not 0
/// @param end delete all folds from start to end when not 0
@@ -698,7 +651,6 @@ void foldCreate(win_T *wp, pos_T start, pos_T end)
void deleteFold(win_T *const wp, const linenr_T start, const linenr_T end, const int recursive,
const bool had_visual)
{
- fold_T *fp;
fold_T *found_fp = NULL;
linenr_T found_off = 0;
bool maybe_small = false;
@@ -717,6 +669,7 @@ void deleteFold(win_T *const wp, const linenr_T start, const linenr_T end, const
linenr_T lnum_off = 0;
bool use_level = false;
for (;;) {
+ fold_T *fp;
if (!foldFind(gap, lnum - lnum_off, &fp)) {
break;
}
@@ -734,10 +687,10 @@ void deleteFold(win_T *const wp, const linenr_T start, const linenr_T end, const
// check nested folds
gap = &fp->fd_nested;
lnum_off += fp->fd_top;
- ++level;
+ level++;
}
if (found_ga == NULL) {
- ++lnum;
+ lnum++;
} else {
lnum = found_fp->fd_top + found_fp->fd_len + found_off;
@@ -784,15 +737,12 @@ void deleteFold(win_T *const wp, const linenr_T start, const linenr_T end, const
// the modification of the *first* line of the fold, but we send through a
// notification that includes every line that was part of the fold
int64_t num_changed = last_lnum - first_lnum;
- buf_updates_send_changes(wp->w_buffer, first_lnum, num_changed,
- num_changed, true);
+ buf_updates_send_changes(wp->w_buffer, first_lnum, num_changed, num_changed);
}
}
// clearFolding() {{{2
-/*
- * Remove all folding for window "win".
- */
+/// Remove all folding for window "win".
void clearFolding(win_T *win)
{
deleteFoldRecurse(win->w_buffer, &win->w_folds);
@@ -800,15 +750,13 @@ void clearFolding(win_T *win)
}
// foldUpdate() {{{2
-/*
- * Update folds for changes in the buffer of a window.
- * Note that inserted/deleted lines must have already been taken care of by
- * calling foldMarkAdjust().
- * The changes in lines from top to bot (inclusive).
- */
+/// Update folds for changes in the buffer of a window.
+/// Note that inserted/deleted lines must have already been taken care of by
+/// calling foldMarkAdjust().
+/// The changes in lines from top to bot (inclusive).
void foldUpdate(win_T *wp, linenr_T top, linenr_T bot)
{
- if (compl_busy || State & INSERT) {
+ if (disable_fold_update || compl_busy || State & MODE_INSERT) {
return;
}
@@ -818,11 +766,18 @@ void foldUpdate(win_T *wp, linenr_T top, linenr_T bot)
}
if (wp->w_folds.ga_len > 0) {
- // Mark all folds from top to bot as maybe-small.
+ linenr_T maybe_small_start = top;
+ linenr_T maybe_small_end = bot;
+
+ // Mark all folds from top to bot (or bot to top) as maybe-small.
+ if (top > bot) {
+ maybe_small_start = bot;
+ maybe_small_end = top;
+ }
fold_T *fp;
- (void)foldFind(&wp->w_folds, top, &fp);
+ (void)foldFind(&wp->w_folds, maybe_small_start, &fp);
while (fp < (fold_T *)wp->w_folds.ga_data + wp->w_folds.ga_len
- && fp->fd_top < bot) {
+ && fp->fd_top <= maybe_small_end) {
fp->fd_small = kNone;
fp++;
}
@@ -836,7 +791,7 @@ void foldUpdate(win_T *wp, linenr_T top, linenr_T bot)
int save_got_int = got_int;
// reset got_int here, otherwise it won't work
- got_int = FALSE;
+ got_int = false;
foldUpdateIEMS(wp, top, bot);
got_int |= save_got_int;
}
@@ -856,12 +811,10 @@ void foldUpdateAfterInsert(void)
}
// foldUpdateAll() {{{2
-/*
- * Update all lines in a window for folding.
- * Used when a fold setting changes or after reloading the buffer.
- * The actual updating is postponed until fold info is used, to avoid doing
- * every time a setting is changed or a syntax item is added.
- */
+/// Update all lines in a window for folding.
+/// Used when a fold setting changes or after reloading the buffer.
+/// The actual updating is postponed until fold info is used, to avoid doing
+/// every time a setting is changed or a syntax item is added.
void foldUpdateAll(win_T *win)
{
win->w_foldinvalid = true;
@@ -974,7 +927,7 @@ int foldMoveTo(const bool updown, const int dir, const long count)
// Check nested folds (if any).
gap = &fp->fd_nested;
lnum_off += fp->fd_top;
- ++level;
+ level++;
}
if (lnum_found != curwin->w_cursor.lnum) {
if (retval == FAIL) {
@@ -992,26 +945,21 @@ int foldMoveTo(const bool updown, const int dir, const long count)
}
// foldInitWin() {{{2
-/*
- * Init the fold info in a new window.
- */
+/// Init the fold info in a new window.
void foldInitWin(win_T *new_win)
{
ga_init(&new_win->w_folds, (int)sizeof(fold_T), 10);
}
// find_wl_entry() {{{2
-/*
- * Find an entry in the win->w_lines[] array for buffer line "lnum".
- * Only valid entries are considered (for entries where wl_valid is FALSE the
- * line number can be wrong).
- * Returns index of entry or -1 if not found.
- */
+/// Find an entry in the win->w_lines[] array for buffer line "lnum".
+/// Only valid entries are considered (for entries where wl_valid is false the
+/// line number can be wrong).
+///
+/// @return index of entry or -1 if not found.
int find_wl_entry(win_T *win, linenr_T lnum)
{
- int i;
-
- for (i = 0; i < win->w_lines_valid; ++i) {
+ for (int i = 0; i < win->w_lines_valid; i++) {
if (win->w_lines[i].wl_valid) {
if (lnum < win->w_lines[i].wl_lnum) {
return -1;
@@ -1025,18 +973,16 @@ int find_wl_entry(win_T *win, linenr_T lnum)
}
// foldAdjustVisual() {{{2
-/*
- * Adjust the Visual area to include any fold at the start or end completely.
- */
+/// Adjust the Visual area to include any fold at the start or end completely.
void foldAdjustVisual(void)
{
- pos_T *start, *end;
- char_u *ptr;
-
if (!VIsual_active || !hasAnyFolding(curwin)) {
return;
}
+ pos_T *start, *end;
+ char_u *ptr;
+
if (ltoreq(VIsual, curwin->w_cursor)) {
start = &VIsual;
end = &curwin->w_cursor;
@@ -1059,9 +1005,7 @@ void foldAdjustVisual(void)
}
// cursor_foldstart() {{{2
-/*
- * Move the cursor to the first line of a closed fold.
- */
+/// Move the cursor to the first line of a closed fold.
void foldAdjustCursor(void)
{
(void)hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL);
@@ -1069,14 +1013,9 @@ void foldAdjustCursor(void)
// Internal functions for "fold_T" {{{1
// cloneFoldGrowArray() {{{2
-/*
- * Will "clone" (i.e deep copy) a garray_T of folds.
- */
+/// Will "clone" (i.e deep copy) a garray_T of folds.
void cloneFoldGrowArray(garray_T *from, garray_T *to)
{
- fold_T *from_p;
- fold_T *to_p;
-
ga_init(to, from->ga_itemsize, from->ga_growsize);
if (GA_EMPTY(from)) {
@@ -1085,8 +1024,8 @@ void cloneFoldGrowArray(garray_T *from, garray_T *to)
ga_grow(to, from->ga_len);
- from_p = (fold_T *)from->ga_data;
- to_p = (fold_T *)to->ga_data;
+ fold_T *from_p = (fold_T *)from->ga_data;
+ fold_T *to_p = (fold_T *)to->ga_data;
for (int i = 0; i < from->ga_len; i++) {
to_p->fd_top = from_p->fd_top;
@@ -1094,36 +1033,31 @@ void cloneFoldGrowArray(garray_T *from, garray_T *to)
to_p->fd_flags = from_p->fd_flags;
to_p->fd_small = from_p->fd_small;
cloneFoldGrowArray(&from_p->fd_nested, &to_p->fd_nested);
- ++to->ga_len;
- ++from_p;
- ++to_p;
+ to->ga_len++;
+ from_p++;
+ to_p++;
}
}
// foldFind() {{{2
/// Search for line "lnum" in folds of growarray "gap".
-/// Set *fpp to the fold struct for the fold that contains "lnum" or
+/// Set "*fpp" to the fold struct for the fold that contains "lnum" or
/// the first fold below it (careful: it can be beyond the end of the array!).
///
/// @return false when there is no fold that contains "lnum".
static bool foldFind(const garray_T *gap, linenr_T lnum, fold_T **fpp)
{
- linenr_T low, high;
- fold_T *fp;
-
if (gap->ga_len == 0) {
*fpp = NULL;
return false;
}
- /*
- * Perform a binary search.
- * "low" is lowest index of possible match.
- * "high" is highest index of possible match.
- */
- fp = (fold_T *)gap->ga_data;
- low = 0;
- high = gap->ga_len - 1;
+ // Perform a binary search.
+ // "low" is lowest index of possible match.
+ // "high" is highest index of possible match.
+ fold_T *fp = (fold_T *)gap->ga_data;
+ linenr_T low = 0;
+ linenr_T high = gap->ga_len - 1;
while (low <= high) {
linenr_T i = (low + high) / 2;
if (fp[i].fd_top > lnum) {
@@ -1143,18 +1077,15 @@ static bool foldFind(const garray_T *gap, linenr_T lnum, fold_T **fpp)
}
// foldLevelWin() {{{2
-/*
- * Return fold level at line number "lnum" in window "wp".
- */
+/// @return fold level at line number "lnum" in window "wp".
static int foldLevelWin(win_T *wp, linenr_T lnum)
{
fold_T *fp;
linenr_T lnum_rel = lnum;
int level = 0;
- garray_T *gap;
// Recursively search for a fold that contains "lnum".
- gap = &wp->w_folds;
+ garray_T *gap = &wp->w_folds;
for (;;) {
if (!foldFind(gap, lnum_rel, &fp)) {
break;
@@ -1162,16 +1093,14 @@ static int foldLevelWin(win_T *wp, linenr_T lnum)
// Check nested folds. Line number is relative to containing fold.
gap = &fp->fd_nested;
lnum_rel -= fp->fd_top;
- ++level;
+ level++;
}
return level;
}
// checkupdate() {{{2
-/*
- * Check if the folds in window "wp" are invalid and update them if needed.
- */
+/// Check if the folds in window "wp" are invalid and update them if needed.
static void checkupdate(win_T *wp)
{
if (wp->w_foldinvalid) {
@@ -1181,17 +1110,12 @@ static void checkupdate(win_T *wp)
}
// setFoldRepeat() {{{2
-/*
- * Open or close fold for current window at line "lnum".
- * Repeat "count" times.
- */
+/// Open or close fold for current window at position `pos`.
+/// Repeat "count" times.
static void setFoldRepeat(pos_T pos, long count, int do_open)
{
- int done;
- long n;
-
- for (n = 0; n < count; ++n) {
- done = DONE_NOTHING;
+ for (int n = 0; n < count; n++) {
+ int done = DONE_NOTHING;
(void)setManualFold(pos, do_open, false, &done);
if (!(done & DONE_ACTION)) {
// Only give an error message when no fold could be opened.
@@ -1204,22 +1128,18 @@ static void setFoldRepeat(pos_T pos, long count, int do_open)
}
// setManualFold() {{{2
-///
/// Open or close the fold in the current window which contains "lnum".
/// Also does this for other windows in diff mode when needed.
///
-/// @param opening TRUE when opening, FALSE when closing
-/// @param recurse TRUE when closing/opening recursive
+/// @param opening true when opening, false when closing
+/// @param recurse true when closing/opening recursive
static linenr_T setManualFold(pos_T pos, int opening, int recurse, int *donep)
{
- linenr_T lnum = pos.lnum;
if (foldmethodIsDiff(curwin) && curwin->w_p_scb) {
linenr_T dlnum;
- /*
- * Do the same operation in other windows in diff mode. Calculate the
- * line number from the diffs.
- */
+ // Do the same operation in other windows in diff mode. Calculate the
+ // line number from the diffs.
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb) {
dlnum = diff_lnum_win(curwin->w_cursor.lnum, wp);
@@ -1230,7 +1150,7 @@ static linenr_T setManualFold(pos_T pos, int opening, int recurse, int *donep)
}
}
- return setManualFoldWin(curwin, lnum, opening, recurse, donep);
+ return setManualFoldWin(curwin, pos.lnum, opening, recurse, donep);
}
// setManualFoldWin() {{{2
@@ -1240,31 +1160,27 @@ static linenr_T setManualFold(pos_T pos, int opening, int recurse, int *donep)
/// When "donep" is NULL give an error message when no fold was found for
/// "lnum", but only if "wp" is "curwin".
///
-/// @param opening TRUE when opening, FALSE when closing
-/// @param recurse TRUE when closing/opening recursive
+/// @param opening true when opening, false when closing
+/// @param recurse true when closing/opening recursive
///
/// @return the line number of the next line that could be closed.
-/// It's only valid when "opening" is TRUE!
+/// It's only valid when "opening" is true!
static linenr_T setManualFoldWin(win_T *wp, linenr_T lnum, int opening, int recurse, int *donep)
{
fold_T *fp;
fold_T *fp2;
fold_T *found = NULL;
- int j;
int level = 0;
- int use_level = FALSE;
- int found_fold = FALSE;
- garray_T *gap;
+ bool use_level = false;
+ bool found_fold = false;
linenr_T next = MAXLNUM;
linenr_T off = 0;
int done = 0;
checkupdate(wp);
- /*
- * Find the fold, open or close it.
- */
- gap = &wp->w_folds;
+ // Find the fold, open or close it.
+ garray_T *gap = &wp->w_folds;
for (;;) {
if (!foldFind(gap, lnum, &fp)) {
// If there is a following fold, continue there next time.
@@ -1275,7 +1191,7 @@ static linenr_T setManualFoldWin(win_T *wp, linenr_T lnum, int opening, int recu
}
// lnum is inside this fold
- found_fold = TRUE;
+ found_fold = true;
// If there is a following fold, continue there next time.
if (fp + 1 < (fold_T *)gap->ga_data + gap->ga_len) {
@@ -1284,14 +1200,14 @@ static linenr_T setManualFoldWin(win_T *wp, linenr_T lnum, int opening, int recu
// Change from level-dependent folding to manual.
if (use_level || fp->fd_flags == FD_LEVEL) {
- use_level = TRUE;
+ use_level = true;
if (level >= wp->w_p_fdl) {
fp->fd_flags = FD_CLOSED;
} else {
fp->fd_flags = FD_OPEN;
}
fp2 = (fold_T *)fp->fd_nested.ga_data;
- for (j = 0; j < fp->fd_nested.ga_len; ++j) {
+ for (int j = 0; j < fp->fd_nested.ga_len; j++) {
fp2[j].fd_flags = FD_LEVEL;
}
}
@@ -1319,7 +1235,7 @@ static linenr_T setManualFoldWin(win_T *wp, linenr_T lnum, int opening, int recu
gap = &fp->fd_nested;
lnum -= fp->fd_top;
off += fp->fd_top;
- ++level;
+ level++;
}
if (found_fold) {
// When closing and not recurse, close deepest open fold.
@@ -1344,24 +1260,21 @@ static linenr_T setManualFoldWin(win_T *wp, linenr_T lnum, int opening, int recu
}
// foldOpenNested() {{{2
-/*
- * Open all nested folds in fold "fpr" recursively.
- */
+/// Open all nested folds in fold "fpr" recursively.
static void foldOpenNested(fold_T *fpr)
{
- fold_T *fp;
-
- fp = (fold_T *)fpr->fd_nested.ga_data;
- for (int i = 0; i < fpr->fd_nested.ga_len; ++i) {
+ fold_T *fp = (fold_T *)fpr->fd_nested.ga_data;
+ for (int i = 0; i < fpr->fd_nested.ga_len; i++) {
foldOpenNested(&fp[i]);
fp[i].fd_flags = FD_OPEN;
}
}
// deleteFoldEntry() {{{2
-// Delete fold "idx" from growarray "gap".
-// When "recursive" is true also delete all the folds contained in it.
-// When "recursive" is false contained folds are moved one level up.
+/// Delete fold "idx" from growarray "gap".
+///
+/// @param recursive when true, also delete all the folds contained in it.
+/// when false, contained folds are moved one level up.
static void deleteFoldEntry(win_T *const wp, garray_T *const gap, const int idx,
const bool recursive)
{
@@ -1408,9 +1321,7 @@ static void deleteFoldEntry(win_T *const wp, garray_T *const gap, const int idx,
}
// deleteFoldRecurse() {{{2
-/*
- * Delete nested folds in a fold.
- */
+/// Delete nested folds in a fold.
void deleteFoldRecurse(buf_T *bp, garray_T *gap)
{
#define DELETE_FOLD_NESTED(fd) deleteFoldRecurse(bp, &((fd)->fd_nested))
@@ -1418,10 +1329,9 @@ void deleteFoldRecurse(buf_T *bp, garray_T *gap)
}
// foldMarkAdjust() {{{2
-/*
- * Update line numbers of folds for inserted/deleted lines.
- */
-void foldMarkAdjust(win_T *wp, linenr_T line1, linenr_T line2, long amount, long amount_after)
+/// Update line numbers of folds for inserted/deleted lines.
+void foldMarkAdjust(win_T *wp, linenr_T line1, linenr_T line2, linenr_T amount,
+ linenr_T amount_after)
{
// If deleting marks from line1 to line2, but not deleting all those
// lines, set line2 so that only deleted lines have their folds removed.
@@ -1430,7 +1340,7 @@ void foldMarkAdjust(win_T *wp, linenr_T line1, linenr_T line2, long amount, long
}
// If appending a line in Insert mode, it should be included in the fold
// just above the line.
- if ((State & INSERT) && amount == (linenr_T)1 && line2 == MAXLNUM) {
+ if ((State & MODE_INSERT) && amount == (linenr_T)1 && line2 == MAXLNUM) {
line1--;
}
foldMarkAdjustRecurse(wp, &wp->w_folds, line1, line2, amount, amount_after);
@@ -1438,44 +1348,39 @@ void foldMarkAdjust(win_T *wp, linenr_T line1, linenr_T line2, long amount, long
// foldMarkAdjustRecurse() {{{2
static void foldMarkAdjustRecurse(win_T *wp, garray_T *gap, linenr_T line1, linenr_T line2,
- long amount, long amount_after)
+ linenr_T amount, linenr_T amount_after)
{
- fold_T *fp;
- linenr_T last;
- linenr_T top;
-
if (gap->ga_len == 0) {
return;
}
+ linenr_T top;
+
// In Insert mode an inserted line at the top of a fold is considered part
// of the fold, otherwise it isn't.
- if ((State & INSERT) && amount == (linenr_T)1 && line2 == MAXLNUM) {
+ if ((State & MODE_INSERT) && amount == (linenr_T)1 && line2 == MAXLNUM) {
top = line1 + 1;
} else {
top = line1;
}
// Find the fold containing or just below "line1".
+ fold_T *fp;
(void)foldFind(gap, line1, &fp);
- /*
- * Adjust all folds below "line1" that are affected.
- */
- for (int i = (int)(fp - (fold_T *)gap->ga_data); i < gap->ga_len; ++i, ++fp) {
- /*
- * Check for these situations:
- * 1 2 3
- * 1 2 3
- * line1 2 3 4 5
- * 2 3 4 5
- * 2 3 4 5
- * line2 2 3 4 5
- * 3 5 6
- * 3 5 6
- */
-
- last = fp->fd_top + fp->fd_len - 1; // last line of fold
+ // Adjust all folds below "line1" that are affected.
+ for (int i = (int)(fp - (fold_T *)gap->ga_data); i < gap->ga_len; i++, fp++) {
+ // Check for these situations:
+ // 1 2 3
+ // 1 2 3
+ // line1 2 3 4 5
+ // 2 3 4 5
+ // 2 3 4 5
+ // line2 2 3 4 5
+ // 3 5 6
+ // 3 5 6
+
+ linenr_T last = fp->fd_top + fp->fd_len - 1; // last line of fold
// 1. fold completely above line1: nothing to do
if (last < line1) {
@@ -1538,10 +1443,8 @@ static void foldMarkAdjustRecurse(win_T *wp, garray_T *gap, linenr_T line1, line
}
// getDeepestNesting() {{{2
-/*
- * Get the lowest 'foldlevel' value that makes the deepest nested fold in the
- * current window open.
- */
+/// Get the lowest 'foldlevel' value that makes the deepest nested fold in
+/// window `wp`.
int getDeepestNesting(win_T *wp)
{
checkupdate(wp);
@@ -1550,13 +1453,11 @@ int getDeepestNesting(win_T *wp)
static int getDeepestNestingRecurse(garray_T *gap)
{
- int level;
int maxlevel = 0;
- fold_T *fp;
- fp = (fold_T *)gap->ga_data;
- for (int i = 0; i < gap->ga_len; ++i) {
- level = getDeepestNestingRecurse(&fp[i].fd_nested) + 1;
+ fold_T *fp = (fold_T *)gap->ga_data;
+ for (int i = 0; i < gap->ga_len; i++) {
+ int level = getDeepestNestingRecurse(&fp[i].fd_nested) + 1;
if (level > maxlevel) {
maxlevel = level;
}
@@ -1633,7 +1534,7 @@ static void checkSmall(win_T *const wp, fold_T *const fp, const linenr_T lnum_of
}
// setSmallMaybe() {{{2
-// Set small flags in "gap" to kNone.
+/// Set small flags in "gap" to kNone.
static void setSmallMaybe(garray_T *gap)
{
fold_T *fp = (fold_T *)gap->ga_data;
@@ -1643,10 +1544,8 @@ static void setSmallMaybe(garray_T *gap)
}
// foldCreateMarkers() {{{2
-/*
- * Create a fold from line "start" to line "end" (inclusive) in the current
- * window by adding markers.
- */
+/// Create a fold from line "start" to line "end" (inclusive) in window `wp`
+/// by adding markers.
static void foldCreateMarkers(win_T *wp, pos_T start, pos_T end)
{
buf_T *buf = wp->w_buffer;
@@ -1668,24 +1567,21 @@ static void foldCreateMarkers(win_T *wp, pos_T start, pos_T end)
// u_save() is unable to save the buffer line, but we send the
// nvim_buf_lines_event anyway since it won't do any harm.
int64_t num_changed = 1 + end.lnum - start.lnum;
- buf_updates_send_changes(buf, start.lnum, num_changed, num_changed, true);
+ buf_updates_send_changes(buf, start.lnum, num_changed, num_changed);
}
// foldAddMarker() {{{2
-/*
- * Add "marker[markerlen]" in 'commentstring' to line "lnum".
- */
+/// Add "marker[markerlen]" in 'commentstring' to position `pos`.
static void foldAddMarker(buf_T *buf, pos_T pos, const char_u *marker, size_t markerlen)
{
char_u *cms = buf->b_p_cms;
- char_u *line;
char_u *newline;
char_u *p = (char_u *)strstr((char *)buf->b_p_cms, "%s");
bool line_is_comment = false;
linenr_T lnum = pos.lnum;
// Allocate a new line: old-line + 'cms'-start + marker + 'cms'-end
- line = ml_get_buf(buf, lnum, false);
+ char_u *line = ml_get_buf(buf, lnum, false);
size_t line_len = STRLEN(line);
size_t added = 0;
@@ -1702,11 +1598,11 @@ static void foldAddMarker(buf_T *buf, pos_T pos, const char_u *marker, size_t ma
STRCPY(newline + line_len, cms);
memcpy(newline + line_len + (p - cms), marker, markerlen);
STRCPY(newline + line_len + (p - cms) + markerlen, p + 2);
- added = markerlen + STRLEN(cms)-2;
+ added = markerlen + STRLEN(cms) - 2;
}
ml_replace_buf(buf, lnum, newline, false);
if (added) {
- extmark_splice_cols(buf, (int)lnum-1, (int)line_len,
+ extmark_splice_cols(buf, (int)lnum - 1, (int)line_len,
0, (int)added, kExtmarkUndo);
}
}
@@ -1724,28 +1620,25 @@ static void deleteFoldMarkers(win_T *wp, fold_T *fp, int recursive, linenr_T lnu
lnum_off + fp->fd_top);
}
}
- foldDelMarker(wp->w_buffer, fp->fd_top+lnum_off, wp->w_p_fmr,
+ foldDelMarker(wp->w_buffer, fp->fd_top + lnum_off, wp->w_p_fmr,
foldstartmarkerlen);
foldDelMarker(wp->w_buffer, fp->fd_top + lnum_off + fp->fd_len - 1,
foldendmarker, foldendmarkerlen);
}
// foldDelMarker() {{{2
-//
-// Delete marker "marker[markerlen]" at the end of line "lnum".
-// Delete 'commentstring' if it matches.
-// If the marker is not found, there is no error message. Could be a missing
-// close-marker.
+/// Delete marker "marker[markerlen]" at the end of line "lnum".
+/// Delete 'commentstring' if it matches.
+/// If the marker is not found, there is no error message. Could be a missing
+/// close-marker.
static void foldDelMarker(buf_T *buf, linenr_T lnum, char_u *marker, size_t markerlen)
{
- char_u *newline;
- char_u *cms = buf->b_p_cms;
- char_u *cms2;
-
// end marker may be missing and fold extends below the last line
if (lnum > buf->b_ml.ml_line_count) {
return;
}
+
+ char_u *cms = buf->b_p_cms;
char_u *line = ml_get_buf(buf, lnum, false);
for (char_u *p = line; *p != NUL; p++) {
if (STRNCMP(p, marker, markerlen) != 0) {
@@ -1754,11 +1647,11 @@ static void foldDelMarker(buf_T *buf, linenr_T lnum, char_u *marker, size_t mark
// Found the marker, include a digit if it's there.
size_t len = markerlen;
if (ascii_isdigit(p[len])) {
- ++len;
+ len++;
}
if (*cms != NUL) {
// Also delete 'commentstring' if it matches.
- cms2 = (char_u *)strstr((char *)cms, "%s");
+ char_u *cms2 = (char_u *)strstr((char *)cms, "%s");
if (p - line >= cms2 - cms
&& STRNCMP(p - (cms2 - cms), cms, cms2 - cms) == 0
&& STRNCMP(p + len, cms2 + 2, STRLEN(cms2 + 2)) == 0) {
@@ -1768,12 +1661,12 @@ static void foldDelMarker(buf_T *buf, linenr_T lnum, char_u *marker, size_t mark
}
if (u_save(lnum - 1, lnum + 1) == OK) {
// Make new line: text-before-marker + text-after-marker
- newline = xmalloc(STRLEN(line) - len + 1);
+ char_u *newline = xmalloc(STRLEN(line) - len + 1);
assert(p >= line);
memcpy(newline, line, (size_t)(p - line));
STRCPY(newline + (p - line), p + len);
ml_replace_buf(buf, lnum, newline, false);
- extmark_splice_cols(buf, (int)lnum-1, (int)(p - line),
+ extmark_splice_cols(buf, (int)lnum - 1, (int)(p - line),
(int)len, 0, kExtmarkUndo);
}
break;
@@ -1794,26 +1687,23 @@ char_u *get_foldtext(win_T *wp, linenr_T lnum, linenr_T lnume, foldinfo_T foldin
{
char_u *text = NULL;
// an error occurred when evaluating 'fdt' setting
- static int got_fdt_error = FALSE;
+ static bool got_fdt_error = false;
int save_did_emsg = did_emsg;
static win_T *last_wp = NULL;
static linenr_T last_lnum = 0;
if (last_wp == NULL || last_wp != wp || last_lnum > lnum || last_lnum == 0) {
// window changed, try evaluating foldtext setting once again
- got_fdt_error = FALSE;
+ got_fdt_error = false;
}
if (!got_fdt_error) {
// a previous error should not abort evaluating 'foldexpr'
- did_emsg = FALSE;
+ did_emsg = false;
}
if (*wp->w_p_fdt != NUL) {
char dashes[MAX_LEVEL + 2];
- win_T *save_curwin;
- int level;
- char_u *p;
// Set "v:foldstart" and "v:foldend".
set_vim_var_nr(VV_FOLDSTART, (varnumber_T)lnum);
@@ -1821,7 +1711,7 @@ char_u *get_foldtext(win_T *wp, linenr_T lnum, linenr_T lnume, foldinfo_T foldin
// Set "v:folddashes" to a string of "level" dashes.
// Set "v:foldlevel" to "level".
- level = foldinfo.fi_level;
+ int level = foldinfo.fi_level;
if (level > (int)sizeof(dashes) - 1) {
level = (int)sizeof(dashes) - 1;
}
@@ -1832,16 +1722,18 @@ char_u *get_foldtext(win_T *wp, linenr_T lnum, linenr_T lnume, foldinfo_T foldin
// skip evaluating foldtext on errors
if (!got_fdt_error) {
- save_curwin = curwin;
+ win_T *save_curwin = curwin;
curwin = wp;
curbuf = wp->w_buffer;
emsg_silent++; // handle exceptions, but don't display errors
- text = eval_to_string_safe(wp->w_p_fdt, NULL, was_set_insecurely(wp, "foldtext", OPT_LOCAL));
+ text =
+ (char_u *)eval_to_string_safe((char *)wp->w_p_fdt, NULL,
+ was_set_insecurely(wp, "foldtext", OPT_LOCAL));
emsg_silent--;
if (text == NULL || did_emsg) {
- got_fdt_error = TRUE;
+ got_fdt_error = true;
}
curwin = save_curwin;
@@ -1858,17 +1750,18 @@ char_u *get_foldtext(win_T *wp, linenr_T lnum, linenr_T lnume, foldinfo_T foldin
if (text != NULL) {
// Replace unprintable characters, if there are any. But
// replace a TAB with a space.
+ char_u *p;
for (p = text; *p != NUL; p++) {
- int len = utfc_ptr2len(p);
+ int len = utfc_ptr2len((char *)p);
if (len > 1) {
- if (!vim_isprintc(utf_ptr2char(p))) {
+ if (!vim_isprintc(utf_ptr2char((char *)p))) {
break;
}
p += len - 1;
} else if (*p == TAB) {
*p = ' ';
- } else if (ptr2cells(p) > 1) {
+ } else if (ptr2cells((char *)p) > 1) {
break;
}
}
@@ -1892,18 +1785,11 @@ char_u *get_foldtext(win_T *wp, linenr_T lnum, linenr_T lnume, foldinfo_T foldin
}
// foldtext_cleanup() {{{2
-/*
- * Remove 'foldmarker' and 'commentstring' from "str" (in-place).
- */
+/// Remove 'foldmarker' and 'commentstring' from "str" (in-place).
void foldtext_cleanup(char_u *str)
{
- char_u *s;
- char_u *p;
- bool did1 = false;
- bool did2 = false;
-
// Ignore leading and trailing white space in 'commentstring'.
- char_u *cms_start = skipwhite(curbuf->b_p_cms);
+ char_u *cms_start = (char_u *)skipwhite((char *)curbuf->b_p_cms);
size_t cms_slen = STRLEN(cms_start);
while (cms_slen > 0 && ascii_iswhite(cms_start[cms_slen - 1])) {
--cms_slen;
@@ -1922,13 +1808,16 @@ void foldtext_cleanup(char_u *str)
}
// skip "%s" and white space after it
- s = skipwhite(cms_end + 2);
+ char_u *s = (char_u *)skipwhite((char *)cms_end + 2);
cms_elen -= (size_t)(s - cms_end);
cms_end = s;
}
parseMarker(curwin);
- for (s = str; *s != NUL;) {
+ bool did1 = false;
+ bool did2 = false;
+
+ for (char_u *s = str; *s != NUL;) {
size_t len = 0;
if (STRNCMP(s, curwin->w_p_fmr, foldstartmarkerlen) == 0) {
len = foldstartmarkerlen;
@@ -1937,13 +1826,13 @@ void foldtext_cleanup(char_u *str)
}
if (len > 0) {
if (ascii_isdigit(s[len])) {
- ++len;
+ len++;
}
// May remove 'commentstring' start. Useful when it's a double
// quote and we already removed a double quote.
- for (p = s; p > str && ascii_iswhite(p[-1]); p--) {
- }
+ char_u *p;
+ for (p = s; p > str && ascii_iswhite(p[-1]); p--) {}
if (p >= str + cms_slen
&& STRNCMP(p - cms_slen, cms_start, cms_slen) == 0) {
len += (size_t)(s - p) + cms_slen;
@@ -1961,7 +1850,7 @@ void foldtext_cleanup(char_u *str)
}
if (len != 0) {
while (ascii_iswhite(s[len])) {
- ++len;
+ len++;
}
STRMOVE(s, s + len);
} else {
@@ -1974,16 +1863,10 @@ void foldtext_cleanup(char_u *str)
// Function declarations. {{{2
// foldUpdateIEMS() {{{2
-/*
- * Update the folding for window "wp", at least from lines "top" to "bot".
- * IEMS = "Indent Expr Marker Syntax"
- */
+/// Update the folding for window "wp", at least from lines "top" to "bot".
+/// IEMS = "Indent Expr Marker Syntax"
static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot)
{
- fline_T fline;
- LevelGetter getlevel = NULL;
- fold_T *fp;
-
// Avoid problems when being called recursively.
if (invalid_top != (linenr_T)0) {
return;
@@ -1995,7 +1878,7 @@ static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot)
bot = wp->w_buffer->b_ml.ml_line_count;
wp->w_foldinvalid = false;
- // Mark all folds a maybe-small.
+ // Mark all folds as maybe-small.
setSmallMaybe(&wp->w_folds);
}
@@ -2015,6 +1898,8 @@ static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot)
top = wp->w_buffer->b_ml.ml_line_count;
}
+ fline_T fline;
+
fold_changed = false;
fline.wp = wp;
fline.off = 0;
@@ -2027,6 +1912,8 @@ static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot)
invalid_top = top;
invalid_bot = bot;
+ LevelGetter getlevel = NULL;
+
if (foldmethodIsMarker(wp)) {
getlevel = foldlevelMarker;
@@ -2062,7 +1949,7 @@ static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot)
// start one line back, because a "<1" may indicate the end of a
// fold in the topline
if (top > 1) {
- --fline.lnum;
+ fline.lnum--;
}
} else if (foldmethodIsSyntax(wp)) {
getlevel = foldlevelSyntax;
@@ -2070,6 +1957,12 @@ static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot)
getlevel = foldlevelDiff;
} else {
getlevel = foldlevelIndent;
+ // Start one line back, because if the line above "top" has an
+ // undefined fold level, folding it relies on the line under it,
+ // which is "top".
+ if (top > 1) {
+ fline.lnum--;
+ }
}
// Backup to a line for which the fold level is defined. Since it's
@@ -2086,13 +1979,11 @@ static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot)
}
}
- /*
- * If folding is defined by the syntax, it is possible that a change in
- * one line will cause all sub-folds of the current fold to change (e.g.,
- * closing a C-style comment can cause folds in the subsequent lines to
- * appear). To take that into account we should adjust the value of "bot"
- * to point to the end of the current fold:
- */
+ // If folding is defined by the syntax, it is possible that a change in
+ // one line will cause all sub-folds of the current fold to change (e.g.,
+ // closing a C-style comment can cause folds in the subsequent lines to
+ // appear). To take that into account we should adjust the value of "bot"
+ // to point to the end of the current fold:
if (foldlevelSyntax == getlevel) {
garray_T *gap = &wp->w_folds;
fold_T *fpn = NULL;
@@ -2104,7 +1995,7 @@ static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot)
if (!foldFind(gap, lnum_rel, &fpn)) {
break;
}
- ++current_fdl;
+ current_fdl++;
fold_start_lnum += fpn->fd_top;
gap = &fpn->fd_nested;
@@ -2125,6 +2016,9 @@ static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot)
if (start > end && end < wp->w_buffer->b_ml.ml_line_count) {
end = start;
}
+
+ fold_T *fp;
+
while (!got_int) {
// Always stop at the end of the file ("end" can be past the end of
// the file).
@@ -2169,7 +2063,7 @@ static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot)
if (fline.lnum == wp->w_buffer->b_ml.ml_line_count) {
break;
}
- ++fline.lnum;
+ fline.lnum++;
fline.lvl = fline.lvl_next;
getlevel(&fline);
}
@@ -2227,23 +2121,12 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
const linenr_T startlnum, fline_T *const flp,
LevelGetter getlevel, linenr_T bot, const char topflags)
{
- linenr_T ll;
fold_T *fp = NULL;
- fold_T *fp2;
- int lvl = level;
- linenr_T startlnum2 = startlnum;
- const linenr_T firstlnum = flp->lnum; // first lnum we got
- int i;
- bool finish = false;
- const linenr_T linecount = flp->wp->w_buffer->b_ml.ml_line_count - flp->off;
- int concat;
-
- /*
- * If using the marker method, the start line is not the start of a fold
- * at the level we're dealing with and the level is non-zero, we must use
- * the previous fold. But ignore a fold that starts at or below
- * startlnum, it must be deleted.
- */
+
+ // If using the marker method, the start line is not the start of a fold
+ // at the level we're dealing with and the level is non-zero, we must use
+ // the previous fold. But ignore a fold that starts at or below
+ // startlnum, it must be deleted.
if (getlevel == foldlevelMarker && flp->start <= flp->lvl - level
&& flp->lvl > 0) {
(void)foldFind(gap, startlnum - 1, &fp);
@@ -2254,17 +2137,22 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
}
}
- /*
- * Loop over all lines in this fold, or until "bot" is hit.
- * Handle nested folds inside of this fold.
- * "flp->lnum" is the current line. When finding the end of the fold, it
- * is just below the end of the fold.
- * "*flp" contains the level of the line "flp->lnum" or a following one if
- * there are lines with an invalid fold level. "flp->lnum_save" is the
- * line number that was used to get the fold level (below "flp->lnum" when
- * it has an invalid fold level). When called the fold level is always
- * valid, thus "flp->lnum_save" is equal to "flp->lnum".
- */
+ fold_T *fp2;
+ int lvl = level;
+ linenr_T startlnum2 = startlnum;
+ const linenr_T firstlnum = flp->lnum; // first lnum we got
+ bool finish = false;
+ const linenr_T linecount = flp->wp->w_buffer->b_ml.ml_line_count - flp->off;
+
+ // Loop over all lines in this fold, or until "bot" is hit.
+ // Handle nested folds inside of this fold.
+ // "flp->lnum" is the current line. When finding the end of the fold, it
+ // is just below the end of the fold.
+ // "*flp" contains the level of the line "flp->lnum" or a following one if
+ // there are lines with an invalid fold level. "flp->lnum_save" is the
+ // line number that was used to get the fold level (below "flp->lnum" when
+ // it has an invalid fold level). When called the fold level is always
+ // valid, thus "flp->lnum_save" is equal to "flp->lnum".
flp->lnum_save = flp->lnum;
while (!got_int) {
// Updating folds can be slow, check for CTRL-C.
@@ -2294,15 +2182,15 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
&& getlevel != foldlevelSyntax) {
break;
}
- i = 0;
+ int i = 0;
fp2 = fp;
if (lvl >= level) {
// Compute how deep the folds currently are, if it's deeper
// than "lvl" then some must be deleted, need to update
// at least one nested fold.
- ll = flp->lnum - fp->fd_top;
+ int ll = flp->lnum - fp->fd_top;
while (foldFind(&fp2->fd_nested, ll, &fp2)) {
- ++i;
+ i++;
ll -= fp2->fd_top;
}
}
@@ -2327,13 +2215,12 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
|| flp->start != 0
|| flp->had_end <= MAX_LEVEL
|| flp->lnum == linecount)) {
- /*
- * Remove or update folds that have lines between startlnum and
- * firstlnum.
- */
+ // Remove or update folds that have lines between startlnum and
+ // firstlnum.
while (!got_int) {
// set concat to 1 if it's allowed to concatenate this fold
// with a previous one that touches it.
+ int concat;
if (flp->start != 0 || flp->had_end <= MAX_LEVEL) {
concat = 0;
} else {
@@ -2376,6 +2263,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
}
fp->fd_len += fp->fd_top - firstlnum;
fp->fd_top = firstlnum;
+ fp->fd_small = kNone;
fold_changed = true;
} else if ((flp->start != 0 && lvl == level)
|| (firstlnum != startlnum)) {
@@ -2400,7 +2288,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
}
foldRemove(flp->wp, &fp->fd_nested, breakstart - fp->fd_top,
breakend - fp->fd_top);
- i = (int)(fp - (fold_T *)gap->ga_data);
+ int i = (int)(fp - (fold_T *)gap->ga_data);
foldSplit(flp->wp->w_buffer, gap, i, breakstart, breakend - 1);
fp = (fold_T *)gap->ga_data + i + 1;
// If using the "marker" or "syntax" method, we
@@ -2413,7 +2301,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
}
}
if (fp->fd_top == startlnum && concat) {
- i = (int)(fp - (fold_T *)gap->ga_data);
+ int i = (int)(fp - (fold_T *)gap->ga_data);
if (i != 0) {
fp2 = fp - 1;
if (fp2->fd_top + fp2->fd_len == fp->fd_top) {
@@ -2442,6 +2330,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
} else {
// Insert new fold. Careful: ga_data may be NULL and it
// may change!
+ int i;
if (gap->ga_len == 0) {
i = 0;
} else {
@@ -2482,17 +2371,13 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
}
if (lvl < level || flp->lnum > linecount) {
- /*
- * Found a line with a lower foldlevel, this fold ends just above
- * "flp->lnum".
- */
+ // Found a line with a lower foldlevel, this fold ends just above
+ // "flp->lnum".
break;
}
- /*
- * The fold includes the line "flp->lnum" and "flp->lnum_save".
- * Check "fp" for safety.
- */
+ // The fold includes the line "flp->lnum" and "flp->lnum_save".
+ // Check "fp" for safety.
if (lvl > level && fp != NULL) {
// There is a nested fold, handle it recursively.
// At least do one line (can happen when finish is true).
@@ -2504,7 +2389,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
// this fold.
flp->lnum = flp->lnum_save - fp->fd_top;
flp->off += fp->fd_top;
- i = (int)(fp - (fold_T *)gap->ga_data);
+ int i = (int)(fp - (fold_T *)gap->ga_data);
bot = foldUpdateIEMSRecurse(&fp->fd_nested, level + 1,
startlnum2 - fp->fd_top, flp, getlevel,
bot - fp->fd_top, fp->fd_flags);
@@ -2517,14 +2402,12 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
// This fold may end at the same line, don't incr. flp->lnum.
} else {
- /*
- * Get the level of the next line, then continue the loop to check
- * if it ends there.
- * Skip over undefined lines, to find the foldlevel after it.
- * For the last line in the file the foldlevel is always valid.
- */
+ // Get the level of the next line, then continue the loop to check
+ // if it ends there.
+ // Skip over undefined lines, to find the foldlevel after it.
+ // For the last line in the file the foldlevel is always valid.
flp->lnum = flp->lnum_save;
- ll = flp->lnum + 1;
+ int ll = flp->lnum + 1;
while (!got_int) {
// Make the previous level available to foldlevel().
prev_lnum = flp->lnum;
@@ -2555,11 +2438,9 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
return bot;
}
- /*
- * Get here when:
- * lvl < level: the folds ends just above "flp->lnum"
- * lvl >= level: fold continues below "bot"
- */
+ // Get here when:
+ // lvl < level: the folds ends just above "flp->lnum"
+ // lvl >= level: fold continues below "bot"
// Current fold at least extends until lnum.
if (fp->fd_len < flp->lnum - fp->fd_top) {
@@ -2591,7 +2472,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
} else {
// indent or expr method: split fold to create a new one
// below bot
- i = (int)(fp - (fold_T *)gap->ga_data);
+ int i = (int)(fp - (fold_T *)gap->ga_data);
foldSplit(flp->wp->w_buffer, gap, i, flp->lnum, bot);
fp = (fold_T *)gap->ga_data + i;
}
@@ -2614,7 +2495,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
// Make fold that includes lnum start at lnum.
foldMarkAdjustRecurse(flp->wp, &fp2->fd_nested,
(linenr_T)0, (flp->lnum - fp2->fd_top - 1),
- (linenr_T)MAXLNUM, (fp2->fd_top-flp->lnum));
+ (linenr_T)MAXLNUM, (fp2->fd_top - flp->lnum));
fp2->fd_len -= flp->lnum - fp2->fd_top;
fp2->fd_top = flp->lnum;
fold_changed = true;
@@ -2640,16 +2521,12 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
}
// foldInsert() {{{2
-/*
- * Insert a new fold in "gap" at position "i".
- */
+/// Insert a new fold in "gap" at position "i".
static void foldInsert(garray_T *gap, int i)
{
- fold_T *fp;
-
ga_grow(gap, 1);
- fp = (fold_T *)gap->ga_data + i;
+ fold_T *fp = (fold_T *)gap->ga_data + i;
if (gap->ga_len > 0 && i < gap->ga_len) {
memmove(fp + 1, fp, sizeof(fold_T) * (size_t)(gap->ga_len - i));
}
@@ -2658,13 +2535,11 @@ static void foldInsert(garray_T *gap, int i)
}
// foldSplit() {{{2
-/*
- * Split the "i"th fold in "gap", which starts before "top" and ends below
- * "bot" in two pieces, one ending above "top" and the other starting below
- * "bot".
- * The caller must first have taken care of any nested folds from "top" to
- * "bot"!
- */
+/// Split the "i"th fold in "gap", which starts before "top" and ends below
+/// "bot" in two pieces, one ending above "top" and the other starting below
+/// "bot".
+/// The caller must first have taken care of any nested folds from "top" to
+/// "bot"!
static void foldSplit(buf_T *buf, garray_T *const gap, const int i, const linenr_T top,
const linenr_T bot)
{
@@ -2686,7 +2561,8 @@ static void foldSplit(buf_T *buf, garray_T *const gap, const int i, const linenr
// any between top and bot, they have been removed by the caller.
garray_T *const gap1 = &fp->fd_nested;
garray_T *const gap2 = &fp[1].fd_nested;
- if (foldFind(gap1, bot + 1 - fp->fd_top, &fp2)) {
+ (void)foldFind(gap1, bot + 1 - fp->fd_top, &fp2);
+ if (fp2 != NULL) {
const int len = (int)((fold_T *)gap1->ga_data + gap1->ga_len - fp2);
if (len > 0) {
ga_grow(gap2, len);
@@ -2704,32 +2580,30 @@ static void foldSplit(buf_T *buf, garray_T *const gap, const int i, const linenr
}
// foldRemove() {{{2
-/*
- * Remove folds within the range "top" to and including "bot".
- * Check for these situations:
- * 1 2 3
- * 1 2 3
- * top 2 3 4 5
- * 2 3 4 5
- * bot 2 3 4 5
- * 3 5 6
- * 3 5 6
- *
- * 1: not changed
- * 2: truncate to stop above "top"
- * 3: split in two parts, one stops above "top", other starts below "bot".
- * 4: deleted
- * 5: made to start below "bot".
- * 6: not changed
- */
+/// Remove folds within the range "top" to and including "bot".
+/// Check for these situations:
+/// 1 2 3
+/// 1 2 3
+/// top 2 3 4 5
+/// 2 3 4 5
+/// bot 2 3 4 5
+/// 3 5 6
+/// 3 5 6
+///
+/// 1: not changed
+/// 2: truncate to stop above "top"
+/// 3: split in two parts, one stops above "top", other starts below "bot".
+/// 4: deleted
+/// 5: made to start below "bot".
+/// 6: not changed
static void foldRemove(win_T *const wp, garray_T *gap, linenr_T top, linenr_T bot)
{
- fold_T *fp = NULL;
-
if (bot < top) {
return; // nothing to do
}
+ fold_T *fp = NULL;
+
while (gap->ga_len > 0) {
// Find fold that includes top or a following one.
if (foldFind(gap, top, &fp) && fp->fd_top < top) {
@@ -2786,35 +2660,35 @@ static void foldReverseOrder(garray_T *gap, const linenr_T start_arg, const line
}
// foldMoveRange() {{{2
-// Move folds within the inclusive range "line1" to "line2" to after "dest"
-// require "line1" <= "line2" <= "dest"
-//
-// There are the following situations for the first fold at or below line1 - 1.
-// 1 2 3 4
-// 1 2 3 4
-// line1 2 3 4
-// 2 3 4 5 6 7
-// line2 3 4 5 6 7
-// 3 4 6 7 8 9
-// dest 4 7 8 9
-// 4 7 8 10
-// 4 7 8 10
-//
-// In the following descriptions, "moved" means moving in the buffer, *and* in
-// the fold array.
-// Meanwhile, "shifted" just means moving in the buffer.
-// 1. not changed
-// 2. truncated above line1
-// 3. length reduced by line2 - line1, folds starting between the end of 3 and
-// dest are truncated and shifted up
-// 4. internal folds moved (from [line1, line2] to dest)
-// 5. moved to dest.
-// 6. truncated below line2 and moved.
-// 7. length reduced by line2 - dest, folds starting between line2 and dest are
-// removed, top is moved down by move_len.
-// 8. truncated below dest and shifted up.
-// 9. shifted up
-// 10. not changed
+/// Move folds within the inclusive range "line1" to "line2" to after "dest"
+/// require "line1" <= "line2" <= "dest"
+///
+/// There are the following situations for the first fold at or below line1 - 1.
+/// 1 2 3 4
+/// 1 2 3 4
+/// line1 2 3 4
+/// 2 3 4 5 6 7
+/// line2 3 4 5 6 7
+/// 3 4 6 7 8 9
+/// dest 4 7 8 9
+/// 4 7 8 10
+/// 4 7 8 10
+///
+/// In the following descriptions, "moved" means moving in the buffer, *and* in
+/// the fold array.
+/// Meanwhile, "shifted" just means moving in the buffer.
+/// 1. not changed
+/// 2. truncated above line1
+/// 3. length reduced by line2 - line1, folds starting between the end of 3 and
+/// dest are truncated and shifted up
+/// 4. internal folds moved (from [line1, line2] to dest)
+/// 5. moved to dest.
+/// 6. truncated below line2 and moved.
+/// 7. length reduced by line2 - dest, folds starting between line2 and dest are
+/// removed, top is moved down by move_len.
+/// 8. truncated below dest and shifted up.
+/// 9. shifted up
+/// 10. not changed
static void truncate_fold(win_T *const wp, fold_T *fp, linenr_T end)
{
// I want to stop *at here*, foldRemove() stops *above* top
@@ -2824,9 +2698,10 @@ static void truncate_fold(win_T *const wp, fold_T *fp, linenr_T end)
}
#define FOLD_END(fp) ((fp)->fd_top + (fp)->fd_len - 1)
+// -V:VALID_FOLD:V560
#define VALID_FOLD(fp, gap) \
((gap)->ga_len > 0 && (fp) < ((fold_T *)(gap)->ga_data + (gap)->ga_len))
-#define FOLD_INDEX(fp, gap) ((size_t)(fp - ((fold_T *)(gap)->ga_data)))
+#define FOLD_INDEX(fp, gap) ((size_t)((fp) - ((fold_T *)(gap)->ga_data)))
void foldMoveRange(win_T *const wp, garray_T *gap, const linenr_T line1, const linenr_T line2,
const linenr_T dest)
{
@@ -2929,35 +2804,32 @@ void foldMoveRange(win_T *const wp, garray_T *gap, const linenr_T line1, const l
#undef FOLD_INDEX
// foldMerge() {{{2
-/*
- * Merge two adjacent folds (and the nested ones in them).
- * This only works correctly when the folds are really adjacent! Thus "fp1"
- * must end just above "fp2".
- * The resulting fold is "fp1", nested folds are moved from "fp2" to "fp1".
- * Fold entry "fp2" in "gap" is deleted.
- */
+/// Merge two adjacent folds (and the nested ones in them).
+/// This only works correctly when the folds are really adjacent! Thus "fp1"
+/// must end just above "fp2".
+/// The resulting fold is "fp1", nested folds are moved from "fp2" to "fp1".
+/// Fold entry "fp2" in "gap" is deleted.
static void foldMerge(win_T *const wp, fold_T *fp1, garray_T *gap, fold_T *fp2)
{
fold_T *fp3;
fold_T *fp4;
- int idx;
garray_T *gap1 = &fp1->fd_nested;
garray_T *gap2 = &fp2->fd_nested;
// If the last nested fold in fp1 touches the first nested fold in fp2,
// merge them recursively.
- if (foldFind(gap1, fp1->fd_len - 1L, &fp3) && foldFind(gap2, 0L, &fp4)) {
+ if (foldFind(gap1, fp1->fd_len - 1, &fp3) && foldFind(gap2, 0L, &fp4)) {
foldMerge(wp, fp3, gap2, fp4);
}
// Move nested folds in fp2 to the end of fp1.
if (!GA_EMPTY(gap2)) {
ga_grow(gap1, gap2->ga_len);
- for (idx = 0; idx < gap2->ga_len; ++idx) {
+ for (int idx = 0; idx < gap2->ga_len; idx++) {
((fold_T *)gap1->ga_data)[gap1->ga_len]
= ((fold_T *)gap2->ga_data)[idx];
((fold_T *)gap1->ga_data)[gap1->ga_len].fd_top += fp1->fd_len;
- ++gap1->ga_len;
+ gap1->ga_len++;
}
gap2->ga_len = 0;
}
@@ -2968,23 +2840,20 @@ static void foldMerge(win_T *const wp, fold_T *fp1, garray_T *gap, fold_T *fp2)
}
// foldlevelIndent() {{{2
-/*
- * Low level function to get the foldlevel for the "indent" method.
- * Doesn't use any caching.
- * Returns a level of -1 if the foldlevel depends on surrounding lines.
- */
+/// Low level function to get the foldlevel for the "indent" method.
+/// Doesn't use any caching.
+///
+/// @return a level of -1 if the foldlevel depends on surrounding lines.
static void foldlevelIndent(fline_T *flp)
{
- char_u *s;
- buf_T *buf;
linenr_T lnum = flp->lnum + flp->off;
- buf = flp->wp->w_buffer;
- s = skipwhite(ml_get_buf(buf, lnum, false));
+ buf_T *buf = flp->wp->w_buffer;
+ char_u *s = (char_u *)skipwhite((char *)ml_get_buf(buf, lnum, false));
// empty line or lines starting with a character in 'foldignore': level
// depends on surrounding lines
- if (*s == NUL || vim_strchr(flp->wp->w_p_fdi, *s) != NULL) {
+ if (*s == NUL || vim_strchr((char *)flp->wp->w_p_fdi, *s) != NULL) {
// first and last line can't be undefined, use level 0
if (lnum == 1 || lnum == buf->b_ml.ml_line_count) {
flp->lvl = 0;
@@ -3000,10 +2869,8 @@ static void foldlevelIndent(fline_T *flp)
}
// foldlevelDiff() {{{2
-/*
- * Low level function to get the foldlevel for the "diff" method.
- * Doesn't use any caching.
- */
+/// Low level function to get the foldlevel for the "diff" method.
+/// Doesn't use any caching.
static void foldlevelDiff(fline_T *flp)
{
if (diff_infold(flp->wp, flp->lnum + flp->off)) {
@@ -3014,18 +2881,15 @@ static void foldlevelDiff(fline_T *flp)
}
// foldlevelExpr() {{{2
-/*
- * Low level function to get the foldlevel for the "expr" method.
- * Doesn't use any caching.
- * Returns a level of -1 if the foldlevel depends on surrounding lines.
- */
+/// Low level function to get the foldlevel for the "expr" method.
+/// Doesn't use any caching.
+///
+/// @return a level of -1 if the foldlevel depends on surrounding lines.
static void foldlevelExpr(fline_T *flp)
{
- win_T *win;
- int c;
linenr_T lnum = flp->lnum + flp->off;
- win = curwin;
+ win_T *win = curwin;
curwin = flp->wp;
curbuf = flp->wp->w_buffer;
set_vim_var_nr(VV_LNUM, (varnumber_T)lnum);
@@ -3040,7 +2904,9 @@ static void foldlevelExpr(fline_T *flp)
// KeyTyped may be reset to 0 when calling a function which invokes
// do_cmdline(). To make 'foldopen' work correctly restore KeyTyped.
const bool save_keytyped = KeyTyped;
- const int n = eval_foldexpr(flp->wp->w_p_fde, &c);
+
+ int c;
+ const int n = eval_foldexpr((char *)flp->wp->w_p_fde, &c);
KeyTyped = save_keytyped;
switch (c) {
@@ -3113,55 +2979,46 @@ static void foldlevelExpr(fline_T *flp)
}
// parseMarker() {{{2
-/*
- * Parse 'foldmarker' and set "foldendmarker", "foldstartmarkerlen" and
- * "foldendmarkerlen".
- * Relies on the option value to have been checked for correctness already.
- */
+/// Parse 'foldmarker' and set "foldendmarker", "foldstartmarkerlen" and
+/// "foldendmarkerlen".
+/// Relies on the option value to have been checked for correctness already.
static void parseMarker(win_T *wp)
{
- foldendmarker = vim_strchr(wp->w_p_fmr, ',');
+ foldendmarker = (char_u *)vim_strchr((char *)wp->w_p_fmr, ',');
foldstartmarkerlen = (size_t)(foldendmarker++ - wp->w_p_fmr);
foldendmarkerlen = STRLEN(foldendmarker);
}
// foldlevelMarker() {{{2
-/*
- * Low level function to get the foldlevel for the "marker" method.
- * "foldendmarker", "foldstartmarkerlen" and "foldendmarkerlen" must have been
- * set before calling this.
- * Requires that flp->lvl is set to the fold level of the previous line!
- * Careful: This means you can't call this function twice on the same line.
- * Doesn't use any caching.
- * Sets flp->start when a start marker was found.
- */
+/// Low level function to get the foldlevel for the "marker" method.
+/// "foldendmarker", "foldstartmarkerlen" and "foldendmarkerlen" must have been
+/// set before calling this.
+/// Requires that flp->lvl is set to the fold level of the previous line!
+/// Careful: This means you can't call this function twice on the same line.
+/// Doesn't use any caching.
+/// Sets flp->start when a start marker was found.
static void foldlevelMarker(fline_T *flp)
{
- char_u *startmarker;
- int cstart;
- int cend;
int start_lvl = flp->lvl;
- char_u *s;
- int n;
// cache a few values for speed
- startmarker = flp->wp->w_p_fmr;
- cstart = *startmarker;
- ++startmarker;
- cend = *foldendmarker;
+ char_u *startmarker = flp->wp->w_p_fmr;
+ int cstart = *startmarker;
+ startmarker++;
+ int cend = *foldendmarker;
// Default: no start found, next level is same as current level
flp->start = 0;
flp->lvl_next = flp->lvl;
- s = ml_get_buf(flp->wp->w_buffer, flp->lnum + flp->off, false);
+ char_u *s = ml_get_buf(flp->wp->w_buffer, flp->lnum + flp->off, false);
while (*s) {
if (*s == cstart
&& STRNCMP(s + 1, startmarker, foldstartmarkerlen - 1) == 0) {
// found startmarker: set flp->lvl
s += foldstartmarkerlen;
if (ascii_isdigit(*s)) {
- n = atoi((char *)s);
+ int n = atoi((char *)s);
if (n > 0) {
flp->lvl = n;
flp->lvl_next = n;
@@ -3172,16 +3029,16 @@ static void foldlevelMarker(fline_T *flp)
}
}
} else {
- ++flp->lvl;
- ++flp->lvl_next;
- ++flp->start;
+ flp->lvl++;
+ flp->lvl_next++;
+ flp->start++;
}
} else if (*s == cend && STRNCMP(s + 1, foldendmarker + 1,
foldendmarkerlen - 1) == 0) {
// found endmarker: set flp->lvl_next
s += foldendmarkerlen;
if (ascii_isdigit(*s)) {
- n = atoi((char *)s);
+ int n = atoi((char *)s);
if (n > 0) {
flp->lvl = n;
flp->lvl_next = n - 1;
@@ -3205,20 +3062,17 @@ static void foldlevelMarker(fline_T *flp)
}
// foldlevelSyntax() {{{2
-/*
- * Low level function to get the foldlevel for the "syntax" method.
- * Doesn't use any caching.
- */
+/// Low level function to get the foldlevel for the "syntax" method.
+/// Doesn't use any caching.
static void foldlevelSyntax(fline_T *flp)
{
linenr_T lnum = flp->lnum + flp->off;
- int n;
// Use the maximum fold level at the start of this line and the next.
flp->lvl = syn_get_foldlevel(flp->wp, lnum);
flp->start = 0;
if (lnum < flp->wp->w_buffer->b_ml.ml_line_count) {
- n = syn_get_foldlevel(flp->wp, lnum + 1);
+ int n = syn_get_foldlevel(flp->wp, lnum + 1);
if (n > flp->lvl) {
flp->start = n - flp->lvl; // fold(s) start here
flp->lvl = n;
@@ -3228,11 +3082,9 @@ static void foldlevelSyntax(fline_T *flp)
// functions for storing the fold state in a View {{{1
// put_folds() {{{2
-
-/*
- * Write commands to "fd" to restore the manual folds in window "wp".
- * Return FAIL if writing fails.
- */
+/// Write commands to "fd" to restore the manual folds in window "wp".
+///
+/// @return FAIL if writing fails.
int put_folds(FILE *fd, win_T *wp)
{
if (foldmethodIsManual(wp)) {
@@ -3252,10 +3104,9 @@ int put_folds(FILE *fd, win_T *wp)
}
// put_folds_recurse() {{{2
-/*
- * Write commands to "fd" to recreate manually created folds.
- * Returns FAIL when writing failed.
- */
+/// Write commands to "fd" to recreate manually created folds.
+///
+/// @return FAIL when writing failed.
static int put_folds_recurse(FILE *fd, garray_T *gap, linenr_T off)
{
fold_T *fp = (fold_T *)gap->ga_data;
@@ -3270,20 +3121,17 @@ static int put_folds_recurse(FILE *fd, garray_T *gap, linenr_T off)
|| put_eol(fd) == FAIL) {
return FAIL;
}
- ++fp;
+ fp++;
}
return OK;
}
// put_foldopen_recurse() {{{2
-/*
- * Write commands to "fd" to open and close manually opened/closed folds.
- * Returns FAIL when writing failed.
- */
+/// Write commands to "fd" to open and close manually opened/closed folds.
+///
+/// @return FAIL when writing failed.
static int put_foldopen_recurse(FILE *fd, win_T *wp, garray_T *gap, linenr_T off)
{
- int level;
-
fold_T *fp = (fold_T *)gap->ga_data;
for (int i = 0; i < gap->ga_len; i++) {
if (fp->fd_flags != FD_LEVEL) {
@@ -3309,7 +3157,7 @@ static int put_foldopen_recurse(FILE *fd, win_T *wp, garray_T *gap, linenr_T off
// Open or close the leaf according to the window foldlevel.
// Do not close a leaf that is already closed, as it will close
// the parent.
- level = foldLevelWin(wp, off + fp->fd_top);
+ int level = foldLevelWin(wp, off + fp->fd_top);
if ((fp->fd_flags == FD_CLOSED && wp->w_p_fdl >= level)
|| (fp->fd_flags != FD_CLOSED && wp->w_p_fdl < level)) {
if (put_fold_open_close(fd, fp, off) == FAIL) {
@@ -3318,20 +3166,19 @@ static int put_foldopen_recurse(FILE *fd, win_T *wp, garray_T *gap, linenr_T off
}
}
}
- ++fp;
+ fp++;
}
return OK;
}
// put_fold_open_close() {{{2
-/*
- * Write the open or close command to "fd".
- * Returns FAIL when writing failed.
- */
+/// Write the open or close command to "fd".
+///
+/// @return FAIL when writing failed.
static int put_fold_open_close(FILE *fd, fold_T *fp, linenr_T off)
{
- if (fprintf(fd, "%" PRId64, (int64_t)(fp->fd_top + off)) < 0
+ if (fprintf(fd, "%" PRIdLINENR, fp->fd_top + off) < 0
|| put_eol(fd) == FAIL
|| fprintf(fd, "normal! z%c",
fp->fd_flags == FD_CLOSED ? 'c' : 'o') < 0