1 ;;; org-id.el --- Global identifiers for Org-mode entries
2 ;; Copyright (C) 2008, 2009 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <carsten at orgmode dot org>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
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.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file implements globally unique identifiers for Org-mode entries.
28 ;; Identifiers are stored in the entry as an :ID: property. Functions
29 ;; are provided that create and retrieve such identifiers, and that find
30 ;; entries based on the identifier.
32 ;; Identifiers consist of a prefix (default "Org" given by the variable
33 ;; `org-id-prefix') and a unique part that can be created by a number
34 ;; of different methods, see the variable `org-id-method'.
35 ;; Org has a builtin method that uses a compact encoding of the creation
36 ;; time of the ID, with microsecond accuracy. This virtually
37 ;; guarantees globally unique identifiers, even if several people are
38 ;; creating IDs at the same time in files that will eventually be used
39 ;; together. As an external method `uuidgen' is supported, if installed
42 ;; This file defines the following API:
45 ;; Create an ID for the entry at point if it does not yet have one.
46 ;; Returns the ID (old or new). This function can be used
47 ;; interactively, with prefix argument the creation of a new ID is
48 ;; forced, even if there was an old one.
51 ;; Get the ID property of an entry. Using appropriate arguments
52 ;; to the function, it can also create the ID for this entry.
55 ;; Command to go to a specific ID, this command can be used
58 ;; org-id-get-with-outline-path-completion
59 ;; Retrieve the ID of an entry, using outline path completion.
60 ;; This function can work for multiple files.
62 ;; org-id-get-with-outline-drilling
63 ;; Retrieve the ID of an entry, using outline path completion.
64 ;; This function only works for the current file.
67 ;; Find the location of an entry with specific id.
72 (declare-function message-make-fqdn
"message" ())
77 "Options concerning global entry identifiers in Org-mode."
82 (defcustom org-id-method
84 (if (string-match "\\`[-0-9a-fA-F]\\{36\\}\\'"
85 (org-trim (shell-command-to-string "uuidgen")))
89 "The method that should be used to create new IDs.
91 If `uuidgen' is available on the system, it will be used as the default method.
92 if not, the method `org' is used.
93 An ID will consist of the optional prefix specified in `org-id-prefix',
94 and a unique part created by the method this variable specifies.
98 org Org's own internal method, using an encoding of the current time to
99 microsecond accuracy, and optionally the current domain of the
100 computer. See the variable `org-id-include-domain'.
102 uuidgen Call the external command uuidgen."
105 (const :tag
"Org's internal method" org
)
106 (const :tag
"external: uuidgen" uuidgen
)))
108 (defcustom org-id-prefix nil
111 This may be a string, or it can be nil to indicate that no prefix is required.
112 When a string, the string should have no space characters as IDs are expected
113 to have no space characters in them."
116 (const :tag
"No prefix")
117 (string :tag
"Prefix")))
119 (defcustom org-id-include-domain nil
120 "Non-nil means, add the domain name to new IDs.
121 This ensures global uniqueness of IDs, and is also suggested by
122 RFC 2445 in combination with RFC 822. This is only relevant if
123 `org-id-method' is `org'. When uuidgen is used, the domain will never
125 The default is to not use this because we have no really good way to get
126 the true domain, and Org entries will normally not be shared with enough
127 people to make this necessary."
131 (defcustom org-id-track-globally t
132 "Non-nil means, track IDs through files, so that links work globally.
133 This work by maintaining a hash table for IDs and writing this table
134 to disk when exiting Emacs. Because of this, it works best if you use
135 a single Emacs process, not many.
137 When nil, IDs are not tracked. Links to IDs will still work within
138 a buffer, but not if the entry is located in another file.
139 IDs can still be used if the entry with the id is in the same file as
144 (defcustom org-id-locations-file
(convert-standard-filename
145 "~/.emacs.d/.org-id-locations")
146 "The file for remembering in which file an ID was defined.
147 This variable is only relevant when `org-id-track-globally' is set."
151 (defvar org-id-locations nil
152 "List of files with IDs in those files.
153 Depending on `org-id-use-hash' this can also be a hash table mapping IDs
156 (defvar org-id-files nil
157 "List of files that contain IDs.")
159 (defcustom org-id-extra-files
'org-agenda-text-search-extra-files
160 "Files to be searched for IDs, besides the agenda files.
161 When Org reparses files to remake the list of files and IDs it is tracking,
162 it will normally scan the agenda files, the archives related to agenda files,
163 any files that are listed as ID containing in the current register, and
164 any Org-mode files currently visited by Emacs.
165 You can list additional files here.
166 This variable is only relevant when `org-id-track-globally' is set."
170 (symbol :tag
"Variable")
171 (repeat :tag
"List of files"
174 (defcustom org-id-search-archives t
175 "Non-nil means, search also the archive files of agenda files for entries.
176 This is a possibility to reduce overhead, but it means that entries moved
177 to the archives can no longer be found by ID.
178 This variable is only relevant when `org-id-track-globally' is set."
182 ;;; The API functions
185 (defun org-id-get-create (&optional force
)
186 "Create an ID for the current entry and return it.
187 If the entry already has an ID, just return it.
188 With optional argument FORCE, force the creation of a new ID."
191 (org-entry-put (point) "ID" nil
))
192 (org-id-get (point) 'create
))
195 (defun org-id-copy ()
196 "Copy the ID of the entry at point to the kill ring.
197 Create an ID if necessary."
199 (kill-new (org-id-get nil
'create
)))
202 (defun org-id-get (&optional pom create prefix
)
203 "Get the ID property of the entry at point-or-marker POM.
204 If POM is nil, refer to the entry at point.
205 If the entry does not have an ID, the function returns nil.
206 However, when CREATE is non nil, create an ID if none is present already.
207 PREFIX will be passed through to `org-id-new'.
208 In any case, the ID of the entry is returned."
209 (let ((id (org-entry-get pom
"ID")))
211 ((and id
(stringp id
) (string-match "\\S-" id
))
214 (setq id
(org-id-new prefix
))
215 (org-entry-put pom
"ID" id
)
216 (org-id-add-location id
(buffer-file-name (buffer-base-buffer)))
221 (defun org-id-get-with-outline-path-completion (&optional targets
)
222 "Use outline-path-completion to retrieve the ID of an entry.
223 TARGETS may be a setting for `org-refile-targets' to define the eligible
224 headlines. When omitted, all headlines in all agenda files are
226 It returns the ID of the entry. If necessary, the ID is created."
227 (let* ((org-refile-targets (or targets
'((nil .
(:maxlevel .
10)))))
228 (org-refile-use-outline-path
229 (if (caar org-refile-targets
) 'file t
))
230 (spos (org-refile-get-location "Entry: "))
231 (pom (and spos
(move-marker (make-marker) (nth 3 spos
)
232 (get-file-buffer (nth 1 spos
))))))
233 (prog1 (org-id-get pom
'create
)
234 (move-marker pom nil
))))
237 (defun org-id-get-with-outline-drilling (&optional targets
)
238 "Use an outline-cycling interface to retrieve the ID of an entry.
239 This only finds entries in the current buffer, using `org-get-location'.
240 It returns the ID of the entry. If necessary, the ID is created."
241 (let* ((spos (org-get-location (current-buffer) org-goto-help
))
242 (pom (and spos
(move-marker (make-marker) (car spos
)))))
243 (prog1 (org-id-get pom
'create
)
244 (move-marker pom nil
))))
247 (defun org-id-goto (id)
248 "Switch to the buffer containing the entry with id ID.
249 Move the cursor to that entry in that buffer."
250 (interactive "sID: ")
251 (let ((m (org-id-find id
'marker
)))
253 (error "Cannot find entry with ID \"%s\"" id
))
254 (switch-to-buffer (marker-buffer m
))
260 (defun org-id-find (id &optional markerp
)
261 "Return the location of the entry with the id ID.
262 The return value is a cons cell (file-name . position), or nil
263 if there is no entry with that ID.
264 With optional argument MARKERP, return the position as a new marker."
265 (let ((file (org-id-find-id-file id
))
266 org-agenda-new-buffers where
)
268 (setq where
(org-id-find-id-in-file id file markerp
)))
270 (org-id-update-id-locations)
271 (setq file
(org-id-find-id-file id
))
273 (setq where
(org-id-find-id-in-file id file markerp
))))
276 ;;; Internal functions
280 (defun org-id-new (&optional prefix
)
281 "Create a new globally unique ID.
283 An ID consists of two parts separated by a colon:
285 - a unique part that will be created according to `org-id-method'.
287 PREFIX can specify the prefix, the default is given by the variable
288 `org-id-prefix'. However, if PREFIX is the symbol `none', don't use any
289 prefix even if `org-id-prefix' specifies one.
291 So a typical ID could look like \"Org:4nd91V40HI\"."
292 (let* ((prefix (if (eq prefix
'none
)
294 (concat (or prefix org-id-prefix
) ":")))
296 (if (equal prefix
":") (setq prefix
""))
298 ((eq org-id-method
'uuidgen
)
299 (setq unique
(org-trim (shell-command-to-string "uuidgen"))))
300 ((eq org-id-method
'org
)
301 (let* ((etime (org-id-reverse-string (org-id-time-to-b36)))
302 (postfix (if org-id-include-domain
305 (concat "@" (message-make-fqdn))))))
306 (setq unique
(concat etime postfix
))))
307 (t (error "Invalid `org-id-method'")))
308 (concat prefix unique
)))
310 (defun org-id-reverse-string (s)
311 (mapconcat 'char-to-string
(nreverse (string-to-list s
)) ""))
313 (defun org-id-int-to-b36-one-digit (i)
314 "Turn an integer between 0 and 61 into a single character 0..9, A..Z, a..z."
317 ((< i
36) (+ ?a i -
10))
318 (t (error "Larger that 35"))))
320 (defun org-id-b36-to-int-one-digit (i)
321 "Turn a character 0..9, A..Z, a..z into a number 0..61.
322 The input I may be a character, or a single-letter string."
323 (and (stringp i
) (setq i
(string-to-char i
)))
325 ((and (>= i ?
0) (<= i ?
9)) (- i ?
0))
326 ((and (>= i ?a
) (<= i ?z
)) (+ (- i ?a
) 10))
327 (t (error "Invalid b36 letter"))))
329 (defun org-id-int-to-b36 (i &optional length
)
330 "Convert an integer to a base-36 number represented as a string."
333 (setq s
(concat (char-to-string
334 (org-id-int-to-b36-one-digit (mod i
36))) s
)
336 (setq length
(max 1 (or length
1)))
337 (if (< (length s
) length
)
338 (setq s
(concat (make-string (- length
(length s
)) ?
0) s
)))
341 (defun org-id-b36-to-int (s)
342 "Convert a base-36 string into the corresponding integer."
344 (mapc (lambda (i) (setq r
(+ (* r
36) (org-id-b36-to-int-one-digit i
))))
348 (defun org-id-time-to-b36 (&optional time
)
349 "Encode TIME as a 10-digit string.
350 This string holds the time to micro-second accuracy, and can be decoded
351 using `org-id-decode'."
352 (setq time
(or time
(current-time)))
353 (concat (org-id-int-to-b36 (nth 0 time
) 4)
354 (org-id-int-to-b36 (nth 1 time
) 4)
355 (org-id-int-to-b36 (or (nth 2 time
) 0) 4)))
357 (defun org-id-decode (id)
358 "Split ID into the prefix and the time value that was used to create it.
359 The return value is (prefix . time) where PREFIX is nil or a string,
360 and time is the usual three-integer representation of time."
361 (let (prefix time parts
)
362 (setq parts
(org-split-string id
":"))
363 (if (= 2 (length parts
))
364 (setq prefix
(car parts
) time
(nth 1 parts
))
365 (setq prefix nil time
(nth 0 parts
)))
366 (setq time
(org-id-reverse-string time
))
367 (setq time
(list (org-id-b36-to-int (substring time
0 4))
368 (org-id-b36-to-int (substring time
4 8))
369 (org-id-b36-to-int (substring time
8 12))))
372 ;; Storing ID locations (files)
374 (defun org-id-update-id-locations (&optional files
)
375 "Scan relevant files for IDs.
376 Store the relation between files and corresponding IDs.
377 This will scan all agenda files, all associated archives, and all
378 files currently mentioned in `org-id-locations'.
379 When FILES is given, scan these files instead.
380 When CHECK is given, prepare detailed information about duplicate IDs."
382 (if (not org-id-track-globally
)
383 (error "Please turn on `org-id-track-globally' if you want to track IDs.")
387 ;; Agenda files and all associated archives
388 (org-agenda-files t org-id-search-archives
)
389 ;; Explicit extra files
390 (if (symbolp org-id-extra-files
)
391 (symbol-value org-id-extra-files
)
393 ;; Files associated with live org-mode buffers
396 (with-current-buffer b
397 (and (org-mode-p) (buffer-file-name))))
399 ;; All files known to have IDs
401 org-agenda-new-buffers
402 file nfiles tfile ids reg found id seen
(ndup 0))
403 (setq nfiles
(length files
))
404 (while (setq file
(pop files
))
405 (message "Finding ID locations (%d/%d files): %s"
406 (- nfiles
(length files
)) nfiles file
)
407 (setq tfile
(file-truename file
))
408 (when (and (file-exists-p file
) (not (member tfile seen
)))
411 (with-current-buffer (org-get-agenda-file-buffer file
)
415 (goto-char (point-min))
416 (while (re-search-forward "^[ \t]*:ID:[ \t]+\\(\\S-+\\)[ \t]*$"
418 (setq id
(org-match-string-no-properties 1))
419 (if (member id found
)
421 (message "Duplicate ID \"%s\", also in file %s"
426 (if (member id
(cdr x
)) (car x
)))
431 (setq ndup
(1+ ndup
)))
434 (push (cons (abbreviate-file-name file
) ids
) reg
))))))
435 (org-release-buffers org-agenda-new-buffers
)
436 (setq org-agenda-new-buffers nil
)
437 (setq org-id-locations reg
)
438 (setq org-id-files
(mapcar 'car org-id-locations
))
439 (org-id-locations-save) ;; this function can also handle the alist form
440 ;; now convert to a hash
441 (setq org-id-locations
(org-id-alist-to-hash org-id-locations
))
443 (message "WARNING: %d duplicate IDs found, check *Messages* buffer" ndup
)
444 (message "%d unique files scanned for IDs" (length org-id-files
)))
447 (defun org-id-locations-save ()
448 "Save `org-id-locations' in `org-id-locations-file'."
449 (when org-id-track-globally
450 (let ((out (if (hash-table-p org-id-locations
)
451 (org-id-hash-to-alist org-id-locations
)
453 (with-temp-file org-id-locations-file
454 (print out
(current-buffer))))))
456 (defun org-id-locations-load ()
457 "Read the data from `org-id-locations-file'."
458 (setq org-id-locations nil
)
459 (when org-id-track-globally
463 (insert-file-contents-literally org-id-locations-file
)
464 (goto-char (point-min))
465 (setq org-id-locations
(read (current-buffer))))
467 (message "Could not read org-id-values from %s. Setting it to nil."
468 org-id-locations-file
))))
469 (setq org-id-files
(mapcar 'car org-id-locations
))
470 (setq org-id-locations
(org-id-alist-to-hash org-id-locations
))))
472 (defun org-id-add-location (id file
)
473 "Add the ID with location FILE to the database of ID locations."
474 ;; Only if global tracking is on, and when the buffer has a file
475 (when (and org-id-track-globally id file
)
476 (unless org-id-locations
(org-id-locations-load))
477 (puthash id
(abbreviate-file-name file
) org-id-locations
)
478 (add-to-list 'org-id-files
(abbreviate-file-name file
))))
480 (add-hook 'kill-emacs-hook
'org-id-locations-save
)
482 (defun org-id-hash-to-alist (hash)
483 "Turn an org-id hash into an alist, so that it can be written to a file."
487 (if (setq x
(member v res
))
488 (setcdr x
(cons k
(cdr x
)))
489 (push (list v k
) res
)))
493 (defun org-id-alist-to-hash (list)
494 "Turn an org-id location list into a hash table."
495 (let ((res (make-hash-table
497 :size
(apply '+ (mapcar 'length list
))))
502 (mapc (lambda (i) (puthash i f res
)) (cdr x
)))
506 (defun org-id-paste-tracker (txt &optional buffer-or-file
)
507 "Update any IDs in TXT and assign BUFFER-OR-FILE to them."
508 (when org-id-track-globally
510 (setq buffer-or-file
(or buffer-or-file
(current-buffer)))
511 (when (bufferp buffer-or-file
)
512 (setq buffer-or-file
(or (buffer-base-buffer buffer-or-file
)
514 (setq buffer-or-file
(buffer-file-name buffer-or-file
)))
516 (let ((fname (abbreviate-file-name buffer-or-file
))
518 (while (string-match "^[ \t]*:ID:[ \t]+\\([^ \t\n\r]+\\)" txt s
)
519 (setq s
(match-end 0))
520 (org-id-add-location (match-string 1 txt
) fname
)))))))
522 ;; Finding entries with specified id
524 (defun org-id-find-id-file (id)
525 "Query the id database for the file in which this ID is located."
526 (unless org-id-locations
(org-id-locations-load))
527 (or (gethash id org-id-locations
)
528 ;; ball back on current buffer
529 (buffer-file-name (or (buffer-base-buffer (current-buffer))
532 (defun org-id-find-id-in-file (id file
&optional markerp
)
533 "Return the position of the entry ID in FILE.
534 If that files does not exist, or if it does not contain this ID,
536 The position is returned as a cons cell (file-name . position). With
537 optional argument MARKERP, return the position as a new marker."
538 (let (org-agenda-new-buffers m buf pos
)
541 ((not (file-exists-p file
)) nil
)
542 (t (with-current-buffer (setq buf
(org-get-agenda-file-buffer file
))
543 (setq pos
(org-find-entry-with-id id
))
546 (move-marker (make-marker) pos buf
)
547 (cons file pos
))))))))
551 ;; Calling the following function is hard-coded into `org-store-link',
552 ;; so we do have to add it to `org-store-link-functions'.
554 (defun org-id-store-link ()
555 "Store a link to the current entry, using it's ID."
557 (let* ((link (org-make-link "id:" (org-id-get-create)))
558 (desc (save-excursion
559 (org-back-to-heading t
)
560 (or (and (looking-at org-complex-heading-regexp
)
561 (if (match-end 4) (match-string 4) (match-string 0)))
563 (org-store-link-props :link link
:description desc
:type
"id")
566 (defun org-id-open (id)
567 "Go to the entry with id ID."
569 (let ((m (org-id-find id
'marker
)))
571 (error "Cannot find entry with ID \"%s\"" id
))
572 (if (not (equal (current-buffer) (marker-buffer m
)))
573 (switch-to-buffer-other-window (marker-buffer m
)))
578 (org-add-link-type "id" 'org-id-open
)
582 ;;; org-id.el ends here
584 ;; arch-tag: e5abaca4-e16f-4b25-832a-540cfb63a712