From a24c8c481f63113215e66a70382d93cce82c9c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20Wikstr=C3=B6m?= Date: Thu, 6 Feb 2020 22:01:08 +0100 Subject: [PATCH] Refactor attachment path expansion * lisp/org-attach.el (org-attach-link-expand): New function for link element expansion. * lisp/org-element.el (org-element-link-parser): Remove info about expanded attachment paths from link elements. * lisp/ol.el (org-link-open) * lisp/ox-texinfo.el (org-texinfo-link) * lisp/ox-odt.el (org-odt-link) * lisp/ox-md.el (org-md-link) * lisp/ox-man.el (org-man-link) * lisp/ox-latex.el (org-latex--inline-image, org-latex-link) * lisp/ox-html.el (org-html-link) * lisp/ox-ascii.el (org-ascii-link): Refactor to use new link expansion function from org-attach.el instead of (now removed) custom link property from org-element.el. --- lisp/ol.el | 3 ++- lisp/org-attach.el | 16 ++++++++++++++++ lisp/org-element.el | 39 +++++++++++++-------------------------- lisp/ox-ascii.el | 3 ++- lisp/ox-html.el | 3 ++- lisp/ox-latex.el | 6 ++++-- lisp/ox-man.el | 4 +++- lisp/ox-md.el | 4 +++- lisp/ox-odt.el | 4 +++- lisp/ox-texinfo.el | 4 +++- 10 files changed, 51 insertions(+), 35 deletions(-) diff --git a/lisp/ol.el b/lisp/ol.el index 31c34ec7b..10ce83f50 100644 --- a/lisp/ol.el +++ b/lisp/ol.el @@ -75,6 +75,7 @@ (declare-function org-src-source-type "org-src" ()) (declare-function org-time-stamp-format "org" (&optional long inactive)) (declare-function outline-next-heading "outline" ()) +(declare-function org-attach-link-expand "org-attach" (link &optional buffer-or-name)) ;;; Customization @@ -934,7 +935,7 @@ a \"file\" link." (cond ((member type '("file" "attachment")) (when (string= type "attachment") - (setq path (org-element-property :attachment-path link))) + (setq path (org-attach-link-expand link))) (if (string-match "[*?{]" (file-name-nondirectory path)) (dired path) ;; Look into `org-link-parameters' in order to find diff --git a/lisp/org-attach.el b/lisp/org-attach.el index c2aa5c329..0fac6274b 100644 --- a/lisp/org-attach.el +++ b/lisp/org-attach.el @@ -40,6 +40,7 @@ (require 'org-id) (declare-function dired-dwim-target-directory "dired-aux") +(declare-function org-element-property "org-element" (property element)) (defgroup org-attach nil "Options concerning attachments in Org mode." @@ -650,6 +651,21 @@ See `org-attach-open'." Basically, this adds the path to the attachment directory." (expand-file-name file (org-attach-dir))) +(defun org-attach-link-expand (link &optional buffer-or-name) + "Return the full path to the attachment in the LINK element. +Takes LINK which is a link element, as defined by +`org-element-link-parser'. If LINK `:type' is attachment the +full path to the attachment is expanded and returned. Otherwise, +return nil. If BUFFER-OR-NAME is specified, LINK is expanded in +that buffer, otherwise current buffer is assumed." + (let ((type (org-element-property :type link)) + (file (org-element-property :path link)) + (pos (org-element-property :begin link))) + (when (string= type "attachment") + (with-current-buffer (or buffer-or-name (current-buffer)) + (goto-char pos) + (org-attach-expand file))))) + (org-link-set-parameters "attachment" :complete #'org-attach-complete-link) diff --git a/lisp/org-element.el b/lisp/org-element.el index 4fde27e8a..5c46f372c 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -3116,11 +3116,7 @@ When at a link, return a list whose car is `link' and cdr a plist with `:type', `:path', `:format', `:raw-link', `:application', `:search-option', `:begin', `:end', `:contents-begin', `:contents-end' and `:post-blank' as keywords. Otherwise, return -nil. Additionally, in the context of attachment links one -further property, `:attachment-path' is set. That property -contains the attachment link expanded into a full filesystem -path. - +nil. Assume point is at the beginning of the link." (catch 'no-object @@ -3229,27 +3225,18 @@ Assume point is at the beginning of the link." (when trans (setq type (car trans)) (setq path (cdr trans)))) - (let ((link - (list 'link - (list :type type - :path path - :format format - :raw-link (or raw-link path) - :application application - :search-option search-option - :begin begin - :end end - :contents-begin contents-begin - :contents-end contents-end - :post-blank post-blank)))) - ;; Add additional type specific properties for link types that - ;; need it - (when (string= type "attachment") - (org-element-put-property - link :attachment-path - (file-relative-name - (org-attach-expand path)))) - link)))) + (list 'link + (list :type type + :path path + :format format + :raw-link (or raw-link path) + :application application + :search-option search-option + :begin begin + :end end + :contents-begin contents-begin + :contents-end contents-end + :post-blank post-blank))))) (defun org-element-link-interpreter (link contents) "Interpret LINK object as Org syntax. diff --git a/lisp/ox-ascii.el b/lisp/ox-ascii.el index 019c26c24..4ffb44a97 100644 --- a/lisp/ox-ascii.el +++ b/lisp/ox-ascii.el @@ -34,6 +34,7 @@ ;;; Function Declarations (declare-function aa2u "ext:ascii-art-to-unicode" ()) +(declare-function org-attach-link-expand "org-attach" (link &optional buffer-or-name)) ;;; Define Back-End ;; @@ -1573,7 +1574,7 @@ INFO is a plist holding contextual information." (raw-path (org-element-property :path link)) (path (cond ((string= type "attachment") - (setq raw-path (org-element-property :attachment-path link)) + (setq raw-path (org-attach-link-expand link)) (concat type ":" raw-path)) (t (concat type ":" raw-path))))) (cond diff --git a/lisp/ox-html.el b/lisp/ox-html.el index 39ccae3ea..fa30bde95 100644 --- a/lisp/ox-html.el +++ b/lisp/ox-html.el @@ -42,6 +42,7 @@ (declare-function org-id-find-id-file "org-id" (id)) (declare-function htmlize-region "ext:htmlize" (beg end)) (declare-function mm-url-decode-entities "mm-url" ()) +(declare-function org-attach-link-expand "org-attach" (link &optional buffer-or-name)) (defvar htmlize-css-name-prefix) (defvar htmlize-output-type) @@ -3074,7 +3075,7 @@ INFO is a plist holding contextual information. See (url-encode-url (concat type ":" raw-path))) ((member type '("file" "attachment")) (when (string= type "attachment") - (setq raw-path (org-element-property :attachment-path link))) + (setq raw-path (org-attach-link-expand link))) ;; During publishing, turn absolute file names belonging ;; to base directory into relative file names. Otherwise, ;; append "file" protocol to absolute file name. diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el index b307ff49a..1bc15a818 100644 --- a/lisp/ox-latex.el +++ b/lisp/ox-latex.el @@ -32,6 +32,8 @@ ;;; Function Declarations +(declare-function org-attach-link-expand "org-attach" (link &optional buffer-or-name)) + (defvar org-latex-default-packages-alist) (defvar org-latex-packages-alist) (defvar orgtbl-exp-regexp) @@ -2360,7 +2362,7 @@ LINK is the link pointing to the inline image. INFO is a plist used as a communication channel." (let* ((parent (org-export-get-parent-element link)) (path (let ((raw-path (if (string= (org-element-property :type link) "attachment") - (org-element-property :attachment-path link) + (org-attach-link-expand link) (org-element-property :path link)))) (if (not (file-name-absolute-p raw-path)) raw-path (expand-file-name raw-path)))) @@ -2528,7 +2530,7 @@ INFO is a plist holding contextual information. See (concat type ":" raw-path)) ((member type '("file" "attachment")) (when (string= type "attachment") - (setq raw-path (org-element-property :attachment-path link))) + (setq raw-path (org-attach-link-expand link))) (org-export-file-uri raw-path)) (t raw-path))))) diff --git a/lisp/ox-man.el b/lisp/ox-man.el index 5de4c5ea5..b6925c696 100644 --- a/lisp/ox-man.el +++ b/lisp/ox-man.el @@ -42,6 +42,8 @@ ;;; Function Declarations +(declare-function org-attach-link-expand "org-attach" (link &optional buffer-or-name)) + (defvar org-export-man-default-packages-alist) (defvar org-export-man-packages-alist) (defvar orgtbl-exp-regexp) @@ -616,7 +618,7 @@ INFO is a plist holding contextual information. See (concat type ":" raw-path)) ((member type '("file" "attachment")) (when (string= type "attachment") - (setq raw-path (org-element-property :attachment-path link))) + (setq raw-path (org-attach-link-expand link))) (org-export-file-uri raw-path)) (t raw-path)))) (cond diff --git a/lisp/ox-md.el b/lisp/ox-md.el index 61b31f987..7515df3a2 100644 --- a/lisp/ox-md.el +++ b/lisp/ox-md.el @@ -35,6 +35,8 @@ ;;; Function Declarations +(declare-function org-attach-link-expand "org-attach" (link &optional buffer-or-name)) + ;;; User-Configurable Variables (defgroup org-export-md nil @@ -405,7 +407,7 @@ INFO is a plist holding contextual information. See (concat type ":" raw-path)) ((member type '("file" "attachment")) (when (string= type "attachment") - (setq raw-path (org-element-property :attachment-path link))) + (setq raw-path (org-attach-link-expand link))) (org-export-file-uri (funcall link-org-files-as-md raw-path))) (t raw-path)))) (cond diff --git a/lisp/ox-odt.el b/lisp/ox-odt.el index 140323c2d..49e37cc1d 100644 --- a/lisp/ox-odt.el +++ b/lisp/ox-odt.el @@ -34,6 +34,8 @@ ;;; Function Declarations +(declare-function org-attach-link-expand "org-attach" (link &optional buffer-or-name)) + ;;; Define Back-End (org-export-define-backend 'odt @@ -2706,7 +2708,7 @@ INFO is a plist holding contextual information. See (concat type ":" raw-path)) ((member type '("file" "attachment")) (when (string= type "attachment") - (setq raw-path (org-element-property :attachment-path link))) + (setq raw-path (org-attach-link-expand link))) (org-export-file-uri raw-path)) (t raw-path))) ;; Convert & to & for correct XML representation diff --git a/lisp/ox-texinfo.el b/lisp/ox-texinfo.el index 22ea86e1c..89c6746d8 100644 --- a/lisp/ox-texinfo.el +++ b/lisp/ox-texinfo.el @@ -30,6 +30,8 @@ ;;; Function Declarations +(declare-function org-attach-link-expand "org-attach" (link &optional buffer-or-name)) + (defvar orgtbl-exp-regexp) @@ -1058,7 +1060,7 @@ INFO is a plist holding contextual information. See (concat type ":" raw-path)) ((member type '("file" "attachment")) (when (string= type "attachment") - (setq raw-path (org-element-property :attachment-path link))) + (setq raw-path (org-attach-link-expand link))) (org-export-file-uri raw-path)) (t raw-path)))) (cond