Skip to content

Instantly share code, notes, and snippets.

@lagbox
Last active May 3, 2016 20:17
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 lagbox/ceb20645980eed9dc16c63789546e9af to your computer and use it in GitHub Desktop.
Save lagbox/ceb20645980eed9dc16c63789546e9af to your computer and use it in GitHub Desktop.
Komodo - Indent and Unindent entire line, respecting cursor position and selection
// Macro recorded on: Thu Apr 28 2016 11:27:27 GMT-0400 (EDT)
komodo.assertMacroVersion(3);
if (komodo.view) { komodo.view.setFocus(); }
var editor = require("ko/editor");
// current cursor position
var pos = editor.getCursorPosition();
// current line size
var osize = editor.getLineSize();
var selected = true;
// the selection range
var selection = editor.getSelectionRange();
// if there is no difference in range, we don't have selected text
if (selection.start.absolute == selection.end.absolute) {
selected = false;
// move cursor to the beginning of the line
ko.commands.doCommand('cmd_home');
}
// indent the line/selected lines
ko.commands.doCommand('cmd_indent');
// new line size
var nsize = editor.getLineSize();
// the difference in line size after indent
var diff = nsize - osize;
// adjust the cursor position by the difference
// if position is at the beginning, leave at the beginning
pos.ch += pos.ch === 0 ? 0 : diff;
editor.setCursor(pos);
// if we had selected text, reselect that same text
// taking into account the indent
if (selected) {
// if position as at beginning of a line, leave at beginning
selection.start.ch += selection.start.ch === 0 ? 0 : diff;
selection.end.ch += selection.end.ch === 0 ? 0 : diff;
editor.setSelection(selection.start, selection.end);
}
// Macro recorded on: Thu Apr 28 2016 11:29:15 GMT-0400 (EDT)
komodo.assertMacroVersion(3);
if (komodo.view) { komodo.view.setFocus(); }
var editor = require("ko/editor");
// current cursor position
var pos = editor.getCursorPosition();
// current line size
var osize = editor.getLineSize();
var selected = true;
// the selection range
var selection = editor.getSelectionRange();
// if there is no difference in range, we don't have selected text
if (selection.start.absolute == selection.end.absolute) {
selected = false;
// move the cursor to the beginning of the line
ko.commands.doCommand('cmd_home');
}
// unindent the line / selected lines
ko.commands.doCommand('cmd_dedent');
// new line size
var nsize = editor.getLineSize();
// the difference in line size after unindent
var diff = osize - nsize;
// adjust the cursor position by the difference
// if the position from beginning of line is less than the difference don't adjust
pos.ch -= pos.ch <= diff ? 0 : diff;
editor.setCursor(pos);
// if we had selected text, reselect that same text
// taking into account the unindent
if (selected) {
// if the position from beginning of line is less than the difference don't adjust
selection.start.ch -= selection.start.ch <= diff ? 0 : diff;
selection.end.ch -= selection.end.ch <= diff ? 0 : diff;
editor.setSelection(selection.start, selection.end);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment