1 ;;; gap-buffer.scm --- String buffer that supports point
3 ;;; Copyright (C) 2002 Free Software Foundation, Inc.
5 ;;; This program is free software; you can redistribute it and/or modify
6 ;;; it under the terms of the GNU General Public License as published by
7 ;;; the Free Software Foundation; either version 2, or (at your option)
10 ;;; This program is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;; GNU General Public License for more details.
15 ;;; You should have received a copy of the GNU General Public License
16 ;;; along with this software; see the file COPYING. If not, write to
17 ;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 ;;; Boston, MA 02111-1307 USA
20 ;;; As a special exception, the Free Software Foundation gives permission
21 ;;; for additional uses of the text contained in its release of GUILE.
23 ;;; The exception is that, if you link the GUILE library with other files
24 ;;; to produce an executable, this does not by itself cause the
25 ;;; resulting executable to be covered by the GNU General Public License.
26 ;;; Your use of that executable is in no way restricted on account of
27 ;;; linking the GUILE library code into it.
29 ;;; This exception does not however invalidate any other reasons why
30 ;;; the executable file might be covered by the GNU General Public License.
32 ;;; This exception applies only to the code released by the
33 ;;; Free Software Foundation under the name GUILE. If you copy
34 ;;; code from other Free Software Foundation releases into a copy of
35 ;;; GUILE, as the General Public License permits, the exception does
36 ;;; not apply to the code that you add in this way. To avoid misleading
37 ;;; anyone as to the status of such modified files, you must delete
38 ;;; this exception notice from them.
40 ;;; If you write modifications of your own for GUILE, it is your choice
41 ;;; whether to permit this exception to apply to your modifications.
42 ;;; If you do not wish that, delete this exception notice.
45 ;;; Author: Thien-Thi Nguyen <ttn@gnu.org>
49 ;; A gap buffer is a structure that models a string but allows relatively
50 ;; efficient insertion of text somewhere in the middle. The insertion
51 ;; location is called `point' with minimum value 1, and a maximum value of the
52 ;; length of the string (which is not fixed).
54 ;; Specifically, we allocate a continuous buffer of characters that is
55 ;; composed of the BEFORE, the GAP and the AFTER (reading L->R), like so:
59 ;; +--------------------+--------------------+--------------------+
60 ;; | BEFORE | GAP | AFTER |
61 ;; +--------------------+--------------------+--------------------+
63 ;; <----- bef-sz ----->|<----- gap-sz ----->|<----- aft-sz ----->
65 ;; <-------------------| usr-sz |------------------->
67 ;; <-------------------------- all-sz -------------------------->
69 ;; This diagram also shows how the different sizes are computed, and the
70 ;; location of POINT. Note that the user-visible buffer size `usr-sz' does
71 ;; NOT include the GAP, while the allocation `all-sz' DOES.
73 ;; The consequence of this arrangement is that "moving point" is simply a
74 ;; matter of kicking characters across the GAP, while insertion can be viewed
75 ;; as filling up the gap, increasing `bef-sz' and decreasing `gap-sz'. When
76 ;; `gap-sz' falls below some threshold, we reallocate with a larger `all-sz'.
78 ;; In the implementation, we actually keep track of the AFTER start offset
79 ;; `aft-ofs' since it is used more often than `gap-sz'. In fact, most of the
80 ;; variables in the diagram are for conceptualization only.
82 ;; A gap buffer port is a soft port (see Guile manual) that wraps a gap
83 ;; buffer. Character and string writes, as well as character reads, are
84 ;; supported. Flushing and closing are not supported.
86 ;; These procedures are exported:
88 ;; (make-gap-buffer . INIT)
92 ;; (gb-insert-string! GB STRING)
93 ;; (gb-insert-char! GB CHAR)
94 ;; (gb-delete-char! GB COUNT)
95 ;; (gb-goto-char GB LOCATION)
97 ;; (gb-filter! GB STRING-PROC)
99 ;; (gb-filter-lines! GB LINES-PROC)
100 ;; (make-gap-buffer-port GB)
102 ;; INIT is an optional port or a string. COUNT and LOCATION are integers.
103 ;; STRING-PROC is a procedure that takes and returns a string. LINES-PROC is
104 ;; a procedure that takes and returns a list of strings, each representing a
105 ;; line of text (newlines are stripped and added back automatically).
107 ;; (The term and concept of "gap buffer" are borrowed from Emacs. We will
108 ;; gladly return them when libemacs.so is available. ;-)
111 ;; - overrun errors are suppressed silently
115 (define-module (ice-9 gap-buffer)
116 :autoload (srfi srfi-13) (string-join)
131 make-gap-buffer-port))
134 (make-record-type 'gap-buffer
135 '(s ; the buffer, a string
136 all-sz ; total allocation
137 gap-ofs ; GAP starts, aka (1- point)
138 aft-ofs ; AFTER starts
141 (define gb? (record-predicate gap-buffer))
143 (define s: (record-accessor gap-buffer 's))
144 (define all-sz: (record-accessor gap-buffer 'all-sz))
145 (define gap-ofs: (record-accessor gap-buffer 'gap-ofs))
146 (define aft-ofs: (record-accessor gap-buffer 'aft-ofs))
148 (define s! (record-modifier gap-buffer 's))
149 (define all-sz! (record-modifier gap-buffer 'all-sz))
150 (define gap-ofs! (record-modifier gap-buffer 'gap-ofs))
151 (define aft-ofs! (record-modifier gap-buffer 'aft-ofs))
154 (define default-initial-allocation 128)
155 (define default-chunk-size 128)
156 (define default-realloc-threshold 32)
159 (* default-chunk-size (+ 1 (quotient n default-chunk-size))))
161 (define new (record-constructor gap-buffer '()))
163 (define (realloc gb inc)
164 (let* ((old-s (s: gb))
165 (all-sz (all-sz: gb))
166 (new-sz (+ all-sz inc))
167 (gap-ofs (gap-ofs: gb))
168 (aft-ofs (aft-ofs: gb))
169 (new-s (make-string new-sz))
170 (new-aft-ofs (+ aft-ofs inc)))
171 (substring-move! old-s 0 gap-ofs new-s 0)
172 (substring-move! old-s aft-ofs all-sz new-s new-aft-ofs)
175 (aft-ofs! gb new-aft-ofs)))
177 (define (make-gap-buffer . init) ; port/string
180 (s! gb (make-string default-initial-allocation))
181 (all-sz! gb default-initial-allocation)
183 (aft-ofs! gb default-initial-allocation))
184 (else (let ((jam! (lambda (string len)
185 (let ((alloc (round-up len)))
186 (s! gb (make-string alloc))
188 (substring-move! string 0 len (s: gb) 0)
190 (aft-ofs! gb alloc))))
193 (let ((next (lambda () (read-char v))))
194 (let loop ((c (next)) (acc '()) (len 0))
196 (jam! (list->string (reverse acc)) len)
197 (loop (next) (cons c acc) (1+ len))))))
199 (jam! v (string-length v)))
200 (else (error "bad init type"))))))
203 (define (gb-point gb)
206 (define (gb-point-min gb) 1) ; no narrowing (for now)
208 (define (gb-point-max gb)
209 (1+ (- (all-sz: gb) (- (aft-ofs: gb) (gap-ofs: gb)))))
211 (define (insert-prep gb len)
212 (let* ((gap-ofs (gap-ofs: gb))
213 (aft-ofs (aft-ofs: gb))
214 (slack (- (- aft-ofs gap-ofs) len)))
215 (and (< slack default-realloc-threshold)
216 (realloc gb (round-up (- slack))))
219 (define (gb-insert-string! gb string)
220 (let* ((len (string-length string))
221 (gap-ofs (insert-prep gb len)))
222 (substring-move! string 0 len (s: gb) gap-ofs)
223 (gap-ofs! gb (+ gap-ofs len))))
225 (define (gb-insert-char! gb char)
226 (let ((gap-ofs (insert-prep gb 1)))
227 (string-set! (s: gb) gap-ofs char)
228 (gap-ofs! gb (+ gap-ofs 1))))
230 (define (gb-delete-char! gb count)
231 (cond ((< count 0) ; backwards
232 (gap-ofs! gb (max 0 (+ (gap-ofs: gb) count))))
233 ((> count 0) ; forwards
234 (aft-ofs! gb (min (all-sz: gb) (+ (aft-ofs: gb) count))))
235 ((= count 0) ; do nothing
238 (define (gb-erase! gb)
240 (aft-ofs! gb (all-sz: gb)))
242 (define (point++n! gb n s gap-ofs aft-ofs) ; n>0; warning: reckless
243 (substring-move-left! s aft-ofs (+ aft-ofs n) s gap-ofs)
244 (gap-ofs! gb (+ gap-ofs n))
245 (aft-ofs! gb (+ aft-ofs n)))
247 (define (point+-n! gb n s gap-ofs aft-ofs) ; n<0; warning: reckless
248 (substring-move-right! s (+ gap-ofs n) gap-ofs s (+ aft-ofs n))
249 (gap-ofs! gb (+ gap-ofs n))
250 (aft-ofs! gb (+ aft-ofs n)))
252 (define (gb-goto-char gb new-point)
253 (let ((pmax (gb-point-max gb)))
254 (or (and (< new-point 1) (gb-goto-char gb 1))
255 (and (> new-point pmax) (gb-goto-char gb pmax))
256 (let ((delta (- new-point (gb-point gb))))
261 gb delta (s: gb) (gap-ofs: gb) (aft-ofs: gb))))))
264 (define (gb->string gb)
266 (string-append (substring s 0 (gap-ofs: gb))
267 (substring s (aft-ofs: gb)))))
269 (define (gb-filter! gb string-proc)
270 (let ((new (string-proc (gb->string gb))))
272 (gb-insert-string! gb new)))
274 (define (gb->lines gb)
275 (let ((str (gb->string gb)))
276 (let loop ((start 0) (acc '()))
277 (cond ((string-index str #\newline start)
279 (loop (1+ w) (cons (substring str start w) acc))))
280 (else (reverse (cons (substring str start) acc)))))))
282 (define (gb-filter-lines! gb lines-proc)
283 (let ((new-lines (lines-proc (gb->lines gb))))
285 (gb-insert-string! gb (string-join new-lines #\newline))))
287 (define (make-gap-buffer-port gb)
289 (error "not a gap-buffer:" gb))
292 (lambda (c) (gb-insert-char! gb c))
293 (lambda (s) (gb-insert-string! gb s))
295 (lambda () (let ((gap-ofs (gap-ofs: gb))
296 (aft-ofs (aft-ofs: gb)))
297 (if (= aft-ofs (all-sz: gb))
300 (c (string-ref s aft-ofs)))
301 (string-set! s gap-ofs c)
302 (gap-ofs! gb (1+ gap-ofs))
303 (aft-ofs! gb (1+ aft-ofs))
308 ;;; gap-buffer.scm ends here