From 7ddfda9b0af623d5039f7073f6c043f370c129cc Mon Sep 17 00:00:00 2001 From: jao Date: Mon, 31 May 2021 05:21:13 +0100 Subject: jao-mpc: more fat trimming: mpdel to the attic --- lib/media/jao-mpc.el | 81 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 25 deletions(-) (limited to 'lib/media') diff --git a/lib/media/jao-mpc.el b/lib/media/jao-mpc.el index 512d21b..9004920 100644 --- a/lib/media/jao-mpc.el +++ b/lib/media/jao-mpc.el @@ -20,7 +20,7 @@ ;;; Commentary: -;; Simple mpd control using mpc commands. +;; Simple mpd control using elmpd and mpc. ;;; Code: @@ -29,6 +29,9 @@ (require 'jao-lyrics) (defvar jao-mpc--connection nil) +(defvar-local jao-mpc--local-connecton nil) +(defvar-local jao-mpc--local-port nil) + (defvar jao-mpc-host "localhost") (defvar jao-mpc-port 6600) @@ -38,15 +41,18 @@ (delete-process (elmpd-connection--fd jao-mpc--connection)) (setq jao-mpc--connection nil))) +(defun jao-mpc--connect (name &optional cb) + (elmpd-connect :name name + :host jao-mpc-host + :port jao-mpc-port + :subsystems + (when cb `((player) . ,cb)))) + (defun jao-mpc-connect (&optional force) (interactive) (when force (jao-mpc-disconnect)) (unless jao-mpc--connection - (setq jao-mpc--connection - (elmpd-connect :name "jao-mpc" - :host jao-mpc-host - :port jao-mpc-port - :subsystems '((player) . jao-mpc--watcher))) + (setq jao-mpc--connection (jao-mpc--connect "jao-mpc" 'jao-mpc--watcher)) (jao-mpc--watcher jao-mpc--connection 'player)) jao-mpc--connection) @@ -57,14 +63,17 @@ (defvar jao-mpc--current '()) (defvar jao-mpc-minibuffer-str "") +(defun jao-mpc--parse-retort (txt) + (let (res) + (dolist (e (split-string txt "\n" t " ") res) + (let ((e (split-string e ": " t " "))) + (when (and (car e) (cadr e)) + (push (cons (car e) (cadr e)) res)))))) + (defun jao-mpc--update-status (next) (let ((cb (lambda (_c ok txt) (when ok - (setq jao-mpc--play-status '()) - (dolist (e (split-string txt "\n" t " ")) - (let ((e (split-string e ": " t " "))) - (when (and (car e) (cadr e)) - (push (cons (car e) (cadr e)) jao-mpc--play-status)))) + (setq jao-mpc--play-status (jao-mpc--parse-retort txt)) (when next (funcall next)))))) (jao-mpc--send "status" cb))) @@ -98,15 +107,12 @@ (if (jao-mpc--playing-p) (jao-mpc--current-str) "")) (jao-minibuffer-refresh)) -(defun jao-mpc--update-current () +(defun jao-mpc--update-current (&optional next) (let ((cb (lambda (_c ok txt) (when ok - (setq jao-mpc--current '()) - (dolist (e (split-string txt "\n" t " ")) - (let ((e (split-string e ": " t " "))) - (when (and (car e) (cadr e)) - (push (cons (car e) (cadr e)) jao-mpc--current)))) - (jao-mpc--update-minibuffer))))) + (setq jao-mpc--current (jao-mpc--parse-retort txt)) + (jao-mpc--update-minibuffer) + (when next (funcall next)))))) (jao-mpc--send "currentsong" cb))) (defun jao-mpc--watcher (_conn _subsys) @@ -148,20 +154,37 @@ "Mode to display the list of playlist known by mpd." (read-only-mode -1) (delete-region (point-min) (point-max)) - (insert (shell-command-to-string "mpc playlist")) + (insert (shell-command-to-string (format "mpc -p %s playlist" jao-mpc-port))) (goto-char (point-min)) + (display-line-numbers-mode) (read-only-mode 1)) +(defun jao-mpc--playlist-goto-current () + (interactive) + (let ((jao-mpc--connection + (or jao-mpc--local-connecton jao-mpc--local-connecton))) + (jao-mpc--update-current + (lambda () + (when-let (no (string-to-number (jao-mpc--current-get "Pos"))) + (goto-char (point-min)) + (forward-line no)))))) + +(defun jao-mpc--playlist-play () + (interactive) + (shell-command-to-string (format "mpc -p %s play %s" + (or jao-mpc--local-port jao-mpc-port) + (line-number-at-pos)))) + (define-key jao-mpc-playlist-mode-map (kbd "n") #'next-line) (define-key jao-mpc-playlist-mode-map (kbd "p") #'previous-line) (define-key jao-mpc-playlist-mode-map (kbd "q") #'bury-buffer) +(define-key jao-mpc-playlist-mode-map (kbd ".") #'jao-mpc--playlist-goto-current) +(define-key jao-mpc-playlist-mode-map (kbd "RET") #'jao-mpc--playlist-play) (defun jao-mpc--playlist-buffer () - (if-let (b (get-buffer jao-mpc--playlist)) - b - (with-current-buffer (get-buffer-create jao-mpc--playlist) - (jao-mpc-playlist-mode) - (current-buffer)))) + (with-current-buffer (get-buffer-create jao-mpc--playlist) + (jao-mpc-playlist-mode) + (current-buffer))) ;;;###autoload (defun jao-mpc-stop () @@ -188,6 +211,11 @@ (interactive) (jao-mpc--send "previous" nil)) +;;;###autoload +(defun jao-mpc-clear () + (interactive) + (jao-mpc--send "clear" nil)) + ;;;###autoload (defun jao-mpc-echo-current () (interactive) @@ -203,7 +231,10 @@ (defun jao-mpc-show-playlist () "Show current playlist." (interactive) - (pop-to-buffer (jao-mpc--playlist-buffer))) + (pop-to-buffer (jao-mpc--playlist-buffer)) + (setq-local jao-mpc--local-connecton jao-mpc--connection + jao-mpc--local-port jao-mpc-port) + (jao-mpc--playlist-goto-current)) ;;;###autoload (defun jao-mpc-lyrics-track-data () -- cgit v1.2.3