org-id.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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.01
  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. This file
  28. ;; provides functions to create and retrieve such identifiers.
  29. ;; It provides the following API:
  30. ;;
  31. ;;
  32. ;; Higer-level-functions
  33. ;;
  34. ;; org-id-create
  35. ;; Create an ID for the entry at point if it does not yet have one.
  36. ;; Returns the ID (old or new). This function can be used
  37. ;; interactively, with prefix argument the creation of a new ID is
  38. ;; forced, even if there was an old one.
  39. ;;
  40. ;; org-id-get
  41. ;; Get the ID property of an entry. Using appropriate arguments
  42. ;; to the function, it can also create the ID for this entry.
  43. ;;
  44. ;; org-id-goto
  45. ;; Command to go to a specific ID, this command can be used
  46. ;; interactively.
  47. ;;
  48. ;; org-id-get-with-outline-path-completion
  49. ;; Retrieve the ID of an entry, using outline path completion.
  50. ;; This function can work for multiple files.
  51. ;;
  52. ;; org-id-get-with-outline-drilling
  53. ;; Retrieve the ID of an entry, using outline path completion.
  54. ;; This function only works for the current file.
  55. ;;
  56. ;; org-id-find
  57. ;; Find the location of an entry with specific id.
  58. ;;
  59. ;; TODO:
  60. ;; - get/create id at current entry, safe in kill or so.
  61. (require 'org)
  62. ;;; Customization
  63. (defgroup org-id nil
  64. "Options concerning global entry identifiers in Org-mode."
  65. :tag "Org ID"
  66. :group 'org)
  67. (defcustom org-id-structure (list "Org" (user-login-name) t 4)
  68. "Components of globaly unique id's created by Org-mode.
  69. An Org-mode ID has 4 components:
  70. prefix A prefix to identify the ID type (default \"Org\".
  71. creator The creator of the ID, defaults to the users login name.
  72. incremental An incremental number, specific for the creator.
  73. random A random sequence of characters, to make sure ID created
  74. in distributed development will still be unique.
  75. This may be a short random sequence, or an md5 sum created
  76. based on the current time, the current computer, and the user."
  77. :group 'org-id
  78. :type '(list
  79. (string :tag "Prefix")
  80. (string :tag "Creator")
  81. (boolean :tag "Incremental")
  82. (choice :tag "Random part"
  83. (const :tag "No random part" nil)
  84. (integer :tag "N characters")
  85. (const :tag "MD5 digest" md5))))
  86. (defcustom org-id-tracking-file "~/.org-id"
  87. "The file for remembering the last ID number generated, for each type."
  88. :group 'org-id
  89. :type 'file)
  90. (defvar org-id-values nil
  91. "Association list of ID types+creator with largest index used so far.")
  92. (defcustom org-id-locations-file "~/.org-id-locations"
  93. "The file for remembering the last ID number generated."
  94. :group 'org-id
  95. :type 'file)
  96. (defvar org-id-locations nil
  97. "List of files with ID's in those files.")
  98. (defcustom org-id-extra-files 'org-agenda-multi-occur-extra-files
  99. "Files to be searched for ID's, besides the agenda files."
  100. :group 'org-id
  101. :type
  102. '(choice
  103. (symbol :tag "Variable")
  104. (repeat :tag "List of files"
  105. (file))))
  106. ;;; The API functions
  107. (defun org-id-create (&optional force)
  108. "Create an ID for the current entry and return it.
  109. If the entry already has an ID, just return it.
  110. With optional argument FORCE, force the creation of a new ID."
  111. (interactive "P")
  112. (when force
  113. (org-entry-put (point) "ID" nil))
  114. (org-id-get (point) 'create))
  115. (defun org-id-copy ()
  116. "Copy the ID of the entry at point to the kill ring.
  117. Create an ID if necessary."
  118. (interactive)
  119. (kill-new (org-id-get nil 'create)))
  120. (defun org-id-get (&optional pom create type nrandom)
  121. "Get the ID property of the entry at point-or-marker POM.
  122. If POM is nil, refer to the entry at point.
  123. If the entry does not have an ID, the function returns nil.
  124. However, when CREATE is non nil, create an ID if none is present already.
  125. TYPE and NRANDOM will be passed through to `org-id-new'.
  126. In any case, the ID of the entry is returned."
  127. (let ((id (org-entry-get pom "ID")))
  128. (cond
  129. ((and id (stringp id) (string-match "\\S-" id))
  130. id)
  131. (create
  132. (setq id (org-id-new type nrandom))
  133. (org-entry-put pom "ID" id)
  134. (org-id-add-location id (buffer-file-name (buffer-base-buffer)))
  135. id)
  136. (t nil))))
  137. (defun org-id-get-with-outline-path-completion (&optional targets)
  138. "Use outline-path-completion to retrieve the ID of an entry.
  139. TARGETS may be a setting for `org-refile-targets' to define the elegible
  140. headlines. When omitted, all headlines in all agenda files are
  141. elegible.
  142. It returns the ID of the entry. If necessary, the ID is created."
  143. (let* ((org-refile-targets (or targets '((nil . (:maxlevel . 10)))))
  144. (org-refile-use-outline-path
  145. (if (caar org-refile-targets) 'file t))
  146. (spos (org-refile-get-location "Entry: "))
  147. (pom (and spos (move-marker (make-marker) (nth 3 spos)
  148. (get-file-buffer (nth 1 spos))))))
  149. (org-id-get pom 'create)
  150. (move-marker pom nil)))
  151. (defun org-id-get-with-outline-drilling (&optional targets)
  152. "Use an outline-cycling interface to retrieve the ID of an entry.
  153. This only finds entries in the current buffer, using `org-get-location'.
  154. It returns the ID of the entry. If necessary, the ID is created."
  155. (let* ((spos (org-get-location (current-buffer) org-goto-help))
  156. (pom (and spos (move-marker (make-marker) (car spos)))))
  157. (org-id-get pom 'create)
  158. (move-marker pom nil)))
  159. (defun org-id-goto (id)
  160. "Switch to the buffer containing the entry with id ID.
  161. Move the cursor to that entry in that buffer."
  162. (interactive)
  163. (let ((m (org-id-find id 'marker)))
  164. (unless m
  165. (error "Cannot find entry with ID \"%s\"" id))
  166. (switch-to-buffer (marker-buffer m))
  167. (goto-char m)
  168. (org-show-context)))
  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 markerp."
  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 type creator n xrandom)
  187. "Create a new globally unique ID.
  188. The ID is a string with four colon-separated parts:
  189. 1. The type or prefix, given by the argument TYPE, or by the first element
  190. 2. The creator, given by the argument CREATOR, or by the second element
  191. of `org-id-structure' (default is the user's login name).
  192. 3. An incremental number linked to the ID type. This is a number that
  193. runs for each ID type from 1 up, each time a new ID is created.
  194. Org-mode keeps track of these numbers in the file `org-id-tracking-file',
  195. so if you only work on a single computer or synchronize this file,
  196. this is enough as a unique identifier. If you work with other people,
  197. or on different computers, the uniqueness of this number is not certain.
  198. A specific value for N can be forces by passing it into the function.
  199. 4. An extra string guaranteeing the uniqueness of the ID.
  200. This is either a random string of XRANDOM characters if XRANDOM is an
  201. integer. If XRANDOM is the symbol `md5', the extra string is a MD5 digest
  202. of a string consisting of ID info, the current time, and a random number.
  203. If you are sure the sequence number (component 3) is unique in your
  204. setting, the random part can be omitted from the ID.
  205. So a typical ID could look like \"Org:dominik:105:2HtZ\"."
  206. (unless n (org-id-load))
  207. (let* ((type (or type (car org-id-structure)))
  208. (creator (or creator (nth 1 org-id-structure)))
  209. (n-p n)
  210. (key (concat type ":" creator))
  211. (ass (and (not n-p) (assoc key org-id-values)))
  212. (n (or n (1+ (or (cdr ass) 0))))
  213. (xrandom (or xrandom (nth 3 org-id-structure)))
  214. (random
  215. (cond
  216. ((not xrandom) nil)
  217. ((eq xrandom 'none) nil)
  218. ((integerp xrandom) (org-id-random-string xrandom))
  219. ((eq xrandom 'md5) (org-id-md5 type creator n))
  220. (t nil))))
  221. (unless n-p
  222. (if ass
  223. (setcdr ass n)
  224. (push (cons key n) org-id-values))
  225. (org-id-save))
  226. (concat type
  227. ":" creator
  228. ":" (number-to-string n)
  229. (if random (concat ":" random)))))
  230. (defun org-id-random-string (n)
  231. "Return a string of N random characters."
  232. (let ((rtn "") x)
  233. (while (>= (setq n (1- n)) 0)
  234. (setq x (random 62))
  235. (setq rtn (concat rtn (cond
  236. ((< x 10) (char-to-string (+ ?0 x)))
  237. ((< x 36) (char-to-string (+ ?A x -10)))
  238. ((< x 62) (char-to-string (+ ?a x -36)))
  239. (t (error "xxx"))))))
  240. rtn))
  241. (defun org-id-md5 (type creator n)
  242. "Return the md5 digest of a string.
  243. This function concatenates ID info with random stuff like the time
  244. and then computes the md5 digest. The result should be unique."
  245. (md5 (concat type ":" creator ":" (number-to-string (or n 0)) ":"
  246. (system-name) ":"
  247. (prin1-to-string (current-time)) ":"
  248. (number-to-string (random)))))
  249. ;; Storing id indices
  250. (defun org-id-save ()
  251. "Save `org-id-values' in `org-id-tracking-file'."
  252. (with-temp-file org-id-tracking-file
  253. (print org-id-values (current-buffer))))
  254. (defun org-id-load ()
  255. "Read the data from `org-id-tracking-file'."
  256. (setq org-id-values nil)
  257. (with-temp-buffer
  258. (condition-case nil
  259. (progn
  260. (insert-file-contents-literally org-id-tracking-file)
  261. (goto-char (point-min))
  262. (setq org-id-values (read (current-buffer))))
  263. (error
  264. (message "Could not read org-id-values from %s. Setting it to nil."
  265. org-id-tracking-file)))))
  266. ;; Storing ID locations (files)
  267. (defun org-id-update-id-locations ()
  268. "Scan relevant files for ID's.
  269. Store the relation between files and corresponding ID's."
  270. (interactive)
  271. (let ((files (append (org-agenda-files)
  272. (if (symbolp org-id-extra-files)
  273. (symbol-value org-id-extra-files)
  274. org-id-extra-files)))
  275. org-agenda-new-buffers
  276. file ids reg found id)
  277. (while (setq file (pop files))
  278. (setq ids nil)
  279. (with-current-buffer (org-get-agenda-file-buffer file)
  280. (save-excursion
  281. (save-restriction
  282. (widen)
  283. (goto-char (point-min))
  284. (while (re-search-forward "^[ \t]*:ID:[ \t]+\\(\\S-+\\)[ \t]*$"
  285. nil t)
  286. (setq id (org-match-string-no-properties 1))
  287. (if (member id found)
  288. (error "Duplicate ID \"%s\"" id))
  289. (push id found)
  290. (push id ids))
  291. (push (cons file ids) reg)))))
  292. (org-release-buffers org-agenda-new-buffers)
  293. (setq org-agenda-new-buffers nil)
  294. (setq org-id-locations reg)
  295. (org-id-locations-save)))
  296. (defun org-id-locations-save ()
  297. "Save `org-id-locations' in `org-id-locations-file'."
  298. (with-temp-file org-id-locations-file
  299. (print org-id-locations (current-buffer))))
  300. (defun org-id-locations-load ()
  301. "Read the data from `org-id-locations-file'."
  302. (setq org-id-locations nil)
  303. (with-temp-buffer
  304. (condition-case nil
  305. (progn
  306. (insert-file-contents-literally org-id-locations-file)
  307. (goto-char (point-min))
  308. (setq org-id-locations (read (current-buffer))))
  309. (error
  310. (message "Could not read org-id-values from %s. Setting it to nil."
  311. org-id-locations-file)))))
  312. (defun org-id-add-location (id file)
  313. "Add the ID with location FILE to the database of ID loations."
  314. (unless org-id-locations (org-id-locations-load))
  315. (catch 'exit
  316. (let ((locs org-id-locations) list)
  317. (while (setq list (pop locs))
  318. (when (equal (file-truename file) (file-truename (car list)))
  319. (setcdr list (cons id (cdr list)))
  320. (throw 'exit t)))
  321. (push (list file id) org-id-locations))
  322. (org-id-locations-save)))
  323. ;; Finding entries with specified id
  324. (defun org-id-find-id-file (id)
  325. "Query the id database for the file in which this ID is located."
  326. (unless org-id-locations (org-id-locations-load))
  327. (catch 'found
  328. (mapc (lambda (x) (if (member id (cdr x))
  329. (throw 'found (car x))))
  330. org-id-locations)
  331. nil))
  332. (defun org-id-find-id-in-file (id file &optional markerp)
  333. "Return the position of the entry ID in FILE.
  334. If that files does not exist, or if it does not contain this ID,
  335. return nil.
  336. The position is returned as a cons cell (file-name . position). With
  337. optional argument MARKERP, return the position as a marker."
  338. (let (org-agenda-new-buffers m buf pos)
  339. (cond
  340. ((not file) nil)
  341. ((not (file-exists-p file)) nil)
  342. (t (with-current-buffer (setq buf (org-get-agenda-file-buffer file))
  343. (setq pos (org-find-entry-with-id id))
  344. (when pos
  345. (if markerp
  346. (move-marker (make-marker) pos buf)
  347. (cons file pos))))))))
  348. (provide 'org-id)
  349. ;;; org-id.el ends here