forked from mpdel/mpdel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpdel-core.el
228 lines (183 loc) · 7.82 KB
/
mpdel-core.el
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
;;; mpdel-core.el --- Provide code to be reused by mpdel modes -*- lexical-binding: t; -*-
;; Copyright (C) 2018-2023 Damien Cassou
;; Author: Damien Cassou <[email protected]>
;; Keywords: multimedia
;; Url: https://github.com/mpdel/mpdel
;; Package-requires: ((emacs "25.1"))
;; Version: 2.1.0
;; 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 3 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, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This file provides some common ground for all MPDel
;; user-interfaces.
;;; Code:
(require 'libmpdel)
(require 'navigel)
;;; `navigel' general configuration
(navigel-method mpdel navigel-name (entity)
(libmpdel-entity-name entity))
(navigel-method mpdel navigel-children (entity callback)
(libmpdel-list entity callback))
(navigel-method mpdel navigel-equal (entity1 entity2)
(libmpdel-equal entity1 entity2))
(navigel-method mpdel navigel-parent (entity)
(libmpdel-entity-parent entity))
(defcustom mpdel-core-volume-step 5
"Amount to increase / decrease the playback volume by."
:type 'integer
:group 'mpdel-core)
;;; Public functions
(defun mpdel-core-open (entity &optional target)
"Open a buffer showing ENTITY.
If TARGET is non nil and visible on the buffer, move point to
it."
(let ((navigel-app 'mpdel))
(navigel-open entity target)))
(defun mpdel-core-selected-entities ()
"Return the selected entities in the current buffer.
If any entity is marked, return the list of all marked entities.
If no entity is marked but there is an entity at point, return a
list with this entity. Otherwise, return nil."
(navigel-marked-entities t))
(defun mpdel-core-add-to-current-playlist ()
"Add selected entities to current playlist."
(interactive)
(libmpdel-current-playlist-add (mpdel-core-selected-entities)))
(defun mpdel-core-add-to-stored-playlist ()
"Add selected entities to a stored playlist."
(interactive)
(libmpdel-stored-playlist-add (mpdel-core-selected-entities)))
(defun mpdel-core-replace-current-playlist ()
"Replace current playlist with selected entities."
(interactive)
(libmpdel-current-playlist-replace (mpdel-core-selected-entities)))
(defun mpdel-core-replace-stored-playlist ()
"Replace a stored playlist with selected entities."
(interactive)
(libmpdel-stored-playlist-replace (mpdel-core-selected-entities)))
(defun mpdel-core-insert-current-playlist ()
"Insert selected entities after currently-played song.
Start playing the first.
If no entity is selected, restart playing the current song."
(interactive)
(let ((entities (mpdel-core-selected-entities)))
(if (not entities)
(libmpdel-playback-seek "0")
(libmpdel-current-playlist-insert entities))))
(defun mpdel-core-dired ()
"Open `dired' on the entity at point."
(interactive)
(libmpdel-dired (car (mpdel-core-selected-entities))))
;;;###autoload
(defun mpdel-core-open-artists ()
"Display all artists in the MPD database."
(interactive)
(mpdel-core-open 'artists))
;;;###autoload
(defun mpdel-core-open-stored-playlists ()
"Display all stored playlists in MPD."
(interactive)
(mpdel-core-open 'stored-playlists))
;;;###autoload
(defun mpdel-core-open-albums ()
"Display all albums in the MPD database."
(interactive)
(mpdel-core-open 'albums))
;;;###autoload
(defun mpdel-core-open-directories ()
"Display all top-level directories in the MPD database."
(interactive)
(mpdel-core-open 'directories))
;;;###autoload
(defun mpdel-core-search-by-artist (name)
"Display all songs whose artist's name match NAME.
Interactively, ask for NAME."
(interactive (list (read-from-minibuffer "Search for artist: ")))
(mpdel-core-open (libmpdel-search-criteria-create :type "artist" :what name)))
;;;###autoload
(defun mpdel-core-search-by-album (name)
"Display all songs whose album's name match NAME.
Interactively, ask for NAME."
(interactive (list (read-from-minibuffer "Search for album: ")))
(mpdel-core-open (libmpdel-search-criteria-create :type "album" :what name)))
;;;###autoload
(defun mpdel-core-search-by-title (title)
"Display all songs matching TITLE.
Interactively, ask for TITLE."
(interactive (list (read-from-minibuffer "Search for title: ")))
(mpdel-core-open (libmpdel-search-criteria-create :type "title" :what title)))
;;;###autoload
(defun mpdel-core-search-by-filter (filter)
"Display all songs matching the mpd filter expression FILTER.
Interactively, ask for FILTER.
Example: ((artist == \\='name\\=') AND (any contains \\='text\\='))
Documentation: https://www.musicpd.org/doc/html/protocol.html#filters"
(interactive (list (read-from-minibuffer "Search with filter: ")))
(mpdel-core-open (libmpdel-filter-create :text filter)))
(defun mpdel-core-volume-adjust (amount)
"Adjust the playback volume by AMOUNT."
(let* ((volume (string-to-number (libmpdel-volume)))
(new-volume (max 0 (min 100 (+ volume amount)))))
(libmpdel-playback-set-volume new-volume)))
;;;###autoload
(defun mpdel-core-volume-increase (&optional amount)
"Increase the playback volume by AMOUNT.
If AMOUNT is nil, `mpdel-core-volume-step' is used instead.
Called interactively, AMOUNT can be passed as a prefix argument."
(interactive "P")
(cond
((null amount) (mpdel-core-volume-adjust mpdel-core-volume-step))
((listp amount)
(mpdel-core-volume-adjust (* (car amount) mpdel-core-volume-step)))
(t (mpdel-core-volume-adjust amount))))
;;;###autoload
(defun mpdel-core-volume-decrease (&optional amount)
"Decrease the playback volume by AMOUNT.
If AMOUNT is nil, `mpdel-core-volume-step' is used instead.
Called interactively, AMOUNT can be passed as a prefix argument."
(interactive "P")
(cond
((null amount) (mpdel-core-volume-adjust (- mpdel-core-volume-step)))
((listp amount)
(mpdel-core-volume-adjust (- (* (car amount) mpdel-core-volume-step))))
(t (mpdel-core-volume-adjust (- amount)))))
;;; Mode
(defvar mpdel-core-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "SPC") #'libmpdel-playback-play-pause)
(define-key map (kbd "M-n") #'libmpdel-playback-next)
(define-key map (kbd "M-p") #'libmpdel-playback-previous)
(define-key map (kbd "a") #'mpdel-core-add-to-current-playlist)
(define-key map (kbd "A") #'mpdel-core-add-to-stored-playlist)
(define-key map (kbd "r") #'mpdel-core-replace-current-playlist)
(define-key map (kbd "R") #'mpdel-core-replace-stored-playlist)
(define-key map (kbd "P") #'mpdel-core-insert-current-playlist)
(define-key map (kbd "C-x C-j") #'mpdel-core-dired)
(define-key map (kbd "N") #'mpdel-core-open-artists)
(define-key map (kbd "L") #'mpdel-core-open-stored-playlists)
(define-key map (kbd "s s") #'mpdel-core-search-by-title)
(define-key map (kbd "s l") #'mpdel-core-search-by-album)
(define-key map (kbd "s r") #'mpdel-core-search-by-artist)
(define-key map (kbd "^") #'navigel-open-parent)
(define-key map (kbd "n") #'next-line)
(define-key map (kbd "p") #'previous-line)
(define-key map (kbd "+") #'mpdel-core-volume-increase)
(define-key map (kbd "-") #'mpdel-core-volume-decrease)
(define-key map (kbd "C") #'libmpdel-connect-profile)
map)
"Keybindings for all MPDel buffers.")
;; Make it possible to activate `mpdel-core-map' from a keybinding:
(fset 'mpdel-core-map mpdel-core-map)
(provide 'mpdel-core)
;;; mpdel-core.el ends here
;; Local Variables:
;; checkdoc-symbol-words: ("top-level")
;; End: