Emacs calendar: show more than 3 months?

前端 未结 4 981
青春惊慌失措
青春惊慌失措 2021-02-14 09:46

In Emacs, when you display the calendar with M-x calendar, you get a three-month display – last month, this month, and next month – in a new window that\'s just 8 l

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-14 10:27

    I modified "12-MONTH CALENDAR -- SCROLLS BY MONTH (FORWARDS / BACKWARDS)" answer and adapted it to Emacs post 23.3 version - no calendar-for-loop macro - and changed scroll from by one month to by one year. This version show entire calendar for current year. Going backwards which < and forwards > by one year. It doesn't show on full screen, but half screen, which make it easy to use when working which vertical splits and it's more like extended version of build in calendar.

    ;; https://stackoverflow.com/questions/9547912/emacs-calendar-show-more-than-3-months
    (defun farynaio/year-calendar (&optional year)
      "Generate a one year calendar that can be scrolled by year in each direction.
    This is a modification of:  http://homepage3.nifty.com/oatu/emacs/calendar.html
    See also: https://stackoverflow.com/questions/9547912/emacs-calendar-show-more-than-3-months"
      (interactive)
      (require 'calendar)
      (let* (
          (current-year (number-to-string (nth 5 (decode-time (current-time)))))
          (month 0)
          (year (if year year (string-to-number (format-time-string "%Y" (current-time))))))
        (switch-to-buffer (get-buffer-create calendar-buffer))
        (when (not (eq major-mode 'calendar-mode))
          (calendar-mode))
        (setq displayed-month month)
        (setq displayed-year year)
        (setq buffer-read-only nil)
        (erase-buffer)
        ;; horizontal rows
        (dotimes (j 4)
          ;; vertical columns
          (dotimes (i 3)
            (calendar-generate-month
              (setq month (+ month 1))
              year
              ;; indentation / spacing between months
              (+ 5 (* 25 i))))
          (goto-char (point-max))
          (insert (make-string (- 10 (count-lines (point-min) (point-max))) ?\n))
          (widen)
          (goto-char (point-max))
          (narrow-to-region (point-max) (point-max)))
        (widen)
        (goto-char (point-min))
        (setq buffer-read-only t)))
    
    (defun farynaio/scroll-year-calendar-forward (&optional arg event)
      "Scroll the yearly calendar by year in a forward direction."
      (interactive (list (prefix-numeric-value current-prefix-arg)
                         last-nonmenu-event))
      (unless arg (setq arg 0))
      (save-selected-window
        (if (setq event (event-start event)) (select-window (posn-window event)))
        (unless (zerop arg)
          (let* (
                  (year (+ displayed-year arg)))
            (jarfar/year-calendar year)))
        (goto-char (point-min))
        (run-hooks 'calendar-move-hook)))
    
    (defun farynaio/scroll-year-calendar-backward (&optional arg event)
      "Scroll the yearly calendar by year in a backward direction."
      (interactive (list (prefix-numeric-value current-prefix-arg)
                         last-nonmenu-event))
      (farynaio/scroll-year-calendar-forward (- (or arg 1)) event))
    
    (define-key calendar-mode-map "<" 'farynaio/scroll-year-calendar-backward)
    (define-key calendar-mode-map ">" 'farynaio/scroll-year-calendar-forward)
    
    (defalias 'year-calendar 'farynaio/year-calendar)
    

提交回复
热议问题