Skip to content

Commit

Permalink
Key B to add bookmarks on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
jarun committed Aug 17, 2021
1 parent 0aed67f commit 78ea670
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
24 changes: 21 additions & 3 deletions src/nnn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -6882,8 +6897,8 @@ static bool browse(char *ipath, const char *session, int pkey)

/* 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);
Expand Down Expand Up @@ -6941,6 +6956,9 @@ static bool browse(char *ipath, const char *session, int pkey)
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;
Expand Down
9 changes: 6 additions & 3 deletions src/nnn.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ enum action {
SEL_CDBEGIN,
SEL_CDLAST,
SEL_CDROOT,
SEL_BOOKMARK,
SEL_BMOPEN,
SEL_REMOTE,
SEL_CYCLE,
SEL_CYCLER,
Expand All @@ -75,6 +75,7 @@ enum action {
SEL_CTX8,
#endif
SEL_MARK,
SEL_BMARK,
SEL_FLTR,
SEL_MFLTR,
SEL_HIDDEN,
Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down

0 comments on commit 78ea670

Please sign in to comment.