在 Emacs 中将光标移动到 12 个月旋转日历上的日期的算法

2023-11-23

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取决于该点是否打开row1、2、3 或 4——即从上到下。

第二个公式必须等于6, 31, or 56取决于该点是否打开column1、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))))))

我一定错过了一些东西,因为看起来公式就像(伪代码)一样简单:

first  = 9 * ( rows - 1 ) 
second = 6 + 25 * ( cols - 1 )

根据您的编辑,您可以计算要移动的行和列:

if target > display
   difference = target - display
else 
   difference = 12 + target - display
 rows = difference / 3 
 cols = difference % 3
 rowmove = 9 * rows 
 colmove = 6 + 25 * cols

然后使用上面的公式。

我对 elisp 的尝试:

(let difference (if (>= target-month display-month) 
                      (- target-month display-month) 
                    (- (+ target-month 12) display-month)))
(let rows (/ difference 3))
(let cols (% difference 3))
(let rowmove (* 9 rows))   
(let colmove (+ 6 (* 25 cols)))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Emacs 中将光标移动到 12 个月旋转日历上的日期的算法 的相关文章

随机推荐

  • AppDelegate for Cocoa 应用程序在 Xcode 6 中使用 Storyboards

    我有一个现有的 OS X 应用程序 在转换为 Storyboards 作为主界面后 我的应用程序委托不再被使用 之前 MainMenu xib 有一个 App Delegate 对象 我可以将其类设置为我的应用程序委托 然而 故事板不包含这
  • Chrome Service Worker iOS 支持

    随着 Apple 几个月前宣布 Service Worker 支持 iOS 11 3 我最近开始尝试让 Service Worker 在 iOS 上工作 在 Safari 上 它按预期工作 访问网站后 它可以离线工作 然而 当我尝试在 Ch
  • “6k 浏览次数”是什么意思以及如何在 PHP 中格式化该数字

    6k 浏览次数 是什么意思以及如何在 PHP 中格式化这个数字 k是缩写基洛前缀并表示千 所以6k就是六千 您可以使用以下除法函数来格式化数字 function format number prefixes kMGTPEZY if numb
  • SQL Server MERGE + 连接其他表

    我在数据库项目中使用 MERGE 语句从静态值集中填充参考数据 如下所示 MERGE INTO dbo User AS TARGET USING VALUES email protected My Name AS SOURCE UserNa
  • 这是在 C++ 中实现有界缓冲区的正确方法吗?

    Closed 这个问题是无关 目前不接受答案 我正在开发一个程序 该程序处理多个线程访问 存入和从有界缓冲区容器中取出的问题 我注意到线程的一些主要问题 并怀疑我的缓冲区在某处部分或根本不正确 为了确保我知道我在做什么 我希望检查一下我的缓
  • 通过 HTML/PHP 使用 htaccess 自定义登录

    我正在开发一个网站 该网站的目录受 htaccess 保护 我想创建一个自定义登录页面 而不是依赖浏览器默认页面 有人对此有经验吗 我想通过 HTML 表单进行连接 有人觉得可能吗 Thanks 是的 这是可能的 但您不应该使用 htacc
  • 在视图中显示当地时间

    在 config application rb 中 我的文件中有 config time zone UTC 不带引号 我假设这是为了将输入到视图中的用户时间转换为存储在数据库中的 UTC 我的问题是 如何将数据库中的 UTC 值转换为用户的
  • 创建 iOS/OSX 框架:在分发给其他开发人员之前是否有必要对其进行协同设计?

    我正在学习如何创建 iOS 和 OSX 框架 让我们以 iOS 为例 到目前为止 以下步骤对我有效 使用 sdk iphonesimulator 和 Build 操作的 xcodebuild 框架 使用 sdk iphoneos 和 Bui
  • 使用 iOS hooks 将照片发布到 Instagram

    我在 iOS 应用程序中使用以下代码来使用 Instagram iPhone 挂钩将照片发布到 Instagram 我只希望 打开方式 菜单有 Instagram 应用程序 没有其他应用程序 但就我而言 Camera 也出现了 如何限制 I
  • 在 slime REPL 中使用 clojure.contrib 函数

    我想在 REPL 的 slime 中使用 clojure contrib trace 命名空间中的函数 我怎样才能让史莱姆自动加载它们 一个相关的问题 如何将特定的命名空间添加到正在运行的 repl 中 在 clojure contrib
  • 尝试使用 ImageMagick 将 200 个 jpg 文件转换为 mp4。一遍又一遍地收到相同的错误

    我正在尝试将 200 个 ppm 文件中的 jpg 文件转换为一个 mp4 文件 在与 jpg 文件相同的目录中 我运行了以下代码 convert delay 6 quality 95 test ppm movie mp4 并收到此错误消息
  • 将参数列表传递给 shell 中的命令

    如果我有一个文件列表说file1 file20 如何运行以文件列表作为参数的命令 例如myccommand file1 file2 file20 如果你的列表在你的参数向量中 也就是说 如果你从以下内容开始 yourscript file1
  • 如何应用 ndimage.generic_filter()

    我正在尝试学习 ndimage 但不知道如何通用过滤器 功能有效 文档提到用户函数将应用于用户定义的足迹 但不知何故我无法做到 这是示例 gt gt gt import numpy as np gt gt gt from scipy imp
  • 如何从视图控制器以编程方式绘制一条线?

    我有一个UIViewController 如何在以编程方式创建的视图之一中画一条线 有两种常见的技术 Using CAShapeLayer 创建一个UIBezierPath 将坐标替换为您想要的任何坐标 UIBezierPath path
  • 在 WCF 测试客户端 (WcfTestClient.exe) 中按字母顺序列出服务操作

    我经常使用WCF测试客户端 不幸的是 该工具不按字母顺序排序 也不按 出现顺序 在服务合同类中声明操作的顺序 排序 如果有很多操作 那么很难找到具体的操作 有没有办法强制 WCF 测试客户端按字母顺序对操作进行排序 默认排序从何而来 尽管似
  • 使用自适应语法[关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 目前不接受答案 我正在尝试实现一种语法可以动态更改的语言 或语言族 我没有找到可以作为研究案例的例子 您能给我一些在现实世界 甚至是学术界 中实际使用的参考吗 使用
  • 如何增加 freebase API 结果限制

    我在 freebase 的文档中找不到任何相关内容 但他们似乎将结果数量限制为 100 例如 type base popstra celebrity name null 仅返回 100 个结果 而 type base popstra cel
  • 如何可视化堆转储?

    我们使用 golang 开发了一个服务器 它将接收并发请求并处理请求 创建大对象 树 然后发回回复 但这些对象不会被垃圾回收 所以我决定分析内存中的对象 首先 我编写了一个简单的程序 package main import fmt io i
  • 64 位领域中的堆碎片

    过去 当我研究长期运行的 C 守护进程时 我必须处理堆碎片问题 为了避免耗尽连续的堆空间 需要保留大量分配池之类的技巧 对于 64 位地址空间来说这仍然是一个问题吗 性能对我来说不是一个问题 所以我更愿意简化我的代码 不再处理缓冲池之类的事
  • 在 Emacs 中将光标移动到 12 个月旋转日历上的日期的算法

    GOAL 此线程的目标是创建两 2 个数学公式 以替换函数中 lawlist 的长手解lawlist calendar cursor to visible date 以下 故事问题 Emacs 中现在存在一个 12 个月的日历 每次向前和向