From 4b562c2a389d6c265a94e9a2dfa9b079a36b6431 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jonathan=20Neusch=C3=A4fer?= Date: Sun, 11 Dec 2011 12:50:02 +0100 Subject: [PATCH] screen_lyrics: new key to edit lyrics Thanks for the idea and an initial patch go to Jitka Novotna --- src/command.c | 2 ++ src/command.h | 1 + src/screen_help.c | 1 + src/screen_lyrics.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/src/command.c b/src/command.c index 0e7e5a2..43741cf 100644 --- a/src/command.c +++ b/src/command.c @@ -241,6 +241,8 @@ static command_definition_t cmds[] = { N_("Interrupt action") }, { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE, "lyrics-update", N_("Update Lyrics") }, + { {'e', 0, 0 }, 0, CMD_LYRICS_EDIT, "lyrics-edit", + N_("Edit Lyrics") }, #endif #ifdef ENABLE_OUTPUTS_SCREEN diff --git a/src/command.h b/src/command.h index d1e8122..a05ea55 100644 --- a/src/command.h +++ b/src/command.h @@ -96,6 +96,7 @@ typedef enum { CMD_SCREEN_LYRICS, CMD_SCREEN_OUTPUTS, CMD_LYRICS_UPDATE, + CMD_LYRICS_EDIT, CMD_INTERRUPT, CMD_GO_ROOT_DIRECTORY, CMD_GO_PARENT_DIRECTORY, diff --git a/src/screen_help.c b/src/screen_help.c index 038ab3b..e3b2f3d 100644 --- a/src/screen_help.c +++ b/src/screen_help.c @@ -162,6 +162,7 @@ static const struct help_text_row help_text[] = { from the server */ { 0, CMD_INTERRUPT, N_("Interrupt retrieval") }, { 0, CMD_LYRICS_UPDATE, N_("Download lyrics for currently playing song") }, + { 0, CMD_LYRICS_EDIT, N_("Add or edit lyrics") }, { 0, CMD_SAVE_PLAYLIST, N_("Save lyrics") }, { 0, CMD_DELETE, N_("Delete saved lyrics") }, #endif diff --git a/src/screen_lyrics.c b/src/screen_lyrics.c index a8c7210..676ea15 100644 --- a/src/screen_lyrics.c +++ b/src/screen_lyrics.c @@ -28,9 +28,12 @@ #include "screen.h" #include "lyrics.h" #include "screen_text.h" +#include "ncu.h" #include +#include #include +#include #include #include #include @@ -341,6 +344,59 @@ lyrics_paint(void) screen_text_paint(&text); } +/* save current lyrics to a file and run editor on it */ +static void +lyrics_edit(void) +{ + char *editor = options.text_editor; + int status; + + if (editor == NULL) { + screen_status_message(_("Editor not configured")); + return; + } + + if (store_lyr_hd() < 0) + return; + + ncu_deinit(); + + /* TODO: fork/exec/wait won't work on Windows, but building a command + string for system() is too tricky */ + pid_t pid = fork(); + if (pid == -1) { + screen_status_printf(("%s (%s)"), _("Can't start editor"), g_strerror(errno)); + } else if (pid == 0) { + char path[1024]; + path_lyr_file(path, sizeof(path), current.artist, current.title); + execlp(editor, editor, path, NULL); + /* exec failed, do what system does */ + _exit(127); + } else { + int ret; + do { + ret = waitpid(pid, &status, 0); + } while (ret == -1 && errno == EINTR); + } + + ncu_init(); + + /* TODO: hardly portable */ + if (WIFEXITED(status)) { + if (WEXITSTATUS(status) == 0) + /* update to get the changes */ + screen_lyrics_reload(); + else if (WEXITSTATUS(status) == 127) + screen_status_message(_("Can't start editor")); + else + screen_status_printf(_("Editor exited unexpectedly (%d)"), + WEXITSTATUS(status)); + } else if (WIFSIGNALED(status)) { + screen_status_printf(_("Editor exited unexpectedly (signal %d)"), + WTERMSIG(status)); + } +} + static bool lyrics_cmd(struct mpdclient *c, command_t cmd) { @@ -379,6 +435,9 @@ lyrics_cmd(struct mpdclient *c, command_t cmd) screen_text_repaint(&text); } return true; + case CMD_LYRICS_EDIT: + lyrics_edit(); + return true; case CMD_SELECT: screen_lyrics_reload(); return true; -- 2.11.4.GIT