/* The message editor (editor.c): multi-line text, history, collapsed * pastes. No I/O: keys in, lines out. */ #ifndef PIC_TUI_EDITOR_H #define PIC_TUI_EDITOR_H #include "term.h" #include "width.h" typedef struct tui_editor tui_editor; enum { TUI_EDIT_NONE, /* the key did nothing */ TUI_EDIT_CHANGED, /* text or cursor changed: redraw */ TUI_EDIT_SUBMIT, /* Enter on non-empty text: take it with tui_editor_take */ }; tui_editor *tui_editor_new(void); void tui_editor_free(tui_editor *e); /* Applies one key: * CHAR inserts; NEWLINE inserts "\n"; ENTER submits (nothing when the text * is only whitespace); BACKSPACE / DELETE remove one character, or a whole * paste marker; LEFT/RIGHT move by character (across lines), UP/DOWN by * line, keeping the column, and on the first/last line recall the * previous/next history entry (the unsent text is kept and restored past * the newest); HOME/END go to the start/end of the line; WORD_LEFT/RIGHT * by word; DELETE_WORD deletes the word before the cursor and * DELETE_WORD_FWD the one after it; KILL_LINE and KILL_START delete to the * end/start of the line. Every deletion of a word or line goes into a kill * ring (consecutive ones merge into one entry, as in Pi): YANK inserts the * newest entry and, right after it, YANK_POP swaps it for the next older one. * UNDO steps back one edit (a run of typed word characters is one step, each * space another). JUMP / JUMP_BACK wait for a character and move to its next * / previous occurrence (the same key again cancels). PASTE inserts the text, * except that a paste over 10 lines or 1000 characters is shown as one * atomic marker "[paste #N +L lines]" (or "[paste #N C chars]") and * expanded on submit. Other keys: TUI_EDIT_NONE (the caller handles them). */ int tui_editor_key(tui_editor *e, const tui_key *k); /* An image attached to the message: its bytes, malloc'd, owned by the holder. */ typedef struct { unsigned char *data; size_t len; } tui_attachment; /* Attaches an image at the cursor as one atomic marker "[image #N]" (numbered * from 1, restarting after each submit/clear), deleted like a paste marker. * Takes DATA (malloc'd) on success and returns N; -1 (DATA not taken) on * failure. The marker stays in the sent text. */ int tui_editor_attach(tui_editor *e, unsigned char *data, size_t len); /* How many attachments have their marker in the text now. */ size_t tui_editor_attached(const tui_editor *e); /* tui_editor_take, also moving the attachments whose marker is still in the * text, in text order, into *LIST (malloc'd; each data too; *N of them). */ char *tui_editor_take_attached(tui_editor *e, tui_attachment **list, size_t *n); /* The text with paste markers expanded (malloc'd); the editor is unchanged. */ char *tui_editor_expanded(const tui_editor *e); /* The submitted text with markers expanded (malloc'd); the editor is left * empty and the text is added to the history (no repeat of the last entry, * at most 200 entries). */ char *tui_editor_take(tui_editor *e); /* Adds an entry to the history as if it had been submitted (no repeat of * the last entry): the way a resumed conversation's messages are recalled. */ void tui_editor_history_add(tui_editor *e, const char *text); /* The history, oldest first, for Ctrl+R: how many entries, one of them, and * the newest entry at or before FROM (-1: the newest) that contains Q, ASCII * case-insensitively (an empty Q matches every entry); its index, or -1. */ size_t tui_editor_history_count(const tui_editor *e); /* Forgets the newest entry (wiped first): a message that was held back, not sent. */ void tui_editor_history_drop_last(tui_editor *e); const char *tui_editor_history_at(const tui_editor *e, size_t i); long tui_editor_history_find(const tui_editor *e, const char *q, long from); int tui_editor_empty(const tui_editor *e); void tui_editor_clear(tui_editor *e); /* Replaces the text (e.g. queued messages restored), cursor at the end. */ void tui_editor_set(tui_editor *e, const char *text); /* Replaces the bytes [FROM, TO) with TEXT as one undoable edit and puts the * cursor CURSOR_OFF bytes into the new text (clamped): how a completion lands. */ void tui_editor_replace(tui_editor *e, size_t from, size_t to, const char *text, size_t cursor_off); /* The text as lines for cols columns, appended to out: the first line * starts with "> ", later ones with " ", wrapped (tui_wrap rules) within * cols; the cursor's row (counted in out from the first line appended) and * column in *crow, *ccol. 0 or -1. */ int tui_editor_render(const tui_editor *e, size_t cols, tui_lines *out, int *crow, int *ccol); /* The current text as shown (paste markers included), NUL-terminated, valid * until the next edit; and the cursor's byte offset into it. */ const char *tui_editor_text(const tui_editor *e); size_t tui_editor_cursor(const tui_editor *e); #endif