1 xtk_richtext
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_richtext.h

A rich text editor that reads and writes Markdown, and can also show its Markdown source.

An XtkRichText edits formatted text: bold, italic and the other inline styles, colors, headings, lists and task lists, block quotes, admonitions, horizontal rules, links and wiki links, images, footnotes, fenced code blocks with simple syntax colors, tables whose cells are edited in place, and LaTeX math (laid out with xtk_mathtext.h). Lines wrap at word boundaries; code blocks and wide tables scroll sideways.

The document is one plain text plus a list of spans (XtkRichTextSpan) that format runs of it. Markdown is how it is loaded and saved: xtk_richtext_set_markdown() parses Markdown (with xtk_markdown.h) into text and spans, and xtk_richtext_get_markdown() writes them back. The same document has two views (XtkRichTextMode): RICHTEXT shows it formatted, and MARKDOWN shows its Markdown source as plain text to edit; xtk_richtext_set_mode() converts between them.

Making an editor. xtk_richtext_create() takes an XtkTextViewTheme for its colors, normally &config.textview. The editor keeps a copy (NULL gives the dark defaults of xtk_textview_theme_init_dark()); after the colors change, call xtk_richtext_set_theme() to copy them again. The editor is mapped at once. It draws in "DejaVu Sans" at 12 pixels (see xtk_richtext_set_font_family() and xtk_richtext_set_font_size()), code in "monospace", and has a right-click menu (Cut, Copy, Paste, Select All, Undo, Redo). It owns its window, fonts, text and history; xtk_richtext_destroy() frees them. The clipboard is the caller's.

Events. Pass every X event to xtk_richtext_handle_event(), and the clipboard's events to xtk_clipboard_handle_event(). Call xtk_richtext_update_blink() often (for example every 50 ms). The editor takes X focus when clicked; a program whose other widgets do not take X focus calls xtk_richtext_set_focus() with 0 when the user clicks them.

Keys and mouse. The arrows move by character and by wrapped row, Home and End go to the ends of the row, Page Up and Page Down move and scroll by a screen, and Shift with them selects. Backspace, Delete and Return edit, and Tab types a tab (Shift+Tab and Ctrl+Tab are left to the program). Ctrl+A selects all, Ctrl+C copies, Ctrl+X cuts, Ctrl+V pastes, Ctrl+Z undoes, Ctrl+Y or Ctrl+Shift+Z redoes, and Ctrl+B, Ctrl+I and Ctrl+U toggle bold, italic and underline on the selection (in MARKDOWN mode they put the Markdown marks around it, or take them off). A double click selects a word and a triple click a line. A click on a link calls the link callback; a click on a footnote reference scrolls to its definition, and the arrows after a definition go back to its references. The wheel scrolls; with Shift, or over the bottom scrollbar, it scrolls sideways. In a table cell, Tab and Shift+Tab move between cells, Return moves down, and the arrows leave the table at its edges.

Clipboard. Copy in RICHTEXT mode offers the selection as Markdown, both as plain text and in a format of the widget's own, so a paste into an XtkRichText keeps the formatting. In MARKDOWN mode it is plain text. A selection is also offered as the PRIMARY selection.

Undo. Each edit stores a copy of the whole document; up to 200 are kept, and each typed character is one step. xtk_richtext_is_undo_clean() tells whether the document is back at the point marked with xtk_richtext_mark_undo_clean().

Theme keys. The editor uses the [textview] section, through the XtkTextViewTheme it is given (see xtk_textview.h for the defaults):

  • background: the background (it can be a gradient), and the matte behind images.
  • text: the text. selection and selection-text: the selection. cursor: the caret.
  • border and focus-border: the frame, also horizontal rules, table lines and the rule under level 1 and 2 headings.
  • link: links and footnote back arrows.
  • highlight: the background of ==highlighted== text (it can be a gradient).
  • line-numbers-background: the background of code, code blocks, table header rows, the scrollbar tracks, and the box shown for an image that cannot be decoded. (Admonitions use fixed colors of their own.)
  • key: inline code text and code keywords; value: code strings; number: code numbers; comment: code comments, the quote bar, HTML comments and footnote numbers.
  • scrollbar-thumb: the scrollbar thumbs.

The right-click menu is drawn in "sans" at 12 pixels, in text, background, border, focus-border and selection. The caret blinks at cursor-blink-ms in [behavior] (default 600).

Example.

static void open_link(XtkRichText *editor, const char *target, void *user_data) {
    (void)editor; (void)user_data;
    printf("open %s\n", target);
}

XtkConfig config;
xtk_config_init_defaults(&config);
XtkTheme *theme = xtk_theme_new();
if (theme && xtk_theme_builtin("Dark", theme)) xtk_theme_apply(&config, theme);
xtk_theme_free(theme);
XtkClipboard *clipboard = xtk_clipboard_create(display);

XtkRichText *editor = xtk_richtext_create(display, screen, window, 0, 0, 640, 480,
                                          &config.textview, clipboard);
xtk_richtext_set_link_click_callback(editor, open_link, NULL);
xtk_richtext_set_markdown(editor, "# Notes\n\nSome **bold** text and a [link](https://example.org/).\n");
xtk_richtext_mark_undo_clean(editor);
xtk_richtext_draw(editor);

for (;;) {
    XEvent event;
    while (XPending(display)) {
        XNextEvent(display, &event);
        if (xtk_clipboard_handle_event(clipboard, &event)) continue;
        xtk_richtext_handle_event(editor, &event);
    }
    xtk_richtext_update_blink(editor);
    usleep(50000);
}

char *markdown = xtk_richtext_get_markdown(editor);   // save it
free(markdown);
xtk_richtext_destroy(editor);
 * 

