1 ;;; org-id.el --- Global identifier for Org-mode entries
2 ;; Copyright (C) 2008 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 ID's at the same time in files that will eventually be used
39 ;; together. As an exernal 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."
81 (defcustom org-id-method
'org
82 "The method that should be used to create new ID's.
84 An ID will consist of the prefix specified in `org-id-prefix', and a unique
85 part created by the method this variable specifies.
89 org Org's own internal method, using an encoding of the current time,
90 and the current domain of the computer. This method will
91 honor the variable `org-id-include-domain'.
93 uuidgen Call the external command uuidgen."
96 (const :tag
"Org's internal method" org
)
97 (const :tag
"external: uuidgen" uuidgen
)))
99 (defcustom org-id-prefix
"Org"
102 This may be a string, or it can be nil to indicate that no prefix is required.
103 When a string, the string should have no space characters as IDs are expected
104 to have no space characters in them."
107 (const :tag
"No prefix")
108 (string :tag
"Prefix")))
110 (defcustom org-id-include-domain t
111 "Non-nil means, add the domain name to new IDs.
112 This ensures global uniqueness of ID's, and is also suggested by
113 RFC 2445 in combination with RFC 822. This is only relevant if
114 `org-id-method' is `org'. When uuidgen is used, the domain will never
119 (defcustom org-id-locations-file
"~/.org-id-locations"
120 "The file for remembering the last ID number generated."
124 (defvar org-id-locations nil
125 "List of files with ID's in those files.")
127 (defcustom org-id-extra-files
'org-agenda-text-search-extra-files
128 "Files to be searched for ID's, besides the agenda files."
132 (symbol :tag
"Variable")
133 (repeat :tag
"List of files"
136 ;;; The API functions
139 (defun org-id-get-create (&optional force
)
140 "Create an ID for the current entry and return it.
141 If the entry already has an ID, just return it.
142 With optional argument FORCE, force the creation of a new ID."
145 (org-entry-put (point) "ID" nil
))
146 (org-id-get (point) 'create
))
149 (defun org-id-copy ()
150 "Copy the ID of the entry at point to the kill ring.
151 Create an ID if necessary."
153 (kill-new (org-id-get nil
'create
)))
156 (defun org-id-get (&optional pom create prefix
)
157 "Get the ID property of the entry at point-or-marker POM.
158 If POM is nil, refer to the entry at point.
159 If the entry does not have an ID, the function returns nil.
160 However, when CREATE is non nil, create an ID if none is present already.
161 PREFIX will be passed through to `org-id-new'.
162 In any case, the ID of the entry is returned."
163 (let ((id (org-entry-get pom
"ID")))
165 ((and id
(stringp id
) (string-match "\\S-" id
))
168 (setq id
(org-id-new prefix
))
169 (org-entry-put pom
"ID" id
)
170 (org-id-add-location id
(buffer-file-name (buffer-base-buffer)))
175 (defun org-id-get-with-outline-path-completion (&optional targets
)
176 "Use outline-path-completion to retrieve the ID of an entry.
177 TARGETS may be a setting for `org-refile-targets' to define the eligible
178 headlines. When omitted, all headlines in all agenda files are
180 It returns the ID of the entry. If necessary, the ID is created."
181 (let* ((org-refile-targets (or targets
'((nil .
(:maxlevel .
10)))))
182 (org-refile-use-outline-path
183 (if (caar org-refile-targets
) 'file t
))
184 (spos (org-refile-get-location "Entry: "))
185 (pom (and spos
(move-marker (make-marker) (nth 3 spos
)
186 (get-file-buffer (nth 1 spos
))))))
187 (prog1 (org-id-get pom
'create
)
188 (move-marker pom nil
))))
191 (defun org-id-get-with-outline-drilling (&optional targets
)
192 "Use an outline-cycling interface to retrieve the ID of an entry.
193 This only finds entries in the current buffer, using `org-get-location'.
194 It returns the ID of the entry. If necessary, the ID is created."
195 (let* ((spos (org-get-location (current-buffer) org-goto-help
))
196 (pom (and spos
(move-marker (make-marker) (car spos
)))))
197 (prog1 (org-id-get pom
'create
)
198 (move-marker pom nil
))))
201 (defun org-id-goto (id)
202 "Switch to the buffer containing the entry with id ID.
203 Move the cursor to that entry in that buffer."
205 (let ((m (org-id-find id
'marker
)))
207 (error "Cannot find entry with ID \"%s\"" id
))
208 (switch-to-buffer (marker-buffer m
))
214 (defun org-id-find (id &optional markerp
)
215 "Return the location of the entry with the id ID.
216 The return value is a cons cell (file-name . position), or nil
217 if there is no entry with that ID.
218 With optional argument MARKERP, return the position as a new marker."
219 (let ((file (org-id-find-id-file id
))
220 org-agenda-new-buffers where
)
222 (setq where
(org-id-find-id-in-file id file markerp
)))
224 (org-id-update-id-locations)
225 (setq file
(org-id-find-id-file id
))
227 (setq where
(org-id-find-id-in-file id file markerp
))))
230 ;;; Internal functions
234 (defun org-id-new (&optional prefix
)
235 "Create a new globally unique ID.
237 An ID consists of two parts separated by a colon:
239 - a unique part that will be created according to `org-id-method'.
241 PREFIX can specify the prefix, the default is given by the variable
242 `org-id-prefix'. However, if PREFIX is the symbol `none', don't use any
243 prefix even if `org-id-prefix' specifies one.
245 So a typical ID could look like \"Org:4nd91V40HI\"."
246 (let* ((prefix (if (eq prefix
'none
)
248 (concat (or prefix org-id-prefix
) ":")))
250 (if (equal prefix
":") (setq prefix
""))
252 ((eq org-id-method
'uuidgen
)
253 (setq unique
(substring (shell-command-to-string "uuidgen") 1 -
1)))
254 ((eq org-id-method
'org
)
255 (let* ((etime (org-id-time-to-b62))
256 (postfix (if org-id-include-domain
259 (concat "@" (message-make-fqdn))))))
260 (setq unique
(concat etime postfix
))))
261 (t (error "Invalid `org-id-method'")))
262 (concat prefix unique
)))
264 (defun org-id-int-to-b62-one-digit (i)
265 "Turn an integer between 0 and 61 into a single character 0..9, A..Z, a..z."
268 ((< i
36) (+ ?A i -
10))
269 ((< i
62) (+ ?a i -
36))
270 (t (error "Larger that 61"))))
272 (defun org-id-b62-to-int-one-digit (i)
273 "Turn a character 0..9, A..Z, a..z into a number 0..61.
274 The input I may be a character, or a single-letter string."
275 (and (stringp i
) (setq i
(string-to-char i
)))
277 ((and (>= i ?
0) (<= i ?
9)) (- i ?
0))
278 ((and (>= i ?A
) (<= i ?Z
)) (+ (- i ?A
) 10))
279 ((and (>= i ?a
) (<= i ?z
)) (+ (- i ?a
) 36))
280 (t (error "Invalid b62 letter"))))
282 (defun org-id-int-to-b62 (i &optional length
)
283 "Convert an integer to a base-62 number represented as a string."
286 (setq s
(concat (char-to-string
287 (org-id-int-to-b62-one-digit (mod i
62))) s
)
289 (setq length
(max 1 (or length
1)))
290 (if (< (length s
) length
)
291 (setq s
(concat (make-string (- length
(length s
)) ?
0) s
)))
294 (defun org-id-b62-to-int (s)
295 "Convert a base-62 string into the corresponding integer."
297 (mapc (lambda (i) (setq r
(+ (* r
62) (org-id-b62-to-int-one-digit i
))))
301 (defun org-id-time-to-b62 (&optional time
)
302 "Encode TIME as a 10-digit string.
303 This string holds the time to micro-second accuracy, and can be decoded
304 using `org-id-decode'."
305 (setq time
(or time
(current-time)))
306 (concat (org-id-int-to-b62 (nth 0 time
) 3)
307 (org-id-int-to-b62 (nth 1 time
) 3)
308 (org-id-int-to-b62 (or (nth 2 time
) 0) 4)))
310 (defun org-id-decode (id)
311 "Split ID into the prefix and the time value that was used to create it.
312 The return value is (prefix . time) where PREFIX is nil or a string,
313 and time is the usual three-integer representation of time."
314 (let (prefix time parts
)
315 (setq parts
(org-split-string id
":"))
316 (if (= 2 (length parts
))
317 (setq prefix
(car parts
) time
(nth 1 parts
))
318 (setq prefix nil time
(nth 0 parts
)))
319 (setq time
(list (org-id-b62-to-int (substring time
0 3))
320 (org-id-b62-to-int (substring time
3 6))
321 (org-id-b62-to-int (substring time
6 10))))
324 ;; Storing ID locations (files)
326 (defun org-id-update-id-locations ()
327 "Scan relevant files for ID's.
328 Store the relation between files and corresponding ID's."
330 (let ((files (append (org-agenda-files)
331 (if (symbolp org-id-extra-files
)
332 (symbol-value org-id-extra-files
)
333 org-id-extra-files
)))
334 org-agenda-new-buffers
335 file ids reg found id
)
336 (while (setq file
(pop files
))
338 (with-current-buffer (org-get-agenda-file-buffer file
)
342 (goto-char (point-min))
343 (while (re-search-forward "^[ \t]*:ID:[ \t]+\\(\\S-+\\)[ \t]*$"
345 (setq id
(org-match-string-no-properties 1))
346 (if (member id found
)
347 (error "Duplicate ID \"%s\"" id
))
350 (push (cons file ids
) reg
)))))
351 (org-release-buffers org-agenda-new-buffers
)
352 (setq org-agenda-new-buffers nil
)
353 (setq org-id-locations reg
)
354 (org-id-locations-save)))
356 (defun org-id-locations-save ()
357 "Save `org-id-locations' in `org-id-locations-file'."
358 (with-temp-file org-id-locations-file
359 (print org-id-locations
(current-buffer))))
361 (defun org-id-locations-load ()
362 "Read the data from `org-id-locations-file'."
363 (setq org-id-locations nil
)
367 (insert-file-contents-literally org-id-locations-file
)
368 (goto-char (point-min))
369 (setq org-id-locations
(read (current-buffer))))
371 (message "Could not read org-id-values from %s. Setting it to nil."
372 org-id-locations-file
)))))
374 (defun org-id-add-location (id file
)
375 "Add the ID with location FILE to the database of ID loations."
376 (unless org-id-locations
(org-id-locations-load))
378 (let ((locs org-id-locations
) list
)
379 (while (setq list
(pop locs
))
380 (when (equal (file-truename file
) (file-truename (car list
)))
381 (setcdr list
(cons id
(cdr list
)))
383 (push (list file id
) org-id-locations
))
384 (org-id-locations-save))
386 ;; Finding entries with specified id
388 (defun org-id-find-id-file (id)
389 "Query the id database for the file in which this ID is located."
390 (unless org-id-locations
(org-id-locations-load))
392 (mapc (lambda (x) (if (member id
(cdr x
))
393 (throw 'found
(car x
))))
397 (defun org-id-find-id-in-file (id file
&optional markerp
)
398 "Return the position of the entry ID in FILE.
399 If that files does not exist, or if it does not contain this ID,
401 The position is returned as a cons cell (file-name . position). With
402 optional argument MARKERP, return the position as a new marker."
403 (let (org-agenda-new-buffers m buf pos
)
406 ((not (file-exists-p file
)) nil
)
407 (t (with-current-buffer (setq buf
(org-get-agenda-file-buffer file
))
408 (setq pos
(org-find-entry-with-id id
))
411 (move-marker (make-marker) pos buf
)
412 (cons file pos
))))))))
416 ;;; org-id.el ends here
418 ;; arch-tag: e5abaca4-e16f-4b25-832a-540cfb63a712