about summary refs log tree commit diff stats
path: root/src/command
diff options
context:
space:
mode:
authorStefan <stefan@devlug.de>2021-02-15 19:47:21 +0100
committerMichael Vetter <jubalh@iodoru.org>2021-04-16 18:03:58 +0200
commit8be8f75b874ddbeddb070e9efda6d12b2230d646 (patch)
treeac7de57dfd70ae6755be844c44ed868a766062f2 /src/command
parent791b13cb9a720372e5d436c9f7d6dbdf4bec732a (diff)
downloadprofani-tty-8be8f75b874ddbeddb070e9efda6d12b2230d646.tar.gz
Add new editor command
Goal is to launch an external editor (eg vim) to edit the text there.
Diffstat (limited to 'src/command')
-rw-r--r--src/command/cmd_defs.c14
-rw-r--r--src/command/cmd_funcs.c40
-rw-r--r--src/command/cmd_funcs.h1
3 files changed, 55 insertions, 0 deletions
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 <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;
+}
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