Function: hyrolo-grep-file

hyrolo-grep-file is a byte-compiled function defined in hyrolo.el.

Signature

(hyrolo-grep-file HYROLO-FILE-OR-BUF PATTERN &optional MAX-MATCHES COUNT-ONLY HEADLINE-ONLY)

Documentation

Retrieve entries in HYROLO-FILE-OR-BUF matching PATTERN.

PATTERN is searched for using the function given by hyrolo-next-match-function, so it can be a text property for example, rather than just a regexp matching buffer text.

Retrieve a maximum of optional MAX-MATCHES. Nil value of MAX-MATCHES means find all matches, t value means find all matches but omit file headers, negative values mean find up to the inverse of that number of entries and omit file headers.

Optional COUNT-ONLY non-nil skips display of matching entries. Optional HEADLINE-ONLY non-nil searches only the first line of entries, rather than the full text.

Return number of matching entries found.

Source Code

;; Defined in ~/.emacs.d/elpa/hyperbole-20260414.325/hyrolo.el
(defun hyrolo-grep-file (hyrolo-file-or-buf pattern &optional max-matches count-only headline-only)
  "Retrieve entries in HYROLO-FILE-OR-BUF matching PATTERN.
PATTERN is searched for using the function given by
`hyrolo-next-match-function', so it can be a text property for
example, rather than just a regexp matching buffer text.

Retrieve a maximum of optional MAX-MATCHES.  Nil value of
MAX-MATCHES means find all matches, t value means find all
matches but omit file headers, negative values mean find up to
the inverse of that number of entries and omit file headers.

Optional COUNT-ONLY non-nil skips display of matching entries.
Optional HEADLINE-ONLY non-nil searches only the first line of
entries, rather than the full text.

Return number of matching entries found."
  ;;
  ;; Save pattern as last rolo search expression.
  (setq hyrolo-match-regexp pattern)
  ;;
  (let ((src-buf)
	;; Temporarily disable hywiki-mode for speed
	(hywiki-mode)
	;; Temporarily disable magit-auto-revert-mode-enable-in-buffers for hyrolo
	;; buffers; not needed and can slow/hang file loading
	(after-change-major-mode-hook
	 (remove 'magit-auto-revert-mode-enable-in-buffers after-change-major-mode-hook)))
    (if (and (or (null max-matches) (eq max-matches t) (integerp max-matches))
	     (or (setq src-buf (hyrolo-buffer-exists-p hyrolo-file-or-buf))
		 (when (file-exists-p hyrolo-file-or-buf)
		   (setq src-buf (hyrolo-find-file-noselect hyrolo-file-or-buf)))))
	(let ((num-found 0)
	      (incl-hdr t)
	      (stuck-negative-point 0)
	      (search-pattern pattern)
	      entry-start
	      hdr-pos)
	  (when max-matches
	    (cond ((eq max-matches t)
		   (setq incl-hdr nil max-matches nil))
		  ((< max-matches 0)
		   (setq incl-hdr nil
			 max-matches (- max-matches)))))
	  (set-buffer src-buf)

	  ;; Allow for initial asterisks being regexp-quoted in
	  ;; string-match below.
	  (when (and headline-only
		     (not (string-match (concat "\\`\\([\\*#]+[ \t]+\\|"
						"\\\\\\*+[ \t]+\\|"
						"#+[ \t]+\\|"
						(regexp-quote "^") "\\|"
						(regexp-quote "\\`") "\\)")
					pattern)))
	    ;; If matching only to headlines and pattern is not already
	    ;; anchored to the beginning of lines, add a file-type-specific
	    ;; headline prefix regexp to the pattern to match.
	    (setq search-pattern (concat hyrolo-entry-regexp ".*" pattern)))

	  (setq stuck-negative-point
		(catch 'stuck
		  (save-excursion
		    (save-restriction
		      (hyrolo-widen)
		      ;; Ensure no entries in outline mode are hidden.
		      (outline-show-all)
		      (goto-char (point-min))
		      (when (hyrolo-hdr-move-after-p)
			(setq hdr-pos (cons (point-min) (point))))
		      (let* ((case-fold-search t)
			     match-end)
			(while (and (or (null max-matches) (< num-found max-matches))
				    (funcall hyrolo-next-match-function search-pattern))
                          (when (and (not count-only) (zerop num-found))
                            (hyrolo--pre-display-buffer src-buf))
			  (setq match-end (point))
			  ;; If no entry delimiters found, just return
			  ;; the single line of the match alone.
			  (unless (re-search-backward hyrolo-hdr-and-entry-regexp nil t)
			    (goto-char (line-beginning-position)))
			  (setq entry-start (point))
			  (unless (re-search-forward hyrolo-hdr-and-entry-regexp nil t)
			    (goto-char (line-end-position)))
			  (unless (hyrolo-to-entry-end t)
			    ;; If at the end of a line, move to the next line;
			    ;; otherwise, move forward a character if possible.
			    (if (eolp)
				(when (not (eobp))
				  (forward-line 1))
			      (goto-char (1+ (point)))))

			  (when (<= (point) match-end)
			    ;; Stuck looping without moving to next entry,
			    ;; probably *word* at beginning of a line.
			    (throw 'stuck (- (point))))
			  (or count-only
			      (when (and (zerop num-found) incl-hdr)
				(let* ((src (or (hypb:buffer-file-name src-buf)
						src-buf))
				       (src-line
					(format
					 (concat (if (boundp 'hbut:source-prefix)
						     hbut:source-prefix
						   "@loc> ")
						 "%s")
					 (prin1-to-string src))))
                                  (hyrolo-set-display-buffer)
				  (goto-char (point-max))
                                  (let ((start (point)))
				    (if hdr-pos
                                        (progn
					  (insert-buffer-substring
					   src-buf (car hdr-pos) (cdr hdr-pos))
					  (insert src-line "\n\n"))
				      (insert (format hyrolo-hdr-format src-line)))
                                    ;; Add the `:hyrolo-level' property on
                                    ;; the first char in the first line of
                                    ;; the file header so outline movement
                                    ;; commands stop there.
                                    (add-text-properties start (1+ start) '(:hyrolo-level t)))
				  (set-buffer src-buf))))
			  (setq num-found (1+ num-found))
			  (or count-only
			      ;; Highlight original pattern only here,
			      ;; not the potentially bol-anchored 'search-pattern'.
			      (hyrolo-add-match pattern entry-start (point) headline-only))))))
		  num-found))
	  (when (and (> num-found 0) (not count-only))
	    (with-current-buffer hyrolo-display-buffer
	      (push-mark nil t)
	      ;; Require a final blank line in `hyrolo-display-buffer'
	      ;; so that `outline-hide-sublevels' won't hide it and
	      ;; combine with any next file header.
	      (when (/= (char-after (1- (point-max))) ?\n)
		(save-excursion
		  (goto-char (point-max))
		  (newline))))
            (hyrolo--cache-major-mode src-buf))
	  (when (< stuck-negative-point 0)
	    (pop-to-buffer src-buf)
	    (goto-char (- stuck-negative-point))
	    (error "(hyrolo-grep-file): Stuck looping in buffer \"%s\" at position %d"
		   (buffer-name) (point)))
	  (hyrolo-kill-buffer src-buf)
	  num-found)
      0)))