public
Last active

  • Download Gist
transpose-chars.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// ============================================================ //
// 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 begin = input.selectionEnd - 1;
let end = begin + 1;
let to = end + (typeof arg === 'number' ? Math.max(arg, 1) : 1);
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);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.