org-id.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. ;;; org-id.el --- Global identifiers for Org-mode entries
  2. ;;
  3. ;; Copyright (C) 2008, 2009, 2010 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. ;; Version: 6.35d
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (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.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;; This file implements globally unique identifiers for Org-mode entries.
  26. ;; Identifiers are stored in the entry as an :ID: property. Functions
  27. ;; are provided that create and retrieve such identifiers, and that find
  28. ;; entries based on the identifier.
  29. ;; Identifiers consist of a prefix (default "Org" given by the variable
  30. ;; `org-id-prefix') and a unique part that can be created by a number
  31. ;; of different methods, see the variable `org-id-method'.
  32. ;; Org has a builtin method that uses a compact encoding of the creation
  33. ;; time of the ID, with microsecond accuracy. This virtually
  34. ;; guarantees globally unique identifiers, even if several people are
  35. ;; creating IDs at the same time in files that will eventually be used
  36. ;; together. As an external method `uuidgen' is supported, if installed
  37. ;; on the system.
  38. ;;
  39. ;; This file defines the following API:
  40. ;;
  41. ;; org-id-get-create
  42. ;; Create an ID for the entry at point if it does not yet have one.
  43. ;; Returns the ID (old or new). This function can be used
  44. ;; interactively, with prefix argument the creation of a new ID is
  45. ;; forced, even if there was an old one.
  46. ;;
  47. ;; org-id-get
  48. ;; Get the ID property of an entry. Using appropriate arguments
  49. ;; to the function, it can also create the ID for this entry.
  50. ;;
  51. ;; org-id-goto
  52. ;; Command to go to a specific ID, this command can be used
  53. ;; interactively.
  54. ;;
  55. ;; org-id-get-with-outline-path-completion
  56. ;; Retrieve the ID of an entry, using outline path completion.
  57. ;; This function can work for multiple files.
  58. ;;
  59. ;; org-id-get-with-outline-drilling
  60. ;; Retrieve the ID of an entry, using outline path completion.
  61. ;; This function only works for the current file.
  62. ;;
  63. ;; org-id-find
  64. ;; Find the location of an entry with specific id.
  65. ;;
  66. (require 'org)
  67. (declare-function message-make-fqdn "message" ())
  68. ;;; Customization
  69. (defgroup org-id nil
  70. "Options concerning global entry identifiers in Org-mode."
  71. :tag "Org ID"
  72. :group 'org)
  73. (defcustom org-id-uuid-program "uuidgen"
  74. "The uuidgen program."
  75. :group 'org-id
  76. :type 'string)
  77. (defcustom org-id-method
  78. (condition-case nil
  79. (if (string-match "\\`[-0-9a-fA-F]\\{36\\}\\'"
  80. (org-trim (shell-command-to-string
  81. org-id-uuid-program)))
  82. 'uuidgen
  83. 'org)
  84. (error 'org))
  85. "The method that should be used to create new IDs.
  86. If `uuidgen' is available on the system, it will be used as the default method.
  87. if not, the method `org' is used.
  88. An ID will consist of the optional prefix specified in `org-id-prefix',
  89. and a unique part created by the method this variable specifies.
  90. Allowed values are:
  91. org Org's own internal method, using an encoding of the current time to
  92. microsecond accuracy, and optionally the current domain of the
  93. computer. See the variable `org-id-include-domain'.
  94. uuidgen Call the external command uuidgen."
  95. :group 'org-id
  96. :type '(choice
  97. (const :tag "Org's internal method" org)
  98. (const :tag "external: uuidgen" uuidgen)))
  99. (defcustom org-id-prefix nil
  100. "The prefix for IDs.
  101. This may be a string, or it can be nil to indicate that no prefix is required.
  102. When a string, the string should have no space characters as IDs are expected
  103. to have no space characters in them."
  104. :group 'org-id
  105. :type '(choice
  106. (const :tag "No prefix")
  107. (string :tag "Prefix")))
  108. (defcustom org-id-include-domain nil
  109. "Non-nil means add the domain name to new IDs.
  110. This ensures global uniqueness of IDs, and is also suggested by
  111. RFC 2445 in combination with RFC 822. This is only relevant if
  112. `org-id-method' is `org'. When uuidgen is used, the domain will never
  113. be added.
  114. The default is to not use this because we have no really good way to get
  115. the true domain, and Org entries will normally not be shared with enough
  116. people to make this necessary."
  117. :group 'org-id
  118. :type 'boolean)
  119. (defcustom org-id-track-globally t
  120. "Non-nil means track IDs through files, so that links work globally.
  121. This work by maintaining a hash table for IDs and writing this table
  122. to disk when exiting Emacs. Because of this, it works best if you use
  123. a single Emacs process, not many.
  124. When nil, IDs are not tracked. Links to IDs will still work within
  125. a buffer, but not if the entry is located in another file.
  126. IDs can still be used if the entry with the id is in the same file as
  127. the link."
  128. :group 'org-id
  129. :type 'boolean)
  130. (defcustom org-id-locations-file (convert-standard-filename
  131. "~/.emacs.d/.org-id-locations")
  132. "The file for remembering in which file an ID was defined.
  133. This variable is only relevant when `org-id-track-globally' is set."
  134. :group 'org-id
  135. :type 'file)
  136. (defvar org-id-locations nil
  137. "List of files with IDs in those files.
  138. Depending on `org-id-use-hash' this can also be a hash table mapping IDs
  139. to files.")
  140. (defvar org-id-files nil
  141. "List of files that contain IDs.")
  142. (defcustom org-id-extra-files 'org-agenda-text-search-extra-files
  143. "Files to be searched for IDs, besides the agenda files.
  144. When Org reparses files to remake the list of files and IDs it is tracking,
  145. it will normally scan the agenda files, the archives related to agenda files,
  146. any files that are listed as ID containing in the current register, and
  147. any Org-mode files currently visited by Emacs.
  148. You can list additional files here.
  149. This variable is only relevant when `org-id-track-globally' is set."
  150. :group 'org-id
  151. :type
  152. '(choice
  153. (symbol :tag "Variable")
  154. (repeat :tag "List of files"
  155. (file))))
  156. (defcustom org-id-search-archives t
  157. "Non-nil means search also the archive files of agenda files for entries.
  158. This is a possibility to reduce overhead, but it means that entries moved
  159. to the archives can no longer be found by ID.
  160. This variable is only relevant when `org-id-track-globally' is set."
  161. :group 'org-id
  162. :type 'boolean)
  163. ;;; The API functions
  164. ;;;###autoload
  165. (defun org-id-get-create (&optional force)
  166. "Create an ID for the current entry and return it.
  167. If the entry already has an ID, just return it.
  168. With optional argument FORCE, force the creation of a new ID."
  169. (interactive "P")
  170. (when force
  171. (org-entry-put (point) "ID" nil))
  172. (org-id-get (point) 'create))
  173. ;;;###autoload
  174. (defun org-id-copy ()
  175. "Copy the ID of the entry at point to the kill ring.
  176. Create an ID if necessary."
  177. (interactive)
  178. (org-kill-new (org-id-get nil 'create)))
  179. ;;;###autoload
  180. (defun org-id-get (&optional pom create prefix)
  181. "Get the ID property of the entry at point-or-marker POM.
  182. If POM is nil, refer to the entry at point.
  183. If the entry does not have an ID, the function returns nil.
  184. However, when CREATE is non nil, create an ID if none is present already.
  185. PREFIX will be passed through to `org-id-new'.
  186. In any case, the ID of the entry is returned."
  187. (org-with-point-at pom
  188. (let ((id (org-entry-get nil "ID")))
  189. (cond
  190. ((and id (stringp id) (string-match "\\S-" id))
  191. id)
  192. (create
  193. (setq id (org-id-new prefix))
  194. (org-entry-put pom "ID" id)
  195. (org-id-add-location id (buffer-file-name (buffer-base-buffer)))
  196. id)
  197. (t nil)))))
  198. ;;;###autoload
  199. (defun org-id-get-with-outline-path-completion (&optional targets)
  200. "Use outline-path-completion to retrieve the ID of an entry.
  201. TARGETS may be a setting for `org-refile-targets' to define the eligible
  202. headlines. When omitted, all headlines in all agenda files are
  203. eligible.
  204. It returns the ID of the entry. If necessary, the ID is created."
  205. (let* ((org-refile-targets (or targets '((nil . (:maxlevel . 10)))))
  206. (org-refile-use-outline-path
  207. (if (caar org-refile-targets) 'file t))
  208. (org-refile-target-verify-function nil)
  209. (spos (org-refile-get-location "Entry: "))
  210. (pom (and spos (move-marker (make-marker) (nth 3 spos)
  211. (get-file-buffer (nth 1 spos))))))
  212. (prog1 (org-id-get pom 'create)
  213. (move-marker pom nil))))
  214. ;;;###autoload
  215. (defun org-id-get-with-outline-drilling (&optional targets)
  216. "Use an outline-cycling interface to retrieve the ID of an entry.
  217. This only finds entries in the current buffer, using `org-get-location'.
  218. It returns the ID of the entry. If necessary, the ID is created."
  219. (let* ((spos (org-get-location (current-buffer) org-goto-help))
  220. (pom (and spos (move-marker (make-marker) (car spos)))))
  221. (prog1 (org-id-get pom 'create)
  222. (move-marker pom nil))))
  223. ;;;###autoload
  224. (defun org-id-goto (id)
  225. "Switch to the buffer containing the entry with id ID.
  226. Move the cursor to that entry in that buffer."
  227. (interactive "sID: ")
  228. (let ((m (org-id-find id 'marker)))
  229. (unless m
  230. (error "Cannot find entry with ID \"%s\"" id))
  231. (switch-to-buffer (marker-buffer m))
  232. (goto-char m)
  233. (move-marker m nil)
  234. (org-show-context)))
  235. ;;;###autoload
  236. (defun org-id-find (id &optional markerp)
  237. "Return the location of the entry with the id ID.
  238. The return value is a cons cell (file-name . position), or nil
  239. if there is no entry with that ID.
  240. With optional argument MARKERP, return the position as a new marker."
  241. (cond
  242. ((symbolp id) (setq id (symbol-name id)))
  243. ((numberp id) (setq id (number-to-string id))))
  244. (let ((file (org-id-find-id-file id))
  245. org-agenda-new-buffers where)
  246. (when file
  247. (setq where (org-id-find-id-in-file id file markerp)))
  248. (unless where
  249. (org-id-update-id-locations)
  250. (setq file (org-id-find-id-file id))
  251. (when file
  252. (setq where (org-id-find-id-in-file id file markerp))))
  253. where))
  254. ;;; Internal functions
  255. ;; Creating new IDs
  256. (defun org-id-new (&optional prefix)
  257. "Create a new globally unique ID.
  258. An ID consists of two parts separated by a colon:
  259. - a prefix
  260. - a unique part that will be created according to `org-id-method'.
  261. PREFIX can specify the prefix, the default is given by the variable
  262. `org-id-prefix'. However, if PREFIX is the symbol `none', don't use any
  263. prefix even if `org-id-prefix' specifies one.
  264. So a typical ID could look like \"Org:4nd91V40HI\"."
  265. (let* ((prefix (if (eq prefix 'none)
  266. ""
  267. (concat (or prefix org-id-prefix) ":")))
  268. unique)
  269. (if (equal prefix ":") (setq prefix ""))
  270. (cond
  271. ((eq org-id-method 'uuidgen)
  272. (setq unique (org-trim (shell-command-to-string org-id-uuid-program))))
  273. ((eq org-id-method 'org)
  274. (let* ((etime (org-id-reverse-string (org-id-time-to-b36)))
  275. (postfix (if org-id-include-domain
  276. (progn
  277. (require 'message)
  278. (concat "@" (message-make-fqdn))))))
  279. (setq unique (concat etime postfix))))
  280. (t (error "Invalid `org-id-method'")))
  281. (concat prefix unique)))
  282. (defun org-id-reverse-string (s)
  283. (mapconcat 'char-to-string (nreverse (string-to-list s)) ""))
  284. (defun org-id-int-to-b36-one-digit (i)
  285. "Turn an integer between 0 and 61 into a single character 0..9, A..Z, a..z."
  286. (cond
  287. ((< i 10) (+ ?0 i))
  288. ((< i 36) (+ ?a i -10))
  289. (t (error "Larger that 35"))))
  290. (defun org-id-b36-to-int-one-digit (i)
  291. "Turn a character 0..9, A..Z, a..z into a number 0..61.
  292. The input I may be a character, or a single-letter string."
  293. (and (stringp i) (setq i (string-to-char i)))
  294. (cond
  295. ((and (>= i ?0) (<= i ?9)) (- i ?0))
  296. ((and (>= i ?a) (<= i ?z)) (+ (- i ?a) 10))
  297. (t (error "Invalid b36 letter"))))
  298. (defun org-id-int-to-b36 (i &optional length)
  299. "Convert an integer to a base-36 number represented as a string."
  300. (let ((s ""))
  301. (while (> i 0)
  302. (setq s (concat (char-to-string
  303. (org-id-int-to-b36-one-digit (mod i 36))) s)
  304. i (/ i 36)))
  305. (setq length (max 1 (or length 1)))
  306. (if (< (length s) length)
  307. (setq s (concat (make-string (- length (length s)) ?0) s)))
  308. s))
  309. (defun org-id-b36-to-int (s)
  310. "Convert a base-36 string into the corresponding integer."
  311. (let ((r 0))
  312. (mapc (lambda (i) (setq r (+ (* r 36) (org-id-b36-to-int-one-digit i))))
  313. s)
  314. r))
  315. (defun org-id-time-to-b36 (&optional time)
  316. "Encode TIME as a 10-digit string.
  317. This string holds the time to micro-second accuracy, and can be decoded
  318. using `org-id-decode'."
  319. (setq time (or time (current-time)))
  320. (concat (org-id-int-to-b36 (nth 0 time) 4)
  321. (org-id-int-to-b36 (nth 1 time) 4)
  322. (org-id-int-to-b36 (or (nth 2 time) 0) 4)))
  323. (defun org-id-decode (id)
  324. "Split ID into the prefix and the time value that was used to create it.
  325. The return value is (prefix . time) where PREFIX is nil or a string,
  326. and time is the usual three-integer representation of time."
  327. (let (prefix time parts)
  328. (setq parts (org-split-string id ":"))
  329. (if (= 2 (length parts))
  330. (setq prefix (car parts) time (nth 1 parts))
  331. (setq prefix nil time (nth 0 parts)))
  332. (setq time (org-id-reverse-string time))
  333. (setq time (list (org-id-b36-to-int (substring time 0 4))
  334. (org-id-b36-to-int (substring time 4 8))
  335. (org-id-b36-to-int (substring time 8 12))))
  336. (cons prefix time)))
  337. ;; Storing ID locations (files)
  338. (defun org-id-update-id-locations (&optional files)
  339. "Scan relevant files for IDs.
  340. Store the relation between files and corresponding IDs.
  341. This will scan all agenda files, all associated archives, and all
  342. files currently mentioned in `org-id-locations'.
  343. When FILES is given, scan these files instead.
  344. When CHECK is given, prepare detailed information about duplicate IDs."
  345. (interactive)
  346. (if (not org-id-track-globally)
  347. (error "Please turn on `org-id-track-globally' if you want to track IDs")
  348. (let* ((org-id-search-archives
  349. (or org-id-search-archives
  350. (and (symbolp org-id-extra-files)
  351. (symbol-value org-id-extra-files)
  352. (member 'agenda-archives org-id-extra-files))))
  353. (files
  354. (or files
  355. (append
  356. ;; Agenda files and all associated archives
  357. (org-agenda-files t org-id-search-archives)
  358. ;; Explicit extra files
  359. (if (symbolp org-id-extra-files)
  360. (symbol-value org-id-extra-files)
  361. org-id-extra-files)
  362. ;; Files associated with live org-mode buffers
  363. (delq nil
  364. (mapcar (lambda (b)
  365. (with-current-buffer b
  366. (and (org-mode-p) (buffer-file-name))))
  367. (buffer-list)))
  368. ;; All files known to have IDs
  369. org-id-files)))
  370. org-agenda-new-buffers
  371. file nfiles tfile ids reg found id seen (ndup 0))
  372. (when (member 'agenda-archives files)
  373. (setq files (delq 'agenda-archives (copy-sequence files))))
  374. (setq nfiles (length files))
  375. (while (setq file (pop files))
  376. (message "Finding ID locations (%d/%d files): %s"
  377. (- nfiles (length files)) nfiles file)
  378. (setq tfile (file-truename file))
  379. (when (and (file-exists-p file) (not (member tfile seen)))
  380. (push tfile seen)
  381. (setq ids nil)
  382. (with-current-buffer (org-get-agenda-file-buffer file)
  383. (save-excursion
  384. (save-restriction
  385. (widen)
  386. (goto-char (point-min))
  387. (while (re-search-forward "^[ \t]*:ID:[ \t]+\\(\\S-+\\)[ \t]*$"
  388. nil t)
  389. (setq id (org-match-string-no-properties 1))
  390. (if (member id found)
  391. (progn
  392. (message "Duplicate ID \"%s\", also in file %s"
  393. id (or (car (delq
  394. nil
  395. (mapcar
  396. (lambda (x)
  397. (if (member id (cdr x))
  398. (car x)))
  399. reg)))
  400. (buffer-file-name)))
  401. (when (= ndup 0)
  402. (ding)
  403. (sit-for 2))
  404. (setq ndup (1+ ndup)))
  405. (push id found)
  406. (push id ids)))
  407. (push (cons (abbreviate-file-name file) ids) reg))))))
  408. (org-release-buffers org-agenda-new-buffers)
  409. (setq org-agenda-new-buffers nil)
  410. (setq org-id-locations reg)
  411. (setq org-id-files (mapcar 'car org-id-locations))
  412. (org-id-locations-save) ;; this function can also handle the alist form
  413. ;; now convert to a hash
  414. (setq org-id-locations (org-id-alist-to-hash org-id-locations))
  415. (if (> ndup 0)
  416. (message "WARNING: %d duplicate IDs found, check *Messages* buffer" ndup)
  417. (message "%d unique files scanned for IDs" (length org-id-files)))
  418. org-id-locations)))
  419. (defun org-id-locations-save ()
  420. "Save `org-id-locations' in `org-id-locations-file'."
  421. (when (and org-id-track-globally org-id-locations)
  422. (let ((out (if (hash-table-p org-id-locations)
  423. (org-id-hash-to-alist org-id-locations)
  424. org-id-locations)))
  425. (with-temp-file org-id-locations-file
  426. (print out (current-buffer))))))
  427. (defun org-id-locations-load ()
  428. "Read the data from `org-id-locations-file'."
  429. (setq org-id-locations nil)
  430. (when org-id-track-globally
  431. (with-temp-buffer
  432. (condition-case nil
  433. (progn
  434. (insert-file-contents-literally org-id-locations-file)
  435. (goto-char (point-min))
  436. (setq org-id-locations (read (current-buffer))))
  437. (error
  438. (message "Could not read org-id-values from %s. Setting it to nil."
  439. org-id-locations-file))))
  440. (setq org-id-files (mapcar 'car org-id-locations))
  441. (setq org-id-locations (org-id-alist-to-hash org-id-locations))))
  442. (defun org-id-add-location (id file)
  443. "Add the ID with location FILE to the database of ID locations."
  444. ;; Only if global tracking is on, and when the buffer has a file
  445. (when (and org-id-track-globally id file)
  446. (unless org-id-locations (org-id-locations-load))
  447. (puthash id (abbreviate-file-name file) org-id-locations)
  448. (add-to-list 'org-id-files (abbreviate-file-name file))))
  449. (add-hook 'kill-emacs-hook 'org-id-locations-save)
  450. (defun org-id-hash-to-alist (hash)
  451. "Turn an org-id hash into an alist, so that it can be written to a file."
  452. (let (res x)
  453. (maphash
  454. (lambda (k v)
  455. (if (setq x (member v res))
  456. (setcdr x (cons k (cdr x)))
  457. (push (list v k) res)))
  458. hash)
  459. res))
  460. (defun org-id-alist-to-hash (list)
  461. "Turn an org-id location list into a hash table."
  462. (let ((res (make-hash-table
  463. :test 'equal
  464. :size (apply '+ (mapcar 'length list))))
  465. f)
  466. (mapc
  467. (lambda (x)
  468. (setq f (car x))
  469. (mapc (lambda (i) (puthash i f res)) (cdr x)))
  470. list)
  471. res))
  472. (defun org-id-paste-tracker (txt &optional buffer-or-file)
  473. "Update any IDs in TXT and assign BUFFER-OR-FILE to them."
  474. (when org-id-track-globally
  475. (save-match-data
  476. (setq buffer-or-file (or buffer-or-file (current-buffer)))
  477. (when (bufferp buffer-or-file)
  478. (setq buffer-or-file (or (buffer-base-buffer buffer-or-file)
  479. buffer-or-file))
  480. (setq buffer-or-file (buffer-file-name buffer-or-file)))
  481. (when buffer-or-file
  482. (let ((fname (abbreviate-file-name buffer-or-file))
  483. (s 0))
  484. (while (string-match "^[ \t]*:ID:[ \t]+\\([^ \t\n\r]+\\)" txt s)
  485. (setq s (match-end 0))
  486. (org-id-add-location (match-string 1 txt) fname)))))))
  487. ;; Finding entries with specified id
  488. ;;;###autoload
  489. (defun org-id-find-id-file (id)
  490. "Query the id database for the file in which this ID is located."
  491. (unless org-id-locations (org-id-locations-load))
  492. (or (and org-id-locations
  493. (hash-table-p org-id-locations)
  494. (gethash id org-id-locations))
  495. ;; ball back on current buffer
  496. (buffer-file-name (or (buffer-base-buffer (current-buffer))
  497. (current-buffer)))))
  498. (defun org-id-find-id-in-file (id file &optional markerp)
  499. "Return the position of the entry ID in FILE.
  500. If that files does not exist, or if it does not contain this ID,
  501. return nil.
  502. The position is returned as a cons cell (file-name . position). With
  503. optional argument MARKERP, return the position as a new marker."
  504. (let (org-agenda-new-buffers buf pos)
  505. (cond
  506. ((not file) nil)
  507. ((not (file-exists-p file)) nil)
  508. (t (with-current-buffer (setq buf (org-get-agenda-file-buffer file))
  509. (setq pos (org-find-entry-with-id id))
  510. (when pos
  511. (if markerp
  512. (move-marker (make-marker) pos buf)
  513. (cons file pos))))))))
  514. ;; id link type
  515. ;; Calling the following function is hard-coded into `org-store-link',
  516. ;; so we do have to add it to `org-store-link-functions'.
  517. (defun org-id-store-link ()
  518. "Store a link to the current entry, using its ID."
  519. (interactive)
  520. (let* ((link (org-make-link "id:" (org-id-get-create)))
  521. (case-fold-search nil)
  522. (desc (save-excursion
  523. (org-back-to-heading t)
  524. (or (and (looking-at org-complex-heading-regexp)
  525. (if (match-end 4) (match-string 4) (match-string 0)))
  526. link))))
  527. (org-store-link-props :link link :description desc :type "id")
  528. link))
  529. (defun org-id-open (id)
  530. "Go to the entry with id ID."
  531. (org-mark-ring-push)
  532. (let ((m (org-id-find id 'marker))
  533. cmd)
  534. (unless m
  535. (error "Cannot find entry with ID \"%s\"" id))
  536. ;; Use a buffer-switching command in analogy to finding files
  537. (setq cmd
  538. (or
  539. (cdr
  540. (assq
  541. (cdr (assq 'file org-link-frame-setup))
  542. '((find-file . switch-to-buffer)
  543. (find-file-other-window . switch-to-buffer-other-window)
  544. (find-file-other-frame . switch-to-buffer-other-frame))))
  545. 'switch-to-buffer-other-window))
  546. (if (not (equal (current-buffer) (marker-buffer m)))
  547. (funcall cmd (marker-buffer m)))
  548. (goto-char m)
  549. (move-marker m nil)
  550. (org-show-context)))
  551. (org-add-link-type "id" 'org-id-open)
  552. (provide 'org-id)
  553. ;;; org-id.el ends here
  554. ;; arch-tag: e5abaca4-e16f-4b25-832a-540cfb63a712