3

I'm following the instruction at http://www.emacswiki.org/emacs/AllOut#toc3 to have the allout mode fontify its headers (enclosed below).

It works but the existing major mode's fontifying effort would be gone. Is it possible to have them both?

(defvar rf-allout-font-lock-keywords
  '(;;
    ;; Highlight headings according to the level.
    (eval . (list (concat "^\\(" outline-regexp "\\).+")
        0 '(or (cdr (assq (outline-depth)
                  '((1 . font-lock-function-name-face)
                    (2 . font-lock-variable-name-face)
                    (3 . font-lock-keyword-face)
                    (4 . font-lock-builtin-face)
                    (5 . font-lock-comment-face)
                    (6 . font-lock-constant-face)
                    (7 . font-lock-type-face)
                    (8 . font-lock-string-face))))
               font-lock-warning-face)
        nil t)))
  "Additional expressions to highlight in Outline mode.")

;; add font-lock to allout mode
(defun rf-allout-font-lock-hook ()
  (set (make-local-variable 'font-lock-defaults)
       '(rf-allout-font-lock-keywords t nil nil outline-back-to-current-heading)))

(add-hook 'outline-mode-hook 'rf-allout-font-lock-hook)
Scott Weldon
  • 2,695
  • 1
  • 18
  • 31
xpt
  • 467
  • 1
  • 4
  • 17

1 Answers1

3

The code on the web page is a bit outdated. You spotted that it replaces existing syntax highlighting. In addition, it uses functions (outline-depth) which doesn't seem to exist in modern Emacs versions. The code below reimplements this using font-lock-add-keywords. Also, it doesn't use the eval form, as it typically is only used in extreme situations where keywords can't be formed when the hook is called, fortunately, this isn't the case here.

(defun my-allout-mode-hook ()
  (font-lock-add-keywords
   nil
   `((,(concat "^\\(" outline-regexp "\\).+")
      (0 (or (cdr (assq (- (match-end 1) (match-beginning 1))
                        '((1 . font-lock-function-name-face)
                          (2 . font-lock-variable-name-face)
                          (3 . font-lock-keyword-face)
                          (4 . font-lock-builtin-face)
                          (5 . font-lock-comment-face)
                          (6 . font-lock-constant-face)
                          (7 . font-lock-type-face)
                          (8 . font-lock-string-face))))
             font-lock-warning-face)))))
  (if (fboundp 'font-lock-flush)
      (font-lock-flush)
    (when font-lock-mode
      (with-no-warnings
        (font-lock-fontify-buffer)))))

(add-hook 'allout-mode-hook 'my-allout-mode-hook)
Scott Weldon
  • 2,695
  • 1
  • 18
  • 31
Lindydancer
  • 6,150
  • 1
  • 15
  • 26