| 1 |
;;;-*- Mode:LISP; Package:CHAOS; Base:10; Syntax:Common-lisp -*- |
| 2 |
;;; |
| 3 |
;;; Copyright (c) 2000-2018, Toshimi Sawada. All rights reserved. |
| 4 |
;;; |
| 5 |
;;; Redistribution and use in source and binary forms, with or without |
| 6 |
;;; modification, are permitted provided that the following conditions |
| 7 |
;;; are met: |
| 8 |
;;; |
| 9 |
;;; * Redistributions of source code must retain the above copyright |
| 10 |
;;; notice, this list of conditions and the following disclaimer. |
| 11 |
;;; |
| 12 |
;;; * Redistributions in binary form must reproduce the above |
| 13 |
;;; copyright notice, this list of conditions and the following |
| 14 |
;;; disclaimer in the documentation and/or other materials |
| 15 |
;;; provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED |
| 18 |
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 |
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 21 |
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 23 |
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 |
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 |
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
;;; |
| 29 |
(in-package :chaos) |
| 30 |
#|============================================================================== |
| 31 |
System: CHAOS |
| 32 |
Module: comlib |
| 33 |
File: print-utils.lisp |
| 34 |
=============================================================================|# |
| 35 |
#-:chaos-debug |
| 36 |
(declaim (optimize (speed 3) (safety 0) #-GCL (debug 0))) |
| 37 |
#+:chaos-debug |
| 38 |
(declaim (optimize (speed 1) (safety 3) #-GCL (debug 3))) |
| 39 |
|
| 40 |
;;;----------------------------------------------------------------------------- |
| 41 |
;;; Utilities for printing |
| 42 |
;;;----------------------------------------------------------------------------- |
| 43 |
|
| 44 |
;;; PRINT CONTROL VARIABLES 00-------------------------------------------------- |
| 45 |
;;; *print-line-limit* : the limit of length of a line |
| 46 |
;;; *print-indent* : current indent level. |
| 47 |
;;; *print-indent-increment* : number of spaces per one indentation. |
| 48 |
;;; |
| 49 |
|
| 50 |
;;; FILECOL -- output column position |
| 51 |
;;; |
| 52 |
(declaim (inline filecol) |
| 53 |
(ftype (function (t) fixnum) filecol)) |
| 54 |
|
| 55 |
#+GCL |
| 56 |
(Clines "static object filecol(x) object x; {return(make_fixnum(file_column(x)));}") |
| 57 |
|
| 58 |
#+GCL |
| 59 |
(defentry filecol (object) (object filecol)) |
| 60 |
|
| 61 |
#+LUCID |
| 62 |
(defun filecol (x) (declare (values fixnum)) (lucid::calculate-output-column x)) |
| 63 |
|
| 64 |
#+CMU |
| 65 |
(defun filecol (x) |
| 66 |
(declare (values fixnum)) |
| 67 |
(let ((val (lisp::charpos x))) |
| 68 |
(if val val |
| 69 |
0))) |
| 70 |
|
| 71 |
#+EXCL |
| 72 |
(defun filecol (x) |
| 73 |
(declare (values fixnum)) |
| 74 |
(let ((val (excl::charpos x))) |
| 75 |
(if val val 0))) |
| 76 |
|
| 77 |
#+:openmcl |
| 78 |
(defun filecol (x) |
| 79 |
(declare (values fixnum)) |
| 80 |
(stream-line-column x)) |
| 81 |
|
| 82 |
#+:SBCL |
| 83 |
(defun filecol (x) |
| 84 |
(declare (values fixnum)) |
| 85 |
(let ((val (sb-kernel::charpos x))) |
| 86 |
(if val val |
| 87 |
0))) |
| 88 |
|
| 89 |
#-(or GCL KCL LUCID CMU EXCL :openmcl SBCL) |
| 90 |
(defun filecol (x) (declare (ignore x)) 0) ; use this if you cannot define as |
| 91 |
|
| 92 |
(declaim (ftype (function (stream) fixnum) file-column)) |
| 93 |
(defun file-column (strm) |
| 94 |
(declare (type stream strm) |
| 95 |
(inline filecol) |
| 96 |
(values fixnum)) |
| 97 |
(filecol strm)) |
| 98 |
|
| 99 |
;;; print-check |
| 100 |
;;; checks the current column exceeds the line limit, if so |
| 101 |
;;; newline and indent. |
| 102 |
;;; |
| 103 |
(defun print-check (&optional (indent 0) (fwd 0) (stream *standard-output*)) |
| 104 |
(declare (type fixnum indent fwd)) |
| 105 |
(if (<= *print-line-limit* (+ (file-column stream) fwd)) |
| 106 |
(progn |
| 107 |
(print-next) |
| 108 |
(when (>= (1+ indent) *print-line-limit*) |
| 109 |
(setq .file-col. (* *print-indent* *print-indent-increment*))) |
| 110 |
t) |
| 111 |
nil)) |
| 112 |
|
| 113 |
;;; print-indent |
| 114 |
;;; indentation. |
| 115 |
;;; |
| 116 |
(defun print-indent (indent-char &optional (n *print-indent*) (stream *standard-output*)) |
| 117 |
(declare (type fixnum n)) |
| 118 |
(dotimes (i (the fixnum (* n *print-indent-increment*))) |
| 119 |
(declare (type fixnum i)) |
| 120 |
(princ indent-char stream))) |
| 121 |
|
| 122 |
;;; print-centering |
| 123 |
;;; print the given string centering |
| 124 |
;;; |
| 125 |
(defconstant .terminal-width. 70) |
| 126 |
(defun print-centering (string &optional (fill-char " ") (stream *standard-output*)) |
| 127 |
(declare (type simple-string string)) |
| 128 |
(let ((fill-col (truncate (+ (/ (- .terminal-width. (the fixnum (length string))) 2.0) 0.5)))) |
| 129 |
(declare (type fixnum fill-col)) |
| 130 |
(dotimes (x fill-col) |
| 131 |
(declare (type fixnum x)) |
| 132 |
(princ fill-char stream)) |
| 133 |
(princ string stream) |
| 134 |
(unless (equal fill-char " ") |
| 135 |
(dotimes (x fill-col) |
| 136 |
(declare (type fixnum x)) |
| 137 |
(princ fill-char stream))))) |
| 138 |
|
| 139 |
;;; print-to-right |
| 140 |
;;; print the given string |
| 141 |
;;; |
| 142 |
(defun print-to-right (string &optional (fill-char " ") (stream *standard-output*)) |
| 143 |
(declare (type simple-string string) |
| 144 |
(type (or character simple-string) fill-char) |
| 145 |
(type stream stream)) |
| 146 |
(dotimes (x (- (1- .terminal-width.) |
| 147 |
(filecol stream) |
| 148 |
*print-indent* |
| 149 |
(the fixnum (length string)))) |
| 150 |
(declare (type fixnum x)) |
| 151 |
(princ fill-char stream)) |
| 152 |
(princ " " stream) |
| 153 |
(princ string stream)) |
| 154 |
|
| 155 |
;;; print-to-left |
| 156 |
;;; print the given string |
| 157 |
;;; |
| 158 |
(defun print-to-left (string &optional (fill-char nil) (stream *standard-output*)) |
| 159 |
(declare (type simple-string string) |
| 160 |
(type (or null character simple-string) fill-char) |
| 161 |
(type stream stream)) |
| 162 |
(let ((*print-line-limit* .terminal-width.)) |
| 163 |
(princ string stream) |
| 164 |
(princ " " stream) |
| 165 |
(if fill-char |
| 166 |
(dotimes (x (- (1- *print-line-limit*) |
| 167 |
*print-indent* |
| 168 |
(filecol stream) |
| 169 |
(length string))) |
| 170 |
(declare (type fixnum x)) |
| 171 |
(princ fill-char stream))))) |
| 172 |
|
| 173 |
;;; print-next |
| 174 |
;;; print new-line iff the current column is not at the beggining of line |
| 175 |
;;; and then indent. given prefix is printed afer the indentation if any. |
| 176 |
;;; |
| 177 |
(defun print-next (&optional (prefix nil) (n *print-indent*) (stream *standard-output*)) |
| 178 |
(declare (type fixnum n) |
| 179 |
(type stream stream)) |
| 180 |
#+SBCL |
| 181 |
(progn (terpri stream) (print-indent #\space n stream)) |
| 182 |
#-SBCL |
| 183 |
(when (fresh-line stream) |
| 184 |
(print-indent #\space n stream)) |
| 185 |
(when prefix (princ prefix stream))) |
| 186 |
|
| 187 |
(defun print-next-prefix (prefix-char &optional (prefix nil) (n *print-indent*) (stream *standard-output*)) |
| 188 |
(declare (type fixnum n) |
| 189 |
(type stream stream) |
| 190 |
(type character prefix-char)) |
| 191 |
(when (fresh-line stream) |
| 192 |
(print-indent prefix-char n stream)) |
| 193 |
(when prefix (princ prefix stream))) |
| 194 |
|
| 195 |
;;; print-simple |
| 196 |
;;; |
| 197 |
(defun print-simple (x &optional (stream *standard-output*)) |
| 198 |
(declare (type stream stream)) |
| 199 |
(cond ((atom x) (prin1 x stream)) |
| 200 |
(t (let ((flag nil) (tail x)) |
| 201 |
(princ "(" stream) |
| 202 |
(loop (when (not (consp tail)) (return)) |
| 203 |
(if flag |
| 204 |
(princ " " stream) |
| 205 |
(setq flag t)) |
| 206 |
(print-simple (car tail) stream) |
| 207 |
(setq tail (cdr tail))) |
| 208 |
(when tail |
| 209 |
(princ " . " stream) |
| 210 |
(prin1 tail stream)) |
| 211 |
(princ ")" stream))))) |
| 212 |
|
| 213 |
;;; print-simple-princ |
| 214 |
;;; |
| 215 |
(defun print-simple-princ (x &optional (stream *standard-output*)) |
| 216 |
(declare (type stream stream)) |
| 217 |
(let ((.file-col. .file-col.)) |
| 218 |
(cond ((atom x) (princ x stream)) |
| 219 |
(t (let ((flag nil) |
| 220 |
(tail x)) |
| 221 |
(princ "(" stream) |
| 222 |
(setq .file-col. (1+ (file-column stream))) |
| 223 |
(loop (when (not (consp tail)) (return)) |
| 224 |
(if flag |
| 225 |
(princ " " stream) |
| 226 |
(setq flag t)) |
| 227 |
(print-simple-princ (car tail) stream) |
| 228 |
(setq tail (cdr tail))) |
| 229 |
(when tail |
| 230 |
(princ " . " stream) |
| 231 |
(prin1 tail stream)) |
| 232 |
(princ ")" stream)))))) |
| 233 |
|
| 234 |
;;; print-simple-princ-open |
| 235 |
;;; |
| 236 |
(defun print-simple-princ-open (x &optional (stream *standard-output*)) |
| 237 |
(declare (type stream stream)) |
| 238 |
(let ((.file-col. .file-col.)) |
| 239 |
(print-check .file-col. 0 stream) |
| 240 |
(cond ((atom x) (princ x stream)) |
| 241 |
(t (let ((flag nil) |
| 242 |
(tail x)) |
| 243 |
(loop (when (not (consp tail)) (return)) |
| 244 |
(if flag |
| 245 |
(princ #\space stream) |
| 246 |
(setq flag t)) |
| 247 |
(print-simple-princ (car tail) stream) |
| 248 |
(setq tail (cdr tail))) |
| 249 |
(when tail |
| 250 |
(princ " ... " stream) |
| 251 |
(prin1 tail stream))))))) |
| 252 |
|
| 253 |
;;; EOF |