0

I use org mode for many things including a Getting things done file and daily I am adding many headings, mostly in the format:

* TODO [#1] "Heading text here, to be typed in by me"
SCHEDULED: <2023-09-03 Sun .+1d>

I do use M-Ret or S-M-Ret to enter a new heading, but this does not add a priority, nor the SCHEDULED line.

I would like to add a menu item to automatically insert a heading in this format.

So far I have come up with this:

(defun myheading1 ()
  
  (interactive)       ; permit invocation in minibuffer

  (insert (format-time-string " \nSCHEDULED: <%Y-%m-%d .+1d>"))

  ) 


;; Add to toolbar:

(tool-bar-add-item "paste" 'myheading1
     'myheading1
      :help   "Insert heading"
      :label " Heading")

Which gives:

[Blank line]
SCHEDULED: <2023-09-16 .+1d>

Now I would like to use:

(org-insert-todo-heading)

in my code to insert eg:

** TODO

in front of

SCHEDULED: <2023-09-16 .+1d>

to get:

** TODO 
SCHEDULED: <2023-09-16 .+1d>

I tried:

(defun myheading1 ()
  
  (interactive)       ; permit invocation in minibuffer

  (org-insert-todo-heading)
  
  (insert (format-time-string "\nSCHEDULED: <%Y-%m-%d .+1d>"))

  ) 

;; Add to toolbar:
;; See other sections in this file for comments as to how to
;; Icons are in /usr/share/emacs/26.1/etc/images
(tool-bar-add-item "paste" 'myheading1
     'myheading1
      :help   "Insert heading"
      :label " Heading")

But I get error: Wrong number of arguments

What am I doing wrong with my code?

5
  • Please narrow the question - one, specific question per post. Thx.
    – Drew
    Sep 16, 2023 at 21:30
  • I think you are looking for org-capture.
    – NickD
    Sep 16, 2023 at 21:41
  • 1
    @Drew - I learned more about how to achieve what I want (using org-insert-todo-heading) and have made it one question :)
    – ironfish
    Sep 16, 2023 at 23:58
  • I still stand by my previous comment: you are looking for org-capture.
    – NickD
    Sep 17, 2023 at 0:19
  • @NickD, thanks once again for your input. I looked at org-capture and TBH it seems complex and I dont understand it. It seems I have to specify a destination file, and execute a number of commands over and over. I would like to first try and get my code running.
    – ironfish
    Sep 17, 2023 at 0:20

2 Answers 2

1

Org mode has a built-in way to facilitate adding task entries like the ones you want: org-capture. It is a very flexible (and quick!) way to add a new task for later consideration and get back to whatever you were doing without breaking stride. It is also easy to start with (although admittedly you can get into some deep waters when trying to do complicated things).

All you have to do is add the following to your init file and restart Emacs:

  ;; as recommended in the Activation section of the manual, we bind `org-capture' to `C-c c'
  (global-set-key (kbd "C-c c") #'org-capture)

  ;; add an entry to `org-capture-templates' after `org-capture' has been loaded
  (with-eval-after-load 'org-capture
    (add-to-list 'org-capture-templates
                 '("T" "add my TODO task"
                   entry (file+headline "~/org/tasks.org" "My tasks")
                   "* TODO [#A] %?\nSCHEDULED: <%<%Y-%m-%d %a> .+1d>")))

Then C-c c T will bring up the filled-in template, with the cursor at the place indicated by %? in the template so you can fill in the description. Then press C-c C-c to finalize the capture and you are back where you were before the interruption, but with the new task safely squirreled away.

You have to create the file ~/org/tasks.org and the heading My tasks in that file by hand before you can use the above template, but after that you can keep adding tasks to your heart's content.


Notes:

  • IMO, the best organization is to have a single file where all incoming tasks are squirreled away (like ~/orgh/tasks.org above). During daily review, you can refile those tasks to the appropriate place (which will vary depending on many things - there is no way to cover all the possibilities).

  • Things like SCHEDULED, priorities etc are best added during review, not during capture. Capture is meant to be fast and require virtually no thinking: it just makes sure that you won't forget something.

See the Capture and Refiling sections in the manual. To see how one person does things with Org mode, I recommend Bernt Hansen's document Org mode - Organize your life in plain text!.

20
  • To the downvoter: I assume you did that because the OP specifically asked for a fix to his code. My answer however is not meant for the OP so much (he posted his own answer with the fix) but for people who stumble on this question without knowing that that there is a better answer to the original question (how to add a heading according to a template). Having them exposed to org-capture might be a useful counterbalance to the tendency to go write code to do it. Emacs is extensible, but that does not mean that every question requires an extension to be answered.
    – NickD
    Sep 18, 2023 at 15:34
  • NickD thanks for taking the time to write your post about how to use Capture. If a destination file is required, this would not work for me, since I version files using the filename. Also, the solution I came up with works really well for me, and does not seem as complex as having to setup Capture. I will give your code a try though and see how it works.
    – ironfish
    Sep 18, 2023 at 18:59
  • Hi NickD. I dont think your answer works for me for a bunch of reasons. 1) It requires a destination file which I do not have or want 2) I disagree that it is best to later add eg SCHEDULED - why would I enter a task and know when to schedule it, but not schedule it, leave it till later, have to come back to it, and then schedule it. 3) Your way to enter the TODO needs Cc c T to initiate it, C-c C-c to finish it. My way needs a button click to initiate it and nothing to finish it. That saves hundreds of key presses per day.
    – ironfish
    Sep 18, 2023 at 19:10
  • 4) My way inherets the TODO type eg DEADLINE, from the previous heading, which I need. Your way does not - I would need to change from TODO to eg DEADLINE each time. For this reason, I dont think your post is an answer.
    – ironfish
    Sep 18, 2023 at 19:11
  • Re your: "that there is a better answer to the original question (how to add a heading according to a template)." - I disagree that this is an answer for the reason explained, much less a better one.
    – ironfish
    Sep 18, 2023 at 19:16
0

Searching thru many posts, I found the answer. Just had to add a "t" in:

  (org-insert-todo-heading t)

This code works:

(defun myheading1 ()
  
  (interactive)       ; permit invocation in minibuffer

  (org-insert-todo-heading t)
  
  (insert (format-time-string "[#1] \nSCHEDULED: <%Y-%m-%d %a .+1d>"))

  ) 

;; Add to toolbar:
;; See other sections in this file for comments as to how to
;; Icons are in /usr/share/emacs/26.1/etc/images
(tool-bar-add-item "paste" 'myheading1
     'myheading1
      :help   "Insert heading"
      :label " Heading")

And outputs, if there is no previous heading, eg:

* TODO [#1] 
SCHEDULED: <2023-09-16 Sat .+1d>

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.