1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-20 13:47:57 +01:00

Setup abbreviation behavior to be like an isk-less Vim.

For now, the internal equivalent of "iskeyword" is [^\s'"].

In the future, the internal "iskeyword" equivalent can be expanded. It may
be overboard to add an "iskeyword" option, as it applies to lots more
things than :abbr. Additionally, it's not clear why it's bad to call
keyword characters everything except whitespace and quotes.

TODO: Should abbreviations be triggered by any non-keyword character?

TODO: Should abbreviations be triggered by <CR>?

TODO: Should abbreviations be able to include <Left>, etc.?
This commit is contained in:
Ted Pavlic
2009-01-17 00:39:01 -05:00
parent ba948246cc
commit 21c4f0f89e
2 changed files with 63 additions and 4 deletions

View File

@@ -46,6 +46,45 @@ function Editor() //{{{
// XXX: this strikes me as a rather odd ds; everyone's a critic --djk // XXX: this strikes me as a rather odd ds; everyone's a critic --djk
var abbreviations = {}; // abbreviations["lhr"][0]["{i,c,!}","rhs"] 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() function getEditor()
{ {
return window.document.commandDispatcher.focusedElement; return window.document.commandDispatcher.focusedElement;
@@ -164,7 +203,7 @@ function Editor() //{{{
"Abbreviate a key sequence" + modeDescription, "Abbreviate a key sequence" + modeDescription,
function (args) function (args)
{ {
let matches = args.string.match(/^\s*([^\s"']*)(?:\s*$|\s+(.*))/); let matches = args.string.match(RegExp("^\\s*($|" + abbrevmatch + ")(?:\\s*$|\\s+(.*))"));
if (! matches) if (! matches)
{ {
liberator.echoerr("E474: Invalid argument"); liberator.echoerr("E474: Invalid argument");
@@ -1016,7 +1055,7 @@ function Editor() //{{{
let text = textbox.value; let text = textbox.value;
let currStart = textbox.selectionStart; let currStart = textbox.selectionStart;
let currEnd = textbox.selectionEnd; 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) if (!foundWord)
return true; return true;

View File

@@ -172,6 +172,27 @@ ________________________________________________________________________________
section:Abbreviations[abbreviations] 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| |:abbreviate|
||:ab[breviate] {lhs} {rhs}|| + ||:ab[breviate] {lhs} {rhs}|| +
||:ab[breviate] {lhs}|| + ||:ab[breviate] {lhs}|| +
@@ -179,8 +200,7 @@ section:Abbreviations[abbreviations]
________________________________________________________________________________ ________________________________________________________________________________
Abbreviate a key sequence. Abbreviate {lhs} to {rhs}. If only {lhs} is given, 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 list all abbreviations that start with {lhs}. List all abbreviations, if no
arguments are given. To prevent ambiguity, the {lhs} cannot contain arguments are given.
quotes (' or ") or spaces.
________________________________________________________________________________ ________________________________________________________________________________