I am annoyed that when I C-g out of my expand-region session my cursor does not return to its original location.

In trying to resolve this annoyance I found that expand-region comes with a variable er/save-mode-excursion that is described as:

A function to save excursion state while expanding

I set this variable to (lambda (&rest body) () (save-excursion body)) and I found that this does not suit my needs for the following reasons.

Next I tried taking advantage of the deactivate-mark-hook to programatically hit C-u C-SPC.

(add-hook 'deactivate-mark-hook #'(lambda () (let ((current-prefix-arg 4)) (call-interactively 'set-mark-command)))) 

This works great for my non expand-region region endeavours but when I try to use any er/ commands I get unpredictable behavior. I believe the problem is that expand-region resets the mark multiple times and therefore deactivate-mark-hook is called multiple times.

In regards to the er/mark- functions I tried to wrap them all up in new functions that wrap a save-excursion but the behavior is not what I expected. For example if I try this on er/mark-defun then the marked region becomes from the current point to the end of the defun instead of marking the entire defun.

My next idea is to make my own expand-region-minor-mode where I can probably solve this problem. Before I do this though I want to know if there are already existing solutions.

submitted by /u/thirtySeven73
[link] [comments]