org-element: Extract search option and application in "file"-type links

* lisp/org-element.el (org-element-link-type-is-file): New variable.
(org-element-link-parser): Extract search option and application in
"file"-type links.
* contrib/lisp/org-e-html.el (org-e-html-link): Remove components
  extraction since this in now done in `org-element-link-parser'.
* contrib/lisp/org-e-odt.el: Remove components extraction since this
  in now done in `org-element-link-parser'.
* contrib/lisp/org-e-texinfo.el (org-e-texinfo-link): Remove
  components extraction since this in now done in
  `org-element-link-parser'.
* contrib/lisp/org-e-latex.el (org-e-latex-link): Remove components
  extraction since this in now done in `org-element-link-parser'.
* contrib/lisp/org-e-man.el (org-e-man-link): Remove components
  extraction since this in now done in `org-element-link-parser'.
* contrib/lisp/org-e-groff.el (org-e-groff-link): Remove components
  extraction since this in now done in `org-element-link-parser'.
* contrib/lisp/org-md.el (org-md-link): Remove components extraction
  since this in now done in `org-element-link-parser'.
* testing/lisp/test-org-element.el: Add tests.
This commit is contained in:
Nicolas Goaziou 2012-08-25 10:38:25 +02:00
parent d09a6b3db5
commit d86f09a854
9 changed files with 46 additions and 21 deletions

View File

@ -1336,8 +1336,6 @@ INFO is a plist holding contextual information. See
((member type '("http" "https" "ftp" "mailto")) ((member type '("http" "https" "ftp" "mailto"))
(concat type ":" raw-path)) (concat type ":" raw-path))
((string= type "file") ((string= type "file")
(when (string-match "\\(.+\\)::.+" raw-path)
(setq raw-path (match-string 1 raw-path)))
(if (file-name-absolute-p raw-path) (if (file-name-absolute-p raw-path)
(concat "file://" (expand-file-name raw-path)) (concat "file://" (expand-file-name raw-path))
(concat "file://" raw-path))) (concat "file://" raw-path)))

View File

@ -2277,10 +2277,6 @@ INFO is a plist holding contextual information. See
((member type '("http" "https" "ftp" "mailto")) ((member type '("http" "https" "ftp" "mailto"))
(concat type ":" raw-path)) (concat type ":" raw-path))
((string= type "file") ((string= type "file")
;; Extract just the file path and strip all other
;; components.
(when (string-match "\\(.+\\)::.+" raw-path)
(setq raw-path (match-string 1 raw-path)))
;; Treat links to ".org" files as ".html", if needed. ;; Treat links to ".org" files as ".html", if needed.
(setq raw-path (funcall --link-org-files-as-html-maybe (setq raw-path (funcall --link-org-files-as-html-maybe
raw-path info)) raw-path info))

View File

@ -1748,8 +1748,6 @@ INFO is a plist holding contextual information. See
((member type '("http" "https" "ftp" "mailto")) ((member type '("http" "https" "ftp" "mailto"))
(concat type ":" raw-path)) (concat type ":" raw-path))
((string= type "file") ((string= type "file")
(when (string-match "\\(.+\\)::.+" raw-path)
(setq raw-path (match-string 1 raw-path)))
(if (file-name-absolute-p raw-path) (if (file-name-absolute-p raw-path)
(concat "file://" (expand-file-name raw-path)) (concat "file://" (expand-file-name raw-path))
(concat "file://" raw-path))) (concat "file://" raw-path)))

View File

@ -731,8 +731,6 @@ INFO is a plist holding contextual information. See
((member type '("http" "https" "ftp" "mailto")) ((member type '("http" "https" "ftp" "mailto"))
(concat type ":" raw-path)) (concat type ":" raw-path))
((string= type "file") ((string= type "file")
(when (string-match "\\(.+\\)::.+" raw-path)
(setq raw-path (match-string 1 raw-path)))
(if (file-name-absolute-p raw-path) (if (file-name-absolute-p raw-path)
(concat "file://" (expand-file-name raw-path)) (concat "file://" (expand-file-name raw-path))
(concat "file://" raw-path))) (concat "file://" raw-path)))

View File

@ -2485,8 +2485,6 @@ INFO is a plist holding contextual information. See
((member type '("http" "https" "ftp" "mailto")) ((member type '("http" "https" "ftp" "mailto"))
(concat type ":" raw-path)) (concat type ":" raw-path))
((string= type "file") ((string= type "file")
(when (string-match "\\(.+\\)::.+" raw-path)
(setq raw-path (match-string 1 raw-path)))
(if (file-name-absolute-p raw-path) (if (file-name-absolute-p raw-path)
(concat "file://" (expand-file-name raw-path)) (concat "file://" (expand-file-name raw-path))
(concat "file://" raw-path))) (concat "file://" raw-path)))

View File

@ -1223,8 +1223,6 @@ INFO is a plist holding contextual information. See
((member type '("http" "https" "ftp")) ((member type '("http" "https" "ftp"))
(concat type ":" raw-path)) (concat type ":" raw-path))
((string= type "file") ((string= type "file")
(when (string-match "\\(.+\\)::.+" raw-path)
(setq raw-path (match-string 1 raw-path)))
(if (file-name-absolute-p raw-path) (if (file-name-absolute-p raw-path)
(concat "file://" (expand-file-name raw-path)) (concat "file://" (expand-file-name raw-path))
(concat "file://" raw-path))) (concat "file://" raw-path)))

View File

@ -307,10 +307,6 @@ a communication channel."
((member type '("http" "https" "ftp")) ((member type '("http" "https" "ftp"))
(concat type ":" raw-path)) (concat type ":" raw-path))
((equal type "file") ((equal type "file")
;; Extract just the file path and strip
;; all other components.
(when (string-match "\\(.+\\)::.+" raw-path)
(setq raw-path (match-string 1 raw-path)))
;; Treat links to ".org" files as ".html", ;; Treat links to ".org" files as ".html",
;; if needed. ;; if needed.
(setq raw-path (setq raw-path

View File

@ -214,6 +214,12 @@ regexp matching one object can also match the other object.")
Names must be uppercase. Any block whose name has no association Names must be uppercase. Any block whose name has no association
is parsed with `org-element-special-block-parser'.") is parsed with `org-element-special-block-parser'.")
(defconst org-element-link-type-is-file
'("file" "file+emacs" "file+sys" "docview")
"List of link types equivalent to \"file\".
Only these types can accept search options and an explicit
application to open them.")
(defconst org-element-affiliated-keywords (defconst org-element-affiliated-keywords
'("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT" '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
"RESULTS" "SOURCE" "SRCNAME" "TBLNAME") "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
@ -2717,14 +2723,15 @@ beginning position."
"Parse link at point. "Parse link at point.
Return a list whose CAR is `link' and CDR a plist with `:type', Return a list whose CAR is `link' and CDR a plist with `:type',
`:path', `:raw-link', `:begin', `:end', `:contents-begin', `:path', `:raw-link', `:application', `:search-option', `:begin',
`:contents-end' and `:post-blank' as keywords. `:end', `:contents-begin', `:contents-end' and `:post-blank' as
keywords.
Assume point is at the beginning of the link." Assume point is at the beginning of the link."
(save-excursion (save-excursion
(let ((begin (point)) (let ((begin (point))
end contents-begin contents-end link-end post-blank path type end contents-begin contents-end link-end post-blank path type
raw-link link) raw-link link search-option application)
(cond (cond
;; Type 1: Text targeted from a radio target. ;; Type 1: Text targeted from a radio target.
((and org-target-link-regexp (looking-at org-target-link-regexp)) ((and org-target-link-regexp (looking-at org-target-link-regexp))
@ -2779,10 +2786,26 @@ Assume point is at the beginning of the link."
;; LINK-END variable. ;; LINK-END variable.
(setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t")) (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
end (point)) end (point))
;; Extract search option and opening application out of
;; "file"-type links.
(when (member type org-element-link-type-is-file)
;; Application.
(cond ((string-match "^file\\+\\(.*\\)$" type)
(setq application (match-string 1 type)))
((not (string-match "^file" type))
(setq application type)))
;; Extract search option from PATH.
(when (string-match "::\\(.*\\)$" path)
(setq search-option (match-string 1 path)
path (replace-match "" nil nil path)))
;; Make sure TYPE always report "file".
(setq type "file"))
(list 'link (list 'link
(list :type type (list :type type
:path path :path path
:raw-link (or raw-link path) :raw-link (or raw-link path)
:application application
:search-option search-option
:begin begin :begin begin
:end end :end end
:contents-begin contents-begin :contents-begin contents-begin

View File

@ -1101,6 +1101,26 @@ e^{i\\pi}+1=0
(org-element-property (org-element-property
:type :type
(org-element-map (org-element-parse-buffer) 'link 'identity nil t))))) (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
;; 2.9 File-type link with search option.
(should
(equal
'(("file" "projects.org" "*task title"))
(org-test-with-temp-text "[[file:projects.org::*task title]]"
(org-element-map
(org-element-parse-buffer) 'link
(lambda (l) (list (org-element-property :type l)
(org-element-property :path l)
(org-element-property :search-option l)))))))
;; 2.10 File-type link with application.
(should
(equal
'(("file" "projects.org" "docview"))
(org-test-with-temp-text "[[docview:projects.org]]"
(org-element-map
(org-element-parse-buffer) 'link
(lambda (l) (list (org-element-property :type l)
(org-element-property :path l)
(org-element-property :application l)))))))
;; 3. Plain link. ;; 3. Plain link.
(should (should
(org-test-with-temp-text "A link: http://orgmode.org" (org-test-with-temp-text "A link: http://orgmode.org"