Function: hpath:call

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

Signature

(hpath:call FUNC PATH &optional NON-EXIST)

Documentation

Call FUNC with a PATH and optional NON-EXIST flag.

Path is stripped of any prefix operator and suffix location, NON-EXIST may be either t (path cannot contain whitespace) or
'allow-spaces to allow for whitespace.

Return the result of calling FUNC, which must be either nil or the possibly modified path, but with the prefix and suffix reattached. Make any existing path within a file buffer absolute before returning.

Source Code

;; Defined in ~/.emacs.d/elpa/hyperbole-20260414.325/hpath.el
(defun hpath:call (func path &optional non-exist)
  "Call FUNC with a PATH and optional NON-EXIST flag.
Path is stripped of any prefix operator and suffix location,
NON-EXIST may be either t (path cannot contain whitespace) or
\\='allow-spaces to allow for whitespace.

Return the result of calling FUNC, which must be either nil or the
possibly modified path, but with the prefix and suffix reattached.
Make any existing path within a file buffer absolute before returning."
  (unless (or (functionp func) (subrp func))
    (error "(hpath:call): Invalid function: %s" func))
  (unless (stringp path)
    (error "(%s): '%s' must be a string" func path))
  ;; Convert tabs and newlines to space.
  (setq path (hbut:key-to-label (hbut:label-to-key path)))
  (let* ((expanded-path)
	 (prefix (when (stringp path)
		   (car (delq nil (list (when (string-match hpath:prefix-regexp path)
					  (prog1 (match-string 0 path)
					    (setq path (substring path (match-end 0)))))
					(when (string-match "\\`file://" path)
					  (setq path (substring path (match-end 0)))
					  nil)
					(when (string-match hpath:prefix-regexp path)
					  (prog1 (match-string 0 path)
					    (setq path (substring path (match-end 0))))))))))
	 (suffix (when (stringp path)
		   (apply #'concat (nreverse (list (when (string-match hpath:instance-line-column-regexp path)
						     (prog1 (match-string 0 path)
						       (setq path (substring path 0 (match-beginning 0)))))
						   (if (string-match-p hpath:variable-regexp path)
						       ;; Path may be a link reference with a suffix component
						       ;; following a comma or # symbol, so temporarily strip
						       ;; these, if any, before expanding any embedded variables.
						       (when (string-match "[ \t\n\r]*[#,]" path)
							 (prog1 (substring path (1- (match-end 0)))
							   (setq path (substring path 0 (match-beginning 0)))))
						     (unless (file-exists-p path) ;; might be #autosave-file#
						       (when (string-match hpath:markup-link-anchor-regexp path)
							 (prog1 (concat "#" (match-string 3 path))
							   (setq path (substring path 0 (match-beginning 2)))))))))))))
    (if (or (null path) (string-empty-p path))
	(setq expanded-path ""
	      path "")
      ;; Never expand paths with a prefix character, e.g. program
      ;; names which need to use exec-directory expansion.
      (setq expanded-path (if prefix (hpath:resolve path) (hpath:expand path))
	    path (funcall func path non-exist)))
    ;;
    ;; If path is just a local reference that begins with #,
    ;; in a file buffer, prepend the file name to it.  If an HTML
    ;; file, prepend file:// to it.
    (let ((mode-prefix (if (memq major-mode '(js2-mode js-mode js3-mode javascript-mode html-mode web-mode))
			   "file://"
			 "")))
      (if (and path
	       (not (string-empty-p path))
	       (or (file-exists-p path)
		   ;; If just a numeric suffix like ":40" by itself, ignore
		   ;; it, but if a markdown type suffix alone, like
		   ;; "#section", use it.
		   (and suffix (not (string-empty-p suffix))
			(= ?# (aref suffix 0)))))
	  (progn
	    (setq path (concat prefix path suffix))
	    (cond ((and (hypb:buffer-file-name)
			;; match to in-file #anchor references
			(string-match "\\`#[^+\'\"<>#]+\\'" path)
			;; ignore HTML color strings
			(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)))
		   (setq path (concat mode-prefix (hypb:buffer-file-name) path)))
		  ((string-match "\\`\\([^#]+\\)\\(#[^#+]*.*\\)\\'" path)
		   ;; file and #anchor reference
		   (setq suffix (match-string 2 path)
			 path (match-string 1 path))
		   (unless (file-name-absolute-p path)
		     ;; make absolute
		     (setq path (hpath:expand path))
		     (unless (string-match-p hpath:variable-regexp path)
		       (setq path (expand-file-name path))))
		   (when (or non-exist (file-exists-p path))
		     (setq path (concat mode-prefix path suffix))))
		  (t
		   (when (or non-exist (file-exists-p path))
		     (setq path (concat mode-prefix path))))))

	(when (or (and (stringp suffix) (not (string-empty-p suffix))
		       (= ?# (aref suffix 0)))
		  (and (stringp expanded-path)
		       (or non-exist
			   (file-name-absolute-p expanded-path) ;; absolute path
			   (string-match-p hpath:variable-regexp expanded-path) ;; path with var
			   (string-match-p "\\`([^\):]+)" expanded-path)))) ;; Info node
	  (when (or non-exist (and (not (string-empty-p expanded-path))
                                   (file-exists-p expanded-path))
		    (string-match-p ".+\\.info\\([.#]\\|\\'\\)" expanded-path))
	    (if (string-empty-p expanded-path)
		(concat prefix expanded-path suffix)
	      (concat prefix mode-prefix expanded-path suffix))))))))