3

I want my agenda to show the current week starting from today until the end of the week, Sunday. How do I go about achieving this?

The code

(setq org-agenda-custom-commands '(("w" "Current week"
                                  (agenda "" ((org-agenda-span 'week)
                                              (org-agenda-start-day "-0d")
                                              (org-agenda-start-on-weekday 1))))))

Shows the current week starting from Monday rather than the current day, if I set org-agenda-start-on-weekday to nil then the agenda shows until next Tuesday (if today is Tuesday for example).

NOTE: I can compute the number of days from today until next Monday (though I am not 100% sure how to do it in elisp) and set org-agenda-span accordingly, but I don't think I can do it as part of org-agenda-custom-commands.

1

That can be done easily if you can compute the days between today and next sunday, something you said you already achieved, which can be done in several ways and I find to be the trickiest part.

Adding this to your custom agenda definitions (and re-evaluating it) will do it:

(setq org-agenda-custom-commands
  '(("s" "From today to next sunday"
      ((agenda ""
           ((org-agenda-start-on-weekday nil)
            (org-agenda-span (my/days-to-next-sunday))))))
         )
  ;; ... other commands here
)

my/days-to-next-sunday should return the number of days between today and next Sunday you want to include in the agenda, I do assume that you want today included, so there is no need to look back 1 day in custom command definition.

Key s to call it from the agenda menu can be changed to anything you like.


As an aside example, taking the bare-bones, lazy (and not the better/proper) lane, given that (format-time-string "%u") will return the current day of week, and those run from 0 (Sunday) to 6 (Saturday) this will return the number of days in between today and Sunday, today included. Please, spare any possible dumb bug, it's untested code just to show the idea.

(defun my/days-to-next-sunday()
  (let ((dayspan 0)
        (today (string-to-number (format-time-string "%u"))))
    (cond
      ((> today 0)
        ; from today till sunday, today included
        (setq dayspan (- 8 today))) 
      ((= today 0)
        ; sunday to sunday
        (setq dayspan 8)))))

Probably I'd rely on calc or system date to find the dayspan if I were to write a more reusable way to find intervals, but it'll make the example harder to understand.

1
  • 1
    Thanks! I didn't know that a function would be evaluated when the agenda is shown (I didn't notice the quote in my own code). I ended up simply setting org-agenda-span to (- 8 (string-to-number (format-time-string "%u"))) – Tohiko Jun 12 '20 at 13:40

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.