Limitations.

  • It edits a subset of Markdown, not all of CommonMark: what it has no span for (spoilers, definition lists, nested structure beyond its indent levels) is not kept exactly when a document is read and written.
  • A middle click does not paste the PRIMARY selection.
  • In MARKDOWN mode only the inline marks (Ctrl+B and the other xtk_richtext_toggle_inline_span() kinds) and links (xtk_richtext_set_link()) can be applied, as Markdown text; the block formats, colors and the insert functions do nothing there.
  • The editor is not thread-safe.
#include "xtk_richtext.h"

Types: XtkRichTextMode, XtkRichTextFlavor, XtkRichTextSpanType, XtkRichTextAlign, XtkRichTextListKind, XtkRichTextSpan, XtkRichTextTable, XtkRichText, XtkRichTextImageDecoder, XtkRichTextChangeCallback, XtkRichTextLinkClickCallback

Constants: XTK_RICHTEXT_TABLE_MAX_COLUMNS, XTK_RICHTEXT_TABLE_MAX_ROWS, XTK_RICHTEXT_LINK_TARGET_MAX, XTK_RICHTEXT_BORDER_TOP, XTK_RICHTEXT_BORDER_RIGHT, XTK_RICHTEXT_BORDER_BOTTOM, XTK_RICHTEXT_BORDER_LEFT, XTK_RICHTEXT_BORDER_ALL

Functions: xtk_richtext_create, xtk_richtext_destroy, xtk_richtext_set_theme, xtk_richtext_get_window, xtk_richtext_resize, xtk_richtext_set_position, xtk_richtext_handle_event, xtk_richtext_draw, xtk_richtext_update_blink, xtk_richtext_set_focus, xtk_richtext_get_focus, xtk_richtext_set_ime, xtk_richtext_set_border_visible, xtk_richtext_set_border_sides, xtk_richtext_set_readonly, xtk_richtext_set_wrap_code, xtk_richtext_set_base_directory, xtk_richtext_set_image_decoder, xtk_richtext_get_mode, xtk_richtext_get_detected_flavor, xtk_richtext_set_mode, xtk_richtext_set_content, xtk_richtext_get_text, xtk_richtext_get_span_count, xtk_richtext_get_spans, xtk_richtext_get_markdown, xtk_richtext_get_markdown_map, xtk_richtext_get_cursor, xtk_richtext_set_cursor, xtk_richtext_get_top_offset, xtk_richtext_set_top_offset, xtk_richtext_set_markdown, xtk_richtext_toggle_inline_span, xtk_richtext_set_inline_color, xtk_richtext_add_inline_color, xtk_richtext_set_link, xtk_richtext_set_block_type, xtk_richtext_insert_horizontal_rule, xtk_richtext_insert_image, xtk_richtext_insert_table, xtk_richtext_insert_footnote, xtk_richtext_insert_math_block, xtk_richtext_adjust_indent, xtk_richtext_get_active_spans, xtk_richtext_get_active_heading_level, xtk_richtext_get_active_admonition_kind, xtk_richtext_set_font_size, xtk_richtext_set_font_family, xtk_richtext_set_math_font, xtk_richtext_can_undo, xtk_richtext_can_redo, xtk_richtext_mark_undo_clean, xtk_richtext_clear_undo_clean, xtk_richtext_is_undo_clean, xtk_richtext_find, xtk_richtext_replace_selection, xtk_richtext_replace_all, xtk_richtext_undo, xtk_richtext_redo, xtk_richtext_set_change_callback, xtk_richtext_set_link_click_callback

Types

XtkRichTextMode

What the editor shows: the formatted document, or its Markdown source.

Name Value Description
XTK_RICHTEXT_MODE_RICHTEXT 0 The formatted document.
XTK_RICHTEXT_MODE_MARKDOWN The Markdown source, as plain editable text.

XtkRichTextFlavor

The Markdown dialect found in the loaded document (see xtk_richtext_get_detected_flavor()).

The dialects are not supersets of each other, and a document can mix them; the order is only the priority for choosing one name.

Name Value Description
XTK_RICHTEXT_FLAVOR_COMMONMARK 0 Nothing beyond CommonMark was found.
XTK_RICHTEXT_FLAVOR_GFM GitHub syntax was found: a table, strikethrough, or a task list.
XTK_RICHTEXT_FLAVOR_EXTENDED One of the editor's further extensions was found: a [[wiki link]] or ==highlight==
XTK_RICHTEXT_FLAVOR_KRAMDOWN Kramdown or PHP Markdown Extra syntax was found: a definition list, a footnote definition, or an attribute list {: ...}
XTK_RICHTEXT_FLAVOR_PANDOC Pandoc or MultiMarkdown syntax was found: a citation [@key], YAML front matter, or $ math.
XTK_RICHTEXT_FLAVOR_OBSIDIAN Obsidian syntax was found: an embed ![[...]], a callout > [!...], or a block reference ^id

XtkRichTextSpanType

What a span does.

An inline span formats a run of characters. A block span formats one whole line: its start is the line's first byte and its length the line's length, and a line has at most one block span. Block constructs of several lines (a code block, a quote, an admonition, an HTML block) have one span per line. Display math is always one line.

