]> code.delx.au - gnu-emacs-elpa/blob - ampc.el
* ampc.el: Refine options for sending commands.
[gnu-emacs-elpa] / ampc.el
1 ;;; ampc.el --- Asynchronous Music Player Controller -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
4
5 ;; Author: Christopher Schmidt <christopher@ch.ristopher.com>
6 ;; Maintainer: Christopher Schmidt <christopher@ch.ristopher.com>
7 ;; Version: 0.1.3
8 ;; Created: 2011-12-06
9 ;; Keywords: ampc, mpc, mpd
10 ;; Compatibility: GNU Emacs: 24.x
11
12 ;; This file is part of ampc.
13
14 ;; This program is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28 ;;; * description
29 ;; ampc is a controller for the Music Player Daemon (http://mpd.wikia.com/).
30
31 ;;; ** installation
32 ;; If you use GNU ELPA, install ampc via M-x package-list-packages RET or
33 ;; (package-install 'ampc). Otherwise, grab the files in this repository and
34 ;; put the emacs lisp ones somewhere in your load-path or add the directory the
35 ;; files are in to it, e.g.:
36 ;;
37 ;; (add-to-list 'load-path "~/.emacs.d/ampc")
38 ;; (autoload 'ampc "ampc" nil t)
39 ;;
40 ;; Byte-compile ampc (M-x byte-compile-file RET /path/to/ampc.el RET) to improve
41 ;; its performance!
42
43 ;;; *** tagger
44 ;; ampc is not only a frontend to MPD but also a full-blown audio file tagger.
45 ;; To use this feature you have to build the backend application, `ampc_tagger',
46 ;; which in turn uses TagLib (http://taglib.github.com/), a dual-licended
47 ;; (LGPL/MPL) audio meta-data library written in C++. TagLib has no
48 ;; dependencies on its own.
49 ;;
50 ;; To build `ampc_tagger', locate ampc_tagger.cpp. The file can be found in the
51 ;; directory in which this file, ampc.el, is located. Compile the file and
52 ;; either customize `ampc-tagger-executable' to point to the binary file or move
53 ;; the executable in a suitable directory so Emacs finds it via consulting
54 ;; `exec-path'.
55 ;;
56 ;; g++ -O2 ampc_tagger.cpp -oampc_tagger -ltag && sudo cp ampc_tagger /usr/local/bin && rm ampc_tagger
57 ;;
58 ;; You have to customize `ampc-tagger-music-directories' in order to use the
59 ;; tagger. This variable should be a list of directories in which your music
60 ;; files are located. Usually this list should have only one entry, the value
61 ;; of your mpd.conf's `music_directory'.
62 ;;
63 ;; If `ampc-tagger-backup-directory' is non-nil, the tagger saved copies of all
64 ;; files that are about to be modified to this directory. Emacs's regular
65 ;; numeric backup filename syntax is used for the backup file names. By default
66 ;; `ampc-tagger-backup-directory' is set to "~/.emacs.d/ampc-backups/".
67
68 ;;; ** usage
69 ;; To invoke ampc call the command `ampc', e.g. via M-x ampc RET. The first
70 ;; argument to `ampc' is the host, the second is the port. Both values default
71 ;; to nil. If nil, ampc will use the value specified in `ampc-default-server',
72 ;; by default localhost:6600. To make ampc use the full frame rather than the
73 ;; selected window for its window setup, customise `ampc-use-full-frame' to a
74 ;; non-nil value.
75 ;;
76 ;; ampc offers three independent views which expose different parts of the user
77 ;; interface. The current playlist view, the default view at startup, may be
78 ;; accessed using the `J' key (that is `S-j'). The playlist view may be
79 ;; accessed using the `K' key. The outputs view may be accessed by pressing
80 ;; `L'.
81
82 ;;; *** current playlist view
83 ;; The playlist view looks like this:
84 ;;
85 ;; .........................
86 ;; . 1 . 3 . 4 . 5 .
87 ;; .......... . . .
88 ;; . 2 . . . .
89 ;; . . . . .
90 ;; . . . . .
91 ;; . ................
92 ;; . . 6 .
93 ;; . . .
94 ;; .........................
95 ;;
96 ;; Window one exposes basic information about the daemon, such as the current
97 ;; state (stop/play/pause), the song currently playing or the volume.
98 ;;
99 ;; All windows, except the status window, contain a tabular list of items. Each
100 ;; item may be selected/marked. There may be multiple selections.
101 ;;
102 ;; To mark an entry, move the point to the entry and press `m' (ampc-mark). To
103 ;; unmark an entry, press `u' (ampc-unmark). To unmark all entries, press `U'
104 ;; (ampc-unmark-all). To toggle marks, press `t' (ampc-toggle-marks). Pressing
105 ;; `<down-mouse-1>' with the mouse mouse cursor on a list entry will move point
106 ;; to the entry and toggle the mark. To navigate to the next entry, press `n'
107 ;; (ampc-next-line). Analogous, pressing `p' (ampc-previous-line) moves the
108 ;; point to the previous entry.
109 ;;
110 ;; Window two shows the current playlist. The song that is currently played by
111 ;; the daemon, if any, is highlighted. To delete the selected songs from the
112 ;; playlist, press `d' (ampc-delete). Pressing `<down-mouse-3>' will move the
113 ;; point to the entry under cursor and delete it from the playlist. To move the
114 ;; selected songs up, press `<up>' (ampc-up). Analogous, press `<down>'
115 ;; (ampc-down) to move the selected songs down. Pressing `<return>'
116 ;; (ampc-play-this) or `<down-mouse-2>' will play the song at point/cursor.
117 ;;
118 ;; Windows three to five are tag browsers. You use them to narrow the song
119 ;; database to certain songs. Think of tag browsers as filters, analogous to
120 ;; piping `grep' outputs through additional `grep' filters. The property of the
121 ;; songs that is filtered is displayed in the header line of the window.
122 ;;
123 ;; Window six shows the songs that match the filters defined by windows three to
124 ;; five. To add the selected song to the playlist, press `a' (ampc-add).
125 ;; Pressing `<down-mouse-3>' will move the point to the entry under the cursor
126 ;; and execute `ampc-add'. These key bindings works in tag browsers as well.
127 ;; Calling `ampc-add' in a tag browser adds all songs filtered up to the
128 ;; selected browser to the playlist.
129 ;;
130 ;; The tag browsers of the current playlist view (accessed via `J') are `Genre'
131 ;; (window 3), `Artist' (window 4) and `Album' (window 5). The key `M' may be
132 ;; used to fire up a slightly modified current playlist view. There is no
133 ;; difference to the default current playlist view other than that the tag
134 ;; browsers filter to `Genre' (window 3), `Album' (window 4) and `Artist'
135 ;; (window 5). Metaphorically speaking, the order of the `grep' filters defined
136 ;; by the tag browsers is different.
137
138 ;;; *** playlist view
139 ;; The playlist view resembles the current playlist view. The window, which
140 ;; exposes the playlist content, is replaced by three windows, vertically
141 ;; arragned, though. The top one still shows the current playlist. The bottom
142 ;; one shows a list of stored playlists. The middle window exposes the content
143 ;; of the selected (stored) playlist. All commands that used to work in the
144 ;; current playlist view and modify the current playlist now modify the selected
145 ;; (stored) playlist unless the point is within the current playlist buffer.
146 ;; The list of stored playlists is the only view in ampc that may have only one
147 ;; marked entry.
148 ;;
149 ;; To queue a playlist, press `l' (ampc-load) or `<down-mouse-2>'. To delete a
150 ;; playlist, press `d' (ampc-delete-playlist) or `<down-mouse-3>'. The command
151 ;; `ampc-rename-playlist', bound to `r', can be used to rename a playlist.
152 ;;
153 ;; Again, the key `<' may be used to setup a playlist view with a different
154 ;; order of tag browsers.
155
156 ;;; *** outputs view
157 ;; The outputs view contains a single list which shows the configured outputs of
158 ;; MPD. To toggle the enabled property of the selected outputs, press `a'
159 ;; (ampc-toggle-output-enabled) or `<mouse-3>'.
160
161 ;;; ** tagger
162 ;; To start the tagging subsystem, press `I' (ampc-tagger). This key binding
163 ;; works in every buffer associated with ampc. First, the command tries to
164 ;; determine which files you want to tag. The files are collected using either
165 ;; the selected entries within the current buffer, the file associated with the
166 ;; entry at point, or, if both sources did not provide any files, the audio file
167 ;; that is currently played by MPD. Next, the tagger view is created. On the
168 ;; right there is the buffer that contain the tag data. Each line in this
169 ;; buffer represents a tag with a value. Tag and value are separated by a
170 ;; colon. Valid tags are "Title", "Artist", "Album", "Comment", "Genre", "Year"
171 ;; and "Track". The value can be an arbitrary string. Whitespaces in front and
172 ;; at the end of the value are ignored. If the value is "<keep>", the tag line
173 ;; is ignored.
174 ;;
175 ;; To save the specified tag values back to the files, press `C-c C-c'
176 ;; (ampc-tagger-save). To exit the tagger and restore the previous window
177 ;; configuration, press `C-c C-q'. `C-u C-c C-c' saved the tags and exits the
178 ;; tagger. Only tags that are actually specified within the tagger buffer
179 ;; written back to the file. Other tags will not be touched by ampc. For
180 ;; example, to clear the "Commentary" tag, you need to specify the line
181 ;;
182 ;; Commentary:
183 ;;
184 ;; In the tagger buffer. Omitting this line will make the tagger not touch the
185 ;; "Commentary" tag at all.
186 ;;
187 ;; On the right there is the files list buffer. The selection of this buffer
188 ;; specifies which files the command `ampc-tag-save' will write to. If no file
189 ;; is selected, the file at point in the file list buffer is used.
190 ;;
191 ;; To reset the values of the tags specified in the tagger buffer to the common
192 ;; values of all selected files specified by the selection of the files list
193 ;; buffer, press `C-c C-r' (ampc-tagger-reset). With a prefix argument,
194 ;; `ampc-tagger-reset' restores missing tags as well.
195 ;;
196 ;; You can use tab-completion within the tagger buffer for both tags and tag
197 ;; values.
198 ;;
199 ;; You can also use the tagging subsystem on its own without a running ampc
200 ;; instance. To start the tagger, call `ampc-tag-files'. This function accepts
201 ;; one argument, a list of absolute file names which are the files to tag. ampc
202 ;; provides a minor mode for dired, `ampc-tagger-dired-mode'. If this mode is
203 ;; enabled within a dired buffer, pressing `C-c C-t' (ampc-tagger-dired) will
204 ;; start the tagger on the current selection.
205 ;;
206 ;; The following ampc-specific hooks are run during tagger usage:
207 ;;
208 ;; `ampc-tagger-grab-hook': Run by the tagger before grabbing tags of a file.
209 ;; Each function is called with one argument, the file name.
210 ;;
211 ;; `ampc-tagger-grabbed-hook': Run by the tagger after grabbing tags of a file.
212 ;; Each function is called with one argument, the file name.
213 ;;
214 ;; `ampc-tagger-store-hook': Run by the tagger before writing tags back to a
215 ;; file. Each function is called with two arguments, FOUND-CHANGED and DATA.
216 ;; FOUND-CHANGED is non-nil if the tags that are about to be written differ from
217 ;; the ones in the file. DATA is a cons. The car specifies the full file name
218 ;; of the file that is about to be written to, the cdr is an alist that
219 ;; specifies the tags that are about to be (over-)written. The car of each
220 ;; entry in this list is a symbol specifying the tag (one of the ones in
221 ;; `ampc-tagger-tags'), the cdr a string specifying the value. The cdr of DATA
222 ;; may be modified. If FOUND-CHANGED is nil and the cdr of DATA is not modified
223 ;; throughout the hook is run, the file is not touched.
224 ;; `ampc-tagger-stored-hook' is still run, though.
225 ;;
226 ;; `ampc-tagger-stored-hook': Run by the tagger after writing tags back to a
227 ;; file. Each function is called with two arguments, FOUND-CHANGED and DATA.
228 ;; These are the same arguments that were already passed to
229 ;; `ampc-tagger-store-hook'. The car of DATA, the file name, may be modified.
230 ;;
231 ;; These hooks can be used to handle vc locking and unlocking of files. For
232 ;; renaming files according to their (new) tag values, ampc provides the
233 ;; function `ampc-tagger-rename-artist-title' which may be added to
234 ;; `ampc-tagger-stored-hook'. The new file name generated by this function is
235 ;; "Artist"_-_"Title"."extension". Characters within "Artist" and "Title" that
236 ;; are not alphanumeric are substituted with underscores.
237
238 ;;; ** global keys
239 ;; Aside from `J', `M', `K', `<' and `L', which may be used to select different
240 ;; views, and `I' which starts the tagger, ampc defines the following global
241 ;; keys. These binding are available in every buffer associated with ampc:
242 ;;
243 ;; `k' (ampc-toggle-play): Toggle play state. If MPD does not play a song,
244 ;; start playing the song at point if the current buffer is the playlist buffer,
245 ;; otherwise start at the beginning of the playlist. With numeric prefix
246 ;; argument 4, stop player rather than pause if applicable.
247 ;;
248 ;; `l' (ampc-next): Play next song.
249 ;; `j' (ampc-previous): Play previous song
250 ;;
251 ;; `c' (ampc-clear): Clear playlist.
252 ;; `s' (ampc-shuffle): Shuffle playlist.
253 ;;
254 ;; `S' (ampc-store): Store playlist.
255 ;; `O' (ampc-load): Load selected playlist into the current playlist.
256 ;; `R' (ampc-rename-playlist): Rename selected playlist.
257 ;; `D' (ampc-delete-playlist): Delete selected playlist.
258 ;;
259 ;; `y' (ampc-increase-volume): Increase volume.
260 ;; `M-y' (ampc-decrease-volume): Decrease volume.
261 ;; `C-M-y' (ampc-set-volume): Set volume.
262 ;; `h' (ampc-increase-crossfade): Increase crossfade.
263 ;; `M-h' (ampc-decrease-crossfade): Decrease crossfade.
264 ;; `C-M-h' (ampc-set-crossfade): Set crossfade.
265 ;;
266 ;; `e' (ampc-toggle-repeat): Toggle repeat state.
267 ;; `r' (ampc-toggle-random): Toggle random state.
268 ;; `f' (ampc-toggle-consume): Toggle consume state.
269 ;;
270 ;; `P' (ampc-goto-current-song): Select the current playlist window and move
271 ;; point to the current song.
272 ;; `G' (ampc-mini): Select song to play via `completing-read'.
273 ;;
274 ;; `T' (ampc-trigger-update): Trigger a database update.
275 ;; `Z' (ampc-suspend): Suspend ampc.
276 ;; `q' (ampc-quit): Quit ampc.
277 ;;
278 ;; The keymap of ampc is designed to fit the QWERTY United States keyboard
279 ;; layout. If you use another keyboard layout, feel free to modify
280 ;; `ampc-mode-map'. For example, I use a regular QWERTZ German keyboard
281 ;; (layout), so I modify `ampc-mode-map' in my init.el like this:
282 ;;
283 ;; (eval-after-load 'ampc
284 ;; '(flet ((substitute-ampc-key
285 ;; (from to)
286 ;; (define-key ampc-mode-map to (lookup-key ampc-mode-map from))
287 ;; (define-key ampc-mode-map from nil)))
288 ;; (substitute-ampc-key (kbd "z") (kbd "Z"))
289 ;; (substitute-ampc-key (kbd "y") (kbd "z"))
290 ;; (substitute-ampc-key (kbd "M-y") (kbd "M-z"))
291 ;; (substitute-ampc-key (kbd "C-M-y") (kbd "C-M-z"))
292 ;; (substitute-ampc-key (kbd "<") (kbd ";"))))
293 ;;
294 ;; If ampc is suspended, you can still use every interactive command that does
295 ;; not directly operate on or with the user interace of ampc. For example it is
296 ;; perfectly fine to call `ampc-increase-volume' or `ampc-toggle-play' via M-x
297 ;; RET. Especially the commands `ampc-status' and `ampc-mini' are predesignated
298 ;; to be bound in the global keymap and called when ampc is suspended.
299 ;; `ampc-status' messages the information that is displayed by the status window
300 ;; of ampc. `ampc-mini' lets you select a song to play via `completing-read'.
301 ;; To start ampc suspended, call `ampc' with the third argument being non-nil.
302 ;; To check whether ampc is connected to the daemon and/or suspended, call
303 ;; `ampc-is-on-p' or `ampc-suspended-p'.
304 ;;
305 ;; (global-set-key (kbd "<f7>")
306 ;; (lambda ()
307 ;; (interactive)
308 ;; (unless (ampc-on-p)
309 ;; (ampc nil nil t))
310 ;; (ampc-status)))
311 ;; (global-set-key (kbd "<f8>")
312 ;; (lambda ()
313 ;; (interactive)
314 ;; (unless (ampc-on-p)
315 ;; (ampc nil nil t))
316 ;; (ampc-mini)))
317
318 ;;; Code:
319 ;;; * code
320 (eval-when-compile
321 (require 'cl))
322 (require 'network-stream)
323 (require 'avl-tree)
324
325 ;;; ** declarations
326 (defgroup ampc ()
327 "Asynchronous client for the Music Player Daemon."
328 :prefix "ampc-"
329 :group 'multimedia
330 :group 'applications)
331
332 ;;; *** customs
333 (defcustom ampc-debug nil
334 "Non-nil means log outgoing communication between ampc and MPD.
335 If the value is neither t nor nil, also log incoming data."
336 :type '(choice (const :tag "Disable" nil)
337 (const :tag "Outgoing" t)
338 (const :tag "Incoming and outgoing" full)))
339
340 (defcustom ampc-use-full-frame nil
341 "If non-nil, ampc will use the entire Emacs screen."
342 :type 'boolean)
343
344 (defcustom ampc-truncate-lines t
345 "If non-nil, truncate lines in ampc buffers."
346 :type 'boolean)
347
348 (defcustom ampc-default-server '("localhost" . 6600)
349 "The MPD server to connect to if the arguments to `ampc' are nil.
350 This variable is a cons cell, with the car specifying the
351 hostname and the cdr specifying the port. Both values can be
352 nil, which will make ampc query the user for values on each
353 invocation."
354 :type '(cons (choice :tag "Hostname"
355 (string)
356 (const :tag "Ask" nil))
357 (choice :tag "Port"
358 (string)
359 (integer)
360 (const :tag "Ask" nil))))
361
362 (defcustom ampc-synchronous-commands '(t status currentsong play)
363 "List of MPD commands that should be executed synchronously.
364 Executing commands that print lots of output synchronously will
365 result in massive performance improvements of ampc. If the car
366 of this list is `t', execute all commands synchronously other
367 than the ones specified by the rest of the list."
368 :type '(repeat symbol))
369
370 (defcustom ampc-status-tags nil
371 "List of additional tags of the current song that are added to
372 the internal status of ampc and thus are passed to the functions
373 in `ampc-status-changed-hook'. Each element may be a string that
374 specifies a tag that is returned by MPD's `currentsong'
375 command."
376 :type '(list symbol))
377
378 (defcustom ampc-volume-step 5
379 "Default step of `ampc-increase-volume' and
380 `ampc-decrease-volume' for changing the volume."
381 :type 'integer)
382
383 (defcustom ampc-crossfade-step 5
384 "Default step of `ampc-increase-crossfade' and
385 `ampc-decrease-crossfade' for changing the crossfade."
386 :type 'integer)
387
388 (defcustom ampc-tag-transform-funcs '(("Time" . ampc-transform-time)
389 ("Track" . ampc-transform-track))
390 "Alist of tag treatment functions.
391 The car, a string, of each entry specifies the MPD tag, the cdr a
392 function which transforms the tag to the value that should be
393 used by ampc. The function is called with one string argument,
394 the tag value, and should return the treated value."
395 :type '(alist :key-type string :value-type function))
396
397 (defcustom ampc-tagger-music-directories nil
398 "List of base directories in which your music files are located.
399 Usually this list should have only one entry, the value of your
400 mpd.conf's `music_directory'"
401 :type '(list directory))
402
403 (defcustom ampc-tagger-executable "ampc_tagger"
404 "The name or full path to ampc's tagger executable."
405 :type 'string)
406
407 (defcustom ampc-tagger-backup-directory
408 (file-name-directory (locate-user-emacs-file "ampc-backups/"))
409 "The directory in which the tagger copies files before modifying.
410 If nil, disable backups."
411 :type '(choice (const :tag "Disable backups" nil)
412 (directory :tag "Directory")))
413
414 ;;; **** hooks
415 (defcustom ampc-before-startup-hook nil
416 "A hook run before startup.
417 This hook is called as the first thing when ampc is started."
418 :type 'hook)
419
420 (defcustom ampc-connected-hook nil
421 "A hook run after ampc connected to MPD."
422 :type 'hook)
423
424 (defcustom ampc-suspend-hook nil
425 "A hook run when suspending ampc."
426 :type 'hook)
427
428 (defcustom ampc-quit-hook nil
429 "A hook run when exiting ampc."
430 :type 'hook)
431
432 (defcustom ampc-status-changed-hook nil
433 "A hook run whenever the status of the daemon (that is volatile
434 properties such as volume or current song) changes. The hook is
435 run with one arg, an alist that contains the new status. The car
436 of each entry is a symbol, the cdr is a string. Valid keys are:
437
438 volume
439 repeat
440 random
441 consume
442 xfade
443 state
444 song
445 Artist
446 Title
447
448 and the keys in `ampc-status-tags'. Not all keys may be present
449 all the time!"
450 :type 'hook)
451
452 (defcustom ampc-tagger-grab-hook nil
453 "Hook run by the tagger before grabbing tags of a file.
454 Each function is called with one argument, the file name."
455 :type 'hook)
456 (defcustom ampc-tagger-grabbed-hook nil
457 "Hook run by the tagger after grabbing tags of a file.
458 Each function is called with one argument, the file name."
459 :type 'hook)
460
461 (defcustom ampc-tagger-store-hook nil
462 "Hook run by the tagger before writing tags back to a file.
463 Each function is called with two arguments, FOUND-CHANGED and
464 DATA. FOUND-CHANGED is non-nil if the tags that are about to be
465 written differ from the ones in the file. DATA is a cons. The
466 car specifies the full file name of the file that is about to be
467 written to, the cdr is an alist that specifies the tags that are
468 about to be (over-)written. The car of each entry in this list
469 is a symbol specifying the tag (one of the ones in
470 `ampc-tagger-tags'), the cdr a string specifying the value. The
471 cdr of DATA may be modified. If FOUND-CHANGED is nil and the cdr
472 of DATA is not modified throughout the hook is run, the file is
473 not touched. `ampc-tagger-stored-hook' is still run, though."
474 :type 'hook)
475 (defcustom ampc-tagger-stored-hook nil
476 "Hook run by the tagger after writing tags back to a file.
477 Each function is called with two arguments, FOUND-CHANGED and
478 DATA. These are the same arguments that were already passed to
479 `ampc-tagger-store-hook'. The car of DATA, the file name, may be
480 modified."
481 :type 'hook)
482
483 ;;; *** faces
484 (defface ampc-mark-face '((t (:inherit font-lock-constant-face)))
485 "Face of the mark.")
486 (defface ampc-marked-face '((t (:inherit warning)))
487 "Face of marked entries.")
488 (defface ampc-unmarked-face '((t (:inerhit default)))
489 "Face of unmarked entries.")
490 (defface ampc-current-song-mark-face '((t (:inherit region)))
491 "Face of mark of the current song.")
492 (defface ampc-current-song-marked-face '((t (:inherit region)))
493 "Face of the current song if marked.")
494
495 (defface ampc-tagger-tag-face '((t (:inherit font-lock-constant-face)))
496 "Face of tags within the tagger.")
497 (defface ampc-tagger-keyword-face '((t (:inherit font-lock-keyword-face)))
498 "Face of tags within the tagger.")
499
500 ;;; *** internal variables
501 (defvar ampc-views
502 (let* ((songs '(1.0 song :properties (("Track" :title "#" :width 4)
503 ("Title" :min 15 :max 40)
504 ("Time" :width 6)
505 ("Artist" :min 15 :max 40)
506 ("Album" :min 15 :max 40))))
507 (rs_a `(1.0 vertical
508 (0.7 horizontal
509 (0.33 tag :tag "Genre" :id 1 :select t)
510 (0.33 tag :tag "Artist" :id 2)
511 (1.0 tag :tag "Album" :id 3))
512 ,songs))
513 (rs_b `(1.0 vertical
514 (0.7 horizontal
515 (0.33 tag :tag "Genre" :id 1 :select t)
516 (0.33 tag :tag "Album" :id 2)
517 (1.0 tag :tag "Artist" :id 3))
518 ,songs))
519 (pl-prop '(:properties (("Title" :min 15 :max 40)
520 ("Artist" :min 15 :max 40)
521 ("Album" :min 15 :max 40)
522 ("Time" :width 6)))))
523 `((tagger
524 horizontal
525 (0.65 files-list
526 :properties ((filename :shrink t :title "File" :min 20 :max 40)
527 ("Title" :min 15 :max 40)
528 ("Artist" :min 15 :max 40)
529 ("Album" :min 15 :max 40)
530 ("Genre" :min 15 :max 40)
531 ("Year" :width 5)
532 ("Track" :title "#" :width 4)
533 ("Comment" :min 15 :max 40))
534 :dedicated nil)
535 (1.0 tagger))
536 ("Current playlist view (Genre|Artist|Album)"
537 ,(kbd "J")
538 horizontal
539 (0.4 vertical
540 (6 status)
541 (1.0 current-playlist ,@pl-prop))
542 ,rs_a)
543 ("Current playlist view (Genre|Album|Artist)"
544 ,(kbd "M")
545 horizontal
546 (0.4 vertical
547 (6 status)
548 (1.0 current-playlist ,@pl-prop))
549 ,rs_b)
550 ("Playlist view (Genre|Artist|Album)"
551 ,(kbd "K")
552 horizontal
553 (0.4 vertical
554 (6 status)
555 (1.0 vertical
556 (0.4 current-playlist ,@pl-prop)
557 (0.4 playlist ,@pl-prop)
558 (1.0 playlists)))
559 ,rs_a)
560 ("Playlist view (Genre|Album|Artist)"
561 ,(kbd "<")
562 horizontal
563 (0.4 vertical
564 (6 status)
565 (1.0 vertical
566 (0.4 current-playlist ,@pl-prop)
567 (0.4 playlist ,@pl-prop)
568 (1.0 playlists)))
569 ,rs_b)
570 ("Outputs view"
571 ,(kbd "L")
572 outputs :properties (("outputname" :title "Name" :min 10 :max 30)
573 ("outputenabled" :title "Enabled" :width 9))))))
574
575 (defvar ampc-connection nil)
576 (defvar ampc-host nil)
577 (defvar ampc-port nil)
578 (defvar ampc-outstanding-commands nil)
579
580 (defvar ampc-no-implicit-next-dispatch nil)
581 (defvar ampc-working-timer nil)
582 (defvar ampc-yield nil)
583 (defvar ampc-yield-redisplay nil)
584
585 (defvar ampc-windows nil)
586 (defvar ampc-all-buffers nil)
587
588 (defvar ampc-type nil)
589 (make-variable-buffer-local 'ampc-type)
590 (defvar ampc-dirty nil)
591 (make-variable-buffer-local 'ampc-dirty)
592
593 (defvar ampc-internal-db nil)
594 (defvar ampc-status nil)
595
596 (defvar ampc-tagger-previous-configuration nil)
597 (defvar ampc-tagger-version-verified nil)
598 (defvar ampc-tagger-completion-all-files nil)
599 (defvar ampc-tagger-genres nil)
600
601 (defconst ampc-tagger-version "0.1")
602 (defconst ampc-tagger-tags '(Title Artist Album Comment Genre Year Track))
603
604 ;;; *** mode maps
605 (defvar ampc-mode-map
606 (let ((map (make-sparse-keymap)))
607 (suppress-keymap map)
608 (define-key map (kbd "k") 'ampc-toggle-play)
609 (define-key map (kbd "l") 'ampc-next)
610 (define-key map (kbd "j") 'ampc-previous)
611 (define-key map (kbd "c") 'ampc-clear)
612 (define-key map (kbd "s") 'ampc-shuffle)
613 (define-key map (kbd "S") 'ampc-store)
614 (define-key map (kbd "O") 'ampc-load)
615 (define-key map (kbd "R") 'ampc-rename-playlist)
616 (define-key map (kbd "D") 'ampc-delete-playlist)
617 (define-key map (kbd "y") 'ampc-increase-volume)
618 (define-key map (kbd "M-y") 'ampc-decrease-volume)
619 (define-key map (kbd "C-M-y") 'ampc-set-volume)
620 (define-key map (kbd "h") 'ampc-increase-crossfade)
621 (define-key map (kbd "M-h") 'ampc-decrease-crossfade)
622 (define-key map (kbd "C-M-h") 'ampc-set-crossfade)
623 (define-key map (kbd "e") 'ampc-toggle-repeat)
624 (define-key map (kbd "r") 'ampc-toggle-random)
625 (define-key map (kbd "f") 'ampc-toggle-consume)
626 (define-key map (kbd "P") 'ampc-goto-current-song)
627 (define-key map (kbd "G") 'ampc-mini)
628 (define-key map (kbd "q") 'ampc-quit)
629 (define-key map (kbd "z") 'ampc-suspend)
630 (define-key map (kbd "T") 'ampc-trigger-update)
631 (define-key map (kbd "I") 'ampc-tagger)
632 (loop for view in ampc-views
633 do (when (stringp (car view))
634 (define-key map (cadr view)
635 `(lambda ()
636 (interactive)
637 (ampc-change-view ',view)))))
638 map))
639
640 (defvar ampc-item-mode-map
641 (let ((map (make-sparse-keymap)))
642 (suppress-keymap map)
643 (define-key map (kbd "m") 'ampc-mark)
644 (define-key map (kbd "u") 'ampc-unmark)
645 (define-key map (kbd "U") 'ampc-unmark-all)
646 (define-key map (kbd "n") 'ampc-next-line)
647 (define-key map (kbd "p") 'ampc-previous-line)
648 (define-key map (kbd "<down-mouse-1>") 'ampc-mouse-toggle-mark)
649 (define-key map (kbd "<mouse-1>") 'ampc-mouse-align-point)
650 (define-key map [remap next-line] 'ampc-next-line)
651 (define-key map [remap previous-line] 'ampc-previous-line)
652 (define-key map [remap tab-to-tab-stop] 'ampc-move-to-tab)
653 map))
654
655 (defvar ampc-current-playlist-mode-map
656 (let ((map (make-sparse-keymap)))
657 (suppress-keymap map)
658 (define-key map (kbd "<return>") 'ampc-play-this)
659 (define-key map (kbd "<down-mouse-2>") 'ampc-mouse-play-this)
660 (define-key map (kbd "<mouse-2>") 'ampc-mouse-align-point)
661 (define-key map (kbd "<down-mouse-3>") 'ampc-mouse-delete)
662 (define-key map (kbd "<mouse-3>") 'ampc-mouse-align-point)
663 map))
664
665 (defvar ampc-playlist-mode-map
666 (let ((map (make-sparse-keymap)))
667 (suppress-keymap map)
668 (define-key map (kbd "t") 'ampc-toggle-marks)
669 (define-key map (kbd "d") 'ampc-delete)
670 (define-key map (kbd "<up>") 'ampc-up)
671 (define-key map (kbd "<down>") 'ampc-down)
672 (define-key map (kbd "<down-mouse-3>") 'ampc-mouse-delete)
673 (define-key map (kbd "<mouse-3>") 'ampc-mouse-align-point)
674 map))
675
676 (defvar ampc-playlists-mode-map
677 (let ((map (make-sparse-keymap)))
678 (suppress-keymap map)
679 (define-key map (kbd "l") 'ampc-load)
680 (define-key map (kbd "r") 'ampc-rename-playlist)
681 (define-key map (kbd "d") 'ampc-delete-playlist)
682 (define-key map (kbd "<down-mouse-2>") 'ampc-mouse-load)
683 (define-key map (kbd "<mouse-2>") 'ampc-mouse-align-point)
684 (define-key map (kbd "<down-mouse-3>") 'ampc-mouse-delete-playlist)
685 (define-key map (kbd "<mouse-3>") 'ampc-mouse-align-point)
686 map))
687
688 (defvar ampc-tag-song-mode-map
689 (let ((map (make-sparse-keymap)))
690 (suppress-keymap map)
691 (define-key map (kbd "t") 'ampc-toggle-marks)
692 (define-key map (kbd "a") 'ampc-add)
693 (define-key map (kbd "<down-mouse-3>") 'ampc-mouse-add)
694 (define-key map (kbd "<mouse-3>") 'ampc-mouse-align-point)
695 map))
696
697 (defvar ampc-outputs-mode-map
698 (let ((map (make-sparse-keymap)))
699 (suppress-keymap map)
700 (define-key map (kbd "t") 'ampc-toggle-marks)
701 (define-key map (kbd "a") 'ampc-toggle-output-enabled)
702 (define-key map (kbd "<down-mouse-3>") 'ampc-mouse-toggle-output-enabled)
703 (define-key map (kbd "<mouse-3>") 'ampc-mouse-align-point)
704 map))
705
706 (defvar ampc-files-list-mode-map
707 (let ((map (make-sparse-keymap)))
708 (suppress-keymap map)
709 (define-key map (kbd "t") 'ampc-toggle-marks)
710 (define-key map (kbd "C-c C-q") 'ampc-tagger-quit)
711 (define-key map (kbd "C-c C-c") 'ampc-tagger-save)
712 (define-key map (kbd "C-c C-r") 'ampc-tagger-reset)
713 (define-key map [remap ampc-tagger] nil)
714 (define-key map [remap ampc-quit] 'ampc-tagger-quit)
715 (loop for view in ampc-views
716 do (when (stringp (car view))
717 (define-key map (cadr view) nil)))
718 map))
719
720 (defvar ampc-tagger-mode-map
721 (let ((map (make-sparse-keymap)))
722 (define-key map (kbd "C-c C-q") 'ampc-tagger-quit)
723 (define-key map (kbd "C-c C-c") 'ampc-tagger-save)
724 (define-key map (kbd "C-c C-r") 'ampc-tagger-reset)
725 (define-key map (kbd "<tab>") 'ampc-tagger-completion-at-point)
726 map))
727
728 (defvar ampc-tagger-dired-mode-map
729 (let ((map (make-sparse-keymap)))
730 (define-key map (kbd "C-c C-t") 'ampc-tagger-dired)
731 map))
732
733 ;;; **** menu
734 (easy-menu-define nil ampc-mode-map nil
735 `("ampc"
736 ("Change view" ,@(loop for view in ampc-views
737 when (stringp (car view))
738 collect (vector (car view)
739 `(lambda ()
740 (interactive)
741 (ampc-change-view ',view)))
742 end))
743 ["Run tagger" ampc-tagger]
744 "--"
745 ["Play" ampc-toggle-play
746 :visible (and ampc-status
747 (not (equal (cdr (assq 'state ampc-status)) "play")))]
748 ["Pause" ampc-toggle-play
749 :visible (and ampc-status
750 (equal (cdr (assq 'state ampc-status)) "play"))]
751 ["Stop" (lambda () (interactive) (ampc-toggle-play 4))
752 :visible (and ampc-status
753 (equal (cdr (assq 'state ampc-status)) "play"))]
754 ["Next" ampc-next]
755 ["Previous" ampc-previous]
756 "--"
757 ["Clear playlist" ampc-clear]
758 ["Shuffle playlist" ampc-shuffle]
759 ["Store playlist" ampc-store]
760 ["Queue Playlist" ampc-load :visible (ampc-playlist)]
761 ["Rename Playlist" ampc-rename-playlist :visible (ampc-playlist)]
762 ["Delete Playlist" ampc-delete-playlist :visible (ampc-playlist)]
763 "--"
764 ["Increase volume" ampc-increase-volume]
765 ["Decrease volume" ampc-decrease-volume]
766 ["Set volume" ampc-set-volume]
767 ["Increase crossfade" ampc-increase-crossfade]
768 ["Decrease crossfade" ampc-decrease-crossfade]
769 ["Set crossfade" ampc-set-crossfade]
770 ["Toggle repeat" ampc-toggle-repeat
771 :style toggle
772 :selected (equal (cdr (assq 'repeat ampc-status)) "1")]
773 ["Toggle random" ampc-toggle-random
774 :style toggle
775 :selected (equal (cdr (assq 'random ampc-status)) "1")]
776 ["Toggle consume" ampc-toggle-consume
777 :style toggle
778 :selected (equal (cdr (assq 'consume ampc-status)) "1")]
779 "--"
780 ["Trigger update" ampc-trigger-update]
781 ["Suspend" ampc-suspend]
782 ["Quit" ampc-quit]))
783
784 (easy-menu-define ampc-selection-menu ampc-item-mode-map
785 "Selection menu for ampc"
786 '("ampc Mark"
787 ["Add to playlist" ampc-add
788 :visible (not (eq (car ampc-type) 'outputs))]
789 ["Toggle enabled" ampc-toggle-output-enabled
790 :visible (eq (car ampc-type) 'outputs)]
791 "--"
792 ["Next line" ampc-next-line]
793 ["Previous line" ampc-previous-line]
794 ["Mark" ampc-mark]
795 ["Unmark" ampc-unmark]
796 ["Unmark all" ampc-unmark-all]
797 ["Toggle marks" ampc-toggle-marks
798 :visible (not (eq (car ampc-type) 'playlists))]))
799
800 (defvar ampc-tool-bar-map
801 (let ((map (make-sparse-keymap)))
802 (tool-bar-local-item
803 "mpc/prev" 'ampc-previous 'previous map
804 :help "Previous")
805 (tool-bar-local-item
806 "mpc/play" 'ampc-toggle-play 'play map
807 :help "Play"
808 :visible '(and ampc-status
809 (not (equal (cdr (assq 'state ampc-status)) "play"))))
810 (tool-bar-local-item
811 "mpc/pause" 'ampc-toggle-play 'pause map
812 :help "Pause"
813 :visible '(and ampc-status
814 (equal (cdr (assq 'state ampc-status)) "play")))
815 (tool-bar-local-item
816 "mpc/stop" (lambda () (interactive) (ampc-toggle-play 4)) 'stop map
817 :help "Stop"
818 :visible '(and ampc-status
819 (equal (cdr (assq 'state ampc-status)) "play")))
820 (tool-bar-local-item
821 "mpc/next" 'ampc-next 'next map
822 :help "Next")
823 map))
824
825 ;;; ** code
826 ;;; *** macros
827 (defmacro ampc-with-buffer (type &rest body)
828 (declare (indent 1) (debug t))
829 `(let* ((type- ,type)
830 (w (if (windowp type-)
831 type-
832 (loop for w in (ampc-normalize-windows)
833 thereis (when (with-current-buffer
834 (window-buffer w)
835 (etypecase type-
836 (symbol (eq (car ampc-type) type-))
837 (cons (equal ampc-type type-))))
838 w)))))
839 (when w
840 (with-selected-window w
841 (with-current-buffer (window-buffer w)
842 (let ((inhibit-read-only t))
843 ,@(if (eq (car body) 'no-se)
844 (cdr body)
845 `((save-excursion
846 (goto-char (point-min))
847 ,@body)))))))))
848
849 (defmacro ampc-fill-skeleton (tag &rest body)
850 (declare (indent 1) (debug t))
851 `(let ((tag- ,tag)
852 (data-buffer (current-buffer)))
853 (ampc-with-buffer tag-
854 no-se
855 (let ((old-point-data (get-text-property (point) 'cmp-data))
856 (old-window-start-offset
857 (1- (count-lines (window-start) (point)))))
858 (put-text-property (point-min) (point-max) 'not-updated t)
859 (when (eq ampc-dirty 'erase)
860 (put-text-property (point-min) (point-max) 'data nil))
861 (goto-char (point-min))
862 ,@body
863 (goto-char (point-min))
864 (loop until (eobp)
865 do (if (get-text-property (point) 'not-updated)
866 (kill-line 1)
867 (add-text-properties (+ (point) 2)
868 (progn (forward-line nil)
869 (1- (point)))
870 '(mouse-face highlight))))
871 (remove-text-properties (point-min) (point-max) '(not-updated))
872 (goto-char (point-min))
873 (when old-point-data
874 (loop until (eobp)
875 do (when (equal (get-text-property (point) 'cmp-data)
876 old-point-data)
877 (set-window-start
878 nil
879 (save-excursion
880 (forward-line (- old-window-start-offset))
881 (point))
882 t)
883 (return))
884 (forward-line)
885 finally do (goto-char (point-min)))))
886 (let ((effective-height (- (window-height)
887 (if mode-line-format 1 0)
888 (if header-line-format 1 0))))
889 (when (< (- (1- (line-number-at-pos (point-max)))
890 (line-number-at-pos (window-start)))
891 effective-height)
892 (set-window-start nil
893 (save-excursion
894 (goto-char (point-max))
895 (forward-line (- (1+ effective-height)))
896 (point))
897 t)))
898 (ampc-align-point)
899 (ampc-set-dirty nil))))
900
901 (defmacro ampc-with-selection (arg &rest body)
902 (declare (indent 1) (debug t))
903 `(let ((arg- ,arg))
904 (if (or (and (not arg-)
905 (save-excursion
906 (goto-char (point-min))
907 (search-forward-regexp "^* " nil t)))
908 (and arg- (symbolp arg-)))
909 (loop initially do (goto-char (point-min))
910 finally do (ampc-align-point)
911 while (search-forward-regexp "^* " nil t)
912 for index from 0
913 do (save-excursion
914 ,@body))
915 (setf arg- (prefix-numeric-value arg-))
916 (ampc-align-point)
917 (loop until (eobp)
918 for index from 0 to (1- (abs arg-))
919 do (save-excursion
920 ,@body)
921 until (if (< arg- 0) (ampc-previous-line) (ampc-next-line))))))
922
923 (defmacro ampc-iterate-source (data-buffer delimiter bindings &rest body)
924 (declare (indent 3) (debug t))
925 (when (memq (intern delimiter) bindings)
926 (callf2 delq (intern delimiter) bindings)
927 (push (list (intern delimiter)
928 '(buffer-substring (point) (line-end-position)))
929 bindings))
930 `(,@(if data-buffer `(with-current-buffer ,data-buffer) '(progn))
931 (when (search-forward-regexp
932 ,(concat "^" (regexp-quote delimiter) ": ")
933 nil t)
934 (loop with next
935 do (save-restriction
936 (setf next (ampc-narrow-entry
937 ,(concat "^" (regexp-quote delimiter) ": ")))
938 (let ,(loop for binding in bindings
939 if (consp binding)
940 collect binding
941 else
942 collect `(,binding (ampc-extract
943 (ampc-extract-regexp
944 ,(symbol-name binding))))
945 end)
946 ,@body))
947 while next
948 do (goto-char next)))))
949
950 (defmacro ampc-iterate-source-output (delimiter bindings pad-data &rest body)
951 (declare (indent 2) (debug t))
952 `(let ((output-buffer (current-buffer))
953 (tags (loop for (tag . props) in
954 (plist-get (cdr ampc-type) :properties)
955 collect (cons tag (ampc-extract-regexp tag)))))
956 (ampc-iterate-source
957 data-buffer ,delimiter ,bindings
958 (let ((pad-data ,pad-data))
959 (with-current-buffer output-buffer
960 (ampc-insert (ampc-pad pad-data) ,@body))))))
961
962 (defmacro ampc-extract-regexp (tag)
963 (if (stringp tag)
964 (concat "^" (regexp-quote tag) ": \\(.*\\)$")
965 `(concat "^" (regexp-quote ,tag) ": \\(.*\\)$")))
966
967 (defmacro ampc-tagger-log (&rest what)
968 (declare (indent 0) (debug t))
969 `(with-current-buffer (get-buffer-create "*Tagger Log*")
970 (ampc-tagger-log-mode)
971 (save-excursion
972 (goto-char (point-max))
973 (let ((inhibit-read-only t)
974 (what (concat ,@what)))
975 (when ampc-debug
976 (message "ampc: %s" what))
977 (insert what)))))
978
979 ;;; *** modes
980 (define-derived-mode ampc-outputs-mode ampc-item-mode "ampc-o")
981
982 (define-derived-mode ampc-tag-song-mode ampc-item-mode "ampc-ts")
983
984 (define-derived-mode ampc-current-playlist-mode ampc-playlist-mode "ampc-cpl"
985 (ampc-highlight-current-song-mode))
986
987 (define-derived-mode ampc-playlist-mode ampc-item-mode "ampc-pl")
988
989 (define-derived-mode ampc-playlists-mode ampc-item-mode "ampc-pls")
990
991 (define-derived-mode ampc-files-list-mode ampc-item-mode "ampc-files-list")
992
993 (define-derived-mode ampc-tagger-mode nil "ampc-tagger"
994 (set (make-local-variable 'tool-bar-map) ampc-tool-bar-map)
995 (set (make-local-variable 'tab-stop-list)
996 (list (+ (loop for tag in ampc-tagger-tags
997 maximize (length (symbol-name tag)))
998 2)))
999 (set (make-local-variable 'completion-at-point-functions)
1000 '(ampc-tagger-complete-tag ampc-tagger-complete-value))
1001 (setf truncate-lines ampc-truncate-lines
1002 font-lock-defaults
1003 `(((,(concat "^\\([ \t]*\\(?:"
1004 (mapconcat 'symbol-name ampc-tagger-tags "\\|")
1005 "\\)[ \t]*:\\)"
1006 "\\(\\(?:[ \t]*"
1007 "\\(?:"
1008 (mapconcat 'identity ampc-tagger-genres "\\|") "\\|<keep>"
1009 "\\)"
1010 "[ \t]*$\\)?\\)")
1011 (1 'ampc-tagger-tag-face)
1012 (2 'ampc-tagger-keyword-face)))
1013 t)))
1014
1015 (define-derived-mode ampc-tagger-log-mode nil "ampc-tagger-log")
1016
1017 (define-derived-mode ampc-item-mode ampc-mode "ampc-item"
1018 (setf font-lock-defaults '((("^\\(\\*\\)\\(.*\\)$"
1019 (1 'ampc-mark-face)
1020 (2 'ampc-marked-face))
1021 ("" 0 'ampc-unmarked-face))
1022 t)))
1023
1024 (define-derived-mode ampc-mode special-mode "ampc"
1025 (buffer-disable-undo)
1026 (set (make-local-variable 'tool-bar-map) ampc-tool-bar-map)
1027 (setf truncate-lines ampc-truncate-lines
1028 mode-line-modified "--"))
1029
1030 (define-minor-mode ampc-highlight-current-song-mode ""
1031 nil
1032 nil
1033 nil
1034 (funcall (if ampc-highlight-current-song-mode
1035 'font-lock-add-keywords
1036 'font-lock-remove-keywords)
1037 nil
1038 '((ampc-find-current-song
1039 (1 'ampc-current-song-mark-face)
1040 (2 'ampc-current-song-marked-face)))))
1041
1042 ;;;###autoload
1043 (define-minor-mode ampc-tagger-dired-mode
1044 "Minor mode that adds a audio file meta data tagging key binding to dired."
1045 nil
1046 " ampc-tagger"
1047 nil
1048 (assert (derived-mode-p 'dired-mode)))
1049
1050 ;;; *** internal functions
1051 (defun ampc-tagger-report (args status)
1052 (unless (zerop status)
1053 (let ((message (format (concat "ampc_tagger (%s %s) returned with a "
1054 "non-zero exit status (%s)")
1055 ampc-tagger-executable
1056 (mapconcat 'identity args " ")
1057 status)))
1058 (ampc-tagger-log message "\n")
1059 (error message))))
1060
1061 (defun ampc-tagger-call (&rest args)
1062 (ampc-tagger-report
1063 args
1064 (apply 'call-process ampc-tagger-executable nil t nil args)))
1065
1066 (defun ampc-int-insert-cmp (p1 p2)
1067 (cond ((< p1 p2) 'insert)
1068 ((eq p1 p2) 'overwrite)
1069 (t (- p1 p2))))
1070
1071 (defun ampc-normalize-windows ()
1072 (setf ampc-windows
1073 (loop for (window . buffer) in ampc-windows
1074 collect (cons (if (and (window-live-p window)
1075 (eq (window-buffer window) buffer))
1076 window
1077 (get-buffer-window buffer))
1078 buffer)))
1079 (delq nil (mapcar 'car ampc-windows)))
1080
1081 (defun ampc-restore-window-configuration ()
1082 (let ((windows
1083 (sort (delq nil
1084 (mapcar (lambda (w)
1085 (when (eq (window-frame w)
1086 (selected-frame))
1087 w))
1088 (ampc-normalize-windows)))
1089 (lambda (w1 w2)
1090 (loop for w in (window-list nil nil (frame-first-window))
1091 do (when (eq w w1)
1092 (return t))
1093 (when (eq w w2)
1094 (return nil)))))))
1095 (when windows
1096 (setf (window-dedicated-p (car windows)) nil)
1097 (loop for w in (cdr windows)
1098 do (delete-window w)))))
1099
1100 (defun ampc-tagger-tags-modified (tags new-tags)
1101 (loop with found-changed
1102 for (tag . value) in new-tags
1103 for prop = (assq tag tags)
1104 do (unless (equal (cdr prop) value)
1105 (setf (cdr prop) value
1106 found-changed t))
1107 finally return found-changed))
1108
1109 (defun ampc-change-view (view)
1110 (if (equal ampc-outstanding-commands '((idle nil)))
1111 (ampc-configure-frame (cddr view))
1112 (message "ampc is busy, cannot change window layout")))
1113
1114 (defun ampc-quote (string)
1115 (concat "\"" (replace-regexp-in-string "\"" "\\\"" string) "\""))
1116
1117 (defun ampc-in-ampc-p (&optional or-in-tagger)
1118 (or (when (ampc-on-p)
1119 ampc-type)
1120 (when or-in-tagger
1121 (memq (car ampc-type) '(files-list tagger)))))
1122
1123 (defun ampc-add-impl (&optional data)
1124 (ampc-on-files (lambda (file)
1125 (if (ampc-playlist)
1126 (ampc-send-command 'playlistadd
1127 '(:keep-prev t)
1128 (ampc-quote (ampc-playlist))
1129 file)
1130 (ampc-send-command 'add '(:keep-prev t) (ampc-quote file)))
1131 data)))
1132
1133 (defun ampc-on-files (func &optional data)
1134 (cond ((null data)
1135 (loop for d in (get-text-property (line-end-position) 'data)
1136 do (ampc-on-files func d)))
1137 ((avl-tree-p data)
1138 (avl-tree-mapc (lambda (e) (ampc-on-files func (cdr e))) data))
1139 ((stringp data)
1140 (funcall func data))
1141 (t
1142 (loop for d in (reverse data)
1143 do (ampc-on-files func (cdr (assoc "file" d)))))))
1144
1145 (defun ampc-skip (N)
1146 (ampc-send-command
1147 'play
1148 `(:callback ,(lambda ()
1149 (ampc-send-command 'status '(:front t))))
1150 (lambda ()
1151 (let ((song (cdr (assq 'song ampc-status)))
1152 (playlist-length (cdr (assq 'playlistlength ampc-status))))
1153 (unless (and song playlist-length)
1154 (throw 'skip nil))
1155 (max 0 (min (+ (string-to-number song) N)
1156 (1- (string-to-number playlist-length))))))))
1157
1158 (defun* ampc-find-current-song
1159 (limit &aux (point (point)) (song (cdr (assq 'song ampc-status))))
1160 (when (and song
1161 (<= (1- (line-number-at-pos (point)))
1162 (setf song (string-to-number song)))
1163 (>= (1- (line-number-at-pos limit)) song))
1164 (goto-char (point-min))
1165 (forward-line song)
1166 (save-restriction
1167 (narrow-to-region (max point (point)) (min limit (line-end-position)))
1168 (search-forward-regexp "\\(?1:\\(\\`\\*\\)?\\)\\(?2:.*\\)$"))))
1169
1170 (defun ampc-set-volume-impl (arg &optional func)
1171 (when arg
1172 (setf arg (prefix-numeric-value arg)))
1173 (ampc-send-command
1174 'setvol
1175 `(:callback ,(lambda ()
1176 (ampc-send-command 'status '(:front t))))
1177 (lambda ()
1178 (unless ampc-status
1179 (throw 'skip nil))
1180 (max (min (if func
1181 (funcall func
1182 (string-to-number
1183 (cdr (assq 'volume ampc-status)))
1184 (or arg ampc-volume-step))
1185 arg)
1186 100)
1187 0))))
1188
1189 (defun ampc-set-crossfade-impl (arg &optional func)
1190 (when arg
1191 (setf arg (prefix-numeric-value arg)))
1192 (ampc-send-command
1193 'crossfade
1194 `(:callback ,(lambda ()
1195 (ampc-send-command 'status '(:front t))))
1196 (lambda ()
1197 (unless ampc-status
1198 (throw 'skip nil))
1199 (max (if func
1200 (funcall func
1201 (string-to-number
1202 (cdr (assq 'xfade ampc-status)))
1203 (or arg ampc-crossfade-step))
1204 arg)
1205 0))))
1206
1207 (defun* ampc-tagger-make-backup (file)
1208 (unless ampc-tagger-backup-directory
1209 (return-from ampc-tagger-make-backup))
1210 (when (functionp ampc-tagger-backup-directory)
1211 (funcall ampc-tagger-backup-directory file)
1212 (return-from ampc-tagger-make-backup))
1213 (unless (file-directory-p ampc-tagger-backup-directory)
1214 (make-directory ampc-tagger-backup-directory t))
1215 (let* ((real-file
1216 (loop with real-file = file
1217 for target = (file-symlink-p real-file)
1218 while target
1219 do (setf real-file (expand-file-name
1220 target (file-name-directory real-file)))
1221 finally return real-file))
1222 (target
1223 (loop with base = (file-name-nondirectory real-file)
1224 for i from 1
1225 for file = (expand-file-name
1226 (concat base ".~"
1227 (int-to-string i)
1228 "~")
1229 ampc-tagger-backup-directory)
1230 while (file-exists-p file)
1231 finally return file)))
1232 (ampc-tagger-log "\tBackup file: " (abbreviate-file-name target) "\n")
1233 (copy-file real-file target nil t)))
1234
1235 (defun* ampc-move (N &aux with-marks entries-to-move (up (< N 0)))
1236 (save-excursion
1237 (goto-char (point-min))
1238 (loop while (search-forward-regexp "^* " nil t)
1239 do (push (point) entries-to-move)))
1240 (if entries-to-move
1241 (setf with-marks t)
1242 (push (point) entries-to-move))
1243 (when (save-excursion
1244 (loop with max = (1- (count-lines (point-min) (point-max)))
1245 for p in entries-to-move
1246 do (goto-char p)
1247 for line = (+ (1- (line-number-at-pos)) N)
1248 always (and (>= line 0) (<= line max))))
1249 (when up
1250 (setf entries-to-move (nreverse entries-to-move)))
1251 (when with-marks
1252 (ampc-unmark-all))
1253 (loop for p in entries-to-move
1254 do (goto-char p)
1255 for line = (1- (line-number-at-pos))
1256 do (if (and (not (eq (car ampc-type) 'current-playlist))
1257 (ampc-playlist))
1258 (ampc-send-command 'playlistmove
1259 '(:keep-prev t)
1260 (ampc-quote (ampc-playlist))
1261 line
1262 (+ line N))
1263 (ampc-send-command 'move '(:keep-prev t) line (+ line N))))
1264 (if with-marks
1265 (loop for p in (nreverse entries-to-move)
1266 do (goto-char p)
1267 (forward-line N)
1268 (save-excursion
1269 (ampc-mark-impl t 1))
1270 (ampc-align-point))
1271 (forward-line N)
1272 (ampc-align-point))))
1273
1274 (defun ampc-toggle-state (state arg)
1275 (when (or arg ampc-status)
1276 (ampc-send-command
1277 state
1278 nil
1279 (cond ((null arg)
1280 (if (equal (cdr (assq state ampc-status)) "1")
1281 0
1282 1))
1283 ((> (prefix-numeric-value arg) 0) 1)
1284 (t 0)))))
1285
1286 (defun ampc-playlist (&optional at-point)
1287 (ampc-with-buffer 'playlists
1288 (if (and (not at-point)
1289 (search-forward-regexp "^* \\(.*\\)$" nil t))
1290 (let ((result (match-string 1)))
1291 (set-text-properties 0 (length result) nil result)
1292 result)
1293 (unless (eobp)
1294 (buffer-substring-no-properties
1295 (+ (line-beginning-position) 2)
1296 (line-end-position))))))
1297
1298 (defun* ampc-mark-impl (select N &aux result (inhibit-read-only t))
1299 (when (eq (car ampc-type) 'playlists)
1300 (assert (or (not select) (null N) (eq N 1)))
1301 (ampc-with-buffer 'playlists
1302 (loop while (search-forward-regexp "^\\* " nil t)
1303 do (replace-match " " nil nil))))
1304 (loop repeat (or N 1)
1305 until (eobp)
1306 do (move-beginning-of-line nil)
1307 (delete-char 1)
1308 (insert (if select "*" " "))
1309 (setf result (ampc-next-line nil)))
1310 (ampc-post-mark-change-update)
1311 result)
1312
1313 (defun ampc-post-mark-change-update ()
1314 (ecase (car ampc-type)
1315 ((current-playlist playlist outputs))
1316 (playlists
1317 (ampc-update-playlist))
1318 ((song tag)
1319 (loop
1320 for w in
1321 (loop for w on (ampc-normalize-windows)
1322 thereis (when (or (eq (car w) (selected-window))
1323 (and (eq (car ampc-type) 'tag)
1324 (eq (with-current-buffer
1325 (window-buffer (car w))
1326 (car ampc-type))
1327 'song)))
1328 (cdr w)))
1329 do (with-current-buffer (window-buffer w)
1330 (when (memq (car ampc-type) '(song tag))
1331 (ampc-set-dirty t))))
1332 (ampc-fill-tag-song))
1333 (files-list
1334 (ampc-tagger-update))))
1335
1336 (defun* ampc-tagger-get-values (tag all-files &aux result)
1337 (ampc-with-buffer 'files-list
1338 no-se
1339 (save-excursion
1340 (macrolet
1341 ((add-file
1342 ()
1343 `(let ((value (cdr (assq tag (get-text-property (point) 'data)))))
1344 (unless (member value result)
1345 (push value result)))))
1346 (if all-files
1347 (loop until (eobp)
1348 initially do (goto-char (point-min))
1349 (ampc-align-point)
1350 do (add-file)
1351 until (ampc-next-line))
1352 (ampc-with-selection nil
1353 (add-file))))))
1354 result)
1355
1356 (defun ampc-tagger-update ()
1357 (ampc-with-buffer 'tagger
1358 (loop
1359 while (search-forward-regexp (concat "^[ \t]*\\("
1360 (mapconcat 'symbol-name
1361 ampc-tagger-tags
1362 "\\|")
1363 "\\)[ \t]*:"
1364 "[ \t]*\\(<keep>[ \t]*?\\)"
1365 "\\(?:\n\\)?$")
1366 nil
1367 t)
1368 for tag = (intern (match-string 1))
1369 do (when (memq tag ampc-tagger-tags)
1370 (let ((values (save-match-data (ampc-tagger-get-values tag nil))))
1371 (when (eq (length values) 1)
1372 (replace-match (car values) nil t nil 2)))))))
1373
1374 (defun ampc-tagger-complete-tag ()
1375 (save-excursion
1376 (save-restriction
1377 (narrow-to-region (line-beginning-position) (line-end-position))
1378 (unless (search-backward-regexp "^.*:" nil t)
1379 (when (search-backward-regexp "\\(^\\|[ \t]\\).*" nil t)
1380 (when (looking-at "[ \t]")
1381 (forward-char 1))
1382 (list (point)
1383 (search-forward-regexp ":\\|$")
1384 (mapcar (lambda (tag) (concat (symbol-name tag) ":"))
1385 ampc-tagger-tags)))))))
1386
1387 (defun* ampc-tagger-complete-value (&aux tag)
1388 (save-excursion
1389 (save-restriction
1390 (narrow-to-region (line-beginning-position) (line-end-position))
1391 (save-excursion
1392 (unless (search-backward-regexp (concat "^[ \t]*\\("
1393 (mapconcat 'symbol-name
1394 ampc-tagger-tags
1395 "\\|")
1396 "\\)[ \t]*:")
1397 nil t)
1398 (return-from ampc-tagger-complete-tag))
1399 (setf tag (intern (match-string 1))))
1400 (save-excursion
1401 (search-backward-regexp "[: \t]")
1402 (forward-char 1)
1403 (list (point)
1404 (search-forward-regexp "[ \t]\\|$")
1405 (let ((values (cons "<keep>" (ampc-tagger-get-values
1406 tag
1407 ampc-tagger-completion-all-files))))
1408 (when (eq tag 'Genre)
1409 (loop for g in ampc-tagger-genres
1410 do (unless (member g values)
1411 (push g values))))
1412 values))))))
1413
1414 (defun ampc-align-point ()
1415 (unless (eobp)
1416 (move-beginning-of-line nil)
1417 (forward-char 2)
1418 (re-search-forward " *" nil t)))
1419
1420 (defun* ampc-pad (tabs &optional dont-honour-item-mode)
1421 (loop with new-tab-stop-list
1422 with offset-dec = (if (and (not dont-honour-item-mode)
1423 (derived-mode-p 'ampc-item-mode))
1424 2
1425 0)
1426 for tab in tabs
1427 for offset-cell on (if (derived-mode-p 'ampc-item-mode)
1428 tab-stop-list
1429 (cons 0 tab-stop-list))
1430 for offset = (car offset-cell)
1431 for props in (or (plist-get (cdr ampc-type) :properties)
1432 '(nil . nil))
1433 by (lambda (cell) (or (cdr cell) '(nil . nil)))
1434 do (decf offset offset-dec)
1435 with first = t
1436 with current-offset = 0
1437 when (<= current-offset offset)
1438 do (when (and (not first) (eq (- offset current-offset) 0))
1439 (incf offset))
1440 and concat (make-string (- offset current-offset) ? ) into result
1441 and do (setf current-offset offset)
1442 else
1443 concat " " into result
1444 and do (incf current-offset)
1445 end
1446 do (unless tab
1447 (setf tab ""))
1448 (when (and (plist-get (cdr props) :shrink)
1449 (cadr offset-cell)
1450 (>= (+ current-offset (length tab) 1) (- (cadr offset-cell)
1451 offset-dec)))
1452 (setf tab (concat (substring tab 0 (max (- (cadr offset-cell)
1453 offset-dec
1454 current-offset
1455 4)
1456 3))
1457 "...")))
1458 concat tab into result
1459 do (push (+ current-offset offset-dec) new-tab-stop-list)
1460 (incf current-offset (length tab))
1461 (setf first nil)
1462 finally return
1463 (if (equal (callf nreverse new-tab-stop-list) tab-stop-list)
1464 result
1465 (propertize result 'tab-stop-list new-tab-stop-list))))
1466
1467 (defun ampc-update-header ()
1468 (when (or (memq (car ampc-type) '(tag playlists))
1469 (plist-get (cdr ampc-type) :properties))
1470 (setf header-line-format
1471 (concat
1472 (make-string (floor (fringe-columns 'left t)) ? )
1473 (ecase (car ampc-type)
1474 (tag
1475 (concat " " (plist-get (cdr ampc-type) :tag)))
1476 (playlists
1477 " Playlists")
1478 (t
1479 (ampc-pad (loop for (name . props) in
1480 (plist-get (cdr ampc-type) :properties)
1481 collect (or (plist-get props :title) name))
1482 t)))))))
1483
1484 (defun ampc-set-dirty (tag-or-dirty &optional dirty)
1485 (if (or (null tag-or-dirty) (memq tag-or-dirty '(t erase)))
1486 (setf ampc-dirty tag-or-dirty)
1487 (loop for w in (ampc-normalize-windows)
1488 do (with-current-buffer (window-buffer w)
1489 (when (eq (car ampc-type) tag-or-dirty)
1490 (ampc-set-dirty dirty))))))
1491
1492 (defun ampc-update ()
1493 (if ampc-status
1494 (loop for w in (ampc-normalize-windows)
1495 do (with-current-buffer (window-buffer w)
1496 (when (and ampc-dirty (not (eq ampc-dirty 'keep-dirty)))
1497 (ecase (car ampc-type)
1498 (outputs
1499 (ampc-send-command 'outputs))
1500 (playlist
1501 (ampc-update-playlist))
1502 ((tag song)
1503 (if (assoc (ampc-tags) ampc-internal-db)
1504 (ampc-fill-tag-song)
1505 (push (cons (ampc-tags) nil) ampc-internal-db)
1506 (ampc-send-command 'listallinfo)))
1507 (status
1508 (ampc-send-command 'status)
1509 (ampc-send-command 'currentsong))
1510 (playlists
1511 (ampc-send-command 'listplaylists))
1512 (current-playlist
1513 (ampc-send-command 'playlistinfo))))))
1514 (ampc-send-command 'status)
1515 (ampc-send-command 'currentsong)))
1516
1517 (defun ampc-update-playlist ()
1518 (ampc-with-buffer 'playlists
1519 (if (search-forward-regexp "^\\* " nil t)
1520 (ampc-send-command 'listplaylistinfo
1521 nil
1522 (get-text-property (point) 'data))
1523 (ampc-with-buffer 'playlist
1524 (erase-buffer)
1525 (ampc-set-dirty nil)))))
1526
1527 (defun ampc-send-command-impl (command)
1528 (when ampc-debug
1529 (message "ampc: -> %s" command))
1530 (process-send-string ampc-connection (concat command "\n")))
1531
1532 (defun* ampc-send-command (command &optional props &rest args)
1533 (destructuring-bind (&key (front nil) (callback nil) (keep-prev nil)
1534 (full-remove nil) (remove-other nil)
1535 &aux idle)
1536 props
1537 (when (and (not keep-prev)
1538 (eq (caar ampc-outstanding-commands) command)
1539 (equal (cddar ampc-outstanding-commands) args))
1540 (return-from ampc-send-command))
1541 (unless ampc-working-timer
1542 (setf ampc-yield 0
1543 ampc-working-timer (run-at-time nil 0.1 'ampc-yield)))
1544 (when (equal (caar ampc-outstanding-commands) 'idle)
1545 (pop ampc-outstanding-commands)
1546 (setf idle t))
1547 (when (and (not keep-prev) (cdr ampc-outstanding-commands))
1548 (setf (cdr ampc-outstanding-commands)
1549 (loop for other-cmd in (cdr ampc-outstanding-commands)
1550 unless (and (memq (car other-cmd) (list command remove-other))
1551 (or (not full-remove)
1552 (progn
1553 (assert (null remove-other))
1554 (equal (cddr other-cmd) args))))
1555 collect other-cmd
1556 end)))
1557 (setf command (apply 'list command (list :callback callback) args))
1558 (if front
1559 (push command ampc-outstanding-commands)
1560 (setf ampc-outstanding-commands
1561 (nconc ampc-outstanding-commands
1562 (list command))))
1563 (when idle
1564 (push '(noidle nil) ampc-outstanding-commands)
1565 (ampc-send-command-impl "noidle"))))
1566
1567 (defun ampc-send-next-command ()
1568 (loop while ampc-outstanding-commands
1569 for command =
1570 (loop for command = (car ampc-outstanding-commands)
1571 for command-id = (replace-regexp-in-string
1572 "^.*?-" ""
1573 (symbol-name (car command)))
1574 thereis
1575 (catch 'skip
1576 (ampc-send-command-impl
1577 (concat command-id
1578 (loop for a in (cddr command)
1579 concat " "
1580 do (when (functionp a)
1581 (callf funcall a))
1582 concat (etypecase a
1583 (integer (number-to-string a))
1584 (string a)))))
1585 (let ((callback (plist-get (cadar ampc-outstanding-commands)
1586 :callback))
1587 (old-head (pop ampc-outstanding-commands)))
1588 (when callback (funcall callback))
1589 (push old-head ampc-outstanding-commands))
1590 command-id)
1591 do (pop ampc-outstanding-commands)
1592 while ampc-outstanding-commands)
1593 while command
1594 while (let ((member (memq (intern command) ampc-synchronous-commands)))
1595 (if member
1596 (not (eq (car ampc-synchronous-commands) t))
1597 (eq (car ampc-synchronous-commands) t)))
1598 do (loop with head = ampc-outstanding-commands
1599 with ampc-no-implicit-next-dispatch = t
1600 with ampc-yield-redisplay = t
1601 while (eq head ampc-outstanding-commands)
1602 do (accept-process-output ampc-connection 0 100)))
1603 (unless ampc-outstanding-commands
1604 (when ampc-working-timer
1605 (cancel-timer ampc-working-timer)
1606 (setf ampc-yield nil
1607 ampc-working-timer nil)
1608 (ampc-fill-status))
1609 (setf ampc-outstanding-commands '((idle nil)))
1610 (ampc-send-command-impl "idle")))
1611
1612 (defun ampc-tree< (a b)
1613 (string< (car a) (car b)))
1614
1615 (defun ampc-create-tree ()
1616 (avl-tree-create 'ampc-tree<))
1617
1618 (defsubst ampc-extract (regexp)
1619 (goto-char (point-min))
1620 (when (search-forward-regexp regexp nil t)
1621 (match-string 1)))
1622
1623 (defsubst ampc-clean-tag (tag value)
1624 (if value
1625 (let ((func (cdr (assoc tag ampc-tag-transform-funcs))))
1626 (if func
1627 (funcall func value)
1628 value))
1629 (unless (equal tag "Track")
1630 "[Not Specified]")))
1631
1632 (defun ampc-insert (element data &optional cmp cmp-data)
1633 (goto-char (point-min))
1634 (unless cmp-data
1635 (setf cmp-data data))
1636 (let ((action
1637 (if (functionp cmp)
1638 (loop until (eobp)
1639 for tp = (get-text-property (+ (point) 2) 'cmp-data)
1640 thereis (let ((r (funcall cmp cmp-data tp)))
1641 (if (symbolp r)
1642 r
1643 (forward-line r)
1644 nil))
1645 finally return 'insert)
1646 (loop with stringp-cmp-data = (stringp cmp-data)
1647 with min = 1
1648 with max = (1+ (count-lines (point-min) (point-max)))
1649 with at-min = t
1650 do (when (< (- max min) 20)
1651 (unless at-min
1652 (forward-line (- min max)))
1653 (return (loop repeat (- max min)
1654 for tp = (get-text-property (+ (point) 2)
1655 'cmp-data)
1656 thereis
1657 (if (equal tp cmp-data)
1658 'update
1659 (unless (if stringp-cmp-data
1660 (string< tp cmp-data)
1661 (string<
1662 (buffer-substring-no-properties
1663 (+ (point) 2)
1664 (line-end-position))
1665 element))
1666 'insert))
1667 do (forward-line)
1668 finally return 'insert)))
1669 do (forward-line (funcall (if at-min '+ '-) (/ (- max min) 2)))
1670 for tp = (get-text-property (+ (point) 2) 'cmp-data)
1671 thereis (when (equal tp cmp-data) 'update)
1672 do (if (setf at-min (if stringp-cmp-data
1673 (string< tp cmp-data)
1674 (string< (buffer-substring-no-properties
1675 (+ (point) 2)
1676 (line-end-position))
1677 element)))
1678 (incf min (floor (/ (- max min) 2.0)))
1679 (decf max (floor (/ (- max min) 2.0))))
1680 finally return 'insert))))
1681 (ecase action
1682 (insert
1683 (insert (propertize (concat " " element "\n")
1684 'data (if (eq cmp t) (list data) data)
1685 'cmp-data cmp-data)))
1686 ((update overwrite)
1687 (remove-text-properties (point) (1+ (point)) '(not-updated))
1688 (when (or (eq ampc-dirty 'erase) (eq action 'overwrite))
1689 (let ((origin (point)))
1690 (forward-char 2)
1691 (kill-line 1)
1692 (insert element "\n")
1693 (goto-char origin)))
1694 (let ((next (1+ (line-end-position))))
1695 (put-text-property (point) next 'cmp-data cmp-data)
1696 (put-text-property
1697 (point) next
1698 'data (cond ((eq cmp t)
1699 (let ((rest (get-text-property (point) 'data)))
1700 (if (memq data rest)
1701 rest
1702 (cons data rest))))
1703 (t data))))
1704 (eq (char-after) ?*)))))
1705
1706 (defun ampc-fill-tag (trees)
1707 (put-text-property (point-min) (point-max) 'data nil)
1708 (loop with new-trees
1709 for tree in trees
1710 do (when tree
1711 (avl-tree-mapc
1712 (lambda (e)
1713 (when (ampc-insert (car e) (cdr e) t (car e))
1714 (push (cdr e) new-trees)))
1715 tree))
1716 finally return new-trees))
1717
1718 (defun ampc-fill-song (trees)
1719 (loop
1720 for songs in trees
1721 do (loop for song in songs
1722 do (ampc-insert
1723 (ampc-pad
1724 (loop for (p . v) in (plist-get (cdr ampc-type) :properties)
1725 collect (cdr (assoc p song))))
1726 `((,song))))))
1727
1728 (defsubst ampc-narrow-entry (delimiter-regexp)
1729 (let ((result))
1730 (narrow-to-region
1731 (line-beginning-position)
1732 (or (save-excursion
1733 (goto-char (line-end-position))
1734 (when (search-forward-regexp delimiter-regexp nil t)
1735 (setf result (point))
1736 (1- (line-beginning-position))))
1737 (point-max)))
1738 result))
1739
1740 (defun ampc-fill-playlist ()
1741 (ampc-fill-skeleton 'playlist
1742 (let ((index 0))
1743 (ampc-iterate-source-output "file" (file)
1744 (loop for (tag . tag-regexp) in tags
1745 collect (ampc-clean-tag tag (ampc-extract tag-regexp)))
1746 `(("file" . ,file)
1747 (index . ,(1- (incf index))))
1748 'ampc-int-insert-cmp
1749 index))))
1750
1751 (defun ampc-fill-outputs ()
1752 (ampc-fill-skeleton 'outputs
1753 (ampc-iterate-source-output "outputid" (outputid outputenabled)
1754 (loop for (tag . tag-regexp) in tags
1755 collect (ampc-clean-tag tag (ampc-extract tag-regexp)))
1756 `(("outputid" . ,outputid)
1757 ("outputenabled" . ,outputenabled)))))
1758
1759 (defun* ampc-mini-impl (&aux songs)
1760 (ampc-iterate-source
1761 nil
1762 "file"
1763 (Title
1764 Artist
1765 (Pos (string-to-number (ampc-extract (ampc-extract-regexp "Pos")))))
1766 (let ((entry (cons (concat Title
1767 (when Artist
1768 (concat " - " Artist)))
1769 Pos)))
1770 (loop with mentry = (cons (car entry) (cdr entry))
1771 for index from 2
1772 while (assoc (car mentry) songs)
1773 do (setf (car mentry) (concat (car entry)
1774 " (" (int-to-string index) ")"))
1775 finally do (push mentry songs))))
1776 (unless songs
1777 (message "No song in the playlist")
1778 (return-from ampc-mini-impl))
1779 (let ((song (assoc (let ((inhibit-quit t))
1780 (prog1
1781 (with-local-quit
1782 (completing-read "Song to play: " songs nil t))
1783 (setf quit-flag nil)))
1784 songs)))
1785 (when song
1786 (ampc-play-this (cdr song)))))
1787
1788 (defun ampc-fill-current-playlist ()
1789 (ampc-fill-skeleton 'current-playlist
1790 (ampc-iterate-source-output
1791 "file"
1792 (file (pos (string-to-number (ampc-extract
1793 (ampc-extract-regexp "Pos")))))
1794 (loop for (tag . tag-regexp) in tags
1795 collect (ampc-clean-tag tag (ampc-extract tag-regexp)))
1796 `(("file" . ,file)
1797 ("Pos" . ,pos))
1798 'ampc-int-insert-cmp
1799 pos)))
1800
1801 (defun ampc-fill-playlists ()
1802 (ampc-fill-skeleton 'playlists
1803 (with-current-buffer data-buffer
1804 (loop while (search-forward-regexp "^playlist: \\(.*\\)$" nil t)
1805 for playlist = (match-string 1)
1806 do (ampc-with-buffer 'playlists
1807 (ampc-insert playlist playlist)))))
1808 (ampc-set-dirty 'playlist t)
1809 (ampc-update))
1810
1811 (defun ampc-yield ()
1812 (incf ampc-yield)
1813 (ampc-fill-status)
1814 (when ampc-yield-redisplay
1815 (redisplay t)))
1816
1817 (defun ampc-fill-status ()
1818 (ampc-with-buffer 'status
1819 (erase-buffer)
1820 (funcall (or (plist-get (cadr ampc-type) :filler)
1821 (lambda (_)
1822 (insert (ampc-status t) "\n")))
1823 ampc-status)
1824 (ampc-set-dirty nil)))
1825
1826 (defun ampc-fill-tag-song ()
1827 (loop
1828 with trees = (list (cdr (assoc (ampc-tags) ampc-internal-db)))
1829 for type in '(tag song)
1830 do
1831 (loop
1832 for w in (ampc-normalize-windows)
1833 do
1834 (with-current-buffer (window-buffer w)
1835 (when (eq (car ampc-type) type)
1836 (if ampc-dirty
1837 (if (not trees)
1838 (progn
1839 (let ((inhibit-read-only t))
1840 (erase-buffer))
1841 (ampc-set-dirty nil))
1842 (ampc-fill-skeleton w
1843 (if (eq type 'tag)
1844 (setf trees (ampc-fill-tag trees))
1845 (ampc-fill-song trees))))
1846 (setf trees nil)
1847 (save-excursion
1848 (goto-char (point-min))
1849 (loop while (search-forward-regexp "^* " nil t)
1850 do (callf append trees
1851 (get-text-property (point) 'data))))))))))
1852
1853 (defun ampc-transform-track (track)
1854 (when (eq (length track) 1)
1855 (setf track (concat "0" track)))
1856 track)
1857
1858 (defun* ampc-transform-time (data &aux (time (string-to-number data)))
1859 (concat (number-to-string (/ time 60))
1860 ":"
1861 (when (< (% time 60) 10)
1862 "0")
1863 (number-to-string (% time 60))))
1864
1865 (defun ampc-handle-idle ()
1866 (loop until (eobp)
1867 for subsystem = (buffer-substring (point) (line-end-position))
1868 do (when (string-match "^changed: \\(.*\\)$" subsystem)
1869 (case (intern (match-string 1 subsystem))
1870 (database
1871 (setf ampc-internal-db (list (cons (ampc-tags) nil)))
1872 (ampc-set-dirty 'tag 'keep-dirty)
1873 (ampc-set-dirty 'song 'keep-dirty)
1874 (ampc-send-command 'listallinfo))
1875 (output
1876 (ampc-set-dirty 'outputs t))
1877 ((player options mixer)
1878 (setf ampc-status nil)
1879 (ampc-set-dirty 'status t))
1880 (stored_playlist
1881 (ampc-set-dirty 'playlists t))
1882 (playlist
1883 (ampc-set-dirty 'current-playlist t)
1884 (ampc-set-dirty 'status t))))
1885 (forward-line))
1886 (ampc-update))
1887
1888 (defun ampc-handle-setup (status)
1889 (unless (and (string-match "^ MPD \\(.+\\)\\.\\(.+\\)\\.\\(.+\\)$"
1890 status)
1891 (let ((version-a (string-to-number (match-string 1 status)))
1892 (version-b (string-to-number (match-string 2 status)))
1893 ;; (version-c (string-to-number (match-string 2 status)))
1894 )
1895 (or (> version-a 0)
1896 (>= version-b 15))))
1897 (error (concat "Your version of MPD is not supported. "
1898 "ampc supports MPD protocol version 0.15.0 "
1899 "and later"))))
1900
1901 (defun ampc-fill-internal-db (running)
1902 (loop with tree = (assoc (ampc-tags) ampc-internal-db)
1903 with tags =
1904 (loop for w in (ampc-normalize-windows)
1905 for props = (with-current-buffer (window-buffer w)
1906 (when (eq (car ampc-type) 'tag)
1907 (ampc-set-dirty t)
1908 (plist-get (cdr ampc-type) :tag)))
1909 when props
1910 collect props
1911 end)
1912 with song-props = (ampc-with-buffer 'song
1913 (ampc-set-dirty t)
1914 (plist-get (cdr ampc-type) :properties))
1915 for origin = (and (search-forward-regexp "^file: " nil t)
1916 (line-beginning-position))
1917 then next
1918 while origin
1919 do (goto-char (1+ origin))
1920 for next = (and (search-forward-regexp "^file: " nil t)
1921 (line-beginning-position))
1922 while (or (not running) next)
1923 do (save-restriction
1924 (narrow-to-region origin (or next (point-max)))
1925 (ampc-fill-internal-db-entry tree tags song-props))
1926 (when running
1927 (delete-region origin next)
1928 (setf next origin))))
1929
1930 (defun ampc-tags ()
1931 (loop for w in (ampc-normalize-windows)
1932 for tag = (with-current-buffer (window-buffer w)
1933 (when (eq (car ampc-type) 'tag)
1934 (plist-get (cdr ampc-type) :tag)))
1935 when tag
1936 collect tag
1937 end))
1938
1939 (defun ampc-fill-internal-db-entry (tree tags song-props)
1940 (loop for tag in tags
1941 for data = (ampc-clean-tag tag (ampc-extract (ampc-extract-regexp tag)))
1942 do (unless (cdr tree)
1943 (setf (cdr tree) (ampc-create-tree)))
1944 (setf tree (avl-tree-enter (cdr tree)
1945 (cons data nil)
1946 (lambda (_ match)
1947 match))))
1948 (push (cons (cons "file" (ampc-extract (ampc-extract-regexp "file")))
1949 (loop for p in song-props
1950 for data = (ampc-clean-tag (car p)
1951 (ampc-extract
1952 (ampc-extract-regexp (car p))))
1953 when data
1954 collect (cons (car p) data)
1955 end))
1956 (cdr tree)))
1957
1958 (defun ampc-fill-status-var (tags)
1959 (loop for k in tags
1960 for v = (ampc-extract (ampc-extract-regexp k))
1961 for s = (intern k)
1962 do (if v
1963 (setf (cdr (or (assq s ampc-status)
1964 (car (push (cons s nil) ampc-status))))
1965 v)
1966 (callf2 assq-delete-all s ampc-status))))
1967
1968 (defun ampc-handle-current-song ()
1969 (ampc-fill-status-var (append ampc-status-tags '("Artist" "Title" "file")))
1970 (ampc-fill-status)
1971 (run-hook-with-args ampc-status-changed-hook ampc-status))
1972
1973 (defun ampc-handle-status ()
1974 (ampc-fill-status-var '("volume" "repeat" "random" "consume" "xfade" "state"
1975 "song" "playlistlength"))
1976 (ampc-with-buffer 'current-playlist
1977 (when ampc-highlight-current-song-mode
1978 (font-lock-fontify-buffer))))
1979
1980 (defun ampc-handle-update ()
1981 (message "Database update started"))
1982
1983 (defun ampc-handle-command (status)
1984 (cond
1985 ((eq status 'error)
1986 (pop ampc-outstanding-commands))
1987 ((eq status 'running)
1988 (case (caar ampc-outstanding-commands)
1989 (listallinfo (ampc-fill-internal-db t))))
1990 (t
1991 (case (car (pop ampc-outstanding-commands))
1992 (idle
1993 (ampc-handle-idle))
1994 (setup
1995 (ampc-handle-setup status))
1996 (currentsong
1997 (ampc-handle-current-song))
1998 (status
1999 (ampc-handle-status))
2000 (update
2001 (ampc-handle-update))
2002 (listplaylistinfo
2003 (ampc-fill-playlist))
2004 (listplaylists
2005 (ampc-fill-playlists))
2006 (playlistinfo
2007 (ampc-fill-current-playlist))
2008 (mini-playlistinfo
2009 (ampc-mini-impl))
2010 (mini-currentsong
2011 (ampc-status))
2012 (listallinfo
2013 (ampc-handle-listallinfo))
2014 (outputs
2015 (ampc-fill-outputs)))
2016 (unless ampc-outstanding-commands
2017 (ampc-update)))))
2018
2019 (defun ampc-handle-listallinfo ()
2020 (ampc-fill-internal-db nil)
2021 (ampc-set-dirty 'tag t)
2022 (ampc-set-dirty 'song t))
2023
2024 (defun ampc-filter (_process string)
2025 (assert (buffer-live-p (process-buffer ampc-connection)))
2026 (with-current-buffer (process-buffer ampc-connection)
2027 (when string
2028 (when (and ampc-debug (not (eq ampc-debug t)))
2029 (message "ampc: <- %s" string))
2030 (goto-char (process-mark ampc-connection))
2031 (insert string)
2032 (set-marker (process-mark ampc-connection) (point)))
2033 (save-excursion
2034 (goto-char (point-min))
2035 (let ((success))
2036 (if (or (progn
2037 (when (search-forward-regexp
2038 "^ACK \\[\\(.*\\)\\] {.*} \\(.*\\)\n\\'"
2039 nil
2040 t)
2041 (message "ampc command error: %s (%s; %s)"
2042 (match-string 2)
2043 (match-string 1)
2044 (funcall (if ampc-debug 'identity 'car)
2045 (car ampc-outstanding-commands)))
2046 t))
2047 (when (search-forward-regexp "^OK\\(.*\\)\n\\'" nil t)
2048 (setf success t)))
2049 (progn
2050 (let ((match-end (match-end 0)))
2051 (save-restriction
2052 (narrow-to-region (point-min) match-end)
2053 (goto-char (point-min))
2054 (ampc-handle-command (if success (match-string 1) 'error)))
2055 (delete-region (point-min) match-end))
2056 (unless ampc-no-implicit-next-dispatch
2057 (ampc-send-next-command))))
2058 (ampc-handle-command 'running)))))
2059
2060 (defun* ampc-set-tab-offsets
2061 (&rest properties &aux (min 2) (optional-padding 0))
2062 (unless properties
2063 (return-from ampc-set-tab-offsets))
2064 (set (make-local-variable 'tab-stop-list) nil)
2065 (loop for (title . props) in properties
2066 for min- = (plist-get props :min)
2067 do (incf min (or (plist-get props :width) min-))
2068 (when min-
2069 (incf optional-padding (- (plist-get props :max) min-))))
2070 (loop for (title . props) in properties
2071 with offset = 2
2072 do (push offset tab-stop-list)
2073 (incf offset (or (plist-get props :width)
2074 (let ((min- (plist-get props :min))
2075 (max (plist-get props :max)))
2076 (if (>= min (window-width))
2077 min-
2078 (min max
2079 (+ min-
2080 (floor (* (/ (float (- max min-))
2081 optional-padding)
2082 (- (window-width)
2083 min))))))))))
2084 (callf nreverse tab-stop-list))
2085
2086 (defun* ampc-configure-frame-1 (split &aux (split-type (car split)))
2087 (if (memq split-type '(vertical horizontal))
2088 (let* ((sizes))
2089 (loop with length = (if (eq split-type 'horizontal)
2090 (window-total-width)
2091 (window-total-height))
2092 with rest = length
2093 with rest-car
2094 for (size . subsplit) in (cdr split)
2095 do (if (equal size 1.0)
2096 (progn (push t sizes)
2097 (setf rest-car sizes))
2098 (let ((l (if (integerp size) size (round (* size length)))))
2099 (decf rest l)
2100 (push l sizes)))
2101 finally do (setf (car rest-car) rest))
2102 (let ((first-window (selected-window)))
2103 (callf nreverse sizes)
2104 (loop for size in (copy-sequence sizes)
2105 for window on (cdr sizes)
2106 do (select-window
2107 (setf (car window)
2108 (split-window nil size (eq split-type 'horizontal)))))
2109 (setf (car sizes) first-window))
2110 (loop for subsplit in (cdr split)
2111 for window in sizes
2112 with result
2113 do (with-selected-window window
2114 (setf result
2115 (or (ampc-configure-frame-1 (cdr subsplit)) result)))
2116 finally return result))
2117 (setf (window-dedicated-p (selected-window)) nil)
2118 (pop-to-buffer-same-window
2119 (get-buffer-create
2120 (concat "*"
2121 (mapconcat (lambda (s) (concat (upcase (substring s 0 1))
2122 (substring s 1)))
2123 (if (memq split-type '(tag song))
2124 (list (or (plist-get (cdr split) :tag) "song"))
2125 (split-string (symbol-name split-type) "-"))
2126 " ")
2127 "*")))
2128 (if (memq split-type '(tag song))
2129 (ampc-tag-song-mode)
2130 (let ((mode (intern (concat "ampc-" (symbol-name split-type) "-mode"))))
2131 (unless (fboundp mode)
2132 (setf mode 'ampc-mode))
2133 (unless (eq major-mode 'mode)
2134 (funcall mode))))
2135 (destructuring-bind
2136 (&key (properties nil) (dedicated t) (mode-line t) &allow-other-keys)
2137 (cdr split)
2138 (apply 'ampc-set-tab-offsets properties)
2139 (setf ampc-type split
2140 (window-dedicated-p (selected-window)) dedicated
2141 mode-line-format (when mode-line
2142 (default-value 'mode-line-format))))
2143 (set (make-local-variable 'mode-line-buffer-identification)
2144 '(:eval (let ((result
2145 (concat (car-safe (propertized-buffer-identification
2146 (buffer-name)))
2147 (when ampc-dirty
2148 " [Updating...]"))))
2149 (if (< (length result) 12)
2150 (concat result (make-string (- 12 (length result)) ? ))
2151 result))))
2152 (ampc-update-header)
2153 (add-to-list 'ampc-all-buffers (current-buffer))
2154 (push (cons (or (plist-get (cdr split) :id) 9999) (selected-window))
2155 ampc-windows)
2156 (ampc-set-dirty t)
2157 (when (plist-get (cdr split) :select)
2158 (selected-window))))
2159
2160 (defun* ampc-configure-frame
2161 (split &optional no-update &aux (old-selection ampc-type) old-window-starts)
2162 (loop for w in (ampc-normalize-windows)
2163 do (with-selected-window w
2164 (with-current-buffer (window-buffer w)
2165 (push (cons (current-buffer) (window-start))
2166 old-window-starts))))
2167 (if (not ampc-use-full-frame)
2168 (ampc-restore-window-configuration)
2169 (setf (window-dedicated-p (selected-window)) nil)
2170 (delete-other-windows))
2171 (setf ampc-windows nil)
2172 (let ((select-window (ampc-configure-frame-1 split)))
2173 (setf ampc-windows
2174 (mapcar (lambda (window)
2175 (cons window (window-buffer window)))
2176 (mapcar 'cdr (sort ampc-windows
2177 (lambda (a b) (< (car a) (car b)))))))
2178 (loop for w in (ampc-normalize-windows)
2179 do (with-selected-window w
2180 (let ((old-window-start (cdr (assq (current-buffer)
2181 old-window-starts))))
2182 (when old-window-start
2183 (set-window-start nil old-window-start)))
2184 (when (and (derived-mode-p 'ampc-item-mode)
2185 (> (length tab-stop-list) 1))
2186 (ampc-set-dirty 'erase))))
2187 (select-window (or (loop for w in (ampc-normalize-windows)
2188 thereis
2189 (when (equal (with-current-buffer (window-buffer w)
2190 ampc-type)
2191 old-selection)
2192 w))
2193 select-window
2194 (selected-window))))
2195 (unless no-update
2196 (ampc-update)))
2197
2198 (defun ampc-tagger-rename-artist-title (_changed-tags data)
2199 "Rename music file according to its tags.
2200 This function is meant to be inserted into
2201 `ampc-tagger-stored-hook'. The new file name is
2202 `Artist'_-_`Title'.`extension'. Characters within `Artist' and
2203 `Title' that are not alphanumeric are substituted with underscore."
2204 (let* ((artist (replace-regexp-in-string
2205 "[^a-zA-Z0-9]" "_"
2206 (or (cdr (assq 'Artist (cdr data))) "")))
2207 (title (replace-regexp-in-string
2208 "[^a-zA-Z0-9]" "_"
2209 (or (cdr (assq 'Title (cdr data))) "")))
2210 (new-file
2211 (expand-file-name (replace-regexp-in-string
2212 "_\\(_\\)+"
2213 "_"
2214 (concat artist
2215 (when (and (> (length artist) 0)
2216 (> (length title) 0))
2217 "_-_")
2218 title
2219 (file-name-extension (car data) t)))
2220 (file-name-directory (car data)))))
2221 (unless (equal (car data) new-file)
2222 (ampc-tagger-log "Renaming file " (abbreviate-file-name (car data))
2223 " to " (abbreviate-file-name new-file) "\n")
2224 (rename-file (car data) new-file)
2225 (setf (car data) new-file))))
2226
2227 ;;; *** interactives
2228 (defun ampc-tagger-completion-at-point (&optional all-files)
2229 "Perform completion at point via `completion-at-point'.
2230 If optional prefix argument ALL-FILES is non-nil, use all files
2231 within the files list buffer as source for completion. The
2232 default behaviour is to use only the selected ones."
2233 (interactive "P")
2234 (let ((ampc-tagger-completion-all-files all-files))
2235 (completion-at-point)))
2236
2237 (defun ampc-tagger-reset (&optional reset-all-tags)
2238 "Reset all tag values within the tagger, based on the selection of files.
2239 If optional prefix argument RESET-ALL-TAGS is non-nil, restore
2240 all tags."
2241 (interactive "P")
2242 (when reset-all-tags
2243 (ampc-with-buffer 'tagger
2244 no-se
2245 (erase-buffer)
2246 (loop for tag in ampc-tagger-tags
2247 do (insert (ampc-pad (list (concat (symbol-name tag) ":") "dummy"))
2248 "\n"))
2249 (goto-char (point-min))
2250 (re-search-forward ":\\( \\)+")))
2251 (ampc-with-buffer 'tagger
2252 (loop while (search-forward-regexp
2253 (concat "^\\([ \t]*\\)\\("
2254 (mapconcat 'symbol-name ampc-tagger-tags "\\|")
2255 "\\)\\([ \t]*\\):\\([ \t]*.*\\)$")
2256 nil
2257 t)
2258 do (replace-match "" nil nil nil 1)
2259 (replace-match "" nil nil nil 3)
2260 (replace-match (concat (make-string (- (car tab-stop-list)
2261 (1+ (length (match-string 2))))
2262 ? )
2263 "<keep>")
2264 nil nil nil 4)))
2265 (ampc-tagger-update)
2266 (ampc-with-buffer 'tagger
2267 no-se
2268 (when (looking-at "[ \t]+")
2269 (goto-char (match-end 0)))))
2270
2271 (defun* ampc-tagger-save (&optional quit &aux tags)
2272 "Save tags.
2273 If optional prefix argument QUIT is non-nil, quit tagger
2274 afterwards. If the numeric value of QUIT is 16, quit tagger and
2275 do not trigger a database update"
2276 (interactive "P")
2277 (ampc-with-buffer 'tagger
2278 (loop do (loop until (eobp)
2279 while (looking-at "^[ \t]*$")
2280 do (forward-line))
2281 until (eobp)
2282 do (unless (and (looking-at
2283 (concat "^[ \t]*\\("
2284 (mapconcat 'symbol-name
2285 ampc-tagger-tags
2286 "\\|")
2287 "\\)[ \t]*:"
2288 "[ \t]*\\(.*\\)[ \t]*$"))
2289 (not (assq (intern (match-string 1)) tags)))
2290 (error "Malformed line \"%s\""
2291 (buffer-substring (line-beginning-position)
2292 (line-end-position))))
2293 (push (cons (intern (match-string 1))
2294 (let ((val (match-string 2)))
2295 (if (string= "<keep>" val)
2296 t
2297 (set-text-properties 0 (length val) nil val)
2298 val)))
2299 tags)
2300 (forward-line)))
2301 (callf2 rassq-delete-all t tags)
2302 (with-temp-buffer
2303 (loop for (tag . value) in tags
2304 do (insert (symbol-name tag) "\n"
2305 value "\n"))
2306 (let ((input-buffer (current-buffer)))
2307 (ampc-with-buffer 'files-list
2308 no-se
2309 (let ((reporter
2310 (make-progress-reporter "Storing tags"
2311 0
2312 (let ((count (count-matches "^\\* ")))
2313 (if (zerop count)
2314 1
2315 count))))
2316 (step 0))
2317 (ampc-with-selection nil
2318 (let* ((data (get-text-property (point) 'data))
2319 (old-tags (loop for (tag . data) in (cdr data)
2320 collect (cons tag data)))
2321 (found-changed (ampc-tagger-tags-modified (cdr data) tags)))
2322 (let ((pre-hook-tags (cdr data)))
2323 (run-hook-with-args 'ampc-tagger-store-hook found-changed data)
2324 (setf found-changed
2325 (or found-changed
2326 (ampc-tagger-tags-modified pre-hook-tags
2327 (cdr data)))))
2328 (when found-changed
2329 (ampc-tagger-log
2330 "Storing tags for file "
2331 (abbreviate-file-name (car data)) "\n"
2332 "\tOld tags:\n"
2333 (loop for (tag . value) in old-tags
2334 concat (concat "\t\t"
2335 (symbol-name tag) ": "
2336 value "\n"))
2337 "\tNew tags:\n"
2338 (loop for (tag . value) in (cdr data)
2339 concat (concat "\t\t"
2340 (symbol-name tag) ": "
2341 value "\n")))
2342 (ampc-tagger-make-backup (car data))
2343 (ampc-tagger-report
2344 (list "--set" (car data))
2345 (with-temp-buffer
2346 (insert-buffer-substring input-buffer)
2347 (prog1
2348 (call-process-region (point-min) (point-max)
2349 ampc-tagger-executable
2350 nil t nil
2351 "--set" (car data))
2352 (when ampc-debug
2353 (message "ampc-tagger: %s"
2354 (buffer-substring
2355 (point-min) (point))))))))
2356 (run-hook-with-args 'ampc-tagger-stored-hook found-changed data)
2357 (let ((inhibit-read-only t))
2358 (move-beginning-of-line nil)
2359 (forward-char 2)
2360 (kill-line 1)
2361 (insert
2362 (ampc-pad (loop for p in (plist-get (cdr ampc-type)
2363 :properties)
2364 when (eq (car p) 'filename)
2365 collect (file-name-nondirectory (car data))
2366 else
2367 collect (cdr (assq (intern (car p))
2368 (cdr data)))
2369 end))
2370 "\n")
2371 (forward-line -1)
2372 (put-text-property (line-beginning-position)
2373 (1+ (line-end-position))
2374 'data data))
2375 (progress-reporter-update reporter (incf step))))
2376 (progress-reporter-done reporter)))))
2377 (when quit
2378 (ampc-tagger-quit (eq (prefix-numeric-value quit) 16))))
2379
2380 (defun ampc-tagger-quit (&optional no-update)
2381 "Quit tagger and restore previous window configuration.
2382 With optional prefix NO-UPDATE, do not trigger a database update."
2383 (interactive "P")
2384 (set-window-configuration (or (car-safe ampc-tagger-previous-configuration)
2385 ampc-tagger-previous-configuration))
2386 (when (car-safe ampc-tagger-previous-configuration)
2387 (unless no-update
2388 (ampc-trigger-update))
2389 (setf ampc-windows (cadr ampc-tagger-previous-configuration)))
2390 (setf ampc-tagger-previous-configuration nil))
2391
2392 (defun ampc-move-to-tab ()
2393 "Move point to next logical tab stop."
2394 (interactive)
2395 (let ((tab (loop for tab in
2396 (or (get-text-property (point) 'tab-stop-list) tab-stop-list)
2397 while (>= (current-column) tab)
2398 finally return tab)))
2399 (when tab
2400 (goto-char (min (+ (line-beginning-position) tab) (line-end-position))))))
2401
2402 (defun ampc-mouse-play-this (event)
2403 (interactive "e")
2404 (select-window (posn-window (event-end event)))
2405 (goto-char (posn-point (event-end event)))
2406 (ampc-play-this))
2407
2408 (defun ampc-mouse-delete (event)
2409 (interactive "e")
2410 (select-window (posn-window (event-end event)))
2411 (goto-char (posn-point (event-end event)))
2412 (ampc-delete 1))
2413
2414 (defun ampc-mouse-add (event)
2415 (interactive "e")
2416 (select-window (posn-window (event-end event)))
2417 (goto-char (posn-point (event-end event)))
2418 (ampc-add-impl))
2419
2420 (defun ampc-mouse-delete-playlist (event)
2421 (interactive "e")
2422 (select-window (posn-window (event-end event)))
2423 (goto-char (posn-point (event-end event)))
2424 (ampc-delete-playlist t))
2425
2426 (defun ampc-mouse-load (event)
2427 (interactive "e")
2428 (select-window (posn-window (event-end event)))
2429 (goto-char (posn-point (event-end event)))
2430 (ampc-load t))
2431
2432 (defun ampc-mouse-toggle-output-enabled (event)
2433 (interactive "e")
2434 (select-window (posn-window (event-end event)))
2435 (goto-char (posn-point (event-end event)))
2436 (ampc-toggle-output-enabled 1))
2437
2438 (defun* ampc-mouse-toggle-mark (event &aux (inhibit-read-only t))
2439 (interactive "e")
2440 (let ((window (posn-window (event-end event))))
2441 (when (with-selected-window window
2442 (goto-char (posn-point (event-end event)))
2443 (unless (eobp)
2444 (move-beginning-of-line nil)
2445 (ampc-mark-impl (not (eq (char-after) ?*)) 1)
2446 t))
2447 (select-window window))))
2448
2449 (defun ampc-mouse-align-point (event)
2450 (interactive "e")
2451 (select-window (posn-window (event-end event)))
2452 (goto-char (posn-point (event-end event)))
2453 (ampc-align-point))
2454
2455 (defun* ampc-unmark-all (&aux (inhibit-read-only t))
2456 "Remove all marks."
2457 (interactive)
2458 (assert (ampc-in-ampc-p t))
2459 (save-excursion
2460 (goto-char (point-min))
2461 (loop while (search-forward-regexp "^\\* " nil t)
2462 do (replace-match " " nil nil)))
2463 (ampc-post-mark-change-update))
2464
2465 (defun ampc-trigger-update ()
2466 "Trigger a database update."
2467 (interactive)
2468 (assert (ampc-on-p))
2469 (ampc-send-command 'update))
2470
2471 (defun* ampc-toggle-marks (&aux (inhibit-read-only t))
2472 "Toggle marks.
2473 Marked entries become unmarked, and vice versa."
2474 (interactive)
2475 (assert (ampc-in-ampc-p t))
2476 (save-excursion
2477 (loop for (a . b) in '(("* " . "T ")
2478 (" " . "* ")
2479 ("T " . " "))
2480 do (goto-char (point-min))
2481 (loop while (search-forward-regexp (concat "^" (regexp-quote a))
2482 nil
2483 t)
2484 do (replace-match b nil nil))))
2485 (ampc-post-mark-change-update))
2486
2487 (defun ampc-up (&optional arg)
2488 "Move selected entries ARG positions upwards.
2489 ARG defaults to one."
2490 (interactive "p")
2491 (assert (ampc-in-ampc-p))
2492 (ampc-move (- (or arg 1))))
2493
2494 (defun ampc-down (&optional arg)
2495 "Move selected entries ARG positions downwards.
2496 ARG defaults to one."
2497 (interactive "p")
2498 (assert (ampc-in-ampc-p))
2499 (ampc-move (or arg 1)))
2500
2501 (defun ampc-mark (&optional arg)
2502 "Mark the next ARG'th entries.
2503 ARG defaults to 1."
2504 (interactive "p")
2505 (assert (ampc-in-ampc-p t))
2506 (ampc-mark-impl t arg))
2507
2508 (defun ampc-unmark (&optional arg)
2509 "Unmark the next ARG'th entries.
2510 ARG defaults to 1."
2511 (interactive "p")
2512 (assert (ampc-in-ampc-p t))
2513 (ampc-mark-impl nil arg))
2514
2515 (defun ampc-set-volume (&optional arg)
2516 "Set volume to ARG percent.
2517 If ARG is nil, read ARG from minibuffer."
2518 (interactive "P")
2519 (assert (ampc-on-p))
2520 (ampc-set-volume-impl (or arg (read-number "Volume: "))))
2521
2522 (defun ampc-increase-volume (&optional arg)
2523 "Increase volume by prefix argument ARG or, if ARG is nil,
2524 `ampc-volume-step'."
2525 (interactive "P")
2526 (assert (ampc-on-p))
2527 (ampc-set-volume-impl arg '+))
2528
2529 (defun ampc-decrease-volume (&optional arg)
2530 "Decrease volume by prefix argument ARG or, if ARG is nil,
2531 `ampc-volume-step'."
2532 (interactive "P")
2533 (assert (ampc-on-p))
2534 (ampc-set-volume-impl arg '-))
2535
2536 (defun ampc-set-crossfade (&optional arg)
2537 "Set crossfade to ARG seconds.
2538 If ARG is nil, read ARG from minibuffer."
2539 (interactive "P")
2540 (assert (ampc-on-p))
2541 (ampc-set-crossfade-impl (or arg (read-number "Crossfade: "))))
2542
2543 (defun ampc-increase-crossfade (&optional arg)
2544 "Increase crossfade by prefix argument ARG or, if ARG is nil,
2545 `ampc-crossfade-step'."
2546 (interactive "P")
2547 (assert (ampc-on-p))
2548 (ampc-set-crossfade-impl arg '+))
2549
2550 (defun ampc-decrease-crossfade (&optional arg)
2551 "Decrease crossfade by prefix argument ARG or, if ARG is nil,
2552 `ampc-crossfade-step'."
2553 (interactive "P")
2554 (assert (ampc-on-p))
2555 (ampc-set-crossfade-impl arg '-))
2556
2557 (defun ampc-toggle-repeat (&optional arg)
2558 "Toggle MPD's repeat state.
2559 With prefix argument ARG, enable repeating if ARG is positive,
2560 otherwise disable it."
2561 (interactive "P")
2562 (assert (ampc-on-p))
2563 (ampc-toggle-state 'repeat arg))
2564
2565 (defun ampc-toggle-consume (&optional arg)
2566 "Toggle MPD's consume state.
2567 With prefix argument ARG, enable consuming if ARG is positive,
2568 otherwise disable it.
2569
2570 When consume is activated, each song played is removed from the playlist."
2571 (interactive "P")
2572 (assert (ampc-on-p))
2573 (ampc-toggle-state 'consume arg))
2574
2575 (defun ampc-toggle-random (&optional arg)
2576 "Toggle MPD's random state.
2577 With prefix argument ARG, enable random playing if ARG is positive,
2578 otherwise disable it."
2579 (interactive "P")
2580 (ampc-toggle-state 'random arg))
2581
2582 (defun ampc-play-this (&optional arg)
2583 "Play selected song.
2584 With prefix argument ARG, play the ARG'th song located at the
2585 zero-indexed position of the current playlist."
2586 (interactive "P")
2587 (assert (and (ampc-on-p) (or arg (ampc-in-ampc-p))))
2588 (if (not arg)
2589 (unless (eobp)
2590 (ampc-send-command 'play nil (1- (line-number-at-pos)))
2591 (ampc-send-command 'pause nil 0))
2592 (ampc-send-command 'play nil arg)
2593 (ampc-send-command 'pause nil 0)))
2594
2595 (defun* ampc-toggle-play
2596 (&optional arg &aux (state (cdr (assq 'state ampc-status))))
2597 "Toggle play state.
2598 If MPD does not play a song already, start playing the song at
2599 point if the current buffer is the playlist buffer, otherwise
2600 start at the beginning of the playlist.
2601
2602 If ARG is 4, stop player rather than pause if applicable."
2603 (interactive "P")
2604 (assert (ampc-on-p))
2605 (unless state
2606 (return-from ampc-toggle-play))
2607 (when arg
2608 (setf arg (prefix-numeric-value arg)))
2609 (ecase (intern state)
2610 (stop
2611 (when (or (null arg) (> arg 0))
2612 (ampc-send-command
2613 'play
2614 '(:remove-other (pause))
2615 (if (and (eq (car ampc-type) 'current-playlist) (not (eobp)))
2616 (1- (line-number-at-pos))
2617 0))))
2618 (pause
2619 (when (or (null arg) (> arg 0))
2620 (ampc-send-command 'pause '(:remove-other (play)) 0)))
2621 (play
2622 (cond ((or (null arg) (< arg 0))
2623 (ampc-send-command 'pause '(:remove-other (play)) 1))
2624 ((eq arg 4)
2625 (ampc-send-command 'stop))))))
2626
2627 (defun ampc-next (&optional arg)
2628 "Play next song.
2629 With prefix argument ARG, skip ARG songs."
2630 (interactive "p")
2631 (assert (ampc-on-p))
2632 (ampc-skip (or arg 1)))
2633
2634 (defun ampc-previous (&optional arg)
2635 "Play previous song.
2636 With prefix argument ARG, skip ARG songs."
2637 (interactive "p")
2638 (assert (ampc-on-p))
2639 (ampc-skip (- (or arg 1))))
2640
2641 (defun ampc-rename-playlist (new-name)
2642 "Rename selected playlist to NEW-NAME.
2643 If NEW-NAME is nil, read NEW-NAME from the minibuffer."
2644 (interactive "M")
2645 (unless new-name
2646 (setf new-name (read-from-minibuffer (concat "New name for playlist "
2647 (ampc-playlist)
2648 ": "))))
2649 (assert (ampc-in-ampc-p))
2650 (if (ampc-playlist)
2651 (ampc-send-command 'rename '(:full-remove t) (ampc-quote new-name))
2652 (message "No playlist selected")))
2653
2654 (defun ampc-load (&optional at-point)
2655 "Load selected playlist in the current playlist.
2656 If optional argument AT-POINT is non-nil (or if no playlist is
2657 selected), use playlist at point rather than the selected one."
2658 (interactive)
2659 (assert (ampc-in-ampc-p))
2660 (if (ampc-playlist at-point)
2661 (ampc-send-command
2662 'load '(:keep-prev t)
2663 (ampc-quote (ampc-playlist at-point)))
2664 (if at-point
2665 (message "No playlist at point")
2666 (message "No playlist selected"))))
2667
2668 (defun ampc-toggle-output-enabled (&optional arg)
2669 "Toggle the next ARG outputs.
2670 If ARG is omitted, use the selected entries."
2671 (interactive "P")
2672 (assert (ampc-in-ampc-p))
2673 (ampc-with-selection arg
2674 (let ((data (get-text-property (point) 'data)))
2675 (ampc-send-command (if (equal (cdr (assoc "outputenabled" data)) "1")
2676 'disableoutput
2677 'enableoutput)
2678 '(:full-remove t)
2679 (cdr (assoc "outputid" data))))))
2680
2681 (defun ampc-delete (&optional arg)
2682 "Delete the next ARG songs from the playlist.
2683 If ARG is omitted, use the selected entries. If ARG is non-nil,
2684 all marks after point are removed nontheless."
2685 (interactive "P")
2686 (assert (ampc-in-ampc-p))
2687 (let ((first-del nil))
2688 (ampc-with-selection arg
2689 (unless (or first-del (when arg (< arg 0)))
2690 (setf first-del (point)))
2691 (let ((val (1- (- (line-number-at-pos) (if (or (not arg) (> arg 0))
2692 index
2693 0)))))
2694 (if (and (not (eq (car ampc-type) 'current-playlist)) (ampc-playlist))
2695 (ampc-send-command 'playlistdelete
2696 '(:keep-prev t)
2697 (ampc-quote (ampc-playlist))
2698 val)
2699 (ampc-send-command 'delete '(:keep-prev t) val))
2700 (ampc-mark-impl nil nil)))
2701 (when first-del
2702 (goto-char first-del))))
2703
2704 (defun ampc-shuffle ()
2705 "Shuffle playlist."
2706 (interactive)
2707 (assert (ampc-on-p))
2708 (if (not (and (not (eq (car ampc-type) 'current-playlist)) (ampc-playlist)))
2709 (ampc-send-command 'shuffle)
2710 (ampc-with-buffer 'playlist
2711 (let ((shuffled
2712 (mapcar 'car
2713 (sort (loop
2714 until (eobp)
2715 collect (cons (cdr (assoc "file" (get-text-property
2716 (+ 2 (point))
2717 'data)))
2718 (random))
2719 do (forward-line))
2720 (lambda (a b)
2721 (< (cdr a) (cdr b)))))))
2722 (ampc-clear)
2723 (loop for s in shuffled
2724 do (ampc-add-impl s))))))
2725
2726 (defun ampc-clear ()
2727 "Clear playlist."
2728 (interactive)
2729 (assert (ampc-on-p))
2730 (if (and (not (eq (car ampc-type) 'current-playlist)) (ampc-playlist))
2731 (ampc-send-command 'playlistclear '(:full-remove t)
2732 (ampc-quote (ampc-playlist)))
2733 (ampc-send-command 'clear)))
2734
2735 (defun ampc-add (&optional arg)
2736 "Add the songs associated with the next ARG entries after point
2737 to the playlist.
2738 If ARG is omitted, use the selected entries in the current buffer."
2739 (interactive "P")
2740 (assert (ampc-in-ampc-p))
2741 (ampc-with-selection arg
2742 (ampc-add-impl)))
2743
2744 (defun ampc-status (&optional no-print)
2745 "Display and return the information that is displayed in the status window.
2746 If optional argument NO-PRINT is non-nil, just return the text.
2747 If NO-PRINT is nil, the display may be delayed if ampc does not
2748 have enough information yet."
2749 (interactive)
2750 (assert (ampc-on-p))
2751 (unless (or ampc-status no-print)
2752 (ampc-send-command 'status)
2753 (ampc-send-command 'mini-currentsong)
2754 (return-from ampc-status))
2755 (let* ((flags (mapconcat
2756 'identity
2757 (loop for (f . n) in '((repeat . "Repeat")
2758 (random . "Random")
2759 (consume . "Consume"))
2760 when (equal (cdr (assq f ampc-status)) "1")
2761 collect n
2762 end)
2763 "|"))
2764 (state (cdr (assq 'state ampc-status)))
2765 (status (concat "State: " state
2766 (when (and ampc-yield no-print)
2767 (concat (make-string (- 10 (length state)) ? )
2768 (nth (% ampc-yield 4) '("|" "/" "-" "\\"))))
2769 "\n"
2770 (when (equal state "play")
2771 (concat "Playing: "
2772 (ampc-clean-tag
2773 'Artist
2774 (cdr (assq 'Artist ampc-status)))
2775 " - "
2776 (ampc-clean-tag
2777 'Title
2778 (cdr (assq 'Title ampc-status)))
2779 "\n"))
2780 "Volume: " (cdr (assq 'volume ampc-status)) "\n"
2781 "Crossfade: " (cdr (assq 'xfade ampc-status))
2782 (unless (equal flags "")
2783 (concat "\n" flags)))))
2784 (unless no-print
2785 (message "%s" status))
2786 status))
2787
2788 (defun ampc-delete-playlist (&optional at-point)
2789 "Delete selected playlist.
2790 If optional argument AT-POINT is non-nil (or if no playlist is
2791 selected), use playlist at point rather than the selected one."
2792 (interactive)
2793 (assert (ampc-in-ampc-p))
2794 (if (ampc-playlist at-point)
2795 (when (y-or-n-p (concat "Delete playlist " (ampc-playlist at-point) "?"))
2796 (ampc-send-command 'rm '(:full-remove t)
2797 (ampc-quote (ampc-playlist at-point))))
2798 (if at-point
2799 (message "No playlist at point")
2800 (message "No playlist selected"))))
2801
2802 ;;;###autoload
2803 (defun ampc-tagger-dired (&optional arg)
2804 "Start the tagging subsystem on dired's marked files.
2805 With optional prefix argument ARG, use the next ARG files."
2806 (interactive "P")
2807 (assert (derived-mode-p 'dired-mode))
2808 (ampc-tag-files
2809 (loop for file in (dired-map-over-marks (dired-get-filename) arg)
2810 unless (file-directory-p file)
2811 collect file
2812 end)))
2813
2814 ;;;###autoload
2815 (defun ampc-tag-files (files)
2816 "Start the tagging subsystem.
2817 FILES should be a list of absolute file names, the files to tag."
2818 (unless files
2819 (message "No files specified")
2820 (return-from ampc-tagger-files t))
2821 (when (memq (car ampc-type) '(files-list tagger))
2822 (message "You are already within the tagger")
2823 (return-from ampc-tagger-files t))
2824 (let ((reporter (make-progress-reporter "Grabbing tags" 0 (length files))))
2825 (loop for file in-ref files
2826 for i from 1
2827 do (run-hook-with-args 'ampc-tagger-grab-hook file)
2828 (with-temp-buffer
2829 (ampc-tagger-call "--get" file)
2830 (setf file
2831 (apply 'list
2832 file
2833 (loop for tag in ampc-tagger-tags
2834 collect
2835 (cons tag (or (ampc-extract (ampc-extract-regexp
2836 (symbol-name tag)))
2837 ""))))))
2838 (run-hook-with-args 'ampc-tagger-grabbed-hook file)
2839 (progress-reporter-update reporter i))
2840 (progress-reporter-done reporter))
2841 (unless ampc-tagger-previous-configuration
2842 (setf ampc-tagger-previous-configuration (current-window-configuration)))
2843 (ampc-configure-frame (cdr (assq 'tagger ampc-views)) t)
2844 (ampc-with-buffer 'files-list
2845 (erase-buffer)
2846 (loop for (file . props) in files
2847 do (insert (propertize
2848 (concat
2849 " "
2850 (ampc-pad
2851 (loop for p in (plist-get (cdr ampc-type) :properties)
2852 when (eq (car p) 'filename)
2853 collect (file-name-nondirectory file)
2854 else
2855 collect (cdr (assq (intern (car p)) props))
2856 end))
2857 "\n")
2858 'data (cons file props))))
2859 (ampc-set-dirty nil)
2860 (ampc-toggle-marks))
2861 (ampc-with-buffer 'tagger
2862 no-se
2863 (ampc-tagger-reset t)
2864 (goto-char (point-min))
2865 (search-forward-regexp ": *")
2866 (ampc-set-dirty nil))
2867 nil)
2868
2869 (defun* ampc-tagger (&optional arg &aux files)
2870 "Start the tagging subsystem.
2871 The files to tag are collected by using either the selected
2872 entries within the current buffer or the next ARG entries at
2873 point if numeric perfix argument ARG is non-nil, the file
2874 associated with the entry at point, or, if both sources did not
2875 provide any files, the audio file that is currently played by
2876 MPD."
2877 (interactive "P")
2878 (assert (ampc-in-ampc-p))
2879 (unless ampc-tagger-version-verified
2880 (with-temp-buffer
2881 (ampc-tagger-call "--version")
2882 (goto-char (point-min))
2883 (let ((version (buffer-substring (line-beginning-position)
2884 (line-end-position))))
2885 (unless (equal version ampc-tagger-version)
2886 (message (concat "The reported version of %s is not supported - "
2887 "got \"%s\", want \"%s\"")
2888 ampc-tagger-executable
2889 version
2890 ampc-tagger-version)
2891 (return-from ampc-tagger))))
2892 (setf ampc-tagger-version-verified t))
2893 (unless ampc-tagger-genres
2894 (with-temp-buffer
2895 (ampc-tagger-call "--genres")
2896 (loop while (search-backward-regexp "^\\(.+\\)$" nil t)
2897 do (push (match-string 1) ampc-tagger-genres))))
2898 (unless ampc-tagger-music-directories
2899 (message (concat "ampc-tagger-music-directories is nil. Fill it via "
2900 "M-x customize-variable RET ampc-tagger-music-directories "
2901 "RET"))
2902 (return-from ampc-tagger))
2903 (case (car ampc-type)
2904 (current-playlist
2905 (save-excursion
2906 (ampc-with-selection arg
2907 (callf nconc files (list (cdr (assoc "file" (get-text-property
2908 (line-end-position)
2909 'data))))))))
2910 ((playlist tag song)
2911 (save-excursion
2912 (ampc-with-selection arg
2913 (ampc-on-files (lambda (file) (push file files)))))
2914 (callf nreverse files))
2915 (t
2916 (let ((file (cdr (assoc 'file ampc-status))))
2917 (when file
2918 (setf files (list file))))))
2919 (loop for file in-ref files
2920 for read-file = (locate-file file ampc-tagger-music-directories)
2921 do (unless read-file
2922 (error "Cannot locate file %s in ampc-tagger-music-directories"
2923 file)
2924 (return-from ampc-tagger))
2925 (setf file (expand-file-name read-file)))
2926 (setf ampc-tagger-previous-configuration
2927 (list (current-window-configuration) ampc-windows))
2928 (when (ampc-tag-files files)
2929 (setf ampc-tagger-previous-configuration nil)))
2930
2931 (defun ampc-store (&optional name-or-append)
2932 "Store current playlist as NAME-OR-APPEND.
2933 If NAME is non-nil and not a string, append selected entries
2934 within the current playlist buffer to the selected playlist. If
2935 NAME-OR-APPEND is a negative integer, append the next (-
2936 NAME-OR-APPEND) entries after point within the current playlist
2937 buffer to the selected playlist. If NAME-OR-APPEND is nil, read
2938 playlist name from the minibuffer."
2939 (interactive "P")
2940 (assert (ampc-in-ampc-p))
2941 (unless name-or-append
2942 (setf name-or-append (read-from-minibuffer "Save playlist as: ")))
2943 (if (stringp name-or-append)
2944 (ampc-send-command 'save '(:full-remove t) (ampc-quote name-or-append))
2945 (if (not (ampc-playlist))
2946 (message "No playlist selected")
2947 (ampc-with-buffer 'current-playlist
2948 (when name-or-append
2949 (callf prefix-numeric-value name-or-append))
2950 (ampc-with-selection (if (and name-or-append (< name-or-append 0))
2951 (- name-or-append)
2952 nil)
2953 (ampc-send-command
2954 'playlistadd
2955 '(:keep-prev t)
2956 (ampc-quote (ampc-playlist))
2957 (ampc-quote (cdr (assoc "file"
2958 (get-text-property (point) 'data))))))))))
2959
2960 (defun* ampc-goto-current-song (&aux (song (cdr (assq 'song ampc-status))))
2961 "Select the current playlist window and move point to the current song."
2962 (interactive)
2963 (assert (ampc-in-ampc-p))
2964 (let ((window (ampc-with-buffer 'current-playlist
2965 (selected-window))))
2966 (when window
2967 (select-window window)
2968 (when song
2969 (goto-char (point-min))
2970 (forward-line (string-to-number song)))
2971 (ampc-align-point))))
2972
2973 (defun ampc-previous-line (&optional arg)
2974 "Go to previous ARG'th entry in the current buffer.
2975 ARG defaults to 1."
2976 (interactive "p")
2977 (assert (ampc-in-ampc-p t))
2978 (ampc-next-line (* (or arg 1) -1)))
2979
2980 (defun ampc-next-line (&optional arg)
2981 "Go to next ARG'th entry in the current buffer.
2982 ARG defaults to 1."
2983 (interactive "p")
2984 (assert (ampc-in-ampc-p t))
2985 (forward-line arg)
2986 (if (eobp)
2987 (progn (forward-line -1)
2988 (forward-char 2)
2989 t)
2990 (ampc-align-point)
2991 nil))
2992
2993 (defun* ampc-suspend (&optional (run-hook t))
2994 "Suspend ampc.
2995 This function resets the window configuration, but does not close
2996 the connection to MPD or destroy the internal cache of ampc.
2997 This means subsequent startups of ampc will be faster."
2998 (interactive)
2999 (when ampc-working-timer
3000 (cancel-timer ampc-working-timer))
3001 (ampc-restore-window-configuration)
3002 (loop for b in ampc-all-buffers
3003 do (when (buffer-live-p b)
3004 (kill-buffer b)))
3005 (setf ampc-windows nil
3006 ampc-all-buffers nil
3007 ampc-working-timer nil)
3008 (when run-hook
3009 (run-hooks 'ampc-suspend-hook)))
3010
3011 (defun ampc-mini ()
3012 "Select song to play via `completing-read'."
3013 (interactive)
3014 (assert (ampc-on-p))
3015 (ampc-send-command 'mini-playlistinfo))
3016
3017 (defun ampc-quit (&optional arg)
3018 "Quit ampc.
3019 If prefix argument ARG is non-nil, kill the MPD instance that
3020 ampc is connected to."
3021 (interactive "P")
3022 (when (ampc-on-p)
3023 (set-process-filter ampc-connection nil)
3024 (when (equal (car-safe ampc-outstanding-commands) '(idle nil))
3025 (ampc-send-command-impl "noidle")
3026 (with-current-buffer (process-buffer ampc-connection)
3027 (loop do (goto-char (point-min))
3028 until (search-forward-regexp "^\\(ACK\\)\\|\\(OK\\).*\n\\'" nil t)
3029 do (accept-process-output ampc-connection nil 50))))
3030 (ampc-send-command-impl (if arg "kill" "close"))
3031 (delete-process ampc-connection))
3032 (when ampc-working-timer
3033 (cancel-timer ampc-working-timer))
3034 (ampc-suspend nil)
3035 (setf ampc-connection nil
3036 ampc-internal-db nil
3037 ampc-outstanding-commands nil
3038 ampc-status nil)
3039 (run-hooks 'ampc-quit-hook))
3040
3041 ;;;###autoload
3042 (defun ampc-suspended-p ()
3043 "Return non-nil if ampc is suspended."
3044 (interactive)
3045 (and (ampc-on-p) (not ampc-windows)))
3046
3047 ;;;###autoload
3048 (defun ampc-on-p ()
3049 "Return non-nil if ampc is connected to the daemon."
3050 (interactive)
3051 (and ampc-connection (memq (process-status ampc-connection) '(open run))))
3052
3053 ;;;###autoload
3054 (defun ampc (&optional host port suspend)
3055 "ampc is an asynchronous client for the MPD media player.
3056 This function is the main entry point for ampc.
3057
3058 HOST and PORT specify the MPD instance to connect to. The values
3059 default to the ones specified in `ampc-default-server'."
3060 (interactive)
3061 (unless (byte-code-function-p (symbol-function 'ampc))
3062 (message "You should byte-compile ampc"))
3063 (run-hooks 'ampc-before-startup-hook)
3064 (unless host
3065 (setf host (or (car ampc-default-server) (read-string "Host: "))))
3066 (unless port
3067 (setf port (or (cdr ampc-default-server) (read-string "Port: "))))
3068 (when (and ampc-connection
3069 (or (not (equal host ampc-host))
3070 (not (equal port ampc-port))
3071 (not (ampc-on-p))))
3072 (ampc-quit))
3073 (unless ampc-connection
3074 (let ((connection (open-network-stream "ampc"
3075 (with-current-buffer
3076 (get-buffer-create " *ampc*")
3077 (erase-buffer)
3078 (current-buffer))
3079 host
3080 port
3081 :type 'plain :return-list t)))
3082 (unless (car connection)
3083 (error "Failed connecting to server: %s"
3084 (plist-get ampc-connection :error)))
3085 (setf ampc-connection (car connection)
3086 ampc-host host
3087 ampc-port port))
3088 (set-process-coding-system ampc-connection 'utf-8-unix 'utf-8-unix)
3089 (set-process-filter ampc-connection 'ampc-filter)
3090 (set-process-query-on-exit-flag ampc-connection nil)
3091 (setf ampc-outstanding-commands '((setup))))
3092 (if suspend
3093 (ampc-update)
3094 (ampc-configure-frame (cddadr ampc-views)))
3095 (run-hooks 'ampc-connected-hook)
3096 (when suspend
3097 (ampc-suspend))
3098 (ampc-filter (process-buffer ampc-connection) nil))
3099
3100 (provide 'ampc)
3101
3102 ;; Local Variables:
3103 ;; eval: (outline-minor-mode 1)
3104 ;; outline-regexp: ";;; \\*+"
3105 ;; fill-column: 80
3106 ;; indent-tabs-mode: nil
3107 ;; End: