Skip to content

Instantly share code, notes, and snippets.

@felixr
Created July 31, 2012 19:34
Show Gist options
  • Save felixr/3219831 to your computer and use it in GitHub Desktop.
Save felixr/3219831 to your computer and use it in GitHub Desktop.
Generate ctags for R scripts
function addtag(name, lnum, line, kind)
{
# Change all occurences of "/" to "\/" and "\" to "\\"
gsub(/\\/,"\\\\",line)
gsub(/\//,"\\/",line)
tag[name] = FILENAME "\t/^" line "$/;\"\t" kind "\tline:" lnum
}
{ curline = $0 }
/^#/ { next } #skip comment lines
# functions
/.*<-.*/ {
if ( match($0, /^([a-zA-Z0-9.]+)\$new[[:space:]]*<-[[:space:]]*function/, a) ) {
addtag(a[1],NR, curline, "c")
addtag("new", NR, curline, "m\tclass:" a[1] )
}else
if ( match($0, /^([a-zA-Z0-9.]+)\$([a-zA-Z0-9]+)[[:space:]]*<-[[:space:]]*function/, a) ) {
addtag(a[2], NR, curline, "m\tclass:" a[1] )
}else
if ( match($0, /^([a-zA-Z0-9.]+)[[:space:]]*<-[[:space:]]*function/, a) ) {
addtag(a[1], NR, curline, "f")
}else
if (match($0, /^([a-zA-Z.][a-zA-Z0-9._]*)[[:space:]]<-/, a )) {
# variables
match($0, /^([a-zA-Z0-9._]+)[[:space:]]*<-/, a)
addtag(a[1], NR, curline, "v")
}
}
/setClass\(/ {
match($0, /setClass\(["']([a-zA-Z0-9._]+)["']\)/, a)
addtag(a[1], NR, curline, "c")
}
# package imports
/(require|library)\(/ {
if ( match($0, /^(require|library)\(([a-zA-Z0-9.]+)\)/, a) ) {
addtag(a[2], NR, curline, "i")
}
}
# Ignore all other lines
{ next }
END {
for (i in tag) {
print i "\t" tag[i] | "sort"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment