robin/laufey

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

mgleonard425feat(terminal): synchronized output (mode 2026) #38284b38173e493

master
6.6 KiB295 linesraw
1#pragma once
2
3#include <stdarg.h>
4
5#include "nvim/mbyte_defs.h"
6#include "nvim/vterm/vterm_defs.h"
7
8#ifdef DEBUG
9# define DEBUG_LOG(...) fprintf(stderr, __VA_ARGS__)
10#else
11# define DEBUG_LOG(...)
12#endif
13
14#define ESC_S "\x1b"
15
16#define INTERMED_MAX 16
17
18#define CSI_ARGS_MAX 32
19#define CSI_LEADER_MAX 16
20
21#define BUFIDX_PRIMARY   0
22#define BUFIDX_ALTSCREEN 1
23
24#define KEY_ENCODING_DISAMBIGUATE 0x1
25#define KEY_ENCODING_REPORT_EVENTS 0x2
26#define KEY_ENCODING_REPORT_ALTERNATE 0x4
27#define KEY_ENCODING_REPORT_ALL_KEYS 0x8
28#define KEY_ENCODING_REPORT_ASSOCIATED 0x10
29
30typedef struct VTermEncoding VTermEncoding;
31typedef struct VTermKeyEncodingFlags VTermKeyEncodingFlags;
32
33typedef struct {
34  VTermEncoding *enc;
35
36  // This size should be increased if required by other stateful encodings
37  char data[4 * sizeof(uint32_t)];
38} VTermEncodingInstance;
39
40struct VTermPen {
41  VTermColor fg;
42  VTermColor bg;
43  int uri;
44  unsigned bold:1;
45  unsigned underline:2;
46  unsigned italic:1;
47  unsigned blink:1;
48  unsigned reverse:1;
49  unsigned conceal:1;
50  unsigned strike:1;
51  unsigned font:4;  // To store 0-9
52  unsigned small:1;
53  unsigned baseline:2;
54  unsigned dim:1;
55  unsigned overline:1;
56};
57
58// https://sw.kovidgoyal.net/kitty/keyboard-protocol/#progressive-enhancement
59struct VTermKeyEncodingFlags {
60  bool disambiguate:1;
61  bool report_events:1;
62  bool report_alternate:1;
63  bool report_all_keys:1;
64  bool report_associated:1;
65};
66
67struct VTermKeyEncodingStack {
68  VTermKeyEncodingFlags items[16];
69  uint8_t size;  ///< Number of items in the stack. This is at least 1 and at
70                 ///< most the length of the "items" array.
71};
72
73struct VTermState {
74  VTerm *vt;
75
76  const VTermStateCallbacks *callbacks;
77  void *cbdata;
78
79  const VTermStateFallbacks *fallbacks;
80  void *fbdata;
81
82  int rows;
83  int cols;
84
85  // Current cursor position
86  VTermPos pos;
87
88  int at_phantom;  // True if we're on the "81st" phantom column to defer a wraparound
89
90  int scrollregion_top;
91  int scrollregion_bottom;  // -1 means unbounded
92#define SCROLLREGION_BOTTOM(state) ((state)->scrollregion_bottom > \
93                                    -1 ? (state)->scrollregion_bottom : (state)->rows)
94  int scrollregion_left;
95#define SCROLLREGION_LEFT(state)  ((state)->mode.leftrightmargin ? (state)->scrollregion_left : 0)
96  int scrollregion_right;  // -1 means unbounded
97#define SCROLLREGION_RIGHT(state) ((state)->mode.leftrightmargin \
98                                   && (state)->scrollregion_right > \
99                                   -1 ? (state)->scrollregion_right : (state)->cols)
100
101  // Bitvector of tab stops
102  uint8_t *tabstops;
103
104  // Primary and Altscreen; lineinfos[1] is lazily allocated as needed
105  VTermLineInfo *lineinfos[2];
106
107  // lineinfo will == lineinfos[0] or lineinfos[1], depending on altscreen
108  VTermLineInfo *lineinfo;
109#define ROWWIDTH(state, \
110                 row) ((state)->lineinfo[(row)].doublewidth ? ((state)->cols / 2) : (state)->cols)
111#define THISROWWIDTH(state) ROWWIDTH(state, (state)->pos.row)
112
113  // Mouse state
114  int mouse_col, mouse_row;
115  int mouse_buttons;
116  int mouse_flags;
117#define MOUSE_WANT_CLICK 0x01
118#define MOUSE_WANT_DRAG  0x02
119#define MOUSE_WANT_MOVE  0x04
120
121  enum { MOUSE_X10, MOUSE_UTF8, MOUSE_SGR, MOUSE_RXVT, } mouse_protocol;
122
123// Last glyph output, for Unicode recombining purposes
124  char grapheme_buf[MAX_SCHAR_SIZE];
125  size_t grapheme_len;
126  uint32_t grapheme_last;  // last added UTF-32 char
127  GraphemeState grapheme_state;
128  int combine_width;  // The width of the glyph above
129  VTermPos combine_pos;   // Position before movement
130
131  struct {
132    unsigned keypad:1;
133    unsigned cursor:1;
134    unsigned autowrap:1;
135    unsigned insert:1;
136    unsigned newline:1;
137    unsigned cursor_visible:1;
138    unsigned cursor_blink:1;
139    unsigned cursor_shape:2;
140    unsigned alt_screen:1;
141    unsigned origin:1;
142    unsigned screen:1;
143    unsigned leftrightmargin:1;
144    unsigned bracketpaste:1;
145    unsigned report_focus:1;
146    unsigned theme_updates:1;
147    unsigned synchronized_output:1;
148  } mode;
149
150  VTermEncodingInstance encoding[4], encoding_utf8;
151  int gl_set, gr_set, gsingle_set;
152
153  struct VTermPen pen;
154
155  VTermColor default_fg;
156  VTermColor default_bg;
157  VTermColor colors[16];  // Store the 8 ANSI and the 8 ANSI high-brights only
158
159  int bold_is_highbright;
160
161  unsigned protected_cell : 1;
162
163// Saved state under DEC mode 1048/1049
164  struct {
165    VTermPos pos;
166    struct VTermPen pen;
167
168    struct {
169      unsigned cursor_visible:1;
170      unsigned cursor_blink:1;
171      unsigned cursor_shape:2;
172    } mode;
173  } saved;
174
175// Temporary state for DECRQSS parsing
176  union {
177    char decrqss[4];
178    struct {
179      uint16_t mask;
180      enum {
181        SELECTION_INITIAL,
182        SELECTION_SELECTED,
183        SELECTION_QUERY,
184        SELECTION_SET_INITIAL,
185        SELECTION_SET,
186        SELECTION_INVALID,
187      } state : 8;
188      uint32_t recvpartial;
189      uint32_t sendpartial;
190    } selection;
191  } tmp;
192
193  struct {
194    const VTermSelectionCallbacks *callbacks;
195    void *user;
196    char *buffer;
197    size_t buflen;
198  } selection;
199
200  // Maintain two stacks, one for primary screen and one for altscreen
201  struct VTermKeyEncodingStack key_encoding_stacks[2];
202};
203
204struct VTerm {
205  const VTermAllocatorFunctions *allocator;
206  void *allocdata;
207
208  int rows;
209  int cols;
210
211  struct {
212    unsigned utf8:1;
213    unsigned ctrl8bit:1;
214  } mode;
215
216  struct {
217    enum VTermParserState {
218      NORMAL,
219      CSI_LEADER,
220      CSI_ARGS,
221      CSI_INTERMED,
222      DCS_COMMAND,
223      // below here are the "string states"
224      OSC_COMMAND,
225      OSC,
226      DCS_VTERM,
227      APC,
228      PM,
229      SOS,
230    } state;
231
232    bool in_esc : 1;
233
234    int intermedlen;
235    char intermed[INTERMED_MAX];
236
237    union {
238      struct {
239        int leaderlen;
240        char leader[CSI_LEADER_MAX];
241
242        int argi;
243        long args[CSI_ARGS_MAX];
244      } csi;
245      struct {
246        int command;
247      } osc;
248      struct {
249        int commandlen;
250        char command[CSI_LEADER_MAX];
251      } dcs;
252    } v;
253
254    const VTermParserCallbacks *callbacks;
255    void *cbdata;
256
257    bool string_initial;
258
259    bool emit_nul;
260  } parser;
261
262  // len == malloc()ed size; cur == number of valid bytes
263
264  VTermOutputCallback *outfunc;
265  void *outdata;
266
267  char *outbuffer;
268  size_t outbuffer_len;
269  size_t outbuffer_cur;
270
271  char *tmpbuffer;
272  size_t tmpbuffer_len;
273
274  VTermState *state;
275  VTermScreen *screen;
276};
277
278struct VTermEncoding {
279  void (*init)(VTermEncoding *enc, void *data);
280  void (*decode)(VTermEncoding *enc, void *data, uint32_t cp[], int *cpi, int cplen,
281                 const char bytes[], size_t *pos, size_t len);
282};
283
284typedef enum {
285  ENC_UTF8,
286  ENC_SINGLE_94,
287} VTermEncodingType;
288
289enum {
290  C1_SS3 = 0x8f,
291  C1_DCS = 0x90,
292  C1_CSI = 0x9b,
293  C1_ST  = 0x9c,
294  C1_OSC = 0x9d,
295};