AureumLeonis · 2022-11-08 19:59
Expand region may be my most used thing in emacs yet it's a bit frustrating out of the box. I know there's ways to 'advice' the command to expand in certain ways, there's 2 specific things I want:
**On assignments/expressions**
I'd like expansion to always select the expression first but it seems to work backwards e.g. with this region
a = b[c]
^
expanding again leads to this
a = b[c]
^^^^^
instead of
a = b[c]
^^^^
**On vterm/truncated lines** Assume that the screen ends at col 5 with this
barbaz
foobar\
qux
If the region is currently `foobar` but the actual full string is `foobarqux`, expand region will go back and select all the way to the top of the buffer instead of the remaining part `qux`.
**TL;DR** How can I bias expand-region to select forward instead of backward? Also to stop on newlines and not default to the entire buffer when it doesn't seem to know where else to go?
uita23 · 2022-11-08 22:28
It's a tangent, but I was wondering about expand-region working with tree-sitter. It would probably do what you want here too, since I'd expect it to expand to the array expression first and then the assignment statement containing it.
mickeyp · 2022-11-08 22:34
Why not `C-M-SPC` twice? Or better still, `C-M-k` twice?
AureumLeonis · 2022-11-08 23:30
Because currently expand region is replacing lots of random commands with a single one that works 99% of the time. I don't want to go back and have to remember which shortcut to use depending on where I'm anymore. It feels like going backwards.
AFAIK expand region was made to be tweaked I'm just having a hard time figuring out how.
karthink · 2022-11-09 01:02
(defun er/mark-index-expression ()
"Mark the current symbol (including dots) and then paren to closing paren."
(interactive)
(let ((symbol-regexp "\\(\\s_\\|\\sw\\|\\.\\)+"))
(when (or (looking-at symbol-regexp)
(er/looking-back-on-line symbol-regexp))
(skip-syntax-backward "_w.")
(set-mark (point))
(when (looking-at symbol-regexp)
(goto-char (match-end 0)))
(if (looking-at "\\[\\|{")
(forward-list))
(exchange-point-and-mark))))
(add-to-list 'er/try-expand-list 'er/mark-index-expression)
karthink · 2022-11-09 01:07
Expand region is very useful but hacky. The marking logic also seems inefficient to me, it runs through many more tests in `er/try-expand-list` on each invocation than it needs to. Once Tree-sitter support is part of Emacs I'll look into updating it.
AureumLeonis · 2022-11-09 01:41
I gave this a try but it seems like the region is still expanded left before expanding the brackets. This was in a python mode buffer. Is there some verbose/debug mode to figure out if this is kicking in at all?
karthink · 2022-11-09 03:54
`er/try-expand-list` is a buffer local variable. Put the above command in `python-mode-hook`.
AureumLeonis · 2022-11-09 04:47
I'll give this a try tomorrow! Thanks so much!
sebhoagie · 2022-11-09 14:30
I started using more of these mark commands after reading your book. One place where mark word/sexp fall short is that you need to move point to the beginning of what you want to mark (or use negative argument).
With expand-region you just hit SPC until the area covers what you want.