0

Suppose we have

*** Sample Task
:PROPERTIES:
:Tally:    42
:END:

Is there a way to quickly add a number to :Tally: without having to compute the sum manually and changing the property by hand?

EDIT: I tried making an interactive region selecting function which replaces the first instance of :Tally: with the added value:

(defun tally-add (start end num)
  (interactive "r\nn Number:")
  (query-replace-regexp ":Tally:\ *\([0-9]+\)" "\,(format ":Tally: %d" (+ num \#1))" start end)) 

However, when running on a region I get "%d Symbol's value as a variable is void".

2 Answers 2

2

I would use this instead:

(defun tally-add (n)
  (interactive "nN: ")
  (org-entry-put
   nil "TALLY"
   (format "%s" (+ n (string-to-number
                      (or (org-entry-get nil "TALLY") "0"))))))
0

You can do a query-replace-regexp to find the numbers and return a function applied to the number. Usual keybinding is M-C-%.

For the query, find lines with the property and match the number parte:

:Tally:\ *\([0-9]+\)

For the replacement, call an elisp form that reproduces the property line (in this case, adding 3 to the number found):

\,(format ":Tally: %d" (+ 3 \#1))

The \#1 tag refers to the first match group interpreted as a number.

2
  • I tried making a function from this answer above but am getting an error. Can you take a look at it?
    – George
    Commented Jul 27, 2017 at 19:18
  • What is the error? I re-tested it again on the sample you provided.
    – Juancho
    Commented Jul 27, 2017 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.