about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--Makefile.am2
-rw-r--r--src/command/cmd_funcs.c91
-rw-r--r--src/tools/editor.c132
-rw-r--r--src/tools/editor.h44
-rw-r--r--src/ui/inputwin.c26
5 files changed, 208 insertions, 87 deletions
diff --git a/Makefile.am b/Makefile.am
index 997607e0..19374f2c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -51,6 +51,7 @@ core_sources = \
 	src/tools/bookmark_ignore.h \
 	src/tools/autocomplete.c src/tools/autocomplete.h \
 	src/tools/clipboard.c src/tools/clipboard.h \
+	src/tools/editor.c src/tools/editor.h \
 	src/config/files.c src/config/files.h \
 	src/config/conflists.c src/config/conflists.h \
 	src/config/accounts.c src/config/accounts.h \
@@ -91,6 +92,7 @@ unittest_sources = \
 	src/tools/parser.h \
 	src/tools/autocomplete.c src/tools/autocomplete.h \
 	src/tools/clipboard.c src/tools/clipboard.h \
+	src/tools/editor.c src/tools/editor.h \
 	src/tools/bookmark_ignore.c \
 	src/tools/bookmark_ignore.h \
 	src/config/accounts.h \
diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c
index b18fd6a2..eabfb4bf 100644
--- a/src/command/cmd_funcs.c
+++ b/src/command/cmd_funcs.c
@@ -77,6 +77,7 @@
 #include "tools/autocomplete.h"
 #include "tools/parser.h"
 #include "tools/bookmark_ignore.h"
+#include "tools/editor.h"
 #include "plugins/plugins.h"
 #include "ui/ui.h"
 #include "ui/window_list.h"
