diff options
| author | Lukas Fürmetz <fuermetz@mailbox.org> | 2019-07-30 16:13:46 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-30 16:13:46 +0200 |
| commit | 4d34280d6001faf8c4035c51083f94f7f0bb3620 (patch) | |
| tree | 440c633106b59c7b09f69edb83fd874d050c2537 | |
| parent | 40f39427413841dc716e1d309ed36cf67d6dcb06 (diff) | |
| parent | f3e68ea809adb3df1e7bb4cd95f78bc913102854 (diff) | |
Merge pull request #82 from jixiuf/support-256color
Support 256color
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | README.md | 27 | ||||
| -rw-r--r-- | elisp.c | 12 | ||||
| -rw-r--r-- | elisp.h | 6 | ||||
| -rw-r--r-- | vterm-module.c | 26 | ||||
| -rw-r--r-- | vterm-module.h | 10 | ||||
| -rw-r--r-- | vterm.el | 110 |
7 files changed, 134 insertions, 59 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 281c067..3d45441 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,7 +34,7 @@ if(USE_SYSTEM_LIBVTERM) else() ExternalProject_add(libvterm GIT_REPOSITORY https://github.com/neovim/libvterm.git - GIT_TAG a6293a0e033e7e86c74889b4527787993656883a + GIT_TAG 89675ffdda615ffc3f29d1c47a933f4f44183364 CONFIGURE_COMMAND "" BUILD_COMMAND make "CFLAGS='-fPIC'" BUILD_IN_SOURCE ON @@ -77,22 +77,17 @@ bound to `C-c C-c`. Set the `:foreground` and `:background` attributes of the following faces to a color you like: -- vterm-color-default-fg -- vterm-color-default-bg -- vterm-color-black-fg -- vterm-color-black-bg -- vterm-color-red-fg -- vterm-color-green-bg -- vterm-color-green-fg -- vterm-color-yellow-bg -- vterm-color-blue-fg -- vterm-color-blue-bg -- vterm-color-magenta-fg -- vterm-color-magenta-bg -- vterm-color-cyan-fg -- vterm-color-cyan-bg -- vterm-color-white-fg -- vterm-color-white-bg +- vterm-color-default +- vterm-color-black +- vterm-color-black +- vterm-color-red +- vterm-color-green +- vterm-color-yellow +- vterm-color-blue +- vterm-color-magenta +- vterm-color-cyan +- vterm-color-white + ## Related packages @@ -109,16 +109,10 @@ void set_cursor_type(emacs_env *env, emacs_value QCursorType) { } -emacs_value get_hex_color_fg(emacs_env *env, emacs_value face) { - return env->funcall(env, Fvterm_face_color_hex, 2, - (emacs_value[]){face, Qforeground}); +emacs_value vterm_get_color(emacs_env *env, int index) { + emacs_value idx = env->make_integer(env, index); + return env->funcall(env, Fvterm_get_color, 1, (emacs_value[]){idx}); } - -emacs_value get_hex_color_bg(emacs_env *env, emacs_value face) { - return env->funcall(env, Fvterm_face_color_hex, 2, - (emacs_value[]){face, Qbackground}); -} - void set_title(emacs_env *env, emacs_value string) { env->funcall(env, Fvterm_set_title, 1, (emacs_value[]){string}); } @@ -22,7 +22,6 @@ emacs_value Qbox; emacs_value Qbar; emacs_value Qhbar; emacs_value Qcursor_type; -emacs_value Qansi_color_names_vector; // Emacs functions emacs_value Fsymbol_value; @@ -42,13 +41,13 @@ emacs_value Fpoint; emacs_value Fput_text_property; emacs_value Fset; -emacs_value Fvterm_face_color_hex; emacs_value Fvterm_flush_output; emacs_value Fget_buffer_window; emacs_value Fselected_window; emacs_value Fvterm_set_title; emacs_value Fvterm_invalidate; emacs_value Feq; +emacs_value Fvterm_get_color; // Utils void bind_function(emacs_env *env, const char *name, emacs_value Sfun); @@ -66,8 +65,6 @@ void forward_line(emacs_env *env, int n); void goto_line(emacs_env *env, int n); void set_cursor_type(emacs_env *env, emacs_value cursor_type); void delete_lines(emacs_env *env, int linenum, int count, bool del_whole_line); -emacs_value get_hex_color_fg(emacs_env *env, emacs_value face); -emacs_value get_hex_color_bg(emacs_env *env, emacs_value face); emacs_value buffer_line_number(emacs_env *env); void recenter(emacs_env *env, emacs_value pos); void set_window_point(emacs_env *env, emacs_value win, emacs_value point); @@ -78,5 +75,6 @@ emacs_value get_buffer_window(emacs_env *env); emacs_value selected_window(emacs_env *env); void set_title(emacs_env *env, emacs_value string); void vterm_invalidate(emacs_env *env); +emacs_value vterm_get_color(emacs_env *env, int index); #endif /* ELISP_H */ diff --git a/vterm-module.c b/vterm-module.c index 224ffcf..7d44293 100644 --- a/vterm-module.c +++ b/vterm-module.c @@ -491,19 +491,21 @@ static emacs_value render_text(emacs_env *env, Term *term, char *buffer, static emacs_value color_to_rgb_string(emacs_env *env, Term *term, VTermColor *color) { - emacs_value palette = symbol_value(env, Qansi_color_names_vector); if (VTERM_COLOR_IS_DEFAULT_FG(color)) { - return env->vec_get(env, palette, 7); + return vterm_get_color(env, -1); } if (VTERM_COLOR_IS_DEFAULT_BG(color)) { - return env->vec_get(env, palette, 0); + return vterm_get_color(env, -2); } - if (VTERM_COLOR_IS_INDEXED(color) < 16) { - return env->vec_get(env, palette, color->indexed.idx % 8); - } - if (VTERM_COLOR_IS_INDEXED(color) >= 16) { - VTermState *state = vterm_obtain_state(term->vt); - vterm_state_get_palette_color(state, color->indexed.idx, color); + if (VTERM_COLOR_IS_INDEXED(color)) { + if (color->indexed.idx < 16) { + return vterm_get_color(env, color->indexed.idx); + } else { + VTermState *state = vterm_obtain_state(term->vt); + vterm_state_get_palette_color(state, color->indexed.idx, color); + } + } else if (VTERM_COLOR_IS_RGB(color)) { + /* do nothing just use the argument color directly */ } char buffer[8]; @@ -733,8 +735,6 @@ int emacs_module_init(struct emacs_runtime *ert) { Qbar = env->make_global_ref(env, env->intern(env, "bar")); Qhbar = env->make_global_ref(env, env->intern(env, "hbar")); Qcursor_type = env->make_global_ref(env, env->intern(env, "cursor-type")); - Qansi_color_names_vector = - env->make_global_ref(env, env->intern(env, "ansi-color-names-vector")); // Functions Fsymbol_value = env->make_global_ref(env, env->intern(env, "symbol-value")); @@ -746,8 +746,6 @@ int emacs_module_init(struct emacs_runtime *ert) { Fput_text_property = env->make_global_ref(env, env->intern(env, "put-text-property")); Fset = env->make_global_ref(env, env->intern(env, "set")); - Fvterm_face_color_hex = - env->make_global_ref(env, env->intern(env, "vterm--face-color-hex")); Fvterm_flush_output = env->make_global_ref(env, env->intern(env, "vterm--flush-output")); Fforward_line = env->make_global_ref(env, env->intern(env, "forward-line")); @@ -771,6 +769,8 @@ int emacs_module_init(struct emacs_runtime *ert) { Fvterm_invalidate = env->make_global_ref(env, env->intern(env, "vterm--invalidate")); Feq = env->make_global_ref(env, env->intern(env, "eq")); + Fvterm_get_color = + env->make_global_ref(env, env->intern(env, "vterm--get-color")); // Exported functions emacs_value fun; diff --git a/vterm-module.h b/vterm-module.h index 9ed1831..356a6dd 100644 --- a/vterm-module.h +++ b/vterm-module.h @@ -8,7 +8,6 @@ int plugin_is_GPL_compatible; - #define SB_MAX 100000 // Maximum 'scrollback' value. #ifndef MIN @@ -64,11 +63,10 @@ typedef struct Term { static bool compare_cells(VTermScreenCell *a, VTermScreenCell *b); static bool is_key(unsigned char *key, size_t len, char *key_description); -static emacs_value render_text(emacs_env *env, Term *term, char *string, int len, - VTermScreenCell *cell); -static emacs_value cell_to_face(emacs_env *env, Term *term, VTermScreenCell *cell); -static emacs_value color_to_face(emacs_env *env, VTermColor *color, emacs_value palette); -static emacs_value color_to_rgb_string(emacs_env *env, Term *term, VTermColor *color); +static emacs_value render_text(emacs_env *env, Term *term, char *string, + int len, VTermScreenCell *cell); +static emacs_value color_to_rgb_string(emacs_env *env, Term *term, + VTermColor *color); static int term_settermprop(VTermProp prop, VTermValue *val, void *user_data); @@ -42,7 +42,7 @@ ;;; Code: -(require 'ansi-color) +(require 'term) (defvar vterm-install-buffer-name " *Install vterm" "Name of the buffer used for compiling vterm-module.") @@ -120,12 +120,93 @@ for different shell" :type 'hook :group 'vterm) +(defface vterm-color-default + `((t :inherit default)) + "The default normal color and bright color. +the foreground color are used for normal color, +and background color are used for bright color. " + :group 'vterm) + +(defface vterm-color-black + `((t :inherit term-color-black)) + "Face used to render black color code. +the foreground color are used for normal color, +and background color are used for bright color. " + :group 'vterm) + +(defface vterm-color-red + `((t :inherit term-color-red)) + "Face used to render red color code. +the foreground color are used for normal color, +and background color are used for bright color. " + :group 'vterm) + +(defface vterm-color-green + `((t :inherit term-color-green)) + "Face used to render green color code. +the foreground color are used for normal color, +and background color are used for bright color. " + :group 'vterm) + +(defface vterm-color-yellow + `((t :inherit term-color-yellow)) + "Face used to render yellow color code. +the foreground color are used for normal color, +and background color are used for bright color. " + :group 'vterm) + +(defface vterm-color-blue + `((t :inherit term-color-blue)) + "Face used to render blue color code. +the foreground color are used for normal color, +and background color are used for bright color. " + :group 'vterm) + +(defface vterm-color-magenta + `((t :inherit term-color-magenta)) + "Face used to render magenta color code. +the foreground color are used for normal color, +and background color are used for bright color. " + :group 'vterm) + +(defface vterm-color-cyan + `((t :inherit term-color-cyan)) + "Face used to render cyan color code. +the foreground color are used for normal color, +and background color are used for bright color. " + :group 'vterm) + +(defface vterm-color-white + `((t :inherit term-color-white)) + "Face used to render white color code. +the foreground color are used for normal color, +and background color are used for bright color. " + :group 'vterm) + +(defvar vterm-color-palette + [vterm-color-black + vterm-color-red + vterm-color-green + vterm-color-yellow + vterm-color-blue + vterm-color-magenta + vterm-color-cyan + vterm-color-white] + "Color palette for the foreground and background.") + (defvar-local vterm--term nil "Pointer to Term.") (defvar-local vterm--process nil "Shell process of current term.") +(defvar-local vterm--redraw-timer nil) + +(defvar vterm-timer-delay 0.01 + "Delay for refreshing the buffer after receiving updates from libvterm. +Improves performance when receiving large bursts of data. +If nil, never delay") + (define-derived-mode vterm-mode fundamental-mode "VTerm" "Major mode for vterm buffer." (buffer-disable-undo) @@ -140,7 +221,7 @@ for different shell" (if (version< emacs-version "27") (add-hook 'window-size-change-functions #'vterm--window-size-change-26 t t) (add-hook 'window-size-change-functions #'vterm--window-size-change t t)) - (let ((process-environment (append '("TERM=xterm" + (let ((process-environment (append '("TERM=xterm-256color" "INSIDE_EMACS=vterm" "LINES" "COLUMNS") @@ -245,13 +326,6 @@ Optional argument PASTE-P paste-p." (when paste-p (vterm--update vterm--term "<end_paste>" nil nil nil)))) -(defvar-local vterm--redraw-timer nil) - -(defvar vterm-timer-delay 0.01 - "Delay for refreshing the buffer after receiving updates from libvterm. -Improves performance when receiving large bursts of data. -If nil, never delay") - (defun vterm--invalidate() "The terminal buffer is invalidated, the buffer needs redrawing." (if vterm-timer-delay @@ -307,7 +381,7 @@ Then triggers a redraw from the module." (vterm--write-input vterm--term input) (vterm--update vterm--term))))) -(defun vterm--sentinel (process event) +(defun vterm--sentinel (process _event) "Sentinel of vterm PROCESS. Argument EVENT process event." (let ((buf (process-buffer process))) @@ -361,6 +435,22 @@ Feeds the size change to the virtual terminal." "Run the `vterm--set-title-hook' with TITLE as argument." (run-hook-with-args 'vterm-set-title-functions title)) +(defun vterm--get-color(index) + "Get color by index from `vterm-color-palette'. +Argument INDEX index of color." + (cond + ((and (>= index 0)(< index 8 )) + (face-foreground + (elt vterm-color-palette index) + nil 'default)) + ((and (>= index 8 )(< index 16 )) + (face-background + (elt vterm-color-palette (% index 8)) + nil 'default)) + ( (= index -1) ;-1 foreground + (face-foreground 'vterm-color-default nil 'default)) + (t ;-2 background + (face-background 'vterm-color-default nil 'default)))) (provide 'vterm) ;;; vterm.el ends here |
