0

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?

NickD
  • 29,717
  • 3
  • 27
  • 44
C-Entropy
  • 143
  • 5
  • I have updated the question, please take a look. – C-Entropy Feb 24 '22 at 02: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 '22 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 '22 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 '22 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 '22 at 03:46
  • I think I have figure it out, I have to (require 'expand-region) first to make it work. But I still don't quiet understand. As far as I can see, expan-region won't been loaded at the meantime when I open a lisp-mode buffer, which cause the original er/try-expand-list to be nil. However, my own function to add support is already called, set the er/try-expand-list to (er/lisp-mode). What I don't understand is make-variable-buffer-local should retained that value buffer local, and other buffers shouldn't been effect. – C-Entropy Feb 25 '22 at 05:10
  • One way to make sure is to (require 'expand-region) early in your init file. That way it will always be available in the emacs session. – NickD Feb 25 '22 at 16:28

0 Answers0