org-noter.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ;;; org-noter.el --- A synchronized, Org-mode, document annotator -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017-2018 Gonçalo Santos
  3. ;; Author: Gonçalo Santos (aka. weirdNox@GitHub)
  4. ;; Homepage: https://github.com/weirdNox/org-noter
  5. ;; Keywords: lisp pdf interleave annotate external sync notes documents org-mode
  6. ;; Package-Requires: ((emacs "24.4") (cl-lib "0.6") (org "9.0"))
  7. ;; Version: 1.4.1
  8. ;; This file is not part of GNU Emacs.
  9. ;; This program is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; The idea is to let you create notes that are kept in sync when you scroll through the
  21. ;; document, but that are external to it - the notes themselves live in an Org-mode file. As
  22. ;; such, this leverages the power of Org-mode (the notes may have outlines, latex fragments,
  23. ;; babel, etc...) while acting like notes that are made /in/ the document.
  24. ;; Also, I must thank Sebastian for the original idea and inspiration!
  25. ;; Link to the original Interleave package:
  26. ;; https://github.com/rudolfochrist/interleave
  27. ;;; Code:
  28. (require 'org-element)
  29. (require 'cl-lib)
  30. (require 'org-noter-core)
  31. (declare-function org-entry-put "org")
  32. (declare-function org-with-wide-buffer "org-macs")
  33. ;;;###autoload
  34. (defun org-noter (&optional arg)
  35. "Start `org-noter' session.
  36. There are two modes of operation. You may create the session from:
  37. - The Org notes file
  38. - The document to be annotated (PDF, EPUB, ...)
  39. - Creating the session from notes file -----------------------------------------
  40. This will open a session for taking your notes, with indirect
  41. buffers to the document and the notes side by side. Your current
  42. window configuration won't be changed, because this opens in a
  43. new frame.
  44. You only need to run this command inside a heading (which will
  45. hold the notes for this document). If no document path property is found,
  46. this command will ask you for the target file.
  47. With a prefix universal argument ARG, only check for the property
  48. in the current heading, don't inherit from parents.
  49. With 2 prefix universal arguments ARG, ask for a new document,
  50. even if the current heading annotates one.
  51. With a prefix number ARG:
  52. - Greater than 0: Open the document like `find-file'
  53. - Equal to 0: Create session with `org-noter-always-create-frame' toggled
  54. - Less than 0: Open the folder containing the document
  55. - Creating the session from the document ---------------------------------------
  56. This will try to find a notes file in any of the parent folders.
  57. The names it will search for are defined in `org-noter-default-notes-file-names'.
  58. It will also try to find a notes file with the same name as the
  59. document, giving it the maximum priority.
  60. When it doesn't find anything, it will interactively ask you what
  61. you want it to do. The target notes file must be in a parent
  62. folder (direct or otherwise) of the document.
  63. You may pass a prefix ARG in order to make it let you choose the
  64. notes file, even if it finds one."
  65. (interactive "P")
  66. (cond
  67. ;; NOTE(nox): Creating the session from notes file
  68. ((eq major-mode 'org-mode)
  69. (let* ((notes-file-path (buffer-file-name))
  70. (document-property (org-noter--get-or-read-document-property
  71. (not (equal arg '(4)))
  72. (equal arg '(16))))
  73. (org-noter-always-create-frame
  74. (if (and (numberp arg) (= arg 0))
  75. (not org-noter-always-create-frame)
  76. org-noter-always-create-frame))
  77. (ast (org-noter--parse-root (vector (current-buffer) document-property)))
  78. (session-id (get-text-property (org-element-property :begin ast) org-noter--id-text-property))
  79. session)
  80. ;; Check for prefix value
  81. (if (or (numberp arg) (eq arg '-))
  82. ;; Yes, user's given a prefix value.
  83. (cond ((> (prefix-numeric-value arg) 0)
  84. ;; Is the prefix value greater than 0?
  85. (find-file document-property))
  86. ;; Open the document like `find-file'.
  87. ;; Is the prefix value less than 0?
  88. ((< (prefix-numeric-value arg) 0)
  89. ;; Open the folder containing the document.
  90. (find-file (file-name-directory document-property))))
  91. ;; No, user didn't give a prefix value
  92. ;; NOTE(nox): Check if it is an existing session
  93. (when session-id
  94. (setq session (cl-loop for session in org-noter--sessions
  95. when (= (org-noter--session-id session) session-id)
  96. return session))))
  97. (if session
  98. (let* ((org-noter--session session)
  99. (location (org-noter--parse-location-property
  100. (org-noter--get-containing-element))))
  101. (org-noter--setup-windows session)
  102. (when location (org-noter--doc-goto-location location))
  103. (select-frame-set-input-focus (org-noter--session-frame session)))
  104. ;; It's not an existing session, create a new session.
  105. (org-noter--create-session ast document-property notes-file-path))))
  106. ;; NOTE(nox): Creating the session from the annotated document
  107. ((memq major-mode org-noter-supported-modes)
  108. (if (org-noter--valid-session org-noter--session)
  109. (progn (org-noter--setup-windows org-noter--session)
  110. (select-frame-set-input-focus (org-noter--session-frame org-noter--session)))
  111. ;; NOTE(nox): `buffer-file-truename' is a workaround for modes that delete
  112. ;; `buffer-file-name', and may not have the same results
  113. (let* ((buffer-file-name (or (run-hook-with-args-until-success 'org-noter-get-buffer-file-name-hook major-mode)
  114. buffer-file-name))
  115. (document-path (or buffer-file-name buffer-file-truename
  116. (error "This buffer does not seem to be visiting any file")))
  117. (document-name (file-name-nondirectory document-path))
  118. (document-base (file-name-base document-name))
  119. (document-directory (if buffer-file-name
  120. (file-name-directory buffer-file-name)
  121. (if (file-equal-p document-name buffer-file-truename)
  122. default-directory
  123. (file-name-directory buffer-file-truename))))
  124. ;; NOTE(nox): This is the path that is actually going to be used, and should
  125. ;; be the same as `buffer-file-name', but is needed for the truename workaround
  126. (document-used-path (expand-file-name document-name document-directory))
  127. (search-names (append org-noter-default-notes-file-names
  128. (list (concat document-base ".org"))
  129. (list (run-hook-with-args-until-success 'org-noter-find-additional-notes-functions document-path))))
  130. notes-files-annotating ; List of files annotating document
  131. notes-files ; List of found notes files (annotating or not)
  132. (document-location (org-noter--doc-approx-location)))
  133. ;; NOTE(nox): Check the search path
  134. (dolist (path org-noter-notes-search-path)
  135. (dolist (name search-names)
  136. (let ((file-name (expand-file-name name path)))
  137. (when (file-exists-p file-name)
  138. (push file-name notes-files)
  139. (when (org-noter--check-if-document-is-annotated-on-file document-path file-name)
  140. (push file-name notes-files-annotating))))))
  141. ;; NOTE(nox): `search-names' is in reverse order, so we only need to (push ...)
  142. ;; and it will end up in the correct order
  143. (dolist (name search-names)
  144. (let ((directory (locate-dominating-file document-directory name))
  145. file)
  146. (when directory
  147. (setq file (expand-file-name name directory))
  148. (unless (member file notes-files) (push file notes-files))
  149. (when (org-noter--check-if-document-is-annotated-on-file document-path file)
  150. (push file notes-files-annotating)))))
  151. (setq search-names (nreverse search-names))
  152. (when (or arg (not notes-files-annotating))
  153. (when (or arg (not notes-files))
  154. (let* ((notes-file-name (completing-read "What name do you want the notes to have? "
  155. search-names nil t))
  156. list-of-possible-targets
  157. target)
  158. ;; NOTE(nox): Create list of targets from current path
  159. (catch 'break
  160. (let ((current-directory document-directory)
  161. file-name)
  162. (while t
  163. (setq file-name (expand-file-name notes-file-name current-directory))
  164. (when (file-exists-p file-name)
  165. (setq file-name (propertize file-name 'display
  166. (concat file-name
  167. (propertize " -- Exists!" 'face '(:foregorund "green")))))
  168. (push file-name list-of-possible-targets)
  169. (throw 'break nil))
  170. (push file-name list-of-possible-targets)
  171. (when (string= current-directory
  172. (setq current-directory
  173. (file-name-directory (directory-file-name current-directory))))
  174. (throw 'break nil)))))
  175. (setq list-of-possible-targets (nreverse list-of-possible-targets))
  176. ;; NOTE(nox): Create list of targets from search path
  177. (dolist (path org-noter-notes-search-path)
  178. (when (file-exists-p path)
  179. (let ((file-name (expand-file-name notes-file-name path)))
  180. (unless (member file-name list-of-possible-targets)
  181. (when (file-exists-p file-name)
  182. (setq file-name (propertize file-name 'display
  183. (concat file-name
  184. (propertize " -- Exists!" 'face '(:foreground "green"))))))
  185. (push file-name list-of-possible-targets)))))
  186. (setq target (completing-read "Where do you want to save it? " list-of-possible-targets
  187. nil t))
  188. (set-text-properties 0 (length target) nil target)
  189. (unless (file-exists-p target) (write-region "" nil target))
  190. (setq notes-files (list target))))
  191. (when (> (length notes-files) 1)
  192. (setq notes-files (list (completing-read "In which notes file should we create the heading? "
  193. notes-files nil t))))
  194. (if (member (car notes-files) notes-files-annotating)
  195. ;; NOTE(nox): This is needed in order to override with the arg
  196. (setq notes-files-annotating notes-files)
  197. (with-current-buffer (find-file-noselect (car notes-files))
  198. (goto-char (point-max))
  199. (insert (if (save-excursion (beginning-of-line) (looking-at "[[:space:]]*$")) "" "\n")
  200. "* " document-base)
  201. (org-entry-put nil org-noter-property-doc-file
  202. (file-relative-name document-used-path
  203. (file-name-directory (car notes-files)))))
  204. (setq notes-files-annotating notes-files)))
  205. (when (> (length (delete-dups notes-files-annotating)) 1)
  206. (setq notes-files-annotating (list (completing-read "Which notes file should we open? "
  207. notes-files-annotating nil t))))
  208. (with-current-buffer (find-file-noselect (car notes-files-annotating))
  209. (org-with-point-at (point-min)
  210. (catch 'break
  211. (while (re-search-forward (org-re-property org-noter-property-doc-file) nil t)
  212. (when (file-equal-p (expand-file-name (match-string 3)
  213. (file-name-directory (car notes-files-annotating)))
  214. document-path)
  215. (let ((org-noter--start-location-override document-location))
  216. (org-noter arg))
  217. (throw 'break t)))))))))))
  218. (provide 'org-noter)
  219. ;;; org-noter.el ends here