diff --git a/dired-x.el b/dired-x.el index 80a266f..d122027 100644 --- a/dired-x.el +++ b/dired-x.el @@ -1039,64 +1039,50 @@ REGEXP is matched case-sensitively." (defun dired-guess-default (files) "Return a shell command, or a list of commands, appropriate for FILES. See `dired-guess-shell-alist-user'." - (let* ((case-fold-search dired-guess-shell-case-fold-search) ;; Prepend the user's alist to the default alist. (alist (append dired-guess-shell-alist-user dired-guess-shell-alist-default)) - (file (car files)) - (flist (cdr files)) elt regexp cmds) - - ;; Find the first match in the alist for first file in FILES. - (while alist - (setq elt (car alist) - regexp (car elt) - alist (cdr alist)) - (if (string-match-p regexp file) - (setq cmds (cdr elt) - alist nil))) - - ;; If more than one file, see if all of FILES match regular expression. - (while (and flist - (string-match-p regexp (car flist))) - (setq flist (cdr flist))) - - ;; If flist is still non-nil, then do not guess since this means that not - ;; all the files in FILES were matched by the regexp. - (setq cmds (and (not flist) cmds)) - - ;; Return commands or nil if flist is still non-nil. - ;; Evaluate the commands in order that any logical testing will be done. - (if (cdr cmds) - (delete-dups (mapcar (lambda (cmd) (eval cmd `((file . ,file)))) cmds)) - (eval (car cmds) `((file . ,file)))))) ; single command + (cl-loop + for elt in alist + do (setq regexp (car elt)) + (cl-loop + for file in files + always (string-match-p regexp file) + finally + (cl-loop + for cmd in (cdr elt) do + (unless (stringp cmd) + (setq cmd (condition-case nil + (funcall cmd `((file . ,file))) + (error nil)))) + (and (stringp cmd) + (executable-find cmd) + (push cmd cmds))))) + (nreverse (delete-dups cmds)))) (defun dired-guess-shell-command (prompt files) "Ask user with PROMPT for a shell command, guessing a default from FILES." (let ((default (dired-guess-default files)) - default-list val) + val) (if (null default) ;; Nothing to guess (read-shell-command prompt nil 'dired-shell-command-history) (setq prompt (replace-regexp-in-string ": $" " " prompt)) - (if (listp default) + (if (cdr default) ;; More than one guess - (setq default-list default - default (car default) - prompt (concat + (setq prompt (concat prompt - (format "{%d guesses} " (length default-list)))) - ;; Just one guess - (setq default-list (list default))) + (format "{%d guesses} " (length default-list))))) ;; Put the first guess in the prompt but not in the initial value. - (setq prompt (concat prompt (format "[%s]: " default))) + (setq prompt (concat prompt (format "[%s]: " (car default)))) ;; All guesses can be retrieved with M-n (setq val (read-shell-command prompt nil 'dired-shell-command-history - default-list)) + default)) ;; If we got a return, then return default. - (if (equal val "") default val)))) + (if (equal val "") (car default) val)))) ;;; RELATIVE SYMBOLIC LINKS.