-
-
Save knu/0a7be209fba29f5399d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ============================================================ // | |
// Inside of PRESERVE area. | |
// ============================================================ // | |
function transposeSubString(input, beg, end, to) { | |
let txt = input.value; | |
let head = txt.slice(0, beg); | |
let left = txt.slice(beg, end); | |
let right = txt.slice(end, to); | |
let tail = txt.slice(to); | |
let {scrollTop, scrollLeft} = input; | |
input.value = head + right + left + tail; | |
input.selectionStart = input.selectionEnd = txt.length - tail.length; | |
if (scrollTop === scrollLeft === 0) | |
command.inputScrollSelectionIntoView(input); | |
else | |
input.scrollTop = scrollTop, input.scrollLeft = scrollLeft; | |
} | |
function transposeChars(ev, arg) { | |
let input = ev.originalTarget; | |
let narg = (typeof arg === 'number' ? Math.max(arg, 1) : 1); | |
let pos = input.selectionEnd; | |
if (narg == 1 && | |
(pos == input.value.length || input.value.slice(pos, pos + 1) == "\n") && | |
!(pos >= 1 && input.value.slice(pos - 1, pos) == "\n")) { | |
pos--; | |
} | |
let begin = pos - 1; | |
let end = begin + 1; | |
let to = end + narg; | |
transposeSubString(input, begin, end, to); | |
} | |
ext.add("transpose-chars", transposeChars, "Interchange characters around point"); | |
// ============================================================ // | |
// Bottom of the .keysnail.js | |
// ============================================================ // | |
key.setEditKey("C-t", function (ev, arg) { ext.exec("transpose-chars", arg, ev); }, "Transpose chars", true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment