aboutsummaryrefslogtreecommitdiff
path: root/emms-playlist-mode.el
blob: 46b6badc9d420fa138c6256abb7d55a288a10912 (plain) (blame)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
;;; emms-playlist-mode.el --- Playlist mode for Emms.

;;; Copyright Yoni Rabkin under the GNU General Public License.
;;; This file is a part of the Emacs Multimedia system.

;;; Commentary:
;;;
;;; I'm designing this as a method of displaying and manipulating the
;;; different Emms playlist buffers defined by the user.

;;; Feature requests:
;;;
;;; (1) Lukhas wants `emms-list-playlist-buffers' to list all the
;;;     playlist buffers.
;;;
;;; (2) Add emms-info support which re-writes the track titles.
;;;
;;; (3) Add multi-line formatting and arbitrary comment entry.
;;;
;;; (4) Font-locking for unselected tracks via overlays.

;;; --------------------------------------------------------
;;; Variables
;;; --------------------------------------------------------

(defvar emms-playlist-mode-hook nil
  "Emms playlist mode hook.")

(defvar emms-playlist-mode-selected-overlay-marker nil)

(make-variable-buffer-local
 'emms-playlist-mode-selected-overlay-marker)

;; Is this needed at all?
(defvar emms-playlist-mode-prepend-show-string
  "Now Playling:")

(defgroup emms-playlist-mode nil
  "*The Emacs Multimedia System playlist mode."
  :prefix "emms-playlist-mode-"
  :group 'multimedia)

;;; --------------------------------------------------------
;;; Faces and font locking
;;; --------------------------------------------------------

;; change the eye-gouging colors before release
(defface emms-playlist-track-face
  '((((class color) (background dark))
     :background "yellow")
    (((type tty) (class mono))
     :inverse-video t)
    (t :background "red"))
  "Basic face for highlighting the selected track."
  :group 'emms-playlist-mode)

(defface emms-playlist-selected-face
  '((((class color) (background dark))
     :background "blue1")
    (((type tty) (class mono))
     :inverse-video t)
    (t :background "blue3"))
  "Basic face for highlighting the region of the track."
  :group 'emms-playlist-mode)

;; FIXME: automatic font-locking does not work. See below.
(defvar emms-playlist-mode-font-lock-keywords
  '(("\\(.*\\)" (1 emms-playlist-track-face)))
  "Keyword highlighting specification for `emms-playlist-mode'.")

;;; --------------------------------------------------------
;;; Keys
;;; --------------------------------------------------------