@@ -122,7 +123,6 @@ static void _who_roster(ProfWin* window, const char* const command, gchar** args
 static gboolean _cmd_execute(ProfWin* window, const char* const command, const char* const inp);
 static gboolean _cmd_execute_default(ProfWin* window, const char* inp);
 static gboolean _cmd_execute_alias(ProfWin* window, const char* const inp, gboolean* ran);
-gboolean _get_message_from_editor(gchar* message, gchar** returned_message);
 
 /*
  * Take a line of input and process it, return TRUE if profanity is to
@@ -4099,7 +4099,7 @@ cmd_subject(ProfWin* window, const char* const command, gchar** args)
         gchar* message = NULL;
         char* subject = muc_subject(mucwin->roomjid);
 
-        if (_get_message_from_editor(subject, &message)) {
+        if (get_message_from_editor(subject, &message)) {
             return TRUE;
         }
 
@@ -9453,89 +9453,6 @@ cmd_change_password(ProfWin* window, const char* const command, gchar** args)
     return TRUE;
 }
 
-// Returns true if an error occurred
-gboolean
-_get_message_from_editor(gchar* message, gchar** returned_message)
-{
-    // create editor dir if not present
-    char* jid = connection_get_barejid();
-    gchar* path = files_get_account_data_path(DIR_EDITOR, jid);
-    free(jid);
-    if (g_mkdir_with_parents(path, S_IRWXU) != 0) {
-        cons_show_error("Failed to create directory at '%s' with error '%s'", path, strerror(errno));
-        g_free(path);
-        return TRUE;
-    }
-
-    // build temp file name. Example: /home/user/.local/share/profanity/editor/jid/compose.md
-    char* filename = g_strdup_printf("%s/compose.md", path);
-    g_free(path);
-
-    GError* creation_error = NULL;
-    GFile* file = g_file_new_for_path(filename);
-    GFileOutputStream* fos = g_file_create(file, G_FILE_CREATE_PRIVATE, NULL, &creation_error);
-
-    free(filename);
-
-    if (message != NULL && strlen(message) > 0) {
-        int fd_output_file = open(g_file_get_path(file), O_WRONLY);
-        if (fd_output_file < 0) {
-            cons_show_error("Editor: Could not open file '%s': %s", file, strerror(errno));
-            return TRUE;
-        }
-        if (-1 == write(fd_output_file, message, strlen(message))) {
-            cons_show_error("Editor: failed to write '%s' to file: %s", message, strerror(errno));
-            return TRUE;
-        }
-        close(fd_output_file);
-    }
-
-    if (creation_error) {
-        cons_show_error("Editor: could not create temp file");
-        return TRUE;
-    }
-    g_object_unref(fos);
-
-    char* editor = prefs_get_string(PREF_COMPOSE_EDITOR);
-
-    // Fork / exec
-    pid_t pid = fork();
-    if (pid == 0) {
-        int x = execlp(editor, editor, g_file_get_path(file), (char*)NULL);
-        if (x == -1) {
-            cons_show_error("Editor:Failed to exec %s", editor);
-        }
-        _exit(EXIT_FAILURE);
-    } else {
-        if (pid == -1) {
-            return TRUE;
-        }
-        int status = 0;
-        waitpid(pid, &status, 0);
-        int fd_input_file = open(g_file_get_path(file), 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);
-            *returned_message = g_strdup(text->str);
-            g_string_free(text, TRUE);
-        }
-        close(fd_input_file);
-
-        GError* deletion_error = NULL;
-        g_file_delete(file, NULL, &deletion_error);
-        if (deletion_error) {
-            cons_show("Editor: error during file deletion");
-            return TRUE;
-        }
-        g_object_unref(file);
-    }
-
-    return FALSE;
-}
-
 gboolean
 cmd_editor(ProfWin* window, const char* const command, gchar** args)
 {
@@ -9548,7 +9465,7 @@ cmd_editor(ProfWin* window, const char* const command, gchar** args)
 
     gchar* message = NULL;
 
-    if (_get_message_from_editor(NULL, &message)) {
+    if (get_message_from_editor(NULL, &message)) {
         return TRUE;
     }
 
@@ -9571,7 +9488,7 @@ cmd_correct_editor(ProfWin* window, const char* const command, gchar** args)
     gchar* initial_message = win_get_last_sent_message(window);
 
     gchar* message = NULL;
-    if (_get_message_from_editor(initial_message, &message)) {
+    if (get_message_from_editor(initial_message, &message)) {
         return TRUE;
     }
 
diff --git a/src/tools/editor.c b/src/tools/editor.c
new file mode 100644
index 00000000..068f5e84
--- /dev/null
+++ b/src/tools/editor.c
@@ -0,0 +1,132 @@
+/*
+ * editor.c
+ * vim: expandtab:ts=4:sts=4:sw=4
+ *
+ * Copyright (C) 2022 Michael Vetter <jubalh@iodoru.org>
+ * Copyright (C) 2022 MarcoPolo PasTonMolo  <marcopolopastonmolo@protonmail.com>
+ *
+ * This file is part of Profanity.
+ *
+ * Profanity is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Profanity is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Profanity.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ * In addition, as a special exception, the copyright holders give permission to
+ * link the code of portions of this program with the OpenSSL library under
+ * certain conditions as described in each individual source file, and
+ * distribute linked combinations including the two.
+ *
+ * You must obey the GNU General Public License in all respects for all of the
+ * code used other than OpenSSL. If you modify file(s) with this exception, you
+ * may extend this exception to your version of the file(s), but you are not
+ * obligated to do so. If you do not wish to do so, delete this exception
+ * statement from your version. If you delete this exception statement from all
+ * source files in the program, then also delete it here.
+ *
+ */
+
+#include <fcntl.h>
+#include <glib.h>
+#include <sys/wait.h>
+#include <gio/gio.h>
+
+#include "config/files.h"
+#include "config/preferences.h"
+#include "ui/ui.h"
+
+// Returns true if an error occurred
+gboolean
+get_message_from_editor(gchar* message, gchar** returned_message)
+{
+    // create editor dir if not present
+    char* jid = connection_get_barejid();
+    gchar* path = files_get_account_data_path(DIR_EDITOR, jid);
+    free(jid);
+    if (g_mkdir_with_parents(path, S_IRWXU) != 0) {
+        cons_show_error("Failed to create directory at '%s' with error '%s'", path, strerror(errno));
+        g_free(path);
+        return TRUE;
+    }
+
+    // build temp file name. Example: /home/user/.local/share/profanity/editor/jid/compose.md
+    char* filename = g_strdup_printf("%s/compose.md", path);
+    g_free(path);
+
+    GError* creation_error = NULL;
+    GFile* file = g_file_new_for_path(filename);
+    GFileOutputStream* fos = g_file_create(file, G_FILE_CREATE_PRIVATE, NULL, &creation_error);
+
+    free(filename);
+
+    if (message != NULL && strlen(message) > 0) {
+        int fd_output_file = open(g_file_get_path(file), O_WRONLY);
+        if (fd_output_file < 0) {
+            cons_show_error("Editor: Could not open file '%s': %s", file, strerror(errno));
+            return TRUE;
+        }
+        if (-1 == write(fd_output_file, message, strlen(message))) {
+            cons_show_error("Editor: failed to write '%s' to file: %s", message, strerror(errno));
+            return TRUE;
+        }
+        close(fd_output_file);
+    }
+
+    if (creation_error) {
+        cons_show_error("Editor: could not create temp file");
+        return TRUE;
+    }
+    g_object_unref(fos);
+
+    char* editor = prefs_get_string(PREF_COMPOSE_EDITOR);
+
+    // Fork / exec
+    pid_t pid = fork();
+    if (pid == 0) {
+        int x = execlp(editor, editor, g_file_get_path(file), (char*)NULL);
+        if (x == -1) {
+            cons_show_error("Editor:Failed to exec %s", editor);
+        }
+        _exit(EXIT_FAILURE);
+    } else {
+        if (pid == -1) {
+            return TRUE;
+        }
+        int status = 0;
+        waitpid(pid, &status, 0);
+        int fd_input_file = open(g_file_get_path(file), 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);
+            *returned_message = g_strdup(text->str);
+            g_string_free(text, TRUE);
+        } else {
+            *returned_message = g_strdup("");
+        }
+        close(fd_input_file);
+
+        GError* deletion_error = NULL;
+        g_file_delete(file, NULL, &deletion_error);
+        if (deletion_error) {
+            cons_show("Editor: error during file deletion");
+            g_free(*returned_message);
+            return TRUE;
+        }
+        g_object_unref(file);
+    }
+
+    g_free(editor);
+
+    return FALSE;
+}
diff --git a/src/tools/editor.h b/src/tools/editor.h
new file mode 100644
index 00000000..4b239a5b
--- /dev/null
+++ b/src/tools/editor.h
@@ -0,0 +1,44 @@
+/*
+ * editor.h
+ * vim: expandtab:ts=4:sts=4:sw=4
+ *
+ * Copyright (C) 2022 Michael Vetter <jubalh@iodoru.org>
+ * Copyright (C) 2022 MarcoPolo PasTonMolo  <marcopolopastonmolo@protonmail.com>
+ *
+ * This file is part of Profanity.
+ *
+ * Profanity is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Profanity is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Profanity.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ * In addition, as a special exception, the copyright holders give permission to
+ * link the code of portions of this program with the OpenSSL library under
+ * certain conditions as described in each individual source file, and
+ * distribute linked combinations including the two.
+ *
+ * You must obey the GNU General Public License in all respects for all of the
+ * code used other than OpenSSL. If you modify file(s) with this exception, you
+ * may extend this exception to your version of the file(s), but you are not
+ * obligated to do so. If you do not wish to do so, delete this exception
+ * statement from your version. If you delete this exception statement from all
+ * source files in the program, then also delete it here.
+ *
+ */
+
+#ifndef TOOLS_EDITOR_H
+#define TOOLS_EDITOR_H
+
+#include <glib.h>
+
+gboolean get_message_from_editor(gchar* message, gchar** returned_message);
+
+#endif
diff --git a/src/ui/inputwin.c b/src/ui/inputwin.c
index 4ea773c8..70d3d9d6 100644
--- a/src/ui/inputwin.c
+++ b/src/ui/inputwin.c
@@ -74,6 +74,7 @@
 #include "xmpp/muc.h"
 #include "xmpp/roster_list.h"
 #include "xmpp/chat_state.h"
