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: John Mastro
Subject: Re: Apply Emacs-Lisp `font-lock' rules to a string
Date: Mon, 24 Aug 2015 13:23:43 -0700

> 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').

This seems to work here:

(defun my-font-lock-string (string &optional mode)
  (let ((mode (or mode major-mode)))
    (with-temp-buffer
      (insert string)
      (funcall mode)
      (funcall font-lock-fontify-buffer-function)
      (buffer-string))))

(my-font-lock-string "\"\\\\(1\\\\|2\\\\)\"" #'emacs-lisp-mode)

> 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.

Hm, maybe not. When I do (message (my-font-lock-string ...)) it prints
the string readably rather than colorized. Hopefully someone else will
know how to get around that.

-- 
john



reply via email to

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