annotate lisp/org/org-id.el @ 98468:bc63ca36bd15

* faces.el (inhibit-frame-set-background-mode): New var. (frame-set-background-mode): Use it to avoid a loop in face-spec-recalc.
author Chong Yidong <cyd@stupidchicken.com>
date Thu, 02 Oct 2008 20:19:11 +0000
parents adc2dffbdfc3
children e1cc41b9282d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
96045
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
1 ;;; org-id.el --- Global identifier for Org-mode entries
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
2 ;; Copyright (C) 2008 Free Software Foundation, Inc.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
3 ;;
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
4 ;; Author: Carsten Dominik <carsten at orgmode dot org>
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
5 ;; Keywords: outlines, hypermedia, calendar, wp
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
6 ;; Homepage: http://orgmode.org
97027
adc2dffbdfc3 2008-07-25 Carsten Dominik <dominik@science.uva.nl>
Carsten Dominik <dominik@science.uva.nl>
parents: 96975
diff changeset
7 ;; Version: 6.06b
96045
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
8 ;;
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
9 ;; This file is part of GNU Emacs.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
10 ;;
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
13 ;; the Free Software Foundation, either version 3 of the License, or
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
14 ;; (at your option) any later version.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
15
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
19 ;; GNU General Public License for more details.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
20
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
24 ;;
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
25 ;;; Commentary:
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
26
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
27 ;; This file implements globally unique identifiers for Org-mode entries.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
28 ;; Identifiers are stored in the entry as an :ID: property. Functions
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
29 ;; are provided that create and retrieve such identifiers, and that find
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
30 ;; entries based on the identifier.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
31
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
32 ;; Identifiers consist of a prefix (default "Org") and a compact encoding
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
33 ;; of the creation time of the ID, with microsecond accuracy. This virtually
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
34 ;; guarantees globally unique identifiers, even if several people are
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
35 ;; creating ID's at the same time in files that will eventually be used
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
36 ;; together. Even higher security can be achieved by using different
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
37 ;; prefix values for each collaborator or file.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
38 ;;
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
39 ;; This file defines the following API:
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
40 ;;
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
41 ;; org-id-get-create
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
42 ;; Create an ID for the entry at point if it does not yet have one.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
43 ;; Returns the ID (old or new). This function can be used
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
44 ;; interactively, with prefix argument the creation of a new ID is
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
45 ;; forced, even if there was an old one.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
46 ;;
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
47 ;; org-id-get
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
48 ;; Get the ID property of an entry. Using appropriate arguments
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
49 ;; to the function, it can also create the ID for this entry.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
50 ;;
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
51 ;; org-id-goto
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
52 ;; Command to go to a specific ID, this command can be used
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
53 ;; interactively.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
54 ;;
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
55 ;; org-id-get-with-outline-path-completion
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
56 ;; Retrieve the ID of an entry, using outline path completion.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
57 ;; This function can work for multiple files.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
58 ;;
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
59 ;; org-id-get-with-outline-drilling
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
60 ;; Retrieve the ID of an entry, using outline path completion.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
61 ;; This function only works for the current file.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
62 ;;
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
63 ;; org-id-find
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
64 ;; Find the location of an entry with specific id.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
65 ;;
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
66
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
67 (require 'org)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
68
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
69 (declare-function message-make-fqdn "message" ())
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
70
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
71 ;;; Customization
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
72
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
73 (defgroup org-id nil
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
74 "Options concerning global entry identifiers in Org-mode."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
75 :tag "Org ID"
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
76 :group 'org)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
77
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
78 (defcustom org-id-prefix "Org"
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
79 "The prefix for IDs.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
80
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
81 This may be a string, or it can be nil to indicate that no prefix is required.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
82 When a string, the string should have no space characters as IDs are expected
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
83 to have no space characters in them."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
84 :group 'org-id
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
85 :type '(choice
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
86 (const :tag "No prefix")
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
87 (string :tag "Prefix")))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
88
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
89 (defcustom org-id-include-domain t
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
90 "Non-nil means, add the domain name to new IDs.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
91 This ensures global uniqueness of ID's, and is also suggested by
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
92 RFC 2445 in combination with RFC 822."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
93 :group 'org-id
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
94 :type 'boolean)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
95
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
96 (defcustom org-id-locations-file "~/.org-id-locations"
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
97 "The file for remembering the last ID number generated."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
98 :group 'org-id
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
99 :type 'file)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
100
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
101 (defvar org-id-locations nil
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
102 "List of files with ID's in those files.")
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
103
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
104 (defcustom org-id-extra-files 'org-agenda-text-search-extra-files
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
105 "Files to be searched for ID's, besides the agenda files."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
106 :group 'org-id
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
107 :type
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
108 '(choice
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
109 (symbol :tag "Variable")
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
110 (repeat :tag "List of files"
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
111 (file))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
112
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
113 ;;; The API functions
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
114
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
115 ;;;###autoload
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
116 (defun org-id-get-create (&optional force)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
117 "Create an ID for the current entry and return it.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
118 If the entry already has an ID, just return it.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
119 With optional argument FORCE, force the creation of a new ID."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
120 (interactive "P")
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
121 (when force
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
122 (org-entry-put (point) "ID" nil))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
123 (org-id-get (point) 'create))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
124
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
125 ;;;###autoload
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
126 (defun org-id-copy ()
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
127 "Copy the ID of the entry at point to the kill ring.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
128 Create an ID if necessary."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
129 (interactive)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
130 (kill-new (org-id-get nil 'create)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
131
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
132 ;;;###autoload
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
133 (defun org-id-get (&optional pom create prefix)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
134 "Get the ID property of the entry at point-or-marker POM.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
135 If POM is nil, refer to the entry at point.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
136 If the entry does not have an ID, the function returns nil.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
137 However, when CREATE is non nil, create an ID if none is present already.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
138 PREFIX will be passed through to `org-id-new'.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
139 In any case, the ID of the entry is returned."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
140 (let ((id (org-entry-get pom "ID")))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
141 (cond
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
142 ((and id (stringp id) (string-match "\\S-" id))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
143 id)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
144 (create
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
145 (setq id (org-id-new prefix))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
146 (org-entry-put pom "ID" id)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
147 (org-id-add-location id (buffer-file-name (buffer-base-buffer)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
148 id)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
149 (t nil))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
150
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
151 ;;;###autoload
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
152 (defun org-id-get-with-outline-path-completion (&optional targets)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
153 "Use outline-path-completion to retrieve the ID of an entry.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
154 TARGETS may be a setting for `org-refile-targets' to define the eligible
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
155 headlines. When omitted, all headlines in all agenda files are
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
156 eligible.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
157 It returns the ID of the entry. If necessary, the ID is created."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
158 (let* ((org-refile-targets (or targets '((nil . (:maxlevel . 10)))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
159 (org-refile-use-outline-path
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
160 (if (caar org-refile-targets) 'file t))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
161 (spos (org-refile-get-location "Entry: "))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
162 (pom (and spos (move-marker (make-marker) (nth 3 spos)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
163 (get-file-buffer (nth 1 spos))))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
164 (prog1 (org-id-get pom 'create)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
165 (move-marker pom nil))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
166
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
167 ;;;###autoload
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
168 (defun org-id-get-with-outline-drilling (&optional targets)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
169 "Use an outline-cycling interface to retrieve the ID of an entry.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
170 This only finds entries in the current buffer, using `org-get-location'.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
171 It returns the ID of the entry. If necessary, the ID is created."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
172 (let* ((spos (org-get-location (current-buffer) org-goto-help))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
173 (pom (and spos (move-marker (make-marker) (car spos)))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
174 (prog1 (org-id-get pom 'create)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
175 (move-marker pom nil))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
176
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
177 ;;;###autoload
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
178 (defun org-id-goto (id)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
179 "Switch to the buffer containing the entry with id ID.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
180 Move the cursor to that entry in that buffer."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
181 (interactive)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
182 (let ((m (org-id-find id 'marker)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
183 (unless m
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
184 (error "Cannot find entry with ID \"%s\"" id))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
185 (switch-to-buffer (marker-buffer m))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
186 (goto-char m)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
187 (move-marker m nil)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
188 (org-show-context)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
189
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
190 ;;;###autoload
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
191 (defun org-id-find (id &optional markerp)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
192 "Return the location of the entry with the id ID.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
193 The return value is a cons cell (file-name . position), or nil
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
194 if there is no entry with that ID.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
195 With optional argument MARKERP, return the position as a new marker."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
196 (let ((file (org-id-find-id-file id))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
197 org-agenda-new-buffers where)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
198 (when file
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
199 (setq where (org-id-find-id-in-file id file markerp)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
200 (unless where
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
201 (org-id-update-id-locations)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
202 (setq file (org-id-find-id-file id))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
203 (when file
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
204 (setq where (org-id-find-id-in-file id file markerp))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
205 where))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
206
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
207 ;;; Internal functions
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
208
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
209 ;; Creating new IDs
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
210
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
211 (defun org-id-new (&optional prefix)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
212 "Create a new globally unique ID.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
213
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
214 An ID consists of two parts separated by a colon:
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
215 - a prefix
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
216 - an encoding of the current time to micro-second accuracy
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
217
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
218 PREFIX can specify the prefix, the default is given by the variable
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
219 `org-id-prefix'. However, if PREFIX is the symbol `none', don't use any
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
220 prefix even if `org-id-prefix' specifies one.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
221
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
222 So a typical ID could look like \"Org:4nd91V40HI\"."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
223 (let* ((prefix (if (eq prefix 'none)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
224 nil
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
225 (or prefix org-id-prefix)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
226 (etime (org-id-time-to-b62))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
227 (postfix (if org-id-include-domain
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
228 (progn
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
229 (require 'message)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
230 (concat "@" (message-make-fqdn))))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
231 (if prefix
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
232 (concat prefix ":" etime postfix)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
233 (concat etime postfix))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
234
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
235 (defun org-id-int-to-b62-one-digit (i)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
236 "Turn an integer between 0 and 61 into a single character 0..9, A..Z, a..z."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
237 (cond
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
238 ((< i 10) (+ ?0 i))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
239 ((< i 36) (+ ?A i -10))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
240 ((< i 62) (+ ?a i -36))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
241 (t (error "Larger that 61"))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
242
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
243 (defun org-id-b62-to-int-one-digit (i)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
244 "Turn a character 0..9, A..Z, a..z into a number 0..61.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
245 The input I may be a character, or a single-letter string."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
246 (and (stringp i) (setq i (string-to-char i)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
247 (cond
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
248 ((and (>= i ?0) (<= i ?9)) (- i ?0))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
249 ((and (>= i ?A) (<= i ?Z)) (+ (- i ?A) 10))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
250 ((and (>= i ?a) (<= i ?z)) (+ (- i ?a) 36))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
251 (t (error "Invalid b62 letter"))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
252
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
253 (defun org-id-int-to-b62 (i &optional length)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
254 "Convert an integer to a base-62 number represented as a string."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
255 (let ((s ""))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
256 (while (> i 0)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
257 (setq s (concat (char-to-string
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
258 (org-id-int-to-b62-one-digit (mod i 62))) s)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
259 i (/ i 62)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
260 (setq length (max 1 (or length 1)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
261 (if (< (length s) length)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
262 (setq s (concat (make-string (- length (length s)) ?0) s)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
263 s))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
264
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
265 (defun org-id-b62-to-int (s)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
266 "Convert a base-62 string into the corresponding integer."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
267 (let ((r 0))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
268 (mapc (lambda (i) (setq r (+ (* r 62) (org-id-b62-to-int-one-digit i))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
269 s)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
270 r))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
271
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
272 (defun org-id-time-to-b62 (&optional time)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
273 "Encode TIME as a 10-digit string.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
274 This string holds the time to micro-second accuracy, and can be decoded
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
275 using `org-id-decode'."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
276 (setq time (or time (current-time)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
277 (concat (org-id-int-to-b62 (nth 0 time) 3)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
278 (org-id-int-to-b62 (nth 1 time) 3)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
279 (org-id-int-to-b62 (or (nth 2 time) 0) 4)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
280
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
281 (defun org-id-decode (id)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
282 "Split ID into the prefix and the time value that was used to create it.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
283 The return value is (prefix . time) where PREFIX is nil or a string,
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
284 and time is the usual three-integer representation of time."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
285 (let (prefix time parts)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
286 (setq parts (org-split-string id ":"))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
287 (if (= 2 (length parts))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
288 (setq prefix (car parts) time (nth 1 parts))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
289 (setq prefix nil time (nth 0 parts)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
290 (setq time (list (org-id-b62-to-int (substring time 0 3))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
291 (org-id-b62-to-int (substring time 3 6))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
292 (org-id-b62-to-int (substring time 6 10))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
293 (cons prefix time)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
294
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
295 ;; Storing ID locations (files)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
296
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
297 (defun org-id-update-id-locations ()
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
298 "Scan relevant files for ID's.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
299 Store the relation between files and corresponding ID's."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
300 (interactive)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
301 (let ((files (append (org-agenda-files)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
302 (if (symbolp org-id-extra-files)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
303 (symbol-value org-id-extra-files)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
304 org-id-extra-files)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
305 org-agenda-new-buffers
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
306 file ids reg found id)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
307 (while (setq file (pop files))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
308 (setq ids nil)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
309 (with-current-buffer (org-get-agenda-file-buffer file)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
310 (save-excursion
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
311 (save-restriction
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
312 (widen)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
313 (goto-char (point-min))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
314 (while (re-search-forward "^[ \t]*:ID:[ \t]+\\(\\S-+\\)[ \t]*$"
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
315 nil t)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
316 (setq id (org-match-string-no-properties 1))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
317 (if (member id found)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
318 (error "Duplicate ID \"%s\"" id))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
319 (push id found)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
320 (push id ids))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
321 (push (cons file ids) reg)))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
322 (org-release-buffers org-agenda-new-buffers)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
323 (setq org-agenda-new-buffers nil)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
324 (setq org-id-locations reg)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
325 (org-id-locations-save)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
326
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
327 (defun org-id-locations-save ()
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
328 "Save `org-id-locations' in `org-id-locations-file'."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
329 (with-temp-file org-id-locations-file
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
330 (print org-id-locations (current-buffer))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
331
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
332 (defun org-id-locations-load ()
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
333 "Read the data from `org-id-locations-file'."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
334 (setq org-id-locations nil)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
335 (with-temp-buffer
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
336 (condition-case nil
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
337 (progn
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
338 (insert-file-contents-literally org-id-locations-file)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
339 (goto-char (point-min))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
340 (setq org-id-locations (read (current-buffer))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
341 (error
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
342 (message "Could not read org-id-values from %s. Setting it to nil."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
343 org-id-locations-file)))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
344
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
345 (defun org-id-add-location (id file)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
346 "Add the ID with location FILE to the database of ID loations."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
347 (unless org-id-locations (org-id-locations-load))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
348 (catch 'exit
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
349 (let ((locs org-id-locations) list)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
350 (while (setq list (pop locs))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
351 (when (equal (file-truename file) (file-truename (car list)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
352 (setcdr list (cons id (cdr list)))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
353 (throw 'exit t))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
354 (push (list file id) org-id-locations))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
355 (org-id-locations-save))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
356
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
357 ;; Finding entries with specified id
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
358
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
359 (defun org-id-find-id-file (id)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
360 "Query the id database for the file in which this ID is located."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
361 (unless org-id-locations (org-id-locations-load))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
362 (catch 'found
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
363 (mapc (lambda (x) (if (member id (cdr x))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
364 (throw 'found (car x))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
365 org-id-locations)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
366 nil))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
367
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
368 (defun org-id-find-id-in-file (id file &optional markerp)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
369 "Return the position of the entry ID in FILE.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
370 If that files does not exist, or if it does not contain this ID,
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
371 return nil.
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
372 The position is returned as a cons cell (file-name . position). With
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
373 optional argument MARKERP, return the position as a new marker."
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
374 (let (org-agenda-new-buffers m buf pos)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
375 (cond
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
376 ((not file) nil)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
377 ((not (file-exists-p file)) nil)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
378 (t (with-current-buffer (setq buf (org-get-agenda-file-buffer file))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
379 (setq pos (org-find-entry-with-id id))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
380 (when pos
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
381 (if markerp
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
382 (move-marker (make-marker) pos buf)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
383 (cons file pos))))))))
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
384
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
385 (provide 'org-id)
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
386
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
387 ;;; org-id.el ends here
19a1d523548c New file: org-id.el.
Carsten Dominik <dominik@science.uva.nl>
parents:
diff changeset
388
96052
2d6e9cbc2d7f Add arch tagline
Miles Bader <miles@gnu.org>
parents: 96045
diff changeset
389 ;; arch-tag: e5abaca4-e16f-4b25-832a-540cfb63a712