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