From 8798b229852669a021b268c13555469219ad007c Mon Sep 17 00:00:00 2001 From: Tim Vaughan Date: Tue, 21 May 2019 10:03:21 +0200 Subject: [PATCH] Add start and stop chars to sixel strings, skip unknown chars. --- sixel.el | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/sixel.el b/sixel.el index 6626e1d..81fa32e 100644 --- a/sixel.el +++ b/sixel.el @@ -14,15 +14,15 @@ ;;; Code: (defvar test-string - (concat "q" + (concat "Pq" "#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0" "#1~~@@vv@@~~@@~~$" "#2??}}GG}}??}}??-" - "#1!14@")) + "#1!14@\\")) (defun sixel-get-params (string) "Retrieve the sixel parameters." - (car (split-string string "q"))) + (car (split-string (substring string 2) "q"))) (defun sixel-get-data (string) "Retrieve data string." @@ -57,6 +57,7 @@ Returns a sixel image object." finished) (while (not finished) (cond + ;; Define colour: ((looking-at "#\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\)") (let ((tag (format "%02x" (string-to-number (match-string 1)))) (mode (match-string 2)) @@ -64,19 +65,19 @@ Returns a sixel image object." (g (string-to-number (match-string 4))) (b (string-to-number (match-string 5)))) (push (list tag r g b) colour-map))) - + ;; Set current colour: ((looking-at "#\\([0-9]+\\)") (let ((tag (format "%02x" (string-to-number (match-string 1))))) (setq current-colour tag))) - + ;; Carriage return: ((looking-at "\\$") (setq idx-out 0)) - + ;; New line: ((looking-at "-") (push (reverse this-row) rows) (setq this-row nil) (setq idx-out 0)) - + ;; RLE sixel char sequence: ((looking-at "!\\([0-9]+\\)\\([?-~]\\)") (let ((repeat-count (string-to-number (match-string 1))) (char (elt (match-string 2) 0))) @@ -84,15 +85,17 @@ Returns a sixel image object." (setq this-row (sixel-tag-sixel-in-row this-row idx-out char current-colour)) (setq idx-out (1+ idx-out))))) - - ((looking-at "\\([?-~]\\)") + ;; Sixel char: + ((looking-at "\\([?-~]\\)") ; Sixel char (let ((char (elt (match-string 1) 0))) (setq this-row (sixel-tag-sixel-in-row this-row idx-out char current-colour)) (setq idx-out (1+ idx-out)))) - - (t (setq finished t))) - + ;; Termination sequence: + ((looking-at "\\\\") + (setq finished t)) + ;; Skip other char: + ((looking-at "[[:ascii:]]"))) (goto-char (match-end 0))) (push (reverse this-row) rows) (cons colour-map @@ -172,11 +175,13 @@ of SIXEL-IMAGE." (let* ((param-string (sixel-get-params string)) (data-string (sixel-get-data string)) (sixel-image (sixel-process-data data-string))) - (concat - "/* XPM */" - "static char * pixmap[] = {" - (sixel-image-to-xpm-values sixel-image) ",\n" - (sixel-image-to-xpm-colours sixel-image) ",\n" - (sixel-image-to-xpm-pixels sixel-image) "};"))) + (if (string-prefix-p "P" string) + (concat + "/* XPM */" + "static char * pixmap[] = {" + (sixel-image-to-xpm-values sixel-image) ",\n" + (sixel-image-to-xpm-colours sixel-image) ",\n" + (sixel-image-to-xpm-pixels sixel-image) "};") + (error "Incorrecly formatted sixel string.")))) ;; sixel.el ends here -- 2.20.1