org-id.el 22 KB

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