1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-20 08:07:59 +01:00

add abbreviation completion

This commit is contained in:
Doug Kearns
2008-12-13 01:05:45 +11:00
parent 05ac5cce49
commit e9c53d3776
3 changed files with 82 additions and 68 deletions

View File

@@ -743,13 +743,13 @@ function Commands() //{{{
// TODO: offer completion.ex? // TODO: offer completion.ex?
var completeOptionMap = { var completeOptionMap = {
altstyle: "alternateStyleSheet", bookmark: "bookmark", abbreviation: "abbreviation", altstyle: "alternateStyleSheet",
buffer: "buffer", color: "colorScheme", command: "command", bookmark: "bookmark", buffer: "buffer", color: "colorScheme",
dialog: "dialog", dir: "directory", environment: "environment", command: "command", dialog: "dialog", dir: "directory",
event: "autocmdEvent", file: "file", help: "help", environment: "environment", event: "autocmdEvent", file: "file",
highlight: "highlightGroup", javascript: "javascript", macro: "macro", help: "help", highlight: "highlightGroup", javascript: "javascript",
mapping: "userMapping", menu: "menuItem", option: "option", macro: "macro", mapping: "userMapping", menu: "menuItem",
preference: "preference", search: "search", option: "option", preference: "preference", search: "search",
shellcmd: "shellCommand", sidebar: "sidebar", url: "url", shellcmd: "shellCommand", sidebar: "sidebar", url: "url",
usercommand: "userCommand" usercommand: "userCommand"
}; };

View File

@@ -517,7 +517,7 @@ CompletionContext.prototype = {
liberator.threadYield(false, interruptable); liberator.threadYield(false, interruptable);
return this.incomplete; return this.incomplete;
} }
} };
function Completion() //{{{ function Completion() //{{{
{ {
@@ -1161,6 +1161,15 @@ function Completion() //{{{
////////////////////// COMPLETION TYPES //////////////////////////////////////// ////////////////////// COMPLETION TYPES ////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
abbreviation: function abbreviation(context, args, mode)
{
if (args.completeArg == 0)
{
let abbreviations = editor.getAbbreviations(mode);
context.completions = [[lhs, ""] for ([, [, lhs,]] in Iterator(abbreviations))];
}
},
alternateStyleSheet: function alternateStylesheet(context) alternateStyleSheet: function alternateStylesheet(context)
{ {
context.title = ["Stylesheet", "Location"]; context.title = ["Stylesheet", "Location"];
@@ -1683,7 +1692,7 @@ function Completion() //{{{
context.completions = maps; context.completions = maps;
} }
} }
// }}} //}}}
}; };
const UrlCompleter = new Struct("name", "description", "completer"); const UrlCompleter = new Struct("name", "description", "completer");

View File

