nano-devel
[Top][All Lists]
Advanced

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

[Nano-devel] [PATCH V2] possible feature: the ability to set an anchor t


From: Benno Schulenberg
Subject: [Nano-devel] [PATCH V2] possible feature: the ability to set an anchor to which one can jump back
Date: Wed, 25 Sep 2019 19:18:41 +0200

A first press of the anchor key (M-' by default) will place the anchor
on the current line.  When M-' is pressed again later, the cursor will
jump back to the start of the line where the anchor was placed, and
then the anchor is cleared.

The anchor is thus like an ephemeral bookmark: it disappears as soon
as you return to it.  This is done so that it can be managed with a
single key.

The line number of the anchor line is adjusted when lines are added or
deleted before it.

The anchor is not visible in any way.

V2: * The line where the anchor was placed can be deleted, the anchor remains.
    * The anchor is cleared as soon the user returns to it.
    * The x position is not remembered -- it would make the compensation code
      much more messy.
---
 src/cut.c    |  4 ++++
 src/files.c  |  1 +
 src/global.c |  5 +++++
 src/nano.c   | 15 ++++++++++++++-
 src/nano.h   |  2 ++
 src/proto.h  |  1 +
 src/text.c   | 20 ++++++++++++++++++++
 7 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/src/cut.c b/src/cut.c
index 8481a3df..db93df46 100644
--- a/src/cut.c
+++ b/src/cut.c
@@ -87,6 +87,10 @@ void do_deletion(undo_type action)
                        openfile->mark = openfile->current;
                        openfile->mark_x += openfile->current_x;
                }
+
+               /* When there's an anchor after the current line, adjust it. */
+               if (openfile->anchored > openfile->current->lineno)
+                       openfile->anchored--;
 #endif
                unlink_node(joining);
                renumber_from(openfile->current);
diff --git a/src/files.c b/src/files.c
index 6ace5766..7490a58f 100644
--- a/src/files.c
+++ b/src/files.c
@@ -107,6 +107,7 @@ void make_new_buffer(void)
 #endif
 #ifndef NANO_TINY
        openfile->mark = NULL;
+       openfile->anchored = 0;
 
        openfile->fmt = NIX_FILE;
 
diff --git a/src/global.c b/src/global.c
index 4a99643b..d0771110 100644
--- a/src/global.c
+++ b/src/global.c
@@ -620,6 +620,7 @@ void shortcut_init(void)
                N_("Comment/uncomment the current line (or marked lines)");
 #endif
        const char *savefile_gist = N_("Save file without prompting");
+       const char *anchor_gist = N_("Place an anchor, or jump to it and clear 
it");
        const char *findprev_gist = N_("Search next occurrence backward");
        const char *findnext_gist = N_("Search next occurrence forward");
 #ifndef NANO_TINY
@@ -970,6 +971,9 @@ void shortcut_init(void)
        add_to_funcs(run_macro, MMAIN,
                N_("Run Macro"), WITHORSANS(runmacro_gist), BLANKAFTER, VIEW);
 
+       add_to_funcs(do_anchor, MMAIN,
+               N_("Anchor"), WITHORSANS(anchor_gist), BLANKAFTER, VIEW);
+
        add_to_funcs(zap_text, MMAIN,
                N_("Zap Text"), WITHORSANS(zap_gist), BLANKAFTER, NOVIEW);
 
@@ -1119,6 +1123,7 @@ 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-'", 0, do_anchor, 0);
        add_to_sclist(MMAIN, "M-6", 0, copy_text, 0);
        add_to_sclist(MMAIN, "M-^", 0, copy_text, 0);
        add_to_sclist(MMAIN, "M-}", 0, do_indent, 0);
diff --git a/src/nano.c b/src/nano.c
index 3122b803..813ae752 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -309,6 +309,12 @@ void extract(linestruct *top, size_t top_x, linestruct 
*bot, size_t bot_x)
                                                                                
                openfile->mark_x <= bot_x));
                same_line = (openfile->mark == openfile->filetop);
        }
