emacs-devel
[Top][All Lists]
Advanced

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

[PATCH] font-core: add font-lock-{enable, disable}-global-for functions


From: Michal Nazarewicz
Subject: [PATCH] font-core: add font-lock-{enable, disable}-global-for functions
Date: Sat, 3 Jan 2015 16:51:40 +0100

From: Michal Nazarewicz <address@hidden>

* lisp/font-core.el (font-lock-enable-global-for)
(font-lock-disable-global-for): New functions for easy manipulation
of `font-lock-global-modes' variable.

* test/automated/font-core.el (font-lock-test-disable-global-for)
(font-lock-test-enable-global-for): New tests for aforementioned
functions.

* lisp/speedbar.el: Use `font-lock-disable-global-for' instead of
implementing the same functionality.
---
 lisp/font-core.el           | 41 ++++++++++++++++++++++++++++++
 lisp/speedbar.el            | 10 ++------
 test/automated/font-core.el | 62 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+), 8 deletions(-)
 create mode 100644 test/automated/font-core.el

diff --git a/lisp/font-core.el b/lisp/font-core.el
index 5dd6ad3..667d650 100644
--- a/lisp/font-core.el
+++ b/lisp/font-core.el
@@ -276,6 +276,47 @@ means that Font Lock mode is turned on for buffers in C 
and C++ modes only."
     (let (inhibit-quit)
       (turn-on-font-lock))))
 
+;;;###autoload
+(defun font-lock-disable-global-for (mode)
+  "Disable `global-font-lock-mode' for MODE major mode.
+Modify `font-lock-global-modes' variable so that
+`global-font-lock-mode' is disabled for MODE.
+
+If `font-lock-global-modes' is a '(not …) list, make sure MODE is
+on the list; if it's a list, make sure MODE is not on it; if it's
+nil, do nothing; otherwise changes the variable to '(not MODE)."
+  (let ((modes font-lock-global-modes))
+    (cond
+     ;; t, change to '(not MODE) to disable for MODE
+     ((not (listp modes))
+      (setq font-lock-global-modes (list 'not mode)))
+     ;; (not …), add MODE to the list to disable in MODE
+     ((eq (car modes) 'not)
+      (unless (memq mode (cdr modes))
+        (setcdr modes (cons mode (cdr modes)))))
+     ;; list of modes (may be nil), make sure MODE is not on it
+     ((setq font-lock-global-modes (delq mode modes))))))
+
+;;;###autoload
+(defun font-lock-enable-global-for (mode)
+  "Enable `global-font-lock-mode' for MODE major mode.
+Modify `font-lock-global-modes' variable so that
+`global-font-lock-mode' is enabled for MODE.
+
+If `font-lock-global-modes' is a '(not …) list, make sure MODE is
+not on the list; if it's a list, make sure MODE is on it; if it's
+nil, change the variable to '(MODE) list; otherwise do nothing."
+  (let ((modes font-lock-global-modes))
+    (cond
+     ;; t, nothing to do, already enabled for all modes
+     ((not (listp modes)))
+     ;; (not …), make sure MODE is not on the list
+     ((eq (car modes) 'not)
+      (unless (setcdr modes (delq mode (cdr modes)))
+        (setq font-lock-global-modes t)))
+     ;; list of modes (may be nil), make sure MODE is on it
+     ((add-to-list 'font-lock-global-modes mode)))))
+
 (define-globalized-minor-mode global-font-lock-mode
   font-lock-mode turn-on-font-lock-if-desired
   ;; What was this :extra-args thingy for?  --Stef
diff --git a/lisp/speedbar.el b/lisp/speedbar.el
index 2989274..3c980f7 100644
--- a/lisp/speedbar.el
+++ b/lisp/speedbar.el
@@ -4068,14 +4068,8 @@ TEXT is the buffer's name, TOKEN and INDENT are unused."
            (def-edebug-spec speedbar-with-writable def-body)))
 
 ;; Fix a font lock problem for some versions of Emacs
-(and (boundp 'font-lock-global-modes)
-     font-lock-global-modes
-     (if (eq font-lock-global-modes t)
-        (setq font-lock-global-modes '(not speedbar-mode))
-       (if (eq (car font-lock-global-modes) 'not)
-          (add-to-list 'font-lock-global-modes 'speedbar-mode t)
-        (setq font-lock-global-modes (delq 'speedbar-mode
-                                           font-lock-global-modes)))))
+(when (fboundp 'font-lock-disable-global-for)
+  (font-lock-disable-global-for 'speedbar-mode))
 
 ;;; Obsolete variables and functions
 
diff --git a/test/automated/font-core.el b/test/automated/font-core.el
new file mode 100644
index 0000000..647488b
--- /dev/null
+++ b/test/automated/font-core.el
@@ -0,0 +1,62 @@
+;;; font-core.el --- ERT tests for font-core.el -*- lexical-binding: t -*-
+
+;; Copyright (C) 2015 Free Software Foundation, Inc.
+
+;; Author: Michal Nazarewicz <address@hidden>
+;; Keywords: languages, faces
+;; Package: emacs
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; This package defines regression tests for the font-core package.
+
+;;; Code:
+
+(require 'ert)
+(require 'font-core)
+
+(defun font-lock-test-global-modes (func expected-pairs)
+  (dolist (pair expected-pairs)
+    (let ((font-lock-global-modes (car pair)))
+      (funcall func 'foo-mode)
+      (should (equal (cdr pair) font-lock-global-modes)))))
+
+(ert-deftest font-lock-test-disable-global-for ()
+  "Test `font-lock-disable-global-for' function"
+  (font-lock-test-global-modes 'font-lock-disable-global-for
+                               '((nil . nil)
+                                 (t . (not foo-mode))
+                                 ((not bar-mode) . (not foo-mode bar-mode))
+                                 ((not foo-mode) . (not foo-mode))
+                                 ((foo-mode) . nil)
+                                 ((foo-mode bar-mode) . (bar-mode)))))
+
+(ert-deftest font-lock-test-enable-global-for ()
+  "Test `font-lock-enable-global-for' function"
+  (font-lock-test-global-modes 'font-lock-enable-global-for
+                               '((nil . (foo-mode))
+                                 (t . t)
+                                 ((not bar-mode) . (not bar-mode))
+                                 ((not foo-mode bar-mode) . (not bar-mode))
+                                 ((not foo-mode) . t)
+                                 ((foo-mode) . (foo-mode))
+                                 ((bar-mode) . (foo-mode bar-mode)))))
+
+(provide 'font-core-tests)
+
+;;; font-core.el ends here
-- 
2.2.0.rc0.207.ga3a616c




reply via email to

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