smart-region icon indicating copy to clipboard operation
smart-region copied to clipboard

Make C-SPC SPC start a er/expand-region

Open mattiasb opened this issue 10 years ago • 6 comments

Hi!

I have a little feature request for you. I'd like to have C-SPC SPC (with no mark set) trigger er/expand-region in addition to the current behaviour of C-SPC C-SPC with the marked region on a single line.

The reason I want this is that with er/expand-region you enter a mode (of sorts) that lets you continually hit SPC to enlarge the marked region. It would feel a lot more natural to just have to hit C-SPC SPC SPC SPC to enlarge it three times instead of the current C-SPC C-SPC SPC SPC.

The current effect of C-SPC SPC (with no mark) is basically just a SPACE character so this shouldn't interfere with anyone's regular work flow.

What do you think?

Finally I'd like to say that it's a great package you've made. I've tried it out for a little while now and really love it, it makes the use of expand-region and multiple-cursors feel much more accessible.

mattiasb avatar Sep 04 '15 22:09 mattiasb

Here's a diff that seems to work: when there is no region, set the mark and then set a temporary map with SPC bound to expand-region.

+++ #<buffer smart-region.el>
@@ -70,7 +70,13 @@
    ;;region not exist
    ((not (region-active-p))
     (setq this-command 'set-mark-command)
-    (call-interactively 'set-mark-command))
+    (call-interactively 'set-mark-command)
+    (er/set-temporary-overlay-map
+     (let ((map (make-sparse-keymap)))
+       (define-key map (kbd "<SPC>") 'er/expand-region)
+       map)
+       t)) 
+
    ;;region exist & single line
    ((= (line-number-at-pos) (line-number-at-pos (mark)))
     ;;(setq this-command 'er/expand-region)

glucas avatar Dec 09 '16 22:12 glucas

Awesome! I will test this as soon as I get the time! :)

mattiasb avatar Dec 10 '16 03:12 mattiasb

Tried this out now, much later (sorry for that).

Unfortunately there seems to be a bug where if you expand the region and press C-g to abort C-SPC no longer sets the mark. If you try to set the mark with C-SPC one- or two times then it starts working again.

My guess is that the temporary overlay keymap should be disabled when expand-region is aborted but isn't?

mattiasb avatar Mar 18 '17 02:03 mattiasb

I think this is actually an issue with expand-region: see https://github.com/magnars/expand-region.el/issues/220. There is a patch submitted but it has not been merged yet.

From the discussion on that issue, you can disable shift-select-mode as a workaround.

glucas avatar Mar 18 '17 19:03 glucas

One minor tweak (not related to the issue mentioned above):

-    (call-interactively 'set-mark-command))
+    (call-interactively 'set-mark-command)
+    (set-transient-map
+     (let ((map (make-sparse-keymap)))
+       (define-key map (kbd "<SPC>") 'er/expand-region)
+       map))) 

We should probably use set-transient-map because set-temporary-overlay-map is marked obsolete since Emacs 24.4. Also we can drop the t argument to keep the transient map active, because expand-region will manage its own transient map once activated.

glucas avatar Mar 18 '17 19:03 glucas

Ah, disabling shift-select-mode worked fine, thanks! Weird that I didn't experience this before.

mattiasb avatar Mar 19 '17 15:03 mattiasb