From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stephen Berman Newsgroups: gmane.emacs.help Subject: Re: Colourise text in a buffer using a list Date: Fri, 13 Sep 2024 10:07:09 +0200 Message-ID: <87y13wf042.fsf@gmx.net> References: Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="10018"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) 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 Fri Sep 13 10:07:46 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 1sp1LF-0002TE-QG for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 13 Sep 2024 10:07:45 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sp1Kp-0004y8-B2; Fri, 13 Sep 2024 04:07:19 -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 1sp1Kn-0004xj-Hk for help-gnu-emacs@gnu.org; Fri, 13 Sep 2024 04:07:17 -0400 Original-Received: from mout.gmx.net ([212.227.17.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sp1Kl-0004fj-LF for help-gnu-emacs@gnu.org; Fri, 13 Sep 2024 04:07:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.net; s=s31663417; t=1726214829; x=1726819629; i=stephen.berman@gmx.net; bh=WoNCUDP9AfwvM1JW+QXwZ6nKdf+e4Yfu98tiHBu4DSQ=; h=X-UI-Sender-Class:From:To:Cc:Subject:In-Reply-To:References:Date: Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=YKvEv4PsbA3nNuDyDU4jPANTuW8GrHPkoLZTyUMllk2xRsTvenph12/g58cSEn1f ecObnn4tLC75vflqTWExmY30KPRx9i6tsbmvO3UOcQGaI/eg2s6HJw82iGqGFZezM c0FpfeFfGXtAnlfd576kcSbNIf0vQ4IbNAUj+/+zUb2m+yzKv2wCN+Uv6ZA0aTuZi JMjvFmXAqlinn//Cg1zJk0ZBELl0fRsucVOEBZxcjURNAUBmBDHd8yJSpwf3ybze2 3aAe48w6D9LJQ46omDxHvJpYkHjyErLim42VLLDD0DQLSVtCcNDZ4kdQO1Zrt03pB Ne0wZwYtxH4R+oFD7g== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Original-Received: from strobelfssd ([94.134.95.169]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MgesG-1sNNcP3CEW-00mjZ9; Fri, 13 Sep 2024 10:07:09 +0200 In-Reply-To: (Heime's message of "Fri, 13 Sep 2024 01:21:17 +0000") X-Provags-ID: V03:K1:TsP+Mc/du4JiMwDG9JAaQRniHu9O7gvSsyCZTCLGNAC1ACrnW/u Hb2/pxi+8fmBW4iO1OdwMmLjGDVMKNZ73pP+wlyI+TC9bIVTp+mTx7YLIDMNeE8osYaVley zU9v6KJj1/tQIQNY2unLzto3jPL7iE+iqdaBa9KKmUyxsdHvRjCeD9FlwcUkdxh/RUyuP4Y YpkJLWJoBN+6lsaILsjlQ== UI-OutboundReport: notjunk:1;M01:P0:+Lqrnm1vpRA=;TkBoHulu3h7Hbn3EK06rMsC1YJ5 L9KFnIyYQgXfmbDVEAbEESiq6kG2AGADHeE0ZPcfCb7wvjpA5UEUDlr2b+zuLwsUHx8g6CXyK uDkTOh6SnZBk6TtGNWR+yZOs3YzJIV3c/vQuZ+wyiEJCyxRxEYkbrIplTNoL2cOEaLRI/YX+H EePf84W5nENkZOhaNpGekzj/aXu3/XBNFh4ukrOigQelntaKrvlulxGCgsDajXKlKSyauR/RP bpjsTt1/Oa2KQbjsnula6XSc6dqU6c1+q4kGwiWSpUZixz2YxAva6q8VRxsX9comMPinPqq3S +vSE1/G1TM72vCjuchPG73xOtxmpFIHDh1jJ2xw2pqBrbVd9VDbsJWyK/3XdWnZW7u0U869SY puxwNaeHsD1AtPWYtXPspEszpt8yf3w5L4I2lWFx1r/jOjQp0mJHZEHem5Bk/rOEuN1VyjAa2 DZsNGJZiHkIx71FZR0Kiwzu1asCiWYnCrs8GglWIJavR0gWuSSgRYBZfy5QwKdSZvEoaE5C1a UgF3iV0uLjnlBWVxdDjdq3vanBO9PzwyucDzEml6QvNXzKuA0y2MjeHbM8HHDoWmEgFc8CDy7 kmW0ZuHHT5TYZBw4SB01Av2HXz0nL4kN4xY563VnNWKMUVK+3iY2mdK+UPmveozENPNT2Vm2I x7uXqKsUlzzK7plKOUOhE2DXQinYbyu8sGGStyyxIyNu8VbbShEdjLgLwqx8jdgOnkH7AOND4 HtexLXMlbnIm4Qqr+OXg4k3QwH5EVT6Cs+YcLlnwlLZJ0g+tk86W7RqmwNzCELuiixHjltsX Received-SPF: pass client-ip=212.227.17.21; envelope-from=stephen.berman@gmx.net; helo=mout.gmx.net X-Spam_score_int: 5 X-Spam_score: 0.5 X-Spam_bar: / X-Spam_report: (0.5 / 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_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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:147982 Archived-At: On Fri, 13 Sep 2024 01:21:17 +0000 Heime wrot= e: > I have a buffer with text inserted from a list from a list. > > How can I print specific text form the list at INDEX by colouring > that text it red ? > > (defun xiakos-check-red (lst index buffer) > "Color the element at INDEX in LST red." > > (with-current-buffer buffer > > (let* ( (text (nth index lst)) > (start-pos nil) > (end-pos nil) ) > > (save-excursion > > ;; Find the start and end positions of the text > (goto-char (point-min)) > (dotimes (i index) > (forward-line) > (when (re-search-forward > (regexp-quote text) (line-end-position) t) > (setq start-pos (match-beginning 0)) > (setq end-pos (match-end 0))) > (unless start-pos > (goto-char (point-min)))) > > ;; Apply the red color to the found range > (when (and start-pos end-pos) > (add-text-properties start-pos end-pos > '(face (:foreground "red")))))))) I'm not sure whether "from a list from a list" is a typo, but IIUC what you want, here's one way, with a test buffer and invocation: (defun xiakos-check-red (lst index buffer) "Color each INDEXth element of LST found in BUFFER red." (with-current-buffer buffer (let* ((text (nth index lst)) (start-pos nil) (end-pos nil)) (save-excursion ;; Find the start and end positions of the text (goto-char (point-min)) (while (re-search-forward (regexp-quote text) (point-max) t) (setq start-pos (match-beginning 0)) (setq end-pos (match-end 0)) ;; Apply the red color to the found range (when (and start-pos end-pos) (add-text-properties start-pos end-pos '(face (:foreground "red"))))))))) =2D------------------- buffer "a" -------------------- This is a test. This is not a test. This is another test. This is a third test. This is not a test. =2D--------------------------------------------------- (xiakos-check-red '("is a") 0 "a") Steve Berman