Name Value Description
XTK_RICHTEXT_SPAN_BOLD 0 Inline: bold.
XTK_RICHTEXT_SPAN_ITALIC Inline: italic.
XTK_RICHTEXT_SPAN_UNDERLINE Inline: underlined.
XTK_RICHTEXT_SPAN_STRIKETHROUGH Inline: struck through.
XTK_RICHTEXT_SPAN_SUPERSCRIPT Inline: superscript.
XTK_RICHTEXT_SPAN_SUBSCRIPT Inline: subscript.
XTK_RICHTEXT_SPAN_CODE Inline: code.
XTK_RICHTEXT_SPAN_FOREGROUND_COLOR Inline: text color, in color_r, color_g, color_b.
XTK_RICHTEXT_SPAN_BACKGROUND_COLOR Inline: background color, in color_r, color_g, color_b.
XTK_RICHTEXT_SPAN_HEADING Block: a heading; level is 1 to 6
XTK_RICHTEXT_SPAN_LIST_ITEM Block: a list item; level is an XtkRichTextListKind, indent its depth
XTK_RICHTEXT_SPAN_QUOTE Block: a line of a block quote
XTK_RICHTEXT_SPAN_HORIZONTAL_RULE Block: a horizontal rule, on an empty line
XTK_RICHTEXT_SPAN_IMAGE Block: an image; the line's text is the image's path
XTK_RICHTEXT_SPAN_LINK Inline: a link; link_target is where it points
XTK_RICHTEXT_SPAN_CODE_BLOCK Block: a line of a fenced code block; level is the highlighting language, link_target the fence's language as written
XTK_RICHTEXT_SPAN_TABLE Block: a table, on one empty placeholder line; its cells are in an XtkRichTextTable
XTK_RICHTEXT_SPAN_HIGHLIGHT Inline: highlighted text (==text==), drawn on the highlight color
XTK_RICHTEXT_SPAN_ADMONITION Block: a line of an admonition; level is its kind, 0 to 4: note, tip, important, warning, caution
XTK_RICHTEXT_SPAN_FOOTNOTE_REFERENCE Inline: a footnote reference, whose text is "[N]"; level is the footnote's number, link_target its label
XTK_RICHTEXT_SPAN_FOOTNOTE_DEFINITION Block: a line of a footnote definition; level is the footnote's number, link_target its label
XTK_RICHTEXT_SPAN_MATH_INLINE Inline: LaTeX math ($...$); the covered text is the LaTeX source
XTK_RICHTEXT_SPAN_MATH_BLOCK Block: display math ($$...$$), always one line (line breaks in its source become spaces)
XTK_RICHTEXT_SPAN_ENTITY Inline: an HTML entity (©); the covered text is its character, link_target the entity as written, which is written back while the character is unchanged
XTK_RICHTEXT_SPAN_COMMENT Inline: the text of an HTML comment, drawn dimmed and italic. level & 1: this piece opens the comment; level & 2: it closes it. A comment over several lines has one span per line.
XTK_RICHTEXT_SPAN_HTML_BLOCK Block: a line of a raw HTML block; the lines of one block are written back together

XtkRichTextAlign

A table column's alignment.

Name Value Description
XTK_RICHTEXT_ALIGN_DEFAULT 0 Not given.
XTK_RICHTEXT_ALIGN_LEFT Left.
XTK_RICHTEXT_ALIGN_CENTER Centered.
XTK_RICHTEXT_ALIGN_RIGHT Right.

XtkRichTextListKind

A list item's kind, kept in a LIST_ITEM span's level.

Name Value Description
XTK_RICHTEXT_LIST_BULLET 0 A bulleted item.
XTK_RICHTEXT_LIST_ORDERED A numbered item.
XTK_RICHTEXT_LIST_TODO_OPEN An unchecked task.
XTK_RICHTEXT_LIST_TODO_DONE A checked task.

XtkRichTextSpan

Formatting over a run of the text. It holds no pointers, so it can be copied as it is.

Field Type Description
start size_t The byte offset of the first byte covered.
length size_t The number of bytes covered.
type XtkRichTextSpanType What the span does.
level int HEADING: 1 to 6. LIST_ITEM: an XtkRichTextListKind. ADMONITION: its kind, 0 to 4. FOOTNOTE_REFERENCE and FOOTNOTE_DEFINITION: the footnote's number. CODE_BLOCK: the highlighting language. Unused otherwise.
indent int Block spans only: the nesting depth, 0 for none
color_r unsigned char FOREGROUND_COLOR and BACKGROUND_COLOR only: red, 0 to 255
color_g unsigned char Likewise, green
color_b unsigned char Likewise, blue
link_target[XTK_RICHTEXT_LINK_TARGET_MAX] char LINK: where it points. Also used by CODE_BLOCK, footnotes and ENTITY (see XtkRichTextSpanType). Empty otherwise.

XtkRichTextTable

The cells of one table (see XTK_RICHTEXT_SPAN_TABLE).

A table's text is not in the document's text: its line is an empty placeholder, and the editor keeps the cells here. In RICHTEXT mode the user edits the cells in place, their formatting too (bold, italic, code, links ...); rows and columns are added or removed in MARKDOWN mode, in the pipe-table source.

Field Type Description
key size_t The start of the TABLE span the table belongs to
column_count int The number of columns.
row_count int The number of rows; row 0 is always the header row.
cells char ** Each cell's text as it is shown and edited (its characters, without Markdown's marks; of any length): row_count times column_count strings, row after row, each allocated, none NULL.
spans XtkRichTextSpan ** Each cell's inline formatting, in the same order as cells: an allocated array of inline spans (bold, italic, underline, strikethrough, superscript, subscript, code, highlight, a link, math) whose start and length are byte offsets in the cell's text, or NULL for a cell with none.
span_counts int * How many spans each cell has, in the same order as cells.
sources char ** Each cell as its Markdown source has it (**bold**, a link, code), in the same order as cells. A cell that was not edited is written back from this, exactly as it was. NULL for a cell the user edited, or one made by xtk_richtext_insert_table(): its text and spans are written.
aligns XtkRichTextAlign * Each column's alignment: column_count of them.
column_x int * Private to the editor: where the columns are, measured for one content, width and font.
column_version unsigned long Private: the content column_x was measured for.
column_width int Private: the widget width column_x was measured for.
column_font int Private: the font size column_x was measured for.

XtkRichText

typedef struct XtkRichText XtkRichText;

A rich text editor; opaque, made by xtk_richtext_create().

XtkRichTextImageDecoder

typedef int (*XtkRichTextImageDecoder)(const char *path, int max_width, int max_height, unsigned char **rgba, int *width, int *height, void *user_data);

A function of the program's that decodes an image file for the editor.

A file can be hostile, so a program can decode it its own way (in a process of its own, for example). The image must fit within the size asked for (xtk_image_load_rgba8_scaled() makes such pixels); the editor checks that before it draws it.

