I'm trying to add new support to expand-region
:
(defun er/lisp-mode ()
(interactive)
"Enhancement for lisp like #'() ,@()"
(when (looking-at "\\(\\(`,?\\)\\|\\('?,?\\)\\|\\(,?@?\\)\\|\\(#?'?\\)\\)?(")
(cond ((er/looking-back-on-line ",\\|@\\|?\\|#\\|'\\|`")
(backward-char 1)))
(set-mark (point))
(forward-list)
(exchange-point-and-mark)))
(defun er/add-lisp-mode-expansions ()
(make-variable-buffer-local 'er/try-expand-list)
(setq er/try-expand-list (append er/try-expand-list '(er/lisp-mode))))
(use-package expand-region
:ensure t
:bind
("C-SPC" . er/expand-region)
:hook ((lisp-mode slime-mode) . er/add-lisp-mode-expansions))
When I open the first .lisp
file, it looks fine.
But after the first file, it doesn't work: expand-region
will select all text of the entire buffer regardless of where I call er/expand-region
.
When I look at er/try-expand-list
in those buffers, it gives (er/lisp-mode er/lisp-mode)
in lisp-mode buffers, and nil
in buffers of other modes.
As far as I can see, it is er/try-expand-list
that controls the behaviour of er/expand-region
, so the problem is the wrong value of er/try-expand-region
, which should be composed from the original value provided by expand-region
with/without er/lisp-mode
added, depending on the mode of the buffer.
How should I fix it?
use-package
declaration (but maybe because I don't useuse-package
so I don't know enough about it). But these things tend to add complexity and hide it at the same time, so there are subtle (and not so subtle) problems that crop up. If I were you, I'd get rid of that, try to set up everything "by hand" and only after that is working, would I go back to usinguse-package
: the old technique ofdivide-and-conquer
...(add-hook 'lisp-mode-hook 'er/add-lisp-mode-expansions)
, but it performance likeuse-package
too. Still don't know why that happen. I guess it may be caused by(make-variable-buffer-local 'er/try-expand-list)
, but I'm not familiar with emacs lisp... If I caller/add-lisp-mode-expansions
manually in each lisp-mode buffer, then it is okay, but once usehook
, then it go wrong.