Skip to content
Browse files

C-g cancels and moves point to original location

 - when used straight after expand or contract

 - also changed ecukes tests to use the commands directly,
   so that this-command and last-command works.

Fixes #32
  • Loading branch information...
1 parent a6ef42c commit fe04a86750e0e6b10a0612c06185de4340af9d87 @magnars magnars committed Apr 12, 2012
View
4 expand-region-core.el
@@ -375,6 +375,10 @@ before calling `er/expand-region' for the first time."
(deactivate-mark)
(er/clear-history))))))
+(defadvice keyboard-quit (before collapse-region activate)
+ (when (memq last-command '(er/expand-region er/contract-region))
+ (er/contract-region 0)))
+
(defun er/clear-history (&rest args)
"Clear the history."
(setq er/history '())
View
126 features/expand-region.feature
@@ -7,21 +7,23 @@ Feature: Expand Region
Given there is no region selected
When I insert "This is some text"
And I go to point "10"
- And I expand the region
+ And I press "C-@"
Then the region should be "some"
Scenario: Mark word just behind point
Given there is no region selected
When I insert "This is some text"
And I go to point "13"
- And I expand the region
+ And I press "C-@"
Then the region should be "some"
Scenario: Multiple expand-region
Given there is no region selected
When I insert "This (is some) text"
And I go to point "10"
- And I expand the region 3 times
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-@"
Then the region should be "(is some)"
Scenario: Expand from existing selection
@@ -30,21 +32,21 @@ Feature: Expand Region
And I go to point "7"
And I set the mark
And I go to point "14"
- And I expand the region
+ And I press "C-@"
Then the region should be "(is some)"
Scenario: Skip white space forward if spaces on both sides of cursor
Given there is no region selected
When I insert "This is some text"
And I go to point "10"
- And I expand the region
+ And I press "C-@"
Then the region should be "some"
Scenario: Skip white space forward if at beginning of buffer
Given there is no region selected
When I insert " This is some text"
And I go to beginning of buffer
- And I expand the region
+ And I press "C-@"
Then the region should be "This"
Scenario: Skip white space forward if at beginning of line
@@ -55,7 +57,7 @@ Feature: Expand Region
some text
"""
And I go to point "9"
- And I expand the region
+ And I press "C-@"
Then the region should be "some"
Scenario: Do not skip white space forward with active region
@@ -64,40 +66,40 @@ Feature: Expand Region
And I go to point "10"
And I set the mark
And I go to point "14"
- And I expand the region
+ And I press "C-@"
Then the region should be "This is some text"
Scenario: Contract region once
Given there is no region selected
When I insert "(((45678)))"
And I go to point "6"
- And I expand the region
- And I expand the region
- And I expand the region
- And I contract the region
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-S-@"
Then the region should be "(45678)"
Scenario: Contract region twice
Given there is no region selected
When I insert "(((45678)))"
And I go to point "6"
- And I expand the region
- And I expand the region
- And I expand the region
- And I contract the region
- And I contract the region
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-S-@"
+ And I press "C-S-@"
Then the region should be "45678"
Scenario: Contract region all the way back to start
Given there is no region selected
When I insert "(((45678)))"
And I go to point "6"
- And I expand the region
- And I expand the region
- And I expand the region
- And I contract the region
- And I contract the region
- And I contract the region
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-S-@"
+ And I press "C-S-@"
+ And I press "C-S-@"
Then the region should not be active
And cursor should be at point "6"
@@ -107,45 +109,55 @@ Feature: Expand Region
And I go to point "7"
And I set the mark
And I go to point "14"
- And I contract the region
+ And I press "C-S-@"
Then the region should be "is some"
Scenario: Contract history should be reset when changing buffer
Given there is no region selected
When I insert "This is some text"
And I go to point "10"
- And I expand the region
- And I expand the region
+ And I press "C-@"
+ And I press "C-@"
And I deactivate the mark
And I insert "More text"
- And I contract the region
+ And I press "C-S-@"
Then the region should not be active
Scenario: Expanding past the entire buffer should not add duplicates to the history
Given there is no region selected
When I insert "This is some text"
- And I expand the region
- And I expand the region
- And I expand the region
- And I expand the region
- And I expand the region
- And I contract the region
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-S-@"
Then the region should be "text"
+ Scenario: C-g to deactivate mark and move back to start of expansions
+ Given there is no region selected
+ When I insert "(((45678)))"
+ And I go to point "6"
+ And I press "C-@"
+ And I press "C-@"
+ And I quit
+ Then the region should not be active
+ And cursor should be at point "6"
+
Scenario: Pop mark twice to get back to start of expansions
Given there is no region selected
When I insert "(((45678)))"
And I go to point "6"
- And I expand the region
- And I expand the region
- And I contract the region
- And I expand the region
- And I expand the region
- And I expand the region
- And I expand the region
- And I contract the region
- And I expand the region
- And I expand the region
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-S-@"
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-S-@"
+ And I press "C-@"
+ And I press "C-@"
And I pop the mark
And I pop the mark
Then cursor should be at point "6"
@@ -157,16 +169,16 @@ Feature: Expand Region
And I set the mark
And I deactivate the mark
And I go to point "6"
- And I expand the region
- And I expand the region
- And I contract the region
- And I expand the region
- And I expand the region
- And I expand the region
- And I expand the region
- And I contract the region
- And I expand the region
- And I expand the region
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-S-@"
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-@"
+ And I press "C-S-@"
+ And I press "C-@"
+ And I press "C-@"
And I pop the mark
And I pop the mark
And I pop the mark
@@ -177,7 +189,7 @@ Feature: Expand Region
And there is no region selected
When I insert "This is some text"
And I go to point "10"
- And I expand the region
+ And I press "C-@"
Then the region should be "some"
Scenario: Expand from existing selection without transient-mark-mode
@@ -188,7 +200,7 @@ Feature: Expand Region
And I set the mark
And I activate the mark
And I go to point "14"
- And I expand the region
+ And I press "C-@"
Then the region should be "(is some)"
Scenario: Do not skip white space forward with active region without tmm
@@ -199,13 +211,13 @@ Feature: Expand Region
And I set the mark
And I activate the mark
And I go to point "14"
- And I expand the region
+ And I press "C-@"
Then the region should be "This is some text"
Scenario: Set-mark-default-inactive
Given mark is inactive by default
And there is no region selected
When I insert "This (is some) text"
And I go to point "6"
- And I expand the region
+ And I press "C-@"
Then the region should be "(is some)"
View
11 features/step-definitions/expand-region-steps.el
@@ -7,6 +7,11 @@
(flet ((message (&rest args) nil))
(er/expand-region 1))))
+(When "^I quit$"
+ (lambda ()
+ (flet ((signal (&rest args) nil))
+ (keyboard-quit))))
+
(When "^I expand the region \\([0-9]+\\) times$"
(lambda (arg)
(flet ((message (&rest args) nil))
@@ -29,9 +34,9 @@
(activate-mark)))
(Then "^the region should not be active$"
- (lambda ()
- (should
- (not (region-active-p)))))
+ (lambda ()
+ (should
+ (not (region-active-p)))))
(Then "^cursor should be at point \"\\(.+\\)\"$"
(lambda (arg)
View
2 features/support/env.el
@@ -12,6 +12,8 @@
(require 'ert)
(Before
+ (global-set-key (kbd "C-@") 'er/expand-region)
+ (global-set-key (kbd "C-S-@") 'er/contract-region)
(switch-to-buffer
(get-buffer-create "*expand-region*"))
(erase-buffer)

0 comments on commit fe04a86

Please sign in to comment.
Something went wrong with that request. Please try again.