1 ;;;; functions to implement FORMAT and FORMATTER
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!FORMAT")
19 (defun format (destination control-string &rest format-arguments)
21 "Provides various facilities for formatting output.
22 CONTROL-STRING contains a string to be output, possibly with embedded
23 directives, which are flagged with the escape character \"~\". Directives
24 generally expand into additional text to be output, usually consuming one
25 or more of the FORMAT-ARGUMENTS in the process. A few useful directives
27 ~A or ~nA Prints one argument as if by PRINC
28 ~S or ~nS Prints one argument as if by PRIN1
29 ~D or ~nD Prints one argument as a decimal integer
33 where n is the width of the field in which the object is printed.
35 DESTINATION controls where the result will go. If DESTINATION is T, then
36 the output is sent to the standard output stream. If it is NIL, then the
37 output is returned in a string as the value of the call. Otherwise,
38 DESTINATION must be a stream to which the output will be sent.
40 Example: (FORMAT NIL \"The answer is ~D.\" 10) => \"The answer is 10.\"
42 FORMAT has many additional capabilities not described here. Consult the
44 (etypecase destination
46 (with-output-to-string (stream)
47 (%format stream control-string format-arguments)))
49 (with-output-to-string (stream destination)
50 (%format stream control-string format-arguments)))
52 (%format *standard-output* control-string format-arguments)
55 (%format destination control-string format-arguments)
58 (defun %format (stream string-or-fun orig-args &optional (args orig-args))
59 (if (functionp string-or-fun)
60 (apply string-or-fun stream args)
62 (let* ((string (etypecase string-or-fun
66 (coerce string-or-fun 'simple-string))))
67 (*default-format-error-control-string* string)
68 (*logical-block-popper* nil))
69 (interpret-directive-list stream (tokenize-control-string string)
72 (defun interpret-directive-list (stream directives orig-args args)
74 (let ((directive (car directives)))
77 (write-string directive stream)
78 (interpret-directive-list stream (cdr directives) orig-args args))
80 (multiple-value-bind (new-directives new-args)
82 (svref *format-directive-interpreters*
83 (char-code (format-directive-character
85 (*default-format-error-offset*
86 (1- (format-directive-end directive))))
89 :complaint "unknown format directive"))
90 (multiple-value-bind (new-directives new-args)
91 (funcall function stream directive
92 (cdr directives) orig-args args)
93 (values new-directives new-args)))
94 (interpret-directive-list stream new-directives
95 orig-args new-args)))))
98 ;;;; FORMAT directive definition macros and runtime support
100 (eval-when (:compile-toplevel :execute)
102 ;;; This macro is used to extract the next argument from the current arg list.
103 ;;; This is the version used by format directive interpreters.
104 (sb!xc:defmacro next-arg (&optional offset)
108 :complaint "no more arguments"
110 `(:offset ,offset))))
111 (when *logical-block-popper*
112 (funcall *logical-block-popper*))
115 (sb!xc:defmacro def-complex-format-interpreter (char lambda-list &body body)
118 "~:@(~:C~)-FORMAT-DIRECTIVE-INTERPRETER"
121 (directives (if lambda-list (car (last lambda-list)) (gensym))))
123 (defun ,defun-name (stream ,directive ,directives orig-args args)
124 (declare (ignorable stream orig-args args))
126 `((let ,(mapcar #'(lambda (var)
128 (,(intern (concatenate
132 (symbol-package 'foo))
134 (butlast lambda-list))
135 (values (progn ,@body) args)))
136 `((declare (ignore ,directive ,directives))
138 (%set-format-directive-interpreter ,char #',defun-name))))
140 (sb!xc:defmacro def-format-interpreter (char lambda-list &body body)
141 (let ((directives (gensym)))
142 `(def-complex-format-interpreter ,char (,@lambda-list ,directives)
146 (sb!xc:defmacro interpret-bind-defaults (specs params &body body)
147 (once-only ((params params))
148 (collect ((bindings))
150 (destructuring-bind (var default) spec
151 (bindings `(,var (let* ((param-and-offset (pop ,params))
152 (offset (car param-and-offset))
153 (param (cdr param-and-offset)))
155 (:arg (next-arg offset))
156 (:remaining (length args))
163 "too many parameters, expected no more than ~D"
164 :arguments (list ,(length specs))
165 :offset (caar ,params)))
170 ;;;; format interpreters and support functions for simple output
172 (defun format-write-field (stream string mincol colinc minpad padchar padleft)
174 (write-string string stream))
176 (write-char padchar stream))
177 (do ((chars (+ (length string) minpad) (+ chars colinc)))
180 (write-char padchar stream)))
182 (write-string string stream)))
184 (defun format-princ (stream arg colonp atsignp mincol colinc minpad padchar)
185 (format-write-field stream
186 (if (or arg (not colonp))
187 (princ-to-string arg)
189 mincol colinc minpad padchar atsignp))
191 (def-format-interpreter #\A (colonp atsignp params)
193 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
196 (format-princ stream (next-arg) colonp atsignp
197 mincol colinc minpad padchar))
198 (princ (if colonp (or (next-arg) "()") (next-arg)) stream)))
200 (defun format-prin1 (stream arg colonp atsignp mincol colinc minpad padchar)
201 (format-write-field stream
202 (if (or arg (not colonp))
203 (prin1-to-string arg)
205 mincol colinc minpad padchar atsignp))
207 (def-format-interpreter #\S (colonp atsignp params)
209 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
212 (format-prin1 stream (next-arg) colonp atsignp
213 mincol colinc minpad padchar)))
215 (let ((arg (next-arg)))
218 (princ "()" stream))))
220 (prin1 (next-arg) stream))))
222 (def-format-interpreter #\C (colonp atsignp params)
223 (interpret-bind-defaults () params
225 (format-print-named-character (next-arg) stream)
227 (prin1 (next-arg) stream)
228 (write-char (next-arg) stream)))))
230 (defun format-print-named-character (char stream)
231 (let* ((name (char-name char)))
233 (write-string (string-capitalize name) stream))
234 ((<= 0 (char-code char) 31)
235 ;; Print control characters as "^"<char>
236 (write-char #\^ stream)
237 (write-char (code-char (+ 64 (char-code char))) stream))
239 (write-char char stream)))))
241 (def-format-interpreter #\W (colonp atsignp params)
242 (interpret-bind-defaults () params
243 (let ((*print-pretty* (or colonp *print-pretty*))
244 (*print-level* (and atsignp *print-level*))
245 (*print-length* (and atsignp *print-length*)))
246 (output-object (next-arg) stream))))
248 ;;;; format interpreters and support functions for integer output
250 ;;; FORMAT-PRINT-NUMBER does most of the work for the numeric printing
251 ;;; directives. The parameters are interpreted as defined for ~D.
252 (defun format-print-integer (stream number print-commas-p print-sign-p
253 radix mincol padchar commachar commainterval)
254 (let ((*print-base* radix)
256 (if (integerp number)
257 (let* ((text (princ-to-string (abs number)))
258 (commaed (if print-commas-p
259 (format-add-commas text commachar commainterval)
261 (signed (cond ((minusp number)
262 (concatenate 'string "-" commaed))
264 (concatenate 'string "+" commaed))
266 ;; colinc = 1, minpad = 0, padleft = t
267 (format-write-field stream signed mincol 1 0 padchar t))
270 (defun format-add-commas (string commachar commainterval)
271 (let ((length (length string)))
272 (multiple-value-bind (commas extra) (truncate (1- length) commainterval)
273 (let ((new-string (make-string (+ length commas)))
274 (first-comma (1+ extra)))
275 (replace new-string string :end1 first-comma :end2 first-comma)
276 (do ((src first-comma (+ src commainterval))
277 (dst first-comma (+ dst commainterval 1)))
279 (setf (schar new-string dst) commachar)
280 (replace new-string string :start1 (1+ dst)
281 :start2 src :end2 (+ src commainterval)))
284 ;;; FIXME: This is only needed in this file, could be defined with
285 ;;; SB!XC:DEFMACRO inside EVAL-WHEN
286 (defmacro interpret-format-integer (base)
287 `(if (or colonp atsignp params)
288 (interpret-bind-defaults
289 ((mincol 0) (padchar #\space) (commachar #\,) (commainterval 3))
291 (format-print-integer stream (next-arg) colonp atsignp ,base mincol
292 padchar commachar commainterval))
293 (write (next-arg) :stream stream :base ,base :radix nil :escape nil)))
295 (def-format-interpreter #\D (colonp atsignp params)
296 (interpret-format-integer 10))
298 (def-format-interpreter #\B (colonp atsignp params)
299 (interpret-format-integer 2))
301 (def-format-interpreter #\O (colonp atsignp params)
302 (interpret-format-integer 8))
304 (def-format-interpreter #\X (colonp atsignp params)
305 (interpret-format-integer 16))
307 (def-format-interpreter #\R (colonp atsignp params)
309 (interpret-bind-defaults
310 ((base 10) (mincol 0) (padchar #\space) (commachar #\,)
313 (format-print-integer stream (next-arg) colonp atsignp base mincol
314 padchar commachar commainterval))
317 (format-print-old-roman stream (next-arg))
318 (format-print-roman stream (next-arg)))
320 (format-print-ordinal stream (next-arg))
321 (format-print-cardinal stream (next-arg))))))
323 (defconstant cardinal-ones
324 #(nil "one" "two" "three" "four" "five" "six" "seven" "eight" "nine"))
326 (defconstant cardinal-tens
327 #(nil nil "twenty" "thirty" "forty"
328 "fifty" "sixty" "seventy" "eighty" "ninety"))
330 (defconstant cardinal-teens
331 #("ten" "eleven" "twelve" "thirteen" "fourteen" ;;; RAD
332 "fifteen" "sixteen" "seventeen" "eighteen" "nineteen"))
334 (defconstant cardinal-periods
335 #("" " thousand" " million" " billion" " trillion" " quadrillion"
336 " quintillion" " sextillion" " septillion" " octillion" " nonillion"
337 " decillion" " undecillion" " duodecillion" " tredecillion"
338 " quattuordecillion" " quindecillion" " sexdecillion" " septendecillion"
339 " octodecillion" " novemdecillion" " vigintillion"))
341 (defconstant ordinal-ones
342 #(nil "first" "second" "third" "fourth"
343 "fifth" "sixth" "seventh" "eighth" "ninth")
345 "Table of ordinal ones-place digits in English")
347 (defconstant ordinal-tens
348 #(nil "tenth" "twentieth" "thirtieth" "fortieth"
349 "fiftieth" "sixtieth" "seventieth" "eightieth" "ninetieth")
351 "Table of ordinal tens-place digits in English")
353 (defun format-print-small-cardinal (stream n)
354 (multiple-value-bind (hundreds rem) (truncate n 100)
355 (when (plusp hundreds)
356 (write-string (svref cardinal-ones hundreds) stream)
357 (write-string " hundred" stream)
359 (write-char #\space stream)))
361 (multiple-value-bind (tens ones) (truncate rem 10)
363 (write-string (svref cardinal-tens tens) stream)
365 (write-char #\- stream)
366 (write-string (svref cardinal-ones ones) stream)))
368 (write-string (svref cardinal-teens ones) stream))
370 (write-string (svref cardinal-ones ones) stream)))))))
372 (defun format-print-cardinal (stream n)
374 (write-string "negative " stream)
375 (format-print-cardinal-aux stream (- n) 0 n))
377 (write-string "zero" stream))
379 (format-print-cardinal-aux stream n 0 n))))
381 (defun format-print-cardinal-aux (stream n period err)
382 (multiple-value-bind (beyond here) (truncate n 1000)
383 (unless (<= period 20)
384 (error "number too large to print in English: ~:D" err))
385 (unless (zerop beyond)
386 (format-print-cardinal-aux stream beyond (1+ period) err))
388 (unless (zerop beyond)
389 (write-char #\space stream))
390 (format-print-small-cardinal stream here)
391 (write-string (svref cardinal-periods period) stream))))
393 (defun format-print-ordinal (stream n)
395 (write-string "negative " stream))
396 (let ((number (abs n)))
397 (multiple-value-bind (top bot) (truncate number 100)
399 (format-print-cardinal stream (- number bot)))
400 (when (and (plusp top) (plusp bot))
401 (write-char #\space stream))
402 (multiple-value-bind (tens ones) (truncate bot 10)
403 (cond ((= bot 12) (write-string "twelfth" stream))
405 (write-string (svref cardinal-teens ones) stream);;;RAD
406 (write-string "th" stream))
407 ((and (zerop tens) (plusp ones))
408 (write-string (svref ordinal-ones ones) stream))
409 ((and (zerop ones)(plusp tens))
410 (write-string (svref ordinal-tens tens) stream))
412 (write-string (svref cardinal-tens tens) stream)
413 (write-char #\- stream)
414 (write-string (svref ordinal-ones ones) stream))
416 (write-string "th" stream))
418 (write-string "zeroth" stream)))))))
420 ;;; Print Roman numerals
422 (defun format-print-old-roman (stream n)
424 (error "Number too large to print in old Roman numerals: ~:D" n))
425 (do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
426 (val-list '(500 100 50 10 5 1) (cdr val-list))
427 (cur-char #\M (car char-list))
428 (cur-val 1000 (car val-list))
429 (start n (do ((i start (progn
430 (write-char cur-char stream)
435 (defun format-print-roman (stream n)
437 (error "Number too large to print in Roman numerals: ~:D" n))
438 (do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
439 (val-list '(500 100 50 10 5 1) (cdr val-list))
440 (sub-chars '(#\C #\X #\X #\I #\I) (cdr sub-chars))
441 (sub-val '(100 10 10 1 1 0) (cdr sub-val))
442 (cur-char #\M (car char-list))
443 (cur-val 1000 (car val-list))
444 (cur-sub-char #\C (car sub-chars))
445 (cur-sub-val 100 (car sub-val))
446 (start n (do ((i start (progn
447 (write-char cur-char stream)
450 (cond ((<= (- cur-val cur-sub-val) i)
451 (write-char cur-sub-char stream)
452 (write-char cur-char stream)
453 (- i (- cur-val cur-sub-val)))
459 (def-format-interpreter #\P (colonp atsignp params)
460 (interpret-bind-defaults () params
461 (let ((arg (if colonp
462 (if (eq orig-args args)
464 :complaint "no previous argument")
465 (do ((arg-ptr orig-args (cdr arg-ptr)))
466 ((eq (cdr arg-ptr) args)
470 (write-string (if (eql arg 1) "y" "ies") stream)
471 (unless (eql arg 1) (write-char #\s stream))))))
473 ;;;; format interpreters and support functions for floating point output
475 (defun decimal-string (n)
476 (write-to-string n :base 10 :radix nil :escape nil))
478 (def-format-interpreter #\F (colonp atsignp params)
482 "cannot specify the colon modifier with this directive"))
483 (interpret-bind-defaults ((w nil) (d nil) (k nil) (ovf nil) (pad #\space))
485 (format-fixed stream (next-arg) w d k ovf pad atsignp)))
487 (defun format-fixed (stream number w d k ovf pad atsign)
490 (format-fixed-aux stream number w d k ovf pad atsign)
491 (if (rationalp number)
492 (format-fixed-aux stream
493 (coerce number 'single-float)
494 w d k ovf pad atsign)
495 (format-write-field stream
496 (decimal-string number)
498 (format-princ stream number nil nil w 1 0 pad)))
500 ;;; We return true if we overflowed, so that ~G can output the overflow char
501 ;;; instead of spaces.
502 (defun format-fixed-aux (stream number w d k ovf pad atsign)
506 (or (float-infinity-p number)
507 (float-nan-p number))))
508 (prin1 number stream)
512 (when (and w (or atsign (minusp number))) (decf spaceleft))
513 (multiple-value-bind (str len lpoint tpoint)
514 (sb!impl::flonum-to-string (abs number) spaceleft d k)
515 ;;if caller specifically requested no fraction digits, suppress the
516 ;;optional trailing zero
517 (when (and d (zerop d)) (setq tpoint nil))
520 ;;optional leading zero
522 (if (or (> spaceleft 0) tpoint) ;force at least one digit
525 ;;optional trailing zero
530 (cond ((and w (< spaceleft 0) ovf)
531 ;;field width overflow
532 (dotimes (i w) (write-char ovf stream))
535 (when w (dotimes (i spaceleft) (write-char pad stream)))
537 (write-char #\- stream)
538 (if atsign (write-char #\+ stream)))
539 (when lpoint (write-char #\0 stream))
540 (write-string str stream)
541 (when tpoint (write-char #\0 stream))
544 (def-format-interpreter #\E (colonp atsignp params)
548 "cannot specify the colon modifier with this directive"))
549 (interpret-bind-defaults
550 ((w nil) (d nil) (e nil) (k 1) (ovf nil) (pad #\space) (mark nil))
552 (format-exponential stream (next-arg) w d e k ovf pad mark atsignp)))
554 (defun format-exponential (stream number w d e k ovf pad marker atsign)
557 (format-exp-aux stream number w d e k ovf pad marker atsign)
558 (if (rationalp number)
559 (format-exp-aux stream
560 (coerce number 'single-float)
561 w d e k ovf pad marker atsign)
562 (format-write-field stream
563 (decimal-string number)
565 (format-princ stream number nil nil w 1 0 pad)))
567 (defun format-exponent-marker (number)
568 (if (typep number *read-default-float-format*)
576 ;;; Here we prevent the scale factor from shifting all significance out of
577 ;;; a number to the right. We allow insignificant zeroes to be shifted in
578 ;;; to the left right, athough it is an error to specify k and d such that this
579 ;;; occurs. Perhaps we should detect both these condtions and flag them as
580 ;;; errors. As for now, we let the user get away with it, and merely guarantee
581 ;;; that at least one significant digit will appear.
583 ;;; toy@rtp.ericsson.se: The Hyperspec seems to say that the exponent
584 ;;; marker is always printed. Make it so. Also, the original version
585 ;;; causes errors when printing infinities or NaN's. The Hyperspec is
586 ;;; silent here, so let's just print out infinities and NaN's instead
587 ;;; of causing an error.
588 (defun format-exp-aux (stream number w d e k ovf pad marker atsign)
589 (if (and (floatp number)
590 (or (float-infinity-p number)
591 (float-nan-p number)))
592 (prin1 number stream)
593 (multiple-value-bind (num expt) (sb!impl::scale-exponent (abs number))
594 (let* ((expt (- expt k))
595 (estr (decimal-string (abs expt)))
596 (elen (if e (max (length estr) e) (length estr)))
597 (fdig (if d (if (plusp k) (1+ (- d k)) d) nil))
598 (fmin (if (minusp k) (- 1 k) nil))
601 (if (or atsign (minusp number))
604 (if (and w ovf e (> elen e)) ;exponent overflow
605 (dotimes (i w) (write-char ovf stream))
606 (multiple-value-bind (fstr flen lpoint)
607 (sb!impl::flonum-to-string num spaceleft fdig k fmin)
609 (decf spaceleft flen)
614 (cond ((and w (< spaceleft 0) ovf)
615 ;;significand overflow
616 (dotimes (i w) (write-char ovf stream)))
618 (dotimes (i spaceleft) (write-char pad stream)))
620 (write-char #\- stream)
621 (if atsign (write-char #\+ stream)))
622 (when lpoint (write-char #\0 stream))
623 (write-string fstr stream)
624 (write-char (if marker
626 (format-exponent-marker number))
628 (write-char (if (minusp expt) #\- #\+) stream)
630 ;;zero-fill before exponent if necessary
631 (dotimes (i (- e (length estr)))
632 (write-char #\0 stream)))
633 (write-string estr stream)))))))))
635 (def-format-interpreter #\G (colonp atsignp params)
639 "cannot specify the colon modifier with this directive"))
640 (interpret-bind-defaults
641 ((w nil) (d nil) (e nil) (k nil) (ovf nil) (pad #\space) (mark nil))
643 (format-general stream (next-arg) w d e k ovf pad mark atsignp)))
645 (defun format-general (stream number w d e k ovf pad marker atsign)
648 (format-general-aux stream number w d e k ovf pad marker atsign)
649 (if (rationalp number)
650 (format-general-aux stream
651 (coerce number 'single-float)
652 w d e k ovf pad marker atsign)
653 (format-write-field stream
654 (decimal-string number)
656 (format-princ stream number nil nil w 1 0 pad)))
658 ;;; toy@rtp.ericsson.se: Same change as for format-exp-aux.
659 (defun format-general-aux (stream number w d e k ovf pad marker atsign)
660 (if (and (floatp number)
661 (or (float-infinity-p number)
662 (float-nan-p number)))
663 (prin1 number stream)
664 (multiple-value-bind (ignore n) (sb!impl::scale-exponent (abs number))
665 (declare (ignore ignore))
666 ;; KLUDGE: Default d if omitted. The procedure is taken directly from
667 ;; the definition given in the manual, and is not very efficient, since
668 ;; we generate the digits twice. Future maintainers are encouraged to
669 ;; improve on this. -- rtoy?? 1998??
671 (multiple-value-bind (str len)
672 (sb!impl::flonum-to-string (abs number))
673 (declare (ignore str))
674 (let ((q (if (= len 1) 1 (1- len))))
675 (setq d (max q (min n 7))))))
676 (let* ((ee (if e (+ e 2) 4))
677 (ww (if w (- w ee) nil))
680 (let ((char (if (format-fixed-aux stream number ww dd nil
684 (dotimes (i ee) (write-char char stream))))
686 (format-exp-aux stream number w d e (or k 1)
687 ovf pad marker atsign)))))))
689 (def-format-interpreter #\$ (colonp atsignp params)
690 (interpret-bind-defaults ((d 2) (n 1) (w 0) (pad #\space)) params
691 (format-dollars stream (next-arg) d n w pad colonp atsignp)))
693 (defun format-dollars (stream number d n w pad colon atsign)
694 (if (rationalp number) (setq number (coerce number 'single-float)))
696 (let* ((signstr (if (minusp number) "-" (if atsign "+" "")))
697 (signlen (length signstr)))
698 (multiple-value-bind (str strlen ig2 ig3 pointplace)
699 (sb!impl::flonum-to-string number nil d nil)
700 (declare (ignore ig2 ig3))
701 (when colon (write-string signstr stream))
702 (dotimes (i (- w signlen (- n pointplace) strlen))
703 (write-char pad stream))
704 (unless colon (write-string signstr stream))
705 (dotimes (i (- n pointplace)) (write-char #\0 stream))
706 (write-string str stream)))
707 (format-write-field stream
708 (decimal-string number)
711 ;;;; format interpreters and support functions for line/page breaks etc.
713 (def-format-interpreter #\% (colonp atsignp params)
714 (when (or colonp atsignp)
717 "cannot specify either colon or atsign for this directive"))
718 (interpret-bind-defaults ((count 1)) params
722 (def-format-interpreter #\& (colonp atsignp params)
723 (when (or colonp atsignp)
726 "cannot specify either colon or atsign for this directive"))
727 (interpret-bind-defaults ((count 1)) params
729 (dotimes (i (1- count))
732 (def-format-interpreter #\| (colonp atsignp params)
733 (when (or colonp atsignp)
736 "cannot specify either colon or atsign for this directive"))
737 (interpret-bind-defaults ((count 1)) params
739 (write-char (code-char form-feed-char-code) stream))))
741 (def-format-interpreter #\~ (colonp atsignp params)
742 (when (or colonp atsignp)
745 "cannot specify either colon or atsign for this directive"))
746 (interpret-bind-defaults ((count 1)) params
748 (write-char #\~ stream))))
750 (def-complex-format-interpreter #\newline (colonp atsignp params directives)
751 (when (and colonp atsignp)
754 "cannot specify both colon and atsign for this directive"))
755 (interpret-bind-defaults () params
757 (write-char #\newline stream)))
758 (if (and (not colonp)
760 (simple-string-p (car directives)))
761 (cons (string-left-trim *format-whitespace-chars*
766 ;;;; format interpreters and support functions for tabs and simple pretty
769 (def-format-interpreter #\T (colonp atsignp params)
771 (interpret-bind-defaults ((n 1) (m 1)) params
772 (pprint-tab (if atsignp :section-relative :section) n m stream))
774 (interpret-bind-defaults ((colrel 1) (colinc 1)) params
775 (format-relative-tab stream colrel colinc))
776 (interpret-bind-defaults ((colnum 1) (colinc 1)) params
777 (format-absolute-tab stream colnum colinc)))))
779 (defun output-spaces (stream n)
780 (let ((spaces #.(make-string 100 :initial-element #\space)))
782 (when (< n (length spaces))
784 (write-string spaces stream)
785 (decf n (length spaces)))
786 (write-string spaces stream :end n)))
788 (defun format-relative-tab (stream colrel colinc)
789 (if (sb!pretty:pretty-stream-p stream)
790 (pprint-tab :line-relative colrel colinc stream)
791 (let* ((cur (sb!impl::charpos stream))
792 (spaces (if (and cur (plusp colinc))
793 (- (* (ceiling (+ cur colrel) colinc) colinc) cur)
795 (output-spaces stream spaces))))
797 (defun format-absolute-tab (stream colnum colinc)
798 (if (sb!pretty:pretty-stream-p stream)
799 (pprint-tab :line colnum colinc stream)
800 (let ((cur (sb!impl::charpos stream)))
802 (write-string " " stream))
804 (output-spaces stream (- colnum cur)))
806 (unless (zerop colinc)
807 (output-spaces stream
808 (- colinc (rem (- cur colnum) colinc)))))))))
810 (def-format-interpreter #\_ (colonp atsignp params)
811 (interpret-bind-defaults () params
812 (pprint-newline (if colonp
821 (def-format-interpreter #\I (colonp atsignp params)
824 :complaint "cannot specify the at-sign modifier"))
825 (interpret-bind-defaults ((n 0)) params
826 (pprint-indent (if colonp :current :block) n stream)))
828 ;;;; format interpreter for ~*
830 (def-format-interpreter #\* (colonp atsignp params)
834 :complaint "cannot specify both colon and at-sign")
835 (interpret-bind-defaults ((posn 0)) params
836 (if (<= 0 posn (length orig-args))
837 (setf args (nthcdr posn orig-args))
839 :complaint "Index ~D is out of bounds. (It should ~
840 have been between 0 and ~D.)"
841 :arguments (list posn (length orig-args))))))
843 (interpret-bind-defaults ((n 1)) params
844 (do ((cur-posn 0 (1+ cur-posn))
845 (arg-ptr orig-args (cdr arg-ptr)))
847 (let ((new-posn (- cur-posn n)))
848 (if (<= 0 new-posn (length orig-args))
849 (setf args (nthcdr new-posn orig-args))
852 "Index ~D is out of bounds. (It should
853 have been between 0 and ~D.)"
855 (list new-posn (length orig-args))))))))
856 (interpret-bind-defaults ((n 1)) params
860 ;;;; format interpreter for indirection
862 (def-format-interpreter #\? (colonp atsignp params string end)
865 :complaint "cannot specify the colon modifier"))
866 (interpret-bind-defaults () params
869 #'(lambda (condition)
872 "~A~%while processing indirect format string:"
873 :arguments (list condition)
875 :control-string string
878 (setf args (%format stream (next-arg) orig-args args))
879 (%format stream (next-arg) (next-arg))))))
881 ;;;; format interpreters for capitalization
883 (def-complex-format-interpreter #\( (colonp atsignp params directives)
884 (let ((close (find-directive directives #\) nil)))
887 :complaint "no corresponding close paren"))
888 (interpret-bind-defaults () params
889 (let* ((posn (position close directives))
890 (before (subseq directives 0 posn))
891 (after (nthcdr (1+ posn) directives))
892 (stream (make-case-frob-stream stream
900 (setf args (interpret-directive-list stream before orig-args args))
903 (def-complex-format-interpreter #\) ()
905 :complaint "no corresponding open paren"))
907 ;;;; format interpreters and support functions for conditionalization
909 (def-complex-format-interpreter #\[ (colonp atsignp params directives)
910 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
911 (parse-conditional-directive directives)
917 "cannot specify both the colon and at-sign modifiers")
921 "can only specify one section")
922 (interpret-bind-defaults () params
923 (let ((prev-args args)
926 (interpret-directive-list stream
932 (if (= (length sublists) 2)
933 (interpret-bind-defaults () params
935 (interpret-directive-list stream (car sublists)
937 (interpret-directive-list stream (cadr sublists)
941 "must specify exactly two sections"))
942 (interpret-bind-defaults ((index (next-arg))) params
943 (let* ((default (and last-semi-with-colon-p
945 (last (1- (length sublists)))
947 (if (<= 0 index last)
948 (nth (- last index) sublists)
950 (interpret-directive-list stream sublist orig-args
954 (def-complex-format-interpreter #\; ()
957 "~~; not contained within either ~~[...~~] or ~~<...~~>"))
959 (def-complex-format-interpreter #\] ()
962 "no corresponding open bracket"))
964 ;;;; format interpreter for up-and-out
966 (defvar *outside-args*)
968 (def-format-interpreter #\^ (colonp atsignp params)
971 :complaint "cannot specify the at-sign modifier"))
972 (when (and colonp (not *up-up-and-out-allowed*))
974 :complaint "attempt to use ~~:^ outside a ~~:{...~~} construct"))
975 (when (case (length params)
977 (null *outside-args*)
979 (1 (interpret-bind-defaults ((count 0)) params
981 (2 (interpret-bind-defaults ((arg1 0) (arg2 0)) params
983 (t (interpret-bind-defaults ((arg1 0) (arg2 0) (arg3 0)) params
984 (<= arg1 arg2 arg3))))
985 (throw (if colonp 'up-up-and-out 'up-and-out)
988 ;;;; format interpreters for iteration
990 (def-complex-format-interpreter #\{
991 (colonp atsignp params string end directives)
992 (let ((close (find-directive directives #\} nil)))
996 "no corresponding close brace"))
997 (interpret-bind-defaults ((max-count nil)) params
998 (let* ((closed-with-colon (format-directive-colonp close))
999 (posn (position close directives))
1000 (insides (if (zerop posn)
1002 (subseq directives 0 posn)))
1003 (*up-up-and-out-allowed* colonp))
1005 ((do-guts (orig-args args)
1009 #'(lambda (condition)
1010 (error 'format-error
1012 "~A~%while processing indirect format string:"
1013 :arguments (list condition)
1015 :control-string string
1016 :offset (1- end)))))
1017 (%format stream insides orig-args args))
1018 (interpret-directive-list stream insides
1020 (bind-args (orig-args args)
1022 (let* ((arg (next-arg))
1023 (*logical-block-popper* nil)
1024 (*outside-args* args))
1028 (do-guts orig-args args)))
1029 (do-loop (orig-args args)
1030 (catch (if colonp 'up-up-and-out 'up-and-out)
1032 (when (and (not closed-with-colon) (null args))
1034 (when (and max-count (minusp (decf max-count)))
1036 (setf args (bind-args orig-args args))
1037 (when (and closed-with-colon (null args))
1041 (setf args (do-loop orig-args args))
1042 (let ((arg (next-arg))
1043 (*logical-block-popper* nil))
1045 (nthcdr (1+ posn) directives))))))
1047 (def-complex-format-interpreter #\} ()
1048 (error 'format-error
1049 :complaint "no corresponding open brace"))
1051 ;;;; format interpreters and support functions for justification
1053 (def-complex-format-interpreter #\<
1054 (colonp atsignp params string end directives)
1055 (multiple-value-bind (segments first-semi close remaining)
1056 (parse-format-justification directives)
1058 (if (format-directive-colonp close)
1059 (multiple-value-bind (prefix per-line-p insides suffix)
1060 (parse-format-logical-block segments colonp first-semi
1061 close params string end)
1062 (interpret-format-logical-block stream orig-args args
1063 prefix per-line-p insides
1065 (interpret-format-justification stream orig-args args
1066 segments colonp atsignp
1067 first-semi params)))
1070 (defun interpret-format-justification
1071 (stream orig-args args segments colonp atsignp first-semi params)
1072 (interpret-bind-defaults
1073 ((mincol 0) (colinc 1) (minpad 0) (padchar #\space))
1075 (let ((newline-string nil)
1081 (when (and first-semi (format-directive-colonp first-semi))
1082 (interpret-bind-defaults
1084 (len (or (sb!impl::line-length stream) 72)))
1085 (format-directive-params first-semi)
1086 (setf newline-string
1087 (with-output-to-string (stream)
1089 (interpret-directive-list stream
1093 (setf extra-space extra)
1094 (setf line-len len)))
1095 (dolist (segment segments)
1096 (push (with-output-to-string (stream)
1098 (interpret-directive-list stream segment
1102 (format-justification stream newline-string extra-space line-len strings
1103 colonp atsignp mincol colinc minpad padchar)))
1106 (defun format-justification (stream newline-prefix extra-space line-len strings
1107 pad-left pad-right mincol colinc minpad padchar)
1108 (setf strings (reverse strings))
1109 (when (and (not pad-left) (not pad-right) (null (cdr strings)))
1111 (let* ((num-gaps (+ (1- (length strings))
1113 (if pad-right 1 0)))
1114 (chars (+ (* num-gaps minpad)
1116 for string in strings
1117 summing (length string))))
1118 (length (if (> chars mincol)
1119 (+ mincol (* (ceiling (- chars mincol) colinc) colinc))
1121 (padding (- length chars)))
1122 (when (and newline-prefix
1123 (> (+ (or (sb!impl::charpos stream) 0)
1126 (write-string newline-prefix stream))
1127 (flet ((do-padding ()
1128 (let ((pad-len (truncate padding num-gaps)))
1129 (decf padding pad-len)
1131 (dotimes (i pad-len) (write-char padchar stream)))))
1135 (write-string (car strings) stream)
1136 (dolist (string (cdr strings))
1138 (write-string string stream)))
1142 (defun interpret-format-logical-block
1143 (stream orig-args args prefix per-line-p insides suffix atsignp)
1144 (let ((arg (if atsignp args (next-arg))))
1146 (pprint-logical-block
1147 (stream arg :per-line-prefix prefix :suffix suffix)
1148 (let ((*logical-block-popper* #'(lambda () (pprint-pop))))
1150 (interpret-directive-list stream insides
1151 (if atsignp orig-args arg)
1153 (pprint-logical-block (stream arg :prefix prefix :suffix suffix)
1154 (let ((*logical-block-popper* #'(lambda () (pprint-pop))))
1156 (interpret-directive-list stream insides
1157 (if atsignp orig-args arg)
1159 (if atsignp nil args))
1161 ;;;; format interpreter and support functions for user-defined method
1163 (def-format-interpreter #\/ (string start end colonp atsignp params)
1164 (let ((symbol (extract-user-function-name string start end)))
1166 (dolist (param-and-offset params)
1167 (let ((param (cdr param-and-offset)))
1169 (:arg (args (next-arg)))
1170 (:remaining (args (length args)))
1172 (apply (fdefinition symbol) stream (next-arg) colonp atsignp (args)))))