org-id.el 24 KB

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