emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Orgmode] Re: My Python solution to generating unique Ids in headlin


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:

Nick Dokos <nicholas.dokos <at> hp.com> writes:

Try
     import sys
     sys.stdout.write("[%d]" % val)

Thanks. That works fine.

(shell-command "nextnum" t)

This worked fine.

It may be necessary to specify a complete path to the command.

I diodnt need to because the .BAT file was in a directory which is
part of the PAHT list.  By the way, I had to include a beginning
line of @ECHO OFF in the bat file.

But I still don't understand why you need an external program: what is
wrong with (insert (format "[%s]" (org-id-new)))? Are the IDs too ugly
or is there some other problem?

I'm struggling to find documentation or installing and using org-id.
I added the line (require 'org-id) to my .eamcs file
then discovered a variable to customise the method to internal
or to use an external command uuidgen which doesnt exist on Windows.

How do I change the name of the external command from uuidgent
to nextnum

The trouble with unique IDs in files is that it's easy for them to get
out of sync (leading to non-uniqueness), e.g. if there are two processes
trying to get a unique id at the same time.

This shouldnt be a problem as I am the only user.

Thanks for your help,
Charles




_______________________________________________
Emacs-orgmode mailing list
Remember: use `Reply All' to send replies to the list.
address@hidden
http://lists.gnu.org/mailman/listinfo/emacs-orgmode


reply via email to

[Prev in Thread] Current Thread [Next in Thread]