| 1 |
@rem = '--*-Perl-*-- |
| 2 |
@echo off |
| 3 |
if "%OS%" == "Windows_NT" goto WinNT |
| 4 |
perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9 |
| 5 |
goto endofperl |
| 6 |
:WinNT |
| 7 |
perl -x -S %0 %* |
| 8 |
if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl |
| 9 |
if %errorlevel% == 9009 echo You do not have Perl in your PATH. |
| 10 |
if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul |
| 11 |
goto endofperl |
| 12 |
@rem '; |
| 13 |
#!/usr/bin/perl |
| 14 |
#line 15 |
| 15 |
|
| 16 |
use warnings; |
| 17 |
use Getopt::Std; |
| 18 |
$opt_l = 'kKsSvVzZoOuUiIA'; # defaults for Slovak and Czech |
| 19 |
$opt_c = $opt_f = $opt_h = $opt_v = 0; |
| 20 |
getopts('cfphvl:'); |
| 21 |
|
| 22 |
$version_msg = "findhyph version 3.4\n"; |
| 23 |
$help_msg = "\nUsage: findhyph [options] <yourtexfile.log>\n" . |
| 24 |
"\n" . |
| 25 |
"Options: -c display hyphenated words in context\n" . |
| 26 |
" -f display font selectors\n" . |
| 27 |
" -h display this help message\n" . |
| 28 |
" -v display version\n" . |
| 29 |
" -p generate file with prepositions left at the end of line\n" . |
| 30 |
" -l=STRING use prepositions/conjunctions listed in STRING\n"; |
| 31 |
|
| 32 |
print ($version_msg) if $opt_v; |
| 33 |
die ($help_msg) if $opt_h || !$ARGV[0]; |
| 34 |
$filename = $ARGV[0]; |
| 35 |
if ($filename =~ /\.(log|tex|dvi|ps|pdf)$/) { |
| 36 |
$filename = $`; |
| 37 |
} |
| 38 |
open(IN, "$filename.log") or die ("Can't read $filename.log: $!\n"); |
| 39 |
open(O1, ">$filename.hyph") or die ("Can't write $filename.hyph: $!\n"); |
| 40 |
if ($opt_p) { |
| 41 |
open(O2, ">$filename.prep") or die ("Can't write $filename.prep: $!\n"); |
| 42 |
} |
| 43 |
|
| 44 |
$search = 0; |
| 45 |
$pageno_allowed = 0; |
| 46 |
$write_hyph_page = 0; |
| 47 |
$write_prep_page = 0; |
| 48 |
$act_text = ""; |
| 49 |
$looseness = 0; |
| 50 |
|
| 51 |
while(<IN>) { |
| 52 |
s/[\r\n]*$//; |
| 53 |
if (/^\@looseness=(\-?\d+)$/) { |
| 54 |
$looseness = $1; |
| 55 |
} |
| 56 |
if (/^\@firstpass$/) { |
| 57 |
$pageno_allowed = 0; |
| 58 |
$endgraf = 0; |
| 59 |
$demerits = 1e10; |
| 60 |
$search = 1; |
| 61 |
$search_hyphens = 0; |
| 62 |
$act_break = 0; |
| 63 |
$act_text = ""; |
| 64 |
@USED = (); |
| 65 |
%BREAKS = (); |
| 66 |
%LOOSE = (); |
| 67 |
next; |
| 68 |
} |
| 69 |
if (/^\@secondpass$/) { |
| 70 |
$search_hyphens = 1; |
| 71 |
$act_break = 0; |
| 72 |
$act_text = ""; |
| 73 |
@USED = (); |
| 74 |
%BREAKS = (); |
| 75 |
%LOOSE = (); |
| 76 |
next; |
| 77 |
} |
| 78 |
if (/^\@emergencypass$/) { |
| 79 |
$act_break = 0; |
| 80 |
$act_text = ""; |
| 81 |
@USED = (); |
| 82 |
%BREAKS = (); |
| 83 |
%LOOSE = (); |
| 84 |
next; |
| 85 |
} |
| 86 |
if ($search && /^@@(\d+): line (\d+)\..+ t=(\-?\d+) (?:s=\S+ (?:g|a)=\S+ )?\-\> @@(\d+)/) { |
| 87 |
$act_break = $1; |
| 88 |
$BREAKS{$act_break}{'prev'} = $4; |
| 89 |
if ($endgraf && $3 < $demerits) { |
| 90 |
$max_break = $1; |
| 91 |
$max_line = $2; |
| 92 |
$demerits = $3; |
| 93 |
} |
| 94 |
if ($endgraf && $looseness != 0) { |
| 95 |
$LOOSE{$1}{"line"} = $2; |
| 96 |
$LOOSE{$1}{"demerits"} = $3; |
| 97 |
} |
| 98 |
$act_text .= "\@$act_break\@"; |
| 99 |
next; |
| 100 |
} |
| 101 |
if ($search && /^@\\par via @@/) { |
| 102 |
$endgraf = 1; |
| 103 |
next; |
| 104 |
} |
| 105 |
if ($search && /^@\\discretionary via @@/) { |
| 106 |
$search_hyphens = 1; # @firstpass may contain words hyphenated by user |
| 107 |
next; # using \discretionary |
| 108 |
} |
| 109 |
if ($search && /^@/) { |
| 110 |
next; |
| 111 |
} |
| 112 |
if ($search && $endgraf && $_ eq "") { |
| 113 |
$pageno_allowed = 1; |
| 114 |
do_hyph(); |
| 115 |
$search = 0; |
| 116 |
$looseness = 0; |
| 117 |
next; |
| 118 |
} |
| 119 |
if ($search) { |
| 120 |
$act_text .= $_; |
| 121 |
} else { |
| 122 |
if ($pageno_allowed && /\[(\-?\d+)(\.\-?\d+){0,9} ?(\]|\{|\<|$)/) { # page number |
| 123 |
$pageno = $1; |
| 124 |
$pageno_allowed = 0; |
| 125 |
if ($write_hyph_page) { |
| 126 |
print O1 "[$pageno]\n\n"; |
| 127 |
$write_hyph_page = 0; |
| 128 |
} |
| 129 |
if ($write_prep_page) { |
| 130 |
print O2 "[$pageno]\n\n"; |
| 131 |
$write_prep_page = 0; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
close(O1); |
| 138 |
close(O2) if $opt_p; |
| 139 |
|
| 140 |
sub do_hyph { |
| 141 |
if ($looseness != 0) { |
| 142 |
$diff_line = 1e10; |
| 143 |
$diff_demer = 1e10; |
| 144 |
for $i (keys %LOOSE) { |
| 145 |
if (abs($LOOSE{$i}{"line"} - $max_line - $looseness) < $diff_line) { |
| 146 |
$diff_line = abs($LOOSE{$i}{"line"} - $max_line - $looseness); |
| 147 |
$diff_demer = $LOOSE{$i}{"demerits"}; |
| 148 |
$max_break = $i; |
| 149 |
} |
| 150 |
if (abs($LOOSE{$i}{"line"} - $max_line - $looseness) == $diff_line && |
| 151 |
$LOOSE{$i}{"demerits"} < $diff_demer) { |
| 152 |
$diff_demer = $LOOSE{$i}{"demerits"}; |
| 153 |
$max_break = $i; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
$br = $max_break; |
| 158 |
while (1) { |
| 159 |
$br = $BREAKS{$br}{'prev'}; |
| 160 |
last if ($br == 0); |
| 161 |
$USED[$br] = 1; |
| 162 |
} |
| 163 |
|
| 164 |
for $i (1..$max_break-1) { |
| 165 |
if (not defined $USED[$i]) { |
| 166 |
$act_text =~ s/\@$i\@//; |
| 167 |
}; |
| 168 |
} |
| 169 |
|
| 170 |
if ($opt_f) { |
| 171 |
$act_text =~ s/(\\\S+)\s+/$1\_/g; |
| 172 |
} else { |
| 173 |
$act_text =~ s/\\\S+\s+//g; # usually font selector, but also backslash in the text |
| 174 |
} |
| 175 |
|
| 176 |
for $i (1..$max_break-1) { |
| 177 |
if (defined $USED[$i]) { |
| 178 |
if ($search_hyphens) { # hyphenated words |
| 179 |
if ($act_text =~ /(\S+)(\@$i\@)(\S+)/) { # only hyphenated words match |
| 180 |
$out_pre = $1; |
| 181 |
$out_post = $3; |
| 182 |
if (substr($out_pre,-1) ne "-") { # hyphenated ligatures |
| 183 |
$out_pre_A = substr($out_pre,0,rindex($out_pre,"-")); |
| 184 |
$out_pre_B = substr($out_pre,rindex($out_pre,"-")+1); |
| 185 |
$out_pre = $out_pre_A; |
| 186 |
$out_post = $out_pre_B.$out_post; |
| 187 |
} |
| 188 |
if ($opt_c) { |
| 189 |
if ($act_text =~ /(\S+)\s+\S+\@$i\@/) {$out_pre = "$1 $out_pre";} |
| 190 |
if ($act_text =~ /\@$i\@\S+\s+(\S+)/) {$out_post = "$out_post $1";} |
| 191 |
} |
| 192 |
$out_pre =~ s/-//g; |
| 193 |
$out_post =~ s/-//g; |
| 194 |
$out_text = "$out_pre-$out_post"; |
| 195 |
$out_text =~ s/\@\d+\@//g; # very narrow columns |
| 196 |
print O1 "$out_text\n"; |
| 197 |
$write_hyph_page = 1; |
| 198 |
} |
| 199 |
} |
| 200 |
if ($opt_p) { # prepositions |
| 201 |
if ($act_text =~ / (\S) \@$i\@/) { |
| 202 |
$out_text = $1; |
| 203 |
if ($out_text =~ /^[$opt_l]$/) { |
| 204 |
if ($opt_c) { |
| 205 |
if($act_text =~ /(\S+) \S \@$i\@/) {$out_text = "$1 $out_text";} |
| 206 |
if($act_text =~ /\@$i\@ ?(\S+)/) {$out_text = "$out_text $1";} |
| 207 |
$out_text =~ s/-//g; |
| 208 |
$out_text =~ s/\@\d+\@//g; # very narrow columns |
| 209 |
} |
| 210 |
print O2 "$out_text\n"; |
| 211 |
$write_prep_page = 1; |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
__END__ |
| 220 |
|
| 221 |
=encoding utf8 |
| 222 |
|
| 223 |
=head1 NAME |
| 224 |
|
| 225 |
B<findhyph> – find words hyphenated by TeX in a document |
| 226 |
|
| 227 |
=head1 INSTALLATION |
| 228 |
|
| 229 |
Copy B<findhyph> or B<findhyph.bat> (depending on OS used) to a directory |
| 230 |
included in system PATH. Perl interpreter is required to be in |
| 231 |
C</usr/bin/> for Unix-like systems or in PATH when using B<findhyph.bat>. |
| 232 |
|
| 233 |
=head1 SYNOPSIS |
| 234 |
|
| 235 |
B<findhyph [options]> F<foo.log> |
| 236 |
|
| 237 |
|
| 238 |
=head1 DESCRIPTION |
| 239 |
|
| 240 |
To use this program: |
| 241 |
|
| 242 |
=over 4 |
| 243 |
|
| 244 |
=item 1) |
| 245 |
|
| 246 |
set C<\tracingparagraphs=1> in a TeX document F<foo.tex> and run: |
| 247 |
|
| 248 |
=item 2) |
| 249 |
|
| 250 |
B<tex> F<foo.tex> |
| 251 |
|
| 252 |
=item 3) |
| 253 |
|
| 254 |
B<findhyph [options]> F<foo.log> |
| 255 |
|
| 256 |
=back |
| 257 |
|
| 258 |
If you are setting C<\looseness=E<lt>NE<gt>> to optimize paragraphs, |
| 259 |
you need to pass that information to the file F<foo.log> in a form |
| 260 |
C<@looseness=E<lt>NE<gt>> (on a separate line). You can use a macro like |
| 261 |
C<\def\setlooseness#1{\looseness=#1 \immediate\write-1{@looseness=#1}}> |
| 262 |
for this purpose. If the paragraph contains material in display math mode, |
| 263 |
you need to use the macro in all split parts of the paragraph. |
| 264 |
|
| 265 |
=head1 OPTIONS |
| 266 |
|
| 267 |
=over 4 |
| 268 |
|
| 269 |
=item B<-c> |
| 270 |
|
| 271 |
display hyphenated words in context |
| 272 |
|
| 273 |
=item B<-f> |
| 274 |
|
| 275 |
display font selectors and other strings starting with a backslash character |
| 276 |
|
| 277 |
=item B<-v> |
| 278 |
|
| 279 |
display program version |
| 280 |
|
| 281 |
=item B<-p> |
| 282 |
|
| 283 |
generate file containing information about one-letter prepositions and |
| 284 |
conjunctions left at the end of line |
| 285 |
|
| 286 |
=item B<-l=STRING> |
| 287 |
|
| 288 |
use prepositions/conjunctions listed in STRING instead of default list of |
| 289 |
prepositions and conjunctions C<kKsSvVzZoOuUiIA> used for Slovak and Czech |
| 290 |
language |
| 291 |
|
| 292 |
=back |
| 293 |
|
| 294 |
=head1 OUTPUT FILES |
| 295 |
|
| 296 |
=over 4 |
| 297 |
|
| 298 |
=item F<foo.hyph> |
| 299 |
|
| 300 |
List of hyphenated words. All punctuation characters, parentheses and |
| 301 |
other character immediately preceding or following displayed words are included |
| 302 |
in this list. TeX constructs which are too difficult to display (C<\hbox{}>, |
| 303 |
C<\mark{}> etc.) are shown as C<[]>. Math mode is indicated by C<$> sign. |
| 304 |
|
| 305 |
Page numbers in square brackets refer to LOG file and may occasionally differ |
| 306 |
from the typeset document. The reason is that TeX may need to break more |
| 307 |
paragraphs than it would eventually fit on the page in order to find a page |
| 308 |
break. |
| 309 |
|
| 310 |
Words hyphenated in footnotes are listed before the words hyphenated in the |
| 311 |
paragraph in which the footnote is referenced. |
| 312 |
|
| 313 |
=item F<foo.prep> |
| 314 |
|
| 315 |
List of prepositions if option B<-p> is used. |
| 316 |
|
| 317 |
=back |
| 318 |
|
| 319 |
=head1 HISTORY |
| 320 |
|
| 321 |
=head4 1.0 (2001-04-08) |
| 322 |
|
| 323 |
=over 4 |
| 324 |
|
| 325 |
=item * |
| 326 |
|
| 327 |
public release |
| 328 |
|
| 329 |
=back |
| 330 |
|
| 331 |
=head4 2.0 (2009-08-10) |
| 332 |
|
| 333 |
=over 4 |
| 334 |
|
| 335 |
=item * |
| 336 |
|
| 337 |
fixes in line breaks detection algorithm; support for the third pass of line |
| 338 |
breaking algorithm in TeX (positive C<\emergencystretch>); support for |
| 339 |
discretionary breaks in the first pass |
| 340 |
|
| 341 |
=item * |
| 342 |
|
| 343 |
page number detection improved (recognized negative page numbers, compound page |
| 344 |
numbers when C<\count1> to C<\count9> registers are non-zero |
| 345 |
and C<[nn{mapfile}]>, C<[nnE<lt>pictureE<gt>]> and C<[nnE<lt>newlineE<gt>> |
| 346 |
formats used by pdfTeX; false page number detection should be much more rare) |
| 347 |
|
| 348 |
=item * |
| 349 |
|
| 350 |
configurable list of prepositions and conjunctions |
| 351 |
|
| 352 |
=item * |
| 353 |
|
| 354 |
hyphenated words can be displayed in context |
| 355 |
|
| 356 |
=item * |
| 357 |
|
| 358 |
suggestions and testing by Pavel Stříž |
| 359 |
|
| 360 |
=back |
| 361 |
|
| 362 |
=head4 3.0 (2012-02-01) |
| 363 |
|
| 364 |
=over 4 |
| 365 |
|
| 366 |
=item * |
| 367 |
|
| 368 |
fixed a bug when total paragraph demerits are negative |
| 369 |
|
| 370 |
=item * |
| 371 |
|
| 372 |
C<\looseness> setting is now taken into account (thanks to Karel Horák |
| 373 |
for identifying the issue) |
| 374 |
|
| 375 |
=back |
| 376 |
|
| 377 |
=head4 3.1 (2012-11-03) |
| 378 |
|
| 379 |
=over 4 |
| 380 |
|
| 381 |
=item * |
| 382 |
|
| 383 |
fixed displaying of hyphenated ligatures (bug reported by Karel Horák) |
| 384 |
|
| 385 |
=back |
| 386 |
|
| 387 |
=head4 3.2 (2012-11-21) |
| 388 |
|
| 389 |
=over 4 |
| 390 |
|
| 391 |
=item * |
| 392 |
|
| 393 |
improved example definition of the C<\setlooseness> macro |
| 394 |
(thanks to Karl Berry) |
| 395 |
|
| 396 |
=back |
| 397 |
|
| 398 |
=head4 3.3 (2013-06-18) |
| 399 |
|
| 400 |
=over 4 |
| 401 |
|
| 402 |
=item * |
| 403 |
|
| 404 |
fixed the manual page as suggested by Eric S. Raymond |
| 405 |
|
| 406 |
=back |
| 407 |
|
| 408 |
=head4 3.4 (2015-10-18) |
| 409 |
|
| 410 |
=over 4 |
| 411 |
|
| 412 |
=item * |
| 413 |
|
| 414 |
fixed processing of logs produced using ε-TeX's C<\lastlinefit> option |
| 415 |
(bug reported by Karel Horák) |
| 416 |
|
| 417 |
=item * |
| 418 |
|
| 419 |
fixed cross-platform handling of linebreaks in the input file |
| 420 |
|
| 421 |
=back |
| 422 |
|
| 423 |
=head1 LICENSE |
| 424 |
|
| 425 |
This program is free software; you can redistribute it and/or modify |
| 426 |
it under the terms of the GNU General Public License as published by |
| 427 |
the Free Software Foundation; either version 2 of the License, or |
| 428 |
any later version. |
| 429 |
|
| 430 |
=head1 AUTHOR |
| 431 |
|
| 432 |
Copyright (c) Martin Budaj E<lt>[email protected]<gt> 2000, 2001, 2009, 2012, |
| 433 |
2015 |
| 434 |
|
| 435 |
=cut |
| 436 |
|
| 437 |
__END__ |
| 438 |
:endofperl |