diff options
| author | Stephen Blott | 2015-01-25 16:20:37 +0000 |
|---|---|---|
| committer | Stephen Blott | 2015-01-25 16:20:37 +0000 |
| commit | 9ab52af747c55608aae27069e416c45111da9be1 (patch) | |
| tree | 515938d58fea12af2e2063c18f9fb536cd221fa2 /content_scripts | |
| parent | 6c2580ae4c9ab14d0b6340c45dd3f5cd05fce2d7 (diff) | |
| download | vimium-9ab52af747c55608aae27069e416c45111da9be1.tar.bz2 | |
Visual/edit modes: establish an initial selection.
Diffstat (limited to 'content_scripts')
| -rw-r--r-- | content_scripts/mode_visual_edit.coffee | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/content_scripts/mode_visual_edit.coffee b/content_scripts/mode_visual_edit.coffee index 7d62b784..d909bf43 100644 --- a/content_scripts/mode_visual_edit.coffee +++ b/content_scripts/mode_visual_edit.coffee @@ -276,8 +276,9 @@ class VisualMode extends Movement @selection = window.getSelection() switch @selection.type when "None" - HUD.showForDuration "Create a selection before entering visual mode.", 2500 - return + unless @establishInitialSelection() + HUD.showForDuration "Create a selection before entering visual mode.", 2500 + return when "Caret" # Try to start with a visible selection. @moveInDirection(forward) or @moveInDirection backward unless options.underEditMode @@ -295,7 +296,7 @@ class VisualMode extends Movement # Special case: "yy" (the first from edit mode, and now the second). @selectLine() if @options.yYanksLine @yank() - "V": -> new VisualLineMode @options + "V": -> new VisualLineMode extend @options, initialRange: @selection.getRangeAt(0).cloneRange() if @options.underEditMode extend @commands, @@ -348,11 +349,33 @@ class VisualMode extends Movement "n": -> executeFind false "N": -> executeFind true + establishInitialSelection: -> + nodes = document.createTreeWalker document.body, NodeFilter.SHOW_TEXT + while node = nodes.nextNode() + if node.nodeType == 3 and 50 <= node.data.trim().length + element = node.parentElement + if DomUtils.getVisibleClientRect(element) and not DomUtils.isEditable element + range = document.createRange() + text = node.data + trimmed = text.replace /^\s+/, "" + offset = text.length - trimmed.length + range.setStart node, offset + range.setEnd node, offset + 1 + @selection.removeAllRanges() + @selection.addRange range + @scrollIntoView() + return true + false + class VisualLineMode extends VisualMode constructor: (options = {}) -> options.name = "visual/line" super options - @selectLine() unless @selection?.type == "None" + unless @selection?.type == "None" + if options.initialRange + @selection.removeAllRanges() + @selection.addRange options.initialRange + @selectLine() handleMovementKeyChar: (keyChar) -> super keyChar |
