Skip to content

Commit

Permalink
Don't error with unknown MIME type for directories
Browse files Browse the repository at this point in the history
Fix #7
  • Loading branch information
FrostyX committed Sep 20, 2024
1 parent a874d1f commit 637ebf8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
26 changes: 16 additions & 10 deletions dired-open-with.el
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,28 @@ selected application."
(complete-with-action action coll string pred))))))
(cdr (assoc value items))))

(defun dired-open-with--mimetype (path)
"Return a mimetype for file or directory at a given PATH."
(let ((name (file-name-nondirectory path))
(extension (file-name-extension path)))
(cond ((file-directory-p path) "inode/directory")
((not extension) (error "File with unknown MIME type: %s" name))
(t (mailcap-extension-to-mime extension)))))

(defun dired-open-with--applications-for-file (path)
"Return a list of applications that can open a given PATH.
Every application is represented as a Hash Table."
(let ((name (file-name-nondirectory path))
(extension (file-name-extension path)))
(unless extension
(mimetype (dired-open-with--mimetype path)))

(unless mimetype
(error "File with unknown MIME type: %s" name))

(let ((mimetype (mailcap-extension-to-mime extension)))
(unless mimetype
(error "File with unknown MIME type: %s" name))
(let ((applications (xdg-mime-apps mimetype)))
(if applications
(mapcar #'xdg-desktop-read-file applications)
(error "No XDG appliations found for MIME type: %s" mimetype)))))

(let ((applications (xdg-mime-apps mimetype)))
(if applications
(mapcar #'xdg-desktop-read-file applications)
(error "No XDG appliations found for MIME type: %s" mimetype))))))

(defun dired-open-with--xdg-format-exec (exec path)
"Format XDG application EXEC string with PATH and return an executable command.
Expand Down Expand Up @@ -138,7 +144,7 @@ string."

;;;; Footer

;; LocalWords: freedesktop org html ar
;; LocalWords: freedesktop org html ar mimetype

(provide 'dired-open-with)

Expand Down
4 changes: 4 additions & 0 deletions tests/dired-open-with-tests.el
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
;;; dired-open-with-tests.el --- Tests for dired-open-with -*- lexical-binding: t; -*-

(require 'ert)
(require 'dired-open-with)

(ert-deftest dired-open-with--mimetype ()
(should (equal (dired-open-with--mimetype "/foo/bar/baz.png") "image/png")))

0 comments on commit 637ebf8

Please sign in to comment.