<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/local/bin/perl

# Perl/Tk version of pretty
# Julius C. Duque
# v1.1.1 2003 November 12 Wednesday
# v1.2.0 2003 November 14 Friday - Added hyphenation
# v1.3.0 2003 November 16 Sunday - Added hyphenation of overfull lines

use diagnostics;
use strict;
use warnings;
use Tk;
use Tk::Balloon;
use TeX::Hyphen;

my $INDENT_DEF = 0;
my $LWIDTH_DEF = 70;
my $VERSION = "1.3.0";
my $TITLE = "Paragraph Adjuster $VERSION";
my $AUTHOR = "Julius C. Duque";
my $indent = $INDENT_DEF;
my $newline = 1;
my $hyphenate = 1;
my $width = $LWIDTH_DEF;
my ($BOTH, $LEFT, $RIGHT, $CENTERED) = (1, 2, 3, 4);
my $format_choice = $BOTH;   # both left- and right-justified
my ($infile, $outfile) = ();

my $hyp = new TeX::Hyphen;

local $/ = "";    # paragraph mode

my $mw = new MainWindow();
drawButtons();
Tk::MainLoop();

sub processfile
{
  my $retval = 0;

  open INFILE, $infile;
  if (open OUTFILE, "&gt; $outfile") {
    while (&lt;INFILE&gt;) {
      my @linein = split;
      $retval = printpar(@linein);
      last if ($retval);
      print OUTFILE "\n" if ($newline);
    }

    close INFILE;
    close OUTFILE;
  }

  printMessage("info", "OK", "File was successfully saved.")
    if (!$retval);
}

