3

I want to change emacs default calendar week number so that it starts at a manually preset date. Normally the year is broken into 52 weeks and week 1 is the one that contains the 1st of January. I'd like to be able to either change this date to another, 20 of July (say), or to have a second week number that gets displayed when org-agenda is called. Like so

Week-agenda (W01):
Monday     20 July 2020 W01
Tuesday    21 July 2020
Wednesday  22 July 2020
Thursday   23 July 2020
Friday     24 July 2020
Saturday   25 July 2020
Sunday     26 July 2020

or

Week-agenda (W30 | W01):
Monday     20 July 2020 W30 | W01
Tuesday    21 July 2020
Wednesday  22 July 2020
Thursday   23 July 2020
Friday     24 July 2020
Saturday   25 July 2020
Sunday     26 July 2020

These are the closest posts that I could find so far link1 link2. I am using emacs 26.3 and org 9.2.6. This question is relevant because at my work place the week system is offset from the normal week system, and it would be great to be able to adjust emacs to it.

2

Looking at the souce code reveals, that Org uses calendar functions to calculate that. ... You need to change calendar-iso-from-absolute

Try following modification, this will affect calendars week calculation and with that org-modes. You can see the result, if you activate calendars week number view.

(defun calendar-iso-from-absolute (date)
  (let* ((approx (calendar-extract-year
                  (calendar-gregorian-from-absolute (+ date 2))))
         (year (+ approx
                  (calendar-sum y approx
                                (>= date (calendar-iso-to-absolute
                                          (list 1 1 (1+ y))))
                                1)))
         (weekstart (calendar-absolute-from-gregorian (list 7 (+ 20 2) year))))
    (list
     (1+ (/ (- date (if (> weekstart date)
                        (calendar-absolute-from-gregorian (list 7 (+ 20 2) (- year 1)))
                      weekstart))
            7))
     (% date 7)
     year)))

The function above needs some minor tweaking to get it 100% correct.
I do not know why the date needs to be 3 days less, I just took it over from the approx calculation at the beginning of this function.

Of course you could also write an advice :after for the function above and then modify the car of its result list.


edit to anser comment: to activate it from within your init, this function needs to be redifined, after the original version has been loaded. To do so you can put this new function definition in the :config part of (use-package cal-iso ...

(use-package cal-iso
  :defer t
  :config
  (defun calendar-iso-from-absolute (date)
    ...
    )
  )

or you could use

(with-eval-after-load "cal-iso"
  (defun calendar-iso-from-absolute (date)
   ...
    )
  )

because calendar-iso-from-absolute is defined in file cal-iso.el.

Or override it via advice (thanks to Basil). Note: you you have to give your own function a different name.

(defun my-calendar-iso-from-absolute (date)
  ...
  )
(advice-add #'calendar-iso-from-absolute :override #'my-calendar-iso-from-absolute)
  • Thanks @jue. I've made some edits to change the month and the offset of number of days (had to be +2 for some reason). I think that this has to do with the definition of the ISO week date described in the function documentation. One question remains though... How can I activate the function in init.el? Probably because it is a compiled function it does not load automatically (i've got to do it manually). – Andre Dec 14 '19 at 15:35
  • @Andre please see my edit – jue Dec 16 '19 at 10:00
  • Instead of redefining a function after its package has been loaded, I find it better to add an :override advice on the corresponding symbol using either define-advice or advice-add. This has several advantages: 1) the advice can be defined before its target function has been defined, i.e. at the top-level of your user-init-file; 2) the advice can easily be disabled and reenabled again, for debugging purposes; 3) the advice is listed in the *Help* buffer for its target function. – Basil Dec 16 '19 at 10:49
  • @Basil thanks, til – jue Dec 16 '19 at 12:23
  • 1
    BTW if you use the define-advice macro in Emacs 25+ (instead of the lower-level advice-add, it will generate the advice function name for you. – Basil Dec 16 '19 at 12:28

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.