>From 77689aa32c420e2c0eefe56051e2d0a199b228ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Diego=20Aur=C3=A9lio=20Mesquita?= Date: Tue, 30 Oct 2018 21:29:32 -0300 Subject: [PATCH] new feature: ability to toogle and jump to bookmarks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Toggling bookmarks is bound to M-2. A buffer can have multiple bookmarks but it can only have one bookmark per line. Jumping to next bookmark is bound to M-' and jumping to previous bookmark is bound to M-". (Bookmarks are not visible in any way.) Signed-off-by: Marco Diego Aurélio Mesquita --- src/global.c | 10 ++++++++++ src/nano.c | 2 ++ src/nano.h | 4 ++++ src/proto.h | 3 +++ src/text.c | 41 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+) diff --git a/src/global.c b/src/global.c index e1d7930f..74cfe42f 100644 --- a/src/global.c +++ b/src/global.c @@ -840,6 +840,13 @@ void shortcut_init(void) /* Place this one here only in the tiny version; otherwise further down. */ add_to_funcs(do_savefile, MMAIN, N_("Save"), WITHORSANS(savefile_gist), BLANKAFTER, NOVIEW); + + add_to_funcs(do_bookmark, MMAIN, + N_("Bookmark"), WITHORSANS("Toggle bookmark"), BLANKAFTER, NOVIEW); + add_to_funcs(do_bookmark_next, MMAIN, + N_("Next mark"), WITHORSANS("Go to next bookmark"), BLANKAFTER, NOVIEW); + add_to_funcs(do_bookmark_previous, MMAIN, + N_("Previous mark"), WITHORSANS("Go to previous bookmark"), BLANKAFTER, NOVIEW); #endif #ifdef ENABLE_BROWSER @@ -1140,6 +1147,9 @@ void shortcut_init(void) add_to_sclist(MMAIN, "M-A", 0, do_mark, 0); add_to_sclist(MMAIN, "^6", 0, do_mark, 0); add_to_sclist(MMAIN, "^^", 0, do_mark, 0); + add_to_sclist(MMAIN, "M-2", 0, do_bookmark, 0); + add_to_sclist(MMAIN, "M-'", 0, do_bookmark_next, 0); + add_to_sclist(MMAIN, "M-\"", 0, do_bookmark_previous, 0); add_to_sclist(MMAIN, "M-6", 0, do_copy_text, 0); add_to_sclist(MMAIN, "M-^", 0, do_copy_text, 0); add_to_sclist(MMAIN, "M-}", 0, do_indent, 0); diff --git a/src/nano.c b/src/nano.c index 3f475905..de44cce4 100644 --- a/src/nano.c +++ b/src/nano.c @@ -79,6 +79,8 @@ filestruct *make_new_node(filestruct *prevnode) newnode->next = NULL; newnode->lineno = (prevnode != NULL) ? prevnode->lineno + 1 : 1; + newnode->bookmarked = FALSE; + #ifdef ENABLE_COLOR newnode->multidata = NULL; #endif diff --git a/src/nano.h b/src/nano.h index 3007881d..23478250 100644 --- a/src/nano.h +++ b/src/nano.h @@ -279,6 +279,10 @@ typedef struct filestruct { /* Next node. */ struct filestruct *prev; /* Previous node. */ + bool bookmarked; + /* Whether this line is bookmarked. */ + size_t bookmark_x; + /* Bookmark position for this line. */ #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 2c056c52..01d3b993 100644 --- a/src/proto.h +++ b/src/proto.h @@ -494,6 +494,9 @@ void do_find_bracket(void); /* Most functions in text.c. */ #ifndef NANO_TINY void do_mark(void); +void do_bookmark(void); +void do_bookmark_next(void); +void do_bookmark_previous(void); #endif void do_delete(void); void do_backspace(void); diff --git a/src/text.c b/src/text.c index 8302c7dd..42a533f3 100644 --- a/src/text.c +++ b/src/text.c @@ -68,6 +68,47 @@ void do_mark(void) refresh_needed = TRUE; } } + +/* Toggles bookmark. */ +void do_bookmark(void) +{ + openfile->current->bookmarked = !openfile->current->bookmarked; + if (openfile->current->bookmarked) + openfile->current->bookmark_x = openfile->current_x; + + statusbar(openfile->current->bookmarked ? + _("Bookmark added.") : + _("Bookmark removed.")); +} + +/* Goes to next or previous bookmark. */ +void go_to_book_mark(bool next) +{ + filestruct *aux = openfile->current->next; + + while(aux != NULL && !aux->bookmarked) + aux = next ? aux->next : aux->prev; + + if (aux == NULL) + return; + + openfile->current = aux; + openfile->current_x = aux->bookmark_x; + refresh_needed = TRUE; +} + +/* Goes to next bookmark. */ +void do_bookmark_next(void) +{ + go_to_book_mark(TRUE); +} + +/* Goes to previous bookmark. */ +void do_bookmark_previous(void) +{ + go_to_book_mark(FALSE); +} + #endif /* !NANO_TINY */ #if defined(ENABLE_COLOR) || defined(ENABLE_SPELLER) -- 2.11.0