From 4cdf69001832bcc180e7c607c15c44c484954c82 Mon Sep 17 00:00:00 2001 From: stardiviner Date: Tue, 4 Oct 2022 12:36:32 +0800 Subject: [PATCH 1/2] org.el: Support auto display inline images when cycling * lisp/org.el (org-toggle-inline-images): Support region. (org-display-inline-images): Fix refresh argument logic. (org-remove-inline-images): Support region. * lisp/org-keys.el (org-toggle-inline-images): Update arguments. * lisp/org-cycle.el (org-cycle-inline-images-display): Add new option to control whether auto display inline images when cycling. (org-cycle-display-inline-images): Add new hook function to auto display inline images when cycling. (org-cycle-hook): Add `org-cycle-display-inline-images' into cycling hook by default. * doc/org-manual.org (Exporting): * etc/ORG-NEWS: Document the new option. --- doc/org-manual.org | 6 ++++++ etc/ORG-NEWS | 7 +++++++ lisp/org-cycle.el | 38 ++++++++++++++++++++++++++++++++++++-- lisp/org-keys.el | 2 +- lisp/org.el | 21 +++++++++++++-------- 5 files changed, 63 insertions(+), 11 deletions(-) diff --git a/doc/org-manual.org b/doc/org-manual.org index ad584d7a5..18a050069 100644 --- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -11489,6 +11489,12 @@ command: - When set to nil, try to get the width from an =#+ATTR.*= keyword and fall back on the original width if none is found. + +#+vindex: org-cycle-inline-images-display +Inline images can also be displayed when cycling the folding state. +When custom option ~org-cycle-inline-images-display~ is set, the +visible inline images under subtree will be displayed automatically. + ** Captions :PROPERTIES: :DESCRIPTION: Describe tables, images... diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index c18c03725..d87b49bd3 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -307,6 +307,13 @@ to pass the contents of a named code block as a string argument. The new property =ORG-IMAGE-ACTUAL-WIDTH= can override the global variable ~org-image-actual-width~ value for inline images display width. +*** Outline cycling can now include inline image visibility + +New ~org-cycle-hook~ function ~org-cycle-display-inline-images~ for +auto-displaying inline images in the visible parts of the subtree. +This behavior is controlled by new custom option +~org-cycle-inline-images-display~. + *** New ~org-babel-tangle-finished-hook~ hook run at the very end of ~org-babel-tangle~ This provides a proper counterpart to ~org-babel-pre-tangle-hook~, as diff --git a/lisp/org-cycle.el b/lisp/org-cycle.el index c1caa3fdc..14388caaf 100644 --- a/lisp/org-cycle.el +++ b/lisp/org-cycle.el @@ -208,8 +208,9 @@ the values `folded', `children', or `subtree'." :type 'hook) (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees - org-cycle-show-empty-lines - org-cycle-optimize-window-after-visibility-change) + org-cycle-show-empty-lines + org-cycle-optimize-window-after-visibility-change + org-cycle-display-inline-images) "Hook that is run after `org-cycle' has changed the buffer visibility. The function(s) in this hook must accept a single argument which indicates the new state that was set by the most recent `org-cycle' command. The @@ -229,6 +230,13 @@ normal outline commands like `show-all', but not with the cycling commands." :group 'org-cycle :type 'boolean) +(defcustom org-cycle-inline-images-display nil + "Non-nil means auto display inline images under subtree when cycling." + :group 'org-startup + :group 'org-cycle + :package-version '(Org . "9.6") + :type 'boolean) + (defvar org-cycle-tab-first-hook nil "Hook for functions to attach themselves to TAB. See `org-ctrl-c-ctrl-c-hook' for more information. @@ -775,6 +783,32 @@ STATE should be one of the symbols listed in the docstring of "Subtree is archived and stays closed. Use \ `\\[org-cycle-force-archived]' to cycle it anyway.")))))) +(defun org-cycle-display-inline-images (state) + "Auto display inline images under subtree when cycling. +It works when `org-cycle-inline-images-display' is non-nil." + (when org-cycle-inline-images-display + (pcase state + ('children + (org-with-wide-buffer + (org-narrow-to-subtree) + ;; If has nested headlines, beg,end only from parent headline + ;; to first child headline which reference to upper + ;; let-binding `org-next-visible-heading'. + (org-display-inline-images + nil nil + (point-min) (progn (org-next-visible-heading 1) (point))))) + ('subtree + (org-with-wide-buffer + (org-narrow-to-subtree) + ;; If has nested headlines, also inline display images under all sub-headlines. + (org-display-inline-images nil nil (point-min) (point-max)))) + ('folded + (org-with-wide-buffer + (org-narrow-to-subtree) + (if (numberp (point-max)) + (org-remove-inline-images (point-min) (point-max)) + (ignore))))))) + (provide 'org-cycle) ;;; org-cycle.el ends here diff --git a/lisp/org-keys.el b/lisp/org-keys.el index d65379a72..79e34cbd1 100644 --- a/lisp/org-keys.el +++ b/lisp/org-keys.el @@ -204,7 +204,7 @@ (declare-function org-toggle-radio-button "org" (&optional arg)) (declare-function org-toggle-comment "org" ()) (declare-function org-toggle-fixed-width "org" ()) -(declare-function org-toggle-inline-images "org" (&optional include-linked)) +(declare-function org-toggle-inline-images "org" (&optional include-linked beg end)) (declare-function org-latex-preview "org" (&optional arg)) (declare-function org-toggle-narrow-to-subtree "org" ()) (declare-function org-toggle-ordered-property "org" ()) diff --git a/lisp/org.el b/lisp/org.el index 8191f0860..1cf091f58 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -16079,16 +16079,16 @@ SNIPPETS-P indicates if this is run to create snippet images for HTML." (defvar-local org-inline-image-overlays nil) -(defun org-toggle-inline-images (&optional include-linked) +(defun org-toggle-inline-images (&optional include-linked beg end) "Toggle the display of inline images. INCLUDE-LINKED is passed to `org-display-inline-images'." (interactive "P") (if org-inline-image-overlays (progn - (org-remove-inline-images) + (org-remove-inline-images beg end) (when (called-interactively-p 'interactive) (message "Inline image display turned off"))) - (org-display-inline-images include-linked) + (org-display-inline-images include-linked nil beg end) (when (called-interactively-p 'interactive) (message (if org-inline-image-overlays (format "%d images displayed inline" @@ -16178,8 +16178,8 @@ BEG and END define the considered part. They default to the buffer boundaries with possible narrowing." (interactive "P") (when (display-graphic-p) - (unless refresh - (org-remove-inline-images) + (when refresh + (org-remove-inline-images beg end) (when (fboundp 'clear-image-cache) (clear-image-cache))) (let ((end (or end (point-max)))) (org-with-point-at (or beg (point-min)) @@ -16329,11 +16329,16 @@ buffer boundaries with possible narrowing." (delete ov org-inline-image-overlays) (delete-overlay ov))) -(defun org-remove-inline-images () +(defun org-remove-inline-images (&optional beg end) "Remove inline display of images." (interactive) - (mapc #'delete-overlay org-inline-image-overlays) - (setq org-inline-image-overlays nil)) + (let* ((beg (or beg (point-min))) + (end (or end (point-max))) + (overlays (overlays-in beg end))) + (dolist (ov overlays) + (when (memq ov org-inline-image-overlays) + (setq org-inline-image-overlays (delq ov org-inline-image-overlays)) + (delete-overlay ov))))) (defvar org-self-insert-command-undo-counter 0) (defvar org-speed-command nil) -- 2.38.0