Your Linux Data Center Experts

vim Presentation: indent.vimrc

" indent.vimrc
autocmd BufRead * python setIndentation()

python << EOF
def setIndentation():
   import vim
   maxSearch = 1000     #  max number of lines to search through

   indentSpaces = None
   cb = vim.current.buffer
   indentCount = { ' ' : 0, '\t' : 0 }
   justSawDefOrClassLine = 0
   for i in xrange(0, min(maxSearch, len(cb))):
      line = cb[i]
      if not line: continue

      #  count spaces after a class or def line
      if justSawDefOrClassLine:
         justSawDefOrClassLine = 0
         if line[0] == ' ':
            indentSpaces = 0
            for c in line:
               if c != ' ': break
               indentSpaces = indentSpaces + 1
      if line[:4] == 'def ' or line[:6] == 'class ':
         justSawDefOrClassLine = 1

      #  add to tab versus space count
      if line[0] in ' \t':
         indentCount[line[0]] = indentCount.get(line[0], 0) + 1

   #  more lines started with space
   if indentCount[' '] > indentCount['\t']:
      vim.command('set smarttab tabstop=8 expandtab')
      if indentSpaces:
         vim.command('set ts=%d sw=%d' % ( indentSpaces, indentSpaces ))

   #  more lines started with tab
   else:
      vim.command('set softtabstop=3 ts=3 sw=3')
EOF