From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Heime Newsgroups: gmane.emacs.help Subject: Colour search-text across multiple lines in the created buffer Date: Sat, 07 Sep 2024 18:50:10 +0000 Message-ID: <94Pp6luuyqGNeR7cWMJsH5l-occTthTqhGLASMeV0xsUFAjzfzbYZxd-z6IuexF1MJJGaHYVjEa_PTvZjnBe0gb79hW6pn_S76omSNaLY5Y=@protonmail.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="8378"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Heime via Users list for the GNU Emacs text editor To: Heime Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sat Sep 07 20:51:07 2024 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1sn0WZ-00022K-F8 for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 07 Sep 2024 20:51:07 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sn0Vo-0008An-QI; Sat, 07 Sep 2024 14:50:20 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sn0Vn-0008Ae-Fm for help-gnu-emacs@gnu.org; Sat, 07 Sep 2024 14:50:19 -0400 Original-Received: from mail-4325.protonmail.ch ([185.70.43.25]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sn0Vl-0006du-9f for help-gnu-emacs@gnu.org; Sat, 07 Sep 2024 14:50:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1725735015; x=1725994215; bh=ccpDyuhjOhuDA7UivON7RXNV3y1y5g7g2LxVuvuU7LU=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=re+KiJyASwBFw+n08ZkzneOM7uH8moNntvdn/W5rkcQniWWcRCMys3vA0dw6nAPEp I8VGYmVG9iELHQYghtTcfZdvSFceNV6sF2yJcrMZiEqZRpT5VQvbO49M1eW9HPEeLD c3/qfFRa29IQ0zDU71FgrlUXXWZXO1sXYGigb+wUhbSY3Dev1Wt6cUN9f5xcf56Dry RLRHxPSHwP9O/PLfYTzEDwoDUddsc+PwzL/rYAwB1ntBKBEQdmunvLagOeqBjEpTRX gexstG8SiC0H68XskUG7+a6EiWC4Yvg/2qASTFVk/sz8bKo1JFs6dl7kulvyMmdzv6 AYSA4SwhU9EGA== In-Reply-To: Feedback-ID: 57735886:user:proton X-Pm-Message-ID: ec5e7d33383ac222248442a8823873e7c4871b65 Received-SPF: pass client-ip=185.70.43.25; envelope-from=heimeborgia@protonmail.com; helo=mail-4325.protonmail.ch X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, TO_EQ_FM_DIRECT_MX=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:147934 Archived-At: Sent with Proton Mail secure email. On Sunday, September 8th, 2024 at 4:20 AM, Heime 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 ? >=20 > (defun xiakos-context (search-text &optional n) >=20 > ;; Set a default value for N if not provided > (setq n (or n 8)) >=20 > (save-excursion >=20 > (goto-char (point-min)) ;; Start from the beginning of the buffer >=20 > (let ( (search-regexp > (replace-regexp-in-string " " "[ \n]" > (regexp-quote search-text)))) >=20 > (if (re-search-forward search-regexp nil t) >=20 > (let ( (match-start (match-beginning 0)) >=20 > (match-end (match-end 0)) >=20 > (start-line (max 1 (- (line-number-at-pos match-start) n))) >=20 > (end-line (+ (line-number-at-pos match-end) n)) >=20 > (context-start (progn (goto-line start-line) (point))) >=20 > (context-end (progn (goto-line end-line) (line-end-position))) >=20 > (context > (buffer-substring-no-properties > context-start context-end)) >=20 > ;; Create a temporary buffer to display the context > (context-buffer > (get-buffer-create "Search Context")) ) >=20 > (with-current-buffer context-buffer > (erase-buffer) > (insert context) >=20 > (font-lock-mode 1) >=20 > ;; Highlight the search text > (let ((pos 0)) > (while (setq pos > (string-match search-regexp context pos)) >=20 > (let ((overlay (make-overlay > (+ context-start pos) > (+ context-start (match-end 0))))) > (overlay-put overlay 'face '(:background "yellow")) > (setq pos (match-end 0))))) >=20 > (goto-char (point-min))) >=20 > (display-buffer context-buffer))) )) ) I see the problem - context-start and context-end refer to the=20 positions in the original buffer, but the highlighting is supposed=20 to be applied to the text in the newly created buffer, which doesn't=20 directly correspond to the same positions. Is it a good strategy to use an overlay to colour the matched string=20 in thus case ?