From ebf31012dc3fe4df2e647367b45cf74423118f9c Mon Sep 17 00:00:00 2001 From: solecki <38192622+solecki@users.noreply.github.com> Date: Sun, 15 Apr 2018 00:51:40 +0200 Subject: [PATCH] Add :move command and alias :m --- lib/ex.coffee | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/lib/ex.coffee b/lib/ex.coffee index cbc7857..3e51a06 100644 --- a/lib/ex.coffee +++ b/lib/ex.coffee @@ -465,4 +465,55 @@ class Ex sortedText = _.sortBy(textLines).join('\n') + '\n' editor.buffer.setTextInRange(sortingRange, sortedText) + move: ({range, args, editor}) -> + args = args.trimLeft() + lastLine = editor.getLastScreenRow() + argsPattern = /[+-]\d+|\d+|[+-]/g + args = args.match(argsPattern) + + if args? + firstArgIsOffset = /[+-]/.test(args[0]) + address = if firstArgIsOffset then range[0] else 0 + + for arg in args + else if arg == '+' + address++ + else if arg == '-' + address-- + else + address += parseInt(arg) + + movingUp = address < range[0] + if movingUp + address++ + if not firstArgIsOffset + address-- + + if isNaN(address) or address < 0 or address > lastLine + throw new CommandError("E14: Invalid address") + if address > range[0] and address < range[1] + throw new CommandError("E134: Move lines into themselves") + if editor.getSelections().length > 1 + throw new CommandError("Multiple selections present") + + if address == range[0] or address == range[1] + editor.setCursorBufferPosition([range[1], 0]) + else + move = -> + numOfLinesToMove = range[1] - range[0] + bufferRange = [[range[0], 0], [range[1] + 1, 0]] + textToMove = editor.getTextInBufferRange(bufferRange) + editor.setTextInBufferRange(bufferRange, '') + + if movingUp + editor.setTextInBufferRange([[address, 0], [address, 0]], textToMove) + editor.setCursorBufferPosition([address + numOfLinesToMove, 0]) + else + editor.setTextInBufferRange([[address - numOfLinesToMove, 0], + [address - numOfLinesToMove, 0]], textToMove) + editor.setCursorBufferPosition([address, 0]) + editor.transact(300, move) + + m: (args) => @move(args) + module.exports = Ex