0

I want to run code after adding a deadline, for example to move it forward by one day in case the deadline is early in the day, or by a few hours in case it's on a different time zone.

This attempt with a hook and a function is not called upon setting a deadline with C-d:

(defun my-org-deadline(&rest ignore)
  (interactive)
  (message "Called!!!!!")
  (let ((todo (org-get-todo-state))
        (deadline (org-entry-get nil "DEADLINE"))
        )

    ;; move deadline ahead by 1 day, to avoid missing it.
    (when (and todo deadline)
        (org-back-to-heading)
        (search-forward "DEADLINE: ")
        (org-timestamp-change -1 'day)
        )))

(add-hook 'org-deadline #'my-org-deadline)

The function works properly if I call it, but it is not called after setting a deadline.

What is the proper way to make code run after setting a deadline?

1 Answer 1

1

There is no hook called org-deadline. Most hooks are called <mumble>-hook so when you are looking for one, try C-h v org--hook and press TAB: that should give you a completion buffer with all the available hooks whose names start with org-.

Unfortunately, there is no hook defined to allow you to call a function after you set a deadline. But you can still use an advice: define your function to do what you want, and then add it as an after advice to org-deadline. Something like this should work with your my-org-deadline function:

(advice-add 'org-deadline :after #'my-org-deadline)

C-h f org-deadline will then tell you that this function has been advised. You can remove the advice with

(advice-remove 'org-deadline #'my-org-deadline)

NOTE: This SHOULD work I think, but it doesn't for me: I get a wrong-number-of-arguments error when org-deadline gets called. The error happens in various functions and I have not had time to debug the problem yet.

OTOH, I tried it on a different machine with slightly more recent emacs and it works fine:

GNU Emacs 28.0.50 (build 3, x86_64-pc-linux-gnu, GTK+ Version 3.24.13, cairo version 1.16.0) of 2020-10-30 Org mode version 9.4 (release_9.4-53-g23f941)

so I suspect the solution is fine in general: there must be something wrong with the emacs/org version on the other machine.

3
  • 1
    Good advice on function advising! I also get the error, apply: Wrong number of arguments: (1 . 2), 0, which is unfortunate, and I'll remember to subtract days myself. Dec 14, 2020 at 16:41
  • What version of emacs/Org mode are you running?
    – NickD
    Dec 14, 2020 at 23:21
  • Emacs is Version 27.1 (9.0), Org-mode is 9.3. Dec 15, 2020 at 12:31

Your Answer

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

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