by spdegabrielle on 12/27/24, 12:00 PM with 9 comments
by neilv on 12/29/24, 8:06 PM
In other languages, all you'd want is the byte string (maybe BASE64-encoded, maybe with byte escape sequences), and you wouldn't need a fancy reader extension: just put a byte string literal.
A variation on the byte string literal you might see is when people create a minilanguage out of strings. For example, if you want to represent a 2x pixel map image with a fairly small color map... you might just have a string literal of ASCII characters for each row, where each ASCII character maps to an RGB color (or transparent) that you specify, with a simple function to translate it to binary. Then it looks like ASCII art in your source code.
Vector images, OTOH, are more likely to have a textual language already (e.g., SVG), so of course you can just embed that as strings in your source file.
by perihelions on 12/29/24, 6:35 PM
(defun imagify-buffer ()
(interactive)
(font-lock-ensure)
(save-excursion
(goto-char (point-min))
(while
(re-search-forward
(rx "(%image \""
(group (1+ (or alphanumeric "+" "/" "=")))
"\")")
nil t)
(when (equal 'font-lock-comment-face
(get-char-property (match-beginning 0) 'face))
(let* ((replaced-string (match-string 0))
(base64 (match-string 1))
(image (create-image
(base64-decode-string base64)
nil t :scale 4.0)))
(goto-char (match-beginning 0))
(kill-region (match-beginning 0) (match-end 0))
(insert-image image replaced-string))))))
(defun insert-image-thingy (filename)
(interactive "f")
(let* ((s (with-temp-buffer
(set-buffer-multibyte nil)
(insert-file-contents-literally filename)
(buffer-substring-no-properties (point-min) (point-max))))
(base64 (base64-encode-string s t))
(thingy (format ";; (%%image \"%s\")" base64)))
(insert thingy)
(imagify-buffer)))
(add-hook 'emacs-lisp-mode-hook #'imagify-buffer)
(add-hook 'lisp-interaction-mode-hook #'imagify-buffer)
(imagify-buffer)
;; (%image "PHN2ZyBoZWlnaHQ9IjE4IiB2aWV3Qm94PSI0IDQgMTg4IDE4OCIgd2lkdGg9IjE4IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxwYXRoIGQ9Im00IDRoMTg4djE4OGgtMTg4eiIgZmlsbD0iI2Y2MCIvPjxwYXRoIGQ9Im03My4yNTIxNzU2IDQ1LjAxIDIyLjc0NzgyNDQgNDcuMzkxMzAwODMgMjIuNzQ3ODI0NC00Ny4zOTEzMDA4M2gxOS41NjU2OTYzMWwtMzQuMzIzNTIwNzEgNjQuNDg2NjE0Njh2NDEuNDkzMzg1MzJoLTE1Ljk4di00MS40OTMzODUzMmwtMzQuMzIzNTIwNzEtNjQuNDg2NjE0Njh6IiBmaWxsPSIjZmZmIi8+PC9zdmc+")
This adaptation hooks onto an Emacs language mode—Emacs Lisp mode, in this snippet—and searches for, and translates, a regex syntax (%image) appearing in the comments section. This piggybacks on the language mode's own parser (the 'font-lock-comment-face thingy). The image-handling part is core Emacs functionality; like DrRacket it does support these things natively.by IshKebab on 12/29/24, 10:56 PM
There's a very long standing VSCode issue for this.
by WiggleGuy on 12/29/24, 5:15 PM
by diath on 12/29/24, 5:59 PM
by Tempest1981 on 12/29/24, 6:04 PM
by adammarples on 12/29/24, 10:55 PM