aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar KlzXS <azszwymmvqdi@yahoo.com>2019-10-10 15:07:16 +0200
committerGravatar Mischievous Meerkat <engineerarun@gmail.com>2019-10-10 18:37:16 +0530
commitb7c2ce741a207f82a51443b49d953508e64165e4 (patch)
tree717773d95c3e2d19917449b6f16572ff63aeb5f9 /src
parent7965fa1b0d4bda1c7ef761265ac75af3a37eb448 (diff)
downloadnnn-b7c2ce741a207f82a51443b49d953508e64165e4.tar.gz
Implemented edit selection buffer (#345)
* Implemented edit selection buffer * Added checks and changed keybind * Forgot check and style fixes * Don't work late at night again You forget things when you're tired
Diffstat (limited to 'src')
-rw-r--r--src/nnn.c87
-rw-r--r--src/nnn.h3
2 files changed, 90 insertions, 0 deletions
diff --git a/src/nnn.c b/src/nnn.c
index 961fab8..fe23450 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -886,6 +886,87 @@ static void endselection(void)
}
}
+static bool seledit(void)
+{
+ bool ret = FALSE;
+ int fd, lines = 0;
+ ssize_t count;
+
+ if (!selbufpos) {
+ DPRINTF_S("empty selection");
+ return FALSE;
+ }
+
+ fd = create_tmp_file();
+ if (fd == -1) {
+ DPRINTF_S("couldn't create tmp file");
+ return FALSE;
+ }
+
+ seltofile(fd, NULL);
+ close(fd);
+
+ spawn(editor, g_tmpfpath, NULL, NULL, F_CLI);
+
+ if ((fd = open(g_tmpfpath, O_RDONLY)) == -1) {
+ DPRINTF_S("couldn't read tmp file");
+ unlink(g_tmpfpath);
+ return FALSE;
+ }
+
+ struct stat sb;
+ fstat(fd, &sb);
+
+ if (sb.st_size > selbufpos) {
+ DPRINTF_S("edited buffer larger than pervious");
+ goto emptyedit;
+ }
+
+ count = read(fd, pselbuf, selbuflen);
+ close(fd);
+ unlink(g_tmpfpath);
+
+ if (!count) {
+ ret = TRUE;
+ goto emptyedit;
+ }
+
+ if (count < 0) {
+ DPRINTF_S("error reading tmp file");
+ goto emptyedit;
+ }
+
+ resetselind();
+ selbufpos = count;
+ /* The last character should be '\n' */
+ pselbuf[--count] = '\0';
+ for (--count; count > 0; --count) {
+ /* Replace every '\n' that separates two paths */
+ if (pselbuf[count] == '\n' && pselbuf[count + 1] == '/') {
+ ++lines;
+ pselbuf[count] = '\0';
+ }
+ }
+
+ if (lines > nselected) {
+ DPRINTF_S("files added to selection");
+ goto emptyedit;
+ }
+
+ nselected = lines;
+ writesel(pselbuf, selbufpos - 1);
+
+ return TRUE;
+
+emptyedit:
+ resetselind();
+ nselected = 0;
+ selbufpos = 0;
+ cfg.selmode = 0;
+ writesel(NULL, 0);
+ return ret;
+}
+
static bool selsafe(void)
{
/* Fail if selection file path not generated */
@@ -4139,6 +4220,12 @@ nochange:
printwait(messages[NONE_SELECTED], &presel);
goto nochange;
+ case SEL_SELEDIT:
+ if (!seledit()){
+ printwait("edit failed!", &presel);
+ goto nochange;
+ }
+ break;
case SEL_CP:
case SEL_MV:
case SEL_RMMUL:
diff --git a/src/nnn.h b/src/nnn.h
index 19a9587..241e16e 100644
--- a/src/nnn.h
+++ b/src/nnn.h
@@ -79,6 +79,7 @@ enum action {
SEL_SELMUL,
SEL_SELALL,
SEL_SELLST,
+ SEL_SELEDIT,
SEL_CP,
SEL_MV,
SEL_RMMUL,
@@ -209,6 +210,8 @@ static struct key bindings[] = {
{ 'a', SEL_SELALL },
/* Show list of copied files */
{ 'M', SEL_SELLST },
+ /* Edit selection buffer */
+ { 'K', SEL_SELEDIT },
/* Copy from selection buffer */
{ 'P', SEL_CP },
/* Move from selection buffer */