From 4ba9bb3063bde50511457b1d30f1299a85e3ffaf Mon Sep 17 00:00:00 2001 From: stardiviner Date: Mon, 12 Sep 2022 09:45:09 +0800 Subject: [PATCH] org.el: Add hook function to auto display inline images of subtree * lisp/org.el (org-display-subtree-with-inline-images): Add an option and hook function to auto display of inline images under current expanded visible subtree. * lisp/org.el (org-display-inline-image--width): support subtree-level property "ORG-IMAGE-ACTUAL-WIDTH" specified width. * lisp/org.el (org-default-properties): Add a new property in to override global org-image-actual-width subtree level. --- doc/org-manual.org | 5 ++ etc/ORG-NEWS | 5 ++ lisp/org.el | 133 ++++++++++++++++++++++++++++++--------------- 3 files changed, 100 insertions(+), 43 deletions(-) diff --git a/doc/org-manual.org b/doc/org-manual.org index a37b8390c..77274e250 100644 --- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -11314,6 +11314,11 @@ command: startup by configuring the variable ~org-startup-with-inline-images~[fn:119]. +If you want to override global variable ~org-image-actual-width~ of +inline image display width, you can specify property +"=ORG-IMAGE-ACTUAL-WIDTH=" in subtree level. It will only affect +inline images under the specified subtree. + ** Captions :PROPERTIES: :DESCRIPTION: Describe tables, images... diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index a7f32671e..42e7e4292 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -296,6 +296,11 @@ headings in HTML export. Use the header argument =:var x=code-block[]= or : #+CALL: fn(x=code-block[]) to pass the contents of a named code block as a string argument. +*** New default property =ORG-IMAGE-ACTUAL-WIDTH= for overriding global ~org-image-actual-width~ + +The subtree-level property "ORG-IMAGE-ACTUAL-WIDTH" can override the +global variable ~org-image-actual-width~ settings for inline images +width under current property specified subtree. ** New options *** New custom settings =org-icalendar-scheduled-summary-prefix= and =org-icalendar-deadline-summary-prefix= diff --git a/lisp/org.el b/lisp/org.el index 6e6c437d5..98cac1c1c 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -1108,6 +1108,13 @@ the following lines anywhere in the buffer: :version "24.1" :type 'boolean) +(defcustom org-cycle-display-inline-images nil + "Non-nil means auto display inline images under subtree when `org-cycle' +by the function `org-display-subtree-with-inline-images' on hook `org-cycle-hook'." + :group 'org-startup + :version "24.1" + :type 'boolean) + (defcustom org-startup-with-latex-preview nil "Non-nil means preview LaTeX fragments when loading a new Org file. @@ -12199,7 +12206,8 @@ but in some other way.") "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED" "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE" - "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS") + "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS" + "ORG-IMAGE-ACTUAL-WIDTH") "Some properties that are used by Org mode for various purposes. Being in this list makes sure that they are offered for completion.") @@ -16215,6 +16223,42 @@ buffer boundaries with possible narrowing." (overlay-put ov 'keymap image-map)) (push ov org-inline-image-overlays)))))))))))))))) +(defun org-display-subtree-inline-images (&optional state) + "Toggle the display of inline images under current expanded visible subtree. +This hook function will auto display or hide inline images after `org-cycle'. +It is used to hook on `org-cycle-hook'. +The function behavior is controlled by `org-cycle-display-inline-images'." + (interactive) + (when (and org-cycle-display-inline-images + ;; not global state change from `org-cycle-global'. + (not (memq state '(overview contents all)))) + (pcase state + ('children + (save-excursion + (save-restriction + (org-narrow-to-subtree) + ;; If has nested headlines, beg,end only from parent headling + ;; to first child headline which reference to upper let-binding + ;; `org-next-visible-heading'. + (org-display-inline-images t t (point-min) (progn (org-next-visible-heading 1) (point)))))) + ('subtree + (save-excursion + (save-restriction + (org-narrow-to-subtree) + ;; If has nested headlines, also display inline images under all subtrees. + (org-display-inline-images t t (point-min) (point-max))))) + ('folded + ;; (save-excursion + ;; (save-restriction + ;; (org-narrow-to-subtree) + ;; ;; FIXME: this will auto expand heading caused `org-cycle' folded failed. + ;; (mapc #'delete-overlay (overlays-in (point-min) (point-max))))) + ) + ('nil (funcall 'org-toggle-inline-images)) + (_ nil)))) + +(add-hook 'org-cycle-hook #'org-display-subtree-inline-images) + (defvar visual-fill-column-width) ; Silence compiler warning (defun org-display-inline-image--width (link) "Determine the display width of the image LINK, in pixels. @@ -16228,48 +16272,51 @@ buffer boundaries with possible narrowing." If the value is a float between 0 and 2, it interpreted as that proportion of the text width in the buffer." ;; Apply `org-image-actual-width' specifications. - (cond - ((eq org-image-actual-width t) nil) - ((listp org-image-actual-width) - (let* ((case-fold-search t) - (par (org-element-lineage link '(paragraph))) - (attr-re "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)") - (par-end (org-element-property :post-affiliated par)) - ;; Try to find an attribute providing a :width. - (attr-width - (when (and par (org-with-point-at - (org-element-property :begin par) - (re-search-forward attr-re par-end t))) - (match-string 1))) - (width - (cond - ;; Treat :width t as if `org-image-actual-width' were t. - ((string= attr-width "t") nil) - ;; Fallback to `org-image-actual-width' if no interprable width is given. - ((or (null attr-width) - (string-match-p "\\`[^0-9]" attr-width)) - (car org-image-actual-width)) - ;; Convert numeric widths to numbers, converting percentages. - ((string-match-p "\\`[0-9.]+%" attr-width) - (/ (string-to-number attr-width) 100.0)) - (t (string-to-number attr-width))))) - (if (and (floatp width) (<= 0.0 width 2.0)) - ;; A float in [0,2] should be interpereted as this portion of - ;; the text width in the window. This works well with cases like - ;; #+attr_latex: :width 0.X\{line,page,column,etc.}width, - ;; as the "0.X" is pulled out as a float. We use 2 as the upper - ;; bound as cases such as 1.2\linewidth are feasible. - (round (* width - (window-pixel-width) - (/ (or (and (bound-and-true-p visual-fill-column-mode) - (or visual-fill-column-width auto-fill-function)) - (when auto-fill-function fill-column) - (- (window-text-width) (line-number-display-width))) - (float (window-total-width))))) - width))) - ((numberp org-image-actual-width) - org-image-actual-width) - (t nil))) + ;; support subtree-level property "ORG-IMAGE-ACTUAL-WIDTH" specified width. + (let ((org-image-actual-width (org-property-or-variable-value 'org-image-actual-width))) + (cond + ((eq org-image-actual-width t) nil) + ((listp org-image-actual-width) + (let* ((case-fold-search t) + (par (org-element-lineage link '(paragraph))) + (attr-re "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)") + (par-end (org-element-property :post-affiliated par)) + ;; Try to find an attribute providing a :width. + (attr-width + (when (and par (org-with-point-at + (org-element-property :begin par) + (re-search-forward attr-re par-end t))) + (match-string 1))) + (width + (cond + ;; Treat :width t as if `org-image-actual-width' were t. + ((string= attr-width "t") nil) + ((or (null attr-width) + (string-match-p "\\`[^0-9]" attr-width)) + ;; Try to use subtree-level width before fallback. + ;; Fallback to `org-image-actual-width' if no interprable width is given. + (car org-image-actual-width)) + ;; Convert numeric widths to numbers, converting percentages. + ((string-match-p "\\`[0-9.]+%" attr-width) + (/ (string-to-number attr-width) 100.0)) + (t (string-to-number attr-width))))) + (if (and (floatp width) (<= 0.0 width 2.0)) + ;; A float in [0,2] should be interpereted as this portion of + ;; the text width in the window. This works well with cases like + ;; #+attr_latex: :width 0.X\{line,page,column,etc.}width, + ;; as the "0.X" is pulled out as a float. We use 2 as the upper + ;; bound as cases such as 1.2\linewidth are feasible. + (round (* width + (window-pixel-width) + (/ (or (and (bound-and-true-p visual-fill-column-mode) + (or visual-fill-column-width auto-fill-function)) + (when auto-fill-function fill-column) + (- (window-text-width) (line-number-display-width))) + (float (window-total-width))))) + width))) + ((numberp org-image-actual-width) + org-image-actual-width) + (t nil)))) (defun org-display-inline-remove-overlay (ov after _beg _end &optional _len) "Remove inline-display overlay if a corresponding region is modified." -- 2.37.2