12

Comments in source code often contain URL's. It would be nice to have these (or links in any plain text context) rendered as clickable links for the convenience factor when navigating a file using the mouse.

Q: Is there built-in emacs functionality or a packate that automatically render text segments that appear to be URL's as clickable links?

If this doesn't already exist, I'm inclined to implement it as a minor mode using auto-overlays. If you have implementation advice I would appreciate it!

Note: I'm aware that browse-url defaults to the URL at point.

Edit for clarification:

I am specifically interested in rendering URL's so that http://www.google.com found anywhere within a buffer to which the link rendering behavior has been applied automatically render as

google link rendering example

It has:

  • a distinct underlined link face applied
  • a keymap applied to that segment
  • a mouse face applied to emphasize that it is clickable
  • a 'help-echo value set that decodes url-encoded characters for readability

I only mention browse-url for completeness regarding the suggestion that fontifying the link is unnecessary.

3 Answers 3

21

Emacs offers goto-address-mode for making a number of things, including HTTP links, clickable.

1
  • This is almost precisely what I had in mind (although I will likely use @Tarsius 's orglink because it is more powerful).
    – ebpa
    Feb 7, 2017 at 19:36
8

My orglink package does that. It "implements support for some Org Mode link types in other major modes. Links can be opened and edited like in Org Mode".

These are some examples of links that it does and does not support:

[[Code]]
[[Code][start of code]]
[[define-derived-mode orglink-mode][orglink-mode]]
<mailto:jonas@bernoul.li>
man:info
<info:man>
https://github.com/tarsius/orglink
TODO support Emacs Lisp xref links like (in) `help-mode'.
TODO support footnote.el links (only)
2
  • 1
    I was not aware that orglink supported a raw URL syntax. I'll give it a shot! Which of those examples that you list are not supported?
    – ebpa
    Feb 7, 2017 at 17:42
  • The ones that are prefixed with TODO.
    – tarsius
    Feb 12, 2018 at 0:36
4

It's not 100% clear if you want only the ability to click on URLs, or if you want to change their faces in the buffer. For the former, you can use either browse-url as you suggest:

https://www.google.com

(global-set-key [mouse-1] #'browse-url) ; pick your preferred mouse button

Or, if you just want it to open without having to confirm the URL, you could use ffap-at-mouse:

(global-set-key [mouse-1] #'ffap-at-mouse)

Here's the docstring for browse-url:

Ask a WWW browser to load URL.

Prompts for a URL, defaulting to the URL at or before point. Variable browse-url-browser-function says which browser to use. If the URL is a mailto: URL, consult browse-url-mailto-function first, if that exists.

And here's the docstring for ffap-at-mouse:

Find file or URL guessed from text around mouse click. Interactively, calls ffap-at-mouse-fallback if no guess is found. Return value:

  • if a guess string is found, return it (after finding it)
  • if the fallback is called, return whatever it returns
  • otherwise, nil
2
  • In response to the clarifying edit of the question: it now looks like you want a lot more than just the ability to click on the URL. In that case, I think @tarsius's answer is closer to what you want. I'll leave this one up here in case someone just wants the clickability aspect and wants to know what to bind.
    – Dan
    Feb 7, 2017 at 17:45
  • Sorry about that. I should have referred to org-mode link behavior originally. I am intrigued by globally setting [mouse-1] though. I wouldn't have considered that approach. It does have the advantage(?) of opening file paths which orglink relies on the org link syntax to perform.
    – ebpa
    Feb 7, 2017 at 19:42

Your Answer

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

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