简体   繁体   English

emacs组织模式文件本地键绑定

[英]emacs org-mode file local key binding

I'm using org-mode in emacs together with ox-reveal. 我正在emacs中使用org-mode和ox-reveal。 The latter defines the command org-reveal-export-to-html, which I would like to bind to a key for buffers with org files, which are presentations (so not for all org files). 后者定义了org-reveal-export-to-html命令,我想将其绑定到具有org文件的缓冲区的键,该文件是演示文稿(并非针对所有org文件)。

So the question is: how can I define file local key bindings in org-mode? 所以问题是:如何在组织模式下定义文件本地键绑定?

What I currently have is this: 我目前所拥有的是:

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

But imho this is not very elegant. 但是恕我直言,这不是很优雅。

You can define a key only for org-mode by using org-defkey, basically add the following to your init file 您可以使用org-defkey定义仅用于org-mode的密钥,基本上将以下内容添加到您的init文件中

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

UPDATE UPDATE

You can use file local variables. 您可以使用文件局部变量。

(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)

The function export-with-reveal-or-html if the variable export-with-reveal has value t or there is a file 'reveal.js' relative to org file , if so it exports with reveal or it falls back to default html export. 函数export-with-reveal-or-html ,如果变量export-with-reveal具有值T或有一个文件“reveal.js”相对于ORG文件,如果因此它与出口reveal或将其退回到默认的HTML出口。 You can specify a file to exported as reveal by adding the following to top of your org file 您可以通过将以下内容添加到组织文件的顶部来指定要导出为显示的文件

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

UPDATE 2 更新2

You can also define arbitrary export function by doing using, file-local variables 您还可以通过使用文件本地变量来定义任意导出功能

(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)

Then at top of the file you can set the export function you want to use eg 然后,在文件顶部,您可以设置要使用的导出功能,例如

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

I came up with following solution, which exploits local variables hook hack and defines buffer lokal hook: 我想出了以下解决方案,该解决方案利用局部变量钩子hack并定义了缓冲区lokal钩子:

(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)))))

Then in org mode I add this: 然后在组织模式下,我添加:

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

Still I would love to see something like this, without any hook hacking: 我仍然很乐意看到类似这样的内容,而没有任何钩子黑客:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM