Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 172 lines (155 sloc) 7.065 kB
343d5fd @talau Initial commit for emms-get-lyrics.el
authored
1 ;;; emms-get-lyrics.el --- Get the lyrics of the song emms is currently playing
2
3 ;; Copyright (C) 2007 Jay Belanger
7347347 @talau Added my mail
authored
4 ;; Copyright (C) 2010 Marcos Talau (talau@users.sourceforge.net)
343d5fd @talau Initial commit for emms-get-lyrics.el
authored
5
6 ;; emms-get-lyrics.el is free software; you can redistribute it and/or
7 ;; modify it under the terms of the GNU General Public License as
8 ;; published by the Free Software Foundation; either version 2, or
9 ;; (at your option) any later version.
10
11 ;; emms-get-lyrics.el is distributed in the hope that it will be
12 ;; useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 ;; of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;; General Public License for more details.
15
16 ;;; Commentary:
17
9519c81 Minor typo in Commentary.
Jorge A. Alfaro-Murillo authored
18 ;; The function 'emms-get-lyrics-current-song' tries to get the lyrics
19 ;; to the song that emms is currently playing. It currently requires
20 ;; w3m to get the lyrics. It copies the lyrics to a file ending in
21 ;; .lyrics; if the variable `emms-get-lyrics-use-files' is nil, it
22 ;; will just display the lyrics in a buffer without saving them to a
23 ;; file. If the variable `emms-get-lyrics-dir' is non-nil, then the
24 ;; lyrics will be put in this directory with the file
25 ;; ARTIST-TITLE.lyrics; otherwise it will be put in the same directory
26 ;; as the song file, in a file with the same name as the song file
27 ;; except the extension will by ".lyrics".
343d5fd @talau Initial commit for emms-get-lyrics.el
authored
28
29 ;;; Code:
30
31 (defvar emms-get-lyrics-version "2.0")
32
33 ;;; User Customization
34
35 (defvar emms-get-lyrics-use-files t)
36 (defvar emms-get-lyrics-dir nil)
37 (defvar emms-get-lyrics-match-lyric-notfound "This page needs content.")
38 (defvar emms-get-lyrics-match-ad-top "Ringtone to your Cell phone")
39 (defvar emms-get-lyrics-match-ad-bottom "Ringtone to your Cell phone")
40 (defvar emms-get-lyrics-debug nil)
41
42 (defun emms-get-lyrics-url (artist title)
43 "Return the URL of lyric."
44 (let ((url (concat
45 "http://www.lyricwiki.org/index.php?title="
46 (replace-regexp-in-string
47 " " "_"
48 (concat
49 artist
50 ":"
51 title))
52 "&printable=yes")))
53 (if emms-get-lyrics-debug
54 (message "emms-get-lyrics-url %s" url)
55 )
56 url))
57
58 (defun emms-get-lyrics-w3m (url buffer)
59 "Get the lyric in the Internet and put it in the BUFFER."
60 (if emms-get-lyrics-debug
61 (message "calling: w3m -dump %s" url))
62 (call-process "w3m" nil buffer nil "-dump" url))
63
64 (defun emms-get-lyrics-redirect-check (url buffer)
65 "If need to redirect to another site, do this!"
66 (goto-char (point-min))
67 (cond ((search-forward "#REDIRECT" nil t)
68 (if emms-get-lyrics-debug
69 (message "Redirecting to %s" url))
70 (delete-region (point-min) (point-max))
71 (emms-get-lyrics-w3m url buffer))))
72
73 (defun emms-get-lyrics-write-lyric (bname title artist)
74 "When Lyric is not found, ask the user if he wants to write it."
75 (switch-to-buffer bname)
76 (delete-region (point-min) (point-max))
77 (insert ";; This lyric is not found at LyricWiki.org\n")
78 (insert ";; You can write the lyric or try to find it\n")
79 (insert ";; in the Internet.\n")
80 (insert ";; Once you writted or finded, please, post it in LyricWiki.org\n\n")
81 (insert title " (" artist ")\n\n"))
82
83 (defun emms-get-lyrics (artist title fn &optional file)
84 "Core function. Get lyrics, put it in the buffer and format it."
85 (let ((bname (concat "Lyrics: " title " (" artist ")")))
86 (cond ((get-buffer bname)
87 (switch-to-buffer bname))
88 ((and file (file-exists-p file))
89 (find-file file)
90 (rename-buffer bname))
91 (t
92 (let ((buffer (if file
93 (find-file-noselect file)
94 (get-buffer-create bname))))
95 (set-buffer buffer)
96 (funcall fn (emms-get-lyrics-url artist title) buffer)
97 (goto-char (point-min))
7f2f54d @talau Removed broken variable. Patch from: Memnon Anon
authored
98 (if (not (search-forward
99 emms-get-lyrics-match-lyric-notfound nil t))
343d5fd @talau Initial commit for emms-get-lyrics.el
authored
100 ; where lyrics from?
101 (let ((frominsert
102 (save-excursion
103 (if (re-search-forward "^Retrieved from")
104 (buffer-substring-no-properties
105 (+ (line-beginning-position) 16)
106 (- (line-end-position) 1))
107 "From LyricWiki"))))
108 (emms-get-lyrics-redirect-check frominsert buffer)
109 (goto-char (point-min))
110 (insert "(from " frominsert ")\n\n")
111 (insert title " (" artist ")\n")
112 ; delete ads, and save disk space ;D
113 (let ((trash-point (point)))
114 (search-forward emms-get-lyrics-match-ad-top)
115 (delete-region trash-point (point)))
116 (search-forward emms-get-lyrics-match-ad-bottom)
117 (move-beginning-of-line nil)
118 (kill-line)
119 ; delete bottom
120 (goto-char (point-max)) (move-beginning-of-line nil)
121 (if (or
122 (search-backward "External links" nil t)
123 (search-backward "Retrieved from" nil t))
124 (delete-region (point) (point-max)))
125 (when file
126 (rename-buffer bname)
127 (save-buffer))
128 (goto-char (point-min))
129 (switch-to-buffer buffer)
130 (goto-char (point-min)))
131 (restore-buffer-modified-p nil)
132 (if (y-or-n-p (concat "Sorry, unable to find lyrics for " artist " (" file "). Do you want help writing this lyrics now? (say yes :-) ) "))
133 (emms-get-lyrics-write-lyric bname title artist)
134 (kill-buffer))
135 ))))))
136
137 (defun emms-get-lyrics-current-song ()
138 "From current song extract info and find the Lyric."
139 (interactive)
140 (let* ((track (emms-playlist-current-selected-track))
141 (artist (cdr (assoc 'info-artist track)))
142 (title (cdr (assoc 'info-title track))))
143 (cond ((not emms-player-playing-p)
144 (message "Nothing playing right now"))
145 ((not (executable-find "w3m"))
146 (message "You need the program w3m"))
147 ((not (executable-find emms-info-mp3info-program-name))
148 (message "You need the program %s (If you install it, reopen your Emacs.)" emms-info-mp3info-program-name))
149 ((not artist)
150 (message "No artist name in current song"))
151 ((not title)
152 (message "No title in current song"))
153 (t (emms-get-lyrics artist title 'emms-get-lyrics-w3m
154 (if emms-get-lyrics-use-files
155 (if emms-get-lyrics-dir
156 (concat
157 emms-get-lyrics-dir
158 "/"
159 (replace-regexp-in-string
160 " " "_"
161 (concat
162 artist
163 "-"
164 title
165 ".lyrics")))
166 (concat
167 (file-name-sans-extension (cdr (assoc 'name track)))
168 ".lyrics"))))))))
169
170 (provide 'emms-get-lyrics)
171 ;;; emms-get-lyrics.el ends here
Something went wrong with that request. Please try again.