aboutsummaryrefslogtreecommitdiffstats
path: root/content_scripts/mode_visual.coffee
diff options
context:
space:
mode:
authorStephen Blott2016-03-25 11:38:18 +0000
committerStephen Blott2016-03-25 11:38:18 +0000
commit9bc02b6fe8329fc6342196070f68f1657075c3db (patch)
tree3c8dbe325a592e27eb7f664d53682b9e927e8227 /content_scripts/mode_visual.coffee
parent69333f609bcdf3724ae639d7389750cfe18f4523 (diff)
downloadvimium-9bc02b6fe8329fc6342196070f68f1657075c3db.tar.bz2
Better choice of callapse on exit.
The question here is where to callapse the selection to, anchor or focus? When exiting visual mode, mimic vim. When trasitioning between visual and caret modes, do what's right to keep the selection in the same place. This also adds some related tests.
Diffstat (limited to 'content_scripts/mode_visual.coffee')
-rw-r--r--content_scripts/mode_visual.coffee16
1 files changed, 14 insertions, 2 deletions
diff --git a/content_scripts/mode_visual.coffee b/content_scripts/mode_visual.coffee
index cbdf6ae2..011d6775 100644
--- a/content_scripts/mode_visual.coffee
+++ b/content_scripts/mode_visual.coffee
@@ -197,7 +197,15 @@ class VisualMode extends KeyHandlerMode
"P": -> chrome.runtime.sendMessage handler: "openUrlInNewTab", url: @yank()
"v": -> new VisualMode
"V": -> new VisualLineMode
- "c": -> @movement.collapseSelectionToAnchor(); new CaretMode
+ "c": ->
+ # If we're already in caret mode, or if the selection looks the same as it would in caret mode, then
+ # callapse to anchor (so that the caret-mode selection will seem unchanged). Otherwise, we're in visual
+ # mode and the user has moved the focus, so collapse to that.
+ if @name == "caret" or @selection.toString().length <= 1
+ @movement.collapseSelectionToAnchor()
+ else
+ @movement.collapseSelectionToFocus()
+ new CaretMode
"o": -> @movement.reverseSelection()
constructor: (options = {}) ->
@@ -228,7 +236,11 @@ class VisualMode extends KeyHandlerMode
commandHandler: @commandHandler.bind this
@onExit (event = null) =>
- @movement.collapseSelectionToAnchor()
+ # This mimics vim: when leaving visual mode via Escape, collapse to focus, otherwise collapse to anchor.
+ if event?.type == "keydown" and KeyboardUtils.isEscape(event) and @name != "caret"
+ @movement.collapseSelectionToFocus()
+ else
+ @movement.collapseSelectionToAnchor()
# Don't leave the user in insert mode just because they happen to have selected an input.
if document.activeElement and DomUtils.isEditable document.activeElement
document.activeElement.blur() unless event?.type == "click"