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

Contents of /src/hemlock/htext2.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Sun Jun 17 12:17:22 1990 UTC (23 years, 10 months ago) by ram
Changes since 1.1: +0 -1 lines
Flushed spurious ignore declaration.
1 ;;; -*- Log: hemlock.log; Package: Hemlock-Internals -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the Spice Lisp project at
5 ;;; Carnegie-Mellon University, and has been placed in the public domain.
6 ;;; Spice Lisp is currently incomplete and under active development.
7 ;;; If you want to use this code or any part of Spice Lisp, please contact
8 ;;; Scott Fahlman (FAHLMAN@CMUC).
9 ;;; **********************************************************************
10 ;;;
11 ;;; More Hemlock Text-Manipulation functions.
12 ;;; Written by Skef Wholey.
13 ;;;
14 ;;; The code in this file implements the non-insert/delete functions in the
15 ;;; "Doing Stuff and Going Places" chapter of the Hemlock Design document.
16 ;;;
17
18 (in-package "HEMLOCK-INTERNALS")
19
20 (export '(region-to-string string-to-region line-to-region
21 previous-character next-character count-lines
22 count-characters line-start line-end buffer-start
23 buffer-end move-mark mark-before mark-after
24 character-offset line-offset region-bounds
25 set-region-bounds *print-region*))
26
27
28
29 (defun region-to-string (region)
30 "Returns a string containing the characters in the given Region."
31 (close-line)
32 (let* ((dst-length (count-characters region))
33 (string (make-string dst-length))
34 (start-mark (region-start region))
35 (end-mark (region-end region))
36 (start-line (mark-line start-mark))
37 (end-line (mark-line end-mark))
38 (start-charpos (mark-charpos start-mark)))
39 (declare (simple-string string))
40 (if (eq start-line end-line)
41 (%sp-byte-blt (line-chars start-line) start-charpos string 0
42 dst-length)
43 (let ((index ()))
44 (let* ((line-chars (line-chars start-line))
45 (dst-end (- (length line-chars) start-charpos)))
46 (declare (simple-string line-chars))
47 (%sp-byte-blt line-chars start-charpos string 0 dst-end)
48 (setf (char string dst-end) #\newline)
49 (setq index (1+ dst-end)))
50 (do* ((line (line-next start-line) (line-next line))
51 (chars (line-chars line) (line-chars line)))
52 ((eq line end-line)
53 (%sp-byte-blt (line-chars line) 0 string index dst-length))
54 (declare (simple-string chars))
55 (%sp-byte-blt (line-chars line) 0 string index
56 (incf index (length chars)))
57 (setf (char string index) #\newline)
58 (setq index (1+ index)))))
59 string))
60
61 (defun string-to-region (string)
62 "Returns a region containing the characters in the given String."
63 (let* ((string (if (simple-string-p string)
64 string (coerce string 'simple-string)))
65 (end (length string)))
66 (declare (simple-string string))
67 (do* ((index 0)
68 (buffer (incf *disembodied-buffer-counter*))
69 (previous-line)
70 (line (make-line :%buffer buffer))
71 (first-line line))
72 (())
73 (let ((right-index (%sp-find-character string index end #\newline)))
74 (cond (right-index
75 (let* ((length (- right-index index))
76 (chars (make-string length)))
77 (%sp-byte-blt string index chars 0 length)
78 (setf (line-chars line) chars))
79 (setq index (1+ right-index))
80 (setq previous-line line)
81 (setq line (make-line :%buffer buffer))
82 (setf (line-next previous-line) line)
83 (setf (line-previous line) previous-line))
84 (t
85 (let* ((length (- end index))
86 (chars (make-string length)))
87 (%sp-byte-blt string index chars 0 length)
88 (setf (line-chars line) chars))
89 (return (renumber-region
90 (internal-make-region
91 (mark first-line 0 :right-inserting)
92 (mark line (length (line-chars line))
93 :left-inserting))))))))))
94
95 (defun line-to-region (line)
96 "Returns a region containing the specified line."
97 (internal-make-region (mark line 0 :right-inserting)
98 (mark line (line-length* line) :left-inserting)))
99
100 (defun previous-character (mark)
101 "Returns the character immediately before the given Mark."
102 (let ((line (mark-line mark))
103 (charpos (mark-charpos mark)))
104 (if (= charpos 0)
105 (if (line-previous line)
106 #\newline
107 nil)
108 (if (eq line open-line)
109 (char (the simple-string open-chars)
110 (if (<= charpos left-open-pos)
111 (1- charpos)
112 (1- (+ right-open-pos (- charpos left-open-pos)))))
113 (schar (line-chars line) (1- charpos))))))
114
115 (defun next-character (mark)
116 "Returns the character immediately after the given Mark."
117 (let ((line (mark-line mark))
118 (charpos (mark-charpos mark)))
119 (if (eq line open-line)
120 (if (= charpos (- line-cache-length (- right-open-pos left-open-pos)))
121 (if (line-next line)
122 #\newline
123 nil)
124 (schar open-chars
125 (if (< charpos left-open-pos)
126 charpos
127 (+ right-open-pos (- charpos left-open-pos)))))
128 (let ((chars (line-chars line)))
129 (if (= charpos (strlen chars))
130 (if (line-next line)
131 #\newline
132 nil)
133 (schar chars charpos))))))
134
135 ;;; %Set-Next-Character -- Internal
136 ;;;
137 ;;; This is the setf form for Next-Character. Since we may change a
138 ;;; character to or from a newline, we must be prepared to split and
139 ;;; join lines. We cannot just delete a character and insert the new one
140 ;;; because the marks would not be right.
141 ;;;
142 (defun %set-next-character (mark character)
143 (let* ((line (mark-line mark))
144 (buffer (line-%buffer line))
145 (next (line-next line)))
146 (modifying-buffer buffer
147 (modifying-line line mark)
148 (cond ((= right-open-pos line-cache-length)
149 ;; The mark is at the end of the line.
150 (unless next
151 (error "~S has no next character, so it cannot be set." mark))
152 (unless (char= character #\newline)
153 ;; If the character is no longer a newline then mash two
154 ;; lines together.
155 (let ((chars (line-chars next)))
156 (declare (simple-string chars))
157 (setq right-open-pos (- line-cache-length (length chars)))
158 (when (<= right-open-pos left-open-pos)
159 (grow-open-chars (* (+ (length chars) left-open-pos 1) 2)))
160 (%sp-byte-blt chars 0 open-chars right-open-pos
161 line-cache-length)
162 (setf (schar open-chars left-open-pos) character)
163 (incf left-open-pos))
164 (move-some-marks (charpos next line)
165 (+ charpos left-open-pos))
166 (setq next (line-next next))
167 (setf (line-next line) next)
168 (when next (setf (line-previous next) line))))
169 ((char= character #\newline)
170 ;; The char is being changed to a newline, so we must split lines.
171 (incf right-open-pos)
172 (let* ((len (- line-cache-length right-open-pos))
173 (chars (make-string len))
174 (new (make-line :chars chars :previous line
175 :next next :%buffer buffer)))
176 (%sp-byte-blt open-chars right-open-pos chars 0 len)
177 (maybe-move-some-marks* (charpos line new) left-open-pos
178 (- charpos left-open-pos 1))
179 (setf (line-next line) new)
180 (when next (setf (line-previous next) new))
181 (setq right-open-pos line-cache-length)
182 (number-line new)))
183 (t
184 (setf (char (the simple-string open-chars) right-open-pos)
185 character)))))
186 character)
187
188 ;;; %Set-Previous-Character -- Internal
189 ;;;
190 ;;; The setf form for Previous-Character. We just Temporarily move the
191 ;;; mark back one and call %Set-Next-Character.
192 ;;;
193 (defun %set-previous-character (mark character)
194 (unless (mark-before mark)
195 (error "~S has no previous character, so it cannot be set." mark))
196 (%set-next-character mark character)
197 (mark-after mark)
198 character)
199
200 (defun count-lines (region)
201 "Returns the number of lines in the region, first and last lines inclusive."
202 (do ((line (mark-line (region-start region)) (line-next line))
203 (count 1 (1+ count))
204 (last-line (mark-line (region-end region))))
205 ((eq line last-line) count)))
206
207 (defun count-characters (region)
208 "Returns the number of characters in the region."
209 (let* ((start (region-start region))
210 (end (region-end region))
211 (first-line (mark-line start))
212 (last-line (mark-line end)))
213 (if (eq first-line last-line)
214 (- (mark-charpos end) (mark-charpos start))
215 (do ((line (line-next first-line) (line-next line))
216 (count (1+ (- (line-length* first-line) (mark-charpos start)))))
217 ((eq line last-line)
218 (+ count (mark-charpos end)))
219 (setq count (+ 1 count (line-length* line)))))))
220
221 (defun line-start (mark &optional line)
222 "Changes the Mark to point to the beginning of the Line and returns it.
223 Line defaults to the line Mark is on."
224 (when line
225 (change-line mark line))
226 (setf (mark-charpos mark) 0)
227 mark)
228
229 (defun line-end (mark &optional line)
230 "Changes the Mark to point to the end of the line and returns it.
231 Line defaults to the line Mark is on."
232 (if line
233 (change-line mark line)
234 (setq line (mark-line mark)))
235 (setf (mark-charpos mark) (line-length* line))
236 mark)
237
238 (defun buffer-start (mark &optional (buffer (line-buffer (mark-line mark))))
239 "Change Mark to point to the beginning of Buffer, which defaults to
240 the buffer Mark is currently in."
241 (unless buffer (error "Mark ~S does not point into a buffer."))
242 (move-mark mark (buffer-start-mark buffer)))
243
244 (defun buffer-end (mark &optional (buffer (line-buffer (mark-line mark))))
245 "Change Mark to point to the end of Buffer, which defaults to
246 the buffer Mark is currently in."
247 (unless buffer (error "Mark ~S does not point into a buffer."))
248 (move-mark mark (buffer-end-mark buffer)))
249
250 (defun move-mark (mark new-position)
251 "Changes the Mark to point to the same position as New-Position."
252 (let ((line (mark-line new-position)))
253 (change-line mark line))
254 (setf (mark-charpos mark) (mark-charpos new-position))
255 mark)
256
257 (defun mark-before (mark)
258 "Changes the Mark to point one character before where it currently points.
259 NIL is returned if there is no previous character."
260 (let ((charpos (mark-charpos mark)))
261 (cond ((zerop charpos)
262 (let ((prev (line-previous (mark-line mark))))
263 (when prev
264 (always-change-line mark prev)
265 (setf (mark-charpos mark) (line-length* prev))
266 mark)))
267 (t
268 (setf (mark-charpos mark) (1- charpos))
269 mark))))
270
271 (defun mark-after (mark)
272 "Changes the Mark to point one character after where it currently points.
273 NIL is returned if there is no previous character."
274 (let ((line (mark-line mark))
275 (charpos (mark-charpos mark)))
276 (cond ((= charpos (line-length* line))
277 (let ((next (line-next line)))
278 (when next
279 (always-change-line mark next)
280 (setf (mark-charpos mark) 0)
281 mark)))
282 (t
283 (setf (mark-charpos mark) (1+ charpos))
284 mark))))
285
286 (defun character-offset (mark n)
287 "Changes the Mark to point N characters after (or -N before if N is negative)
288 where it currently points. If there aren't N characters before (or after)
289 the mark, Nil is returned."
290 (let ((charpos (mark-charpos mark)))
291 (if (< n 0)
292 (let ((n (- n)))
293 (if (< charpos n)
294 (do ((line (line-previous (mark-line mark)) (line-previous line))
295 (n (- n charpos 1)))
296 ((null line) nil)
297 (let ((length (line-length* line)))
298 (cond ((<= n length)
299 (always-change-line mark line)
300 (setf (mark-charpos mark) (- length n))
301 (return mark))
302 (t
303 (setq n (- n (1+ length)))))))
304 (progn (setf (mark-charpos mark) (- charpos n))
305 mark)))
306 (let* ((line (mark-line mark))
307 (length (line-length* line)))
308 (if (> (+ charpos n) length)
309 (do ((line (line-next line) (line-next line))
310 (n (- n (1+ (- length charpos)))))
311 ((null line) nil)
312 (let ((length (line-length* line)))
313 (cond ((<= n length)
314 (always-change-line mark line)
315 (setf (mark-charpos mark) n)
316 (return mark))
317 (t
318 (setq n (- n (1+ length)))))))
319 (progn (setf (mark-charpos mark) (+ charpos n))
320 mark))))))
321
322 (defun line-offset (mark n &optional charpos)
323 "Changes to Mark to point N lines after (-N before if N is negative) where
324 it currently points. If there aren't N lines after (or before) the Mark,
325 Nil is returned."
326 (if (< n 0)
327 (do ((line (mark-line mark) (line-previous line))
328 (n n (1+ n)))
329 ((null line) nil)
330 (when (= n 0)
331 (always-change-line mark line)
332 (setf (mark-charpos mark)
333 (if charpos
334 (min (line-length line) charpos)
335 (min (line-length line) (mark-charpos mark))))
336 (return mark)))
337 (do ((line (mark-line mark) (line-next line))
338 (n n (1- n)))
339 ((null line) nil)
340 (when (= n 0)
341 (change-line mark line)
342 (setf (mark-charpos mark)
343 (if charpos
344 (min (line-length line) charpos)
345 (min (line-length line) (mark-charpos mark))))
346 (return mark)))))
347
348 ;;; region-bounds -- Public
349 ;;;
350 (defun region-bounds (region)
351 "Return as multiple-value the start and end of Region."
352 (values (region-start region) (region-end region)))
353
354 (defun set-region-bounds (region start end)
355 "Set the start and end of Region to the marks Start and End."
356 (let ((sl (mark-line start))
357 (el (mark-line end)))
358 (when (or (neq (line-%buffer sl) (line-%buffer el))
359 (> (line-number sl) (line-number el))
360 (and (eq sl el) (> (mark-charpos start) (mark-charpos end))))
361 (error "Marks ~S and ~S cannot be made into a region." start end))
362 (setf (region-start region) start (region-end region) end))
363 region)
364
365
366 ;;;; Debugging stuff.
367
368 (defun slf (string)
369 "For a good time, figure out what this function does, and why it was written."
370 (delete #\linefeed (the simple-string string)))
371
372 (defun %print-whole-line (structure stream)
373 (cond ((eq structure open-line)
374 (write-string open-chars stream :end left-open-pos)
375 (write-string open-chars stream :start right-open-pos
376 :end line-cache-length))
377 (t
378 (write-string (line-chars structure) stream))))
379
380 (defun %print-before-mark (mark stream)
381 (if (mark-line mark)
382 (let* ((line (mark-line mark))
383 (chars (line-chars line))
384 (charpos (mark-charpos mark))
385 (length (line-length line)))
386 (declare (simple-string chars))
387 (cond ((or (> charpos length) (< charpos 0))
388 (write-string "{bad mark}" stream))
389 ((eq line open-line)
390 (cond ((< charpos left-open-pos)
391 (write-string open-chars stream :end charpos))
392 (t
393 (write-string open-chars stream :end left-open-pos)
394 (let ((p (+ charpos (- right-open-pos left-open-pos))))
395 (write-string open-chars stream :start right-open-pos
396 :end p)))))
397 (t
398 (write-string chars stream :end charpos))))
399 (write-string "{deleted mark}" stream)))
400
401
402 (defun %print-after-mark (mark stream)
403 (if (mark-line mark)
404 (let* ((line (mark-line mark))
405 (chars (line-chars line))
406 (charpos (mark-charpos mark))
407 (length (line-length line)))
408 (declare (simple-string chars))
409 (cond ((or (> charpos length) (< charpos 0))
410 (write-string "{bad mark}" stream))
411 ((eq line open-line)
412 (cond ((< charpos left-open-pos)
413 (write-string open-chars stream :start charpos
414 :end left-open-pos)
415 (write-string open-chars stream :start right-open-pos
416 :end line-cache-length))
417 (t
418 (let ((p (+ charpos (- right-open-pos left-open-pos))))
419 (write-string open-chars stream :start p
420 :end line-cache-length)))))
421 (t
422 (write-string chars stream :start charpos :end length))))
423 (write-string "{deleted mark}" stream)))
424
425 (defun %print-hline (structure stream d)
426 (declare (ignore d))
427 (write-string "#<Hemlock Line \"" stream)
428 (%print-whole-line structure stream)
429 (write-string "\">" stream))
430
431 (defun %print-hmark (structure stream d)
432 (declare (ignore d))
433 (write-string "#<Hemlock Mark \"" stream)
434 (%print-before-mark structure stream)
435 (write-string "/\\" stream)
436 (%print-after-mark structure stream)
437 (write-string "\">" stream))
438
439 (defvar *print-region* 10
440 "The number of lines to print out of a region, or NIL if none.")
441
442 (defun %print-hregion (region stream d)
443 (declare (ignore d))
444 (write-string "#<Hemlock Region \"" stream)
445 (let* ((start (region-start region))
446 (end (region-end region))
447 (first-line (mark-line start))
448 (last-line (mark-line end)))
449 (cond
450 ((not (and (linep first-line) (linep last-line)
451 (eq (line-%buffer first-line) (line-%buffer last-line))
452 (mark<= start end)))
453 (write-string "{bad region}" stream))
454 (*print-region*
455 (cond ((eq first-line last-line)
456 (let ((cs (mark-charpos start))
457 (ce (mark-charpos end))
458 (len (line-length first-line)))
459 (cond
460 ((or (< cs 0) (> ce len))
461 (write-string "{bad region}" stream))
462 ((eq first-line open-line)
463 (let ((gap (- right-open-pos left-open-pos)))
464 (cond
465 ((<= ce left-open-pos)
466 (write-string open-chars stream :start cs :end ce))
467 ((>= cs left-open-pos)
468 (write-string open-chars stream :start (+ cs gap)
469 :end (+ ce gap)))
470 (t
471 (write-string open-chars stream :start cs
472 :end left-open-pos)
473 (write-string open-chars stream :start right-open-pos
474 :end (+ gap ce))))))
475 (t
476 (write-string (line-chars first-line) stream :start cs
477 :end ce)))))
478 (t
479 (%print-after-mark start stream)
480 (write-char #\/ stream)
481 (do ((line (line-next first-line) (line-next line))
482 (last-line (mark-line end))
483 (cnt *print-region* (1- cnt)))
484 ((or (eq line last-line)
485 (when (zerop cnt) (write-string "..." stream) t))
486 (%print-before-mark end stream))
487 (%print-whole-line line stream)
488 (write-char #\/ stream)))))
489 (t
490 (write-string "{mumble}" stream))))
491 (write-string "\">" stream))
492
493 (defun %print-hbuffer (structure stream d)
494 (declare (ignore d))
495 (write-string "#<Hemlock Buffer \"" stream)
496 (write-string (buffer-name structure) stream)
497 (write-string "\">" stream))

  ViewVC Help
Powered by ViewVC 1.1.5