aboutsummaryrefslogtreecommitdiff
path: root/emms-metaplaylist-mode.el
blob: f80c9bbb12c056e638d2a3f937949ec15e571d49 (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
;;; emms-metaplaylist-mode.el --- A major mode for lists of Emms
;;; playlists

;; Copyright (C) 2006 Free Software Foundation, Inc.

;; Author: Yoni Rabkin <yonirabkin@member.fsf.org>

;; This program 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 2
;; of the License, or (at your option) any later version.

;; This program 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, write to the Free Software
;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
;; 02110-1301  USA

;;; Commentary:
;;
;; `emms-metaplaylist-mode' creates an interactive list of all the
;; Emms playlist buffers. The currently active buffer is
;; highlighted. You can choose a buffer from the list with RET and get
;; taken there.

;;; Code:

(require 'emms)
(require 'emms-playlist-mode)

;;; --------------------------------------------------------
;;; Variables, customisation and faces
;;; --------------------------------------------------------

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

(defcustom emms-metaplaylist-mode-buffer-name "*Emms Playlists*"
  "*Name of the buffer in which Emms playlists will be listed."
  :type 'string
  :group 'emms-metaplaylist-mode)

(defcustom emms-metaplaylist-mode-hooks nil
  "*List of hooks to run on entry to emms-metaplaylist-mode."
  :type 'list
  :group 'emms-metaplaylist-mode)

(defface emms-metaplaylist-mode-face
  '((((class color) (background dark))
     :foreground "AntiqueWhite3")
    (((type tty) (class mono))
     :inverse-video t)
    (t :background "WhiteSmoke"))
  "Face for the buffer names in the playlists buffer."
  :group 'emms-metaplaylist-mode)

(defface emms-metaplaylist-mode-current-face
  '((((class color) (background dark))
     :foreground "red2")
    (((type tty) (class mono))
     :inverse-video t)
    (t :background "red3"))
  "Face for the current buffer name in the playlists buffer."
  :group 'emms-metaplaylist-mode)

;;; --------------------------------------------------------
;;; Keymap
;;; --------------------------------------------------------

(defconst emms-metaplaylist-mode-map
  (let ((map (make-sparse-keymap)))
    (set-keymap-parent map text-mode-map)
    (define-key map (kbd "n") 'next-line)
    (define-key map (kbd "p") 'previous-line)
    (define-key map (kbd "RET") 'emms-metaplaylist-mode-goto-current)
    (define-key map (kbd "q") 'kill-this-buffer)
    (define-key map (kbd "?") 'describe-mode)
    map)
  "Keymap for `emms-metaplaylist-mode'.")

;;; --------------------------------------------------------
;;; Metaplaylist
;;; --------------------------------------------------------

(defun emms-metaplaylist-mode-goto-current ()
  "Switch to the buffer at point."
  (interactive)
  (switch-to-buffer
   (buffer-substring (point-at-bol)
		     (point-at-eol))))

(defun get-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))

;; Since there will never be a significantly large amount of playlist
;; buffers co-existing at once, we allow ourselves not to keep
;; state. We regenerate the playlists buffer anew on demand.
(defun emms-metaplaylist-mode-create ()
  "Create or recreate the meta-playlist buffer."
  (let ((name emms-metaplaylist-mode-buffer-name)
	(playlists (get-emms-playlist-buffers)))
    (if playlists
	(progn
	  (condition-case nil
	      (kill-buffer name)
	    (error nil))
	  (get-buffer-create name)
	  (with-current-buffer name
	    (emms-metaplaylist-mode)
	    (save-excursion
	      (mapc (lambda (buf)
		      (let ((inhibit-read-only t))
			(insert (buffer-name buf))
			(emms-playlist-mode-overlay-track
			 (point-at-bol)
			 (point-at-eol)
			 (if (eq buf emms-playlist-buffer)
			     'emms-metaplaylist-mode-face
			   'emms-metaplaylist-mode-current-face
			   1)
			 (newline))))
		    playlists))
	    (current-buffer)))	       ; return the buffer as lisp obj
      (error "No Emms playlist buffers"))))

;;; --------------------------------------------------------
;;; Mode entry
;;; --------------------------------------------------------

(defun emms-metaplaylist-mode-go ()
  "Single entry point to the metaplaylist interface."
  (interactive)
  (emms-metaplaylist-mode-create)
  (switch-to-buffer emms-metaplaylist-mode-buffer-name))

(defun emms-metaplaylist-mode ()
  "A major mode for Emms playlists."
  (interactive)
  (kill-all-local-variables)

  (use-local-map emms-metaplaylist-mode-map)
  (setq major-mode 'emms-metaplaylist-mode
	mode-name "Emms-MetaPlaylist")

  (setq buffer-read-only t)

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

(provide 'emms-metaplaylist-mode)

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