Skip to content

Commit

Permalink
Refactorize helm-open-file-externally and friends.
Browse files Browse the repository at this point in the history
* helm-external.el (helm-get-default-program-for-file): Do it.
(helm-open-file-externally):                            Do it.
  • Loading branch information
Thierry Volpiatto committed Mar 28, 2016
1 parent 781e82f commit e25c075
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions helm-external.el
Original file line number Diff line number Diff line change
Expand Up @@ -131,56 +131,53 @@ Try first in `helm-external-programs-associations' and then in mailcap file
if nothing found return nil."
(let* ((ext (file-name-extension filename))
(def-prog (assoc-default ext helm-external-programs-associations)))
(cond ((and def-prog (not (string= def-prog "")))
(concat def-prog " %s"))
((and helm-default-external-file-browser
(file-directory-p filename))
(concat helm-default-external-file-browser " %s"))
(cond ((and def-prog (not (string= def-prog ""))) def-prog)
((and helm-default-external-file-browser (file-directory-p filename))
helm-default-external-file-browser)
(t (helm-get-mailcap-for-file filename)))))

(defun helm-open-file-externally (file)
"Open FILE with an external program.
Try to guess which program to use with `helm-get-default-program-for-file'.
If not found or a prefix arg is given query the user which tool to use."
(let* ((fname (expand-file-name file))
(collection (helm-external-commands-list-1 'sort))
(def-prog (helm-get-default-program-for-file fname))
(real-prog-name (if (or helm-current-prefix-arg (not def-prog))
;; Prefix arg or no default program.
(prog1
(helm-comp-read
"Program: " collection
:must-match t
:name "Open file Externally"
:del-input nil
:history helm-external-command-history)
;; Always prompt to set this program as default.
(setq def-prog nil))
;; No prefix arg or default program exists.
(replace-regexp-in-string " %s\\| '%s'" "" def-prog)))
(program (concat real-prog-name " %s")))
(let* ((fname (expand-file-name file))
(collection (helm-external-commands-list-1 'sort))
(def-prog (helm-get-default-program-for-file fname))
(program (if (or helm-current-prefix-arg (not def-prog))
;; Prefix arg or no default program.
(prog1
(helm-comp-read
"Program: " collection
:must-match t
:name "Open file Externally"
:del-input nil
:history helm-external-command-history)
;; Always prompt to set this program as default.
(setq def-prog nil))
;; No prefix arg or default program exists.
def-prog)))
(unless (or def-prog ; Association exists, no need to record it.
;; Don't try to record non--filenames associations (e.g urls).
(not (file-exists-p fname)))
(when
(y-or-n-p
(format
"Do you want to make `%s' the default program for this kind of files? "
real-prog-name))
program))
(helm-aif (assoc (file-name-extension fname)
helm-external-programs-associations)
(setq helm-external-programs-associations
(delete it helm-external-programs-associations)))
(push (cons (file-name-extension fname)
(helm-read-string
"Program (Add args maybe and confirm): " real-prog-name))
"Program (Add args maybe and confirm): " program))
helm-external-programs-associations)
(customize-save-variable 'helm-external-programs-associations
helm-external-programs-associations)))
(helm-run-or-raise program file)
(setq helm-external-command-history
(cons real-prog-name
(delete real-prog-name
(cons program
(delete program
(cl-loop for i in helm-external-command-history
when (executable-find i) collect i))))))

Expand Down

0 comments on commit e25c075

Please sign in to comment.