diff --git a/eaf-org.el b/eaf-org.el index e14ecd6..94fef8c 100644 --- a/eaf-org.el +++ b/eaf-org.el @@ -7,7 +7,7 @@ ;; Copyright (C) 2018, Andy Stewart, all rights reserved. ;; Created: 2020-05-17 12:31:12 ;; 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 ;; URL: http://www.emacswiki.org/emacs/download/eaf.el ;; Keywords: @@ -37,9 +37,18 @@ ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth ;; Floor, Boston, MA 02110-1301, USA. +;;; Code: + +(require 'ol) + (defcustom eaf-org-override-pdf-links nil - "This options will override existing PDF file links's open function. - Check out variable `eaf-org-override-pdf-links-list' about link types." + "When enabled, this will override existing PDF file links's open function. + +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 :safe #'booleanp :group 'org-link) @@ -48,16 +57,22 @@ '("docview" "pdfview" "pdftools") "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-' 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 () "Store the page of PDF as link support for `org-store-link'. - -The raw link looks like this: [[eaf:::::]]" +The raw link looks like this: [[eaf:::::][description]]" (interactive) (when (eq major-mode 'eaf-mode) (let* ((app eaf--buffer-app-name) ;; filter temp files which is converted to PDF (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)) (extra-args (cl-case (intern app) ('pdf-viewer @@ -68,25 +83,30 @@ The raw link looks like this: [[eaf:::::]]" (concat "eaf:" app "::" url "::" extra-args) (concat "eaf:" app "::" url))) (description (buffer-name))) - (if (and (string-equal app "pdf-viewer") - eaf-org-override-pdf-links - (or (equal (org-link-get-parameter "docview" :follow) 'eaf-org-open) - (equal (org-link-get-parameter "pdfview" :follow) 'eaf-org-open) - (equal (org-link-get-parameter "pdftools" :follow) 'eaf-org-open))) - (org-link-store-props - :type "eaf" - :link link - :description description) - (require 'ol-docview) ; use `docview' for most wide compatible support. - (org-link-store-props - :type "docview" - :link url - :description description))))) - -(defun eaf-org-open (link _) - "Open EAF link with EAF correspoinding application." + (cl-case app + ('pdf-viewer + (if (and eaf-org-override-pdf-links + (or (equal (org-link-get-parameter "docview" :follow) 'eaf-org-open) + (equal (org-link-get-parameter "pdfview" :follow) 'eaf-org-open) + (equal (org-link-get-parameter "pdftools" :follow) 'eaf-org-open))) + (progn (require 'ol-docview) ; use `docview' for most wide compatible support. + (org-link-store-props + :type "docview" + :link (concat "docview:" url) + :description description)) + (org-link-store-props + :type "eaf" + :link link + :description description))) + (t (org-link-store-props + :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)) - ;; for eaf-org link type spec: "eaf::URL:(parameters)" + ;; for eaf-org link type spec: "eaf:::::" (let* ((list (split-string link "::")) (app (car list)) (url (cadr list)) @@ -110,27 +130,24 @@ The raw link looks like this: [[eaf:::::]]" (let* ((list (split-string link "::")) (url (car list)) (extra-args (cadr list))) - (if eaf-org-override-pdf-links - (cl-case (intern (file-name-extension url)) - ('pdf - (eaf-open (expand-file-name url) "pdf-viewer") - (eaf-call "call_function_with_args" eaf--buffer-id - "jump_to_page_with_num" (format "%s" extra-args)))) - ;; restore to original :follow function - (org-link-set-parameters - type :follow (org-link-get-parameter type :orig-follow)) - ;; re-open link with original :follow function - (apply (org-link-get-parameter type :follow) link))))) + (cl-case (intern (file-name-extension url)) + ('pdf + (if eaf-org-override-pdf-links + (progn (eaf-open (expand-file-name url) "pdf-viewer") + (when extra-args + (eaf-call "call_function_with_args" eaf--buffer-id + "jump_to_page_with_num" (format "%s" extra-args)))) + (dolist (type eaf-org-override-pdf-links-list) + ;; restore to original :follow function, since eaf-org-override-pdf-links is nil + (org-link-set-parameters + 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" - :follow #'eaf-org-open - :store #'eaf-org-store-link) + :follow #'eaf-org-open + :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-' 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) +;;; eaf-org.el ends here diff --git a/eaf.el b/eaf.el index a876761..e95588b 100644 --- a/eaf.el +++ b/eaf.el @@ -7,7 +7,7 @@ ;; Copyright (C) 2018, Andy Stewart, all rights reserved. ;; Created: 2018-06-15 14:10:12 ;; 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 ;; URL: http://www.emacswiki.org/emacs/download/eaf.el ;; Keywords: @@ -1689,19 +1689,17 @@ In that way the corresponding function will be called to retrieve the HTML (defun eaf-is-valid-url (url) "Return the same URL if it is valid." - (when (and - url - ;; URL should not include blank char. - (< (length (split-string url)) 2) - ;; Use regexp matching URL. - (or - (and - (string-prefix-p "file://" url) - (string-suffix-p ".html" url)) - ;; Normal url address. - (string-match "^\\(https?://\\)?[a-z0-9]+\\([-.][a-z0-9]+\\)*.+\\..+[a-z0-9.]\\{1,6\\}\\(:[0-9]{1,5}\\)?\\(/.*\\)?$" url) - ;; Localhost url. - (string-match "^\\(https?://\\)?\\(localhost\\|127.0.0.1\\):[0-9]+/?" url))) + (when (and url + ;; URL should not include blank char. + (< (length (split-string url)) 2) + ;; Use regexp matching URL. + (or (and + (string-prefix-p "file://" url) + (string-suffix-p ".html" url)) + ;; Normal url address. + (string-match "^\\(https?://\\)?[a-z0-9]+\\([-.][a-z0-9]+\\)*.+\\..+[a-z0-9.]\\{1,6\\}\\(:[0-9]{1,5}\\)?\\(/.*\\)?$" url) + ;; Localhost url. + (string-match "^\\(https?://\\)?\\(localhost\\|127.0.0.1\\):[0-9]+/?" url))) url)) (defun eaf-wrap-url (url)