config

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

vterm-module.h (5689B)


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