emacs - 如何向 `format-time-string` 添加格式说明符?

标签 emacs elisp format-string

我有以下函数定义:

(defun nth (n)
  (format
   (concat
    "%d"
    (if (memq n '(11 12 13)) "th"
      (let ((last-digit (% n 10)))
        (case last-digit
          (1 "st")
          (2 "nd")
          (3 "rd")
          (otherwise "th"))))) n))

我希望能够在 format-time-string 中使用它. 通常,我会查看函数的源代码,但这个函数是在 C 源代码中定义的。 (我认为这排除了将某些东西卡在上面的可能性,但我有待纠正。)

我如何添加另一个格式说明符(例如,%o)以将 nth 应用于适当的参数?

期望的用法:

(format-time-string "%A, %B %o, %T (%z)" (seconds-to-time 1250553600))

=> "Monday, August 17th, 20:00:00 (-0400)"

最佳答案

这是你想要做的。 Stefan 和 Drew 已经给出了一些重要的评论(不要覆盖 nth 并查看 emacs-lisp/advising 函数的信息文件)。

(defun ordinal (n)
  "Special day of month format."
  (format
   (concat
    "%d"
    (if (memq n '(11 12 13)) "th"
      (let ((last-digit (% n 10)))
        (case last-digit
          (1 "st")
          (2 "nd")
          (3 "rd")
          (otherwise "th"))))) n))


(defadvice format-time-string (before ordinal activate)
  "Add ordinal to %d."
  (let ((day (nth 3 (decode-time (or time (current-time))))))
    (setq format-string
      (replace-regexp-in-string "%o"
                    (ordinal day)
                    format-string))))

注意事项:

  1. 我没有处理 UNIVERSAL 参数

  2. 当从 C 调用 format-time-string 时,hack 不起作用(您可以在手册中阅读)。

关于emacs - 如何向 `format-time-string` 添加格式说明符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20316754/

相关文章:

emacs - 如何加速 Emacs DocView 模式?

emacs - 安装 cider-nrepl

emacs - 如何缩小 .emacs 配置文件?

Emacs 和存储库之间的跳转

Emacs——修改 `fill-column-indicator.el` 以在空白处显示

c# - 反转格式字符串并确定正确结果

c - 如何在格式字符串攻击中将值写入地址

javascript - 如何更改 js3-mode 的缩进样式?

matlab - 在 Emacs 中跟踪缓冲区更改并在事件发生后触发 Hook