|
| 1 | +#!/bin/bash |
| 2 | +########################################################################### |
| 3 | +# chkspelling_ag.sh |
| 4 | +# --------------------- |
| 5 | +# Date : December 2016 |
| 6 | +# Copyright : (C) 2016 by Denis Rouzaud |
| 7 | +# Email : denis.rouzaud@gmail.com |
| 8 | +########################################################################### |
| 9 | +# # |
| 10 | +# This program is free software; you can redistribute it and/or modify # |
| 11 | +# it under the terms of the GNU General Public License as published by # |
| 12 | +# the Free Software Foundation; either version 2 of the License, or # |
| 13 | +# (at your option) any later version. # |
| 14 | +# # |
| 15 | +########################################################################### |
| 16 | + |
| 17 | +# -i: enter interactive mode to fix errors |
| 18 | +# optional argument: list of files to be checked |
| 19 | + |
| 20 | + |
| 21 | +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 22 | + |
| 23 | +AGIGNORE=${DIR}/.agignore |
| 24 | + |
| 25 | +# ARGUMENTS |
| 26 | +INTERACTIVE=YES |
| 27 | +while getopts ":r" opt; do |
| 28 | + case $opt in |
| 29 | + r) |
| 30 | + echo "interactive mode turned off" >&2 |
| 31 | + INTERACTIVE=NO |
| 32 | + ;; |
| 33 | + \?) |
| 34 | + echo "Invalid option: -$OPTARG" >&2 |
| 35 | + exit 1 |
| 36 | + ;; |
| 37 | + esac |
| 38 | +done |
| 39 | +shift $(expr $OPTIND - 1) |
| 40 | + |
| 41 | +if [ ! $# -eq 0 ]; then |
| 42 | + EXCLUDE=$(cat $AGIGNORE | sed -e 's/\s*#.*$//' -e '/^\s*$/d' | tr '\n' '|' | sed -e 's/|$//') |
| 43 | + INPUTFILES=$(echo $@ | tr -s '[[:blank:]]' '\n' | egrep -iv "$EXCLUDE" | tr '\n' ' ' ) |
| 44 | + echo "Running spell check on files: $INPUTFILES" |
| 45 | +else |
| 46 | + INPUTFILES="." |
| 47 | +fi |
| 48 | + |
| 49 | +SPELLOK='(#\s*spellok|<!--#\s*spellok-->)$' |
| 50 | + |
| 51 | +# split into several files to avoid too long regexes |
| 52 | +SPLIT=4 |
| 53 | +GNUPREFIX= |
| 54 | +if [[ "$OSTYPE" =~ darwin* ]]; then |
| 55 | + GNUPREFIX=g |
| 56 | +fi |
| 57 | + |
| 58 | +${GNUPREFIX}split --number=l/$SPLIT --numeric-suffixes=1 --suffix-length=1 --additional-suffix=~ ${DIR}/spelling.dat spelling |
| 59 | + |
| 60 | + |
| 61 | +for ((I=1;I<=$SPLIT;I++)) ; do |
| 62 | + SPELLFILE=spelling$I~; |
| 63 | + |
| 64 | + # This will try to look for mispelling within larger words. |
| 65 | + # Condition is hard to explain in words. |
| 66 | + # You can test it here: https://regex101.com/r/7kznVA/9 |
| 67 | + # extra words that should not be checked in longer words |
| 68 | + WHOLEWORDS=$(echo "("; perl -ne 'print if not /^(\w)(\w)\w{2,}(\w)(\w):(\2\2|\1(?:(?!\1)\w)|(?:(?!\1)\w)\2|(?:(?!\1)\w)(?:(?!\1)\w)|\2\1)\w*(\3\3|(?:(?!\4)\w)(?:(?!\3)\4)|\3(?:(?!\4).)|(?:(?!\4)\w)(?:(?!\4)\w)|\4\3)(?!:\*)$/' $SPELLFILE | cut -d: -f1 | tr '\n' '\|' | sed -e 's/|$//'; echo ")") |
| 69 | + INWORDS=$( echo "("; perl -ne 'print if /^(\w)(\w)\w{2,}(\w)(\w):(\2\2|\1(?:(?!\1)\w)|(?:(?!\1)\w)\2|(?:(?!\1)\w)(?:(?!\1)\w)|\2\1)\w*(\3\3|(?:(?!\4)\w)(?:(?!\3)\4)|\3(?:(?!\4).)|(?:(?!\4)\w)(?:(?!\4)\w)|\4\3)(?!:\*)$/' $SPELLFILE | cut -d: -f1 | tr '\n' '\|' | sed -e 's/|$//'; echo ")") |
| 70 | + |
| 71 | + FILE=$INPUTFILES # init with input files (if ag is run with single file, file path is now in output) |
| 72 | + COMMANDS="" |
| 73 | + ERRORFOUND=NO |
| 74 | + while read -u 3 -r LINE; do |
| 75 | + echo "$LINE" |
| 76 | + ERRORFOUND=YES |
| 77 | + if [[ "$INTERACTIVE" =~ YES ]]; then |
| 78 | + NOCOLOR=$(echo "$LINE" | ${GNUPREFIX}sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g') |
| 79 | + if [[ "$NOCOLOR" =~ ^[[:alnum:]][[:alnum:]\/\._-]+$ ]]; then |
| 80 | + FILE=$NOCOLOR |
| 81 | + fi |
| 82 | + if [[ "$NOCOLOR" =~ ^[0-9]+: ]]; then |
| 83 | + if [[ -z $FILE ]]; then |
| 84 | + echo "Error: no file" |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + NUMBER=$(echo "$NOCOLOR" | cut -d: -f1) |
| 88 | + ERROR=$(echo "$LINE" | ${GNUPREFIX}sed -r 's/^.*\x1B\[30;43m(.*?)\x1B\[0m.*$/\1/') |
| 89 | + CORRECTION=$(ag --nonumbers --ignore-case --word-regexp "$ERROR" ${DIR}/spelling.dat | cut -d: -f2) |
| 90 | + |
| 91 | + SPELLOKSTR='//#spellok' |
| 92 | + if [[ "$FILE" =~ \.(txt|html|htm)$ ]]; then |
| 93 | + SPELLOKSTR='<!--#spellok-->' |
| 94 | + fi |
| 95 | + if [[ "$FILE" =~ \.(h|cpp|sip)$ ]]; then |
| 96 | + if [[ "$NOCOLOR" =~ ^\s*(\/*)|(\/\/) ]]; then |
| 97 | + SPELLOKSTR='#spellok' |
| 98 | + fi |
| 99 | + fi |
| 100 | + if [[ "$FILE" =~ \.(py)$ ]]; then |
| 101 | + SPELLOKSTR='#spellok' |
| 102 | + fi |
| 103 | + |
| 104 | + echo "" |
| 105 | + echo -e " \x1B[4mr\x1B[0meplace by \x1B[33m$CORRECTION\x1B[0m" |
| 106 | + echo -e " \x1B[4ma\x1B[0mppend \x1B[33m$SPELLOKSTR\x1B[0m at the end of the line to avoid spell check on this line" |
| 107 | + echo -e " en\x1B[4mt\x1B[0mer your own correction" |
| 108 | + echo -e " ignore and \x1B[4mc\x1B[0montinue" |
| 109 | + echo -e " ignore and \x1B[4me\x1B[0mxit" |
| 110 | + |
| 111 | + while read -n 1 n; do |
| 112 | + echo "" |
| 113 | + case $n in |
| 114 | + r) |
| 115 | + MATCHCASE="$ERROR:$CORRECTION" |
| 116 | + CORRECTIONCASE=$(echo "$MATCHCASE" | ${GNUPREFIX}sed -r 's/([A-Z]+):(.*)/\U\2/;s/([A-Z][a-z]+):([a-z])/\U\2\L/') |
| 117 | + echo -e "replacing \x1B[33m$ERROR\x1B[0m by \x1B[33m$CORRECTION\x1B[0m in \x1B[33m$FILE\x1B[0m at line \x1B[33m$NUMBER\x1B[0m" |
| 118 | + ${GNUPREFIX}sed -i "${NUMBER}s/$ERROR/$CORRECTION/g" $FILE |
| 119 | + break |
| 120 | + ;; |
| 121 | + a) |
| 122 | + echo -e "appending \x1B[33m$SPELLOKSTR\x1B[0m to \x1B[33m$FILE\x1B[0m at line \x1B[33m$NUMBER\x1B[0m" |
| 123 | + SPELLOKSTR=$(echo "$SPELLOKSTR" | ${GNUPREFIX}sed -r 's/\//\\\//g') |
| 124 | + ${GNUPREFIX}sed -i "${NUMBER}s/\$/ $SPELLOKSTR/" $FILE |
| 125 | + break |
| 126 | + ;; |
| 127 | + t) |
| 128 | + echo "Enter the correction: " |
| 129 | + read CORRECTION |
| 130 | + MATCHCASE="$ERROR:$CORRECTION" |
| 131 | + CORRECTIONCASE=$(echo "$MATCHCASE" | ${GNUPREFIX}sed -r 's/([A-Z]+):(.*)/\U\2/;s/([A-Z][a-z]+):([a-z])/\U\2\L/') |
| 132 | + echo -e "replacing \x1B[33m$ERROR\x1B[0m by \x1B[33m$CORRECTION\x1B[0m in \x1B[33m$FILE\x1B[0m at line \x1B[33m$NUMBER\x1B[0m" sed -i "${NUMBER}s/$ERROR/$CORRECTION/g" $FILE |
| 133 | + break |
| 134 | + ;; |
| 135 | + c) |
| 136 | + break |
| 137 | + ;; |
| 138 | + e) |
| 139 | + exit 1 |
| 140 | + ;; |
| 141 | + *) invalid option;; |
| 142 | + esac |
| 143 | + done |
| 144 | + |
| 145 | + fi |
| 146 | + if [[ "$NOCOLOR" =~ ^\s*$ ]]; then |
| 147 | + FILE="" |
| 148 | + fi |
| 149 | + fi |
| 150 | + done 3< <(unbuffer ag --smart-case --all-text --nopager --color-match "30;43" --numbers --nomultiline --word-regexp -p $AGIGNORE "${WHOLEWORDS}"'(?!.*'"${SPELLOK}"')' $INPUTFILES ; \ |
| 151 | + unbuffer ag --smart-case --all-text --nopager --color-match "30;43" --numbers --nomultiline -p $AGIGNORE "${INWORDS}"'(?!.*'"${SPELLOK}"')' $INPUTFILES ) |
| 152 | + |
| 153 | + rm $SPELLFILE |
| 154 | + |
| 155 | + if [[ "$ERRORFOUND" =~ YES ]]; then |
| 156 | + echo -e "\x1B[1msome errors have been found.\x1B[0m" |
| 157 | + exit 1 |
| 158 | + else |
| 159 | + exit 0 |
| 160 | + fi |
| 161 | +done |
| 162 | +exit |
0 commit comments