org-id.el 22 KB

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