1

I use org-alert to send notifications about my agenda to libnotify, so I can see the notifications via Dunst. All works well but everytime the notification pops up, its title is pretty generic. I have Todo in multiple org files, so I am trying to differentiate between them, i.e. Work todo, Home todo....etc.

Digging into the org-alert.el, this line seems to change the title:

(defvar org-alert-notification-title "*org*" "Title to be sent with notify-send.")

You can see the default action is to set the title as *org. How do I set it to be the name of the buffer where I have the notified Todo list? I tried:

(defvar org-alert-notification-title (current-buffer) )

with no luck. Any help is appreciated.

2
  • It cannot be done statically (i.e. with just a defvar): it needs to be done dynamically, i.e. when the notification is generated.
    – NickD
    Commented Sep 14, 2021 at 11:43
  • emacs.stackexchange.com/tags/elisp/info
    – NickD
    Commented Sep 14, 2021 at 14:21

1 Answer 1

1

Assuming you have the current version of org-alert, I think the best way to do it is to redefine the org-alert--dispatch function so that the title is dynamically generated by calling a function to calculate it, instead of using the value of a variable:

(defvar org-alert-notification-title-function nil)

(defun org-alert-notification-title ()
  "If `org-alert-notification-title-function' is defined and is a function, call it
and return what it returns; otherwise return the value of the variable `org-alert-notification-title'"
  (cond ((null org-alert-notification-title-function) org-alert-notification-title)
        ((not (functionp org-alert-notification-title-function)) org-alert-notification-title)
        (t (funcall org-alert-notification-title-function))))

(defun org-alert--dispatch ()
  (let* ((entry (org-alert--parse-entry))
     (head (car entry))
     (time (cadr entry)))
    (when (org-alert--check-time time)
      (alert (concat time ": " head) :title (org-alert-notification-title)))))


All of these would be changes to org-alert.el (IOW, you are forking org-alert.el: if this works for you, I might submit the changes as a PR to the github site): the first two definitions are additions to the file, but the third is a replacement The above does not change the current behavior: it redefines org-alert--dispatch to call a function org-alert-notification-title (instead of using the variable of the same name directly - btw, emacs lisp allows the same name to be used for a variable and a function: if the name appears in a function context, the function value is used, otherwise the variable value is used).

When that function is called, it checks a (new) variable org-alert-notification-title-function: if that variable is nil or is not a function, then the value of the variable org-alert-notification-title is returned, which reproduces the current behavior. But if you redefine org-alert-notification-title-function to have a function that returns a string as its value, the function is called and the returned string is the return value of the call to the org-notification-title function. The point is that the function is called when the notification is needed, so it can take advantage of its context to return information of interest (like the file name of the buffer in which it is evaluated: it just so happens that it is evaluated in exactly the buffer of the file that contains the entry that gives rise to the notification - phew!)

So you can customize the behavior by defining your own function and setting it as the value of the variable org-alert-notification-title-function in your init file:

(defun ndk/org-alert-notification-title ()
  (concat "*ORG* " (let ((fn (buffer-file-name))) (if fn (file-name-base fn) "no file?"))))

(setq org-alert-notification-title-function #'ndk/org-alert-notification-title)

BTW, there is a bug in org-alert.el: it only recognizes DEADLINE or SCHEDULED timestamps - it does not recognize plain timestamps. See this Important note in the manual for why this is a serious omission.

In addition, org-alert's error handling is not particularly robust: if org-alert--parse-entry does not recognize a time, then a nil is passed to org-alert-check--time and the latter keels over and dies.

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.