Skip to content

Commit

Permalink
Merge pull request #499 from rswgnu/rsw
Browse files Browse the repository at this point in the history
hyrolo.el - Properly limit headline-only match highlighting region
  • Loading branch information
rswgnu committed Mar 30, 2024
2 parents ba4ba86 + a4c5970 commit e009e24
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
9 changes: 9 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
2024-03-30 Bob Weiner <rsw@gnu.org>

* hyrolo.el (hyrolo-highlight-matches): Limit search to within 'end' bound.
(hyrolo-add-match): Add 'headline-only' flag to limit match highlighting
to within the headline of an entry only (its first line).
(hyrolo-grep-file): Send 'hyrolo-add-match' the 'headline-only' flag; this
fixes match highlighting to just within headlines when the flag is given.

* hyrolo (hyrolo-grep-file): Fix to highlight only the original pattern match, not
the bol-anchored search-pattern used when 'headline-only' non-nil is used.

* hyrolo.el (hyrolo-next-match-function): Change doc so function takes only one arg now
and not a second arg of 'headline-only'. That arg is now only sent to the higher
level 'hyrolo-grep-file' function for efficiency.
Expand Down
25 changes: 15 additions & 10 deletions hyrolo.el
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;; Author: Bob Weiner
;;
;; Orig-Date: 7-Jun-89 at 22:08:29
;; Last-Mod: 30-Mar-24 at 12:47:21 by Bob Weiner
;; Last-Mod: 30-Mar-24 at 13:40:27 by Bob Weiner
;;
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
Expand Down Expand Up @@ -1969,6 +1969,7 @@ Return number of matching entries found."
(let ((num-found 0)
(incl-hdr t)
(stuck-negative-point 0)
(search-pattern pattern)
entry-start
hdr-pos)
(when max-matches
Expand All @@ -1982,12 +1983,11 @@ Return number of matching entries found."
(setq buffer-read-only t))

(when (and headline-only
(not (or (string-match (regexp-quote "^") pattern)
(string-match (regexp-quote "\\`") pattern))))
(not (string-match (concat "\\`\\(" (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 pattern (concat hyrolo-entry-regexp ".*" pattern)))
(setq search-pattern (concat hyrolo-entry-regexp ".*" pattern)))

(setq stuck-negative-point
(catch 'stuck
Expand All @@ -2003,7 +2003,7 @@ Return number of matching entries found."
match-end)
(re-search-forward hyrolo-hdr-and-entry-regexp nil t)
(while (and (or (null max-matches) (< num-found max-matches))
(funcall hyrolo-next-match-function pattern))
(funcall hyrolo-next-match-function search-pattern))
(setq match-end (point))
;; If no entry delimiters found, just return
;; the line of the match alone.
Expand Down Expand Up @@ -2045,7 +2045,9 @@ Return number of matching entries found."
(set-buffer actual-buf))))
(setq num-found (1+ num-found))
(or count-only
(hyrolo-add-match pattern entry-start (point)))))))
;; 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
Expand Down Expand Up @@ -2764,7 +2766,7 @@ entire subtree. Return INCLUDE-SUB-ENTRIES flag value."
;;; Private functions
;;; ************************************************************************

(defun hyrolo-add-match (regexp start end)
(defun hyrolo-add-match (regexp start end headline-only)
"Add in `hyrolo-display-buffer' an entry matching REGEXP from current region.
Entry is inserted before point. The region is between START to END."
(let ((hyrolo-buf (current-buffer))
Expand All @@ -2773,7 +2775,10 @@ Entry is inserted before point. The region is between START to END."
(set-buffer (get-buffer-create hyrolo-display-buffer))
(setq opoint (point))
(insert (funcall hyrolo-display-format-function hyrolo-entry))
(hyrolo-highlight-matches regexp opoint (point))
(hyrolo-highlight-matches regexp opoint
(if headline-only
(save-excursion (goto-char opoint) (line-end-position))
(point)))
(set-buffer hyrolo-buf)))

(defun hyrolo-any-file-type-problem-p ()
Expand Down Expand Up @@ -2906,13 +2911,13 @@ a default of MM/DD/YYYY."
(unless buffer-modified
(kill-buffer buf))))))

(defun hyrolo-highlight-matches (regexp start _end)
(defun hyrolo-highlight-matches (regexp start end)
"Highlight matches for REGEXP in region from START to END."
(when (fboundp 'hproperty:but-add)
(let ((hproperty:but-emphasize-flag))
(save-excursion
(goto-char start)
(while (re-search-forward regexp nil t)
(while (re-search-forward regexp end t)
(hproperty:but-add (match-beginning 0) (match-end 0)
(or hyrolo-highlight-face
hproperty:highlight-face)))))))
Expand Down

0 comments on commit e009e24

Please sign in to comment.