Code

screen_lyrics: new key to edit lyrics
authorJonathan Neuschäfer <j.neuschaefer@gmx.net>
Sun, 11 Dec 2011 11:50:02 +0000 (12:50 +0100)
committerJonathan Neuschäfer <j.neuschaefer@gmx.net>
Sat, 17 Dec 2011 16:47:38 +0000 (17:47 +0100)
Thanks for the idea and an initial patch go to Jitka Novotna <jitka@ucw.cz>

src/command.c
src/command.h
src/screen_help.c
src/screen_lyrics.c

index 0e7e5a2551b2f348088a6875a60ac77b79fabf17..43741cf5686d5179f2c9d1f4bb56e668b046c764 100644 (file)
@@ -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
index d1e8122bc25b74ad5b0ae9ac82bf9a9db11d8e0b..a05ea5518659a00a00043ad0e44097e5b20d5f67 100644 (file)
@@ -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,
index 038ab3becbf52b49c1bec0532e25a7eb10055d66..e3b2f3d699f20fdf90c8c2fd22433253d7391b77 100644 (file)
@@ -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
index a8c721068542563a57713d8c1503733b0e07fac5..676ea151e478e36e6f32b2edb78e30a0cb888f60 100644 (file)
 #include "screen.h"
 #include "lyrics.h"
 #include "screen_text.h"
+#include "ncu.h"
 
 #include <assert.h>
+#include <errno.h>
 #include <sys/stat.h>
+#include <sys/wait.h>
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
@@ -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;