Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a transient map for frequent commands #84

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <kbd>M-p</kbd>.

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
<http://localhost:8080/smudge-api-callback> as the redirect URI (or whichever
Expand Down
26 changes: 22 additions & 4 deletions smudge-controller.el
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand All @@ -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."
Expand Down Expand Up @@ -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"))
Expand Down
9 changes: 9 additions & 0 deletions smudge.el
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading