changeset 47360:c17030759a04

(define-derived-mode): Add keyword arguments. (derived-mode-make-docstring): Take abbrev and syntax table names.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Mon, 09 Sep 2002 23:55:56 +0000
parents 9da6d2e1b2ee
children 5e3418a37a64
files lisp/derived.el
diffstat 1 files changed, 52 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/derived.el	Mon Sep 09 23:22:49 2002 +0000
+++ b/lisp/derived.el	Mon Sep 09 23:55:56 2002 +0000
@@ -126,6 +126,17 @@
 BODY:      forms to execute just before running the
            hooks for the new mode.  Do not use `interactive' here.
 
+BODY can start with a bunch of keyword arguments.  The following keyword
+  arguments are currently understood:
+:group GROUP
+	Declare the customization group that corresponds to this mode.
+:syntax-table TABLE
+	Use TABLE instead of the default.
+	A nil value means to simply use the same syntax-table as the parent.
+:abbrev-table TABLE
+	Use TABLE instead of the default.
+	A nil value means to simply use the same abbrev-table as the parent.
+
 Here is how you could define LaTeX-Thesis mode as a variant of LaTeX mode:
 
   (define-derived-mode LaTeX-thesis-mode LaTeX-mode \"LaTeX-Thesis\")
@@ -155,15 +166,31 @@
   (let ((map (derived-mode-map-name child))
 	(syntax (derived-mode-syntax-table-name child))
 	(abbrev (derived-mode-abbrev-table-name child))
+	(declare-abbrev t)
+	(declare-syntax t)
 	(hook (derived-mode-hook-name child))
-	(docstring (derived-mode-make-docstring parent child docstring)))
+	(group nil))
+
+    ;; Process the keyword args.
+    (while (keywordp (car body))
+      (case (pop body)
+	(:group (setq group (pop body)))
+	(:abbrev-table (setq abbrev (pop body)) (setq declare-abbrev nil))
+	(:syntax-table (setq syntax (pop body)) (setq declare-syntax nil))))
+
+
+    (setq docstring (derived-mode-make-docstring
+		     parent child docstring syntax abbrev))
 
     `(progn
        (defvar ,map (make-sparse-keymap))
-       (defvar ,syntax (make-syntax-table))
-       (defvar ,abbrev
-	 (progn (define-abbrev-table ',abbrev nil) ,abbrev))
+       ,(if declare-syntax
+	    `(defvar ,syntax (make-syntax-table)))
+       ,(if declare-abbrev
+	    `(defvar ,abbrev
+	       (progn (define-abbrev-table ',abbrev nil) ,abbrev)))
        (put ',child 'derived-mode-parent ',parent)
+       ,(if group `(put ',child 'custom-group ,group))
 
        (defun ,child ()
 	 ,docstring
@@ -184,20 +211,25 @@
 					; Set up maps and tables.
 		(unless (keymap-parent ,map)
 		  (set-keymap-parent ,map (current-local-map)))
-		(let ((parent (char-table-parent ,syntax)))
-		  (unless (and parent (not (eq parent (standard-syntax-table))))
-		    (set-char-table-parent ,syntax (syntax-table))))
-		(when local-abbrev-table
-		  (mapatoms
-		   (lambda (symbol)
-		     (or (intern-soft (symbol-name symbol) ,abbrev)
-			 (define-abbrev ,abbrev (symbol-name symbol)
-			   (symbol-value symbol) (symbol-function symbol))))
-		   local-abbrev-table))))
+		,(when declare-syntax
+		   `(let ((parent (char-table-parent ,syntax)))
+		      (unless (and parent
+				   (not (eq parent (standard-syntax-table))))
+			(set-char-table-parent ,syntax (syntax-table)))))
+		,(when declare-abbrev
+		   `(when local-abbrev-table
+		      (mapatoms
+		       (lambda (symbol)
+			 (or (intern-soft (symbol-name symbol) ,abbrev)
+			     (define-abbrev ,abbrev
+			       (symbol-name symbol)
+			       (symbol-value symbol)
+			       (symbol-function symbol))))
+		       local-abbrev-table)))))
 
 	  (use-local-map ,map)
-	  (set-syntax-table ,syntax)
-	  (setq local-abbrev-table ,abbrev)
+	  ,(when syntax `(set-syntax-table ,syntax))
+	  ,(when abbrev `(setq local-abbrev-table ,abbrev))
 					; Splice in the body (if any).
 	  ,@body
 	  )
@@ -220,12 +252,11 @@
 
 ;;; PRIVATE
 
-(defun derived-mode-make-docstring (parent child &optional docstring)
+(defun derived-mode-make-docstring (parent child &optional
+					   docstring syntax abbrev)
   "Construct a docstring for a new mode if none is provided."
 
   (let ((map (derived-mode-map-name child))
-	(syntax (derived-mode-syntax-table-name child))
-	(abbrev (derived-mode-abbrev-table-name child))
 	(hook (derived-mode-hook-name child)))
 
     (unless (stringp docstring)
@@ -244,7 +275,7 @@
 		      parent map abbrev syntax parent))))
 
     (unless (string-match (regexp-quote (symbol-name hook)) docstring)
-      ;; Make sure the docstring mentions the mode's hook
+      ;; Make sure the docstring mentions the mode's hook.
       (setq docstring
 	    (concat docstring
 		    (if (null parent)
@@ -259,7 +290,7 @@
 		    ", as the final step\nduring initialization.")))
 
     (unless (string-match "\\\\[{[]" docstring)
-      ;; And don't forget to put the mode's keymap
+      ;; And don't forget to put the mode's keymap.
       (setq docstring (concat docstring "\n\n\\{" (symbol-name map) "}")))
 
     docstring))