# How to programmatically export org buffer to html ( mimic the org mode sequence C-c C-e h o )?

In brief:

I would like to programmatically mimic the org mode sequence C-c C-e h o to export a buffer to HTML and open the web browser.

My scenario:

• I have a written a shell-command (in my code snippet below, this command is remplaced by the simple "more" command) that processes the current buffer file (the (buffer-file-name) part).
• the shell command (here "more") generates an org mode compatible output and stores it in new_buffer.
• my problem: I want to export this new_buffer into an html file and open the browser (like the manual C-c C-e h o sequence).
• then I kill the temporary new_buffer

-> I do not know how to do that. For the moment I use org-html-export-as-html, however this function simply exports the created new_buffer into a new *Org HTML Export* buffer.

What I have done so far (with my brittle emacs-lisp knowledge):

 (defun generate_html()
(interactive)
(let ((new_buffer (generate-new-buffer (concat (buffer-file-name) ".org"))))
(shell-command (concat "more " (buffer-file-name)) new_buffer)
(org-html-export-as-html ) ;; <-- what to do here to mimic C-c C-e h o ?
(kill-buffer new_buffer)))


Looks like you'll need to define your own command to do it. If you look in ox-html.el and search for ":menu-entry" (on my machine [Org 9.0.9], it's on line 105), you'll see that C-c C-e h H and C-c C-e h h point to commands in ox-html.el, but C-c C-e h o is implemented via a lambda function that you'll have to reproduce. No idea why it was done differently than the other options.

• Thanks for this information. At worst, maybe I can save the exported html into a file and open it with another shell-command chromium file.hmtl – Picaud Vincent Jan 24 at 20:10
• The anonymous function just handles async export. Since the OP is exporting synchronously, only the else part is needed: (org-open-file (org-html-export-to-html)) -- all the optional arguments are nil. – NickD Jan 25 at 2:47
• @NickD thanks for the comment, I will try that today – Picaud Vincent Jan 25 at 7:53

I came to this solution, that do what I wanted:

• calls a shell command I have written in C++ code_to_org that transforms some source code into an org mode compatible document
• exports it to a html file and starts the web-browser to visualize it.

.

(defun my-code-to-html()
(interactive)
(save-buffer)
(setq current_buffer (current-buffer))
(setq current_filename (buffer-file-name))
(with-temp-buffer
(shell-command (concat "code_to_org " current_filename) (current-buffer))
(org-open-file (org-export-to-file 'html (concat current_filename ".html")))
)
)

• Good! --- btw, you might want to look into learning how to use the let form in elisp. setq creates global variables, which can eventually catch up with you. let (and its sister let*) create local variables that disappear when it's done. Cheers! – Sue D. Nymme Jan 25 at 15:08
• Thank you for your clarification @SueD.Nymme. That is one of my regret, not having learnt (emacs) lisp. I must do that. – Picaud Vincent Jan 25 at 15:19