1## rtags -- tag source files recursively in a directory tree
2##
3## Examples:
4##  R CMD rtags -o TAGS /path/to/Rsrc/
5
6revision='$Rev: 47214 $'
7version=`set - ${revision}; echo ${2}`
8version="rtags/etags front-end: ${R_VERSION} (r${version})
9
10Copyright (C) 2008-2020 The R Core Team.
11This is free software; see the GNU General Public License version 2
12or later for copying conditions.  There is NO warranty."
13
14usage="Usage: R CMD rtags [options] [path]
15
16Tag C, R, and Rd files under the directory 'path' (pwd by default).
17
18Options:
19  -h, --help		print short help message and exit
20  -v, --version		print version info and exit
21  -o, --output=FILE	write output to FILE (see below for defaults)
22      --no-c		do not tag C (.c, .h) files
23      --no-R		do not tag R (.R, .r, .S, .s) files
24      --no-Rd		do not tag Rd (.Rd) files
25      --ctags           write Ctags format (default is Etags)
26  -a  --append          append to output file (overwrites by default)
27  -V, --verbose		echo the name of files processed
28
29If unspecified, the output file name defaults to 'TAGS' for
30Emacs-style Etags (the default), and 'tags' for Vi-style Ctags
31(specified using --ctags).
32
33The R files to be tagged are expected to belong to a package, and are
34skipped if their containing directory is not named R. Use the
35'rtags()' function directly for more flexibiity.
36
37Report bugs at <https://bugs.R-project.org>."
38
39
40
41## FIXME: Rd and C files may be simpler to deal with on Windows if we
42## use R to locate the files, and then call system("etags ...") to tag
43## them.
44
45
46cfiles=true
47rfiles=true
48rdfiles=true
49verbose=false
50append=false
51ofile="TAGS"
52tagprog="etags"
53
54while test -n "${1}"; do
55  case ${1} in
56    -h|--help)
57      echo "${usage}"; exit 0 ;;
58    -v|--version)
59      echo "${version}"; exit 0 ;;
60    -V|--verbose)
61      verbose=true ;;
62    -a|--append)
63      append=true ;;
64    --no-c)
65      cfiles=false ;;
66    --no-R)
67      rfiles=false ;;
68    --no-Rd)
69      rdfiles=false ;;
70    --ctags)
71      tagprog="ctags"; ofile="tags" ;;
72    --output=*)
73      ofile=`echo "${1}" | ${SED} -e 's/[^=]*=//'` ;;
74    -o)
75      if test -n "`echo ${2} | ${SED} 's/^-.*//'`"; then
76	ofile="${2}"; shift
77      else
78	echo "ERROR: option '${1}' requires an argument"
79	exit 1
80      fi
81      ;;
82    --|*)
83      break ;;
84  esac
85  shift
86done
87
88
89NDIR=${#}
90case ${NDIR} in
91    0)
92      SRCDIR=`pwd` ;;
93    1)
94      SRCDIR=${1} ;;
95    *)
96      SRCDIR=${1}; echo "WARNING: multiple paths specified; using the first one";;
97esac
98
99RPROG=${R_HOME}/bin/R
100
101type=""
102first=true
103if ${rfiles}; then type="R"; first=false; fi
104if ${cfiles}; then
105    if ${first}; then
106	type="C"
107	first=false
108    else
109	type="${type}/C"
110    fi
111fi
112if ${rdfiles}; then
113    if ${first}; then
114	type="Rd"
115	first=false
116    else
117	type="${type}/Rd"
118    fi
119fi
120
121if test -z ${type}; then
122    echo "ERROR: nothing to tag"
123    exit 1
124fi
125
126echo @ECHO_N@ "
127Tagging ${type} files under ${SRCDIR}; writing to ${ofile}"@ECHO_C@
128if ! ${append}; then
129    echo @ECHO_N@ " (overwriting)"@ECHO_C@
130    echo @ECHO_N@ "" > ${ofile}@ECHO_C@
131else
132    echo @ECHO_N@ " (appending)"@ECHO_C@
133    touch ${ofile}
134fi
135echo "...
136"
137
138## echo RPROG=${RPROG}
139
140if ${verbose}; then VERBOSE=TRUE; else VERBOSE=FALSE; fi
141if ${append}; then APPEND=TRUE; else APPEND=FALSE; fi
142
143if ${rfiles}; then
144    SCRIPT_DIR=$(dirname $0)
145    echo "
146
147require(utils)
148
149rtags(normalizePath('${SRCDIR}'), pattern = '[.]*\\\\\.[RrSs]$',
150      keep.re = '/R/[^/]*\\\\\.[RrSs]',
151      verbose = ${VERBOSE},
152      type = '${tagprog}',
153      ofile = '${ofile}',
154      append = ${APPEND},
155      recursive = TRUE)" | "${RPROG}" --no-echo
156
157fi
158
159if ${rdfiles}; then
160    if ${verbose}; then
161	find -L ${SRCDIR} -type f -name "*.Rd" -print
162    fi
163    find -L ${SRCDIR} -type f -name "*.Rd" -print0 | xargs -0 ${tagprog} -o ${ofile} -a -l none --regex='/\\alias[{]\([^{}]*\)[}]/\1/'
164fi
165
166if ${cfiles}; then
167    if ${verbose}; then
168	find -L ${SRCDIR} -type f -name "*.[ch]" -print
169	find -L ${SRCDIR} -type f -name "*.cpp" -print
170	find -L ${SRCDIR} -type f -name "*.hpp" -print
171	find -L ${SRCDIR} -type f -name "*.cc" -print
172	find -L ${SRCDIR} -type f -name "*.hh" -print
173    fi
174    find -L ${SRCDIR} -type f -name "*.[ch]" -print0 | xargs -0 ${tagprog} -o ${ofile} -a -l c
175    find -L ${SRCDIR} -type f -name "*.cpp" -print0 | xargs -0 ${tagprog} -o ${ofile} -a -l c
176    find -L ${SRCDIR} -type f -name "*.hpp" -print0 | xargs -0 ${tagprog} -o ${ofile} -a -l c
177    find -L ${SRCDIR} -type f -name "*.cc" -print0 | xargs -0 ${tagprog} -o ${ofile} -a -l c
178    find -L ${SRCDIR} -type f -name "*.hh" -print0 | xargs -0 ${tagprog} -o ${ofile} -a -l c
179fi
180
181
182## FIXME: if --ctags then sort TAGS file
183
184echo "Done"
185
186exit 0
187
188
189
190### Local Variables: ***
191### mode: sh ***
192### sh-indentation: 2 ***
193### End: ***
194