1

I only want certain files to be added to the agenda. I'm using the following code to achieve that:

(use-package org-agenda
  :ensure nil
  :custom
  (org-agenda-files
   (list
    (concat org-directory "/bookmarks.org")
    (concat org-directory "/calendar.org")
    (concat org-directory "/contacts.org")
    (concat org-directory "/work.org"))))

The problem is that if the files don't exist in the org-directory then Emacs will complain and gives an error. I was wondering if there is a way to rewrite the code above in such a way that the files are only added if they are available. Also, if the org-directory is not available at all I don't want to receive a warning either.

1 Answer 1

3

Why not do something like this:

(seq-filter #'file-exists-p
            (mapcar #'(lambda (file) (file-name-concat org-directory file))
                    '("bookmarks.org"
                      "calendar.org"
                      "contacts.org"
                      "work.org")))

The seq-filter function only appends items to the newly returned list, if the predicate returns t. In this case, the predicate is file-exists-p, but you can do whatever specific thing you like, including a lambda, of course. Instead of passing the files, and concatenating the name manually, this code snippet does so programmatically, using mapcar. Now you can add files ad nauseum. They are just a list of strings.

2
  • 1
    seq-take-while will not work: if the first item does not exist, the rest are going to be ignored as well (untested, but the doc you linked to seems pretty clear). I think you want seq-filter.
    – NickD
    Nov 15, 2022 at 17:28
  • 1
    @NickD Rats, I meant seq-filter. Editing... thanks! Nov 15, 2022 at 18:07

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.