From 8634f2661ddec3380d000a33032af3f557f84cd2 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sat, 9 Mar 2013 18:55:01 +0400 Subject: [PATCH] update emacs mode --- demo/kitchen-sink/demo.js | 6 +++++ lib/ace/editor.js | 11 ++++---- lib/ace/keyboard/emacs.js | 47 +++++++++++++++++++--------------- lib/ace/keyboard/keybinding.js | 7 ++--- 4 files changed, 42 insertions(+), 29 deletions(-) diff --git a/demo/kitchen-sink/demo.js b/demo/kitchen-sink/demo.js index 04683485..adc47c00 100644 --- a/demo/kitchen-sink/demo.js +++ b/demo/kitchen-sink/demo.js @@ -92,6 +92,12 @@ var cmdLine = new layout.singleLineEditor(consoleEl); cmdLine.editor = env.editor; env.editor.cmdLine = cmdLine; +env.editor.showCommandLine = function(val) { + this.cmdLine.focus(); + if (typeof val == "string") + this.cmdLine.setValue(val, 1); +}; + /** * This demonstrates how you can define commands and bind shortcuts to them. */ diff --git a/lib/ace/editor.js b/lib/ace/editor.js index e77bab90..e3ff2361 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -103,7 +103,9 @@ var Editor = function(renderer, session) { * **/ this.setKeyboardHandler = function(keyboardHandler) { - if (typeof keyboardHandler == "string" && keyboardHandler) { + if (!keyboardHandler) { + this.keyBinding.setKeyboardHandler(null); + } else if (typeof keyboardHandler == "string") { this.$keybindingId = keyboardHandler; var _self = this; config.loadModule(["keybinding", keyboardHandler], function(module) { @@ -1279,16 +1281,15 @@ var Editor = function(renderer, session) { _numberRx.lastIndex = 0 var s = this.session.getLine(row) - while(_numberRx.lastIndex < column - 1 ){ + while (_numberRx.lastIndex < column) { var m = _numberRx.exec(s) if(m.index <= column && m.index+m[0].length >= column){ var number = { value: m[0], start: m.index, end: m.index+m[0].length - } - return number + return number; } } return null; @@ -2228,4 +2229,4 @@ config.defineOptions(Editor.prototype, "editor", { }); exports.Editor = Editor; -}); \ No newline at end of file +}); diff --git a/lib/ace/keyboard/emacs.js b/lib/ace/keyboard/emacs.js index 7fb93b0f..7e47eb2c 100644 --- a/lib/ace/keyboard/emacs.js +++ b/lib/ace/keyboard/emacs.js @@ -91,28 +91,29 @@ exports.handler.attach = function(editor) { editor.session.$emacsMark = null; - exports.markMode = function() { - return editor.session.$emacsMark; + editor.emacsMarkMode = function() { + return this.session.$emacsMark; } - exports.setMarkMode = function(p) { - editor.session.$emacsMark = p; + editor.setEmacsMarkMode = function(p) { + this.session.$emacsMark = p; } - editor.on("click",$resetMarkMode); + editor.on("click", $resetMarkMode); editor.on("changeSession",$kbSessionChange); editor.renderer.screenToTextCoordinates = screenToTextBlockCoordinates; editor.setStyle("emacs-mode"); editor.commands.addCommands(commands); exports.handler.platform = editor.commands.platform; + editor.$emacsModeHandler = this; }; exports.handler.detach = function(editor) { delete editor.renderer.screenToTextCoordinates; editor.session.$selectLongWords = $formerLongWords; editor.session.$useEmacsStyleLineStart = $formerLineStart; - editor.removeEventListener("click",$resetMarkMode); - editor.removeEventListener("changeSession",$kbSessionChange); + editor.removeEventListener("click", $resetMarkMode); + editor.removeEventListener("changeSession", $kbSessionChange); editor.unsetStyle("emacs-mode"); editor.commands.removeCommands(commands); }; @@ -166,9 +167,10 @@ exports.handler.bindKey = function(key, command) { exports.handler.handleKeyboard = function(data, hashId, key, keyCode) { + var editor = data.editor; // insertstring data.count times if (hashId == -1) { - exports.setMarkMode(null); + editor.setEmacsMarkMode(null); if (data.count) { var str = Array(data.count + 1).join(key); data.count = null; @@ -223,7 +225,7 @@ exports.handler.handleKeyboard = function(data, hashId, key, keyCode) { args = command.args; if (command.command) command = command.command; if (command === "goorselect") { - command = exports.markMode() ? args[1] : args[0]; + command = editor.emacsMarkMode() ? args[1] : args[0]; args = null; } } @@ -232,9 +234,9 @@ exports.handler.handleKeyboard = function(data, hashId, key, keyCode) { if (command === "insertstring" || command === "splitline" || command === "togglecomment") { - exports.setMarkMode(null); + editor.setEmacsMarkMode(null); } - command = this.commands[command] || data.editor.commands.commands[command]; + command = this.commands[command] || editor.commands.commands[command]; } if (!command.readonly && !command.isYank) @@ -330,10 +332,10 @@ exports.emacsKeys = { "C-/|C-x u|S-C--|C-z": "undo", "S-C-/|S-C-x u|C--|S-C-z": "redo", //infinite undo? // vertical editing - "C-x r": "selectRectangularRegion" - + "C-x r": "selectRectangularRegion", + "M-x": {command: "focusCommandLine", args: "M-x "} // todo - // "M-x" "C-x C-t" "M-t" "M-c" "F11" "C-M- "M-q" + // "C-x C-t" "M-t" "M-c" "F11" "C-M- "M-q" }; @@ -365,21 +367,20 @@ exports.handler.addCommands({ // "goto" commands become "select" commands. // Any insertion or mouse click resets mark-mode. // setMark twice in a row at the same place resets markmode - var markMode = exports.markMode(); + var markMode = editor.emacsMarkMode(); if (markMode) { var cp = editor.getCursorPosition(); if (editor.selection.isEmpty() && - markMode.row == cp.row && markMode.column == cp.column) { - exports.setMarkMode(null); + markMode.row == cp.row && markMode.column == cp.column) { + editor.setEmacsMarkMode(null); // console.log("Mark mode off"); return; } } // turn on mark mode markMode = editor.getCursorPosition(); - exports.setMarkMode(markMode); + editor.setEmacsMarkMode(markMode); editor.selection.setSelectionAnchor(markMode.row, markMode.column); - }, exchangePointAndMark: { exec: function(editor) { @@ -407,7 +408,7 @@ exports.handler.addCommands({ multiselectAction: "forEach" }, killLine: function(editor) { - exports.setMarkMode(null); + editor.setEmacsMarkMode(null); var pos = editor.getCursorPosition(); if (pos.column == 0 && editor.session.doc.getLine(pos.row).length == 0) { @@ -448,7 +449,11 @@ exports.handler.addCommands({ }, keyboardQuit: function(editor) { editor.selection.clearSelection(); - exports.setMarkMode(null); + editor.setEmacsMarkMode(null); + }, + focusCommandLine: function(editor, arg) { + if (editor.showCommandLine) + editor.showCommandLine(arg); } }); diff --git a/lib/ace/keyboard/keybinding.js b/lib/ace/keyboard/keybinding.js index 3078c2d0..e559ff7b 100644 --- a/lib/ace/keyboard/keybinding.js +++ b/lib/ace/keyboard/keybinding.js @@ -50,11 +50,12 @@ var KeyBinding = function(editor) { }; this.setKeyboardHandler = function(kb) { - if (this.$handlers[this.$handlers.length - 1] == kb) + var h = this.$handlers; + if (h[h.length - 1] == kb) return; - while (this.$handlers[1]) - this.removeKeyboardHandler(this.$handlers[1]); + while (h[h.length - 1] && h[h.length - 1] != this.defaultHandler) + this.removeKeyboardHandler(h[h.length - 1]); this.addKeyboardHandler(kb, 1); };