@@ -166,6 +166,7 @@ function Editor() //{{{
editor.listAbbreviations(mode, lhs || ""); editor.listAbbreviations(mode, lhs || "");
}, },
{ {
completer: function (context, args) completion.abbreviation(context, args, mode),
literal: 1, literal: 1,
serial: function () [ serial: function () [
{ {
@@ -183,6 +184,7 @@ function Editor() //{{{
function (args) { editor.removeAbbreviation(mode, args.literalArg); }, function (args) { editor.removeAbbreviation(mode, args.literalArg); },
{ {
argCount: "1", argCount: "1",
completer: function (context, args) completion.abbreviation(context, args, mode),
literal: 0 literal: 0
}); });
@@ -907,33 +909,6 @@ function Editor() //{{{
// Abbreviations {{{ // Abbreviations {{{
// filter is i, c or "!" (insert or command abbreviations or both)
// ! -> list all, on c or i ! matches too
listAbbreviations: function (filter, lhs)
{
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)
{
liberator.echomsg("No abbreviations found");
}
else if (list.length == 1)
{
let [mode, lhs, rhs] = list[0];
liberator.echo(mode + " " + lhs + " " + rhs, commandline.FORCE_SINGLELINE); // 2 spaces, 3 spaces
}
else
{
list = template.tabular(["", "LHS", "RHS"], [], list);
commandline.echo(list, commandline.HL_NORMAL, commandline.FORCE_MULTILINE);
}
},
// NOTE: I think this comment block is trying to say something but no // 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 // one is listening. In space, no one can hear you scream. --djk
// //
@@ -1019,6 +994,68 @@ function Editor() //{{{
abbreviations[lhs][0] = [filter, rhs]; abbreviations[lhs][0] = [filter, rhs];
}, },
expandAbbreviation: function (filter) // try to find an candidate and replace accordingly
{
var textbox = getEditor();
if (!textbox)
return;
var text = textbox.value;
var currStart = textbox.selectionStart;
var currEnd = textbox.selectionEnd;
var foundWord = text.substring(0, currStart).replace(/^(.|\n)*?(\S+)$/m, "$2"); // get last word \b word boundary
if (!foundWord)
return true;
for (let lhs in abbreviations)
{
for (let i = 0; i < abbreviations[lhs].length; i++)
{
if (lhs == foundWord && (abbreviations[lhs][i][0] == filter || abbreviations[lhs][i][0] == "!"))
{
// if found, replace accordingly
var len = foundWord.length;
var abbrText = abbreviations[lhs][i][1];
text = text.substring(0, currStart - len) + abbrText + text.substring(currStart);
textbox.value = text;
textbox.selectionStart = currStart - len + abbrText.length;
textbox.selectionEnd = currEnd - len + abbrText.length;
break;
}
}
}
return true;
},
// filter is i, c or "!" (insert or command abbreviations or both)
// ! -> list all, on c or i ! matches too
getAbbreviations: function (filter, lhs)
{
let searchFilter = (filter == "!") ? "!ci" : filter + "!";
return list = [[mode, left, right] for ([left, [mode, right]] in abbrevs())
if (searchFilter.indexOf(mode) >= 0 && left.indexOf(lhs || "") == 0)];
},
listAbbreviations: function (filter, lhs)
{
let list = this.getAbbreviations(filter, lhs);
if (!list.length)
{
liberator.echomsg("No abbreviations found");
}
else if (list.length == 1)
{
let [mode, lhs, rhs] = list[0];
liberator.echo(mode + " " + lhs + " " + rhs, commandline.FORCE_SINGLELINE); // 2 spaces, 3 spaces
}
else
{
list = template.tabular(["", "LHS", "RHS"], [], list);
commandline.echo(list, commandline.HL_NORMAL, commandline.FORCE_MULTILINE);
}
},
removeAbbreviation: function (filter, lhs) removeAbbreviation: function (filter, lhs)
{ {
if (!lhs) if (!lhs)
@@ -1082,38 +1119,6 @@ function Editor() //{{{
} }
} }
} }
},
expandAbbreviation: function (filter) // try to find an candidate and replace accordingly
{
var textbox = getEditor();
if (!textbox)
return;
var text = textbox.value;
var currStart = textbox.selectionStart;
var currEnd = textbox.selectionEnd;
var foundWord = text.substring(0, currStart).replace(/^(.|\n)*?(\S+)$/m, "$2"); // get last word \b word boundary
if (!foundWord)
return true;
for (let lhs in abbreviations)
{
for (let i = 0; i < abbreviations[lhs].length; i++)
{
if (lhs == foundWord && (abbreviations[lhs][i][0] == filter || abbreviations[lhs][i][0] == "!"))
{
// if found, replace accordingly
var len = foundWord.length;
var abbrText = abbreviations[lhs][i][1];
text = text.substring(0, currStart - len) + abbrText + text.substring(currStart);
textbox.value = text;
textbox.selectionStart = currStart - len + abbrText.length;
textbox.selectionEnd = currEnd - len + abbrText.length;
break;
}
}
}
return true;
} }
//}}} //}}}
}; };