2 # Gadget to take two LaTeX files and produce a third which
3 # has changebars highlighting the difference between them.
7 # Don Ward, Careful Computing (don@careful.co.uk)
9 # v1.1 Feb 93 Amended to use changebar.sty (v3.0) and dvips
10 # v1.2 Aug 95 Added support for LaTeX209/LaTeX2e
11 # Added RCS support to retrive old files
26 TMPDIR=${TMP-/tmp}/$CMD.$$
27 trap 'test $DEBUG = NO && rm -rf $TMPDIR' 0 1 2 3 6 7 13 15
28 mkdir $TMPDIR || { echo "cannot create directory \`$TMPDIR'." >&2; exit 1; }
29 TMPFILE=${TMPDIR}/$CMD.$$
30 SED_CMD_FILE=$TMPFILE.sed
36 $CMD [-hgG] [-d dir] old new [output]
37 default output is stdout
39 $CMD [-hgG] [-d dir] old
40 new file on stdin, output on stdout
42 $CMD [-hgG] -d dir -r rev files
43 old file retrieved using RCS
45 Gadget to take two LaTeX files and produce a third which
46 has changebars highlighting the difference between them.
47 Changebars are inserted for differences after '\begin{document}'.
49 Feature: \`new' can not be named \`-'.
52 -d dir : Write the output to file \`dir/new', if \`new' is given or
54 If \`dir' does not exist, it is created.
55 If \`output' is given, it is discarded.
57 -r rev : If the LaTeX \`files' are kept under control of the
58 Revision Control System RCS, the old files of
59 the revision \`rev' can be retrived.
60 \`rev' is specified using the RCS conventions.
61 This option must be used together with the \`-d dir' option.
62 \`files' must be a nonempty list of files.
64 -h : Print this info text.
65 -g : Print some debugging info.
66 -G : Even more debug info.
68 Version 1.2: August 3. 1995
73 # parse options and arguments
78 while getopts d:r:gGh i $*
84 G ) set -x; DEBUG="YES";;
90 shift `expr $OPTIND - 1`
93 1 ) OLD=$1; NEW="-"; OUT="" ;;
94 2 ) OLD=$1; NEW=$2; OUT="" ;;
95 3 ) OLD=$1; NEW=$2; OUT="$3" ;;
99 # check correct options
102 [ -d $DIR ] || $MKDIR $DIR
107 [ -z "$DIR" ] && usage
128 $CO -p"$REV" -q $NEW > $OLD
131 [ $DEBUG = "YES" ] && echo "OLD=\`$OLD' NEW=\`$NEW' OUT=\`$OUT'"
133 # gather some info about the file
134 # Since we have for sure only the name of the OLD file, ...
135 $GREP "^\\\\begin{document}" $OLD > /dev/null
138 [ $DEBUG = "YES" ] && echo "contains a \\begin{document}"
141 [ $DEBUG = "YES" ] && echo "contains no \\begin{document}"
145 # Method to do the work:
146 # 1 Use diff to get an ed script to go from file1 to file2.
147 # 2 Breath on it a bit (with sed) to insert changebar commands.
148 # 3 Apply modified ed script to produce (nearly) the output.
149 # 4 Use awk to insert the changebars option into the \documentstyle
150 # and to handle changebar commands inside verbatim environments.
151 # 5 Remove changebars before \begin{document} with sed
153 # SED commands to edit ED commands to edit old file
154 $CAT > $SED_CMD_FILE <<\_END_
157 /^[0-9][0-9]*[ac]$/a\
159 /^[0-9][0-9]*,[0-9][0-9]*[ac]$/a\
165 /^[0-9][0-9]*,[0-9][0-9]*d$/a\
171 # note DIFF accepts `-' as stdin
172 $DIFF -b -e $OLD $NEW | \
173 ( $SED -f $SED_CMD_FILE ; echo w ${TMPFILE}.1 ; echo q ) | \
176 # AWK commands to insert Changebars style and to protect
177 # changebar commands in verbatim environments
178 # and to tell what driver is in use; we assume the `dvips' driver
181 BEGIN {kind=""; # we saw now \documentXXX[]{}
185 if (index($0, "changebar") == 0 ) {
186 opts = index($0, "[")
188 printf "%schangebar,%s\n",substr($0,1,opts),substr($0,opts+1)
190 printf "\\documentstyle[changebar]%s\n", substr($0,15)
197 printf "\\usepackage[dvips]{changebar}\n"
200 /\\begin{document}/ {if (kind == "209" ) {print "\\driver{dvips}"}}
201 /\\begin{verbatim}/{++nesting}
202 /\\end{verbatim}/{--nesting}
203 /\\cbstart{}%|\\cbend{}%|\cbdelete{}%/ {
205 # changebar command in a verbatim environment: Temporarily exit,
206 # do the changebar command and reenter.
208 # The obvious ( printf "\\end{verbatim}%s\\begin{verbatim} , $0 )
209 # leaves too much vertical space around the changed line(s).
210 # The following magic seeems to work
212 print "\\end{verbatim}\\nointerlineskip"
213 print "\\vskip -\\ht\\strutbox\\vskip -\\ht\\strutbox"
214 printf "\\vbox to 0pt{\\vskip \\ht\\strutbox%s\\vss}\n", $0
215 print "\\begin{verbatim}"
220 ' ${TMPFILE}.1 > ${TMPFILE}.2
222 # if a \begin{document} is contained in the file,
223 # remove the changebar commands before them
225 if [ $HAS_BEGIN_DOC = "YES" ]
227 SED_CMD="1,/\\\\begin{document}/s/\(\\\\cb[sed][tne][adl][^{}]*{}%\)$/%%\1/"
228 $SED "$SED_CMD" ${TMPFILE}.2 > ${TMPFILE}.3
230 $CAT ${TMPFILE}.2 > ${TMPFILE}.3
236 $MV ${TMPFILE}.3 $OUT
241 [ $DEBUG = "NO" ] && $RM ${TMPFILE}.*
243 ###############################################################