From: Heime <heimeborgia@protonmail.com>
To: Heime via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org>
Subject: Inserting before-match, matched-text, after-match to coloursize matched-text
Date: Sun, 08 Sep 2024 01:23:53 +0000 [thread overview]
Message-ID: <CX5TULqj1U5g3iQ4b6J_HuxjbH6hkrdP98SoWnhNECQn-vHAQWbGmMxDX5IC4LuNKsUDc5yM05_QivlLOXaByehA_u5Vn9MKWViE91VDUQM=@protonmail.com> (raw)
Is it better to insert the whole match by first inserting the context
before the match, then the highlighted match itself, followed by the
context after the match ?
Or as I am doing, inserting the whole context using re-search-forward
and then applying the overlay ?
(defun xiakos-context (search-text &optional n)
(setq n (or n 8))
(save-excursion
(goto-char (point-min)) ;; Start from the beginning of the buffer
(let ( (search-regexp
(replace-regexp-in-string " " "[ \n]*"
(regexp-quote search-text))))
(if (re-search-forward search-regexp nil t)
(let* ( (match-start (match-beginning 0))
(match-end (match-end 0))
;; Calculate the line numbers for context
(start-line (max 1 (- (line-number-at-pos match-start) n)))
(end-line (+ (line-number-at-pos match-end) n))
(context-start (progn (goto-line start-line) (point)))
(context-end (progn (goto-line end-line) (line-end-position)))
(context (buffer-substring-no-properties context-start context-end))
(context-buffer (get-buffer-create "*Search Context*")) )
(with-current-buffer context-buffer
(erase-buffer)
(insert context)
(goto-char (point-min))
(font-lock-mode 1)
(while (re-search-forward search-regexp nil t)
(let ( (overlay (make-overlay (match-beginning 0) (match-end 0))) )
(overlay-put overlay 'face '(:foreground "yellow")))) )
(goto-char (point-min)))
(display-buffer context-buffer))) )) )
reply other threads:[~2024-09-08 1:23 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CX5TULqj1U5g3iQ4b6J_HuxjbH6hkrdP98SoWnhNECQn-vHAQWbGmMxDX5IC4LuNKsUDc5yM05_QivlLOXaByehA_u5Vn9MKWViE91VDUQM=@protonmail.com' \
--to=heimeborgia@protonmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).