help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Apply Emacs-Lisp `font-lock' rules to a string


From: Emanuel Berg
Subject: Re: Apply Emacs-Lisp `font-lock' rules to a string
Date: Mon, 24 Aug 2015 22:07:12 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

Alexander Shukaev <address@hidden> writes:

> Assume I have a string somewhere in Emacs-Lisp
> program. I want this string to be propertized in
> such a way that when I display this string
> somewhere, it should have identical highlighting as
> if it would have been typed in Emacs-Lisp major mode
> (with `font-lock').
>
> To expand on the problem, I have a string which
> contains regular expression, which I display in the
> echo area. It would be nice to apply standard
> highlighting to it. Is there any way to achieve this
> smoothly (preferably with one or a few calls to
> built-in functions)? Thanks.

Check out this Lisp I wrote some years ago. Be sure to
evaluate the test forms. If you are into colors, check
out this page as well:

    http://user.it.uu.se/~embe8573/cols/www/index.html

Keep it up!

;; This file: http://user.it.uu.se/~embe8573/cols/www/emacs-test-faces.el

(defun insert-colored-text (str color bright)
  "Insert STR at point, in COLOR, and sometimes BRIGHT."
  (interactive (list (read-string "string: ")
                     (read-string "color: ")
                     (y-or-n-p    "bright? ") ))
  (insert (propertize str 'font-lock-face
                      `(:weight ,(if bright 'bold 'normal) :foreground ,color) 
)))

;; use this to test
(when nil
  (progn
    (forward-line 1)
    (insert "The French flag is ")
    (insert-colored-text "blue, "   "blue"  t)
    (insert-colored-text "white, "  "white" t)
    (insert-colored-text "and red." "red"   nil) ) ; <- eval me

  )

(defun test-all-faces ()
  "Print a test strings in every color, normal and bright."
  (interactive)
  (forward-line 1)
  (let ((str "This is what it looks like"))
    (dolist (bold '(nil t))
      (dolist (color
               '("black" "red" "green" "yellow" "blue"
                 "magenta" "cyan" "white"))
        (insert-colored-text
         (format "%s in %s (that is %sbold)\n" str color
                 (if bold "" "not ")) color bold) ))))

;; test all colors
;; (test-all-faces) ; <- eval me

-- 
underground experts united
http://user.it.uu.se/~embe8573




reply via email to

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