font-core.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. ;;; font-core.el --- Core interface to font-lock
  2. ;; Copyright (C) 1992-2017 Free Software Foundation, Inc.
  3. ;; Maintainer: emacs-devel@gnu.org
  4. ;; Keywords: languages, faces
  5. ;; Package: emacs
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Code:
  18. ;; This variable is used by mode packages that support Font Lock mode by
  19. ;; defining their own keywords to use for `font-lock-keywords'. (The mode
  20. ;; command should make it buffer-local and set it to provide the set up.)
  21. (defvar font-lock-defaults nil
  22. "Defaults for Font Lock mode specified by the major mode.
  23. Defaults should be of the form:
  24. (KEYWORDS [KEYWORDS-ONLY [CASE-FOLD [SYNTAX-ALIST ...]]])
  25. KEYWORDS may be a symbol (a variable or function whose value is the keywords
  26. to use for fontification) or a list of symbols (specifying different levels
  27. of fontification).
  28. If KEYWORDS-ONLY is non-nil, syntactic fontification (strings and
  29. comments) is not performed.
  30. If CASE-FOLD is non-nil, the case of the keywords is ignored when fontifying.
  31. If SYNTAX-ALIST is non-nil, it should be a list of cons pairs of the form
  32. \(CHAR-OR-STRING . STRING) used to set the local Font Lock syntax table, for
  33. keyword and syntactic fontification (see `modify-syntax-entry').
  34. These item elements are used by Font Lock mode to set the variables
  35. `font-lock-keywords', `font-lock-keywords-only',
  36. `font-lock-keywords-case-fold-search', `font-lock-syntax-table'.
  37. Further item elements are alists of the form (VARIABLE . VALUE) and are in no
  38. particular order. Each VARIABLE is made buffer-local before set to VALUE.
  39. Currently, appropriate variables include `font-lock-mark-block-function'.
  40. If this is non-nil, it should be a function with no args used to mark any
  41. enclosing block of text, for fontification via \\[font-lock-fontify-block].
  42. Typical values are `mark-defun' for programming modes or `mark-paragraph' for
  43. textual modes (i.e., the mode-dependent function is known to put point and mark
  44. around a text block relevant to that mode).
  45. Other variables include that for syntactic keyword fontification,
  46. `font-lock-syntactic-keywords' and those for buffer-specialized fontification
  47. functions, `font-lock-fontify-buffer-function',
  48. `font-lock-unfontify-buffer-function', `font-lock-fontify-region-function',
  49. `font-lock-unfontify-region-function', and `font-lock-inhibit-thing-lock'.")
  50. ;;;###autoload
  51. (put 'font-lock-defaults 'risky-local-variable t)
  52. (make-variable-buffer-local 'font-lock-defaults)
  53. (defvar font-lock-function 'font-lock-default-function
  54. "A function which is called when `font-lock-mode' is toggled.
  55. It will be passed one argument, which is the current value of
  56. `font-lock-mode'.")
  57. ;; The mode for which font-lock was initialized, or nil if none.
  58. (defvar font-lock-major-mode)
  59. (define-minor-mode font-lock-mode
  60. "Toggle syntax highlighting in this buffer (Font Lock mode).
  61. With a prefix argument ARG, enable Font Lock mode if ARG is
  62. positive, and disable it otherwise. If called from Lisp, enable
  63. the mode if ARG is omitted or nil.
  64. When Font Lock mode is enabled, text is fontified as you type it:
  65. - Comments are displayed in `font-lock-comment-face';
  66. - Strings are displayed in `font-lock-string-face';
  67. - Certain other expressions are displayed in other faces
  68. according to the value of the variable `font-lock-keywords'.
  69. To customize the faces (colors, fonts, etc.) used by Font Lock for
  70. fontifying different parts of buffer text, use \\[customize-face].
  71. You can enable Font Lock mode in any major mode automatically by
  72. turning on in the major mode's hook. For example, put in your
  73. ~/.emacs:
  74. (add-hook \\='c-mode-hook \\='turn-on-font-lock)
  75. Alternatively, you can use Global Font Lock mode to automagically
  76. turn on Font Lock mode in buffers whose major mode supports it
  77. and whose major mode is one of `font-lock-global-modes'. For
  78. example, put in your ~/.emacs:
  79. (global-font-lock-mode t)
  80. Where major modes support different levels of fontification, you
  81. can use the variable `font-lock-maximum-decoration' to specify
  82. which level you generally prefer. When you turn Font Lock mode
  83. on/off the buffer is fontified/defontified, though fontification
  84. occurs only if the buffer is less than `font-lock-maximum-size'.
  85. To add your own highlighting for some major mode, and modify the
  86. highlighting selected automatically via the variable
  87. `font-lock-maximum-decoration', you can use
  88. `font-lock-add-keywords'.
  89. To fontify a buffer, without turning on Font Lock mode and
  90. regardless of buffer size, you can use \\[font-lock-fontify-buffer].
  91. To fontify a block (the function or paragraph containing point,
  92. or a number of lines around point), perhaps because modification
  93. on the current line caused syntactic change on other lines, you
  94. can use \\[font-lock-fontify-block].
  95. You can set your own default settings for some mode, by setting a
  96. buffer local value for `font-lock-defaults', via its mode hook.
  97. The above is the default behavior of `font-lock-mode'; you may
  98. specify your own function which is called when `font-lock-mode'
  99. is toggled via `font-lock-function'. "
  100. nil nil nil
  101. :after-hook (font-lock-initial-fontify)
  102. ;; Don't turn on Font Lock mode if we don't have a display (we're running a
  103. ;; batch job) or if the buffer is invisible (the name starts with a space).
  104. (when (or noninteractive (eq (aref (buffer-name) 0) ?\s))
  105. (setq font-lock-mode nil))
  106. (funcall font-lock-function font-lock-mode)
  107. ;; Arrange to unfontify this buffer if we change major mode later.
  108. (if font-lock-mode
  109. (add-hook 'change-major-mode-hook 'font-lock-change-mode nil t)
  110. (remove-hook 'change-major-mode-hook 'font-lock-change-mode t)))
  111. ;; Get rid of fontification for the old major mode.
  112. ;; We do this when changing major modes.
  113. (defun font-lock-change-mode ()
  114. (font-lock-mode -1))
  115. (defun font-lock-defontify ()
  116. "Clear out all `font-lock-face' properties in current buffer.
  117. A major mode that uses `font-lock-face' properties might want to put
  118. this function onto `change-major-mode-hook'."
  119. (let ((modp (buffer-modified-p))
  120. (inhibit-read-only t))
  121. (save-restriction
  122. (widen)
  123. (remove-list-of-text-properties (point-min) (point-max)
  124. '(font-lock-face)))
  125. (restore-buffer-modified-p modp)))
  126. (defvar font-lock-set-defaults)
  127. (defun font-lock-default-function (mode)
  128. ;; Turn on Font Lock mode.
  129. (when mode
  130. (set (make-local-variable 'char-property-alias-alist)
  131. (copy-tree char-property-alias-alist))
  132. ;; Add `font-lock-face' as an alias for the `face' property.
  133. (let ((elt (assq 'face char-property-alias-alist)))
  134. (if elt
  135. (unless (memq 'font-lock-face (cdr elt))
  136. (setcdr elt (nconc (cdr elt) (list 'font-lock-face))))
  137. (push (list 'face 'font-lock-face) char-property-alias-alist))))
  138. ;; Turn off Font Lock mode.
  139. (unless mode
  140. ;; Remove `font-lock-face' as an alias for the `face' property.
  141. (set (make-local-variable 'char-property-alias-alist)
  142. (copy-tree char-property-alias-alist))
  143. (let ((elt (assq 'face char-property-alias-alist)))
  144. (when elt
  145. (setcdr elt (remq 'font-lock-face (cdr elt)))
  146. (when (null (cdr elt))
  147. (setq char-property-alias-alist
  148. (delq elt char-property-alias-alist))))))
  149. ;; Only do hard work if the mode has specified stuff in
  150. ;; `font-lock-defaults'.
  151. (when (font-lock-specified-p mode)
  152. (font-lock-mode-internal mode)))
  153. (defun turn-on-font-lock ()
  154. "Turn on Font Lock mode (only if the terminal can display it)."
  155. (unless font-lock-mode
  156. (font-lock-mode)))
  157. ;;; Global Font Lock mode.
  158. ;; A few people have hassled in the past for a way to make it easier to turn on
  159. ;; Font Lock mode, without the user needing to know for which modes s/he has to
  160. ;; turn it on, perhaps the same way hilit19.el/hl319.el does. I've always
  161. ;; balked at that way, as I see it as just re-molding the same problem in
  162. ;; another form. That is; some person would still have to keep track of which
  163. ;; modes (which may not even be distributed with Emacs) support Font Lock mode.
  164. ;; The list would always be out of date. And that person might have to be me.
  165. ;; Implementation.
  166. ;;
  167. ;; In a previous discussion the following hack came to mind. It is a gross
  168. ;; hack, but it generally works. We use the convention that major modes start
  169. ;; by calling the function `kill-all-local-variables', which in turn runs
  170. ;; functions on the hook variable `change-major-mode-hook'. We attach our
  171. ;; function `font-lock-change-major-mode' to that hook. Of course, when this
  172. ;; hook is run, the major mode is in the process of being changed and we do not
  173. ;; know what the final major mode will be. So, `font-lock-change-major-mode'
  174. ;; only (a) notes the name of the current buffer, and (b) adds our function
  175. ;; `turn-on-font-lock-if-desired' to the hook variables
  176. ;; `after-change-major-mode-hook' and `post-command-hook' (for modes
  177. ;; that do not yet run `after-change-major-mode-hook'). By the time
  178. ;; the functions on the first of these hooks to be run are run, the new major
  179. ;; mode is assumed to be in place. This way we get a Font Lock function run
  180. ;; when a major mode is turned on, without knowing major modes or their hooks.
  181. ;;
  182. ;; Naturally this requires that major modes run `kill-all-local-variables'
  183. ;; and `after-change-major-mode-hook', as they are supposed to. For modes
  184. ;; that do not run `after-change-major-mode-hook' yet, `post-command-hook'
  185. ;; takes care of things if the mode is set directly or indirectly by
  186. ;; an interactive command; however, problems can occur if the mode is
  187. ;; set by a timer or process: in that case, proper handling of Font Lock mode
  188. ;; may be delayed until the next interactive command.
  189. ;; User interface.
  190. ;;
  191. ;; Although Global Font Lock mode is a pseudo-mode, I think that the user
  192. ;; interface should conform to the usual Emacs convention for modes, i.e., a
  193. ;; command to toggle the feature (`global-font-lock-mode') with a variable for
  194. ;; finer control of the mode's behavior (`font-lock-global-modes').
  195. ;;
  196. ;; The feature should not be enabled by loading font-lock.el, since other
  197. ;; mechanisms for turning on Font Lock mode, such as M-x font-lock-mode RET or
  198. ;; (add-hook 'c-mode-hook 'turn-on-font-lock), would cause Font Lock mode to be
  199. ;; turned on everywhere. That would not be intuitive or informative because
  200. ;; loading a file tells you nothing about the feature or how to control it. It
  201. ;; would also be contrary to the Principle of Least Surprise. sm.
  202. (defcustom font-lock-global-modes t
  203. "Modes for which Font Lock mode is automagically turned on.
  204. Global Font Lock mode is controlled by the command `global-font-lock-mode'.
  205. If nil, means no modes have Font Lock mode automatically turned on.
  206. If t, all modes that support Font Lock mode have it automatically turned on.
  207. If a list, it should be a list of `major-mode' symbol names for which Font Lock
  208. mode should be automatically turned on. The sense of the list is negated if it
  209. begins with `not'. For example:
  210. (c-mode c++-mode)
  211. means that Font Lock mode is turned on for buffers in C and C++ modes only."
  212. :type '(choice (const :tag "none" nil)
  213. (const :tag "all" t)
  214. (set :menu-tag "mode specific" :tag "modes"
  215. :value (not)
  216. (const :tag "Except" not)
  217. (repeat :inline t (symbol :tag "mode"))))
  218. :group 'font-lock)
  219. (defun turn-on-font-lock-if-desired ()
  220. (when (cond ((eq font-lock-global-modes t)
  221. t)
  222. ((eq (car-safe font-lock-global-modes) 'not)
  223. (not (memq major-mode (cdr font-lock-global-modes))))
  224. (t (memq major-mode font-lock-global-modes)))
  225. (let (inhibit-quit)
  226. (turn-on-font-lock))))
  227. (define-globalized-minor-mode global-font-lock-mode
  228. font-lock-mode turn-on-font-lock-if-desired
  229. ;; What was this :extra-args thingy for? --Stef
  230. ;; :extra-args (dummy)
  231. :initialize 'custom-initialize-delay
  232. :init-value (not (or noninteractive emacs-basic-display))
  233. :group 'font-lock
  234. :version "22.1")
  235. ;;; End of Global Font Lock mode.
  236. (provide 'font-core)
  237. ;;; font-core.el ends here