Function: consult--grep-format

consult--grep-format is a byte-compiled function defined in consult.el.

Signature

(consult--grep-format BUILDER)

Documentation

Async function highlighting grep match results.

BUILDER is the command line builder function.

Source Code

;; Defined in ~/.emacs.d/elpa/consult-20260805.1130/consult.el
;;;;; Command: consult-grep

(defun consult--grep-format (builder)
  "Async function highlighting grep match results.
BUILDER is the command line builder function."
  (consult--async-transform-by-input
   (lambda (input)
     (let ((highlight (cdr (funcall builder input))))
       (lambda (cands)
         (let ((file "") (file-len 0) result)
           (save-match-data
             (dolist (str cands (nreverse result))
               (when (string-match consult--grep-match-regexp str)
                 ;; We share the file name across candidates to reduce
                 ;; the amount of allocated memory.
                 (unless (and (= file-len (- (match-end 1) (match-beginning 1)))
                              (eq t (compare-strings
                                     file 0 file-len
                                     str (match-beginning 1) (match-end 1) nil)))
                   (setq file (match-string 1 str)
                         file-len (length file)))
                 (let* ((line (match-string 2 str))
                        (ctx (= (aref str (match-beginning 3)) ?-))
                        (sep (if ctx "-" ":"))
                        (content (substring str (match-end 0)))
                        (line-len (length line)))
                   (when (and consult-grep-max-columns
                              (length> content consult-grep-max-columns))
                     (setq content (substring content 0 consult-grep-max-columns)))
                   (when highlight
                     (funcall highlight content))
                   (setq str (concat file sep line sep content))
                   ;; Store file name in order to avoid allocations in `consult--prefix-group'
                   (add-text-properties 0 file-len `(face consult-file consult--prefix-group ,file) str)
                   (put-text-property (1+ file-len) (+ 1 file-len line-len) 'face 'consult-line-number str)
                   (when ctx
                     (add-face-text-property (+ 2 file-len line-len) (length str) 'consult-grep-context 'append str))
                   (push str result)))))))))))