Next: Efficient Accumulation, Previous: Gap Buffer, Up: Top
To load support for the Emacsoid buffer abstraction:
(use-modules (ice-9 editing-buffer))
This module provides a convenient abstraction over the gap-buffer module (see Gap Buffer), particularly suited for operation focused on one buffer, known as the current buffer.
Consider buffer the current buffer and execute body. In body, applications of one of the following procs are handled specially.
(toggle-read-only [arg]) (search-forward string [bound [noerror [repeat]]]) (match-end [n]) (match-data) (set-match-data m) (buffer-substring beg end) (search-backward string [bound [noerror [repeat]]]) (match-string n) (forward-char n) (backward-char n) (forward-line [n]) (beginning-of-line [n]) (end-of-line [n]) (re-search-forward regexp [bound [noerror [repeat]]]) (match-beginning [n]) (match-end [n]) (delete-region beg end) (buffer-port) (replace-match newtext) (insert obj) ; gb/char/string/symbol/number/sexp (looking-at regexp) (delete-char n) (erase-buffer) (goto-char pos) (buffer-string) (point-min) (point-max) (char-after [pos]) (char-before [pos]) (point) (bolp) ; beginning/end of line/buffer (eolp) (bobp) (eobp) (write-to-port port [beg [end]]) (flush-lines regexp) ; leaves point at eob (keep-lines regexp) ; likewiseThese calls are all translated by
editing-bufferto procedures that take gap-buffer buffer as initial argument (so that the "first" arg above would actually be passed as the second arg, and so on).If buffer is #t, create and use a new (empty) gap-buffer. If buffer is not a gap-buffer object and not #t, it is passed to
make-gap-bufferand the result used for editing.The value is that of the last form in body, or the gap-buffer object if there are no body forms.
Here is a sample procedure that double-spaces a file:
(define (double-space filename)
(let ((port (open-io-file filename)))
(editing-buffer port
(goto-char (point-min))
(while (not (eobp))
(forward-line 1)
(insert "\n"))
(seek port 0 SEEK_SET)
(write-to-port port))
(close-port port)))
This example mixes a while loop (more idiomatic for Emacs Lisp than
for Scheme, but we don't mind), with some low-level input/output operations.
The following two procedures visit a file by setting the
filename: object property for a buffer to its filename (string).
(filename: (find-file "foo"))
⇒ "foo"
See Object Properties.