Search code examples
emacselispfont-lock

Remove all fontlock faces (except flyspell-incorrect flyspell-duplicate)


To shave off approximately .3 seconds from the run-time of a custom function that is set with a 2 second idle timer (which custom function combs through the buffer), I disable font-lock mode with (font-lock-mode -1). The side effect is that my flyspell-incorrect and flyspell-duplicate get removed. To put those flyspell fonts back again with (flyspell-region (window-start) (window-end)) costs a lot more than .3 seconds. So, I was thinking of a midway ground by removing all the font-lock fonts except flyspell-incorrect and flyspell-duplicate. Is there a way to add a condition to the following code snippet from font-core that will preserve the flyspell fonts, yet remove everything else?

;; Turn off Font Lock mode.

(unless mode
  ;; Remove `font-lock-face' as an alias for the `face' property.
  (set (make-local-variable 'char-property-alias-alist)
 (copy-tree char-property-alias-alist))
  (let ((elt (assq 'face char-property-alias-alist)))
    (when elt
(setcdr elt (remq 'font-lock-face (cdr elt)))
(when (null (cdr elt))
  (setq char-property-alias-alist
  (delq elt char-property-alias-alist))))))

Solution

  • To my surprise, it turned out that flyspell uses overlays, not font-lock. My problem occurred because I improperly used remove-overlays by failing to include a precise value that existed. Improper usage of remove-overlays removes the whole kitten-kaboodle in the region -- including flyspell overlays. The following related thread describes how to avoid this problem by ensuring that a correct value is always included when using remove-overlays if a value exists:

    https://stackoverflow.com/a/23773934/2112489

    Here is a helpful little function to see what is going on behind the scenes:

    (defun reveal-overlays (&optional beg end)
    (interactive)
      (let* (
          (beg (if beg beg (window-start)))
          (end (if end end (window-end))))
        (overlay-recenter end)
        (dolist (o (overlays-in beg end))
          (when o
            (message "%s\n%s\n" (overlay-properties o) o)))
        (switch-to-buffer "*Messages*")))