-
Notifications
You must be signed in to change notification settings - Fork 0
/
ampc-0.2.tar
3339 lines (3103 loc) · 140 KB
/
ampc-0.2.tar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
ampc-0.2/ 0000775 0001750 0001750 00000000000 14051364140 010761 5 ustar kimr kimr ampc-0.2/ampc.el 0000664 0001750 0001750 00000362460 14051364140 012236 0 ustar kimr kimr ;;; ampc.el --- Asynchronous Music Player Controller -*- lexical-binding: t -*-
;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
;; Author: Christopher Schmidt <[email protected]>
;; Maintainer: Christopher Schmidt <[email protected]>
;; Version: 0.2
;; Created: 2011-12-06
;; Keywords: ampc, mpc, mpd
;; Compatibility: GNU Emacs: 24.x
;; This file is part of ampc.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; * description
;; ampc is a controller for the Music Player Daemon (http://mpd.wikia.com/).
;;; ** installation
;; If you use GNU ELPA, install ampc via M-x package-list-packages RET or
;; (package-install 'ampc). Otherwise, grab the files in this repository and
;; put the Emacs Lisp ones somewhere in your load-path or add the directory the
;; files are in to it, e.g.:
;;
;; (add-to-list 'load-path "~/.emacs.d/ampc")
;; (autoload 'ampc "ampc" nil t)
;;
;; Byte-compile ampc (M-x byte-compile-file RET /path/to/ampc.el RET) to improve
;; its performance!
;;; *** tagger
;; ampc is not only a frontend to MPD but also a full-blown audio file tagger.
;; To use this feature you have to build the backend application, `ampc_tagger',
;; which in turn uses TagLib (http://taglib.github.com/), a dual-licended
;; (LGPL/MPL) audio meta-data library written in C++. TagLib has no
;; dependencies on its own.
;;
;; To build `ampc_tagger', locate ampc_tagger.cpp. The file can be found in the
;; directory in which this file, ampc.el, is located. Compile the file and
;; either customize `ampc-tagger-executable' to point to the binary file or move
;; the executable in a suitable directory so Emacs finds it via consulting
;; `exec-path'.
;;
;; g++ -O2 ampc_tagger.cpp -oampc_tagger -ltag && sudo cp ampc_tagger /usr/local/bin && rm ampc_tagger
;;
;; You have to customize `ampc-tagger-music-directories' in order to use the
;; tagger. This variable should be a list of directories in which your music
;; files are located. Usually this list should have only one entry, the value
;; of your mpd.conf's `music_directory'.
;;
;; If `ampc-tagger-backup-directory' is non-nil, the tagger saved copies of all
;; files that are about to be modified to this directory. Emacs's regular
;; numeric backup filename syntax is used for the backup file names. By default
;; `ampc-tagger-backup-directory' is set to "~/.emacs.d/ampc-backups/".
;;; ** usage
;; To invoke ampc call the command `ampc', e.g. via M-x ampc RET. The first
;; argument to `ampc' is the host, the second is the port. Both values default
;; to nil. If nil, ampc will use the value specified in `ampc-default-server',
;; by default localhost:6600. To make ampc use the full frame rather than the
;; selected window for its window setup, customise `ampc-use-full-frame' to a
;; non-nil value.
;;
;; ampc offers three independent views which expose different parts of the user
;; interface. The current playlist view, the default view at startup, may be
;; accessed using the `J' key (that is `S-j'). The playlist view may be
;; accessed using the `K' key. The outputs view may be accessed by pressing
;; `L'.
;;; *** current playlist view
;; The playlist view looks like this:
;;
;; .........................
;; . 1 . 3 . 4 . 5 .
;; .......... . . .
;; . 2 . . . .
;; . . . . .
;; . . . . .
;; . ................
;; . . 6 .
;; . . .
;; .........................
;;
;; Window one exposes basic information about the daemon, such as the current
;; state (stop/play/pause), the song currently playing or the volume.
;;
;; All windows, except the status window, contain a tabular list of items. Each
;; item may be selected/marked. There may be multiple selections.
;;
;; To mark an entry, move the point to the entry and press `m' (ampc-mark). To
;; unmark an entry, press `u' (ampc-unmark). To unmark all entries, press `U'
;; (ampc-unmark-all). To toggle marks, press `t' (ampc-toggle-marks). Pressing
;; `<down-mouse-1>' with the mouse mouse cursor on a list entry will move point
;; to the entry and toggle the mark. To navigate to the next entry, press `n'
;; (ampc-next-line). Analogous, pressing `p' (ampc-previous-line) moves the
;; point to the previous entry.
;;
;; Window two shows the current playlist. The song that is currently played by
;; the daemon, if any, is highlighted. To delete the selected songs from the
;; playlist, press `d' (ampc-delete). Pressing `<down-mouse-3>' will move the
;; point to the entry under cursor and delete it from the playlist. To move the
;; selected songs up, press `<up>' (ampc-up). Analogous, press `<down>'
;; (ampc-down) to move the selected songs down. Pressing `<return>'
;; (ampc-play-this) or `<down-mouse-2>' will play the song at point/cursor.
;;
;; Windows three to five are tag browsers. You use them to narrow the song
;; database to certain songs. Think of tag browsers as filters, analogous to
;; piping `grep' outputs through additional `grep' filters. The property of the
;; songs that is filtered is displayed in the header line of the window.
;;
;; Window six shows the songs that match the filters defined by windows three to
;; five. To add the selected song to the playlist, press `a' (ampc-add).
;; Pressing `<down-mouse-3>' will move the point to the entry under the cursor
;; and execute `ampc-add'. These key bindings works in tag browsers as well.
;; Calling `ampc-add' in a tag browser adds all songs filtered up to the
;; selected browser to the playlist.
;;
;; The tag browsers of the current playlist view (accessed via `J') are `Genre'
;; (window 3), `Artist' (window 4) and `Album' (window 5). The key `M' may be
;; used to fire up a slightly modified current playlist view. There is no
;; difference to the default current playlist view other than that the tag
;; browsers filter to `Genre' (window 3), `Album' (window 4) and `Artist'
;; (window 5). Metaphorically speaking, the order of the `grep' filters defined
;; by the tag browsers is different.
;;; *** playlist view
;; The playlist view resembles the current playlist view. The window, which
;; exposes the playlist content, is replaced by three windows, vertically
;; arragned, though. The top one still shows the current playlist. The bottom
;; one shows a list of stored playlists. The middle window exposes the content
;; of the selected (stored) playlist. All commands that used to work in the
;; current playlist view and modify the current playlist now modify the selected
;; (stored) playlist unless the point is within the current playlist buffer.
;; The list of stored playlists is the only view in ampc that may have only one
;; marked entry.
;;
;; To queue a playlist, press `l' (ampc-load) or `<down-mouse-2>'. To delete a
;; playlist, press `d' (ampc-delete-playlist) or `<down-mouse-3>'. The command
;; `ampc-rename-playlist', bound to `r', can be used to rename a playlist.
;;
;; Again, the key `<' may be used to setup a playlist view with a different
;; order of tag browsers.
;;; *** outputs view
;; The outputs view contains a single list which shows the configured outputs of
;; MPD. To toggle the enabled property of the selected outputs, press `a'
;; (ampc-toggle-output-enabled) or `<mouse-3>'.
;;; ** tagger
;; To start the tagging subsystem, press `I' (ampc-tagger). This key binding
;; works in every buffer associated with ampc. First, the command tries to
;; determine which files you want to tag. The files are collected using either
;; the selected entries within the current buffer, the file associated with the
;; entry at point, or, if both sources did not provide any files, the audio file
;; that is currently played by MPD. Next, the tagger view is created. On the
;; right there is the buffer that contain the tag data. Each line in this
;; buffer represents a tag with a value. Tag and value are separated by a
;; colon. Valid tags are "Title", "Artist", "Album", "Comment", "Genre", "Year"
;; and "Track". The value can be an arbitrary string. Whitespaces in front and
;; at the end of the value are ignored. If the value is "<keep>", the tag line
;; is ignored.
;;
;; To save the specified tag values back to the files, press `C-c C-c'
;; (ampc-tagger-save). To exit the tagger and restore the previous window
;; configuration, press `C-c C-q'. `C-u C-c C-c' saved the tags and exits the
;; tagger. Only tags that are actually specified within the tagger buffer
;; written back to the file. Other tags will not be touched by ampc. For
;; example, to clear the "Commentary" tag, you need to specify the line
;;
;; Commentary:
;;
;; In the tagger buffer. Omitting this line will make the tagger not touch the
;; "Commentary" tag at all.
;;
;; On the right there is the files list buffer. The selection of this buffer
;; specifies which files the command `ampc-tag-save' will write to. If no file
;; is selected, the file at point in the file list buffer is used.
;;
;; To reset the values of the tags specified in the tagger buffer to the common
;; values of all selected files specified by the selection of the files list
;; buffer, press `C-c C-r' (ampc-tagger-reset). With a prefix argument,
;; `ampc-tagger-reset' restores missing tags as well.
;;
;; You can use tab-completion within the tagger buffer for both tags and tag
;; values.
;;
;; You can also use the tagging subsystem on its own without a running ampc
;; instance. To start the tagger, call `ampc-tag-files'. This function accepts
;; one argument, a list of absolute file names which are the files to tag. ampc
;; provides a minor mode for dired, `ampc-tagger-dired-mode'. If this mode is
;; enabled within a dired buffer, pressing `C-c C-t' (ampc-tagger-dired) will
;; start the tagger on the current selection.
;;
;; The following ampc-specific hooks are run during tagger usage:
;;
;; `ampc-tagger-grab-hook': Run by the tagger before grabbing tags of a file.
;; Each function is called with one argument, the file name.
;;
;; `ampc-tagger-grabbed-hook': Run by the tagger after grabbing tags of a file.
;; Each function is called with one argument, the file name.
;;
;; `ampc-tagger-store-hook': Run by the tagger before writing tags back to a
;; file. Each function is called with two arguments, FOUND-CHANGED and DATA.
;; FOUND-CHANGED is non-nil if the tags that are about to be written differ from
;; the ones in the file. DATA is a cons. The car specifies the full file name
;; of the file that is about to be written to, the cdr is an alist that
;; specifies the tags that are about to be (over-)written. The car of each
;; entry in this list is a symbol specifying the tag (one of the ones in
;; `ampc-tagger-tags'), the cdr a string specifying the value. The cdr of DATA
;; may be modified. If FOUND-CHANGED is nil and the cdr of DATA is not modified
;; throughout the hook is run, the file is not touched.
;; `ampc-tagger-stored-hook' is still run, though.
;;
;; `ampc-tagger-stored-hook': Run by the tagger after writing tags back to a
;; file. Each function is called with two arguments, FOUND-CHANGED and DATA.
;; These are the same arguments that were already passed to
;; `ampc-tagger-store-hook'. The car of DATA, the file name, may be modified.
;;
;; These hooks can be used to handle vc locking and unlocking of files. For
;; renaming files according to their (new) tag values, ampc provides the
;; function `ampc-tagger-rename-artist-title' which may be added to
;; `ampc-tagger-stored-hook'. The new file name generated by this function is
;; "Artist"_-_"Title"."extension". Characters within "Artist" and "Title" that
;; are not alphanumeric are substituted with underscores.
;;; ** global keys
;; Aside from `J', `M', `K', `<' and `L', which may be used to select different
;; views, and `I' which starts the tagger, ampc defines the following global
;; keys. These binding are available in every buffer associated with ampc:
;;
;; `k' (ampc-toggle-play): Toggle play state. If MPD does not play a song,
;; start playing the song at point if the current buffer is the playlist buffer,
;; otherwise start at the beginning of the playlist. With numeric prefix
;; argument 4, stop player rather than pause if applicable.
;;
;; `l' (ampc-next): Play next song.
;; `j' (ampc-previous): Play previous song
;;
;; `c' (ampc-clear): Clear playlist.
;; `s' (ampc-shuffle): Shuffle playlist.
;;
;; `S' (ampc-store): Store playlist.
;; `O' (ampc-load): Load selected playlist into the current playlist.
;; `R' (ampc-rename-playlist): Rename selected playlist.
;; `D' (ampc-delete-playlist): Delete selected playlist.
;;
;; `y' (ampc-increase-volume): Increase volume.
;; `M-y' (ampc-decrease-volume): Decrease volume.
;; `C-M-y' (ampc-set-volume): Set volume.
;; `h' (ampc-increase-crossfade): Increase crossfade.
;; `M-h' (ampc-decrease-crossfade): Decrease crossfade.
;; `C-M-h' (ampc-set-crossfade): Set crossfade.
;;
;; `e' (ampc-toggle-repeat): Toggle repeat state.
;; `r' (ampc-toggle-random): Toggle random state.
;; `f' (ampc-toggle-consume): Toggle consume state.
;;
;; `P' (ampc-goto-current-song): Select the current playlist window and move
;; point to the current song.
;; `G' (ampc-mini): Select song to play via `completing-read'.
;;
;; `T' (ampc-trigger-update): Trigger a database update.
;; `Z' (ampc-suspend): Suspend ampc.
;; `q' (ampc-quit): Quit ampc.
;;
;; The keymap of ampc is designed to fit the QWERTY United States keyboard
;; layout. If you use another keyboard layout, feel free to modify
;; `ampc-mode-map'. For example, I use a regular QWERTZ German keyboard
;; (layout), so I modify `ampc-mode-map' in my init.el like this:
;;
;; (eval-after-load 'ampc
;; '(flet ((substitute-ampc-key
;; (from to)
;; (define-key ampc-mode-map to (lookup-key ampc-mode-map from))
;; (define-key ampc-mode-map from nil)))
;; (substitute-ampc-key (kbd "z") (kbd "Z"))
;; (substitute-ampc-key (kbd "y") (kbd "z"))
;; (substitute-ampc-key (kbd "M-y") (kbd "M-z"))
;; (substitute-ampc-key (kbd "C-M-y") (kbd "C-M-z"))
;; (substitute-ampc-key (kbd "<") (kbd ";"))))
;;
;; If ampc is suspended, you can still use every interactive command that does
;; not directly operate on or with the user interace of ampc. For example it is
;; perfectly fine to call `ampc-increase-volume' or `ampc-toggle-play' via M-x
;; RET. Especially the commands `ampc-status' and `ampc-mini' are predesignated
;; to be bound in the global keymap and called when ampc is suspended.
;; `ampc-status' messages the information that is displayed by the status window
;; of ampc. `ampc-mini' lets you select a song to play via `completing-read'.
;; To start ampc suspended, call `ampc' with the third argument being non-nil.
;; To check whether ampc is connected to the daemon and/or suspended, call
;; `ampc-is-on-p' or `ampc-suspended-p'.
;;
;; (global-set-key (kbd "<f7>")
;; (lambda ()
;; (interactive)
;; (unless (ampc-on-p)
;; (ampc nil nil t))
;; (ampc-status)))
;; (global-set-key (kbd "<f8>")
;; (lambda ()
;; (interactive)
;; (unless (ampc-on-p)
;; (ampc nil nil t))
;; (ampc-mini)))
;;; Code:
;;; * code
(eval-when-compile
(require 'cl))
(require 'network-stream)
(require 'avl-tree)
;;; ** declarations
(defgroup ampc ()
"Asynchronous client for the Music Player Daemon."
:prefix "ampc-"
:group 'multimedia
:group 'applications)
;;; *** customs
(defcustom ampc-debug nil
"Non-nil means log outgoing communication between ampc and MPD.
If the value is neither t nor nil, also log incoming data."
:type '(choice (const :tag "Disable" nil)
(const :tag "Outgoing" t)
(const :tag "Incoming and outgoing" full)))
(defcustom ampc-use-full-frame nil
"If non-nil, ampc will use the entire Emacs screen."
:type 'boolean)
(defcustom ampc-truncate-lines t
"If non-nil, truncate lines in ampc buffers."
:type 'boolean)
(defcustom ampc-default-server '("localhost" . 6600)
"The MPD server to connect to if the arguments to `ampc' are nil.
This variable is a cons cell, with the car specifying the
hostname and the cdr specifying the port. Both values can be
nil, which will make ampc query the user for values on each
invocation."
:type '(cons (choice :tag "Hostname"
(string)
(const :tag "Ask" nil))
(choice :tag "Port"
(string)
(integer)
(const :tag "Ask" nil))))
(defcustom ampc-synchronous-commands '(t status currentsong play)
"List of MPD commands that should be executed synchronously.
Executing commands that print lots of output synchronously will
result in massive performance improvements of ampc. If the car
of this list is t, execute all commands synchronously other
than the ones specified by the rest of the list."
:type '(repeat symbol))
(defcustom ampc-status-tags nil
"List of additional tags of the current song that are added to
the internal status of ampc and thus are passed to the functions
in `ampc-status-changed-hook'. Each element may be a string that
specifies a tag that is returned by MPD's `currentsong'
command."
:type '(list symbol))
(defcustom ampc-volume-step 5
"Default step of `ampc-increase-volume' and
`ampc-decrease-volume' for changing the volume."
:type 'integer)
(defcustom ampc-crossfade-step 5
"Default step of `ampc-increase-crossfade' and
`ampc-decrease-crossfade' for changing the crossfade."
:type 'integer)
(defcustom ampc-tag-transform-funcs '(("Time" . ampc-transform-time)
("Track" . ampc-transform-track))
"Alist of tag treatment functions.
The car, a string, of each entry specifies the MPD tag, the cdr a
function which transforms the tag to the value that should be
used by ampc. The function is called with one string argument,
the tag value, and should return the treated value."
:type '(alist :key-type string :value-type function))
(defcustom ampc-tagger-music-directories nil
"List of base directories in which your music files are located.
Usually this list should have only one entry, the value of your
mpd.conf's `music_directory'"
:type '(list directory))
(defcustom ampc-tagger-executable "ampc_tagger"
"The name or full path to ampc's tagger executable."
:type 'string)
(defcustom ampc-tagger-backup-directory
(file-name-directory (locate-user-emacs-file "ampc-backups/"))
"The directory in which the tagger copies files before modifying.
If nil, disable backups."
:type '(choice (const :tag "Disable backups" nil)
(directory :tag "Directory")))
;;; **** hooks
(defcustom ampc-before-startup-hook nil
"A hook run before startup.
This hook is called as the first thing when ampc is started."
:type 'hook)
(defcustom ampc-connected-hook nil
"A hook run after ampc connected to MPD."
:type 'hook)
(defcustom ampc-suspend-hook nil
"A hook run when suspending ampc."
:type 'hook)
(defcustom ampc-quit-hook nil
"A hook run when exiting ampc."
:type 'hook)
(defcustom ampc-status-changed-hook nil
"A hook run whenever the status of the daemon (that is volatile
properties such as volume or current song) changes. The hook is
run with one arg, an alist that contains the new status. The car
of each entry is a symbol, the cdr is a string. Valid keys are:
volume
repeat
random
consume
xfade
state
song
Artist
Title
and the keys in `ampc-status-tags'. Not all keys may be present
all the time!"
:type 'hook)
(defcustom ampc-tagger-grab-hook nil
"Hook run by the tagger before grabbing tags of a file.
Each function is called with one argument, the file name."
:type 'hook)
(defcustom ampc-tagger-grabbed-hook nil
"Hook run by the tagger after grabbing tags of a file.
Each function is called with one argument, the file name."
:type 'hook)
(defcustom ampc-tagger-store-hook nil
"Hook run by the tagger before writing tags back to a file.
Each function is called with two arguments, FOUND-CHANGED and
DATA. FOUND-CHANGED is non-nil if the tags that are about to be
written differ from the ones in the file. DATA is a cons. The
car specifies the full file name of the file that is about to be
written to, the cdr is an alist that specifies the tags that are
about to be (over-)written. The car of each entry in this list
is a symbol specifying the tag (one of the ones in
`ampc-tagger-tags'), the cdr a string specifying the value. The
cdr of DATA may be modified. If FOUND-CHANGED is nil and the cdr
of DATA is not modified throughout the hook is run, the file is
not touched. `ampc-tagger-stored-hook' is still run, though."
:type 'hook)
(defcustom ampc-tagger-stored-hook nil
"Hook run by the tagger after writing tags back to a file.
Each function is called with two arguments, FOUND-CHANGED and
DATA. These are the same arguments that were already passed to
`ampc-tagger-store-hook'. The car of DATA, the file name, may be
modified."
:type 'hook)
;;; *** faces
(defface ampc-mark-face '((t (:inherit font-lock-constant-face)))
"Face of the mark.")
(defface ampc-marked-face '((t (:inherit warning)))
"Face of marked entries.")
(defface ampc-unmarked-face '((t (:inerhit default)))
"Face of unmarked entries.")
(defface ampc-current-song-mark-face '((t (:inherit region)))
"Face of mark of the current song.")
(defface ampc-current-song-marked-face '((t (:inherit region)))
"Face of the current song if marked.")
(defface ampc-tagger-tag-face '((t (:inherit font-lock-constant-face)))
"Face of tags within the tagger.")
(defface ampc-tagger-keyword-face '((t (:inherit font-lock-keyword-face)))
"Face of tags within the tagger.")
;;; *** internal variables
(defvar ampc-views
(let* ((songs '(1.0 song :properties (("Track" :title "#" :width 4)
("Title" :min 15 :max 40)
("Time" :width 6)
("Artist" :min 15 :max 40)
("Album" :min 15 :max 40))))
(rs_a `(1.0 vertical
(0.7 horizontal
(0.33 tag :tag "Genre" :id 1 :select t)
(0.33 tag :tag "Artist" :id 2)
(1.0 tag :tag "Album" :id 3))
,songs))
(rs_b `(1.0 vertical
(0.7 horizontal
(0.33 tag :tag "Genre" :id 1 :select t)
(0.33 tag :tag "Album" :id 2)
(1.0 tag :tag "Artist" :id 3))
,songs))
(pl-prop '(:properties (("Title" :min 15 :max 40)
("Artist" :min 15 :max 40)
("Album" :min 15 :max 40)
("Time" :width 6)))))
`((tagger
horizontal
(0.65 files-list
:properties ((filename :shrink t :title "File" :min 20 :max 40)
("Title" :min 15 :max 40)
("Artist" :min 15 :max 40)
("Album" :min 15 :max 40)
("Genre" :min 15 :max 40)
("Year" :width 5)
("Track" :title "#" :width 4)
("Comment" :min 15 :max 40))
:dedicated nil)
(1.0 tagger))
("Current playlist view (Genre|Artist|Album)"
,(kbd "J")
horizontal
(0.4 vertical
(6 status)
(1.0 current-playlist ,@pl-prop))
,rs_a)
("Current playlist view (Genre|Album|Artist)"
,(kbd "M")
horizontal
(0.4 vertical
(6 status)
(1.0 current-playlist ,@pl-prop))
,rs_b)
("Playlist view (Genre|Artist|Album)"
,(kbd "K")
horizontal
(0.4 vertical
(6 status)
(1.0 vertical
(0.4 current-playlist ,@pl-prop)
(0.4 playlist ,@pl-prop)
(1.0 playlists)))
,rs_a)
("Playlist view (Genre|Album|Artist)"
,(kbd "<")
horizontal
(0.4 vertical
(6 status)
(1.0 vertical
(0.4 current-playlist ,@pl-prop)
(0.4 playlist ,@pl-prop)
(1.0 playlists)))
,rs_b)
("Outputs view"
,(kbd "L")
outputs :properties (("outputname" :title "Name" :min 10 :max 30)
("outputenabled" :title "Enabled" :width 9))))))
(defvar ampc-connection nil)
(defvar ampc-host nil)
(defvar ampc-port nil)
(defvar ampc-outstanding-commands nil)
(defvar ampc-no-implicit-next-dispatch nil)
(defvar ampc-working-timer nil)
(defvar ampc-yield nil)
(defvar ampc-yield-redisplay nil)
(defvar ampc-windows nil)
(defvar ampc-all-buffers nil)
(defvar ampc-type nil)
(make-variable-buffer-local 'ampc-type)
(defvar ampc-dirty nil)
(make-variable-buffer-local 'ampc-dirty)
(defvar ampc-internal-db nil)
(defvar ampc-status nil)
(defvar ampc-tagger-previous-configuration nil)
(defvar ampc-tagger-version-verified nil)
(defvar ampc-tagger-completion-all-files nil)
(defvar ampc-tagger-genres nil)
(defconst ampc-tagger-version "0.1")
(defconst ampc-tagger-tags '(Title Artist Album Comment Genre Year Track))
;;; *** mode maps
(defvar ampc-mode-map
(let ((map (make-sparse-keymap)))
(suppress-keymap map)
(define-key map (kbd "k") 'ampc-toggle-play)
(define-key map (kbd "l") 'ampc-next)
(define-key map (kbd "j") 'ampc-previous)
(define-key map (kbd "c") 'ampc-clear)
(define-key map (kbd "s") 'ampc-shuffle)
(define-key map (kbd "S") 'ampc-store)
(define-key map (kbd "O") 'ampc-load)
(define-key map (kbd "R") 'ampc-rename-playlist)
(define-key map (kbd "D") 'ampc-delete-playlist)
(define-key map (kbd "y") 'ampc-increase-volume)
(define-key map (kbd "M-y") 'ampc-decrease-volume)
(define-key map (kbd "C-M-y") 'ampc-set-volume)
(define-key map (kbd "h") 'ampc-increase-crossfade)
(define-key map (kbd "M-h") 'ampc-decrease-crossfade)
(define-key map (kbd "C-M-h") 'ampc-set-crossfade)
(define-key map (kbd "e") 'ampc-toggle-repeat)
(define-key map (kbd "r") 'ampc-toggle-random)
(define-key map (kbd "f") 'ampc-toggle-consume)
(define-key map (kbd "P") 'ampc-goto-current-song)
(define-key map (kbd "G") 'ampc-mini)
(define-key map (kbd "q") 'ampc-quit)
(define-key map (kbd "z") 'ampc-suspend)
(define-key map (kbd "T") 'ampc-trigger-update)
(define-key map (kbd "I") 'ampc-tagger)
(loop for view in ampc-views
do (when (stringp (car view))
(define-key map (cadr view)
`(lambda ()
(interactive)
(ampc-change-view ',view)))))
map))
(defvar ampc-item-mode-map
(let ((map (make-sparse-keymap)))
(suppress-keymap map)
(define-key map (kbd "m") 'ampc-mark)
(define-key map (kbd "u") 'ampc-unmark)
(define-key map (kbd "U") 'ampc-unmark-all)
(define-key map (kbd "n") 'ampc-next-line)
(define-key map (kbd "p") 'ampc-previous-line)
(define-key map (kbd "<down-mouse-1>") 'ampc-mouse-toggle-mark)
(define-key map (kbd "<mouse-1>") 'ampc-mouse-align-point)
(define-key map [remap next-line] 'ampc-next-line)
(define-key map [remap previous-line] 'ampc-previous-line)
(define-key map [remap tab-to-tab-stop] 'ampc-move-to-tab)
map))
(defvar ampc-current-playlist-mode-map
(let ((map (make-sparse-keymap)))
(suppress-keymap map)
(define-key map (kbd "<return>") 'ampc-play-this)
(define-key map (kbd "<down-mouse-2>") 'ampc-mouse-play-this)
(define-key map (kbd "<mouse-2>") 'ampc-mouse-align-point)
(define-key map (kbd "<down-mouse-3>") 'ampc-mouse-delete)
(define-key map (kbd "<mouse-3>") 'ampc-mouse-align-point)
map))
(defvar ampc-playlist-mode-map
(let ((map (make-sparse-keymap)))
(suppress-keymap map)
(define-key map (kbd "t") 'ampc-toggle-marks)
(define-key map (kbd "d") 'ampc-delete)
(define-key map (kbd "<up>") 'ampc-up)
(define-key map (kbd "<down>") 'ampc-down)
(define-key map (kbd "<down-mouse-3>") 'ampc-mouse-delete)
(define-key map (kbd "<mouse-3>") 'ampc-mouse-align-point)
map))
(defvar ampc-playlists-mode-map
(let ((map (make-sparse-keymap)))
(suppress-keymap map)
(define-key map (kbd "l") 'ampc-load)
(define-key map (kbd "r") 'ampc-rename-playlist)
(define-key map (kbd "d") 'ampc-delete-playlist)
(define-key map (kbd "<down-mouse-2>") 'ampc-mouse-load)
(define-key map (kbd "<mouse-2>") 'ampc-mouse-align-point)
(define-key map (kbd "<down-mouse-3>") 'ampc-mouse-delete-playlist)
(define-key map (kbd "<mouse-3>") 'ampc-mouse-align-point)
map))
(defvar ampc-tag-song-mode-map
(let ((map (make-sparse-keymap)))
(suppress-keymap map)
(define-key map (kbd "t") 'ampc-toggle-marks)
(define-key map (kbd "a") 'ampc-add)
(define-key map (kbd "<down-mouse-3>") 'ampc-mouse-add)
(define-key map (kbd "<mouse-3>") 'ampc-mouse-align-point)
map))
(defvar ampc-outputs-mode-map
(let ((map (make-sparse-keymap)))
(suppress-keymap map)
(define-key map (kbd "t") 'ampc-toggle-marks)
(define-key map (kbd "a") 'ampc-toggle-output-enabled)
(define-key map (kbd "<down-mouse-3>") 'ampc-mouse-toggle-output-enabled)
(define-key map (kbd "<mouse-3>") 'ampc-mouse-align-point)
map))
(defvar ampc-files-list-mode-map
(let ((map (make-sparse-keymap)))
(suppress-keymap map)
(define-key map (kbd "t") 'ampc-toggle-marks)
(define-key map (kbd "C-c C-q") 'ampc-tagger-quit)
(define-key map (kbd "C-c C-c") 'ampc-tagger-save)
(define-key map (kbd "C-c C-r") 'ampc-tagger-reset)
(define-key map [remap ampc-tagger] nil)
(define-key map [remap ampc-quit] 'ampc-tagger-quit)
(loop for view in ampc-views
do (when (stringp (car view))
(define-key map (cadr view) nil)))
map))
(defvar ampc-tagger-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-q") 'ampc-tagger-quit)
(define-key map (kbd "C-c C-c") 'ampc-tagger-save)
(define-key map (kbd "C-c C-r") 'ampc-tagger-reset)
(define-key map (kbd "<tab>") 'ampc-tagger-completion-at-point)
map))
(defvar ampc-tagger-dired-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-t") 'ampc-tagger-dired)
map))
;;; **** menu
(easy-menu-define nil ampc-mode-map nil
`("ampc"
("Change view" ,@(loop for view in ampc-views
when (stringp (car view))
collect (vector (car view)
`(lambda ()
(interactive)
(ampc-change-view ',view)))
end))
["Run tagger" ampc-tagger]
"--"
["Play" ampc-toggle-play
:visible (and ampc-status
(not (equal (cdr (assq 'state ampc-status)) "play")))]
["Pause" ampc-toggle-play
:visible (and ampc-status
(equal (cdr (assq 'state ampc-status)) "play"))]
["Stop" (lambda () (interactive) (ampc-toggle-play 4))
:visible (and ampc-status
(equal (cdr (assq 'state ampc-status)) "play"))]
["Next" ampc-next]
["Previous" ampc-previous]
"--"
["Clear playlist" ampc-clear]
["Shuffle playlist" ampc-shuffle]
["Store playlist" ampc-store]
["Queue Playlist" ampc-load :visible (ampc-playlist)]
["Rename Playlist" ampc-rename-playlist :visible (ampc-playlist)]
["Delete Playlist" ampc-delete-playlist :visible (ampc-playlist)]
"--"
["Increase volume" ampc-increase-volume]
["Decrease volume" ampc-decrease-volume]
["Set volume" ampc-set-volume]
["Increase crossfade" ampc-increase-crossfade]
["Decrease crossfade" ampc-decrease-crossfade]
["Set crossfade" ampc-set-crossfade]
["Toggle repeat" ampc-toggle-repeat
:style toggle
:selected (equal (cdr (assq 'repeat ampc-status)) "1")]
["Toggle random" ampc-toggle-random
:style toggle
:selected (equal (cdr (assq 'random ampc-status)) "1")]
["Toggle consume" ampc-toggle-consume
:style toggle
:selected (equal (cdr (assq 'consume ampc-status)) "1")]
"--"
["Trigger update" ampc-trigger-update]
["Suspend" ampc-suspend]
["Quit" ampc-quit]))
(easy-menu-define ampc-selection-menu ampc-item-mode-map
"Selection menu for ampc"
'("ampc Mark"
["Add to playlist" ampc-add
:visible (not (eq (car ampc-type) 'outputs))]
["Toggle enabled" ampc-toggle-output-enabled
:visible (eq (car ampc-type) 'outputs)]
"--"
["Next line" ampc-next-line]
["Previous line" ampc-previous-line]
["Mark" ampc-mark]
["Unmark" ampc-unmark]
["Unmark all" ampc-unmark-all]
["Toggle marks" ampc-toggle-marks
:visible (not (eq (car ampc-type) 'playlists))]))
(defvar ampc-tool-bar-map
(let ((map (make-sparse-keymap)))
(tool-bar-local-item
"mpc/prev" 'ampc-previous 'previous map
:help "Previous")
(tool-bar-local-item
"mpc/play" 'ampc-toggle-play 'play map
:help "Play"
:visible '(and ampc-status
(not (equal (cdr (assq 'state ampc-status)) "play"))))
(tool-bar-local-item
"mpc/pause" 'ampc-toggle-play 'pause map
:help "Pause"
:visible '(and ampc-status
(equal (cdr (assq 'state ampc-status)) "play")))
(tool-bar-local-item
"mpc/stop" (lambda () (interactive) (ampc-toggle-play 4)) 'stop map
:help "Stop"
:visible '(and ampc-status
(equal (cdr (assq 'state ampc-status)) "play")))
(tool-bar-local-item
"mpc/next" 'ampc-next 'next map
:help "Next")
map))
;;; ** code
;;; *** macros
(defmacro ampc-with-buffer (type &rest body)
(declare (indent 1) (debug t))
`(let* ((type- ,type)
(w (if (windowp type-)
type-
(loop for w in (ampc-normalize-windows)
thereis (when (with-current-buffer
(window-buffer w)
(etypecase type-
(symbol (eq (car ampc-type) type-))
(cons (equal ampc-type type-))))
w)))))
(when w
(with-selected-window w
(with-current-buffer (window-buffer w)
(let ((inhibit-read-only t))
,@(if (eq (car body) 'no-se)
(cdr body)
`((save-excursion
(goto-char (point-min))
,@body)))))))))
(defmacro ampc-fill-skeleton (tag &rest body)
(declare (indent 1) (debug t))
`(let ((tag- ,tag)
(data-buffer (current-buffer)))
(ampc-with-buffer tag-
no-se
(unless (eq ampc-dirty 'keep-dirty)
(let ((old-point-data (get-text-property (point) 'cmp-data))
(old-window-start-offset
(1- (count-lines (window-start) (point)))))
(put-text-property (point-min) (point-max) 'not-updated t)
(when (eq ampc-dirty 'erase)
(put-text-property (point-min) (point-max) 'data nil))
(goto-char (point-min))
,@body
(goto-char (point-min))
(loop until (eobp)
do (if (get-text-property (point) 'not-updated)
(kill-line 1)
(add-text-properties (+ (point) 2)
(progn (forward-line nil)
(1- (point)))
'(mouse-face highlight))))
(remove-text-properties (point-min) (point-max) '(not-updated))
(goto-char (point-min))
(when old-point-data
(loop until (eobp)
do (when (equal (get-text-property (point) 'cmp-data)
old-point-data)
(set-window-start
nil
(save-excursion
(forward-line (- old-window-start-offset))
(point))
t)
(return))
(forward-line)
finally do (goto-char (point-min)))))
(let ((effective-height (- (window-height)
(if mode-line-format 1 0)
(if header-line-format 1 0))))
(when (< (- (1- (line-number-at-pos (point-max)))
(line-number-at-pos (window-start)))
effective-height)
(set-window-start nil
(save-excursion
(goto-char (point-max))
(forward-line (- (1+ effective-height)))
(point))
t)))
(ampc-align-point)
(ampc-set-dirty nil)))))
(defmacro ampc-with-selection (arg &rest body)
(declare (indent 1) (debug t))
`(let ((arg- ,arg))
(if (or (and (not arg-)
(save-excursion
(goto-char (point-min))
(search-forward-regexp "^* " nil t)))
(and arg- (symbolp arg-)))
(loop initially do (goto-char (point-min))
finally do (ampc-align-point)
while (search-forward-regexp "^* " nil t)
for index from 0
do (save-excursion
,@body))
(setf arg- (prefix-numeric-value arg-))
(ampc-align-point)
(loop until (eobp)
for index from 0 to (1- (abs arg-))
do (save-excursion
,@body)
until (if (< arg- 0) (ampc-previous-line) (ampc-next-line))))))
(defmacro ampc-iterate-source (data-buffer delimiter bindings &rest body)
(declare (indent 3) (debug t))
(when (memq (intern delimiter) bindings)
(callf2 delq (intern delimiter) bindings)
(push (list (intern delimiter)
'(buffer-substring (point) (line-end-position)))
bindings))
`(,@(if data-buffer `(with-current-buffer ,data-buffer) '(progn))
(when (search-forward-regexp
,(concat "^" (regexp-quote delimiter) ": ")
nil t)
(loop with next
do (save-restriction
(setf next (ampc-narrow-entry
,(concat "^" (regexp-quote delimiter) ": ")))
(let ,(loop for binding in bindings
if (consp binding)
collect binding
else
collect `(,binding (ampc-extract
(ampc-extract-regexp
,(symbol-name binding))))
end)
,@body))
while next
do (goto-char next)))))
(defmacro ampc-iterate-source-output (delimiter bindings pad-data &rest body)
(declare (indent 2) (debug t))
`(let ((output-buffer (current-buffer))
(tags (loop for (tag . props) in
(plist-get (cdr ampc-type) :properties)
collect (cons tag (ampc-extract-regexp tag)))))
(ampc-iterate-source
data-buffer ,delimiter ,bindings
(let ((pad-data ,pad-data))
(with-current-buffer output-buffer
(ampc-insert (ampc-pad pad-data) ,@body))))))
(defmacro ampc-extract-regexp (tag)
(if (stringp tag)
(concat "^" (regexp-quote tag) ": \\(.*\\)$")
`(concat "^" (regexp-quote ,tag) ": \\(.*\\)$")))
(defmacro ampc-tagger-log (&rest what)
(declare (indent 0) (debug t))
`(with-current-buffer (get-buffer-create "*Tagger Log*")
(ampc-tagger-log-mode)
(save-excursion
(goto-char (point-max))
(let ((inhibit-read-only t)
(what (concat ,@what)))
(when ampc-debug
(message "ampc: %s" what))
(insert what)))))
;;; *** modes
(define-derived-mode ampc-outputs-mode ampc-item-mode "ampc-o")
(define-derived-mode ampc-tag-song-mode ampc-item-mode "ampc-ts")
(define-derived-mode ampc-current-playlist-mode ampc-playlist-mode "ampc-cpl"
(ampc-highlight-current-song-mode))
(define-derived-mode ampc-playlist-mode ampc-item-mode "ampc-pl")
(define-derived-mode ampc-playlists-mode ampc-item-mode "ampc-pls")
(define-derived-mode ampc-files-list-mode ampc-item-mode "ampc-files-list")
(define-derived-mode ampc-tagger-mode nil "ampc-tagger"
(set (make-local-variable 'tool-bar-map) ampc-tool-bar-map)
(set (make-local-variable 'tab-stop-list)
(list (+ (loop for tag in ampc-tagger-tags
maximize (length (symbol-name tag)))
2)))
(set (make-local-variable 'completion-at-point-functions)