2

I have a lot of org files in emacs 27.1 so when I do an agenda listing for the week it takes about 20seconds. Is there a nice way to do this in the background? I've put together the following function and it sort of works. The Org Agenda buffer that it shows in the finish lambda is blank, but the agenda.org file is correct but it's just an org file not the special Org Agenda buffer.

(defun async-org-agenda-list()
  (interactive)
  (async-start

   ;; start
   (lambda ()
     (setq org-agenda-files '("~/org/" "~/org/projects"))
     (org-agenda-list)
     (switch-to-buffer "*Org Agenda*")
     (append-to-file (point-min) (point-max) "~/agenda.org")
     )

   ;; finish
   (lambda (msg)
     (message "done")
     )
   )
  )

Is there a way to do the Agenda in the background and display it in the Org Agenda buffer in the correct mode?

2
  • have you tried calling (org-agenda) rather than list?
    – zzkt
    Aug 16, 2021 at 9:27
  • @zzkt (org-agenda) just pops up a menu, I used (org-agenda-list) so that I get the results for the week which is what is taking so long to do with all my tasks (378 TODOS last count)
    – map7
    Aug 17, 2021 at 1:14

1 Answer 1

1

SECOND EDIT

A solution that also fixes the links has been posted/implemented here.

Thanks Thierry!

EDIT

Okay, so you can fix it by adding the following two lines before just before the (goto-char (point-max)) in the async-when-done function (see the comment to this answer also):

(goto-char (point-min))
(replace-string "#" "")

I will send a mail about it to the emacs-devel mailing list.

END EDIT

Nice idea!

The following answer works for simple org files. However, the async API seems to error on some more complex org files (bug?). Anyway, would be nice to hear back how it works on your files.

Okay, then here follows the code:

(defun map-text-properties (props)
  (let ((plist (caddr props)))
    (while plist
      (put-text-property (1+ (nth 0 props))
                         (1+ (nth 1 props))
                         (car plist)
                         (cadr plist))
      (setq plist (cddr plist)))
    (when props
      (map-text-properties (nthcdr 3 props)))))

(async-start
 ;; What to do in the child process
 (lambda ()
   (setq org-agenda-files '("~/spacemacs/diary.org"))
   (org-agenda-list)
   (buffer-string))

 ;; What to do when it finishes
 (lambda (result)
   (switch-to-buffer-other-window "*Org Agenda*")
   (insert (car result))
   (map-text-properties (cdr result))
   (org-agenda-mode)))

The map-text-properties properties function is required because the async API returns the text object as a list. There is probably an easier way to convert the list back into a text object (but I did not look very much into that).

6
  • I have debugged it a little, and I have found that the problem is that the async sentinel function (async-when-done) is not able to read the returned string. However, this is not due to the async API, because when I print my Org Agenda buffer (using (buffer-string)) and then try to read the output, I already get the same error. But at least this method works, if you 'dismiss' the text properties. Also there must be a workaround, e.g. to first replace all # in the async process buffer, before reading it. Aug 16, 2021 at 21:42
  • I have opened an issue/notification/suggestion about it at emacs-async Aug 16, 2021 at 23:26
  • That worked. I did have to put the two lines in my async-when-done function like you. Thanks. It's getting other tags in my list like REDUNDANT. Also I cannot click on any of the agenda items to take me there. So all the links are gone. Is there a way to keep the links intact?
    – map7
    Aug 17, 2021 at 1:33
  • Also the colour formatting is off. Other than that it works great. I can continue using my emacs as it's getting my agenda ready.
    – map7
    Aug 17, 2021 at 1:42
  • Ah, the link and color formatting work fine here (i.e. jumping to the original location works). So how far is the color formatting off? Because I was surprised that I has to put the 1+'s into the put-text-property form. Maybe you can fix it by removing them? If not, then maybe open a chat, or contact me on the Spacemacs gitter (because Stack exchange warns that comments are not meant for long discussions). If we find a solution then we can edit the answer again here... Aug 17, 2021 at 5:42

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.