org-id.el 22 KB

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