X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?p=sixel.git;a=blobdiff_plain;f=sixel.el;h=87d387d728f317615747c1a904652ce36f2cc346;hp=053739d300c36026541775c1539844050c9544a0;hb=HEAD;hpb=e3bfe837fe1c04c4319ebb57d70bd641f7906829 diff --git a/sixel.el b/sixel.el index 053739d..87d387d 100644 --- a/sixel.el +++ b/sixel.el @@ -2,15 +2,30 @@ ;; Copyright (C) 2019 Tim Vaughan -;; Author: Tim Vaughan +;; Author: Tim Vaughan ;; Created: 19 May 2019 ;; Version: 1.0.0 -;; Keywords: -;; Homepage: https://github.com/tgvaughan/sixel -;; Package-Requires: ((emacs "25")) +;; Keywords: +;; Homepage: gopher://thelambdalab.xyz/1/projects/sixel +;; Package-Requires: ((emacs "26")) ;;; Commentary: +;; This file is not part of GNU Emacs. + +;; This program is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this file. If not, see . + ;;; Code: (defvar test-string @@ -115,23 +130,22 @@ Returns a sixel image object." sixel-image) (defun sixel-image-colour-map (sixel-image) - "Extract colour map from SIXEL-DATA." + "Extract colour map from SIXEL-IMAGE." (car sixel-image)) (defun sixel-image-sixels (sixel-image) - "Extract sixels from SIXEL-DATA." + "Extract sixels from SIXEL-IMAGE." (cdr sixel-image)) (defun sixel-image-dims (sixel-image) - "Computes image width from SIXEL-DATA. Returns pair (width . height)." + "Compute image width from SIXEL-IMAGE. Return pair (width . height)." (let ((sixels (sixel-image-sixels sixel-image))) (cons (apply #'max (mapcar (lambda (row) (length row)) sixels)) (* 6 (length sixels))))) (defun sixel-image-to-xpm-values (sixel-image) - "Produce string representing parameter values component of XPM -representation of SIXEL-IMAGE." + "Produce parameter values component of XPM representation of SIXEL-IMAGE." (let* ((dims (sixel-image-dims sixel-image)) (colour-map (sixel-image-colour-map sixel-image)) (n-colours (1+ (length colour-map)))) @@ -141,8 +155,7 @@ representation of SIXEL-IMAGE." (number-to-string n-colours) " 2\""))) (defun sixel-image-to-xpm-colours (sixel-image) - "Produce string representing colour definitions component of XPM -representation of SIXEL-IMAGE." + "Produce colour definitions component of XPM representation of SIXEL-IMAGE." (let ((colour-map (sixel-image-colour-map sixel-image))) (concat (string-join @@ -162,8 +175,7 @@ representation of SIXEL-IMAGE." "\"-- c #000000\""))) (defun sixel-image-to-xpm-pixels (sixel-image) - "Produce string representating pixels component of XPM representation -of SIXEL-IMAGE." + "Produce pixels component of XPM representation of SIXEL-IMAGE." (concat "\"" (string-join @@ -184,7 +196,7 @@ of SIXEL-IMAGE." "\"")) (defun sixel-to-xpm (string) - "Returns an XPM image representation of the SIXEL graphic encoded in STRING." + "Return an XPM image representation of the SIXEL graphic encoded in STRING." (let* ((param-string (sixel-get-params string)) (data-string (sixel-get-data string)) (sixel-image (sixel-pad-rows (sixel-process-data data-string)))) @@ -195,6 +207,32 @@ of SIXEL-IMAGE." (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.")))) + (error "Incorrecly formatted sixel string")))) -;; sixel.el ends here +(defun sixel-render-images-in-buffer () + "Find and render any sixel images in the current buffer." + (interactive) + (save-excursion + (goto-char (point-min)) + (while (re-search-forward "P[[:ascii:]]*\\\\" nil t) + (let ((sixel-string (match-string 0)) + (inhibit-read-only t)) + (delete-region (match-beginning 0) + (match-end 0)) + (insert-image + (create-image (sixel-to-xpm sixel-string) 'xpm t)) + (insert "\n"))))) + +(defgroup sixel nil + "Render sixel images." + :group 'multimedia) + +(define-minor-mode sixel-mode + "A minor mode which renders sixel graphics." nil "sixel" nil + (add-hook 'after-change-functions + (lambda (start end size) + (sixel-render-images-in-buffer) + (message "Render complete.")) + nil t)) + +;;; sixel.el ends here