clojure-mode
clojure-mode copied to clipboard
After turning off paredit-mode, can't type unbalanced curly brace }
Expected behavior
Turn on paredit-mode, then turn it off, and be able to type unbalanced characters ()[]{}.
Actual behavior
Can't type an unbalanced }
Steps to reproduce the problem
Open a clojure file, run M-x paredit-mode, then run it again to turn it off. Try to type a }. Something of paredit mode persists and prevents it.
Environment & Version information
clojure-mode version information
Clojure Mode 5.10.0
I see this is package list, but oddly, clojure-mode-display-version is showing clojure-mode (version nil) in the minibuffer.
Emacs version
Emacs 25.3.1
Operating system
macOS 10.13.6
This function might have something to do with it: https://github.com/clojure-emacs/clojure-mode/blob/a4ed7a4152f8a6514dd3fd82532aa5a2bdba024f/clojure-mode.el#L490-L501
Probably needs a another function to undo this when leaving paredit-mode.
paredit's config should have no effect if paredit is disabled.
@bbatsov I was able to reproduce the problem, and in fact there are side effects in the code above. The defined keys are not restored when the paredit-mode is disabled.
I wrote the following code to restore the desired behavior. If you think this is a proper solution, I can submit a PR with the changes.
(defun clojure-paredit-restore-default-keys (&optional keymap)
"Disabling \"paredit-mode\" should restore the `paredit` defined keys on `clojure-mode-map'.
If an optional KEYMAP is passed the changes are applied to it,
instead of to `clojure-mode-map'."
(when (>= paredit-version 21)
(let ((keymap (or keymap clojure-mode-map)))
(define-key keymap "{" #'self-insert-command)
(define-key keymap "}" #'self-insert-command))))
(defadvice paredit-mode (after toggle-clojure-setup-mode activate)
"Advice to restore defined `paredit' keys on the `clojure-mode-map' when `paredit-mode' is disabled."
(when (not paredit-mode)
(clojure-paredit-restore-default-keys)))
I was refactoring my proposed solution and just noticed that I'm not considering the case where a keymap was provided to the clojure-paredit-setup function. To proper solve this case, I would need to provide the same keymap as an argument to my defadvice function, I don't know if this is possible in elisp, though.
Something link:
(defun clojure-paredit-setup (&optional keymap)
....
(let ((keymap (or keymap clojure-mode-map)))
(define-key keymap "{" #'paredit-open-curly)
(define-key keymap "}" #'paredit-close-curly)
;; added advice
(advice-add 'paredit-mode :after (lambda (keymap) (clojure-paredit-restore-default-keys keymap))))
....