Release 6.12a
[org-mode.git] / lisp / org-id.el
blob764c0254e75352ef0bcf029bd97840e13cabcee1
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.12a
8 ;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
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
40 ;; on the system.
42 ;; This file defines the following API:
44 ;; org-id-get-create
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.
50 ;; org-id-get
51 ;; Get the ID property of an entry. Using appropriate arguments
52 ;; to the function, it can also create the ID for this entry.
54 ;; org-id-goto
55 ;; Command to go to a specific ID, this command can be used
56 ;; interactively.
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.
66 ;; org-id-find
67 ;; Find the location of an entry with specific id.
70 (require 'org)
72 (declare-function message-make-fqdn "message" ())
74 ;;; Customization
76 (defgroup org-id nil
77 "Options concerning global entry identifiers in Org-mode."
78 :tag "Org ID"
79 :group 'org)
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.
87 Allowed values are:
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."
94 :group 'org-id
95 :type '(choice
96 (const :tag "Org's internal method" org)
97 (const :tag "external: uuidgen" uuidgen)))
99 (defcustom org-id-prefix nil
100 "The prefix for IDs.
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."
105 :group 'org-id
106 :type '(choice
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
115 be added."
116 :group 'org-id
117 :type 'boolean)
119 (defcustom org-id-locations-file (convert-standard-filename
120 "~/.org-id-locations")
121 "The file for remembering the last ID number generated."
122 :group 'org-id
123 :type 'file)
125 (defvar org-id-locations nil
126 "List of files with ID's in those files.")
128 (defcustom org-id-extra-files 'org-agenda-text-search-extra-files
129 "Files to be searched for ID's, besides the agenda files."
130 :group 'org-id
131 :type
132 '(choice
133 (symbol :tag "Variable")
134 (repeat :tag "List of files"
135 (file))))
137 ;;; The API functions
139 ;;;###autoload
140 (defun org-id-get-create (&optional force)
141 "Create an ID for the current entry and return it.
142 If the entry already has an ID, just return it.
143 With optional argument FORCE, force the creation of a new ID."
144 (interactive "P")
145 (when force
146 (org-entry-put (point) "ID" nil))
147 (org-id-get (point) 'create))
149 ;;;###autoload
150 (defun org-id-copy ()
151 "Copy the ID of the entry at point to the kill ring.
152 Create an ID if necessary."
153 (interactive)
154 (kill-new (org-id-get nil 'create)))
156 ;;;###autoload
157 (defun org-id-get (&optional pom create prefix)
158 "Get the ID property of the entry at point-or-marker POM.
159 If POM is nil, refer to the entry at point.
160 If the entry does not have an ID, the function returns nil.
161 However, when CREATE is non nil, create an ID if none is present already.
162 PREFIX will be passed through to `org-id-new'.
163 In any case, the ID of the entry is returned."
164 (let ((id (org-entry-get pom "ID")))
165 (cond
166 ((and id (stringp id) (string-match "\\S-" id))
168 (create
169 (setq id (org-id-new prefix))
170 (org-entry-put pom "ID" id)
171 (org-id-add-location id (buffer-file-name (buffer-base-buffer)))
173 (t nil))))
175 ;;;###autoload
176 (defun org-id-get-with-outline-path-completion (&optional targets)
177 "Use outline-path-completion to retrieve the ID of an entry.
178 TARGETS may be a setting for `org-refile-targets' to define the eligible
179 headlines. When omitted, all headlines in all agenda files are
180 eligible.
181 It returns the ID of the entry. If necessary, the ID is created."
182 (let* ((org-refile-targets (or targets '((nil . (:maxlevel . 10)))))
183 (org-refile-use-outline-path
184 (if (caar org-refile-targets) 'file t))
185 (spos (org-refile-get-location "Entry: "))
186 (pom (and spos (move-marker (make-marker) (nth 3 spos)
187 (get-file-buffer (nth 1 spos))))))
188 (prog1 (org-id-get pom 'create)
189 (move-marker pom nil))))
191 ;;;###autoload
192 (defun org-id-get-with-outline-drilling (&optional targets)
193 "Use an outline-cycling interface to retrieve the ID of an entry.
194 This only finds entries in the current buffer, using `org-get-location'.
195 It returns the ID of the entry. If necessary, the ID is created."
196 (let* ((spos (org-get-location (current-buffer) org-goto-help))
197 (pom (and spos (move-marker (make-marker) (car spos)))))
198 (prog1 (org-id-get pom 'create)
199 (move-marker pom nil))))
201 ;;;###autoload
202 (defun org-id-goto (id)
203 "Switch to the buffer containing the entry with id ID.
204 Move the cursor to that entry in that buffer."
205 (interactive)
206 (let ((m (org-id-find id 'marker)))
207 (unless m
208 (error "Cannot find entry with ID \"%s\"" id))
209 (switch-to-buffer (marker-buffer m))
210 (goto-char m)
211 (move-marker m nil)
212 (org-show-context)))
214 ;;;###autoload
215 (defun org-id-find (id &optional markerp)
216 "Return the location of the entry with the id ID.
217 The return value is a cons cell (file-name . position), or nil
218 if there is no entry with that ID.
219 With optional argument MARKERP, return the position as a new marker."
220 (let ((file (org-id-find-id-file id))
221 org-agenda-new-buffers where)
222 (when file
223 (setq where (org-id-find-id-in-file id file markerp)))
224 (unless where
225 (org-id-update-id-locations)
226 (setq file (org-id-find-id-file id))
227 (when file
228 (setq where (org-id-find-id-in-file id file markerp))))
229 where))
231 ;;; Internal functions
233 ;; Creating new IDs
235 (defun org-id-new (&optional prefix)
236 "Create a new globally unique ID.
238 An ID consists of two parts separated by a colon:
239 - a prefix
240 - a unique part that will be created according to `org-id-method'.
242 PREFIX can specify the prefix, the default is given by the variable
243 `org-id-prefix'. However, if PREFIX is the symbol `none', don't use any
244 prefix even if `org-id-prefix' specifies one.
246 So a typical ID could look like \"Org:4nd91V40HI\"."
247 (let* ((prefix (if (eq prefix 'none)
249 (concat (or prefix org-id-prefix) ":")))
250 unique)
251 (if (equal prefix ":") (setq prefix ""))
252 (cond
253 ((eq org-id-method 'uuidgen)
254 (setq unique (org-trim (shell-command-to-string "uuidgen"))))
255 ((eq org-id-method 'org)
256 (let* ((etime (org-id-reverse-string (org-id-time-to-b36)))
257 (postfix (if org-id-include-domain
258 (progn
259 (require 'message)
260 (concat "@" (message-make-fqdn))))))
261 (setq unique (concat etime postfix))))
262 (t (error "Invalid `org-id-method'")))
263 (concat prefix unique)))
265 (defun org-id-reverse-string (s)
266 (mapconcat 'char-to-string (nreverse (string-to-list s)) ""))
268 (defun org-id-int-to-b36-one-digit (i)
269 "Turn an integer between 0 and 61 into a single character 0..9, A..Z, a..z."
270 (cond
271 ((< i 10) (+ ?0 i))
272 ((< i 36) (+ ?a i -10))
273 (t (error "Larger that 35"))))
275 (defun org-id-b36-to-int-one-digit (i)
276 "Turn a character 0..9, A..Z, a..z into a number 0..61.
277 The input I may be a character, or a single-letter string."
278 (and (stringp i) (setq i (string-to-char i)))
279 (cond
280 ((and (>= i ?0) (<= i ?9)) (- i ?0))
281 ((and (>= i ?a) (<= i ?z)) (+ (- i ?a) 10))
282 (t (error "Invalid b36 letter"))))
284 (defun org-id-int-to-b36 (i &optional length)
285 "Convert an integer to a base-36 number represented as a string."
286 (let ((s ""))
287 (while (> i 0)
288 (setq s (concat (char-to-string
289 (org-id-int-to-b36-one-digit (mod i 36))) s)
290 i (/ i 36)))
291 (setq length (max 1 (or length 1)))
292 (if (< (length s) length)
293 (setq s (concat (make-string (- length (length s)) ?0) s)))
296 (defun org-id-b36-to-int (s)
297 "Convert a base-36 string into the corresponding integer."
298 (let ((r 0))
299 (mapc (lambda (i) (setq r (+ (* r 36) (org-id-b36-to-int-one-digit i))))
303 (defun org-id-time-to-b36 (&optional time)
304 "Encode TIME as a 10-digit string.
305 This string holds the time to micro-second accuracy, and can be decoded
306 using `org-id-decode'."
307 (setq time (or time (current-time)))
308 (concat (org-id-int-to-b36 (nth 0 time) 4)
309 (org-id-int-to-b36 (nth 1 time) 4)
310 (org-id-int-to-b36 (or (nth 2 time) 0) 4)))
312 (defun org-id-decode (id)
313 "Split ID into the prefix and the time value that was used to create it.
314 The return value is (prefix . time) where PREFIX is nil or a string,
315 and time is the usual three-integer representation of time."
316 (let (prefix time parts)
317 (setq parts (org-split-string id ":"))
318 (if (= 2 (length parts))
319 (setq prefix (car parts) time (nth 1 parts))
320 (setq prefix nil time (nth 0 parts)))
321 (setq time (org-id-reverse-string time))
322 (setq time (list (org-id-b36-to-int (substring time 0 4))
323 (org-id-b36-to-int (substring time 4 8))
324 (org-id-b36-to-int (substring time 8 12))))
325 (cons prefix time)))
327 ;; Storing ID locations (files)
329 (defun org-id-update-id-locations ()
330 "Scan relevant files for ID's.
331 Store the relation between files and corresponding ID's."
332 (interactive)
333 (let ((files (append (org-agenda-files)
334 (if (symbolp org-id-extra-files)
335 (symbol-value org-id-extra-files)
336 org-id-extra-files)))
337 org-agenda-new-buffers
338 file ids reg found id)
339 (while (setq file (pop files))
340 (setq ids nil)
341 (with-current-buffer (org-get-agenda-file-buffer file)
342 (save-excursion
343 (save-restriction
344 (widen)
345 (goto-char (point-min))
346 (while (re-search-forward "^[ \t]*:ID:[ \t]+\\(\\S-+\\)[ \t]*$"
347 nil t)
348 (setq id (org-match-string-no-properties 1))
349 (if (member id found)
350 (error "Duplicate ID \"%s\"" id))
351 (push id found)
352 (push id ids))
353 (push (cons file ids) reg)))))
354 (org-release-buffers org-agenda-new-buffers)
355 (setq org-agenda-new-buffers nil)
356 (setq org-id-locations reg)
357 (org-id-locations-save)))
359 (defun org-id-locations-save ()
360 "Save `org-id-locations' in `org-id-locations-file'."
361 (with-temp-file org-id-locations-file
362 (print org-id-locations (current-buffer))))
364 (defun org-id-locations-load ()
365 "Read the data from `org-id-locations-file'."
366 (setq org-id-locations nil)
367 (with-temp-buffer
368 (condition-case nil
369 (progn
370 (insert-file-contents-literally org-id-locations-file)
371 (goto-char (point-min))
372 (setq org-id-locations (read (current-buffer))))
373 (error
374 (message "Could not read org-id-values from %s. Setting it to nil."
375 org-id-locations-file)))))
377 (defun org-id-add-location (id file)
378 "Add the ID with location FILE to the database of ID loations."
379 (unless org-id-locations (org-id-locations-load))
380 (catch 'exit
381 (let ((locs org-id-locations) list)
382 (while (setq list (pop locs))
383 (when (equal (file-truename file) (file-truename (car list)))
384 (setcdr list (cons id (cdr list)))
385 (throw 'exit t))))
386 (push (list file id) org-id-locations))
387 (org-id-locations-save))
389 ;; Finding entries with specified id
391 (defun org-id-find-id-file (id)
392 "Query the id database for the file in which this ID is located."
393 (unless org-id-locations (org-id-locations-load))
394 (catch 'found
395 (mapc (lambda (x) (if (member id (cdr x))
396 (throw 'found (car x))))
397 org-id-locations)
398 nil))
400 (defun org-id-find-id-in-file (id file &optional markerp)
401 "Return the position of the entry ID in FILE.
402 If that files does not exist, or if it does not contain this ID,
403 return nil.
404 The position is returned as a cons cell (file-name . position). With
405 optional argument MARKERP, return the position as a new marker."
406 (let (org-agenda-new-buffers m buf pos)
407 (cond
408 ((not file) nil)
409 ((not (file-exists-p file)) nil)
410 (t (with-current-buffer (setq buf (org-get-agenda-file-buffer file))
411 (setq pos (org-find-entry-with-id id))
412 (when pos
413 (if markerp
414 (move-marker (make-marker) pos buf)
415 (cons file pos))))))))
417 (provide 'org-id)
419 ;;; org-id.el ends here
421 ;; arch-tag: e5abaca4-e16f-4b25-832a-540cfb63a712