Fix eaf-org-store-link and eaf-org-open, revise eaf-org-override-pdf-links

eaf-org-override-pdf-links should be set to 't when the user wants to
ensure the PDF link in the org file can be opened without EAF enabled.

Address #404

Signed-off-by: Mingde (Matthew) Zeng <matthewzmd@posteo.net>
master
Mingde (Matthew) Zeng 6 years ago
parent 937e5300a4
commit 7ebd0d02f4
  1. 103
      eaf-org.el
  2. 26
      eaf.el

@ -7,7 +7,7 @@
;; Copyright (C) 2018, Andy Stewart, all rights reserved. ;; Copyright (C) 2018, Andy Stewart, all rights reserved.
;; Created: 2020-05-17 12:31:12 ;; Created: 2020-05-17 12:31:12
;; Version: 0.5 ;; Version: 0.5
;; Last-Updated: Wed May 20 11:48:43 2020 (-0400) ;; Last-Updated: Sun Sep 27 23:44:55 2020 (-0400)
;; By: Mingde (Matthew) Zeng ;; By: Mingde (Matthew) Zeng
;; URL: http://www.emacswiki.org/emacs/download/eaf.el ;; URL: http://www.emacswiki.org/emacs/download/eaf.el
;; Keywords: ;; Keywords:
@ -37,9 +37,18 @@
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA. ;; Floor, Boston, MA 02110-1301, USA.
;;; Code:
(require 'ol)
(defcustom eaf-org-override-pdf-links nil (defcustom eaf-org-override-pdf-links nil
"This options will override existing PDF file links's open function. "When enabled, this will override existing PDF file links's open function.
Check out variable `eaf-org-override-pdf-links-list' about link types."
So that every existing PDF org-link that's supposed to be opened
by something in `eaf-org-override-pdf-links-list' will be opened using EAF.
Enable this when the you want to ensure the PDF link in the org file can be
opened without EAF enabled."
:type 'boolean :type 'boolean
:safe #'booleanp :safe #'booleanp
:group 'org-link) :group 'org-link)
@ -48,16 +57,22 @@
'("docview" "pdfview" "pdftools") '("docview" "pdfview" "pdftools")
"A list of all PDF file link types which will be override by EAF open function.") "A list of all PDF file link types which will be override by EAF open function.")
(dolist (type eaf-org-override-pdf-links-list)
(when (and eaf-org-override-pdf-links
(org-link-get-parameter type :follow)) ; if `nil' means `ol-<link>' not loaded.
(org-link-set-parameters ; store original `:follow' function
type :orig-follow (org-link-get-parameter type :follow))
(org-link-set-parameters type :follow #'eaf-org-open)))
(defun eaf-org-store-link () (defun eaf-org-store-link ()
"Store the page of PDF as link support for `org-store-link'. "Store the page of PDF as link support for `org-store-link'.
The raw link looks like this: [[eaf:<app>::<path>::<extra-args>][description]]"
The raw link looks like this: [[eaf:<app>::<path>::<extra-args>]]"
(interactive) (interactive)
(when (eq major-mode 'eaf-mode) (when (eq major-mode 'eaf-mode)
(let* ((app eaf--buffer-app-name) (let* ((app eaf--buffer-app-name)
;; filter temp files which is converted to PDF ;; filter temp files which is converted to PDF
(url (if (string-prefix-p "/tmp/" eaf--buffer-url) (url (if (string-prefix-p "/tmp/" eaf--buffer-url)
(warn "[EAF] don't support this application link which is converted to temporary PDF file.") (warn "[EAF] doesn't support this application link which is converted to temporary PDF file.")
eaf--buffer-url)) eaf--buffer-url))
(extra-args (cl-case (intern app) (extra-args (cl-case (intern app)
('pdf-viewer ('pdf-viewer
@ -68,25 +83,30 @@ The raw link looks like this: [[eaf:<app>::<path>::<extra-args>]]"
(concat "eaf:" app "::" url "::" extra-args) (concat "eaf:" app "::" url "::" extra-args)
(concat "eaf:" app "::" url))) (concat "eaf:" app "::" url)))
(description (buffer-name))) (description (buffer-name)))
(if (and (string-equal app "pdf-viewer") (cl-case app
eaf-org-override-pdf-links ('pdf-viewer
(or (equal (org-link-get-parameter "docview" :follow) 'eaf-org-open) (if (and eaf-org-override-pdf-links
(equal (org-link-get-parameter "pdfview" :follow) 'eaf-org-open) (or (equal (org-link-get-parameter "docview" :follow) 'eaf-org-open)
(equal (org-link-get-parameter "pdftools" :follow) 'eaf-org-open))) (equal (org-link-get-parameter "pdfview" :follow) 'eaf-org-open)
(org-link-store-props (equal (org-link-get-parameter "pdftools" :follow) 'eaf-org-open)))
:type "eaf" (progn (require 'ol-docview) ; use `docview' for most wide compatible support.
:link link (org-link-store-props
:description description) :type "docview"
(require 'ol-docview) ; use `docview' for most wide compatible support. :link (concat "docview:" url)
(org-link-store-props :description description))
:type "docview" (org-link-store-props
:link url :type "eaf"
:description description))))) :link link
:description description)))
(defun eaf-org-open (link _) (t (org-link-store-props
"Open EAF link with EAF correspoinding application." :type "eaf"
:link link
:description description))))))
(defun eaf-org-open (link &optional _)
"Open LINK using EAF on an EAF supported file."
(if (member (car (split-string link "::")) (mapcar 'car eaf-app-extensions-alist)) (if (member (car (split-string link "::")) (mapcar 'car eaf-app-extensions-alist))
;; for eaf-org link type spec: "eaf:<app>:URL:(parameters)" ;; for eaf-org link type spec: "eaf:<app>::<path>::<extra-args>"
(let* ((list (split-string link "::")) (let* ((list (split-string link "::"))
(app (car list)) (app (car list))
(url (cadr list)) (url (cadr list))
@ -110,27 +130,24 @@ The raw link looks like this: [[eaf:<app>::<path>::<extra-args>]]"
(let* ((list (split-string link "::")) (let* ((list (split-string link "::"))
(url (car list)) (url (car list))
(extra-args (cadr list))) (extra-args (cadr list)))
(if eaf-org-override-pdf-links (cl-case (intern (file-name-extension url))
(cl-case (intern (file-name-extension url)) ('pdf
('pdf (if eaf-org-override-pdf-links
(eaf-open (expand-file-name url) "pdf-viewer") (progn (eaf-open (expand-file-name url) "pdf-viewer")
(eaf-call "call_function_with_args" eaf--buffer-id (when extra-args
"jump_to_page_with_num" (format "%s" extra-args)))) (eaf-call "call_function_with_args" eaf--buffer-id
;; restore to original :follow function "jump_to_page_with_num" (format "%s" extra-args))))
(org-link-set-parameters (dolist (type eaf-org-override-pdf-links-list)
type :follow (org-link-get-parameter type :orig-follow)) ;; restore to original :follow function, since eaf-org-override-pdf-links is nil
;; re-open link with original :follow function (org-link-set-parameters
(apply (org-link-get-parameter type :follow) link))))) type :follow (org-link-get-parameter type :orig-follow))
;; re-open link with original :follow function
(apply (org-link-get-parameter type :follow) link))))))))
(org-link-set-parameters "eaf" (org-link-set-parameters "eaf"
:follow #'eaf-org-open :follow #'eaf-org-open
:store #'eaf-org-store-link) :store #'eaf-org-store-link)
(if eaf-org-override-pdf-links
(dolist (type eaf-org-override-pdf-links-list)
(when (org-link-get-parameter type :follow) ; if `nil' means `ol-<link>' not loaded.
(org-link-set-parameters ; store original `:follow' function
type :orig-follow (org-link-get-parameter type :follow))
(org-link-set-parameters type :follow #'eaf-org-open))))
(provide 'eaf-org) (provide 'eaf-org)
;;; eaf-org.el ends here

@ -7,7 +7,7 @@
;; Copyright (C) 2018, Andy Stewart, all rights reserved. ;; Copyright (C) 2018, Andy Stewart, all rights reserved.
;; Created: 2018-06-15 14:10:12 ;; Created: 2018-06-15 14:10:12
;; Version: 0.5 ;; Version: 0.5
;; Last-Updated: Sun Sep 13 23:07:25 2020 (-0400) ;; Last-Updated: Wed Sep 23 02:33:02 2020 (-0400)
;; By: Mingde (Matthew) Zeng ;; By: Mingde (Matthew) Zeng
;; URL: http://www.emacswiki.org/emacs/download/eaf.el ;; URL: http://www.emacswiki.org/emacs/download/eaf.el
;; Keywords: ;; Keywords:
@ -1689,19 +1689,17 @@ In that way the corresponding function will be called to retrieve the HTML
(defun eaf-is-valid-url (url) (defun eaf-is-valid-url (url)
"Return the same URL if it is valid." "Return the same URL if it is valid."
(when (and (when (and url
url ;; URL should not include blank char.
;; URL should not include blank char. (< (length (split-string url)) 2)
(< (length (split-string url)) 2) ;; Use regexp matching URL.
;; Use regexp matching URL. (or (and
(or (string-prefix-p "file://" url)
(and (string-suffix-p ".html" url))
(string-prefix-p "file://" url) ;; Normal url address.
(string-suffix-p ".html" url)) (string-match "^\\(https?://\\)?[a-z0-9]+\\([-.][a-z0-9]+\\)*.+\\..+[a-z0-9.]\\{1,6\\}\\(:[0-9]{1,5}\\)?\\(/.*\\)?$" url)
;; Normal url address. ;; Localhost url.
(string-match "^\\(https?://\\)?[a-z0-9]+\\([-.][a-z0-9]+\\)*.+\\..+[a-z0-9.]\\{1,6\\}\\(:[0-9]{1,5}\\)?\\(/.*\\)?$" url) (string-match "^\\(https?://\\)?\\(localhost\\|127.0.0.1\\):[0-9]+/?" url)))
;; Localhost url.
(string-match "^\\(https?://\\)?\\(localhost\\|127.0.0.1\\):[0-9]+/?" url)))
url)) url))
(defun eaf-wrap-url (url) (defun eaf-wrap-url (url)

Loading…
Cancel
Save