robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
5.5 KiB99 linesraw
1#pragma once
2
3#include <stdbool.h>
4
5#include "klib/kvec.h"
6#include "nvim/buffer_defs.h"
7#include "nvim/eval/typval_defs.h"
8#include "nvim/input_defs.h"
9
10// Concepts (see :help dev-cmdatom):
11// - atom, span, composite (atom with subatoms)
12// - insert-session, insertion
13// - payload
14// - replay, cascade, insert-cascade
15
16typedef enum CmdAtomType {
17  kAComp,          ///< Composite: complex mapping/macro. >=2 subatoms, or 0 (captured nothing).
18  kAExcmd,         ///< Ex command (":cnext<CR>"): the typed cmdline is the payload.
19  kAInsert,        ///< Insert session: entry command + text + <Esc>.
20  kAInsertSpan,    ///< Span (chunk) of an ongoing insert-session, cascaded mid-session.
21  kAJump,          ///< Cursor movement by absolute/shared navigation (jumplist, marks, `*`):
22                   ///< not followable (its target would collapse cursors onto one position).
23  kAMotion,        ///< Motion (cascades in "q=" follow-motion mode).
24  kAMouse,         ///< Mouse action: emit-only (not replayable).
25  kANormal,        ///< Normal-mode command that is not a motion or jump ("u", CTRL-R, "za", …).
26                   ///< Never cascades, except as part of a mapping's composite.
27  kAOperator,      ///< Operator+motion, or a self-contained edit command.
28  kAScroll,        ///< Scroll (CTRL-Y/D/…, wheel): emit-only, like kAMouse.
29  kAVisual,        ///< Visual-mode sequence ("viwee" + operator). Captures subatoms.
30  kAVisualSpan,    ///< Span (chunk) of an ongoing Visual session, cascaded mid-session.
31} CmdAtomType;
32
33/// State gathered at start of a command, composite, or insert. For calculating the "delta" at end.
34typedef struct {
35  bufref_T buf;       ///< Buffer.
36  const win_T *win;   ///< Window.
37  pos_T pos;          ///< Primary cursor position at the start (`win.w_cursor` moves after).
38  uint32_t mcursor;   ///< Mcursor overlapping primary at `pos` (mark id, 0: none). Not replayed.
39  varnumber_T tick;   ///< b:changedtick.
40  int maptick;        ///< Advances on typed input (globals.h:maptick).
41} CmdOrigin;
42
43/// How an insert-session was entered from Visual mode.
44typedef enum {
45  kVInsNone,    ///< Not entered from Visual mode.
46  kVInsKeys,    ///< (Replayable) Redo opens with the selection's captured keys.
47  kVInsMotion,  ///< (Replayable) Motion selected the region: Ex/Lua omap ("c" + Lua textobj), "gn".
48  kVInsOther,   ///< Redo without captured keys: forced motion, "gv", or "1v" fixed-size fallback.
49} VisualIns;
50
51/// The insert-session delimited by atom_ins_start()/atom_ins_end().
52typedef struct {
53  CmdOrigin origin;  ///< State at start.
54  bool typed;        ///< Session is user input (typed, or via mapping/macro).
55  VisualIns vis;     ///< Session was entered from Visual mode.
56  char *vsel;        ///< Visual keys ("gv…"); supplants redo's "1v" in atom+cascade. Owned.
57} InsSession;
58
59typedef struct CmdAtom CmdAtom;
60typedef kvec_t(CmdAtom) CmdAtomVec;
61
62/// One repeatable operation. `keys` is the replay bytes; `spec` is the structured form.
63struct CmdAtom {
64  CmdAtomType type;
65  CmdSpec spec;   ///< Structured fields.
66  CmdOrigin origin;  ///< Pre-command state.
67  CmdAtomVec atoms;  ///< Composite (multi-command mapping, Visual sequence): its subatoms,
68                     ///< in order; their keys concatenate to `keys`. Empty: non-composite.
69  char *keys;     ///< Resolved keysequence (typeahead encoding), including `["x][count]` prefix
70                  ///< (unlike `CmdSpec.body`, the raw unprefixed form).
71  char *text;     ///< Insert-session text, or Ex/search cmdline payload.
72  char *lhs;      ///< Unresolved user input: mapping LHS, macro ("@q"), Visual op, or translation
73                  ///< ("x" => "dl"). NULL: untranslated, same as `keys`.
74  int undoseq;    ///< Undo state at settlement. Not monotonic (decreases on undo).
75  bool changed;   ///< The command changed the buffer.
76  bool moved;     ///< The command moved the cursor.
77  bool remap;     ///< True if `keys` cannot replay (lossy/empty capture). Replay `lhs` instead.
78  bool cascaded;  ///< This atom already cascaded as spans: emit-only.
79};
80
81/// Key classes (atom_key_class()).
82/// Flags, bc same char can mean different things per mode (CTRL-T: tag-jump vs i_CTRL-T indent).
83enum {
84  kKeyOpaque     = 1 << 0,  ///< Cmds not reified from subatoms (<Cmd>, K_LUA), plus kKeySynthetic.
85                            ///< The cmd itself is the atom, else its only trace is its effect.
86  kKeySynthetic  = 1 << 1,  ///< Not a user keystroke (K_EVENT, K_IGNORE): unlike <Cmd>/K_LUA, never
87                            ///< reaches us from a mapping.
88  kKeyPayload    = 1 << 2,  ///< Interactively-typed payload (/, ?, :, !).
89  kKeyScrollMove = 1 << 3,  ///< Scroll may move cursor (C-D/…): viewport-dependent, unreplayable.
90  kKeyScrollView = 1 << 4,  ///< Viewport-only scroll (C-Y,wheel): cursor stays, unless 'scrolloff'.
91  kKeyJump       = 1 << 5,  ///< Absolute motion (multiplexed "gg", "g;", …), see NV_JUMP.
92  kKeyMotion     = 1 << 6,  ///< Cursor-relative motion (multiplexed, special keys), see NV_MOTION.
93  kKeyInsFlush   = 1 << 7,  ///< Insert-mode cmd a literal preview cannot represent:
94                            ///< - deletions/indent-shifts (<Del>, CTRL-W, …) may edit text
95                            ///<   outside the tracked region by per-cursor amounts;
96                            ///< - cursor-moves (start_arrow()) move the insertion point itself.
97  kKeyMouse      = 1 << 8,  ///< Mouse button press (<LeftMouse>, …). Drag/release/move are the
98                            ///< press's continuation: no class, invisible to capture.
99};