How to make emacs org export work?


I use emacs on Nixos 20.03. When I try to export org file to odt emacs shows message
OpenDocument export failed: Buffer is read-only: #

org-version tells me
Org mode version 9.1.9 (release_9.1.9-65-g5e4542 @ /nix/store/5wkx7kjmgi0s5vszxvkafmdp4d42bq53-emacs-26.3/share/emacs/26.3/lisp/org/)

Emacs exports org to html but does not open it with external browser.

How can I make org export work and open it with external apps?

So there are a few different issues here, right?

  1. unable to export to docx
  2. unable to open the exported document in an external application

I’m using org export extensively and while no longer using the versions you mention, I definitely was at some point.

So for item 1, are you using the pandoc export to generate the docx? If not, what are you using?
For item 2, EmacsWiki: Open With

There are two issues, right.

  1. I use org-mode’s own publishing exporters which are accessible via C-c C-e
  2. OpenWith emacs package and customized file associations didn’t help unfortunately.
  1. I use org-mode’s own publishing exporters which are accessible via C-c

Sure, and then what? p for the pandoc exporter to create the docx or another one?

  1. OpenWith emacs package and customized file associations didn’t help

What exactly didn’t work? Can you use openwith to open another file externally from dired (as an example)?

I was trying to make export from org not to docx but to odt via default org exporter C-c C-e o o (“Export to ODT” “As ODT file”).
After hours of research and trials I finally found solution or rather workaround for the first part of my problem.
I checked possibility that my emacs configuration was interfering and I tried vanilla emacs, but my preferred method of export still didn’t work. Then I tried to upgrade org-mode 9.1.9 from elpa repository to org-mode vers. 9.3.6 according to this link. Then emacs gave me more comprehensible error message:

zip error: Zip file invalid, could not spawn unzip, or wrong unzip (original files unmodified)

I had to install unzip package (zip was already installed). And then it worked.

So, in order to make org export from org to odt (C-c C-e o o) in vanilla emacs 26.3 on Nixos 20.03 you have to:

  1. install org vers. 9.3.6 from elpa repository (M-x package-list-packages select org select install)
  2. install zip and unzip packages.

So, two tasks remain:

  1. make exported files open with system default external app.
  2. make it all work with my current emacs configuration.

You’re still going to have to provide some additional details about what exactly doesn’t work before it’s possible to help you out.

To make some kind of closure of my learning process I will describe workaround which appeared to be ridiculously complex but at least I learned a thing or two.
My questions were how to make these features work on vanilla emacs 26.3 installation on NixOS 20.03

  1. Export org buffer to ODT (C-c C-e o o) (Was failing completely)
  2. Export org buffer to html file and open (C-c C-e h o) (Was exporting to html but was opening internally as html code in emacs buffer)
  3. Export org buffer to ODT and open (C-c C-e o O) (Was failing completely at first and after #1 was fixed was opening odt file internally as zip archive in emacs buffer but not in external app)


  1. Upgrading org package to 9.3.6. Since I use straight.el as emacs package manager I just added

    (use-package org
    :straight t)

and M-x ev-b
Upgrade broke my org-drill but that’s another story.

  1. Editing org-file-apps variable to open html files with external app

M-x customize-variable RET org-file-apps

INS DEL Cons-cell:
Choice: Value Menu Extension: \.x?html?\’
Choice: Value Menu Command: firefox %s

  1. That was the toughest one

Installing package openwith.el kind of worked, but resulted in unexpected behaviour of export to ODT (C-c C-e o o). Instead of just exporting it exported AND opened file in external app. It really hurted my OCD so I decided to make org export work as it should. My workaround was:
a) repeat step 2) for .odt files:

INS DEL Cons-cell:
Choice: Value Menu Extension: \.odt\’
Choice: Value Menu Command: soffice %s

I was expecting to find solution on this step but C-c C-e o O was still opening odt files as zip archives in emacs buffer. It seemmed quite strange because it worked for html files but not for odt. Playing with some elisp from here in scratch buffer I found that C-c C-e o O started to work properly but I didn’t manage to make this code work outside scratch buffer. So I decided to play with code directly in ox-odt.el

b) Edit ox-odt.el

Find this block

'(?o "Export to ODT"
     ((?o "As ODT file" org-odt-export-to-odt)
      (?O "As ODT file and open"
	  (lambda (a s v b)
	    (if a (org-odt-export-to-odt t s v)
	      (org-open-file (org-odt-export-to-odt nil s v) 'system))))))

Edit line

(org-open-file (org-odt-export-to-odt nil s v) 'system))))))

delete 'system

This line should look like this:

(org-open-file (org-odt-export-to-odt nil s v)))))))

c) Recompile org. Package manager straight.el recompiled it after restart of emacs.

Could I ask:

  1. Why these workarounds (especially 3d one) work?
  2. I believe that directly editing .el files is bad practice, maybe there is a better way?
  3. How emacs works differently in NixOS comparing to other distros (Debian)? Does emacs run in some kind of container?
  4. Is it possible to patch emacs in order to make mentioned features work out of the box or maybe to write some .nix script for configuration.nix?

I can answer one of your questions, at least partially.

I believe that directly editing .el files is bad practice, maybe there is a better way?

Editing .el files isn’t bad, in fact it’s a great way to customise your installation. But there are some choices you need to make,

  1. Install packages the nix way, or install them using the Emacs install function/menus?

Personally, I prefer to use nix to manage my emacs packages. The advantage is that I have my NixOS configuration mirrored on several machines, so when I log into any one of those machines, I have emacs set up just the way I like it. The only disadvantage that I can think of is that a few really obscure packages aren’t available. When I started doing things this way, years ago, I was initially afraid that I’d run into problems that would be difficult for me to debug, because I’m not very Nix-savvy. But that hasn’t turned out to be the case. Almost all of the emacs problems I have are of the “oh, the version of that package in melpa stable is buggy, take it from melpa instead” variety. Those problems have nothing to do with Nix or NixOS.

To do this, I create an emacs.nix file that looks like this one. For comparison, here’s my emacs.nix. And then in my NixOS configuration I invoke it using the following snippet:

environment.systemPackages = [
    . . .
    (import ./emacs.nix { inherit pkgs; })
    . . .
  1. Configure packages using the .el files, or configure them using the Emacs customisation functions/menus?

This is a choice that you face whether you’re using NixOS or some other distro. When you configure packages using the menus, it makes changes to your emacs configuration file (init.el, usually). Fortunately, it puts the changes in a block at the end of the file, and as long as you don’t mess with that block, you should be fine.

However, eventually you may find you want more control. This is especially true when you want different settings for different file types. Then you may want to start modifying your .el files yourself. At that point, it’s easier to avoid using the configuration menus entirely; otherwise they may undo your careful changes.

Take a look at the help for org-file-apps and org-file-apps-gnu to see how the lookup is done. It eventually is delegated to mailcap. You shouldn’t have to touch the org sources - just defining those variables properly will allow you to open as you want.