[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Fontify Hex colors
From: |
Andreas Politz |
Subject: |
Re: Fontify Hex colors |
Date: |
Wed, 08 Dec 2010 15:11:38 -0000 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) |
Sébastien Vauban <wxhgmqzgwmuf@spammotel.com> writes:
> Hi,
>
> I'd like to automatically get Hex colors fontified -- when opening my Emacs
> color-theme, an HTML/CSS file or a LaTeX class file with color definitions.
>
> 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 others 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?
>
> Best regards,
> Seb
I don't know exactly, why this doesn't work. But instead of working
against 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
pseudo 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
not shure about this either.
-ap
- Re: Fontify Hex colors,
Andreas Politz <=