gap-buffer.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. ;;; gap-buffer.scm --- String buffer that supports point
  2. ;;; Copyright (C) 2002, 2003, 2006 Free Software Foundation, Inc.
  3. ;;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 2.1 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;;
  18. ;;; Author: Thien-Thi Nguyen <ttn@gnu.org>
  19. ;;; Commentary:
  20. ;; A gap buffer is a structure that models a string but allows relatively
  21. ;; efficient insertion of text somewhere in the middle. The insertion
  22. ;; location is called `point' with minimum value 1, and a maximum value of the
  23. ;; length of the string (which is not fixed).
  24. ;;
  25. ;; Specifically, we allocate a continuous buffer of characters that is
  26. ;; composed of the BEFORE, the GAP and the AFTER (reading L->R), like so:
  27. ;;
  28. ;; +--- POINT
  29. ;; v
  30. ;; +--------------------+--------------------+--------------------+
  31. ;; | BEFORE | GAP | AFTER |
  32. ;; +--------------------+--------------------+--------------------+
  33. ;;
  34. ;; <----- bef-sz ----->|<----- gap-sz ----->|<----- aft-sz ----->
  35. ;;
  36. ;; <-------------------| usr-sz |------------------->
  37. ;;
  38. ;; <-------------------------- all-sz -------------------------->
  39. ;;
  40. ;; This diagram also shows how the different sizes are computed, and the
  41. ;; location of POINT. Note that the user-visible buffer size `usr-sz' does
  42. ;; NOT include the GAP, while the allocation `all-sz' DOES.
  43. ;;
  44. ;; The consequence of this arrangement is that "moving point" is simply a
  45. ;; matter of kicking characters across the GAP, while insertion can be viewed
  46. ;; as filling up the gap, increasing `bef-sz' and decreasing `gap-sz'. When
  47. ;; `gap-sz' falls below some threshold, we reallocate with a larger `all-sz'.
  48. ;;
  49. ;; In the implementation, we actually keep track of the AFTER start offset
  50. ;; `aft-ofs' since it is used more often than `gap-sz'. In fact, most of the
  51. ;; variables in the diagram are for conceptualization only.
  52. ;;
  53. ;; A gap buffer port is a soft port (see Guile manual) that wraps a gap
  54. ;; buffer. Character and string writes, as well as character reads, are
  55. ;; supported. Flushing and closing are not supported.
  56. ;;
  57. ;; These procedures are exported:
  58. ;; (gb? OBJ)
  59. ;; (make-gap-buffer . INIT)
  60. ;; (gb-point GB)
  61. ;; (gb-point-min GB)
  62. ;; (gb-point-max GB)
  63. ;; (gb-insert-string! GB STRING)
  64. ;; (gb-insert-char! GB CHAR)
  65. ;; (gb-delete-char! GB COUNT)
  66. ;; (gb-goto-char GB LOCATION)
  67. ;; (gb->string GB)
  68. ;; (gb-filter! GB STRING-PROC)
  69. ;; (gb->lines GB)
  70. ;; (gb-filter-lines! GB LINES-PROC)
  71. ;; (make-gap-buffer-port GB)
  72. ;;
  73. ;; INIT is an optional port or a string. COUNT and LOCATION are integers.
  74. ;; STRING-PROC is a procedure that takes and returns a string. LINES-PROC is
  75. ;; a procedure that takes and returns a list of strings, each representing a
  76. ;; line of text (newlines are stripped and added back automatically).
  77. ;;
  78. ;; (The term and concept of "gap buffer" are borrowed from Emacs. We will
  79. ;; gladly return them when libemacs.so is available. ;-)
  80. ;;
  81. ;; Notes:
  82. ;; - overrun errors are suppressed silently
  83. ;;; Code:
  84. (define-module (ice-9 gap-buffer)
  85. :autoload (srfi srfi-13) (string-join)
  86. :export (gb?
  87. make-gap-buffer
  88. gb-point
  89. gb-point-min
  90. gb-point-max
  91. gb-insert-string!
  92. gb-insert-char!
  93. gb-delete-char!
  94. gb-erase!
  95. gb-goto-char
  96. gb->string
  97. gb-filter!
  98. gb->lines
  99. gb-filter-lines!
  100. make-gap-buffer-port))
  101. (define gap-buffer
  102. (make-record-type 'gap-buffer
  103. '(s ; the buffer, a string
  104. all-sz ; total allocation
  105. gap-ofs ; GAP starts, aka (1- point)
  106. aft-ofs ; AFTER starts
  107. )))
  108. (define gb? (record-predicate gap-buffer))
  109. (define s: (record-accessor gap-buffer 's))
  110. (define all-sz: (record-accessor gap-buffer 'all-sz))
  111. (define gap-ofs: (record-accessor gap-buffer 'gap-ofs))
  112. (define aft-ofs: (record-accessor gap-buffer 'aft-ofs))
  113. (define s! (record-modifier gap-buffer 's))
  114. (define all-sz! (record-modifier gap-buffer 'all-sz))
  115. (define gap-ofs! (record-modifier gap-buffer 'gap-ofs))
  116. (define aft-ofs! (record-modifier gap-buffer 'aft-ofs))
  117. ;; todo: expose
  118. (define default-initial-allocation 128)
  119. (define default-chunk-size 128)
  120. (define default-realloc-threshold 32)
  121. (define (round-up n)
  122. (* default-chunk-size (+ 1 (quotient n default-chunk-size))))
  123. (define new (record-constructor gap-buffer '()))
  124. (define (realloc gb inc)
  125. (let* ((old-s (s: gb))
  126. (all-sz (all-sz: gb))
  127. (new-sz (+ all-sz inc))
  128. (gap-ofs (gap-ofs: gb))
  129. (aft-ofs (aft-ofs: gb))
  130. (new-s (make-string new-sz))
  131. (new-aft-ofs (+ aft-ofs inc)))
  132. (substring-move! old-s 0 gap-ofs new-s 0)
  133. (substring-move! old-s aft-ofs all-sz new-s new-aft-ofs)
  134. (s! gb new-s)
  135. (all-sz! gb new-sz)
  136. (aft-ofs! gb new-aft-ofs)))
  137. (define (make-gap-buffer . init) ; port/string
  138. (let ((gb (new)))
  139. (cond ((null? init)
  140. (s! gb (make-string default-initial-allocation))
  141. (all-sz! gb default-initial-allocation)
  142. (gap-ofs! gb 0)
  143. (aft-ofs! gb default-initial-allocation))
  144. (else (let ((jam! (lambda (string len)
  145. (let ((alloc (round-up len)))
  146. (s! gb (make-string alloc))
  147. (all-sz! gb alloc)
  148. (substring-move! string 0 len (s: gb) 0)
  149. (gap-ofs! gb len)
  150. (aft-ofs! gb alloc))))
  151. (v (car init)))
  152. (cond ((port? v)
  153. (let ((next (lambda () (read-char v))))
  154. (let loop ((c (next)) (acc '()) (len 0))
  155. (if (eof-object? c)
  156. (jam! (list->string (reverse acc)) len)
  157. (loop (next) (cons c acc) (1+ len))))))
  158. ((string? v)
  159. (jam! v (string-length v)))
  160. (else (error "bad init type"))))))
  161. gb))
  162. (define (gb-point gb)
  163. (1+ (gap-ofs: gb)))
  164. (define (gb-point-min gb) 1) ; no narrowing (for now)
  165. (define (gb-point-max gb)
  166. (1+ (- (all-sz: gb) (- (aft-ofs: gb) (gap-ofs: gb)))))
  167. (define (insert-prep gb len)
  168. (let* ((gap-ofs (gap-ofs: gb))
  169. (aft-ofs (aft-ofs: gb))
  170. (slack (- (- aft-ofs gap-ofs) len)))
  171. (and (< slack default-realloc-threshold)
  172. (realloc gb (round-up (- slack))))
  173. gap-ofs))
  174. (define (gb-insert-string! gb string)
  175. (let* ((len (string-length string))
  176. (gap-ofs (insert-prep gb len)))
  177. (substring-move! string 0 len (s: gb) gap-ofs)
  178. (gap-ofs! gb (+ gap-ofs len))))
  179. (define (gb-insert-char! gb char)
  180. (let ((gap-ofs (insert-prep gb 1)))
  181. (string-set! (s: gb) gap-ofs char)
  182. (gap-ofs! gb (+ gap-ofs 1))))
  183. (define (gb-delete-char! gb count)
  184. (cond ((< count 0) ; backwards
  185. (gap-ofs! gb (max 0 (+ (gap-ofs: gb) count))))
  186. ((> count 0) ; forwards
  187. (aft-ofs! gb (min (all-sz: gb) (+ (aft-ofs: gb) count))))
  188. ((= count 0) ; do nothing
  189. #t)))
  190. (define (gb-erase! gb)
  191. (gap-ofs! gb 0)
  192. (aft-ofs! gb (all-sz: gb)))
  193. (define (point++n! gb n s gap-ofs aft-ofs) ; n>0; warning: reckless
  194. (substring-move! s aft-ofs (+ aft-ofs n) s gap-ofs)
  195. (gap-ofs! gb (+ gap-ofs n))
  196. (aft-ofs! gb (+ aft-ofs n)))
  197. (define (point+-n! gb n s gap-ofs aft-ofs) ; n<0; warning: reckless
  198. (substring-move! s (+ gap-ofs n) gap-ofs s (+ aft-ofs n))
  199. (gap-ofs! gb (+ gap-ofs n))
  200. (aft-ofs! gb (+ aft-ofs n)))
  201. (define (gb-goto-char gb new-point)
  202. (let ((pmax (gb-point-max gb)))
  203. (or (and (< new-point 1) (gb-goto-char gb 1))
  204. (and (> new-point pmax) (gb-goto-char gb pmax))
  205. (let ((delta (- new-point (gb-point gb))))
  206. (or (= delta 0)
  207. ((if (< delta 0)
  208. point+-n!
  209. point++n!)
  210. gb delta (s: gb) (gap-ofs: gb) (aft-ofs: gb))))))
  211. new-point)
  212. (define (gb->string gb)
  213. (let ((s (s: gb)))
  214. (string-append (substring s 0 (gap-ofs: gb))
  215. (substring s (aft-ofs: gb)))))
  216. (define (gb-filter! gb string-proc)
  217. (let ((new (string-proc (gb->string gb))))
  218. (gb-erase! gb)
  219. (gb-insert-string! gb new)))
  220. (define (gb->lines gb)
  221. (let ((str (gb->string gb)))
  222. (let loop ((start 0) (acc '()))
  223. (cond ((string-index str #\newline start)
  224. => (lambda (w)
  225. (loop (1+ w) (cons (substring str start w) acc))))
  226. (else (reverse (cons (substring str start) acc)))))))
  227. (define (gb-filter-lines! gb lines-proc)
  228. (let ((new-lines (lines-proc (gb->lines gb))))
  229. (gb-erase! gb)
  230. (gb-insert-string! gb (string-join new-lines #\newline))))
  231. (define (make-gap-buffer-port gb)
  232. (or (gb? gb)
  233. (error "not a gap-buffer:" gb))
  234. (make-soft-port
  235. (vector
  236. (lambda (c) (gb-insert-char! gb c))
  237. (lambda (s) (gb-insert-string! gb s))
  238. #f
  239. (lambda () (let ((gap-ofs (gap-ofs: gb))
  240. (aft-ofs (aft-ofs: gb)))
  241. (if (= aft-ofs (all-sz: gb))
  242. #f
  243. (let* ((s (s: gb))
  244. (c (string-ref s aft-ofs)))
  245. (string-set! s gap-ofs c)
  246. (gap-ofs! gb (1+ gap-ofs))
  247. (aft-ofs! gb (1+ aft-ofs))
  248. c))))
  249. #f)
  250. "rw"))
  251. ;;; gap-buffer.scm ends here