Search code examples
emacscalendar

How to create an emacs diary entry that combines diary-block and diary-cyclic?


I would have thought this was an FAQ,but so far I've been unable to write a search expression that finds it. SO...

I want to write expressions in an emacs diary that uses both the diary-block and diary cyclic functions to select dates. That is, something that marks e.g., rehearsals that take place every Tuesday but only from September 8 through November 12. This seems like something that should be possible, but I can't figure out how to do it from the emacs calendar guides that I've read.


Solution

  • You're probably missing the key point that when you write %%(...), the part after the %% is an arbitrary lisp form, rather than one of a limited set of options. Hence the answer is simply:

    %%(and (diary-block ...) (diary-cyclic ...))
    

    Some other examples of combining things:

    %%(or (diary-float t 3 2) (diary-float t 3 4))
    The 2nd and 4th Wednesday of each month.  (Not a 14-day cycle)
    
    %%(or (diary-float '(1 3 5 7 9 11) 4 3) (diary-float '(2 4 6 8 10 12) 2 3))
    Monthly dates alternating between the 3rd Tuesday and the 3rd Thursday
    of each month.
    
    %%(my-diary-foo)
    Completely custom...
    

    Where my-diary-foo is a custom function:

    ;; To be called from diary-sexp-entry, where DATE, ENTRY are bound.
    (defvar date)
    (defun my-diary-foo ()
      "The first Thursday of the month following the 1st."
      ;; The first Thursday of the month unless the 1st is a Thursday,
      ;; in which case it will be on the 8th (being the 2nd Thursday).
      (if (and (eql (calendar-day-of-week date) 4) ;; Thursday
               (or (eql (calendar-extract-day date) 1)   ;; 1st of the month
                   (eql (calendar-extract-day date) 8))) ;; 8th of the month
          (diary-float t 4 2)
        (diary-float t 4 1)))
    

    For more information, read C-hig (emacs)Sexp Diary Entries