diff --git a/main.go b/main.go index 2726f37..c526665 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,8 @@ import ( ) const ( - Ver = "1.0.1" // 当前 Wide 版本 + Ver = "1.0.1" // Wide 版本 + CodeMirrorVer = "4.7" // 编辑器版本 ) // Wide 中唯一一个 init 函数. @@ -146,7 +147,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale, "session": wideSession, "latestSessionContent": userConf.LatestSessionContent, - "pathSeparator": conf.PathSeparator} + "pathSeparator": conf.PathSeparator, "codeMirrorVer": CodeMirrorVer} glog.V(3).Infof("User [%s] has [%d] sessions", username, len(wideSessions)) diff --git a/static/js/editors.js b/static/js/editors.js index 721ddc7..031b577 100644 --- a/static/js/editors.js +++ b/static/js/editors.js @@ -445,7 +445,10 @@ var editors = { var rulers = []; rulers.push({color: "#ccc", column: 120, lineStyle: "dashed"}); - var editor = CodeMirror.fromTextArea(document.getElementById("editor" + id), { + var textArea = document.getElementById("editor" + id); + textArea.value = data.content; + + var editor = CodeMirror.fromTextArea(textArea, { lineNumbers: true, autofocus: true, autoCloseBrackets: true, @@ -505,7 +508,6 @@ var editors = { }); editor.setSize('100%', $(".edit-panel").height() - $(".edit-panel .tabs").height()); - editor.setValue(data.content); editor.setOption("mode", data.mode); editor.setOption("gutters", ["CodeMirror-lint-markers", "CodeMirror-foldgutter"]); diff --git a/static/js/lib/codemirror-4.5/mode/meta.js b/static/js/lib/codemirror-4.5/mode/meta.js deleted file mode 100644 index e3c32b6..0000000 --- a/static/js/lib/codemirror-4.5/mode/meta.js +++ /dev/null @@ -1,115 +0,0 @@ -// CodeMirror, copyright (c) by Marijn Haverbeke and others -// Distributed under an MIT license: http://codemirror.net/LICENSE - -(function(mod) { - if (typeof exports == "object" && typeof module == "object") // CommonJS - mod(require("../lib/codemirror")); - else if (typeof define == "function" && define.amd) // AMD - define(["../lib/codemirror"], mod); - else // Plain browser env - mod(CodeMirror); -})(function(CodeMirror) { - "use strict"; - -CodeMirror.modeInfo = [ - {name: "APL", mime: "text/apl", mode: "apl"}, - {name: "Asterisk", mime: "text/x-asterisk", mode: "asterisk"}, - {name: "C", mime: "text/x-csrc", mode: "clike"}, - {name: "C++", mime: "text/x-c++src", mode: "clike"}, - {name: "Cobol", mime: "text/x-cobol", mode: "cobol"}, - {name: "Java", mime: "text/x-java", mode: "clike"}, - {name: "C#", mime: "text/x-csharp", mode: "clike"}, - {name: "Scala", mime: "text/x-scala", mode: "clike"}, - {name: "Clojure", mime: "text/x-clojure", mode: "clojure"}, - {name: "CoffeeScript", mime: "text/x-coffeescript", mode: "coffeescript"}, - {name: "Common Lisp", mime: "text/x-common-lisp", mode: "commonlisp"}, - {name: "Cypher", mime: "application/x-cypher-query", mode: "cypher"}, - {name: "CSS", mime: "text/css", mode: "css"}, - {name: "D", mime: "text/x-d", mode: "d"}, - {name: "diff", mime: "text/x-diff", mode: "diff"}, - {name: "DTD", mime: "application/xml-dtd", mode: "dtd"}, - {name: "Dylan", mime: "text/x-dylan", mode: "dylan"}, - {name: "ECL", mime: "text/x-ecl", mode: "ecl"}, - {name: "Eiffel", mime: "text/x-eiffel", mode: "eiffel"}, - {name: "Erlang", mime: "text/x-erlang", mode: "erlang"}, - {name: "Fortran", mime: "text/x-fortran", mode: "fortran"}, - {name: "F#", mime: "text/x-fsharp", mode: "mllike"}, - {name: "Gas", mime: "text/x-gas", mode: "gas"}, - {name: "Gherkin", mime: "text/x-feature", mode: "gherkin"}, - {name: "GitHub Flavored Markdown", mime: "text/x-gfm", mode: "gfm"}, - {name: "Go", mime: "text/x-go", mode: "go"}, - {name: "Groovy", mime: "text/x-groovy", mode: "groovy"}, - {name: "HAML", mime: "text/x-haml", mode: "haml"}, - {name: "Haskell", mime: "text/x-haskell", mode: "haskell"}, - {name: "Haxe", mime: "text/x-haxe", mode: "haxe"}, - {name: "ASP.NET", mime: "application/x-aspx", mode: "htmlembedded"}, - {name: "Embedded Javascript", mime: "application/x-ejs", mode: "htmlembedded"}, - {name: "JavaServer Pages", mime: "application/x-jsp", mode: "htmlembedded"}, - {name: "HTML", mime: "text/html", mode: "htmlmixed"}, - {name: "HTTP", mime: "message/http", mode: "http"}, - {name: "Jade", mime: "text/x-jade", mode: "jade"}, - {name: "JavaScript", mime: "text/javascript", mode: "javascript"}, - {name: "JavaScript", mime: "application/javascript", mode: "javascript"}, - {name: "JSON", mime: "application/x-json", mode: "javascript"}, - {name: "JSON", mime: "application/json", mode: "javascript"}, - {name: "JSON-LD", mime: "application/ld+json", mode: "javascript"}, - {name: "TypeScript", mime: "application/typescript", mode: "javascript"}, - {name: "Jinja2", mime: null, mode: "jinja2"}, - {name: "Julia", mime: "text/x-julia", mode: "julia"}, - {name: "Kotlin", mime: "text/x-kotlin", mode: "kotlin"}, - {name: "LESS", mime: "text/x-less", mode: "css"}, - {name: "LiveScript", mime: "text/x-livescript", mode: "livescript"}, - {name: "Lua", mime: "text/x-lua", mode: "lua"}, - {name: "Markdown (GitHub-flavour)", mime: "text/x-markdown", mode: "markdown"}, - {name: "mIRC", mime: "text/mirc", mode: "mirc"}, - {name: "Nginx", mime: "text/x-nginx-conf", mode: "nginx"}, - {name: "NTriples", mime: "text/n-triples", mode: "ntriples"}, - {name: "OCaml", mime: "text/x-ocaml", mode: "mllike"}, - {name: "Octave", mime: "text/x-octave", mode: "octave"}, - {name: "Pascal", mime: "text/x-pascal", mode: "pascal"}, - {name: "PEG.js", mime: null, mode: "pegjs"}, - {name: "Perl", mime: "text/x-perl", mode: "perl"}, - {name: "PHP", mime: "text/x-php", mode: "php"}, - {name: "PHP(HTML)", mime: "application/x-httpd-php", mode: "php"}, - {name: "Pig", mime: "text/x-pig", mode: "pig"}, - {name: "Plain Text", mime: "text/plain", mode: "null"}, - {name: "Properties files", mime: "text/x-properties", mode: "properties"}, - {name: "Python", mime: "text/x-python", mode: "python"}, - {name: "Puppet", mime: "text/x-puppet", mode: "puppet"}, - {name: "Cython", mime: "text/x-cython", mode: "python"}, - {name: "R", mime: "text/x-rsrc", mode: "r"}, - {name: "reStructuredText", mime: "text/x-rst", mode: "rst"}, - {name: "Ruby", mime: "text/x-ruby", mode: "ruby"}, - {name: "Rust", mime: "text/x-rustsrc", mode: "rust"}, - {name: "Sass", mime: "text/x-sass", mode: "sass"}, - {name: "Scheme", mime: "text/x-scheme", mode: "scheme"}, - {name: "SCSS", mime: "text/x-scss", mode: "css"}, - {name: "Shell", mime: "text/x-sh", mode: "shell"}, - {name: "Sieve", mime: "application/sieve", mode: "sieve"}, - {name: "Slim", mime: "text/x-slim", mode: "slim"}, - {name: "Smalltalk", mime: "text/x-stsrc", mode: "smalltalk"}, - {name: "Smarty", mime: "text/x-smarty", mode: "smarty"}, - {name: "SmartyMixed", mime: "text/x-smarty", mode: "smartymixed"}, - {name: "Solr", mime: "text/x-solr", mode: "solr"}, - {name: "SPARQL", mime: "application/x-sparql-query", mode: "sparql"}, - {name: "SQL", mime: "text/x-sql", mode: "sql"}, - {name: "MariaDB", mime: "text/x-mariadb", mode: "sql"}, - {name: "sTeX", mime: "text/x-stex", mode: "stex"}, - {name: "LaTeX", mime: "text/x-latex", mode: "stex"}, - {name: "SystemVerilog", mime: "text/x-systemverilog", mode: "verilog"}, - {name: "Tcl", mime: "text/x-tcl", mode: "tcl"}, - {name: "TiddlyWiki ", mime: "text/x-tiddlywiki", mode: "tiddlywiki"}, - {name: "Tiki wiki", mime: "text/tiki", mode: "tiki"}, - {name: "TOML", mime: "text/x-toml", mode: "toml"}, - {name: "Turtle", mime: "text/turtle", mode: "turtle"}, - {name: "VB.NET", mime: "text/x-vb", mode: "vb"}, - {name: "VBScript", mime: "text/vbscript", mode: "vbscript"}, - {name: "Velocity", mime: "text/velocity", mode: "velocity"}, - {name: "Verilog", mime: "text/x-verilog", mode: "verilog"}, - {name: "XML", mime: "application/xml", mode: "xml"}, - {name: "XQuery", mime: "application/xquery", mode: "xquery"}, - {name: "YAML", mime: "text/x-yaml", mode: "yaml"}, - {name: "Z80", mime: "text/x-z80", mode: "z80"} -]; - -}); diff --git a/static/js/lib/codemirror-4.5/addon/comment/comment.js b/static/js/lib/codemirror-4.7/addon/comment/comment.js similarity index 98% rename from static/js/lib/codemirror-4.5/addon/comment/comment.js rename to static/js/lib/codemirror-4.7/addon/comment/comment.js index d7f569c..2dd114d 100644 --- a/static/js/lib/codemirror-4.5/addon/comment/comment.js +++ b/static/js/lib/codemirror-4.7/addon/comment/comment.js @@ -109,7 +109,7 @@ CodeMirror.defineExtension("uncomment", function(from, to, options) { if (!options) options = noOptions; var self = this, mode = self.getModeAt(from); - var end = Math.min(to.line, self.lastLine()), start = Math.min(from.line, end); + var end = Math.min(to.ch != 0 || to.line == from.line ? to.line : to.line - 1, self.lastLine()), start = Math.min(from.line, end); // Try finding line comments var lineString = options.lineComment || mode.lineComment, lines = []; diff --git a/static/js/lib/codemirror-4.5/addon/comment/continuecomment.js b/static/js/lib/codemirror-4.7/addon/comment/continuecomment.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/comment/continuecomment.js rename to static/js/lib/codemirror-4.7/addon/comment/continuecomment.js diff --git a/static/js/lib/codemirror-4.5/addon/dialog/dialog.css b/static/js/lib/codemirror-4.7/addon/dialog/dialog.css similarity index 100% rename from static/js/lib/codemirror-4.5/addon/dialog/dialog.css rename to static/js/lib/codemirror-4.7/addon/dialog/dialog.css diff --git a/static/js/lib/codemirror-4.5/addon/dialog/dialog.js b/static/js/lib/codemirror-4.7/addon/dialog/dialog.js similarity index 94% rename from static/js/lib/codemirror-4.5/addon/dialog/dialog.js rename to static/js/lib/codemirror-4.7/addon/dialog/dialog.js index 946040c..5a88d99 100644 --- a/static/js/lib/codemirror-4.5/addon/dialog/dialog.js +++ b/static/js/lib/codemirror-4.7/addon/dialog/dialog.js @@ -56,7 +56,10 @@ var inp = dialog.getElementsByTagName("input")[0], button; if (inp) { - if (options.value) inp.value = options.value; + if (options.value) { + inp.value = options.value; + inp.select(); + } if (options.onInput) CodeMirror.on(inp, "input", function(e) { options.onInput(e, inp.value, close);}); @@ -129,8 +132,8 @@ CodeMirror.defineExtension("openNotification", function(template, options) { closeNotification(this, close); var dialog = dialogDiv(this, template, options && options.bottom); - var duration = options && (options.duration === undefined ? 5000 : options.duration); var closed = false, doneTimer; + var duration = options && typeof options.duration !== "undefined" ? options.duration : 5000; function close() { if (closed) return; @@ -143,7 +146,10 @@ CodeMirror.e_preventDefault(e); close(); }); + if (duration) - doneTimer = setTimeout(close, options.duration); + doneTimer = setTimeout(close, duration); + + return close; }); }); diff --git a/static/js/lib/codemirror-4.5/addon/display/fullscreen.css b/static/js/lib/codemirror-4.7/addon/display/fullscreen.css similarity index 100% rename from static/js/lib/codemirror-4.5/addon/display/fullscreen.css rename to static/js/lib/codemirror-4.7/addon/display/fullscreen.css diff --git a/static/js/lib/codemirror-4.5/addon/display/fullscreen.js b/static/js/lib/codemirror-4.7/addon/display/fullscreen.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/display/fullscreen.js rename to static/js/lib/codemirror-4.7/addon/display/fullscreen.js diff --git a/static/js/lib/codemirror-4.5/addon/display/placeholder.js b/static/js/lib/codemirror-4.7/addon/display/placeholder.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/display/placeholder.js rename to static/js/lib/codemirror-4.7/addon/display/placeholder.js diff --git a/static/js/lib/codemirror-4.5/addon/display/rulers.js b/static/js/lib/codemirror-4.7/addon/display/rulers.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/display/rulers.js rename to static/js/lib/codemirror-4.7/addon/display/rulers.js diff --git a/static/js/lib/codemirror-4.5/addon/edit/closebrackets.js b/static/js/lib/codemirror-4.7/addon/edit/closebrackets.js similarity index 96% rename from static/js/lib/codemirror-4.5/addon/edit/closebrackets.js rename to static/js/lib/codemirror-4.7/addon/edit/closebrackets.js index 1a04f36..32eca01 100644 --- a/static/js/lib/codemirror-4.5/addon/edit/closebrackets.js +++ b/static/js/lib/codemirror-4.7/addon/edit/closebrackets.js @@ -78,24 +78,25 @@ for (var i = 0; i < ranges.length; i++) { var range = ranges[i], cur = range.head, curType; var next = cm.getRange(cur, Pos(cur.line, cur.ch + 1)); - if (!range.empty()) + if (!range.empty()) { curType = "surround"; - else if (left == right && next == right) { + } else if (left == right && next == right) { if (cm.getRange(cur, Pos(cur.line, cur.ch + 3)) == left + left + left) curType = "skipThree"; else curType = "skip"; } else if (left == right && cur.ch > 1 && cm.getRange(Pos(cur.line, cur.ch - 2), cur) == left + left && - (cur.ch <= 2 || cm.getRange(Pos(cur.line, cur.ch - 3), Pos(cur.line, cur.ch - 2)) != left)) + (cur.ch <= 2 || cm.getRange(Pos(cur.line, cur.ch - 3), Pos(cur.line, cur.ch - 2)) != left)) { curType = "addFour"; - else if (left == '"' || left == "'") { + } else if (left == '"' || left == "'") { if (!CodeMirror.isWordChar(next) && enteringString(cm, cur, left)) curType = "both"; else return CodeMirror.Pass; - } else if (cm.getLine(cur.line).length == cur.ch || closingBrackets.indexOf(next) >= 0 || SPACE_CHAR_REGEX.test(next)) + } else if (cm.getLine(cur.line).length == cur.ch || closingBrackets.indexOf(next) >= 0 || SPACE_CHAR_REGEX.test(next)) { curType = "both"; - else + } else { return CodeMirror.Pass; + } if (!type) type = curType; else if (type != curType) return CodeMirror.Pass; } diff --git a/static/js/lib/codemirror-4.5/addon/edit/closetag.js b/static/js/lib/codemirror-4.7/addon/edit/closetag.js similarity index 88% rename from static/js/lib/codemirror-4.5/addon/edit/closetag.js rename to static/js/lib/codemirror-4.7/addon/edit/closetag.js index 69ea444..a0bec7d 100644 --- a/static/js/lib/codemirror-4.5/addon/edit/closetag.js +++ b/static/js/lib/codemirror-4.7/addon/edit/closetag.js @@ -102,11 +102,25 @@ var pos = ranges[i].head, tok = cm.getTokenAt(pos); var inner = CodeMirror.innerMode(cm.getMode(), tok.state), state = inner.state; if (tok.type == "string" || tok.string.charAt(0) != "<" || - tok.start != pos.ch - 1 || inner.mode.name != "xml" || - !state.context || !state.context.tagName || - closingTagExists(cm, state.context.tagName, pos, state)) + tok.start != pos.ch - 1) return CodeMirror.Pass; - replacements[i] = "/" + state.context.tagName + ">"; + // Kludge to get around the fact that we are not in XML mode + // when completing in JS/CSS snippet in htmlmixed mode. Does not + // work for other XML embedded languages (there is no general + // way to go from a mixed mode to its current XML state). + if (inner.mode.name != "xml") { + if (cm.getMode().name == "htmlmixed" && inner.mode.name == "javascript") + replacements[i] = "/script>"; + else if (cm.getMode().name == "htmlmixed" && inner.mode.name == "css") + replacements[i] = "/style>"; + else + return CodeMirror.Pass; + } else { + if (!state.context || !state.context.tagName || + closingTagExists(cm, state.context.tagName, pos, state)) + return CodeMirror.Pass; + replacements[i] = "/" + state.context.tagName + ">"; + } } cm.replaceSelections(replacements); ranges = cm.listSelections(); diff --git a/static/js/lib/codemirror-4.5/addon/edit/continuelist.js b/static/js/lib/codemirror-4.7/addon/edit/continuelist.js similarity index 51% rename from static/js/lib/codemirror-4.5/addon/edit/continuelist.js rename to static/js/lib/codemirror-4.7/addon/edit/continuelist.js index 8cee761..9ad0a98 100644 --- a/static/js/lib/codemirror-4.5/addon/edit/continuelist.js +++ b/static/js/lib/codemirror-4.7/addon/edit/continuelist.js @@ -11,7 +11,8 @@ })(function(CodeMirror) { "use strict"; - var listRE = /^(\s*)([*+-]|(\d+)\.)(\s+)/, + var listRE = /^(\s*)([> ]+|[*+-]|(\d+)\.)(\s+)/, + emptyListRE = /^(\s*)([> ]+|[*+-]|(\d+)\.)(\s*)$/, unorderedBullets = "*+-"; CodeMirror.commands.newlineAndIndentContinueMarkdownList = function(cm) { @@ -19,18 +20,30 @@ var ranges = cm.listSelections(), replacements = []; for (var i = 0; i < ranges.length; i++) { var pos = ranges[i].head, match; - var inList = cm.getStateAfter(pos.line).list !== false; + var eolState = cm.getStateAfter(pos.line); + var inList = eolState.list !== false; + var inQuote = eolState.quote !== false; - if (!ranges[i].empty() || !inList || !(match = cm.getLine(pos.line).match(listRE))) { + if (!ranges[i].empty() || (!inList && !inQuote) || !(match = cm.getLine(pos.line).match(listRE))) { cm.execCommand("newlineAndIndent"); return; } - var indent = match[1], after = match[4]; - var bullet = unorderedBullets.indexOf(match[2]) >= 0 - ? match[2] - : (parseInt(match[3], 10) + 1) + "."; + if (cm.getLine(pos.line).match(emptyListRE)) { + cm.replaceRange("", { + line: pos.line, ch: 0 + }, { + line: pos.line, ch: pos.ch + 1 + }); + replacements[i] = "\n"; - replacements[i] = "\n" + indent + bullet + after; + } else { + var indent = match[1], after = match[4]; + var bullet = unorderedBullets.indexOf(match[2]) >= 0 || match[2].indexOf(">") >= 0 + ? match[2] + : (parseInt(match[3], 10) + 1) + "."; + + replacements[i] = "\n" + indent + bullet + after; + } } cm.replaceSelections(replacements); diff --git a/static/js/lib/codemirror-4.5/addon/edit/matchbrackets.js b/static/js/lib/codemirror-4.7/addon/edit/matchbrackets.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/edit/matchbrackets.js rename to static/js/lib/codemirror-4.7/addon/edit/matchbrackets.js diff --git a/static/js/lib/codemirror-4.5/addon/edit/matchtags.js b/static/js/lib/codemirror-4.7/addon/edit/matchtags.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/edit/matchtags.js rename to static/js/lib/codemirror-4.7/addon/edit/matchtags.js diff --git a/static/js/lib/codemirror-4.5/addon/edit/trailingspace.js b/static/js/lib/codemirror-4.7/addon/edit/trailingspace.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/edit/trailingspace.js rename to static/js/lib/codemirror-4.7/addon/edit/trailingspace.js diff --git a/static/js/lib/codemirror-4.5/addon/fold/brace-fold.js b/static/js/lib/codemirror-4.7/addon/fold/brace-fold.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/fold/brace-fold.js rename to static/js/lib/codemirror-4.7/addon/fold/brace-fold.js diff --git a/static/js/lib/codemirror-4.5/addon/fold/comment-fold.js b/static/js/lib/codemirror-4.7/addon/fold/comment-fold.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/fold/comment-fold.js rename to static/js/lib/codemirror-4.7/addon/fold/comment-fold.js diff --git a/static/js/lib/codemirror-4.5/addon/fold/foldcode.js b/static/js/lib/codemirror-4.7/addon/fold/foldcode.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/fold/foldcode.js rename to static/js/lib/codemirror-4.7/addon/fold/foldcode.js diff --git a/static/js/lib/codemirror-4.5/addon/fold/foldgutter.css b/static/js/lib/codemirror-4.7/addon/fold/foldgutter.css similarity index 100% rename from static/js/lib/codemirror-4.5/addon/fold/foldgutter.css rename to static/js/lib/codemirror-4.7/addon/fold/foldgutter.css diff --git a/static/js/lib/codemirror-4.5/addon/fold/foldgutter.js b/static/js/lib/codemirror-4.7/addon/fold/foldgutter.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/fold/foldgutter.js rename to static/js/lib/codemirror-4.7/addon/fold/foldgutter.js diff --git a/static/js/lib/codemirror-4.5/addon/fold/indent-fold.js b/static/js/lib/codemirror-4.7/addon/fold/indent-fold.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/fold/indent-fold.js rename to static/js/lib/codemirror-4.7/addon/fold/indent-fold.js diff --git a/static/js/lib/codemirror-4.5/addon/fold/markdown-fold.js b/static/js/lib/codemirror-4.7/addon/fold/markdown-fold.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/fold/markdown-fold.js rename to static/js/lib/codemirror-4.7/addon/fold/markdown-fold.js diff --git a/static/js/lib/codemirror-4.5/addon/fold/xml-fold.js b/static/js/lib/codemirror-4.7/addon/fold/xml-fold.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/fold/xml-fold.js rename to static/js/lib/codemirror-4.7/addon/fold/xml-fold.js diff --git a/static/js/lib/codemirror-4.5/addon/hint/anyword-hint.js b/static/js/lib/codemirror-4.7/addon/hint/anyword-hint.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/hint/anyword-hint.js rename to static/js/lib/codemirror-4.7/addon/hint/anyword-hint.js diff --git a/static/js/lib/codemirror-4.5/addon/hint/css-hint.js b/static/js/lib/codemirror-4.7/addon/hint/css-hint.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/hint/css-hint.js rename to static/js/lib/codemirror-4.7/addon/hint/css-hint.js diff --git a/static/js/lib/codemirror-4.5/addon/hint/html-hint.js b/static/js/lib/codemirror-4.7/addon/hint/html-hint.js similarity index 99% rename from static/js/lib/codemirror-4.5/addon/hint/html-hint.js rename to static/js/lib/codemirror-4.7/addon/hint/html-hint.js index addd9b7..992218f 100644 --- a/static/js/lib/codemirror-4.5/addon/hint/html-hint.js +++ b/static/js/lib/codemirror-4.7/addon/hint/html-hint.js @@ -3,9 +3,9 @@ (function(mod) { if (typeof exports == "object" && typeof module == "object") // CommonJS - mod(require("../../lib/codemirror")); + mod(require("../../lib/codemirror", "./xml-hint")); else if (typeof define == "function" && define.amd) // AMD - define(["../../lib/codemirror"], mod); + define(["../../lib/codemirror", "./xml-hint"], mod); else // Plain browser env mod(CodeMirror); })(function(CodeMirror) { diff --git a/static/js/lib/codemirror-4.5/addon/hint/javascript-hint.js b/static/js/lib/codemirror-4.7/addon/hint/javascript-hint.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/hint/javascript-hint.js rename to static/js/lib/codemirror-4.7/addon/hint/javascript-hint.js diff --git a/static/js/lib/codemirror-4.5/addon/hint/python-hint.js b/static/js/lib/codemirror-4.7/addon/hint/python-hint.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/hint/python-hint.js rename to static/js/lib/codemirror-4.7/addon/hint/python-hint.js diff --git a/static/js/lib/codemirror-4.5/addon/hint/show-hint.css b/static/js/lib/codemirror-4.7/addon/hint/show-hint.css similarity index 95% rename from static/js/lib/codemirror-4.5/addon/hint/show-hint.css rename to static/js/lib/codemirror-4.7/addon/hint/show-hint.css index 8a4ff05..924e638 100644 --- a/static/js/lib/codemirror-4.5/addon/hint/show-hint.css +++ b/static/js/lib/codemirror-4.7/addon/hint/show-hint.css @@ -32,7 +32,7 @@ cursor: pointer; } -.CodeMirror-hint-active { +li.CodeMirror-hint-active { background: #08f; color: white; } diff --git a/static/js/lib/codemirror-4.5/addon/hint/show-hint.js b/static/js/lib/codemirror-4.7/addon/hint/show-hint.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/hint/show-hint.js rename to static/js/lib/codemirror-4.7/addon/hint/show-hint.js diff --git a/static/js/lib/codemirror-4.5/addon/hint/sql-hint.js b/static/js/lib/codemirror-4.7/addon/hint/sql-hint.js similarity index 66% rename from static/js/lib/codemirror-4.5/addon/hint/sql-hint.js rename to static/js/lib/codemirror-4.7/addon/hint/sql-hint.js index 20653b5..c2b511f 100644 --- a/static/js/lib/codemirror-4.5/addon/hint/sql-hint.js +++ b/static/js/lib/codemirror-4.7/addon/hint/sql-hint.js @@ -12,6 +12,7 @@ "use strict"; var tables; + var defaultTable; var keywords; var CONS = { QUERY_DIV: ";", @@ -43,18 +44,55 @@ } } - function columnCompletion(result, editor) { + function nameCompletion(result, editor) { var cur = editor.getCursor(); var token = editor.getTokenAt(cur); + var useBacktick = (token.string.charAt(0) == "`"); var string = token.string.substr(1); - var prevCur = Pos(cur.line, token.start); - var table = editor.getTokenAt(prevCur).string; - if (!tables.hasOwnProperty(table)) - table = findTableByAlias(table, editor); - var columns = tables[table]; - if (!columns) return; + var prevToken = editor.getTokenAt(Pos(cur.line, token.start)); + if (token.string.charAt(0) == "." || prevToken.string == "."){ + //Suggest colunm names + if (prevToken.string == ".") { + var prevToken = editor.getTokenAt(Pos(cur.line, token.start - 1)); + } + var table = prevToken.string; + //Check if backtick is used in table name. If yes, use it for columns too. + var useBacktickTable = false; + if (table.match(/`/g)) { + useBacktickTable = true; + table = table.replace(/`/g, ""); + } + //Check if table is available. If not, find table by Alias + if (!tables.hasOwnProperty(table)) + table = findTableByAlias(table, editor); + var columns = tables[table]; + if (!columns) return; - addMatches(result, string, columns, function(w) {return "." + w;}); + if (useBacktick) { + addMatches(result, string, columns, function(w) {return "`" + w + "`";}); + } + else if(useBacktickTable) { + addMatches(result, string, columns, function(w) {return ".`" + w + "`";}); + } + else { + addMatches(result, string, columns, function(w) {return "." + w;}); + } + } + else { + //Suggest table names or colums in defaultTable + while (token.start && string.charAt(0) == ".") { + token = editor.getTokenAt(Pos(cur.line, token.start - 1)); + string = token.string + string; + } + if (useBacktick) { + addMatches(result, string, tables, function(w) {return "`" + w + "`";}); + addMatches(result, string, defaultTable, function(w) {return "`" + w + "`";}); + } + else { + addMatches(result, string, tables, function(w) {return w;}); + addMatches(result, string, defaultTable, function(w) {return w;}); + } + } } function eachWord(lineText, f) { @@ -128,11 +166,14 @@ CodeMirror.registerHelper("hint", "sql", function(editor, options) { tables = (options && options.tables) || {}; + var defaultTableName = options && options.defaultTable; + defaultTable = (defaultTableName && tables[defaultTableName] || []); keywords = keywords || getKeywords(editor); + var cur = editor.getCursor(); var result = []; var token = editor.getTokenAt(cur), start, end, search; - if (token.string.match(/^[.\w@]\w*$/)) { + if (token.string.match(/^[.`\w@]\w*$/)) { search = token.string; start = token.start; end = token.end; @@ -140,18 +181,11 @@ start = end = cur.ch; search = ""; } - if (search.charAt(0) == ".") { - columnCompletion(result, editor); - if (!result.length) { - while (start && search.charAt(0) == ".") { - token = editor.getTokenAt(Pos(cur.line, token.start - 1)); - start = token.start; - search = token.string + search; - } - addMatches(result, search, tables, function(w) {return w;}); - } + if (search.charAt(0) == "." || search.charAt(0) == "`") { + nameCompletion(result, editor); } else { addMatches(result, search, tables, function(w) {return w;}); + addMatches(result, search, defaultTable, function(w) {return w;}); addMatches(result, search, keywords, function(w) {return w.toUpperCase();}); } diff --git a/static/js/lib/codemirror-4.5/addon/hint/xml-hint.js b/static/js/lib/codemirror-4.7/addon/hint/xml-hint.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/hint/xml-hint.js rename to static/js/lib/codemirror-4.7/addon/hint/xml-hint.js diff --git a/static/js/lib/codemirror-4.5/addon/lint/coffeescript-lint.js b/static/js/lib/codemirror-4.7/addon/lint/coffeescript-lint.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/lint/coffeescript-lint.js rename to static/js/lib/codemirror-4.7/addon/lint/coffeescript-lint.js diff --git a/static/js/lib/codemirror-4.5/addon/lint/css-lint.js b/static/js/lib/codemirror-4.7/addon/lint/css-lint.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/lint/css-lint.js rename to static/js/lib/codemirror-4.7/addon/lint/css-lint.js diff --git a/static/js/lib/codemirror-4.5/addon/lint/javascript-lint.js b/static/js/lib/codemirror-4.7/addon/lint/javascript-lint.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/lint/javascript-lint.js rename to static/js/lib/codemirror-4.7/addon/lint/javascript-lint.js diff --git a/static/js/lib/codemirror-4.5/addon/lint/json-lint.js b/static/js/lib/codemirror-4.7/addon/lint/json-lint.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/lint/json-lint.js rename to static/js/lib/codemirror-4.7/addon/lint/json-lint.js diff --git a/static/js/lib/codemirror-4.5/addon/lint/lint.css b/static/js/lib/codemirror-4.7/addon/lint/lint.css similarity index 100% rename from static/js/lib/codemirror-4.5/addon/lint/lint.css rename to static/js/lib/codemirror-4.7/addon/lint/lint.css diff --git a/static/js/lib/codemirror-4.5/addon/lint/lint.js b/static/js/lib/codemirror-4.7/addon/lint/lint.js similarity index 88% rename from static/js/lib/codemirror-4.5/addon/lint/lint.js rename to static/js/lib/codemirror-4.7/addon/lint/lint.js index a87e70c..66f187e 100644 --- a/static/js/lib/codemirror-4.5/addon/lint/lint.js +++ b/static/js/lib/codemirror-4.7/addon/lint/lint.js @@ -118,10 +118,11 @@ function startLinting(cm) { var state = cm.state.lint, options = state.options; + var passOptions = options.options || options; // Support deprecated passing of `options` property in options if (options.async) - options.getAnnotations(cm, updateLinting, options); + options.getAnnotations(cm.getValue(), updateLinting, passOptions, cm); else - updateLinting(cm, options.getAnnotations(cm.getValue(), options.options)); + updateLinting(cm, options.getAnnotations(cm.getValue(), passOptions, cm)); } function updateLinting(cm, annotationsNotSorted) { @@ -170,20 +171,14 @@ showTooltipFor(e, annotationTooltip(ann), target); } - // When the mouseover fires, the cursor might not actually be over - // the character itself yet. These pairs of x,y offsets are used to - // probe a few nearby points when no suitable marked range is found. - var nearby = [0, 0, 0, 5, 0, -5, 5, 0, -5, 0]; - function onMouseOver(cm, e) { - if (!/\bCodeMirror-lint-mark-/.test((e.target || e.srcElement).className)) return; - for (var i = 0; i < nearby.length; i += 2) { - var spans = cm.findMarksAt(cm.coordsChar({left: e.clientX + nearby[i], - top: e.clientY + nearby[i + 1]}, "client")); - for (var j = 0; j < spans.length; ++j) { - var span = spans[j], ann = span.__annotation; - if (ann) return popupSpanTooltip(ann, e); - } + var target = e.target || e.srcElement; + if (!/\bCodeMirror-lint-mark-/.test(target.className)) return; + var box = target.getBoundingClientRect(), x = (box.left + box.right) / 2, y = (box.top + box.bottom) / 2; + var spans = cm.findMarksAt(cm.coordsChar({left: x, top: y}, "client")); + for (var i = 0; i < spans.length; ++i) { + var ann = spans[i].__annotation; + if (ann) return popupSpanTooltip(ann, e); } } diff --git a/static/js/lib/codemirror-4.5/addon/lint/yaml-lint.js b/static/js/lib/codemirror-4.7/addon/lint/yaml-lint.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/lint/yaml-lint.js rename to static/js/lib/codemirror-4.7/addon/lint/yaml-lint.js diff --git a/static/js/lib/codemirror-4.5/addon/merge/merge.css b/static/js/lib/codemirror-4.7/addon/merge/merge.css similarity index 100% rename from static/js/lib/codemirror-4.5/addon/merge/merge.css rename to static/js/lib/codemirror-4.7/addon/merge/merge.css diff --git a/static/js/lib/codemirror-4.5/addon/merge/merge.js b/static/js/lib/codemirror-4.7/addon/merge/merge.js similarity index 98% rename from static/js/lib/codemirror-4.5/addon/merge/merge.js rename to static/js/lib/codemirror-4.7/addon/merge/merge.js index d9b2776..da3ea47 100644 --- a/static/js/lib/codemirror-4.5/addon/merge/merge.js +++ b/static/js/lib/codemirror-4.7/addon/merge/merge.js @@ -1,17 +1,17 @@ // CodeMirror, copyright (c) by Marijn Haverbeke and others // Distributed under an MIT license: http://codemirror.net/LICENSE +// declare global: diff_match_patch, DIFF_INSERT, DIFF_DELETE, DIFF_EQUAL + (function(mod) { if (typeof exports == "object" && typeof module == "object") // CommonJS - mod(require("../../lib/codemirror")); + mod(require("../../lib/codemirror"), require("diff_match_patch")); else if (typeof define == "function" && define.amd) // AMD - define(["../../lib/codemirror"], mod); + define(["../../lib/codemirror", "diff_match_patch"], mod); else // Plain browser env - mod(CodeMirror); -})(function(CodeMirror) { + mod(CodeMirror, diff_match_patch); +})(function(CodeMirror, diff_match_patch) { "use strict"; - // declare global: diff_match_patch, DIFF_INSERT, DIFF_DELETE, DIFF_EQUAL - var Pos = CodeMirror.Pos; var svgNS = "http://www.w3.org/2000/svg"; diff --git a/static/js/lib/codemirror-4.5/addon/mode/loadmode.js b/static/js/lib/codemirror-4.7/addon/mode/loadmode.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/mode/loadmode.js rename to static/js/lib/codemirror-4.7/addon/mode/loadmode.js diff --git a/static/js/lib/codemirror-4.5/addon/mode/multiplex.js b/static/js/lib/codemirror-4.7/addon/mode/multiplex.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/mode/multiplex.js rename to static/js/lib/codemirror-4.7/addon/mode/multiplex.js diff --git a/static/js/lib/codemirror-4.5/addon/mode/multiplex_test.js b/static/js/lib/codemirror-4.7/addon/mode/multiplex_test.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/mode/multiplex_test.js rename to static/js/lib/codemirror-4.7/addon/mode/multiplex_test.js diff --git a/static/js/lib/codemirror-4.5/addon/mode/overlay.js b/static/js/lib/codemirror-4.7/addon/mode/overlay.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/mode/overlay.js rename to static/js/lib/codemirror-4.7/addon/mode/overlay.js diff --git a/static/js/lib/codemirror-4.7/addon/mode/simple.js b/static/js/lib/codemirror-4.7/addon/mode/simple.js new file mode 100644 index 0000000..a4a86b9 --- /dev/null +++ b/static/js/lib/codemirror-4.7/addon/mode/simple.js @@ -0,0 +1,210 @@ +// CodeMirror, copyright (c) by Marijn Haverbeke and others +// Distributed under an MIT license: http://codemirror.net/LICENSE + +(function(mod) { + if (typeof exports == "object" && typeof module == "object") // CommonJS + mod(require("../../lib/codemirror")); + else if (typeof define == "function" && define.amd) // AMD + define(["../../lib/codemirror"], mod); + else // Plain browser env + mod(CodeMirror); +})(function(CodeMirror) { + "use strict"; + + CodeMirror.defineSimpleMode = function(name, states, props) { + CodeMirror.defineMode(name, function(config) { + return CodeMirror.simpleMode(config, states, props); + }); + }; + + CodeMirror.simpleMode = function(config, states) { + ensureState(states, "start"); + var states_ = {}, meta = states.meta || {}, hasIndentation = false; + for (var state in states) if (state != meta && states.hasOwnProperty(state)) { + var list = states_[state] = [], orig = states[state]; + for (var i = 0; i < orig.length; i++) { + var data = orig[i]; + list.push(new Rule(data, states)); + if (data.indent || data.dedent) hasIndentation = true; + } + } + var mode = { + startState: function() { + return {state: "start", pending: null, + local: null, localState: null, + indent: hasIndentation ? [] : null}; + }, + copyState: function(state) { + var s = {state: state.state, pending: state.pending, + local: state.local, localState: null, + indent: state.indent && state.indent.slice(0)}; + if (state.localState) + s.localState = CodeMirror.copyState(state.local.mode, state.localState); + if (state.stack) + s.stack = state.stack.slice(0); + for (var pers = state.persistentStates; pers; pers = pers.next) + s.persistentStates = {mode: pers.mode, + spec: pers.spec, + state: pers.state == state.localState ? s.localState : CodeMirror.copyState(pers.mode, pers.state), + next: s.persistentStates}; + return s; + }, + token: tokenFunction(states_, config), + innerMode: function(state) { return state.local && {mode: state.local.mode, state: state.localState}; }, + indent: indentFunction(states_, meta) + }; + if (meta) for (var prop in meta) if (meta.hasOwnProperty(prop)) + mode[prop] = meta[prop]; + return mode; + }; + + function ensureState(states, name) { + if (!states.hasOwnProperty(name)) + throw new Error("Undefined state " + name + "in simple mode"); + } + + function toRegex(val, caret) { + if (!val) return /(?:)/; + var flags = ""; + if (val instanceof RegExp) { + if (val.ignoreCase) flags = "i"; + val = val.source; + } else { + val = String(val); + } + return new RegExp((caret === false ? "" : "^") + "(?:" + val + ")", flags); + } + + function asToken(val) { + if (!val) return null; + if (typeof val == "string") return val.replace(/\./g, " "); + var result = []; + for (var i = 0; i < val.length; i++) + result.push(val[i] && val[i].replace(/\./g, " ")); + return result; + } + + function Rule(data, states) { + if (data.next || data.push) ensureState(states, data.next || data.push); + this.regex = toRegex(data.regex); + this.token = asToken(data.token); + this.data = data; + } + + function tokenFunction(states, config) { + return function(stream, state) { + if (state.pending) { + var pend = state.pending.shift(); + if (state.pending.length == 0) state.pending = null; + stream.pos += pend.text.length; + return pend.token; + } + + if (state.local) { + if (state.local.end && stream.match(state.local.end)) { + var tok = state.local.endToken || null; + state.local = state.localState = null; + return tok; + } else { + var tok = state.local.mode.token(stream, state.localState), m; + if (state.local.endScan && (m = state.local.endScan.exec(stream.current()))) + stream.pos = stream.start + m.index; + return tok; + } + } + + var curState = states[state.state]; + for (var i = 0; i < curState.length; i++) { + var rule = curState[i]; + var matches = stream.match(rule.regex); + if (matches) { + if (rule.data.next) { + state.state = rule.data.next; + } else if (rule.data.push) { + (state.stack || (state.stack = [])).push(state.state); + state.state = rule.data.push; + } else if (rule.data.pop && state.stack && state.stack.length) { + state.state = state.stack.pop(); + } + + if (rule.data.mode) + enterLocalMode(config, state, rule.data.mode, rule.token); + if (rule.data.indent) + state.indent.push(stream.indentation() + config.indentUnit); + if (rule.data.dedent) + state.indent.pop(); + if (matches.length > 2) { + state.pending = []; + for (var j = 2; j < matches.length; j++) + if (matches[j]) + state.pending.push({text: matches[j], token: rule.token[j - 1]}); + stream.backUp(matches[0].length - (matches[1] ? matches[1].length : 0)); + return rule.token[0]; + } else if (rule.token && rule.token.join) { + return rule.token[0]; + } else { + return rule.token; + } + } + } + stream.next(); + return null; + }; + } + + function cmp(a, b) { + if (a === b) return true; + if (!a || typeof a != "object" || !b || typeof b != "object") return false; + var props = 0; + for (var prop in a) if (a.hasOwnProperty(prop)) { + if (!b.hasOwnProperty(prop) || !cmp(a[prop], b[prop])) return false; + props++; + } + for (var prop in b) if (b.hasOwnProperty(prop)) props--; + return props == 0; + } + + function enterLocalMode(config, state, spec, token) { + var pers; + if (spec.persistent) for (var p = state.persistentStates; p && !pers; p = p.next) + if (spec.spec ? cmp(spec.spec, p.spec) : spec.mode == p.mode) pers = p; + var mode = pers ? pers.mode : spec.mode || CodeMirror.getMode(config, spec.spec); + var lState = pers ? pers.state : CodeMirror.startState(mode); + if (spec.persistent && !pers) + state.persistentStates = {mode: mode, spec: spec.spec, state: lState, next: state.persistentStates}; + + state.localState = lState; + state.local = {mode: mode, + end: spec.end && toRegex(spec.end), + endScan: spec.end && spec.forceEnd !== false && toRegex(spec.end, false), + endToken: token && token.join ? token[token.length - 1] : token}; + } + + function indexOf(val, arr) { + for (var i = 0; i < arr.length; i++) if (arr[i] === val) return true; + } + + function indentFunction(states, meta) { + return function(state, textAfter, line) { + if (state.local && state.local.mode.indent) + return state.local.mode.indent(state.localState, textAfter, line); + if (state.indent == null || state.local || meta.dontIndentStates && indexOf(state.state, meta.dontIndentStates) > -1) + return CodeMirror.Pass; + + var pos = state.indent.length - 1, rules = states[state.state]; + scan: for (;;) { + for (var i = 0; i < rules.length; i++) { + var rule = rules[i], m = rule.regex.exec(textAfter); + if (m && m[0]) { + if (rule.data.dedent && rule.data.dedentIfLineStart !== false) pos--; + if (rule.next || rule.push) rules = states[rule.next || rule.push]; + textAfter = textAfter.slice(m[0].length); + continue scan; + } + } + break; + } + return pos < 0 ? 0 : state.indent[pos]; + }; + } +}); diff --git a/static/js/lib/codemirror-4.5/addon/runmode/colorize.js b/static/js/lib/codemirror-4.7/addon/runmode/colorize.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/runmode/colorize.js rename to static/js/lib/codemirror-4.7/addon/runmode/colorize.js diff --git a/static/js/lib/codemirror-4.5/addon/runmode/runmode-standalone.js b/static/js/lib/codemirror-4.7/addon/runmode/runmode-standalone.js similarity index 96% rename from static/js/lib/codemirror-4.5/addon/runmode/runmode-standalone.js rename to static/js/lib/codemirror-4.7/addon/runmode/runmode-standalone.js index 0dd0c28..f4f352c 100644 --- a/static/js/lib/codemirror-4.5/addon/runmode/runmode-standalone.js +++ b/static/js/lib/codemirror-4.7/addon/runmode/runmode-standalone.js @@ -74,7 +74,11 @@ CodeMirror.startState = function (mode, a1, a2) { }; var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {}; -CodeMirror.defineMode = function (name, mode) { modes[name] = mode; }; +CodeMirror.defineMode = function (name, mode) { + if (arguments.length > 2) + mode.dependencies = Array.prototype.slice.call(arguments, 2); + modes[name] = mode; +}; CodeMirror.defineMIME = function (mime, spec) { mimeModes[mime] = spec; }; CodeMirror.resolveMode = function(spec) { if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) { diff --git a/static/js/lib/codemirror-4.5/addon/runmode/runmode.js b/static/js/lib/codemirror-4.7/addon/runmode/runmode.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/runmode/runmode.js rename to static/js/lib/codemirror-4.7/addon/runmode/runmode.js diff --git a/static/js/lib/codemirror-4.5/addon/runmode/runmode.node.js b/static/js/lib/codemirror-4.7/addon/runmode/runmode.node.js similarity index 96% rename from static/js/lib/codemirror-4.5/addon/runmode/runmode.node.js rename to static/js/lib/codemirror-4.7/addon/runmode/runmode.node.js index f64b8a1..8b8140b 100644 --- a/static/js/lib/codemirror-4.5/addon/runmode/runmode.node.js +++ b/static/js/lib/codemirror-4.7/addon/runmode/runmode.node.js @@ -74,10 +74,8 @@ exports.startState = function(mode, a1, a2) { var modes = exports.modes = {}, mimeModes = exports.mimeModes = {}; exports.defineMode = function(name, mode) { - if (arguments.length > 2) { - mode.dependencies = []; - for (var i = 2; i < arguments.length; ++i) mode.dependencies.push(arguments[i]); - } + if (arguments.length > 2) + mode.dependencies = Array.prototype.slice.call(arguments, 2); modes[name] = mode; }; exports.defineMIME = function(mime, spec) { mimeModes[mime] = spec; }; diff --git a/static/js/lib/codemirror-4.5/addon/scroll/scrollpastend.js b/static/js/lib/codemirror-4.7/addon/scroll/scrollpastend.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/scroll/scrollpastend.js rename to static/js/lib/codemirror-4.7/addon/scroll/scrollpastend.js diff --git a/static/js/lib/codemirror-4.5/addon/search/match-highlighter.js b/static/js/lib/codemirror-4.7/addon/search/match-highlighter.js similarity index 74% rename from static/js/lib/codemirror-4.5/addon/search/match-highlighter.js rename to static/js/lib/codemirror-4.7/addon/search/match-highlighter.js index 14dd4d4..e9a2272 100644 --- a/static/js/lib/codemirror-4.5/addon/search/match-highlighter.js +++ b/static/js/lib/codemirror-4.7/addon/search/match-highlighter.js @@ -8,12 +8,15 @@ // document. // // The option can be set to true to simply enable it, or to a -// {minChars, style, showToken} object to explicitly configure it. -// minChars is the minimum amount of characters that should be +// {minChars, style, wordsOnly, showToken, delay} object to explicitly +// configure it. minChars is the minimum amount of characters that should be // selected for the behavior to occur, and style is the token style to // apply to the matches. This will be prefixed by "cm-" to create an -// actual CSS class name. showToken, when enabled, will cause the -// current token to be highlighted when nothing is selected. +// actual CSS class name. If wordsOnly is enabled, the matches will be +// highlighted only if the selected text is a word. showToken, when enabled, +// will cause the current token to be highlighted when nothing is selected. +// delay is used to specify how much time to wait, in milliseconds, before +// highlighting the matches. (function(mod) { if (typeof exports == "object" && typeof module == "object") // CommonJS @@ -28,6 +31,7 @@ var DEFAULT_MIN_CHARS = 2; var DEFAULT_TOKEN_STYLE = "matchhighlight"; var DEFAULT_DELAY = 100; + var DEFAULT_WORDS_ONLY = false; function State(options) { if (typeof options == "object") { @@ -35,10 +39,12 @@ this.style = options.style; this.showToken = options.showToken; this.delay = options.delay; + this.wordsOnly = options.wordsOnly; } if (this.style == null) this.style = DEFAULT_TOKEN_STYLE; if (this.minChars == null) this.minChars = DEFAULT_MIN_CHARS; if (this.delay == null) this.delay = DEFAULT_DELAY; + if (this.wordsOnly == null) this.wordsOnly = DEFAULT_WORDS_ONLY; this.overlay = this.timeout = null; } @@ -81,12 +87,30 @@ } var from = cm.getCursor("from"), to = cm.getCursor("to"); if (from.line != to.line) return; + if (state.wordsOnly && !isWord(cm, from, to)) return; var selection = cm.getRange(from, to).replace(/^\s+|\s+$/g, ""); if (selection.length >= state.minChars) cm.addOverlay(state.overlay = makeOverlay(selection, false, state.style)); }); } + function isWord(cm, from, to) { + var str = cm.getRange(from, to); + if (str.match(/^\w+$/) !== null) { + if (from.ch > 0) { + var pos = {line: from.line, ch: from.ch - 1}; + var chr = cm.getRange(pos, from); + if (chr.match(/\W/) === null) return false; + } + if (to.ch < cm.getLine(from.line).length) { + var pos = {line: to.line, ch: to.ch + 1}; + var chr = cm.getRange(to, pos); + if (chr.match(/\W/) === null) return false; + } + return true; + } else return false; + } + function boundariesAround(stream, re) { return (!stream.start || !re.test(stream.string.charAt(stream.start - 1))) && (stream.pos == stream.string.length || !re.test(stream.string.charAt(stream.pos))); diff --git a/static/js/lib/codemirror-4.5/addon/search/search.js b/static/js/lib/codemirror-4.7/addon/search/search.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/search/search.js rename to static/js/lib/codemirror-4.7/addon/search/search.js diff --git a/static/js/lib/codemirror-4.5/addon/search/searchcursor.js b/static/js/lib/codemirror-4.7/addon/search/searchcursor.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/search/searchcursor.js rename to static/js/lib/codemirror-4.7/addon/search/searchcursor.js diff --git a/static/js/lib/codemirror-4.5/addon/selection/active-line.js b/static/js/lib/codemirror-4.7/addon/selection/active-line.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/selection/active-line.js rename to static/js/lib/codemirror-4.7/addon/selection/active-line.js diff --git a/static/js/lib/codemirror-4.5/addon/selection/mark-selection.js b/static/js/lib/codemirror-4.7/addon/selection/mark-selection.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/selection/mark-selection.js rename to static/js/lib/codemirror-4.7/addon/selection/mark-selection.js diff --git a/static/js/lib/codemirror-4.5/addon/tern/tern.css b/static/js/lib/codemirror-4.7/addon/tern/tern.css similarity index 100% rename from static/js/lib/codemirror-4.5/addon/tern/tern.css rename to static/js/lib/codemirror-4.7/addon/tern/tern.css diff --git a/static/js/lib/codemirror-4.5/addon/tern/tern.js b/static/js/lib/codemirror-4.7/addon/tern/tern.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/tern/tern.js rename to static/js/lib/codemirror-4.7/addon/tern/tern.js diff --git a/static/js/lib/codemirror-4.5/addon/tern/worker.js b/static/js/lib/codemirror-4.7/addon/tern/worker.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/tern/worker.js rename to static/js/lib/codemirror-4.7/addon/tern/worker.js diff --git a/static/js/lib/codemirror-4.5/addon/wrap/hardwrap.js b/static/js/lib/codemirror-4.7/addon/wrap/hardwrap.js similarity index 100% rename from static/js/lib/codemirror-4.5/addon/wrap/hardwrap.js rename to static/js/lib/codemirror-4.7/addon/wrap/hardwrap.js diff --git a/static/js/lib/codemirror-4.5/codemirror.css b/static/js/lib/codemirror-4.7/codemirror.css similarity index 95% rename from static/js/lib/codemirror-4.5/codemirror.css rename to static/js/lib/codemirror-4.7/codemirror.css index c089777..68c67b1 100644 --- a/static/js/lib/codemirror-4.5/codemirror.css +++ b/static/js/lib/codemirror-4.7/codemirror.css @@ -57,6 +57,10 @@ border: 0; background: #7e7; } +.CodeMirror.cm-keymap-fat-cursor div.CodeMirror-cursors { + z-index: 1; +} + .cm-animate-fat-cursor { width: auto; border: 0; @@ -83,7 +87,7 @@ /* Can style cursor different in overwrite (non-insert) mode */ div.CodeMirror-overwrite div.CodeMirror-cursor {} -.cm-tab { display: inline-block; } +.cm-tab { display: inline-block; text-decoration: inherit; } .CodeMirror-ruler { border-left: 1px solid #ccc; @@ -213,6 +217,7 @@ div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #f22;} .CodeMirror-lines { cursor: text; + min-height: 1px; /* prevents collapsing before first draw */ } .CodeMirror pre { /* Reset some styles that the rest of the page might have set */ @@ -272,7 +277,7 @@ div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #f22;} div.CodeMirror-cursors { visibility: hidden; position: relative; - z-index: 1; + z-index: 3; } .CodeMirror-focused div.CodeMirror-cursors { visibility: visible; @@ -299,3 +304,6 @@ div.CodeMirror-cursors { visibility: hidden; } } + +/* Help users use markselection to safely style text background */ +span.CodeMirror-selectedtext { background: none; } diff --git a/static/js/lib/codemirror-4.5/codemirror.js b/static/js/lib/codemirror-4.7/codemirror.js similarity index 99% rename from static/js/lib/codemirror-4.5/codemirror.js rename to static/js/lib/codemirror-4.7/codemirror.js index 61e8c41..4f8a23b 100644 --- a/static/js/lib/codemirror-4.5/codemirror.js +++ b/static/js/lib/codemirror-4.7/codemirror.js @@ -61,7 +61,7 @@ function CodeMirror(place, options) { if (!(this instanceof CodeMirror)) return new CodeMirror(place, options); - this.options = options = options || {}; + this.options = options = options ? copyObj(options) : {}; // Determine effective options based on given values and defaults. copyObj(defaults, options, false); setGuttersForLineNumbers(options); @@ -96,21 +96,20 @@ registerEventHandlers(this); ensureGlobalHandlers(); - var cm = this; - runInOp(this, function() { - cm.curOp.forceUpdate = true; - attachDoc(cm, doc); + startOperation(this); + this.curOp.forceUpdate = true; + attachDoc(this, doc); - if ((options.autofocus && !mobile) || activeElt() == display.input) - setTimeout(bind(onFocus, cm), 20); - else - onBlur(cm); + if ((options.autofocus && !mobile) || activeElt() == display.input) + setTimeout(bind(onFocus, this), 20); + else + onBlur(this); - for (var opt in optionHandlers) if (optionHandlers.hasOwnProperty(opt)) - optionHandlers[opt](cm, options[opt], Init); - maybeUpdateLineNumberWidth(cm); - for (var i = 0; i < initHooks.length; ++i) initHooks[i](cm); - }); + for (var opt in optionHandlers) if (optionHandlers.hasOwnProperty(opt)) + optionHandlers[opt](this, options[opt], Init); + maybeUpdateLineNumberWidth(this); + for (var i = 0; i < initHooks.length; ++i) initHooks[i](this); + endOperation(this); } // DISPLAY CONSTRUCTOR @@ -723,9 +722,10 @@ // view, so that we don't interleave reading and writing to the DOM. function getDimensions(cm) { var d = cm.display, left = {}, width = {}; + var gutterLeft = d.gutters.clientLeft; for (var n = d.gutters.firstChild, i = 0; n; n = n.nextSibling, ++i) { - left[cm.options.gutters[i]] = n.offsetLeft; - width[cm.options.gutters[i]] = n.offsetWidth; + left[cm.options.gutters[i]] = n.offsetLeft + n.clientLeft + gutterLeft; + width[cm.options.gutters[i]] = n.clientWidth; } return {fixedPos: compensateForHScroll(d), gutterTotalWidth: d.gutters.offsetWidth, @@ -1646,7 +1646,7 @@ var rect; if (node.nodeType == 3) { // If it is a text node, use a range to retrieve the coordinates. - for (;;) { + for (var i = 0; i < 4; i++) { // Retry a maximum of 4 times when nonsense rectangles are returned while (start && isExtendingChar(prepared.line.text.charAt(mStart + start))) --start; while (mStart + end < mEnd && isExtendingChar(prepared.line.text.charAt(mStart + end))) ++end; if (ie && ie_version < 9 && start == 0 && end == mEnd - mStart) { @@ -1665,6 +1665,7 @@ start = start - 1; collapse = "right"; } + if (ie && ie_version < 11) rect = maybeUpdateRectForZooming(cm.display.measure, rect); } else { // If it is a widget, simply get the box for the whole widget. if (start > 0) collapse = bias = "right"; var rects; @@ -1681,8 +1682,6 @@ rect = nullRect; } - if (ie && ie_version < 11) rect = maybeUpdateRectForZooming(cm.display.measure, rect); - var rtop = rect.top - prepared.rect.top, rbot = rect.bottom - prepared.rect.top; var mid = (rtop + rbot) / 2; var heights = prepared.view.measure.heights; @@ -2090,11 +2089,11 @@ display.wheelStartX = display.wheelStartY = null; // Propagate the scroll position to the actual DOM scroller - if (op.scrollTop != null && display.scroller.scrollTop != op.scrollTop) { + if (op.scrollTop != null && (display.scroller.scrollTop != op.scrollTop || op.forceScroll)) { var top = Math.max(0, Math.min(display.scroller.scrollHeight - display.scroller.clientHeight, op.scrollTop)); display.scroller.scrollTop = display.scrollbarV.scrollTop = doc.scrollTop = top; } - if (op.scrollLeft != null && display.scroller.scrollLeft != op.scrollLeft) { + if (op.scrollLeft != null && (display.scroller.scrollLeft != op.scrollLeft || op.forceScroll)) { var left = Math.max(0, Math.min(display.scroller.scrollWidth - display.scroller.clientWidth, op.scrollLeft)); display.scroller.scrollLeft = display.scrollbarH.scrollLeft = doc.scrollLeft = left; alignHorizontally(cm); @@ -2457,16 +2456,16 @@ cm.options.smartIndent && range.head.ch < 100 && (!i || doc.sel.ranges[i - 1].head.line != range.head.line)) { var mode = cm.getModeAt(range.head); + var end = changeEnd(changeEvent); if (mode.electricChars) { for (var j = 0; j < mode.electricChars.length; j++) if (inserted.indexOf(mode.electricChars.charAt(j)) > -1) { - indentLine(cm, range.head.line, "smart"); + indentLine(cm, end.line, "smart"); break; } } else if (mode.electricInput) { - var end = changeEnd(changeEvent); if (mode.electricInput.test(getLine(doc, end.line).text.slice(0, end.ch))) - indentLine(cm, range.head.line, "smart"); + indentLine(cm, end.line, "smart"); } } } @@ -2528,7 +2527,7 @@ var pos = posFromMouse(cm, e); if (!pos || clickInGutter(cm, e) || eventInWidget(cm.display, e)) return; e_preventDefault(e); - var word = findWordAt(cm, pos); + var word = cm.findWordAt(pos); extendSelection(cm.doc, word.anchor, word.head); })); else @@ -2807,7 +2806,7 @@ start = posFromMouse(cm, e, true, true); ourIndex = -1; } else if (type == "double") { - var word = findWordAt(cm, start); + var word = cm.findWordAt(start); if (cm.display.shift || doc.extend) ourRange = extendRange(doc, ourRange, word.anchor, word.head); else @@ -2861,7 +2860,7 @@ var anchor = oldRange.anchor, head = pos; if (type != "single") { if (type == "double") - var range = findWordAt(cm, pos); + var range = cm.findWordAt(pos); else var range = new Range(Pos(pos.line, 0), clipPos(doc, Pos(pos.line + 1, 0))); if (cmp(range.anchor, anchor) > 0) { @@ -3721,7 +3720,7 @@ // measured, the position of something may 'drift' during drawing). function scrollPosIntoView(cm, pos, end, margin) { if (margin == null) margin = 0; - for (;;) { + for (var limit = 0; limit < 5; limit++) { var changed = false, coords = cursorCoords(cm, pos); var endCoords = !end || end == pos ? coords : cursorCoords(cm, end); var scrollPos = calculateScrollPos(cm, Math.min(coords.left, endCoords.left), @@ -3770,7 +3769,7 @@ var screenleft = cm.curOp && cm.curOp.scrollLeft != null ? cm.curOp.scrollLeft : display.scroller.scrollLeft; var screenw = display.scroller.clientWidth - scrollerCutOff - display.gutters.offsetWidth; var tooWide = x2 - x1 > screenw; - if (tooWide) x2 = y1 + screen; + if (tooWide) x2 = x1 + screenw; if (x1 < 10) result.scrollLeft = 0; else if (x1 < screenleft) @@ -3999,24 +3998,6 @@ return target; } - // Find the word at the given position (as returned by coordsChar). - function findWordAt(cm, pos) { - var doc = cm.doc, line = getLine(doc, pos.line).text; - var start = pos.ch, end = pos.ch; - if (line) { - var helper = cm.getHelper(pos, "wordChars"); - if ((pos.xRel < 0 || end == line.length) && start) --start; else ++end; - var startChar = line.charAt(start); - var check = isWordChar(startChar, helper) - ? function(ch) { return isWordChar(ch, helper); } - : /\s/.test(startChar) ? function(ch) {return /\s/.test(ch);} - : function(ch) {return !/\s/.test(ch) && !isWordChar(ch);}; - while (start > 0 && check(line.charAt(start - 1))) --start; - while (end < line.length && check(line.charAt(end))) ++end; - } - return new Range(Pos(pos.line, start), Pos(pos.line, end)); - } - // EDITOR METHODS // The publicly visible API. Note that methodOp(f) means @@ -4358,6 +4339,24 @@ doc.sel.ranges[i].goalColumn = goals[i]; }), + // Find the word at the given position (as returned by coordsChar). + findWordAt: function(pos) { + var doc = this.doc, line = getLine(doc, pos.line).text; + var start = pos.ch, end = pos.ch; + if (line) { + var helper = this.getHelper(pos, "wordChars"); + if ((pos.xRel < 0 || end == line.length) && start) --start; else ++end; + var startChar = line.charAt(start); + var check = isWordChar(startChar, helper) + ? function(ch) { return isWordChar(ch, helper); } + : /\s/.test(startChar) ? function(ch) {return /\s/.test(ch);} + : function(ch) {return !/\s/.test(ch) && !isWordChar(ch);}; + while (start > 0 && check(line.charAt(start - 1))) --start; + while (end < line.length && check(line.charAt(end))) ++end; + } + return new Range(Pos(pos.line, start), Pos(pos.line, end)); + }, + toggleOverwrite: function(value) { if (value != null && value == this.state.overwrite) return; if (this.state.overwrite = !this.state.overwrite) @@ -4444,6 +4443,7 @@ clearCaches(this); resetInput(this); this.scrollTo(doc.scrollLeft, doc.scrollTop); + this.curOp.forceScroll = true; signalLater(this, "swapDoc", this, old); return old; }), @@ -4570,10 +4570,8 @@ // load a mode. (Preferred mechanism is the require/define calls.) CodeMirror.defineMode = function(name, mode) { if (!CodeMirror.defaults.mode && name != "null") CodeMirror.defaults.mode = name; - if (arguments.length > 2) { - mode.dependencies = []; - for (var i = 2; i < arguments.length; ++i) mode.dependencies.push(arguments[i]); - } + if (arguments.length > 2) + mode.dependencies = Array.prototype.slice.call(arguments, 2); modes[name] = mode; }; @@ -4865,7 +4863,7 @@ // are simply ignored. keyMap.pcDefault = { "Ctrl-A": "selectAll", "Ctrl-D": "deleteLine", "Ctrl-Z": "undo", "Shift-Ctrl-Z": "redo", "Ctrl-Y": "redo", - "Ctrl-Home": "goDocStart", "Ctrl-Up": "goDocStart", "Ctrl-End": "goDocEnd", "Ctrl-Down": "goDocEnd", + "Ctrl-Home": "goDocStart", "Ctrl-End": "goDocEnd", "Ctrl-Up": "goLineUp", "Ctrl-Down": "goLineDown", "Ctrl-Left": "goGroupLeft", "Ctrl-Right": "goGroupRight", "Alt-Left": "goLineStart", "Alt-Right": "goLineEnd", "Ctrl-Backspace": "delGroupBefore", "Ctrl-Delete": "delGroupAfter", "Ctrl-S": "save", "Ctrl-F": "find", "Ctrl-G": "findNext", "Shift-Ctrl-G": "findPrev", "Shift-Ctrl-F": "replace", "Shift-Ctrl-R": "replaceAll", @@ -4880,7 +4878,7 @@ "Ctrl-Alt-Backspace": "delGroupAfter", "Alt-Delete": "delGroupAfter", "Cmd-S": "save", "Cmd-F": "find", "Cmd-G": "findNext", "Shift-Cmd-G": "findPrev", "Cmd-Alt-F": "replace", "Shift-Cmd-Alt-F": "replaceAll", "Cmd-[": "indentLess", "Cmd-]": "indentMore", "Cmd-Backspace": "delWrappedLineLeft", "Cmd-Delete": "delWrappedLineRight", - "Cmd-U": "undoSelection", "Shift-Cmd-U": "redoSelection", + "Cmd-U": "undoSelection", "Shift-Cmd-U": "redoSelection", "Ctrl-Up": "goDocStart", "Ctrl-Down": "goDocEnd", fallthrough: ["basic", "emacsy"] }; // Very basic readline/emacs-style bindings, which are standard on Mac. @@ -4988,6 +4986,7 @@ cm.save = save; cm.getTextArea = function() { return textarea; }; cm.toTextArea = function() { + cm.toTextArea = isNaN; // Prevent this from being ran twice save(); textarea.parentNode.removeChild(cm.getWrapperElement()); textarea.style.display = ""; @@ -7295,7 +7294,7 @@ return function(){return f.apply(null, args);}; } - var nonASCIISingleCaseWordChar = /[\u00df\u3040-\u309f\u30a0-\u30ff\u3400-\u4db5\u4e00-\u9fcc\uac00-\ud7af]/; + var nonASCIISingleCaseWordChar = /[\u00df\u0590-\u05f4\u0600-\u06ff\u3040-\u309f\u30a0-\u30ff\u3400-\u4db5\u4e00-\u9fcc\uac00-\ud7af]/; var isWordCharBasic = CodeMirror.isWordChar = function(ch) { return /\w/.test(ch) || ch > "\x80" && (ch.toUpperCase() != ch.toLowerCase() || nonASCIISingleCaseWordChar.test(ch)); @@ -7462,7 +7461,7 @@ if (badBidiRects != null) return badBidiRects; var txt = removeChildrenAndAdd(measure, document.createTextNode("A\u062eA")); var r0 = range(txt, 0, 1).getBoundingClientRect(); - if (r0.left == r0.right) return false; + if (!r0 || r0.left == r0.right) return false; // Safari returns null in some cases (#2780) var r1 = range(txt, 1, 2).getBoundingClientRect(); return badBidiRects = (r1.right - r0.right < 3); } @@ -7825,7 +7824,7 @@ // THE END - CodeMirror.version = "4.5.0"; + CodeMirror.version = "4.7.0"; return CodeMirror; }); diff --git a/static/js/lib/codemirror-4.5/keymap/emacs.js b/static/js/lib/codemirror-4.7/keymap/emacs.js similarity index 100% rename from static/js/lib/codemirror-4.5/keymap/emacs.js rename to static/js/lib/codemirror-4.7/keymap/emacs.js diff --git a/static/js/lib/codemirror-4.5/keymap/sublime.js b/static/js/lib/codemirror-4.7/keymap/sublime.js similarity index 100% rename from static/js/lib/codemirror-4.5/keymap/sublime.js rename to static/js/lib/codemirror-4.7/keymap/sublime.js diff --git a/static/js/lib/codemirror-4.5/keymap/vim.js b/static/js/lib/codemirror-4.7/keymap/vim.js similarity index 86% rename from static/js/lib/codemirror-4.5/keymap/vim.js rename to static/js/lib/codemirror-4.7/keymap/vim.js index 0bf2c78..a0a01e0 100644 --- a/static/js/lib/codemirror-4.5/keymap/vim.js +++ b/static/js/lib/codemirror-4.7/keymap/vim.js @@ -72,290 +72,205 @@ var defaultKeymap = [ // Key to key mapping. This goes first to make it possible to override // existing mappings. - { keys: [''], type: 'keyToKey', toKeys: ['h'] }, - { keys: [''], type: 'keyToKey', toKeys: ['l'] }, - { keys: [''], type: 'keyToKey', toKeys: ['k'] }, - { keys: [''], type: 'keyToKey', toKeys: ['j'] }, - { keys: [''], type: 'keyToKey', toKeys: ['l'] }, - { keys: [''], type: 'keyToKey', toKeys: ['h'] }, - { keys: [''], type: 'keyToKey', toKeys: ['W'] }, - { keys: [''], type: 'keyToKey', toKeys: ['B'] }, - { keys: [''], type: 'keyToKey', toKeys: ['w'] }, - { keys: [''], type: 'keyToKey', toKeys: ['b'] }, - { keys: [''], type: 'keyToKey', toKeys: ['j'] }, - { keys: [''], type: 'keyToKey', toKeys: ['k'] }, - { keys: [''], type: 'keyToKey', toKeys: [''] }, - { keys: [''], type: 'keyToKey', toKeys: [''] }, - { keys: ['s'], type: 'keyToKey', toKeys: ['c', 'l'], context: 'normal' }, - { keys: ['s'], type: 'keyToKey', toKeys: ['x', 'i'], context: 'visual'}, - { keys: ['S'], type: 'keyToKey', toKeys: ['c', 'c'], context: 'normal' }, - { keys: ['S'], type: 'keyToKey', toKeys: ['d', 'c', 'c'], context: 'visual' }, - { keys: [''], type: 'keyToKey', toKeys: ['0'] }, - { keys: [''], type: 'keyToKey', toKeys: ['$'] }, - { keys: [''], type: 'keyToKey', toKeys: [''] }, - { keys: [''], type: 'keyToKey', toKeys: [''] }, - { keys: [''], type: 'keyToKey', toKeys: ['j', '^'], context: 'normal' }, + { keys: '', type: 'keyToKey', toKeys: 'h' }, + { keys: '', type: 'keyToKey', toKeys: 'l' }, + { keys: '', type: 'keyToKey', toKeys: 'k' }, + { keys: '', type: 'keyToKey', toKeys: 'j' }, + { keys: '', type: 'keyToKey', toKeys: 'l' }, + { keys: '', type: 'keyToKey', toKeys: 'h' }, + { keys: '', type: 'keyToKey', toKeys: 'W' }, + { keys: '', type: 'keyToKey', toKeys: 'B' }, + { keys: '', type: 'keyToKey', toKeys: 'w' }, + { keys: '', type: 'keyToKey', toKeys: 'b' }, + { keys: '', type: 'keyToKey', toKeys: 'j' }, + { keys: '', type: 'keyToKey', toKeys: 'k' }, + { keys: '', type: 'keyToKey', toKeys: '' }, + { keys: '', type: 'keyToKey', toKeys: '' }, + { keys: '', type: 'keyToKey', toKeys: '', context: 'insert' }, + { keys: '', type: 'keyToKey', toKeys: '', context: 'insert' }, + { keys: 's', type: 'keyToKey', toKeys: 'cl', context: 'normal' }, + { keys: 's', type: 'keyToKey', toKeys: 'xi', context: 'visual'}, + { keys: 'S', type: 'keyToKey', toKeys: 'cc', context: 'normal' }, + { keys: 'S', type: 'keyToKey', toKeys: 'dcc', context: 'visual' }, + { keys: '', type: 'keyToKey', toKeys: '0' }, + { keys: '', type: 'keyToKey', toKeys: '$' }, + { keys: '', type: 'keyToKey', toKeys: '' }, + { keys: '', type: 'keyToKey', toKeys: '' }, + { keys: '', type: 'keyToKey', toKeys: 'j^', context: 'normal' }, // Motions - { keys: ['H'], type: 'motion', - motion: 'moveToTopLine', - motionArgs: { linewise: true, toJumplist: true }}, - { keys: ['M'], type: 'motion', - motion: 'moveToMiddleLine', - motionArgs: { linewise: true, toJumplist: true }}, - { keys: ['L'], type: 'motion', - motion: 'moveToBottomLine', - motionArgs: { linewise: true, toJumplist: true }}, - { keys: ['h'], type: 'motion', - motion: 'moveByCharacters', - motionArgs: { forward: false }}, - { keys: ['l'], type: 'motion', - motion: 'moveByCharacters', - motionArgs: { forward: true }}, - { keys: ['j'], type: 'motion', - motion: 'moveByLines', - motionArgs: { forward: true, linewise: true }}, - { keys: ['k'], type: 'motion', - motion: 'moveByLines', - motionArgs: { forward: false, linewise: true }}, - { keys: ['g','j'], type: 'motion', - motion: 'moveByDisplayLines', - motionArgs: { forward: true }}, - { keys: ['g','k'], type: 'motion', - motion: 'moveByDisplayLines', - motionArgs: { forward: false }}, - { keys: ['w'], type: 'motion', - motion: 'moveByWords', - motionArgs: { forward: true, wordEnd: false }}, - { keys: ['W'], type: 'motion', - motion: 'moveByWords', - motionArgs: { forward: true, wordEnd: false, bigWord: true }}, - { keys: ['e'], type: 'motion', - motion: 'moveByWords', - motionArgs: { forward: true, wordEnd: true, inclusive: true }}, - { keys: ['E'], type: 'motion', - motion: 'moveByWords', - motionArgs: { forward: true, wordEnd: true, bigWord: true, - inclusive: true }}, - { keys: ['b'], type: 'motion', - motion: 'moveByWords', - motionArgs: { forward: false, wordEnd: false }}, - { keys: ['B'], type: 'motion', - motion: 'moveByWords', - motionArgs: { forward: false, wordEnd: false, bigWord: true }}, - { keys: ['g', 'e'], type: 'motion', - motion: 'moveByWords', - motionArgs: { forward: false, wordEnd: true, inclusive: true }}, - { keys: ['g', 'E'], type: 'motion', - motion: 'moveByWords', - motionArgs: { forward: false, wordEnd: true, bigWord: true, - inclusive: true }}, - { keys: ['{'], type: 'motion', motion: 'moveByParagraph', - motionArgs: { forward: false, toJumplist: true }}, - { keys: ['}'], type: 'motion', motion: 'moveByParagraph', - motionArgs: { forward: true, toJumplist: true }}, - { keys: [''], type: 'motion', - motion: 'moveByPage', motionArgs: { forward: true }}, - { keys: [''], type: 'motion', - motion: 'moveByPage', motionArgs: { forward: false }}, - { keys: [''], type: 'motion', - motion: 'moveByScroll', - motionArgs: { forward: true, explicitRepeat: true }}, - { keys: [''], type: 'motion', - motion: 'moveByScroll', - motionArgs: { forward: false, explicitRepeat: true }}, - { keys: ['g', 'g'], type: 'motion', - motion: 'moveToLineOrEdgeOfDocument', - motionArgs: { forward: false, explicitRepeat: true, linewise: true, toJumplist: true }}, - { keys: ['G'], type: 'motion', - motion: 'moveToLineOrEdgeOfDocument', - motionArgs: { forward: true, explicitRepeat: true, linewise: true, toJumplist: true }}, - { keys: ['0'], type: 'motion', motion: 'moveToStartOfLine' }, - { keys: ['^'], type: 'motion', - motion: 'moveToFirstNonWhiteSpaceCharacter' }, - { keys: ['+'], type: 'motion', - motion: 'moveByLines', - motionArgs: { forward: true, toFirstChar:true }}, - { keys: ['-'], type: 'motion', - motion: 'moveByLines', - motionArgs: { forward: false, toFirstChar:true }}, - { keys: ['_'], type: 'motion', - motion: 'moveByLines', - motionArgs: { forward: true, toFirstChar:true, repeatOffset:-1 }}, - { keys: ['$'], type: 'motion', - motion: 'moveToEol', - motionArgs: { inclusive: true }}, - { keys: ['%'], type: 'motion', - motion: 'moveToMatchedSymbol', - motionArgs: { inclusive: true, toJumplist: true }}, - { keys: ['f', 'character'], type: 'motion', - motion: 'moveToCharacter', - motionArgs: { forward: true , inclusive: true }}, - { keys: ['F', 'character'], type: 'motion', - motion: 'moveToCharacter', - motionArgs: { forward: false }}, - { keys: ['t', 'character'], type: 'motion', - motion: 'moveTillCharacter', - motionArgs: { forward: true, inclusive: true }}, - { keys: ['T', 'character'], type: 'motion', - motion: 'moveTillCharacter', - motionArgs: { forward: false }}, - { keys: [';'], type: 'motion', motion: 'repeatLastCharacterSearch', - motionArgs: { forward: true }}, - { keys: [','], type: 'motion', motion: 'repeatLastCharacterSearch', - motionArgs: { forward: false }}, - { keys: ['\'', 'character'], type: 'motion', motion: 'goToMark', - motionArgs: {toJumplist: true, linewise: true}}, - { keys: ['`', 'character'], type: 'motion', motion: 'goToMark', - motionArgs: {toJumplist: true}}, - { keys: [']', '`'], type: 'motion', motion: 'jumpToMark', motionArgs: { forward: true } }, - { keys: ['[', '`'], type: 'motion', motion: 'jumpToMark', motionArgs: { forward: false } }, - { keys: [']', '\''], type: 'motion', motion: 'jumpToMark', motionArgs: { forward: true, linewise: true } }, - { keys: ['[', '\''], type: 'motion', motion: 'jumpToMark', motionArgs: { forward: false, linewise: true } }, + { keys: 'H', type: 'motion', motion: 'moveToTopLine', motionArgs: { linewise: true, toJumplist: true }}, + { keys: 'M', type: 'motion', motion: 'moveToMiddleLine', motionArgs: { linewise: true, toJumplist: true }}, + { keys: 'L', type: 'motion', motion: 'moveToBottomLine', motionArgs: { linewise: true, toJumplist: true }}, + { keys: 'h', type: 'motion', motion: 'moveByCharacters', motionArgs: { forward: false }}, + { keys: 'l', type: 'motion', motion: 'moveByCharacters', motionArgs: { forward: true }}, + { keys: 'j', type: 'motion', motion: 'moveByLines', motionArgs: { forward: true, linewise: true }}, + { keys: 'k', type: 'motion', motion: 'moveByLines', motionArgs: { forward: false, linewise: true }}, + { keys: 'gj', type: 'motion', motion: 'moveByDisplayLines', motionArgs: { forward: true }}, + { keys: 'gk', type: 'motion', motion: 'moveByDisplayLines', motionArgs: { forward: false }}, + { keys: 'w', type: 'motion', motion: 'moveByWords', motionArgs: { forward: true, wordEnd: false }}, + { keys: 'W', type: 'motion', motion: 'moveByWords', motionArgs: { forward: true, wordEnd: false, bigWord: true }}, + { keys: 'e', type: 'motion', motion: 'moveByWords', motionArgs: { forward: true, wordEnd: true, inclusive: true }}, + { keys: 'E', type: 'motion', motion: 'moveByWords', motionArgs: { forward: true, wordEnd: true, bigWord: true, inclusive: true }}, + { keys: 'b', type: 'motion', motion: 'moveByWords', motionArgs: { forward: false, wordEnd: false }}, + { keys: 'B', type: 'motion', motion: 'moveByWords', motionArgs: { forward: false, wordEnd: false, bigWord: true }}, + { keys: 'ge', type: 'motion', motion: 'moveByWords', motionArgs: { forward: false, wordEnd: true, inclusive: true }}, + { keys: 'gE', type: 'motion', motion: 'moveByWords', motionArgs: { forward: false, wordEnd: true, bigWord: true, inclusive: true }}, + { keys: '{', type: 'motion', motion: 'moveByParagraph', motionArgs: { forward: false, toJumplist: true }}, + { keys: '}', type: 'motion', motion: 'moveByParagraph', motionArgs: { forward: true, toJumplist: true }}, + { keys: '', type: 'motion', motion: 'moveByPage', motionArgs: { forward: true }}, + { keys: '', type: 'motion', motion: 'moveByPage', motionArgs: { forward: false }}, + { keys: '', type: 'motion', motion: 'moveByScroll', motionArgs: { forward: true, explicitRepeat: true }}, + { keys: '', type: 'motion', motion: 'moveByScroll', motionArgs: { forward: false, explicitRepeat: true }}, + { keys: 'gg', type: 'motion', motion: 'moveToLineOrEdgeOfDocument', motionArgs: { forward: false, explicitRepeat: true, linewise: true, toJumplist: true }}, + { keys: 'G', type: 'motion', motion: 'moveToLineOrEdgeOfDocument', motionArgs: { forward: true, explicitRepeat: true, linewise: true, toJumplist: true }}, + { keys: '0', type: 'motion', motion: 'moveToStartOfLine' }, + { keys: '^', type: 'motion', motion: 'moveToFirstNonWhiteSpaceCharacter' }, + { keys: '+', type: 'motion', motion: 'moveByLines', motionArgs: { forward: true, toFirstChar:true }}, + { keys: '-', type: 'motion', motion: 'moveByLines', motionArgs: { forward: false, toFirstChar:true }}, + { keys: '_', type: 'motion', motion: 'moveByLines', motionArgs: { forward: true, toFirstChar:true, repeatOffset:-1 }}, + { keys: '$', type: 'motion', motion: 'moveToEol', motionArgs: { inclusive: true }}, + { keys: '%', type: 'motion', motion: 'moveToMatchedSymbol', motionArgs: { inclusive: true, toJumplist: true }}, + { keys: 'f', type: 'motion', motion: 'moveToCharacter', motionArgs: { forward: true , inclusive: true }}, + { keys: 'F', type: 'motion', motion: 'moveToCharacter', motionArgs: { forward: false }}, + { keys: 't', type: 'motion', motion: 'moveTillCharacter', motionArgs: { forward: true, inclusive: true }}, + { keys: 'T', type: 'motion', motion: 'moveTillCharacter', motionArgs: { forward: false }}, + { keys: ';', type: 'motion', motion: 'repeatLastCharacterSearch', motionArgs: { forward: true }}, + { keys: ',', type: 'motion', motion: 'repeatLastCharacterSearch', motionArgs: { forward: false }}, + { keys: '\'', type: 'motion', motion: 'goToMark', motionArgs: {toJumplist: true, linewise: true}}, + { keys: '`', type: 'motion', motion: 'goToMark', motionArgs: {toJumplist: true}}, + { keys: ']`', type: 'motion', motion: 'jumpToMark', motionArgs: { forward: true } }, + { keys: '[`', type: 'motion', motion: 'jumpToMark', motionArgs: { forward: false } }, + { keys: ']\'', type: 'motion', motion: 'jumpToMark', motionArgs: { forward: true, linewise: true } }, + { keys: '[\'', type: 'motion', motion: 'jumpToMark', motionArgs: { forward: false, linewise: true } }, // the next two aren't motions but must come before more general motion declarations - { keys: [']', 'p'], type: 'action', action: 'paste', isEdit: true, - actionArgs: { after: true, isEdit: true, matchIndent: true}}, - { keys: ['[', 'p'], type: 'action', action: 'paste', isEdit: true, - actionArgs: { after: false, isEdit: true, matchIndent: true}}, - { keys: [']', 'character'], type: 'motion', - motion: 'moveToSymbol', - motionArgs: { forward: true, toJumplist: true}}, - { keys: ['[', 'character'], type: 'motion', - motion: 'moveToSymbol', - motionArgs: { forward: false, toJumplist: true}}, - { keys: ['|'], type: 'motion', - motion: 'moveToColumn', - motionArgs: { }}, - { keys: ['o'], type: 'motion', motion: 'moveToOtherHighlightedEnd', motionArgs: { }, context:'visual'}, - { keys: ['O'], type: 'motion', motion: 'moveToOtherHighlightedEnd', motionArgs: {sameLine: true}, context:'visual'}, + { keys: ']p', type: 'action', action: 'paste', isEdit: true, actionArgs: { after: true, isEdit: true, matchIndent: true}}, + { keys: '[p', type: 'action', action: 'paste', isEdit: true, actionArgs: { after: false, isEdit: true, matchIndent: true}}, + { keys: ']', type: 'motion', motion: 'moveToSymbol', motionArgs: { forward: true, toJumplist: true}}, + { keys: '[', type: 'motion', motion: 'moveToSymbol', motionArgs: { forward: false, toJumplist: true}}, + { keys: '|', type: 'motion', motion: 'moveToColumn'}, + { keys: 'o', type: 'motion', motion: 'moveToOtherHighlightedEnd', context:'visual'}, + { keys: 'O', type: 'motion', motion: 'moveToOtherHighlightedEnd', motionArgs: {sameLine: true}, context:'visual'}, // Operators - { keys: ['d'], type: 'operator', operator: 'delete' }, - { keys: ['y'], type: 'operator', operator: 'yank' }, - { keys: ['c'], type: 'operator', operator: 'change' }, - { keys: ['>'], type: 'operator', operator: 'indent', - operatorArgs: { indentRight: true }}, - { keys: ['<'], type: 'operator', operator: 'indent', - operatorArgs: { indentRight: false }}, - { keys: ['g', '~'], type: 'operator', operator: 'swapcase' }, - { keys: ['n'], type: 'motion', motion: 'findNext', - motionArgs: { forward: true, toJumplist: true }}, - { keys: ['N'], type: 'motion', motion: 'findNext', - motionArgs: { forward: false, toJumplist: true }}, + { keys: 'd', type: 'operator', operator: 'delete' }, + { keys: 'y', type: 'operator', operator: 'yank' }, + { keys: 'c', type: 'operator', operator: 'change' }, + { keys: '>', type: 'operator', operator: 'indent', operatorArgs: { indentRight: true }}, + { keys: '<', type: 'operator', operator: 'indent', operatorArgs: { indentRight: false }}, + { keys: 'g~', type: 'operator', operator: 'swapcase' }, + { keys: 'n', type: 'motion', motion: 'findNext', motionArgs: { forward: true, toJumplist: true }}, + { keys: 'N', type: 'motion', motion: 'findNext', motionArgs: { forward: false, toJumplist: true }}, // Operator-Motion dual commands - { keys: ['x'], type: 'operatorMotion', operator: 'delete', - motion: 'moveByCharacters', motionArgs: { forward: true }, - operatorMotionArgs: { visualLine: false }}, - { keys: ['X'], type: 'operatorMotion', operator: 'delete', - motion: 'moveByCharacters', motionArgs: { forward: false }, - operatorMotionArgs: { visualLine: true }}, - { keys: ['D'], type: 'operatorMotion', operator: 'delete', - motion: 'moveToEol', motionArgs: { inclusive: true }, - operatorMotionArgs: { visualLine: true }}, - { keys: ['Y'], type: 'operatorMotion', operator: 'yank', - motion: 'moveToEol', motionArgs: { inclusive: true }, - operatorMotionArgs: { visualLine: true }}, - { keys: ['C'], type: 'operatorMotion', - operator: 'change', - motion: 'moveToEol', motionArgs: { inclusive: true }, - operatorMotionArgs: { visualLine: true }}, - { keys: ['~'], type: 'operatorMotion', - operator: 'swapcase', operatorArgs: { shouldMoveCursor: true }, - motion: 'moveByCharacters', motionArgs: { forward: true }}, + { keys: 'x', type: 'operatorMotion', operator: 'delete', motion: 'moveByCharacters', motionArgs: { forward: true }, operatorMotionArgs: { visualLine: false }}, + { keys: 'X', type: 'operatorMotion', operator: 'delete', motion: 'moveByCharacters', motionArgs: { forward: false }, operatorMotionArgs: { visualLine: true }}, + { keys: 'D', type: 'operatorMotion', operator: 'delete', motion: 'moveToEol', motionArgs: { inclusive: true }, operatorMotionArgs: { visualLine: true }}, + { keys: 'Y', type: 'operatorMotion', operator: 'yank', motion: 'moveToEol', motionArgs: { inclusive: true }, operatorMotionArgs: { visualLine: true }}, + { keys: 'C', type: 'operatorMotion', operator: 'change', motion: 'moveToEol', motionArgs: { inclusive: true }, operatorMotionArgs: { visualLine: true }}, + { keys: '~', type: 'operatorMotion', operator: 'swapcase', operatorArgs: { shouldMoveCursor: true }, motion: 'moveByCharacters', motionArgs: { forward: true }}, + { keys: '', type: 'operatorMotion', operator: 'delete', motion: 'moveByWords', motionArgs: { forward: false, wordEnd: false }, context: 'insert' }, // Actions - { keys: [''], type: 'action', action: 'jumpListWalk', - actionArgs: { forward: true }}, - { keys: [''], type: 'action', action: 'jumpListWalk', - actionArgs: { forward: false }}, - { keys: [''], type: 'action', - action: 'scroll', - actionArgs: { forward: true, linewise: true }}, - { keys: [''], type: 'action', - action: 'scroll', - actionArgs: { forward: false, linewise: true }}, - { keys: ['a'], type: 'action', action: 'enterInsertMode', isEdit: true, - actionArgs: { insertAt: 'charAfter' }}, - { keys: ['A'], type: 'action', action: 'enterInsertMode', isEdit: true, - actionArgs: { insertAt: 'eol' }}, - { keys: ['A'], type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'endOfSelectedArea' }, context: 'visual' }, - { keys: ['i'], type: 'action', action: 'enterInsertMode', isEdit: true, - actionArgs: { insertAt: 'inplace' }}, - { keys: ['I'], type: 'action', action: 'enterInsertMode', isEdit: true, - actionArgs: { insertAt: 'firstNonBlank' }}, - { keys: ['o'], type: 'action', action: 'newLineAndEnterInsertMode', - isEdit: true, interlaceInsertRepeat: true, - actionArgs: { after: true }}, - { keys: ['O'], type: 'action', action: 'newLineAndEnterInsertMode', - isEdit: true, interlaceInsertRepeat: true, - actionArgs: { after: false }}, - { keys: ['v'], type: 'action', action: 'toggleVisualMode' }, - { keys: ['V'], type: 'action', action: 'toggleVisualMode', - actionArgs: { linewise: true }}, - { keys: [''], type: 'action', action: 'toggleVisualMode', - actionArgs: { blockwise: true }}, - { keys: ['g', 'v'], type: 'action', action: 'reselectLastSelection' }, - { keys: ['J'], type: 'action', action: 'joinLines', isEdit: true }, - { keys: ['p'], type: 'action', action: 'paste', isEdit: true, - actionArgs: { after: true, isEdit: true }}, - { keys: ['P'], type: 'action', action: 'paste', isEdit: true, - actionArgs: { after: false, isEdit: true }}, - { keys: ['r', 'character'], type: 'action', action: 'replace', isEdit: true }, - { keys: ['@', 'character'], type: 'action', action: 'replayMacro' }, - { keys: ['q', 'character'], type: 'action', action: 'enterMacroRecordMode' }, + { keys: '', type: 'action', action: 'jumpListWalk', actionArgs: { forward: true }}, + { keys: '', type: 'action', action: 'jumpListWalk', actionArgs: { forward: false }}, + { keys: '', type: 'action', action: 'scroll', actionArgs: { forward: true, linewise: true }}, + { keys: '', type: 'action', action: 'scroll', actionArgs: { forward: false, linewise: true }}, + { keys: 'a', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'charAfter' }, context: 'normal' }, + { keys: 'A', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'eol' }, context: 'normal' }, + { keys: 'A', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'endOfSelectedArea' }, context: 'visual' }, + { keys: 'i', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'inplace' }, context: 'normal' }, + { keys: 'I', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'firstNonBlank' }}, + { keys: 'o', type: 'action', action: 'newLineAndEnterInsertMode', isEdit: true, interlaceInsertRepeat: true, actionArgs: { after: true }, context: 'normal' }, + { keys: 'O', type: 'action', action: 'newLineAndEnterInsertMode', isEdit: true, interlaceInsertRepeat: true, actionArgs: { after: false }, context: 'normal' }, + { keys: 'v', type: 'action', action: 'toggleVisualMode' }, + { keys: 'V', type: 'action', action: 'toggleVisualMode', actionArgs: { linewise: true }}, + { keys: '', type: 'action', action: 'toggleVisualMode', actionArgs: { blockwise: true }}, + { keys: 'gv', type: 'action', action: 'reselectLastSelection' }, + { keys: 'J', type: 'action', action: 'joinLines', isEdit: true }, + { keys: 'p', type: 'action', action: 'paste', isEdit: true, actionArgs: { after: true, isEdit: true }}, + { keys: 'P', type: 'action', action: 'paste', isEdit: true, actionArgs: { after: false, isEdit: true }}, + { keys: 'r', type: 'action', action: 'replace', isEdit: true }, + { keys: '@', type: 'action', action: 'replayMacro' }, + { keys: 'q', type: 'action', action: 'enterMacroRecordMode' }, // Handle Replace-mode as a special case of insert mode. - { keys: ['R'], type: 'action', action: 'enterInsertMode', isEdit: true, - actionArgs: { replace: true }}, - { keys: ['u'], type: 'action', action: 'undo' }, - { keys: ['u'], type: 'action', action: 'changeCase', actionArgs: {toLower: true}, context: 'visual', isEdit: true }, - { keys: ['U'],type: 'action', action: 'changeCase', actionArgs: {toLower: false}, context: 'visual', isEdit: true }, - { keys: [''], type: 'action', action: 'redo' }, - { keys: ['m', 'character'], type: 'action', action: 'setMark' }, - { keys: ['"', 'character'], type: 'action', action: 'setRegister' }, - { keys: ['z', 'z'], type: 'action', action: 'scrollToCursor', - actionArgs: { position: 'center' }}, - { keys: ['z', '.'], type: 'action', action: 'scrollToCursor', - actionArgs: { position: 'center' }, - motion: 'moveToFirstNonWhiteSpaceCharacter' }, - { keys: ['z', 't'], type: 'action', action: 'scrollToCursor', - actionArgs: { position: 'top' }}, - { keys: ['z', ''], type: 'action', action: 'scrollToCursor', - actionArgs: { position: 'top' }, - motion: 'moveToFirstNonWhiteSpaceCharacter' }, - { keys: ['z', '-'], type: 'action', action: 'scrollToCursor', - actionArgs: { position: 'bottom' }}, - { keys: ['z', 'b'], type: 'action', action: 'scrollToCursor', - actionArgs: { position: 'bottom' }, - motion: 'moveToFirstNonWhiteSpaceCharacter' }, - { keys: ['.'], type: 'action', action: 'repeatLastEdit' }, - { keys: [''], type: 'action', action: 'incrementNumberToken', - isEdit: true, - actionArgs: {increase: true, backtrack: false}}, - { keys: [''], type: 'action', action: 'incrementNumberToken', - isEdit: true, - actionArgs: {increase: false, backtrack: false}}, + { keys: 'R', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { replace: true }}, + { keys: 'u', type: 'action', action: 'undo', context: 'normal' }, + { keys: 'u', type: 'action', action: 'changeCase', actionArgs: {toLower: true}, context: 'visual', isEdit: true }, + { keys: 'U',type: 'action', action: 'changeCase', actionArgs: {toLower: false}, context: 'visual', isEdit: true }, + { keys: '', type: 'action', action: 'redo' }, + { keys: 'm', type: 'action', action: 'setMark' }, + { keys: '"', type: 'action', action: 'setRegister' }, + { keys: 'zz', type: 'action', action: 'scrollToCursor', actionArgs: { position: 'center' }}, + { keys: 'z.', type: 'action', action: 'scrollToCursor', actionArgs: { position: 'center' }, motion: 'moveToFirstNonWhiteSpaceCharacter' }, + { keys: 'zt', type: 'action', action: 'scrollToCursor', actionArgs: { position: 'top' }}, + { keys: 'z', type: 'action', action: 'scrollToCursor', actionArgs: { position: 'top' }, motion: 'moveToFirstNonWhiteSpaceCharacter' }, + { keys: 'z-', type: 'action', action: 'scrollToCursor', actionArgs: { position: 'bottom' }}, + { keys: 'zb', type: 'action', action: 'scrollToCursor', actionArgs: { position: 'bottom' }, motion: 'moveToFirstNonWhiteSpaceCharacter' }, + { keys: '.', type: 'action', action: 'repeatLastEdit' }, + { keys: '', type: 'action', action: 'incrementNumberToken', isEdit: true, actionArgs: {increase: true, backtrack: false}}, + { keys: '', type: 'action', action: 'incrementNumberToken', isEdit: true, actionArgs: {increase: false, backtrack: false}}, // Text object motions - { keys: ['a', 'character'], type: 'motion', - motion: 'textObjectManipulation' }, - { keys: ['i', 'character'], type: 'motion', - motion: 'textObjectManipulation', - motionArgs: { textObjectInner: true }}, + { keys: 'a', type: 'motion', motion: 'textObjectManipulation' }, + { keys: 'i', type: 'motion', motion: 'textObjectManipulation', motionArgs: { textObjectInner: true }}, // Search - { keys: ['/'], type: 'search', - searchArgs: { forward: true, querySrc: 'prompt', toJumplist: true }}, - { keys: ['?'], type: 'search', - searchArgs: { forward: false, querySrc: 'prompt', toJumplist: true }}, - { keys: ['*'], type: 'search', - searchArgs: { forward: true, querySrc: 'wordUnderCursor', wholeWordOnly: true, toJumplist: true }}, - { keys: ['#'], type: 'search', - searchArgs: { forward: false, querySrc: 'wordUnderCursor', wholeWordOnly: true, toJumplist: true }}, - { keys: ['g', '*'], type: 'search', searchArgs: { forward: true, querySrc: 'wordUnderCursor', toJumplist: true }}, - { keys: ['g', '#'], type: 'search', searchArgs: { forward: false, querySrc: 'wordUnderCursor', toJumplist: true }}, + { keys: '/', type: 'search', searchArgs: { forward: true, querySrc: 'prompt', toJumplist: true }}, + { keys: '?', type: 'search', searchArgs: { forward: false, querySrc: 'prompt', toJumplist: true }}, + { keys: '*', type: 'search', searchArgs: { forward: true, querySrc: 'wordUnderCursor', wholeWordOnly: true, toJumplist: true }}, + { keys: '#', type: 'search', searchArgs: { forward: false, querySrc: 'wordUnderCursor', wholeWordOnly: true, toJumplist: true }}, + { keys: 'g*', type: 'search', searchArgs: { forward: true, querySrc: 'wordUnderCursor', toJumplist: true }}, + { keys: 'g#', type: 'search', searchArgs: { forward: false, querySrc: 'wordUnderCursor', toJumplist: true }}, // Ex command - { keys: [':'], type: 'ex' } + { keys: ':', type: 'ex' } ]; var Pos = CodeMirror.Pos; + var modifierCodes = [16, 17, 18, 91]; + var specialKey = {Enter:'CR',Backspace:'BS',Delete:'Del'}; + var mac = /Mac/.test(navigator.platform); var Vim = function() { CodeMirror.defineOption('vimMode', false, function(cm, val) { + function lookupKey(e) { + var keyCode = e.keyCode; + if (modifierCodes.indexOf(keyCode) != -1) { return; } + var hasModifier = e.ctrlKey || e.metaKey; + var key = CodeMirror.keyNames[keyCode]; + key = specialKey[key] || key; + var name = ''; + if (e.ctrlKey) { name += 'C-'; } + if (e.altKey) { name += 'A-'; } + if (mac && e.metaKey || (!hasModifier && e.shiftKey) && key.length < 2) { + // Shift key bindings can only specified for special characters. + return; + } else if (e.shiftKey && !/^[A-Za-z]$/.test(key)) { + name += 'S-'; + } + if (key.length == 1) { key = key.toLowerCase(); } + name += key; + if (name.length > 1) { name = '<' + name + '>'; } + return name; + } + // Keys with modifiers are handled using keydown due to limitations of + // keypress event. + function handleKeyDown(cm, e) { + var name = lookupKey(e); + if (!name) { return; } + + CodeMirror.signal(cm, 'vim-keypress', name); + if (CodeMirror.Vim.handleKey(cm, name, 'user')) { + CodeMirror.e_stop(e); + } + } + // Keys without modifiers are handled using keypress to work best with + // non-standard keyboard layouts. + function handleKeyPress(cm, e) { + var code = e.charCode || e.keyCode; + if (e.ctrlKey || e.metaKey || e.altKey || + e.shiftKey && code < 32) { return; } + var name = String.fromCharCode(code); + + CodeMirror.signal(cm, 'vim-keypress', name); + if (CodeMirror.Vim.handleKey(cm, name, 'user')) { + CodeMirror.e_stop(e); + } + } if (val) { cm.setOption('keyMap', 'vim'); cm.setOption('disableInput', true); @@ -364,12 +279,16 @@ cm.on('cursorActivity', onCursorActivity); maybeInitVimState(cm); CodeMirror.on(cm.getInputField(), 'paste', getOnPasteFn(cm)); + cm.on('keypress', handleKeyPress); + cm.on('keydown', handleKeyDown); } else if (cm.state.vim) { cm.setOption('keyMap', 'default'); cm.setOption('disableInput', false); cm.off('cursorActivity', onCursorActivity); CodeMirror.off(cm.getInputField(), 'paste', getOnPasteFn(cm)); cm.state.vim = null; + cm.off('keypress', handleKeyPress); + cm.off('keydown', handleKeyDown); } }); function getOnPasteFn(cm) { @@ -397,9 +316,6 @@ var upperCaseAlphabet = makeKeyRange(65, 26); var lowerCaseAlphabet = makeKeyRange(97, 26); var numbers = makeKeyRange(48, 10); - var specialSymbols = '~`!@#$%^&*()_-+=[{}]\\|/?.,<>:;"\''.split(''); - var specialKeys = ['Left', 'Right', 'Up', 'Down', 'Space', 'Backspace', - 'Esc', 'Home', 'End', 'PageUp', 'PageDown', 'Enter']; var validMarks = [].concat(upperCaseAlphabet, lowerCaseAlphabet, numbers, ['<', '>']); var validRegisters = [].concat(upperCaseAlphabet, lowerCaseAlphabet, numbers, ['-', '"', '.', ':', '/']); @@ -648,6 +564,7 @@ } } + var lastInsertModeKeyTimer; var vimApi= { buildKeyMap: function() { // TODO: Convert keymap into dictionary format for fast lookup. @@ -685,58 +602,111 @@ }, // This is the outermost function called by CodeMirror, after keys have // been mapped to their Vim equivalents. - handleKey: function(cm, key) { - var command; + handleKey: function(cm, key, origin) { var vim = maybeInitVimState(cm); - var macroModeState = vimGlobalState.macroModeState; - if (macroModeState.isRecording) { - if (key == 'q') { - macroModeState.exitMacroRecordMode(); + function handleMacroRecording() { + var macroModeState = vimGlobalState.macroModeState; + if (macroModeState.isRecording) { + if (key == 'q') { + macroModeState.exitMacroRecordMode(); + clearInputState(cm); + return true; + } + if (origin != 'mapping') { + logKey(macroModeState, key); + } + } + } + function handleEsc() { + if (key == '') { + // Clear input state and get back to normal mode. clearInputState(cm); - return; + if (vim.visualMode) { + exitVisualMode(cm); + } else if (vim.insertMode) { + exitInsertMode(cm); + } + return true; } } - if (key == '') { - // Clear input state and get back to normal mode. - clearInputState(cm); - if (vim.visualMode) { - exitVisualMode(cm); - } - return; - } - // Enter visual mode when the mouse selects text. - if (!vim.visualMode && - !cursorEqual(cm.getCursor('head'), cm.getCursor('anchor'))) { - vim.visualMode = true; - vim.visualLine = false; - CodeMirror.signal(cm, "vim-mode-change", {mode: "visual"}); - cm.on('mousedown', exitVisualMode); - } - if (key != '0' || (key == '0' && vim.inputState.getRepeat() === 0)) { - // Have to special case 0 since it's both a motion and a number. - command = commandDispatcher.matchCommand(key, defaultKeymap, vim); - } - if (!command) { - if (isNumber(key)) { - // Increment count unless count is 0 and key is 0. - vim.inputState.pushRepeatDigit(key); - } - if (macroModeState.isRecording) { - logKey(macroModeState, key); - } - return; - } - if (command.type == 'keyToKey') { + function doKeyToKey(keys) { // TODO: prevent infinite recursion. - for (var i = 0; i < command.toKeys.length; i++) { - this.handleKey(cm, command.toKeys[i]); + var match; + while (keys) { + // Pull off one command key, which is either a single character + // or a special sequence wrapped in '<' and '>', e.g. ''. + match = (/<\w+-.+?>|<\w+>|./).exec(keys); + key = match[0]; + keys = keys.substring(match.index + key.length); + CodeMirror.Vim.handleKey(cm, key, 'mapping'); } - } else { - if (macroModeState.isRecording) { - logKey(macroModeState, key); - } - commandDispatcher.processCommand(cm, vim, command); } + + function handleKeyInsertMode() { + if (handleEsc()) { return true; } + var keys = vim.inputState.keyBuffer = vim.inputState.keyBuffer + key; + var keysAreChars = key.length == 1; + var match = commandDispatcher.matchCommand(keys, defaultKeymap, vim.inputState, 'insert'); + // Need to check all key substrings in insert mode. + while (keys.length > 1 && match.type != 'full') { + var keys = vim.inputState.keyBuffer = keys.slice(1); + var thisMatch = commandDispatcher.matchCommand(keys, defaultKeymap, vim.inputState, 'insert'); + if (thisMatch.type != 'none') { match = thisMatch; } + } + if (match.type == 'none') { clearInputState(cm); return false; } + else if (match.type == 'partial') { + if (lastInsertModeKeyTimer) { window.clearTimeout(lastInsertModeKeyTimer); } + lastInsertModeKeyTimer = window.setTimeout( + function() { if (vim.insertMode && vim.inputState.keyBuffer) { clearInputState(cm); } }, + getOption('insertModeEscKeysTimeout')); + return !keysAreChars; + } + + if (lastInsertModeKeyTimer) { window.clearTimeout(lastInsertModeKeyTimer); } + if (keysAreChars) { + var here = cm.getCursor(); + cm.replaceRange('', offsetCursor(here, 0, -(keys.length - 1)), here, '+input'); + } + clearInputState(cm); + var command = match.command; + if (command.type == 'keyToKey') { + doKeyToKey(command.toKeys); + } else { + commandDispatcher.processCommand(cm, vim, command); + } + return !keysAreChars; + } + + function handleKeyNonInsertMode() { + if (handleMacroRecording() || handleEsc()) { return true; }; + + var keys = vim.inputState.keyBuffer = vim.inputState.keyBuffer + key; + if (/^[1-9]\d*$/.test(keys)) { return true; } + + var keysMatcher = /^(\d*)(.*)$/.exec(keys); + if (!keysMatcher) { clearInputState(cm); return false; } + var context = vim.visualMode ? 'visual' : + 'normal'; + var match = commandDispatcher.matchCommand(keysMatcher[2] || keysMatcher[1], defaultKeymap, vim.inputState, context); + if (match.type == 'none') { clearInputState(cm); return false; } + else if (match.type == 'partial') { return true; } + + vim.inputState.keyBuffer = ''; + var command = match.command; + var keysMatcher = /^(\d*)(.*)$/.exec(keys); + if (keysMatcher[1] && keysMatcher[1] != '0') { + vim.inputState.pushRepeatDigit(keysMatcher[1]); + } + if (command.type == 'keyToKey') { + doKeyToKey(command.toKeys); + } else { + commandDispatcher.processCommand(cm, vim, command); + } + return true; + } + + if (vim.insertMode) { return handleKeyInsertMode(); } + else { return handleKeyNonInsertMode(); } }, handleEx: function(cm, input) { exCommandDispatcher.processCommand(cm, input); @@ -953,83 +923,25 @@ } }; var commandDispatcher = { - matchCommand: function(key, keyMap, vim) { - var inputState = vim.inputState; - var keys = inputState.keyBuffer.concat(key); - var matchedCommands = []; - var selectedCharacter; - for (var i = 0; i < keyMap.length; i++) { - var command = keyMap[i]; - if (matchKeysPartial(keys, command.keys)) { - if (inputState.operator && command.type == 'action') { - // Ignore matched action commands after an operator. Operators - // only operate on motions. This check is really for text - // objects since aW, a[ etcs conflicts with a. - continue; - } - // Match commands that take as an argument. - if (command.keys[keys.length - 1] == 'character') { - selectedCharacter = keys[keys.length - 1]; - if (selectedCharacter.length>1){ - switch(selectedCharacter){ - case '': - selectedCharacter='\n'; - break; - case '': - selectedCharacter=' '; - break; - default: - continue; - } - } - } - // Add the command to the list of matched commands. Choose the best - // command later. - matchedCommands.push(command); - } + matchCommand: function(keys, keyMap, inputState, context) { + var matches = commandMatches(keys, keyMap, context, inputState); + if (!matches.full && !matches.partial) { + return {type: 'none'}; + } else if (!matches.full && matches.partial) { + return {type: 'partial'}; } - // Returns the command if it is a full match, or null if not. - function getFullyMatchedCommandOrNull(command) { - if (keys.length < command.keys.length) { - // Matches part of a multi-key command. Buffer and wait for next - // stroke. - inputState.keyBuffer.push(key); - return null; - } else { - if (command.keys[keys.length - 1] == 'character') { - inputState.selectedCharacter = selectedCharacter; - } - // Clear the buffer since a full match was found. - inputState.keyBuffer = []; - return command; + var bestMatch; + for (var i = 0; i < matches.full.length; i++) { + var match = matches.full[i]; + if (!bestMatch) { + bestMatch = match; } } - - if (!matchedCommands.length) { - // Clear the buffer since there were no matches. - inputState.keyBuffer = []; - return null; - } else if (matchedCommands.length == 1) { - return getFullyMatchedCommandOrNull(matchedCommands[0]); - } else { - // Find the best match in the list of matchedCommands. - var context = vim.visualMode ? 'visual' : 'normal'; - var bestMatch; // Default to first in the list. - for (var i = 0; i < matchedCommands.length; i++) { - var current = matchedCommands[i]; - if (current.context == context) { - bestMatch = current; - break; - } else if (!bestMatch && !current.context) { - // Only set an imperfect match to best match if no best match is - // set and the imperfect match is not restricted to another - // context. - bestMatch = current; - } - } - return getFullyMatchedCommandOrNull(bestMatch); + if (bestMatch.keys.slice(-11) == '') { + inputState.selectedCharacter = lastChar(keys); } + return {type: 'full', command: bestMatch}; }, processCommand: function(cm, vim, command) { vim.inputState.repeatOverride = command.repeatOverride; @@ -1584,8 +1496,8 @@ var equal = cursorEqual(cursor, best); var between = (motionArgs.forward) ? - cusrorIsBetween(cursor, mark, best) : - cusrorIsBetween(best, mark, cursor); + cursorIsBetween(cursor, mark, best) : + cursorIsBetween(best, mark, cursor); if (equal || between) { best = mark; @@ -1837,7 +1749,11 @@ return null; } - return [tmp.start, tmp.end]; + if (!cm.state.vim.visualMode) { + return [tmp.start, tmp.end]; + } else { + return expandSelection(cm, tmp.start, tmp.end); + } }, repeatLastCharacterSearch: function(cm, motionArgs) { @@ -1944,10 +1860,15 @@ // including the trailing \n, include the \n before the starting line if (operatorArgs.linewise && curEnd.line == cm.lastLine() && curStart.line == curEnd.line) { - var tmp = copyCursor(curEnd); - curStart.line--; - curStart.ch = lineLength(cm, curStart.line); - curEnd = tmp; + if (curEnd.line == 0) { + curStart.ch = 0; + } + else { + var tmp = copyCursor(curEnd); + curStart.line--; + curStart.ch = lineLength(cm, curStart.line); + curEnd = tmp; + } cm.replaceRange('', curStart, curEnd); } else { cm.replaceSelections(replacement); @@ -2604,7 +2525,8 @@ if (vim.visualMode) { exitVisualMode(cm); } - } + }, + exitInsertMode: exitInsertMode }; /* @@ -2634,14 +2556,54 @@ function offsetCursor(cur, offsetLine, offsetCh) { return Pos(cur.line + offsetLine, cur.ch + offsetCh); } - function matchKeysPartial(pressed, mapped) { - for (var i = 0; i < pressed.length; i++) { - // 'character' means any character. For mark, register commads, etc. - if (pressed[i] != mapped[i] && mapped[i] != 'character') { - return false; + function commandMatches(keys, keyMap, context, inputState) { + // Partial matches are not applied. They inform the key handler + // that the current key sequence is a subsequence of a valid key + // sequence, so that the key buffer is not cleared. + var match, partial = [], full = []; + for (var i = 0; i < keyMap.length; i++) { + var command = keyMap[i]; + if (context == 'insert' && command.context != 'insert' || + command.context && command.context != context || + inputState.operator && command.type == 'action' || + !(match = commandMatch(keys, command.keys))) { continue; } + if (match == 'partial') { partial.push(command); } + if (match == 'full') { full.push(command); } + } + return { + partial: partial.length && partial, + full: full.length && full + }; + } + function commandMatch(pressed, mapped) { + if (mapped.slice(-11) == '') { + // Last character matches anything. + var prefixLen = mapped.length - 11; + var pressedPrefix = pressed.slice(0, prefixLen); + var mappedPrefix = mapped.slice(0, prefixLen); + return pressedPrefix == mappedPrefix && pressed.length > prefixLen ? 'full' : + mappedPrefix.indexOf(pressedPrefix) == 0 ? 'partial' : false; + } else { + return pressed == mapped ? 'full' : + mapped.indexOf(pressed) == 0 ? 'partial' : false; + } + } + function lastChar(keys) { + var match = /^.*(<[\w\-]+>)$/.exec(keys); + var selectedCharacter = match ? match[1] : keys.slice(-1); + if (selectedCharacter.length > 1){ + switch(selectedCharacter){ + case '': + selectedCharacter='\n'; + break; + case '': + selectedCharacter=' '; + break; + default: + break; } } - return true; + return selectedCharacter; } function repeatFn(cm, fn, repeat) { return function() { @@ -2665,7 +2627,13 @@ } return false; } - function cusrorIsBetween(cur1, cur2, cur3) { + function cursorMin(cur1, cur2) { + return cursorIsBefore(cur1, cur2) ? cur1 : cur2; + } + function cursorMax(cur1, cur2) { + return cursorIsBefore(cur1, cur2) ? cur2 : cur1; + } + function cursorIsBetween(cur1, cur2, cur3) { // returns true if cur2 is between cur1 and cur3. var cur1before2 = cursorIsBefore(cur1, cur2); var cur2before3 = cursorIsBefore(cur2, cur3); @@ -2892,6 +2860,33 @@ 'visualLine': vim.visualLine, 'visualBlock': block}; } + function expandSelection(cm, start, end) { + var head = cm.getCursor('head'); + var anchor = cm.getCursor('anchor'); + var tmp; + if (cursorIsBefore(end, start)) { + tmp = end; + end = start; + start = tmp; + } + if (cursorIsBefore(head, anchor)) { + head = cursorMin(start, head); + anchor = cursorMax(anchor, end); + } else { + anchor = cursorMin(start, anchor); + head = cursorMax(head, end); + } + return [anchor, head]; + } + function getHead(cm) { + var cur = cm.getCursor('head'); + if (cm.getSelection().length == 1) { + // Small corner case when only 1 character is selected. The "real" + // head is the left of head and anchor. + cur = cursorMin(cur, cm.getCursor('anchor')); + } + return cur; + } function exitVisualMode(cm) { cm.off('mousedown', exitVisualMode); @@ -2964,7 +2959,7 @@ } function expandWordUnderCursor(cm, inclusive, _forward, bigWord, noSymbol) { - var cur = cm.getCursor(); + var cur = getHead(cm); var line = cm.getLine(cur.line); var idx = cur.ch; @@ -3350,7 +3345,7 @@ // TODO: perhaps this finagling of start and end positions belonds // in codmirror/replaceRange? function selectCompanionObject(cm, symb, inclusive) { - var cur = cm.getCursor(), start, end; + var cur = getHead(cm), start, end; var bracketRegexp = ({ '(': /[()]/, ')': /[()]/, @@ -3395,7 +3390,7 @@ // have identical opening and closing symbols // TODO support across multiple lines function findBeginningAndEnd(cm, symb, inclusive) { - var cur = copyCursor(cm.getCursor()); + var cur = copyCursor(getHead(cm)); var line = cm.getLine(cur.line); var chars = line.split(''); var start, end, i, len; @@ -3828,6 +3823,7 @@ // shortNames must not match the prefix of the other command. var defaultExCommandMap = [ { name: 'map' }, + { name: 'imap', shortName: 'im' }, { name: 'nmap', shortName: 'nm' }, { name: 'vmap', shortName: 'vm' }, { name: 'unmap' }, @@ -3882,7 +3878,7 @@ if (command.type == 'exToKey') { // Handle Ex to Key mapping. for (var i = 0; i < command.toKeys.length; i++) { - CodeMirror.Vim.handleKey(cm, command.toKeys[i]); + CodeMirror.Vim.handleKey(cm, command.toKeys[i], 'mapping'); } return; } else if (command.type == 'exToEx') { @@ -4006,7 +4002,7 @@ this.commandMap_[commandName] = { name: commandName, type: 'exToKey', - toKeys: parseKeyString(rhs), + toKeys: rhs, user: true }; } @@ -4014,7 +4010,7 @@ if (rhs != ':' && rhs.charAt(0) == ':') { // Key to Ex mapping. var mapping = { - keys: parseKeyString(lhs), + keys: lhs, type: 'keyToEx', exArgs: { input: rhs.substring(1) }, user: true}; @@ -4023,9 +4019,9 @@ } else { // Key to key mapping var mapping = { - keys: parseKeyString(lhs), + keys: lhs, type: 'keyToKey', - toKeys: parseKeyString(rhs), + toKeys: rhs, user: true }; if (ctx) { mapping.context = ctx; } @@ -4034,15 +4030,6 @@ } }, unmap: function(lhs, ctx) { - var arrayEquals = function(a, b) { - if (a === b) return true; - if (a == null || b == null) return true; - if (a.length != b.length) return false; - for (var i = 0; i < a.length; i++) { - if (a[i] !== b[i]) return false; - } - return true; - }; if (lhs != ':' && lhs.charAt(0) == ':') { // Ex to Ex or Ex to key mapping if (ctx) { throw Error('Mode not supported for ex mappings'); } @@ -4053,9 +4040,9 @@ } } else { // Key to Ex or key to key mapping - var keys = parseKeyString(lhs); + var keys = lhs; for (var i = 0; i < defaultKeymap.length; i++) { - if (arrayEquals(keys, defaultKeymap[i].keys) + if (keys == defaultKeymap[i].keys && defaultKeymap[i].context === ctx && defaultKeymap[i].user) { defaultKeymap.splice(i, 1); @@ -4067,21 +4054,6 @@ } }; - // Converts a key string sequence of the form abd into Vim's - // keymap representation. - function parseKeyString(str) { - var key, match; - var keys = []; - while (str) { - match = (/<\w+-.+?>|<\w+>|./).exec(str); - if (match === null)break; - key = match[0]; - str = str.substring(match.index + key.length); - keys.push(key); - } - return keys; - } - var exCommands = { map: function(cm, params, ctx) { var mapArgs = params.args; @@ -4093,6 +4065,7 @@ } exCommandDispatcher.map(mapArgs[0], mapArgs[1], ctx); }, + imap: function(cm, params) { this.map(cm, params, 'insert'); }, nmap: function(cm, params) { this.map(cm, params, 'normal'); }, vmap: function(cm, params) { this.map(cm, params, 'visual'); }, unmap: function(cm, params, ctx) { @@ -4574,65 +4547,10 @@ }); } - // Register Vim with CodeMirror - function buildVimKeyMap() { - /** - * Handle the raw key event from CodeMirror. Translate the - * Shift + key modifier to the resulting letter, while preserving other - * modifers. - */ - function cmKeyToVimKey(key, modifier) { - var vimKey = key; - if (isUpperCase(vimKey) && modifier == 'Ctrl') { - vimKey = vimKey.toLowerCase(); - } - if (modifier) { - // Vim will parse modifier+key combination as a single key. - vimKey = modifier.charAt(0) + '-' + vimKey; - } - var specialKey = ({Enter:'CR',Backspace:'BS',Delete:'Del'})[vimKey]; - vimKey = specialKey ? specialKey : vimKey; - vimKey = vimKey.length > 1 ? '<'+ vimKey + '>' : vimKey; - return vimKey; - } - - // Closure to bind CodeMirror, key, modifier. - function keyMapper(vimKey) { - return function(cm) { - CodeMirror.signal(cm, 'vim-keypress', vimKey); - CodeMirror.Vim.handleKey(cm, vimKey); - }; - } - - var cmToVimKeymap = { + CodeMirror.keyMap.vim = { 'nofallthrough': true, 'style': 'fat-cursor' }; - function bindKeys(keys, modifier) { - for (var i = 0; i < keys.length; i++) { - var key = keys[i]; - if (!modifier && key.length == 1) { - // Wrap all keys without modifiers with '' to identify them by their - // key characters instead of key identifiers. - key = "'" + key + "'"; - } - var vimKey = cmKeyToVimKey(keys[i], modifier); - var cmKey = modifier ? modifier + '-' + key : key; - cmToVimKeymap[cmKey] = keyMapper(vimKey); - } - } - bindKeys(upperCaseAlphabet); - bindKeys(lowerCaseAlphabet); - bindKeys(upperCaseAlphabet, 'Ctrl'); - bindKeys(specialSymbols); - bindKeys(specialSymbols, 'Ctrl'); - bindKeys(numbers); - bindKeys(numbers, 'Ctrl'); - bindKeys(specialKeys); - bindKeys(specialKeys, 'Ctrl'); - return cmToVimKeymap; - } - CodeMirror.keyMap.vim = buildVimKeyMap(); function exitInsertMode(cm) { var vim = cm.state.vim; @@ -4688,63 +4606,13 @@ } } - defineOption('enableInsertModeEscKeys', false, 'boolean'); - // Use this option to customize the two-character ESC keymap. - // If you want to use characters other than i j or k you'll have to add - // lines to the vim-insert and await-second keymaps later in this file. - defineOption('insertModeEscKeys', 'kj', 'string'); // The timeout in milliseconds for the two-character ESC keymap should be // adjusted according to your typing speed to prevent false positives. defineOption('insertModeEscKeysTimeout', 200, 'number'); - function firstEscCharacterHandler(ch) { - return function(cm){ - var keys = getOption('insertModeEscKeys'); - var firstEscCharacter = keys && keys.length > 1 && keys.charAt(0); - if (!getOption('enableInsertModeEscKeys')|| firstEscCharacter !== ch) { - return CodeMirror.Pass; - } else { - cm.replaceRange(ch, cm.getCursor(), cm.getCursor(), "+input"); - cm.setOption('keyMap', 'await-second'); - cm.state.vim.awaitingEscapeSecondCharacter = true; - setTimeout( - function(){ - if(cm.state.vim.awaitingEscapeSecondCharacter) { - cm.state.vim.awaitingEscapeSecondCharacter = false; - cm.setOption('keyMap', 'vim-insert'); - } - }, - getOption('insertModeEscKeysTimeout')); - } - }; - } - function secondEscCharacterHandler(ch){ - return function(cm) { - var keys = getOption('insertModeEscKeys'); - var secondEscCharacter = keys && keys.length > 1 && keys.charAt(1); - if (!getOption('enableInsertModeEscKeys')|| secondEscCharacter !== ch) { - return CodeMirror.Pass; - // This is not the handler you're looking for. Just insert as usual. - } else { - if (cm.state.vim.insertMode) { - var lastChange = vimGlobalState.macroModeState.lastInsertModeChanges; - if (lastChange && lastChange.changes.length) { - lastChange.changes.pop(); - } - } - cm.state.vim.awaitingEscapeSecondCharacter = false; - cm.replaceRange('', {ch: cm.getCursor().ch - 1, line: cm.getCursor().line}, - cm.getCursor(), "+input"); - exitInsertMode(cm); - } - }; - } CodeMirror.keyMap['vim-insert'] = { // TODO: override navigation keys so that Esc will cancel automatic // indentation from o, O, i_ - 'Esc': exitInsertMode, - 'Ctrl-[': exitInsertMode, - 'Ctrl-C': exitInsertMode, 'Ctrl-N': 'autocomplete', 'Ctrl-P': 'autocomplete', 'Enter': function(cm) { @@ -4752,20 +4620,10 @@ CodeMirror.commands.newlineAndIndent; fn(cm); }, - // The next few lines are where you'd add additional handlers if - // you wanted to use keys other than i j and k for two-character - // escape sequences. Don't forget to add them in the await-second - // section as well. - "'i'": firstEscCharacterHandler('i'), - "'j'": firstEscCharacterHandler('j'), - "'k'": firstEscCharacterHandler('k'), fallthrough: ['default'] }; CodeMirror.keyMap['await-second'] = { - "'i'": secondEscCharacterHandler('i'), - "'j'": secondEscCharacterHandler('j'), - "'k'": secondEscCharacterHandler('k'), fallthrough: ['vim-insert'] }; @@ -4789,7 +4647,7 @@ match = (/<\w+-.+?>|<\w+>|./).exec(text); key = match[0]; text = text.substring(match.index + key.length); - CodeMirror.Vim.handleKey(cm, key); + CodeMirror.Vim.handleKey(cm, key, 'macro'); if (vim.insertMode) { var changes = register.insertModeChanges[imc++].changes; vimGlobalState.macroModeState.lastInsertModeChanges.changes = @@ -4869,10 +4727,7 @@ } else if (cm.doc.history.lastSelOrigin == '*mouse') { // Reset lastHPos if mouse click was done in normal mode. vim.lastHPos = cm.doc.getCursor().ch; - if (cm.somethingSelected()) { - // If something is still selected, enter visual mode. - vim.visualMode = true; - } + handleExternalSelection(cm, vim); } if (vim.visualMode) { var from, head; @@ -4891,6 +4746,22 @@ } } + function handleExternalSelection(cm, vim) { + var anchor = cm.getCursor('anchor'); + var head = cm.getCursor('head'); + // Enter visual mode when the mouse selects text. + if (!vim.visualMode && !vim.insertMode && cm.somethingSelected()) { + vim.visualMode = true; + vim.visualLine = false; + CodeMirror.signal(cm, "vim-mode-change", {mode: "visual"}); + cm.on('mousedown', exitVisualMode); + } + if (vim.visualMode) { + updateMark(cm, vim, '<', cursorMin(head, anchor)); + updateMark(cm, vim, '>', cursorMax(head, anchor)); + } + } + /** Wrapper for special keys pressed in insert mode */ function InsertModeKey(keyName) { this.keyName = keyName; diff --git a/static/js/lib/codemirror-4.5/mode/apl/apl.js b/static/js/lib/codemirror-4.7/mode/apl/apl.js similarity index 100% rename from static/js/lib/codemirror-4.5/mode/apl/apl.js rename to static/js/lib/codemirror-4.7/mode/apl/apl.js diff --git a/static/js/lib/codemirror-4.5/mode/apl/index.html b/static/js/lib/codemirror-4.7/mode/apl/index.html similarity index 92% rename from static/js/lib/codemirror-4.5/mode/apl/index.html rename to static/js/lib/codemirror-4.7/mode/apl/index.html index f8282ac..53dda6b 100644 --- a/static/js/lib/codemirror-4.5/mode/apl/index.html +++ b/static/js/lib/codemirror-4.7/mode/apl/index.html @@ -12,12 +12,12 @@ .CodeMirror { border: 2px inset #dee; }