|
| 1 | +;;; haskell-svg.el --- SVG Rendering -*- lexical-binding: t -*- |
| 2 | + |
| 3 | +;; Copyright (c) 2018 Federico Beffa. All rights reserved. |
| 4 | + |
| 5 | +;; This file is free software; you can redistribute it and/or modify |
| 6 | +;; it under the terms of the GNU General Public License as published by |
| 7 | +;; the Free Software Foundation; either version 3, or (at your option) |
| 8 | +;; any later version. |
| 9 | + |
| 10 | +;; This file is distributed in the hope that it will be useful, |
| 11 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +;; GNU General Public License for more details. |
| 14 | + |
| 15 | +;; You should have received a copy of the GNU General Public License |
| 16 | +;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +;;; Code: |
| 19 | + |
| 20 | +(defcustom haskell-svg-render-images nil |
| 21 | + "Replace SVG image text with actual images." |
| 22 | + :group 'haskell-interactive |
| 23 | + :type 'boolean) |
| 24 | + |
| 25 | +(defconst haskell-svg-supported (image-type-available-p 'svg) |
| 26 | + "Defines if SVG images are supported by this instance of Emacs.") |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +(defun haskell-svg-render-images-p () |
| 31 | + "Shall we render SVG images?" |
| 32 | + (and haskell-svg-supported (display-images-p) haskell-svg-render-images)) |
| 33 | + |
| 34 | +(defun haskell-svg-maybe-render-images (text) |
| 35 | + "Render SVG images if desired and supported, or terurn the |
| 36 | +input unmodified." |
| 37 | + (if (haskell-svg-render-images-p) |
| 38 | + (haskell-svg-render-images text) |
| 39 | + text)) |
| 40 | + |
| 41 | +(defun haskell-svg-render-images (text) |
| 42 | + "Replace an SVG image text with an actual image." |
| 43 | + (with-temp-buffer |
| 44 | + (insert text) |
| 45 | + (goto-char (point-min)) |
| 46 | + (when (re-search-forward |
| 47 | + "\"?<\\?xml\\(.\\|\n\\|\r\\)* PUBLIC \"-//W3C//DTD SVG [0-9]\.[0-9]//EN\\(.\\|\n\\|\r\\)*</svg>\"?" |
| 48 | + nil t) |
| 49 | + (let ((svg-string (match-string 0)) |
| 50 | + (begin (match-beginning 0)) |
| 51 | + (end (match-end 0))) |
| 52 | + (delete-region begin end) |
| 53 | + (goto-char begin) |
| 54 | + (insert-image (create-image svg-string nil t) "SVG image"))) |
| 55 | + (buffer-substring (point-min) (point-max)))) |
| 56 | + |
| 57 | +(defun haskell-svg-toggle-render-images () |
| 58 | + "Toggle rendering of SVG images at the REPL output." |
| 59 | + (interactive) |
| 60 | + (setq haskell-svg-render-images (not haskell-svg-render-images))) |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +(provide 'haskell-svg) |
| 65 | + |
| 66 | +;;; haskell-svg.el ends here |
0 commit comments