:PROPERTIES:
:ID:       10b1f18e-b221-4ab8-bce4-b6f20ad417db
:END:
#+title: Emacs Calendar/Agenda
#+filetags: :emacs:workflow:work:

**For the config, see [[id:1c87e4f1-fa7e-4200-9a29-a5a98d4c7ac9][Emacs Config]] for the Source of truth**

* Config:
#+begin_src emacs-lisp
  (require 'org-caldav)
  (require 'url)
  (require 'icalendar)
  (modify-coding-system-alist 'file "caldav-work\\.org\\'" 'utf-8-unix)
  (setq org-icalendar-timezone "Europe/London")
  (setq calendar-time-zone 60) ;; minutes offset from UTC (BST = +60)
  (setq calendar-standard-time-zone-name "GMT")
  (setq calendar-daylight-time-zone-name "BST")
  (setq org-caldav-timezone "Europe/London")
  (defvar my/outlook-ics-url
    "https://outlook.office365.com/owa/calendar/b910ea835e414663831e505f3d10baa2@microlise.com/d2568a0134fb49af92d39c1669c4729317662288011094411305/calendar.ics")

  (defvar my/outlook-org-file
    "D:\\_nextcloud\\master-folder\\org_files\\calendar\\caldav-work.org")

  (defvar my/ics-python-script
    "D:\\_nextcloud\\master-folder\\org_files\\calendar\\ics_to_org.py")

  (defun my/sync-outlook-calendar ()
    "Fetch and expand Outlook ICS (including recurrences) into org file."
    (interactive)
    (message "Syncing Outlook calendar...")
    (let* ((cmd (format "python \"%s\" \"%s\" \"%s\""
                        my/ics-python-script
                        my/outlook-ics-url
                        my/outlook-org-file))
           (result (shell-command-to-string cmd)))
      (message "Outlook calendar sync: %s" (string-trim result))))

  ;; Sync on startup and every 30 minutes
  (my/sync-outlook-calendar)
  (run-with-timer (* 30 60) (* 30 60) #'my/sync-outlook-calendar)
  ;; --- CalDAV for native Nextcloud calendars ---
  (setq org-caldav-url "https://nextcloud.zainezq.com/remote.php/dav/calendars/zaine/")
  (setq org-caldav-calendars
        '((:calendar-id "personal"
  		      :inbox "D:\\_nextcloud\\master-folder\\org_files\\calendar\\caldav-personal.org"
  		      :files nil)
  	(:calendar-id "zxh"
  		      :inbox "D:\\_nextcloud\\master-folder\\org_files\\calendar\\caldav-family.org"
  		      :files nil)))

  ;; --- Agenda files ---
  (setq org-agenda-files
        '("D:\\_nextcloud\\master-folder\\org_files\\calendar\\caldav-personal.org"
  	"D:\\_nextcloud\\master-folder\\org_files\\calendar\\caldav-work.org"
  	"D:\\_nextcloud\\master-folder\\org_files\\calendar\\caldav-family.org"
  	"D:\\_nextcloud\\master-folder\\org_files\\org_roam\\Books\\20250724230557-books_org_agenda.org"))
  


#+end_src

** Explanation:
- This configuration sets up Emacs to sync with both a Nextcloud calendar (via CalDAV) and an Outlook calendar (via ICS). It uses a Python script to fetch and convert the Outlook ICS data into an org file format that Emacs can read. The Nextcloud calendar is synced directly using the org-caldav package. Both calendars are included in the org-agenda files, so that I can view all events in one place. The sync for the Outlook calendar runs every 30 minutes to keep it up to date.
- To sync the Outlook calendar, you call the ~my/sync-outlook-calendar~ function, which runs the Python script to fetch and convert the ICS data. The Nextcloud calendar is synced automatically by org-caldav whenever you open Emacs or refresh the calendar.
