new feature: bindable functions for toggling and jumping to "bookmarks"

With the 'bookmark' function, the user can place a bookmark on any
line in the buffer.  Multiple lines can be bookmarked in this way.
With 'prevbookmark' and 'nextbookmark', the user can then easily
return to the bookmarked lines.  The search for a bookmark wraps
around, as if start and end of buffer are connected.

[However, in this implementation, when a bookmarked line is deleted,
the bookmark is deleted too.  This is undesirable.  Also, when such
a deleted line is pasted elsewhere, the bookmark reappears with it,
and when pasted multiple times, the bookmark will be there as many
times.  This is thoroughly undesirable.  These behaviors will be
changed in a later commit.]

A bookmark is not yet visible in any way.

This fulfills https://savannah.gnu.org/bugs/?57577.
Requested-by: Ken Tyler <kent@werple.net.au>

Signed-off-by: Marco Diego Aurélio Mesquita <marcodiegomesquita@gmail.com>
Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
master
Marco Diego Aurélio Mesquita 2018-11-12 18:30:47 -02:00 committed by Benno Schulenberg
parent fd9f6e4619
commit f091c34bac
6 changed files with 76 additions and 0 deletions

View File

@ -670,6 +670,9 @@ void shortcut_init(void)
#ifndef NANO_TINY
const char *recordmacro_gist = N_("Start/stop recording a macro");
const char *runmacro_gist = N_("Run the last recorded macro");
const char *bookmark_gist = N_("Set or remove a bookmark on the current line");
const char *prevbookmark_gist = N_("Go to previous bookmark");
const char *nextbookmark_gist = N_("Go to next bookmark");
#endif
const char *case_gist = N_("Toggle the case sensitivity of the search");
const char *reverse_gist = N_("Reverse the direction of the search");
@ -1023,6 +1026,13 @@ void shortcut_init(void)
add_to_funcs(run_macro, MMAIN,
N_("Run Macro"), WITHORSANS(runmacro_gist), BLANKAFTER, VIEW);
add_to_funcs(bookmark, MMAIN,
N_("Bookmark"), WITHORSANS(bookmark_gist), TOGETHER, NOVIEW);
add_to_funcs(to_prev_bookmark, MMAIN,
N_("Previous mark"), WITHORSANS(prevbookmark_gist), TOGETHER, NOVIEW);
add_to_funcs(to_next_bookmark, MMAIN,
N_("Next mark"), WITHORSANS(nextbookmark_gist), BLANKAFTER, NOVIEW);
add_to_funcs(zap_text, MMAIN,
N_("Zap Text"), WITHORSANS(zap_gist), BLANKAFTER, NOVIEW);

View File

@ -75,6 +75,9 @@ linestruct *make_new_node(linestruct *prevnode)
newnode->multidata = NULL;
#endif
newnode->lineno = (prevnode) ? prevnode->lineno + 1 : 1;
#ifndef NANO_TINY
newnode->bookmarked = FALSE;
#endif
return newnode;
}
@ -147,6 +150,9 @@ linestruct *copy_node(const linestruct *src)
dst->multidata = NULL;
#endif
dst->lineno = src->lineno;
#ifndef NANO_TINY
dst->bookmarked = src->bookmarked;
#endif
return dst;
}

View File

@ -292,6 +292,10 @@ typedef struct linestruct {
short *multidata;
/* Array of which multi-line regexes apply to this line. */
#endif
#ifndef NANO_TINY
bool bookmarked;
/* Whether the user bookmarked this line. */
#endif
} linestruct;
#ifndef NANO_TINY

View File

@ -482,6 +482,9 @@ void do_gotolinecolumn(ssize_t line, ssize_t column, bool use_answer,
void do_gotolinecolumn_void(void);
#ifndef NANO_TINY
void do_find_bracket(void);
void bookmark(void);
void to_prev_bookmark(void);
void to_next_bookmark(void);
#endif
/* Most functions in text.c. */

View File

@ -317,6 +317,12 @@ keystruct *strtosc(const char *input)
s->func = record_macro;
else if (!strcmp(input, "runmacro"))
s->func = run_macro;
else if (!strcmp(input, "bookmark"))
s->func = bookmark;
else if (!strcmp(input, "prevbookmark"))
s->func = to_prev_bookmark;
else if (!strcmp(input, "nextbookmark"))
s->func = to_next_bookmark;
else if (!strcmp(input, "undo"))
s->func = do_undo;
else if (!strcmp(input, "redo"))

View File

@ -977,4 +977,51 @@ void do_find_bracket(void)
openfile->current = was_current;
openfile->current_x = was_current_x;
}
/* Set a bookmark on the current line, or remove an existing one. */
void bookmark(void)
{
openfile->current->bookmarked = !openfile->current->bookmarked;
statusbar(openfile->current->bookmarked ?
_("Bookmarked this line") : _("Removed bookmark"));
}
/* Jump to the next or previous bookmark, if any. */
static void go_to_bookmark(bool forward)
{
linestruct *was_current = openfile->current;
linestruct *line = was_current;
do {
line = (forward ? line->next : line->prev);
if (line == NULL)
line = (forward ? openfile->filetop : openfile->filebot);
if (line == openfile->current) {
statusbar(line->bookmarked ?
_("This is the only bookmark") : _("No bookmark found"));
return;
}
} while (!line->bookmarked);
openfile->current = line;
openfile->current_x = 0;
edit_redraw(was_current, CENTERING);
statusbar(_("Jumped to bookmark"));
}
/* Jump to the first bookmark before the current line. */
void to_prev_bookmark(void)
{
go_to_bookmark(BACKWARD);
}
/* Jump to the first bookmark after the current line. */
void to_next_bookmark(void)
{
go_to_bookmark(FORWARD);
}
#endif /* !NANO_TINY */