org-id.el 13 KB

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