From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: =?utf-8?Q?S=C3=A9bastien_Vauban?= Newsgroups: gmane.emacs.help Subject: Re: Fontify Hex colors Date: Mon, 07 Jun 2010 09:52:55 +0200 Organization: Sebastien Vauban Message-ID: <877hmbxors.fsf@mundaneum.com> References: <87wruf15os.fsf@mundaneum.com> <87zkzbhrcm.fsf@fh-trier.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1291838742 15775 80.91.229.12 (8 Dec 2010 20:05:42 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 8 Dec 2010 20:05:42 +0000 (UTC) To: help-gnu-emacs-mXXj517/zsQ@public.gmane.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org-mXXj517/zsQ@public.gmane.org Wed Dec 08 21:05:38 2010 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PQQGe-0006CI-3m for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 21:05:36 +0100 Original-Received: from localhost ([127.0.0.1]:41134 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQQGd-0003Gz-Jq for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 15:05:35 -0500 Original-Path: usenet.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!feeder.news-service.com!85.214.198.2.MISMATCH!eternal-september.org!.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 78 Injection-Date: Mon, 7 Jun 2010 07:52:58 +0000 (UTC) Injection-Info: mx01.eternal-september.org; posting-host="No+jx9xcG75RY5G9Zb0asg"; logging-data="6537"; mail-complaints-to="abuse-VVbKFVtnif8H+i2N2EyTrmui9UKz+5OX@public.gmane.org"; posting-account="U2FsdGVkX19G9dJgiBCFWqkKGbmV9JKA" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux) X-Archive: encrypt X-Www-site: Under construction... Cancel-Lock: sha1:Cm81W7zxqGxoiTsABAVxDttrv+M= sha1:60nugLmmSwvMBVtxGTTlSgrRCl8= Original-Xref: usenet.stanford.edu gnu.emacs.help:178702 X-BeenThere: help-gnu-emacs-mXXj517/zsQ@public.gmane.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org-mXXj517/zsQ@public.gmane.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org-mXXj517/zsQ@public.gmane.org Xref: news.gmane.org gmane.emacs.help:75945 Archived-At: Hi Andreas, Andreas Politz wrote: > S=C3=A9bastien Vauban writes: > >> I'd like to automatically get Hex colors fontified -- when opening my Em= acs >> color-theme, an HTML/CSS file or a LaTeX class file with color definitio= ns. >> >> To (try to) do so, I've added the following snippet of code into my >> `.emacs' file: >> >> #105099 >> ;; #105099 >> ;; this `#105099' is it >> >> (defun fontify-hex-colors (start end) >> "Fontify Hex colors." >> (interactive "r") >> (save-restriction >> (narrow-to-region start end) >> (goto-char (point-min)) >> (while (search-forward-regexp >> "\\(#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]= [0-9A-Fa-f]\\)" >> nil t) >> (put-text-property (match-beginning 0) (match-end 0) >> 'font-lock-face (list :background (match-string= 1)))))) >> >> Though, calling `M-x fontify-hex-colors' on that same region only >> highlights the first occurrence of the color code `#105099'. The two oth= ers >> get overridden by their normal color applied by the `emacs-lisp-mode'. >> >> The same appears in whatever mode that uses `font-lock'... >> >> Any idea how to go around this? > > I don't know exactly, why this doesn't work. But instead of working again= st > font-lock, why not let it work for you. > > I also don't know if and how it would be possible to use dynamic faces in > regular font-lock keywords. But you can use overlays combined with a pseu= do > font-lock function. > > (defun fontify-hex-colors (limit) > (remove-overlays (point) limit 'fontify-hex-colors t) > (while (re-search-forward "\\(#[[:xdigit:]]\\{6\\}\\)" limit t) > (let ((ov (make-overlay (match-beginning 0) > (match-end 0)))) > (overlay-put ov 'face (list :background (match-string 1))) > (overlay-put ov 'fontify-hex-colors t) > (overlay-put ov 'evaporate t))) > ;; return nil telling font-lock not to fontify anything from this > ;; function > nil) > > (font-lock-add-keywords nil > '((fontify-hex-colors))) > > I think this is safe, because fontifying always starts at bol, but I am n= ot > shure about this either. I did not know about all this. Thanks a lot for the explanation, and for the code. I've used it for the past day, and it seems to work exactly as expected. Thank you very much, Seb --=20 S=C3=A9bastien Vauban