Opening files with default Windows application from within emacs
To extend on the 'org-open-file' proposal:
(defun my-dired-find-file (&optional prefix) (interactive "P") (if prefix (org-open-file (dired-get-file-for-visit) 'system) (dired-find-file)))(define-key dired-mode-map "\r" 'my-dired-find-file)
Will let you open a file externally with `C-u RET'.
found at http://lists.gnu.org/archive/html/bug-gnu-emacs/2012-11/msg01069.html
- Evaluate the following elisp
- Run dired (M-x dired)
- Browse to directory and file
With point on file, pressing F3 will open the file based on the windows extension.
(defun w32-browser (doc) (w32-shell-execute 1 doc))
(eval-after-load "dired" '(define-key dired-mode-map [f3] (lambda () (interactive) (w32-browser (dired-replace-in-string "/" "\\" (dired-get-filename))))))
I found this terrific web page via google, which let me to a technique using RunDll that works. I'm putting it up here in case anyone else is curious.
Here is the key piece of code, which opens filename
using the appropriate application:
(shell-command (concat "rundll32 shell32,ShellExec_RunDLL " (shell-quote-argument filename)))
And here is my full solution. (Note that dired-find-file
is just a wrapper round find-file
which doesn't know the filename, so that you have to advise find-file
rather than dired-find-file
as in the question. If you don't want the behaviour for find-file
you will probably need to rewrite dired-find-file
or write more complicated advice.)
(defun open-externally (filename) (shell-command (concat "rundll32 shell32,ShellExec_RunDLL " (shell-quote-argument filename))))(defun is-file-type? (filename type) (string= type (substring filename (- (length filename) (length type)))))(defun should-open-externally? (filename) (let ((file-types '(".pdf" ".doc" ".xls"))) (member t (mapcar #'(lambda (type) (is-file-type? filename type)) file-types))))(defadvice find-file (around find-file-external-file-advice (filename &optional wildcards)) "Open non-emacs files with an appropriate external program" (if (should-open-externally? filename) (open-externally filename) ad-do-it))(ad-activate 'find-file)