Cleanup some folding.c

This commit is contained in:
rexy712 2022-07-13 12:19:08 -07:00
parent 8daf175b09
commit 0ac593a9ce
3 changed files with 24 additions and 25 deletions

View File

@ -20,8 +20,6 @@
#include "prototypes.h"
#include <string.h>
/* strlen */
#include <ctype.h>
/* isspace */
@ -66,7 +64,7 @@ linestruct *get_end_of_folded_segment(linestruct *line)
}
/* Get the span length of a folded_segment */
int folded_segment_length(linestruct *line)
int get_folded_segment_length(linestruct *line)
{
int i = 1;
@ -82,18 +80,17 @@ int folded_segment_length(linestruct *line)
/* Remove any folded_segments within the range [top, bottom].
* Returns true if any segments are removed. */
bool do_unfold_segments(linestruct *top, linestruct *bot)
bool do_unfold_segment(linestruct *line)
{
if (top != bot)
return FALSE;
/* Attempt to find a bracketed region first.
* If not, top is unmodified. */
if (!top->folded)
find_bracketed_region(openfile->current, &top, &bot);
if (!line->folded) {
linestruct *bot;
find_bracketed_region(openfile->current, &line, &bot);
}
if (top->folded) {
unfold_folded_segment(top);
if (line->folded) {
unfold_folded_segment(line);
move_cursor_to_proper_column();
return TRUE;
}
@ -108,17 +105,19 @@ void do_fold_segment(void)
{
linestruct *top, *bot;
get_range(&top, &bot);
if (do_unfold_segments(top, bot)) {
return;
}
/* When not selecting multiple lines, try to find bounding
* brackets to act as top and bot. */
if (top == bot)
if (top == bot) {
/* First try to unfold if this line/bracketed region is folded */
if (do_unfold_segment(top))
return;
/* When not selecting multiple lines, try to find bounding
* brackets to act as top and bot. */
if (!find_bracketed_region(openfile->current, &top, &bot)) {
statusline(AHEM, _("No valid region found for automatic fold"));
return;
}
}
for (linestruct* line = top;line != bot->next;line = line->next)
line->folded = TRUE;
@ -129,7 +128,8 @@ void do_fold_segment(void)
/* Place the cursor at the start of the fold segment.
* Anywhere else within the segment is invalid. */
openfile->current = get_start_of_folded_segment(openfile->current);
if (openfile->current->folded)
openfile->current = get_start_of_folded_segment(top);
openfile->mark = NULL;
refresh_needed = TRUE;

View File

@ -669,13 +669,12 @@ void flip_newbuffer(void);
void discard_buffer(void);
void do_cancel(void);
/* Folding related functions */
/* Most functions in folding.c. */
#ifdef ENABLE_FOLDING
void do_fold_segment(void);
void unfold_folded_segment(linestruct *line);
void unfold_folded_segment_from(linestruct *line);
void unfold_if_folded(linestruct* line);
linestruct *get_start_of_folded_segment(linestruct* line);
linestruct *get_end_of_folded_segment(linestruct* line);
int folded_segment_length(linestruct *line);
int get_folded_segment_length(linestruct *line);
#endif

View File

@ -2848,7 +2848,7 @@ void draw_row(int row, const char *converted, linestruct *line,
/* Draw a folded segment at the given row. */
int update_folded_line(linestruct *line, int row)
{
const int seg_len = folded_segment_length(line);
const int seg_len = get_folded_segment_length(line);
const int string_len = snprintf(NULL, 0, "%d lines folded", seg_len);
const int fill_length = (editwincols > string_len) ?
(editwincols - string_len) : 0;
@ -3149,7 +3149,7 @@ void draw_scrollbar(void)
int i = 0;
#ifdef ENABLE_FOLDING
if (line->folded)
coveredlines += folded_segment_length(line);
coveredlines += get_folded_segment_length(line);
else
#endif
{
@ -3161,7 +3161,7 @@ void draw_scrollbar(void)
line = get_next_visible_line(line);
#ifdef ENABLE_FOLDING
if (line->folded)
coveredlines += folded_segment_length(line);
coveredlines += get_folded_segment_length(line);
else
#endif
{
@ -3173,7 +3173,7 @@ void draw_scrollbar(void)
#ifdef ENABLE_FOLDING
for (int i = 0;i < editwinrows && line->next != NULL;++i) {
if (line->folded)
coveredlines += folded_segment_length(line);
coveredlines += get_folded_segment_length(line);
else
++coveredlines;
line = get_next_visible_line(line);