CalendarWeekNumbers

GnuEmacs has support for displaying ISO weeks (WikiPedia:ISO_week_date) in CalendarMode since version 23.

Displaying ISO week numbers in CalendarMode

GnuEmacs 23.1 introduced the variable calendar-intermonth-text that can be used for displaying ISO week numbers in the Calendar window. The code bellow was adapted from this variable’s documentation:

(copy-face font-lock-constant-face 'calendar-iso-week-face)
(set-face-attribute 'calendar-iso-week-face nil
                    :height 0.7)
(setq calendar-intermonth-text
      '(propertize
        (format "%2d"
                (car
                 (calendar-iso-from-absolute
                  (calendar-absolute-from-gregorian (list month day year)))))
        'font-lock-face 'calendar-iso-week-face))

This will display the week number in a somewhat (the factor 0.7 above) smaller font. One can also add a header for the week numbers:

(copy-face 'default 'calendar-iso-week-header-face)
(set-face-attribute 'calendar-iso-week-header-face nil
                    :height 0.7)
(setq calendar-intermonth-header
      (propertize "Wk"                  ; or e.g. "KW" in Germany
                  'font-lock-face 'calendar-iso-week-header-face))

Navigating to a given ISO week

To display a given ISO week in Calendar use the command calendar-iso-goto-week, which is bound to g w in CalendarMode.

Support for ISO weeks in older versions of GnuEmacs

It’s also possible to display week numbers in the calendar on GnuEmacs 22, see e.g. [1].) However, EdwardReingold, the author of the CalendarMode, has stated many times [2] [3] [4], that this is not recommended since it can break things.

Here’s another patch to Emacs 22 (2008-12-07) that provides the following view of the calendar:

           May 2007                June 2007                July 2007      
     Su Mo Tu We Th Fr Sa     Su Mo Tu We Th Fr Sa     Su Mo Tu We Th Fr Sa 
 W18        1  2  3  4  5 W22                 1  2 W27  1  2  3  4  5  6  7 
 W19  6  7  8  9 10 11 12 W23  3  4  5  6  7  8  9 W28  8  9 10 11 12 13 14 
 W20 13 14 15 16 17 18 19 W24 10 11 12 13 14 15 16 W29 15 16 17 18 19 20 21 
 W21 20 21 22 23 24 25 26 W25 17 18 19 20 21 22 23 W30 22 23 24 25 26 27 28 
 W22 27 28 29 30 31       W26 24 25 26 27 28 29 30 W31 29 30 31 

The use of “W52” at the beginning of the week may be easier to read than the format of calendar-hack.el.

It has only been tested for the US-English settings of ‘M-x calendar’ using the assumption that Sunday is the first day of the week. Testing is also less than thorough. Its accuracy in producing week numbers for calendar months has only been reviewed back to 2004. Whether it affects negatively other features and functions of Emacs’s Calendar/Diary are also unknown. Use at your own risk.

The patch:

--- lisp/calendar/cal-iso.el
+++ lisp/calendar/cal-iso.el
@@ -131,6 +131,13 @@
                        (calendar-absolute-from-iso date)))
   (or noecho (calendar-print-iso-date)))
 
+(defun calendar-iso-week-string (&optional date)
+  "String of ISO week number of Gregorian DATE."
+  (format "W%02d "
+	  (extract-calendar-month
+	   (calendar-iso-from-absolute
+	    (calendar-absolute-from-gregorian date)))))
+
 (defun diary-iso-date ()
   "ISO calendar equivalent of date diary entry."
   (format "ISO date: %s" (calendar-iso-date-string date)))


--- lisp/calendar/calendar.el
+++ lisp/calendar/calendar.el
@@ -1810,6 +1810,10 @@
 (autoload 'calendar-iso-date-string "cal-iso"
   "String of ISO date of Gregorian date.")
 
+(autoload 'calendar-iso-from-absolute "cal-iso")
+
+(autoload 'calendar-iso-week-string "cal-iso")
+
 (autoload 'calendar-goto-islamic-date "cal-islam"
   "Move cursor to Islamic date."
   t)
@@ -2141,8 +2145,15 @@
              (truncate-string-to-width string 2)
            (substring string 0 2)))
        " "))
+   (setq indent (- indent 4))
    (calendar-insert-indented "" 0 t);; Force onto following line
-   (calendar-insert-indented "" indent);; Go to proper spot
+   (calendar-insert-indented ;; Go to proper spot
+    (calendar-iso-week-string ;; Insert ISO week number.
+     (list
+      month
+      (if (and (= 0 blank-days) (= 0 calendar-week-start-day))
+	  2 ;; Use 2nd day if 1st day of calendar month is Sunday.
+	1) year)) indent)
    ;; Add blank days before the first of the month
    (calendar-for-loop i from 1 to blank-days do (insert "   "))
    ;; Put in the days of the month
@@ -2155,7 +2166,10 @@
       (and (zerop (mod (+ i blank-days) 7))
            (/= i last)
            (calendar-insert-indented "" 0 t)    ;; Force onto following line
-           (calendar-insert-indented "" indent)))));; Go to proper spot
+           (calendar-insert-indented            ;; Go to proper spot
+            (calendar-iso-week-string ;; Insert ISO week number.
+             (list month (+ 2 i) year))
+            indent)))))
 
 (defun calendar-insert-indented (string indent &optional newline)
   "Insert STRING at column INDENT.

CategoryCalendar