+ (emus-volume-increase-by 10))
+
+(defun emus-volume-down ()
+ "Decrease the playback volume by 10%."
+ (interactive)
+ (emus-volume-increase-by -10))
+
+(defun emus--play-adjacent-track (&optional prev)
+ "Play the next track in the library, or the previous if PREV is non-nil."
+ (emus--with-library
+ (let ((idx (seq-position emus-tracks emus-current-track))
+ (offset (if prev -1 +1)))
+ (if idx
+ (let ((next-track (elt emus-tracks (+ idx offset))))
+ (if next-track
+ (if (eq emus-state 'playing)
+ (emus-play-track next-track)
+ (emus-select-track next-track))
+ (error "Track does not exist")))
+ (error "No track selected")))))
+
+(defun emus--play-adjacent-album (&optional prev)
+ "Play the first track of the next album in the library.
+If PREV is non-nil, plays the last track of the previous album."
+ (emus--with-library
+ (let ((idx (seq-position emus-tracks emus-current-track)))
+ (if idx
+ (let* ((search-list (if prev
+ (reverse (seq-subseq emus-tracks 0 idx))
+ (seq-subseq emus-tracks (+ idx 1))))
+ (current-album (emus-track-album emus-current-track))
+ (next-track (seq-some (lambda (r)
+ (if (string= (emus-track-album r)
+ current-album)
+ nil
+ r))
+ search-list)))
+ (if next-track
+ (if (eq emus-state 'playing)
+ (emus-play-track next-track)
+ (emus-select-track next-track))
+ (error "Track does not exist")))
+ (error "No track selected")))))
+
+(defun emus-play-next ()
+ "Play the next track in the library."
+ (interactive)
+ (emus--play-adjacent-track))
+
+(defun emus-play-prev ()
+ "Play the previous track in the library."
+ (interactive)
+ (emus--play-adjacent-track t))
+
+(defun emus-play-next-album ()
+ "Play the first track of the next album in the library."
+ (interactive)
+ (emus--play-adjacent-album))
+
+(defun emus-play-prev-album ()
+ "Play the last track of the previous album in the library."
+ (interactive)
+ (emus--play-adjacent-album t))
+
+(defun emus-jump (seconds)
+ "Jump forward in current track by SECONDS seconds."
+ (emus--with-library
+ (emus-send-cmd "jump" (format "%+ds" seconds))))
+
+(defun emus-jump-10s-forward ()
+ "Jump 10 seconds forward in current track."
+ (interactive)
+ (emus-jump 10))
+
+(defun emus-jump-10s-backward ()
+ "Jump 10 seconds backward in current track."
+ (interactive)
+ (emus-jump -10))
+
+(defun emus-jump-1m-forward ()
+ "Jump 1 minute forward in current track."
+ (interactive)
+ (emus-jump 60))
+
+(defun emus-jump-1m-backward ()
+ "Jump 1 minute backward in current track."
+ (interactive)
+ (emus-jump -60))
+
+(defun emus-display-status ()
+ "Display the current playback status in the minibuffer."
+ (interactive)
+ (emus--with-library
+ (message
+ (concat "Emus: Volume %d%%"
+ (pcase emus-state
+ ('stopped " [Stopped]")
+ ('paused (format " [Paused%s]" emus-current-progress))
+ ('playing (format " [Playing%s]" emus-current-progress))
+ (_ ""))
+ " %s")
+ emus-current-volume
+ (if emus-current-track
+ (format "- %.30s (%.20s, %.20s)"
+ (emus-track-title emus-current-track)
+ (emus-track-album emus-current-track)
+ (emus-track-artist emus-current-track))
+ ""))))
+
+(defun emus-toggle-progress-tracking ()
+ "Enable/disable progress tracking."
+ (interactive)
+ (setq emus-progress-enabled (not emus-progress-enabled))
+ (if emus-progress-enabled
+ (progn
+ (emus-send-cmd "progress")
+ (setq emus-current-progress ""))
+ (progn
+ (emus-send-cmd "silence")
+ (setq emus-current-progress " (progress diabled)"))))
+
+
+;;; Browser
+;;
+
+(defun emus--insert-track (track &optional prev-track first)
+ "Insert a button representing TRACK into the current buffer.
+
+When provided, PREV-TRACK is used to determine whether to insert additional
+headers representing the artist or the album title.
+
+If non-nil, FIRST indicates that the track is the first in the library
+and thus requires both artist and album headers."
+ (let* ((artist (emus-track-artist track))
+ (album (emus-track-album track))
+ (title (emus-track-title track))
+ (album-symb (intern (concat artist album)))
+ (help-str (format "mouse-1, RET: Play '%.30s' (%.20s)" title artist))
+ (field (intern album))) ;Allows easy jumping between albums with cursor.
+ (when (or prev-track first)
+ (unless (equal (emus-track-artist prev-track) artist)
+ (insert-text-button
+ (propertize artist 'face 'emus-artist)
+ 'action #'emus--click-track
+ 'follow-link t
+ 'help-echo help-str
+ 'emus-track track
+ 'field field)
+ (insert (propertize "\n"
+ 'face 'emus-artist
+ 'field field)))
+ (unless (equal (emus-track-album prev-track) album)
+ (insert-text-button
+ (propertize (concat " " album) 'face 'emus-album)
+ 'action #'emus--click-track
+ 'follow-link t
+ 'help-echo help-str
+ 'emus-track track
+ 'field field)
+ (insert (propertize "\n"
+ 'face 'emus-album
+ 'field field))))
+ (emus-set-track-browser-pos track (point))
+ (let ((is-current (equal track emus-current-track)))
+ (insert-text-button
+ (concat
+ (if is-current
+ (propertize
+ (pcase emus-state
+ ('playing "->")
+ ('paused "-)")
+ ('stopped "-]"))
+ 'face 'emus-cursor)
+ (propertize " " 'face 'default))
+ (propertize (format " %s" title)
+ 'face (if is-current
+ 'emus-track-current
+ 'emus-track)))
+ 'action #'emus--click-track
+ 'follow-link t
+ 'help-echo help-str
+ 'emus-track track
+ 'invisible album-symb
+ 'field field)
+ (insert (propertize "\n"
+ 'face (if is-current
+ 'emus-track-current
+ 'emus-track)
+ 'field field
+ 'invisible album-symb)))))
+
+(defun emus--update-track (track)
+ "Rerender entry for TRACK in emus browser buffer.
+Used to update browser display when `emus-current-track' and/or `emus-state' changes."
+ (let ((track-pos (emus-track-browser-pos track)))
+ (when (and (get-buffer "*emus*")
+ (emus-track-browser-pos track))
+ (with-current-buffer "*emus*"
+ (let ((inhibit-read-only t)
+ (old-point (point)))
+ (goto-char track-pos)
+ (search-forward "\n")
+ (delete-region track-pos (point))
+ (goto-char track-pos)
+ (emus--insert-track track)
+ (goto-char old-point))))))
+
+(defun emus--render-tracks ()
+ "Render all library tracks in emus browser buffer."
+ (with-current-buffer "*emus*"
+ (let ((inhibit-read-only t)
+ (old-pos (point)))
+ (erase-buffer)
+ (goto-char (point-min))
+ (let ((prev-track nil))
+ (dolist (track emus-tracks)
+ (emus--insert-track track prev-track (not prev-track))
+ (setq prev-track track)))
+ (goto-char old-pos))))
+
+(defun emus--click-track (button)
+ "Begin playback of track indicated by BUTTON."
+ (emus-play-track (button-get button 'emus-track))
+ (emus-display-status))
+
+(defun emus-goto-current ()
+ "Move point to the current track in the browser buffer, if available."
+ (interactive)
+ (when (and (get-buffer "*emus*")
+ emus-current-track)
+ (with-current-buffer "*emus*"
+ (goto-char (emus-track-browser-pos emus-current-track)))))
+
+(defun emus-browse ()
+ "Switch to *emus* audio library browser."
+ (interactive)
+ (emus--with-library
+ (pop-to-buffer-same-window "*emus*")
+ (emus-browser-mode)
+ (emus--render-tracks)
+ (emus-goto-current)))
+
+(defun emus-refresh ()
+ "Refresh the emus library."
+ (interactive)
+ (emus-stop)
+ (setq emus-tracks nil)
+ (emus-browse))
+
+
+;;; Playback + status display commands
+;;
+
+(defun emus-playpause-status ()
+ "Start, pause or resume playback, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-playpause)
+ (emus-display-status))
+
+(defun emus-stop-status ()
+ "Stop playback, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-stop)
+ (emus-display-status))
+
+(defun emus-volume-up-status ()
+ "Increase volume by 10%, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-volume-up)
+ (emus-display-status))
+
+(defun emus-volume-down-status ()
+ "Decrease volume by 10%, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-volume-down)
+ (emus-display-status))
+
+(defun emus-play-next-status ()
+ "Play next track, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-play-next)
+ (emus-display-status))
+
+(defun emus-play-prev-status ()
+ "Play previous track, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-play-prev)
+ (emus-display-status))
+
+(defun emus-play-next-album-status ()
+ "Play first track of next album, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-play-next-album)
+ (emus-display-status))
+
+(defun emus-play-prev-album-status ()
+ "Play last track of previous album, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-play-prev-album)
+ (emus-display-status))
+
+(defun emus-jump-10s-forward-status ()
+ "Jump 10s forward in current track, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-jump-10s-forward)
+ (emus-display-status))
+
+(defun emus-jump-10s-backward-status ()
+ "Jump 10s backward in current track, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-jump-10s-backward)
+ (emus-display-status))
+
+(defun emus-jump-1m-forward-status ()
+ "Jump 10s forward in current track, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-jump-1m-forward)
+ (emus-display-status))
+
+(defun emus-jump-1m-backward-status ()
+ "Jump 10s backward in current track, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-jump-1m-backward)
+ (emus-display-status))
+
+(defun emus-toggle-progress-status ()
+ "Toggle progress tracking, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-toggle-progress-tracking)
+ (emus-display-status))
+
+(defun emus-goto-current-status ()
+ "Move point to the current track, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-goto-current)
+ (emus-display-status))
+
+(defun emus-refresh-status ()
+ "Refresh the emus library, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-stop)
+ (setq emus-tracks nil)
+ (emus-browse)
+ (emus-display-status))
+
+(defun emus-restart-browse ()
+ "Restart the emus process, then refresh the browse window."
+ (interactive)
+ (message "Restarting mpg123.")
+ (emus-kill-process)
+ (run-at-time 0.1 nil #'emus-browse)) ;Slight delay to wait for kill signal to take effect
+
+(defun emus-restart-status ()
+ "Restart the emus process, then display the status."
+ (interactive)
+ (message "Restarting mpg123.")
+ (emus-kill-process)
+ (run-at-time 0.1 nil #'emus-display-status)) ;Slight delay to wait for kill signal to take effect
+
+
+(defvar emus-browser-mode-map
+ (let ((map (make-sparse-keymap)))
+ (define-key map (kbd "SPC") 'emus-playpause-status)
+ (define-key map (kbd "o") 'emus-stop-status)
+ (define-key map (kbd "+") 'emus-volume-up-status)
+ (define-key map (kbd "=") 'emus-volume-up-status)
+ (define-key map (kbd "-") 'emus-volume-down-status)
+ (define-key map (kbd "R") 'emus-refresh-status)
+ (define-key map (kbd "n") 'emus-play-next-status)
+ (define-key map (kbd "p") 'emus-play-prev-status)
+ (define-key map (kbd "N") 'emus-play-next-album-status)
+ (define-key map (kbd "P") 'emus-play-prev-album-status)
+ (define-key map (kbd ",") 'emus-jump-10s-backward-status)
+ (define-key map (kbd ".") 'emus-jump-10s-forward-status)
+ (define-key map (kbd "<") 'emus-jump-1m-backward-status)
+ (define-key map (kbd ">") 'emus-jump-1m-forward-status)
+ (define-key map (kbd "c") 'emus-goto-current-status)
+ (define-key map (kbd "#") 'emus-toggle-progress-status)
+ (define-key map (kbd "!") 'emus-restart-browse)
+ (when (fboundp 'evil-define-key*)
+ (evil-define-key* 'motion map
+ (kbd "SPC") 'emus-playpause-status
+ (kbd "o") 'emus-stop-status
+ (kbd "+") 'emus-volume-up-status
+ (kbd "=") 'emus-volume-up-status
+ (kbd "-") 'emus-volume-down-status
+ (kbd "R") 'emus-refresh-status
+ (kbd "n") 'emus-play-next-status
+ (kbd "p") 'emus-play-prev-status
+ (kbd "N") 'emus-play-next-album-status
+ (kbd "P") 'emus-play-prev-album-status
+ (kbd ",") 'emus-jump-10s-backward-status
+ (kbd ".") 'emus-jump-10s-forward-status
+ (kbd "<") 'emus-jump-1m-backward-status
+ (kbd ">") 'emus-jump-1m-forward-status
+ (kbd "c") 'emus-goto-current-status
+ (kbd "#") 'emus-toggle-progress-status
+ (kbd "!") #'emus-restart-browse))
+ map)
+ "Keymap for emus browser.")
+
+(define-derived-mode emus-browser-mode special-mode "emus-browser"
+ "Major mode for EMUS music player file browser."
+ (setq-local buffer-invisibility-spec nil))
+
+(when (fboundp 'evil-set-initial-state)
+ (evil-set-initial-state 'emus-browser-mode 'motion))
+
+;;; emus.el ends here