Using ctags with Octave code base

Are other developers using ctags functionality (say with vim or emacs)?

make ctags

works, but the tags cover just the C/C++/Fortran files. Instead, I generated a tags file at the top-level of the source tree using

ctags -R .

This isn’t too bad. Now I can edit a file like ls.m and put the cursor on ispc() and jump through the tag to ispc.m.

But this doesn’t work for functions like strcmp() which are written in C++. I looked at the tags file format and all of the C++ functions are listed under the same tag DEFUN.

I wrote a small Perl script to munge the tags file and make this work.

#!/usr/bin/perl -wp -i.bak

if (/^DEFUN\t/ && m#\t/\^DEFUN \((\w+),#)
  $func_name = $1;

Usage is tags

Now it seems a bit more reasonable.

But, I would be interested to know if any other developers are using ctags and whether they have any tips.

For I’m not sure what reason, I never made it a habit to use ctags. I tend to just use “hg locate | xargs grep …” But I might change even after all these years if ctags covered more symbols in the Octave sources correctly.

I’ve never been a ctags user before either, and did somethnig similar to your grep solution whenever I needed to locate a particular function. But, I am trying to adapt, be more productive, and jumping through tags and back to the original file is pretty nice.

One thing I found is that after using my script, the tags file is no longer sorted. This is a bit of an issue so it needs to be re-sorted and it turns out that ‘sort’ from the command line produces the incorrect order. I needed to use a ‘sort’ built for tags files so I happened to turn to vim.

Hence, I now use a second script, which calls the script above.

The code is

#!/usr/bin/perl -w

## Create tags file
$r = system ("ctags -R .");

## Use Perl script to clean up DEFUN
$r = system (" tags");

## Use Vim to sort tags in correct ordering
$r = system ("vim -c 'sort' -c 'update' -c 'quit' tags");

I’ve used cscope before. It’s been a while, but iirc it’s similar to ctags, may have more features, may handle complex and multiple definitions better, and is handled automatically by vim as an alternate to ctags.

The difference is that cscope is very targeted towards C-like languages. Exuberant ctags, on the other hand, can figure out what might be an interesting tag for many different languages. Using the ctags -R . in the Octave source tree it is able to index C, C++, Fortran, Octave, Makefile, and HTML files.

I’ve improved so that it detects DEFUN, DEFUN_DLD, DEFMETHOD, DEFMETHOD_DLD. I can’t upload it because Discourse says that ‘.pl’ is not an approved extension. That’s annoying and I’m sure configurable somewhere. For the moment, I’ll just put it in the post.

#!/usr/bin/perl -wp -i.bak

if (m#^(DEF(?:UN|METHOD)(?:_DLD)?)\t.*\t/\^\1 \((\w+),#)
  $type = $1;  $fcn_name = $2;

Now all extensions are allowed to be uploaded in this Discourse forum. Regarding your file, I prefer to just paste the content of files (in fences ```) with less than 20 lines (same on Savannah). Downloading, opening, and finally deleting a file is tiring action :weight_lifting_man:

I post code in fence blocks when the goal is to show it and discuss some aspect of it. When the goal is to just use the code, I find it easier to have a single download button. In this case, the two pieces of Perl code rely on each other and on specific file names. Hence, it is harder to open an editor, copy and paste the code from this report in to it, and save the file under the correct name.

So, for those who are interested, here are the two files. (251 Bytes) (143 Bytes)