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