Jump to content

Word wrap: Difference between revisions

m
→‎{{header|REXX}}: replaced with a shorter program, changed the way "JUSTIFY" is specified. -- ~~~~
(added ocaml)
m (→‎{{header|REXX}}: replaced with a shorter program, changed the way "JUSTIFY" is specified. -- ~~~~)
Line 368:
<br>No hyphenation (or de-hyphenation) is attempted.
<br>Words longer than the width of the output are acceptable and are shown, a simple change could be made to issue a notification.
<br>If the specified width is negative, then the output is shown justified to both margins (with the absolute value of width).
<lang rexx>
/*REXX (Classic) pgm reads a file and displays itjustifies (withby wordwords) wrapa tostring theof words──>screen). */
 
arg justify width . /*───────JUSTIFY─────────────*/
parse arg width fileID .; if fileID='' then fileID='LAWS.TXT'
call tell /*handleCenter: any residual words.<centered> */
if width=='' then width=linesize()%2; if width==0 then width=40
/*Both: <──both margins──> */
if \datatype(width,'W') then call err "WIDTH (1st arg) isn't an integer."
/*Right: ─────>right margin */
txt=; $=; justify=width<0; width=abs(width)
/*Left: left margin<─────- */
/*─────pick one of the above.*/
 
just=left(justify,1) /*only use 1st capital letter*/
do j=0 while lines(fileID)\==0 /*read from the file until E-O-F.*/
txt=txt linein(fileID)
end
 
if width=='' then width=linesize()%2; if width==0/*null? then width=40Then pick a default.*/
txt=space(txt) /*eliminate superflous blanks. */
if jwidth==0 then width=40 then call err 'file' fileID " /*not founddeterminable? Use 40."*/
if txt=='' then call err 'file' fileID "is empty."
 
txt="Diplomacy is the art of saying 'Nice Doggy' until",
/*─────────────────────────────────────where the rubber meets the road. */
"you can find a rock. ─── Will Rodgers"
do k=1 for words(txt)
x=word(txt,k)
_=$ x
if length(_)>width then call tell /*this word exceeded linewidth ? */
$=_
end
 
/*─────────────────────────────────────where theda rubber meets theda road. */
call tell /*handle any residual words. */
$='' /*this is where the money is.*/
exit
 
do k=1 for words(txt) /*parse 'til we exhaust TXT. */
/*─────────────────────────────────────TELL subroutine──────────────────*/
tell: if $x==''word(txt,k) then return /*1stget a word mayfrom beTXT too long (wide)string.*/
_=$ x /*append it to da money & see*/
if justify then say justify($,width)
if length(_)>width then call tell /*this word(s) exceeded linewidththe width? */
else say strip($)
txt $=space(txt)_ /*eliminatethe new superflouswords blanks.are OK so far*/
_=x
end
return
 
call tell /*handle any residual words. */
/*─────────────────────────────────────ERR subroutine───────────────────*/
exit /*let's go home & celebrate. */
err: say; say '***error!***'; say; say arg(1); say; say arg(1);say;exit 13
 
/*─────────────────────────────────────TELL subroutine──────────────────subroutine──────────────*/
tell: if $=='' then return /*first word may be too long.*/
select
when just=='B' then $=justify($,width) /*<───both───>*/
when just=='C' then $= center($,width) /* <centered> */
when just=='R' then $= right($,width) /*──────>right*/
otherwise $= strip($) /*left<───────*/
end
say $ /*show&tell, or write──>file?*/
_=x /*handle any word overflow. */
return /*go back and keep truckin'. */
</lang>
The input file:
Line 499 ⟶ 503:
∙ Parameters are variables travelling under an assumed name.
</pre>
Output when specifying: <tt> , 155 </tt>
<pre style="height:15ex;overflow:scroll">
────────── Computer programming laws ────────── The Primal Scenario -or- Basic Datum of Experience: ∙ Systems in general work poorly or not at all. ∙
Line 537 ⟶ 541:
assumed name.
</pre>
Output when specifying: <tt> , 76 </tt>
<pre style="height:15ex;overflow:scroll">
────────── Computer programming laws ────────── The Primal Scenario -or-
Line 612 ⟶ 616:
variables.) ∙ Parameters are variables travelling under an assumed name.
</pre>
Output [justified] when specifying: <tt> -j 70 </tt>
<pre style="height:15ex;overflow:scroll">
────────── Computer programming laws ────────── The Primal Scenario
Cookies help us deliver our services. By using our services, you agree to our use of cookies.