XtkRichTextChangeCallback

typedef void (*XtkRichTextChangeCallback)(XtkRichText *richtext, void *user_data);

The function the editor calls after each edit.

Edits are the user's and the program's (formatting, inserts, undo, redo), but not a mode switch, and not loading with xtk_richtext_set_markdown() or xtk_richtext_set_content(). It is called in the middle of an edit: it may read the document, but must not edit or destroy the editor.

XtkRichTextLinkClickCallback

typedef void (*XtkRichTextLinkClickCallback)(XtkRichText *richtext, const char *target, void *user_data);

The function the editor calls when a link is clicked.

A plain left click on a link's text calls it; no modifier key is needed, so a click on a link never places the caret there (the keyboard, or MARKDOWN mode, reaches its text). The editor does not open anything itself: the program decides what the target is (a file, a page, a document of its own) and opens it. Wiki links are links too.

Constants

XTK_RICHTEXT_TABLE_MAX_COLUMNS

#define XTK_RICHTEXT_TABLE_MAX_COLUMNS 64

The most columns xtk_richtext_insert_table() makes. A table read from Markdown has as many as its source.

XTK_RICHTEXT_TABLE_MAX_ROWS

#define XTK_RICHTEXT_TABLE_MAX_ROWS 1024

The most rows xtk_richtext_insert_table() makes, the header row included. A table read from Markdown has as many as its source.

#define XTK_RICHTEXT_LINK_TARGET_MAX 256

The size of a span's link_target buffer, the NUL included; a longer target is cut to fit.

XTK_RICHTEXT_BORDER_TOP

#define XTK_RICHTEXT_BORDER_TOP (1 << 0)

The top edge.

XTK_RICHTEXT_BORDER_RIGHT

#define XTK_RICHTEXT_BORDER_RIGHT (1 << 1)

The right edge.

XTK_RICHTEXT_BORDER_BOTTOM

#define XTK_RICHTEXT_BORDER_BOTTOM (1 << 2)

The bottom edge.

XTK_RICHTEXT_BORDER_LEFT

#define XTK_RICHTEXT_BORDER_LEFT (1 << 3)

The left edge.

XTK_RICHTEXT_BORDER_ALL

#define XTK_RICHTEXT_BORDER_ALL 								  (XTK_RICHTEXT_BORDER_TOP | XTK_RICHTEXT_BORDER_RIGHT | XTK_RICHTEXT_BORDER_BOTTOM | XTK_RICHTEXT_BORDER_LEFT)

All four edges.

Functions

xtk_richtext_create

XtkRichText *xtk_richtext_create(
    Display *display,
    int screen,
    Window parent,
    int x,
    int y,
    int width,
    int height,
    const XtkTextViewTheme *theme,
    XtkClipboard *clipboard);

Make a rich text editor, mapped, as a child of a window.

The editor is empty, in RICHTEXT mode.

Parameter Description
display The X display
screen The screen number
parent The window the editor is made in
x The x position in the parent
y The y position in the parent
width The width in pixels (less than 1 counts as 1)
height The height in pixels (less than 1 counts as 1)
theme The colors (normally &config.textview), copied. With NULL the editor uses the dark defaults (xtk_textview_theme_init_dark()).
clipboard The clipboard for copy and paste, owned by the caller

Returns: The editor, or NULL if display is NULL, memory runs out or the window cannot be made

xtk_richtext_destroy

void xtk_richtext_destroy(XtkRichText *richtext);

Destroy an editor: its window, fonts, document and history.

Parameter Description
richtext The editor (NULL is ignored)

xtk_richtext_set_theme

void xtk_richtext_set_theme(XtkRichText *richtext, const XtkTextViewTheme *theme);

Give the editor its colors again, and redraw.

Call it after the theme's colors changed; the right-click menu takes them too. The colors are copied, as in xtk_richtext_create().

Parameter Description
richtext The editor
theme The colors; NULL is ignored

xtk_richtext_get_window

Window xtk_richtext_get_window(const XtkRichText *richtext);

The editor's window.

Parameter Description
richtext The editor

Returns: The window, or None if richtext is NULL

xtk_richtext_resize

void xtk_richtext_resize(XtkRichText *richtext, int width, int height);

Resize the editor.

The text wraps again, and the line at the top stays at the top. The editor is not redrawn; it gets an Expose, or call xtk_richtext_draw().

Parameter Description
richtext The editor
width The new width in pixels
height The new height in pixels; when either is 0 or less, the call does nothing

xtk_richtext_set_position

void xtk_richtext_set_position(XtkRichText *richtext, int x, int y);

Move the editor in its parent.

Parameter Description
richtext The editor
x The new x position
y The new y position

xtk_richtext_handle_event

int xtk_richtext_handle_event(XtkRichText *richtext, XEvent *event);

Give the editor an X event.

Pass it every event; see "Keys and mouse" in the file description.

Parameter Description
richtext The editor
event The event

