org-id.el 14 KB

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