From: Xah Lee <xahlee@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: how to attach properties to a piece of text?
Date: Wed, 18 Mar 2009 20:19:06 -0700 (PDT) [thread overview]
Message-ID: <3189ffdc-6fb6-4723-89a8-d87a78407aad@q30g2000prq.googlegroups.com> (raw)
In-Reply-To: 877i2mgwmr.fsf@catnip.gol.com
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/
☄
next prev parent reply other threads:[~2009-03-19 3:19 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2009-03-19 15:51 ` Drew Adams
2009-03-19 13:11 ` Xah Lee
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3189ffdc-6fb6-4723-89a8-d87a78407aad@q30g2000prq.googlegroups.com \
--to=xahlee@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.