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.

With optional EXISTS-FLAG non-nil, return the expanded path if it exists or if it contains file wildcards of '[]', '*', or '?'; if neither of those cases, return nil.

Without EXISTS-FLAG, return the expanded path if it is a string else the original path.

Source Code

;; Defined in ~/.emacs.d/elpa/hyperbole-20260822.1645/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'.

With optional EXISTS-FLAG non-nil, return the expanded path if it exists or
if it contains file wildcards of '[]', '*', or '?'; if neither of those
cases, return nil.

Without EXISTS-FLAG, return the expanded path if it is a string else the
original path."
  (when (stringp path)
    (unless (string-match-p hpath:variable-regexp path)
      ;; Replace any $VAR environment variable references
      (setq path (substitute-in-file-name path)))
    ;; Don't expand if path is a "*Buffer Name*"
    (let ((case-fold-search nil))
      (if (string-match-p "\\`\\*[A-Z].*\\*\\'" path)
          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 (stringp page-file)
                           (setq substituted-path (expand-file-name page-file hywiki-directory))))))
                 (t (expand-file-name substituted-path))))
          (if exists-flag
              (when (and (stringp expanded-path)
                         (or (file-exists-p expanded-path)
                             (string-match "[[*?]" (file-local-name expanded-path))))
                expanded-path)
            (if (stringp expanded-path)
                expanded-path
	      path)))))))