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")
16 (defun format (destination control-string &rest format-arguments)
18 "Provides various facilities for formatting output.
19 CONTROL-STRING contains a string to be output, possibly with embedded
20 directives, which are flagged with the escape character \"~\". Directives
21 generally expand into additional text to be output, usually consuming one
22 or more of the FORMAT-ARGUMENTS in the process. A few useful directives
24 ~A or ~nA Prints one argument as if by PRINC
25 ~S or ~nS Prints one argument as if by PRIN1
26 ~D or ~nD Prints one argument as a decimal integer
30 where n is the width of the field in which the object is printed.
32 DESTINATION controls where the result will go. If DESTINATION is T, then
33 the output is sent to the standard output stream. If it is NIL, then the
34 output is returned in a string as the value of the call. Otherwise,
35 DESTINATION must be a stream to which the output will be sent.
37 Example: (FORMAT NIL \"The answer is ~D.\" 10) => \"The answer is 10.\"
39 FORMAT has many additional capabilities not described here. Consult the
41 (etypecase destination
43 (with-output-to-string (stream)
44 (%format stream control-string format-arguments)))
46 (with-output-to-string (stream destination)
47 (%format stream control-string format-arguments)))
49 (%format *standard-output* control-string format-arguments)
52 (%format destination control-string format-arguments)
55 (defun %format (stream string-or-fun orig-args &optional (args orig-args))
56 (if (functionp string-or-fun)
57 (apply string-or-fun stream args)
59 (let* ((string (etypecase string-or-fun
63 (coerce string-or-fun 'simple-string))))
64 (*default-format-error-control-string* string)
65 (*logical-block-popper* nil))
66 (interpret-directive-list stream (tokenize-control-string string)
69 (defun interpret-directive-list (stream directives orig-args args)
71 (let ((directive (car directives)))
74 (write-string directive stream)
75 (interpret-directive-list stream (cdr directives) orig-args args))
77 (multiple-value-bind (new-directives new-args)
79 (svref *format-directive-interpreters*
80 (char-code (format-directive-character
82 (*default-format-error-offset*
83 (1- (format-directive-end directive))))
86 :complaint "unknown format directive"))
87 (multiple-value-bind (new-directives new-args)
88 (funcall function stream directive
89 (cdr directives) orig-args args)
90 (values new-directives new-args)))
91 (interpret-directive-list stream new-directives
92 orig-args new-args)))))
95 ;;;; FORMAT directive definition macros and runtime support
97 (eval-when (:compile-toplevel :execute)
99 ;;; This macro is used to extract the next argument from the current arg list.
100 ;;; This is the version used by format directive interpreters.
101 (sb!xc:defmacro next-arg (&optional offset)
105 :complaint "no more arguments"
107 `(:offset ,offset))))
108 (when *logical-block-popper*
109 (funcall *logical-block-popper*))
112 (sb!xc:defmacro def-complex-format-interpreter (char lambda-list &body body)
115 "~:@(~:C~)-FORMAT-DIRECTIVE-INTERPRETER"
118 (directives (if lambda-list (car (last lambda-list)) (gensym))))
120 (defun ,defun-name (stream ,directive ,directives orig-args args)
121 (declare (ignorable stream orig-args args))
123 `((let ,(mapcar (lambda (var)
125 (,(symbolicate "FORMAT-DIRECTIVE-" var)
127 (butlast lambda-list))
128 (values (progn ,@body) args)))
129 `((declare (ignore ,directive ,directives))
131 (%set-format-directive-interpreter ,char #',defun-name))))
133 (sb!xc:defmacro def-format-interpreter (char lambda-list &body body)
134 (let ((directives (gensym)))
135 `(def-complex-format-interpreter ,char (,@lambda-list ,directives)
139 (sb!xc:defmacro interpret-bind-defaults (specs params &body body)
140 (once-only ((params params))
141 (collect ((bindings))
143 (destructuring-bind (var default) spec
144 (bindings `(,var (let* ((param-and-offset (pop ,params))
145 (offset (car param-and-offset))
146 (param (cdr param-and-offset)))
148 (:arg (next-arg offset))
149 (:remaining (length args))
156 "too many parameters, expected no more than ~D"
157 :arguments (list ,(length specs))
158 :offset (caar ,params)))
163 ;;;; format interpreters and support functions for simple output
165 (defun format-write-field (stream string mincol colinc minpad padchar padleft)
167 (write-string string stream))
169 (write-char padchar stream))
170 (do ((chars (+ (length string) minpad) (+ chars colinc)))
173 (write-char padchar stream)))
175 (write-string string stream)))
177 (defun format-princ (stream arg colonp atsignp mincol colinc minpad padchar)
178 (format-write-field stream
179 (if (or arg (not colonp))
180 (princ-to-string arg)
182 mincol colinc minpad padchar atsignp))
184 (def-format-interpreter #\A (colonp atsignp params)
186 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
189 (format-princ stream (next-arg) colonp atsignp
190 mincol colinc minpad padchar))
191 (princ (if colonp (or (next-arg) "()") (next-arg)) stream)))
193 (defun format-prin1 (stream arg colonp atsignp mincol colinc minpad padchar)
194 (format-write-field stream
195 (if (or arg (not colonp))
196 (prin1-to-string arg)
198 mincol colinc minpad padchar atsignp))
200 (def-format-interpreter #\S (colonp atsignp params)
202 (interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
205 (format-prin1 stream (next-arg) colonp atsignp
206 mincol colinc minpad padchar)))
208 (let ((arg (next-arg)))
211 (princ "()" stream))))
213 (prin1 (next-arg) stream))))
215 (def-format-interpreter #\C (colonp atsignp params)
216 (interpret-bind-defaults () params
218 (format-print-named-character (next-arg) stream)
220 (prin1 (next-arg) stream)
221 (write-char (next-arg) stream)))))
223 (defun format-print-named-character (char stream)
224 (let* ((name (char-name char)))
226 (write-string (string-capitalize name) stream))
227 ((<= 0 (char-code char) 31)
228 ;; Print control characters as "^"<char>. (This seems to be
229 ;; old pre-ANSI behavior, but ANSI just says that the "#^"
230 ;; sequence is undefined and not reserved for the user, so
231 ;; this behavior should be ANSI-compliant.)
232 (write-char #\^ stream)
233 (write-char (code-char (+ 64 (char-code char))) stream))
235 (write-char char stream)))))
237 (def-format-interpreter #\W (colonp atsignp params)
238 (interpret-bind-defaults () params
239 (let ((*print-pretty* (or colonp *print-pretty*))
240 (*print-level* (and atsignp *print-level*))
241 (*print-length* (and atsignp *print-length*)))
242 (output-object (next-arg) stream))))
244 ;;;; format interpreters and support functions for integer output
246 ;;; FORMAT-PRINT-NUMBER does most of the work for the numeric printing
247 ;;; directives. The parameters are interpreted as defined for ~D.
248 (defun format-print-integer (stream number print-commas-p print-sign-p
249 radix mincol padchar commachar commainterval)
250 (let ((*print-base* radix)
252 (if (integerp number)
253 (let* ((text (princ-to-string (abs number)))
254 (commaed (if print-commas-p
255 (format-add-commas text commachar commainterval)
257 (signed (cond ((minusp number)
258 (concatenate 'string "-" commaed))
260 (concatenate 'string "+" commaed))
262 ;; colinc = 1, minpad = 0, padleft = t
263 (format-write-field stream signed mincol 1 0 padchar t))
266 (defun format-add-commas (string commachar commainterval)
267 (let ((length (length string)))
268 (multiple-value-bind (commas extra) (truncate (1- length) commainterval)
269 (let ((new-string (make-string (+ length commas)))
270 (first-comma (1+ extra)))
271 (replace new-string string :end1 first-comma :end2 first-comma)
272 (do ((src first-comma (+ src commainterval))
273 (dst first-comma (+ dst commainterval 1)))
275 (setf (schar new-string dst) commachar)
276 (replace new-string string :start1 (1+ dst)
277 :start2 src :end2 (+ src commainterval)))
280 ;;; FIXME: This is only needed in this file, could be defined with
281 ;;; SB!XC:DEFMACRO inside EVAL-WHEN
282 (defmacro interpret-format-integer (base)
283 `(if (or colonp atsignp params)
284 (interpret-bind-defaults
285 ((mincol 0) (padchar #\space) (commachar #\,) (commainterval 3))
287 (format-print-integer stream (next-arg) colonp atsignp ,base mincol
288 padchar commachar commainterval))
289 (write (next-arg) :stream stream :base ,base :radix nil :escape nil)))
291 (def-format-interpreter #\D (colonp atsignp params)
292 (interpret-format-integer 10))
294 (def-format-interpreter #\B (colonp atsignp params)
295 (interpret-format-integer 2))
297 (def-format-interpreter #\O (colonp atsignp params)
298 (interpret-format-integer 8))
300 (def-format-interpreter #\X (colonp atsignp params)
301 (interpret-format-integer 16))
303 (def-format-interpreter #\R (colonp atsignp params)
305 (interpret-bind-defaults
306 ((base 10) (mincol 0) (padchar #\space) (commachar #\,)
309 (format-print-integer stream (next-arg) colonp atsignp base mincol
310 padchar commachar commainterval))
313 (format-print-old-roman stream (next-arg))
314 (format-print-roman stream (next-arg)))
316 (format-print-ordinal stream (next-arg))
317 (format-print-cardinal stream (next-arg))))))
319 (defparameter *cardinal-ones*
320 #(nil "one" "two" "three" "four" "five" "six" "seven" "eight" "nine"))
322 (defparameter *cardinal-tens*
323 #(nil nil "twenty" "thirty" "forty"
324 "fifty" "sixty" "seventy" "eighty" "ninety"))
326 (defparameter *cardinal-teens*
327 #("ten" "eleven" "twelve" "thirteen" "fourteen" ;;; RAD
328 "fifteen" "sixteen" "seventeen" "eighteen" "nineteen"))
330 (defparameter *cardinal-periods*
331 #("" " thousand" " million" " billion" " trillion" " quadrillion"
332 " quintillion" " sextillion" " septillion" " octillion" " nonillion"
333 " decillion" " undecillion" " duodecillion" " tredecillion"
334 " quattuordecillion" " quindecillion" " sexdecillion" " septendecillion"
335 " octodecillion" " novemdecillion" " vigintillion"))
337 (defparameter *ordinal-ones*
338 #(nil "first" "second" "third" "fourth"
339 "fifth" "sixth" "seventh" "eighth" "ninth"))
341 (defparameter *ordinal-tens*
342 #(nil "tenth" "twentieth" "thirtieth" "fortieth"
343 "fiftieth" "sixtieth" "seventieth" "eightieth" "ninetieth"))
345 (defun format-print-small-cardinal (stream n)
346 (multiple-value-bind (hundreds rem) (truncate n 100)
347 (when (plusp hundreds)
348 (write-string (svref *cardinal-ones* hundreds) stream)
349 (write-string " hundred" stream)
351 (write-char #\space stream)))
353 (multiple-value-bind (tens ones) (truncate rem 10)
355 (write-string (svref *cardinal-tens* tens) stream)
357 (write-char #\- stream)
358 (write-string (svref *cardinal-ones* ones) stream)))
360 (write-string (svref *cardinal-teens* ones) stream))
362 (write-string (svref *cardinal-ones* ones) stream)))))))
364 (defun format-print-cardinal (stream n)
366 (write-string "negative " stream)
367 (format-print-cardinal-aux stream (- n) 0 n))
369 (write-string "zero" stream))
371 (format-print-cardinal-aux stream n 0 n))))
373 (defun format-print-cardinal-aux (stream n period err)
374 (multiple-value-bind (beyond here) (truncate n 1000)
375 (unless (<= period 20)
376 (error "number too large to print in English: ~:D" err))
377 (unless (zerop beyond)
378 (format-print-cardinal-aux stream beyond (1+ period) err))
380 (unless (zerop beyond)
381 (write-char #\space stream))
382 (format-print-small-cardinal stream here)
383 (write-string (svref *cardinal-periods* period) stream))))
385 (defun format-print-ordinal (stream n)
387 (write-string "negative " stream))
388 (let ((number (abs n)))
389 (multiple-value-bind (top bot) (truncate number 100)
391 (format-print-cardinal stream (- number bot)))
392 (when (and (plusp top) (plusp bot))
393 (write-char #\space stream))
394 (multiple-value-bind (tens ones) (truncate bot 10)
395 (cond ((= bot 12) (write-string "twelfth" stream))
397 (write-string (svref *cardinal-teens* ones) stream);;;RAD
398 (write-string "th" stream))
399 ((and (zerop tens) (plusp ones))
400 (write-string (svref *ordinal-ones* ones) stream))
401 ((and (zerop ones)(plusp tens))
402 (write-string (svref *ordinal-tens* tens) stream))
404 (write-string (svref *cardinal-tens* tens) stream)
405 (write-char #\- stream)
406 (write-string (svref *ordinal-ones* ones) stream))
408 (write-string "th" stream))
410 (write-string "zeroth" stream)))))))
412 ;;; Print Roman numerals
414 (defun format-print-old-roman (stream n)
416 (error "Number too large to print in old Roman numerals: ~:D" n))
417 (do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
418 (val-list '(500 100 50 10 5 1) (cdr val-list))
419 (cur-char #\M (car char-list))
420 (cur-val 1000 (car val-list))
421 (start n (do ((i start (progn
422 (write-char cur-char stream)
427 (defun format-print-roman (stream n)
429 (error "Number too large to print in Roman numerals: ~:D" n))
430 (do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
431 (val-list '(500 100 50 10 5 1) (cdr val-list))
432 (sub-chars '(#\C #\X #\X #\I #\I) (cdr sub-chars))
433 (sub-val '(100 10 10 1 1 0) (cdr sub-val))
434 (cur-char #\M (car char-list))
435 (cur-val 1000 (car val-list))
436 (cur-sub-char #\C (car sub-chars))
437 (cur-sub-val 100 (car sub-val))
438 (start n (do ((i start (progn
439 (write-char cur-char stream)
442 (cond ((<= (- cur-val cur-sub-val) i)
443 (write-char cur-sub-char stream)
444 (write-char cur-char stream)
445 (- i (- cur-val cur-sub-val)))
451 (def-format-interpreter #\P (colonp atsignp params)
452 (interpret-bind-defaults () params
453 (let ((arg (if colonp
454 (if (eq orig-args args)
456 :complaint "no previous argument")
457 (do ((arg-ptr orig-args (cdr arg-ptr)))
458 ((eq (cdr arg-ptr) args)
462 (write-string (if (eql arg 1) "y" "ies") stream)
463 (unless (eql arg 1) (write-char #\s stream))))))
465 ;;;; format interpreters and support functions for floating point output
467 (defun decimal-string (n)
468 (write-to-string n :base 10 :radix nil :escape nil))
470 (def-format-interpreter #\F (colonp atsignp params)
474 "cannot specify the colon modifier with this directive"))
475 (interpret-bind-defaults ((w nil) (d nil) (k nil) (ovf nil) (pad #\space))
477 (format-fixed stream (next-arg) w d k ovf pad atsignp)))
479 (defun format-fixed (stream number w d k ovf pad atsign)
482 (format-fixed-aux stream number w d k ovf pad atsign)
483 (if (rationalp number)
484 (format-fixed-aux stream
485 (coerce number 'single-float)
486 w d k ovf pad atsign)
487 (format-write-field stream
488 (decimal-string number)
490 (format-princ stream number nil nil w 1 0 pad)))
492 ;;; We return true if we overflowed, so that ~G can output the overflow char
493 ;;; instead of spaces.
494 (defun format-fixed-aux (stream number w d k ovf pad atsign)
498 (or (float-infinity-p number)
499 (float-nan-p number))))
500 (prin1 number stream)
504 (when (and w (or atsign (minusp number))) (decf spaceleft))
505 (multiple-value-bind (str len lpoint tpoint)
506 (sb!impl::flonum-to-string (abs number) spaceleft d k)
507 ;;if caller specifically requested no fraction digits, suppress the
508 ;;optional trailing zero
509 (when (and d (zerop d)) (setq tpoint nil))
512 ;;optional leading zero
514 (if (or (> spaceleft 0) tpoint) ;force at least one digit
517 ;;optional trailing zero
522 (cond ((and w (< spaceleft 0) ovf)
523 ;;field width overflow
524 (dotimes (i w) (write-char ovf stream))
527 (when w (dotimes (i spaceleft) (write-char pad stream)))
529 (write-char #\- stream)
530 (if atsign (write-char #\+ stream)))
531 (when lpoint (write-char #\0 stream))
532 (write-string str stream)
533 (when tpoint (write-char #\0 stream))
536 (def-format-interpreter #\E (colonp atsignp params)
540 "cannot specify the colon modifier with this directive"))
541 (interpret-bind-defaults
542 ((w nil) (d nil) (e nil) (k 1) (ovf nil) (pad #\space) (mark nil))
544 (format-exponential stream (next-arg) w d e k ovf pad mark atsignp)))
546 (defun format-exponential (stream number w d e k ovf pad marker atsign)
549 (format-exp-aux stream number w d e k ovf pad marker atsign)
550 (if (rationalp number)
551 (format-exp-aux stream
552 (coerce number 'single-float)
553 w d e k ovf pad marker atsign)
554 (format-write-field stream
555 (decimal-string number)
557 (format-princ stream number nil nil w 1 0 pad)))
559 (defun format-exponent-marker (number)
560 (if (typep number *read-default-float-format*)
568 ;;; Here we prevent the scale factor from shifting all significance out of
569 ;;; a number to the right. We allow insignificant zeroes to be shifted in
570 ;;; to the left right, athough it is an error to specify k and d such that this
571 ;;; occurs. Perhaps we should detect both these condtions and flag them as
572 ;;; errors. As for now, we let the user get away with it, and merely guarantee
573 ;;; that at least one significant digit will appear.
575 ;;; toy@rtp.ericsson.se: The Hyperspec seems to say that the exponent
576 ;;; marker is always printed. Make it so. Also, the original version
577 ;;; causes errors when printing infinities or NaN's. The Hyperspec is
578 ;;; silent here, so let's just print out infinities and NaN's instead
579 ;;; of causing an error.
580 (defun format-exp-aux (stream number w d e k ovf pad marker atsign)
581 (if (and (floatp number)
582 (or (float-infinity-p number)
583 (float-nan-p number)))
584 (prin1 number stream)
585 (multiple-value-bind (num expt) (sb!impl::scale-exponent (abs number))
586 (let* ((expt (- expt k))
587 (estr (decimal-string (abs expt)))
588 (elen (if e (max (length estr) e) (length estr)))
589 (fdig (if d (if (plusp k) (1+ (- d k)) d) nil))
590 (fmin (if (minusp k) (- 1 k) nil))
593 (if (or atsign (minusp number))
596 (if (and w ovf e (> elen e)) ;exponent overflow
597 (dotimes (i w) (write-char ovf stream))
598 (multiple-value-bind (fstr flen lpoint)
599 (sb!impl::flonum-to-string num spaceleft fdig k fmin)
601 (decf spaceleft flen)
606 (cond ((and w (< spaceleft 0) ovf)
607 ;;significand overflow
608 (dotimes (i w) (write-char ovf stream)))
610 (dotimes (i spaceleft) (write-char pad stream)))
612 (write-char #\- stream)
613 (if atsign (write-char #\+ stream)))
614 (when lpoint (write-char #\0 stream))
615 (write-string fstr stream)
616 (write-char (if marker
618 (format-exponent-marker number))
620 (write-char (if (minusp expt) #\- #\+) stream)
622 ;;zero-fill before exponent if necessary
623 (dotimes (i (- e (length estr)))
624 (write-char #\0 stream)))
625 (write-string estr stream)))))))))
627 (def-format-interpreter #\G (colonp atsignp params)
631 "cannot specify the colon modifier with this directive"))
632 (interpret-bind-defaults
633 ((w nil) (d nil) (e nil) (k nil) (ovf nil) (pad #\space) (mark nil))
635 (format-general stream (next-arg) w d e k ovf pad mark atsignp)))
637 (defun format-general (stream number w d e k ovf pad marker atsign)
640 (format-general-aux stream number w d e k ovf pad marker atsign)
641 (if (rationalp number)
642 (format-general-aux stream
643 (coerce number 'single-float)
644 w d e k ovf pad marker atsign)
645 (format-write-field stream
646 (decimal-string number)
648 (format-princ stream number nil nil w 1 0 pad)))
650 ;;; toy@rtp.ericsson.se: Same change as for format-exp-aux.
651 (defun format-general-aux (stream number w d e k ovf pad marker atsign)
652 (if (and (floatp number)
653 (or (float-infinity-p number)
654 (float-nan-p number)))
655 (prin1 number stream)
656 (multiple-value-bind (ignore n) (sb!impl::scale-exponent (abs number))
657 (declare (ignore ignore))
658 ;; KLUDGE: Default d if omitted. The procedure is taken directly from
659 ;; the definition given in the manual, and is not very efficient, since
660 ;; we generate the digits twice. Future maintainers are encouraged to
661 ;; improve on this. -- rtoy?? 1998??
663 (multiple-value-bind (str len)
664 (sb!impl::flonum-to-string (abs number))
665 (declare (ignore str))
666 (let ((q (if (= len 1) 1 (1- len))))
667 (setq d (max q (min n 7))))))
668 (let* ((ee (if e (+ e 2) 4))
669 (ww (if w (- w ee) nil))
672 (let ((char (if (format-fixed-aux stream number ww dd nil
676 (dotimes (i ee) (write-char char stream))))
678 (format-exp-aux stream number w d e (or k 1)
679 ovf pad marker atsign)))))))
681 (def-format-interpreter #\$ (colonp atsignp params)
682 (interpret-bind-defaults ((d 2) (n 1) (w 0) (pad #\space)) params
683 (format-dollars stream (next-arg) d n w pad colonp atsignp)))
685 (defun format-dollars (stream number d n w pad colon atsign)
686 (if (rationalp number) (setq number (coerce number 'single-float)))
688 (let* ((signstr (if (minusp number) "-" (if atsign "+" "")))
689 (signlen (length signstr)))
690 (multiple-value-bind (str strlen ig2 ig3 pointplace)
691 (sb!impl::flonum-to-string number nil d nil)
692 (declare (ignore ig2 ig3))
693 (when colon (write-string signstr stream))
694 (dotimes (i (- w signlen (- n pointplace) strlen))
695 (write-char pad stream))
696 (unless colon (write-string signstr stream))
697 (dotimes (i (- n pointplace)) (write-char #\0 stream))
698 (write-string str stream)))
699 (format-write-field stream
700 (decimal-string number)
703 ;;;; format interpreters and support functions for line/page breaks etc.
705 (def-format-interpreter #\% (colonp atsignp params)
706 (when (or colonp atsignp)
709 "cannot specify either colon or atsign for this directive"))
710 (interpret-bind-defaults ((count 1)) params
714 (def-format-interpreter #\& (colonp atsignp params)
715 (when (or colonp atsignp)
718 "cannot specify either colon or atsign for this directive"))
719 (interpret-bind-defaults ((count 1)) params
721 (dotimes (i (1- count))
724 (def-format-interpreter #\| (colonp atsignp params)
725 (when (or colonp atsignp)
728 "cannot specify either colon or atsign for this directive"))
729 (interpret-bind-defaults ((count 1)) params
731 (write-char (code-char form-feed-char-code) stream))))
733 (def-format-interpreter #\~ (colonp atsignp params)
734 (when (or colonp atsignp)
737 "cannot specify either colon or atsign for this directive"))
738 (interpret-bind-defaults ((count 1)) params
740 (write-char #\~ stream))))
742 (def-complex-format-interpreter #\newline (colonp atsignp params directives)
743 (when (and colonp atsignp)
746 "cannot specify both colon and atsign for this directive"))
747 (interpret-bind-defaults () params
749 (write-char #\newline stream)))
750 (if (and (not colonp)
752 (simple-string-p (car directives)))
753 (cons (string-left-trim *format-whitespace-chars*
758 ;;;; format interpreters and support functions for tabs and simple pretty
761 (def-format-interpreter #\T (colonp atsignp params)
763 (interpret-bind-defaults ((n 1) (m 1)) params
764 (pprint-tab (if atsignp :section-relative :section) n m stream))
766 (interpret-bind-defaults ((colrel 1) (colinc 1)) params
767 (format-relative-tab stream colrel colinc))
768 (interpret-bind-defaults ((colnum 1) (colinc 1)) params
769 (format-absolute-tab stream colnum colinc)))))
771 (defun output-spaces (stream n)
772 (let ((spaces #.(make-string 100 :initial-element #\space)))
774 (when (< n (length spaces))
776 (write-string spaces stream)
777 (decf n (length spaces)))
778 (write-string spaces stream :end n)))
780 (defun format-relative-tab (stream colrel colinc)
781 (if (sb!pretty:pretty-stream-p stream)
782 (pprint-tab :line-relative colrel colinc stream)
783 (let* ((cur (sb!impl::charpos stream))
784 (spaces (if (and cur (plusp colinc))
785 (- (* (ceiling (+ cur colrel) colinc) colinc) cur)
787 (output-spaces stream spaces))))
789 (defun format-absolute-tab (stream colnum colinc)
790 (if (sb!pretty:pretty-stream-p stream)
791 (pprint-tab :line colnum colinc stream)
792 (let ((cur (sb!impl::charpos stream)))
794 (write-string " " stream))
796 (output-spaces stream (- colnum cur)))
798 (unless (zerop colinc)
799 (output-spaces stream
800 (- colinc (rem (- cur colnum) colinc)))))))))
802 (def-format-interpreter #\_ (colonp atsignp params)
803 (interpret-bind-defaults () params
804 (pprint-newline (if colonp
813 (def-format-interpreter #\I (colonp atsignp params)
816 :complaint "cannot specify the at-sign modifier"))
817 (interpret-bind-defaults ((n 0)) params
818 (pprint-indent (if colonp :current :block) n stream)))
820 ;;;; format interpreter for ~*
822 (def-format-interpreter #\* (colonp atsignp params)
826 :complaint "cannot specify both colon and at-sign")
827 (interpret-bind-defaults ((posn 0)) params
828 (if (<= 0 posn (length orig-args))
829 (setf args (nthcdr posn orig-args))
831 :complaint "Index ~D is out of bounds. (It should ~
832 have been between 0 and ~D.)"
833 :arguments (list posn (length orig-args))))))
835 (interpret-bind-defaults ((n 1)) params
836 (do ((cur-posn 0 (1+ cur-posn))
837 (arg-ptr orig-args (cdr arg-ptr)))
839 (let ((new-posn (- cur-posn n)))
840 (if (<= 0 new-posn (length orig-args))
841 (setf args (nthcdr new-posn orig-args))
844 "Index ~D is out of bounds. (It should
845 have been between 0 and ~D.)"
847 (list new-posn (length orig-args))))))))
848 (interpret-bind-defaults ((n 1)) params
852 ;;;; format interpreter for indirection
854 (def-format-interpreter #\? (colonp atsignp params string end)
857 :complaint "cannot specify the colon modifier"))
858 (interpret-bind-defaults () params
861 #'(lambda (condition)
864 "~A~%while processing indirect format string:"
865 :arguments (list condition)
867 :control-string string
870 (setf args (%format stream (next-arg) orig-args args))
871 (%format stream (next-arg) (next-arg))))))
873 ;;;; format interpreters for capitalization
875 (def-complex-format-interpreter #\( (colonp atsignp params directives)
876 (let ((close (find-directive directives #\) nil)))
879 :complaint "no corresponding close paren"))
880 (interpret-bind-defaults () params
881 (let* ((posn (position close directives))
882 (before (subseq directives 0 posn))
883 (after (nthcdr (1+ posn) directives))
884 (stream (make-case-frob-stream stream
892 (setf args (interpret-directive-list stream before orig-args args))
895 (def-complex-format-interpreter #\) ()
897 :complaint "no corresponding open paren"))
899 ;;;; format interpreters and support functions for conditionalization
901 (def-complex-format-interpreter #\[ (colonp atsignp params directives)
902 (multiple-value-bind (sublists last-semi-with-colon-p remaining)
903 (parse-conditional-directive directives)
909 "cannot specify both the colon and at-sign modifiers")
913 "can only specify one section")
914 (interpret-bind-defaults () params
915 (let ((prev-args args)
918 (interpret-directive-list stream
924 (if (= (length sublists) 2)
925 (interpret-bind-defaults () params
927 (interpret-directive-list stream (car sublists)
929 (interpret-directive-list stream (cadr sublists)
933 "must specify exactly two sections"))
934 (interpret-bind-defaults ((index (next-arg))) params
935 (let* ((default (and last-semi-with-colon-p
937 (last (1- (length sublists)))
939 (if (<= 0 index last)
940 (nth (- last index) sublists)
942 (interpret-directive-list stream sublist orig-args
946 (def-complex-format-interpreter #\; ()
949 "~~; not contained within either ~~[...~~] or ~~<...~~>"))
951 (def-complex-format-interpreter #\] ()
954 "no corresponding open bracket"))
956 ;;;; format interpreter for up-and-out
958 (defvar *outside-args*)
960 (def-format-interpreter #\^ (colonp atsignp params)
963 :complaint "cannot specify the at-sign modifier"))
964 (when (and colonp (not *up-up-and-out-allowed*))
966 :complaint "attempt to use ~~:^ outside a ~~:{...~~} construct"))
967 (when (case (length params)
969 (null *outside-args*)
971 (1 (interpret-bind-defaults ((count 0)) params
973 (2 (interpret-bind-defaults ((arg1 0) (arg2 0)) params
975 (t (interpret-bind-defaults ((arg1 0) (arg2 0) (arg3 0)) params
976 (<= arg1 arg2 arg3))))
977 (throw (if colonp 'up-up-and-out 'up-and-out)
980 ;;;; format interpreters for iteration
982 (def-complex-format-interpreter #\{
983 (colonp atsignp params string end directives)
984 (let ((close (find-directive directives #\} nil)))
988 "no corresponding close brace"))
989 (interpret-bind-defaults ((max-count nil)) params
990 (let* ((closed-with-colon (format-directive-colonp close))
991 (posn (position close directives))
992 (insides (if (zerop posn)
994 (subseq directives 0 posn)))
995 (*up-up-and-out-allowed* colonp))
997 ((do-guts (orig-args args)
1001 #'(lambda (condition)
1002 (error 'format-error
1004 "~A~%while processing indirect format string:"
1005 :arguments (list condition)
1007 :control-string string
1008 :offset (1- end)))))
1009 (%format stream insides orig-args args))
1010 (interpret-directive-list stream insides
1012 (bind-args (orig-args args)
1014 (let* ((arg (next-arg))
1015 (*logical-block-popper* nil)
1016 (*outside-args* args))
1020 (do-guts orig-args args)))
1021 (do-loop (orig-args args)
1022 (catch (if colonp 'up-up-and-out 'up-and-out)
1024 (when (and (not closed-with-colon) (null args))
1026 (when (and max-count (minusp (decf max-count)))
1028 (setf args (bind-args orig-args args))
1029 (when (and closed-with-colon (null args))
1033 (setf args (do-loop orig-args args))
1034 (let ((arg (next-arg))
1035 (*logical-block-popper* nil))
1037 (nthcdr (1+ posn) directives))))))
1039 (def-complex-format-interpreter #\} ()
1040 (error 'format-error
1041 :complaint "no corresponding open brace"))
1043 ;;;; format interpreters and support functions for justification
1045 (def-complex-format-interpreter #\<
1046 (colonp atsignp params string end directives)
1047 (multiple-value-bind (segments first-semi close remaining)
1048 (parse-format-justification directives)
1050 (if (format-directive-colonp close)
1051 (multiple-value-bind (prefix per-line-p insides suffix)
1052 (parse-format-logical-block segments colonp first-semi
1053 close params string end)
1054 (interpret-format-logical-block stream orig-args args
1055 prefix per-line-p insides
1057 (interpret-format-justification stream orig-args args
1058 segments colonp atsignp
1059 first-semi params)))
1062 (defun interpret-format-justification
1063 (stream orig-args args segments colonp atsignp first-semi params)
1064 (interpret-bind-defaults
1065 ((mincol 0) (colinc 1) (minpad 0) (padchar #\space))
1067 (let ((newline-string nil)
1073 (when (and first-semi (format-directive-colonp first-semi))
1074 (interpret-bind-defaults
1076 (len (or (sb!impl::line-length stream) 72)))
1077 (format-directive-params first-semi)
1078 (setf newline-string
1079 (with-output-to-string (stream)
1081 (interpret-directive-list stream
1085 (setf extra-space extra)
1086 (setf line-len len)))
1087 (dolist (segment segments)
1088 (push (with-output-to-string (stream)
1090 (interpret-directive-list stream segment
1094 (format-justification stream newline-string extra-space line-len strings
1095 colonp atsignp mincol colinc minpad padchar)))
1098 (defun format-justification (stream newline-prefix extra-space line-len strings
1099 pad-left pad-right mincol colinc minpad padchar)
1100 (setf strings (reverse strings))
1101 (when (and (not pad-left) (not pad-right) (null (cdr strings)))
1103 (let* ((num-gaps (+ (1- (length strings))
1105 (if pad-right 1 0)))
1106 (chars (+ (* num-gaps minpad)
1108 for string in strings
1109 summing (length string))))
1110 (length (if (> chars mincol)
1111 (+ mincol (* (ceiling (- chars mincol) colinc) colinc))
1113 (padding (- length chars)))
1114 (when (and newline-prefix
1115 (> (+ (or (sb!impl::charpos stream) 0)
1118 (write-string newline-prefix stream))
1119 (flet ((do-padding ()
1120 (let ((pad-len (truncate padding num-gaps)))
1121 (decf padding pad-len)
1123 (dotimes (i pad-len) (write-char padchar stream)))))
1127 (write-string (car strings) stream)
1128 (dolist (string (cdr strings))
1130 (write-string string stream)))
1134 (defun interpret-format-logical-block
1135 (stream orig-args args prefix per-line-p insides suffix atsignp)
1136 (let ((arg (if atsignp args (next-arg))))
1138 (pprint-logical-block
1139 (stream arg :per-line-prefix prefix :suffix suffix)
1140 (let ((*logical-block-popper* #'(lambda () (pprint-pop))))
1142 (interpret-directive-list stream insides
1143 (if atsignp orig-args arg)
1145 (pprint-logical-block (stream arg :prefix prefix :suffix suffix)
1146 (let ((*logical-block-popper* #'(lambda () (pprint-pop))))
1148 (interpret-directive-list stream insides
1149 (if atsignp orig-args arg)
1151 (if atsignp nil args))
1153 ;;;; format interpreter and support functions for user-defined method
1155 (def-format-interpreter #\/ (string start end colonp atsignp params)
1156 (let ((symbol (extract-user-function-name string start end)))
1158 (dolist (param-and-offset params)
1159 (let ((param (cdr param-and-offset)))
1161 (:arg (args (next-arg)))
1162 (:remaining (args (length args)))
1164 (apply (fdefinition symbol) stream (next-arg) colonp atsignp (args)))))