Function: hpath:expand

hpath:expand is a byte-compiled function defined in hpath.el.

Signature

(hpath:expand PATH &optional EXISTS-FLAG)

Documentation

Expand relative PATH using match in hpath:auto-variable-alist.

Any single ${variable} within PATH is resolved. Then PATH is expanded from the first file matching regexp in hpath:auto-variable-alist.

Return expanded path if it exists or it contains file wildcards of
'[]', '*', or '?'.

Return any absolute or invalid PATH unchanged unless optional EXISTS-FLAG is non-nil in which case, return the expanded path only if it exists, otherwise, return nil.

Source Code

;; Defined in ~/.emacs.d/elpa/hyperbole-20260414.325/hpath.el
(defun hpath:expand (path &optional exists-flag)
  "Expand relative PATH using match in `hpath:auto-variable-alist'.
Any single ${variable} within PATH is resolved.  Then PATH is
expanded from the first file matching regexp in
`hpath:auto-variable-alist'.

Return expanded path if it exists or it contains file wildcards of
'[]', '*', or '?'.

Return any absolute or invalid PATH unchanged unless optional
EXISTS-FLAG is non-nil in which case, return the expanded path
only if it exists, otherwise, return nil."

  (when (stringp path)
    (unless (string-match-p hpath:variable-regexp path)
      ;; Replace any $VAR environment variable references
      (setq path (substitute-in-file-name path)))
    (let (variable-path
	  substituted-path
	  expanded-path)
      (setq
       ;; Expand relative path from appropriate multi-path prefix variables
       variable-path (hpath:expand-with-variable path)
       ;; Substitute values for Emacs Lisp variables and environment variables in PATH.
       substituted-path (hpath:substitute-value variable-path)
       expanded-path
       (cond ((or (null substituted-path) (string-empty-p substituted-path))
	      path)
	     ((and (string-match-p hpath:variable-regexp variable-path)
		   (string-match-p hpath:variable-regexp substituted-path))
	      ;; If a path is invalid, then a variable may have been prepended but
	      ;; it will remain unresolved in `substituted-path', in which case we
	      ;; want to return `path' without any further changes.
	      path)
	     ;; For compressed Elisp libraries, add any found compressed suffix to the path.
	     ((string-match-p "\\.el\\(\\.\\|\\'\\)" substituted-path)
	      (or (locate-library substituted-path t) path))
	     ((or (string-match-p "\\`\\(#[^#+.]\\|([^\)\\/]+)\\|[^.\\/].*\\.[^.\\/]\\)" substituted-path)
		  (string-match-p "[\\/~]" substituted-path))
	      ;; Don't expand if an Info path, URL, #anchor or has a directory prefix
	      substituted-path)
             ((and (null (file-name-directory substituted-path))
                   ;; Could be an existing HyWikiWord
                   (let ((page-file (cdr (hywiki-get-referent substituted-path))))
                     (when page-file
                       (setq substituted-path (expand-file-name page-file hywiki-directory))))))
	     (t (expand-file-name substituted-path))))
      (if (and (stringp expanded-path)
	       (or (file-exists-p expanded-path)
		   (string-match "[[*?]" (file-local-name expanded-path))))
	  expanded-path
	(unless exists-flag
	  path)))))