neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

vterm_internal_defs.h (6735B)


      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 
     30 typedef struct VTermEncoding VTermEncoding;
     31 typedef struct VTermKeyEncodingFlags VTermKeyEncodingFlags;
     32 
     33 typedef 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 
     40 struct 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
     59 struct 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 
     67 struct 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 
     73 struct 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  } mode;
    148 
    149  VTermEncodingInstance encoding[4], encoding_utf8;
    150  int gl_set, gr_set, gsingle_set;
    151 
    152  struct VTermPen pen;
    153 
    154  VTermColor default_fg;
    155  VTermColor default_bg;
    156  VTermColor colors[16];  // Store the 8 ANSI and the 8 ANSI high-brights only
    157 
    158  int bold_is_highbright;
    159 
    160  unsigned protected_cell : 1;
    161 
    162 // Saved state under DEC mode 1048/1049
    163  struct {
    164    VTermPos pos;
    165    struct VTermPen pen;
    166 
    167    struct {
    168      unsigned cursor_visible:1;
    169      unsigned cursor_blink:1;
    170      unsigned cursor_shape:2;
    171    } mode;
    172  } saved;
    173 
    174 // Temporary state for DECRQSS parsing
    175  union {
    176    char decrqss[4];
    177    struct {
    178      uint16_t mask;
    179      enum {
    180        SELECTION_INITIAL,
    181        SELECTION_SELECTED,
    182        SELECTION_QUERY,
    183        SELECTION_SET_INITIAL,
    184        SELECTION_SET,
    185        SELECTION_INVALID,
    186      } state : 8;
    187      uint32_t recvpartial;
    188      uint32_t sendpartial;
    189    } selection;
    190  } tmp;
    191 
    192  struct {
    193    const VTermSelectionCallbacks *callbacks;
    194    void *user;
    195    char *buffer;
    196    size_t buflen;
    197  } selection;
    198 
    199  // Maintain two stacks, one for primary screen and one for altscreen
    200  struct VTermKeyEncodingStack key_encoding_stacks[2];
    201 };
    202 
    203 struct VTerm {
    204  const VTermAllocatorFunctions *allocator;
    205  void *allocdata;
    206 
    207  int rows;
    208  int cols;
    209 
    210  struct {
    211    unsigned utf8:1;
    212    unsigned ctrl8bit:1;
    213  } mode;
    214 
    215  struct {
    216    enum VTermParserState {
    217      NORMAL,
    218      CSI_LEADER,
    219      CSI_ARGS,
    220      CSI_INTERMED,
    221      DCS_COMMAND,
    222      // below here are the "string states"
    223      OSC_COMMAND,
    224      OSC,
    225      DCS_VTERM,
    226      APC,
    227      PM,
    228      SOS,
    229    } state;
    230 
    231    bool in_esc : 1;
    232 
    233    int intermedlen;
    234    char intermed[INTERMED_MAX];
    235 
    236    union {
    237      struct {
    238        int leaderlen;
    239        char leader[CSI_LEADER_MAX];
    240 
    241        int argi;
    242        long args[CSI_ARGS_MAX];
    243      } csi;
    244      struct {
    245        int command;
    246      } osc;
    247      struct {
    248        int commandlen;
    249        char command[CSI_LEADER_MAX];
    250      } dcs;
    251    } v;
    252 
    253    const VTermParserCallbacks *callbacks;
    254    void *cbdata;
    255 
    256    bool string_initial;
    257 
    258    bool emit_nul;
    259  } parser;
    260 
    261  // len == malloc()ed size; cur == number of valid bytes
    262 
    263  VTermOutputCallback *outfunc;
    264  void *outdata;
    265 
    266  char *outbuffer;
    267  size_t outbuffer_len;
    268  size_t outbuffer_cur;
    269 
    270  char *tmpbuffer;
    271  size_t tmpbuffer_len;
    272 
    273  VTermState *state;
    274  VTermScreen *screen;
    275 };
    276 
    277 struct VTermEncoding {
    278  void (*init)(VTermEncoding *enc, void *data);
    279  void (*decode)(VTermEncoding *enc, void *data, uint32_t cp[], int *cpi, int cplen,
    280                 const char bytes[], size_t *pos, size_t len);
    281 };
    282 
    283 typedef enum {
    284  ENC_UTF8,
    285  ENC_SINGLE_94,
    286 } VTermEncodingType;
    287 
    288 enum {
    289  C1_SS3 = 0x8f,
    290  C1_DCS = 0x90,
    291  C1_CSI = 0x9b,
    292  C1_ST  = 0x9c,
    293  C1_OSC = 0x9d,
    294 };