From: Arun Prakash Jana <engineerarun@gmail.com>
Date: Tue, 17 Aug 2021 14:58:12 +0000 (+0530)
Subject: Key B to add bookmarks on the fly
X-Git-Tag: v4.3~41
X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=78ea6702f7dae427c7a102cb8420533b7ebcc6ae;p=nnn.git

Key B to add bookmarks on the fly
---

diff --git a/src/nnn.c b/src/nnn.c
index 9ca979bb..3b5f1275 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -4918,6 +4918,21 @@ static size_t handle_bookmark(const char *bmark, char *newpath)
 	return r;
 }
 
+static void add_bookmark(char *path, char *newpath, int *presel)
+{
+	char *dir = xbasename(path);
+
+	dir = xreadline(dir[0] ? dir : NULL, "name: ");
+	if (dir && *dir) {
+		size_t r = mkpath(cfgpath, toks[TOK_BM], newpath);
+
+		newpath[r - 1] = '/';
+		xstrsncpy(newpath + r, dir, PATH_MAX - r);
+		printwait((symlink(path, newpath) == -1) ? strerror(errno) : newpath, presel);
+	} else
+		printwait(messages[MSG_CANCEL], presel);
+}
+
 /*
  * The help string tokens (each line) start with a HEX value
  * which indicates the number of spaces to print before the
@@ -4938,7 +4953,7 @@ static void show_help(const char *path)
 	   "5Ret Rt l  Open%-20c'  First file/match\n"
 	       "9g ^A  Top%-21c.  Toggle hidden\n"
 	       "9G ^E  End%-21c+  Toggle auto-advance\n"
-		  "c,  Mark CWD%-13cb ^/  Select bookmark\n"
+	      "8B (,)  Book(mark)%-11cb ^/  Select bookmark\n"
 		"a1-4  Context%-11c(Sh)Tab  Cycle/new context\n"
 	    "62Esc ^Q  Quit%-20cq  Quit context\n"
 		 "b^G  QuitCD%-18cQ  Pick/err, quit\n"
@@ -6882,8 +6897,8 @@ nochange:
 
 			/* SEL_CDLAST: dir pointing to lastdir */
 			xstrsncpy(newpath, dir, PATH_MAX); // fallthrough
-		case SEL_BOOKMARK:
-			if (sel == SEL_BOOKMARK) {
+		case SEL_BMOPEN:
+			if (sel == SEL_BMOPEN) {
 				r = (int)handle_bookmark(mark, newpath);
 				if (r) {
 					printwait(messages[r], &presel);
@@ -6941,6 +6956,9 @@ nochange:
 			mark = xstrdup(path);
 			printwait(mark, &presel);
 			goto nochange;
+		case SEL_BMARK:
+			add_bookmark(path, newpath, &presel);
+			goto nochange;
 		case SEL_FLTR:
 			if (!ndents)
 				goto nochange;
diff --git a/src/nnn.h b/src/nnn.h
index 6e934dfa..969419b8 100644
--- a/src/nnn.h
+++ b/src/nnn.h
@@ -60,7 +60,7 @@ enum action {
 	SEL_CDBEGIN,
 	SEL_CDLAST,
 	SEL_CDROOT,
-	SEL_BOOKMARK,
+	SEL_BMOPEN,
 	SEL_REMOTE,
 	SEL_CYCLE,
 	SEL_CYCLER,
@@ -75,6 +75,7 @@ enum action {
 	SEL_CTX8,
 #endif
 	SEL_MARK,
+	SEL_BMARK,
 	SEL_FLTR,
 	SEL_MFLTR,
 	SEL_HIDDEN,
@@ -167,8 +168,8 @@ static struct key bindings[] = {
 	/* Go to / */
 	{ '`',            SEL_CDROOT },
 	/* Leader key */
-	{ 'b',            SEL_BOOKMARK },
-	{ CONTROL('_'),   SEL_BOOKMARK },
+	{ 'b',            SEL_BMOPEN },
+	{ CONTROL('_'),   SEL_BMOPEN },
 	/* Connect to server over SSHFS */
 	{ 'c',            SEL_REMOTE },
 	/* Cycle contexts in forward direction */
@@ -188,6 +189,8 @@ static struct key bindings[] = {
 #endif
 	/* Mark a path to visit later */
 	{ ',',            SEL_MARK },
+	/* Create a bookmark */
+	{ 'B',            SEL_BMARK },
 	/* Filter */
 	{ '/',            SEL_FLTR },
 	/* Toggle filter mode */