From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Emanuel Berg Newsgroups: gmane.emacs.help Subject: Re: Apply Emacs-Lisp `font-lock' rules to a string Date: Mon, 24 Aug 2015 22:07:12 +0200 Message-ID: <87a8tglabz.fsf@nl106-137-147.student.uu.se> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1440446974 19282 80.91.229.3 (24 Aug 2015 20:09:34 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 24 Aug 2015 20:09:34 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Aug 24 22:09:24 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZTy3X-0004fJ-33 for geh-help-gnu-emacs@m.gmane.org; Mon, 24 Aug 2015 22:09:23 +0200 Original-Received: from localhost ([::1]:56518 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZTy3W-0000jU-73 for geh-help-gnu-emacs@m.gmane.org; Mon, 24 Aug 2015 16:09:22 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58771) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZTy3I-0000j1-9L for help-gnu-emacs@gnu.org; Mon, 24 Aug 2015 16:09:12 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZTy3E-0002XR-6J for help-gnu-emacs@gnu.org; Mon, 24 Aug 2015 16:09:08 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:48407) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZTy3D-0002XB-V7 for help-gnu-emacs@gnu.org; Mon, 24 Aug 2015 16:09:04 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1ZTy3C-0004Q6-CO for help-gnu-emacs@gnu.org; Mon, 24 Aug 2015 22:09:02 +0200 Original-Received: from nl106-137-190.student.uu.se ([130.243.137.190]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 24 Aug 2015 22:09:02 +0200 Original-Received: from embe8573 by nl106-137-190.student.uu.se with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 24 Aug 2015 22:09:02 +0200 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: help-gnu-emacs@gnu.org Original-Lines: 64 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: nl106-137-190.student.uu.se Mail-Copies-To: never User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:sS7Kq8oSdbo7YcQkNnXXM3IHM58= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:106813 Archived-At: Alexander Shukaev 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