【问题标题】:How to create Emacs key bindings for interactive org-mode functions / org-mode commands with arguments?如何为带有参数的交互式 org-mode 函数/org-mode 命令创建 Emacs 键绑定?
【发布时间】:2020-01-10 13:31:36
【问题描述】:

使用函数org-deadline,可以在 Emacs org-mode 中定义任务的截止日期。描述如下:

(org-deadline ARG &optional TIME)

Insert the "DEADLINE:" string with a timestamp to make a deadline.
With one universal prefix argument, remove any deadline from the item.
With two universal prefix arguments, prompt for a warning delay.
With argument TIME, set the deadline at the corresponding date.  TIME
can either be an Org date like "2011-07-24" or a delta like "+2d".

我想创建一个键绑定来直接将到期日期设置为未来 1 周。所以键绑定应该调用org-deadline 函数并将+1w 作为参数。编写 (org-deadline nil "+1w") 然后在该区域上执行 eval 可以正常工作。

但是为什么我不能把它绑定到一个键上呢?我尝试了以下两个选项:

  1. (defun org-deadline-in-one-week ()
      (interactive)
      (org-deadline nil "+1w"))
    (global-set-key (kbd "C-c C-s") 'org-deadline-in-one-week)
    
  2. (global-set-key (kbd "C-c C-s") (lambda () (interactive) (org-deadline nil "+1w")))
    

两种方式都失败了,因为用于选择日期的交互式菜单仍然显示。它提示我用光标键选择一个日期,然后用RET 确认。但我想以非交互方式使用交互功能,只需将截止日期设置为未来一周。我错过了什么?

更新:我正在使用Org mode version 9.1.9 (release_9.1.9-65-g5e4542 @ /usr/share/emacs/26.1/lisp/org/)。

【问题讨论】:

  • 您使用的是哪个版本的组织模式? (试试M-x org-version)
  • 我有理由确定这是 org 模式的问题,而不是如何处理可能希望在一般情况下以交互方式调用的调用函数:您所做的看起来是正确的。
  • 我用组织模式版本更新了问题。

标签: emacs command org-mode key-bindings


【解决方案1】:

您遇到了键盘映射问题:C-c C-s 绑定在 org-mode-map(到 org-schedule)中。这是 Org 模式的主要模式键映射,它覆盖 Org 模式缓冲区中的全局映射。请参阅Active keymaps in the Emacs Lisp manual - 事实上,阅读(和重读)整章是个好主意。

你应该做两件事:在org-mode-map中定义键,而不是在全局映射中;并确保尚未定义密钥(或者至少您不介意丢失其当前设置)。例如 C-c s 在 org-mode-map 中未定义(默认情况下),所以我会这样做

(define-key org-mode-map (kbd "C-c s") 'org-deadline-in-one-week)

【讨论】:

  • 感谢您的解决方案以及参考关键地图!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-10
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多