uk-ar / smart-region

20 stars 5 forks source link

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

Open mattiasb opened 9 years ago

mattiasb commented 9 years ago

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.

glucas commented 7 years ago

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)
mattiasb commented 7 years ago

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

mattiasb commented 7 years ago

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?

glucas commented 7 years ago

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 commented 7 years ago

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.

mattiasb commented 7 years ago

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