unindent line command

0 views
Skip to first unread message

Joe

unread,
Feb 17, 2010, 12:28:14 AM2/17/10
to Ymacs - AJAX code editor

Thanks for the explanation on some of the internals of ymacs Mihai!
I'm still wrapping my head around it. I wanted to create a command to
unindent a line (bound to S-TAB) I'd like the line to unindent to the
first line it finds above it that is indented less than itself. This
is what I came up with:

// find a previous line that is less than our indentation
// indent to that level, if there are none, indent to 0
unindent: Ymacs_Interactive("P", function(noEmpty) {
if (!this.deleteTransientRegion()) {
this.cmd("back_to_indentation");
var c = this._rowcol.col, r = this._rowcol.row;
var m = /^\s+/.exec(this.code[r]);
if(!m) return
var curr = m[0].length;
var len = 0;
for (var i = r-1; i > 0; i--) {
m = /^\s+/.exec(this.code[i]);
if(m) {
len = m[0].length;
if( len < curr) {
var n = curr - len;
this.cmd("backward_delete_char",n);
return true;
}
}
}
// no indents above, delete to beginning of line
this.cmd("backward_delete_char",curr);
return true;
}
}),

I put it in ymacs-commands.js.

eventually I'd like to associate it with a particular mode but for now
i just have the keybinding in my keymap file.

I think it would be good if it could work on a region or a line, so it
would unindent the entire region if there is one. have to figure out
how to get the region startrow and endrow...

I'm sure the code could be made better, so if you have an suggestions,
fire away.
thanks,
-joe

Reply all
Reply to author
Forward
0 new messages