From mboxrd@z Thu Jan 1 00:00:00 1970 From: Carsten Dominik Subject: Re: Re: My Python solution to generating unique Ids in headlines Date: Thu, 5 Mar 2009 10:37:47 +0100 Message-ID: <5639E37F-D378-4C36-8DB9-CBE303CC36BC@uva.nl> References: <200903042320.n24NKDmD007471@mail11.syd.optusnet.com.au> <13242.1236211183@alphaville.usa.hp.com> Mime-Version: 1.0 (Apple Message framework v930.3) Content-Type: multipart/mixed; boundary="===============0817746649==" Return-path: Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LfA1Z-0005Pz-5X for emacs-orgmode@gnu.org; Thu, 05 Mar 2009 04:37:53 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LfA1X-0005OO-4S for emacs-orgmode@gnu.org; Thu, 05 Mar 2009 04:37:51 -0500 Received: from [199.232.76.173] (port=32817 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LfA1W-0005O3-R8 for emacs-orgmode@gnu.org; Thu, 05 Mar 2009 04:37:50 -0500 Received: from mail-ew0-f179.google.com ([209.85.219.179]:52759) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1LfA1V-0006V6-Ts for emacs-orgmode@gnu.org; Thu, 05 Mar 2009 04:37:50 -0500 Received: by ewy27 with SMTP id 27so3128322ewy.42 for ; Thu, 05 Mar 2009 01:37:48 -0800 (PST) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Charles Cave Cc: emacs-orgmode@gnu.org --===============0817746649== Content-Type: multipart/alternative; boundary=Apple-Mail-3-240629761 --Apple-Mail-3-240629761 Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit 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 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. > Emacs-orgmode@gnu.org > http://lists.gnu.org/mailman/listinfo/emacs-orgmode --Apple-Mail-3-240629761 Content-Type: text/html; charset=US-ASCII Content-Transfer-Encoding: quoted-printable 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)

<= div>   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.
Emacs-orgmode@gnu.org
http://lists.= gnu.org/mailman/listinfo/emacs-orgmode
= --Apple-Mail-3-240629761-- --===============0817746649== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Emacs-orgmode mailing list Remember: use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode --===============0817746649==--