;; -*- lexical-binding: t; -*-

(require 'syd/base)
(require 'syd/org)

(use-package org-inline-anim
  :hook (org-mode . org-inline-anim-mode)
  :config
  (defun syd-inline-animation-get-image-overlay ()
    "Return image overlay at point or nil.  Extracted from
`org-inline-anim-animate'."
    (save-excursion
      (when-let* ((element (org-element-at-point)))
	(if (eq (org-element-type element) 'src-block)
	    (org-inline-anim--get-image-overlay-in-result-of-this)
	  (if (and (eq (org-element-type element) 'paragraph)
		   (org-element-property :results element))
	      (org-inline-anim--get-image-overlay-in-element element)
	    (org-inline-anim--get-image-overlay-at-point))))))

  (syd-add-hook 'org-ctrl-c-ctrl-c-hook
    (defun syd-inline-animation--ctrl-c-ctrl-c-h ()
      (interactive)
      (when (syd-inline-animation-get-image-overlay)
	(call-interactively #'org-inline-anim-animate)))))

(provide 'syd/org/inline-animation)
