/[cmucl]/src/hemlock/indent.lisp
ViewVC logotype

Contents of /src/hemlock/indent.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Sun Jun 23 15:15:26 1991 UTC (22 years, 10 months ago) by chiles
Branch: MAIN
Changes since 1.2: +3 -2 lines
Fixed a bug in "Center Line" which caused an internal error when invoked on the
last line of the buffer with the buffer end at the end of the line.
1 ;;; -*- Log: hemlock.log; Package: Hemlock -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;; If you want to use this code or any part of CMU Common Lisp, please contact
7 ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
8 ;;;
9 (ext:file-comment
10 "$Header: /tiger/var/lib/cvsroots/cmucl/src/hemlock/indent.lisp,v 1.3 1991/06/23 15:15:26 chiles Exp $")
11 ;;;
12 ;;; **********************************************************************
13 ;;;
14 ;;; Hemlock indentation commands
15 ;;;
16 ;;; Written by Bill Maddox and Bill Chiles
17 ;;;
18 (in-package "HEMLOCK")
19
20
21
22 (defhvar "Spaces per Tab"
23 "The number of spaces a tab is equivalent to. NOTE: This is not incorporated
24 everywhere in Hemlock yet, so do not change it."
25 :value 8)
26
27 (defun indent-using-tabs (mark column)
28 "Inserts at mark a maximum number of tabs and a minimum number of spaces to
29 move mark to column. This assumes mark is at the beginning of a line."
30 (multiple-value-bind (tabs spaces) (floor column (value spaces-per-tab))
31 (dotimes (i tabs) (insert-character mark #\tab))
32 (dotimes (i spaces) (insert-character mark #\space))))
33
34 (defhvar "Indent with Tabs"
35 "Function that takes a mark and a number of spaces and inserts tabs and spaces
36 to indent that number of spaces using \"Spaces per Tab\"."
37 :value #'indent-using-tabs)
38
39
40 (defun tab-to-tab-stop (mark)
41 (insert-character mark #\tab))
42
43 (defhvar "Indent Function"
44 "Indentation function which is invoked by \"Indent\" command.
45 It takes a :left-inserting mark that may be moved."
46 :value #'tab-to-tab-stop)
47
48
49 (defun generic-indent (mark)
50 (let* ((line (mark-line mark))
51 (prev (do ((line (line-previous line) (line-previous line)))
52 ((or (null line) (not (blank-line-p line))) line))))
53 (unless prev (editor-error))
54 (line-start mark prev)
55 (find-attribute mark :space #'zerop)
56 (let ((indentation (mark-column mark)))
57 (line-start mark line)
58 (delete-horizontal-space mark)
59 (funcall (value indent-with-tabs) mark indentation))))
60
61
62 (defcommand "Indent New Line" (p)
63 "Moves point to a new blank line and indents it.
64 Any whitespace before point is deleted. The value of \"Indent Function\"
65 is used for indentation unless there is a Fill Prefix, in which case it is
66 used. Any argument is passed onto \"New Line\"."
67 "Moves point to a new blank line and indents it.
68 Any whitespace before point is deleted. The value of \"Indent Function\"
69 is used for indentation unless there is a Fill Prefix, in which case it is
70 used. Any argument is passed onto \"New Line\"."
71 (let ((point (current-point))
72 (prefix (value fill-prefix)))
73 (delete-horizontal-space point)
74 (new-line-command p)
75 (if prefix
76 (insert-string point prefix)
77 (funcall (value indent-function) point))))
78
79
80 (defcommand "Indent" (p)
81 "Invokes function held by the Hemlock variable \"Indent Function\",
82 moving point past region if called with argument."
83 "Invokes function held by the Hemlock variable \"Indent Function\"
84 moving point past region if called with argument."
85 (let ((point (current-point)))
86 (with-mark ((mark point :left-inserting))
87 (cond ((or (not p) (zerop p))
88 (funcall (value indent-function) mark))
89 (t
90 (if (plusp p)
91 (unless (line-offset point (1- p))
92 (buffer-end point))
93 (unless (line-offset mark (1+ p))
94 (buffer-start mark)))
95 (indent-region-for-commands (region mark point))
96 (find-attribute (line-start point) :whitespace #'zerop))))))
97
98 (defcommand "Indent Region" (p)
99 "Invokes function held by Hemlock variable \"Indent Function\" on every
100 line between point and mark, inclusively."
101 "Invokes function held by Hemlock variable \"Indent Function\" on every
102 line between point and mark, inclusively."
103 (declare (ignore p))
104 (let* ((region (current-region)))
105 (with-mark ((start (region-start region) :left-inserting)
106 (end (region-end region) :left-inserting))
107 (indent-region-for-commands (region start end)))))
108
109 (defun indent-region-for-commands (region)
110 "Indents region undoably with INDENT-REGION."
111 (let* ((start (region-start region))
112 (end (region-end region))
113 (undo-region (copy-region (region (line-start start) (line-end end)))))
114 (indent-region region)
115 (make-region-undo :twiddle "Indent"
116 (region (line-start (copy-mark start :left-inserting))
117 (line-end (copy-mark end :right-inserting)))
118 undo-region)))
119
120 (defun indent-region (region)
121 "Invokes function held by Hemlock variable \"Indent Function\" on every
122 line of region."
123 (let ((indent-function (value indent-function)))
124 (with-mark ((start (region-start region) :left-inserting)
125 (end (region-end region)))
126 (line-start start)
127 (line-start end)
128 (loop (when (mark= start end)
129 (funcall indent-function start)
130 (return))
131 (funcall indent-function start)
132 (line-offset start 1 0)))))
133
134 (defcommand "Center Line" (p)
135 "Centers current line using \"Fill Column\". If an argument is supplied,
136 it is used instead of the \"Fill Column\"."
137 "Centers current line using fill-column."
138 (let* ((indent-function (value indent-with-tabs))
139 (region (if (region-active-p)
140 (current-region)
141 (region (current-point) (current-point))))
142 (end (region-end region)))
143 (with-mark ((temp (region-start region) :left-inserting))
144 (loop
145 (when (mark> temp end) (return))
146 (delete-horizontal-space (line-end temp))
147 (delete-horizontal-space (line-start temp))
148 (let* ((len (line-length (mark-line temp)))
149 (spaces (- (or p (value fill-column)) len)))
150 (if (and (plusp spaces)
151 (not (zerop len)))
152 (funcall indent-function temp (ceiling spaces 2)))
153 (unless (line-offset temp 1) (return))
154 (line-start temp))))))
155
156
157 (defcommand "Quote Tab" (p)
158 "Insert tab character."
159 "Insert tab character."
160 (if (and p (> p 1))
161 (insert-string (current-point) (make-string p :initial-element #\tab))
162 (insert-character (current-point) #\tab)))
163
164
165 (defcommand "Open Line" (p)
166 "Inserts a newline into the buffer without moving the point."
167 "Inserts a newline into the buffer without moving the point.
168 With argument, inserts p newlines."
169 (let ((point (current-point))
170 (count (if p p 1)))
171 (if (not (minusp count))
172 (dotimes (i count)
173 (insert-character point #\newline)
174 (mark-before point))
175 (editor-error))))
176
177
178 (defcommand "New Line" (p)
179 "Moves the point to a new blank line.
180 A newline is inserted if the next two lines are not already blank.
181 With an argument, repeats p times."
182 "Moves the point to a new blank line."
183 (let ((point (current-point))
184 (count (if p p 1)))
185 (if (not (minusp count))
186 (do* ((next (line-next (mark-line point))
187 (line-next (mark-line point)))
188 (i 1 (1+ i)))
189 ((> i count))
190 (cond ((and (blank-after-p point)
191 next (blank-line-p next)
192 (let ((after (line-next next)))
193 (or (not after) (blank-line-p after))))
194 (line-start point next)
195 (let ((len (line-length next)))
196 (unless (zerop len)
197 (delete-characters point len))))
198 (t
199 (insert-character point #\newline))))
200 (editor-error))))
201
202
203 (defattribute "Space"
204 "This attribute is used by the indentation commands to determine which
205 characters are treated as space."
206 '(mod 2) 0)
207
208 (setf (character-attribute :space #\space) 1)
209 (setf (character-attribute :space #\tab) 1)
210
211 (defun delete-horizontal-space (mark)
212 "Deletes all :space characters on either side of mark."
213 (with-mark ((start mark))
214 (reverse-find-attribute start :space #'zerop)
215 (find-attribute mark :space #'zerop)
216 (delete-region (region start mark))))
217
218
219
220 (defcommand "Delete Indentation" (p)
221 "Join current line with the previous one, deleting excess whitespace.
222 All whitespace is replaced with a single space, unless it is at the beginning
223 of a line, immmediately following a \"(\", or immediately preceding a \")\",
224 in which case the whitespace is merely deleted. If the preceeding character
225 is a sentence terminator, two spaces are left instead of one. If a prefix
226 argument is given, the following line is joined with the current line."
227 "Join current line with the previous one, deleting excess whitespace."
228 (with-mark ((m (current-point) :right-inserting))
229 (when p (line-offset m 1))
230 (line-start m)
231 (unless (delete-characters m -1) (editor-error "No previous line."))
232 (delete-horizontal-space m)
233 (let ((prev (previous-character m)))
234 (when (and prev (char/= prev #\newline))
235 (cond ((not (zerop (character-attribute :sentence-terminator prev)))
236 (insert-string m " "))
237 ((not (or (eq (character-attribute :lisp-syntax prev) :open-paren)
238 (eq (character-attribute :lisp-syntax (next-character m))
239 :close-paren)))
240 (insert-character m #\space)))))))
241
242
243 (defcommand "Delete Horizontal Space" (p)
244 "Delete spaces and tabs surrounding the point."
245 "Delete spaces and tabs surrounding the point."
246 (declare (ignore p))
247 (delete-horizontal-space (current-point)))
248
249 (defcommand "Just One Space" (p)
250 "Leave one space.
251 Surrounding space is deleted, and then one space is inserted.
252 with prefix argument insert that number of spaces."
253 "Delete surrounding space and insert P spaces."
254 (let ((point (current-point)))
255 (delete-horizontal-space point)
256 (dotimes (i (or p 1)) (insert-character point #\space))))
257
258 (defcommand "Back to Indentation" (p)
259 "Move point to the first non-whitespace character on the line."
260 "Move point to the first non-whitespace character on the line."
261 (declare (ignore p))
262 (let ((point (current-point)))
263 (line-start point)
264 (find-attribute point :whitespace #'zerop)))
265
266 (defcommand "Indent Rigidly" (p)
267 "Indent the region rigidly by p spaces.
268 Each line in the region is moved p spaces to the right (left if p is
269 negative). When moving a line to the left, tabs are converted to spaces."
270 "Indent the region rigidly p spaces to the right (left if p is negative)."
271 (let ((p (or p (value spaces-per-tab)))
272 (region (current-region)))
273 (with-mark ((mark1 (region-start region) :left-inserting)
274 (mark2 (region-end region) :left-inserting))
275 (line-start mark1)
276 (line-start mark2)
277 (do ()
278 ((mark= mark1 mark2))
279 (cond ((empty-line-p mark1))
280 ((blank-after-p mark1)
281 (delete-characters mark1 (line-length (mark-line mark1))))
282 (t (find-attribute mark1 :whitespace #'zerop)
283 (let ((new-column (+ p (mark-column mark1))))
284 (delete-characters mark1 (- (mark-charpos mark1)))
285 (if (plusp new-column)
286 (funcall (value indent-with-tabs) mark1 new-column)))))
287 (line-offset mark1 1 0)))))

  ViewVC Help
Powered by ViewVC 1.1.5