>From ad955746e30e4cfe0680849079d775c6bcefc7b2 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/2] 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 next and go to previous bookmark are respectively bindable with "bookmark", "bookmarknext" and "bookmarkprevious". Signed-off-by: Marco Diego Aurélio Mesquita --- doc/nano.texi | 9 +++++++++ doc/nanorc.5 | 9 +++++++++ src/global.c | 16 ++++++++++++++++ src/nano.c | 6 ++++++ src/nano.h | 4 ++++ src/proto.h | 3 +++ src/text.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 95 insertions(+) diff --git a/doc/nano.texi b/doc/nano.texi index d5d275dd..bef44d57 100644 --- a/doc/nano.texi +++ b/doc/nano.texi @@ -1153,6 +1153,15 @@ current cursor position. Throw away the current line (or the marked region). (This function is bound by default to .) address@hidden bookmark +Toggles bookmark fo current line. + address@hidden bookmarknext +Goes to next bookmark on the current buffer. + address@hidden bookmarkprevious +Goes to previous bookmark on the current buffer. + @item cutwordleft Cuts from the cursor position to the beginning of the preceding word. (This function is not bound by default. If your terminal produces diff --git a/doc/nanorc.5 b/doc/nanorc.5 index cad8b50d..f1f0dd38 100644 --- a/doc/nanorc.5 +++ b/doc/nanorc.5 @@ -510,6 +510,15 @@ current cursor position. Throw away the current line (or the marked region). (This function is bound by default to .) .TP +.B bookmark +Toggles bookmark fo current line. +.TP +.B bookmarknext +Goes to next bookmark on the current buffer. +.TP +.B bookmarkprevious +Goes to previous bookmark on the current buffer. +.TP .B cutwordleft Cuts from the cursor position to the beginning of the preceding word. (This function is not bound by default. If your terminal produces diff --git a/src/global.c b/src/global.c index a0d088c8..a39972a7 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_("Toggle bookmark"); + const char *nextbookmark_gist = N_("Next bookmark"); + const char *prevbookmark_gist = N_("Previous 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(bookmark_next, MMAIN, + N_("Next mark"), WITHORSANS(nextbookmark_gist), TOGETHER, NOVIEW); + add_to_funcs(bookmark_previous, MMAIN, + N_("Previous mark"), WITHORSANS(prevbookmark_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, "bookmarknext")) + s->func = bookmark_next; + else if (!strcasecmp(input, "bookmarkprevious")) + s->func = bookmark_previous; #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..f23f19ba 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 bookmark_next(void); +void bookmark_previous(void); #endif void do_delete(void); void do_backspace(void); diff --git a/src/text.c b/src/text.c index 7190de5e..272549b7 100644 --- a/src/text.c +++ b/src/text.c @@ -62,6 +62,54 @@ 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_book_mark(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; + refresh_needed = TRUE; +} + +/* Go to next bookmark. */ +void bookmark_next(void) +{ + go_to_book_mark(FORWARD); +} + +/* Go to previous bookmark. */ +void bookmark_previous(void) +{ + go_to_book_mark(BACKWARD); +} + #endif /* !NANO_TINY */ #if defined(ENABLE_COLOR) || defined(ENABLE_SPELLER) -- 2.11.0