[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to attach properties to a piece of text?

From: Xah Lee
Subject: Re: how to attach properties to a piece of text?
Date: Wed, 18 Mar 2009 20:19:06 -0700 (PDT)
User-agent: G2/1.0

On Mar 18, 5:54 pm, Miles Bader <address@hidden> wrote:
> Xah Lee <address@hidden> 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.
 (convert-rgb-vector-to-hex [0 1 0.5])
   (lambda (x)
     (format "%02x" (round (* x 255.0)) ))
   rgb ""))

(defun syntax-color-vector (start end)
  "Make vectors <r,g,b> syntax colored "
  (interactive "r")
    (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
              (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. (^_^)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]