Suppose I have a python-mode user buffer with content import os, I can use (buffer-substring (point-min) (point-max)) to get a colorized string
#("import os" 0 6
(fontified t face font-lock-keyword-face)
6 9
(fontified t))
But now I want to generate this using emacs lisp code like
(setq string
(with-temp-buffer
(python-mode)
(font-lock-mode 1)
(insert "import os")
(buffer-substring (point-min) (point-max))))
but got result
#("import os" 0 9 (fontified nil))
So my question is how can I generate the same string just as I substring it from a truly existed user buffer?
Thanks in advance.
(font-lock-fontify-region (point-min) (point-max))usually does the trick. Note thatfont-lock-fontify-bufferdoesn't fontify the entire buffer. – Lindydancer Apr 04 '17 at 17:52font-lock-fontify-*function! – Saddle Point Apr 05 '17 at 01:35