Made progress tracking optional.
[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             ((rx (: string-start
343                     "@I ICY-META: StreamTitle="
344                     (let str (+ (not ";")))
345                     ";"))
346              (message (concat "Emus: Playing stream " str)))
347             ((rx (: string-start
348                     "@F "
349                     (+ digit)
350                     " "
351                     (+ digit)
352                     " "
353                     (let left-str (+ (not " ")))
354                     " "
355                     (let right-str (+ any))))
356              (let* ((left (string-to-number left-str))
357                     (right (string-to-number right-str))
358                     (total (+ left right)))
359                (setq emus-current-progress
360                      (format " %s/%s"
361                              (emus--timestamp left)
362                              (emus--timestamp total)))))
363             ))))
364      (emus--update-track old-track)
365      (emus--update-track track)
366      (emus--resume-cp)
367      (emus-goto-current))))
368
369 (defun emus-select-track (track)
370   "Set TRACK as current, but do not start playing."
371   (emus--with-library
372    (let ((old-track emus-current-track))
373      (setq emus-state 'stopped)
374      (setq emus-current-track track)
375      (emus--update-track old-track)
376      (emus--update-track track)
377      (emus-send-cmd "o")
378      (emus--resume-cp)
379      (emus-goto-current))))
380
381 (defun emus-stop ()
382   "Stop playback of the current track."
383   (interactive)
384   (emus--with-library
385    (setq emus-state 'stopped)
386    (emus--update-track emus-current-track)
387    (emus-send-cmd "s")))
388
389 (defun emus-playpause ()
390   "Begin playback of the current track.
391 If the track is already playing, pause playback.
392 If the track is currently paused, resume playback."
393   (interactive)
394   (emus--with-library
395    (when emus-current-track
396      (if (eq emus-state 'stopped)
397          (emus-play-track emus-current-track)
398        (emus-send-cmd "p")
399        (pcase emus-state
400          ((or 'paused 'stopped) (setq emus-state 'playing))
401          ('playing (setq emus-state 'paused)))
402        (unless (eq emus-state 'paused)))
403      (emus--update-track emus-current-track))))
404
405 (defun emus-set-volume (pct)
406   "Set the playback volume to PCT %."
407   (emus--with-library
408    (setq emus-current-volume pct)
409    (emus-send-cmd "v" (number-to-string pct))))
410
411 (defun emus-volume-increase-by (delta)
412   "Increase the playback volume by DELTA %."
413   (emus-set-volume (max 0 (min 100 (+ emus-current-volume delta)))))
414
415 (defun emus-volume-up ()
416   "Increase the playback volume by 10%."
417   (interactive)
418   (emus-volume-increase-by 10))
419
420 (defun emus-volume-down ()
421   "Decrease the playback volume by 10%."
422   (interactive)
423   (emus-volume-increase-by -10))
424
425 (defun emus--play-adjacent-track (&optional prev)
426   "Play the next track in the library, or the previous if PREV is non-nil."
427   (emus--with-library
428    (let ((idx (seq-position emus-tracks emus-current-track))
429          (offset (if prev -1 +1)))
430      (if idx
431          (let ((next-track (elt emus-tracks (+ idx offset))))
432            (if next-track
433                (if (eq emus-state 'playing)
434                    (emus-play-track next-track)
435                  (emus-select-track next-track))
436              (error "Track does not exist")))
437        (error "No track selected")))))
438
439 (defun emus--play-adjacent-album (&optional prev)
440   "Play the first track of the next album in the library.
441 If PREV is non-nil, plays the last track of the previous album."
442   (emus--with-library
443    (let ((idx (seq-position emus-tracks emus-current-track)))
444      (if idx
445          (let* ((search-list (if prev
446                                  (reverse (seq-subseq emus-tracks 0 idx))
447                                (seq-subseq emus-tracks (+ idx 1))))
448                 (current-album (emus-track-album emus-current-track))
449                 (next-track (seq-some (lambda (r)
450                                         (if (string= (emus-track-album r)
451                                                      current-album)
452                                             nil
453                                           r))
454                                       search-list)))
455            (if next-track
456                (if (eq emus-state 'playing)
457                    (emus-play-track next-track)
458                  (emus-select-track next-track))
459              (error "Track does not exist")))
460        (error "No track selected")))))
461
462 (defun emus-play-next ()
463   "Play the next track in the library."
464   (interactive)
465   (emus--play-adjacent-track))
466
467 (defun emus-play-prev ()
468   "Play the previous track in the library."
469   (interactive)
470   (emus--play-adjacent-track t))
471
472 (defun emus-play-next-album ()
473   "Play the first track of the next album in the library."
474   (interactive)
475   (emus--play-adjacent-album))
476
477 (defun emus-play-prev-album ()
478   "Play the last track of the previous album in the library."
479   (interactive)
480   (emus--play-adjacent-album t))
481
482 (defun emus-jump (seconds)
483   "Jump forward in current track by SECONDS seconds."
484   (emus--with-library
485    (emus-send-cmd "jump" (format "%+ds" seconds))))
486
487 (defun emus-jump-10s-forward ()
488   "Jump 10 seconds forward in current track."
489   (interactive)
490   (emus-jump 10))
491
492 (defun emus-jump-10s-backward ()
493   "Jump 10 seconds backward in current track."
494   (interactive)
495   (emus-jump -10))
496
497 (defun emus-jump-1m-forward ()
498   "Jump 1 minute forward in current track."
499   (interactive)
500   (emus-jump 60))
501
502 (defun emus-jump-1m-backward ()
503   "Jump 1 minute backward in current track."
504   (interactive)
505   (emus-jump -60))
506
507 (defun emus-display-status ()
508   "Display the current playback status in the minibuffer."
509   (interactive)
510   (emus--with-library
511    (message
512     (concat "Emus: Volume %d%%"
513             (pcase emus-state
514               ('stopped " [Stopped]")
515               ('paused (format " [Paused%s]" emus-current-progress))
516               ('playing (format " [Playing%s]" emus-current-progress))
517               (_ ""))
518             " %s")
519     emus-current-volume
520     (if emus-current-track
521         (format "- %.30s (%.20s, %.20s)"
522                 (emus-track-title emus-current-track)
523                 (emus-track-album emus-current-track)
524                 (emus-track-artist emus-current-track))
525       ""))))
526
527 (defun emus-toggle-progress-tracking ()
528   "Enable/disable progress tracking."
529   (interactive)
530   (setq emus-progress-enabled (not emus-progress-enabled))
531   (if emus-progress-enabled
532       (progn
533         (emus-send-cmd "progress")
534         (setq emus-current-progress ""))
535     (progn
536       (emus-send-cmd "silence")
537       (setq emus-current-progress " (progress diabled)"))))
538
539
540 ;;; Browser
541 ;;
542
543 (defun emus--insert-track (track &optional prev-track first)
544   "Insert a button representing TRACK into the current buffer.
545
546 When provided, PREV-TRACK is used to determine whether to insert additional
547 headers representing the artist or the album title.
548
549 If non-nil, FIRST indicates that the track is the first in the library
550 and thus requires both artist and album headers."
551   (let* ((artist (emus-track-artist track))
552          (album (emus-track-album track))
553          (title (emus-track-title track))
554          (album-symb (intern (concat artist album)))
555          (help-str (format "mouse-1, RET: Play '%.30s' (%.20s)" title artist))
556          (field (intern album))) ;Allows easy jumping between albums with cursor.
557     (when (or prev-track first)
558       (unless (equal (emus-track-artist prev-track) artist)
559         (insert-text-button
560          (propertize artist 'face 'emus-artist)
561          'action #'emus--click-track
562          'follow-link t
563          'help-echo help-str
564          'emus-track track
565          'field field)
566         (insert (propertize "\n"
567                             'face 'emus-artist
568                             'field field)))
569       (unless (equal (emus-track-album prev-track) album)
570         (insert-text-button
571          (propertize (concat "  " album) 'face 'emus-album)
572          'action #'emus--click-track
573          'follow-link t
574          'help-echo help-str
575          'emus-track track
576          'field field)
577         (insert (propertize "\n"
578                             'face 'emus-album
579                             'field field))))
580     (emus-set-track-browser-pos track (point))
581     (let ((is-current (equal track emus-current-track)))
582       (insert-text-button
583        (concat
584         (if is-current
585             (propertize
586              (pcase emus-state
587                ('playing "->")
588                ('paused "-)")
589                ('stopped "-]"))
590              'face 'emus-cursor)
591           (propertize "  " 'face 'default))
592         (propertize (format "   %s" title)
593                     'face (if is-current
594                               'emus-track-current
595                             'emus-track)))
596        'action #'emus--click-track
597        'follow-link t
598        'help-echo help-str
599        'emus-track track
600        'invisible album-symb
601        'field field)
602       (insert (propertize "\n"
603                           'face (if is-current
604                                     'emus-track-current
605                                   'emus-track)
606                           'field field
607                           'invisible album-symb)))))
608
609 (defun emus--update-track (track)
610   "Rerender entry for TRACK in emus browser buffer.
611 Used to update browser display when `emus-current-track' and/or `emus-state' changes."
612   (let ((track-pos (emus-track-browser-pos track)))
613     (when (and (get-buffer "*emus*")
614                (emus-track-browser-pos track))
615       (with-current-buffer "*emus*"
616         (let ((inhibit-read-only t)
617               (old-point (point)))
618           (goto-char track-pos)
619           (search-forward "\n")
620           (delete-region track-pos (point))
621           (goto-char track-pos)
622           (emus--insert-track track)
623           (goto-char old-point))))))
624
625 (defun emus--render-tracks ()
626   "Render all library tracks in emus browser buffer."
627   (with-current-buffer "*emus*"
628     (let ((inhibit-read-only t)
629           (old-pos (point)))
630       (erase-buffer)
631       (goto-char (point-min))
632       (let ((prev-track nil))
633         (dolist (track emus-tracks)
634           (emus--insert-track track prev-track (not prev-track))
635           (setq prev-track track)))
636       (goto-char old-pos))))
637
638 (defun emus--click-track (button)
639   "Begin playback of track indicated by BUTTON."
640   (emus-play-track (button-get button 'emus-track))
641   (emus-display-status))
642
643 (defun emus-goto-current ()
644   "Move point to the current track in the browser buffer, if available."
645   (interactive)
646   (when (and (get-buffer "*emus*")
647              emus-current-track)
648     (with-current-buffer "*emus*"
649         (goto-char (emus-track-browser-pos emus-current-track)))))
650
651 (defun emus-browse ()
652   "Switch to *emus* audio library browser."
653   (interactive)
654   (emus--with-library
655    (pop-to-buffer-same-window "*emus*")
656    (emus-browser-mode)
657    (emus--render-tracks)
658    (emus-goto-current)))
659
660 (defun emus-refresh ()
661   "Refresh the emus library."
662   (interactive)
663   (emus-stop)
664   (setq emus-tracks nil)
665   (emus-browse))
666
667
668 ;;; Playback + status display commands
669 ;;
670
671 (defun emus-playpause-status ()
672   "Start, pause or resume playback, then display the emus status in the minibuffer."
673   (interactive)
674   (emus-playpause)
675   (emus-display-status))
676
677 (defun emus-stop-status ()
678   "Stop playback, then display the emus status in the minibuffer."
679   (interactive)
680   (emus-stop)
681   (emus-display-status))
682
683 (defun emus-volume-up-status ()
684   "Increase volume by 10%, then display the emus status in the minibuffer."
685   (interactive)
686   (emus-volume-up)
687   (emus-display-status))
688
689 (defun emus-volume-down-status ()
690   "Decrease volume by 10%, then display the emus status in the minibuffer."
691   (interactive)
692   (emus-volume-down)
693   (emus-display-status))
694
695 (defun emus-play-next-status ()
696   "Play next track, then display the emus status in the minibuffer."
697   (interactive)
698   (emus-play-next)
699   (emus-display-status))
700
701 (defun emus-play-prev-status ()
702   "Play previous track, then display the emus status in the minibuffer."
703   (interactive)
704   (emus-play-prev)
705   (emus-display-status))
706
707 (defun emus-play-next-album-status ()
708   "Play first track of next album, then display the emus status in the minibuffer."
709   (interactive)
710   (emus-play-next-album)
711   (emus-display-status))
712
713 (defun emus-play-prev-album-status ()
714   "Play last track of previous album, then display the emus status in the minibuffer."
715   (interactive)
716   (emus-play-prev-album)
717   (emus-display-status))
718
719 (defun emus-jump-10s-forward-status ()
720   "Jump 10s forward in current track, then display the emus status in the minibuffer."
721   (interactive)
722   (emus-jump-10s-forward)
723   (emus-display-status))
724
725 (defun emus-jump-10s-backward-status ()
726   "Jump 10s backward in current track, then display the emus status in the minibuffer."
727   (interactive)
728   (emus-jump-10s-backward)
729   (emus-display-status))
730
731 (defun emus-jump-1m-forward-status ()
732   "Jump 10s forward in current track, then display the emus status in the minibuffer."
733   (interactive)
734   (emus-jump-1m-forward)
735   (emus-display-status))
736
737 (defun emus-jump-1m-backward-status ()
738   "Jump 10s backward in current track, then display the emus status in the minibuffer."
739   (interactive)
740   (emus-jump-1m-backward)
741   (emus-display-status))
742
743 (defun emus-toggle-progress-status ()
744   "Toggle progress tracking, then display the emus status in the minibuffer."
745   (interactive)
746   (emus-toggle-progress-tracking)
747   (emus-display-status))
748
749 (defun emus-goto-current-status ()
750   "Move point to the current track, then display the emus status in the minibuffer."
751   (interactive)
752   (emus-goto-current)
753   (emus-display-status))
754
755 (defun emus-refresh-status ()
756   "Refresh the emus library, then display the emus status in the minibuffer."
757   (interactive)
758   (emus-stop)
759   (setq emus-tracks nil)
760   (emus--with-library
761    (emus-browse)
762    (emus-display-status)))
763
764 (defvar emus-browser-mode-map
765   (let ((map (make-sparse-keymap)))
766     (define-key map (kbd "SPC") 'emus-playpause-status)
767     (define-key map (kbd "o") 'emus-stop-status)
768     (define-key map (kbd "+") 'emus-volume-up-status)
769     (define-key map (kbd "=") 'emus-volume-up-status)
770     (define-key map (kbd "-") 'emus-volume-down-status)
771     (define-key map (kbd "R") 'emus-refresh-status)
772     (define-key map (kbd "n") 'emus-play-next-status)
773     (define-key map (kbd "p") 'emus-play-prev-status)
774     (define-key map (kbd "N") 'emus-play-next-album-status)
775     (define-key map (kbd "P") 'emus-play-prev-album-status)
776     (define-key map (kbd ",") 'emus-jump-10s-backward-status)
777     (define-key map (kbd ".") 'emus-jump-10s-forward-status)
778     (define-key map (kbd "<") 'emus-jump-1m-backward-status)
779     (define-key map (kbd ">") 'emus-jump-1m-forward-status)
780     (define-key map (kbd "c") 'emus-goto-current-status)
781     (define-key map (kbd "!") 'emus-toggle-progress-status)
782     (when (fboundp 'evil-define-key*)
783       (evil-define-key* 'motion map
784                         (kbd "SPC") 'emus-playpause-status
785                         (kbd "o") 'emus-stop-status
786                         (kbd "+") 'emus-volume-up-status
787                         (kbd "=") 'emus-volume-up-status
788                         (kbd "-") 'emus-volume-down-status
789                         (kbd "R") 'emus-refresh-status
790                         (kbd "n") 'emus-play-next-status
791                         (kbd "p") 'emus-play-prev-status
792                         (kbd "N") 'emus-play-next-album-status
793                         (kbd "P") 'emus-play-prev-album-status
794                         (kbd ",") 'emus-jump-10s-backward-status
795                         (kbd ".") 'emus-jump-10s-forward-status
796                         (kbd "<") 'emus-jump-1m-backward-status
797                         (kbd ">") 'emus-jump-1m-forward-status
798                         (kbd "c") 'emus-goto-current-status
799                         (kbd "!") 'emus-toggle-progress-status))
800     map)
801   "Keymap for emus browser.")
802
803 (defun emus-restart ()
804   "Restart emus."
805   (interactive)
806   (emus-kill-process))
807
808 (define-derived-mode emus-browser-mode special-mode "emus-browser"
809   "Major mode for EMUS music player file browser."
810   (setq-local buffer-invisibility-spec nil))
811
812 (when (fboundp 'evil-set-initial-state)
813   (evil-set-initial-state 'emus-browser-mode 'motion))
814
815 ;;; emus.el ends here