0

Short version: how can I make sure that \% strings are replaced by %, in the url argument of browse-url?

Long version:

In LaTeX, URLs that appear in \url{} macros need to have any % character escaped by \ in many situations (eg when \url is within another macro; see this question). So there could be a LaTeX piece with, say,

\url{http://www.math.cmu.edu/~wn0g/Surface\%20Interactions.pdf}

This is fine, but if I wish to browse that URL from Emacs with browse-url, the escape-character \ causes problems: either

  1. browse-url recognizes the url only up to the escape, in this case http://www.math.cmu.edu/~wn0g/Surface; or
  2. if I select the full URL as a region, then browse-url includes the escape: http://www.math.cmu.edu/~wn0g/Surface\%20Interactions.pdf so that I have to remove it by hand (laborious if the URL has many %).

So I wonder if someone can help me with some solution. For example:

  • (as suggested by @db48x) Wrapping browse-url into a custom function which also takes care of stripping \ in front of %. Unfortunately my elisp knowledge is too rudimentary for writing something like this.
  • Changing browse-url, for instance with defadvice in such a way that \% strings in the url argument are always converted to %.

I first checked among Emacs's customizable variables if something like this is already built-in, but didn't find anything relevant. Also checked several questions here for this, for example this one and this one but they seem somewhat overkill.

1 Answer 1

1

You shouldn’t change browse-url at all; it should continue to accept a simple string with a properly–formed url in it. This habit of putting slashes in front of the percent–signs is unique to LaTeX, so it should be handed by code that deals with LaTeX not code that deals with urls in general. You should correct the url before you pass it along, presumably just by doing a regex replace on it first.

3
  • Thank you, but I doubt that the \% behaviour on the latex side can be easily changed, owing to the special role of % in latex. There's plenty of questions on Stackexchange about that and I found no solution on that side. But I changed my help request: an alternative would be to wrap browse-url into another function that does the replacement. This is above my skills. Regex replace by hand is not very useful – then I wouldn't even need to use browse-url.
    – pglpm
    Sep 17, 2023 at 15:33
  • 3
    You misunderstand. I don’t mean change the syntax of TeX, I mean that whatever function you write that opens the url in the browser should sanitize the url before calling browse-url to actually open the browser.
    – db48x
    Sep 17, 2023 at 15:34
  • Now I understand, yes that would be a nice solution! Unfortunately I wouldn't know where to start for writing something of the kind. But I added your suggestion in my question.
    – pglpm
    Sep 17, 2023 at 15:35

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.