I'm trying to implement a gamification system in org-mode.

Basically, I assign a number of points to a checkboxed task and when I tick the checkbox I add the number of points to a total at the top of my file.

I'm trying to write a function that does this automatically, like for example turning this:

Total points: 45

- Things to do today
  - [ ] Wa|sh clothes: 5

into this:

Total points: 50

- Things to do today
  - [X] Wa|sh clothes: 5

when the cursor is at the vertical bar.

How would I write a function that extracts the number from the end of the line and stores it in a variable, extracts the top number and stores it in a variable, adds the variables together and replaces the top number with the new total?

  • 1
    What have you tried so far? Is this just a request that someone write you a function? If so, it should be off-topic, IMO. – Drew May 6 '17 at 16:56
  • 1
    Please clarify what you mean by "just one function": the number of functions used is usually of no relevance at all (invisible to the user, for example), kind of like the number of feet used while washing the dishes. – Stefan May 6 '17 at 17:39
  • So now there's no question at all. If you want to keep your question here on Emacs.SX I suggest you remove your answer and replace it with a properly formulated question. Then you can separately provide an answer to it (but not as an edit of your question). – Stefan May 7 '17 at 1:36
  • Sorry for the lack of clarity, my bad. I've edited the question to ask a specific question now. I will separately answer my question with the right answer. Just waiting for the question to be taken off hold. – Antonius Vado May 7 '17 at 1:53

I found the answer to my own question - the trick was to use setq with thing-at-point to get the value into a variable:

(defun tally-adder ()
  "Adds local tally to global tally"
    (move-end-of-line nil)
    (setq end-of-line-tally (thing-at-point 'number))
    (move-end-of-line nil)
    (setq global-tally (thing-at-point 'number))
    (backward-kill-word 1)
    (insert (number-to-string (+ end-of-line-tally global-tally)))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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