org-id.el 24 KB

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