-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemms.el
1728 lines (1514 loc) · 57.6 KB
/
emms.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
;;; emms.el --- The Emacs Multimedia System -*- lexical-binding: t; -*-
;; Copyright (C) 2003-2022 Free Software Foundation, Inc.
;; Author: Jorgen Schäfer <[email protected]>, the Emms developers (see AUTHORS file)
;; Maintainer: Yoni Rabkin <[email protected]>
;; Version: 21
;; Keywords: emms, mp3, ogg, flac, music, mpeg, video, multimedia
;; Package-Type: multi
;; Package-Requires: ((cl-lib "0.5") (nadvice "0.3") (seq))
;; url: https://www.gnu.org/software/emms/
;; This file is part of EMMS.
;; EMMS 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, or (at your option)
;; any later version.
;; EMMS is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
;; License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This is the very core of EMMS. It provides ways to play a track
;; using `emms-start', to go through the playlist using the commands
;; `emms-next' and `emms-previous', to stop the playback using
;; `emms-stop', and to see what's currently playing using `emms-show'.
;; But in itself, this core is useless, because it doesn't know how to
;; play any tracks --- you need players for this. In fact, it doesn't
;; even know how to find any tracks to consider playing --- for this,
;; you need sources.
;; A sample configuration is offered in emms-setup.el, and the
;; Friendly Manual in the doc/ directory is both detailed, and kept up
;; to date.
;;; Code:
(require 'emms-compat)
(require 'seq)
(defvar emms-version "21"
"EMMS version string.")
;;; User Customization
(defgroup emms nil
"*The Emacs Multimedia System."
:prefix "emms-"
:group 'multimedia
:group 'applications)
(defgroup emms-player nil
"*Track players for EMMS."
:prefix "emms-player-"
:group 'emms)
(defgroup emms-source nil
"*Track sources for EMMS."
:prefix "emms-source-"
:group 'emms)
(defcustom emms-player-list nil
"*List of players that EMMS can use."
:group 'emms
:type '(repeat (symbol :tag "Player")))
(defcustom emms-show-format "Currently playing: %s"
"*The format to use for `emms-show'.
Any \"%s\" is replaced by what `emms-track-description-function' returns
for the currently playing track."
:group 'emms
:type 'string)
(defcustom emms-repeat-playlist nil
"*Non-nil if the EMMS playlist should automatically repeat.
If nil, playback will stop when the last track finishes playing.
If non-nil, EMMS will wrap back to the first track when that happens."
:group 'emms
:type 'boolean)
(defcustom emms-player-next-function 'emms-next-noerror
"*A function run when EMMS thinks the next song should be played."
:group 'emms
:type 'function
:options '(emms-next-noerror
emms-random))
(defcustom emms-random-playlist nil
"*Non-nil means that tracks are played randomly. If nil, tracks
are played sequentially."
:group 'emms
:set (lambda (symbol value)
(set symbol value)
(if value
(setq emms-player-next-function #'emms-random)
(setq emms-player-next-function #'emms-next-noerror)))
:type 'boolean)
(defvar-local emms-repeat-track nil
"Non-nil, playback will repeat current track. If nil, EMMS will play
track by track normally.")
(defvar-local emms-single-track nil
"Non-nil, play the current track and then stop.")
(defcustom emms-completing-read-function
(if (and (boundp 'ido-mode)
ido-mode)
'ido-completing-read
'completing-read)
"Function to call when prompting user to choose between a list of options.
This should take the same arguments as `completing-read'. Some
possible values are `completing-read' and `ido-completing-read'.
Note that you must set `ido-mode' if using
`ido-completing-read'."
:group 'emms
:type 'function)
(defcustom emms-track-description-function 'emms-track-simple-description
"*Function for describing an EMMS track in a user-friendly way."
:group 'emms
:type 'function)
(defcustom emms-player-delay 0
"The delay to pause after a player finished.
This is a floating-point number of seconds. This is necessary
for some platforms where it takes a bit to free the audio device
after a player has finished. If EMMS is skipping songs, increase
this number."
:type 'number
:group 'emms)
(defcustom emms-playlist-shuffle-function 'emms-playlist-simple-shuffle
"*The function to use for shuffling the playlist."
:type 'function
:group 'emms)
(defcustom emms-playlist-sort-function 'emms-playlist-simple-sort
"*The function to use for sorting the playlist."
:type 'function
:group 'emms)
(defcustom emms-playlist-uniq-function 'emms-playlist-simple-uniq
"*The function to use for removing duplicate tracks in the playlist."
:type 'function
:group 'emms)
(defcustom emms-sort-lessp-function 'emms-sort-track-name-less-p
"*Function for comparing two EMMS tracks.
The function should return non-nil if and only if the first track
sorts before the second (see `sort')."
:group 'emms
:type 'function)
(defcustom emms-playlist-buffer-name " *EMMS Playlist*"
"*The default name of the EMMS playlist buffer."
:type 'string
:group 'emms)
(defcustom emms-playlist-default-major-mode 'emms-playlist-mode
"*The default major mode for EMMS playlist."
:type 'function
:group 'emms)
(defcustom emms-playlist-insert-track-function 'emms-playlist-simple-insert-track
"*A function to insert a track into the playlist buffer."
:group 'emms
:type 'function)
(make-variable-buffer-local 'emms-playlist-insert-track-function)
(defcustom emms-playlist-update-track-function 'emms-playlist-simple-update-track
"*A function to update the track at point.
This is called when the track information changed. This also
shouldn't assume that the track has been inserted before."
:group 'emms
:type 'function)
(make-variable-buffer-local 'emms-playlist-insert-track-function)
(defcustom emms-playlist-delete-track-function 'emms-playlist-simple-delete-track
"*A function to delete the track at point in the playlist buffer."
:group 'emms
:type 'function)
(make-variable-buffer-local 'emms-playlist-delete-track-function)
(defcustom emms-ok-track-function 'emms-default-ok-track-function
"*Function returns true if we shouldn't skip this track."
:group 'emms
:type 'function)
(defcustom emms-playlist-source-inserted-hook nil
"*Hook run when a source got inserted into the playlist.
The buffer is narrowed to the new tracks."
:type 'hook
:group 'emms)
(defcustom emms-playlist-selection-changed-hook nil
"*Hook run after another track is selected in the EMMS playlist."
:group 'emms
:type 'hook)
(defcustom emms-playlist-cleared-hook nil
"*Hook run after the current EMMS playlist is cleared.
This happens both when the playlist is cleared and when a new
buffer is created for it."
:group 'emms
:type 'hook)
(defcustom emms-track-initialize-functions nil
"*List of functions to call for each new EMMS track.
This can be used to initialize tracks with various info."
:group 'emms
:type 'hook)
(defcustom emms-track-info-filters nil
"*List of functions to call when a track changes data, before updating
the display.
These functions are passed the track as an argument."
:group 'emms
:type 'hook)
(defcustom emms-track-updated-functions nil
"*List of functions to call when a track changes data, after updating
the display.
These functions are passed the track as an argument."
:group 'emms
:type 'hook)
(defcustom emms-player-started-hook nil
"*Hook run when an EMMS player starts playing."
:group 'emms
:type 'hook
:options '(emms-show))
(defcustom emms-player-stopped-hook nil
"*Hook run when an EMMS player is stopped by the user.
See `emms-player-finished-hook'."
:group 'emms
:type 'hook)
(defcustom emms-player-finished-hook nil
"*Hook run when an EMMS player finishes playing a track.
Please pay attention to the differences between
`emms-player-finished-hook' and `emms-player-stopped-hook'. The
former is called only when the player actually finishes playing a
track; the latter, only when the player is stopped
interactively."
:group 'emms
:type 'hook)
(defcustom emms-player-paused-hook nil
"*Hook run when a player is paused or resumed.
Use `emms-player-paused-p' to find the current state."
:group 'emms
:type 'hook)
(defcustom emms-seek-seconds 10
"The number of seconds to seek forward or backward when seeking."
:group 'emms
:type 'number)
(defcustom emms-player-seeked-functions nil
"*Functions called when a player is seeking.
The functions are called with a single argument, the amount of
seconds the player did seek."
:group 'emms
:type 'hook)
(defcustom emms-player-time-set-functions nil
"*Functions called when a player is setting the elapsed time of a track.
The functions are called with a single argument, the time elapsed
since the beginning of the current track."
:group 'emms
:type 'hook)
(defcustom emms-cache-get-function nil
"A function to retrieve a track entry from the cache.
This is called with two arguments, the type and the name."
:group 'emms
:type 'function)
(defcustom emms-cache-set-function nil
"A function to add/set a track entry from the cache.
This is called with three arguments: the type of the track, the
name of the track, and the track itself."
:group 'emms
:type 'function)
(defcustom emms-cache-modified-function nil
"A function to be called when a track is modified.
The modified track is passed as the argument to this function."
:group 'emms
:type 'function)
(defcustom emms-directory (expand-file-name "emms" user-emacs-directory)
"*Directory variable from which all other emms file variables are derived."
:group 'emms
:type 'string)
(defvar emms-player-playing-p nil
"The currently playing EMMS player, or nil.")
(defvar emms-player-paused-p nil
"Whether the current player is paused or not.")
(defvar emms-source-old-buffer nil
"The active buffer before a source was invoked.
This can be used if the source depends on the current buffer not
being the playlist buffer.")
(defvar emms-playlist-buffer nil
"The current playlist buffer, if any.")
(defvar emms-players-preference-f #'emms-players-default-preference-f
"Default function for player preference.")
;;; ------------------------------------------------------------------
;;; macros
;;; ------------------------------------------------------------------
;;; These need to be at the top of the file so that compilation works.
(defmacro with-current-emms-playlist (&rest body)
"Run BODY with the current buffer being the current playlist buffer.
This also disables any read-onliness of the current buffer."
`(progn
(when (or (not emms-playlist-buffer)
(not (buffer-live-p emms-playlist-buffer)))
(emms-playlist-current-clear))
(let ((emms-source-old-buffer (or emms-source-old-buffer
(current-buffer))))
(with-current-buffer emms-playlist-buffer
(let ((inhibit-read-only t))
,@body)))))
(put 'with-current-emms-playlist 'lisp-indent-function 0)
(put 'with-current-emms-playlist 'edebug-form-spec '(body))
(defmacro emms-with-inhibit-read-only-t (&rest body)
"Simple wrapper around `inhibit-read-only'."
`(let ((inhibit-read-only t))
,@body))
(put 'emms-with-inhibit-read-only-t 'edebug-form-spec '(body))
(defmacro emms-with-widened-buffer (&rest body)
`(save-restriction
(widen)
,@body))
(put 'emms-with-widened-buffer 'edebug-form-spec '(body))
(defmacro emms-walk-tracks (&rest body)
"Execute BODY for each track in the current buffer, starting at point.
Point will be placed at the beginning of the track before
executing BODY.
Point will not be restored afterward."
(let ((donep (make-symbol "donep")))
`(let ((,donep nil))
;; skip to first track if not on one
(unless (emms-playlist-track-at (point))
(condition-case nil
(emms-playlist-next)
(error
(setq ,donep t))))
;; walk tracks
(while (not ,donep)
,@body
(condition-case nil
(emms-playlist-next)
(error
(setq ,donep t)))))))
(put 'emms-walk-tracks 'lisp-indent-function 0)
(put 'emms-walk-tracks 'edebug-form-spec '(body))
(defvar emms-player-base-format-list
'("ogg" "mp3" "wav" "mpg" "mpeg" "wmv" "wma"
"mov" "avi" "divx" "ogm" "ogv" "asf" "mkv"
"rm" "rmvb" "mp4" "flac" "vob" "m4a" "ape"
"flv" "webm" "aif" "opus" "spc")
"A list of common formats which player definitions can use.")
;;; ------------------------------------------------------------------
;;; User Interface
;;; ------------------------------------------------------------------
(defun emms-start ()
"Start playing the current track in the EMMS playlist."
(interactive)
(unless emms-player-playing-p
(emms-player-start (emms-playlist-current-selected-track))))
(defun emms-stop ()
"Stop any current EMMS playback."
(interactive)
(when emms-player-playing-p
(emms-player-stop)))
(defun emms-next ()
"Start playing the next track in the EMMS playlist.
This might behave funny if called from `emms-player-next-function',
so use `emms-next-noerror' in that case."
(interactive)
(when emms-player-playing-p
(emms-stop))
(emms-playlist-current-select-next)
(emms-start))
(defun emms-next-noerror ()
"Start playing the next track in the EMMS playlist.
Unlike `emms-next', this function doesn't signal an error when called
at the end of the playlist.
This function should only be called when no player is playing.
This is a good function to put in `emms-player-next-function'."
(interactive)
(when emms-player-playing-p
(error "A track is already being played"))
(cond (emms-repeat-track
(emms-start))
(emms-single-track ; buffer local
(emms-stop))
;; attempt to play the next track but ignore errors
((condition-case nil
(progn
(emms-playlist-current-select-next)
t)
(error nil))
(if (funcall emms-ok-track-function
(emms-playlist-current-selected-track))
(emms-start)
(emms-next-noerror)))
(t
(message "No next track in playlist"))))
(defun emms-previous ()
"Start playing the previous track in the EMMS playlist."
(interactive)
(when emms-player-playing-p
(emms-stop))
(emms-playlist-current-select-previous)
(emms-start))
(defun emms-random ()
"Jump to a random track."
(interactive)
(when emms-player-playing-p
(emms-stop))
(emms-playlist-current-select-random)
(emms-start))
(defun emms-pause ()
"Pause the current player.
If player hasn't started, then start it now."
(interactive)
(if emms-player-playing-p
(emms-player-pause)
(emms-start)))
(defun emms-seek (duration)
"Seek the current player by DURATION from its current position.
DURATION can be:
- A single number, in which case it is interpreted as seconds.
- A string of form [-][HH:]MM:SS.m, where HH is hours, MM is
minutes, and SS is seconds.
In both forms seconds can be a floating point number. A negative
value seeks backwards."
(interactive "sDuration to seek: ")
(emms-ensure-player-playing-p)
(emms-player-seek (emms-timespec-to-secs duration)))
(defun emms-seek-to (timestamp)
"Seek the current player to TIMESTAMP.
TIMESTAMP can be:
- A single number, in which case it is interpreted as seconds.
- A string of form [HH:]MM:SS.m, where HH is hours, MM is
minutes, and SS is seconds.
In both forms seconds can be a floating point number."
(interactive "sTimestamp to seek to: ")
(emms-ensure-player-playing-p)
(emms-player-seek-to (max 0 (emms-timespec-to-secs timestamp))))
(defun emms-timespec-to-secs (timespec)
"Convert TIMESPEC to seconds.
If TIMESPEC is number, use it verbatim. If TIMESPEC is string,
use `emms-timestr-to-secs' for conversion. Otherwise return
zero."
(cond ((numberp timespec) timespec)
((stringp timespec) (emms-timestr-to-secs timespec))
(t 0)))
(defun emms-timestr-to-secs (timespec)
"Convert TIMESPEC to seconds.
TIMESPEC is assumed to be a string of form [-][[HH:]MM:]SS, where
HH is hours, MM is minutes and SS is seconds. Each element is
converted to number by calling `string-to-number'. Missing or
invalid elements are treated as zeros."
(let ((tokens (split-string timespec ":")))
(if (= (length tokens) 1)
;; seconds only
(string-to-number (car tokens))
;; HH:MM:SS
(let* ((sign (if (< (string-to-number (car tokens)) 0) -1 1))
(revtokens (reverse tokens))
(seconds (abs (string-to-number (or (pop revtokens) "0"))))
(minutes (abs (string-to-number (or (pop revtokens) "0"))))
(hours (abs (string-to-number (or (pop revtokens) "0")))))
(* sign (+ (* 60 60 hours) (* 60 minutes) seconds))))))
(defun emms-seek-forward ()
"Seek ten seconds forward."
(interactive)
(when emms-player-playing-p
(emms-player-seek emms-seek-seconds)))
(defun emms-seek-backward ()
"Seek ten seconds backward."
(interactive)
(when emms-player-playing-p
(emms-player-seek (- emms-seek-seconds))))
(defun emms-show (&optional insertp)
"Describe the current EMMS track in the minibuffer.
If INSERTP is non-nil, insert the description into the current buffer instead.
This function uses `emms-show-format' to format the current track."
(interactive "P")
(let ((string (if emms-player-playing-p
(format emms-show-format
(emms-track-description
(emms-playlist-current-selected-track)))
"Nothing playing right now")))
(if insertp
(insert string)
(message "%s" string))))
(defun emms-shuffle ()
"Shuffle the current playlist.
This uses `emms-playlist-shuffle-function'."
(interactive)
(with-current-emms-playlist
(save-excursion
(funcall emms-playlist-shuffle-function))))
(defun emms-sort ()
"Sort the current playlist.
This uses `emms-playlist-sort-function'."
(interactive)
(with-current-emms-playlist
(save-excursion
(funcall emms-playlist-sort-function))))
(defun emms-uniq ()
"Remove duplicates from the current playlist.
This uses `emms-playlist-uniq-function'."
(interactive)
(with-current-emms-playlist
(save-excursion
(funcall emms-playlist-uniq-function))))
(defun emms-toggle-single-track ()
"Toggle if Emms plays a single track and stops."
(interactive)
(with-current-emms-playlist
(cond (emms-single-track
(setq emms-single-track nil)
(message "single track mode disabled for %s"
(buffer-name)))
(t (setq emms-single-track t)
(message "single track mode enabled for %s"
(buffer-name))))))
(defun emms-toggle-random-playlist ()
"Toggle whether emms plays the tracks randomly or sequentially.
See `emms-random-playlist'."
(interactive)
(customize-set-variable 'emms-random-playlist (not emms-random-playlist))
(if emms-random-playlist
(message "Will play the tracks randomly.")
(message "Will play the tracks sequentially.")))
(defun emms-toggle-repeat-playlist ()
"Toggle whether emms repeats the playlist after it is done.
See `emms-repeat-playlist'."
(interactive)
(setq emms-repeat-playlist (not emms-repeat-playlist))
(if emms-repeat-playlist
(message "Will repeat the playlist after it is done.")
(message "Will stop after the playlist is over.")))
(defun emms-toggle-repeat-track ()
"Toggle whether emms repeats the current track.
See `emms-repeat-track'."
(interactive)
(setq emms-repeat-track (not emms-repeat-track))
(if emms-repeat-track
(message "Will repeat the current track.")
(message "Will advance to the next track after this one.")))
(defun emms-sort-track-name-less-p (a b)
"Return non-nil if the track name of A sorts before B."
(string< (emms-track-name a)
(emms-track-name b)))
(defun emms-ensure-player-playing-p ()
"Raise an error if no player is playing right now."
(when (not emms-player-playing-p)
(error "No EMMS player playing right now")))
(defun emms-completing-read (&rest args)
"Read a string in the minibuffer, with completion.
Set `emms-completing-read' to determine which function to use.
See `completing-read' for a description of ARGS."
(apply emms-completing-read-function args))
(defun emms-display-modes ()
"Display the current EMMS play modes."
(interactive)
(with-current-emms-playlist
(message
"repeat playlist: %s, repeat track: %s, random: %s, single %s"
(if emms-repeat-playlist "yes" "no")
(if emms-repeat-track "yes" "no")
(if emms-random-playlist "yes" "no")
(if emms-single-track "yes" "no"))))
;;; ------------------------------------------------------------------
;;; Utility functions
;;; ------------------------------------------------------------------
(defun emms-insert-file-contents (filename &optional visit)
"Insert the contents of file FILENAME after point.
Do character code conversion and end-of-line conversion, but none
of the other unnecessary things like format decoding or
`find-file-hook'.
If VISIT is non-nil, the buffer's visited filename
and last save file modtime are set, and it is marked unmodified.
If visiting and the file does not exist, visiting is completed
before the error is signaled."
(let ((format-alist nil)
(after-insert-file-functions nil)
(inhibit-file-name-handlers
(append '(jka-compr-handler image-file-handler epa-file-handler)
inhibit-file-name-handlers))
(inhibit-file-name-operation 'insert-file-contents))
(insert-file-contents filename visit)))
;;; ------------------------------------------------------------------
;;; Dictionaries
;;; ------------------------------------------------------------------
;; This is a simple helper data structure, used by both players
;; and tracks.
(defsubst emms-dictionary (name)
"Create a new dictionary of type NAME."
(list name))
(defsubst emms-dictionary-type (dict)
"Return the type of the dictionary DICT."
(car dict))
(defun emms-dictionary-get (dict name &optional default)
"Return the value of NAME in DICT."
(let ((item (assq name (cdr dict))))
(if item
(cdr item)
default)))
(defun emms-dictionary-set (dict name value)
"Set the value of NAME in DICT to VALUE."
(let ((item (assq name (cdr dict))))
(if item
(setcdr item value)
(setcdr dict (append (cdr dict)
(list (cons name value))))))
dict)
;;; ------------------------------------------------------------------
;;; Tracks
;;; ------------------------------------------------------------------
;; This is a simple datatype to store track information.
;; Each track consists of a type (a symbol) and a name (a string).
;; In addition, each track has an associated dictionary of information.
(defun emms-track (type name)
"Create an EMMS track with type TYPE and name NAME."
(let ((track (when emms-cache-get-function
(funcall emms-cache-get-function type name))))
(when (not track)
(setq track (emms-dictionary '*track*))
;; Prevent the cache from being called for these two sets
(let ((emms-cache-modified-function nil))
(emms-track-set track 'type type)
(emms-track-set track 'name name))
(when emms-cache-set-function
(funcall emms-cache-set-function type name track)))
;; run any hooks regardless of a cache hit, as the entry may be
;; old
(run-hook-with-args 'emms-track-initialize-functions track)
track))
(defun emms-track-p (obj)
"True if OBJ is an emms track."
(and (listp obj)
(eq (car obj) '*track*)))
(defun emms-track-type (track)
"Return the type of TRACK."
(emms-track-get track 'type))
(defun emms-track-file-p (track)
"True if TRACK is a file type"
(eq 'file (emms-track-type track)))
(defun emms-track-name (track)
"Return the name of TRACK."
(emms-track-get track 'name))
(defun emms-track-get (track name &optional default)
"Return the value of NAME for TRACK.
If there is no value, return DEFAULT (or nil, if not given)."
(emms-dictionary-get track name default))
(defun emms-track-set (track name value)
"Set the value of NAME for TRACK to VALUE."
(emms-dictionary-set track name value)
(when emms-cache-modified-function
(funcall emms-cache-modified-function track)))
(defun emms-track-description (track)
"Return a description of TRACK.
This function uses the global value for
`emms-track-description-function', rather than anything the
current mode might have set.
Use `emms-track-force-description' instead if you need to insert
a description into a playlist buffer."
(funcall (default-value 'emms-track-description-function) track))
(defun emms-track-updated (track)
"Information in TRACK got updated."
(run-hook-with-args 'emms-track-info-filters track)
(emms-playlist-track-updated track)
(run-hook-with-args 'emms-track-updated-functions track))
(defun emms-track-simple-description (track)
"Simple function to give a user-readable description of a track.
If it's a file track, just return the file name. Otherwise,
return the type and the name with a colon in between.
Hex-encoded characters in URLs are replaced by the decoded
character."
(let ((type (emms-track-type track)))
(cond ((eq 'file type)
(emms-track-name track))
((eq 'url type)
(emms-format-url-track-name (emms-track-name track)))
(t (concat (symbol-name type)
": " (emms-track-name track))))))
(defun emms-format-url-track-name (name)
"Format URL track name for better readability."
(url-unhex-string name))
(defun emms-track-force-description (track)
"Always return text that describes TRACK.
This is used when inserting a description into a buffer.
The reason for this is that if no text was returned (i.e. the
user defined a track function that returned nil or the empty
string), a confusing error message would result."
(let ((desc (funcall emms-track-description-function track)))
(if (and (stringp desc) (not (string= desc "")))
desc
(emms-track-simple-description track))))
(defun emms-track-get-year (track)
"Get year of TRACK for display.
There is the separation between the \\='release date\\=' and the
\\='original date\\='. This difference matters e.g. for
re-releases (anniversaries and such) where the release date is
more recent than the original release date. In such cases the
user probably wants the original release date so this is what we
show."
(or
(emms-format-date-to-year (emms-track-get track 'info-date))
(emms-format-date-to-year (emms-track-get track 'info-originaldate))
(emms-track-get track 'info-year)
(emms-track-get track 'info-originalyear)))
(defun emms-format-date-to-year (date)
"Try to extract year part from DATE.
Return nil if the year cannot be extracted."
(when date
(let ((year (nth 5 (parse-time-string date))))
(if year (number-to-string year)
(when (string-match "^[ \t]*\\([0-9]\\{4\\}\\)" date)
(match-string 1 date))))))
;;; ------------------------------------------------------------------
;;; The Playlist
;;; ------------------------------------------------------------------
;; Playlists are stored in buffers. The current playlist buffer is
;; remembered in the `emms-playlist' variable. The buffer consists of
;; any kind of data. Strings of text with a `emms-track' property are
;; the tracks in the buffer.
(defvar emms-playlist-buffers nil
"The list of EMMS playlist buffers.
You should use the `emms-playlist-buffer-list' function to
retrieve a current list of EMMS buffers. Never use this variable
for that purpose.")
(defvar emms-playlist-selected-marker nil
"The marker for the currently selected track.")
(make-variable-buffer-local 'emms-playlist-selected-marker)
(defvar emms-playlist-buffer-p nil
"Non-nil if the current buffer is an EMMS playlist.")
(make-variable-buffer-local 'emms-playlist-buffer-p)
(defvar emms-queue-lock nil
"The playlist name the active playlist queue is locked to, if any.")
(defun emms-playlist-ensure-playlist-buffer ()
"Throw an error if we're not in a playlist-buffer."
(when (not emms-playlist-buffer-p)
(error "Not an EMMS playlist buffer")))
(defun emms-lock-queue ()
"Lock the current active playlist queue to it's playlist.
This allows for uninterrupted playlist playback while browsing other playlists."
(interactive)
(setq emms-queue-lock (buffer-name emms-playlist-buffer))
(message (concat "Active playlist queue is locked to " emms-queue-lock)))
(defun emms-unlock-queue ()
"Unlock the current active playlist queue from its playlist."
(interactive)
(setq emms-queue-lock nil)
(message "Active playlist queue is unlocked."))
(defun emms-playlist-set-playlist-buffer (&optional buffer)
"Set the current playlist buffer if the queue is not locked to it's playlist."
(interactive
(if (not emms-queue-lock)
(list (let* ((buf-list (mapcar #'(lambda (buf)
(list (buffer-name buf)))
(emms-playlist-buffer-list)))
(sorted-buf-list (sort buf-list
#'(lambda (lbuf rbuf)
(< (length (car lbuf))
(length (car rbuf))))))
(default (or (and emms-playlist-buffer-p
;; default to current buffer
(buffer-name))
;; pick shortest buffer name, since it is
;; likely to be a shared prefix
(car sorted-buf-list))))
(emms-completing-read "Playlist buffer to make current: "
sorted-buf-list nil t default)))))
(if (not emms-queue-lock)
(let ((buf (if buffer
(get-buffer buffer)
(current-buffer))))
(with-current-buffer buf
(emms-playlist-ensure-playlist-buffer))
(setq emms-playlist-buffer buf)
(when (called-interactively-p 'interactive)
(message "Set current EMMS playlist buffer"))
buf)
(message (concat "The active playlist queue is locked to " emms-queue-lock))))
(defun emms-playlist-new (&optional name)
"Create a new playlist buffer.
The buffer is named NAME, but made unique. NAME defaults to
`emms-playlist-buffer-name'. If called interactively, the new
buffer is also selected."
(interactive)
(let ((buf (generate-new-buffer (or name
emms-playlist-buffer-name))))
(with-current-buffer buf
(when (not (eq major-mode emms-playlist-default-major-mode))
(funcall emms-playlist-default-major-mode))
(setq emms-playlist-buffer-p t))
(add-to-list 'emms-playlist-buffers buf)
(when (called-interactively-p 'interactive)
(switch-to-buffer buf))
buf))
(defun emms-playlist-buffer-list ()
"Return a list of EMMS playlist buffers.
The first element is guaranteed to be the current EMMS playlist
buffer, if it exists, otherwise the slot will be used for the
other EMMS buffers. The list will be in newest-first order."
;; prune dead buffers
(setq emms-playlist-buffers (emms-delete-if (lambda (buf)
(not (buffer-live-p buf)))
emms-playlist-buffers))
;; add new buffers
(mapc (lambda (buf)
(when (buffer-live-p buf)
(with-current-buffer buf
(when (and emms-playlist-buffer-p
(not (memq buf emms-playlist-buffers)))
(setq emms-playlist-buffers
(cons buf emms-playlist-buffers))))))
(buffer-list))
;; force current playlist buffer to head position
(when (and (buffer-live-p emms-playlist-buffer)
(not (eq (car emms-playlist-buffers) emms-playlist-buffer)))
(setq emms-playlist-buffers (cons emms-playlist-buffer
(delete emms-playlist-buffer
emms-playlist-buffers))))
emms-playlist-buffers)
(defun emms-playlist-current-kill ()
"Kill the current EMMS playlist buffer and switch to the next one."
(interactive)
(when (buffer-live-p emms-playlist-buffer)
(let ((new (cadr (emms-playlist-buffer-list))))
(if new
(let ((old emms-playlist-buffer))
(setq emms-playlist-buffer new
emms-playlist-buffers (cdr emms-playlist-buffers))
(kill-buffer old)
(switch-to-buffer emms-playlist-buffer))
(with-current-buffer emms-playlist-buffer
(bury-buffer))))))
(defun emms-playlist-current-clear ()
"Clear the current playlist.
If no current playlist exists, a new one is generated."
(interactive)
(if (or (not emms-playlist-buffer)
(not (buffer-live-p emms-playlist-buffer)))
(setq emms-playlist-buffer (emms-playlist-new))
(with-current-buffer emms-playlist-buffer
(emms-playlist-clear))))
(defun emms-playlist-clear ()
"Clear the current buffer."
(interactive)
(emms-playlist-ensure-playlist-buffer)
(let ((inhibit-read-only t))
(widen)
(delete-region (point-min)
(point-max)))
(run-hooks 'emms-playlist-cleared-hook))
;;; Point movement within the playlist buffer.
(defun emms-playlist-track-at (&optional pos)
"Return the track at POS (point if not given), or nil if none."
(emms-playlist-ensure-playlist-buffer)
(emms-with-widened-buffer
(get-text-property (or pos (point))
'emms-track)))
(defun emms-playlist-next ()
"Move to the next track in the current buffer."
(emms-playlist-ensure-playlist-buffer)
(let ((next (next-single-property-change (point)
'emms-track)))
(when (not next)
(error "No next track"))
(when (not (emms-playlist-track-at next))
(setq next (next-single-property-change next 'emms-track)))
(when (or (not next)
(= next (point-max)))
(error "No next track"))
(goto-char next)))
(defun emms-playlist-previous ()
"Move to the previous track in the current buffer."
(emms-playlist-ensure-playlist-buffer)
(let ((prev (previous-single-property-change (point)
'emms-track)))
(when (not prev)
(error "No previous track"))
(when (not (get-text-property prev 'emms-track))
(setq prev (or (previous-single-property-change prev 'emms-track)
(point-min))))
(when (or (not prev)
(not (get-text-property prev 'emms-track)))
(error "No previous track"))
(goto-char prev)))
(defun emms-playlist-first ()
"Move to the first track in the current buffer."
(emms-playlist-ensure-playlist-buffer)