config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

vterm-module.h (5667B)


      1 #ifndef VTERM_MODULE_H
      2 #define VTERM_MODULE_H
      3 
      4 #include "emacs-module.h"
      5 #include <inttypes.h>
      6 #include <stdbool.h>
      7 #include <vterm.h>
      8 
      9 // https://gcc.gnu.org/wiki/Visibility
     10 #if defined _WIN32 || defined __CYGWIN__
     11 #ifdef __GNUC__
     12 #define VTERM_EXPORT __attribute__((dllexport))
     13 #else
     14 #define VTERM_EXPORT __declspec(dllexport)
     15 #endif
     16 #else
     17 #if __GNUC__ >= 4
     18 #define VTERM_EXPORT __attribute__((visibility("default")))
     19 #else
     20 #define VTERM_EXPORT
     21 #endif
     22 #endif
     23 
     24 VTERM_EXPORT int plugin_is_GPL_compatible;
     25 
     26 #define SB_MAX 100000 // Maximum 'scrollback' value.
     27 
     28 #ifndef MIN
     29 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
     30 #endif
     31 #ifndef MAX
     32 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
     33 #endif
     34 
     35 typedef struct LineInfo {
     36   char *directory; /* working directory */
     37 
     38   int prompt_col; /* end column of the prompt, if the current line contains the
     39                    * prompt */
     40 } LineInfo;
     41 
     42 typedef struct ScrollbackLine {
     43   size_t cols;
     44   LineInfo *info;
     45   VTermScreenCell cells[];
     46 } ScrollbackLine;
     47 
     48 typedef struct ElispCodeListNode {
     49   char *code;
     50   size_t code_len;
     51   struct ElispCodeListNode *next;
     52 } ElispCodeListNode;
     53 
     54 /*  c , p , q , s , 0 , 1 , 2 , 3 , 4 , 5 , 6 , and 7  */
     55 /* clipboard, primary, secondary, select, or cut buffers 0 through 7 */
     56 #define SELECTION_BUF_LEN 4096
     57 
     58 typedef struct Cursor {
     59   int row, col;
     60   int cursor_type;
     61   bool cursor_visible;
     62   bool cursor_blink;
     63   bool cursor_type_changed;
     64   bool cursor_blink_changed;
     65 } Cursor;
     66 
     67 typedef struct Term {
     68   VTerm *vt;
     69   VTermScreen *vts;
     70   // buffer used to:
     71   //  - convert VTermScreen cell arrays into utf8 strings
     72   //  - receive data from libvterm as a result of key presses.
     73   ScrollbackLine **sb_buffer; // Scrollback buffer storage for libvterm
     74   size_t sb_current;          // number of rows pushed to sb_buffer
     75   size_t sb_size;             // sb_buffer size
     76   // "virtual index" that points to the first sb_buffer row that we need to
     77   // push to the terminal buffer when refreshing the scrollback. When negative,
     78   // it actually points to entries that are no longer in sb_buffer (because the
     79   // window height has increased) and must be deleted from the terminal buffer
     80   int sb_pending;
     81   int sb_pending_by_height_decr;
     82   bool sb_clear_pending;
     83   long linenum;
     84   long linenum_added;
     85 
     86   int invalid_start, invalid_end; // invalid rows in libvterm screen
     87   bool is_invalidated;
     88 
     89   Cursor cursor;
     90   char *title;
     91   bool title_changed;
     92 
     93   char *directory;
     94   bool directory_changed;
     95 
     96   // Single-linked list of elisp_code.
     97   // Newer commands are added at the tail.
     98   ElispCodeListNode *elisp_code_first;
     99   ElispCodeListNode **elisp_code_p_insert; // pointer to the position where new
    100                                            // node should be inserted
    101 
    102   /*  c , p , q , s , 0 , 1 , 2 , 3 , 4 , 5 , 6 , and 7  */
    103   /* clipboard, primary, secondary, select, or cut buffers 0 through 7 */
    104   int selection_mask; /* see VTermSelectionMask in vterm.h */
    105   char *selection_data;
    106   char selection_buf[SELECTION_BUF_LEN];
    107 
    108   /* the size of dirs almost = window height, value = directory of that line */
    109   LineInfo **lines;
    110   int lines_len;
    111 
    112   int width, height;
    113   int height_resize;
    114   bool resizing;
    115   bool disable_bold_font;
    116   bool disable_underline;
    117   bool disable_inverse_video;
    118   bool ignore_blink_cursor;
    119 
    120   char *cmd_buffer;
    121 
    122   int pty_fd;
    123 } Term;
    124 
    125 static bool compare_cells(VTermScreenCell *a, VTermScreenCell *b);
    126 static bool is_key(unsigned char *key, size_t len, char *key_description);
    127 static emacs_value render_text(emacs_env *env, Term *term, char *string,
    128                                int len, VTermScreenCell *cell);
    129 static emacs_value render_fake_newline(emacs_env *env, Term *term);
    130 static emacs_value render_prompt(emacs_env *env, emacs_value text);
    131 static emacs_value cell_rgb_color(emacs_env *env, Term *term,
    132                                   VTermScreenCell *cell, bool is_foreground);
    133 
    134 static int term_settermprop(VTermProp prop, VTermValue *val, void *user_data);
    135 
    136 static void term_redraw(Term *term, emacs_env *env);
    137 static void term_flush_output(Term *term, emacs_env *env);
    138 static void term_process_key(Term *term, emacs_env *env, unsigned char *key,
    139                              size_t len, VTermModifier modifier);
    140 static void invalidate_terminal(Term *term, int start_row, int end_row);
    141 
    142 void term_finalize(void *object);
    143 
    144 emacs_value Fvterm_new(emacs_env *env, ptrdiff_t nargs, emacs_value args[],
    145                        void *data);
    146 emacs_value Fvterm_update(emacs_env *env, ptrdiff_t nargs, emacs_value args[],
    147                           void *data);
    148 emacs_value Fvterm_redraw(emacs_env *env, ptrdiff_t nargs, emacs_value args[],
    149                           void *data);
    150 emacs_value Fvterm_write_input(emacs_env *env, ptrdiff_t nargs,
    151                                emacs_value args[], void *data);
    152 emacs_value Fvterm_set_size(emacs_env *env, ptrdiff_t nargs, emacs_value args[],
    153                             void *data);
    154 emacs_value Fvterm_set_pty_name(emacs_env *env, ptrdiff_t nargs,
    155                                 emacs_value args[], void *data);
    156 emacs_value Fvterm_get_icrnl(emacs_env *env, ptrdiff_t nargs,
    157                              emacs_value args[], void *data);
    158 
    159 emacs_value Fvterm_get_pwd(emacs_env *env, ptrdiff_t nargs, emacs_value args[],
    160                            void *data);
    161 
    162 emacs_value Fvterm_get_prompt_point(emacs_env *env, ptrdiff_t nargs,
    163                                     emacs_value args[], void *data);
    164 emacs_value Fvterm_reset_cursor_point(emacs_env *env, ptrdiff_t nargs,
    165                                       emacs_value args[], void *data);
    166 
    167 VTERM_EXPORT int emacs_module_init(struct emacs_runtime *ert);
    168 
    169 #endif /* VTERM_MODULE_H */