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

Contents of /src/hemlock/htext2.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5