sub printpar
{
  my (@par) = @_;
  my $firstline = 0;

  while (@par) {
    $firstline++;
    my ($buffer, $word);
    my ($charcount, $wordlen) = (0, 0);
    my $linewidth = $width;

    if ($firstline == 1) {
      $linewidth -= $indent;
      print OUTFILE " " x $indent;
    }

    while (($charcount &lt; $linewidth) and (@par)) {
      $word = shift @par;
      $buffer .= $word;
      $wordlen = length($word);
      $charcount += $wordlen;
      $buffer .= " ";
      $charcount++;
    }

    chop $buffer;
    $charcount--;

    if ($charcount == $wordlen) {
      $linewidth = $wordlen;
      my ($pos, $pre_word_len) = (0, 0);
      if ($hyphenate) {
        if ($word =~ /^([^a-zA-Z]*)([a-zA-Z-']+)([^a-zA-Z]*)$/) {
          my $pre_word = $1;
          $pre_word_len = length($pre_word);
          my $stripped_word = $2;
          $pos = hyphenate_word($stripped_word, $width);
          $pos = 0 if ($wordlen &lt;= $width);
        }

        if ($pos) {
          $charcount = $pre_word_len + $pos;
          my $post_word = substr $word, $charcount;
          unshift(@par, $post_word);
          $buffer = substr $word, 0, $charcount;
          $buffer .= "-";
          $charcount++;
        }
      }
    }

    my $lineout = $buffer;

    if ($charcount &gt; $linewidth) {
      my ($pos, $pre_word_len) = (0, 0);
      if ($hyphenate) {
        if ($word =~ /^([^a-zA-Z]*)([a-zA-Z-']+)([^a-zA-Z]*)$/) {
          my $pre_word = $1;
          $pre_word_len = length($pre_word);
          my $stripped_word = $2;
          my $unfilled = $linewidth - $charcount + $wordlen
            - $pre_word_len + 1;

          $pos = hyphenate_word($stripped_word, $unfilled);
        }
      }

      $charcount -= $wordlen;

      if ($pos == 0) {
        $charcount--;
        unshift(@par, $word);
      } else {
        my $post_word = substr $word, ($pre_word_len + $pos);
        unshift(@par, $post_word);
        $charcount = $charcount + $pre_word_len + $pos;
      }

      $lineout = substr $buffer, 0, $charcount;

      if ($pos) {
        $lineout .= "-";
        $charcount++;
      }
    }

    my $spaces_to_fill = $linewidth - $charcount;

    if ($format_choice == $CENTERED) {
      my $leftfill = int($spaces_to_fill/2);
      print OUTFILE " " x $leftfill;
    } elsif ($format_choice == $RIGHT) {
      print OUTFILE " " x $spaces_to_fill;
    } elsif ($format_choice == $BOTH) {
      my $tempbuf = $lineout;
      my $replacements_made = 0;

      if (@par) {
        my $reps = 1;

        while (length($tempbuf) &lt; $linewidth) {
          last if ($tempbuf !~ /\s/);
          if ($tempbuf =~ /(\S+ {$reps})(\S+)/) {
            $tempbuf =~ s/(\S+ {$reps})(\S+)/$1 $2/;
            $replacements_made++;
            $tempbuf = reverse $tempbuf;
          } else {
            $reps++;
          }
        }
      }

      if ($replacements_made % 2 == 0) {
        $lineout = $tempbuf;
      } else {
        $lineout = reverse $tempbuf;
      }
    }

    print OUTFILE "$lineout\n";
  }
}

sub hyphenate_word
{
  my ($tword, $unfilled) = @_;
  my @hyphen_places = $hyp-&gt;hyphenate($tword);

  if (@hyphen_places) {
    @hyphen_places = reverse @hyphen_places;

    foreach my $places (@hyphen_places) {
      return $places if ($places &lt; $unfilled - 1);
    }
  }

  return 0;
}

sub drawButtons
{
  $mw-&gt;title($TITLE);

  # Status bar widget
  my $status = $mw-&gt;Label(-width =&gt; 70, -relief =&gt; "sunken",
    -anchor =&gt; "w")-&gt;pack(-side =&gt; "bottom", -padx =&gt; 1, -pady =&gt; 1,
    -fill =&gt; "x");

  # Create balloon widget
  my $b = $mw-&gt;Balloon(-statusbar =&gt; $status);

  # Create menu bar frame
  my $menubar = $mw-&gt;Frame(-borderwidth =&gt; 4, -relief =&gt; "ridge")-&gt;
    pack(-side =&gt; "top", -fill =&gt; "x"); 

  # Create Open File button
  my $openfilebutton = $menubar-&gt;
    Button(-text =&gt; "Open File", -relief =&gt; "raised", -width =&gt; 10,
      -command =&gt; [\&amp;fileDialog, $mw, "open"])-&gt;pack(-side =&gt; "left");

  $b-&gt;attach($openfilebutton, -msg =&gt; "Open a file to be reformatted");

  # Create Save File button
  my $savefilebutton = $menubar-&gt;
    Button(-text =&gt; "Save To File", -relief =&gt; "raised", -width =&gt; 10,
      -command =&gt; sub {
          if (defined $infile and $infile ne "") {
            fileDialog($mw, "save");
          } else {
              printMessage("warning", "OK",
                "You must open a file to reformat first.");
          }
        })-&gt;pack(-side =&gt; "left");

  $b-&gt;attach($savefilebutton,
    -msg =&gt; "Proceed with reformatting and save result to a file");

  # Create About button
  my $aboutbutton = $menubar-&gt;Button(-text =&gt; "About",
    -relief =&gt; "raised", -width =&gt; 10,
    -command =&gt; [\&amp;printMessage, "info", "OK",
      "A Perl/Tk script created by $AUTHOR"])-&gt;pack(-side =&gt; "left");

  $b-&gt;attach($aboutbutton,
    -msg =&gt; "$TITLE created by $AUTHOR");

  # Create Quit button
  my $quitbutton = $menubar-&gt;Button(-text =&gt; "Dismiss",
    -relief =&gt; "raised", -width =&gt; 10, -command =&gt; sub { exit })-&gt;
    pack(-side =&gt; "right");

  $b-&gt;attach($quitbutton, -msg =&gt; "Quit this Perl/Tk script");

  my $both = $mw-&gt;Radiobutton(-variable =&gt; \$format_choice,
    -value =&gt; $BOTH, -text =&gt; "Both left- and right-justified")-&gt;
    pack(-side =&gt; "top", -anchor =&gt; "w");

  $b-&gt;attach($both, -msg =&gt; "Each line is left- and right-justified");

  my $left = $mw-&gt;Radiobutton(-variable =&gt; \$format_choice,
    -value =&gt; $LEFT, -text =&gt; "Left-justified")-&gt;pack(-side =&gt; "top",
    -anchor =&gt; "w");

  $b-&gt;attach($left, -msg =&gt; "Each line is left-justified, ragged-right");

  my $right = $mw-&gt;Radiobutton(-variable =&gt; \$format_choice,
    -value =&gt; $RIGHT, -text =&gt; "Right-justified")-&gt;pack(-side =&gt; "top",
    -anchor =&gt; "w");

  $b-&gt;attach($right, -msg =&gt; "Each line is right-justified, ragged-left");

  my $centered = $mw-&gt;Radiobutton(-variable =&gt; \$format_choice,
    -value =&gt; $CENTERED, -text =&gt; "Centered")-&gt;pack(-side =&gt; "top",
    -anchor =&gt; "w");

  $b-&gt;attach($centered, -msg =&gt;
    "Each line is equidistant from the left and right margins");

  $both-&gt;select;   # Set default to $both

  my $chknewline = $mw-&gt;Checkbutton(-variable =&gt; \$newline,
    -text =&gt; "Insert empty lines between paragraphs")-&gt;
    pack(-side =&gt; "top", -anchor =&gt; "w");

  $b-&gt;attach($chknewline,
    -msg =&gt; "Insert a blank line between two consecutive paragraphs");

  $chknewline-&gt;select;   # Set default to $newline

  my $chkhyphen = $mw-&gt;Checkbutton(-variable =&gt; \$hyphenate,
    -text =&gt; "Hyphenate")-&gt;
    pack(-side =&gt; "top", -anchor =&gt; "w");

  $b-&gt;attach($chkhyphen,
    -msg =&gt; "Hyphenate word that does not fit on a line");

  $chknewline-&gt;select;   # Set default to $hyphenate

  my $f = $mw-&gt;Frame-&gt;pack(-side =&gt; "left");

  my $l = $f-&gt;Label(-text =&gt; "Indention: ", -justify =&gt; "left");

  $b-&gt;attach($l,
    -msg =&gt; "Number of spaces at the start of every paragraph");

  Tk::grid($l, -row =&gt; 0, -column =&gt; 0);

  my $tindent = $f-&gt;Entry(-width =&gt; 2, -textvariable =&gt; \$indent,
    -justify =&gt; "right");

  $b-&gt;attach($tindent,
    -msg =&gt; "Number of spaces at the start of every paragraph");

  Tk::grid($tindent, -row =&gt; 0, -column =&gt; 1);

  $l = $f-&gt;Label(-text =&gt; "characters (default: $INDENT_DEF) ",
    -justify =&gt; "left");

  $b-&gt;attach($l,
    -msg =&gt; "Number of spaces at the beginning of each paragraph");

  Tk::grid($l, -row =&gt; 0, -column =&gt; 2);

  $l = $f-&gt;Label(-text =&gt; "Line width: ", -justify =&gt; "left");
  Tk::grid($l, -row =&gt; 1, -column =&gt; 0);
  $b-&gt;attach($l, -msg =&gt; "Maximum length of every line");

  my $tlwidth = $f-&gt;Entry(-width =&gt; 2, -textvariable =&gt; \$width,
    -justify =&gt; "right");

  $b-&gt;attach($tlwidth, -msg =&gt; "Maximum length of every line");
  Tk::grid($tlwidth, -row =&gt; 1, -column =&gt; 1);

  $l = $f-&gt;Label(-text =&gt; "characters (default: $LWIDTH_DEF)",
    -justify =&gt; "left");

  $b-&gt;attach($l, -msg =&gt; "Maximum length of every line");
  Tk::grid($l, -row =&gt; 1, -column =&gt; 2);
}

sub printMessage
{
  my ($icon, $type, $outputmsg) = @_;
  my $msg = $mw-&gt;messageBox(-icon =&gt; $icon, -type =&gt; $type,
  -title =&gt; $TITLE, -message =&gt; $outputmsg);
}

sub fileDialog {
  my ($w, $operation) = @_;
  my @types = (["Text files", [qw/.txt .doc/]],
    ["Text files", "", "TEXT"],
    ["All files", "*"]
  );

  if ($operation eq "open") {
    $infile = $w-&gt;getOpenFile(-filetypes =&gt; \@types);
  }

  if ($operation eq "save") {
      $outfile = $w-&gt;getSaveFile(-filetypes =&gt; \@types,
        -initialfile =&gt; "Untitled",
        -defaultextension =&gt; ".txt");

    processfile() if (defined $outfile and $outfile ne "");
  }
}

=head1 NAME

paradj-tk - a small Perl script that reformats lines of ASCII text so that
the resulting lines are  justified in any of the following formats:
left-justified, right-justified, centered, or both left- and
right-justified (default).

=head1 README

Paragraph Adjuster with Hyphenation (PAwH) is a small Perl script that
reformats lines of ASCII text so that the resulting lines are justified
in  any  of  the following  formats:  left-justified, right-justified,
centered, or both left- and right-justified (default). PAwH has
various  switches, most are optional, to control its output. The  only
mandatory switch is the line width. For PAwH to work properly, input
paragraphs must be separated by blank lines.

PAwH is also capable of hyphenating a word that cannot be accommodated
on a line.

=head1 DESCRIPTION

Paragraph Adjuster with Hyphenation (PAwH) is a small Perl script that
reformats lines of ASCII text so that the resulting lines are justified
in  any  of  the following  formats:  left-justified, right-justified,
centered, or both left- and right-justified (default). PAwH has
various  switches, most are optional, to control its output. The  only
mandatory switch is the line width. For PAwH to work properly, input
paragraphs must be separated by blank lines.

PAwH is also capable of hyphenating a word that cannot be accommodated
on a line.

=head1 COMMAND-LINE VERSION

This is a Perl/Tk version of the command-line paradj.pl.

=head1 PREREQUISITE

You  need Jan Pazdziora's Perl module, TeX::Hyphen, available from the
Comprehensive Perl Archive Network (CPAN), to use the hyphenation
feature. The latest is version 0.140.

For  Windows  users,  you can install TeX::Hyphen by  following  these
steps:

1. Uncompress the TeX::Hyphen module, TeX-Hyphen-0.140.tar.gz.

2.  Descend (cd) into the TeX-Hyphen-0.140/lib and copy the TeX directory
into &lt;Perl directory&gt;\lib. For example, if your Perl binaries are
installed on E:\Perl, copy the TeX directory into E:\Perl\lib.

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2003 Julius C. Duque &lt;{jcduque}{at}{lycos}{dot}{com}&gt;

This  library is free software; you can redistribute it and/or  modify
it under the same terms as the GNU General Public License.

=pod SCRIPT CATEGORIES

CPAN/Administrative
Fun/Educational

=cut

</pre></body></html>