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.
This commit is contained in:
Gustav Wikström 2020-02-06 22:01:08 +01:00
parent 7d74b5b769
commit a24c8c481f
10 changed files with 51 additions and 35 deletions

View File

@ -75,6 +75,7 @@
(declare-function org-src-source-type "org-src" ()) (declare-function org-src-source-type "org-src" ())
(declare-function org-time-stamp-format "org" (&optional long inactive)) (declare-function org-time-stamp-format "org" (&optional long inactive))
(declare-function outline-next-heading "outline" ()) (declare-function outline-next-heading "outline" ())
(declare-function org-attach-link-expand "org-attach" (link &optional buffer-or-name))
;;; Customization ;;; Customization
@ -934,7 +935,7 @@ a \"file\" link."
(cond (cond
((member type '("file" "attachment")) ((member type '("file" "attachment"))
(when (string= type "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)) (if (string-match "[*?{]" (file-name-nondirectory path))
(dired path) (dired path)
;; Look into `org-link-parameters' in order to find ;; Look into `org-link-parameters' in order to find

View File

@ -40,6 +40,7 @@
(require 'org-id) (require 'org-id)
(declare-function dired-dwim-target-directory "dired-aux") (declare-function dired-dwim-target-directory "dired-aux")
(declare-function org-element-property "org-element" (property element))
(defgroup org-attach nil (defgroup org-attach nil
"Options concerning attachments in Org mode." "Options concerning attachments in Org mode."
@ -650,6 +651,21 @@ See `org-attach-open'."
Basically, this adds the path to the attachment directory." Basically, this adds the path to the attachment directory."
(expand-file-name file (org-attach-dir))) (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" (org-link-set-parameters "attachment"
:complete #'org-attach-complete-link) :complete #'org-attach-complete-link)

View File

@ -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', with `:type', `:path', `:format', `:raw-link', `:application',
`:search-option', `:begin', `:end', `:contents-begin', `:search-option', `:begin', `:end', `:contents-begin',
`:contents-end' and `:post-blank' as keywords. Otherwise, return `:contents-end' and `:post-blank' as keywords. Otherwise, return
nil. Additionally, in the context of attachment links one nil.
further property, `:attachment-path' is set. That property
contains the attachment link expanded into a full filesystem
path.
Assume point is at the beginning of the link." Assume point is at the beginning of the link."
(catch 'no-object (catch 'no-object
@ -3229,27 +3225,18 @@ Assume point is at the beginning of the link."
(when trans (when trans
(setq type (car trans)) (setq type (car trans))
(setq path (cdr trans)))) (setq path (cdr trans))))
(let ((link (list 'link
(list 'link (list :type type
(list :type type :path path
:path path :format format
:format format :raw-link (or raw-link path)
:raw-link (or raw-link path) :application application
:application application :search-option search-option
:search-option search-option :begin begin
:begin begin :end end
:end end :contents-begin contents-begin
:contents-begin contents-begin :contents-end contents-end
:contents-end contents-end :post-blank post-blank)))))
: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))))
(defun org-element-link-interpreter (link contents) (defun org-element-link-interpreter (link contents)
"Interpret LINK object as Org syntax. "Interpret LINK object as Org syntax.

View File

@ -34,6 +34,7 @@
;;; Function Declarations ;;; Function Declarations
(declare-function aa2u "ext:ascii-art-to-unicode" ()) (declare-function aa2u "ext:ascii-art-to-unicode" ())
(declare-function org-attach-link-expand "org-attach" (link &optional buffer-or-name))
;;; Define Back-End ;;; Define Back-End
;; ;;
@ -1573,7 +1574,7 @@ INFO is a plist holding contextual information."
(raw-path (org-element-property :path link)) (raw-path (org-element-property :path link))
(path (cond (path (cond
((string= type "attachment") ((string= type "attachment")
(setq raw-path (org-element-property :attachment-path link)) (setq raw-path (org-attach-link-expand link))
(concat type ":" raw-path)) (concat type ":" raw-path))
(t (concat type ":" raw-path))))) (t (concat type ":" raw-path)))))
(cond (cond

View File

@ -42,6 +42,7 @@
(declare-function org-id-find-id-file "org-id" (id)) (declare-function org-id-find-id-file "org-id" (id))
(declare-function htmlize-region "ext:htmlize" (beg end)) (declare-function htmlize-region "ext:htmlize" (beg end))
(declare-function mm-url-decode-entities "mm-url" ()) (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-css-name-prefix)
(defvar htmlize-output-type) (defvar htmlize-output-type)
@ -3074,7 +3075,7 @@ INFO is a plist holding contextual information. See
(url-encode-url (concat type ":" raw-path))) (url-encode-url (concat type ":" raw-path)))
((member type '("file" "attachment")) ((member type '("file" "attachment"))
(when (string= type "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 ;; During publishing, turn absolute file names belonging
;; to base directory into relative file names. Otherwise, ;; to base directory into relative file names. Otherwise,
;; append "file" protocol to absolute file name. ;; append "file" protocol to absolute file name.

View File

@ -32,6 +32,8 @@
;;; Function Declarations ;;; Function Declarations
(declare-function org-attach-link-expand "org-attach" (link &optional buffer-or-name))
(defvar org-latex-default-packages-alist) (defvar org-latex-default-packages-alist)
(defvar org-latex-packages-alist) (defvar org-latex-packages-alist)
(defvar orgtbl-exp-regexp) (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." used as a communication channel."
(let* ((parent (org-export-get-parent-element link)) (let* ((parent (org-export-get-parent-element link))
(path (let ((raw-path (if (string= (org-element-property :type link) "attachment") (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)))) (org-element-property :path link))))
(if (not (file-name-absolute-p raw-path)) raw-path (if (not (file-name-absolute-p raw-path)) raw-path
(expand-file-name raw-path)))) (expand-file-name raw-path))))
@ -2528,7 +2530,7 @@ INFO is a plist holding contextual information. See
(concat type ":" raw-path)) (concat type ":" raw-path))
((member type '("file" "attachment")) ((member type '("file" "attachment"))
(when (string= type "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)) (org-export-file-uri raw-path))
(t (t
raw-path))))) raw-path)))))

