Created
May 15, 2010 12:43
-
-
Save satyr/402175 to your computer and use it in GitHub Desktop.
autoindent
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
// ==UserScript== | |
// @name autoindent | |
// @desc Performs basic auto-indentation on ENTER. | |
// @include main | |
// @include chrome://xqjs/content/xqjs.xul | |
// @include chrome://stylish/content/edit.xul | |
// @include chrome://keyconfig/content/edit.xul | |
// @include chrome://firegestures/content/edit.xul | |
// @compat Fx3.6+ | |
// @author satyr | |
// ==/UserScript== | |
autoindent.chars = /^[ \t\u3000]*([ \t\u3000#@>=|*/+-])\1*[ \t\u3000]{0,}/ | |
function autoindent(ev){ | |
if(ev.shiftKey || ev.ctrlKey || ev.altKey || ev.metaKey || | |
ev.keyCode != ev.DOM_VK_RETURN) return | |
var tb = ev.originalTarget | |
if(!(tb instanceof HTMLTextAreaElement)) return | |
var {value, selectionStart: pos} = tb = XPCNativeWrapper(tb) | |
var line = value.slice(value.lastIndexOf('\n', pos - 1) + 1, pos) | |
if(!autoindent.chars.test(line)) return | |
var indent = RegExp.lastMatch | |
tb.selectionStart -= /\s*$/.exec(RegExp.rightContext)[0].length | |
tb instanceof Ci.nsIDOMNSEditableElement | |
tb.editor.QueryInterface(Ci.nsIPlaintextEditor).insertText('\n'+ indent) | |
ev.preventDefault() | |
ev.stopPropagation() | |
} | |
addEventListener('keypress', autoindent, false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment