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

««Mar 2010»»
SMTWTFS
  123456
78910111213
14151617181920
21222324252627
28293031

Mailing List

My RSS Feeds








Python and emacs (6): (exuberant) tags and Python

posted 2009.09.15 Tue

Since ctags is somewhat language agnostic, this isn't really Python specific. Neither python.el nor python-mode.el do anything with tags that I can tell.

Welcome to 80's style code navigation! Ctags is a program the analyzes code and creates an index (called a TAGS file). Unlike a compiler that knows what's going on in your code, the index makes best guesses (using advanced technology such as regexes). The basic idea is that with this index in hand one can quickly navigate to the definition of a function or class. (Although it may be ancient tech, there are quite a few editors that support this old school technology, including emacs, jedit, kate, notepad++, vi and even Textmate)

I've been reading through Programming in Emacs Lisp, and in section 4.1 it talks about using TAGS files for navigation of elisp files. So I thought I'd mess around with it in Python.

One way to build a TAGS file is to use the etags command included in emacs. I used exuberant tags, a more featureful etags replacement (though I'm not really using too many features just the recurse feature (rather than find/pipe/xargs to normal etags)).

Apparently, buildout has support for creating TAG files. Since I've never used buildout, I can't comment more on that, but it looks straightforward enough for buildout users.

Another way as suggested in section 12.5 "Creating Your Own TAGS File (of the lisp intro book), is to run the command from M-x compile. (Though first you might want to M-x cd to get to the root of your project if your current python buffer isn't at the root. Then instead of running the normal compile command, you'll give something like this (note --recurse is an exuberant feature, also note that "src" should be a directory containing python modules)

etags --recurse=yes src
. Note, another binary is included with exuberant called ctags. Don't use that one, as emacs will complain about an invalid tag file.

That's all there is to creating TAGS. Of course, once you edit your files, you need to recreate the TAGS file, as it will be out of date. (If I find a good way to automate that, I'll post here).

How to use tags? Well emacswiki has some suggestions for using tags. The main one is M-. (find-tag) which should take you to the definition of said tag (after you point it to the TAGS file you just created). There's also tags-search which is a little like C-s but if you follow it with M-,, it takes you to all references of a tag which includes going to other files as well. Quite handy.

If anyone has any more suggestions for tags, I'd love to hear them. I'd like to look into js/html/css support. I'll update this post as I use tags more.

tags:          

links: digg this    del.icio.us    reddit




1. schaueho left...
2009.09.16 Wed 12:36 am :: http://hillview.1on.de/

I'm using a simple shell alias for generating the tags, which I typically invoke from a top-level directory of a project:

  • alias update-tags='find . -type f -name '\*py'\ | xargs etags -o TAGS -a -l python'

Two things here: using the '-a' switch results in appending to an existing TAGS file and -l specifies the language. The recursive switch you show doesn't seem to be provided by older versions of etags.


2. Reinout van Rees left...
2009.09.16 Wed 3:55 am :: http://reinout.vanrees.org/

Handy command line alias! I'm using buildout with "omelette", which creates a symlink structure to all the libraries used. So I had to add the -L option for traversing the symlinks:

find -L . -type f -name '*py'|xargs etags -a -o TAGS -l python


3. Marius Gedminas left...
2009.09.16 Wed 9:09 am :: http://gedmin.as

I wrote a custom vim plugin that lets me press F5 while on top of an identifier, looks up the file defining that name, and adds the appropriate 'from foo.bar.baz import X' at the top. Incredibly awesomely useful.

I believe my ex-coworker Ignas Mikalajūnas had the same thing in his Emacs.


4. Erick Bodine left...
2009.09.16 Wed 11:55 am

"ctags -e -R *" at the top of your tree produces an Emacs compatible TAGS file - Exuberant Ctags 5.7 - emacs 23.1


5. Marius Gedminas left...
2009.09.16 Wed 4:58 pm :: http://gedmin.as

Another useful use of tags -- again for vim, I'm not that familiar with emacs -- is that they are used for keyword completion (vim's equivalent of dabbrev-expand). This way you get a complete database of all identifiers used in your project.


6. Adam left...
2009.09.22 Tue 2:49 am :: http://www.adamsinfo.com

Hi

Sub : Tech Blog Link Request - http://www.adamsinfo.com

For leveraging traffic from the internet, I have manually collected a list of quality blogs

and sites with whom I am interested in getting associated.

I liked your Site/blog and i'm interested in having my blog's text link in your blog roll.

Please let me know your interest in this association and then we can move towards next step.

Sincerely,

Webmaster Adamsinfo.com seo@apnicsolutions.com