org-id.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. ;;; org-id.el --- Global identifier for Org-mode entries
  2. ;; Copyright (C) 2008 Free Software Foundation, Inc.
  3. ;;
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 6.06b
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; This file implements globally unique identifiers for Org-mode entries.
  25. ;; Identifiers are stored in the entry as an :ID: property. Functions
  26. ;; are provided that create and retrieve such identifiers, and that find
  27. ;; entries based on the identifier.
  28. ;; Identifiers consist of a prefix (default "Org") and a compact encoding
  29. ;; of the creation time of the ID, with microsecond accuracy. This virtually
  30. ;; guarantees globally unique identifiers, even if several people are
  31. ;; creating ID's at the same time in files that will eventually be used
  32. ;; together. Even higher security can be achieved by using different
  33. ;; prefix values for each collaborator or file.
  34. ;;
  35. ;; This file defines the following API:
  36. ;;
  37. ;; org-id-get-create
  38. ;; Create an ID for the entry at point if it does not yet have one.
  39. ;; Returns the ID (old or new). This function can be used
  40. ;; interactively, with prefix argument the creation of a new ID is
  41. ;; forced, even if there was an old one.
  42. ;;
  43. ;; org-id-get
  44. ;; Get the ID property of an entry. Using appropriate arguments
  45. ;; to the function, it can also create the ID for this entry.
  46. ;;
  47. ;; org-id-goto
  48. ;; Command to go to a specific ID, this command can be used
  49. ;; interactively.
  50. ;;
  51. ;; org-id-get-with-outline-path-completion
  52. ;; Retrieve the ID of an entry, using outline path completion.
  53. ;; This function can work for multiple files.
  54. ;;
  55. ;; org-id-get-with-outline-drilling
  56. ;; Retrieve the ID of an entry, using outline path completion.
  57. ;; This function only works for the current file.
  58. ;;
  59. ;; org-id-find
  60. ;; Find the location of an entry with specific id.
  61. ;;
  62. (require 'org)
  63. (declare-function message-make-fqdn "message" ())
  64. ;;; Customization
  65. (defgroup org-id nil
  66. "Options concerning global entry identifiers in Org-mode."
  67. :tag "Org ID"
  68. :group 'org)
  69. (defcustom org-id-prefix "Org"
  70. "The prefix for IDs.
  71. This may be a string, or it can be nil to indicate that no prefix is required.
  72. When a string, the string should have no space characters as IDs are expected
  73. to have no space characters in them."
  74. :group 'org-id
  75. :type '(choice
  76. (const :tag "No prefix")
  77. (string :tag "Prefix")))
  78. (defcustom org-id-include-domain t
  79. "Non-nil means, add the domain name to new IDs.
  80. This ensures global uniqueness of ID's, and is also suggested by
  81. RFC 2445 in combination with RFC 822."
  82. :group 'org-id
  83. :type 'boolean)
  84. (defcustom org-id-locations-file "~/.org-id-locations"
  85. "The file for remembering the last ID number generated."
  86. :group 'org-id
  87. :type 'file)
  88. (defvar org-id-locations nil
  89. "List of files with ID's in those files.")
  90. (defcustom org-id-extra-files 'org-agenda-text-search-extra-files
  91. "Files to be searched for ID's, besides the agenda files."
  92. :group 'org-id
  93. :type
  94. '(choice
  95. (symbol :tag "Variable")
  96. (repeat :tag "List of files"
  97. (file))))
  98. ;;; The API functions
  99. ;;;###autoload
  100. (defun org-id-get-create (&optional force)
  101. "Create an ID for the current entry and return it.
  102. If the entry already has an ID, just return it.
  103. With optional argument FORCE, force the creation of a new ID."
  104. (interactive "P")
  105. (when force
  106. (org-entry-put (point) "ID" nil))
  107. (org-id-get (point) 'create))
  108. ;;;###autoload
  109. (defun org-id-copy ()
  110. "Copy the ID of the entry at point to the kill ring.
  111. Create an ID if necessary."
  112. (interactive)
  113. (kill-new (org-id-get nil 'create)))
  114. ;;;###autoload
  115. (defun org-id-get (&optional pom create prefix)
  116. "Get the ID property of the entry at point-or-marker POM.
  117. If POM is nil, refer to the entry at point.
  118. If the entry does not have an ID, the function returns nil.
  119. However, when CREATE is non nil, create an ID if none is present already.
  120. PREFIX will be passed through to `org-id-new'.
  121. In any case, the ID of the entry is returned."
  122. (let ((id (org-entry-get pom "ID")))
  123. (cond
  124. ((and id (stringp id) (string-match "\\S-" id))
  125. id)
  126. (create
  127. (setq id (org-id-new prefix))
  128. (org-entry-put pom "ID" id)
  129. (org-id-add-location id (buffer-file-name (buffer-base-buffer)))
  130. id)
  131. (t nil))))
  132. ;;;###autoload
  133. (defun org-id-get-with-outline-path-completion (&optional targets)
  134. "Use outline-path-completion to retrieve the ID of an entry.
  135. TARGETS may be a setting for `org-refile-targets' to define the eligible
  136. headlines. When omitted, all headlines in all agenda files are
  137. eligible.
  138. It returns the ID of the entry. If necessary, the ID is created."
  139. (let* ((org-refile-targets (or targets '((nil . (:maxlevel . 10)))))
  140. (org-refile-use-outline-path
  141. (if (caar org-refile-targets) 'file t))
  142. (spos (org-refile-get-location "Entry: "))
  143. (pom (and spos (move-marker (make-marker) (nth 3 spos)
  144. (get-file-buffer (nth 1 spos))))))
  145. (prog1 (org-id-get pom 'create)
  146. (move-marker pom nil))))
  147. ;;;###autoload
  148. (defun org-id-get-with-outline-drilling (&optional targets)
  149. "Use an outline-cycling interface to retrieve the ID of an entry.
  150. This only finds entries in the current buffer, using `org-get-location'.
  151. It returns the ID of the entry. If necessary, the ID is created."
  152. (let* ((spos (org-get-location (current-buffer) org-goto-help))
  153. (pom (and spos (move-marker (make-marker) (car spos)))))
  154. (prog1 (org-id-get pom 'create)
  155. (move-marker pom nil))))
  156. ;;;###autoload
  157. (defun org-id-goto (id)
  158. "Switch to the buffer containing the entry with id ID.
  159. Move the cursor to that entry in that buffer."
  160. (interactive)
  161. (let ((m (org-id-find id 'marker)))
  162. (unless m
  163. (error "Cannot find entry with ID \"%s\"" id))
  164. (switch-to-buffer (marker-buffer m))
  165. (goto-char m)
  166. (move-marker m nil)
  167. (org-show-context)))
  168. ;;;###autoload
  169. (defun org-id-find (id &optional markerp)
  170. "Return the location of the entry with the id ID.
  171. The return value is a cons cell (file-name . position), or nil
  172. if there is no entry with that ID.
  173. With optional argument MARKERP, return the position as a new marker."
  174. (let ((file (org-id-find-id-file id))
  175. org-agenda-new-buffers where)
  176. (when file
  177. (setq where (org-id-find-id-in-file id file markerp)))
  178. (unless where
  179. (org-id-update-id-locations)
  180. (setq file (org-id-find-id-file id))
  181. (when file
  182. (setq where (org-id-find-id-in-file id file markerp))))
  183. where))
  184. ;;; Internal functions
  185. ;; Creating new IDs
  186. (defun org-id-new (&optional prefix)
  187. "Create a new globally unique ID.
  188. An ID consists of two parts separated by a colon:
  189. - a prefix
  190. - an encoding of the current time to micro-second accuracy
  191. PREFIX can specify the prefix, the default is given by the variable
  192. `org-id-prefix'. However, if PREFIX is the symbol `none', don't use any
  193. prefix even if `org-id-prefix' specifies one.
  194. So a typical ID could look like \"Org:4nd91V40HI\"."
  195. (let* ((prefix (if (eq prefix 'none)
  196. nil
  197. (or prefix org-id-prefix)))
  198. (etime (org-id-time-to-b62))
  199. (postfix (if org-id-include-domain
  200. (progn
  201. (require 'message)
  202. (concat "@" (message-make-fqdn))))))
  203. (if prefix
  204. (concat prefix ":" etime postfix)
  205. (concat etime postfix))))
  206. (defun org-id-int-to-b62-one-digit (i)
  207. "Turn an integer between 0 and 61 into a single character 0..9, A..Z, a..z."
  208. (cond
  209. ((< i 10) (+ ?0 i))
  210. ((< i 36) (+ ?A i -10))
  211. ((< i 62) (+ ?a i -36))
  212. (t (error "Larger that 61"))))
  213. (defun org-id-b62-to-int-one-digit (i)
  214. "Turn a character 0..9, A..Z, a..z into a number 0..61.
  215. The input I may be a character, or a single-letter string."
  216. (and (stringp i) (setq i (string-to-char i)))
  217. (cond
  218. ((and (>= i ?0) (<= i ?9)) (- i ?0))
  219. ((and (>= i ?A) (<= i ?Z)) (+ (- i ?A) 10))
  220. ((and (>= i ?a) (<= i ?z)) (+ (- i ?a) 36))
  221. (t (error "Invalid b62 letter"))))
  222. (defun org-id-int-to-b62 (i &optional length)
  223. "Convert an integer to a base-62 number represented as a string."
  224. (let ((s ""))
  225. (while (> i 0)
  226. (setq s (concat (char-to-string
  227. (org-id-int-to-b62-one-digit (mod i 62))) s)
  228. i (/ i 62)))
  229. (setq length (max 1 (or length 1)))
  230. (if (< (length s) length)
  231. (setq s (concat (make-string (- length (length s)) ?0) s)))
  232. s))
  233. (defun org-id-b62-to-int (s)
  234. "Convert a base-62 string into the corresponding integer."
  235. (let ((r 0))
  236. (mapc (lambda (i) (setq r (+ (* r 62) (org-id-b62-to-int-one-digit i))))
  237. s)
  238. r))
  239. (defun org-id-time-to-b62 (&optional time)
  240. "Encode TIME as a 10-digit string.
  241. This string holds the time to micro-second accuracy, and can be decoded
  242. using `org-id-decode'."
  243. (setq time (or time (current-time)))
  244. (concat (org-id-int-to-b62 (nth 0 time) 3)
  245. (org-id-int-to-b62 (nth 1 time) 3)
  246. (org-id-int-to-b62 (or (nth 2 time) 0) 4)))
  247. (defun org-id-decode (id)
  248. "Split ID into the prefix and the time value that was used to create it.
  249. The return value is (prefix . time) where PREFIX is nil or a string,
  250. and time is the usual three-integer representation of time."
  251. (let (prefix time parts)
  252. (setq parts (org-split-string id ":"))
  253. (if (= 2 (length parts))
  254. (setq prefix (car parts) time (nth 1 parts))
  255. (setq prefix nil time (nth 0 parts)))
  256. (setq time (list (org-id-b62-to-int (substring time 0 3))
  257. (org-id-b62-to-int (substring time 3 6))
  258. (org-id-b62-to-int (substring time 6 10))))
  259. (cons prefix time)))
  260. ;; Storing ID locations (files)
  261. (defun org-id-update-id-locations ()
  262. "Scan relevant files for ID's.
  263. Store the relation between files and corresponding ID's."
  264. (interactive)
  265. (let ((files (append (org-agenda-files)
  266. (if (symbolp org-id-extra-files)
  267. (symbol-value org-id-extra-files)
  268. org-id-extra-files)))
  269. org-agenda-new-buffers
  270. file ids reg found id)
  271. (while (setq file (pop files))
  272. (setq ids nil)
  273. (with-current-buffer (org-get-agenda-file-buffer file)
  274. (save-excursion
  275. (save-restriction
  276. (widen)
  277. (goto-char (point-min))
  278. (while (re-search-forward "^[ \t]*:ID:[ \t]+\\(\\S-+\\)[ \t]*$"
  279. nil t)
  280. (setq id (org-match-string-no-properties 1))
  281. (if (member id found)
  282. (error "Duplicate ID \"%s\"" id))
  283. (push id found)
  284. (push id ids))
  285. (push (cons file ids) reg)))))
  286. (org-release-buffers org-agenda-new-buffers)
  287. (setq org-agenda-new-buffers nil)
  288. (setq org-id-locations reg)
  289. (org-id-locations-save)))
  290. (defun org-id-locations-save ()
  291. "Save `org-id-locations' in `org-id-locations-file'."
  292. (with-temp-file org-id-locations-file
  293. (print org-id-locations (current-buffer))))
  294. (defun org-id-locations-load ()
  295. "Read the data from `org-id-locations-file'."
  296. (setq org-id-locations nil)
  297. (with-temp-buffer
  298. (condition-case nil
  299. (progn
  300. (insert-file-contents-literally org-id-locations-file)
  301. (goto-char (point-min))
  302. (setq org-id-locations (read (current-buffer))))
  303. (error
  304. (message "Could not read org-id-values from %s. Setting it to nil."
  305. org-id-locations-file)))))
  306. (defun org-id-add-location (id file)
  307. "Add the ID with location FILE to the database of ID loations."
  308. (unless org-id-locations (org-id-locations-load))
  309. (catch 'exit
  310. (let ((locs org-id-locations) list)
  311. (while (setq list (pop locs))
  312. (when (equal (file-truename file) (file-truename (car list)))
  313. (setcdr list (cons id (cdr list)))
  314. (throw 'exit t))))
  315. (push (list file id) org-id-locations))
  316. (org-id-locations-save))
  317. ;; Finding entries with specified id
  318. (defun org-id-find-id-file (id)
  319. "Query the id database for the file in which this ID is located."
  320. (unless org-id-locations (org-id-locations-load))
  321. (catch 'found
  322. (mapc (lambda (x) (if (member id (cdr x))
  323. (throw 'found (car x))))
  324. org-id-locations)
  325. nil))
  326. (defun org-id-find-id-in-file (id file &optional markerp)
  327. "Return the position of the entry ID in FILE.
  328. If that files does not exist, or if it does not contain this ID,
  329. return nil.
  330. The position is returned as a cons cell (file-name . position). With
  331. optional argument MARKERP, return the position as a new marker."
  332. (let (org-agenda-new-buffers m buf pos)
  333. (cond
  334. ((not file) nil)
  335. ((not (file-exists-p file)) nil)
  336. (t (with-current-buffer (setq buf (org-get-agenda-file-buffer file))
  337. (setq pos (org-find-entry-with-id id))
  338. (when pos
  339. (if markerp
  340. (move-marker (make-marker) pos buf)
  341. (cons file pos))))))))
  342. (provide 'org-id)
  343. ;;; org-id.el ends here
  344. ;; arch-tag: e5abaca4-e16f-4b25-832a-540cfb63a712