summaryrefslogtreecommitdiffhomepage
path: root/emms/jao-emms-lyrics.el
blob: 8a0b0ce72d6dd3ed7d71cf9224493bd9667011f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
;; jao-emms-lyrics.el -- simple show lyrics in emms

;; Copyright (C) 2009, 2010, 2017 Jose Antonio Ortega Ruiz

;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
;; Start date: Sat Jul 04, 2009 13:41

;; This file 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.

;; This file 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 this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Code:

(require 'emms)

(defvar jao-emms-lyrics-cache-dir "~/.emacs.d/emms-lyrics")

(defun jao-emms-lyrics--filename (artist title)
  (expand-file-name (format "%s.lyr" title)
                    (jao-emms-lyrics--ensure-dir artist)))

(defun jao-emms-lyrics--ensure-dir (artist)
  (let ((candidate (expand-file-name artist jao-emms-lyrics-cache-dir)))
    (unless (file-directory-p candidate)
      (make-directory candidate t))
    candidate))

(defun jao-emms-lyrics--get-cached (artist title)
  (let ((candidate (jao-emms-lyrics--filename artist title)))
    (and (file-exists-p candidate)
         (with-current-buffer (find-file-noselect candidate)
           (prog1
               (buffer-string)
             (kill-buffer))))))

(defun jao-emms-lyrics--cache (artist title lyrics)
  (with-current-buffer
      (find-file-noselect (jao-emms-lyrics--filename artist title))
    (delete-region (point-min) (point-max))
    (insert lyrics)
    (save-buffer)
    (kill-buffer)))

(make-variable-buffer-local
 (defvar jao-emms-lyrics--path nil))

(defvar jao-emms-lyrics-mode-map)
(setq jao-emms-lyrics-mode-map
      (let ((map (make-keymap)))
        (suppress-keymap map)
        (define-key map [?q] 'bury-buffer)
        (define-key map [?g] 'jao-emms-show-lyrics)
        (define-key map [?G] 'jao-emms-show-lyrics-force)
        (define-key map [?e] 'jao-emms-edit-lyrics)
        map))

(defvar jao-emms-font-lock-artist 'bold)
(defvar jao-emms-font-lock-title 'bold)

(defun jao-emms-lyrics-mode ()
  (interactive)
  (kill-all-local-variables)
  (use-local-map jao-emms-lyrics-mode-map)
  (setq major-mode 'jao-emms-lyrics-mode)
  (setq mode-name "lyrics")
  (toggle-read-only 1))

(defun jao-emms-lyrics-buffer ()
  (or (get-buffer "*Emms Lyrics*")
      (with-current-buffer (get-buffer-create "*Emms Lyrics*")
        (jao-emms-lyrics-mode)
        (current-buffer))))

(defun jao-emms-lyrics-track-data ()
  (let ((track (or (emms-playlist-current-selected-track)
                   (error "No playing track"))))
    (cons (or (emms-track-get track 'info-artist nil)
              (error "No artist"))
          (or (emms-track-get track 'info-title nil)
              (error "No artist")))))

(defun jao-emms-edit-lyrics ()
  (interactive)
  (unless jao-emms-lyrics--path
    (error "No track data available."))
  (find-file-other-window jao-emms-lyrics--path))



(defun jao-emms-lyrics--download (artist title)
  (message "Retrieving lyrics...")
  (let ((fn (jao-emms-lyrics--filename artist title)))
    (shell-command-to-string (format "glyrc lyrics -n 1-8 -Y -a %s -t %s -w %s"
                                     (shell-quote-argument artist)
                                     (shell-quote-argument title)
                                     (shell-quote-argument fn)))
    (prog1 (jao-emms-lyrics--get-cached artist title) (message nil))))

(defun jao-emms-show-lyrics (&optional force)
  (interactive "P")
  (let* ((a/t (jao-emms-lyrics-track-data))
         (artist (or (car a/t) ""))
         (title (or (cdr a/t) ""))
         (buffer (jao-emms-lyrics-buffer))
         (cached (and (not force) (jao-emms-lyrics--get-cached artist title)))
         (cached (and (not (zerop (length cached))) cached))
         (lyrics (or cached (jao-emms-lyrics--download artist title)))
         (inhibit-read-only t))
    (with-current-buffer buffer
      (delete-region (point-min) (point-max))
      (insert (format "♪ %s - %s\n\n"
                      (propertize artist 'face jao-emms-font-lock-artist)
                      (propertize title 'face jao-emms-font-lock-title)))
      (when lyrics (insert lyrics))
      (goto-char (point-min))
      (setq jao-emms-lyrics--path (jao-emms-lyrics--filename artist title)))
    (pop-to-buffer buffer)))

(defun jao-emms-show-lyrics-force ()
  (interactive)
  (jao-emms-show-lyrics t))


(provide 'jao-emms-lyrics)
;;; jao-emms-lyrics.el ends here