robin/laufey

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

Justin M. Keyesfeat(cmdatom)!: eliminate the need for vim-repeat #414146423657352

master
3.8 KiB88 linesraw
1#pragma once
2
3#include <stdbool.h>
4#include <stddef.h>
5#include <stdint.h>
6
7#include "nvim/api/private/defs.h"
8#include "nvim/types_defs.h"
9
10/// Readahead ("stuff") buffer: consumable queue of keys in typeahead encoding (K_SPECIAL
11/// escaped). add_buff() writes at `insert`, read_readbuf() consumes from `read`.
12typedef struct {
13  StringBuilder keys;  ///< Bytes; `[read, keys.size)` is unread.
14  size_t read;         ///< Read position: consumed up to here. Reset (keeping capacity) on drain.
15  size_t insert;       ///< Write position: `keys.size`, or the read position as of start_stuff()
16                       ///< (newly stuffed keys drain before older readahead).
17} StuffBuf;
18
19/// Represents any command. Projection of (`cmdarg_T`, `oparg_T`), which are stack-transient.
20///
21/// Used two ways:
22/// - Capture (prep_redo()): the command appends its own bytes to the redo body; only `regname`
23///   and `count` are functional (the `["x][count]` prefix), the rest is CmdAtom metadata.
24/// - Reconstruction (atom_from_spec()): a "non-prepped" command ("u", motions) has no body, so
25///   atom_redo_keys() composes the keysequence fully from the spec.
26typedef struct {
27  long count;        ///< Effective count (0 = none)
28  int regname;       ///< Register (`"x` prefix; 0 = none)
29  int op;            ///< Operator char ('d'; 0 = none)
30  int op_extra;      ///< Second operator char ("g~" => '~'; 0 = none)
31  int motion_force;  ///< Forced motion type ('v'/'V'/CTRL-V; 0 = none)
32  int cmd;           ///< Command/motion char ('J', 'p', 'f', K_LEFT, …; 0 = none)
33  int cmd2;          ///< Command char 2 (for two-char cmds: "gJ" => 'J'; 0 = none)
34  int cmdarg;        ///< Command operand ("fx" => 'x', "ma" => 'a'; 0 = none)
35  StringBuilder body;  ///< Captured cmd "body": keys without the `["x][count]` "prefix", updated
36                       ///< as the cmd executes (redo_append_xx); redo_keys() prepends the prefix.
37                       ///< Empty in spec-only contexts (i.e. not a "redo" buf).
38} CmdSpec;
39
40/// Dot-repeat state: the pending change and the previous one.
41/// Also the "save" shape for preserving it across user code (save_redobuff()).
42typedef struct {
43  CmdSpec cur;  ///< Pending change: "." replays it (start_redo()).
44  CmdSpec old;  ///< Last-but-one change: an insert session's own prep moved the previous change
45                ///< here (redo_new()); "i_CTRL-O ." replays it.
46} RedoState;
47
48/// Used for the typeahead buffer: typebuf.
49typedef struct {
50  uint8_t *tb_buf;      ///< buffer for typed characters
51  uint8_t *tb_noremap;  ///< mapping flags for characters in tb_buf[]
52  int tb_buflen;        ///< size of tb_buf[]
53  int tb_off;           ///< current position in tb_buf[]
54  int tb_len;           ///< number of valid bytes in tb_buf[]
55  int tb_maplen;        ///< nr of mapped bytes in tb_buf[]
56  int tb_silent;        ///< nr of silently mapped bytes in tb_buf[]
57  int tb_no_abbr_cnt;   ///< nr of bytes without abbrev. in tb_buf[]
58  int tb_change_cnt;    ///< nr of time tb_buf was changed; never zero
59} typebuf_T;
60
61/// Char put back by vungetc() (`c == -1`: none), with the read state restored when it is re-read:
62/// a later input event must not leak its mod_mask/mouse coords into the ungot key.
63typedef struct {
64  int c;  ///< -1: none
65  int mod_mask;
66  int mouse_grid;
67  int mouse_row;
68  int mouse_col;
69  bool stuffed;  ///< `KeyStuffed` when ungotten.
70} UngotKey;
71
72/// Struct to hold the saved typeahead for save_typeahead().
73typedef struct {
74  typebuf_T save_typebuf;
75  UngotKey ungot;
76  StuffBuf save_readbuf1;
77  StuffBuf save_readbuf2;
78} tasave_T;
79
80/// Values for "noremap" argument of ins_typebuf()
81///
82/// Also used for map->m_noremap and menu->noremap[].
83enum RemapValues {
84  REMAP_YES = 0,      ///< Allow remapping.
85  REMAP_NONE = -1,    ///< No remapping.
86  REMAP_SCRIPT = -2,  ///< Remap script-local mappings only.
87  REMAP_SKIP = -3,    ///< No remapping for first char.
88};