From aa0004004b7f7e38e9169b24bcd32dd17f3c2a22 Mon Sep 17 00:00:00 2001 From: David Maus Date: Sun, 16 May 2010 16:27:33 +0200 Subject: [PATCH 1/2] Provide function that returns a string with a random (version 4) UUID. --- lisp/ChangeLog | 5 +++++ lisp/org-id.el | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 4fb3aac20..fafe7dbff 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,8 @@ +2010-05-16 David Maus + + * org-id.el (org-id-uuid): New function. Return string with + random (version 4) UUID. + 2010-05-15 Carsten Dominik * org-latex.el (org-export-latex-format-image): Add support diff --git a/lisp/org-id.el b/lisp/org-id.el index 06781613a..6175eee2c 100644 --- a/lisp/org-id.el +++ b/lisp/org-id.el @@ -318,6 +318,30 @@ So a typical ID could look like \"Org:4nd91V40HI\"." (t (error "Invalid `org-id-method'"))) (concat prefix unique))) +(defun org-id-uuid () + "Return string with random (version 4) UUID." + (let ((rnd (md5 (format "%s%s%s%s%s%s%s" + (random t) + (current-time) + (user-uid) + (emacs-pid) + (user-full-name) + user-mail-address + (recent-keys))))) + (format "%s-%s-4%s-%s%s-%s" + (substring rnd 0 8) + (substring rnd 8 12) + (substring rnd 13 16) + (format "%x" + (logior + #B10000000 + (logand + #B10111111 + (string-to-number + (substring rnd 16 18) 16)))) + (substring rnd 18 20) + (substring rnd 20 32)))) + (defun org-id-reverse-string (s) (mapconcat 'char-to-string (nreverse (string-to-list s)) "")) From d6064d2cdf5d8ed0a6568fe31d48d7cef258e214 Mon Sep 17 00:00:00 2001 From: David Maus Date: Sun, 16 May 2010 16:48:51 +0200 Subject: [PATCH 2/2] Use new customization value for `org-id-method'. Silently accept the old value for backward compatibility. --- lisp/ChangeLog | 3 +++ lisp/org-id.el | 28 ++++++++++++---------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index fafe7dbff..b143fbf60 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -2,6 +2,9 @@ * org-id.el (org-id-uuid): New function. Return string with random (version 4) UUID. + (org-id-method): Make 'uuid the new default value. + (org-id-new): Use `org-id-uuid' if call to uuidgen program + does not return a UUID. 2010-05-15 Carsten Dominik diff --git a/lisp/org-id.el b/lisp/org-id.el index 6175eee2c..66b1790cc 100644 --- a/lisp/org-id.el +++ b/lisp/org-id.el @@ -37,8 +37,9 @@ ;; time of the ID, with microsecond accuracy. This virtually ;; guarantees globally unique identifiers, even if several people are ;; creating IDs at the same time in files that will eventually be used -;; together. As an external method `uuidgen' is supported, if installed -;; on the system. +;; together. +;; +;; By default Org uses UUIDs as global unique identifiers. ;; ;; This file defines the following API: ;; @@ -84,18 +85,9 @@ :group 'org-id :type 'string) -(defcustom org-id-method - (condition-case nil - (if (string-match "\\`[-0-9a-fA-F]\\{36\\}\\'" - (org-trim (shell-command-to-string - org-id-uuid-program))) - 'uuidgen - 'org) - (error 'org)) +(defcustom org-id-method 'uuid "The method that should be used to create new IDs. -If `uuidgen' is available on the system, it will be used as the default method. -if not, the method `org' is used. An ID will consist of the optional prefix specified in `org-id-prefix', and a unique part created by the method this variable specifies. @@ -105,11 +97,13 @@ org Org's own internal method, using an encoding of the current time to microsecond accuracy, and optionally the current domain of the computer. See the variable `org-id-include-domain'. -uuidgen Call the external command uuidgen." +uuid Create random (version 4) UUIDs. If the program defined in + `org-id-uuid-program' is available it is used to create the ID. + Otherwise an internal functions is used." :group 'org-id :type '(choice (const :tag "Org's internal method" org) - (const :tag "external: uuidgen" uuidgen))) + (const :tag "external: uuidgen" uuid))) (defcustom org-id-prefix nil "The prefix for IDs. @@ -306,8 +300,10 @@ So a typical ID could look like \"Org:4nd91V40HI\"." unique) (if (equal prefix ":") (setq prefix "")) (cond - ((eq org-id-method 'uuidgen) - (setq unique (org-trim (shell-command-to-string org-id-uuid-program)))) + ((memq org-id-method '(uuidgen uuid)) + (setq unique (org-trim (shell-command-to-string org-id-uuid-program))) + (unless (org-uuidgen-p unique) + (setq unique (org-id-uuid)))) ((eq org-id-method 'org) (let* ((etime (org-id-reverse-string (org-id-time-to-b36))) (postfix (if org-id-include-domain