eaf-interleave: refactor eaf-interleave-open-notes-file

master
luhuaei 6 years ago
parent 4995c0e8bb
commit f560df741a
  1. 103
      app/interleave/eaf-interleave.el

@ -217,56 +217,43 @@ This show the previous notes and synchronizes the PDF to the right page number."
(org-cycle-hide-drawers t) (org-cycle-hide-drawers t)
(eaf-interleave-sync-current-note)) (eaf-interleave-sync-current-note))
(defun eaf-interleave-open-notes-file ()
"Find current EAF url corresponding note files if exists"
(interactive)
(if (derived-mode-p 'eaf-mode)
(cond ((equal eaf--buffer-app-name "pdf-viewer")
(eaf-interleave--open-notes-file-for-pdf))
((equal eaf--buffer-app-name "browser")
(eaf-interleave--open-notes-file-for-browser))))
)
;;;###autoload ;;;###autoload
(defun eaf-interleave-open-notes-file-for-pdf () (defun eaf-interleave--open-notes-file-for-pdf ()
"Open the notes org file for the current pdf file if it exists. "Open the notes org file for the current pdf file if it exists.
Else create it. Else create it.
It is assumed that the notes org file will have the exact same base name It is assumed that the notes org file will have the exact same base name
as the pdf file (just that the notes file will have a .org extension instead as the pdf file (just that the notes file will have a .org extension instead
of .pdf)." of .pdf)."
(interactive) (let ((org-file (concat (file-name-base eaf--buffer-url) ".org"))
(when (derived-mode-p 'eaf-mode) (default-dir (nth 0 eaf-interleave-org-notes-dir-list))
(let* ((pdf-file-name (eaf-get-path-or-url)) (org-file-path (eaf-interleave--find-match-org eaf-interleave-org-notes-dir-list eaf--buffer-url))
(org-file-name-sans-directory (concat (file-name-base pdf-file-name) ".org")) (buffer (eaf-interleave--find-buffer eaf--buffer-url)))
org-file-create-dir ;; Create the notes org file if it does not exist
(cnt 0) (unless org-file-path
try-org-file-name (setq org-file-path (eaf-interleave--ensure-org-file-exist eaf-interleave-org-notes-dir-list org-file)))
(org-file-name (catch 'break ;; Open the notes org file and enable `eaf-interleave-mode'
(dolist (dir eaf-interleave-org-notes-dir-list) (find-file org-file-path)
;; If dir is "." or begins with "./", replace (eaf-interleave-mode)
;; the "." or "./" with the pdf dir name (eaf-interleave--select-split-function)
(setq dir (replace-regexp-in-string (switch-to-buffer buffer)
"^\\(\\.$\\|\\./\\).*" ))
(file-name-directory pdf-file-name)
dir nil nil 1)) (defun eaf-interleave--open-notes-file-for-browser ()
(when (= cnt 0) "Find current open interleave-mode org file, if exists, else will create new org file with URL."
;; In the event the org file is needed to be (if eaf-interleave-org-buffer
;; created, it will be created in the directory nil
;; listed as the first element in nil))
;; `eaf-interleave-org-notes-dir-list'
(setq org-file-create-dir dir))
(setq cnt (1+ cnt))
(setq try-org-file-name (locate-file
org-file-name-sans-directory
(list dir)))
(when try-org-file-name
;; return the first match
(throw 'break try-org-file-name))))))
;; Create the notes org file if it does not exist
(when (null org-file-name)
(setq org-file-name (if (null eaf-interleave-org-notes-dir-list)
(read-file-name "Path: " "~/")
(progn
(when (null (file-exists-p org-file-create-dir))
(make-directory org-file-create-dir))
(expand-file-name org-file-name-sans-directory
org-file-create-dir))))
(with-temp-file org-file-name
(insert "#+INTERLEAVE_PDF: " pdf-file-name)))
;; Open the notes org file and enable `eaf-interleave-mode'
(find-file org-file-name)
(eaf-interleave-mode))))
(defun eaf-interleave-quit () (defun eaf-interleave-quit ()
"Quit interleave mode." "Quit interleave mode."
@ -494,5 +481,35 @@ Consider a headline with property PROPERTY as parent headline."
(eaf-interleave--select-split-function) (eaf-interleave--select-split-function)
(switch-to-buffer buffer))) (switch-to-buffer buffer)))
(defun eaf-interleave--parse-current-dir (dir url)
"If dir is '.' or begins with './', replace the '.' or './' with the current url name"
(replace-regexp-in-string
"^\\(\\.$\\|\\./\\).*"
(file-name-directory url)
dir nil nil 1))
(defun eaf-interleave--find-match-org (dir-list url)
"Find corresponding org file base url on dir list"
(let ((org-file (concat (file-name-base url) ".org"))
path)
(catch 'break
(dolist (dir dir-list)
(setq dir (eaf-interleave--parse-current-dir dir url))
(setq path (locate-file org-file (list dir)))
(when path
;; return the first match
(throw 'break path))))
path))
(defun eaf-interleave--ensure-org-file-exist (dir-list file-name)
"If the org file directory exist return this path, else created directory."
(let ((default-dir (nth 0 dir-list)))
(if dir-list
(progn
(unless (file-exists-p default-dir)
(make-directory default-dir))
(expand-file-name file-name default-dir))
(read-file-name "Path for org file: " "~/"))))
(provide 'eaf-interleave) (provide 'eaf-interleave)
;;; interleave.el ends here ;;; interleave.el ends here

Loading…
Cancel
Save