From 71f17de750e52e89f2182937c60f02096f4858ac Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Tue, 1 Dec 2026 10:18:58 +0100 Subject: [PATCH] Skip paths starting with /\ in file link detection On Windows a backslash is a directory separator, so `expand-file-name' turns a candidate such as the regex `/\r/g' into the network share `//r/g', and `file-exists-p' blocks for seconds looking up the host. The guard that skips `//' candidates tests the raw match, which let these through. Closes #853. --- CHANGELOG.md | 3 +++ lisp/ghostel-links.el | 5 +++-- test/ghostel-links-test.el | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1544fb01..1a7b8d24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ All notable changes to this project will be documented in this file. shell in a bridged terminal, such as a rexel or ghostel-tmux buffer. - A `file://` link without a host, or with `localhost`, opens on the remote host in a terminal whose `default-directory` is remote. +- File link detection skips paths starting with `/\`, such as the regex + `/\r/g`. On Windows such text names a network share, and looking up the + host froze Emacs for seconds. ## [0.68.0] — 2026-11-30 diff --git a/lisp/ghostel-links.el b/lisp/ghostel-links.el index d217a8f5..0be83519 100644 --- a/lisp/ghostel-links.el +++ b/lisp/ghostel-links.el @@ -572,8 +572,9 @@ Return the \(BEGIN . END) covered, the bounds widened to whole logical lines." (fragments (ghostel--wrap-fragments beg mend))) ;; `//' starts a comment or the host half of a URL, not a file; ;; this alone keeps a URL's `//host/path' out of the file pass. - ;; On Windows it names a share whose lookup can hang Emacs. - (unless (string-prefix-p "//" path) + ;; On Windows it names a share whose lookup can hang Emacs, and + ;; so does `/\', as in the regex `/\r/g'. + (unless (string-match-p "\\`/[/\\]" path) (if (ghostel--skip-match-p fragments raw active-bounds) ;; Whatever is there stays; it must not count as stranded. (push (cons beg mend) matched) diff --git a/test/ghostel-links-test.el b/test/ghostel-links-test.el index 048f9ff4..db21ded1 100644 --- a/test/ghostel-links-test.el +++ b/test/ghostel-links-test.el @@ -1479,6 +1479,24 @@ A single leading slash and relative paths still link." (mapcar #'cadr (ghostel-test--link-runs)))) (ignore dir)))) +(ert-deftest ghostel-test-detect-skips-slash-backslash-paths () + "A `/\\' candidate, as in the regex `/\\r/g', is never checked or linked. +On Windows it expands to a `//' network share." + (ghostel-test--with-link-fixture (dir file) + (with-temp-buffer + (insert "s.replace(/\\r/g, '') x = /\\d+/.test(y) " file "\n") + (goto-char (point-max)) + (let ((checked nil)) + (cl-letf* ((exists (symbol-function 'file-exists-p)) + ((symbol-function 'file-exists-p) + (lambda (f) (push f checked) (funcall exists f)))) + (ghostel--detect-urls)) + (should-not (seq-find (lambda (f) (string-match-p "\\`/[/\\]" f)) + checked))) + (should (equal (list (concat "fileref:" file)) + (mapcar #'cadr (ghostel-test--link-runs)))) + (ignore dir)))) + (ert-deftest ghostel-test-detect-replaces-stale-url-link-in-one-scan () "A leftover URL link over what is now a path becomes the path link. One scan is enough: the file pass must not treat a URL link no URL