For Gauche 0.9.10


Next: , Previous: , Up: ライブラリモジュール - ユーティリティ   [Contents][Index]

12.59 text.gap-buffer - ギャップバッファ

Module: text.gap-buffer

This module provides a gap buffer, a data structure useful for editable text.

A gap buffer is a vector of characters with a “cursor”, where you can insert or delete a character in O(1) time. Most of the editing API operate on the current cursor position.

Function: make-gap-buffer :key initial-capacity

{text.gap-buffer} Creates a fresh gap buffer and returns it. The initial content is emtpy.

The keyword argument initial-capacity must be a positive exact integer if given, and specifies the initial capacity of the buffer. The buffer is automatically extended if necessary, so the argument is only a hint; if you know you’ll insert a long string, for example, you can create a buffer that’s large enough to hold it to avoid reallocation overhead.

Function: string->gap-buffer string :optional pos whence start end

{text.gap-buffer} Creates a gap buffer large enough to contain the given string, and returns it.

The initial cursor position is set at the end of the string by default. The pos and whence argument can set the initial cursor position. The whence argument must be either a symbol beginning or a symbol end, and pos must be an exact integer offset from the whence. For example, pos = 10 and whence = beginning sets the cursor at the 10th character of the string from the beginning, and pos = -1 and whence = end sets the cursor position at one character before the end of the string. If the combination of pos and whence points the outside of the string, an error is signaled.

The optional start and end trims the input string before creating the gap buffer.

Function: gap-buffer? obj

{text.gap-buffer} Returns #t iff obj is a gap buffer.

Function: gap-buffer-copy gbuf

{text.gap-buffer} Returns a fresh copy of a gap buffer gbuf, with the same content and cursor position.

Function: gap-buffer->string gbuf :optional start end
Function: gap-buffer->generator gbuf :optional start end

{text.gap-buffer} Retrieve the content of a gap buffer gbuf as a string or a generator of characters, respectively. The optional start and end arguments are nonnegative exact integers of character index in the content of the buffer, and limits the output within the specified range.

If you modify the content of gap buffer before the returned generator is exhausted, consistent behavior isn’t guaranteed.

Function: gap-buffer-pos gbuf

{text.gap-buffer} Returns the current cursor position of gbuf, in a nonnegative exact integer.

Function: gap-buffer-capacity gbuf

{text.gap-buffer} Returns the currently allocated storage size of gbuf. If you insert more characters into gbuf, the storage is automatically expanded.

Function: gap-buffer-content-length gbuf

{text.gap-buffer} Returns the length of current content of gbuf, in the number of characters.

Function: gap-buffer-gap-at? gbuf whence

{text.gap-buffer} The whence argument must be either a symbol beginning or a symbol end. Returns #t iff the current cursor position of gbuf is at the beginning or at the end, respectively.

Function: gap-buffer-ref gbuf index :optional fallback

{text.gap-buffer} Returns index-th character of gbuf. If index is out of range, fallback is returned if given, or an error is raised.

Function: gap-buffer-set! gbuf index char

{text.gap-buffer} Replaces index-th character of gbuf with char. If index is out of range, an error is signaled.

Function: gap-buffer-move! gbuf pos :optional whence

{text.gap-buffer} Moves the cursor position of gbuf to a position pos, which must be an exact integer. The position pos is relative to whence, which must be either one of the symbols beginning, current or end.

Function: gap-buffer-insert! gbuf content

{text.gap-buffer} Inserts content, which must be a character or a string, at the current cursor position of gbuf. The current cursor position is moved to the end of the inserted content.

Function: gap-buffer-delete! gbuf size

{text.gap-buffer} Delets size characters from the current cursor position of gbuf. It is an error if size overruns.

Function: gap-buffer-clear! gbuf

{text.gap-buffer} Makes gbuf empty.

Function: gap-buffer-replace! gbuf size content

{text.gap-buffer} This is a combination of (gap-buffer-delete! gbuf size) and (gap-buffer-insert! gbuf content). The size argument must be a nonnegative exact integer, and the content argument must be either a string or a character.

Function: gap-buffer-edit! gbuf edit-command

{text.gap-buffer} This is a convenience procedure to “replay” editing of the gap buffer gbuf. The edit-command argument must be either one of the following:

(i pos string)

Insert string at a position pos.

(d pos length)

Delete length characters from a position pos.

(c pos length string)

Change: Delets length characters from a position pos, then insert string.

Here, pos must be a nonnegative exact integer specifying the position in the gap buffer.

One of the appliations of this procedure is to handle undo/redo list.


Next: , Previous: , Up: ライブラリモジュール - ユーティリティ   [Contents][Index]