Returns: 1 if the editor used the event (an Expose of its window, a button press or release on it, motion it acted on, a key press or focus event on its window, or, while its menu is open, any key, button or motion event and any event of the menu's window), 0 otherwise

xtk_richtext_draw

void xtk_richtext_draw(XtkRichText *richtext);

Draw the editor now.

Call it after the program changes the document, the mode, the fonts or the cursor; those calls do not draw by themselves. Drawing also offers the selection as the PRIMARY selection.

Parameter Description
richtext The editor (nothing is drawn if it is NULL)
void xtk_richtext_update_blink(XtkRichText *richtext);

Blink the caret.

Call it often from the event loop (for example every 50 ms). While the editor has focus, the caret turns on or off every cursor_blink_ms of its theme ([behavior] cursor-blink-ms, default 600 ms) and the editor is redrawn.

Parameter Description
richtext The editor (NULL is ignored)

xtk_richtext_set_focus

void xtk_richtext_set_focus(XtkRichText *richtext, int focused);

Give the editor focus, or take it away, without an X focus change.

The editor follows X FocusIn and FocusOut, but a click on a widget that does not take X focus sends none. A program calls this with 0 when the user clicks such a widget, so the caret and the focus border go away. It redraws only when the state changes.

Parameter Description
richtext The editor
focused 1 to give it focus, 0 to take focus away

xtk_richtext_get_focus

int xtk_richtext_get_focus(const XtkRichText *richtext);

Whether the editor has focus.

Parameter Description
richtext The editor

Returns: 1 if it has focus, 0 if not or if richtext is NULL

xtk_richtext_set_ime

void xtk_richtext_set_ime(XtkRichText *richtext, XIM input_method);

Attach or detach an input method, for composed and CJK input.

See xtk_ime.h for the setup the program needs (setlocale() and XFilterEvent() in the event loop).

Parameter Description
richtext The editor
input_method An XIM from xtk_ime_open(), or NULL to detach

xtk_richtext_set_border_visible

void xtk_richtext_set_border_visible(XtkRichText *richtext, int visible);

Draw the editor's one-pixel frame on all four sides (the default), or on none.

Turn it off when the editor sits against another widget's border, so the two do not make a line two pixels wide. It is the same as xtk_richtext_set_border_sides() with XTK_RICHTEXT_BORDER_ALL or 0.

Parameter Description
richtext The editor
visible 1 to draw the frame, 0 not to

xtk_richtext_set_border_sides

void xtk_richtext_set_border_sides(XtkRichText *richtext, int sides_mask);

Choose which edges of the editor get the frame, and redraw.

For example only the top edge, for an editor that should line up with the title bars next to it while its other sides touch other widgets.

Parameter Description
richtext The editor
sides_mask An OR of the XTK_RICHTEXT_BORDER_* bits; 0 for none

xtk_richtext_set_readonly

void xtk_richtext_set_readonly(XtkRichText *richtext, int readonly);

Make the editor read-only, or editable again.

A read-only editor ignores every key that changes the document (typing, Backspace, Delete, Return, Cut, Paste, Undo, Redo and the formatting keys) and edits of table cells, but navigation, selection, Select All and Copy work. Its right-click menu has only Copy and Select All. The formatting and insert calls, xtk_richtext_replace_selection(), xtk_richtext_replace_all(), xtk_richtext_undo() and xtk_richtext_redo() do nothing either; xtk_richtext_set_markdown(), xtk_richtext_set_content() and xtk_richtext_set_mode() still load and show a document.

Parameter Description
richtext The editor
readonly 1 for read-only, 0 for editable

xtk_richtext_set_wrap_code

void xtk_richtext_set_wrap_code(XtkRichText *richtext, int wrap);

Make long code block lines wrap, or scroll sideways (the default).

Wrapped, a long line goes on in the next row, in the same monospace font, broken at spaces. The editor is not redrawn.

Parameter Description
richtext The editor
wrap 1 to wrap, 0 to scroll sideways

xtk_richtext_set_base_directory

void xtk_richtext_set_base_directory(XtkRichText *richtext, const char *directory);

Set the folder that relative image paths are found in.

A path that starts with '/' is used as it is; any other is taken from this folder, so a document's images are found wherever the document is. When the folder changes, the images already read are dropped and read again from the new folder when next drawn.

Parameter Description
richtext The editor
directory The folder, or NULL or "" for the current directory

xtk_richtext_set_image_decoder

void xtk_richtext_set_image_decoder(
    XtkRichText *richtext,
    XtkRichTextImageDecoder decoder,
    void *user_data);

Have the program decode the images a document names, or the editor itself (the default).

Parameter Description
richtext The editor
decoder The program's decoder, or NULL for the editor's own
user_data Passed to decoder

xtk_richtext_get_mode

XtkRichTextMode xtk_richtext_get_mode(const XtkRichText *richtext);

The current mode.

Parameter Description
richtext The editor

Returns: XTK_RICHTEXT_MODE_RICHTEXT or XTK_RICHTEXT_MODE_MARKDOWN; XTK_RICHTEXT_MODE_RICHTEXT if richtext is NULL

xtk_richtext_get_detected_flavor

XtkRichTextFlavor xtk_richtext_get_detected_flavor(const XtkRichText *richtext);

The Markdown dialect the loaded document uses.

It is found when Markdown is parsed (xtk_richtext_set_markdown(), and xtk_richtext_set_mode() back to RICHTEXT): from the constructs the parser reported, and from a scan of the source for syntax the parser does not know (citations, front matter, definition lists, embeds). A document set with xtk_richtext_set_content() is not parsed and keeps the last result.

Parameter Description
richtext The editor

Returns: The dialect; XTK_RICHTEXT_FLAVOR_COMMONMARK if richtext is NULL

xtk_richtext_set_mode

void xtk_richtext_set_mode(XtkRichText *richtext, XtkRichTextMode mode);

Switch between the formatted document and its Markdown source.

To MARKDOWN, the document is written as Markdown and shown as plain text. To RICHTEXT, the text, edited or not, is parsed back into a document. The caret and the scroll position are carried across, and the switch is an undo step and calls the change callback. Something that one side cannot express can be lost, so the switch is never made by itself. The editor is not redrawn.

Parameter Description
richtext The editor
mode The mode to show; the current mode does nothing

xtk_richtext_set_content

void xtk_richtext_set_content(
    XtkRichText *richtext,
    const char *text,
    const XtkRichTextSpan *spans,
    int span_count);

Replace the whole document with a text and its spans.

The text and the spans are copied. The caret goes to the start and the editor scrolls to the top. Tables cannot be given this way: their cell data is cleared. The undo history is kept, the change callback is not called, and the editor is not redrawn.

Parameter Description
richtext The editor
text The text; NULL for empty
spans The spans, with offsets into text (may be NULL when span_count is 0)
span_count The number of spans

xtk_richtext_get_text

const char *xtk_richtext_get_text(const XtkRichText *richtext);

The document's text, as the current mode has it.

In RICHTEXT mode it is the plain text the spans format; in MARKDOWN mode it is the Markdown source being edited.

Parameter Description
richtext The editor

Returns: The text, owned by the editor and valid until its next change; "" if richtext is NULL

xtk_richtext_get_span_count

int xtk_richtext_get_span_count(const XtkRichText *richtext);

The number of spans in the document.

Parameter Description
richtext The editor

Returns: The number of spans; 0 if richtext is NULL. In MARKDOWN mode there are none

xtk_richtext_get_spans

const XtkRichTextSpan *xtk_richtext_get_spans(const XtkRichText *richtext);

The document's spans.

Parameter Description
richtext The editor

Returns: The spans (xtk_richtext_get_span_count() of them), owned by the editor and valid until its next change; it can be NULL when there are none, so use xtk_richtext_get_span_count()

xtk_richtext_get_markdown

char *xtk_richtext_get_markdown(const XtkRichText *richtext);

The document as Markdown source.

In RICHTEXT mode the document is written as Markdown. In MARKDOWN mode the text already is the Markdown, and a copy of it comes back exactly as typed.

Parameter Description
richtext The editor

Returns: The Markdown, allocated; free it with free(). An empty string if richtext is NULL.

xtk_richtext_get_markdown_map

char *xtk_richtext_get_markdown_map(
    const XtkRichText *richtext,
    size_t **text_offsets,
    size_t **markdown_offsets,
    int *count);

The document as Markdown, with where each of its lines went.

For a program that shows the Markdown in a view of its own and wants to carry a place from one to the other, as xtk_richtext_set_mode() does: for each line of the text, its offset in the text (xtk_richtext_get_text()) and the offset in the Markdown where that line starts (before a marker such as #, - or >, or a code block's opening fence). It is meant for RICHTEXT mode; in MARKDOWN mode the text is the Markdown.

Parameter Description
richtext The editor
text_offsets Receives the offsets in the text, in ascending order; free it with free()
markdown_offsets Receives the matching offsets in the Markdown; free it with free()
count Receives the number of pairs

Returns: The Markdown, allocated (free it with free()), or NULL if an argument is NULL

xtk_richtext_get_cursor

size_t xtk_richtext_get_cursor(const XtkRichText *richtext);

The caret's position.

Parameter Description
richtext The editor

Returns: The caret's byte offset in xtk_richtext_get_text(); 0 if richtext is NULL

xtk_richtext_set_cursor

void xtk_richtext_set_cursor(XtkRichText *richtext, size_t offset);

Put the caret at a position, with nothing selected.

It neither scrolls nor draws: see xtk_richtext_set_top_offset() and xtk_richtext_draw().

Parameter Description
richtext The editor
offset A byte offset in the text; past the end counts as the end

xtk_richtext_get_top_offset

size_t xtk_richtext_get_top_offset(const XtkRichText *richtext);

The start of the paragraph (line) whose row is shown first; for a wrapped paragraph that is not the start of the row itself.

Parameter Description
richtext The editor

Returns: A byte offset in the text; 0 if richtext is NULL

xtk_richtext_set_top_offset

void xtk_richtext_set_top_offset(XtkRichText *richtext, size_t offset);

Scroll so that the row holding a position is the first row shown.

The scroll stays within the document, as the wheel's does. It does not draw.

Parameter Description
richtext The editor
offset A byte offset in the text

xtk_richtext_set_markdown

void xtk_richtext_set_markdown(XtkRichText *richtext, const char *markdown);

Load a document: replace everything with parsed Markdown.

The mode does not change. In RICHTEXT mode the Markdown is parsed into the formatted document; in MARKDOWN mode it is shown as its source, as plain text (the dialect is found in both). The undo history is cleared and the document counts as clean (see xtk_richtext_is_undo_clean()). The caret goes to the start, the change callback is not called, and the editor is not redrawn.

Parameter Description
richtext The editor
markdown The Markdown source; NULL for an empty document

xtk_richtext_toggle_inline_span

void xtk_richtext_toggle_inline_span(XtkRichText *richtext, XtkRichTextSpanType type);

Toggle an inline style over the selection.

When a span of this type covers exactly the selection, it is removed; otherwise one is added. In MARKDOWN mode the selection gets the type's Markdown marks (**, *, _, ~~, ^, ~, ', ==, $), or loses them when they are right around it. Nothing happens without a selection, in a read-only editor, or for a block type, a color (use xtk_richtext_set_inline_color()) or a link (use xtk_richtext_set_link()). It is an undo step and calls the change callback; the editor is not redrawn.

Parameter Description
richtext The editor
type An inline span type, such as XTK_RICHTEXT_SPAN_BOLD or XTK_RICHTEXT_SPAN_MATH_INLINE

xtk_richtext_set_inline_color

void xtk_richtext_set_inline_color(
    XtkRichText *richtext,
    XtkRichTextSpanType type,
    unsigned char r,
    unsigned char g,
    unsigned char b);

Set or clear a text or background color over the selection.

The selection gets the color; when it already has exactly this color over exactly this range, the color is removed instead. Nothing happens without a selection, in a read-only editor or in MARKDOWN mode. It is an undo step and calls the change callback.

Parameter Description
richtext The editor
type XTK_RICHTEXT_SPAN_FOREGROUND_COLOR or XTK_RICHTEXT_SPAN_BACKGROUND_COLOR
r Red, 0 to 255
g Green, 0 to 255
b Blue, 0 to 255

xtk_richtext_add_inline_color

void xtk_richtext_add_inline_color(
    XtkRichText *richtext,
    XtkRichTextSpanType type,
    size_t start,
    size_t length,
    unsigned char r,
    unsigned char g,
    unsigned char b);

Color a range of the text for display only, as the program works it out.

For what Markdown cannot say, such as syntax colors that a program that understands the code computes for a code block. It is not an edit: no undo step and no change callback. The color is an ordinary span: it moves with later edits, is kept in later undo steps, and outside a code block is written into the Markdown as <span style="color:...">. It is gone after the next xtk_richtext_set_markdown() or xtk_richtext_set_content(). Draw afterwards to see it. A range outside the text is ignored.

Parameter Description
richtext The editor
type XTK_RICHTEXT_SPAN_FOREGROUND_COLOR or XTK_RICHTEXT_SPAN_BACKGROUND_COLOR
start The byte offset in xtk_richtext_get_text()
length The number of bytes; 0 is ignored
r Red, 0 to 255
g Green, 0 to 255
b Blue, 0 to 255
void xtk_richtext_set_link(XtkRichText *richtext, const char *target);

Make the selection a link, or remove the link.

When the selection is already a link to exactly this target, the link is removed; otherwise any link there is replaced. The link is drawn underlined in the link color. The editor does not follow links itself: a click calls the link click callback. In MARKDOWN mode the selection becomes [selection](target) in the source. Nothing happens without a selection or in a read-only editor. It is an undo step and calls the change callback.

Parameter Description
richtext The editor
target Where the link points, copied (cut to XTK_RICHTEXT_LINK_TARGET_MAX - 1 bytes); NULL or "" does nothing

xtk_richtext_set_block_type

void xtk_richtext_set_block_type(
    XtkRichText *richtext,
    XtkRichTextSpanType type,
    int level);

Set the block format of every line the selection touches.

A line has at most one block format, so the new one replaces any other. A line that already has this type at this level loses it and becomes a plain paragraph (a toggle); the same type at another level is replaced, so H2 on an H1 line makes it H2, and Numbered on a bullet item makes it numbered. Nothing happens without a selection, in a read-only editor or in MARKDOWN mode (as for every insert function below). Use xtk_richtext_insert_horizontal_rule() for a rule. It is an undo step and calls the change callback.

Parameter Description
richtext The editor
type A block span type, such as XTK_RICHTEXT_SPAN_HEADING or XTK_RICHTEXT_SPAN_QUOTE
level For HEADING the level, 1 to 6; for LIST_ITEM an XtkRichTextListKind; for ADMONITION the kind, 0 to 4; not used otherwise

xtk_richtext_insert_horizontal_rule

void xtk_richtext_insert_horizontal_rule(XtkRichText *richtext);

Insert a horizontal rule at the caret, on a line of its own.

A selection is deleted first. It is an undo step and calls the change callback.

Parameter Description
richtext The editor

xtk_richtext_insert_image

void xtk_richtext_insert_image(XtkRichText *richtext, const char *path);

Insert an image at the caret, on a line of its own.

The line's text is the path, which is also shown when the image cannot be decoded. The image is read when it is first drawn, through the program's decoder or the editor's own (PNG, JPEG, GIF, BMP or XPM), with a relative path taken from the base directory, and scaled down to fit 320 by 178 pixels. A selection is deleted first. It is an undo step and calls the change callback.

Parameter Description
richtext The editor
path The image file; NULL, "" or a path with a line break does nothing

xtk_richtext_insert_table

void xtk_richtext_insert_table(XtkRichText *richtext, int rows, int columns);

Insert a table at the caret, on a line of its own.

Row 0, the header row, is filled with "Header 1", "Header 2" and so on; the other cells are empty. The user edits the cells in place. A selection is deleted first. It is an undo step and calls the change callback.

Parameter Description
richtext The editor
rows The number of rows, the header row included; at most XTK_RICHTEXT_TABLE_MAX_ROWS. 0 or less does nothing.
columns The number of columns; at most XTK_RICHTEXT_TABLE_MAX_COLUMNS. 0 or less does nothing.

xtk_richtext_insert_footnote

void xtk_richtext_insert_footnote(XtkRichText *richtext);

Insert a footnote: a reference at the caret and an empty definition at the end.

The reference is "[N]", with the next free number. The definition line is added at the end of the document, and the caret moves there so the note can be typed. The numbers are not renumbered until the document is written as Markdown and read again. It is an undo step and calls the change callback.

Parameter Description
richtext The editor

xtk_richtext_insert_math_block

void xtk_richtext_insert_math_block(XtkRichText *richtext);

Insert an empty display math line at the caret, and put the caret in it.

The LaTeX source is then typed on the line. Inline math needs no insert: toggle XTK_RICHTEXT_SPAN_MATH_INLINE over a selection with xtk_richtext_toggle_inline_span(). It is an undo step and calls the change callback.

Parameter Description
richtext The editor

xtk_richtext_adjust_indent

void xtk_richtext_adjust_indent(XtkRichText *richtext, int delta);

Indent or outdent the lines the selection (or the caret) touches.

Only lines with a block format (a heading, a list item, a quote and the others) change; the indent never goes below 0. When a line changed, it is an undo step and calls the change callback; otherwise nothing happens.

Parameter Description
richtext The editor
delta The levels to add; negative to remove; 0 does nothing

xtk_richtext_get_active_spans

unsigned int xtk_richtext_get_active_spans(const XtkRichText *richtext);

The formatting at the caret or selection, as bits, for a toolbar to show.

Each span type that applies has its bit (1 << type) set. A block type counts when it is on the line where the selection (or the caret) starts. An inline type counts only when it covers the whole selection; with no selection, no inline type counts. The HEADING bit does not tell the level: see xtk_richtext_get_active_heading_level().

Parameter Description
richtext The editor

Returns: The bits; 0 if richtext is NULL

xtk_richtext_get_active_heading_level

int xtk_richtext_get_active_heading_level(const XtkRichText *richtext);

The heading level of the line where the selection (or the caret) starts.

Parameter Description
richtext The editor

Returns: The level, 1 to 6, or 0 if the line is not a heading or richtext is NULL

xtk_richtext_get_active_admonition_kind

int xtk_richtext_get_active_admonition_kind(const XtkRichText *richtext);

The admonition kind of the line where the selection (or the caret) starts.

Parameter Description
richtext The editor

Returns: The kind, 0 to 4 (note, tip, important, warning, caution), or -1 if the line is not in an admonition or richtext is NULL

xtk_richtext_set_font_size

void xtk_richtext_set_font_size(XtkRichText *richtext, int size);

Set the base font size, in pixels (12 at first).

Every font (body, bold, italic, code, small, and the larger fonts of headings 1 to 3) is opened again, and the math font follows. Call xtk_richtext_draw() afterwards.

Parameter Description
richtext The editor
size The pixel size; 0 or less, or the current size, does nothing

xtk_richtext_set_font_family

void xtk_richtext_set_font_family(XtkRichText *richtext, const char *family);

Set the body font family ("DejaVu Sans" at first).

Every font is opened again at the current size. Code stays in "monospace", and equations follow xtk_richtext_set_math_font(). Call xtk_richtext_draw() afterwards.

Parameter Description
richtext The editor
family A fontconfig family, or NULL or "" for "DejaVu Sans"

xtk_richtext_set_math_font

void xtk_richtext_set_math_font(XtkRichText *richtext, const char *family);

Set an OpenType math font for equations.

Without one (the default), equations are set in the body font, scaled for scripts. Call xtk_richtext_draw() afterwards.

Parameter Description
richtext The editor
family A math font's family, such as "Latin Modern Math", or NULL or "" for none

xtk_richtext_can_undo

int xtk_richtext_can_undo(const XtkRichText *richtext);

Whether there is an edit to undo.

Parameter Description
richtext The editor

Returns: 1 if there is one, 0 if not or if richtext is NULL

xtk_richtext_can_redo

int xtk_richtext_can_redo(const XtkRichText *richtext);

Whether there is an undone edit to redo.

Parameter Description
richtext The editor

Returns: 1 if there is one, 0 if not or if richtext is NULL

xtk_richtext_mark_undo_clean

void xtk_richtext_mark_undo_clean(XtkRichText *richtext);

Mark the document as it is now as saved.

Call it right after a document is saved or loaded; xtk_richtext_is_undo_clean() then follows undo and redo back to this point.

Parameter Description
richtext The editor (NULL is ignored)

xtk_richtext_clear_undo_clean

void xtk_richtext_clear_undo_clean(XtkRichText *richtext);

Make xtk_richtext_is_undo_clean() return 0 until the next mark, whatever is undone.

For a program whose save failed after it had called xtk_richtext_mark_undo_clean().

Parameter Description
richtext The editor (NULL is ignored)

xtk_richtext_is_undo_clean

int xtk_richtext_is_undo_clean(const XtkRichText *richtext);

Whether the document is at the point last marked with xtk_richtext_mark_undo_clean().

Undoing back to the saved document makes it 1 again, so a program's "modified" mark can follow undo. The marked point is lost when a new edit is made after undoing past it, or when it drops out of the 200 stored steps.

Parameter Description
richtext The editor

Returns: 1 if it is at the marked point, 0 if not or if richtext is NULL

xtk_richtext_find

int xtk_richtext_find(XtkRichText *richtext, const char *needle, int case_sensitive);

Find the next place a string occurs, select it, scroll to it and redraw.

The search is in xtk_richtext_get_text(): the plain text in RICHTEXT mode, the Markdown source in MARKDOWN mode. It starts at the end of the selection (or at the caret) and goes on from the top when it reaches the end, as Find Next does. Table cells are not searched.

Parameter Description
richtext The editor
needle The text to look for
case_sensitive Non-zero to match case exactly, 0 to ignore the case of ASCII letters

Returns: 1 if found, 0 if it occurs nowhere or needle is empty

xtk_richtext_replace_selection

int xtk_richtext_replace_selection(XtkRichText *richtext, const char *replacement);

Replace the selection, for example a match from xtk_richtext_find(), and redraw.

Parameter Description
richtext The editor
replacement The text to put in; NULL or "" deletes the selection

Returns: 1 if there was a selection, 0 if not or if the editor is read-only

xtk_richtext_replace_all

int xtk_richtext_replace_all(
    XtkRichText *richtext,
    const char *needle,
    const char *replacement,
    int case_sensitive);

Replace every place a string occurs, and redraw.

The search goes once from the start to the end and never looks inside text it has just put in, so it ends even when the replacement holds the string. Each replacement is its own undo step.

Parameter Description
richtext The editor
needle The text to look for
replacement The text to put in; NULL or "" deletes each match
case_sensitive Non-zero to match case exactly, 0 to ignore the case of ASCII letters

Returns: The number of replacements made; 0 in a read-only editor

xtk_richtext_undo

void xtk_richtext_undo(XtkRichText *richtext);

Undo the last edit, and call the change callback. The editor is not redrawn.

Parameter Description
richtext The editor (nothing happens when there is nothing to undo or it is read-only)

xtk_richtext_redo

void xtk_richtext_redo(XtkRichText *richtext);

Redo the last undone edit, and call the change callback. The editor is not redrawn.

Parameter Description
richtext The editor (nothing happens when there is nothing to redo or it is read-only)

xtk_richtext_set_change_callback

void xtk_richtext_set_change_callback(
    XtkRichText *richtext,
    XtkRichTextChangeCallback callback,
    void *user_data);

Set the function called after each edit (see XtkRichTextChangeCallback).

Parameter Description
richtext The editor
callback The function, or NULL for none
user_data Passed to the callback
void xtk_richtext_set_link_click_callback(
    XtkRichText *richtext,
    XtkRichTextLinkClickCallback callback,
    void *user_data);

Set the function called when a link is clicked (see XtkRichTextLinkClickCallback).

Parameter Description
richtext The editor
callback The function, or NULL for none (a click then places the caret)
user_data Passed to the callback