all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: John Mastro <john.b.mastro@gmail.com>
To: help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: Re: Apply Emacs-Lisp `font-lock' rules to a string
Date: Mon, 24 Aug 2015 13:23:43 -0700	[thread overview]
Message-ID: <CAOj2CQS+13EsWVptxfvvD=Zo6JXSJFLrQA80OOEBN5VOsr=BYA@mail.gmail.com> (raw)
In-Reply-To: <CAKu-7WxvsH=FU0M-OEvbuxsEn-T72A=Fou2_SQfX8HFLtsAXNw@mail.gmail.com>

> 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



  parent reply	other threads:[~2015-08-24 20:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-24  3:19 Apply Emacs-Lisp `font-lock' rules to a string Alexander Shukaev
2015-08-24 20:07 ` Emanuel Berg
2015-08-24 20:23 ` John Mastro [this message]
2015-08-24 20:29   ` Alexander Shukaev
2015-08-24 20:39     ` John Mastro
2015-08-24 20:40       ` John Mastro
2015-08-25 22:38         ` Alexander Shukaev
2015-08-26  0:09           ` Alexander Shukaev

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='CAOj2CQS+13EsWVptxfvvD=Zo6JXSJFLrQA80OOEBN5VOsr=BYA@mail.gmail.com' \
    --to=john.b.mastro@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.