From 9aa2edd2031c2364b0a7a2fa1f7e74b01d4f80a6 Mon Sep 17 00:00:00 2001 From: Magne Hov Date: Wed, 13 Dec 2023 19:37:09 +0000 Subject: [PATCH] Add a transient map for frequent commands (#84) Co-authored-by: Daniel Martins <29534+danielfm@users.noreply.github.com> --- README.md | 16 +++++++++++++--- smudge-controller.el | 26 ++++++++++++++++++++++---- smudge.el | 9 +++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 385fc56..7c86ce5 100644 --- a/README.md +++ b/README.md @@ -66,9 +66,11 @@ Add the following to the `config.el` file: ``` el (use-package! smudge :bind-keymap ("C-c ." . smudge-command-map) - :config - (setq smudge-oauth2-client-secret "..." - smudge-oauth2-client-id "...")) + :custom + (smudge-oauth2-client-secret "...") + (smudge-oauth2-client-id "...") + ;; optional: enable transient map for frequent commands + (smudge-player-use-transient-map t)) ``` ## Configuration @@ -85,6 +87,14 @@ conventions suggested for minor modes as defined in the Emacs manual [Key Binding Conventions](https://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Binding-Conventions.html#Key-Binding-Conventions). Previous versions of this package used M-p. +A transient map can be enabled to allow repeating frequent commands +(defined in `smudge-transient-command-map`) without having to repeat the +prefix key for `smudge-command-map`. + +```el +(setq smudge-player-use-transient-map t) +``` + In order to get the client ID and client secret, you need to create a [Spotify app](https://developer.spotify.com/my-applications), specifying as the redirect URI (or whichever diff --git a/smudge-controller.el b/smudge-controller.el index 96892fe..3608f7a 100644 --- a/smudge-controller.el +++ b/smudge-controller.el @@ -100,6 +100,11 @@ The following placeholders are supported: :type 'string :group 'smudge) +(defcustom smudge-player-use-transient-map nil + "Whether a transient map should be activated after commands that are likely to be repeated." + :type 'bool + :group 'smudge) + (defvar smudge-controller-timer nil) (defvar smudge-controller-player-status "" @@ -108,6 +113,19 @@ The following placeholders are supported: (defvar smudge-controller-player-metadata nil "The metadata about the currently playing track.") +(defmacro defun-smudge-transient (&rest body) + "Create a transient smudge command from BODY. + +A transient command allows you to immediately invoke another command from +`smudge-transient-command-map'. See `set-transient-map'. + +The transient map is enabled if `smudge-player-use-transient-map' is non-nil." + (declare (doc-string 3) + (indent defun)) + `(defun ,@body + (when smudge-player-use-transient-map + (set-transient-map smudge-transient-command-map)))) + (defun smudge-controller-apply (suffix &rest args) "Simple facility to emulate multimethods. Apply SUFFIX to smudge-controller-prefixed functions, applying ARGS." @@ -202,22 +220,22 @@ This corresponds to the current REPEATING state." (interactive) (smudge-controller-apply "player-toggle-play")) -(defun smudge-controller-next-track () +(defun-smudge-transient smudge-controller-next-track () "Sends a `next track' command to Spotify process." (interactive) (smudge-controller-apply "player-next-track")) -(defun smudge-controller-previous-track () +(defun-smudge-transient smudge-controller-previous-track () "Sends a `previous track' command to Spotify process." (interactive) (smudge-controller-apply "player-previous-track")) -(defun smudge-controller-volume-up () +(defun-smudge-transient smudge-controller-volume-up () "Increase the volume for the active device." (interactive) (smudge-controller-apply "volume-up")) -(defun smudge-controller-volume-down () +(defun-smudge-transient smudge-controller-volume-down () "Increase the volume for the active device." (interactive) (smudge-controller-apply "volume-down")) diff --git a/smudge.el b/smudge.el index 358275f..5b340d9 100644 --- a/smudge.el +++ b/smudge.el @@ -157,6 +157,15 @@ Prompt for the NAME and whether it should be made PUBLIC." (fset 'smudge-command-map smudge-command-map) +(defvar smudge-transient-command-map + (let ((map (make-sparse-keymap))) + (define-key map (kbd "M-b") #'smudge-controller-previous-track) + (define-key map (kbd "M-f") #'smudge-controller-next-track) + (define-key map (kbd "M-u") #'smudge-controller-volume-up) + (define-key map (kbd "M-d") #'smudge-controller-volume-down) + map) + "Transient keymap for commands that are likely to be repeated.") + (easy-menu-add-item nil '("Tools") '("Smudge" ["Play/Pause" smudge-controller-toggle-play :active global-smudge-remote-mode]