Skip to content

Commit b84e73e

Browse files
committed
feat(io): add +open-with-default-app
1 parent 5c568d8 commit b84e73e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

elisp/+io.el

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,29 @@ current process."
337337
(delete-file (+lock--file name))
338338
t))
339339

340+
(autoload #'mailcap-extension-to-mime "mailcap")
341+
342+
;;;###autoload
343+
(defun +open-with-default-app (&optional file-or-url)
344+
"Open FILE-OR-URL in the system's default application (via \"xdg-open\").
345+
346+
When FILE-OR-URL is nil, try to guess a file/directory name or ask for one."
347+
(interactive)
348+
(let* ((file (or file-or-url
349+
;; When in `dired', get file at point
350+
(and (derived-mode-p 'dired-mode) (dired-x-guess-file-name-at-point))
351+
(thing-at-point 'existing-filename)
352+
(thing-at-point 'url)
353+
(read-file-name "File or directory to open: " nil default-directory)))
354+
(file-ext (file-name-extension file))
355+
(file-mime (and file-ext (mailcap-extension-to-mime file-ext)))
356+
(default-app (or (and file-mime (car (xdg-mime-apps file-mime))) "")))
357+
(if (and file-mime (string-match-p "^emacs\\(client\\)?$" (file-name-base default-app)))
358+
(progn (message "The default application for %S files (*.%s) is Emacs, openning with `find-file'." file-mime file-ext)
359+
(find-file file))
360+
(message "Opening %S with the default system application..." (abbreviate-file-name file))
361+
(call-process-shell-command (format "xdg-open %S" (expand-file-name file))))))
362+
340363
;;;###autoload
341364
(defun +minemacs-root-dir-cleanup ()
342365
(let ((default-directory minemacs-root-dir))

0 commit comments

Comments
 (0)