Happy New Year!
I was playing around with storing data in heading properties and then using links to modify them. For example, a heading might be a quiz question, and the links are the possible answers. Clicking on a link stores which link was clicked on as a property, and also counts the number of attempts.
I wrote a link type to do this that looks like this:
(org-add-link-type
"ans2"
(lambda (path)
(org-entry-put (point) "ANSWER" path)
(let* ((nattempts (org-entry-get (point) "NUM-ATTEMPTS"))
(num-attempts (if nattempts (string-to-number nattempts) 0)))
(org-entry-put (point) "NUM-ATTEMPTS" (number-to-string (+ num-attempts 1))))
(message "You selected %s for your answer" path)))
This works fine, but there were a few questions that came up in my mind about it.
1. It took two lines to set num-attempts because the first time the property does not exist and so you get nil from org-entry-get, so the var needs to be initialized to 0, and after that it does exist, and it appears the property is read as a string which must be converted to a number so i can do math on it later. Is that the most elegant way to do that?
2. I guess it makes sense that properties would be read as strings, but that wasn't obvious from the documentation they would be strings.
3. It also wasn't obvious that you have to give org-entry-put a string. If you try to set it to an integer, you get strange control characters like ^A or ^C.
Thanks!
John
-----------------------------------
John Kitchin
Associate Professor
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
http://kitchingroup.cheme.cmu.edu