Subject: RE: [External] : bug#18132: Time for a smarter dired-guess-shell-alist-default? (dired-x.el)

From: Drew Adams <[email-masked]>
Date: Sat, 23 Oct 2021 19:22:33 +0000

> Such a command is long overdue.  I used the following command
> for a long time, and forgot it's not in dired-x.  I don't suggest
> to use this implementation, but something like this.

Likewise.  There are lots of similar possibilities.
These are what I use, for MS Windows.  In Dired, 
I have these bindings (plus mouse versions):

  C-RET    dired-w32explore
  M-RET    dired-w32-browser
  C-M-RET  dired-multiple-w32-browser

(defun dired-w32-browser ()
  "Run default Windows application associated with current line's file.
If file is a directory, then `dired-find-file' instead.
If no application is associated with file, then `find-file'."
  (interactive)
  (let ((file  (dired-get-filename nil t)))
    (if (file-directory-p file)
        (dired-find-file)
      (w32-browser (subst-char-in-string ?/ ?\\ file)))))

(defun w32-browser (file)
  "Run default Windows application associated with FILE.
If no associated application, then `find-file' FILE."
  (interactive "fFile: ")
  (or (condition-case nil
          (w32-shell-execute nil file)  ; Use Windows file association
        (error nil))
      (find-file file)))

(defun dired-w32explore ()
  "Open Windows Explorer to current file or folder."
  (interactive)
  (w32explore (dired-get-filename nil t)))

(defun w32explore (file)
  "Open Windows Explorer to FILE (a file or a folder)."
  (interactive "fFile: ")
  (let ((w32file (subst-char-in-string ?/ ?\\ (expand-file-name file))))
    (if (file-directory-p w32file)
        (w32-shell-execute "explore" w32file "/e,/select,")
      (w32-shell-execute "open" "explorer" (concat "/e,/select," w32file)))))

(defun dired-multiple-w32-browser ()
  "Run default Windows applications associated with marked files."
  (interactive)
  (let ((files (dired-get-marked-files)))
    (while files
      (w32-browser (car files))
      (sleep-for w32-browser-wait-time)
      (setq files (cdr files)))))

(defun dlgopen-open-files (&optional flip)
  "Open files using the Windows standard Open dialog box.
Var `dlgopen-executable-path' is path of executable `getfile.exe'.

If `w32-browser' is defined, then it is used to open the selected
files.  Otherwise, standard Emacs `find-file' functions are used.

If `w32-browser' is not defined, then `dlgopen-other-window'
controls how selected files are opened:
  non-nil => Display chosen file(s) in separate windows.
  nil     => Display a single chosen file in the current window;
             don't display multiple chosen files.

Optional prefix arg FLIP non-nil reverses the effect of variable
`dlgopen-other-window', for this call."
  (interactive "P")
  (unless (eq system-type 'windows-nt)
    (error "Command `dlgopen-open-files' is for Windows only"))
  (let ((buffer "")
        (file-fqn "")
        (lines-in-page 0)
        (dir-path "")
        (other-win (if flip
                       (not dlgopen-other-window)
                     dlgopen-other-window)))
    (setq buffer (generate-new-buffer "files-to-open"))
    (when (call-process dlgopen-executable-path nil buffer)
      (with-current-buffer buffer
        (save-excursion
          (goto-line 1)
          (setq dir-path (get-current-line))
          ;; If buffer empty, then user has cancelled or the open failed.
          ;; If only one line in buffer, then only one file selected, so use it.
          (when (> (buffer-size) 0)
            (if (= 1 (setq lines-in-page (count-lines 1 (buffer-size))))
                (if (fboundp 'w32-browser)
                    (w32-browser dir-path)
                  (if other-win
                      (find-file-other-window dir-path)
                    (find-file dir-path)))
              (while (> lines-in-page 1)
                (decf lines-in-page)
                (forward-line)
                (setq file-fqn (concat dir-path "/" (get-current-line)))
                (save-excursion
                  (if (fboundp 'w32-browser)
                      (w32-browser file-fqn)
                    (if other-win
                        (find-file-other-window file-fqn)
                      (find-file-noselect file-fqn)))))))))) ; no display
    (kill-buffer buffer)))