* how to attach properties to a piece of text?
@ 2009-03-19 0:20 Xah Lee
2009-03-19 0:54 ` Miles Bader
0 siblings, 1 reply; 5+ messages in thread
From: Xah Lee @ 2009-03-19 0:20 UTC (permalink / raw)
To: help-gnu-emacs
how to write a function that colors particular words matching regex
inside a region?
I was thinking using font-lock-add-keywords, but that does to the
whole buffer.
i haven't studied emacs's face, display property etc systems. But
could anyone give me a rough guide on what function i should use or
lookup?
Here's a bit detail on what i need to do. I'm writing a mode for
Linden scripting language (LSL). Colors are specified using lsl's
vector type, like this:
vector red = <1,0,0>;
vector green = <0,1,0>;
vector blue = <0,0,1>;
vector black = <0,0,0>;
vector white = <1,1,1>;
vector pink = <1,0.07843,0.5765>;
vector orange = <1,0.6471,0>;
vector brown = <0.5451,0.1373,0.1373>;
vector purple = <0.3333,0.102,0.5451>;
vector gold = <1,0.8431,0>;
I want to be able write a function, something named color-vectors-
region, so that any <r,g,b>'s background is colored using the rgb
value. (each is a float from 0 to 1.) The part i don't know how, is
how to attach a text color property to a piece of text.
Thanks.
Xah
∑ http://xahlee.org/
☄
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: how to attach properties to a piece of text?
2009-03-19 0:20 how to attach properties to a piece of text? Xah Lee
@ 2009-03-19 0:54 ` Miles Bader
2009-03-19 3:19 ` Xah Lee
2009-03-19 13:11 ` Xah Lee
0 siblings, 2 replies; 5+ messages in thread
From: Miles Bader @ 2009-03-19 0:54 UTC (permalink / raw)
To: help-gnu-emacs
Xah Lee <xahlee@gmail.com> writes:
> i haven't studied emacs's face, display property etc systems. But
> could anyone give me a rough guide on what function i should use or
> lookup?
`put-text-property'
In particular, you may want the :foreground attribute of the `face'
property.
E.g. try evaluating the following:
(put-text-property (point) (mark) 'face '(:foreground "green"))
However, note that if the buffer's mode uses font-lock, font-lock will
_override_ any `face' property you set (to be its own); in a font-lock'd
buffer, you can use the `font-lock-face' property instead:
(put-text-property (point) (mark) 'font-lock-face '(:foreground "green"))
-Miles
--
Cynic, n. A blackguard whose faulty vision sees things as they are, not as
they ought to be. Hence the custom among the Scythians of plucking out a
cynic's eyes to improve his vision.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: how to attach properties to a piece of text?
2009-03-19 0:54 ` Miles Bader
@ 2009-03-19 3:19 ` Xah Lee
2009-03-19 15:51 ` Drew Adams
2009-03-19 13:11 ` Xah Lee
1 sibling, 1 reply; 5+ messages in thread
From: Xah Lee @ 2009-03-19 3:19 UTC (permalink / raw)
To: help-gnu-emacs
On Mar 18, 5:54 pm, Miles Bader <mi...@gnu.org> wrote:
> Xah Lee <xah...@gmail.com> writes:
> > i haven't studied emacs's face, display property etc systems. But
> > could anyone give me a rough guide on what function i should use or
> > lookup?
>
> `put-text-property'
>
> In particular, you may want the :foreground attribute of the `face'
> property.
>
> E.g. try evaluating the following:
>
> (put-text-property (point) (mark) 'face '(:foreground "green"))
>
> However, note that if the buffer's mode uses font-lock, font-lock will
> _override_ any `face' property you set (to be its own); in a font-lock'd
> buffer, you can use the `font-lock-face' property instead:
>
> (put-text-property (point) (mark) 'font-lock-face '(:foreground "green"))
Thanks Miles.
Here's the code i did.
(defun convert-rgb-vector-to-hex (rgb)
"Convert color RGB from “[r g b]” notation to “\"rrggbb\"” notation.
Example:
(convert-rgb-vector-to-hex [0 1 0.5])
returns
\"00ff80\"
"
(mapconcat
(lambda (x)
(format "%02x" (round (* x 255.0)) ))
rgb ""))
(defun syntax-color-vector (start end)
"Make vectors <r,g,b> syntax colored "
(interactive "r")
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(while (search-forward-regexp
"< *\\([0-9.]+\\) *, *\\([0-9.]+\\) *, *\\([0-9.]+\\) *>"
nil t)
(put-text-property (match-beginning 0)
(match-end 0) 'face (list :background
(concat "#" (convert-rgb-vector-to-hex
(vector
(string-to-number (match-string 1))
(string-to-number (match-string 2))
(string-to-number (match-string 3))
)))
)))))
spent some 4 hours on this. Some 80% time is spend on the hex/decimal
conversion, digging into some details of emacs float, string, hex,
round, etc issues. (^_^)
Xah
∑ http://xahlee.org/
☄
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: how to attach properties to a piece of text?
2009-03-19 3:19 ` Xah Lee
@ 2009-03-19 15:51 ` Drew Adams
0 siblings, 0 replies; 5+ messages in thread
From: Drew Adams @ 2009-03-19 15:51 UTC (permalink / raw)
To: 'Xah Lee', help-gnu-emacs
> (defun convert-rgb-vector-to-hex (rgb)
> "Convert color RGB from "[r g b]" notation to "\"rrggbb\"" notation.
> Example: (convert-rgb-vector-to-hex [0 1 0.5]) returns \"00ff80\""
> (mapconcat (lambda (x) (format "%02x" (round (* x 255.0)) )) rgb ""))
>
> (defun syntax-color-vector (start end)
> "Make vectors <r,g,b> syntax colored "
> (interactive "r")
> (save-restriction
> (narrow-to-region start end)
> (goto-char (point-min))
> (while (search-forward-regexp
> "< *\\([0-9.]+\\) *, *\\([0-9.]+\\) *, *\\([0-9.]+\\) *>"
> nil t)
> (put-text-property (match-beginning 0)
> (match-end 0) 'face (list :background
> (concat "#" (convert-rgb-vector-to-hex
> (vector (string-to-number (match-string 1))
> (string-to-number (match-string 2))
> (string-to-number (match-string 3))))))))))
>
> spent some 4 hours on this. Some 80% time is spend on the hex/decimal
> conversion, digging into some details of emacs float, string, hex,
> round, etc issues. (^_^)
http://www.emacswiki.org/emacs/hexrgb.el (e.g. `hexrgb-rgb-to-hex')
But it's always rewarding to code something oneself - never a waste of time.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: how to attach properties to a piece of text?
2009-03-19 0:54 ` Miles Bader
2009-03-19 3:19 ` Xah Lee
@ 2009-03-19 13:11 ` Xah Lee
1 sibling, 0 replies; 5+ messages in thread
From: Xah Lee @ 2009-03-19 13:11 UTC (permalink / raw)
To: help-gnu-emacs
On Mar 18, 5:54 pm, Miles Bader <mi...@gnu.org> wrote:
> However, note that if the buffer's mode uses font-lock, font-lock will
> _override_ any `face' property you set (to be its own); in a font-lock'd
> buffer, you can use the `font-lock-face' property instead:
>
> (put-text-property (point) (mark) 'font-lock-face '(:foreground "green"))
Great advice. That just happened to me after i put the function into
the package and tried to test it. Thanks.
Xah
∑ http://xahlee.org/
☄
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2009-03-19 15:51 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-19 0:20 how to attach properties to a piece of text? Xah Lee
2009-03-19 0:54 ` Miles Bader
2009-03-19 3:19 ` Xah Lee
2009-03-19 15:51 ` Drew Adams
2009-03-19 13:11 ` Xah Lee
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).