aboutsummaryrefslogtreecommitdiff
path: root/atom/packages/vim-mode/lib/operators/put-operator.coffee
diff options
context:
space:
mode:
authorBen Beltran <ben@nsovocal.com>2015-07-10 11:12:25 -0500
committerBen Beltran <ben@nsovocal.com>2015-07-10 11:12:25 -0500
commit24c7594d62d8d7fbbcdb64b11ce4adc5d8e6991a (patch)
treeded312222bb108923da1820ba40b04d710d20e5b /atom/packages/vim-mode/lib/operators/put-operator.coffee
parenteb786e82d170e2abc351a432ade616d6ecdeeb6b (diff)
Adds atom
Diffstat (limited to 'atom/packages/vim-mode/lib/operators/put-operator.coffee')
-rw-r--r--atom/packages/vim-mode/lib/operators/put-operator.coffee73
1 files changed, 73 insertions, 0 deletions
diff --git a/atom/packages/vim-mode/lib/operators/put-operator.coffee b/atom/packages/vim-mode/lib/operators/put-operator.coffee
new file mode 100644
index 0000000..63151f1
--- /dev/null
+++ b/atom/packages/vim-mode/lib/operators/put-operator.coffee
@@ -0,0 +1,73 @@
+_ = require 'underscore-plus'
+{Operator} = require './general-operators'
+settings = require '../settings'
+
+module.exports =
+#
+# It pastes everything contained within the specifed register
+#
+class Put extends Operator
+ register: null
+
+ constructor: (@editor, @vimState, {@location, @selectOptions}={}) ->
+ @location ?= 'after'
+ @complete = true
+ @register = settings.defaultRegister()
+
+ # Public: Pastes the text in the given register.
+ #
+ # count - The number of times to execute.
+ #
+ # Returns nothing.
+ execute: (count=1) ->
+ {text, type} = @vimState.getRegister(@register) or {}
+ return unless text
+
+ textToInsert = _.times(count, -> text).join('')
+
+ selection = @editor.getSelectedBufferRange()
+ if selection.isEmpty()
+ # Clean up some corner cases on the last line of the file
+ if type is 'linewise'
+ textToInsert = textToInsert.replace(/\n$/, '')
+ if @location is 'after' and @onLastRow()
+ textToInsert = "\n#{textToInsert}"
+ else
+ textToInsert = "#{textToInsert}\n"
+
+ if @location is 'after'
+ if type is 'linewise'
+ if @onLastRow()
+ @editor.moveToEndOfLine()
+
+ originalPosition = @editor.getCursorScreenPosition()
+ originalPosition.row += 1
+ else
+ @editor.moveDown()
+ else
+ unless @onLastColumn()
+ @editor.moveRight()
+
+ if type is 'linewise' and not originalPosition?
+ @editor.moveToBeginningOfLine()
+ originalPosition = @editor.getCursorScreenPosition()
+
+ @editor.insertText(textToInsert)
+
+ if originalPosition?
+ @editor.setCursorScreenPosition(originalPosition)
+ @editor.moveToFirstCharacterOfLine()
+
+ @vimState.activateCommandMode()
+ if type isnt 'linewise'
+ @editor.moveLeft()
+
+ # Private: Helper to determine if the editor is currently on the last row.
+ #
+ # Returns true on the last row and false otherwise.
+ onLastRow: ->
+ {row, column} = @editor.getCursorBufferPosition()
+ row is @editor.getBuffer().getLastRow()
+
+ onLastColumn: ->
+ @editor.getLastCursor().isAtEndOfLine()