lib/gollum/frontend/public/gollum/livepreview/js/ace/lib/ace/keyboard/vim/maps/motions.js in gollum-2.4.4 vs lib/gollum/frontend/public/gollum/livepreview/js/ace/lib/ace/keyboard/vim/maps/motions.js in gollum-2.4.5

- old
+ new

@@ -1,46 +1,38 @@ /* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * Distributed under the BSD license: * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ + * Copyright (c) 2010, Ajax.org B.V. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Ajax.org B.V. nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is Ajax.org Code Editor (ACE). - * - * The Initial Developer of the Original Code is - * Ajax.org B.V. - * Portions created by the Initial Developer are Copyright (C) 2010 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Sergi Mansilla <sergi AT c9 DOT io> - * Harutyun Amirjanyan <harutyun AT c9 DOT io> - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * * ***** END LICENSE BLOCK ***** */ - -"use strict" + define(function(require, exports, module) { +"use strict"; var util = require("./util"); var keepScrollPosition = function(editor, fn) { var scrollTopRow = editor.renderer.getScrollTopRow(); @@ -48,46 +40,31 @@ var diff = initialRow - scrollTopRow; fn && fn.call(editor); editor.renderer.scrollToRow(editor.getCursorPosition().row - diff); }; -function Motion(getRange, type){ - if (type == 'extend') - var extend = true; - else - var reverse = type; - - this.nav = function(editor) { - var r = getRange(editor); - if (!r) +function Motion(m) { + if (typeof m == "function") { + var getPos = m; + m = this; + } else { + var getPos = m.getPos; + } + m.nav = function(editor, range, count, param) { + var a = getPos(editor, range, count, param, false); + if (!a) return; - if (!r.end) - var a = r; - else if (reverse) - var a = r.start; - else - var a = r.end; - editor.clearSelection(); editor.moveCursorTo(a.row, a.column); - } - this.sel = function(editor){ - var r = getRange(editor); - if (!r) + }; + m.sel = function(editor, range, count, param) { + var a = getPos(editor, range, count, param, true); + if (!a) return; - if (extend) - return editor.selection.setSelectionRange(r); - - if (!r.end) - var a = r; - else if (reverse) - var a = r.start; - else - var a = r.end; - editor.selection.selectTo(a.row, a.column); - } + }; + return m; } var nonWordRe = /[\s.\/\\()\"'-:,.;<>~!@#$%^&*|+=\[\]{}`~?]/; var wordSeparatorRe = /[.\/\\()\"'-:,.;<>~!@#$%^&*|+=\[\]{}`~?]/; var whiteRe = /\s/; @@ -96,34 +73,34 @@ this.range = sel.getRange(); cursor = cursor || sel.selectionLead; this.row = cursor.row; this.col = cursor.column; var line = editor.session.getLine(this.row); - var maxRow = editor.session.getLength() - this.ch = line[this.col] || '\n' + var maxRow = editor.session.getLength(); + this.ch = line[this.col] || '\n'; this.skippedLines = 0; this.next = function() { this.ch = line[++this.col] || this.handleNewLine(1); //this.debug() return this.ch; - } + }; this.prev = function() { this.ch = line[--this.col] || this.handleNewLine(-1); //this.debug() return this.ch; - } + }; this.peek = function(dir) { var ch = line[this.col + dir]; if (ch) return ch; if (dir == -1) return '\n'; if (this.col == line.length - 1) return '\n'; return editor.session.getLine(this.row + 1)[0] || '\n'; - } + }; this.handleNewLine = function(dir) { if (dir == 1){ if (this.col == line.length) return '\n'; @@ -134,34 +111,34 @@ line = editor.session.getLine(this.row); this.skippedLines++; return line[0] || '\n'; } if (dir == -1) { - if (this.row == 0) + if (this.row === 0) return ''; this.row --; line = editor.session.getLine(this.row); this.col = line.length; this.skippedLines--; return '\n'; } - } + }; this.debug = function() { console.log(line.substring(0, this.col)+'|'+this.ch+'\''+this.col+'\''+line.substr(this.col+1)); - } -} + }; +}; -var Search = require("ace/search").Search; +var Search = require("../../../search").Search; var search = new Search(); function find(editor, needle, dir) { search.$options.needle = needle; search.$options.backwards = dir == -1; return search.find(editor.session); } -var Range = require("ace/range").Range; +var Range = require("../../../range").Range; module.exports = { "w": new Motion(function(editor) { var str = new StringStream(editor); @@ -185,11 +162,11 @@ if (str.skippedLines == 2) str.prev(); else str.next(); - return {column: str.col, row: str.row} + return {column: str.col, row: str.row}; }), "b": new Motion(function(editor) { var str = new StringStream(editor); str.prev(); @@ -205,20 +182,20 @@ } str.ch && str.next(); return {column: str.col, row: str.row}; }), "B": new Motion(function(editor) { - var str = new StringStream(editor) + var str = new StringStream(editor); str.prev(); while(str.ch && !(!whiteRe.test(str.ch) && whiteRe.test(str.peek(-1))) && str.skippedLines > -2) str.prev(); if (str.skippedLines == -2) str.next(); return {column: str.col, row: str.row}; - }, true), + }), "e": new Motion(function(editor) { var str = new StringStream(editor); str.next(); while (str.ch && whiteRe.test(str.ch)) @@ -268,10 +245,44 @@ var pos = editor.getCursorPosition(); if (pos.column > 0) editor.selection.selectLeft(); } }, + "H": { + nav: function(editor) { + var row = editor.renderer.getScrollTopRow(); + editor.moveCursorTo(row); + }, + sel: function(editor) { + var row = editor.renderer.getScrollTopRow(); + editor.selection.selectTo(row); + } + }, + "M": { + nav: function(editor) { + var topRow = editor.renderer.getScrollTopRow(); + var bottomRow = editor.renderer.getScrollBottomRow(); + var row = topRow + ((bottomRow - topRow) / 2); + editor.moveCursorTo(row); + }, + sel: function(editor) { + var topRow = editor.renderer.getScrollTopRow(); + var bottomRow = editor.renderer.getScrollBottomRow(); + var row = topRow + ((bottomRow - topRow) / 2); + editor.selection.selectTo(row); + } + }, + "L": { + nav: function(editor) { + var row = editor.renderer.getScrollBottomRow(); + editor.moveCursorTo(row); + }, + sel: function(editor) { + var row = editor.renderer.getScrollBottomRow(); + editor.selection.selectTo(row); + } + }, "k": { nav: function(editor) { editor.navigateUp(); }, sel: function(editor) { @@ -361,103 +372,62 @@ break; } } }, - "f": { + "f": new Motion({ param: true, handlesCount: true, - nav: function(editor, range, count, param) { - var ed = editor; - var cursor = ed.getCursorPosition(); + getPos: function(editor, range, count, param, isSel) { + var cursor = editor.getCursorPosition(); var column = util.getRightNthChar(editor, cursor, param, count || 1); if (typeof column === "number") { - ed.selection.clearSelection(); // Why does it select in the first place? - ed.moveCursorTo(cursor.row, column + cursor.column + 1); + cursor.column += column + (isSel ? 2 : 1); + return cursor; } - }, - sel: function(editor, range, count, param) { - var ed = editor; - var cursor = ed.getCursorPosition(); - var column = util.getRightNthChar(editor, cursor, param, count || 1); - - if (typeof column === "number") { - ed.moveCursorTo(cursor.row, column + cursor.column + 1); - } } - }, - "F": { + }), + "F": new Motion({ param: true, handlesCount: true, - nav: function(editor, range, count, param) { - count = parseInt(count, 10) || 1; - var ed = editor; - var cursor = ed.getCursorPosition(); - var column = util.getLeftNthChar(editor, cursor, param, count); - - if (typeof column === "number") { - ed.selection.clearSelection(); // Why does it select in the first place? - ed.moveCursorTo(cursor.row, cursor.column - column - 1); - } - }, - sel: function(editor, range, count, param) { - var ed = editor; - var cursor = ed.getCursorPosition(); + getPos: function(editor, range, count, param, isSel) { + var cursor = editor.getCursorPosition(); var column = util.getLeftNthChar(editor, cursor, param, count || 1); if (typeof column === "number") { - ed.moveCursorTo(cursor.row, cursor.column - column - 1); + cursor.column -= column + 1; + return cursor; } } - }, - "t": { + }), + "t": new Motion({ param: true, handlesCount: true, - nav: function(editor, range, count, param) { - var ed = editor; - var cursor = ed.getCursorPosition(); + getPos: function(editor, range, count, param, isSel) { + var cursor = editor.getCursorPosition(); var column = util.getRightNthChar(editor, cursor, param, count || 1); if (typeof column === "number") { - ed.selection.clearSelection(); // Why does it select in the first place? - ed.moveCursorTo(cursor.row, column + cursor.column); + cursor.column += column + (isSel ? 1 : 0); + return cursor; } - }, - sel: function(editor, range, count, param) { - var ed = editor; - var cursor = ed.getCursorPosition(); - var column = util.getRightNthChar(editor, cursor, param, count || 1); - - if (typeof column === "number") { - ed.moveCursorTo(cursor.row, column + cursor.column); - } } - }, - "T": { + }), + "T": new Motion({ param: true, handlesCount: true, - nav: function(editor, range, count, param) { - var ed = editor; - var cursor = ed.getCursorPosition(); + getPos: function(editor, range, count, param, isSel) { + var cursor = editor.getCursorPosition(); var column = util.getLeftNthChar(editor, cursor, param, count || 1); if (typeof column === "number") { - ed.selection.clearSelection(); // Why does it select in the first place? - ed.moveCursorTo(cursor.row, -column + cursor.column); + cursor.column -= column; + return cursor; } - }, - sel: function(editor, range, count, param) { - var ed = editor; - var cursor = ed.getCursorPosition(); - var column = util.getLeftNthChar(editor, cursor, param, count || 1); - - if (typeof column === "number") { - ed.moveCursorTo(cursor.row, -column + cursor.column); - } } - }, + }), "^": { nav: function(editor) { editor.navigateLineStart(); }, @@ -471,20 +441,13 @@ }, sel: function(editor) { editor.selection.selectLineEnd(); } }, - "0": { - nav: function(editor) { - var ed = editor; - ed.navigateTo(ed.selection.selectionLead.row, 0); - }, - sel: function(editor) { - var ed = editor; - ed.selectTo(ed.selection.selectionLead.row, 0); - } - }, + "0": new Motion(function(ed) { + return {row: ed.selection.lead.row, column: 0}; + }), "G": { nav: function(editor, range, count, param) { if (!count && count !== 0) { // Stupid JS count = editor.session.getLength(); } @@ -578,10 +541,29 @@ column: cursor.column + 1 }); return match; }), + "{": new Motion(function(ed) { + var session = ed.session; + var row = session.selection.lead.row; + while(row > 0 && !/\S/.test(session.getLine(row))) + row--; + while(/\S/.test(session.getLine(row))) + row--; + return {column: 0, row: row}; + }), + "}": new Motion(function(ed) { + var session = ed.session; + var l = session.getLength(); + var row = session.selection.lead.row; + while(row < l && !/\S/.test(session.getLine(row))) + row++; + while(/\S/.test(session.getLine(row))) + row++; + return {column: 0, row: row}; + }), "ctrl-d": { nav: function(editor, range, count, param) { editor.selection.clearSelection(); keepScrollPosition(editor, editor.gotoPageDown); }, @@ -596,10 +578,10 @@ }, sel: function(editor, range, count, param) { keepScrollPosition(editor, editor.selectPageUp); } - }, + } }; module.exports.backspace = module.exports.left = module.exports.h; module.exports.right = module.exports.l; module.exports.up = module.exports.k;