Skip to content

Instantly share code, notes, and snippets.

@satyr
Created May 15, 2010 12:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satyr/402175 to your computer and use it in GitHub Desktop.
Save satyr/402175 to your computer and use it in GitHub Desktop.
autoindent
// ==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