// ==UserScript==
// @name       Text input features
// @version    1.0
// @description Add quotes, parens, etc around selected text
// @match      http://*/*
// @match      https://*/*
// ==/UserScript==


function getSelectionText() {
  var text = ''
  if (window.getSelection) {
    text = window.getSelection().toString()
  } else if (document.selection && document.selection.type != 'Control') {
    text = document.selection.createRange().text
  }
  return text
}

function getInputSelection(el) {
  return {
      start: el.selectionStart
    , end: el.selectionEnd
  }
}

function getCaretPosition(element) {
    var caretOffset = 0
    var doc = element.ownerDocument || element.document
    var win = doc.defaultView || doc.parentWindow
    var sel
    if (typeof win.getSelection != "undefined") {
        sel = win.getSelection()
        if (sel.rangeCount > 0) {
            var range = win.getSelection().getRangeAt(0)
            var preCaretRange = range.cloneRange()
            preCaretRange.selectNodeContents(element)
            preCaretRange.setEnd(range.endContainer, range.endOffset)
            caretOffset = preCaretRange.toString().length
        }
    } else if ( (sel = doc.selection) && sel.type != "Control") {
        var textRange = sel.createRange()
        var preCaretTextRange = doc.body.createTextRange()
        preCaretTextRange.moveToElementText(element)
        preCaretTextRange.setEndPoint("EndToEnd", textRange)
        caretOffset = preCaretTextRange.text.length
    }
    return caretOffset
}

function replaceSelectedHTML(replacementText) {
  var sel, range
  if (window.getSelection) {
    sel = window.getSelection()
    if (sel.rangeCount) {
      range = sel.getRangeAt(0)
      range.deleteContents()
      range.insertNode(document.createTextNode(replacementText))
    }
  } else if (document.selection && document.selection.createRange) {
    range = document.selection.createRange()
    range.text = replacementText
  }
}

function replaceSelectedText(el, text) {
  var start = 0

  if (isContentEditable(el)) {
    replaceSelectedHTML(text)
    // start = getCaretPosition(el)
  } else {
    var val = el.value
      , sel = getInputSelection(el)
    el.value = val.slice(0, sel.start) + text + val.slice(sel.end)
    start = sel.start
  }

  if (text.length === 2) {
    start -= 1
  }
  setCaretToPos(el, start + text.length)
}

function setSelectionRange(input, selectionStart, selectionEnd) {
  var range
  if (input.setSelectionRange) {
    input.focus()
    input.setSelectionRange(selectionStart, selectionEnd)
  }
  else if (input.createTextRange) {
    range = input.createTextRange()
    range.collapse(true)
    range.moveEnd('character', selectionEnd)
    range.moveStart('character', selectionStart)
    range.select()
  }
}

function setContenteditableCaretPosition(el, pos) {
  var range = document.createRange()
    , sel = window.getSelection()
  range.setStart(sel.focusNode.nextSibling || sel.focusNode, pos)
  range.collapse(true)
  sel.removeAllRanges()
  sel.addRange(range)
}

function setCaretToPos(el, pos) {
  if (isContentEditable(el)) {
    setContenteditableCaretPosition(el, pos)
  } else {
    setSelectionRange(el, pos, pos)
  }
}

function isContentEditable(el) {
  return typeof el.value === 'undefined' && el.contentEditable
}

window.addEventListener('keydown', function (event) {
  var selected = getSelectionText() || ''
    , target = event.target
    , value = target.value

  // it's not an input...
  if (typeof value === 'undefined') {
    if (isContentEditable(target)) {
      value = target.innerText
    } else {
      return
    }
  }

  switch (event.keyCode) {
    case 57: // 9 or (
      if (event.shiftKey) {
        replaceSelectedText(target, '(' + selected + ')')
      }
      event.preventDefault()
      break
    case 219: // [ or {
      if (event.shiftKey) {
        replaceSelectedText(target, '{' + selected + '}')
      } else {
        replaceSelectedText(target, '[' + selected + ']')
      }
      event.preventDefault()
      break
    case 222: // ' or "
      if (event.shiftKey) {
        replaceSelectedText(target, '"' + selected + '"')
      } else {
        var start = target.selectionStart || getCaretPosition(target)
        if (selected || start === 0 || /\s/.test(value.charAt(start - 1))) {
          replaceSelectedText(target, '\'' + selected + '\'')
        } else {
          break
        }
      }
      event.preventDefault()
      break
  }
}, true)

@@ -638,19 +638,19 @@
 = 0%0A    
-var
+  ,
  doc = e
@@ -694,19 +694,19 @@
 ent%0A    
-var
+  ,
  win = d
@@ -748,16 +748,96 @@
     
+ 
-var
+ ,
  sel%0A
+      , range%0A      , preCaretRange%0A      , textRange%0A      , preCaretTextRange%0A
     
@@ -961,20 +961,16 @@
         
-var 
 range = 
@@ -1006,36 +1006,32 @@
 (0)%0A            
-var 
 preCaretRange = 
@@ -1315,20 +1315,16 @@
         
-var 
 textRang
@@ -1353,20 +1353,16 @@
         
-var 
 preCaret
@@ -3381,328 +3381,36 @@
 %0A%7D%0A%0A
-window.addEventListener('keydown', function (event) %7B%0A  var selected = getSelectionText() %7C%7C ''%0A    , target = event.target%0A    , value = target.value%0A%0A  // it's not an input...%0A  if (typeof value === 'undefined') %7B%0A    if (isContentEditable(target)) %7B%0A      value = target.innerText%0A    %7D else %7B%0A      return%0A    %7D%0A  %7D%0A
+function handleKeys(event) %7B
 %0A  s
@@ -4253,16 +4253,360 @@
 eak%0A  %7D%0A
+%7D%0A%0Awindow.addEventListener('keydown', function (event) %7B%0A  var selected = getSelectionText() %7C%7C ''%0A    , target = event.target%0A    , value = target.value%0A%0A  // it's not an input...%0A  if (typeof value === 'undefined') %7B%0A    if (isContentEditable(target)) %7B%0A      value = target.innerText%0A    %7D else %7B%0A      return%0A    %7D%0A  %7D%0A%0A  handleKeys(event)%0A
 %7D, true)

Clean up better-text a bit