I'm trying to add new support to expand-region:

(defun er/lisp-mode ()
  "Enhancement for lisp like #'() ,@()"
  (when (looking-at "\\(\\(`,?\\)\\|\\('?,?\\)\\|\\(,?@?\\)\\|\\(#?'?\\)\\)?(")
    (cond ((er/looking-back-on-line ",\\|@\\|?\\|#\\|'\\|`")
           (backward-char 1)))
    (set-mark (point))

(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
  ("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?

  • I have updated the question, please take a look.
    – C-Entropy
    Feb 24, 2022 at 2:17
  • I tend to suspect that use-package declaration (but maybe because I don't use use-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 using use-package: the old technique of divide-and-conquer...
    – NickD
    Feb 24, 2022 at 16:13
  • Thanks to your reply, I have already tried (add-hook 'lisp-mode-hook 'er/add-lisp-mode-expansions), but it performance like use-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 call er/add-lisp-mode-expansions manually in each lisp-mode buffer, then it is okay, but once use hook, then it go wrong.
    – C-Entropy
    Feb 24, 2022 at 16:18
  • I would try to emulate one of the existing extensions, e.g. Ivan Andrus's text expansions although you probably want to find the most recent version of the package and go from there.
    – NickD
    Feb 24, 2022 at 17:07
  • But in that way, I have to modify the source code of expand-region, which will be discard every update.
    – C-Entropy
    Feb 25, 2022 at 3:46


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.