nano-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/3 V14] new feature: bindable functions for toggling and jumping


From: Benno Schulenberg
Subject: [PATCH 1/3 V14] new feature: bindable functions for toggling and jumping to bookmarks
Date: Sun, 5 Apr 2020 19:37:09 +0200

From: Marco Diego Aurélio Mesquita <address@hidden>

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.

In this implementation, when a bookmarked line is deleted, the bookmark
is deleted too.  When such a deleted line is pasted elsewhere, the bookmark
reappears with it.

A bookmark is not visible in any way.

Signed-off-by: Marco Diego Aurélio Mesquita <address@hidden>
Signed-off-by: Benno Schulenberg <address@hidden>
---
 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 440db100..0d1b7606 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -90,6 +90,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;
 }
@@ -162,6 +165,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 6660d603..ec67a117 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -485,6 +485,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 205a925b..0ed61b52 100644
--- a/src/search.c
+++ b/src/search.c
@@ -983,4 +983,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 */
-- 
2.25.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]