sharkey-el/mfm/emoji.el

68 lines
2.6 KiB
EmacsLisp
Raw Permalink Normal View History

;; need to figure out custom emoji resolution
;; let's start by making :rena: render as the rena.webp image...
:neocat_pat_floof::3:
:neofetch:
(defvar sharkey/emoji-cache-dir "~/.emoji"
"Directory under which to store fedi emoji cache.")
(defun sharkey--emoji-cache (name &optional instance)
"Given remote emoji called NAME, return its local path.
INSTANCE defaults to `mastodon-instance-url'.
returns nil if such an emoji does not exist."
(let* ((instance (or instance mastodon-instance-url))
(plain-instance (string-trim instance "https://"))
(plain-name ; should start in colons, remove those
(string-trim name ":" ":"))
(url (format "%s/emoji/%s.webp" instance plain-name))
(local-path (format "%s/%s/%s.webp"
sharkey/emoji-cache-dir
plain-instance
plain-name)))
(unless (file-exists-p local-path)
;; can we do this lazy, like with timers or sth? ehh it's plenty fast as is...
(mkdir (file-name-directory local-path) 'p)
(url-copy-file url local-path))
(if (eq 'webp (image-type-from-file-header local-path))
local-path
nil)))
;; (image-type-from-file-header "~/rena.webp")
:skull:
;; (sharkey--emoji-cache ":rena:")
;; (window-font-height)
;; (insert-image (create-image (sharkey--emoji-cache ":rena:") nil nil
;; :height (window-font-height)
;; :ascent 'center))
(defvar sharkey/emoji-rx (rx (group-n 1 ":" (+ (or alnum "-" "_")) ":")))
(defun next-emoji-overlay (&optional limit)
;; adapted from https://kitchingroup.cheme.cmu.edu/blog/2016/03/21/Displaying-image-overlays-on-image-filenames-in-Emacs/ -- specifically the latter section on overlays
"Turn the next occurence of a colon-delimited emoji name (within LIMIT) into the emoji itself, using an overlay."
;; needs to be `while'. it was `when' and that caused problems.
(while (re-search-forward sharkey/emoji-rx limit t)
(let* ((beg (match-beginning 0))
(end (match-end 0))
(emoj (match-string 1))
(cache (sharkey--emoji-cache emoj)))
(unless (mir/overlay-already-at-p beg end)
(if cache
(let ((img (create-image cache
(if (image-type-available-p 'imagemagick)
'imagemagick
'webp)
nil
:height (window-font-height)
:ascent 'center))
(multi (image-multi-frame-p cache))
(ov (make-overlay beg end)))
(image-animate img nil t)
(overlay-put ov 'display img)
;; (overlay-put ov 'face 'default)
(overlay-put ov 'help-echo emoj)
(overlay-put ov 'org-image-overlay t)
(overlay-put ov 'modification-hooks
(list 'org-display-inline-remove-overlay))))))))
;; :megumin_bakuretsu:
;; (defun mir/timer-at-p)