From 0fe5d5cb9d8fad984892c14f1e047677f3c67e38 Mon Sep 17 00:00:00 2001 From: Akib Azmain Turja Date: Mon, 18 Sep 2023 15:08:32 +0600 Subject: [PATCH] Allow customizing Sixel rendering format * eat.el (eat-sixel-render-formats): New user option. * eat.el (eat-eshell-fallback-if-stty-not-available): Fix customization type. * eat.el (eat--t-term): Rename slot 'sixel-display-method' to 'sixel-render-format'. All callers changed. * eat.el (eat--t-sixel-flush-line): New support render format, 'none'. * eat.el (eat-term-set-parameter): Rename parameter 'sixel-display-method' to 'sixel-render-format'. All callers changed. * eat.el (eat--sixel-render-format): New function. * eat.el (eat-exec, eat--eshell-setup-proc-and-term): Set 'sixel-render-format' according to user customization. --- eat.el | 61 +++++++++++++++++++++++++++++++++++++------------------- eat.texi | 22 ++++++++++++++++++++ 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/eat.el b/eat.el index c84be77..cbd806c 100644 --- a/eat.el +++ b/eat.el @@ -176,7 +176,19 @@ t Fallback to plain Eshell if `stty' is not available. FUNCTION Call FUNCTION with the command and arguments (using `apply') and fallback to plain Eshell if it returns nil." - :type 'boolean + :type '(radio (const :tag "Do nothing" nil) + (const :tag "Fallback to plain Eshell" t) + (const :tag "Ask interactively" ask) + (function :tag "Function")) + :group 'eat-eshell) + +(defcustom eat-sixel-render-formats '(svg half-block background none) + "List of formats to render Sixel, in order of preference." + :type '(repeat (choice (const :tag "SVG Image" svg) + (const :tag "UTF-8 half block" half-block) + (const :tag "Background color" background) + (const :tag "None" none))) + :group 'eat-ui :group 'eat-eshell) (defcustom eat-semi-char-non-bound-keys @@ -1112,12 +1124,12 @@ Nil when not in alternative display mode.") (copy-sequence (make-vector 256 nil)) :documentation "Sixel color registers.") (sixel-color 0 :documentation "Current Sixel color register.") - (sixel-display-method + (sixel-render-format 'background - :documentation "Method to display renders Sixel image.") + :documentation "Format to render Sixel images in.") (sixel-image-height nil - :documentation "Height of images used to display Sixels.") + :documentation "Height of images used to display Sixel.") (sixel-scroll-mode t :documentation "Whether to auto-scroll.") (sixel-initial-cursor-pos '(1 . 1) @@ -3004,7 +3016,8 @@ If NULLIFY is non-nil, nullify flushed part of Sixel buffer." (cl-incf i) (cl-incf j)) (pcase-exhaustive - (eat--t-term-sixel-display-method eat--t-term) + (eat--t-term-sixel-render-format eat--t-term) + ('none) ('background (when-let* ((color (aref (aref bitmap 0) 0))) (put-text-property (point) (1+ (point)) 'face @@ -3965,11 +3978,11 @@ If NULLIFY is non-nil, nullify flushed part of Sixel buffer." (signal 'wrong-type-argument (list 'consp value))) (setf (eat--t-term-char-width terminal) (car value)) (setf (eat--t-term-char-height terminal) (cdr value))) - ('sixel-display-method - (unless (memq value '(background half-block svg)) - (error "`sixel-display-method' parameter must be set to one of\ + ('sixel-render-format + (unless (memq value '(background half-block svg none)) + (error "`sixel-render-format' parameter must be set to one of\ the supported methods")) - (setf (eat--t-term-sixel-display-method terminal) value)) + (setf (eat--t-term-sixel-render-format terminal) value)) ('sixel-image-height (setf (eat--t-term-sixel-image-height terminal) value))) ;; Set the parameter. @@ -5024,6 +5037,20 @@ selection, or nil if none." "Ring the bell." (ding t)) +(defun eat--sixel-render-format () + "Return the suitable Sixel render format." + (cl-block nil + (dolist (fmt eat-sixel-render-formats) + (pcase-exhaustive fmt + ('none (cl-return 'none)) + ('background (cl-return 'background)) + ('half-block (when (char-displayable-p ?▄) + (cl-return 'half-block))) + ('svg (when (and (display-graphic-p) + (image-type-available-p 'svg)) + (cl-return 'svg))))) + 'none)) + (defun eat--set-cwd (_ host cwd) "Set CWD as the current working directory (`default-directory'). @@ -6606,12 +6633,8 @@ same Eat buffer. The hook `eat-exec-hook' is run after each exec." (setf (eat-term-set-cwd-function eat--terminal) #'eat--set-cwd) (setf (eat-term-parameter eat--terminal 'ui-command-function) #'eat--handle-uic) - (setf (eat-term-parameter eat--terminal 'sixel-display-method) - (cond ((and (display-graphic-p) - (image-type-available-p 'svg)) - 'svg) - ((char-displayable-p ?▄) 'half-block) - (t 'background))) + (setf (eat-term-parameter eat--terminal 'sixel-render-format) + (eat--sixel-render-format)) (when (display-graphic-p) (setf (eat-term-parameter eat--terminal 'sixel-image-height) (cons (/ (float (default-font-height)) @@ -6984,12 +7007,8 @@ PROGRAM can be a shell command." (setf (eat-term-set-cwd-function eat--terminal) #'eat--set-cwd) (setf (eat-term-parameter eat--terminal 'ui-command-function) #'eat--eshell-handle-uic) - (setf (eat-term-parameter eat--terminal 'sixel-display-method) - (cond ((and (display-graphic-p) - (image-type-available-p 'svg)) - 'svg) - ((char-displayable-p ?▄) 'half-block) - (t 'background))) + (setf (eat-term-parameter eat--terminal 'sixel-render-format) + (eat--sixel-render-format)) (when (display-graphic-p) (setf (eat-term-parameter eat--terminal 'sixel-image-height) (cons (/ (float (default-font-height)) diff --git a/eat.texi b/eat.texi index bfe36d9..212948b 100644 --- a/eat.texi +++ b/eat.texi @@ -908,6 +908,28 @@ and @var{n} can be any number between 0 and 9 (inclusive). For example, the font 6 is can be changed by customizing @code{eat-term-font-6}. Font 0 is the default font. +@node Sixel +@cindex sixel +@chapter Sixel + +Eat can show Sixel graphics. Sixel is a bitmap graphics format that +can be used to display graphics in a terminal, for example, images, or +plotting graphs. + +Eat converts Sixel graphics to an image format Emacs can natively +display. This preference of image formats can be configured by +customizing the @code{eat-sixel-render-formats} user option. + +@vindex eat-sixel-render-formats +@defopt eat-sixel-render-formats +List of formats to render Sixel, in order of preference. Each element +of the list is one of @code{svg}, @code{half-block}, +@code{background}, @code{none}. @code{svg} means to use SVG image +format, @code{half-block} means to use UTF-8 half block characters, +@code{background} means to just use background color, and @code{none} +means to not render the image, instead just clear the area. +@end defopt + @node Blinking Text @cindex blinking text @cindex text, blinking