From 763d896500c4fc5f9c2de02e5fcac9a2f7b05502 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 toogle 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/text.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+) diff --git a/src/global.c b/src/global.c index a0d088c..c5bccbb 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_previous_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 e121722..99e3cbe 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 04aa315..caa398c 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 5923670..5addc1c 100644 --- a/src/proto.h +++ b/src/proto.h @@ -493,6 +493,9 @@ void do_find_bracket(void); /* Most functions in text.c. */ #ifndef NANO_TINY void do_mark(void); +void bookmark(void); +void to_previous_bookmark(void); +void to_next_bookmark(void); #endif void do_delete(void); void do_backspace(void); diff --git a/src/text.c b/src/text.c index 7190de5..a102eea 100644 --- a/src/text.c +++ b/src/text.c @@ -62,6 +62,55 @@ void do_mark(void) refresh_needed = TRUE; } } + +/* Toggle bookmark. */ +void bookmark(void) +{ + openfile->current->bookmarked = !openfile->current->bookmarked; + statusbar(openfile->current->bookmarked ? + _("Bookmark added.") : + _("Bookmark removed.")); +} + +/* Go to next or previous bookmark. */ +static void go_to_bookmark(bool next) +{ + filestruct *current = openfile->current; + const filestruct *first = current; + + do { + current = next ? current->next : current->prev; + + if (current == NULL) + current = next ? openfile->fileage + : openfile->filebot; + + if (current == first) { + statusbar(current->bookmarked ? + _("No other bookmark found.") : + _("No bookmark found.")); + return; + } + } while(!current->bookmarked); + + openfile->current = current; + openfile->current_x = 0; + openfile->placewewant = 0; + refresh_needed = TRUE; +} + +/* Go to previous bookmark. */ +void to_previous_bookmark(void) +{ + go_to_bookmark(BACKWARD); +} + +/* Go to next bookmark. */ +void to_next_bookmark(void) +{ + go_to_bookmark(FORWARD); +} + #endif /* !NANO_TINY */ #if defined(ENABLE_COLOR) || defined(ENABLE_SPELLER) -- 2.7.4