(defconst emms-playlist-mode-map
  (let ((emms-playlist-mode-map (make-sparse-keymap)))
    (set-keymap-parent emms-playlist-mode-map text-mode-map)
    (define-key emms-playlist-mode-map (kbd "n") 'emms-next)
    (define-key emms-playlist-mode-map (kbd "p") 'emms-previous)
    (define-key emms-playlist-mode-map (kbd "C-x C-s") 'emms-playlist-save)
    (define-key emms-playlist-mode-map (kbd "C-k") 'emms-playlist-mode-kill-track)
    (define-key emms-playlist-mode-map (kbd "d") 'emms-playlist-mode-kill-track)
    (define-key emms-playlist-mode-map (kbd "s") 'emms-stop)
    (define-key emms-playlist-mode-map (kbd "f") 'emms-playlist-mode-show-current-track)
    (define-key emms-playlist-mode-map (kbd "F") 'emms-playlist-mode-show-current-track-clean)
    (define-key emms-playlist-mode-map (kbd "RET") 'emms-playlist-mode-play-current-track)
    (define-key emms-playlist-mode-map (kbd "q") 'bury-buffer)
    (define-key emms-playlist-mode-map (kbd "<mouse-2>") 'emms-playlist-mode-play-current-track)
    (define-key emms-playlist-mode-map (kbd "?") 'describe-mode)
    (define-key emms-playlist-mode-map (kbd "M-<") 'emms-playlist-mode-first)
    (define-key emms-playlist-mode-map (kbd "M->") 'emms-playlist-mode-last)
    (define-key emms-playlist-mode-map (kbd "C-n") 'emms-playlist-mode-select-next)
    (define-key emms-playlist-mode-map (kbd "C-p") 'emms-playlist-mode-select-previous)
    (define-key emms-playlist-mode-map (kbd "r") 'emms-playlist-mode-play-random)
    emms-playlist-mode-map)
  "Keymap for emms-playlist-mode.")

;; We will add to this wrapper boundry checking as needed later.
(defmacro emms-playlist-mode-move-wrapper (name fun)
  `(defun ,name ()
     ,(format "Interactive wrapper around `%s' for playlist-mode."
	      fun)
     (interactive)
     (,fun)))

(defmacro emms-playlist-mode-when-playing-p (&rest body)
  `(lambda ()
     (if emms-player-playing-p
	 ,@body
       (error "No track is currently playing"))))
  
(emms-playlist-mode-move-wrapper emms-playlist-mode-first 
				 emms-playlist-first)

(emms-playlist-mode-move-wrapper emms-playlist-mode-last 
				 emms-playlist-last)

(emms-playlist-mode-move-wrapper emms-playlist-mode-select-next 
				 emms-playlist-next)

(emms-playlist-mode-move-wrapper emms-playlist-mode-select-previous
				 emms-playlist-previous)

(emms-playlist-mode-move-wrapper emms-playlist-mode-select-random
				 emms-playlist-select-random)

(defun emms-playlist-mode-play-random ()
  (interactive)
  (emms-stop)
  (emms-playlist-mode-select-random)
  (emms-start))

(defun emms-playlist-mode-show-current-track ()
  (interactive)
  (funcall
   (emms-playlist-mode-when-playing-p
    (message "%s %s"
	     emms-playlist-mode-prepend-show-string
	     (emms-track-description 
	      (emms-playlist-selected-track))))))

(defun emms-playlist-mode-show-current-track-clean ()
  (interactive)
  (funcall
   (emms-playlist-mode-when-playing-p
    (message "%s"
	     (emms-track-description 
	      (emms-playlist-selected-track))))))

(defun emms-playlist-mode-selected-at ()
  (eq (emms-playlist-track-at) 
      (emms-playlist-selected-track)))

(defun emms-playlist-mode-play-current-track ()
  (interactive)
  (emms-playlist-select (point))
  (when emms-player-playing-p
    (emms-stop))
  (emms-start))

;; The logic for killing tracks in an interactive manner is
;; suprisingly annoying
(defun emms-playlist-mode-kill-track ()
  (interactive)
  (let ((region (emms-property-region (point) 'emms-track)))
    (cond ((not (emms-playlist-track-at)) 
	   (kill-line 1))	     ; Purposfully kills only one line
	  ((and (not (emms-playlist-mode-selected-at))
		(emms-playlist-track-at))
	   (kill-region (car region)
			(cdr region))
	   (emms-playlist-mode-kill-track))
	  ((and emms-player-playing-p
		(emms-playlist-mode-selected-at))
	   (emms-stop)
	   (emms-next-noerror)
	   (kill-region (car region)
			(cdr region))
	   (emms-playlist-mode-kill-track))
	  ((and (not emms-player-playing-p)
		(emms-playlist-mode-selected-at))
	   (kill-region (car region)
			(cdr region))
	   (emms-playlist-mode-kill-track))
	  (t (error "Cannot kill content at point")))))

;;; --------------------------------------------------------
;;; Overlay
;;; --------------------------------------------------------

(defun emms-playlist-mode-overlay-track (start end face)
  (let ((o (make-overlay start end)))
    (overlay-put o 'track t)
    (overlay-put o 'face face)))

(defun emms-playlist-mode-overlay-at (face)
  (let ((o (make-overlay (point-at-bol) (point-at-eol))))
    (overlay-put o 'track t)
    (overlay-put o 'face face)))

;; Selected track overlaying track in constant time.
(defun emms-playlist-mode-overlay-selected ()
  (unless (null emms-playlist-mode-selected-overlay-marker)
    (save-excursion
      (goto-char emms-playlist-mode-selected-overlay-marker)
      (remove-overlays (point) 
		       (point-at-eol))))
  (save-excursion
    (goto-char emms-playlist-selected-marker)
    (setq emms-playlist-mode-selected-overlay-marker 
	  (point-marker)) 
    (emms-playlist-mode-overlay-at
     'emms-playlist-selected-face))
  nil)

;;; --------------------------------------------------------
;;; Hooks
;;; --------------------------------------------------------

(add-hook 'emms-playlist-selection-changed-hook
	  'emms-playlist-mode-overlay-selected)

;;; --------------------------------------------------------
;;; Playlists
;;; --------------------------------------------------------

(defun emms-playlist-buffers ()
  "Return a list of EMMS playlist buffers."
  (let ((lis nil))
    (mapc (lambda (buf)
            (with-current-buffer buf
              (when emms-playlist-buffer-p
                (setq lis (cons buf lis)))))
          (buffer-list))
    lis))

;;; --------------------------------------------------------
;;; Saving/Restoring
;;; --------------------------------------------------------

(defun emms-playlist-mode-save-buffer (buffer filename)
  "Saves a playlist buffer in a file, including annotations."
  (interactive "bPlaylist buffer to save: \nFFile to save buffer as: ")
  (with-current-buffer (find-file-noselect filename)
    (erase-buffer)
    (prin1 (with-current-buffer buffer 
             (buffer-string))
           (current-buffer))
    (save-buffer)
    (kill-buffer (current-buffer))))

(defun emms-playlist-mode-open-buffer (filename)
  "Opens a previously saved playlist buffer.

It creates a buffer called \"filename\", and restore the contents
of the saved playlist inside."
  (interactive "fFile: ")
  (let* ((s)
         (buffer (find-file-noselect filename))
         (name   (buffer-name buffer)))
    (with-current-buffer buffer
      (setq s (read (buffer-string))))
    (kill-buffer buffer)
    (with-current-buffer (get-buffer-create name)
      (insert s))))

(defun emms-playlist-mode-save-active-buffer (filename)
  "Saves the active playlist buffer to a file."
  (interactive "FFile to save playlist buffer as: ")
  (emms-playlist-mode-save-buffer emms-playlist-buffer filename))

;;; --------------------------------------------------------
;;; Entry
;;; --------------------------------------------------------

;; FIXME: everything is wrong here, implement non-twisted logic and an
;; interactive manner of selecting/creating a different playlist to
;; the current/default one.
(defun emms-playlist-mode-go (&optional pl-buffer)
  (interactive)
  (when (null pl-buffer) 
    (setq pl-buffer emms-playlist-buffer)
    (when (not pl-buffer)
      (setq pl-buffer (emms-playlist-new))))
  (switch-to-buffer pl-buffer)
  (unless (eq major-mode 'emms-playlist-mode)
    (emms-playlist-mode)))

(defun emms-playlist-mode-startup ()
  (unless (or emms-playlist-selected-marker
	      emms-player-playing-p)
    (emms-stop)
    (emms-playlist-select-first))
  (when emms-playlist-selected-marker
    (emms-playlist-mode-overlay-selected)
    (goto-char (or emms-playlist-mode-selected-overlay-marker
		   (point-min)))))

;;;###autoload
(defun emms-playlist-mode ()
  "A major mode for Emms playlists."
  (interactive)
  (kill-all-local-variables)  

  (use-local-map emms-playlist-mode-map)
  (setq major-mode 'emms-playlist-mode
	mode-name "Emms-Playlist")

  ;; FIXME: *automatic* font-locking does not work and I have no idea
  ;; why. Anyone who wants to fix it is more than welcomed to it.
  (set (make-local-variable 'font-lock-defaults)
       '(emms-playlist-mode-font-lock-keywords))

  (emms-playlist-mode-startup)

  (run-hooks 'emms-playlist-mode-hooks))

(provide 'emms-playlist-mode)

;;; emms-playlist-mode.el ends here.