Skip to content

Link files inside URLs and keep balanced parentheses in URL links - #760

Merged
dakra merged 1 commit into
mainfrom
fix/file-links-inside-urls
Oct 24, 2026
Merged

dakra merged 1 commit into
mainfrom
fix/file-links-inside-urls

Conversation

@dakra

@dakra dakra commented Oct 24, 2026

Copy link
Copy Markdown
Owner

Follow-up to #759.

File paths inside URLs link as files

The file pass used to skip any candidate overlapping a URL match (url-ranges), plus a second check (ghostel--url-link-p) that protected an earlier scan's URL link when URL detection had been switched off. With // candidates skipped since #759, the only text inside a URL the file pass can still reach follows a : or a parenthesis, e.g. https://host/upload(/tmp/a.log). Linking that path as a file is useful rather than harmful, so both checks are removed.

Every character of a URL link carries the full URL as its target, so after the file pass overlays the path, a click on the scheme, host or closing paren still opens the complete URL, and a click on the path opens the file. Only an existing file links; a non-existent path leaves the URL link whole.

Known side effect, accepted: such a URL becomes three navigation stops for ghostel-next-hyperlink (URL prefix, file, closing paren).

URL links keep balanced parentheses

ghostel--url-regex is built from runs of URL characters interleaved with balanced (...) groups, ending in a non-punctuation character or a balanced group.

Text Before After
https://w.org/Rust_(language) https://w.org/Rust_(language https://w.org/Rust_(language)
(see https://x.org/a) https://x.org/a https://x.org/a
[t](https://x.org/a_(b)) https://x.org/a_(b https://x.org/a_(b)
https://x.org/a)b https://x.org/a)b https://x.org/a

A lone ( or ) ends the URL, as in ghostty's URL regex. Unlike ghostty, any URL character is allowed inside the parens, so (v=vs.110).aspx works too. The group-based form backtracks per paren group, not per character, so a 20 KB URL or 2000 paren groups match without touching the regexp stack limit. Timing on a 1.3 MB URL-dense sample: 28.7 ms before, 29.7 ms after.

Conciseness pass over ghostel-links.el

Docstrings and comments now describe current behaviour only; a few forms lost redundant machinery (ghostel--range-overlaps-p returns the found range, ghostel--goto-hyperlink calls ghostel--find-link-1 directly, the fileref: target is a single concat). No behaviour change intended beyond the two sections above; the uncommitted diff was reviewed for equivalence, including a 20k-case fuzz of the paren-free URL regex against the old one.

Tests

  • ghostel-test-detect-links-path-inside-url-as-file replaces ghostel-test-detect-url-owns-the-path-inside-it and pins both the overlay and the full-URL targets around it.
  • Five paren cases added to the URL detection test.

The file pass used to skip any candidate overlapping a URL match, and a
second check kept it off an earlier scan's URL link when URL detection
was switched off.  Since candidates starting with // are skipped, the
only text inside a URL the file pass can still match follows a `:' or a
parenthesis, such as https://host/upload(/tmp/a.log).  Linking that
path as a file is useful, so the ownership check and ghostel--url-link-p
go.  Every character of the URL keeps the full URL as its target, so a
click on the host still opens the URL and a click on the path opens the
file.

The URL regex now treats a balanced (...) group as part of the URL, so
https://en.wikipedia.org/wiki/Rust_(programming_language) links in
full instead of losing its closing paren.  A `(' or `)' that is not
part of a balanced pair ends the URL, which still leaves out the
trailing paren of "(see https://example.com)".  The regex is built from
runs of URL characters interleaved with balanced groups rather than a
per-character alternation, so a long URL cannot overflow the regexp
matcher; matching time is unchanged within noise on a 1.3 MB sample.

Also shortens the docstrings and comments throughout ghostel-links.el
to describe current behaviour only, drops a few redundant forms
(ghostel--range-overlaps-p returning the range, ghostel--goto-hyperlink
calling ghostel--find-link-1 directly, the fileref concat), and
replaces the URL-ownership test with one pinning the split link.
@dakra
dakra force-pushed the fix/file-links-inside-urls branch from 04a577c to 6a8b838 Compare October 24, 2026 12:49
@dakra
dakra merged commit 6a8b838 into main Oct 24, 2026
27 checks passed
@dakra
dakra deleted the fix/file-links-inside-urls branch October 24, 2026 12:53

This branch was previously deployed

1 inactive deployment
github-pages — 6a8b8388 Deployed Oct 24, 2026 by dakra via deploy #230
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant