GOAL:此线程的目标是创建两 (2) 个数学公式,以替换函数中 @lawlist 的长手解lawlist-calendar-cursor-to-visible-date
(以下)。
故事问题
Emacs 中现在存在一个 12 个月的日历,每次向前和向后滚动一个月(或更长)。功能lawlist-calendar-cursor-to-visible-date
用于标记指定事件的叠加日期(例如生日、假期、约会等);或者,只需将光标移动到特定日期。 @lawlist 老手设计了一个解决方案,不完全使用数学方程来计算显示的 365 天中每一天的光标位置。可以创建两 (2) 个简洁的算法来替代长期解决方案。
12 个月滚动日历的工作草案(without长期解决方案)可以在这里找到:
https://stackoverflow.com/a/21409154/2112489
LEGEND:
displayed-month
(数字 1 到 12)是出现在缓冲区左上角的月份,并且随着 12 个月日历向前或向后滚动,该月份也会发生变化。
目标month
(数字 1 到 12)是我们需要在两个数学公式的帮助下找到的月份——它的位置根据标记的日期(例如生日、假期、约会)而变化,并且根据displayed-month
在缓冲区的左上角。目标month
可以处于 12 个可能位置中的任意一个。有三 (3) 种可能x
轴坐标(即6, 31, or 56)。有四 (4) 种可能y
轴坐标(即0, 9, 18 or 27)。 [引用 x / y 坐标:http://www.mathsisfun.com/data/cartesian-coordinates.html ]
A row
横向定义为3个月。
A column
纵向定义为4个月。
第一个论坛必须等于0, 9, 18 or 27取决于该点是否打开row
1、2、3 或 4——即从上到下。
第二个公式必须等于6, 31, or 56取决于该点是否打开column
1、2 或 3——即从左到右。
EXAMPLE:
If displayed-month
是一月(即 1),目标month
是八月(即 8),那么row
equals 18 and column
equals 31.
If displayed-month
是二月(即 2)并且目标month
是八月(即 8),那么row
equals 18 and column
equals 6.
If displayed-month
是 3 月(即 3),目标month
是八月(即 8),那么row
equals 9 and column
equals 56.
If displayed-month
是四月(即 4)并且目标month
是八月(即 8),那么row
equals 9 and column
equals 31.
If displayed-month
是 5 月(即 5),目标month
是八月(即 8),那么row
equals 9 and column
equals 6.
当布局一次向前滚动一个月时,12 个月的日历如下所示:
;; 1 2 3
;; 4 5 6
;; 7 8 9
;; 10 11 12
;; 2 3 4
;; 5 6 7
;; 8 9 10
;; 11 12 1
;; 3 4 5
;; 6 7 8
;; 9 10 11
;; 12 1 2
;; 4 5 6
;; 7 8 9
;; 10 11 12
;; 1 2 3
;; 5 6 7
;; 8 9 10
;; 11 12 1
;; 2 3 4
;; 6 7 8
;; 9 10 11
;; 12 1 2
;; 3 4 5
;; 7 8 9
;; 10 11 12
;; 1 2 3
;; 4 5 6
;; 8 9 10
;; 11 12 1
;; 2 3 4
;; 5 6 7
;; 9 10 11
;; 12 1 2
;; 3 4 5
;; 6 7 8
;; 10 11 12
;; 1 2 3
;; 4 5 6
;; 7 8 9
;; 11 12 1
;; 2 3 4
;; 5 6 7
;; 8 9 10
;; 12 1 2
;; 3 4 5
;; 6 7 8
;; 9 10 11
@lawlist的长手解决方案如下:
(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))
(first-of-month-weekday (calendar-day-of-week (list month 1 year))))
(goto-line
(+ 3
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(cond
;; 1 2 3
;; 4 5 6
;; 7 8 9
;; 10 11 12
((and
(eq displayed-month 1)
(memq month `(1 2 3)))
0)
((and
(eq displayed-month 1)
(memq month `(4 5 6)))
9)
((and
(eq displayed-month 1)
(memq month `(7 8 9)))
18)
((and
(eq displayed-month 1)
(memq month `(10 11 12)))
27)
;; 2 3 4
;; 5 6 7
;; 8 9 10
;; 11 12 1
((and
(eq displayed-month 2)
(memq month `(2 3 4)))
0)
((and
(eq displayed-month 2)
(memq month `(5 6 7)))
9)
((and
(eq displayed-month 2)
(memq month `(8 9 10)))
18)
((and
(eq displayed-month 2)
(memq month `(11 12 1)))
27)
;; 3 4 5
;; 6 7 8
;; 9 10 11
;; 12 1 2
((and
(eq displayed-month 3)
(memq month `(3 4 5)))
0)
((and
(eq displayed-month 3)
(memq month `(6 7 8)))
9)
((and
(eq displayed-month 3)
(memq month `(9 10 11)))
18)
((and
(eq displayed-month 3)
(memq month `(12 1 2)))
27)
;; 4 5 6
;; 7 8 9
;; 10 11 12
;; 1 2 3
((and
(eq displayed-month 4)
(memq month `(4 5 6)))
0)
((and
(eq displayed-month 4)
(memq month `(7 8 9)))
9)
((and
(eq displayed-month 4)
(memq month `(10 11 12)))
18)
((and
(eq displayed-month 4)
(memq month `(1 2 3)))
27)
;; 5 6 7
;; 8 9 10
;; 11 12 1
;; 2 3 4
((and
(eq displayed-month 5)
(memq month `(5 6 7)))
0)
((and
(eq displayed-month 5)
(memq month `(8 9 10)))
9)
((and
(eq displayed-month 5)
(memq month `(11 12 1)))
18)
((and
(eq displayed-month 5)
(memq month `(2 3 4)))
27)
;; 6 7 8
;; 9 10 11
;; 12 1 2
;; 3 4 5
((and
(eq displayed-month 6)
(memq month `(6 7 8)))
0)
((and
(eq displayed-month 6)
(memq month `(9 10 11)))
9)
((and
(eq displayed-month 6)
(memq month `(12 1 2)))
18)
((and
(eq displayed-month 6)
(memq month `(3 4 5)))
27)
;; 7 8 9
;; 10 11 12
;; 1 2 3
;; 4 5 6
((and
(eq displayed-month 7)
(memq month `(7 8 9)))
0)
((and
(eq displayed-month 7)
(memq month `(10 11 12)))
9)
((and
(eq displayed-month 7)
(memq month `(1 2 3)))
18)
((and
(eq displayed-month 7)
(memq month `(4 5 6)))
27)
;; 8 9 10
;; 11 12 1
;; 2 3 4
;; 5 6 7
((and
(eq displayed-month 8)
(memq month `(8 9 10)))
0)
((and
(eq displayed-month 8)
(memq month `(11 12 1)))
9)
((and
(eq displayed-month 8)
(memq month `(2 3 4)))
18)
((and
(eq displayed-month 8)
(memq month `(5 6 7)))
27)
;; 9 10 11
;; 12 1 2
;; 3 4 5
;; 6 7 8
((and
(eq displayed-month 9)
(memq month `(9 10 11)))
0)
((and
(eq displayed-month 9)
(memq month `(12 1 2)))
9)
((and
(eq displayed-month 9)
(memq month `(3 4 5)))
18)
((and
(eq displayed-month 9)
(memq month `(6 7 8)))
27)
;; 10 11 12
;; 1 2 3
;; 4 5 6
;; 7 8 9
((and
(eq displayed-month 10)
(memq month `(10 11 12)))
0)
((and
(eq displayed-month 10)
(memq month `(1 2 3)))
9)
((and
(eq displayed-month 10)
(memq month `(4 5 6)))
18)
((and
(eq displayed-month 10)
(memq month `(7 8 9)))
27)
;; 11 12 1
;; 2 3 4
;; 5 6 7
;; 8 9 10
((and
(eq displayed-month 11)
(memq month `(11 12 1)))
0)
((and
(eq displayed-month 11)
(memq month `(2 3 4)))
9)
((and
(eq displayed-month 11)
(memq month `(5 6 7)))
18)
((and
(eq displayed-month 11)
(memq month `(8 9 10)))
27)
;; 12 1 2
;; 3 4 5
;; 6 7 8
;; 9 10 11
((and
(eq displayed-month 12)
(memq month `(12 1 2)))
0)
((and
(eq displayed-month 12)
(memq month `(3 4 5)))
9)
((and
(eq displayed-month 12)
(memq month `(6 7 8)))
18)
((and
(eq displayed-month 12)
(memq month `(9 10 11)))
27) )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(/ (+ day -1
(mod
(- (calendar-day-of-week (list month 1 year)) calendar-week-start-day)
7))
7)))
(move-to-column
(+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(cond
;; 1 2 3
;; 4 5 6
;; 7 8 9
;; 10 11 12
((and
(eq displayed-month 1)
(memq month `(1 4 7 10)))
6)
((and
(eq displayed-month 1)
(memq month `(2 5 8 11)))
31)
((and
(eq displayed-month 1)
(memq month `(3 6 9 12)))
56)
;; 2 3 4
;; 5 6 7
;; 8 9 10
;; 11 12 1
((and
(eq displayed-month 2)
(memq month `(2 5 8 11)))
6)
((and
(eq displayed-month 2)
(memq month `(3 6 9 12)))
31)
((and
(eq displayed-month 2)
(memq month `(4 7 10 1)))
56)
;; 3 4 5
;; 6 7 8
;; 9 10 11
;; 12 1 2
((and
(eq displayed-month 3)
(memq month `(3 6 9 12)))
6)
((and
(eq displayed-month 3)
(memq month `(4 7 10 1)))
31)
((and
(eq displayed-month 3)
(memq month `(5 8 11 2)))
56)
;; 4 5 6
;; 7 8 9
;; 10 11 12
;; 1 2 3
((and
(eq displayed-month 4)
(memq month `(4 7 10 1)))
6)
((and
(eq displayed-month 4)
(memq month `(5 8 11 2)))
31)
((and
(eq displayed-month 4)
(memq month `(6 9 12 3)))
56)
;; 5 6 7
;; 8 9 10
;; 11 12 1
;; 2 3 4
((and
(eq displayed-month 5)
(memq month `(5 8 11 2)))
6)
((and
(eq displayed-month 5)
(memq month `(6 9 12 3)))
31)
((and
(eq displayed-month 5)
(memq month `(7 10 1 4)))
56)
;; 6 7 8
;; 9 10 11
;; 12 1 2
;; 3 4 5
((and
(eq displayed-month 6)
(memq month `(6 9 12 3)))
6)
((and
(eq displayed-month 6)
(memq month `(7 10 1 4)))
31)
((and
(eq displayed-month 6)
(memq month `(8 11 2 5)))
56)
;; 7 8 9
;; 10 11 12
;; 1 2 3
;; 4 5 6
((and
(eq displayed-month 7)
(memq month `(7 10 1 4)))
6)
((and
(eq displayed-month 7)
(memq month `(8 11 2 5)))
31)
((and
(eq displayed-month 7)
(memq month `(9 12 3 6)))
56)
;; 8 9 10
;; 11 12 1
;; 2 3 4
;; 5 6 7
((and
(eq displayed-month 8)
(memq month `(8 11 2 5)))
6)
((and
(eq displayed-month 8)
(memq month `(9 12 3 6)))
31)
((and
(eq displayed-month 8)
(memq month `(10 1 4 7)))
56)
;; 9 10 11
;; 12 1 2
;; 3 4 5
;; 6 7 8
((and
(eq displayed-month 9)
(memq month `(9 12 3 6)))
6)
((and
(eq displayed-month 9)
(memq month `(10 1 4 7)))
31)
((and
(eq displayed-month 9)
(memq month `(11 2 5 8)))
56)
;; 10 11 12
;; 1 2 3
;; 4 5 6
;; 7 8 9
((and
(eq displayed-month 10)
(memq month `(10 1 4 7)))
6)
((and
(eq displayed-month 10)
(memq month `(11 2 5 8)))
31)
((and
(eq displayed-month 10)
(memq month `(12 3 6 9)))
56)
;; 11 12 1
;; 2 3 4
;; 5 6 7
;; 8 9 10
((and
(eq displayed-month 11)
(memq month `(11 2 5 8)))
6)
((and
(eq displayed-month 11)
(memq month `(12 3 6 9)))
31)
((and
(eq displayed-month 11)
(memq month `(1 4 7 10)))
56)
;; 12 1 2
;; 3 4 5
;; 6 7 8
;; 9 10 11
((and
(eq displayed-month 12)
(memq month `(12 3 6 9)))
6)
((and
(eq displayed-month 12)
(memq month `(1 4 7 10)))
31)
((and
(eq displayed-month 12)
(memq month `(2 5 8 11)))
56) )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(* 3 (mod
(- (calendar-day-of-week date) calendar-week-start-day)
7))))))