2

I'm using org-mode in emacs together with 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).

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.

theldoria
  • 378
  • 9
  • 22
  • Duplicate of [File-specific key-binding in emacs](http://stackoverflow.com/a/21493693/324105) ? – phils Feb 14 '14 at 12:01

2 Answers2

3

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-map [f5] 'org-reveal-export-to-html)

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. 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

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 -*-
  • Yes, but I do not want this binding for all org files, only for some specific ones (in my case those which I whant to export to html using revealJS). – theldoria Feb 14 '14 at 08:53
  • If I could define a buffer local variable in org-mode then I could write a function handling the export I desire for the buffer. But I am very new to org-mode, so, is there such a posibility (despite the Local Variables mentioned above)? – theldoria Feb 14 '14 at 08:59
  • OK this can be done through org-mode-hook, what do you want to do for other org-mode files when you hit `f5`? –  Feb 14 '14 at 09:01
  • E.g. export to html, but without reveal, so (export-html-export-to-html). – theldoria Feb 14 '14 at 09:29
  • OK this can be accomplished, is there any criteria that can be used to determine if the current buffer needs to be exported using 'reveal'? Something like files in a some particular folder or file with particular name? –  Feb 14 '14 at 09:33
  • There is an subfolder named reveal.js, relative to my document. But that must not always be the case. It could also be stored in a different folder and configured either by (setq org-reveal-root "file:///d:/reveal.js") or in org file with #+REVEAL_ROOT: file:///d:/reveal.js. So I would prefer to have some document property (i.e. a variable), set to export function symbol as override to default export function. – theldoria Feb 14 '14 at 09:44
0

I came up with following solution, which exploits local variables hook hack and defines buffer 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)))))

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
theldoria
  • 378
  • 9
  • 22
  • I have updated my answer, it allows you to specify the function to use for exporting a file –  Feb 14 '14 at 10:22
  • Thanks, your answer is what I was searching for. – theldoria Feb 14 '14 at 10:29
  • I am glad it helped you, just a note make sure the variable name is unique, otherwise it may lead conflicts. I forgot to take care of it sorry will update the answer –  Feb 14 '14 at 10:30