tarsius / keymap-utils

Emacs keymap utilities

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The kmu-remove-key function is broken

minad opened this issue ยท comments

It seems, empty submaps are not removed.

Improved version:

(defun remove-key (key &optional map)
  (setq map (or map global-map)
        key (if (vectorp key) key (vconcat (kbd key))))
  (define-key map key nil)
  ;; Split M-key in ESC key
  (setq key (mapcan (lambda (k)
                      (if (and (integerp k) (/= (logand k ?\M-\0) 0))
                          (list ?\e (logxor k ?\M-\0))
                        (list k)))
                    key))
  ;; Delete single keys directly
  (if (= (length key) 1)
      (delete key map)
    ;; Lookup submap and delete key from there
    (let* ((prefix (vconcat (butlast key)))
           (submap (lookup-key map prefix)))
      (unless (keymapp submap)
        (error "Not a keymap for %s" key))
      (when (symbolp submap)
        (setq submap (symbol-function submap)))
      (delete (last key) submap)
      ;; Delete submap if it is empty
      (when (= 1 (length submap))
          (remove-key prefix map)))))

It seems, empty submaps are not removed.

Your fix is correct, I think, but the defect is the element is not removed from submaps. (That prevents submaps from ever become empty, so yes. "empty" submaps are never removed. But that's a secondary issue.)

And it seems you found a second issue and fixed that as well, though that's more of a feature than a bugfix. I am not sure yet whether we should really allow modifying the bindings of bound prefix commands, but probably yes. In any case, the two changes should be made in separate commits.

Anyway, the two commits are on the remove-from-submap branch. I'll have to experiment and test a bit more.

Yes, the reworked version does more than your original. I would like to see the function in use-package. Would that make sense or do you see any downsides of using the removal approach in contrast to overwriting with nil? See the issue jwiegley/use-package#884.

I've merged it.

Would that make sense or do you see any downsides of using the removal approach in contrast to overwriting with nil?

No ๐Ÿ˜„

No to "would it make sense" or no to "do you see downsides"? ๐Ÿ˜€

Would make sense, there are no downsides. ๐Ÿ˜

Cool, I agree ๐Ÿ˜ฌ I will ask @jwiegley if he would accept a PR to use-package.