spellsift.tcl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/tclsh
  2. set usage {
  3. Usage: spellsift.tcl <source_filenames>
  4. The named .c and .h source files comment blocks are spell-checked.
  5. }
  6. if {[llength $argv] == 0} {
  7. puts stderr $usage
  8. exit 0
  9. }
  10. # Want a Tcl version with 3-argument close.
  11. package require Tcl 8.6
  12. set ::spellchk "aspell --extra-dicts ./custom.rws list"
  13. # Run text through aspell with custom dictionary, return finds.
  14. proc misspelled {text} {
  15. set spellerr [open "|$::spellchk" r+]
  16. puts $spellerr $text
  17. flush $spellerr
  18. close $spellerr write
  19. set huhq [regsub {\s*$} [read $spellerr] {}]
  20. close $spellerr read
  21. return [split $huhq "\n"]
  22. }
  23. # Eliminate some common patterns that need not be well spelled.
  24. proc decruft {text} {
  25. set nopp [regsub -all "\n *#\[^\n\]*\n" $text "\n\n" ]
  26. set noticket [regsub -all {Ticket \[?[0-9a-f]+\]?} $nopp "" ]
  27. return $noticket
  28. }
  29. # Sift out common variable spellings not in normal dictionaries.
  30. proc varsift {words} {
  31. set rv [list]
  32. foreach w $words {
  33. set n [string length $w]
  34. set cr [string range $w 1 end]
  35. if {[string tolower $cr] ne $cr} continue
  36. lappend rv $w;
  37. }
  38. return $rv
  39. }
  40. foreach fname $argv {
  41. set ich [open $fname r]
  42. set dtext [decruft [read $ich]]
  43. close $ich
  44. set cbounds [regexp -indices -inline -all {(/\*)|(\*/)} $dtext]
  45. set ccb -1
  46. set cblocks [list]
  47. foreach {ap cb ce} $cbounds {
  48. set cib [lindex $cb 1]
  49. set cie [lindex $ce 0]
  50. if {$cie != -1} {
  51. if {$ccb != -1} {
  52. set cce [expr $cie - 1]
  53. set destar [string map [list * " "] [string range $dtext $ccb $cce]]
  54. lappend cblocks $destar
  55. set ccb -1
  56. } else continue
  57. } elseif {$cib != -1} {
  58. set ccb [expr $cib + 1]
  59. }
  60. }
  61. set oddspells [varsift [misspelled [join $cblocks "\n"]]]
  62. if {[llength $oddspells] > 0} {
  63. puts "!? Misspellings from $fname:"
  64. puts [join [lsort -nocase -unique $oddspells] "\n"]
  65. }
  66. }