1

Q: in there a function that can find a file or open a URL as needed?

I have a string that can be either a) a local filename, or b) a URL. Is there an existing function that I can call on the string that will do what I mean and open the file if it's a file, or open the URL in a browser if it's a URL?

It wouldn't be hard to write such a function, but I'd rather not reinvent the wheel.

1 Answer 1

1

browse-url.el can easily be configured that way.

The following section of the doc of browse-url-browser-function is relevant:

If the value is not a function it should be a list of pairs (REGEXP . FUNCTION). In this case the function called will be the one associated with the first REGEXP which matches the current URL. The function is passed the URL and any other args of `browse-url'. The last regexp should probably be "." to specify a default browser.

Example configuration:

(custom-set-default 'browse-url-browser-function
            '(("^/" . browse-url-emacs)
              ("\\`[^:]+\\'" . browse-url-emacs)
              ("\\`file://" . browse-url-emacs)
              ("." . browse-url-default-browser)))

(browse-url "/temp/test.txt") ;; opens in a new Emacs window

(let ((default-directory "/temp"))
  (browse-url "test.txt")) ;; opens the same file in Emacs window

(browse-url "file:///temp/test.txt") ;; opens the same file in Emacs window in read-only-mode

(browse-url "https://emacs.stackexchange.com") ;; opens in default browser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.