diff options
| author | Michael Vetter <jubalh@iodoru.org> | 2022-03-03 12:57:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-03 12:57:13 +0100 |
| commit | a6dda412981c17f742622093b40e577b91a7ea79 (patch) | |
| tree | f1f6e422ba5fd4c96097e096b5088d27179bd9ed /src/command | |
| parent | 81b5230da520af54f3d6d81a54c87b38cdd628fe (diff) | |
| parent | 3f78af2591e43e697d644e2ceb9c71070c754092 (diff) | |
| download | profani-tty-a6dda412981c17f742622093b40e577b91a7ea79.tar.gz | |
Merge pull request #1645 from MarcoPolo-PasTonMolo/feature/hotkey-send-to-editor
Add hotkey for sending readline text to editor
Diffstat (limited to 'src/command')
| -rw-r--r-- | src/command/cmd_funcs.c | 91 |
1 files changed, 4 insertions, 87 deletions
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; } |