Table of Contents
- xtk_edit_history.h
- Types
- Functions
- xtk_edit_history_create
- xtk_edit_history_destroy
- xtk_edit_history_clear
- xtk_edit_history_record
- xtk_edit_history_begin_group
- xtk_edit_history_end_group
- xtk_edit_history_can_undo
- xtk_edit_history_can_redo
- xtk_edit_history_peek_undo
- xtk_edit_history_peek_redo
- xtk_edit_history_step_undo
- xtk_edit_history_step_redo
- xtk_edit_history_mark_saved
- xtk_edit_history_is_modified
- xtk_edit_history_mark_unsaved
xtk_edit_history.h
An undo and redo history for a text buffer, which also knows whether the text is saved.
An XtkEditHistory records the edits made to a text, so they can be undone and redone, and remembers where the text was last saved, so it can say whether the text is modified. It holds no text and draws nothing: the text widget keeps the text, records each edit before it makes it, and applies the edits the history hands back. XtkTextView uses it; a program can use it for its own text buffer too.
Each recorded edit is "at this byte offset, these bytes were replaced by those bytes", with the cursor before and after. Only the changed bytes are kept, so an edit in a large file costs little, and any byte value, NUL too, survives. The history is limited by the number of bytes it holds, not by a number of steps: when it holds more than its limit, the oldest edits are dropped.
Grouping. Edits that belong together are joined into one entry, so one undo takes them all back:
- Typed characters (
XTK_EDIT_TYPING) join into a word. A line break, a non-space typed after a space, a move of the cursor, a pause of one second or more, or a save starts a new entry. - Characters removed with Backspace (
XTK_EDIT_BACKSPACE) or with Delete (XTK_EDIT_DELETE) join into a run, with the same rules. XTK_EDIT_OTHER(a paste, a cut, a replaced selection) never joins.- Between
xtk_edit_history_begin_group()andxtk_edit_history_end_group(), every edit is its own entry, but all of them carry the same group number, and the caller undoes and redoes them together (see the example). Use it for "replace all" and other many-edit commands.
Undo and redo. To undo, take the edit from xtk_edit_history_peek_undo(), reverse it in the text (at offset, replace inserted_length bytes with removed), put the cursor back to before, and call xtk_edit_history_step_undo(). Redo is the mirror: take the edit from xtk_edit_history_peek_redo(), replace removed_length bytes at offset with inserted, put the cursor at after, and call xtk_edit_history_step_redo(). Recording a new edit drops everything that could be redone.
The saved state. Call xtk_edit_history_mark_saved() when the text is saved and xtk_edit_history_clear() when a file is loaded. xtk_edit_history_is_modified() is 0 exactly when the text is at the point of the last save, so undoing back to the saved text makes it unmodified again. When the saved point is dropped (a new edit after undoing past it, or the size limit), the text stays modified until the next save.
Example. Undo of one step, which may be a group: Here MyBuffer, my_buffer_replace() and my_buffer_set_cursor() are the program's own.
static void undo(MyBuffer *buffer, XtkEditHistory *history) {
const XtkEdit *edit = xtk_edit_history_peek_undo(history);
if (!edit) return;
unsigned group = edit->group;
XtkEditCursor cursor = edit->before;
do {
my_buffer_replace(buffer, edit->offset, edit->inserted_length,
edit->removed, edit->removed_length);
cursor = edit->before;
xtk_edit_history_step_undo(history);
edit = xtk_edit_history_peek_undo(history);
} while (group && edit && edit->group == group);
my_buffer_set_cursor(buffer, cursor);
}
// Typing "a" at offset 10, with no selection:
XtkEditCursor before = { 10, -1, -1 }, after = { 11, -1, -1 };
xtk_edit_history_record(history, 10, "", 0, "a", 1, XTK_EDIT_TYPING, before, after, now_ms);
my_buffer_replace(buffer, 10, 0, "a", 1);
*
Limitations.
- When memory runs out while recording, the whole history is lost and the text counts as modified.
- It is not thread-safe.
#include "xtk_edit_history.h"
Types: XtkEditKind, XtkEditCursor, XtkEdit, XtkEditHistory
Functions: xtk_edit_history_create, xtk_edit_history_destroy, xtk_edit_history_clear, xtk_edit_history_record, xtk_edit_history_begin_group, xtk_edit_history_end_group, xtk_edit_history_can_undo, xtk_edit_history_can_redo, xtk_edit_history_peek_undo, xtk_edit_history_peek_redo, xtk_edit_history_step_undo, xtk_edit_history_step_redo, xtk_edit_history_mark_saved, xtk_edit_history_is_modified, xtk_edit_history_mark_unsaved
Types
XtkEditKind
What kind of edit it is, which decides what it joins with.
| Name | Value | Description |
|---|---|---|
XTK_EDIT_OTHER |
Never joins: a paste, a cut, a replaced selection, an indent. | |
XTK_EDIT_TYPING |
One typed character (or a few bytes of one); joins into words. | |
XTK_EDIT_BACKSPACE |
One character removed before the cursor; joins into runs. | |
XTK_EDIT_DELETE |
One character removed after the cursor; joins into runs. |
XtkEditCursor
Where the cursor and the selection are, before or after an edit.
| Field | Type | Description |
|---|---|---|
cursor |
int |
The cursor's byte offset. |
selection_anchor |
int |
Where the selection began, as a byte offset; -1 for none. |
selection_active |
int |
Where the selection ends (the moving end); -1 for none. |
XtkEdit
One recorded edit: at offset, the bytes removed were replaced by inserted.
The history owns the bytes; the pointer from xtk_edit_history_peek_undo() or xtk_edit_history_peek_redo() is valid until the next call that changes the history.
| Field | Type | Description |
|---|---|---|
offset |
size_t |
The byte offset in the text where the edit happened. |
removed |
char * |
The bytes the edit took out, NUL-terminated (they may hold NULs too) |
removed_length |
size_t |
The number of bytes in removed. |
inserted |
char * |
The bytes the edit put in, NUL-terminated (they may hold NULs too) |
inserted_length |
size_t |
The number of bytes in inserted. |
before |
XtkEditCursor |
The cursor to put back after an undo. |
after |
XtkEditCursor |
The cursor to put back after a redo. |
kind |
XtkEditKind |
The kind of edit, as recorded. |
group |
unsigned |
0, or the group number shared by edits undone and redone together |
XtkEditHistory
typedef struct XtkEditHistory XtkEditHistory;
An edit history; opaque, made by xtk_edit_history_create().
Functions
xtk_edit_history_create
XtkEditHistory *xtk_edit_history_create(size_t max_bytes);
Make an empty history, whose text counts as saved.
| Parameter | Description |
|---|---|
max_bytes |
The most bytes of edits to hold (removed plus inserted). Above it the oldest edits are dropped, but never the newest, so one edit larger than the limit can still be undone. |
Returns: The history, or NULL when out of memory
xtk_edit_history_destroy
void xtk_edit_history_destroy(XtkEditHistory *history);
Free a history and all its edits.
| Parameter | Description |
|---|---|
history |
The history (NULL is ignored) |
xtk_edit_history_clear
void xtk_edit_history_clear(XtkEditHistory *history);
Forget every edit, and count the text as it is now as saved.
Call it when a file is loaded or the whole text is replaced. It also ends an open group.
| Parameter | Description |
|---|---|
history |
The history (NULL is ignored) |
xtk_edit_history_record
int xtk_edit_history_record(
XtkEditHistory *history,
size_t offset,
const char *removed,
size_t removed_length,
const char *inserted,
size_t inserted_length,
XtkEditKind kind,
XtkEditCursor before,
XtkEditCursor after,
long long now_ms);
Record an edit, just before the caller makes it in the text.
Everything that could be redone is dropped. The edit joins the previous entry when the grouping rules allow it (see "Grouping" above).
| Parameter | Description |
|---|---|
history |
The history; when NULL nothing is recorded and 0 is returned |
offset |
The byte offset where the edit happens |
removed |
The bytes the edit takes out (copied) |
removed_length |
The number of bytes in removed; 0 for none |
inserted |
The bytes the edit puts in (copied) |
inserted_length |
The number of bytes in inserted; 0 for none |
kind |
The kind of edit, which decides what it joins with |
before |
The cursor and selection before the edit |
after |
The cursor and selection after the edit |
now_ms |
The time now in milliseconds, from any steady clock; a gap of 1000 ms or more between edits keeps them apart |
Returns: 0 when recorded, or -1 when memory ran out: the whole history was lost and the text now counts as modified. The caller makes the edit either way.
xtk_edit_history_begin_group
void xtk_edit_history_begin_group(XtkEditHistory *history);
Start a group: the edits recorded until xtk_edit_history_end_group() share one group number.
Groups nest: a begin inside an open group is part of that group, and the edits stay one step until the outermost group's end. Every begin needs its end.
| Parameter | Description |
|---|---|
history |
The history (NULL is ignored) |
xtk_edit_history_end_group
void xtk_edit_history_end_group(XtkEditHistory *history);
End the group started by the matching xtk_edit_history_begin_group().
Only the end of the outermost group closes it; an end with no group open does nothing.
| Parameter | Description |
|---|---|
history |
The history (NULL is ignored) |
xtk_edit_history_can_undo
int xtk_edit_history_can_undo(const XtkEditHistory *history);
Whether there is an edit to undo.
| Parameter | Description |
|---|---|
history |
The history |
Returns: 1 if there is one, 0 if not or if history is NULL
xtk_edit_history_can_redo
int xtk_edit_history_can_redo(const XtkEditHistory *history);
Whether there is an edit to redo.
| Parameter | Description |
|---|---|
history |
The history |
Returns: 1 if there is one, 0 if not or if history is NULL
xtk_edit_history_peek_undo
const XtkEdit *xtk_edit_history_peek_undo(const XtkEditHistory *history);
The edit an undo reverses next, without moving through the history.
| Parameter | Description |
|---|---|
history |
The history |
Returns: The edit (owned by the history), or NULL when there is none
xtk_edit_history_peek_redo
const XtkEdit *xtk_edit_history_peek_redo(const XtkEditHistory *history);
The edit a redo repeats next, without moving through the history.
| Parameter | Description |
|---|---|
history |
The history |
Returns: The edit (owned by the history), or NULL when there is none
xtk_edit_history_step_undo
void xtk_edit_history_step_undo(XtkEditHistory *history);
Step back past the edit from xtk_edit_history_peek_undo(), once the caller has reversed it.
| Parameter | Description |
|---|---|
history |
The history (nothing happens when there is nothing to undo) |
xtk_edit_history_step_redo
void xtk_edit_history_step_redo(XtkEditHistory *history);
Step forward past the edit from xtk_edit_history_peek_redo(), once the caller has repeated it.
| Parameter | Description |
|---|---|
history |
The history (nothing happens when there is nothing to redo) |
xtk_edit_history_mark_saved
void xtk_edit_history_mark_saved(XtkEditHistory *history);
Count the text as it is now as saved.
Call it after the text is written to its file. The next typed character starts a new entry, so undo can stop at the saved text.
| Parameter | Description |
|---|---|
history |
The history (NULL is ignored) |
xtk_edit_history_is_modified
int xtk_edit_history_is_modified(const XtkEditHistory *history);
Whether the text differs from what was last saved.
| Parameter | Description |
|---|---|
history |
The history |
Returns: 1 if it is modified, 0 if it is at the saved point or history is NULL
xtk_edit_history_mark_unsaved
void xtk_edit_history_mark_unsaved(XtkEditHistory *history);
Count the text as modified until the next save, whatever is undone.
Call it when the saved file no longer matches the text, for example when the file was changed or deleted on disk.
| Parameter | Description |
|---|---|
history |
The history (NULL is ignored) |
xtk API
Widgets
- button
- checkbox
- radiobutton
- label
- entry
- textview
- edit_history
- spinbutton
- slider
- progressbar
- combobox
- scrollbar
- swatch
- panel
- statusbar
- tooltip
- toolbar
- icongrid
- iconview
- pathbar
- treeview
- datagrid
- graph
- scheme_wheel
- zoom
- tray
Containers and menus
Dialogs
Rich text
Drawing and images
Color
Configuration and themes
Input and the X server
Safety
Data formats