I'm running emacs on WSL.
The following function launches the binary file marked in dired:
(defun wsl/execute ()
"execute file at point in dired"
(interactive)
(dired-do-shell-command
"cmd.exe /c start \"\""
current-prefix-arg
(dired-get-marked-files t current-prefix-arg)))
I would like to use dired-get-file-for-visit
instead of dired-get-marked-files
to skip the marking.
My attempt at this does not work:
(defun wsl/execute ()
"execute file at point in dired"
(interactive)
(dired-do-shell-command
"cmd.exe /c start \"\""
current-prefix-arg
(list (dired-get-file-for-visit))))
With the latter and /e/Downloads/AnyDesk.exe
I get Invalid switch - "/e".^M
What am I missing from the second implementation?
UPDATE
For posterity, here are the final functions I'm using
(defun wsl/execute ()
"execute file at point in dired"
(interactive)
(dired-do-shell-command
"cmd.exe /c start \"\""
current-prefix-arg
(dired-get-marked-files t t)))
Enter
on file to launch it or open it:
(defun wsl-p ()
"Is the system WSL?"
(and (eq system-type 'gnu/linux)
(string-match
"Linux.*Microsoft.*Linux"
(shell-command-to-string "uname -a"))))
(defun dired-find-file-or-execute (orig-fun &rest args)
"Wrapper for dired-find-file that, on WSL, launches binaries instead of opening them"
(interactive)
(let ((file (dired-get-marked-files t t)))
(if
(and
(wsl-p)
(or (string-match-p "\.exe\\'" (first file))
(string-match-p "\.bat\\'" (first file))
(string-match-p "\.com\\'" (first file))))
(dired-do-shell-command "cmd.exe /c start \"\"" current-prefix-arg (dired-get-marked-files t t))
(apply orig-fun args))))
(advice-add 'dired-find-file :around #'dired-find-file-or-execute)