Function: hpath:get-grep-filename

hpath:get-grep-filename is a byte-compiled function defined in hpath.el.

Signature

(hpath:get-grep-filename)

Documentation

Return a list of (filename-with-path line-number) for grep line without file.

If grep is run over a single file, it omits the filename from each line unless the -H option is given.

This function extracts both the filename searched and the line number selected. For the filename, first it checks if the grep command precedes the numbered lines. If not, it finds the most recent grep command in the command-history(var)/command-history(fun) and uses the last argument if it is an existing filename.

Source Code

;; Defined in ~/.emacs.d/elpa/hyperbole-20260414.325/hpath.el
(defun hpath:get-grep-filename ()
  "Return a list of (filename-with-path line-number) for grep line without file.
If grep is run over a single file, it omits the filename from each line
unless the -H option is given.

This function extracts both the filename searched and the line number
selected.  For the filename, first it checks if the grep command precedes
the numbered lines.  If not, it finds the most recent grep command in the
`command-history' and uses the last argument if it is an existing filename."
  (let ((grep-process (apply #'derived-mode-p '(grep-mode shell-mode)))
	;; for shell command output buffers
	(grep-output  (and (not buffer-file-name) (not (get-buffer-process (current-buffer))))))
    (when (or grep-process grep-output)
      (save-excursion
	(forward-line 0)
	(when (looking-at "\\([1-9][0-9]*\\):.+")
	  (let ((line-num (match-string 1))
		cmd
		file)
	    ;; Skip over lines starting with line numbers
	    (while (re-search-backward "^[1-9][0-9]*:.+" nil t))
	    (cond (grep-process
		   ;; If there is a prior line, move to its end
		   (unless (or (= (point) (point-min)) (not (bolp)))
		     (skip-chars-backward "\n\r\t \"'`")
		     ;; If last item can be expanded as a filename, return a
		     ;; list of the filename and the line number from the
		     ;; original line
		     (setq file (thing-at-point 'existing-filename))
		     (when file
		       (list file line-num))))
		  (grep-output
		   ;; Command line is not in output buffer; have to get
		   ;; it from the command history
		   (setq cmd (cadr (seq-find (lambda (item)
					       (when (eq (car item) 'eval-expression)
						 (setq item (cadr item)))
					       (memq (car item) '(grep rgrep zrgrep hui-select-rgrep hypb:rgrep shell-command async-shell-command)))
					     command-history)))
		   (when cmd
		     (setq file (expand-file-name
				 (string-trim (car (last (split-string cmd)))
					      "[\"'`]" "[\"'`]")))
		     (when (and (file-readable-p file)
				(not (file-directory-p file)))
		       (list file line-num)))))))))))