计算目标月份的目标年度的算法 - 12个月*日历*

时间:2014-02-17 16:51:36

标签: emacs elisp

目标:此主题的目标是创建一个数学公式,以便在函数lawlist-target-year-function(下面)中用@lawlist替换长手解决方案。

注意:此线程的解决方案有点类似,但与@AShelly在相关主题中编写的算法有所不同:https://stackoverflow.com/a/21709710/2112489

故事问题

现在,Emacs中存在一个12个月的日历,一次向前和向后滚动一个月(或更多)。 sevaral假日函数使用一个名为lawlist-target-year-function的辅助函数在每个假日上放置一个叠加层。

可以在此处找到12个月滚动日历包括长手解决方案)的完整工作草案 - [Github源代码已修订]包括@legoscia的简明算法解决方案:

https://github.com/lawlist/calendar-yearly-scroll-by-month/blob/master/lawlist-cal.el

LEGEND

displayed-month(数字1到12)是显示在缓冲区左上角的月份,随着12个月日历向前或向后滚动,这个月会发生变化。

target-month(数字1到12)是包含将使用叠加层标记的假日的月份。有三(3)个可能的x轴坐标(即第1列,第2列或第3列)。有四(4)个可能的y轴坐标(即第1行,第2行,第3行或第4行)。 [引用x / y坐标:http://www.mathsisfun.com/data/cartesian-coordinates.html]

displayed-year是显示在缓冲区左上角的年份,随着12个月日历向前或向后滚动,这一年会发生变化。

目标yeartarget-month的年份。

示例

  • displayed-month为1月(即1)时,所有目标月份的年份相同。

  • displayed-month是2月(即2)时:

    (if (memq target-month `(2 3 4 5 6 7 8 9 10 11 12))
      displayed-year
      (+ displayed-year 1))
    
  • displayed-month是3月(即3)时:

    (if (memq target-month `(3 4 5 6 7 8 9 10 11 12))
      displayed-year
      (+ displayed-year 1))
    
  • displayed-month是4月(即4)时:

    (if (memq target-month `(4 5 6 7 8 9 10 11 12))
      displayed-year
      (+ displayed-year 1))
    
  • displayed-month为5月(即5)时

    (if (memq target-month `(5 6 7 8 9 10 11 12))
      displayed-year
      (+ displayed-year 1))
    
  • displayed-month是6月(即6)时:

    (if (memq target-month `(6 7 8 9 10 11 12))
      displayed-year
      (+ displayed-year 1))
    
  • displayed-month是7月(即7)时:

    (if (memq target-month `(7 8 9 10 11 12))
      displayed-year
      (+ displayed-year 1))
    
  • displayed-month是八月(即8)时:

    (if (memq target-month `(8 9 10 11 12))
      displayed-year
      (+ displayed-year 1))
    
  • displayed-month是九月(即9)时:

    (if (memq target-month `(9 10 11 12))
      displayed-year
      (+ displayed-year 1))
    
  • displayed-month是十月(即10)时:

    (if (memq target-month `(10 11 12))
      displayed-year
      (+ displayed-year 1))
    
  • displayed-month是11月(即11)时:

    (if (memq target-month `(11 12))
      displayed-year
      (+ displayed-year 1))
    
  • displayed-month是12月(即12)时:

    (if (memq target-month `(12))
      displayed-year
      (+ displayed-year 1))
    

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-target-year-function (target-month)
  (cond
    ;;  1 2 3
    ;;  4 5 6
    ;;  7 8 9
    ;;  10 11 12
    ((eq displayed-month 1)
      displayed-year)
    ;;  2 3 4
    ;;  5 6 7
    ;;  8 9 10
    ;;  11 12 1
    ((eq displayed-month 2)
      (if (memq target-month `(2 3 4 5 6 7 8 9 10 11 12))
        displayed-year
        (+ displayed-year 1)))
    ;;  3 4 5
    ;;  6 7 8
    ;;  9 10 11
    ;;  12 1 2
    ((eq displayed-month 3)
      (if (memq target-month `(3 4 5 6 7 8 9 10 11 12))
        displayed-year
        (+ displayed-year 1)))
    ;;  4 5 6
    ;;  7 8 9
    ;;  10 11 12
    ;;  1 2 3
    ((eq displayed-month 4)
      (if (memq target-month `(4 5 6 7 8 9 10 11 12))
        displayed-year
        (+ displayed-year 1)))
    ;;  5 6 7
    ;;  8 9 10
    ;;  11 12 1
    ;;  2 3 4
    ((eq displayed-month 5)
      (if (memq target-month `(5 6 7 8 9 10 11 12))
        displayed-year
        (+ displayed-year 1)))
    ;;  6 7 8
    ;;  9 10 11
    ;;  12 1 2
    ;;  3 4 5
    ((eq displayed-month 6)
      (if (memq target-month `(6 7 8 9 10 11 12))
        displayed-year
        (+ displayed-year 1)))
    ;;  7 8 9
    ;;  10 11 12
    ;;  1 2 3
    ;;  4 5 6
    ((eq displayed-month 7)
      (if (memq target-month `(7 8 9 10 11 12))
        displayed-year
        (+ displayed-year 1)))
    ;;  8 9 10
    ;;  11 12 1
    ;;  2 3 4
    ;;  5 6 7
    ((eq displayed-month 8)
      (if (memq target-month `(8 9 10 11 12))
        displayed-year
        (+ displayed-year 1)))
    ;;  9 10 11
    ;;  12 1 2
    ;;  3 4 5
    ;;  6 7 8
    ((eq displayed-month 9)
      (if (memq target-month `(9 10 11 12))
        displayed-year
        (+ displayed-year 1)))
    ;;  10 11 12
    ;;  1 2 3
    ;;  4 5 6
    ;;  7 8 9
    ((eq displayed-month 10)
      (if (memq target-month `(10 11 12))
        displayed-year
        (+ displayed-year 1)))
    ;;  11 12 1
    ;;  2 3 4
    ;;  5 6 7
    ;;  8 9 10
    ((eq displayed-month 11)
      (if (memq target-month `(11 12))
        displayed-year
        (+ displayed-year 1)))
    ;;  12 1 2
    ;;  3 4 5
    ;;  6 7 8
    ;;  9 10 11
    ((eq displayed-month 12)
      (if (memq target-month `(12))
        displayed-year
        (+ displayed-year 1))) ))

1 个答案:

答案 0 :(得分:2)

这会有用吗?

(defun lawlist-target-year-function (target-month)
  (if (>= target-month displayed-month)
      displayed-year
    (1+ displayed-year)))