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
This commit is contained in:
KlzXS 2019-10-10 15:07:16 +02:00 committed by Mischievous Meerkat
parent 7965fa1b0d
commit b7c2ce741a
2 changed files with 90 additions and 0 deletions

View File

@ -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:

View File

@ -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 */