1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-03-06 05:05:46 +01:00

Use ranges for editor.findChar.

This commit is contained in:
Kris Maglione
2011-10-05 00:23:01 -04:00
parent 1af7252f34
commit db579a1ea5
2 changed files with 37 additions and 51 deletions

View File

@@ -161,41 +161,34 @@ var Editor = Module("editor", {
} }
}, },
findChar: function (key, count, backward) { findChar: function (key, count, backward, offset) {
// TODO: Use extendRange, return range. count = count || 1; // XXX ?
offset = (offset || 0) - !!backward;
util.assert(DOM(this.element).isInput);
// XXX
if (count == null)
count = 1;
// Grab the charcode of the key spec. Using the key name
// directly will break keys like <
let code = DOM.Event.parse(key)[0].charCode; let code = DOM.Event.parse(key)[0].charCode;
util.assert(code); util.assert(code);
let char = String.fromCharCode(code); let char = String.fromCharCode(code);
let text = this.element.value; let range = this.selectionRange.cloneRange();
let caret = this.element.selectionEnd; let collapse = DOM(this.element).whiteSpace == "normal";
if (backward) {
let end = text.lastIndexOf("\n", caret);
while (caret > end && caret >= 0 && count--)
caret = text.lastIndexOf(char, caret - 1);
}
else {
let end = text.indexOf("\n", caret);
if (end == -1)
end = text.length;
while (caret < end && caret >= 0 && count--) // Find the *count*th occurance of *char* before a non-collapsed
caret = text.indexOf(char, caret + 1); // \n, ignoring the character at the caret.
} let i = 0;
function test(c) (collapse || c != "\n") && (!i++ || c != char || --count)
if (count > 0) Editor.extendRange(range, !backward, { test: test }, true);
caret = -1; dactyl.assert(count == 0);
if (caret == -1) range.collapse(backward);
dactyl.beep();
return caret; // Skip to any requested offset.
count = Math.abs(offset);
Editor.extendRange(range, offset > 0, { test: function (c) count-- }, true);
range.collapse(offset < 0);
return range;
}, },
/** /**
@@ -268,7 +261,7 @@ var Editor = Module("editor", {
line = 1 + pre.replace(/[^\n]/g, "").length; line = 1 + pre.replace(/[^\n]/g, "").length;
column = 1 + pre.replace(/[^]*\n/, "").length; column = 1 + pre.replace(/[^]*\n/, "").length;
let origGroup = textBox && textBox.getAttributeNS(NS, "highlight") || ""; let origGroup = DOM(textBox).highlight.toString();
let cleanup = util.yieldable(function cleanup(error) { let cleanup = util.yieldable(function cleanup(error) {
if (timer) if (timer)
timer.cancel(); timer.cancel();
@@ -323,7 +316,7 @@ var Editor = Module("editor", {
throw Error(_("io.cantCreateTempFile")); throw Error(_("io.cantCreateTempFile"));
if (textBox) { if (textBox) {
highlight.highlightNode(textBox, origGroup + " EditorEditing"); DOM(textBox).highlight.add("EditorEditing");
if (!keepFocus) if (!keepFocus)
textBox.blur(); textBox.blur();
} }
@@ -354,7 +347,7 @@ var Editor = Module("editor", {
if (!range.collapsed) if (!range.collapsed)
return; return;
Editor.extendRange(range, false, /\S/); Editor.extendRange(range, false, /\S/, true);
let abbrev = abbreviations.match(mode, String(range)); let abbrev = abbreviations.match(mode, String(range));
if (abbrev) { if (abbrev) {
range.setStart(range.startContainer, range.endOffset - abbrev.lhs.length); range.setStart(range.startContainer, range.endOffset - abbrev.lhs.length);
@@ -926,45 +919,37 @@ var Editor = Module("editor", {
// finding characters // finding characters
function offset(backward, before, pos) { function offset(backward, before, pos) {
if (!backward && modes.main != modes.TEXT_EDIT) if (!backward && modes.main != modes.TEXT_EDIT)
pos += 1; return 1;
if (before) if (before)
pos += backward ? +1 : -1; return backward ? +1 : -1;
return pos; return 0;
} }
bind(["f"], "Find a character on the current line, forwards", bind(["f"], "Find a character on the current line, forwards",
function ({ arg, count }) { function ({ arg, count }) {
let pos = editor.findChar(arg, Math.max(count, 1)); editor.selectionRange = editor.findChar(arg, Math.max(count, 1), false,
if (pos >= 0) offset(false, false));
editor.moveToPosition(offset(false, false, pos),
modes.main == modes.VISUAL);
}, },
{ arg: true, count: true, type: "operator" }); { arg: true, count: true, type: "operator" });
bind(["F"], "Find a character on the current line, backwards", bind(["F"], "Find a character on the current line, backwards",
function ({ arg, count }) { function ({ arg, count }) {
let pos = editor.findChar(arg, Math.max(count, 1), true); editor.selectionRange = editor.findChar(arg, Math.max(count, 1), true,
if (pos >= 0) offset(true, false));
editor.moveToPosition(offset(true, false, pos),
modes.main == modes.VISUAL);
}, },
{ arg: true, count: true, type: "operator" }); { arg: true, count: true, type: "operator" });
bind(["t"], "Find a character on the current line, forwards, and move to the character before it", bind(["t"], "Find a character on the current line, forwards, and move to the character before it",
function ({ arg, count }) { function ({ arg, count }) {
let pos = editor.findChar(arg, Math.max(count, 1)); editor.selectionRange = editor.findChar(arg, Math.max(count, 1), false,
if (pos >= 0) offset(false, true));
editor.moveToPosition(offset(false, true, pos),
modes.main == modes.VISUAL);
}, },
{ arg: true, count: true, type: "operator" }); { arg: true, count: true, type: "operator" });
bind(["T"], "Find a character on the current line, backwards, and move to the character after it", bind(["T"], "Find a character on the current line, backwards, and move to the character after it",
function ({ arg, count }) { function ({ arg, count }) {
let pos = editor.findChar(arg, Math.max(count, 1), true); editor.selectionRange = editor.findChar(arg, Math.max(count, 1), true,
if (pos >= 0) offset(true, true));
editor.moveToPosition(offset(true, true, pos),
modes.main == modes.VISUAL);
}, },
{ arg: true, count: true, type: "operator" }); { arg: true, count: true, type: "operator" });

View File

@@ -135,7 +135,8 @@ Dense /* Arbitrary elements which should be packed densely together
margin-top: 0; margin-bottom: 0; margin-top: 0; margin-bottom: 0;
EditorEditing;;* /* Text fields for which an external editor is open */ \ EditorEditing;;* /* Text fields for which an external editor is open */ \
background-color: #bbb !important; -moz-user-input: none !important; -moz-user-modify: read-only !important; -moz-user-input: none !important; -moz-user-modify: read-only !important;
*EditorEditing>*;;* background-color: #bbb !important;
EditorError;;* /* Text fields briefly after an error has occurred running the external editor */ \ EditorError;;* /* Text fields briefly after an error has occurred running the external editor */ \
background: red !important; background: red !important;
EditorBlink1;;* /* Text fields briefly after successfully running the external editor, alternated with EditorBlink2 */ \ EditorBlink1;;* /* Text fields briefly after successfully running the external editor, alternated with EditorBlink2 */ \