Skip to content

Commit d3e1a9b

Browse files
authored
Merge pull request #827 from rswgnu/rsw
hpath:absolute-to - Rewrite to fix when given multiple 'default-dirs'
2 parents 5a8a0f8 + 22e6e74 commit d3e1a9b

File tree

3 files changed

+46
-37
lines changed

3 files changed

+46
-37
lines changed

ChangeLog

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
2025-12-06 Bob Weiner <rsw@gnu.org>
2+
3+
* hpath.el (hpath:absolute-to): Rewrite to fix when given multiple 'default-dirs'
4+
to test until find the first one that expands to an existing file before
5+
dropping out of the loop.
6+
(hpath:call): Fix so when 'path' exists (may have already been expanded),
7+
it is used rather than the 'expanded-path' variable.
8+
test/hpath-tests.el (hpath--absolute-to): Enable this test; fixed by above changes.
9+
(hpath:call): Fix one case where 'mode-prefix' was not prepended.
10+
111
2025-11-30 Mats Lidell <matsl@gnu.org>
212

313
* hpath.el (hpath:find-file-mailcap): Remove unused.

hpath.el

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
;; Author: Bob Weiner
44
;;
55
;; Orig-Date: 1-Nov-91 at 00:44:23
6-
;; Last-Mod: 30-Nov-25 at 19:58:24 by Mats Lidell
6+
;; Last-Mod: 6-Dec-25 at 22:57:33 by Bob Weiner
77
;;
88
;; SPDX-License-Identifier: GPL-3.0-or-later
99
;;
@@ -762,30 +762,29 @@ used."
762762
(lambda (path non-exist)
763763
(when (stringp path)
764764
(setq path (hpath:trim path)))
765-
(cond ((not (and (stringp path)
766-
(not (hypb:object-p path))
767-
(setq path (hpath:expand path))
768-
(not (get-buffer path))
769-
(not (file-name-absolute-p path))
770-
(hpath:is-p path nil non-exist)))
771-
path)
772-
((not (cond ((null default-dirs)
773-
(setq default-dirs (cons default-directory nil)))
774-
((stringp default-dirs)
775-
(setq default-dirs (cons default-dirs nil)))
776-
((listp default-dirs))
777-
(t nil)))
778-
path)
779-
(t
780-
(let ((rtn) dir)
781-
(while (and default-dirs (null rtn))
782-
(setq dir (expand-file-name
783-
(file-name-as-directory (car default-dirs)))
784-
rtn (expand-file-name path dir)
785-
default-dirs (cdr default-dirs))
786-
(unless (file-exists-p rtn)
787-
(setq rtn nil)))
788-
(or rtn path)))))
765+
(let ((dirs default-dirs)
766+
dir
767+
expanded-path)
768+
(setq expanded-path
769+
(cond ((or (stringp dirs) (null dirs))
770+
(expand-file-name path dirs))
771+
((listp dirs)
772+
(while (and dirs (null expanded-path))
773+
(setq dir (expand-file-name
774+
(file-name-as-directory (car dirs)))
775+
expanded-path (expand-file-name path dir)
776+
dirs (cdr dirs))
777+
(unless (file-exists-p expanded-path)
778+
(setq expanded-path nil)))
779+
(or expanded-path path))
780+
(t (error "(hpath:absolute-to): `default-dirs' must be a string or list, not `%s'" default-dirs))))
781+
(if (and (stringp expanded-path)
782+
(not (hypb:object-p expanded-path))
783+
(not (get-buffer expanded-path))
784+
(file-name-absolute-p expanded-path)
785+
(hpath:is-p expanded-path nil non-exist))
786+
expanded-path
787+
path)))
789788
path 'allow-spaces)))
790789

791790
(defun hpath:tramp-file-name-regexp ()
@@ -1053,7 +1052,7 @@ Make any existing path within a file buffer absolute before returning."
10531052
;; Never expand paths with a prefix character, e.g. program
10541053
;; names which need to use exec-directory expansion.
10551054
(setq expanded-path (if prefix (hpath:resolve path) (hpath:expand path))
1056-
path (funcall func expanded-path non-exist)))
1055+
path (funcall func path non-exist)))
10571056
;;
10581057
;; If path is just a local reference that begins with #,
10591058
;; in a file buffer, prepend the file name to it. If an HTML
@@ -1063,18 +1062,19 @@ Make any existing path within a file buffer absolute before returning."
10631062
"")))
10641063
(if (and path
10651064
(not (string-empty-p path))
1066-
;; If just a numeric suffix like ":40" by itself, ignore
1067-
;; it, but if a markdown type suffix alone, like
1068-
;; "#section", use it.
1069-
(and suffix (not (string-empty-p suffix))
1070-
(= ?# (aref suffix 0))))
1065+
(or (file-exists-p path)
1066+
;; If just a numeric suffix like ":40" by itself, ignore
1067+
;; it, but if a markdown type suffix alone, like
1068+
;; "#section", use it.
1069+
(and suffix (not (string-empty-p suffix))
1070+
(= ?# (aref suffix 0)))))
10711071
(progn
10721072
(setq path (concat prefix path suffix))
10731073
(cond ((and (hypb:buffer-file-name)
1074-
;; ignore HTML color strings
1075-
(not (string-match "\\`#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]\\'" path))
10761074
;; match to in-file #anchor references
1077-
(string-match "\\`#[^+\'\"<>#]+\\'" path))
1075+
(string-match "\\`#[^+\'\"<>#]+\\'" path)
1076+
;; ignore HTML color strings
1077+
(not (string-match "\\`#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]\\'" path)))
10781078
(setq path (concat mode-prefix (hypb:buffer-file-name) path)))
10791079
((string-match "\\`\\([^#]+\\)\\(#[^#+]*.*\\)\\'" path)
10801080
;; file and #anchor reference
@@ -1089,7 +1089,7 @@ Make any existing path within a file buffer absolute before returning."
10891089
(setq path (concat mode-prefix path suffix))))
10901090
(t
10911091
(when (or non-exist (file-exists-p path))
1092-
path))))
1092+
(setq path (concat mode-prefix path))))))
10931093

10941094
(when (or (and (stringp suffix) (not (string-empty-p suffix))
10951095
(= ?# (aref suffix 0)))

test/hpath-tests.el

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
;; Author: Mats Lidell <matsl@gnu.org>
44
;;
55
;; Orig-Date: 28-Feb-21 at 23:26:00
6-
;; Last-Mod: 2-Dec-25 at 12:46:06 by Mats Lidell
6+
;; Last-Mod: 6-Dec-25 at 22:34:32 by Bob Weiner
77
;;
88
;; SPDX-License-Identifier: GPL-3.0-or-later
99
;;
@@ -337,7 +337,6 @@
337337

338338
(ert-deftest hpath--absolute-to ()
339339
"Verify `hpath:absolute-to'."
340-
:expected-result :failed
341340
;; Not valid path return unchanged
342341
(should-not (hpath:absolute-to nil))
343342
(should (= (hpath:absolute-to 1) 1))

0 commit comments

Comments
 (0)