Just closing the loop on this for anyone that follows in the future. I
worked out a solution for this, which allows me to keep these setting in
drawers, but temporarily "unpacks" the draws to just their contents
before painting images or exporting: 

### 

(defun unpack-image-drawers (&rest r)
 "Replace drawers named "IMAGE_INFO" with their contents."
 (let* ((drawer-name "IMAGE_INFO")
 (save-string "#+ATTR_SAVE: truen")
 (image-drawers (reverse (org-element-map (org-element-parse-buffer)
 'drawer
 (lambda (el)
 (when (string= drawer-name (org-element-property :drawer-name el))
 el))))))
 (cl-loop for drawer in image-drawers do
 (setf (buffer-substring (org-element-property :begin drawer)
 (- (org-element-property :end drawer) 1))
 (concat save-string
 (buffer-substring (org-element-property :contents-begin drawer)
 (- (org-element-property :contents-end drawer) 1)))))))

(defun repack-image-drawers (&rest r)
 "Restore image drawers replaced using `unpack-image-drawers'."
 (let* ((drawer-name "IMAGE_INFO")
 (save-string "#+ATTR_SAVE: truen")
 (image-paragraphs (reverse (org-element-map (org-element-parse-buffer)
 'paragraph
 (lambda (el)
 (when (string= "true" (nth 0 (org-element-property :attr_save el)))
 el))))))
 (cl-loop for paragraph in image-paragraphs do
 (setf (buffer-substring (org-element-property :begin paragraph)
 (- (org-element-property :contents-begin paragraph) 1))
 (concat ":" drawer-name ":n"
 (buffer-substring (+ (length save-string) (org-element-property :begin
paragraph))
 (- (org-element-property :contents-begin paragraph) 1))
 "n:END:")))))

(defun apply-with-image-drawers-unpacked (f &rest r)
 "Replace drawers named "IMAGE_INFO" with their contents, run the
function,
finally restore the drawers as they were. Also collapses all drawers
before returning."
 (unpack-image-drawers)
 (apply f r)
 (repack-image-drawers)
 (org-hide-drawer-all))

(advice-add #'org-display-inline-images :around
#'apply-with-image-drawers-unpacked)
(add-hook 'org-export-before-processing-hook 'unpack-image-drawers)

On 2021-07-06 18:25, Matthew Twomey wrote: 

> Greetings, 
> 
> I'm trying to find a way to visually collapse settings related to images in 
> my org docs. I have many images with a syntax similar to: 
> 
> #+NAME: fig:figure name
> #+CAPTION: figure name
> #+ATTR_ORG: :width 200
> #+ATTR_LATEX: :width 2.0in
> #+ATTR_HTML: :width 200
> [[file:homepage.org_imgs/20210706_002617_ok9v4c.png]] 
> 
> The setting vary per image and I prefer tailoring each image, but I don't 
> like all the clutter on my screen while editing. I've tried putting the 
> settings in a drawer, but that make it invisible to the link element. I've 
> been digging through source code, thinking maybe I could give advice to some 
> function or find something to hook into, but I'm not getting anywhere. Any 
> thoughts or ideas on this? 
> 
> Thanks, 
> 
> -Matt

 

Reply via email to