【问题标题】:How do I hard-code arguments in emacs lisp?如何在 emacs lisp 中对参数进行硬编码?
【发布时间】:2014-08-06 16:53:23
【问题描述】:

我的.emacs 中有以下功能,它会在我工作了合适的时间后通知我。

问题是,我无法硬编码时间和味精的值,所以我每次都必须重新输入。

(defun timed-notification(time msg)
  (interactive "sNotification when (e.g: 2 minutes, 60 seconds, 3 days): \nsMessage: ")
  (run-at-time time
               nil
               (lambda (msg) (terminal-notifier-notify "Pomodoro" msg))
               msg))
(setq column-number-mode t)

如何将时间设置为始终为“25 分钟”而消息设置为“休息一下,时间到了!”?

这是我的尝试:

(defun timed-notification()
  ;(interactive "sNotification when (e.g: 2 minutes, 60 seconds, 3 days): \nsMessage: ")
  (run-at-time 25
               nil
               (lambda ("Time's up")
                 (terminal-notifier-notify "Take a break, time's up!" msg))
               msg))
(setq column-number-mode t)

【问题讨论】:

标签: emacs lisp elisp


【解决方案1】:

像最初一样定义你的函数,然后用你想要的参数调用它一次。 interactive 表单,就像它的名字所暗示的那样,仅在您实际以交互方式调用该函数时使用。当你从代码中调用它时,你传递参数;所以interactive 表单会被忽略。

(defun timed-notification (time msg)
  (interactive "sNotification when (e.g: 2 minutes, 60 seconds, 3 days): \nsMessage: ")
  (run-at-time time nil (lambda (msg) (terminal-notifier-notify "Pomodoro" msg)) msg))
(setq column-number-mode t)
(timed-notification 25 "Take a break, time's up!")  ;; New addition

【讨论】:

  • 谢谢。也许我可以创建一个新的(交互式)函数来调用(timed-notifications 25“times up!”)并将其称为其他东西。这样我就不必破坏原来的功能了。
【解决方案2】:

您摆脱了msg 参数,但您仍在尝试使用它。使用let 将局部变量绑定到该值。

(defun timed-notification()
  (interactive)
  (let ((msg "Take a break, time's up!"))
    (run-at-time 25 nil (lambda (mess) (terminal-notifier-notify "pomodoro" mess)) msg)))

【讨论】:

  • 谢谢,我确定这可行,但现在我无法使用 M-x 启动定时通知。添加一个像 (interactive "sadfasdf") 这样的虚假行可以再次使用 M-x 找到定时通知,但它不会运行 ("Wrong number of arguments: (lambda nil (interactive "sadfa..."")。在这里完成 newb,所以解决方案可能很简单。
  • 如果函数没有参数,它应该只是(interactive)
  • interactive 的参数字符串中的每一行都是参数的提示。
  • 谢谢,我自己也意识到了。
  • 当我复制他的代码时,我什至没有注意到这一点。我已经修好了。我为 lambda 中的变量和函数中的变量使用了不同的变量名称,以明确区分。
【解决方案3】:
(defun timed-notification (time msg)
  (interactive "sNotification when (e.g: 2 minutes, 60 seconds, 3 days): \nsMessage: ")
  (run-at-time time nil (lambda (msg) (terminal-notifier-notify "Pomodoro" msg)) msg))
(setq column-number-mode t)

(defun tf()
  (interactive)
  (timed-notification "1 min" "Take a break, time's up!"))

现在可以为常规 pomo 调用 tf,而当我想要 x 分钟休息时,仍然可以使用原始函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多