Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 159 lines (140 sloc) 7.098 kB
d4e74b7 @thierryvolpiatto * helm-emms.el: New file.
thierryvolpiatto authored
1 ;;; helm-emms.el --- Emms for Helm.
2
3 ;; Copyright (C) 2012 Thierry Volpiatto <thierry.volpiatto@gmail.com>
4
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
9
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 ;;; Code:
19
20 (eval-when-compile (require 'cl))
21 (require 'helm)
22
23 (defun helm-emms-stream-edit-bookmark (elm)
24 "Change the information of current emms-stream bookmark from helm."
25 (declare (special emms-stream-list))
26 (let* ((cur-buf helm-current-buffer)
27 (bookmark (assoc elm emms-stream-list))
28 (name (read-from-minibuffer "Description: "
29 (nth 0 bookmark)))
30 (url (read-from-minibuffer "URL: "
31 (nth 1 bookmark)))
32 (fd (read-from-minibuffer "Feed Descriptor: "
33 (int-to-string (nth 2 bookmark))))
34 (type (read-from-minibuffer "Type (url, streamlist, or lastfm): "
35 (format "%s" (car (last bookmark))))))
36 (save-window-excursion
37 (emms-streams)
38 (when (re-search-forward (concat "^" name) nil t)
39 (beginning-of-line)
40 (emms-stream-delete-bookmark)
41 (emms-stream-add-bookmark name url (string-to-number fd) type)
42 (emms-stream-save-bookmarks-file)
43 (emms-stream-quit)
44 (helm-c-switch-to-buffer cur-buf)))))
45
46 (defun helm-emms-stream-delete-bookmark (candidate)
47 "Delete emms-streams bookmarks from helm."
48 (let* ((cands (helm-marked-candidates))
49 (bmks (loop for bm in cands collect
50 (car (assoc bm emms-stream-list))))
51 (bmk-reg (mapconcat 'regexp-quote bmks "\\|^")))
52 (when (y-or-n-p (format "Really delete radios\n -%s: ? "
53 (mapconcat 'identity bmks "\n -")))
54 (save-window-excursion
55 (emms-streams)
56 (goto-char (point-min))
57 (loop while (re-search-forward bmk-reg nil t)
58 do (progn (beginning-of-line)
59 (emms-stream-delete-bookmark))
60 finally do (progn
61 (emms-stream-save-bookmarks-file)
62 (emms-stream-quit)))))))
63
64 (defvar helm-c-source-emms-streams
65 '((name . "Emms Streams")
66 (init . (lambda ()
67 (emms-stream-init)))
68 (candidates . (lambda ()
69 (declare (special emms-stream-list))
70 (mapcar 'car emms-stream-list)))
71 (action . (("Play" . (lambda (elm)
72 (declare (special emms-stream-list))
73 (let* ((stream (assoc elm emms-stream-list))
74 (fn (intern (concat "emms-play-" (symbol-name (car (last stream))))))
75 (url (second stream)))
76 (funcall fn url))))
77 ("Delete" . helm-emms-stream-delete-bookmark)
78 ("Edit" . helm-emms-stream-edit-bookmark)))
79 (filtered-candidate-transformer . helm-c-adaptive-sort)))
80
81 ;; Don't forget to set `emms-source-file-default-directory'
82 (defvar helm-c-source-emms-dired
83 '((name . "Music Directory")
84 (candidates . (lambda ()
85 (cddr (directory-files emms-source-file-default-directory))))
86 (action .
87 (("Play Directory" . (lambda (item)
88 (emms-play-directory
89 (expand-file-name
90 item
91 emms-source-file-default-directory))))
92 ("Open dired in file's directory" . (lambda (item)
93 (helm-c-open-dired
94 (expand-file-name
95 item
96 emms-source-file-default-directory))))))
97 (filtered-candidate-transformer . helm-c-adaptive-sort)))
98
99
100 (defun helm-c-emms-files-modifier (candidates source)
101 (let ((current-playlist (with-current-emms-playlist
102 (loop with cur-list = (emms-playlist-tracks-in-region
103 (point-min) (point-max))
104 for i in cur-list
105 for name = (assoc-default 'name i)
106 when name
107 collect name))))
108 (loop for i in candidates
109 if (member (cdr i) current-playlist)
110 collect (cons (propertize (car i)
111 'face 'helm-emms-playlist)
112 (cdr i)) into lis
113 else collect i into lis
114 finally return (reverse lis))))
115
116 (defun helm-c-emms-play-current-playlist ()
117 "Play current playlist."
118 (with-current-emms-playlist
119 (emms-playlist-first)
120 (emms-playlist-mode-play-smart)))
121
122 (defvar helm-c-source-emms-files
123 '((name . "Emms files")
124 (candidates . (lambda ()
125 (loop for v being the hash-values in emms-cache-db
126 for name = (assoc-default 'name v)
127 for artist = (or (assoc-default 'info-artist v) "unknown")
128 for genre = (or (assoc-default 'info-genre v) "unknown")
129 for tracknum = (or (assoc-default 'info-tracknumber v) "unknown")
130 for song = (or (assoc-default 'info-title v) "unknown")
131 for info = (concat artist " - " genre " - " tracknum ": " song)
132 unless (string-match "^\\(http\\|mms\\):" name)
133 collect (cons info name))))
134 (filtered-candidate-transformer . helm-c-emms-files-modifier)
135 (candidate-number-limit . 9999)
136 (action . (("Play file" . emms-play-file)
137 ("Add to Playlist and play (C-u clear current)"
138 . (lambda (candidate)
139 (when helm-current-prefix-arg
140 (emms-playlist-current-clear))
141 (emms-playlist-new)
142 (mapc 'emms-add-playlist-file (helm-marked-candidates))
143 (unless emms-player-playing-p
144 (helm-c-emms-play-current-playlist))))))))
145
146 ;;;###autoload
147 (defun helm-emms ()
148 "Preconfigured `helm' for emms sources."
149 (interactive)
150 (helm :sources '(helm-c-source-emms-streams
151 helm-c-source-emms-files
152 helm-c-source-emms-dired)
153 :buffer "*Helm Emms*"))
154
155
156 (provide 'helm-emms)
157
158 ;;; helm-emms ends here
Something went wrong with that request. Please try again.