Using etags for parsing Matlab m-files


Etags is an easy and wonderful tool. In combination with an editor like Emacs it allows fast browsing in source files and automatic completion of function and variable names (take a look at the Emacs info pages for more information). It does not natively support Matlab m-Files, but fortunately it is possible to easily extend etags using regular expressions. For parsing Matlab-files with etags simply use the following script:

etags -o TAGS  \
    --lang=none \
    --regex='/[ \t]*function.*=[ \t]*\([^ \t(]*\)/\1/' \
    --regex='/[ \t]*function[ \t]+\([^[ \t=(]*\)[ \t]*\($\|(\)/\1/' \
    --regex='/%%%key \(.*\)/\1/' \
    --regex='/[ \t]*global[ \t]+\([^ \t]+\)/\1/' \
    --regex='/[ \t]*global[ \t]+\([^ \t]+[ \t]+\)\{1\}\([^ \t]+\)/\2/' \
    --regex='/[ \t]*global[ \t]+\([^ \t]+[ \t]+\)\{2\}\([^ \t]+\)/\2/' \
    --regex='/[ \t]*global[ \t]+\([^ \t]+[ \t]+\)\{3\}\([^ \t]+\)/\2/' \
    --regex='/[ \t]*global[ \t]+\([^ \t]+[ \t]+\)\{4\}\([^ \t]+\)/\2/' \
    --regex='/[ \t]*global[ \t]+\([^ \t]+[ \t]+\)\{5\}\([^ \t]+\)/\2/' \
    --regex='/[ \t]*global[ \t]+\([^ \t]+[ \t]+\)\{6\}\([^ \t]+\)/\2/' \
    --regex='/[ \t]*global[ \t]+\([^ \t]+[ \t]+\)\{7\}\([^ \t]+\)/\2/' \
    --regex='/[ \t]*global[ \t]+\([^ \t]+[ \t]+\)\{8\}\([^ \t]+\)/\2/' \
    --regex='/[ \t]*global[ \t]+\([^ \t]+[ \t]+\)\{9\}\([^ \t]+\)/\2/' \
    `find . -follow -name "*.m"`
This script borrows heavily from the example shown in the Emacs info pages for using etags with Octave. However, I had to change a few things to cover all possible function definitions which are allowed in Matlab, which includes
function [a,b] = test1(foo,bar)
function [a] = test2(foo)
function a = test3(foo)
function a = test4
function test5(foo)
function test6
For tagging a script file without any function definitions, one has to include a comment like the following:
%%%key test7
The script also parses global variables, but this is limited to 10 variables per line. However, it should be obvious how to extend the script, I just had to stop somewhere...

See also: Using etags for Matlab command completion