Thursday, November 21, 2019

Open Org Mode Links With Default Windows Application

I've been using Org Mode for several years.

One Org Mode quirk is that the links in Org files always open in the Emacs editor, by default.  Click a link to Emacs_reference_card_v25.pdf on your hard drive and you might end up with a buffer that starts with this content...

%PDF-1.5
%    
3 0 obj
<<
/Length 4340      
/Filter /FlateDecode
>>
stream

I finally figured out a way to tell Emacs to open such "external" links with the default Windows application.  The multi-part solution involves first specifying all such links with a Special Prefix.  I chose "file:".  It's lame, I know, but it's also how you'd prefix an address for your web browser to open a local file.

So, for the PDF above, the link would look like this:
file:C:\emacs-25.2_64\doc\Emacs_reference_card_v25.pdf

The second part of the solution is to figure out which application on Windows you'd like to use to open the file "properly."  Rather than specifying multiple programs, such as a PDF reader for PDF files, a spreadsheet program for XLS/ODS, a word processor for DOC/ODT, etc., I decided that the single Windows command START already knows how to open all the programs on my computer.

If you were to enter the following in a command prompt, the program associated with the PDF extension would open the target file (assuming the file exists).

START C:\emacs-25.2_64\doc\Emacs_reference_card_v25.pdf

But there's a slight complication.  If the link contains a space or other delimiting character, you'll need to surround the file path with quotes like this: "C:\Path With Spaces\My PDF.pdf".  Unfortunately, START interprets quoted content as the title specification. Therefore, invoking the following would merely open a command prompt window with the title C:\Path With Spaces\My PDF.pdf

START "C:\Path With Spaces\My PDF.pdf"

So to get START to work as intended, you'd want to invoke it thusly:

START "DUMMY_TITLE" "C:\Path With Spaces\My PDF.pdf"

So having figured out the proper way to open file links, we create a function to implement it:

(defun ludditegeek-open-ext (path-to-media)
 (shell-command (concat "start \"ludditegeek-open-ext\" " path-to-media)))

Note that I chose to use the function name as the text for the title.  No matter, if all goes well, START will close the command prompt after it carries out the command -- most likely you'll never see the window.  For debugging, you could include the /WAIT switch, in which case you'll see the window, and the application will be listed in Task Manager with the title included.

Another thing to note here is that strings are defined in Lisp as characters enclosed in double-quotes.  But the title also requires double-quotes!  So I used a backslash character to escape the double-quote characters that are used to define the title.

The third part of the solution is to employ org-add-link-type to define the "file" link:

(org-add-link-type "file" 'ludditegeek-open-ext)

I call it with eval-after-load.  Here, finally, is what you can put in your .emacs:

(eval-after-load "org"
  '(progn
     ;; Create links in Org thusly:
     ;; [[file:/path/to/ppt.pptx][name of ppt]]
     ;; [[file:"/path with spaces/to/pdf.pdf"][name of pdf]]
     ;; [[file:/path/to/video.mkv][name of video]]
     (defun ludditegeek-open-ext (path-to-media)
       ;; Use Windows start command to open default application.
       ;; Note that first parameter to START is the command prompt window's title,
       ;; necessary for links that are enclosed in "", such as links with spaces.
       (shell-command (concat "start \"ludditegeek-open-ext\" " path-to-media)))
     (org-add-link-type "file" 'ludditegeek-open-ext)
     ))

I hope you find this useful!