#!/bin/bash # # Syntax: tcheck [-h] [filename [...]] # # Purpose: Checks LaTeX files for the repetitions of words, for words that # occur in the file ~/info/priv/errors.lst, and for words or # parts of words that appear in ~/info/priv/danger_full.lst # and ~/info/priv/danger_part.lst respectively. # If no argument is given, the most recent tex file will be processed. # The -h flag shows the help info for this command. # # Notes: . This command file can easily be edited such that instead # of words also regular expressions are allowed (see below: # remove -F option of grep) # Note however that this will slow down the search. # . If your background is white then replace 1;32 by 0;32 # # Created: Jul 6, 1994 by Bart De Schutter # Last revised: Jan 2, 2011 by Bart De Schutter # # See http://www.deschutter.info/util/scripts.html for the latest version of # this script. # Status: public # Category: latex if [ $# -ge 1 -a "$1" = "-h" ]; then sed '/^$/q;/^#!/d;s/^# //;s/^#//' $0 >&2 exit 1 fi # # Check for existence of awk and presence of lst files. # AWK=$(type -p gawk) if [ -z "$AWK" ]; then AWK=$(type -p nawk) if [ -z "$AWK" ]; then AWK=$(type -p awk) fi fi if [ -z "$AWK" ]; then echo >&2 echo >&2 "WARNING: awk is not installed on this system." echo >&2 " Check for double words will be skipped!" echo >&2 fi ERRORS_LST=~/info/priv/errors.lst DANGER_FULL_LST=~/info/priv/danger_full.lst DANGER_PART_LST=~/info/priv/danger_part.lst if [ ! -e $ERRORS_LST ] then echo >&2 echo >&2 "WARNING: $ERRORS_LST does not exist." echo >&2 " Check for errors will be skipped!" echo >&2 fi if [ ! -e $DANGER_FULL_LST ] || [ ! -e $DANGER_PART_LST ] then echo >&2 echo >&2 "WARNING: $DANGER_FULL_LST or " echo >&2 " $DANGER_PART_LST does not exist." echo >&2 " Check for 'dangerous' words will be skipped!" echo >&2 fi # Perform tcheck declare -a list # Declare list as an array, necessary in order to be able # to deal with files that have a space in their name. if [ $# -eq 0 ]; then texfile=$( (ls -t *.tex | head -1) 2>/dev/null) if [ -n "$texfile" ]; then list=$texfile else echo "No tex files found." fi else list=("$@") fi firsttime=1 for fname in "${list[@]}" do if [ ! -e "$fname" ]; then fname="${fname%.tex}.tex" # Append .tex extension if # necessary. fi if [ $firsttime -eq 0 ]; then echo else firsttime=0 fi echo echo "CHECKING file $fname" echo if [ ! -e "$fname" ] then echo echo "ERROR: File $fname not found." echo else if [ ! -z $AWK ] then echo echo "Checking for repetition of words." echo $AWK ' BEGIN { previous_linenr = 0 previous_line="" previous_word="" previous_fieldnr = 0 } $0 !~ /^[ \t]*%/ { for ( i = 1; i <= NF; i++ ) { if ( $i ~ /^%/ ) next if ( $i !~ /^[a-zA-Z0-9{]/ ) #} { previous_word="" continue } current_word=$i gsub(/{|}|,$|\.$|\?$|!$/,"",current_word) if ( length(current_word) == 0 ) continue if ( current_word == previous_word ) { if ( previous_line != NR ) print_line(previous_linenr,previous_line,previous_fieldnr) print_line(NR,$0,i) printf("\n") } previous_word=current_word previous_fieldnr=i previous_linenr=NR previous_line=$0 } } function print_line(nr,string,fieldnr){ # # Commented out by BDS on Oct 26, 1998: # nwords,words,space,label,defpos,pos,j){ #} # nwords=split(string,words) words[fieldnr]="" words[fieldnr] "" space = "" label = sprintf("Line %d : ",nr); defpos = length(label)+1 printf("%s", label) gsub(/./," ",label) pos = defpos for ( j = 1; j <= nwords; j++ ) if ( pos + length(words[j]) < 81 ) { pos += length(words[j])+1 if ( j == fieldnr ) pos -= 8 printf("%s ", words[j]) } else { pos = defpos printf("\n%s%s ",label,words[j]) } printf("\n"); } END{}' "$fname" fi if [ -e $ERRORS_LST ] then echo echo "Checking for words in ${ERRORS_LST##*/}" echo export GREP_COLOR='1;31' # b;fg(30-39);bf(40-49) # Remove -F if you want to search on regular expressions # also. grep -F --color=always -n -i -f $ERRORS_LST "$fname" fi if [ -e $DANGER_FULL_LST ] && [ -e $DANGER_PART_LST ] then echo echo "Checking for words in ${DANGER_FULL_LST##*/} and"\ "${DANGER_PART_LST##*/}" echo ## ## export GREP_COLOR='1;32' # b;fg(30-39);bf(40-49) ## # Remove -F if you want to search on regular expressions # also. (export GREP_COLOR='1;32';\ grep -F --color=always -n -w -i -f $DANGER_FULL_LST "$fname";\ export GREP_COLOR='1;33';\ grep -F --color=always -n -i -f $DANGER_PART_LST "$fname") \ | sort -n fi fi echo done