Next: Localized messages, Previous: Running external editor, Up: Library modules - Utilities [Contents][Index]
text.gap-buffer
- Gap bufferThis 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.
{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.
{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.
{text.gap-buffer}
Returns #t
iff obj is a gap buffer.
{text.gap-buffer} Returns a fresh copy of a gap buffer gbuf, with the same content and cursor position.
{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.
{text.gap-buffer} Returns the current cursor position of gbuf, in a nonnegative exact integer.
{text.gap-buffer} Returns the currently allocated storage size of gbuf. If you insert more characters into gbuf, the storage is automatically expanded.
{text.gap-buffer} Returns the length of current content of gbuf, in the number of characters.
{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.
{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.
{text.gap-buffer} Replaces index-th character of gbuf with char. If index is out of range, an error is signaled.
{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
.
{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.
{text.gap-buffer} Delets size characters from the current cursor position of gbuf. It is an error if size overruns.
{text.gap-buffer} Makes gbuf empty.
{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.
{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: Localized messages, Previous: Running external editor, Up: Library modules - Utilities [Contents][Index]