Subject: [PATCH] Add new command 'browse-url-here'

From: Philip Kaludercic <[email-masked]>
Date: Tue, 13 Feb 2024 10:09:50 +0000

The idea is to have a command that can be used to open a file or
directory using something outside Emacs.

I find this useful, but it might be that there are edge-cases that I am
not considering that don't pop up on my system.

>From 1f25f653ba35cb2c63a481522ffbf727bc2f5dd3 Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <[email-masked]>
Date: Tue, 13 Feb 2024 11:08:09 +0100
Subject: [PATCH] Add new command 'browse-url-here'

* lisp/net/browse-url.el (browse-url-here): Add it.
* etc/NEWS: Document it.
---
 etc/NEWS               |  4 ++++
 lisp/net/browse-url.el | 13 +++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index e7e516d61a1..ea7cd1fc517 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -317,6 +317,10 @@ Previously, it was set to t but this broke remote file name detection.
 ** Multi-character key echo now ends with a suggestion to use Help.
 Customize 'echo-keystrokes-help' to nil to prevent that.
 
+** New command 'browse-url-here'.
+This command opens the current file or directory using an external tool,
+as defined by 'browse-url-of-file'.
+
 
 * Editing Changes in Emacs 30.1
 
diff --git a/lisp/net/browse-url.el b/lisp/net/browse-url.el
index bc2a7db9a8b..abc1e9e5829 100644
--- a/lisp/net/browse-url.el
+++ b/lisp/net/browse-url.el
@@ -943,6 +943,19 @@ browse-url-at-point
 			  browse-url-new-window-flag))
       (error "No URL found"))))
 
+(defun browse-url-here (arg)
+  "Open current file or directory with external tools.
+With prefix argument ARG, open the current `default-directory' instead
+of the buffer of the current file."
+  (interactive "P")
+  (let ((browse-url-default-handlers '()))
+    (browse-url-of-file
+     (expand-file-name
+      (or (and arg ".")
+          (and (derived-mode-p 'dired-mode)
+               (dired-get-filename))
+          buffer-file-name ".")))))
+
 ;;;###autoload
 (defun browse-url-with-browser-kind (kind url &optional arg)
   "Browse URL with a browser of the given browser KIND.
-- 
2.43.0