org.texi: Fix typo
[org-mode.git] / lisp / org-id.el
blob107ac39a3516b16051eb78f186566454b27e1ffd
1 ;;; org-id.el --- Global identifiers for Org-mode entries
2 ;;
3 ;; Copyright (C) 2008-2016 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
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 IDs at the same time in files that will eventually be used
39 ;; together.
41 ;; By default Org uses UUIDs as global unique identifiers.
43 ;; This file defines the following API:
45 ;; org-id-get-create
46 ;; Create an ID for the entry at point if it does not yet have one.
47 ;; Returns the ID (old or new). This function can be used
48 ;; interactively, with prefix argument the creation of a new ID is
49 ;; forced, even if there was an old one.
51 ;; org-id-get
52 ;; Get the ID property of an entry. Using appropriate arguments
53 ;; to the function, it can also create the ID for this entry.
55 ;; org-id-goto
56 ;; Command to go to a specific ID, this command can be used
57 ;; interactively.
59 ;; org-id-get-with-outline-path-completion
60 ;; Retrieve the ID of an entry, using outline path completion.
61 ;; This function can work for multiple files.
63 ;; org-id-get-with-outline-drilling
64 ;; Retrieve the ID of an entry, using outline path completion.
65 ;; This function only works for the current file.
67 ;; org-id-find
68 ;; Find the location of an entry with specific id.
71 ;;; Code:
73 (require 'org)
75 (declare-function message-make-fqdn "message" ())
76 (declare-function org-pop-to-buffer-same-window
77 "org-compat" (&optional buffer-or-name norecord label))
79 ;;; Customization
81 (defgroup org-id nil
82 "Options concerning global entry identifiers in Org-mode."
83 :tag "Org ID"
84 :group 'org)
86 (define-obsolete-variable-alias
87 'org-link-to-org-use-id 'org-id-link-to-org-use-id "24.3")
88 (defcustom org-id-link-to-org-use-id nil
89 "Non-nil means storing a link to an Org file will use entry IDs.
90 \\<org-mode-map>\
92 The variable can have the following values:
94 t Create an ID if needed to make a link to the current entry.
96 create-if-interactive
97 If `org-store-link' is called directly (interactively, as a user
98 command), do create an ID to support the link. But when doing the
99 job for capture, only use the ID if it already exists. The
100 purpose of this setting is to avoid proliferation of unwanted
101 IDs, just because you happen to be in an Org file when you
102 call `org-capture' that automatically and preemptively creates a
103 link. If you do want to get an ID link in a capture template to
104 an entry not having an ID, create it first by explicitly creating
105 a link to it, using `\\[org-insert-link]' first.
107 create-if-interactive-and-no-custom-id
108 Like create-if-interactive, but do not create an ID if there is
109 a CUSTOM_ID property defined in the entry.
111 use-existing
112 Use existing ID, do not create one.
114 nil Never use an ID to make a link, instead link using a text search for
115 the headline text."
116 :group 'org-link-store
117 :group 'org-id
118 :version "24.3"
119 :type '(choice
120 (const :tag "Create ID to make link" t)
121 (const :tag "Create if storing link interactively"
122 create-if-interactive)
123 (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
124 create-if-interactive-and-no-custom-id)
125 (const :tag "Only use existing" use-existing)
126 (const :tag "Do not use ID to create link" nil)))
128 (defcustom org-id-uuid-program "uuidgen"
129 "The uuidgen program."
130 :group 'org-id
131 :type 'string)
133 (defcustom org-id-method 'uuid
134 "The method that should be used to create new IDs.
136 An ID will consist of the optional prefix specified in `org-id-prefix',
137 and a unique part created by the method this variable specifies.
139 Allowed values are:
141 org Org's own internal method, using an encoding of the current time to
142 microsecond accuracy, and optionally the current domain of the
143 computer. See the variable `org-id-include-domain'.
145 uuid Create random (version 4) UUIDs. If the program defined in
146 `org-id-uuid-program' is available it is used to create the ID.
147 Otherwise an internal functions is used."
148 :group 'org-id
149 :type '(choice
150 (const :tag "Org's internal method" org)
151 (const :tag "external: uuidgen" uuid)))
153 (defcustom org-id-prefix nil
154 "The prefix for IDs.
156 This may be a string, or it can be nil to indicate that no prefix is required.
157 When a string, the string should have no space characters as IDs are expected
158 to have no space characters in them."
159 :group 'org-id
160 :type '(choice
161 (const :tag "No prefix")
162 (string :tag "Prefix")))
164 (defcustom org-id-include-domain nil
165 "Non-nil means add the domain name to new IDs.
166 This ensures global uniqueness of IDs, and is also suggested by
167 RFC 2445 in combination with RFC 822. This is only relevant if
168 `org-id-method' is `org'. When uuidgen is used, the domain will never
169 be added.
170 The default is to not use this because we have no really good way to get
171 the true domain, and Org entries will normally not be shared with enough
172 people to make this necessary."
173 :group 'org-id
174 :type 'boolean)
176 (defcustom org-id-track-globally t
177 "Non-nil means track IDs through files, so that links work globally.
178 This work by maintaining a hash table for IDs and writing this table
179 to disk when exiting Emacs. Because of this, it works best if you use
180 a single Emacs process, not many.
182 When nil, IDs are not tracked. Links to IDs will still work within
183 a buffer, but not if the entry is located in another file.
184 IDs can still be used if the entry with the id is in the same file as
185 the link."
186 :group 'org-id
187 :type 'boolean)
189 (defcustom org-id-locations-file (convert-standard-filename
190 (concat user-emacs-directory ".org-id-locations"))
191 "The file for remembering in which file an ID was defined.
192 This variable is only relevant when `org-id-track-globally' is set."
193 :group 'org-id
194 :type 'file)
196 (defvar org-id-locations nil
197 "List of files with IDs in those files.")
199 (defvar org-id-files nil
200 "List of files that contain IDs.")
202 (defcustom org-id-extra-files 'org-agenda-text-search-extra-files
203 "Files to be searched for IDs, besides the agenda files.
204 When Org reparses files to remake the list of files and IDs it is tracking,
205 it will normally scan the agenda files, the archives related to agenda files,
206 any files that are listed as ID containing in the current register, and
207 any Org-mode files currently visited by Emacs.
208 You can list additional files here.
209 This variable is only relevant when `org-id-track-globally' is set."
210 :group 'org-id
211 :type
212 '(choice
213 (symbol :tag "Variable")
214 (repeat :tag "List of files"
215 (file))))
217 (defcustom org-id-search-archives t
218 "Non-nil means search also the archive files of agenda files for entries.
219 This is a possibility to reduce overhead, but it means that entries moved
220 to the archives can no longer be found by ID.
221 This variable is only relevant when `org-id-track-globally' is set."
222 :group 'org-id
223 :type 'boolean)
225 ;;; The API functions
227 ;;;###autoload
228 (defun org-id-get-create (&optional force)
229 "Create an ID for the current entry and return it.
230 If the entry already has an ID, just return it.
231 With optional argument FORCE, force the creation of a new ID."
232 (interactive "P")
233 (when force
234 (org-entry-put (point) "ID" nil))
235 (org-id-get (point) 'create))
237 ;;;###autoload
238 (defun org-id-copy ()
239 "Copy the ID of the entry at point to the kill ring.
240 Create an ID if necessary."
241 (interactive)
242 (org-kill-new (org-id-get nil 'create)))
244 ;;;###autoload
245 (defun org-id-get (&optional pom create prefix)
246 "Get the ID property of the entry at point-or-marker POM.
247 If POM is nil, refer to the entry at point.
248 If the entry does not have an ID, the function returns nil.
249 However, when CREATE is non nil, create an ID if none is present already.
250 PREFIX will be passed through to `org-id-new'.
251 In any case, the ID of the entry is returned."
252 (org-with-point-at pom
253 (let ((id (org-entry-get nil "ID")))
254 (cond
255 ((and id (stringp id) (string-match "\\S-" id))
257 (create
258 (setq id (org-id-new prefix))
259 (org-entry-put pom "ID" id)
260 (org-id-add-location id (buffer-file-name (buffer-base-buffer)))
261 id)))))
263 ;;;###autoload
264 (defun org-id-get-with-outline-path-completion (&optional targets)
265 "Use `outline-path-completion' to retrieve the ID of an entry.
266 TARGETS may be a setting for `org-refile-targets' to define
267 eligible headlines. When omitted, all headlines in the current
268 file are eligible. This function returns the ID of the entry.
269 If necessary, the ID is created."
270 (let* ((org-refile-targets (or targets '((nil . (:maxlevel . 10)))))
271 (org-refile-use-outline-path
272 (if (caar org-refile-targets) 'file t))
273 (org-refile-target-verify-function nil)
274 (spos (org-refile-get-location "Entry"))
275 (pom (and spos (move-marker (make-marker) (nth 3 spos)
276 (get-file-buffer (nth 1 spos))))))
277 (prog1 (org-id-get pom 'create)
278 (move-marker pom nil))))
280 ;;;###autoload
281 (defun org-id-get-with-outline-drilling (&optional targets)
282 "Use an outline-cycling interface to retrieve the ID of an entry.
283 This only finds entries in the current buffer, using `org-get-location'.
284 It returns the ID of the entry. If necessary, the ID is created."
285 (let* ((spos (org-get-location (current-buffer) org-goto-help))
286 (pom (and spos (move-marker (make-marker) (car spos)))))
287 (prog1 (org-id-get pom 'create)
288 (move-marker pom nil))))
290 ;;;###autoload
291 (defun org-id-goto (id)
292 "Switch to the buffer containing the entry with id ID.
293 Move the cursor to that entry in that buffer."
294 (interactive "sID: ")
295 (let ((m (org-id-find id 'marker)))
296 (unless m
297 (error "Cannot find entry with ID \"%s\"" id))
298 (org-pop-to-buffer-same-window (marker-buffer m))
299 (goto-char m)
300 (move-marker m nil)
301 (org-show-context)))
303 ;;;###autoload
304 (defun org-id-find (id &optional markerp)
305 "Return the location of the entry with the id ID.
306 The return value is a cons cell (file-name . position), or nil
307 if there is no entry with that ID.
308 With optional argument MARKERP, return the position as a new marker."
309 (cond
310 ((symbolp id) (setq id (symbol-name id)))
311 ((numberp id) (setq id (number-to-string id))))
312 (let ((file (org-id-find-id-file id))
313 org-agenda-new-buffers where)
314 (when file
315 (setq where (org-id-find-id-in-file id file markerp)))
316 (unless where
317 (org-id-update-id-locations nil t)
318 (setq file (org-id-find-id-file id))
319 (when file
320 (setq where (org-id-find-id-in-file id file markerp))))
321 where))
323 ;;; Internal functions
325 ;; Creating new IDs
327 ;;;###autoload
328 (defun org-id-new (&optional prefix)
329 "Create a new globally unique ID.
331 An ID consists of two parts separated by a colon:
332 - a prefix
333 - a unique part that will be created according to `org-id-method'.
335 PREFIX can specify the prefix, the default is given by the variable
336 `org-id-prefix'. However, if PREFIX is the symbol `none', don't use any
337 prefix even if `org-id-prefix' specifies one.
339 So a typical ID could look like \"Org:4nd91V40HI\"."
340 (let* ((prefix (if (eq prefix 'none)
342 (concat (or prefix org-id-prefix) ":")))
343 unique)
344 (if (equal prefix ":") (setq prefix ""))
345 (cond
346 ((memq org-id-method '(uuidgen uuid))
347 (setq unique (org-trim (shell-command-to-string org-id-uuid-program)))
348 (unless (org-uuidgen-p unique)
349 (setq unique (org-id-uuid))))
350 ((eq org-id-method 'org)
351 (let* ((etime (org-reverse-string (org-id-time-to-b36)))
352 (postfix (if org-id-include-domain
353 (progn
354 (require 'message)
355 (concat "@" (message-make-fqdn))))))
356 (setq unique (concat etime postfix))))
357 (t (error "Invalid `org-id-method'")))
358 (concat prefix unique)))
360 (defun org-id-uuid ()
361 "Return string with random (version 4) UUID."
362 (let ((rnd (md5 (format "%s%s%s%s%s%s%s"
363 (random)
364 (current-time)
365 (user-uid)
366 (emacs-pid)
367 (user-full-name)
368 user-mail-address
369 (recent-keys)))))
370 (format "%s-%s-4%s-%s%s-%s"
371 (substring rnd 0 8)
372 (substring rnd 8 12)
373 (substring rnd 13 16)
374 (format "%x"
375 (logior
376 #b10000000
377 (logand
378 #b10111111
379 (string-to-number
380 (substring rnd 16 18) 16))))
381 (substring rnd 18 20)
382 (substring rnd 20 32))))
384 (defun org-id-int-to-b36-one-digit (i)
385 "Turn an integer between 0 and 61 into a single character 0..9, A..Z, a..z."
386 (cond
387 ((< i 10) (+ ?0 i))
388 ((< i 36) (+ ?a i -10))
389 (t (error "Larger that 35"))))
391 (defun org-id-b36-to-int-one-digit (i)
392 "Turn a character 0..9, A..Z, a..z into a number 0..61.
393 The input I may be a character, or a single-letter string."
394 (and (stringp i) (setq i (string-to-char i)))
395 (cond
396 ((and (>= i ?0) (<= i ?9)) (- i ?0))
397 ((and (>= i ?a) (<= i ?z)) (+ (- i ?a) 10))
398 (t (error "Invalid b36 letter"))))
400 (defun org-id-int-to-b36 (i &optional length)
401 "Convert an integer to a base-36 number represented as a string."
402 (let ((s ""))
403 (while (> i 0)
404 (setq s (concat (char-to-string
405 (org-id-int-to-b36-one-digit (mod i 36))) s)
406 i (/ i 36)))
407 (setq length (max 1 (or length 1)))
408 (if (< (length s) length)
409 (setq s (concat (make-string (- length (length s)) ?0) s)))
412 (defun org-id-b36-to-int (s)
413 "Convert a base-36 string into the corresponding integer."
414 (let ((r 0))
415 (mapc (lambda (i) (setq r (+ (* r 36) (org-id-b36-to-int-one-digit i))))
419 (defun org-id-time-to-b36 (&optional time)
420 "Encode TIME as a 10-digit string.
421 This string holds the time to micro-second accuracy, and can be decoded
422 using `org-id-decode'."
423 (setq time (or time (current-time)))
424 (concat (org-id-int-to-b36 (nth 0 time) 4)
425 (org-id-int-to-b36 (nth 1 time) 4)
426 (org-id-int-to-b36 (or (nth 2 time) 0) 4)))
428 (defun org-id-decode (id)
429 "Split ID into the prefix and the time value that was used to create it.
430 The return value is (prefix . time) where PREFIX is nil or a string,
431 and time is the usual three-integer representation of time."
432 (let (prefix time parts)
433 (setq parts (org-split-string id ":"))
434 (if (= 2 (length parts))
435 (setq prefix (car parts) time (nth 1 parts))
436 (setq prefix nil time (nth 0 parts)))
437 (setq time (org-reverse-string time))
438 (setq time (list (org-id-b36-to-int (substring time 0 4))
439 (org-id-b36-to-int (substring time 4 8))
440 (org-id-b36-to-int (substring time 8 12))))
441 (cons prefix time)))
443 ;; Storing ID locations (files)
445 ;;;###autoload
446 (defun org-id-update-id-locations (&optional files silent)
447 "Scan relevant files for IDs.
448 Store the relation between files and corresponding IDs.
449 This will scan all agenda files, all associated archives, and all
450 files currently mentioned in `org-id-locations'.
451 When FILES is given, scan these files instead.
452 When CHECK is given, prepare detailed information about duplicate IDs."
453 (interactive)
454 (if (not org-id-track-globally)
455 (error "Please turn on `org-id-track-globally' if you want to track IDs")
456 (let* ((org-id-search-archives
457 (or org-id-search-archives
458 (and (symbolp org-id-extra-files)
459 (symbol-value org-id-extra-files)
460 (member 'agenda-archives org-id-extra-files))))
461 (files
462 (or files
463 (append
464 ;; Agenda files and all associated archives
465 (org-agenda-files t org-id-search-archives)
466 ;; Explicit extra files
467 (if (symbolp org-id-extra-files)
468 (symbol-value org-id-extra-files)
469 org-id-extra-files)
470 ;; Files associated with live org-mode buffers
471 (delq nil
472 (mapcar (lambda (b)
473 (with-current-buffer b
474 (and (derived-mode-p 'org-mode) (buffer-file-name))))
475 (buffer-list)))
476 ;; All files known to have IDs
477 org-id-files)))
478 org-agenda-new-buffers
479 file nfiles tfile ids reg found id seen (ndup 0))
480 (when (member 'agenda-archives files)
481 (setq files (delq 'agenda-archives (copy-sequence files))))
482 (setq nfiles (length files))
483 (while (setq file (pop files))
484 (unless silent
485 (message "Finding ID locations (%d/%d files): %s"
486 (- nfiles (length files)) nfiles file))
487 (setq tfile (file-truename file))
488 (when (and (file-exists-p file) (not (member tfile seen)))
489 (push tfile seen)
490 (setq ids nil)
491 (with-current-buffer (org-get-agenda-file-buffer file)
492 (save-excursion
493 (save-restriction
494 (widen)
495 (goto-char (point-min))
496 (while (re-search-forward "^[ \t]*:ID:[ \t]+\\(\\S-+\\)[ \t]*$"
497 nil t)
498 (setq id (org-match-string-no-properties 1))
499 (if (member id found)
500 (progn
501 (message "Duplicate ID \"%s\", also in file %s"
502 id (or (car (delq
504 (mapcar
505 (lambda (x)
506 (if (member id (cdr x))
507 (car x)))
508 reg)))
509 (buffer-file-name)))
510 (when (= ndup 0)
511 (ding)
512 (sit-for 2))
513 (setq ndup (1+ ndup)))
514 (push id found)
515 (push id ids)))
516 (push (cons (abbreviate-file-name file) ids) reg))))))
517 (org-release-buffers org-agenda-new-buffers)
518 (setq org-agenda-new-buffers nil)
519 (setq org-id-locations reg)
520 (setq org-id-files (mapcar 'car org-id-locations))
521 (org-id-locations-save) ;; this function can also handle the alist form
522 ;; now convert to a hash
523 (setq org-id-locations (org-id-alist-to-hash org-id-locations))
524 (if (> ndup 0)
525 (message "WARNING: %d duplicate IDs found, check *Messages* buffer" ndup)
526 (message "%d unique files scanned for IDs" (length org-id-files)))
527 org-id-locations)))
529 (defun org-id-locations-save ()
530 "Save `org-id-locations' in `org-id-locations-file'."
531 (when (and org-id-track-globally org-id-locations)
532 (let ((out (if (hash-table-p org-id-locations)
533 (org-id-hash-to-alist org-id-locations)
534 org-id-locations)))
535 (with-temp-file org-id-locations-file
536 (let ((print-level nil)
537 (print-length nil))
538 (print out (current-buffer)))))))
540 (defun org-id-locations-load ()
541 "Read the data from `org-id-locations-file'."
542 (setq org-id-locations nil)
543 (when org-id-track-globally
544 (with-temp-buffer
545 (condition-case nil
546 (progn
547 (insert-file-contents-literally org-id-locations-file)
548 (goto-char (point-min))
549 (setq org-id-locations (read (current-buffer))))
550 (error
551 (message "Could not read org-id-values from %s. Setting it to nil."
552 org-id-locations-file))))
553 (setq org-id-files (mapcar 'car org-id-locations))
554 (setq org-id-locations (org-id-alist-to-hash org-id-locations))))
556 (defun org-id-add-location (id file)
557 "Add the ID with location FILE to the database of ID locations."
558 ;; Only if global tracking is on, and when the buffer has a file
559 (when (and org-id-track-globally id file)
560 (unless org-id-locations (org-id-locations-load))
561 (puthash id (abbreviate-file-name file) org-id-locations)
562 (add-to-list 'org-id-files (abbreviate-file-name file))))
564 (unless noninteractive
565 (add-hook 'kill-emacs-hook 'org-id-locations-save))
567 (defun org-id-hash-to-alist (hash)
568 "Turn an org-id hash into an alist, so that it can be written to a file."
569 (let (res x)
570 (maphash
571 (lambda (k v)
572 (if (setq x (member v res))
573 (setcdr x (cons k (cdr x)))
574 (push (list v k) res)))
575 hash)
576 res))
578 (defun org-id-alist-to-hash (list)
579 "Turn an org-id location list into a hash table."
580 (let ((res (make-hash-table
581 :test 'equal
582 :size (apply '+ (mapcar 'length list))))
584 (mapc
585 (lambda (x)
586 (setq f (car x))
587 (mapc (lambda (i) (puthash i f res)) (cdr x)))
588 list)
589 res))
591 (defun org-id-paste-tracker (txt &optional buffer-or-file)
592 "Update any IDs in TXT and assign BUFFER-OR-FILE to them."
593 (when org-id-track-globally
594 (save-match-data
595 (setq buffer-or-file (or buffer-or-file (current-buffer)))
596 (when (bufferp buffer-or-file)
597 (setq buffer-or-file (or (buffer-base-buffer buffer-or-file)
598 buffer-or-file))
599 (setq buffer-or-file (buffer-file-name buffer-or-file)))
600 (when buffer-or-file
601 (let ((fname (abbreviate-file-name buffer-or-file))
602 (s 0))
603 (while (string-match "^[ \t]*:ID:[ \t]+\\([^ \t\n\r]+\\)" txt s)
604 (setq s (match-end 0))
605 (org-id-add-location (match-string 1 txt) fname)))))))
607 ;; Finding entries with specified id
609 ;;;###autoload
610 (defun org-id-find-id-file (id)
611 "Query the id database for the file in which this ID is located."
612 (unless org-id-locations (org-id-locations-load))
613 (or (and org-id-locations
614 (hash-table-p org-id-locations)
615 (gethash id org-id-locations))
616 ;; ball back on current buffer
617 (buffer-file-name (or (buffer-base-buffer (current-buffer))
618 (current-buffer)))))
620 (defun org-id-find-id-in-file (id file &optional markerp)
621 "Return the position of the entry ID in FILE.
622 If that files does not exist, or if it does not contain this ID,
623 return nil.
624 The position is returned as a cons cell (file-name . position). With
625 optional argument MARKERP, return the position as a new marker."
626 (let (org-agenda-new-buffers buf pos)
627 (cond
628 ((not file) nil)
629 ((not (file-exists-p file)) nil)
630 (t (with-current-buffer (setq buf (org-get-agenda-file-buffer file))
631 (setq pos (org-find-entry-with-id id))
632 (when pos
633 (if markerp
634 (move-marker (make-marker) pos buf)
635 (cons file pos))))))))
637 ;; id link type
639 ;; Calling the following function is hard-coded into `org-store-link',
640 ;; so we do have to add it to `org-store-link-functions'.
642 ;;;###autoload
643 (defun org-id-store-link ()
644 "Store a link to the current entry, using its ID."
645 (interactive)
646 (when (and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
647 (let* ((link (concat "id:" (org-id-get-create)))
648 (case-fold-search nil)
649 (desc (save-excursion
650 (org-back-to-heading t)
651 (or (and (looking-at org-complex-heading-regexp)
652 (if (match-end 4)
653 (match-string 4)
654 (match-string 0)))
655 link))))
656 (org-store-link-props :link link :description desc :type "id")
657 link)))
659 (defun org-id-open (id)
660 "Go to the entry with id ID."
661 (org-mark-ring-push)
662 (let ((m (org-id-find id 'marker))
663 cmd)
664 (unless m
665 (error "Cannot find entry with ID \"%s\"" id))
666 ;; Use a buffer-switching command in analogy to finding files
667 (setq cmd
669 (cdr
670 (assq
671 (cdr (assq 'file org-link-frame-setup))
672 '((find-file . switch-to-buffer)
673 (find-file-other-window . switch-to-buffer-other-window)
674 (find-file-other-frame . switch-to-buffer-other-frame))))
675 'switch-to-buffer-other-window))
676 (if (not (equal (current-buffer) (marker-buffer m)))
677 (funcall cmd (marker-buffer m)))
678 (goto-char m)
679 (move-marker m nil)
680 (org-show-context)))
682 (org-add-link-type "id" 'org-id-open)
684 (provide 'org-id)
686 ;; Local variables:
687 ;; generated-autoload-file: "org-loaddefs.el"
688 ;; End:
690 ;;; org-id.el ends here