mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2025-12-20 15:57:57 +01:00
Adjust buffer.currentWord not to modify the selection. Add 'isk', undocumented until we decide on an interface.
This commit is contained in:
@@ -517,33 +517,27 @@ var Buffer = Module("buffer", {
|
|||||||
* null, it tries to guess the word that the caret is
|
* null, it tries to guess the word that the caret is
|
||||||
* positioned in.
|
* positioned in.
|
||||||
*
|
*
|
||||||
* NOTE: might change the selection
|
|
||||||
*
|
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
getCurrentWord: function (win) {
|
getCurrentWord: function (win) {
|
||||||
win = win || this.focusedFrame || content;
|
|
||||||
let selection = win.getSelection();
|
let selection = win.getSelection();
|
||||||
if (selection.rangeCount == 0)
|
if (selection.rangeCount == 0)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
let range = selection.getRangeAt(0);
|
let range = selection.getRangeAt(0).cloneRange();
|
||||||
if (selection.isCollapsed) {
|
if (range.collapsed) {
|
||||||
let controller = util.selectionController(win);
|
let re = options.get("iskeyword").regexp;
|
||||||
let caretmode = controller.getCaretEnabled();
|
util.dump(String.quote(range));
|
||||||
controller.setCaretEnabled(true);
|
Editor.extendRange(range, true, re, true);
|
||||||
|
util.dump(String.quote(range));
|
||||||
// Only move backwards if the previous character is not a space.
|
Editor.extendRange(range, false, re, true);
|
||||||
if (range.startOffset > 0 && !/\s/.test(range.startContainer.textContent[range.startOffset - 1]))
|
util.dump(String.quote(range) + "\n\n\n");
|
||||||
controller.wordMove(false, false);
|
|
||||||
|
|
||||||
controller.wordMove(true, true);
|
|
||||||
controller.setCaretEnabled(caretmode);
|
|
||||||
return String.match(selection, /\w*/)[0];
|
|
||||||
}
|
}
|
||||||
return util.domToString(range);
|
return util.domToString(range);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
get currentWord() this.getCurrentWord(this.focusedFrame),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if a scripts are allowed to focus the given input
|
* Returns true if a scripts are allowed to focus the given input
|
||||||
* element or input elements in the given window.
|
* element or input elements in the given window.
|
||||||
@@ -1836,7 +1830,7 @@ var Buffer = Module("buffer", {
|
|||||||
mappings.add(myModes, ["Y", "<yank-word>"],
|
mappings.add(myModes, ["Y", "<yank-word>"],
|
||||||
"Copy selected text or current word",
|
"Copy selected text or current word",
|
||||||
function () {
|
function () {
|
||||||
let sel = buffer.getCurrentWord();
|
let sel = buffer.currentWord;
|
||||||
dactyl.assert(sel);
|
dactyl.assert(sel);
|
||||||
dactyl.clipboardWrite(sel, true);
|
dactyl.clipboardWrite(sel, true);
|
||||||
});
|
});
|
||||||
@@ -1902,6 +1896,17 @@ var Buffer = Module("buffer", {
|
|||||||
function () { buffer.showPageInfo(true); });
|
function () { buffer.showPageInfo(true); });
|
||||||
},
|
},
|
||||||
options: function () {
|
options: function () {
|
||||||
|
options.add(["iskeyword", "isk"],
|
||||||
|
"Regular expression defining which characters constitute word characters",
|
||||||
|
"string", '[^\\s.,!?:;/"\'^$%&?()[\\]{}<>#*+|=~_-]',
|
||||||
|
{
|
||||||
|
setter: function (value) {
|
||||||
|
this.regexp = util.regexp(value);
|
||||||
|
return value;
|
||||||
|
},
|
||||||
|
validator: function (value) RegExp(value)
|
||||||
|
});
|
||||||
|
|
||||||
options.add(["nextpattern"],
|
options.add(["nextpattern"],
|
||||||
"Patterns to use when guessing the next page in a document sequence",
|
"Patterns to use when guessing the next page in a document sequence",
|
||||||
"regexplist", UTF8("'\\bnext\\b',^>$,^(>>|»)$,^(>|»),(>|»)$,'\\bmore\\b'"),
|
"regexplist", UTF8("'\\bnext\\b',^>$,^(>>|»)$,^(>|»),(>|»)$,'\\bmore\\b'"),
|
||||||
|
|||||||
@@ -387,6 +387,38 @@ var Editor = Module("editor", {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
|
extendRange: function extendRange(range, forward, re, sameWord) {
|
||||||
|
// FIXME: This could be so much simpler if I had more sleep.
|
||||||
|
function advance(positive) {
|
||||||
|
let tmp = range.cloneRange();
|
||||||
|
while (tmp.endOffset < nodeRange.endOffset) {
|
||||||
|
tmp.setEnd(tmp.endContainer, tmp.endOffset + 1);
|
||||||
|
if (!re.test(String.slice(tmp, -1)) == positive)
|
||||||
|
break;
|
||||||
|
range.setEnd(tmp.endContainer, tmp.endOffset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function retreat(positive) {
|
||||||
|
let tmp = range.cloneRange();
|
||||||
|
while (tmp.startOffset > 0) {
|
||||||
|
tmp.setStart(tmp.startContainer, tmp.startOffset - 1);
|
||||||
|
if (re.test(String(tmp)[0]) == positive)
|
||||||
|
break;
|
||||||
|
range.setStart(tmp.startContainer, tmp.startOffset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let nodeRange = range.cloneRange();
|
||||||
|
nodeRange.selectNodeContents(range.startContainer);
|
||||||
|
|
||||||
|
let charge = forward ? advance : retreat;
|
||||||
|
|
||||||
|
charge(true);
|
||||||
|
if (!sameWord || !forward)
|
||||||
|
charge(false);
|
||||||
|
return range;
|
||||||
|
},
|
||||||
|
|
||||||
getEditor: function (elem) {
|
getEditor: function (elem) {
|
||||||
if (arguments.length === 0) {
|
if (arguments.length === 0) {
|
||||||
dactyl.assert(dactyl.focusedElement);
|
dactyl.assert(dactyl.focusedElement);
|
||||||
@@ -520,35 +552,8 @@ var Editor = Module("editor", {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function updateRange(editor, forward, re, modify) {
|
function updateRange(editor, forward, re, modify) {
|
||||||
// FIXME: This could be so much simpler if I had more sleep.
|
let range = Editor.extendRange(editor.selection.getRangeAt(0),
|
||||||
function advance(positive) {
|
forward, re, false);
|
||||||
let tmp = range.cloneRange();
|
|
||||||
while (tmp.endOffset < nodeRange.endOffset) {
|
|
||||||
tmp.setEnd(tmp.endContainer, tmp.endOffset + 1);
|
|
||||||
if (!re.test(String.slice(tmp, -1)) == positive)
|
|
||||||
break;
|
|
||||||
range.setEnd(tmp.endContainer, tmp.endOffset);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function retreat(positive) {
|
|
||||||
let tmp = range.cloneRange();
|
|
||||||
while (tmp.startOffset > 0) {
|
|
||||||
tmp.setStart(tmp.startContainer, tmp.startOffset - 1);
|
|
||||||
if (re.test(String(tmp)[0]) == positive)
|
|
||||||
break;
|
|
||||||
range.setStart(tmp.startContainer, tmp.startOffset);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let range = editor.selection.getRangeAt(0).cloneRange();
|
|
||||||
let nodeRange = range.cloneRange();
|
|
||||||
nodeRange.selectNodeContents(range.startContainer);
|
|
||||||
|
|
||||||
let charge = forward ? advance : retreat;
|
|
||||||
|
|
||||||
charge(true);
|
|
||||||
charge(false);
|
|
||||||
|
|
||||||
modify(range);
|
modify(range);
|
||||||
editor.selection.removeAllRanges();
|
editor.selection.removeAllRanges();
|
||||||
editor.selection.addRange(range);
|
editor.selection.addRange(range);
|
||||||
@@ -749,7 +754,7 @@ var Editor = Module("editor", {
|
|||||||
modes.pop();
|
modes.pop();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
dactyl.clipboardWrite(buffer.getCurrentWord(), true);
|
dactyl.clipboardWrite(buffer.currentWord, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
mappings.add([modes.VISUAL, modes.TEXT_EDIT],
|
mappings.add([modes.VISUAL, modes.TEXT_EDIT],
|
||||||
|
|||||||
Reference in New Issue
Block a user