Wilcox Development Solutions Blog

Balancing Blocks of Python with BBEdit

October 20, 2004

All of the Python (and, increasingly, all my other programming) happens in Barebone’s wonderful BBEdit text editor.

For Brace and Semi-colon based languages (take the C family of languages for example), BBEdit’s Balance command will select the entire code block that contains your insertion point. For languages without curly braces, however (like Python) the Balance command will not work. So I wrote a script. More detail in the extended entry… Some pictures explain BBEdit’s Balance functionality better than I ever could:

code block (in C) that contains my insertion point

Now, the results of the Balance command:

the code block selected using the Balance command in BBEdit

One of the main points about Python is that it tries to eliminate syntactical sugar where it can. This means all the semi-colons, braces, and “redundant punctuation” go away (or are greatly reduced.) Whitespace now controls the extent of code blocks.

code block (in Python) that contains my insertion point

This feature of syntactically important whitespace is an interesting conceptual hurdle for beginning Python programmers (its not as bad as you thing it is!) While presenting only minimal difficulties to the programmer, it does pose serious issues to BBEdit’s Balance command (which, apparently, goes looking for braces to match up, but finds none.)

Tonight I was working on a Python project, and finally decided that enough was enough - I was going to write an Applescript to provide this functionality.

Why Applescript? Mainly because getting the currently selected line, and that line in relation to other lines, is impossible (or very hard) using the current Unix Filters/Scripts interface. That, in addition to “how do I set the selection in a Unix Filter/Script?” sealed my choice: Applescript it was. I guess I could have used the Appscript package by Has, but without an OSA Python component this would have been much harder than just writing an Applescript version.

After a few hours of work tonight I was able to create such a script. This script selects the first column position of every line that is in the same code-block as the current line. note that I said “column” and not “character”. This may, or may not, surprise you (especially if you are using tabs for whitespace.)

The result of my script, after I had selected the 2nd line in the Python document:

the (Python) code block selected using my script

I’ve put this script up on my public access Subversion repository, at https://scm.wilcoxd.com:8081/svn/OpenSource/Scripts/BBEdit/Select_Python_Block/. Even without a Subversion client you should be able to mount this (WebDAV) server in your OS. Of course, you can always navigate and download the script with a web browser.

The repository uses HTTP authentication Use ‘anon’ (without the quotes) as the username and leave the password blank when you are prompted to authenticate. Yes, I know I have a self-signed certificate. I need to get a real one at some point.

The code is licensed under the Creative Commons Attribution License, and modifications to the script are welcome.