1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Add new editor command

Goal is to launch an external editor (eg vim) to edit the text there.
This commit is contained in:
Stefan 2021-02-15 19:47:21 +01:00 committed by Michael Vetter
parent 791b13cb9a
commit 8be8f75b87
3 changed files with 55 additions and 0 deletions

View File

@ -2586,6 +2586,20 @@ static struct cmd_t command_defs[] = {
CMD_NOEXAMPLES
},
{ "/editor",
parse_args, 0, 0, NULL,
CMD_NOSUBFUNCS
CMD_MAINFUNC(cmd_editor)
CMD_TAGS(
CMD_TAG_CHAT)
CMD_SYN(
"/editor")
CMD_DESC(
"Call editor")
CMD_NOARGS
CMD_NOEXAMPLES
},
// NEXT-COMMAND (search helper)
};

View File

@ -52,6 +52,12 @@
#include <langinfo.h>
#include <ctype.h>
// fork / execl
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <readline/readline.h>
#include "profanity.h"
#include "log.h"
#include "common.h"
@ -9304,3 +9310,37 @@ cmd_change_password(ProfWin* window, const char* const command, gchar** args)
return TRUE;
}
gboolean
cmd_editor(ProfWin* window, const char* const command, gchar** args)
{
const char* filename = "/tmp/profanity.txt";
pid_t pid = fork();
if( pid == 0 ) {
int x = execl("/usr/bin/vim", "/usr/bin/vim", filename, (char *) NULL);
if ( x == -1 ) {
cons_show_error("Failed to exec vim");
}
} else {
int status = 0;
waitpid(pid, &status, 0);
ui_redraw();
int fd_input_file = open(filename, O_RDONLY);
const size_t COUNT = 8192;
char buf[COUNT];
ssize_t size_read = read(fd_input_file, buf, COUNT);
if(size_read > 0 && size_read <= COUNT ) {
buf[size_read-1] = '\0';
GString* text = g_string_new(buf);
//ProfWin* window = wins_get_current();
//cmd_process_input(window, text->str);
rl_insert_text(text->str);
g_string_free(text, TRUE);
}
close(fd_input_file);
ui_redraw();
}
return TRUE;
}

View File

@ -242,5 +242,6 @@ gboolean cmd_executable_avatar(ProfWin* window, const char* const command, gchar
gboolean cmd_executable_urlopen(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_executable_urlsave(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_mam(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_editor(ProfWin* window, const char* const command, gchar** args);
#endif