org-id.el 24 KB

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