dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

vterm-module.h (5618B)


      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_TARGET_MAX 12
     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   long linenum;
     83   long linenum_added;
     84 
     85   int invalid_start, invalid_end; // invalid rows in libvterm screen
     86   bool is_invalidated;
     87 
     88   Cursor cursor;
     89   char *title;
     90   bool title_changed;
     91 
     92   char *directory;
     93   bool directory_changed;
     94 
     95   // Single-linked list of elisp_code.
     96   // Newer commands are added at the tail.
     97   ElispCodeListNode *elisp_code_first;
     98   ElispCodeListNode **elisp_code_p_insert; // pointer to the position where new
     99                                            // node should be inserted
    100 
    101   /*  c , p , q , s , 0 , 1 , 2 , 3 , 4 , 5 , 6 , and 7  */
    102   /* clipboard, primary, secondary, select, or cut buffers 0 through 7 */
    103   char selection_target[SELECTION_TARGET_MAX];
    104   char *selection_data;
    105 
    106   /* the size of dirs almost = window height, value = directory of that line */
    107   LineInfo **lines;
    108   int lines_len;
    109 
    110   int width, height;
    111   int height_resize;
    112   bool resizing;
    113   bool disable_bold_font;
    114   bool disable_underline;
    115   bool disable_inverse_video;
    116   bool ignore_blink_cursor;
    117 
    118   char *cmd_buffer;
    119 
    120   int pty_fd;
    121 } Term;
    122 
    123 static bool compare_cells(VTermScreenCell *a, VTermScreenCell *b);
    124 static bool is_key(unsigned char *key, size_t len, char *key_description);
    125 static emacs_value render_text(emacs_env *env, Term *term, char *string,
    126                                int len, VTermScreenCell *cell);
    127 static emacs_value render_fake_newline(emacs_env *env, Term *term);
    128 static emacs_value render_prompt(emacs_env *env, emacs_value text);
    129 static emacs_value cell_rgb_color(emacs_env *env, Term *term,
    130                                   VTermScreenCell *cell, bool is_foreground);
    131 
    132 static int term_settermprop(VTermProp prop, VTermValue *val, void *user_data);
    133 
    134 static void term_redraw(Term *term, emacs_env *env);
    135 static void term_flush_output(Term *term, emacs_env *env);
    136 static void term_process_key(Term *term, emacs_env *env, unsigned char *key,
    137                              size_t len, VTermModifier modifier);
    138 static void invalidate_terminal(Term *term, int start_row, int end_row);
    139 
    140 void term_finalize(void *object);
    141 
    142 emacs_value Fvterm_new(emacs_env *env, ptrdiff_t nargs, emacs_value args[],
    143                        void *data);
    144 emacs_value Fvterm_update(emacs_env *env, ptrdiff_t nargs, emacs_value args[],
    145                           void *data);
    146 emacs_value Fvterm_redraw(emacs_env *env, ptrdiff_t nargs, emacs_value args[],
    147                           void *data);
    148 emacs_value Fvterm_write_input(emacs_env *env, ptrdiff_t nargs,
    149                                emacs_value args[], void *data);
    150 emacs_value Fvterm_set_size(emacs_env *env, ptrdiff_t nargs, emacs_value args[],
    151                             void *data);
    152 emacs_value Fvterm_set_pty_name(emacs_env *env, ptrdiff_t nargs,
    153                                 emacs_value args[], void *data);
    154 emacs_value Fvterm_get_icrnl(emacs_env *env, ptrdiff_t nargs,
    155                              emacs_value args[], void *data);
    156 
    157 emacs_value Fvterm_get_pwd(emacs_env *env, ptrdiff_t nargs, emacs_value args[],
    158                            void *data);
    159 
    160 emacs_value Fvterm_get_prompt_point(emacs_env *env, ptrdiff_t nargs,
    161                                     emacs_value args[], void *data);
    162 emacs_value Fvterm_reset_cursor_point(emacs_env *env, ptrdiff_t nargs,
    163                                       emacs_value args[], void *data);
    164 
    165 VTERM_EXPORT int emacs_module_init(struct emacs_runtime *ert);
    166 
    167 #endif /* VTERM_MODULE_H */