Emacs org-mode; insert PROPERTIES drawer upon heading creation

838 Views Asked by At

I want to insert upon creating a heading a PROPERTIES drawer. So far I have this

(defadvice org-insert-heading (after add-id-stuff activate)
  (template-myid))

(defun template-myid ()
  (insert "\n:PROPERTIES:\n:TIME: "
      (format "%s" (format-time-string "%Y-%m-%dT%H:%M:%S"))
      "\n:VERTEX: "
      (format "%s" (shell-command "uuidgen" t))
      "\n:EDGES:  \n:END:"))

This is working -- sort of. My problem is the uuid is getting thrown around. The output looks like this

* Heading
:PROPERTIES:
:TIME: 2020-03-10T23:34:17
:VERTEX: 12836
:EDGES:  
:END:32bf9499-f9e2-49d9-b8e7-9edb40272411

Not sure how to make this behave. It would be nice to simply call org-tempo and insert my ready-made template, but I've not figured out how to do that...

1

There are 1 best solutions below

0
147pm On

This works

(defadvice org-insert-heading (after add-id-stuff activate)
  (template-myid))

(defun template-myid ()
  (insert "\n:PROPERTIES:\n:TIME: "
      (format-time-string "%Y-%m-%dT%H:%M:%S")
      "\n:VERTEX: "
      (org-id-uuid)
      "\n:EDGES:  \n:END:"))

which produced

* heading
:PROPERTIES:
:TIME: 2020-03-11T08:11:36
:VERTEX: 35e40480-7708-4003-9c84-f950b7ce87ce
:EDGES:  
:END:

Got help from NickD's suggestion above, Diego Zamboni over at the org-mode mail list ([email protected]), a Chris Wellons article, along with puzzling out the usual official ref dump suspects. Still, I'm not sure why (shell-command "uuidgen" t) produces two outputs. This is what they look like in *scratch*

(shell-command "uuidgen" t)
2827
b5da7e0a-84c0-4db8-91f3-871b681f3022

(org-id-uuid)
"0bb7a4e1-9fc2-4428-b8de-a2d9ef5c56ab"

although a straight uuidgen at the bash command line produces just the UUID. What's happening to produce the 2827 above first? I couldn't fathom the *Help* on shell-command.

The (org-id-uuid) function is in org-id.el and seems very thorough at its randomness.

Also, does anyone know how I could have done this by advise-ing a function in org-mode tempo templates? I could never figure out what function was actually handling the <...-TAB.