* Apply Emacs-Lisp `font-lock' rules to a string
@ 2015-08-24 3:19 Alexander Shukaev
2015-08-24 20:07 ` Emanuel Berg
2015-08-24 20:23 ` John Mastro
0 siblings, 2 replies; 8+ messages in thread
From: Alexander Shukaev @ 2015-08-24 3:19 UTC (permalink / raw)
To: help-gnu-emacs
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.
Regards,
Alexander
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Apply Emacs-Lisp `font-lock' rules to a string
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
1 sibling, 0 replies; 8+ messages in thread
From: Emanuel Berg @ 2015-08-24 20:07 UTC (permalink / raw)
To: help-gnu-emacs
Alexander Shukaev <haroogan@gmail.com> 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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Apply Emacs-Lisp `font-lock' rules to a string
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
2015-08-24 20:29 ` Alexander Shukaev
1 sibling, 1 reply; 8+ messages in thread
From: John Mastro @ 2015-08-24 20:23 UTC (permalink / raw)
To: help-gnu-emacs
> 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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Apply Emacs-Lisp `font-lock' rules to a string
2015-08-24 20:23 ` John Mastro
@ 2015-08-24 20:29 ` Alexander Shukaev
2015-08-24 20:39 ` John Mastro
0 siblings, 1 reply; 8+ messages in thread
From: Alexander Shukaev @ 2015-08-24 20:29 UTC (permalink / raw)
To: John Mastro; +Cc: help-gnu-emacs
> 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)
Nice!
>> 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.
There surely must be one as I see that when cursor hovers different
structures of Emacs Lisp code, echo area displays short summary of the
corresponding structures, and that summary is obviously highlighted
according to `font-lock' rules.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Apply Emacs-Lisp `font-lock' rules to a string
2015-08-24 20:29 ` Alexander Shukaev
@ 2015-08-24 20:39 ` John Mastro
2015-08-24 20:40 ` John Mastro
0 siblings, 1 reply; 8+ messages in thread
From: John Mastro @ 2015-08-24 20:39 UTC (permalink / raw)
To: help-gnu-emacs@gnu.org; +Cc: Alexander Shukaev
>> (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)
>>
>> 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.
>
> There surely must be one as I see that when cursor hovers different
> structures of Emacs Lisp code, echo area displays short summary of the
> corresponding structures, and that summary is obviously highlighted
> according to `font-lock' rules.
I may have found it. Try this:
(minibuffer-message
(my-font-lock-string #'emacs-lisp-mode "\"\\\\(1\\\\|2\\\\)\""))
Appears to work as intended, though hopefully someone will still
comment on whether it's the best way to do it.
--
john
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Apply Emacs-Lisp `font-lock' rules to a string
2015-08-24 20:39 ` John Mastro
@ 2015-08-24 20:40 ` John Mastro
2015-08-25 22:38 ` Alexander Shukaev
0 siblings, 1 reply; 8+ messages in thread
From: John Mastro @ 2015-08-24 20:40 UTC (permalink / raw)
To: help-gnu-emacs@gnu.org; +Cc: Alexander Shukaev
> (minibuffer-message
> (my-font-lock-string #'emacs-lisp-mode "\"\\\\(1\\\\|2\\\\)\""))
Oops, got the arguments backward there:
(minibuffer-message
(my-font-lock-string "\"\\\\(1\\\\|2\\\\)\"" #'emacs-lisp-mode))
--
john
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Apply Emacs-Lisp `font-lock' rules to a string
2015-08-24 20:40 ` John Mastro
@ 2015-08-25 22:38 ` Alexander Shukaev
2015-08-26 0:09 ` Alexander Shukaev
0 siblings, 1 reply; 8+ messages in thread
From: Alexander Shukaev @ 2015-08-25 22:38 UTC (permalink / raw)
To: John Mastro; +Cc: help-gnu-emacs@gnu.org
>> (minibuffer-message
>> (my-font-lock-string #'emacs-lisp-mode "\"\\\\(1\\\\|2\\\\)\""))
>
> Oops, got the arguments backward there:
>
> (minibuffer-message
> (my-font-lock-string "\"\\\\(1\\\\|2\\\\)\"" #'emacs-lisp-mode))
Cool! Thanks, John. I just want to point out that plain `message'
also works fine if you do it like this (which is recommended anyway,
even in the documentation):
(message "%s" (my-font-lock-string ...))
Regards,
Alexander
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Apply Emacs-Lisp `font-lock' rules to a string
2015-08-25 22:38 ` Alexander Shukaev
@ 2015-08-26 0:09 ` Alexander Shukaev
0 siblings, 0 replies; 8+ messages in thread
From: Alexander Shukaev @ 2015-08-26 0:09 UTC (permalink / raw)
To: John Mastro; +Cc: help-gnu-emacs@gnu.org
>>> (minibuffer-message
>>> (my-font-lock-string #'emacs-lisp-mode "\"\\\\(1\\\\|2\\\\)\""))
>>
>> Oops, got the arguments backward there:
>>
>> (minibuffer-message
>> (my-font-lock-string "\"\\\\(1\\\\|2\\\\)\"" #'emacs-lisp-mode))
>
> Cool! Thanks, John. I just want to point out that plain `message'
> also works fine if you do it like this (which is recommended anyway,
> even in the documentation):
>
> (message "%s" (my-font-lock-string ...))
Nevertheless, I still can't find a way to make
(user-error "%s" (my-font-lock-string ...))
display colored output. Any suggestions?
Regards,
Alexander
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2015-08-26 0:09 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).