Improved progress tracking disabling.
[emus.git] / emus.el
1 ;;; emus.el --- Simple mp3 player  -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 2019 Tim Vaughan
4
5 ;; Author: Tim Vaughan <timv@ughan.xyz>
6 ;; Created: 8 December 2019
7 ;; Version: 1.0
8 ;; Keywords: multimedia
9 ;; Homepage: http://thelambdalab.xy/emus
10 ;; Package-Requires: ((emacs "26"))
11
12 ;; This file is not part of GNU Emacs.
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 file.  If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28
29 ;; This is a simple package for playing audio from a local directory
30 ;; tree of mp3 files.  It uses the program mpg123 as its back-end.
31 ;; Currently the library is loaded completely every time emus starts.
32
33 ;;; Code:
34
35 (provide 'emus)
36
37
38 ;;; Dependencies
39 ;;
40
41 (require 'seq)
42
43
44 ;;; Customizations
45 ;;
46
47 (defgroup emus nil
48   "Simple music player for Emacs."
49   :group 'multimedia)
50
51 (defcustom emus-directory "~/Music/"
52   "Directory containing audio files for emus."
53   :type '(string))
54
55 (defcustom emus-mpg123-program "mpg123"
56   "Name of (and, optionally, path to) mpg123 binary."
57   :type '(string))
58
59 (defface emus-artist
60   '((((background dark)) :inherit font-lock-string-face :background "#222" :extend t)
61     (t :inherit font-lock-string-face :background "#ddd" :extend t))
62   "Face used for artist names in browser.")
63
64 (defface emus-album
65   '((((background dark)) :inherit font-lock-constant-face :background "#111" :extend t)
66     (t :inherit font-lock-constant-face :background "#eee" :extend t))
67   "Face used for album names in browser.")
68
69 (defface emus-track
70   '((t :inherit font-lock-keyword-face))
71   "Face used for track titles in browser.")
72
73 (defface emus-track-current
74   '((t :inherit font-lock-keyword-face :inverse-video t :extend t))
75   "Face used for track titles in browser.")
76
77 (defface emus-cursor
78   '((t :inherit bold))
79   "Face used for current track cursor")
80
81
82 ;;; Global variables
83
84 (defvar emus--proc-in-use nil
85   "If non-nil, disables `emus-send-cmd'.
86 Used to prevent commands from interfering with library construction.")
87
88 (defvar emus-tracks nil
89   "Emus audio library.")
90
91 (defvar emus-current-track nil
92   "Currently-selected emus track.")
93
94 (defvar emus-state 'stopped
95   "Current playback state.")
96
97 (defvar emus-continuous-playback t
98   "If non-nil, emus will automatically play the next track when the current track is finished.")
99
100 (defvar emus-current-volume 100
101   "The current playback volume.")
102
103 (defvar emus-current-progress ""
104   "String describing the progress through the current track.")
105
106 (defvar emus-progress-enabled t
107   "Current state of progress tracking.
108
109 To enable or disable progress tracking, using `emus-toggle-progress-tracking'.
110 (Changing the value of this variable will not affect anything.)")
111
112 ;;; mpg123 process
113 ;;
114
115
116 (defun emus-get-process ()
117   "Return current or new mpg123 process."
118   (let* ((emus-process-raw (get-process "emus-process"))
119          (emus-process (if emus-process-raw
120                            (if (process-live-p emus-process-raw)
121                                emus-process-raw
122                              (kill-process emus-process-raw)
123                              nil))))
124     (if emus-process
125         emus-process
126       (let ((proc
127              (make-process :name "emus-process"
128                            :command `(,emus-mpg123-program "-R"))))
129         (set-process-query-on-exit-flag proc nil)
130         (unless emus-progress-enabled
131           (process-send-string proc "silence\n"))
132         proc))))
133
134 (defun emus--send-cmd-raw (cmd &rest args)
135   "Send a command CMD with args ARGS to the mpg123 process.
136 This procedure does not respect `emus--proc-in-use' and thus should only
137 be used by `emus--load-library'."
138     (process-send-string (emus-get-process)
139                          (concat
140                           (seq-reduce (lambda (s1 s2) (concat s1 " " s2)) args cmd)
141                           "\n")))
142
143 (defun emus-send-cmd (cmd &rest args)
144   "Send a command CMD with args ARGS to the mpg123 process."
145   (unless emus--proc-in-use
146     (apply #'emus--send-cmd-raw cmd args)))
147
148 (defun emus-kill-process ()
149   "Kill any existing mpg123 process."
150   (let ((emus-process (get-process "emus-process")))
151     (if emus-process
152         (kill-process emus-process))))
153
154 (defun emus-restart ()
155   "Restart emus, regardless of current state."
156   (interactive)
157   (emus-kill-process)
158   (setq emus-state 'stopped
159         emus--proc-in-use nil
160         emus-tracks nil))
161
162 ;;; Library
163 ;;
164
165 (defun emus-get-audio-files ()
166   "Get all mp3 files in main emus directory."
167   (mapcar
168    #'expand-file-name
169    (directory-files-recursively emus-directory ".*\\.mp3")))
170
171 (defun emus-get-playlist-files ()
172   "Get all m3u files in the main emus music directory."
173   (mapcar
174    #'expand-file-name
175    (directory-files-recursively emus-directory ".*\\.m3u")))
176
177 (defun emus-make-track (artist album title filename &optional pos)
178   "Create an object representing an emus track.
179 ARTIST, ALBUM and TITLE are used to describe the track, FILENAME
180 refers to the mp3 file containing the track.  If non-nil, POS
181 specifies the position of the record representing this track in the
182 emus browser buffer."
183   (list artist album title filename pos))
184
185 (defun emus-track-artist (track)
186   "The artist corresponding to TRACK."
187   (elt track 0))
188
189 (defun emus-track-album (track)
190   "The album corresponding to TRACK."
191   (elt track 1))
192
193 (defun emus-track-title (track)
194   "The title of TRACK."
195   (elt track 2))
196
197 (defun emus-track-file (track)
198   "The mp3 file corresponding to TRACK."
199   (elt track 3))
200
201 (defun emus-track-browser-pos (track)
202   "The location of the browser buffer record corresponding to TRACK."
203   (elt track 4))
204
205 (defun emus-set-track-browser-pos (track pos)
206   "Set the location of the browser buffer record corresponding to TRACK to POS."
207   (setf (seq-elt track 4) pos))
208
209 (defun emus--load-library (then)
210   "Initialize the emus track library.
211 Once the library is initialized, the function THEN is called."
212   (unless emus--proc-in-use
213     (setq emus--proc-in-use t)
214     (emus--suspend-cp)
215     (setq emus-state 'stopped)
216     (setq emus-tracks nil)
217     (let ((proc (emus-get-process))
218           (tagstr "")
219           (filenames (emus-get-audio-files)))
220       (set-process-filter proc (lambda (proc string)
221                                  (setq tagstr (concat tagstr string))
222                                  (when (string-suffix-p "@P 1\n" string)
223                                    (add-to-list 'emus-tracks
224                                                 (emus--make-track-from-tagstr (car filenames)
225                                                                               tagstr))
226                                    (setq tagstr "")
227                                    (setq filenames (cdr filenames))
228                                    (if filenames
229                                        (emus--send-cmd-raw "lp" (car filenames))
230                                      (set-process-filter proc nil)
231                                      (setq emus-tracks (reverse emus-tracks))
232                                      (emus--sort-tracks)
233                                      (emus--add-tracks-from-playlist-files)
234                                      (unless emus-current-track
235                                        (setq emus-current-track (car emus-tracks)))
236                                      (funcall then)
237                                      (emus--resume-cp)
238                                      (setq emus--proc-in-use nil)))))
239       (emus--send-cmd-raw "lp" (car filenames)))))
240
241 (defun emus--dump-tracks ()
242     emus-tracks)
243
244 (defun emus--make-track-from-tagstr (filename tagstr)
245   "Parse TAGSTR to populate the fields of a track corresponding to FILENAME."
246   (let ((artist "")
247         (album "")
248         (title ""))
249     (dolist (line (split-string tagstr "\n"))
250       (let ((found-artist (elt (split-string line "@I ID3v2.artist:") 1))
251             (found-album (elt (split-string line "@I ID3v2.album:") 1))
252             (found-title (elt (split-string line "@I ID3v2.title:") 1)))
253         (cond
254          (found-artist (setq artist found-artist))
255          (found-album (setq album found-album))
256          (found-title (setq title found-title)))))
257     (emus-make-track artist album title filename)))
258
259 (defun emus--add-tracks-from-playlist-files ()
260   (let ((tracks nil))
261     (dolist (filename (emus-get-playlist-files))
262       (let ((artist "Playlists")
263             (album (file-name-base filename))
264             (title nil)
265             (lines (split-string (with-temp-buffer
266                                    (insert-file-contents filename)
267                                    (buffer-string))
268                                  "\n")))
269         (dolist (line lines)
270           (pcase (string-trim line)
271             ((rx (: string-start
272                     (* space)
273                     "#extinf:"
274                     (* (not ",")) ","
275                     (let display-title (* any))
276                     string-end))
277              (setq title display-title))
278             ((rx (: string-start (* space) "#"))) ;skip other comments
279             ((rx (let filename (+ any)))
280              (setq tracks (cons (emus-make-track artist album (or title filename) filename)
281                                 tracks))
282              (setq title nil))))))
283     (setq emus-tracks (append emus-tracks (reverse tracks)))))
284
285 (defun emus--sort-tracks ()
286   "Sort the library tracks according to artist and album.
287 Leaves the track titles unsorted, so they will appear in the order specified
288 by the filesystem."
289   (setq emus-tracks
290         (sort emus-tracks
291               (lambda (r1 r2)
292                 (let ((artist1 (emus-track-artist r1))
293                       (artist2 (emus-track-artist r2)))
294                   (if (string= artist1 artist2)
295                       (let ((album1 (emus-track-album r1))
296                             (album2 (emus-track-album r2)))
297                         (string< album1 album2))
298                     (string< artist1 artist2)))))))
299
300 (defmacro emus--with-library (&rest body)
301   "Evaluate BODY with the library initialized."
302   `(if emus-tracks
303        (unless emus--proc-in-use ,@body)
304      (emus--load-library
305       (lambda () ,@body))))
306
307
308 ;;; Playback
309 ;;
310
311 (defun emus--suspend-cp ()
312   "Suspend continuous playback."
313   (setq emus-continuous-playback nil))
314
315 (defun emus--resume-cp ()
316   "Resume continuous playback."
317   (setq emus-continuous-playback t))
318
319 (defun emus--timestamp (seconds-total)
320   "Produce a timestamp string representation of SECONDS-TOTAL."
321   (let* ((seconds (truncate (mod seconds-total 60)))
322          (minutes (truncate (/ seconds-total 60))))
323     (format "%02d:%02d" minutes seconds)))
324
325 (defun emus-play-track (track)
326   "Set TRACK as current and start playing."
327   (emus--with-library
328    (let ((old-track emus-current-track))
329      (emus-send-cmd "l" (emus-track-file track))
330      (setq emus-state 'playing)
331      (setq emus-current-track track)
332      (setq emus-current-progress (if emus-progress-enabled "" " (progress disabled)"))
333      (set-process-filter
334       (emus-get-process)
335       (lambda (_proc string)
336         (dolist (line (string-split string "\n"))
337           (pcase line
338             ((and "@P 0"
339                   (guard emus-continuous-playback)
340                   (guard (eq emus-state 'playing)))
341              (emus-play-next))
342             ((and (guard emus-progress-enabled)
343                   (rx (: string-start
344                          "@I ICY-META: StreamTitle="
345                          (let str (+ (not ";")))
346                          ";")))
347              (message (concat "Emus: Playing stream " str)))
348             ((and (guard emus-progress-enabled)
349                   (rx (: string-start
350                          "@F "
351                          (+ digit)
352                          " "
353                          (+ digit)
354                          " "
355                          (let left-str (+ (not " ")))
356                          " "
357                          (let right-str (+ any)))))
358              (let* ((left (string-to-number left-str))
359                     (right (string-to-number right-str))
360                     (total (+ left right)))
361                (setq emus-current-progress
362                      (format " %s/%s"
363                              (emus--timestamp left)
364                              (emus--timestamp total)))))
365             ))))
366      (emus--update-track old-track)
367      (emus--update-track track)
368      (emus--resume-cp)
369      (emus-goto-current))))
370
371 (defun emus-select-track (track)
372   "Set TRACK as current, but do not start playing."
373   (emus--with-library
374    (let ((old-track emus-current-track))
375      (setq emus-state 'stopped)
376      (setq emus-current-track track)
377      (emus--update-track old-track)
378      (emus--update-track track)
379      (emus-send-cmd "o")
380      (emus--resume-cp)
381      (emus-goto-current))))
382
383 (defun emus-stop ()
384   "Stop playback of the current track."
385   (interactive)
386   (emus--with-library
387    (setq emus-state 'stopped)
388    (emus--update-track emus-current-track)
389    (emus-send-cmd "s")))
390
391 (defun emus-playpause ()
392   "Begin playback of the current track.
393 If the track is already playing, pause playback.
394 If the track is currently paused, resume playback."
395   (interactive)
396   (emus--with-library
397    (when emus-current-track
398      (if (eq emus-state 'stopped)
399          (emus-play-track emus-current-track)
400        (emus-send-cmd "p")
401        (pcase emus-state
402          ((or 'paused 'stopped) (setq emus-state 'playing))
403          ('playing (setq emus-state 'paused)))
404        (unless (eq emus-state 'paused)))
405      (emus--update-track emus-current-track))))
406
407 (defun emus-set-volume (pct)
408   "Set the playback volume to PCT %."
409   (emus--with-library
410    (setq emus-current-volume pct)
411    (emus-send-cmd "v" (number-to-string pct))))
412
413 (defun emus-volume-increase-by (delta)
414   "Increase the playback volume by DELTA %."
415   (emus-set-volume (max 0 (min 100 (+ emus-current-volume delta)))))
416
417 (defun emus-volume-up ()
418   "Increase the playback volume by 10%."
419   (interactive)
420   (emus-volume-increase-by 10))
421
422 (defun emus-volume-down ()
423   "Decrease the playback volume by 10%."
424   (interactive)
425   (emus-volume-increase-by -10))
426
427 (defun emus--play-adjacent-track (&optional prev)
428   "Play the next track in the library, or the previous if PREV is non-nil."
429   (emus--with-library
430    (let ((idx (seq-position emus-tracks emus-current-track))
431          (offset (if prev -1 +1)))
432      (if idx
433          (let ((next-track (elt emus-tracks (+ idx offset))))
434            (if next-track
435                (if (eq emus-state 'playing)
436                    (emus-play-track next-track)
437                  (emus-select-track next-track))
438              (error "Track does not exist")))
439        (error "No track selected")))))
440
441 (defun emus--play-adjacent-album (&optional prev)
442   "Play the first track of the next album in the library.
443 If PREV is non-nil, plays the last track of the previous album."
444   (emus--with-library
445    (let ((idx (seq-position emus-tracks emus-current-track)))
446      (if idx
447          (let* ((search-list (if prev
448                                  (reverse (seq-subseq emus-tracks 0 idx))
449                                (seq-subseq emus-tracks (+ idx 1))))
450                 (current-album (emus-track-album emus-current-track))
451                 (next-track (seq-some (lambda (r)
452                                         (if (string= (emus-track-album r)
453                                                      current-album)
454                                             nil
455                                           r))
456                                       search-list)))
457            (if next-track
458                (if (eq emus-state 'playing)
459                    (emus-play-track next-track)
460                  (emus-select-track next-track))
461              (error "Track does not exist")))
462        (error "No track selected")))))
463
464 (defun emus-play-next ()
465   "Play the next track in the library."
466   (interactive)
467   (emus--play-adjacent-track))
468
469 (defun emus-play-prev ()
470   "Play the previous track in the library."
471   (interactive)
472   (emus--play-adjacent-track t))
473
474 (defun emus-play-next-album ()
475   "Play the first track of the next album in the library."
476   (interactive)
477   (emus--play-adjacent-album))
478
479 (defun emus-play-prev-album ()
480   "Play the last track of the previous album in the library."
481   (interactive)
482   (emus--play-adjacent-album t))
483
484 (defun emus-jump (seconds)
485   "Jump forward in current track by SECONDS seconds."
486   (emus--with-library
487    (emus-send-cmd "jump" (format "%+ds" seconds))))
488
489 (defun emus-jump-10s-forward ()
490   "Jump 10 seconds forward in current track."
491   (interactive)
492   (emus-jump 10))
493
494 (defun emus-jump-10s-backward ()
495   "Jump 10 seconds backward in current track."
496   (interactive)
497   (emus-jump -10))
498
499 (defun emus-jump-1m-forward ()
500   "Jump 1 minute forward in current track."
501   (interactive)
502   (emus-jump 60))
503
504 (defun emus-jump-1m-backward ()
505   "Jump 1 minute backward in current track."
506   (interactive)
507   (emus-jump -60))
508
509 (defun emus-display-status ()
510   "Display the current playback status in the minibuffer."
511   (interactive)
512   (emus--with-library
513    (message
514     (concat "Emus: Volume %d%%"
515             (pcase emus-state
516               ('stopped " [Stopped]")
517               ('paused (format " [Paused%s]" emus-current-progress))
518               ('playing (format " [Playing%s]" emus-current-progress))
519               (_ ""))
520             " %s")
521     emus-current-volume
522     (if emus-current-track
523         (format "- %.30s (%.20s, %.20s)"
524                 (emus-track-title emus-current-track)
525                 (emus-track-album emus-current-track)
526                 (emus-track-artist emus-current-track))
527       ""))))
528
529 (defun emus-toggle-progress-tracking ()
530   "Enable/disable progress tracking."
531   (interactive)
532   (setq emus-progress-enabled (not emus-progress-enabled))
533   (if emus-progress-enabled
534       (progn
535         (emus-send-cmd "progress")
536         (setq emus-current-progress ""))
537     (progn
538       (emus-send-cmd "silence")
539       (setq emus-current-progress " (progress diabled)"))))
540
541
542 ;;; Browser
543 ;;
544
545 (defun emus--insert-track (track &optional prev-track first)
546   "Insert a button representing TRACK into the current buffer.
547
548 When provided, PREV-TRACK is used to determine whether to insert additional
549 headers representing the artist or the album title.
550
551 If non-nil, FIRST indicates that the track is the first in the library
552 and thus requires both artist and album headers."
553   (let* ((artist (emus-track-artist track))
554          (album (emus-track-album track))
555          (title (emus-track-title track))
556          (album-symb (intern (concat artist album)))
557          (help-str (format "mouse-1, RET: Play '%.30s' (%.20s)" title artist))
558          (field (intern album))) ;Allows easy jumping between albums with cursor.
559     (when (or prev-track first)
560       (unless (equal (emus-track-artist prev-track) artist)
561         (insert-text-button
562          (propertize artist 'face 'emus-artist)
563          'action #'emus--click-track
564          'follow-link t
565          'help-echo help-str
566          'emus-track track
567          'field field)
568         (insert (propertize "\n"
569                             'face 'emus-artist
570                             'field field)))
571       (unless (equal (emus-track-album prev-track) album)
572         (insert-text-button
573          (propertize (concat "  " album) 'face 'emus-album)
574          'action #'emus--click-track
575          'follow-link t
576          'help-echo help-str
577          'emus-track track
578          'field field)
579         (insert (propertize "\n"
580                             'face 'emus-album
581                             'field field))))
582     (emus-set-track-browser-pos track (point))
583     (let ((is-current (equal track emus-current-track)))
584       (insert-text-button
585        (concat
586         (if is-current
587             (propertize
588              (pcase emus-state
589                ('playing "->")
590                ('paused "-)")
591                ('stopped "-]"))
592              'face 'emus-cursor)
593           (propertize "  " 'face 'default))
594         (propertize (format "   %s" title)
595                     'face (if is-current
596                               'emus-track-current
597                             'emus-track)))
598        'action #'emus--click-track
599        'follow-link t
600        'help-echo help-str
601        'emus-track track
602        'invisible album-symb
603        'field field)
604       (insert (propertize "\n"
605                           'face (if is-current
606                                     'emus-track-current
607                                   'emus-track)
608                           'field field
609                           'invisible album-symb)))))
610
611 (defun emus--update-track (track)
612   "Rerender entry for TRACK in emus browser buffer.
613 Used to update browser display when `emus-current-track' and/or `emus-state' changes."
614   (let ((track-pos (emus-track-browser-pos track)))
615     (when (and (get-buffer "*emus*")
616                (emus-track-browser-pos track))
617       (with-current-buffer "*emus*"
618         (let ((inhibit-read-only t)
619               (old-point (point)))
620           (goto-char track-pos)
621           (search-forward "\n")
622           (delete-region track-pos (point))
623           (goto-char track-pos)
624           (emus--insert-track track)
625           (goto-char old-point))))))
626
627 (defun emus--render-tracks ()
628   "Render all library tracks in emus browser buffer."
629   (with-current-buffer "*emus*"
630     (let ((inhibit-read-only t)
631           (old-pos (point)))
632       (erase-buffer)
633       (goto-char (point-min))
634       (let ((prev-track nil))
635         (dolist (track emus-tracks)
636           (emus--insert-track track prev-track (not prev-track))
637           (setq prev-track track)))
638       (goto-char old-pos))))
639
640 (defun emus--click-track (button)
641   "Begin playback of track indicated by BUTTON."
642   (emus-play-track (button-get button 'emus-track))
643   (emus-display-status))
644
645 (defun emus-goto-current ()
646   "Move point to the current track in the browser buffer, if available."
647   (interactive)
648   (when (and (get-buffer "*emus*")
649              emus-current-track)
650     (with-current-buffer "*emus*"
651         (goto-char (emus-track-browser-pos emus-current-track)))))
652
653 (defun emus-browse ()
654   "Switch to *emus* audio library browser."
655   (interactive)
656   (emus--with-library
657    (pop-to-buffer-same-window "*emus*")
658    (emus-browser-mode)
659    (emus--render-tracks)
660    (emus-goto-current)))
661
662 (defun emus-refresh ()
663   "Refresh the emus library."
664   (interactive)
665   (emus-stop)
666   (setq emus-tracks nil)
667   (emus-browse))
668
669
670 ;;; Playback + status display commands
671 ;;
672
673 (defun emus-playpause-status ()
674   "Start, pause or resume playback, then display the emus status in the minibuffer."
675   (interactive)
676   (emus-playpause)
677   (emus-display-status))
678
679 (defun emus-stop-status ()
680   "Stop playback, then display the emus status in the minibuffer."
681   (interactive)
682   (emus-stop)
683   (emus-display-status))
684
685 (defun emus-volume-up-status ()
686   "Increase volume by 10%, then display the emus status in the minibuffer."
687   (interactive)
688   (emus-volume-up)
689   (emus-display-status))
690
691 (defun emus-volume-down-status ()
692   "Decrease volume by 10%, then display the emus status in the minibuffer."
693   (interactive)
694   (emus-volume-down)
695   (emus-display-status))
696
697 (defun emus-play-next-status ()
698   "Play next track, then display the emus status in the minibuffer."
699   (interactive)
700   (emus-play-next)
701   (emus-display-status))
702
703 (defun emus-play-prev-status ()
704   "Play previous track, then display the emus status in the minibuffer."
705   (interactive)
706   (emus-play-prev)
707   (emus-display-status))
708
709 (defun emus-play-next-album-status ()
710   "Play first track of next album, then display the emus status in the minibuffer."
711   (interactive)
712   (emus-play-next-album)
713   (emus-display-status))
714
715 (defun emus-play-prev-album-status ()
716   "Play last track of previous album, then display the emus status in the minibuffer."
717   (interactive)
718   (emus-play-prev-album)
719   (emus-display-status))
720
721 (defun emus-jump-10s-forward-status ()
722   "Jump 10s forward in current track, then display the emus status in the minibuffer."
723   (interactive)
724   (emus-jump-10s-forward)
725   (emus-display-status))
726
727 (defun emus-jump-10s-backward-status ()
728   "Jump 10s backward in current track, then display the emus status in the minibuffer."
729   (interactive)
730   (emus-jump-10s-backward)
731   (emus-display-status))
732
733 (defun emus-jump-1m-forward-status ()
734   "Jump 10s forward in current track, then display the emus status in the minibuffer."
735   (interactive)
736   (emus-jump-1m-forward)
737   (emus-display-status))
738
739 (defun emus-jump-1m-backward-status ()
740   "Jump 10s backward in current track, then display the emus status in the minibuffer."
741   (interactive)
742   (emus-jump-1m-backward)
743   (emus-display-status))
744
745 (defun emus-toggle-progress-status ()
746   "Toggle progress tracking, then display the emus status in the minibuffer."
747   (interactive)
748   (emus-toggle-progress-tracking)
749   (emus-display-status))
750
751 (defun emus-goto-current-status ()
752   "Move point to the current track, then display the emus status in the minibuffer."
753   (interactive)
754   (emus-goto-current)
755   (emus-display-status))
756
757 (defun emus-refresh-status ()
758   "Refresh the emus library, then display the emus status in the minibuffer."
759   (interactive)
760   (emus-stop)
761   (setq emus-tracks nil)
762   (emus--with-library
763    (emus-browse)
764    (emus-display-status)))
765
766 (defvar emus-browser-mode-map
767   (let ((map (make-sparse-keymap)))
768     (define-key map (kbd "SPC") 'emus-playpause-status)
769     (define-key map (kbd "o") 'emus-stop-status)
770     (define-key map (kbd "+") 'emus-volume-up-status)
771     (define-key map (kbd "=") 'emus-volume-up-status)
772     (define-key map (kbd "-") 'emus-volume-down-status)
773     (define-key map (kbd "R") 'emus-refresh-status)
774     (define-key map (kbd "n") 'emus-play-next-status)
775     (define-key map (kbd "p") 'emus-play-prev-status)
776     (define-key map (kbd "N") 'emus-play-next-album-status)
777     (define-key map (kbd "P") 'emus-play-prev-album-status)
778     (define-key map (kbd ",") 'emus-jump-10s-backward-status)
779     (define-key map (kbd ".") 'emus-jump-10s-forward-status)
780     (define-key map (kbd "<") 'emus-jump-1m-backward-status)
781     (define-key map (kbd ">") 'emus-jump-1m-forward-status)
782     (define-key map (kbd "c") 'emus-goto-current-status)
783     (define-key map (kbd "!") 'emus-toggle-progress-status)
784     (when (fboundp 'evil-define-key*)
785       (evil-define-key* 'motion map
786                         (kbd "SPC") 'emus-playpause-status
787                         (kbd "o") 'emus-stop-status
788                         (kbd "+") 'emus-volume-up-status
789                         (kbd "=") 'emus-volume-up-status
790                         (kbd "-") 'emus-volume-down-status
791                         (kbd "R") 'emus-refresh-status
792                         (kbd "n") 'emus-play-next-status
793                         (kbd "p") 'emus-play-prev-status
794                         (kbd "N") 'emus-play-next-album-status
795                         (kbd "P") 'emus-play-prev-album-status
796                         (kbd ",") 'emus-jump-10s-backward-status
797                         (kbd ".") 'emus-jump-10s-forward-status
798                         (kbd "<") 'emus-jump-1m-backward-status
799                         (kbd ">") 'emus-jump-1m-forward-status
800                         (kbd "c") 'emus-goto-current-status
801                         (kbd "!") 'emus-toggle-progress-status))
802     map)
803   "Keymap for emus browser.")
804
805 (defun emus-restart ()
806   "Restart emus."
807   (interactive)
808   (emus-kill-process))
809
810 (define-derived-mode emus-browser-mode special-mode "emus-browser"
811   "Major mode for EMUS music player file browser."
812   (setq-local buffer-invisibility-spec nil))
813
814 (when (fboundp 'evil-set-initial-state)
815   (evil-set-initial-state 'emus-browser-mode 'motion))
816
817 ;;; emus.el ends here