LCOV - code coverage report
Current view: top level - lisp - font-core.el (source / functions) Hit Total Coverage
Test: tramp-tests.info Lines: 6 40 15.0 %
Date: 2017-08-27 09:44:50 Functions: 1 6 16.7 %

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

Generated by: LCOV version 1.12