From 8be8f75b874ddbeddb070e9efda6d12b2230d646 Mon Sep 17 00:00:00 2001 From: Stefan Date: Mon, 15 Feb 2021 19:47:21 +0100 Subject: [PATCH] Add new editor command Goal is to launch an external editor (eg vim) to edit the text there. --- src/command/cmd_defs.c | 14 ++++++++++++++ src/command/cmd_funcs.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/command/cmd_funcs.h | 1 + 3 files changed, 55 insertions(+) diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index 730169ba..9eaf3f39 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -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) }; diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index 90c41dfd..cd6d8308 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -52,6 +52,12 @@ #include #include +// fork / execl +#include +#include +#include +#include + #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; +} diff --git a/src/command/cmd_funcs.h b/src/command/cmd_funcs.h index 5a192efb..f4ca72ed 100644 --- a/src/command/cmd_funcs.h +++ b/src/command/cmd_funcs.h @@ -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