View File

@ -42,6 +42,8 @@
;;; Function Declarations ;;; 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-default-packages-alist)
(defvar org-export-man-packages-alist) (defvar org-export-man-packages-alist)
(defvar orgtbl-exp-regexp) (defvar orgtbl-exp-regexp)
@ -616,7 +618,7 @@ INFO is a plist holding contextual information. See
(concat type ":" raw-path)) (concat type ":" raw-path))
((member type '("file" "attachment")) ((member type '("file" "attachment"))
(when (string= type "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)) (org-export-file-uri raw-path))
(t raw-path)))) (t raw-path))))
(cond (cond

View File

@ -35,6 +35,8 @@
;;; Function Declarations ;;; Function Declarations
(declare-function org-attach-link-expand "org-attach" (link &optional buffer-or-name))
;;; User-Configurable Variables ;;; User-Configurable Variables
(defgroup org-export-md nil (defgroup org-export-md nil
@ -405,7 +407,7 @@ INFO is a plist holding contextual information. See
(concat type ":" raw-path)) (concat type ":" raw-path))
((member type '("file" "attachment")) ((member type '("file" "attachment"))
(when (string= type "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))) (org-export-file-uri (funcall link-org-files-as-md raw-path)))
(t raw-path)))) (t raw-path))))
(cond (cond

View File

@ -34,6 +34,8 @@
;;; Function Declarations ;;; Function Declarations
(declare-function org-attach-link-expand "org-attach" (link &optional buffer-or-name))
;;; Define Back-End ;;; Define Back-End
(org-export-define-backend 'odt (org-export-define-backend 'odt
@ -2706,7 +2708,7 @@ INFO is a plist holding contextual information. See
(concat type ":" raw-path)) (concat type ":" raw-path))
((member type '("file" "attachment")) ((member type '("file" "attachment"))
(when (string= type "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)) (org-export-file-uri raw-path))
(t raw-path))) (t raw-path)))
;; Convert & to & for correct XML representation ;; Convert & to & for correct XML representation

View File

@ -30,6 +30,8 @@
;;; Function Declarations ;;; Function Declarations
(declare-function org-attach-link-expand "org-attach" (link &optional buffer-or-name))
(defvar orgtbl-exp-regexp) (defvar orgtbl-exp-regexp)
@ -1058,7 +1060,7 @@ INFO is a plist holding contextual information. See
(concat type ":" raw-path)) (concat type ":" raw-path))
((member type '("file" "attachment")) ((member type '("file" "attachment"))
(when (string= type "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)) (org-export-file-uri raw-path))
(t raw-path)))) (t raw-path))))
(cond (cond