Skip to content
pi-c Artifact [7abbe3de82]
Login

Artifact [7abbe3de82]

Artifact 7abbe3de82062f91dc6bf99511e87ec6d44b75882e3d4ce1af0833c8f429a5ca:


/* Pi's keybindings (keymap.c): the action names and default keys of Pi's
 * keybindings.ts, the ~/.pic/keybindings.json format ("action": "key" or
 * ["key", ...]; a value replaces the action's defaults and [] disables it),
 * and the translation of a decoded key into what the action it is bound to
 * means to the TUI. */
#ifndef PIC_TUI_KEYMAP_H
#define PIC_TUI_KEYMAP_H

#include <stddef.h>

#include "term.h"

/* What a TUI_KEY_ACTION key asks for (tui_key.action). */
enum {
    TUI_ACT_NONE,
    TUI_ACT_SESSION_NEW, TUI_ACT_SESSION_TREE, TUI_ACT_SESSION_FORK, TUI_ACT_SESSION_RESUME,
    TUI_ACT_SCROLL_HALF_UP, TUI_ACT_SCROLL_HALF_DOWN, TUI_ACT_SCROLL_LINE_UP, TUI_ACT_SCROLL_LINE_DOWN,
    TUI_ACT_REDRAW,
    TUI_ACT_SEARCH_TOGGLE, TUI_ACT_SEARCH_NEXT, TUI_ACT_SEARCH_PREV, TUI_ACT_SEARCH_CLOSE,
    TUI_ACT_HISTORY_SEARCH,
    TUI_ACT_CREDENTIAL_SELECT,
};

/* A key as Pi writes it: modifiers and one base key ("shift+ctrl+p", "pageUp", "]"). */
typedef struct {
    unsigned mods; /* TUI_MOD_* */
    char base[12]; /* canonical: "esc" is "escape", "return" is "enter" */
    char text[28]; /* as written, for display */
} tui_keyid;

/* Pi's key syntax (key-id-schema.ts): 0, or -1 when s is not a key id. */
int tui_keyid_parse(const char *s, tui_keyid *out);

typedef struct tui_keymap tui_keymap;

/* Pi's defaults. */
tui_keymap *tui_keymap_new(void);
void tui_keymap_free(tui_keymap *m);
/* Back to the defaults, then the user's file JSON[0..n) over them (LABEL names it in messages). Problems
 * come back as lines of text in *warnings (malloc'd, "" when none; NULL drops them): a file that is not JSON,
 * an action that does not exist, a value that is not a key id or a list of them (that action keeps its
 * default), and one key bound to two actions. 0, or -1 when out of memory. */
int tui_keymap_load(tui_keymap *m, const char *json, size_t n, const char *label, char **warnings);

/* Rewrites a decoded key to the kind of the action it is bound to (for the TUI_KEY_ACTION kind, .action
 * too). Text, pastes and mouse reports pass untouched. 0 when the key is bound to nothing: drop it. */
int tui_keymap_apply(const tui_keymap *m, tui_key *k);

/* Whether the key as decoded (before tui_keymap_apply: its mods and base) is one of the keys the action is
 * bound to. For the keys that mean something only in a mode, as the transcript search's Enter and Escape do. */
int tui_keymap_has(const tui_keymap *m, const char *action, const tui_key *k);

/* The actions in Pi's order. */
size_t tui_keymap_count(void);
const char *tui_keymap_name(size_t i);
const char *tui_keymap_desc(size_t i);
/* Whether pic does something for the action (the others are accepted, and shown by tui_keymap_defaults only). */
int tui_keymap_active(size_t i);
/* The action's keys as written, ", "-separated ("" when it has none). */
void tui_keymap_keys(const tui_keymap *m, size_t i, char *out, size_t cap);
/* The index of an action by name, or -1. */
int tui_keymap_find(const char *name);
/* The letters whose Alt chord some action is bound to (for tui_keys_alt_letters), NUL-terminated. */
void tui_keymap_alt_letters(const tui_keymap *m, char out[40]);
/* The effective bindings of the actions pic acts on, as lines of text for /hotkeys, malloc'd. */
char *tui_keymap_hotkeys(const tui_keymap *m);

#endif