Skip to content

Commit

Permalink
Merge pull request #1727 from Encryptoid/add-undefine-key
Browse files Browse the repository at this point in the history
Add (undefine-key) functionality
  • Loading branch information
cxxxr authored Jan 1, 2025
2 parents 538ca0d + 52b43f8 commit 28f62aa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/internal-packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@
:*global-keymap*
:define-key
:define-keys
:undefine-key
:undefine-keys
:keyseq-to-string
:find-keybind
:insertion-key-p
Expand Down
34 changes: 34 additions & 0 deletions src/keymap.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,40 @@ Example: (define-key *global-keymap* \"C-'\" 'list-modes)"
(setf (gethash k table) new-table)
(setf table new-table))))))))

(defun undefine-key (keymap keyspec)
"Remove a binding for a KEYSPEC in a KEYMAP.
If KEYSPEC argument is a `string', valid prefixes are:
H (Hyper), S (Super), M (Meta), C (Ctrl), Shift
Example: (undefine-key *paredit-mode-keymap* \"C-k\")"
(check-type keyspec (or symbol string))
(typecase keyspec
(symbol
(remhash keyspec (keymap-function-table keymap)))
(string
(let ((keys (parse-keyspec keyspec)))
(undefine-key-internal keymap keys))))
(values))

(defmacro undefine-keys (keymap &body bindings)
`(progn ,@(mapcar
(lambda (binding)
`(undefine-key ,keymap
,(first binding)))
bindings)))

(defun undefine-key-internal (keymap keys)
(loop :with table := (keymap-table keymap)
:for rest :on (uiop:ensure-list keys)
:for k := (car rest)
:do (cond ((null (cdr rest))
(remhash k table))
(t
(let ((next (gethash k table)))
(when (prefix-command-p next)
(setf table next)))))))

(defun parse-keyspec (string)
(labels ((fail ()
(editor-error "parse error: ~A" string))
Expand Down

0 comments on commit 28f62aa

Please sign in to comment.