0

Long story short: I'd like to implement something like the opposite of org-icalendar-exclude-tags (i.e., org-icalendar-include-tags).

I would like to export some entries from my org files into an ICS file using org-icalendar-combine-agenda-files. Only entries with a certain tag ("meet") should be included. I first tried setting org-export-select-tags but apparently, that's ignored by org-icalendar-include-tags.

Reading the documentation for the advanced export configuration I found that probably org-export-before-parsing-hook could do the job. The following is a minimal example of what I achieved so far:

; This is emacsrc.el
(require 'org)

(defun my-delete-entry ()
  "Delete the entry but no sub-entries."
  (delete-region (point) (save-excursion (org-next-visible-heading 1) (point)));
  (setq org-map-continue-from (org-element-property :begin (org-element-at-point)))
)

(defun my-filter-entries (backend)
  "Skip entries without 'meet' tag."
  (when (org-export-derived-backend-p backend 'icalendar)
    (org-map-entries #'my-delete-entry "-meet")))

(add-hook 'org-export-before-parsing-hook 'my-filter-entries)

; configuration for testing this with emacs --batch -q -l emacsrc.el
(setq org-agenda-files (quote ("test.org")))
(setq org-icalendar-combined-agenda-file "cal.ics")
(setq org-icalendar-include-body nil)
(setq org-icalendar-store-UID nil)

(org-icalendar-combine-agenda-files)

Feedings this file to Emacs with /usr/bin/emacs --batch -q -l emacsrc.el for the following example test.org file:

* This should be exported                                              :meet:
<2021-05-31 Mo 10:00>

* This should NOT be exported
<1999-05-31 Mo 10:00>
** <2021-05-31 Mo 12:00> But this should also be exported              :meet:

yields the following cal.ics file:

BEGIN:VCALENDAR
VERSION:2.0
X-WR-CALNAME:OrgMode
PRODID:-//foo//Emacs with Org mode//EN
X-WR-TIMEZONE:CEST
X-WR-CALDESC:
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:20210531T211047Z
UID:TS1-af9107ca-5f42-4dea-b07e-c3cda2638494
DTSTART:20210531T100000
DTEND:20210531T120000
SUMMARY:This should be exported
CATEGORIES:meet,test
END:VEVENT
END:VCALENDAR

As can be seen, the third entry ("But this should also be exported") is missing, although it has a "meet" tag.

Testing the my-delete-entry within test.org itself with the following code:

#+BEGIN_SRC elisp
(defun my-delete-entry ()
  "Delete the entry but no sub-entries."
  (delete-region (point) (save-excursion (org-next-visible-heading 1) (point)));
  (setq org-map-continue-from (org-element-property :begin (org-element-at-point)))
)

(org-map-entries #'my-delete-entry "-meet")
#+END_SRC

works, though, as the file is reduced to:

* This should be exported                                              :meet:
<2021-05-31 Mo 10:00>

** <2021-05-31 Mo 12:00> But this should also be exported              :meet:

Why is the sub-entry not correctly exported?

Is there another, more elegant way to achieve my goal of only exporting entries with a certain tag?

2
  • The question is too broad. Please pose specific questions separately. An example of such a question could be your "Why is the sub-entry not correctly exported?", with just enough info needed to pose (only) that specific problem. – Drew Jun 1 at 14:14
0

The following implementation of my-delete-entry does the job:

(defun my-delete-entry ()
  "Delete the entry but no sub-entries."
  (let ((pt (org-entry-end-position)))
    (when pt (delete-region (point) (- pt 1))))
  )

It's a simplification of

(defun my-delete-entry ()
  "Delete the entry but no sub-entries."
  (let ((pt (save-excursion (apply 'org-agenda-skip-entry-if '(timestamp)))))
    (when pt (delete-region (point) (- pt 1))))
  )

as a result of looking at the code of org-agenda-skip-entry-if (and subsequently org-agenda-skip-if). (Thanks go to DB for this optimization.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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