1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-03-29 10:53:31 +02:00

allow for partial match filtering in :abbreviation list commands

This commit is contained in:
Doug Kearns
2008-12-12 13:53:36 +11:00
parent f27c461c7f
commit d182429dd3
2 changed files with 62 additions and 63 deletions

View File

@@ -38,7 +38,8 @@ function Editor() //{{{
// store our last search with f, F, t or T // store our last search with f, F, t or T
var lastFindChar = null; var lastFindChar = null;
var lastFindCharFunc = null; var lastFindCharFunc = null;
var abbrev = {}; // abbrev["lhr"][0]["{i,c,!}","rhs"] // XXX: this strikes me as a rather odd ds; everyone's a critic --djk
var abbreviations = {}; // abbreviations["lhr"][0]["{i,c,!}","rhs"]
function getEditor() function getEditor()
{ {
@@ -143,7 +144,7 @@ function Editor() //{{{
// away makes me want to pull someone else's hair out. --Kris // away makes me want to pull someone else's hair out. --Kris
function abbrevs() function abbrevs()
{ {
for (let [lhs, abbr] in Iterator(abbrev)) for (let [lhs, abbr] in Iterator(abbreviations))
for (let [,rhs] in Iterator(abbr)) for (let [,rhs] in Iterator(abbr))
yield [lhs, rhs]; yield [lhs, rhs];
} }
@@ -907,37 +908,35 @@ function Editor() //{{{
// Abbreviations {{{ // Abbreviations {{{
// filter is i, c or "!" (insert or command abbreviations or both) // filter is i, c or "!" (insert or command abbreviations or both)
// ! -> list all, on c or i ! matches too
listAbbreviations: function (filter, lhs) listAbbreviations: function (filter, lhs)
{ {
if (lhs) // list only that one let searchFilter = (filter == "!") ? "!ci" : filter + "!";
let list = [[mode, lhs, rhs] for ([lhs, [mode, rhs]] in abbrevs()) if (searchFilter.indexOf(mode) >= 0)];
if (lhs)
list = list.filter(function (abbrev) abbrev[1].indexOf(lhs) == 0);
if (!list.length)
{ {
if (abbrev[lhs])
{
for (let [,abbr] in Iterator(abbrev[lhs]))
{
if (abbr[0] == filter)
liberator.echo(abbr[0] + " " + lhs + " " + abbr[1]);
// Is it me, or is this clearly very wrong? --Kris
return true;
}
}
liberator.echomsg("No abbreviations found"); liberator.echomsg("No abbreviations found");
return false;
} }
else // list all (for that filter {i,c,!}) else if (list.length == 1)
{ {
var searchFilter = (filter == "!") ? "!ci" let [mode, lhs, rhs] = list[0];
: filter + "!"; // ! -> list all, on c or i ! matches too)
let list = [[rhs[0], lhs, rhs[1]] for ([lhs, rhs] in abbrevs()) if (searchFilter.indexOf(rhs[0]) > -1)]; liberator.echo(mode + " " + lhs + " " + rhs, commandline.FORCE_SINGLELINE); // 2 spaces, 3 spaces
}
if (!list.length) else
return liberator.echoerr("No abbreviations found"); {
list = template.tabular(["", "LHS", "RHS"], [], list); list = template.tabular(["", "LHS", "RHS"], [], list);
commandline.echo(list, commandline.HL_NORMAL, commandline.FORCE_MULTILINE); commandline.echo(list, commandline.HL_NORMAL, commandline.FORCE_MULTILINE);
} }
}, },
// NOTE: I think this comment block is trying to say something but no
// one is listening. In space, no one can hear you scream. --djk
//
// System for adding abbreviations: // System for adding abbreviations:
// //
// filter == ! delete all, and set first (END) // filter == ! delete all, and set first (END)
@@ -965,37 +964,37 @@ function Editor() //{{{
// //
addAbbreviation: function (filter, lhs, rhs) addAbbreviation: function (filter, lhs, rhs)
{ {
if (!abbrev[lhs]) if (!abbreviations[lhs])
{ {
abbrev[lhs] = []; abbreviations[lhs] = [];
abbrev[lhs][0] = [filter, rhs]; abbreviations[lhs][0] = [filter, rhs];
return; return;
} }
if (filter == "!") if (filter == "!")
{ {
if (abbrev[lhs][1]) if (abbreviations[lhs][1])
abbrev[lhs][1] = ""; abbreviations[lhs][1] = "";
abbrev[lhs][0] = [filter, rhs]; abbreviations[lhs][0] = [filter, rhs];
return; return;
} }
for (let i = 0; i < abbrev[lhs].length; i++) for (let i = 0; i < abbreviations[lhs].length; i++)
{ {
if (abbrev[lhs][i][1] == rhs) if (abbreviations[lhs][i][1] == rhs)
{ {
if (abbrev[lhs][i][0] == filter) if (abbreviations[lhs][i][0] == filter)
{ {
abbrev[lhs][i] = [filter, rhs]; abbreviations[lhs][i] = [filter, rhs];
return; return;
} }
else else
{ {
if (abbrev[lhs][i][0] != "!") if (abbreviations[lhs][i][0] != "!")
{ {
if (abbrev[lhs][1]) if (abbreviations[lhs][1])
abbrev[lhs][1] = ""; abbreviations[lhs][1] = "";
abbrev[lhs][0] = ["!", rhs]; abbreviations[lhs][0] = ["!", rhs];
return; return;
} }
else else
@@ -1006,18 +1005,18 @@ function Editor() //{{{
} }
} }
if (abbrev[lhs][0][0] == "!") if (abbreviations[lhs][0][0] == "!")
{ {
var tmpOpp = ("i" == filter) ? "c" : "i"; var tmpOpp = ("i" == filter) ? "c" : "i";
abbrev[lhs][1] = [tmpOpp, abbrev[lhs][0][1]]; abbreviations[lhs][1] = [tmpOpp, abbreviations[lhs][0][1]];
abbrev[lhs][0] = [filter, rhs]; abbreviations[lhs][0] = [filter, rhs];
return; return;
} }
if (abbrev[lhs][0][0] != filter) if (abbreviations[lhs][0][0] != filter)
abbrev[lhs][1] = [filter, rhs]; abbreviations[lhs][1] = [filter, rhs];
else else
abbrev[lhs][0] = [filter, rhs]; abbreviations[lhs][0] = [filter, rhs];
}, },
removeAbbreviation: function (filter, lhs) removeAbbreviation: function (filter, lhs)
@@ -1028,34 +1027,34 @@ function Editor() //{{{
return false; return false;
} }
if (abbrev[lhs]) // abbrev exists if (abbreviations[lhs]) // abbreviations exists
{ {
if (filter == "!") if (filter == "!")
{ {
abbrev[lhs] = ""; abbreviations[lhs] = "";
return true; return true;
} }
else else
{ {
if (!abbrev[lhs][1]) // only one exists if (!abbreviations[lhs][1]) // only one exists
{ {
if (abbrev[lhs][0][0] == "!") // exists as ! -> no 'full' delete if (abbreviations[lhs][0][0] == "!") // exists as ! -> no 'full' delete
{ {
abbrev[lhs][0][0] = (filter == "i") ? "c" : "i"; // ! - i = c; ! - c = i abbreviations[lhs][0][0] = (filter == "i") ? "c" : "i"; // ! - i = c; ! - c = i
return true; return true;
} }
else if (abbrev[lhs][0][0] == filter) else if (abbreviations[lhs][0][0] == filter)
{ {
abbrev[lhs] = ""; abbreviations[lhs] = "";
return true; return true;
} }
} }
else // two abbrev's exists ( 'i' or 'c' (filter as well)) else // two abbreviations exist ( 'i' or 'c' (filter as well))
{ {
if (abbrev[lhs][0][0] == "c" && filter == "c") if (abbreviations[lhs][0][0] == "c" && filter == "c")
abbrev[lhs][0] = abbrev[lhs][1]; abbreviations[lhs][0] = abbreviations[lhs][1];
abbrev[lhs][1] = ""; abbreviations[lhs][1] = "";
return true; return true;
} }
@@ -1070,15 +1069,15 @@ function Editor() //{{{
{ {
if (filter == "!") if (filter == "!")
{ {
abbrev = {}; abbreviations = {};
} }
else else
{ {
for (let lhs in abbrev) for (let lhs in abbreviations)
{ {
for (let i = 0; i < abbrev[lhs].length; i++) for (let i = 0; i < abbreviations[lhs].length; i++)
{ {
if (abbrev[lhs][i][0] == "!" || abbrev[lhs][i][0] == filter) if (abbreviations[lhs][i][0] == "!" || abbreviations[lhs][i][0] == filter)
this.removeAbbreviation(filter, lhs); this.removeAbbreviation(filter, lhs);
} }
} }
@@ -1097,15 +1096,15 @@ function Editor() //{{{
if (!foundWord) if (!foundWord)
return true; return true;
for (let lhs in abbrev) for (let lhs in abbreviations)
{ {
for (let i = 0; i < abbrev[lhs].length; i++) for (let i = 0; i < abbreviations[lhs].length; i++)
{ {
if (lhs == foundWord && (abbrev[lhs][i][0] == filter || abbrev[lhs][i][0] == "!")) if (lhs == foundWord && (abbreviations[lhs][i][0] == filter || abbreviations[lhs][i][0] == "!"))
{ {
// if found, replace accordingly // if found, replace accordingly
var len = foundWord.length; var len = foundWord.length;
var abbrText = abbrev[lhs][i][1]; var abbrText = abbreviations[lhs][i][1];
text = text.substring(0, currStart - len) + abbrText + text.substring(currStart); text = text.substring(0, currStart - len) + abbrText + text.substring(currStart);
textbox.value = text; textbox.value = text;
textbox.selectionStart = currStart - len + abbrText.length; textbox.selectionStart = currStart - len + abbrText.length;

View File

@@ -177,9 +177,9 @@ section:Abbreviations[abbreviations]
||:ab[breviate] {lhs}|| + ||:ab[breviate] {lhs}|| +
||:ab[breviate]|| ||:ab[breviate]||
________________________________________________________________________________ ________________________________________________________________________________
Abbreviate a key sequence. Abbreviate {lhs} to {rhs}. Abbreviate a key sequence. Abbreviate {lhs} to {rhs}. If only {lhs} given, list
If only {lhs} given, list that particular abbreviation. any abbreviations that start with {lhs}. List all abbreviations, if no
List all abbreviations, if no arguments to are given. arguments are given.
________________________________________________________________________________ ________________________________________________________________________________