Skip to content

Instantly share code, notes, and snippets.

@marcan
Last active May 20, 2022 18:43
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save marcan/02d48881d2dd1649980fec8915d4f89a to your computer and use it in GitHub Desktop.
var katescript = {
"author": "Hector Martin <marcan@marcan.st>",
"license": "BSD",
"revision": 1,
"kate-version": "5.1",
"functions": ["furiTool"],
"actions": [
{ "function": "furiTool",
"name": "Insert Furigana",
"category": "Quick Coding",
"interactive": "false"
}
]
};
require('cursor.js');
require('range.js');
function doFuri (start, end)
{
if (start.line != end.line) {
return;
}
var width = end.column - start.column;
if (width <= 1) {
document.editBegin();
view.clearSelection();
document.insertText(end, "()");
end.column++;
view.setCursorPosition(end);
document.editEnd();
} else {
document.editBegin();
view.clearSelection();
document.insertText(start, "{");
end.column++;
document.insertText(end, "}()");
end.column += 2;
view.setCursorPosition(end);
document.editEnd();
}
}
function findNext (pos)
{
var kanji = /[\u4e00-\u9fff]+/;
while (pos.line < document.lines()) {
var text = document.line(pos.line);
var left = text.substr(pos.column);
var match = kanji.exec(left);
if (match) {
pos.column += match.index;
var end = new Cursor(pos.line, pos.column + match[0].length);
return [pos, end];
}
pos.line += 1;
pos.column = 0;
}
return null;
}
function furiTool ()
{
var pos = view.cursorPosition();
var selectionRange = view.selection();
if (!selectionRange.isValid()) {
var next = document.charAt(pos);
if (next == ")") {
pos = findNext(pos);
if (pos !== null) {
doFuri(pos[0], pos[1]);
}
} else {
doFuri(pos, pos);
}
} else {
doFuri(selectionRange.start, selectionRange.end);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment