>From 18ccb04360b16641d3f5146a28e5df7a1e7990ef 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 1/3] new feature: bindable ability to toggle and jump to bookmarks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added infrastructure to toggle and jump to bookmarks. A buffer can have multiple bookmarks but it can only have one bookmark per line. It is also possible jump to next or previous bookmark. For now, bookmarks are not visible in any way. Toggle, go to previous and go to next bookmark are respectively bindable with "bookmark", "prevbookmark" and "nextbookmark". Signed-off-by: Marco Diego Aurélio Mesquita --- src/global.c | 16 ++++++++++++++++ src/nano.c | 6 ++++++ src/nano.h | 4 ++++ src/proto.h | 3 +++ src/search.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+) diff --git a/src/global.c b/src/global.c index a0d088c8..304d1adb 100644 --- a/src/global.c +++ b/src/global.c @@ -569,6 +569,9 @@ void shortcut_init(void) const char *copy_gist = N_("Copy current line (or marked region) and store it in cutbuffer"); const char *zap_gist = N_("Throw away the current line (or marked region)"); + 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"); const char *indent_gist = N_("Indent the current line (or marked lines)"); const char *unindent_gist = N_("Unindent the current line (or marked lines)"); const char *undo_gist = N_("Undo the last operation"); @@ -983,6 +986,13 @@ void shortcut_init(void) add_to_funcs(zap_text, MMAIN, N_("Zap Text"), WITHORSANS(zap_gist), BLANKAFTER, NOVIEW); + 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); + #ifdef ENABLE_COLOR if (!ISSET(RESTRICTED)) add_to_funcs(do_linter, MMAIN, @@ -1529,6 +1539,12 @@ sc *strtosc(const char *input) s->func = do_undo; else if (!strcasecmp(input, "redo")) s->func = do_redo; + else if (!strcasecmp(input, "bookmark")) + s->func = bookmark; + else if (!strcasecmp(input, "prevbookmark")) + s->func = to_prev_bookmark; + else if (!strcasecmp(input, "nextbookmark")) + s->func = to_next_bookmark; #endif else if (!strcasecmp(input, "left") || !strcasecmp(input, "back")) diff --git a/src/nano.c b/src/nano.c index e121722a..99e3cbed 100644 --- a/src/nano.c +++ b/src/nano.c @@ -78,6 +78,9 @@ filestruct *make_new_node(filestruct *prevnode) newnode->prev = prevnode; newnode->next = NULL; newnode->lineno = (prevnode != NULL) ? prevnode->lineno + 1 : 1; +#ifndef NANO_TINY + newnode->bookmarked = FALSE; +#endif #ifdef ENABLE_COLOR newnode->multidata = NULL; @@ -95,6 +98,9 @@ filestruct *copy_node(const filestruct *src) dst->next = src->next; dst->prev = src->prev; dst->lineno = src->lineno; +#ifndef NANO_TINY + dst->bookmarked = src->bookmarked; +#endif #ifdef ENABLE_COLOR dst->multidata = NULL; diff --git a/src/nano.h b/src/nano.h index 04aa3150..caa398cf 100644 --- a/src/nano.h +++ b/src/nano.h @@ -279,6 +279,10 @@ typedef struct filestruct { /* Next node. */ struct filestruct *prev; /* Previous node. */ +#ifndef NANO_TINY + bool bookmarked; + /* Whether the line is bookmarked. */ +#endif #ifdef ENABLE_COLOR short *multidata; /* Array of which multi-line regexes apply to this line. */ diff --git a/src/proto.h b/src/proto.h index 5923670f..0ca8ce88 100644 --- a/src/proto.h +++ b/src/proto.h @@ -488,6 +488,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/search.c b/src/search.c index a8b14291..a934e806 100644 --- a/src/search.c +++ b/src/search.c @@ -1022,4 +1022,52 @@ void do_find_bracket(void) } } } + +/* Toggle bookmark. */ +void bookmark(void) +{ + openfile->current->bookmarked = !openfile->current->bookmarked; + statusbar(openfile->current->bookmarked ? + _("Bookmarked") : + _("Removed bookmark")); +} + +/* Go to next or previous bookmark. */ +static void go_to_bookmark(bool forward) +{ + filestruct *line = openfile->current; + const filestruct *first = line; + + do { + line = forward ? line->next :line->prev; + + if (line == NULL) + line = forward ? openfile->fileage + : openfile->filebot; + + if (line == first) { + statusbar(line->bookmarked ? + _("No other bookmark found") : + _("No bookmark found")); + return; + } + } while(!line->bookmarked); + + openfile->current = line; + openfile->current_x = 0; + openfile->placewewant = 0; + refresh_needed = TRUE; +} + +/* Go to previous bookmark. */ +void to_prev_bookmark(void) +{ + go_to_bookmark(BACKWARD); +} + +/* Go to next bookmark. */ +void to_next_bookmark(void) +{ + go_to_bookmark(FORWARD); +} #endif /* !NANO_TINY */ -- 2.11.0