Subject: Centerd calendar buffer

From: E Sabof <[email-masked]>
Date: Thu, 30 May 2013 00:11:41 +0100

The following snippet will center the content of the calendar window. If
there is interest in integrating this into emacs, I can reformat it as a
patch. A possible interface to enable it could be
(setq calendar-left-margin 'auto)

Evgeni

(require 'cl-lib)

(defun calendar-mode-options ()
  (setq calendar-left-margin 1)
  (add-hook 'window-configuration-change-hook
            'es-calendar-win-config-hook nil t))
(add-hook 'calendar-mode-hook 'calendar-mode-options)

(defun es-calendar-win-config-hook ()
  (catch 'exit
    (with-current-buffer
        (or (get-buffer calendar-buffer)
            (throw 'exit nil))
      (dolist (win (get-buffer-window-list nil nil t))
        (with-selected-window win
          (let* (( max (+ (* 7 3 3)
                          (* 2 calendar-intermonth-spacing)
                          -1))
                 ( left (max 0 (/ (- (window-body-width) max) 2))))
            (dolist (ov (overlays-in (point-min) (point-max)))
              (when (and (overlay-get ov 'left-center-margin)
                         (eq (selected-window)
                             (overlay-get ov 'window)))
                (delete-overlay ov)))
            (save-excursion
              (goto-char (point-min))
              (cl-loop for ov
                       = (make-overlay
                          (line-beginning-position)
                          (1+ (line-beginning-position)))
                       do (progn
                            (overlay-put ov 'window (selected-window))
                            (overlay-put ov 'display (make-string left ?\s
))
                            (overlay-put ov 'left-center-margin t)
                            )
                       while (zerop (forward-line))
                       ))))))))

(defadvice calendar-generate (after center-calendar activate)
  (es-calendar-win-config-hook))