org-id.el 23 KB

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