[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Font lock problem when changing major mode

From: Stephen Eglen
Subject: Font lock problem when changing major mode
Date: Thu, 21 Jun 2007 20:00:56 +0100

I've found a minor problem with font locking, when handling changes in
major mode.  I have a major mode, defined roughly below.  It is based
on comint-mode similar to comments in comint.el (around line 3500):

(defun inferior-ess-mode ()
  (setq major-mode 'inferior-ess-mode)
  ;; ...
  (setq font-lock-defaults
        '(inferior-ess-font-lock-keywords nil nil ((?' . "."))))

Importantly, the 2nd element of font-lock-defaults is set to nil, as I
want font-lock-keywords-only to be nil.  However,
font-lock-keywords-only is set to 't during comint-mode, due to this
code near the end of comint-mode:

  (setq font-lock-defaults '(nil t))
  (add-hook 'change-major-mode-hook 'font-lock-defontify nil t)

Even though font-lock-defontify is called in a buffer changing from
comint-mode to inferior-ess-mode, font-lock-set-defaults will not
update any font-lock local variables because the buffer-local flag
font-lock-set-defaults has been set.

The following addition (marked SJE) to font-lock-defontify solves my
immediate problem:

(defun font-lock-defontify ()
  "Clear out all `font-lock-face' properties in current buffer.
A major mode that uses `font-lock-face' properties might want to put
this function onto `change-major-mode-hook'."
  (let ((modp (buffer-modified-p))
        (inhibit-read-only t))
      (remove-list-of-text-properties (point-min) (point-max)
    ;; SJE: next two lines new.
    (kill-local-variable font-lock-set-defaults)
    (kill-local-variable font-lock-keywords-only)
    (restore-buffer-modified-p modp)))

However, this is not a complete solution (e.g. it doesn't handle
resetting font-lock-keywords-case-fold-search).  Just removing the
local var font-lock-set-defaults doesn't work because the function
font-lock-set-defaults changes font-lock-keywords-only (and
-case-fold-search) iff it is non-nil:

      (when (nth 1 defaults)
        (set (make-local-variable 'font-lock-keywords-only) t))

If these tests are changed to something like:

      (when (> (length defaults) 1)
        (set (make-local-variable 'font-lock-keywords-only) 
             (nth 1 defaults)))

then we only need to kill the local var font-lock-set-defaults 
in font-lock-defontify.  

Finally, since font-lock-defaults is now buffer local in Emacs 22, is
it worth removing redundant calls to make it buffer-local?  

  $ grep -r "make-local-variable 'font-lock-defaults" * | wc -l

reveals 80 files in lisp/.  Should those redundant calls be removed?

Rather than send a patch, I thought I'd check first this kind of
change is appropriate.  If it sounds okay, I can send a patch, or
update CVS directly.


In GNU Emacs 22.1.1 (i386-apple-darwin8.9.1, Carbon Version 1.6.0)
 of 2007-06-07 on cpc6-cmbg2-0-0-cust449.cmbg.cable.ntl.com
Windowing system distributor `Apple Inc.', version 10.4.9
configured using `configure  '--enable-carbon-app''

reply via email to

[Prev in Thread] Current Thread [Next in Thread]