Function: hsys-consult--noninteractive-grep-paths

hsys-consult--noninteractive-grep-paths is a byte-compiled function defined in hsys-consult.el.

Signature

(hsys-consult--noninteractive-grep-paths PATHS REGEXP &optional MAX-MATCHES)

Documentation

Non-interactively search PATHS for any REGEXP match with consult grep.

Return a list of (REGEXP (<paths-with-matches)).

Use ripgrep (rg) if found, otherwise, plain grep. Interactively show all matches from PATHS; see the documentation for the dir argument in consult-grep for valid values of PATHS.

Limit matches per file to the absolute value of optional MAX-MATCHES, if given and not 0. If 0, match to the start of headline text only (lines that start with hsys-consult-entry-regexp, which is dynamically simplified if no Koutline ".kotl" files are being searched).

Source Code

;; Defined in ~/.emacs.d/elpa/hyperbole-20261018.1143/hsys-consult.el
(defun hsys-consult--noninteractive-grep-paths (paths regexp &optional max-matches)
  "Non-interactively search PATHS for any REGEXP match with consult grep.
Return a list of (REGEXP (<paths-with-matches)).

Use ripgrep (rg) if found, otherwise, plain grep.  Interactively show all
matches from PATHS; see the documentation for the `dir' argument in
`consult-grep' for valid values of PATHS.

Limit matches per file to the absolute value of optional MAX-MATCHES, if
given and not 0.  If 0, match to the start of headline text only (lines that
start with `hsys-consult-entry-regexp', which is dynamically simplified if no
Koutline \".kotl\" files are being searched)."
  (hsys-consult-require-version)
  (when max-matches
    (setq max-matches (prefix-numeric-value max-matches)))
  (let ((consult-grep-args (if (and (integerp max-matches) (not (zerop max-matches)))
			       (if (listp consult-grep-args)
				   (append consult-grep-args
					   (list (format "-m %d" (abs max-matches))))
				 (concat consult-grep-args
					 (format " -m %d" (abs max-matches))))
			     (if (listp consult-grep-args)
				 (append consult-grep-args '("-l"))
			       (concat consult-grep-args " -l"))))
	(consult-ripgrep-args (if (and (integerp max-matches) (not (zerop max-matches)))
				  (if (listp consult-ripgrep-args)
				      (append consult-ripgrep-args
					      (list (format "-m %d" (abs max-matches))))
				    (concat consult-ripgrep-args
					    (format " -m %d" (abs max-matches))))
				(if (listp consult-ripgrep-args)
				    (append consult-ripgrep-args '("-l"))
				  (concat consult-ripgrep-args " -l"))))
        (hsys-consult-entry-regexp
         (if (hpath:extension-in-list-p "*.kotl" paths)
             hsys-consult-entry-regexp
           ;; If no Koutline files, use simplified heading search regexp
           "^[*#]+ ")))

    (when (and (integerp max-matches) (zerop max-matches))
      ;; Final space in leading regexp in next line makes it work with
      ;; the Orderless package.
      (setq regexp (concat hsys-consult-entry-regexp (or regexp ""))))

    ;; Ensure any env or lisp variables in paths are replaced so
    ;; grep does not ignore them.
    (setq paths (mapcar #'expand-file-name (hpath:expand-list paths)))

    ;; Consult split style usually uses '#' as a separator char but
    ;; that interferes with matching to Markdown # chars at the start
    ;; of a line in the regexp, so disable the separator char as it is
    ;; not needed for simple regexp searches.
    (let* ((consult-async-split-style nil)
	   (builder-result (funcall
			    (funcall
			     (if (executable-find "rg")
				 #'consult--ripgrep-make-builder
			       #'consult--grep-make-builder)
			     paths)
			    regexp))
	 (cmd-list (car builder-result))
	 ;; Grep for filenames with matches in them and cache the line-level results
	 (null-separated-files (ignore-errors (car (apply #'process-lines (car cmd-list) (cdr cmd-list)))))
	 (matching-files (when null-separated-files (nreverse (split-string null-separated-files "\^@" t)))))
      (list regexp matching-files))))