Next: , Previous: Gap Buffer, Up: Top


53 Emacsoid Buffer Abstraction

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.

— Scheme Macro: editing-buffer buffer [body...]

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)          ; likewise

These calls are all translated by editing-buffer to 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-buffer and 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.

53.1 convenience procedures

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.

— Scheme Procedure: find-file-read-only filename

Find filename into a buffer and make it read-only. This causes the buffer to visit the file. Move point to beginning of buffer and return the buffer.

— Scheme Procedure: find-file filename

Find filename into a buffer. This causes the buffer to visit the file. If the file is not readable, return #f. If the file is read-only, make the buffer read-only as well. Move point to beginning of buffer and return the buffer.