From f091c34bac4b2748aa032455a9498a2a6f29c80e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Diego=20Aur=C3=A9lio=20Mesquita?= Date: Mon, 12 Nov 2018 18:30:47 -0200 Subject: [PATCH] new feature: bindable functions for toggling and jumping to "bookmarks" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Marco Diego Aurélio Mesquita Signed-off-by: Benno Schulenberg --- src/global.c | 10 ++++++++++ src/nano.c | 6 ++++++ src/nano.h | 4 ++++ src/proto.h | 3 +++ src/rcfile.c | 6 ++++++ src/search.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 76 insertions(+) diff --git a/src/global.c b/src/global.c index 6c9a981c..14f3c315 100644 --- a/src/global.c +++ b/src/global.c @@ -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); diff --git a/src/nano.c b/src/nano.c index bce2bf68..2dbf3528 100644 --- a/src/nano.c +++ b/src/nano.c @@ -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; } diff --git a/src/nano.h b/src/nano.h index a4a15d84..78e16e96 100644 --- a/src/nano.h +++ b/src/nano.h @@ -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 diff --git a/src/proto.h b/src/proto.h index 37e6a8b2..724e93c8 100644 --- a/src/proto.h +++ b/src/proto.h @@ -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. */ diff --git a/src/rcfile.c b/src/rcfile.c index 722a65f2..8b74886c 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -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")) diff --git a/src/search.c b/src/search.c index a3fe8347..314e6eaa 100644 --- a/src/search.c +++ b/src/search.c @@ -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 */