From: bizenya@coins.is.tsukuba.ac.jp (Katsujiroh NAKANO)
Newsgroups: fj.lang.perl
Subject: jfmt [Re: jcode.pl]
Date: 21 Nov 92 11:56:37 GMT
Distribution: fj
Organization: Coll. Of Info. Sci., U. of Tsukuba, Tsukuba-City, Ibaraki, Japan

>>>>> In <9227@coins.is.tsukuba.ac.jp>,
>>>>>     bizenya@coins.is.tsukuba.ac.jp == $BCfLn(B said ....
$BCfLn(B> $BCfLn(B@$BC^GHBg3X>pJs$G$9(B. 

$BCfLn(B> $B$$$d$"(B, $BD>@\$$$?$@$$$?$N$K(B,  report $B$b$7$J$$$G?=$7Lu$J$$$G(B
$BCfLn(B> $B$9(B. $B$H$j$"$($:(B, $B<j;}$A$N<+J,$N:n$C$?(B jperl + kconv$BHG$N$b$N(B
$BCfLn(B> $B$r(B jcode $B$G=q$-D>$7$F$_$^$7$?(B.

    $B$H$j$"$($:=P$7$^$9(B. 
    $BF|K\8lHG(B fmt + $B&A$N(B jfmt $B$G$9(B. 
    $B%P%0$^$@$$$C$Q$$$"$k$H;W$$$^$9$,(B, 
    $B3'$5$s$H0l=o$KH/E8$7$F$$$-$?$$$H;W$$$^$9$N$G(B, 
    $B$h$m$7$/$*4j$$$7$^$9(B 8-}

    README + source$B$G$9(B. 

-- cut here -- "README"

	jfmt -- $BF|K\8l(Bformatter

    { > $B$G;O$^$k9T$O0zMQ$G:8C<$G$"$k!	(B}
    { $BNc$K;H$C$F$$$k(Bjfmt$B$N(Bwidth$B$O(B40	}

1. $B35MW(B
    $BF|K\8l$r@5$7$/07$&(B fmt clone $B$,M_$7$$$H;W$C$F:n$j$^$7$?(B. 
    $B1Q8lHG(Bfmt$B$H0c$&E@$O!"EvA3F|K\8l$r@5$7$/07$&$H$$$&$3$H$NB>$K!"4JC1(B
$B$J(Bformatting$B$b9T$J$$$^$9!#Nc$r<($7$^$9!#(B

	$B!&(B $itemhead $B$G;O$^$k9T$O2U>r=q$N9T$N;O$^$j$H$_$J$7$F!"$9$3$7(B
          indent$B$r$D$1$F!"5M$a$k!#(B

	    before:
	    >    $B!&(B $B:#F|$N@$3&>p@*$OHs>o$KIT0BDj$G!"C/$K$b9T$/Kv$,$o$+$i$J$$!#(B

	    +--------------------------------------+
	    after:
	    >    $B!&(B $B:#F|$N@$3&>p@*$OHs>o$KIT0BDj$G!"(B
	    >      $BC/$K$b9T$/Kv$,$o$+$i$J$$!#(B

	$B!&(B /^\s+>/ $B$G;O$^$k9T$O0zMQ$H$_$J$7$F!"(Bindentation$B$rJQ$($J$$!#(B

	    before:
	    >    $B!&(B $BJ?2>L>$GF~NO$9$k$N$K$O<!$N$h$&$K(B
	    >
	    >        > % dict -k yanbarukuina
	    >
	    >$B$HBG$C$F2<$5$$!#(B

	    +--------------------------------------+
	    after:
	    >    $B!&(B $BJ?2>L>$GF~NO$9$k$N$K$O<!$N$h$&$K(B
	    >
	    >        > % dict -k yanbarukuina
	    >
	    >      $B$HBG$C$F2<$5$$!#(B

2. install

    jcode.pl $B$H(B perl $B$,I,MW$G$9(B. $B@hF,$N(B perl $B$N(B path $B$@$1JQ99$7$F(B, $B$=(B
$B$N$^$^$*;H$$2<$5$$(B.

3. customize

    $B<!$NJQ?t$,(B, $B$=$l$>$l$N%Q%?!<%s$rI=$7$^$9(B. 

    $B!&(B [$postkinsoku]	$B9TKv$KMh$k$Y$-$G$J$$J8;z(B. 
    $B!&(B [$prekinsoku]	$B9TF,$KMh$k$Y$-$G$J$$J8;z(B. 
    $B!&(B [$itemhead]	$B0zMQ$N@hF,$H$_$J$9(B pattern. 
    $B!&(B [$untermword]	$B9T$r1[$($FM_$7$/$J$$(B pattern $B$r9=@.$9$kJ8;z(B. 

    $B$^$?(B, 

4. options

    -N	N $B$,B7$($kI}$G$9(B. -w N $B$G$bF1$8$$$_$K$J$j$^$9(B. 
    -o	$B=PNO%3!<%I$G$9(B. 'jis', 'euc', 'sjis'$B$NCf$+$i$*A*$S2<$5$$(B. 

5. bugs

    $B2?$,(B bug $B$+$C$F(B, $B$=$j$cCY$/$F$?$^$s$J$$$3$H$K$D$-$^$9(B. 
    $B$=$N$&$A(B C $B$G=q$-D>$7$^$9$M(B. 

-- cut here -- "jfmt"
#!/usr/local/bin/perl

#
# jfmt - Japanese fmt
#
# usage: jfmt [-(w|width) N] [-joutcode JCODE] [filename ...]
#
$version = 'release 0 revision 0.1';
#

require 'jcode.pl';

# inner kanji code for perl

$icode = 'euc';

# standard values

$tabstop = 8;

$w = 70;

$ocode = 'jis';

$SRC = <<'_END_KANJI_BLOCK_';

$postkinsoku = '(\?|\>|\)|\}|\]|$B$C(B|$B$c(B|$B$e(B|$B$g(B|$B$!(B|$B%!(B|$B$#(B|$B%#(B|$B$%(B|$B%%(B|$B$'(B|$B%'(B|$B$)(B|$B%)(B|$B$C(B|$B!<(B|$B!#(B|$B!"(B|$B!W(B|$B!K(B|$B![(B|$B!M(B|$B!O(B|$B!Q(B|$B!S(B|$B!U(B|\,|\.|\')';
$prekinsoku = '($B!V(B|$B!J(B|$B!N(B|$B!P(B|$B!R(B|$B!T(B|$B!L(B|\[|\(|\<|\{|\`)';
$itemhead = '($B!z(B|$B!y(B|$B!v(B|$B!{(B|$B!}(B|$B!|(B|$B""(B|$B"#(B|-|$B!&(B|#|\*|\(?[0-9]+[.)])';
$untermword = '[-a-zA-Z0-9\026]';

_END_KANJI_BLOCK_
$sp = '(\026*\025| )';

&jcode'convert(*SRC, $icode);
eval $SRC || die "KANJI BLOCK:".$!."\n";

# user customise values

$ENV{'JFMT'} && eval $ENV{'JFMT'};

# parse command line option

while ($ARGV[0] =~ /^-/) {
    $_ = shift;
    if (/^-(width|w)$/) {$w = shift;}
    elsif (/^-(\d+)$/) {$w = $1;}
    elsif (/^-v/) {warn $version."\n";exit(0);}
    elsif (/^-ocode$/) {
	$joutcode = shift;
	if (!/^(jis|euc|sjis)$/){
	    warn "jfmt: no such kanji code '$joutcode'\n";
	    exit(1);
	}
    }
    else {
	print STDERR 'jfmt: \'$_\', no such option.';
	exit(1);
    }
}

#
# main routine
#

$k = 1;
$indent = '';
$line = $buf = '';

while (<>) {
    &wraping(*_);
}
&flush;

sub wraping {
    local(*_) = @_;
    chop;
    &jcode'convert(*_, $icode);
    if (/\t/) {&untabify(*_);}
    if (/^(>>>>>|${sp}+>|[^\s]+> )/){
	$line .= $buf;
	&flush;
	$line = "$_";
	&flush;
	print "\n";
	$quote = 1;
	next;
    }
    if (/^${sp}*$/) {
	if ($quote) {$indent = $recindent;}
	else {$recindent = $indent; $indent = '';}
	&flush;
	print "\n";
	$quote = 0;
	next;
    }
    if (/^(${sp}+)([^\025 ].*)$/) {
	$line .= $buf;
	$buf = '';
	$_ = $+;
	$ws = $1;
	if (length($indent) != length($ws)){
	    &flush;
	    $line = $ws;
	    if (/^$itemhead/) {
		$indent = ' ' x (length($ws) + 2);
	    }
	    else {$indent = '';}
	}
	#else {$line =~ s/^${indent}//;}
    }

    if (/($untermword|[.,)(])$/) {
	$_ .= ' ';
    }

    foreach (/[\200-\377]?./g) {
	$k = 1;
	if ($_ =~ /\s/) {
	    if (length($buffer.$i.$_) >= $w) {
		$line .= $buf;
		&flush;
	    }
	    else {
		$line .= $buf . $_;
	    }
	    $buf = '';
	    next;
	}

	if (/$prekinsoku/ 
	    && length($line.$buf.$_) >= ($w - 2)) {
	    $line .= $buf;
	    $buf = '';
	    &flush;
	    $line .= $_;
	    next;
	}
	    
	if (/$postkinsoku/) {
	    $line .= $buf . $_;
	    $buf = '';
	    next;
	}

	if (/$untermword/) {
	    $buf .= $_;
	    if(length($line.$buf) >= $w) {
		&flush;
	    }
	    next;
	}

	# otherwise ...

	if (length($line.$buf.$_) >= $w) {
	    &flush;
	}
	$line .= $buf . $_;
	$buf = '';
    }
}

sub untabify {
    local(*_) = @_;
    s/^([^\t]*)\t/$1 . 
	    ("\026" x ($tabstop - (length($1) % $tabstop) - 1)). "\025"/eg;
}

sub tabify {
    local(*_) = @_;
    s/[^026]\026*\025/\t/g;
}

sub flush {
    if ($line =~ /^${sp}+$/) {return;}
    $line =~ s/([^\026 ])[\026 ]+$/$1/;
    if ($line) {
	local($l) = $line;
	&tabify(*l);
	&jcode'convert(*l, $ocode, $icode);
	if ($k) {print $l."\n";}
	else {print $l;}
	$k = 0;
    }
    $line = $indent;
}
Newsgroups: fj.lang.perl,fj.lang.postscript