Emacs 如何在不切换到*日历的情况下将日历光标移到可见日期*

Emacs 如何在不切换到*日历的情况下将日历光标移到可见日期*,emacs,elisp,Emacs,Elisp,我一直无法弄清楚为什么使用当前缓冲区或设置缓冲区不能将日历光标设置为可见日期。我希望该函数在不切换到日历窗口的情况下工作,也就是说,即使缓冲区被掩埋,它也应该工作。我尝试了0的静坐,但没有效果 我包括了一个使用with current buffer的坏例子和一个使用select窗口的工作例子 编辑:我在2005年3月的Emacs邮件列表中发现了一个类似的问题:我将进行更多的测试,但是下面的代码行修复了这个坏掉的示例——它紧跟在日历光标之后到可见日期 2013年12月25日:修改功能日历光标至可见

我一直无法弄清楚为什么使用当前缓冲区或设置缓冲区不能将日历光标设置为可见日期。我希望该函数在不切换到日历窗口的情况下工作,也就是说,即使缓冲区被掩埋,它也应该工作。我尝试了0的静坐,但没有效果

我包括了一个使用with current buffer的坏例子和一个使用select窗口的工作例子

编辑:我在2005年3月的Emacs邮件列表中发现了一个类似的问题:我将进行更多的测试,但是下面的代码行修复了这个坏掉的示例——它紧跟在日历光标之后到可见日期


2013年12月25日:修改功能日历光标至可见日期的第一个工作草案,使用设置窗口点:


以下是我的建议:

(with-selected-window (or (get-buffer-window (current-buffer) 0)
                          (selected-window))
  (calendar-cursor-to-visible-date date))
当然,事后调用设置窗口点也是完全可以接受的选择

(defun broken-example (&optional month year)
(interactive)
  (delete-other-windows)
  (if (get-buffer "*Calendar*")
    (kill-buffer "*Calendar*"))
  (calendar)
  (other-window 1)
  (when (get-buffer "*Calendar*")
    (with-current-buffer (get-buffer "*Calendar*")
      (calendar-generate-window month year)
      (let ((old-date (calendar-cursor-to-date))
           (today (calendar-current-date)))
        (cond
          ((and
              (calendar-date-is-visible-p old-date)
              (not (equal old-date today)))
            (calendar-cursor-to-visible-date old-date))
          ((calendar-date-is-visible-p today)
            (calendar-cursor-to-visible-date today))
          (t
            (calendar-cursor-to-visible-date (list month 1 year))))))))

(defun working-example (&optional month year)
(interactive)
  (delete-other-windows)
  (if (get-buffer "*Calendar*")
    (kill-buffer "*Calendar*"))
  (calendar)
  (other-window 1)
  (when (get-buffer "*Calendar*")
    (select-window (get-buffer-window "*Calendar*" (selected-frame)))
      (calendar-generate-window month year)
      (let ((old-date (calendar-cursor-to-date))
           (today (calendar-current-date)))
        (cond
          ((and
              (calendar-date-is-visible-p old-date)
              (not (equal old-date today)))
            (calendar-cursor-to-visible-date old-date))
          ((calendar-date-is-visible-p today)
            (calendar-cursor-to-visible-date today))
          (t
            (calendar-cursor-to-visible-date (list month 1 year)))))))
(defun lawlist-calendar-cursor-to-visible-date (date)
  "Move the cursor to DATE that is on the screen."
  (let* (
      (month (calendar-extract-month date))
      (day (calendar-extract-day date))
      (year (calendar-extract-year date))
      (buffer (buffer-name)))
    (goto-char (point-min))
    (forward-line (+ calendar-first-date-row -1
                     (/ (+ day -1
                           (mod
                            (- (calendar-day-of-week (list month 1 year))
                               calendar-week-start-day)
                            7))
                        7)))
    (move-to-column (+ calendar-left-margin (1- calendar-day-digit-width)
                       (* calendar-month-width
                          (1+ (calendar-interval
                               displayed-month displayed-year month year)))
                       (* calendar-column-width
                          (mod
                           (- (calendar-day-of-week date)
                              calendar-week-start-day)
                           7))))
    (if (get-buffer-window buffer (selected-frame))
      (set-window-point (get-buffer-window buffer (selected-frame)) (point)))))
(with-selected-window (or (get-buffer-window (current-buffer) 0)
                          (selected-window))
  (calendar-cursor-to-visible-date date))