home *** CD-ROM | disk | FTP | other *** search
/ Dream 44 / Amiga_Dream_44.iso / RiscPc / programmation / scm4e2.arc / !Scm / slib / format < prev    next >
Text File  |  1994-05-25  |  57KB  |  1,677 lines

  1. ;;; "format.scm" Common LISP text output formatter for SLIB
  2. ; Copyright (C) 1992-1994 by Dirk Lutzebaeck (lutzeb@cs.tu-berlin.de)
  3.  
  4. ; Authors of the original version (< 1.4) were Ken Dickey and Aubrey Jaffer.
  5. ; Please send error reports to the email address above.
  6. ; For documentation see slib.texi and format.doc.
  7. ; For testing load formatst.scm.
  8. ;
  9. ; Version 3.0
  10.  
  11. (provide 'format)
  12. (require 'string-case)
  13. (require 'string-port)
  14. (require 'rev4-optional-procedures)
  15.  
  16. ;;; Configuration ------------------------------------------------------------
  17.  
  18. (define format:symbol-case-conv #f)
  19. ;; Symbols are converted by symbol->string so the case of the printed
  20. ;; symbols is implementation dependent. format:symbol-case-conv is a
  21. ;; one arg closure which is either #f (no conversion), string-upcase!,
  22. ;; string-downcase! or string-capitalize!.
  23.  
  24. (define format:iobj-case-conv #f)
  25. ;; As format:symbol-case-conv but applies for the representation of
  26. ;; implementation internal objects.
  27.  
  28. (define format:expch #\E)
  29. ;; The character prefixing the exponent value in ~e printing.
  30.  
  31. (define format:floats (provided? 'inexact))
  32. ;; Detects if the scheme system implements flonums (see at eof).
  33.  
  34. (define format:complex-numbers (provided? 'complex))
  35. ;; Detects if the scheme system implements complex numbers.
  36.  
  37. (define format:radix-pref (char=? #\# (string-ref (number->string 8 8) 0)))
  38. ;; Detects if number->string adds a radix prefix.
  39.  
  40. (define format:ascii-non-printable-charnames
  41.   '#("nul" "soh" "stx" "etx" "eot" "enq" "ack" "bel"
  42.      "bs"  "ht"  "nl"  "vt"  "np"  "cr"  "so"  "si"
  43.      "dle" "dc1" "dc2" "dc3" "dc4" "nak" "syn" "etb"
  44.      "can" "em"  "sub" "esc" "fs"  "gs"  "rs"  "us" "space"))
  45.  
  46. ;;; End of configuration ----------------------------------------------------
  47.  
  48. (define format:version "3.0")
  49. (define format:port #f)            ; curr. format output port
  50. (define format:output-col 0)        ; curr. format output tty column
  51. (define format:flush-output #f)        ; flush output at end of formatting
  52. (define format:case-conversion #f)
  53. (define format:error-continuation #f)
  54. (define format:args #f)
  55. (define format:pos 0)            ; curr. format string parsing position
  56. (define format:arg-pos 0)        ; curr. format argument position
  57.                     ; this is global for error presentation
  58.  
  59. ; format string and char output routines on format:port
  60.  
  61. (define (format:out-str str)
  62.   (if format:case-conversion
  63.       (display (format:case-conversion str) format:port)
  64.       (display str format:port))
  65.   (set! format:output-col
  66.     (+ format:output-col (string-length str))))
  67.  
  68. (define (format:out-char ch)
  69.   (if format:case-conversion
  70.       (display (format:case-conversion (string ch)) format:port)
  71.       (write-char ch format:port))
  72.   (set! format:output-col
  73.     (if (char=? ch #\newline)
  74.         0
  75.         (+ format:output-col 1))))
  76.  
  77. ;(define (format:out-substr str i n)  ; this allocates a new string
  78. ;  (display (substring str i n) format:port)
  79. ;  (set! format:output-col (+ format:output-col n)))
  80.  
  81. (define (format:out-substr str i n)
  82.   (do ((k i (+ k 1)))
  83.       ((= k n))
  84.     (write-char (string-ref str k) format:port))
  85.   (set! format:output-col (+ format:output-col n)))
  86.  
  87. ;(define (format:out-fill n ch)       ; this allocates a new string
  88. ;  (format:out-str (make-string n ch)))
  89.  
  90. (define (format:out-fill n ch)
  91.   (do ((i 0 (+ i 1)))
  92.       ((= i n))
  93.     (write-char ch format:port))
  94.   (set! format:output-col (+ format:output-col n)))
  95.  
  96. ; format's user error handler
  97.  
  98. (define (format:error . args)        ; never returns!
  99.   (let ((error-continuation format:error-continuation)
  100.     (format-args format:args)
  101.     (port (current-error-port)))
  102.     (set! format:error format:intern-error)
  103.     (if (and (>= (length format:args) 2)
  104.          (string? (cadr format:args)))
  105.     (let ((format-string (cadr format-args)))
  106.       (if (not (zero? format:arg-pos))
  107.           (set! format:arg-pos (- format:arg-pos 1)))
  108.       (format port "~%FORMAT: error with call: (format ~a \"~a<===~a\" ~
  109.                                   ~{~a ~}===>~{~a ~})~%        "
  110.           (car format:args)
  111.           (substring format-string 0 format:pos)
  112.           (substring format-string format:pos
  113.                  (string-length format-string))
  114.           (list-head (cddr format:args) format:arg-pos)
  115.           (list-tail (cddr format:args) format:arg-pos)))
  116.     (format port 
  117.         "~%FORMAT: error with call: (format~{ ~a~})~%        "
  118.         format:args))
  119.     (apply format port args)
  120.     (newline port)
  121.     (set! format:error format:error-save)
  122.     (set! format:error-continuation error-continuation)
  123.     (format:abort)
  124.     (format:intern-error "format:abort does not jump to toplevel!")))
  125.  
  126. (define format:error-save format:error)
  127.  
  128. (define (format:intern-error . args)   ;if something goes wrong in format:error
  129.   (display "FORMAT: INTERNAL ERROR IN FORMAT:ERROR!") (newline)
  130.   (display "        format args: ") (write format:args) (newline)
  131.   (display "        error args:  ") (write args) (newline)
  132.   (set! format:error format:error-save)
  133.   (format:abort))
  134.  
  135. (define (format:format . args)        ; the formatter entry
  136.   (set! format:args args)
  137.   (set! format:arg-pos 0)
  138.   (set! format:pos 0)
  139.   (if (< (length args) 1)
  140.       (format:error "not enough arguments"))
  141.   (let ((destination (car args))
  142.     (arglist (cdr args)))
  143.     (cond
  144.      ((or (and (boolean? destination)    ; port output
  145.            destination)
  146.       (output-port? destination)
  147.       (number? destination))
  148.       (format:out (cond
  149.            ((boolean? destination) (current-output-port))
  150.            ((output-port? destination) destination)
  151.            ((number? destination) (current-error-port)))
  152.           (car arglist) (cdr arglist)))
  153.      ((and (boolean? destination)    ; string output
  154.        (not destination))
  155.       (call-with-output-string
  156.        (lambda (port) (format:out port (car arglist) (cdr arglist)))))
  157.      ((string? destination)        ; dest. is format string (Scheme->C)
  158.       (call-with-output-string
  159.        (lambda (port)
  160.      (format:out port destination arglist))))
  161.      (else
  162.       (format:error "illegal destination `~a'" destination)))))
  163.  
  164. (define (format:out port fmt args)    ; the output handler for a port
  165.   (set! format:port port)        ; global port for output routines
  166.   (set! format:case-conversion #f)    ; modifier case conversion procedure
  167.   (set! format:flush-output #f)        ; ~! reset
  168.   (let ((arg-pos (format:format-work fmt args))
  169.     (arg-len (length args)))
  170.     (cond
  171.      ((< arg-pos arg-len)
  172.       (set! format:arg-pos (+ arg-pos 1))
  173.       (set! format:pos (string-length fmt))
  174.       (format:error "~a superfluous argument~:p" (- arg-len arg-pos)))
  175.      ((> arg-pos arg-len)
  176.       (set! format:arg-pos (+ arg-len 1))
  177.       (display format:arg-pos)
  178.       (format:error "~a missing argument~:p" (- arg-pos arg-len)))
  179.      (else
  180.       (if format:flush-output (force-output port))
  181.       #t))))
  182.  
  183. (define format:parameter-characters
  184.   '(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\- #\+ #\v #\# #\'))
  185.  
  186. (define (format:format-work format-string arglist) ; does the formatting work
  187.   (letrec
  188.       ((format-string-len (string-length format-string))
  189.        (arg-pos 0)            ; argument position in arglist
  190.        (arg-len (length arglist))    ; number of arguments
  191.        (modifier #f)            ; 'colon | 'at | 'colon-at | #f
  192.        (params '())            ; directive parameter list
  193.        (param-value-found #f)        ; a directive parameter value found
  194.        (conditional-nest 0)        ; conditional nesting level
  195.        (clause-pos 0)            ; last cond. clause beginning char pos
  196.        (clause-default #f)        ; conditional default clause string
  197.        (clauses '())            ; conditional clause string list
  198.        (conditional-type #f)        ; reflects the contional modifiers
  199.        (conditional-arg #f)        ; argument to apply the conditional
  200.        (iteration-nest 0)        ; iteration nesting level
  201.        (iteration-pos 0)        ; iteration string beginning char pos
  202.        (iteration-type #f)        ; reflects the iteration modifiers
  203.        (max-iterations #f)        ; maximum number of iterations
  204.        (recursive-pos-save format:pos)
  205.  
  206.        (next-char            ; gets the next char from format-string
  207.     (lambda ()
  208.       (let ((ch (peek-next-char)))
  209.         (set! format:pos (+ 1 format:pos))
  210.         ch)))
  211.  
  212.        (peek-next-char
  213.     (lambda ()
  214.       (if (>= format:pos format-string-len)
  215.           (format:error "illegal format string")
  216.           (string-ref format-string format:pos))))
  217.  
  218.        (one-positive-integer?
  219.     (lambda (params)
  220.       (cond
  221.        ((null? params) #f)
  222.        ((and (integer? (car params))
  223.          (>= (car params) 0)
  224.          (= (length params) 1)) #t)
  225.        (else (format:error "one positive integer parameter expected")))))
  226.  
  227.        (next-arg
  228.     (lambda ()
  229.       (if (>= arg-pos arg-len)
  230.           (begin
  231.         (set! format:arg-pos (+ arg-len 1))
  232.         (format:error "missing argument(s)")))
  233.       (add-arg-pos 1)
  234.       (list-ref arglist (- arg-pos 1))))
  235.  
  236.        (prev-arg
  237.     (lambda ()
  238.       (add-arg-pos -1)
  239.       (if (negative? arg-pos)
  240.           (format:error "missing backward argument(s)"))
  241.       (list-ref arglist arg-pos)))
  242.  
  243.        (rest-args
  244.     (lambda ()
  245.       (let loop ((l arglist) (k arg-pos)) ; list-tail definition
  246.         (if (= k 0) l (loop (cdr l) (- k 1))))))
  247.  
  248.        (add-arg-pos
  249.     (lambda (n) 
  250.       (set! arg-pos (+ n arg-pos))
  251.       (set! format:arg-pos arg-pos)))
  252.  
  253.        (anychar-dispatch        ; dispatches the format-string
  254.     (lambda ()
  255.       (if (>= format:pos format-string-len)
  256.           arg-pos            ; used for ~? continuance
  257.           (let ((char (next-char)))
  258.         (cond
  259.          ((char=? char #\~)
  260.           (set! modifier #f)
  261.           (set! params '())
  262.           (set! param-value-found #f)
  263.           (tilde-dispatch))
  264.          (else
  265.           (if (and (zero? conditional-nest)
  266.                (zero? iteration-nest))
  267.               (format:out-char char))
  268.           (anychar-dispatch)))))))
  269.  
  270.        (tilde-dispatch
  271.     (lambda ()
  272.       (cond
  273.        ((>= format:pos format-string-len)
  274.         (format:out-str "~")    ; tilde at end of string is just output
  275.         arg-pos)            ; used for ~? continuance
  276.        ((and (or (zero? conditional-nest)
  277.              (memv (peek-next-char) ; find conditional directives
  278.                (append '(#\[ #\] #\; #\: #\@ #\^)
  279.                    format:parameter-characters)))
  280.          (or (zero? iteration-nest)
  281.              (memv (peek-next-char) ; find iteration directives
  282.                (append '(#\{ #\} #\: #\@ #\^)
  283.                    format:parameter-characters))))
  284.         (case (char-upcase (next-char))
  285.  
  286.           ;; format directives
  287.  
  288.           ((#\A)            ; Any -- for humans
  289.            (set! format:read-proof (memq modifier '(colon colon-at)))
  290.            (format:out-obj-padded (memq modifier '(at colon-at))
  291.                       (next-arg) #f params)
  292.            (anychar-dispatch))
  293.           ((#\S)            ; Slashified -- for parsers
  294.            (set! format:read-proof (memq modifier '(colon colon-at)))
  295.            (format:out-obj-padded (memq modifier '(at colon-at))
  296.                       (next-arg) #t params)
  297.            (anychar-dispatch))
  298.           ((#\D)            ; Decimal
  299.            (format:out-num-padded modifier (next-arg) params 10)
  300.            (anychar-dispatch))
  301.           ((#\X)            ; Hexadecimal
  302.            (format:out-num-padded modifier (next-arg) params 16)
  303.            (anychar-dispatch))
  304.           ((#\O)            ; Octal
  305.            (format:out-num-padded modifier (next-arg) params 8)
  306.            (anychar-dispatch))
  307.           ((#\B)            ; Binary
  308.            (format:out-num-padded modifier (next-arg) params 2)
  309.            (anychar-dispatch))
  310.           ((#\R)
  311.            (if (null? params)
  312.            (format:out-obj-padded ; Roman, cardinal, ordinal numerals
  313.             #f
  314.             ((case modifier
  315.                ((at) format:num->roman)
  316.                ((colon-at) format:num->old-roman)
  317.                ((colon) format:num->ordinal)
  318.                (else format:num->cardinal))
  319.              (next-arg))
  320.             #f params)
  321.            (format:out-num-padded ; any Radix
  322.             modifier (next-arg) (cdr params) (car params)))
  323.            (anychar-dispatch))
  324.           ((#\F)            ; Fixed-format floating-point
  325.            (if format:floats
  326.            (format:out-fixed modifier (next-arg) params)
  327.            (format:out-str (number->string (next-arg))))
  328.            (anychar-dispatch))
  329.           ((#\E)            ; Exponential floating-point
  330.            (if format:floats
  331.            (format:out-expon modifier (next-arg) params)
  332.            (format:out-str (number->string (next-arg))))
  333.            (anychar-dispatch))
  334.           ((#\G)            ; General floating-point
  335.            (if format:floats
  336.            (format:out-general modifier (next-arg) params)
  337.            (format:out-str (number->string (next-arg))))
  338.            (anychar-dispatch))
  339.           ((#\$)            ; Dollars floating-point
  340.            (if format:floats
  341.            (format:out-dollar modifier (next-arg) params)
  342.            (format:out-str (number->string (next-arg))))
  343.            (anychar-dispatch))
  344.           ((#\I)            ; Complex numbers
  345.            (if (not format:complex-numbers)
  346.            (format:error
  347.             "complex numbers not supported by this scheme system"))
  348.            (let ((z (next-arg)))
  349.          (if (not (complex? z))
  350.              (format:error "argument not a complex number"))
  351.          (format:out-fixed modifier (real-part z) params)
  352.          (format:out-fixed 'at (imag-part z) params)
  353.          (format:out-char #\i))
  354.            (anychar-dispatch))
  355.           ((#\C)            ; Character
  356.            (let ((ch (if (one-positive-integer? params)
  357.                  (integer->char (car params))
  358.                  (next-arg))))
  359.          (if (not (char? ch)) (format:error "~~c expects a character"))
  360.          (case modifier
  361.            ((at)
  362.             (format:out-str (format:char->str ch)))
  363.            ((colon)
  364.             (let ((c (char->integer ch)))
  365.               (if (< c 0)
  366.               (set! c (+ c 256))) ; compensate complement impl.
  367.               (cond
  368.                ((< c #x20)    ; assumes that control chars are < #x20
  369.             (format:out-char #\^)
  370.             (format:out-char
  371.              (integer->char (+ c #x40))))
  372.                ((>= c #x7f)
  373.             (format:out-str "#\\")
  374.             (format:out-str
  375.              (if format:radix-pref
  376.                  (let ((s (number->string c 8)))
  377.                    (substring s 2 (string-length s)))
  378.                  (number->string c 8))))
  379.                (else
  380.             (format:out-char ch)))))
  381.            (else (format:out-char ch))))
  382.            (anychar-dispatch))
  383.           ((#\P)            ; Plural
  384.            (if (memq modifier '(colon colon-at))
  385.            (prev-arg))
  386.            (let ((arg (next-arg)))
  387.          (if (not (number? arg))
  388.              (format:error "~~p expects a number argument"))
  389.          (if (= arg 1)
  390.              (if (memq modifier '(at colon-at))
  391.              (format:out-char #\y))
  392.              (if (memq modifier '(at colon-at))
  393.              (format:out-str "ies")
  394.              (format:out-char #\s))))
  395.            (anychar-dispatch))
  396.           ((#\~)            ; Tilde
  397.            (if (one-positive-integer? params)
  398.            (format:out-fill (car params) #\~)
  399.            (format:out-char #\~))
  400.            (anychar-dispatch))
  401.           ((#\%)            ; Newline
  402.            (if (one-positive-integer? params)
  403.            (format:out-fill (car params) #\newline)
  404.            (format:out-char #\newline))
  405.            (set! format:output-col 0)
  406.            (anychar-dispatch))
  407.           ((#\&)            ; Fresh line
  408.            (if (one-positive-integer? params)
  409.            (begin
  410.              (if (> (car params) 0)
  411.              (format:out-fill (- (car params)
  412.                          (if (> format:output-col 0) 0 1))
  413.                       #\newline))
  414.              (set! format:output-col 0))
  415.            (if (> format:output-col 0)
  416.                (format:out-char #\newline)))
  417.            (anychar-dispatch))
  418.           ((#\_)            ; Space character
  419.            (if (one-positive-integer? params)
  420.            (format:out-fill (car params) #\space)
  421.            (format:out-char #\space))
  422.            (anychar-dispatch))
  423.           ((#\/)            ; Tabulator character
  424.            (if (one-positive-integer? params)
  425.            (format:out-fill (car params) slib:tab)
  426.            (format:out-char slib:tab))
  427.            (anychar-dispatch))
  428.           ((#\|)            ; Page seperator
  429.            (if (one-positive-integer? params)
  430.            (format:out-str (car params) slib:form-feed)
  431.            (format:out-char slib:form-feed))
  432.            (set! format:output-col 0)
  433.            (anychar-dispatch))
  434.           ((#\T)            ; Tabulate
  435.            (format:tabulate modifier params)
  436.            (anychar-dispatch))
  437.           ((#\Y)            ; Pretty-print
  438.            (require 'pretty-print)
  439.            (pretty-print (next-arg) format:port)
  440.            (set! format:output-col 0)
  441.            (anychar-dispatch))
  442.           ((#\? #\K)        ; Indirection (is "~K" in T-Scheme)
  443.            (cond
  444.         ((memq modifier '(colon colon-at))
  445.          (format:error "illegal modifier in ~~?"))
  446.         ((eq? modifier 'at)
  447.          (let* ((frmt (next-arg))
  448.             (args (rest-args)))
  449.            (add-arg-pos (format:format-work frmt args))))
  450.         (else
  451.          (let* ((frmt (next-arg))
  452.             (args (next-arg)))
  453.            (format:format-work frmt args))))
  454.            (anychar-dispatch))
  455.           ((#\!)            ; Flush output
  456.            (set! format:flush-output #t)
  457.            (anychar-dispatch))
  458.           ((#\newline)        ; Continuation lines
  459.            (if (eq? modifier 'at)
  460.            (format:out-char #\newline))
  461.            (if (< format:pos format-string-len)
  462.            (do ((ch (peek-next-char) (peek-next-char)))
  463.                ((or (not (char-whitespace? ch))
  464.                 (= format:pos (- format-string-len 1))))
  465.              (if (eq? modifier 'colon)
  466.              (format:out-char (next-char))
  467.              (next-char))))
  468.            (anychar-dispatch))
  469.           ((#\*)            ; Argument jumping
  470.            (case modifier
  471.          ((colon)        ; jump backwards
  472.           (if (one-positive-integer? params)
  473.               (do ((i 0 (+ i 1)))
  474.               ((= i (car params)))
  475.             (prev-arg))
  476.               (prev-arg)))
  477.          ((at)            ; jump absolute
  478.           (set! arg-pos (if (one-positive-integer? params)
  479.                     (car params) 0)))
  480.          ((colon-at)
  481.           (format:error "illegal modifier `:@' in ~~* directive"))
  482.          (else            ; jump forward
  483.           (if (one-positive-integer? params)
  484.               (do ((i 0 (+ i 1)))
  485.               ((= i (car params)))
  486.             (next-arg))
  487.               (next-arg))))
  488.            (anychar-dispatch))
  489.           ((#\()            ; Case conversion begin
  490.            (set! format:case-conversion
  491.              (case modifier
  492.                ((at) string-capitalize-first)
  493.                ((colon) string-capitalize)
  494.                ((colon-at) string-upcase)
  495.                (else string-downcase)))
  496.            (anychar-dispatch))
  497.           ((#\))            ; Case conversion end
  498.            (if (not format:case-conversion)
  499.            (format:error "missing ~~("))
  500.            (set! format:case-conversion #f)
  501.            (anychar-dispatch))
  502.           ((#\[)            ; Conditional begin
  503.            (set! conditional-nest (+ conditional-nest 1))
  504.            (cond
  505.         ((= conditional-nest 1)
  506.          (set! clause-pos format:pos)
  507.          (set! clause-default #f)
  508.          (set! clauses '())
  509.          (set! conditional-type
  510.                (case modifier
  511.              ((at) 'if-then)
  512.              ((colon) 'if-else-then)
  513.              ((colon-at) (format:error "illegal modifier in ~~["))
  514.              (else 'num-case)))
  515.          (set! conditional-arg
  516.                (if (one-positive-integer? params)
  517.                (car params)
  518.                (next-arg)))))
  519.            (anychar-dispatch))
  520.           ((#\;)                    ; Conditional separator
  521.            (if (zero? conditional-nest)
  522.            (format:error "~~; not in ~~[~~] conditional"))
  523.            (if (not (null? params))
  524.            (format:error "no parameter allowed in ~~;"))
  525.            (if (= conditional-nest 1)
  526.            (let ((clause-str
  527.               (cond
  528.                ((eq? modifier 'colon)
  529.                 (set! clause-default #t)
  530.                 (substring format-string clause-pos 
  531.                        (- format:pos 3)))
  532.                ((memq modifier '(at colon-at))
  533.                 (format:error "illegal modifier in ~~;"))
  534.                (else
  535.                 (substring format-string clause-pos
  536.                        (- format:pos 2))))))
  537.              (set! clauses (append clauses (list clause-str)))
  538.              (set! clause-pos format:pos)))
  539.            (anychar-dispatch))
  540.           ((#\])            ; Conditional end
  541.            (if (zero? conditional-nest) (format:error "missing ~~["))
  542.            (set! conditional-nest (- conditional-nest 1))
  543.            (if modifier
  544.            (format:error "no modifier allowed in ~~]"))
  545.            (if (not (null? params))
  546.            (format:error "no parameter allowed in ~~]"))
  547.            (cond
  548.         ((zero? conditional-nest)
  549.          (let ((clause-str (substring format-string clause-pos
  550.                           (- format:pos 2))))
  551.            (if clause-default
  552.                (set! clause-default clause-str)
  553.                (set! clauses (append clauses (list clause-str)))))
  554.          (case conditional-type
  555.            ((if-then)
  556.             (if conditional-arg
  557.             (format:format-work (car clauses)
  558.                         (list conditional-arg))))
  559.            ((if-else-then)
  560.             (add-arg-pos
  561.              (format:format-work (if conditional-arg
  562.                          (cadr clauses)
  563.                          (car clauses))
  564.                      (rest-args))))
  565.            ((num-case)
  566.             (if (or (not (integer? conditional-arg))
  567.                 (< conditional-arg 0))
  568.             (format:error "argument not a positive integer"))
  569.             (if (not (and (>= conditional-arg (length clauses))
  570.                   (not clause-default)))
  571.             (add-arg-pos
  572.              (format:format-work
  573.               (if (>= conditional-arg (length clauses))
  574.                   clause-default
  575.                   (list-ref clauses conditional-arg))
  576.               (rest-args))))))))
  577.            (anychar-dispatch))
  578.           ((#\{)            ; Iteration begin
  579.            (set! iteration-nest (+ iteration-nest 1))
  580.            (cond
  581.         ((= iteration-nest 1)
  582.          (set! iteration-pos format:pos)
  583.          (set! iteration-type
  584.                (case modifier
  585.              ((at) 'rest-args)
  586.              ((colon) 'sublists)
  587.              ((colon-at) 'rest-sublists)
  588.              (else 'list)))
  589.          (set! max-iterations (if (one-positive-integer? params)
  590.                      (car params) #f))))
  591.            (anychar-dispatch))
  592.           ((#\})            ; Iteration end
  593.            (if (zero? iteration-nest) (format:error "missing ~~{"))
  594.            (set! iteration-nest (- iteration-nest 1))
  595.            (case modifier
  596.          ((colon)
  597.           (if (not max-iterations) (set! max-iterations 1)))
  598.          ((colon-at at) (format:error "illegal modifier"))
  599.          (else (if (not max-iterations) (set! max-iterations 100))))
  600.            (if (not (null? params))
  601.            (format:error "no parameters allowed in ~~}"))
  602.            (if (zero? iteration-nest)
  603.          (let ((iteration-str
  604.             (substring format-string iteration-pos
  605.                    (- format:pos (if modifier 3 2)))))
  606.            (if (string=? iteration-str "")
  607.                (set! iteration-str (next-arg)))
  608.            (case iteration-type
  609.              ((list)
  610.               (let ((args (next-arg))
  611.                 (args-len 0))
  612.             (if (not (list? args))
  613.                 (format:error "expected a list argument"))
  614.             (set! args-len (length args))
  615.             (do ((arg-pos 0 (+ arg-pos
  616.                        (format:format-work
  617.                         iteration-str
  618.                         (list-tail args arg-pos))))
  619.                  (i 0 (+ i 1)))
  620.                 ((or (>= arg-pos args-len)
  621.                  (>= i max-iterations))))))
  622.              ((sublists)
  623.               (let ((args (next-arg))
  624.                 (args-len 0))
  625.             (if (not (list? args))
  626.                 (format:error "expected a list argument"))
  627.             (set! args-len (length args))
  628.             (do ((arg-pos 0 (+ arg-pos 1)))
  629.                 ((or (>= arg-pos args-len)
  630.                  (>= arg-pos max-iterations)))
  631.               (let ((sublist (list-ref args arg-pos)))
  632.                 (if (not (list? sublist))
  633.                 (format:error
  634.                  "expected a list of lists argument"))
  635.                 (format:format-work iteration-str sublist)))))
  636.              ((rest-args)
  637.               (let* ((args (rest-args))
  638.                  (args-len (length args))
  639.                  (usedup-args
  640.                   (do ((arg-pos 0 (+ arg-pos
  641.                          (format:format-work
  642.                           iteration-str
  643.                           (list-tail
  644.                            args arg-pos))))
  645.                    (i 0 (+ i 1)))
  646.                   ((or (>= arg-pos args-len)
  647.                        (>= i max-iterations))
  648.                    arg-pos))))
  649.             (add-arg-pos usedup-args)))
  650.              ((rest-sublists)
  651.               (let* ((args (rest-args))
  652.                  (args-len (length args))
  653.                  (usedup-args
  654.                   (do ((arg-pos 0 (+ arg-pos 1)))
  655.                   ((or (>= arg-pos args-len)
  656.                        (>= arg-pos max-iterations))
  657.                    arg-pos)
  658.                 (let ((sublist (list-ref args arg-pos)))
  659.                   (if (not (list? sublist))
  660.                       (format:error "expected list arguments"))
  661.                   (format:format-work iteration-str sublist)))))
  662.             (add-arg-pos usedup-args)))
  663.              (else (format:error "internal error in ~~}")))))
  664.            (anychar-dispatch))
  665.           ((#\^)            ; Up and out
  666.            (let* ((continue
  667.                (cond
  668.             ((not (null? params))
  669.              (not
  670.               (case (length params)
  671.                ((1) (zero? (car params)))
  672.                ((2) (= (list-ref params 0) (list-ref params 1)))
  673.                ((3) (<= (list-ref params 0)
  674.                     (list-ref params 1)
  675.                     (list-ref params 2)))
  676.                (else (format:error "too much parameters")))))
  677.             (format:case-conversion ; if conversion stop conversion
  678.              (set! format:case-conversion string-copy) #t)
  679.             ((= iteration-nest 1) #t)
  680.             ((= conditional-nest 1) #t)
  681.             ((>= arg-pos arg-len)
  682.              (set! format:pos format-string-len) #f)
  683.             (else #t))))
  684.          (if continue
  685.              (anychar-dispatch))))
  686.  
  687.           ;; format directive modifiers and parameters
  688.  
  689.           ((#\@)            ; `@' modifier
  690.            (if (eq? modifier 'colon-at)
  691.            (format:error "double `@' modifier"))
  692.            (set! modifier (if (eq? modifier 'colon) 'colon-at 'at))
  693.            (tilde-dispatch))
  694.           ((#\:)            ; `:' modifier
  695.            (if modifier (format:error "illegal `:' modifier position"))
  696.            (set! modifier 'colon)
  697.            (tilde-dispatch))
  698.           ((#\')            ; Character parameter
  699.            (if modifier (format:error "misplaced modifier"))
  700.            (set! params (append params (list (char->integer (next-char)))))
  701.            (set! param-value-found #t)
  702.            (tilde-dispatch))
  703.           ((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\- #\+) ; num. paramtr
  704.            (if modifier (format:error "misplaced modifier"))
  705.            (let ((num-str-beg (- format:pos 1))
  706.              (num-str-end format:pos))
  707.          (do ((ch (peek-next-char) (peek-next-char)))
  708.              ((not (char-numeric? ch)))
  709.            (next-char)
  710.            (set! num-str-end (+ 1 num-str-end)))
  711.          (set! params
  712.                (append params
  713.                    (list (string->number
  714.                       (substring format-string
  715.                          num-str-beg
  716.                          num-str-end))))))
  717.            (set! param-value-found #t)
  718.            (tilde-dispatch))
  719.           ((#\V)            ; Variable parameter from next argum.
  720.            (if modifier (format:error "misplaced modifier"))
  721.            (set! params (append params (list (next-arg))))
  722.            (set! param-value-found #t)
  723.            (tilde-dispatch))
  724.           ((#\#)            ; Parameter is number of remaining args
  725.            (if modifier (format:error "misplaced modifier"))
  726.            (set! params (append params (list (length (rest-args)))))
  727.            (set! param-value-found #t)
  728.            (tilde-dispatch))
  729.           ((#\,)            ; Parameter separators
  730.            (if modifier (format:error "misplaced modifier"))
  731.            (if (not param-value-found)
  732.            (set! params (append params '(#f)))) ; append empty paramtr
  733.            (set! param-value-found #f)
  734.            (tilde-dispatch))
  735.           ((#\Q)            ; Inquiry messages
  736.            (if (eq? modifier 'colon)
  737.            (format:out-str format:version)
  738.            (let ((nl (string #\newline)))
  739.              (format:out-str
  740.               (string-append
  741.                "SLIB Common LISP format version " format:version nl
  742.                "  (C) copyright 1992-1994 by Dirk Lutzebaeck" nl
  743.                "  please send bug reports to `lutzeb@cs.tu-berlin.de'"
  744.                nl))))
  745.            (anychar-dispatch))
  746.           (else            ; Unknown tilde directive
  747.            (format:error "unknown control character `~c'"
  748.               (string-ref format-string (- format:pos 1))))))
  749.        (else (anychar-dispatch)))))) ; in case of conditional
  750.  
  751.     (set! format:pos 0)
  752.     (set! format:arg-pos 0)
  753.     (anychar-dispatch)            ; start the formatting
  754.     (set! format:pos recursive-pos-save)
  755.     arg-pos))                ; return the position in the arg. list
  756.  
  757. ;; format:obj->str returns a R4RS representation as a string of an arbitrary
  758. ;; scheme object.
  759. ;; First parameter is the object, second parameter is a boolean if the
  760. ;; representation should be slashified as `write' does.
  761. ;; It uses format:char->str which converts a character into
  762. ;; a slashified string as `write' does and which is implementation dependent.
  763. ;; It uses format:iobj->str to print out internal objects as
  764. ;; quoted strings so that the output can always be processed by (read)
  765.  
  766. (define (format:obj->str obj slashify)
  767.   (cond
  768.    ((string? obj)
  769.     (if slashify
  770.     (let ((obj-len (string-length obj)))
  771.       (string-append
  772.        "\""
  773.        (let loop ((i 0) (j 0))    ; taken from Marc Feeley's pp.scm
  774.          (if (= j obj-len)
  775.          (string-append (substring obj i j) "\"")
  776.          (let ((c (string-ref obj j)))
  777.            (if (or (char=? c #\\)
  778.                (char=? c #\"))
  779.                (string-append (substring obj i j) "\\"
  780.                       (loop j (+ j 1)))
  781.                (loop i (+ j 1))))))))
  782.     obj))
  783.    
  784.    ((boolean? obj) (if obj "#t" "#f"))
  785.    
  786.    ((number? obj) (number->string obj))
  787.  
  788.    ((symbol? obj) 
  789.     (if format:symbol-case-conv
  790.     (format:symbol-case-conv (symbol->string obj))
  791.     (symbol->string obj)))
  792.    
  793.    ((char? obj)
  794.     (if slashify
  795.     (format:char->str obj)
  796.     (string obj)))
  797.    
  798.    ((null? obj) "()")
  799.  
  800.    ((input-port? obj)
  801.     (format:iobj->str obj))
  802.    
  803.    ((output-port? obj)
  804.     (format:iobj->str obj))
  805.      
  806.    ((list? obj)
  807.     (string-append "("
  808.            (let loop ((obj-list obj))
  809.              (if (null? (cdr obj-list))
  810.              (format:obj->str (car obj-list) #t)
  811.              (string-append
  812.               (format:obj->str (car obj-list) #t)
  813.               " "
  814.               (loop (cdr obj-list)))))
  815.            ")"))
  816.  
  817.    ((pair? obj)
  818.     (string-append "("
  819.            (format:obj->str (car obj) #t)
  820.            " . "
  821.            (format:obj->str (cdr obj) #t)
  822.            ")"))
  823.    
  824.    ((vector? obj)
  825.     (string-append "#" (format:obj->str (vector->list obj) #t)))
  826.  
  827.    (else                ; only objects with an #<...> 
  828.     (format:iobj->str obj))))        ; representation should fall in here
  829.  
  830. ;; format:iobj->str reveals the implementation dependent representation of 
  831. ;; #<...> objects with the use of display and call-with-output-string.
  832. ;; If format:read-proof is set to #t the resulting string is additionally 
  833. ;; set into string quotes.
  834.  
  835. (define format:read-proof #f)
  836.  
  837. (define (format:iobj->str iobj)
  838.   (if (or format:read-proof
  839.       format:iobj-case-conv)
  840.       (string-append 
  841.        (if format:read-proof "\"" "")
  842.        (if format:iobj-case-conv
  843.        (format:iobj-case-conv
  844.         (call-with-output-string (lambda (p) (display iobj p))))
  845.        (call-with-output-string (lambda (p) (display iobj p))))
  846.        (if format:read-proof "\"" ""))
  847.       (call-with-output-string (lambda (p) (display iobj p)))))
  848.  
  849.  
  850. ;; format:char->str converts a character into a slashified string as
  851. ;; done by `write'. The procedure is dependent on the integer
  852. ;; representation of characters and assumes a character number according to
  853. ;; the ASCII character set.
  854.  
  855. (define (format:char->str ch)
  856.   (let ((int-rep (char->integer ch)))
  857.     (if (< int-rep 0)            ; if chars are [-128...+127]
  858.     (set! int-rep (+ int-rep 256)))
  859.     (string-append
  860.      "#\\"
  861.      (cond
  862.       ((char=? ch #\newline) "newline")
  863.       ((and (>= int-rep 0) (<= int-rep 32))
  864.        (vector-ref format:ascii-non-printable-charnames int-rep))
  865.       ((= int-rep 127) "del")
  866.       ((>= int-rep 128)        ; octal representation
  867.        (if format:radix-pref
  868.        (let ((s (number->string int-rep 8)))
  869.          (substring s 2 (string-length s)))
  870.        (number->string int-rep 8)))
  871.       (else (string ch))))))
  872.  
  873. (define format:space-ch (char->integer #\space))
  874. (define format:zero-ch (char->integer #\0))
  875.  
  876. (define (format:par pars length index default name)
  877.   (if (> length index)
  878.       (let ((par (list-ref pars index)))
  879.     (if par
  880.         (if name
  881.         (if (< par 0)
  882.             (format:error 
  883.              "~s parameter must be a positive integer" name)
  884.             par)
  885.         par)
  886.         default))
  887.       default))
  888.  
  889. (define (format:out-obj-padded pad-left obj slashify pars)
  890.   (if (null? pars)
  891.       (format:out-str (format:obj->str obj slashify))
  892.       (let ((l (length pars)))
  893.     (let ((mincol (format:par pars l 0 0 "mincol"))
  894.           (colinc (format:par pars l 1 1 "colinc"))
  895.           (minpad (format:par pars l 2 0 "minpad"))
  896.           (padchar (integer->char
  897.             (format:par pars l 3 format:space-ch #f)))
  898.           (objstr (format:obj->str obj slashify)))
  899.       (if (not pad-left)
  900.           (format:out-str objstr))
  901.       (do ((objstr-len (string-length objstr))
  902.            (i minpad (+ i colinc)))
  903.           ((>= (+ objstr-len i) mincol)
  904.            (format:out-fill i padchar)))
  905.       (if pad-left
  906.           (format:out-str objstr))))))
  907.  
  908. (define (format:out-num-padded modifier number pars radix)
  909.   (if (not (integer? number)) (format:error "argument not an integer"))
  910.   (let ((numstr (number->string number radix)))
  911.     (if (and format:radix-pref (not (= radix 10)))
  912.     (set! numstr (substring numstr 2 (string-length numstr))))
  913.     (if (and (null? pars) (not modifier))
  914.     (format:out-str numstr)
  915.     (let ((l (length pars))
  916.           (numstr-len (string-length numstr)))
  917.       (let ((mincol (format:par pars l 0 #f "mincol"))
  918.         (padchar (integer->char
  919.               (format:par pars l 1 format:space-ch #f)))
  920.         (commachar (integer->char
  921.                 (format:par pars l 2 (char->integer #\,) #f)))
  922.         (commawidth (format:par pars l 3 3 "commawidth")))
  923.         (if mincol
  924.         (let ((numlen numstr-len)) ; calc. the output len of number
  925.           (if (and (memq modifier '(at colon-at)) (> number 0))
  926.               (set! numlen (+ numlen 1)))
  927.           (if (memq modifier '(colon colon-at))
  928.               (set! numlen (+ (quotient (- numstr-len 
  929.                            (if (< number 0) 2 1))
  930.                         commawidth)
  931.                       numlen)))
  932.           (if (> mincol numlen)
  933.               (format:out-fill (- mincol numlen) padchar))))
  934.         (if (and (memq modifier '(at colon-at))
  935.              (> number 0))
  936.         (format:out-char #\+))
  937.         (if (memq modifier '(colon colon-at)) ; insert comma character
  938.         (let ((start (remainder numstr-len commawidth))
  939.               (ns (if (< number 0) 1 0)))
  940.           (format:out-substr numstr 0 start)
  941.           (do ((i start (+ i commawidth)))
  942.               ((>= i numstr-len))
  943.             (if (> i ns)
  944.             (format:out-char commachar))
  945.             (format:out-substr numstr i (+ i commawidth))))
  946.         (format:out-str numstr)))))))
  947.  
  948. (define (format:tabulate modifier pars)
  949.   (let ((l (length pars)))
  950.     (let ((colnum (format:par pars l 0 1 "colnum"))
  951.       (colinc (format:par pars l 1 1 "colinc"))
  952.       (padch (integer->char (format:par pars l 2 format:space-ch #f))))
  953.       (case modifier
  954.     ((colon colon-at)
  955.      (format:error "unsupported modifier for ~~t"))
  956.     ((at)                ; relative tabulation
  957.      (format:out-fill
  958.       (if (= colinc 0)
  959.           colnum            ; colnum = colrel
  960.           (do ((c 0 (+ c colinc))
  961.            (col (+ format:output-col colnum)))
  962.           ((>= c col)
  963.            (- c format:output-col))))
  964.       padch))
  965.     (else                ; absolute tabulation
  966.      (format:out-fill
  967.       (cond
  968.        ((< format:output-col colnum)
  969.         (- colnum format:output-col))
  970.        ((= colinc 0)
  971.         0)
  972.        (else
  973.         (do ((c colnum (+ c colinc)))
  974.         ((>= c format:output-col)
  975.          (- c format:output-col)))))
  976.       padch))))))
  977.  
  978.  
  979. ;; roman numerals (from dorai@cs.rice.edu).
  980.  
  981. (define format:roman-alist
  982.   '((1000 #\M) (500 #\D) (100 #\C) (50 #\L)
  983.     (10 #\X) (5 #\V) (1 #\I)))
  984.  
  985. (define format:roman-boundary-values
  986.   '(100 100 10 10 1 1 #f))
  987.  
  988. (define format:num->old-roman
  989.   (lambda (n)
  990.     (if (and (integer? n) (>= n 1))
  991.     (let loop ((n n)
  992.            (romans format:roman-alist)
  993.            (s '()))
  994.       (if (null? romans) (list->string (reverse s))
  995.           (let ((roman-val (caar romans))
  996.             (roman-dgt (cadar romans)))
  997.         (do ((q (quotient n roman-val) (- q 1))
  998.              (s s (cons roman-dgt s)))
  999.             ((= q 0)
  1000.              (loop (remainder n roman-val)
  1001.                (cdr romans) s))))))
  1002.     (format:error "only positive integers can be romanized"))))
  1003.  
  1004. (define format:num->roman
  1005.   (lambda (n)
  1006.     (if (and (integer? n) (> n 0))
  1007.     (let loop ((n n)
  1008.            (romans format:roman-alist)
  1009.            (boundaries format:roman-boundary-values)
  1010.            (s '()))
  1011.       (if (null? romans)
  1012.           (list->string (reverse s))
  1013.           (let ((roman-val (caar romans))
  1014.             (roman-dgt (cadar romans))
  1015.             (bdry (car boundaries)))
  1016.         (let loop2 ((q (quotient n roman-val))
  1017.                 (r (remainder n roman-val))
  1018.                 (s s))
  1019.           (if (= q 0)
  1020.               (if (and bdry (>= r (- roman-val bdry)))
  1021.               (loop (remainder r bdry) (cdr romans)
  1022.                 (cdr boundaries)
  1023.                 (cons roman-dgt
  1024.                   (append
  1025.                 (cdr (assv bdry romans))
  1026.                 s)))
  1027.               (loop r (cdr romans) (cdr boundaries) s))
  1028.               (loop2 (- q 1) r (cons roman-dgt s)))))))
  1029.     (format:error "only positive integers can be romanized"))))
  1030.  
  1031. ;; cardinals & ordinals (from dorai@cs.rice.edu)
  1032.  
  1033. (define format:cardinal-ones-list
  1034.   '(#f "one" "two" "three" "four" "five"
  1035.      "six" "seven" "eight" "nine" "ten" "eleven" "twelve" "thirteen"
  1036.      "fourteen" "fifteen" "sixteen" "seventeen" "eighteen"
  1037.      "nineteen"))
  1038.  
  1039. (define format:cardinal-tens-list
  1040.   '(#f #f "twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty"
  1041.      "ninety"))
  1042.  
  1043. (define format:num->cardinal999
  1044.   (lambda (n)
  1045.     ;this procedure is inspired by the Bruno Haible's CLisp
  1046.     ;function format-small-cardinal, which converts numbers
  1047.     ;in the range 1 to 999, and is used for converting each
  1048.     ;thousand-block in a larger number
  1049.     (let* ((hundreds (quotient n 100))
  1050.        (tens+ones (remainder n 100))
  1051.        (tens (quotient tens+ones 10))
  1052.        (ones (remainder tens+ones 10)))
  1053.       (append
  1054.     (if (> hundreds 0)
  1055.         (append
  1056.           (string->list
  1057.         (list-ref format:cardinal-ones-list hundreds))
  1058.           (string->list" hundred")
  1059.           (if (> tens+ones 0) '(#\space) '()))
  1060.         '())
  1061.     (if (< tens+ones 20)
  1062.         (if (> tens+ones 0)
  1063.         (string->list
  1064.           (list-ref format:cardinal-ones-list tens+ones))
  1065.         '())
  1066.         (append
  1067.           (string->list
  1068.         (list-ref format:cardinal-tens-list tens))
  1069.           (if (> ones 0)
  1070.           (cons #\-
  1071.             (string->list
  1072.               (list-ref format:cardinal-ones-list ones))))))))))
  1073.  
  1074. (define format:cardinal-thousand-block-list
  1075.   '("" " thousand" " million" " billion" " trillion" " quadrillion"
  1076.      " quintillion" " sextillion" " septillion" " octillion" " nonillion"
  1077.      " decillion" " undecillion" " duodecillion" " tredecillion"
  1078.      " quattuordecillion" " quindecillion" " sexdecillion" " septendecillion"
  1079.      " octodecillion" " novemdecillion" " vigintillion"))
  1080.  
  1081. (define format:num->cardinal
  1082.   (lambda (n)
  1083.     (cond ((not (integer? n))
  1084.        (format:error
  1085.          "only integers can be converted to English cardinals"))
  1086.       ((= n 0) "zero")
  1087.       ((< n 0) (string-append "minus " (format:num->cardinal (- n))))
  1088.       (else
  1089.         (let ((power3-word-limit
  1090.             (length format:cardinal-thousand-block-list)))
  1091.           (let loop ((n n)
  1092.              (power3 0)
  1093.              (s '()))
  1094.         (if (= n 0)
  1095.             (list->string s)
  1096.             (let ((n-before-block (quotient n 1000))
  1097.               (n-after-block (remainder n 1000)))
  1098.               (loop n-before-block
  1099.             (+ power3 1)
  1100.             (if (> n-after-block 0)
  1101.                 (append
  1102.                   (if (> n-before-block 0)
  1103.                   (string->list ", ") '())
  1104.                   (format:num->cardinal999 n-after-block)
  1105.                   (if (< power3 power3-word-limit)
  1106.                   (string->list
  1107.                     (list-ref
  1108.                      format:cardinal-thousand-block-list
  1109.                      power3))
  1110.                   (append
  1111.                     (string->list " times ten to the ")
  1112.                     (string->list
  1113.                       (format:num->ordinal
  1114.                     (* power3 3)))
  1115.                     (string->list " power")))
  1116.                   s)
  1117.                 s))))))))))
  1118.  
  1119. (define format:ordinal-ones-list
  1120.   '(#f "first" "second" "third" "fourth" "fifth"
  1121.      "sixth" "seventh" "eighth" "ninth" "tenth" "eleventh" "twelfth"
  1122.      "thirteenth" "fourteenth" "fifteenth" "sixteenth" "seventeenth"
  1123.      "eighteenth" "nineteenth"))
  1124.  
  1125. (define format:ordinal-tens-list
  1126.   '(#f #f "twentieth" "thirtieth" "fortieth" "fiftieth" "sixtieth"
  1127.      "seventieth" "eightieth" "ninetieth"))
  1128.  
  1129. (define format:num->ordinal
  1130.   (lambda (n)
  1131.     (cond ((not (integer? n))
  1132.        (format:error
  1133.          "only integers can be converted to English ordinals"))
  1134.       ((= n 0) "zeroth")
  1135.       ((< n 0) (string-append "minus " (format:num->ordinal (- n))))
  1136.       (else
  1137.         (let ((hundreds (quotient n 100))
  1138.           (tens+ones (remainder n 100)))
  1139.           (string-append
  1140.         (if (> hundreds 0)
  1141.             (string-append
  1142.               (format:num->cardinal (* hundreds 100))
  1143.               (if (= tens+ones 0) "th" " "))
  1144.             "")
  1145.         (if (= tens+ones 0) ""
  1146.             (if (< tens+ones 20)
  1147.             (list-ref format:ordinal-ones-list tens+ones)
  1148.             (let ((tens (quotient tens+ones 10))
  1149.                   (ones (remainder tens+ones 10)))
  1150.               (if (= ones 0)
  1151.                   (list-ref format:ordinal-tens-list tens)
  1152.                   (string-append
  1153.                 (list-ref format:cardinal-tens-list tens)
  1154.                 "-"
  1155.                 (list-ref format:ordinal-ones-list ones))))
  1156.             ))))))))
  1157.  
  1158. ;; format fixed flonums (~F)
  1159.  
  1160. (define (format:out-fixed modifier number pars)
  1161.   (if (not (or (number? number) (string? number)))
  1162.       (format:error "argument is not a number or a number string"))
  1163.  
  1164.   (let ((l (length pars)))
  1165.     (let ((width (format:par pars l 0 #f "width"))
  1166.       (digits (format:par pars l 1 #f "digits"))
  1167.       (scale (format:par pars l 2 0 #f))
  1168.       (overch (format:par pars l 3 #f #f))
  1169.       (padch (format:par pars l 4 format:space-ch #f)))
  1170.  
  1171.     (if digits
  1172.  
  1173.     (begin                ; fixed precision
  1174.       (format:parse-float 
  1175.        (if (string? number) number (number->string number)) #t scale)
  1176.       (if (<= (- format:fn-len format:fn-dot) digits)
  1177.           (format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
  1178.           (format:fn-round digits))
  1179.       (if width
  1180.           (let ((numlen (+ format:fn-len 1)))
  1181.         (if (or (not format:fn-pos?) (eq? modifier 'at))
  1182.             (set! numlen (+ numlen 1)))
  1183.         (if (and (= format:fn-dot 0) (> width (+ digits 1)))
  1184.             (set! numlen (+ numlen 1)))
  1185.         (if (< numlen width)
  1186.             (format:out-fill (- width numlen) (integer->char padch)))
  1187.         (if (and overch (> numlen width))
  1188.             (format:out-fill width (integer->char overch))
  1189.             (format:fn-out modifier (> width (+ digits 1)))))
  1190.           (format:fn-out modifier #t)))
  1191.  
  1192.     (begin                ; free precision
  1193.       (format:parse-float
  1194.        (if (string? number) number (number->string number)) #t scale)
  1195.       (format:fn-strip)
  1196.       (if width
  1197.           (let ((numlen (+ format:fn-len 1)))
  1198.         (if (or (not format:fn-pos?) (eq? modifier 'at))
  1199.             (set! numlen (+ numlen 1)))
  1200.         (if (= format:fn-dot 0)
  1201.             (set! numlen (+ numlen 1)))
  1202.         (if (< numlen width)
  1203.             (format:out-fill (- width numlen) (integer->char padch)))
  1204.         (if (> numlen width)    ; adjust precision if possible
  1205.             (let ((dot-index (- numlen
  1206.                     (- format:fn-len format:fn-dot))))
  1207.               (if (> dot-index width)
  1208.               (if overch    ; numstr too big for required width
  1209.                   (format:out-fill width (integer->char overch))
  1210.                   (format:fn-out modifier #t))
  1211.               (begin
  1212.                 (format:fn-round (- width dot-index))
  1213.                 (format:fn-out modifier #t))))
  1214.             (format:fn-out modifier #t)))
  1215.           (format:fn-out modifier #t)))))))
  1216.  
  1217. ;; format exponential flonums (~E)
  1218.  
  1219. (define (format:out-expon modifier number pars)
  1220.   (if (not (or (number? number) (string? number)))
  1221.       (format:error "argument is not a number"))
  1222.  
  1223.   (let ((l (length pars)))
  1224.     (let ((width (format:par pars l 0 #f "width"))
  1225.       (digits (format:par pars l 1 #f "digits"))
  1226.       (edigits (format:par pars l 2 #f "exponent digits"))
  1227.       (scale (format:par pars l 3 1 #f))
  1228.       (overch (format:par pars l 4 #f #f))
  1229.       (padch (format:par pars l 5 format:space-ch #f))
  1230.       (expch (format:par pars l 6 #f #f)))
  1231.      
  1232.     (if digits                ; fixed precision
  1233.  
  1234.     (let ((digits (if (> scale 0)
  1235.               (if (< scale (+ digits 2))
  1236.                   (+ (- digits scale) 1)
  1237.                   0)
  1238.               digits)))
  1239.       (format:parse-float 
  1240.        (if (string? number) number (number->string number)) #f scale)
  1241.       (if (<= (- format:fn-len format:fn-dot) digits)
  1242.           (format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
  1243.           (format:fn-round digits))
  1244.       (if width
  1245.           (if (and edigits overch (> format:en-len edigits))
  1246.           (format:out-fill width (integer->char overch))
  1247.           (let ((numlen (+ format:fn-len 3))) ; .E+
  1248.             (if (or (not format:fn-pos?) (eq? modifier 'at))
  1249.             (set! numlen (+ numlen 1)))
  1250.             (if (and (= format:fn-dot 0) (> width (+ digits 1)))
  1251.             (set! numlen (+ numlen 1)))    
  1252.             (set! numlen
  1253.               (+ numlen 
  1254.                  (if (and edigits (>= edigits format:en-len))
  1255.                  edigits 
  1256.                  format:en-len)))
  1257.             (if (< numlen width)
  1258.             (format:out-fill (- width numlen)
  1259.                      (integer->char padch)))
  1260.             (if (and overch (> numlen width))
  1261.             (format:out-fill width (integer->char overch))
  1262.             (begin
  1263.               (format:fn-out modifier (> width (- numlen 1)))
  1264.               (format:en-out edigits expch)))))
  1265.           (begin
  1266.         (format:fn-out modifier #t)
  1267.         (format:en-out edigits expch))))
  1268.  
  1269.     (begin                ; free precision
  1270.       (format:parse-float
  1271.        (if (string? number) number (number->string number)) #f scale)
  1272.       (format:fn-strip)
  1273.       (if width
  1274.           (if (and edigits overch (> format:en-len edigits))
  1275.           (format:out-fill width (integer->char overch))
  1276.           (let ((numlen (+ format:fn-len 3))) ; .E+
  1277.             (if (or (not format:fn-pos?) (eq? modifier 'at))
  1278.             (set! numlen (+ numlen 1)))
  1279.             (if (= format:fn-dot 0)
  1280.             (set! numlen (+ numlen 1)))
  1281.             (set! numlen
  1282.               (+ numlen
  1283.                  (if (and edigits (>= edigits format:en-len))
  1284.                  edigits 
  1285.                  format:en-len)))
  1286.             (if (< numlen width)
  1287.             (format:out-fill (- width numlen)
  1288.                      (integer->char padch)))
  1289.             (if (> numlen width) ; adjust precision if possible
  1290.             (let ((f (- format:fn-len format:fn-dot))) ; fract len
  1291.               (if (> (- numlen f) width)
  1292.                   (if overch ; numstr too big for required width
  1293.                   (format:out-fill width 
  1294.                            (integer->char overch))
  1295.                   (begin
  1296.                     (format:fn-out modifier #t)
  1297.                     (format:en-out edigits expch)))
  1298.                   (begin
  1299.                 (format:fn-round (+ (- f numlen) width))
  1300.                 (format:fn-out modifier #t)
  1301.                 (format:en-out edigits expch))))
  1302.             (begin
  1303.               (format:fn-out modifier #t)
  1304.               (format:en-out edigits expch)))))
  1305.           (begin
  1306.         (format:fn-out modifier #t)
  1307.         (format:en-out edigits expch))))))))
  1308.     
  1309. ;; format general flonums (~G)
  1310.  
  1311. (define (format:out-general modifier number pars)
  1312.   (if (not (or (number? number) (string? number)))
  1313.       (format:error "argument is not a number or a number string"))
  1314.  
  1315.   (let ((l (length pars)))
  1316.     (let ((width (if (> l 0) (list-ref pars 0) #f))
  1317.       (digits (if (> l 1) (list-ref pars 1) #f))
  1318.       (edigits (if (> l 2) (list-ref pars 2) #f))
  1319.       (overch (if (> l 4) (list-ref pars 4) #f))
  1320.       (padch (if (> l 5) (list-ref pars 5) #f)))
  1321.     (format:parse-float
  1322.      (if (string? number) number (number->string number)) #t 0)
  1323.     (format:fn-strip)
  1324.     (let* ((ee (if edigits (+ edigits 2) 4)) ; for the following algorithm
  1325.        (ww (if width (- width ee) #f))   ; see Steele's CL book p.395
  1326.        (n (if (= format:fn-dot 0)    ; number less than (abs 1.0) ?
  1327.           (- (format:fn-zlead))
  1328.           format:fn-dot))
  1329.        (d (if digits
  1330.           digits
  1331.           (max format:fn-len (min n 7)))) ; q = format:fn-len
  1332.        (dd (- d n)))
  1333.       (if (<= 0 dd d)
  1334.       (begin
  1335.         (format:out-fixed modifier number (list ww dd #f overch padch))
  1336.         (format:out-fill ee #\space)) ;~@T not implemented yet
  1337.       (format:out-expon modifier number pars))))))
  1338.  
  1339. ;; format dollar flonums (~$)
  1340.  
  1341. (define (format:out-dollar modifier number pars)
  1342.   (if (not (or (number? number) (string? number)))
  1343.       (format:error "argument is not a number or a number string"))
  1344.  
  1345.   (let ((l (length pars)))
  1346.     (let ((digits (format:par pars l 0 2 "digits"))
  1347.       (mindig (format:par pars l 1 1 "mindig"))
  1348.       (width (format:par pars l 2 0 "width"))
  1349.       (padch (format:par pars l 3 format:space-ch #f)))
  1350.  
  1351.     (format:parse-float
  1352.      (if (string? number) number (number->string number)) #t 0)
  1353.     (if (<= (- format:fn-len format:fn-dot) digits)
  1354.     (format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
  1355.     (format:fn-round digits))
  1356.     (let ((numlen (+ format:fn-len 1)))
  1357.       (if (or (not format:fn-pos?) (memq modifier '(at colon-at)))
  1358.       (set! numlen (+ numlen 1)))
  1359.       (if (and mindig (> mindig format:fn-dot))
  1360.       (set! numlen (+ numlen (- mindig format:fn-dot))))
  1361.       (if (and (= format:fn-dot 0) (not mindig))
  1362.       (set! numlen (+ numlen 1)))
  1363.       (if (< numlen width)
  1364.       (case modifier
  1365.         ((colon)
  1366.          (if (not format:fn-pos?)
  1367.          (format:out-char #\-))
  1368.          (format:out-fill (- width numlen) (integer->char padch)))
  1369.         ((at)
  1370.          (format:out-fill (- width numlen) (integer->char padch))
  1371.          (format:out-char (if format:fn-pos? #\+ #\-)))
  1372.         ((colon-at)
  1373.          (format:out-char (if format:fn-pos? #\+ #\-))
  1374.          (format:out-fill (- width numlen) (integer->char padch)))
  1375.         (else
  1376.          (format:out-fill (- width numlen) (integer->char padch))
  1377.          (if (not format:fn-pos?)
  1378.          (format:out-char #\-))))
  1379.       (if format:fn-pos?
  1380.           (if (memq modifier '(at colon-at)) (format:out-char #\+))
  1381.           (format:out-char #\-))))
  1382.     (if (and mindig (> mindig format:fn-dot))
  1383.     (format:out-fill (- mindig format:fn-dot) #\0))
  1384.     (if (and (= format:fn-dot 0) (not mindig))
  1385.     (format:out-char #\0))
  1386.     (format:out-substr format:fn-str 0 format:fn-dot)
  1387.     (format:out-char #\.)
  1388.     (format:out-substr format:fn-str format:fn-dot format:fn-len))))
  1389.  
  1390. ; the flonum buffers
  1391.  
  1392. (define format:fn-max 200)        ; max. number of number digits
  1393. (define format:fn-str (make-string format:fn-max)) ; number buffer
  1394. (define format:fn-len 0)        ; digit length of number
  1395. (define format:fn-dot #f)        ; dot position of number
  1396. (define format:fn-pos? #t)        ; number positive?
  1397. (define format:en-max 10)        ; max. number of exponent digits
  1398. (define format:en-str (make-string format:en-max)) ; exponent buffer
  1399. (define format:en-len 0)        ; digit length of exponent
  1400. (define format:en-pos? #t)        ; exponent positive?
  1401.  
  1402. (define (format:parse-float num-str fixed? scale)
  1403.   (set! format:fn-pos? #t)
  1404.   (set! format:fn-len 0)
  1405.   (set! format:fn-dot #f)
  1406.   (set! format:en-pos? #t)
  1407.   (set! format:en-len 0)
  1408.   (do ((i 0 (+ i 1))
  1409.        (left-zeros 0)
  1410.        (mantissa? #t)
  1411.        (all-zeros? #t)
  1412.        (num-len (string-length num-str))
  1413.        (c #f))            ; current exam. character in num-str
  1414.       ((= i num-len)
  1415.        (if (not format:fn-dot)
  1416.        (set! format:fn-dot format:fn-len))
  1417.  
  1418.        (if all-zeros?
  1419.        (begin
  1420.          (set! left-zeros 0)
  1421.          (set! format:fn-dot 0)
  1422.          (set! format:fn-len 1)))
  1423.  
  1424.        ;; now format the parsed values according to format's need
  1425.  
  1426.        (if fixed?
  1427.  
  1428.        (begin            ; fixed format m.nnn or .nnn
  1429.          (if (and (> left-zeros 0) (> format:fn-dot 0))
  1430.          (if (> format:fn-dot left-zeros) 
  1431.              (begin        ; norm 0{0}nn.mm to nn.mm
  1432.                (format:fn-shiftleft left-zeros)
  1433.                (set! left-zeros 0)
  1434.                (set! format:fn-dot (- format:fn-dot left-zeros)))
  1435.              (begin        ; normalize 0{0}.nnn to .nnn
  1436.                (format:fn-shiftleft format:fn-dot)
  1437.                (set! left-zeros (- left-zeros format:fn-dot))
  1438.                (set! format:fn-dot 0))))
  1439.          (if (or (not (= scale 0)) (> format:en-len 0))
  1440.          (let ((shift (+ scale (format:en-int))))
  1441.            (cond
  1442.             (all-zeros? #t)
  1443.             ((> (+ format:fn-dot shift) format:fn-len)
  1444.              (format:fn-zfill
  1445.               #f (- shift (- format:fn-len format:fn-dot)))
  1446.              (set! format:fn-dot format:fn-len))
  1447.             ((< (+ format:fn-dot shift) 0)
  1448.              (format:fn-zfill #t (- (- shift) format:fn-dot))
  1449.              (set! format:fn-dot 0))
  1450.             (else
  1451.              (if (> left-zeros 0)
  1452.              (if (<= left-zeros shift) ; shift always > 0 here
  1453.                  (format:fn-shiftleft shift) ; shift out 0s
  1454.                  (begin
  1455.                    (format:fn-shiftleft left-zeros)
  1456.                    (set! format:fn-dot (- shift left-zeros))))
  1457.              (set! format:fn-dot (+ format:fn-dot shift))))))))
  1458.  
  1459.        (let ((negexp        ; expon format m.nnnEee
  1460.           (if (> left-zeros 0)
  1461.               (- left-zeros format:fn-dot -1)
  1462.               (if (= format:fn-dot 0) 1 0))))
  1463.          (if (> left-zeros 0)
  1464.          (begin            ; normalize 0{0}.nnn to n.nn
  1465.            (format:fn-shiftleft left-zeros)
  1466.            (set! format:fn-dot 1))
  1467.          (if (= format:fn-dot 0)
  1468.              (set! format:fn-dot 1)))
  1469.          (format:en-set (- (+ (- format:fn-dot scale) (format:en-int))
  1470.                    negexp))
  1471.          (cond 
  1472.           (all-zeros?
  1473.            (format:en-set 0)
  1474.            (set! format:fn-dot 1))
  1475.           ((< scale 0)        ; leading zero
  1476.            (format:fn-zfill #t (- scale))
  1477.            (set! format:fn-dot 0))
  1478.           ((> scale format:fn-dot)
  1479.            (format:fn-zfill #f (- scale format:fn-dot))
  1480.            (set! format:fn-dot scale))
  1481.           (else
  1482.            (set! format:fn-dot scale)))))
  1483.        #t)
  1484.  
  1485.     ;; do body      
  1486.     (set! c (string-ref num-str i))    ; parse the output of number->string
  1487.     (cond                ; which can be any valid number
  1488.      ((char-numeric? c)            ; representation of R4RS except 
  1489.       (if mantissa?            ; complex numbers
  1490.       (begin
  1491.         (if (char=? c #\0)
  1492.         (if all-zeros?
  1493.             (set! left-zeros (+ left-zeros 1)))
  1494.         (begin
  1495.           (set! all-zeros? #f)))
  1496.         (string-set! format:fn-str format:fn-len c)
  1497.         (set! format:fn-len (+ format:fn-len 1)))
  1498.       (begin
  1499.         (string-set! format:en-str format:en-len c)
  1500.         (set! format:en-len (+ format:en-len 1)))))
  1501.      ((or (char=? c #\-) (char=? c #\+))
  1502.       (if mantissa?
  1503.       (set! format:fn-pos? (char=? c #\+))
  1504.       (set! format:en-pos? (char=? c #\+))))
  1505.      ((char=? c #\.)
  1506.       (set! format:fn-dot format:fn-len))
  1507.      ((char=? c #\e)
  1508.       (set! mantissa? #f))
  1509.      ((char=? c #\E)
  1510.       (set! mantissa? #f))
  1511.      ((char-whitespace? c) #t)
  1512.      ((char=? c #\d) #t)        ; decimal radix prefix
  1513.      ((char=? c #\#) #t)
  1514.      (else
  1515.       (format:error "illegal character `~c' in number->string" c)))))
  1516.  
  1517. (define (format:en-int)            ; convert exponent string to integer
  1518.   (if (= format:en-len 0)
  1519.       0
  1520.       (do ((i 0 (+ i 1))
  1521.        (n 0))
  1522.       ((= i format:en-len) 
  1523.        (if format:en-pos?
  1524.            n
  1525.            (- n)))
  1526.     (set! n (+ (* n 10) (- (char->integer (string-ref format:en-str i))
  1527.                    format:zero-ch))))))
  1528.  
  1529. (define (format:en-set en)        ; set exponent string number
  1530.   (set! format:en-len 0)
  1531.   (set! format:en-pos? (>= en 0))
  1532.   (let ((en-str (number->string en)))
  1533.     (do ((i 0 (+ i 1))
  1534.      (en-len (string-length en-str))
  1535.      (c #f))
  1536.     ((= i en-len))
  1537.       (set! c (string-ref en-str i))
  1538.       (if (char-numeric? c)
  1539.       (begin
  1540.         (string-set! format:en-str format:en-len c)
  1541.         (set! format:en-len (+ format:en-len 1)))))))
  1542.  
  1543. (define (format:fn-zfill left? n)    ; fill current number string with 0s
  1544.   (if (> (+ n format:fn-len) format:fn-max) ; from the left or right
  1545.       (format:error "number is too long to format (enlarge format:fn-max)"))
  1546.   (set! format:fn-len (+ format:fn-len n))
  1547.   (if left?
  1548.       (do ((i format:fn-len (- i 1)))    ; fill n 0s to left
  1549.       ((< i 0))
  1550.     (string-set! format:fn-str i
  1551.              (if (< i n)
  1552.              #\0
  1553.              (string-ref format:fn-str (- i n)))))
  1554.       (do ((i (- format:fn-len n) (+ i 1))) ; fill n 0s to the right
  1555.       ((= i format:fn-len))
  1556.     (string-set! format:fn-str i #\0))))
  1557.  
  1558. (define (format:fn-shiftleft n)        ; shift left current number n positions
  1559.   (if (> n format:fn-len)
  1560.       (format:error "internal error in format:fn-shiftleft (~d,~d)"
  1561.             n format:fn-len))
  1562.   (do ((i n (+ i 1)))
  1563.       ((= i format:fn-len)
  1564.        (set! format:fn-len (- format:fn-len n)))
  1565.     (string-set! format:fn-str (- i n) (string-ref format:fn-str i))))
  1566.  
  1567. (define (format:fn-round digits)    ; round format:fn-str
  1568.   (set! digits (+ digits format:fn-dot))
  1569.   (do ((i digits (- i 1))        ; "099",2 -> "10"
  1570.        (c 5))                ; "023",2 -> "02"
  1571.       ((or (= c 0) (< i 0))        ; "999",2 -> "100"
  1572.        (if (= c 1)            ; "005",2 -> "01"
  1573.        (begin            ; carry overflow
  1574.          (set! format:fn-len digits)
  1575.          (format:fn-zfill #t 1)    ; add a 1 before fn-str
  1576.          (string-set! format:fn-str 0 #\1)
  1577.          (set! format:fn-dot (+ format:fn-dot 1)))
  1578.        (set! format:fn-len digits)))
  1579.     (set! c (+ (- (char->integer (string-ref format:fn-str i))
  1580.           format:zero-ch) c))
  1581.     (string-set! format:fn-str i (integer->char
  1582.                   (if (< c 10) 
  1583.                       (+ c format:zero-ch)
  1584.                       (+ (- c 10) format:zero-ch))))
  1585.     (set! c (if (< c 10) 0 1))))
  1586.  
  1587. (define (format:fn-out modifier add-leading-zero?)
  1588.   (if format:fn-pos?
  1589.       (if (eq? modifier 'at) 
  1590.       (format:out-char #\+))
  1591.       (format:out-char #\-))
  1592.   (if (= format:fn-dot 0)
  1593.       (if add-leading-zero?
  1594.       (format:out-char #\0))
  1595.       (format:out-substr format:fn-str 0 format:fn-dot))
  1596.   (format:out-char #\.)
  1597.   (format:out-substr format:fn-str format:fn-dot format:fn-len))
  1598.  
  1599. (define (format:en-out edigits expch)
  1600.   (format:out-char (if expch (integer->char expch) format:expch))
  1601.   (format:out-char (if format:en-pos? #\+ #\-))
  1602.   (if edigits 
  1603.       (if (< format:en-len edigits)
  1604.       (format:out-fill (- edigits format:en-len) #\0)))
  1605.   (format:out-substr format:en-str 0 format:en-len))
  1606.  
  1607. (define (format:fn-strip)        ; strip trailing zeros but one
  1608.   (string-set! format:fn-str format:fn-len #\0)
  1609.   (do ((i format:fn-len (- i 1)))
  1610.       ((or (not (char=? (string-ref format:fn-str i) #\0))
  1611.        (<= i format:fn-dot))
  1612.        (set! format:fn-len (+ i 1)))))
  1613.  
  1614. (define (format:fn-zlead)        ; count leading zeros
  1615.   (do ((i 0 (+ i 1)))
  1616.       ((or (= i format:fn-len)
  1617.        (not (char=? (string-ref format:fn-str i) #\0)))
  1618.        (if (= i format:fn-len)        ; found a real zero
  1619.        0
  1620.        i))))
  1621.  
  1622.  
  1623. ;;; some global functions not found in SLIB
  1624.  
  1625. ;; string-index finds the index of the first occurence of the character `c'
  1626. ;; in the string `s'; it returns #f if there is no such character in `s'.
  1627.  
  1628. (define (string-index s c)
  1629.   (let ((slen-1 (- (string-length s) 1)))
  1630.     (let loop ((i 0))
  1631.       (cond
  1632.        ((char=? c (string-ref s i)) i)
  1633.        ((= i slen-1) #f)
  1634.        (else (loop (+ i 1)))))))
  1635.  
  1636. (define (string-capitalize-first str)    ; "hello" -> "Hello"
  1637.   (let ((cap-str (string-copy str))    ; "hELLO" -> "Hello"
  1638.     (non-first-alpha #f)        ; "*hello" -> "*Hello"
  1639.     (str-len (string-length str)))    ; "hello you" -> "Hello you"
  1640.     (do ((i 0 (+ i 1)))
  1641.     ((= i str-len) cap-str)
  1642.       (let ((c (string-ref str i)))
  1643.     (if (char-alphabetic? c)
  1644.         (if non-first-alpha
  1645.         (string-set! cap-str i (char-downcase c))
  1646.         (begin
  1647.           (set! non-first-alpha #t)
  1648.           (string-set! cap-str i (char-upcase c)))))))))
  1649.  
  1650. (define (list-head l k)
  1651.   (if (= k 0)
  1652.       '()
  1653.       (cons (car l) (list-head (cdr l) (- k 1)))))
  1654.  
  1655.  
  1656. ;; Aborts the program when a formatting error occures. This is a null
  1657. ;; argument closure to jump to the interpreters toplevel continuation.
  1658.  
  1659. (define format:abort (lambda () (slib:error "error in format")))
  1660.  
  1661. (define format format:format)
  1662.  
  1663. ;; If this is not possible then a continuation is used to recover
  1664. ;; properly from a format error. In this case format returns #f.
  1665.  
  1666. ;(define format:abort
  1667. ;  (lambda () (format:error-continuation #f)))
  1668.  
  1669. ;(define format
  1670. ;  (lambda args                ; wraps format:format with an error
  1671. ;    (call-with-current-continuation    ; continuation
  1672. ;     (lambda (cont)
  1673. ;       (set! format:error-continuation cont)
  1674. ;       (apply format:format args)))))
  1675.  
  1676. ;eof
  1677.