Skip to content

Instantly share code, notes, and snippets.

@knu
Forked from mooz/transpose-chars.js
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knu/0a7be209fba29f5399d5 to your computer and use it in GitHub Desktop.
Save knu/0a7be209fba29f5399d5 to your computer and use it in GitHub Desktop.
// ============================================================ //
// 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