all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Heime <heimeborgia@protonmail.com>
To: Heime <heimeborgia@protonmail.com>
Cc: Heime via Users list for the GNU Emacs text editor
	<help-gnu-emacs@gnu.org>
Subject: Colour search-text across multiple lines in the created buffer
Date: Sat, 07 Sep 2024 18:50:10 +0000	[thread overview]
Message-ID: <94Pp6luuyqGNeR7cWMJsH5l-occTthTqhGLASMeV0xsUFAjzfzbYZxd-z6IuexF1MJJGaHYVjEa_PTvZjnBe0gb79hW6pn_S76omSNaLY5Y=@protonmail.com> (raw)
In-Reply-To: <xOkG3HawPznBSz-Lbnv6tIbDOh448jkAuFRYYcGhXqKZ04sz89eW8w7KzVdk5Fv_W9pS3hGnUFdY0wlwFxZF-cDTdo5niy60yGwTUfbOnO0=@protonmail.com>






Sent with Proton Mail secure email.

On Sunday, September 8th, 2024 at 4:20 AM, Heime <heimeborgia@protonmail.com> wrote:

> I want to colour the search-text in the created buffer, even when
> the search text spans multiple lines. How can I do it ?
> 
> (defun xiakos-context (search-text &optional n)
> 
> ;; Set a default value for N if not provided
> (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))
> 
> (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))
> 
> ;; Create a temporary buffer to display the context
> (context-buffer
> (get-buffer-create "Search Context")) )
> 
> (with-current-buffer context-buffer
> (erase-buffer)
> (insert context)
> 
> (font-lock-mode 1)
> 
> ;; Highlight the search text
> (let ((pos 0))
> (while (setq pos
> (string-match search-regexp context pos))
> 
> (let ((overlay (make-overlay
> (+ context-start pos)
> (+ context-start (match-end 0)))))
> (overlay-put overlay 'face '(:background "yellow"))
> (setq pos (match-end 0)))))
> 
> (goto-char (point-min)))
> 
> (display-buffer context-buffer))) )) )

I see the problem - context-start and context-end refer to the 
positions in the original buffer, but the highlighting is supposed 
to be applied to the text in the newly created buffer, which doesn't 
directly correspond to the same positions.

Is it a good strategy to use an overlay to colour the matched string 
in thus case ?



      reply	other threads:[~2024-09-07 18:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-07 16:20 Colour search-text across multiple lines in the created buffer Heime
2024-09-07 18:50 ` Heime [this message]

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='94Pp6luuyqGNeR7cWMJsH5l-occTthTqhGLASMeV0xsUFAjzfzbYZxd-z6IuexF1MJJGaHYVjEa_PTvZjnBe0gb79hW6pn_S76omSNaLY5Y=@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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.