The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/bin/sh

# Generate a tags file which contains PARI functions, and allows going
# to help-message for a PARI functions and to a PARI function of C function
# by repeated C-u M-.

# If you are reading this, you may enjoy imenu-go.el from
#    ftp://ftp.math.ohio-state.edu/pub/users/ilya/emacs/

# Skip language/init.c, gp/gp_init.c and language/helpmessages.c:

messages='language/helpmsg.c'
gp_init='gp/gp_init.c'
init_c='language/init.c'

excluded=`perl -e 'print join "\n",
                      grep { $_ ne "'$init_c'"
			     and $_ ne "'$messages'"
			     and $_ ne "'$gp_init'" }
                           <*/*.[chs]>, <*/*/*.[chs]>'`

etags $excluded

# Process declarations like

# {"polredord",1,(void*)ordred,6,"Gp"},

# => an entry for polredord, and an entry for ordred as well
# (so that one may find which PARI function is implemented by the C one)

re='[ \t]*{[ \t]*"\([a-zA-Z_0-9]+\)"[ \t]*,[ \t]*[0-9]+[ \t]*,[ \t]*([ \t]*void[ \t]*\*[ \t]*)[ \t]*\([a-zA-Z0-9+]+\)[ \t]*,'

etags -a -l c -o TAGS.tmp     -r "/$re/\\1/" -r "/$re/\\2/" $init_c

# Process helpmessages too:
#  "printtex(a): outputs a in TeX format",

helpre='[ \t]*"\([a-zA-Z_0-9]+\)[=(]'

etags -a -l c -o TAGS.tmp     -r "/$re/\\1/" -r "/$re/\\2/" \
    -r "/$helpre/\\1/"    $gp_init

etags -a -l c -o TAGS.tmp     -r "/$helpre/\\1/"  $messages

# This would produces a wrong entry size - though AFAIK, nobody uses it
# uniq TAGS.tm >> TAGS

perl -w014pe 'if (s/^(.*\n)\1+/$1/mg) {		# Remove duplicate lines
		  $chars = chomp;
		  s/^((\n.+,)\d+)/ $2 . (length($_) - length($1) - 1) /e;
		  $_ .= ("\f" x $chars);
	      }' TAGS.tmp > TAGS.tm2

# Since we generate *specific* definitions for the functions in the table,
# they will be prefered by Emacs over non-specific ones for the code, so we
# convert non-specific definitions into specific ones (with \x1):

perl -w014pe 'if (s/^( [^\n\x7F\x01]*\b	# 1:   TAG group
	               (\w+)		#   2: word
		       [^\w\x7F\x01\n]*	#      Most anything
		       \x7F		#      End of description
		     )
		     (\d+,\d+\n)	# 3:   TAGS Trail
		   /$1$2\x01$3/mgx) {	# Add specific marking
		  $chars = chomp;
		  s/^((\n.+,)\d+)/ $2 . (length($_) - length($1) - 1) /e;
		  $_ .= ("\f" x $chars);
	      }' TAGS > TAGS.tm1 && mv TAGS.tm1 TAGS

cat TAGS.tm2 >> TAGS
rm TAGS.tm*