diff --git a/common/content/editor.js b/common/content/editor.js index a8f4c481..f4338c47 100644 --- a/common/content/editor.js +++ b/common/content/editor.js @@ -46,6 +46,45 @@ function Editor() //{{{ // XXX: this strikes me as a rather odd ds; everyone's a critic --djk var abbreviations = {}; // abbreviations["lhr"][0]["{i,c,!}","rhs"] + // (summarized from Vim's ":help abbreviations") + // + // There are three types of abbreviations: + // + // full-id: Consists entirely of keyword characters. + // ("foo", "g3", "-1") + // + // end-id: Ends in a keyword character, but all other + // are not keyword characters. + // ("#i", "..f", "$/7") + // + // non-id: Ends in a non-keyword character, but the + // others can be of any type other than space + // and tab. + // ("def#", "4/7$") + // + // Example strings that cannot be abbreviations: + // "a.b", "#def", "a b", "_$r" + // + // For now, a keyword character is anything except for \s, ", or ' + // (i.e., whitespace and quotes). In Vim, a keyword character is + // specified by the 'iskeyword' setting and is a much less inclusive + // list. + // + // TODO: Make keyword definition closer to Vim's default keyword + // definition (which differs across platforms). + // + + let nonkw = "\\s\"'"; + let keyword = "[^" + nonkw + "]"; + let nonkeyword = "[" + nonkw + "]"; + + let full_id = keyword + "+"; + let end_id = nonkeyword + "+" + keyword; + let non_id = "\\S*" + nonkeyword; + + // Used in addAbbrevation and expandAbbreviation + var abbrevmatch = full_id + "|" + end_id + "|" + non_id; + function getEditor() { return window.document.commandDispatcher.focusedElement; @@ -164,7 +203,7 @@ function Editor() //{{{ "Abbreviate a key sequence" + modeDescription, function (args) { - let matches = args.string.match(/^\s*([^\s"']*)(?:\s*$|\s+(.*))/); + let matches = args.string.match(RegExp("^\\s*($|" + abbrevmatch + ")(?:\\s*$|\\s+(.*))")); if (! matches) { liberator.echoerr("E474: Invalid argument"); @@ -1016,7 +1055,7 @@ function Editor() //{{{ let text = textbox.value; let currStart = textbox.selectionStart; let currEnd = textbox.selectionEnd; - let foundWord = text.substring(0, currStart).replace(/^(.|\n)*?([^\s"']+)$/m, "$2"); // get last word \b word boundary + let foundWord = text.substring(0, currStart).replace(RegExp("^(.|\\n)*?\\s*(" + abbrevmatch + ")$", "m"), "$2"); // get last word \b word boundary if (!foundWord) return true; diff --git a/vimperator/locale/en-US/map.txt b/vimperator/locale/en-US/map.txt index 53fa2920..63eeb989 100644 --- a/vimperator/locale/en-US/map.txt +++ b/vimperator/locale/en-US/map.txt @@ -172,6 +172,27 @@ ________________________________________________________________________________ section:Abbreviations[abbreviations] +Vimperator can automatically replace words identified as abbreviations, +which may be used to save typing or to correct commonly misspelled +words. An abbreviation can be one of three types that are defined by the +types of constituent characters. Whitespace and quotes are non-keyword +types, and all other characters are keyword types. + +. A "full-id" abbreviation consists entirely of characters that are not +keyword characters (e.g., "teh", "msoft"). + +. An "end-id" abbreviation ends in keyword character but otherwise +contains all non-keyword characters (e.g., "'i"). + +. A "non-id" abbreviation ends in a non-keyword character but otherwise +contains any non-whitespace character (e.g., "def'"). + +Strings that cannot be abbreviations include "a'b" and "a b". + +An abbreviation is recognized when a space is typed after the +abbreviation. There are no default abbreviations, and abbreviations are +never recursive. + |:ab| |:abbreviate| ||:ab[breviate] {lhs} {rhs}|| + ||:ab[breviate] {lhs}|| + @@ -179,8 +200,7 @@ section:Abbreviations[abbreviations] ________________________________________________________________________________ Abbreviate a key sequence. Abbreviate {lhs} to {rhs}. If only {lhs} is given, list all abbreviations that start with {lhs}. List all abbreviations, if no -arguments are given. To prevent ambiguity, the {lhs} cannot contain -quotes (' or ") or spaces. +arguments are given. ________________________________________________________________________________