Version 5 of Tags for Vi/ViM editing

Updated 2003-05-02 20:50:01

Pavel Hampl - I use ViM to edit my Tcl/Tk scripts. From my work with C-code I were used to work with tags which allowed me to jump quickly among different files and find procedure or function or class declaration. Unfortunately the program ctags recognizes only C, Fortran and similar languages. Therefore I have prepared one shell script and one awk script do create an equal 'tags' file which allows me to access proc definitions quickly. The main shell script finds all proc definitions in the directory:

 egrep -n '^[ \t]*proc[ ]+[a-zA-Z][a-zA-Z0-9]*[ ]+\{.+\}[ ]+\{[ ]*$'
 *tcl >tcltags
 awk -f tags.awk tcltags |sort >tags

The awk script which is called there transforms the grep output into a valid 'tags' file, which then has to be sorted. This is the awk script:

  {
   printf("%s",$2)
   split($1,a,":")
   printf("\t./%s\t%s;\"\tf\n",a[1],a[2])
   }                                                                     

Of course, all commands of the shell script can be connected into a piped single line. You then won't have to take care of the 'tcltags' file.

Now vim works fine for me it takes me less time to find the correct proc. Seems to me that these tags should be useful for EMACS users, too.


glennj: the egrep regex contains some errors:

 egrep -n '^[ \t]*proc[ \t]+[^ \t]+[ \t]+{.*}[ \t]+{[ \t]*$' *tcl

is better. But it's still lacking:

  • The {args} argument to proc can be empty, and it does not require braces.
  • A procname does not have to look like [a-zA-Z][a-zA-Z0-9]*, as in
 ; proc {::namespace::my procedure} oneArg \
 "
 statement1 ; statement2
 "

Here's a Tcl script that mirrors your egrep|awk

    proc tag fileName {
        set inProc false
        set lineNo 0
        set fid [open $fileName]
        while {[gets $fid line] != -1} {
            incr lineNo
            if {$inProc} {
                append procBody \n $line
                if {[info complete $procBody]} {
                    emit $fileName $procLineNo $procBody
                    set inProc false
                    unset procBody
                }
            } else {
                if {[string match proc* [string trimleft $line " \t;"]]} {
                    set procBody $line
                    set procLineNo $lineNo
                    if {[info complete $procBody]} {
                        emit $fileName $procLineNo $procBody
                        unset procBody
                    } else {
                        set inProc true
                    }
                }
            }
        }
        close $fid
    }

    proc emit {fileName lineNo body} {
        # treat the $body string as a list, in case procName contains whitespace
        set procName [lindex $body 1]
        puts [format "%s\t./%s\t%s;\"\tf" $procName $fileName $lineNo]
    }

    # MAIN
    #
    foreach file $argv {
        tag $file
    }