About

Welcome to Panela, Matt Harrison's take on mostly Open Source, Linux, Python, innovation in those areas, other buzzwords and Dick Proenneke. It comes complete with the illustrations as needed. Note the opinions expressed here are merely my opinions and not the opinions of my employer.

about Matt

Calendar

««Nov 2009»»
SMTWTFS
1234567
8910
11
12
1314
151617
18
192021
22232425262728
2930

Mailing List

My RSS Feeds








Python and emacs (3): (Balancing) parentheses and others

posted 2009.03.10 Tue

(Again not necessarily python specific)

One nice feature of emacs is show-paren-mode. (Naming it show-paren-mode rather than paren-show-mode makes it somewhat less discoverable but I digress). This nice little feature allows one to make sure parens (or brackets or curly brackets) are in order. It does so by visually highlighting the matching pair when the cursor is over the starting one (or after the ending one). It's very useful when you have a long dictionary with lists as values. The emacs wiki page includes a function for showing matching ends when one is offscreen. Very handy indeed.

To enable show-paren-mode type M-x show-paren-mode or put something like this in your .emacs:

;; highlight balanced parens
(add-hook 'python-mode-hook (lambda () (show-paren-mode 1)))

Apparently one of the critically acclaimed of Textmate is autopairs (or something like that). People love it cause it saves them a keystroke. If you type an opening paren, it adds a closing paren and moves the cursor inside the parens. Nice! How to get that in emacs?

As always in the open source world, there are many ways. Here's what I went through:

I searched for electric pair and found this entry in the emacs wiki. It's a good start but it has some drawbacks. It only inserts the matching pair when the cursor is at the end of the line (admittedly that's probably the case 80% of the time). So it won't work on nested items (say a string inside a list).

To deal with that first issue I tried skeleton mode. This gets around the embedding issue.

Then I find this blog post illuminating me on the second issue. I didn't think of this at first but if you have electric completes you probably want electric deletes too. So I'm currently using a modified version of his code (added support for single quote). This appears to handle triple quoting that is common in python docstrings too.

Marcelo's blog also mentioned paredit which seems to be quite featureful for lisp source, but has issues with python (no single quotes, space inserted between started paren and previous character, complex source). So I only tried it for a minute or so.

Good luck with your balancing!

tags:              

links: digg this    del.icio.us    reddit




1. Markus Gritsch left...
2009.03.23 Mon 2:14 am

Instead of advicing backward-delete-char-untabify it is better to advice delete-backward-char to have the deleting of pairs also working in non-C modes like HTML mode.


2. sakthi left...
2009.07.02 Thu 3:51 am :: http://www.macrotesting.com

Good post with correct example... Thank you all your examples are nice and is clear to understand with your point.


3. Marius Andersen left...
2009.07.06 Mon 3:55 am

Have a look at http://www.emacswiki.org/emacs/AutoPairs .