+
+       /* When an anchor is after or in the cut piece, adjust it. */
+       if (openfile->anchored > openfile->filebot->lineno)
+               openfile->anchored -= (openfile->filebot->lineno - 
openfile->filetop->lineno);
+       else if (openfile->anchored > openfile->filetop->lineno)
+               openfile->anchored = openfile->filetop->lineno;
 #endif
 
        /* Subtract the number of characters in the text from the file size. */
@@ -381,6 +387,7 @@ void extract(linestruct *top, size_t top_x, linestruct 
*bot, size_t bot_x)
  * at the current cursor position. */
 void ingraft_buffer(linestruct *topline)
 {
+       size_t was_lineno = openfile->current->lineno;
        size_t current_x_save = openfile->current_x;
        bool edittop_inside;
 #ifndef NANO_TINY
@@ -406,8 +413,10 @@ void ingraft_buffer(linestruct *topline)
         * bottom of the passed buffer. */
        openfile->filetop = topline;
        openfile->filebot = topline;
-       while (openfile->filebot->next != NULL)
+       while (openfile->filebot->next != NULL) {
                openfile->filebot = openfile->filebot->next;
+               openfile->filebot->lineno = openfile->filebot->prev->lineno + 1;
+       }
 
        /* Put the cursor at the end of the pasted text. */
        openfile->current = openfile->filebot;
@@ -428,6 +437,10 @@ void ingraft_buffer(linestruct *topline)
                } else
                        openfile->mark = openfile->filetop;
        }
+
+       /* When an anchor is after the grafting point, adjust it. */
+       if (openfile->anchored > was_lineno)
+               openfile->anchored += (openfile->filebot->lineno - 
openfile->filetop->lineno);
 #endif
 
        /* Add the number of characters in the copied text to the file size. */
diff --git a/src/nano.h b/src/nano.h
index 689f3653..3a542313 100644
--- a/src/nano.h
+++ b/src/nano.h
@@ -379,6 +379,8 @@ typedef struct openfilestruct {
                /* The mark's x position in the above line. */
        mark_type kind_of_mark;
                /* Whether it is a soft (with Shift) or a hard mark. */
+       size_t anchored;
+               /* The line number of the line where the "anchor" was dropped. 
*/
        file_format fmt;
                /* The file's format -- Unix or DOS or Mac or mixed. */
        undo *undotop;
diff --git a/src/proto.h b/src/proto.h
index aa9cd31f..574a6757 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -505,6 +505,7 @@ void do_find_bracket(void);
 /* Most functions in text.c. */
 #ifndef NANO_TINY
 void do_mark(void);
+void do_anchor(void);
 #endif
 void do_tab(void);
 #ifndef NANO_TINY
diff --git a/src/text.c b/src/text.c
index b1a66ba9..5861da2c 100644
--- a/src/text.c
+++ b/src/text.c
@@ -57,6 +57,22 @@ void do_mark(void)
                refresh_needed = TRUE;
        }
 }
+
+/* Place an anchor when none exists, otherwise jump to it and clear it. */
+void do_anchor(void)
+{
+       if (openfile->anchored == 0) {
+               openfile->anchored = openfile->current->lineno;
+               statusbar(_("Placed anchor"));
+       } else {
+               openfile->current = line_from_number(openfile->anchored);
+               openfile->current_x = 0;
+               openfile->placewewant = 0;
+               statusbar(_("Jumped to anchor and cleared it"));
+               openfile->anchored = 0;
+               refresh_needed = TRUE;
+       }
+}
 #endif /* !NANO_TINY */
 
 #if defined(ENABLE_COLOR) || defined(ENABLE_SPELLER)
@@ -878,6 +894,10 @@ void do_enter(void)
                openfile->mark = newnode;
                openfile->mark_x += extra - openfile->current_x;
        }
+
+       /* When an anchor is after the current line, adjust it. */
+       if (openfile->anchored > openfile->current->lineno)
+               openfile->anchored++;
 #endif
 
        /* Insert the newly created line after the current one and renumber. */
-- 
2.23.0




reply via email to

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