added support for multiple files in open-with

This commit is contained in:
petrucci4prez 2018-08-26 00:47:18 -04:00
parent a7591e9189
commit dc645ee317
1 changed files with 36 additions and 23 deletions

View File

@ -288,6 +288,16 @@ The list is comprised of alists where pairs are of the form (name . command)."
multiple files at once for given MIMETYPE." multiple files at once for given MIMETYPE."
(let ((case-fold-search nil)) (let ((case-fold-search nil))
(seq-filter (lambda (a) (string-match ".*%[FU].*" (car a))) (nd/get-apps-from-mime mimetype)))) (seq-filter (lambda (a) (string-match ".*%[FU].*" (car a))) (nd/get-apps-from-mime mimetype))))
(defun nd/execute-desktop-command (cmd file)
"Opens FILE using CMD in separate process."
(call-process-shell-command (concat (replace-regexp-in-string "%[fuFU]" file cmd t) " &")))
(defun nd/get-mime-type (file)
"Get the mime type of FILE."
(let* ((cmd (concat "file --mime-type -b " file))
(mt (shell-command-to-string cmd)))
(replace-regexp-in-string "\n\\'" "" mt)))
#+END_SRC #+END_SRC
** interactive ** interactive
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
@ -478,32 +488,35 @@ Keeping confirmation enabled does weird stuff with helm. Not ideal at the moment
(start-process "" nil "xdg-open" f))) (start-process "" nil "xdg-open" f)))
file-list)))) file-list))))
(defun nd/execute-desktop-command (cmd file)
"Opens FILE using CMD in separate process."
(call-process-shell-command (concat (replace-regexp-in-string "%[fuFU]" file cmd t) " &")))
(defun nd/dired-open-with () (defun nd/dired-open-with ()
"Open marked non-text files in external app via open-with dialog "Open marked non-text files in external app via open-with dialog
according to mime types as listed in all available desktop files" according to mime types as listed in all available desktop files."
(interactive) (interactive)
(let* ((file-list (let* ((marked-files (seq-filter #'file-regular-p (dired-get-marked-files)))
(mapcar (file-mime-list (mapcar (lambda (f) (list f (nd/get-mime-type f))) marked-files)))
(lambda (f) (list f (replace-regexp-in-string
"\n\\'" "" (if (= (length file-mime-list) 0)
(shell-command-to-string (concat "file --mime-type -b " f))))) (message "No files selected")
(seq-filter #'file-regular-p (dired-get-marked-files)))))
(cond (let* ((first-pair (car file-mime-list))
((= (length file-list) 0) (last-pairs (cdr file-mime-list))
(message "No suitable programs found")) mime-alist file-list)
((= (length file-list) 1) (setq file-list (nth 0 first-pair)
(let* ((file-pair (car file-list)) mime-alist (nd/get-apps-from-mime (nth 1 first-pair)))
(file-name (nth 0 file-pair)) ;; if multiple files selected, add to the selection list
(mime-alist (nd/get-apps-from-mime (nth 1 file-pair)))) (if last-pairs
(helm :sources (progn
(helm-build-sync-source "Apps" (setq file-list (string-join (mapcar #'car file-mime-list) " "))
:candidates mime-alist (dolist (mime (mapcar (lambda (f) (nth 1 f)) last-pairs))
:action '(("Open" . (lambda (f) (nd/execute-desktop-command f file-name))))) (setq mime-alist (intersection mime-alist
:buffer "*helm open with*")))))) (nd/get-apps-from-mime mime)
:test #'equal)))))
(helm
:sources (helm-build-sync-source "Apps"
:candidates mime-alist
:action '(("Open" . (lambda (f) (nd/execute-desktop-command f file-list)))))
:buffer "*helm open with*")))))
#+END_SRC #+END_SRC
* org-mode * org-mode
** major mode ** major mode