Since v253 (commit 1ae886f), systemd assumes that $EDITOR understands the +line
syntax to jump to a specific line when opening a file with 'systemctl edit'
(vim, ed, and nano all do). systemd's assumption of this won't go away [0], so
BusyBox vi might as well support it.

[0] https://github.com/systemd/systemd/issues/33023

Signed-off-by: Fredrik Flornes Ellertsen <felle...@fastmail.com>
---
 editors/vi.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/editors/vi.c b/editors/vi.c
index 3cc3d2a0b..063ad1768 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -181,12 +181,13 @@
 //kbuild:lib-$(CONFIG_VI) += vi.o
 
 //usage:#define vi_trivial_usage
-//usage:       IF_FEATURE_VI_COLON("[-c CMD] ")IF_FEATURE_VI_READONLY("[-R] 
")"[-H] [FILE]..."
+//usage:       IF_FEATURE_VI_COLON("[-c CMD] ")IF_FEATURE_VI_READONLY("[-R] 
")"[-H] [+line] [FILE]..."
 //usage:#define vi_full_usage "\n\n"
 //usage:       "Edit FILE\n"
 //usage:       IF_FEATURE_VI_COLON(
 //usage:     "\n       -c CMD  Initial command to run ($EXINIT and ~/.exrc 
also available)"
 //usage:       )
+//usage:     "\n       +LINE   Open FILE with cursor at LINE"
 //usage:       IF_FEATURE_VI_READONLY(
 //usage:     "\n       -R      Read-only"
 //usage:       )
@@ -349,6 +350,7 @@ struct globals {
        int get_rowcol_error;
 #endif
        int crow, ccol;          // cursor is on Crow x Ccol
+       int open_at_line;        // open file at line (default 0)
        int offset;              // chars scrolled off the screen to the left
        int have_status_msg;     // is default edit status needed?
                                 // [don't make smallint!]
@@ -483,6 +485,7 @@ struct globals {
 #define columns                 (G.columns            )
 #define crow                    (G.crow               )
 #define ccol                    (G.ccol               )
+#define open_at_line            (G.open_at_line       )
 #define offset                  (G.offset             )
 #define status_buffer           (G.status_buffer      )
 #define have_status_msg         (G.have_status_msg    )
@@ -4871,6 +4874,8 @@ static void edit_file(char *fn)
        while (initial_cmds)
                run_cmds((char *)llist_pop(&initial_cmds));
 #endif
+       dot = find_line(open_at_line);
+       dot_skip_over_ws();
        redraw(FALSE);                  // dont force every col re-draw
        //------This is the main Vi cmd handling loop -----------------------
        while (editing > 0) {
@@ -5034,10 +5039,24 @@ int vi_main(int argc, char **argv)
                }
        }
 #endif
+
+       // 4: process optional +line argument
+       optind = 0;
+       open_at_line = 0;
+       if (argv[optind]) {
+               int pos;
+               if (sscanf(argv[optind], "+%d%n", &open_at_line, &pos) == 1) {
+                       if (!argv[optind][pos]) {
+                               optind++;
+                       } else {
+                               // garbage in +line argument, assume it's a 
filename
+                               open_at_line = 0;
+                       }
+               }
+       }
        // "Save cursor, use alternate screen buffer, clear screen"
        write1(ESC"[?1049h");
        // This is the main file handling loop
-       optind = 0;
        while (1) {
                edit_file(argv[optind]); // might be NULL on 1st iteration
                // NB: optind can be changed by ":next" and ":rewind" commands
-- 
2.45.0

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to