Textmate command to print block level 12.15.2008

Soft tabs in Textmate were getting annoying because I kept losing track of block levels. With hard tabs, turning on invisible characters shows the tabs, giving an easy visual indicator of the current line’s block level or block depth. With soft tabs, however, there’s just a big expanse of space.

The following Textmate command is a naive scanner that takes the current line and spits out the block level. It assumes that the programmer is sane — meaning, that hard tabs and soft tabs are not mixed. It further assumes that if soft tabs are used, only soft tabs are used, and that leading spaces are cleanly divisible by the soft tab size.

There very well may be something out there like this already, but I’m lazy and too tired to search.

#!/usr/bin/env python
import os
import re

if os.environ['TM_SOFT_TABS'] == 'YES':
    tab = " " * int(os.environ['TM_TAB_SIZE'])
else:
    tab = "\t"

print re.match("(\s*).*", os.environ['TM_CURRENT_LINE']).group(1).count(tab)

I have it assigned to control-option-b.