unindent

Script key: unindent

Posted by Xor

Install

Details

Label
Voice Over unindent
Script
var text = getText()
var n = text.length
var selRange = getSelectedRange()

var p, q

var selectedText = text.substr(selRange[0], selRange[1])

var indentSize = 2

var shift = 0

var lines = selectedText.split('\n')
for (var i = 1; i < lines.length; ++i) {
  lines[i] = unindentLine(lines[i])
}

selectedText = lines.join('\n')
setSelectedText(selectedText)

selectLine()
setSelectedRange(p, q-p)

setSelectedText(unindentLine(getSelectedText()))

setSelectedRange(selRange[0] + shift, selectedText.length)


function unindentLine(line) {
  var i = 0
  while (i < indentSize && 
         i < line.length && 
         line[i] === ' ') {
    ++i
  }
  shift = -i
  return line.substr(i)
}

function selectLine() {
 
  
  var i = selRange[0]
  var delim = /^[\n]$/
  
  while (i > 0) {
    if (delim.test(text[i - 1]))
      break
  
    --i
  }
  
  var j = selRange[0]//+selRange[1]
  
  while (j < n) {
    if (delim.test(text[j]))
      break
    ++j
  }

  ++j

  p = i
  q = j
  
  //if (j-i)
  //  setSelectedRange(i, j-i)
}

Advanced

Keyboard shortcut None
Hide key Yes

Discussion

comments powered by Disqus