; TXTFORM
;
; Program for Proteus
;
; (C) 1998-2004 Simone Zanella Productions
;
; Text formatting.
; All lines are converted to lines less than MaxLength character long;
; after each new dot ('.'), a new line is started.
FUNCTION ONSTART()
  _MaxLength = 60
  _Line = ""
  IF STREQ(ARGV(3), "..")
    CONSOLELN "Syntax: " ARGV(1) " " ARGV(2) " source destination"
    CONSOLELN ""
    CONSOLELN "Purpose: format a text in lines of at most " \
              PUB(MaxLength) " characters."
    ABORT 0
  FI
RETURN
TKH = TOKNEW(L, " ")
; Number of tokens on the line
NTK = TOKNUM(TKH)
FOR CurTok = 1 TO NTK
  Word = TOKGET(TKH, CurTok)
  CurLen = STRLEN(Line)
  ; By adding this word, line length would become too big;
  ; print line and move word at the beginning of the next line
  IF GT(ADD(CurLen, STRLEN(Word)), MaxLength)
    PRINTLN LTRIM(Line, " ")
    Line = Word
  ELSE
    Line = Line " " Word
  FI
  ; Full stop: go to next line
  IF STREQ(RIGHT(Word, 1), ".")
    PRINTLN LTRIM(Line, " ")
    Line = ""
  FI
NEXT
TOKFREE(TKH)
IF AND(EOF, ISNOTEMPTY(Line))
  PRINTLN Line
FI