 ;;
 ;;; Helpers
 
-(defun +org--refresh-inline-images-in-subtree ()
-  "Refresh image previews in the current heading/tree."
-  (if (> (length org-inline-image-overlays) 0)
-      (org-remove-inline-images)
-    (org-display-inline-images
-     t t
-     (if (org-before-first-heading-p)
-         (line-beginning-position)
-       (save-excursion (org-back-to-heading) (point)))
-     (if (org-before-first-heading-p)
-         (line-end-position)
-       (save-excursion (org-end-of-subtree) (point))))))
+(defun +org--toggle-inline-images-in-subtree (&optional beg end refresh)
+  "Refresh inline image previews in the current heading/tree."
+  (let ((beg (or beg
+                 (if (org-before-first-heading-p)
+                     (line-beginning-position)
+                   (save-excursion (org-back-to-heading) (point)))))
+        (end (or end
+                 (if (org-before-first-heading-p)
+                     (line-end-position)
+                   (save-excursion (org-end-of-subtree) (point)))))
+        (overlays (cl-remove-if-not (lambda (ov) (overlay-get ov 'org-image-overlay))
+                                    (ignore-errors (overlays-in beg end)))))
+    (dolist (ov overlays nil)
+      (delete-overlay ov)
+      (setq org-inline-image-overlays (delete ov org-inline-image-overlays)))
+    (when (or refresh (not overlays))
+      (org-display-inline-images t t beg end)
+      t)))
 
 (defun +org--insert-item (direction)
   (let* ((context

              (path (org-element-property :path lineage)))
         (if (or (equal (org-element-property :type lineage) "img")
                 (and path (image-type-from-file-name path)))
             (+org--refresh-inline-images-in-subtree)
           (org-open-at-point arg))))

      ((guard (org-element-property :checkbox (org-element-lineage context '(item) t)))

If on a:
- checkbox list item or todo heading: toggle it.
- clock: update its time.
- headline: cycle ARCHIVE subtrees, toggle latex fragments and inline images in
  subtree; update statistics cookies/checkboxes and ToCs.
- footnote reference: jump to the footnote's definition
- footnote definition: jump to the first reference of this footnote
- table-row or a TBLFM: recalculate the table's formulas
