From 3a8ba30d1fb8d94ab3c63aa678a0059cc8b48b2f Mon Sep 17 00:00:00 2001 From: Yoni Rabkin Date: Thu, 29 Apr 2021 12:53:15 -0400 Subject: *.el: Minor improvements and simplifications * emms-playlist-limit.el (emms-playlist-limit--limit-playlist): Simplify. * emms-playing-time.el (emms-playing-time-display): Simplify. * emms-player-mpv.el (emms-player-mpv-ipc-proc) (emms-player-mpv-ipc-connect-command, emms-player-mpv-ipc-id) (emms-player-mpv-ipc-stop-command) (emms-player-mpv-event-connect-hook) (emms-player-mpv-ipc-conn-emacs-26.1-workaround): Fit docstrings with 80 columns. * emms-player-mpd.el (emms-player-mpd-ensure-process): Simplify `if` with identical branches. * emms-browser.el (emms-browser-run-mode-hooks): Simplify and move it out of `eval-and-compile`. (emms-browser-sort-cadr, emms-browser-sort-car): Add `debug` spec. patch by Stefan Monnier --- emms-browser.el | 155 +++++++++++++++++++++++++------------------------ emms-player-mpd.el | 4 +- emms-player-mpv.el | 37 +++++++----- emms-playing-time.el | 94 ++++++++++++++---------------- emms-playlist-limit.el | 8 ++- emms-playlist-mode.el | 2 +- emms-tag-editor.el | 1 + 7 files changed, 155 insertions(+), 146 deletions(-) diff --git a/emms-browser.el b/emms-browser.el index fcb4ef3..2adada7 100644 --- a/emms-browser.el +++ b/emms-browser.el @@ -482,19 +482,20 @@ Called once for each directory." ;; -------------------------------------------------- (eval-and-compile - (if (fboundp 'with-selected-window) - (defalias 'emms-browser-with-selected-window 'with-selected-window) + (if (fboundp 'with-selected-window) ;Emacs-22 + (defalias 'emms-browser-with-selected-window #'with-selected-window) (defmacro emms-browser-with-selected-window (window &rest body) (ignore window) ;; this emulates the behavior introduced earlier, though it ;; might be best to do something with `window' `(save-selected-window ,body))) (put 'emms-browser-with-selected-window 'lisp-indent-function 1) - (put 'emms-browser-with-selected-window 'edebug-form-spec '(form body)) + (put 'emms-browser-with-selected-window 'edebug-form-spec '(form body))) - (if (fboundp 'run-mode-hooks) - (defalias 'emms-browser-run-mode-hooks 'run-mode-hooks) - (defalias 'emms-browser-run-mode-hooks 'run-hooks))) +(defalias 'emms-browser-run-mode-hooks + (if (fboundp 'run-mode-hooks) ;Emacs-22 + #'run-mode-hooks + #'run-hooks)) ;; -------------------------------------------------- ;; General mode setup @@ -534,7 +535,7 @@ example function is `emms-browse-by-artist'." (defun emms-browser-mode (&optional no-update) "A major mode for the Emms browser. \\{emms-browser-mode-map}" - ;; create a new buffer + ;; create a new buffer (interactive) (use-local-map emms-browser-mode-map) @@ -592,10 +593,10 @@ If a browser search exists, return it." "Create an interactive function emms-browse-by-NAME." (let ((funname (intern (concat "emms-browse-by-" name))) (funcdesc (concat "Browse by " name "."))) - `(defun ,funname () - ,funcdesc - (interactive) - (emms-browse-by ,type)))) + `(defun ,funname () + ,funcdesc + (interactive) + (emms-browse-by ,type)))) (defun emms-browse-by (type) "Render a top level buffer based on TYPE." @@ -699,7 +700,7 @@ For 'info-year TYPE, use 'info-originalyear, 'info-originaldate and (sort-subr nil #'forward-line #'end-of-line (lambda () (buffer-substring-no-properties - (line-beginning-position) (line-end-position))) + (line-beginning-position) (line-end-position))) nil emms-browser-alpha-sort-function)) (sort-lines nil (point-min) (point-max)))))) @@ -796,14 +797,14 @@ artist1 -> album1 -> *track* 1.." (let ((key (car entry)) (track (cadr entry)) artist title) ;; only the first track - (cond - ((eq type 'info-title) - (setq artist (emms-track-get track 'info-artist)) - (setq title (emms-track-get track 'info-title)) - (if (not (and artist title)) - key - (concat artist " - " title))) - (t key)))) + (cond + ((eq type 'info-title) + (setq artist (emms-track-get track 'info-artist)) + (setq title (emms-track-get track 'info-title)) + (if (not (and artist title)) + key + (concat artist " - " title))) + (t key)))) (defun emms-browser-track-number (track) "Return a string representation of a track number. @@ -863,7 +864,7 @@ Items with no metadata for TYPE will be placed in 'misc'" (dolist (track tracks) (setq key (emms-browser-get-track-field track type)) (when (eq type 'info-title) - ;; try and make every track unique + ;; try and make every track unique (setq tracknum (emms-browser-track-number track)) (if (string= tracknum "") (setq key (file-name-nondirectory @@ -915,6 +916,7 @@ TYPE is the metadata to make the alist by - eg. if it's This sorting predicate will compare the cadr of each entry. SORT-FUNC should be a playlist sorting predicate like `emms-playlist-sort-by-natural-order'." + (declare (debug t)) `(lambda (a b) (funcall ,sort-func (cadr a) (cadr b)))) @@ -923,6 +925,7 @@ SORT-FUNC should be a playlist sorting predicate like This sorting predicate will compare the car of each entry. SORT-FUNC should be a playlist sorting predicate like `emms-playlist-sort-by-natural-order'." + (declare (debug t)) `(lambda (a b) (funcall ,sort-func (car a) (car b)))) @@ -931,7 +934,7 @@ SORT-FUNC should be a playlist sorting predicate like Uses `emms-browser-track-sort-function'." (if emms-browser-track-sort-function (sort alist (emms-browser-sort-cadr - emms-browser-track-sort-function)) + emms-browser-track-sort-function)) alist)) (defun emms-browser-sort-by-name (alist) @@ -939,7 +942,7 @@ Uses `emms-browser-track-sort-function'." Uses `emms-browser-alpha-sort-function'." (if emms-browser-alpha-sort-function (sort alist (emms-browser-sort-car - emms-browser-alpha-sort-function)) + emms-browser-alpha-sort-function)) alist)) (defun emms-browser-sort-by-year-or-name (alist) @@ -1405,12 +1408,12 @@ tracks from point, it does not delete files." (when (numberp delete) (setq delete nil)) (when delete (save-mark-and-excursion - (when (use-region-p) (goto-char start)) - (let ((lines (min count (- (line-number-at-pos (point-max)) (line-number-at-pos (point)))))) - (dotimes (_ lines) - ;; TODO: Test this! - (setq tracks (append tracks (emms-browser-tracks-at-point))) - (forward-line)))) + (when (use-region-p) (goto-char start)) + (let ((lines (min count (- (line-number-at-pos (point-max)) (line-number-at-pos (point)))))) + (dotimes (_ lines) + ;; TODO: Test this! + (setq tracks (append tracks (emms-browser-tracks-at-point))) + (forward-line)))) (unless (yes-or-no-p (format "Really permanently delete these %d tracks? " (length tracks))) (error "Cancelled!")) @@ -1497,47 +1500,47 @@ configuration." #'emms-browser-hide-linked-window) ;; switch to the playlist window when adding tracks? (add-hook 'emms-browser-tracks-added-hook - (lambda (start-of-tracks) (interactive) - (let (playlist-window) - (when emms-browser-switch-to-playlist-on-add - (emms-smart-browse)) - ;; center on the first added track/group name - (when - (setq playlist-window - (emms-browser-get-linked-window)) - (emms-browser-with-selected-window - playlist-window - (goto-char start-of-tracks) - (recenter '(4))))))) + (lambda (start-of-tracks) (interactive) + (let (playlist-window) + (when emms-browser-switch-to-playlist-on-add + (emms-smart-browse)) + ;; center on the first added track/group name + (when + (setq playlist-window + (emms-browser-get-linked-window)) + (emms-browser-with-selected-window + playlist-window + (goto-char start-of-tracks) + (recenter '(4))))))) (let (wind) - (cond - ((eq major-mode 'emms-browser-mode) - (setq wind (emms-browser-get-linked-window)) - ;; if the playlist window is visible, select it - (if wind - (select-window wind) - ;; otherwise display and select it - (select-window (emms-browser-display-playlist)))) - ((eq major-mode 'emms-playlist-mode) - (setq wind (emms-browser-get-linked-window)) - ;; if the playlist window is selected, and the browser is visible, - ;; hide both - (if wind - (progn + (cond + ((eq major-mode 'emms-browser-mode) + (setq wind (emms-browser-get-linked-window)) + ;; if the playlist window is visible, select it + (if wind (select-window wind) - (emms-browser-bury-buffer) - ;; After a browser search, the following buffer could be the - ;; unfiltered browser, which we want to bury as well. We don't want - ;; to call `emms-browser-hide-display-hook' for this one so we bury it - ;; directly. - (when (eq major-mode 'emms-browser-mode) - (bury-buffer))) - ;; otherwise bury both - (bury-buffer) - (emms-browser-hide-linked-window))) - (t - ;; show both - (emms-browser))))) + ;; otherwise display and select it + (select-window (emms-browser-display-playlist)))) + ((eq major-mode 'emms-playlist-mode) + (setq wind (emms-browser-get-linked-window)) + ;; if the playlist window is selected, and the browser is visible, + ;; hide both + (if wind + (progn + (select-window wind) + (emms-browser-bury-buffer) + ;; After a browser search, the following buffer could be the + ;; unfiltered browser, which we want to bury as well. We don't want + ;; to call `emms-browser-hide-display-hook' for this one so we bury it + ;; directly. + (when (eq major-mode 'emms-browser-mode) + (bury-buffer))) + ;; otherwise bury both + (bury-buffer) + (emms-browser-hide-linked-window))) + (t + ;; show both + (emms-browser))))) (defun emms-browser-get-linked-buffer () "Return linked buffer (eg browser if playlist is selected." @@ -1867,11 +1870,11 @@ If > album level, most of the track data will not make sense." (name (cond ((or (eq type 'info-year) (eq type 'info-genre)) "year/genre") - ((eq type 'info-artist) "artist") - ((eq type 'info-composer) "composer") - ((eq type 'info-performer) "performer") - ((eq type 'info-album) "album") - ((eq type 'info-title) "track")))) + ((eq type 'info-artist) "artist") + ((eq type 'info-composer) "composer") + ((eq type 'info-performer) "performer") + ((eq type 'info-album) "album") + ((eq type 'info-title) "track")))) (intern (concat "emms-browser-" name "-face")))) @@ -2015,11 +2018,11 @@ the text that it generates." This: - defines an interactive function M-x emms-browser-show-NAME. - defines a variable emms-browser-filter-NAME of (name . func). - - adds the filter to emms-browser-filters." + - adds the filter to `emms-browser-filters'." (let ((funcnam (intern (concat "emms-browser-show-" name))) (var (intern (concat "emms-browser-filter-" name))) (desc (concat "Filter the cache using rule '" - name "'"))) + name "'"))) `(progn (defvar ,var nil ,desc) (setq ,var (cons ,name ,func)) @@ -2046,7 +2049,7 @@ This does not refresh the current buffer." "Redisplay with the next filter." (interactive) (let* ((list (if reverse - (reverse emms-browser-filters) + (reverse emms-browser-filters) emms-browser-filters)) (key emms-browser-current-filter-name) (next (cadr (member (assoc key list) list)))) diff --git a/emms-player-mpd.el b/emms-player-mpd.el index 7ae48dc..39f23d5 100644 --- a/emms-player-mpd.el +++ b/emms-player-mpd.el @@ -311,9 +311,7 @@ return at the end of a request.") #'emms-player-mpd-sentinel) (setq emms-player-mpd-queue (tq-create emms-player-mpd-process)) - (if (fboundp 'set-process-query-on-exit-flag) - (set-process-query-on-exit-flag emms-player-mpd-process nil) - (set-process-query-on-exit-flag emms-player-mpd-process nil)) + (set-process-query-on-exit-flag emms-player-mpd-process nil) ;; send password (when (stringp emms-player-mpd-server-password) (tq-enqueue emms-player-mpd-queue diff --git a/emms-player-mpv.el b/emms-player-mpv.el index 69f81fa..399ec82 100644 --- a/emms-player-mpv.el +++ b/emms-player-mpv.el @@ -181,7 +181,8 @@ if it refuses to exit cleanly on `emms-player-mpv-proc-stop'.") (defvar emms-player-mpv-ipc-proc nil - "Unix socket process that communicates with running `emms-player-mpv-proc' instance.") + "Process that communicates with running `emms-player-mpv-proc' instance. +Connected via a Unix socket.") (defvar emms-player-mpv-ipc-buffer " *emms-player-mpv-ipc*" "Buffer to associate with `emms-player-mpv-ipc-proc' socket/pipe process.") @@ -193,33 +194,40 @@ if it refuses to exit cleanly on `emms-player-mpv-proc-stop'.") "List of delays before initiating socket connection for new mpv process.") (defvar emms-player-mpv-ipc-connect-command nil - "JSON command for `emms-player-mpv-ipc-sentinel' to run as soon as it connects to mpv. -I.e. last command that either initiated connection or was used while connecting to mpv. + "JSON command for `emms-player-mpv-ipc-sentinel' to run when it connects to mpv. +I.e. last command that either initiated connection or was used while +connecting to mpv. Set by `emms-player-mpv-start' and such, cleared once it gets sent by `emms-player-mpv-ipc-sentinel'.") (defvar emms-player-mpv-ipc-id 1 - "Auto-incremented value sent in JSON requests for request_id and observe_property id's. -Use `emms-player-mpv-ipc-id-get' to get and increment this value, instead of using it directly. -Wraps-around upon reaching `emms-player-mpv-ipc-id-max' (unlikely to ever happen).") + "Auto-incremented counter for unique JSON request identifiers. +Use for for `request_id' and `observe_property' identifiers. +Use `emms-player-mpv-ipc-id-get' to get and increment this value, +instead of using it directly. +Wraps-around upon reaching `emms-player-mpv-ipc-id-max' +\(unlikely to ever happen).") (defvar emms-player-mpv-ipc-id-max (expt 2 30) "Max value for `emms-player-mpv-ipc-id' to wrap around after. -Should be fine with both mpv and emacs, and probably never reached anyway.") +Should be fine with both mpv and Emacs, and probably never reached anyway.") (defvar emms-player-mpv-ipc-req-table nil "Auto-initialized hash table of outstanding API req_ids to their handler funcs.") (defvar emms-player-mpv-ipc-stop-command nil "Internal flag to track when stop command starts/finishes before next loadfile. -Set to either nil, t or playback start function to call on end-file event after stop command. -This is a workaround for mpv-0.30+ behavior, where 'stop + loadfile' only runs 'stop'.") +Set to either nil, t or the playback start function to call on end-file event +after stop command. +This is a workaround for mpv-0.30+ behavior, where 'stop + loadfile' only +runs 'stop'.") (defvar emms-player-mpv-event-connect-hook nil - "Normal hook run right after establishing new JSON IPC -connection to mpv instance and before `emms-player-mpv-ipc-connect-command' if any. -Best place to send any observe_property, request_log_messages, enable_event commands. + "Normal hook run right after establishing new JSON IPC connection to mpv. +Run before `emms-player-mpv-ipc-connect-command', if any. +Best place to send any `observe_property', `request_log_messages', +`enable_event' commands. Use `emms-player-mpv-ipc-id-get' to get unique id values for these. See also `emms-player-mpv-event-functions'.") @@ -246,8 +254,9 @@ to indicate that playback should stop instead of switching to next track.") (and (= emacs-major-version 26) (= emacs-minor-version 1)) "Non-nil to enable workaround for issue #31901 in emacs 26.1. -Emacs 26.1 fails to indicate missing socket file error for unix socket network processes -that were started with :nowait t, so blocking connections are used there instead.") +Emacs 26.1 fails to indicate missing socket file error for unix socket +network processes that were started with :nowait t, so blocking connections +are used there instead.") ;; ----- helpers diff --git a/emms-playing-time.el b/emms-playing-time.el index 11dc9e3..e99dc64 100644 --- a/emms-playing-time.el +++ b/emms-playing-time.el @@ -165,55 +165,51 @@ could call `emms-playing-time-enable-display' and (defun emms-playing-time-display () "Display playing time on the mode line." (setq emms-playing-time (round (1+ emms-playing-time))) - (setq emms-playing-time-string "") - (when emms-playing-time-display-p - (let* ((min (/ emms-playing-time 60)) - (sec (% emms-playing-time 60)) - (total-playing-time - (or (emms-track-get - (emms-playlist-current-selected-track) - 'info-playing-time) - 0)) - (total-min-only (/ total-playing-time 60)) - (total-sec-only (% total-playing-time 60))) - (cl-case emms-playing-time-style - ((downtime) ; `downtime' style - (setq emms-playing-time-string - (emms-replace-regexp-in-string - " " "0" - (if (or emms-playing-time-display-short-p - ;; unable to get total playing-time - (eq total-playing-time 0)) - (format "%2d:%2d" min sec) - (format "-%2d:%2d" - (/ (- total-playing-time emms-playing-time) 60) - (% (- total-playing-time sec) 60)))))) - ((bar) ; `bar' style - (if (zerop total-playing-time) - (setq emms-playing-time-string "[==>........]") - (let ((progress "[") - ;; percent based on 10 - (percent (/ (* emms-playing-time 10) total-playing-time))) - (dotimes (_i percent) - (setq progress (concat progress "="))) - (setq progress (concat progress ">")) - (dotimes (_i (- 10 percent)) - (setq progress (concat progress " "))) - (setq progress (concat progress "]")) - (setq emms-playing-time-string progress)))) - (t ; `time' style - (setq emms-playing-time-string - (emms-replace-regexp-in-string - " " "0" - (if (or emms-playing-time-display-short-p - ;; unable to get total playing-time - (eq total-playing-time 0)) - (format "%2d:%2d" min sec) - (format "%2d:%2d/%2s:%2s" - min sec total-min-only total-sec-only)))))) - (setq emms-playing-time-string - (format emms-playing-time-display-format - emms-playing-time-string)))) + (setq emms-playing-time-string + (if (null emms-playing-time-display-p) + "" + (let* ((min (/ emms-playing-time 60)) + (sec (% emms-playing-time 60)) + (total-playing-time + (or (emms-track-get + (emms-playlist-current-selected-track) + 'info-playing-time) + 0)) + (total-min-only (/ total-playing-time 60)) + (total-sec-only (% total-playing-time 60)) + (string + (cl-case emms-playing-time-style + ((downtime) ; `downtime' style + (emms-replace-regexp-in-string + " " "0" + (if (or emms-playing-time-display-short-p + ;; unable to get total playing-time + (eq total-playing-time 0)) + (format "%2d:%2d" min sec) + (format "-%2d:%2d" + (/ (- total-playing-time emms-playing-time) 60) + (% (- total-playing-time sec) 60))))) + ((bar) ; `bar' style + (if (zerop total-playing-time) + "[==>........]" + (let (;; percent based on 10 + (percent (/ (* emms-playing-time 10) + total-playing-time))) + (concat "[" + (make-string percent ?=) + ">" + (make-string (- 10 percent) ?\s) + "]")))) + (t ; `time' style + (emms-replace-regexp-in-string + " " "0" + (if (or emms-playing-time-display-short-p + ;; unable to get total playing-time + (eq total-playing-time 0)) + (format "%2d:%2d" min sec) + (format "%2d:%2d/%2s:%2s" + min sec total-min-only total-sec-only))))))) + (format emms-playing-time-display-format string)))) (force-mode-line-update)) (defun emms-playing-time-mode-line () diff --git a/emms-playlist-limit.el b/emms-playlist-limit.el index 8f274c8..f21f4f8 100644 --- a/emms-playlist-limit.el +++ b/emms-playlist-limit.el @@ -178,9 +178,11 @@ is non-nil." (defun emms-playlist-limit--limit-playlist (playlist type regexp) "Return a new playlist of tracks in PLAYLIST with TYPE matching REGEXP." - (let* ((bufname (concat (buffer-name playlist) - (format "/%s=%s" - (emms-replace-regexp-in-string "info-" "" (symbol-name type)) regexp)))) + (let* ((bufname (format "%s/%s=%s" + (buffer-name playlist) + (emms-replace-regexp-in-string "info-" "" + (symbol-name type)) + regexp))) (emms-playlist-limit--derive-playlist playlist (lambda (track) (let ((field (emms-playlist-limit-track-get track type))) diff --git a/emms-playlist-mode.el b/emms-playlist-mode.el index 2815a56..a2aefca 100644 --- a/emms-playlist-mode.el +++ b/emms-playlist-mode.el @@ -581,7 +581,7 @@ WINDOW-WIDTH should be a positive integer." (setq buffer-undo-list nil)) ;;;###autoload -(defun emms-playlist-mode () +(defun emms-playlist-mode () ;FIXME: Use `define-derived-mode'. "A major mode for Emms playlists. \\{emms-playlist-mode-map}" (interactive) diff --git a/emms-tag-editor.el b/emms-tag-editor.el index d0028d2..c00a4fb 100644 --- a/emms-tag-editor.el +++ b/emms-tag-editor.el @@ -295,6 +295,7 @@ This string is suitable for inserting into the tags buffer." (defvar emms-tag-editor-mode-map (let ((map (make-sparse-keymap))) + ;; FIXME: Bind to "\t" rather than [tab] so it works in ttys as well. (define-key map [tab] #'emms-tag-editor-next-field) (define-key map [backtab] #'emms-tag-editor-prev-field) (define-key map "\C-c\C-n" #'emms-tag-editor-next-track) -- cgit v1.2.3