aboutsummaryrefslogtreecommitdiffstats
path: root/src/command
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2021-04-16 19:27:07 +0200
committerGitHub <noreply@github.com>2021-04-16 19:27:07 +0200
commit2f4289bb3e316e405c55c9757e22cf574c9c9a55 (patch)
tree26ed5cf5f79f1423eb805e16923efdad9d774a1b /src/command
parent791b13cb9a720372e5d436c9f7d6dbdf4bec732a (diff)
parente1249d9bbbe6a7a0741c8a83697f7bf962f4f82a (diff)
downloadprofani-tty-2f4289bb3e316e405c55c9757e22cf574c9c9a55.tar.gz
Merge pull request #1485 from StefanKropp/feature/editor-command
Add basic functionality to launch external editor Regards https://github.com/profanity-im/profanity/issues/1521
Diffstat (limited to 'src/command')
-rw-r--r--src/command/cmd_defs.c16
-rw-r--r--src/command/cmd_funcs.c91
-rw-r--r--src/command/cmd_funcs.h1
3 files changed, 105 insertions, 3 deletions
diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c
index 730169ba..8a8330dc 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)
};
@@ -2894,7 +2908,7 @@ command_mangen(void)
GDateTime* now = g_date_time_new_now_local();
gchar* date = g_date_time_format(now, "%F");
- gchar *header = g_strdup_printf(".TH man 1 \"%s\" \"" PACKAGE_VERSION "\" \"Profanity XMPP client\"\n", date);
+ gchar* header = g_strdup_printf(".TH man 1 \"%s\" \"" PACKAGE_VERSION "\" \"Profanity XMPP client\"\n", date);
if (!header) {
log_error("command_mangen(): could not allocate memory");
return;
diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c
index 90c41dfd..fc38a2b0 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"
@@ -6861,7 +6867,7 @@ cmd_plugins_sourcepath(ProfWin* window, const char* const command, gchar** args)
return TRUE;
}
- char *path = get_expanded_path(args[2]);
+ char* path = get_expanded_path(args[2]);
if (!is_dir(path)) {
cons_show("Plugins sourcepath must be a directory.");
@@ -6882,7 +6888,7 @@ cmd_plugins_sourcepath(ProfWin* window, const char* const command, gchar** args)
gboolean
cmd_plugins_install(ProfWin* window, const char* const command, gchar** args)
{
- char *path;
+ char* path;
if (args[1] == NULL) {
char* sourcepath = prefs_get_string(PREF_PLUGINS_SOURCEPATH);
@@ -9304,3 +9310,84 @@ cmd_change_password(ProfWin* window, const char* const command, gchar** args)
return TRUE;
}
+
+gboolean
+cmd_editor(ProfWin* window, const char* const command, gchar** args)
+{
+ xmpp_ctx_t* const ctx = connection_get_ctx();
+ if (!ctx) {
+ log_debug("Editor: no connection");
+ return TRUE;
+ }
+
+ // build temp file name. Example: /tmp/profanity-f2f271dd-98c8-4118-8d47-3bd49c8e2e63.txt
+ char* uuid = xmpp_uuid_gen(ctx);
+ char* filename = g_strdup_printf("%s%s%s.txt", g_get_tmp_dir(), "/profanity-", uuid);
+ if (uuid) {
+ xmpp_free(ctx, uuid);
+ }
+
+ // Check if file exists and create file
+ if (g_file_test(filename, G_FILE_TEST_EXISTS)) {
+ cons_show("Editor: temp file exists already");
+ return TRUE;
+ }
+
+ 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);
+ 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);
+ if (!g_file_test(editor, G_FILE_TEST_EXISTS)) {
+ cons_show_error("Editor: binary %s not exist", editor);
+ return TRUE;
+ }
+
+ // Fork / exec
+ pid_t pid = fork();
+ if (pid == 0) {
+ int x = execl(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);
+ ProfWin* win = wins_get_current();
+ win_println(win, THEME_DEFAULT, "!", "EDITOR PREVIEW: %s", text->str);
+ rl_insert_text(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);
+ ui_resize();
+ rl_point = rl_end;
+ rl_forced_update_display();
+ }
+ 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