+#include "tools/editor.h"
 
 static WINDOW* inp_win;
 static int pad_start = 0;
@@ -133,6 +134,7 @@ static int _inp_rl_subwin_pageup_handler(int count, int key);
 static int _inp_rl_subwin_pagedown_handler(int count, int key);
 static int _inp_rl_startup_hook(void);
 static int _inp_rl_down_arrow_handler(int count, int key);
+static int _inp_rl_send_to_editor(int count, int key);
 
 void
 create_input_window(void)
@@ -434,6 +436,7 @@ _inp_rl_addfuncs(void)
     rl_add_funmap_entry("prof_subwin_pagedown", _inp_rl_subwin_pagedown_handler);
     rl_add_funmap_entry("prof_win_clear", _inp_rl_win_clear_handler);
     rl_add_funmap_entry("prof_win_close", _inp_rl_win_close_handler);
+    rl_add_funmap_entry("prof_send_to_editor", _inp_rl_send_to_editor);
 }
 
 // Readline callbacks
@@ -484,6 +487,7 @@ _inp_rl_startup_hook(void)
     rl_bind_keyseq("\\ea", _inp_rl_win_next_unread_handler);
     rl_bind_keyseq("\\ev", _inp_rl_win_attention_handler);
     rl_bind_keyseq("\\em", _inp_rl_win_attention_next_handler);
+    rl_bind_keyseq("\\ee", _inp_rl_send_to_editor);
 
     rl_bind_keyseq("\\e\\e[5~", _inp_rl_subwin_pageup_handler);
     rl_bind_keyseq("\\e[5;3~", _inp_rl_subwin_pageup_handler);
@@ -877,3 +881,25 @@ _inp_rl_down_arrow_handler(int count, int key)
     rl_redisplay();
     return 0;
 }
+
+static int
+_inp_rl_send_to_editor(int count, int key)
+{
+    if (!rl_line_buffer) {
+        return 0;
+    }
+
+    gchar* message = NULL;
+
+    if (get_message_from_editor(rl_line_buffer, &message)) {
+        return 0;
+    }
+
+    rl_replace_line(message, 0);
+    ui_resize();
+    rl_point = rl_end;
+    rl_forced_update_display();
+    g_free(message);
+
+    return 0;
+}