-
Notifications
You must be signed in to change notification settings - Fork 11
/
write-wrap.lisp
105 lines (84 loc) · 4.1 KB
/
write-wrap.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
;;;-*- Mode: Lisp; Package: WRITE-WRAP -*-
#|
Copyright (c) 2003 Christopher K. Riesbeck
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
|#
;;; Updates:
;;; 1/3/03 made DEFPACKAGE compatible with Allegro Modern [CKR]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Packages
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(cl:defpackage #:write-wrap
(:use #:common-lisp)
(:export #:write-wrap)
)
(in-package #:write-wrap)
;;; (WRITE-WRAP stream string width &optional indent first-indent)
;;; writes string to stream, split into width-size lengths, breaking
;;; at returns and spaces in the string, if possible, indenting every
;;; line indent spaces (default = 0), except the first line which is
;;; indented first-indent spaces (default = indent).
;;;
;;; Note: to generate a string simply use with-output-to-string
;;; (WITH-OUTPUT-TO-STRING (s) (WRITE-WRAP s ...))
;;;
;;; Had to turn off *PRINT-PRETTY* because Franz turns it on
;;; by default and if you turn it off globally, it breaks the IDE
;;; in 6.0!
(defun write-wrap (stream strng width
&key indent (first-indent indent))
(let ((*print-pretty* nil))
(do* ((end (length strng))
(indent-string (when (and indent (> indent 0))
(make-string indent
:initial-element #\space)))
(first-indent-string (when (and first-indent (> first-indent 0))
(make-string first-indent
:initial-element #\space)))
(start 0 (1+ next))
(next (break-pos strng start end width)
(break-pos strng start end width))
(margin first-indent-string indent-string))
((null next))
(when margin (write-string margin stream))
(write-string strng stream :start start :end next)
(terpri stream))))
;;; (whitespace-p char) is true if ch is whitespace.
(defun whitespace-p (ch)
(member ch '(#\linefeed #\newline #\return #\space #\tab)))
;;; (break-pos string start end width) returns the position to break string
;;; at, guaranteed to be no more than width characters. If there's a`
;;; return, its position is used, else the last space before the width
;;; cutoff, else width. If the end comes before width, then end is
;;; returned.
(defun break-pos (strng start end width)
(unless (or (null start) (>= start end))
(let ((limit (min (+ start width) end)))
(or (position #\newline strng :start start :end limit)
(and (= end limit) end)
(position #\space strng :start start :end limit :from-end t)
limit)))) ;;insert warning here, if desired
#|
;;; (non-whitespace-pos string &optional start) returns the position of
;;; the first non-whitespace character in string, after start, if any.
;;; Not used now but was used before to set and update START in WRITE-WRAP
;;; to skip spaces. The current WRITE-WRAP keeps user spacing, except
;;; when replacing a space with a line break.
(defun non-whitespace-pos (strng &optional (start 0))
(position-if-not #'whitespace-p strng :start start))
|#
(provide "write-wrap")