【问题标题】:emacs org-mode file local key bindingemacs org-mode 文件本地键绑定
【发布时间】:2014-02-14 07:54:37
【问题描述】:

我在 emacs 中使用 org-mode 和 ox-reveal。后者定义了命令 org-reveal-export-to-html,我想将它绑定到带有 org 文件的缓冲区的键,这些文件是演示文稿(因此不适用于所有 org 文件)。

所以问题是:如何在 org-mode 中定义文件本地键绑定?

我目前拥有的是这样的:

#+BEGIN_COMMENT
Local Variables:
eval: (local-set-key [f5] 'org-reveal-export-to-html)
End:
#+END_COMMENT

但是恕我直言,这不是很优雅。

【问题讨论】:

标签: emacs org-mode


【解决方案1】:

您可以使用 org-defkey 只为 org-mode 定义一个键,基本上将以下内容添加到您的 init 文件中

(org-defkey org-mode-map [f5] 'org-reveal-export-to-html)

更新

您可以使用文件局部变量。

(defvar export-with-reveal nil)

(defun export-with-reveal-or-html ()
  (interactive)
  (if (or export-with-reveal (file-exists-p "reveal.js"))
      (call-interactively 'org-reveal-export-to-html)
    (call-interactively 'org-export-as-html)))

(org-defkey org-mode-map [f5] 'export-with-reveal-or-html)

函数 export-with-reveal-or-html 如果变量 export-with-reveal 的值为 t 或存在相对于 org 文件的文件 'reveal.js',则使用 reveal 导出或回退到默认的 html 导出。您可以通过将以下内容添加到您的组织文件的顶部来指定要导出为显示的文件

# -*- export-with-reveal: t -*-

更新 2

您还可以通过使用文件局部变量来定义任意导出函数

(defvar my-export-fn nil)

(defun my-export ()
  (interactive)
  (if my-export-fn
      (call-interactively my-export-fn)
    (call-interactively 'org-export-as-html)))

(org-defkey org-mode-map [f5] 'my-export)

然后在文件的顶部你可以设置你想使用的导出功能例如

# -*- export-fn: org-reveal-export-to-html -*-

【讨论】:

  • 是的,但我不希望对所有 org 文件都使用此绑定,仅对某些特定文件(在我的情况下,我想使用revealJS 导出到 html 的那些)进行绑定。
  • 如果我可以在 org-mode 中定义一个缓冲区局部变量,那么我可以编写一个函数来处理我想要的缓冲区导出。但是我对 org-mode 很陌生,那么,有没有这样的可能性(尽管上面提到了局部变量)?
  • 好的,这可以通过org-mode-hook来完成,当你点击f5时,你想对其他org-mode文件做什么?
  • 例如导出到 html,但不显示,所以 (export-html-export-to-html)。
  • OK 这可以完成,是否有任何标准可用于确定当前缓冲区是否需要使用“显示”导出?类似于某个特定文件夹中的文件或具有特定名称的文件?
【解决方案2】:

我想出了以下解决方案,它利用局部变量 hook hack 并定义缓冲区 lokal hook:

(add-hook 'org-mode-hook 'my-org-mode-hook)
(defun my-org-mode-hook ()
  (add-hook 'hack-local-variables-hook
            (lambda ()
              (local-set-key [f5] (if (boundp 'my-org-export)
                                      my-org-export
                                    'org-html-export-to-html)))))

然后在 org 模式下添加:

#+BEGIN_COMMENT
Local Variables:
my-org-export: org-reveal-export-to-html
End:
#+END_COMMENT

我仍然希望看到这样的东西,没有任何钩子黑客:

#+EXPORT: org-reveal-export-to-html

【讨论】:

  • 我已经更新了我的答案,它允许您指定用于导出文件的函数
  • 谢谢,你的答案就是我要找的。​​span>
  • 很高兴它对您有所帮助,请注意确保变量名是唯一的,否则可能会导致冲突。我忘了照顾它抱歉会更新答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多