|
From: | Carsten Dominik |
Subject: | Re: [Orgmode] Re: My Python solution to generating unique Ids in headlines |
Date: | Thu, 5 Mar 2009 10:37:47 +0100 |
Hi Charles, if you want a pure Emacs solution for this, here is one (defvar charles-num-file "C:/charles/gtd/todonum.txt") (defun charles-add-id () "Add ID number to headline." (interactive) (save-excursion (org-back-to-heading t) (when (looking-at ".*\\[#[0-9]+\\]") (error "ID number already present")) (when (looking-at org-complex-heading-regexp) (goto-char (match-end 4)) (insert (format " [#%s]" (charles-next-num))) (org-set-tags nil 'align)))) (defun charles-next-num () "Get next number" (let (num) (with-temp-buffer (if (file-exists-p charles-num-file) (insert-file-contents-literally charles-num-file)) (goto-char (point-min)) (setq num (1+ (or (ignore-errors (read (current-buffer))) 0)))) (with-temp-file charles-num-file (insert (number-to-string num))) num)) However: It really depends on how much you want to depend on these numbers. In fact, my first implementation of IDs in org did use numbers just like yours, but I moved away from them because they are simply not reliable. 1. If you ever sit on a different computer and would like to create a task, you will mess up the numbering. 2. If you forget to backup and restore the numbering file, you will run into trouble. 3. No good way to collaborate with others. But if you are convinced that you alone will use this, with a single computer, it might work OK. The one advantage of numbers as IDs is that they are better readable with the eye, and that you can even remember "Oh yes, this was task 136". However, if it is a computer that is looking at the IDs, there are much better alternatives: 1. Of course, as Nick proposes, the IDs created by uuidgen. Globally unique, they will never ever give you any trouble. They are long, though, if you want to store them in the headline - that's why Org has them in a property. 2. Instead of (charles-next-num), you could use (org-id-time-to-b36) This will give you an encoding of the current time, to microsecond accuracy, in just 12 characters, good enough for multiple computers and even for large collaborations. Even if 2 people are trying to make an ID at exactly the same time, chances are still 1 in 100000 or so that they would actually get the same ID. 3. If you are sure the you never make 2 IDs in the same second, you can nibble off the last digits, like (substring (org-id-time-to-b36) 0 -4) which is an 8 character encoding of the creation time, accurate to a second. Quite safe for yourself, unless you use a program like org-map-entries to create these IDs for many entries in very short time. You could even nibble away the first digit which will not become significant for another 100 years or so. (substring (org-id-time-to-b36) 1 -4) 7 characters, one second accuracy, hard to beat. HTH - Carsten On Mar 5, 2009, at 3:40 AM, Charles Cave wrote:
|
[Prev in Thread] | Current Thread | [Next in Thread] |