]> code.delx.au - gnu-emacs/blobdiff - lisp/progmodes/meta-mode.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / progmodes / meta-mode.el
index a2fd9cdab044e17d08a5df8871926fa9f57f7dab..b36104bf49b1d94c3be919646a1e32fc98a709a1 100644 (file)
@@ -1,7 +1,6 @@
 ;;; meta-mode.el --- major mode for editing Metafont or MetaPost sources
 
-;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007
-;; Free Software Foundation, Inc.
+;; Copyright (C) 1997, 2001-2011  Free Software Foundation, Inc.
 
 ;; Author: Ulrik Vieth <vieth@thphy.uni-duesseldorf.de>
 ;; Version: 1.0
@@ -9,10 +8,10 @@
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; 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, or (at your option)
-;; any later version.
+;; 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
@@ -20,9 +19,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
@@ -51,7 +48,7 @@
 ;; these lines to your startup file:
 ;;
 ;;  (add-hook 'meta-mode-load-hook
-;;            '(lambda () (require 'meta-buf)))
+;;            (lambda () (require 'meta-buf)))
 ;;
 ;; The add-on package loaded this way may in turn make use of the
 ;; mode-hooks provided in this package to activate additional features
 ;;
 ;; This package was begun on February 1, 1997, exactly 20 years after
 ;; the genesis of TeX took place according to Don Knuth's own account
-;; (cf. ``The Errors of TeX'', reprinted in ``Literate Programming'',
+;; (cf.  ``The Errors of TeX'', reprinted in ``Literate Programming'',
 ;; Chapter 10, p. 249).  What better date could there be to choose?
 ;;
 
         (list
          ;; embedded TeX code in btex ... etex
          (cons (concat "\\(btex\\|verbatimtex\\)"
-                       "[ \t]+\\(.*\\)[ \t]+"
+                       "[ \t\f]+\\(.*\\)[ \t\f]+"
                        "\\(etex\\)")
                '((1 font-lock-keyword-face)
                  (2 font-lock-string-face)
                  (3 font-lock-keyword-face)))
          ;; unary macro definitions: def, vardef, let
          (cons (concat "\\<" macro-keywords-1 "\\>"
-                       "[ \t]+\\(\\sw+\\|\\s_+\\|\\s.+\\)")
+                       "[ \t\f]+\\(\\sw+\\|\\s_+\\|\\s.+\\)")
                '((1 font-lock-keyword-face)
                  (2 font-lock-function-name-face)))
          ;; binary macro defintions: <leveldef> x operator y
          (cons (concat "\\<" macro-keywords-2 "\\>"
-                       "[ \t]+\\(\\sw+\\)"
-                       "[ \t]*\\(\\sw+\\|\\s.+\\)"
-                       "[ \t]*\\(\\sw+\\)")
+                       "[ \t\f]+\\(\\sw+\\)"
+                       "[ \t\f]*\\(\\sw+\\|\\s.+\\)"
+                       "[ \t\f]*\\(\\sw+\\)")
                '((1 font-lock-keyword-face)
                  (2 font-lock-variable-name-face nil t)
                  (3 font-lock-function-name-face nil t)
                  (4 font-lock-variable-name-face nil t)))
          ;; variable declarations: numeric, pair, color, ...
          (cons (concat "\\<" type-keywords "\\>"
-                       "\\([ \t]+\\(\\sw+\\)\\)*")
+                       "\\([ \t\f]+\\(\\sw+\\)\\)*")
                '((1 font-lock-type-face)
                  (font-lock-match-meta-declaration-item-and-skip-to-next
                   (goto-char (match-end 1)) nil
                   (1 font-lock-variable-name-face nil t))))
          ;; argument declarations: expr, suffix, text, ...
          (cons (concat "\\<" args-keywords "\\>"
-                       "\\([ \t]+\\(\\sw+\\|\\s_+\\)\\)*")
+                       "\\([ \t\f]+\\(\\sw+\\|\\s_+\\)\\)*")
                '((1 font-lock-type-face)
                  (font-lock-match-meta-declaration-item-and-skip-to-next
                   (goto-char (match-end 1)) nil
                   (1 font-lock-variable-name-face nil t))))
          ;; special case of arguments: expr x of y
-         (cons (concat "\\(expr\\)[ \t]+\\(\\sw+\\)"
-                       "[ \t]+\\(of\\)[ \t]+\\(\\sw+\\)")
+         (cons (concat "\\(expr\\)[ \t\f]+\\(\\sw+\\)"
+                       "[ \t\f]+\\(of\\)[ \t\f]+\\(\\sw+\\)")
                '((1 font-lock-type-face)
                  (2 font-lock-variable-name-face)
                  (3 font-lock-keyword-face nil t)
                'font-lock-keyword-face)
          ;; input, generate
          (cons (concat "\\<" input-keywords "\\>"
-                       "[ \t]+\\(\\sw+\\)")
+                       "[ \t\f]+\\(\\sw+\\)")
                '((1 font-lock-keyword-face)
                  (2 font-lock-constant-face)))
          ;; embedded Metafont/MetaPost code in comments
   ;; `forward-sexp'.  The list of items is expected to be separated
   ;; by commas and terminated by semicolons or equals signs.
   ;;
-  (if (looking-at "[ \t]*\\(\\sw+\\|\\s_+\\)")
+  (if (looking-at "[ \t\f]*\\(\\sw+\\|\\s_+\\)")
       (save-match-data
         (condition-case nil
             (save-restriction
               (narrow-to-region (point-min) limit)
               (goto-char (match-end 1))
               ;; Move over any item value, etc., to the next item.
-              (while (not (looking-at "[ \t]*\\(\\(,\\)\\|;\\|=\\|$\\)"))
+              (while (not (looking-at "[ \t\f]*\\(\\(,\\)\\|;\\|=\\|$\\)"))
                 (goto-char (or (scan-sexps (point) 1) (point-max))))
               (goto-char (match-end 2)))
           (error t)))))
@@ -476,6 +473,7 @@ If the list was changed, sort the list and remove duplicates first."
 
 (defun meta-complete-symbol ()
   "Perform completion on Metafont or MetaPost symbol preceding point."
+  ;; FIXME: Use completion-at-point-functions.
   (interactive "*")
   (let ((list meta-complete-list)
         entry)
@@ -489,29 +487,12 @@ If the list was changed, sort the list and remove duplicates first."
                (close (nth 3 entry))
                (begin (match-beginning sub))
                (end (match-end sub))
-               (pattern (meta-match-buffer 0))
-               (symbol (buffer-substring begin end))
-               (list (funcall (nth 2 entry)))
-               (completion (try-completion symbol list)))
-          (cond ((eq completion t)
-                 (and close
-                      (not (looking-at (regexp-quote close)))
-                      (insert close)))
-                ((null completion)
-                 (error "Can't find completion for \"%s\"" pattern))
-                ((not (string-equal symbol completion))
-                 (delete-region begin end)
-                 (insert completion)
-                 (and close
-                      (eq (try-completion completion list) t)
-                      (not (looking-at (regexp-quote close)))
-                      (insert close)))
-                (t
-                 (message "Making completion list...")
-                 (let ((list (all-completions symbol list nil)))
-                   (with-output-to-temp-buffer "*Completions*"
-                     (display-completion-list list symbol)))
-                 (message "Making completion list... done"))))
+               (list (funcall (nth 2 entry))))
+          (completion-in-region
+           begin end
+           (if (zerop (length close)) list
+             (apply-partially 'completion-table-with-terminator
+                              close list))))
       (funcall (nth 1 entry)))))
 
 
@@ -536,24 +517,24 @@ If the list was changed, sort the list and remove duplicates first."
 ;;; Indentation.
 
 (defcustom meta-indent-level 2
-  "*Indentation of begin-end blocks in Metafont or MetaPost mode."
+  "Indentation of begin-end blocks in Metafont or MetaPost mode."
   :type 'integer
   :group 'meta-font)
 
 
 (defcustom meta-left-comment-regexp "%%+"
-  "*Regexp matching comments that should be placed on the left margin."
+  "Regexp matching comments that should be placed on the left margin."
   :type 'regexp
   :group 'meta-font)
 
 (defcustom meta-right-comment-regexp nil
-  "*Regexp matching comments that should be placed to the right margin."
+  "Regexp matching comments that should be placed to the right margin."
   :type '(choice regexp
                 (const :tag "None" nil))
   :group 'meta-font)
 
 (defcustom meta-ignore-comment-regexp "%[^%]"
-  "*Regexp matching comments that whose indentation should not be touched."
+  "Regexp matching comments that whose indentation should not be touched."
   :type 'regexp
   :group 'meta-font)
 
@@ -562,21 +543,21 @@ If the list was changed, sort the list and remove duplicates first."
   (concat "\\(begin\\(char\\|fig\\|gr\\(aph\\|oup\\)\\|logochar\\)\\|"
           "def\\|for\\(\\|ever\\|suffixes\\)\\|if\\|mode_def\\|"
           "primarydef\\|secondarydef\\|tertiarydef\\|vardef\\)")
-  "*Regexp matching the beginning of environments to be indented."
+  "Regexp matching the beginning of environments to be indented."
   :type 'regexp
   :group 'meta-font)
 
 (defcustom meta-end-environment-regexp
   (concat "\\(end\\(char\\|def\\|f\\(ig\\|or\\)\\|gr\\(aph\\|oup\\)\\)"
           "\\|fi\\)")
-  "*Regexp matching the end of environments to be indented."
+  "Regexp matching the end of environments to be indented."
   :type 'regexp
   :group 'meta-font)
 
 (defcustom meta-within-environment-regexp
 ; (concat "\\(e\\(lse\\(\\|if\\)\\|xit\\(if\\|unless\\)\\)\\)")
   (concat "\\(else\\(\\|if\\)\\)")
-  "*Regexp matching keywords within environments not to be indented."
+  "Regexp matching keywords within environments not to be indented."
   :type 'regexp
   :group 'meta-font)
 
@@ -586,7 +567,7 @@ If the list was changed, sort the list and remove duplicates first."
   (if (and meta-left-comment-regexp
            (looking-at meta-left-comment-regexp))
       (current-column)
-    (skip-chars-backward "\t ")
+    (skip-chars-backward "\t\f ")
     (max (if (bolp) 0 (1+ (current-column)))
          comment-column)))
 
@@ -594,25 +575,26 @@ If the list was changed, sort the list and remove duplicates first."
   "Indent the line containing point as Metafont or MetaPost source."
   (interactive)
   (let ((indent (meta-indent-calculate)))
-    (save-excursion
-      (if (/= (current-indentation) indent)
-          (let ((beg (progn (beginning-of-line) (point)))
-                (end (progn (back-to-indentation) (point))))
-            (delete-region beg end)
-            (indent-to indent))))
+    (if (/= (current-indentation) indent)
+        (save-excursion
+          (delete-region (line-beginning-position)
+                         (progn (back-to-indentation) (point)))
+          (indent-to indent)))
     (if (< (current-column) indent)
         (back-to-indentation))))
 
 (defun meta-indent-calculate ()
   "Return the indentation of current line of Metafont or MetaPost source."
+  ;; Indentation within strings is not considered as Meta* don't allow multi
+  ;; line strings.
   (save-excursion
     (back-to-indentation)
     (cond
-      ;; Comments to the left margin.
+     ;; Comments to the left margin.
      ((and meta-left-comment-regexp
            (looking-at meta-left-comment-regexp))
       0)
-      ;; Comments to the right margin.
+     ;; Comments to the right margin.
      ((and meta-right-comment-regexp
            (looking-at meta-right-comment-regexp))
       comment-column)
@@ -620,42 +602,117 @@ If the list was changed, sort the list and remove duplicates first."
      ((and meta-ignore-comment-regexp
            (looking-at meta-ignore-comment-regexp))
       (current-indentation))
+     ;; Beginning of buffer.
+     ((eq (point-at-bol) (point-min))
+      0)
      ;; Backindent at end of environments.
-     ((looking-at
+     ((meta-indent-looking-at-code
        (concat "\\<" meta-end-environment-regexp "\\>"))
-      (- (meta-indent-calculate-last) meta-indent-level))
+      (- (meta-indent-current-indentation) meta-indent-level))
      ;; Backindent at keywords within environments.
-     ((looking-at
+     ((meta-indent-looking-at-code
        (concat "\\<" meta-within-environment-regexp "\\>"))
-      (- (meta-indent-calculate-last) meta-indent-level))
-     (t (meta-indent-calculate-last)))))
-
-(defun meta-indent-calculate-last ()
-  "Return the indentation of previous line of Metafont or MetaPost source."
-  (save-restriction
-    (widen)
-    (skip-chars-backward "\n\t ")
-    (move-to-column (current-indentation))
-    ;; Ignore comments.
-    (while (and (looking-at comment-start) (not (bobp)))
-      (skip-chars-backward "\n\t ")
-      (if (not (bobp))
-          (move-to-column (current-indentation))))
-    (cond
-     ((bobp) 0)
-     (t (+ (current-indentation)
-           (meta-indent-level-count)
-           (cond
-            ;; Compensate for backindent at end of environments.
-            ((looking-at
-              (concat "\\<"meta-end-environment-regexp "\\>"))
-             meta-indent-level)
-            ;; Compensate for backindent within environments.
-            ((looking-at
-              (concat "\\<" meta-within-environment-regexp "\\>"))
-             meta-indent-level)
-            (t 0)))))
-    ))
+      (- (meta-indent-current-indentation) meta-indent-level))
+     (t (meta-indent-current-indentation)))))
+
+(defun meta-indent-in-string-p ()
+  "Tell if the point is in a string."
+  (or (nth 3 (syntax-ppss))
+      (eq (get-text-property (point) 'face) font-lock-string-face)))
+
+(defun meta-indent-looking-at-code (regexp)
+  "Same as `looking-at' but checks that the point is not in a string."
+  (unless (meta-indent-in-string-p)
+    (looking-at regexp)))
+
+(defun meta-indent-previous-line ()
+  "Go to the previous line of code, skipping comments."
+  (skip-chars-backward "\n\t\f ")
+  (move-to-column (current-indentation))
+  ;; Ignore comments.
+  (while (and (looking-at comment-start) (not (bobp)))
+    (skip-chars-backward "\n\t\f ")
+    (when (not (bobp))
+      (move-to-column (current-indentation)))))
+
+(defun meta-indent-unfinished-line ()
+  "Tell if the current line of code ends with an unfinished expression."
+  (save-excursion
+    (end-of-line)
+    ;; Skip backward the comments.
+    (let ((point-not-in-string (point)))
+      (while (search-backward comment-start (point-at-bol) t)
+       (unless (meta-indent-in-string-p)
+         (setq point-not-in-string (point))))
+      (goto-char point-not-in-string))
+    ;; Search for the end of the previous expression.
+    (if (search-backward ";" (point-at-bol) t)
+       (progn (while (and (meta-indent-in-string-p)
+                          (search-backward ";" (point-at-bol) t)))
+              (if (= (char-after) ?\;)
+                  (forward-char)
+                (beginning-of-line)))
+      (beginning-of-line))
+    ;; See if the last statement of the line is environment-related,
+    ;; or exists at all.
+    (if (meta-indent-looking-at-code
+        (concat "[ \t\f]*\\($\\|" (regexp-quote comment-start)
+                "\\|\\<" meta-end-environment-regexp "\\>"
+                "\\|\\<" meta-begin-environment-regexp "\\>"
+                "\\|\\<" meta-within-environment-regexp "\\>\\)"))
+       nil
+      t)))
+
+(defun meta-indent-current-indentation ()
+  "Return the indentation wanted for the current line of code."
+  (+ (meta-indent-current-nesting)
+     (if (save-excursion
+          (back-to-indentation)
+          (and (not (looking-at (concat "\\<" meta-end-environment-regexp "\\>"
+                                        "\\|\\<" meta-within-environment-regexp "\\>")))
+               (progn (meta-indent-previous-line)
+                      (meta-indent-unfinished-line))))
+        meta-indent-level
+       0)))
+
+(defun meta-indent-current-nesting ()
+  "Return the indentation according to the nearest environment keyword."
+  (save-excursion
+    (save-restriction
+      (widen)
+      (back-to-indentation)
+      (let ((to-add 0))
+       ;; If we found some environment marker backward...
+       (if (catch 'found
+             (while (re-search-backward
+                     (concat "(\\|)\\|\\<" meta-end-environment-regexp "\\>"
+                             "\\|\\<" meta-begin-environment-regexp "\\>"
+                             "\\|\\<" meta-within-environment-regexp "\\>")
+                     nil t)
+               ;; If we aren't in a string or in a comment, we've found something.
+               (unless (or (meta-indent-in-string-p)
+                           (nth 4 (syntax-ppss)))
+                 (cond ((= (char-after) ?\()
+                        (setq to-add (+ to-add meta-indent-level)))
+                       ((= (char-after) ?\))
+                        (setq to-add (- to-add meta-indent-level)))
+                       (t (throw 'found t))))))
+           (progn
+             ;; ... then use it to compute the current indentation.
+             (back-to-indentation)
+             (+ to-add (current-indentation) (meta-indent-level-count)
+                ;; Compensate for backindent of end and within keywords.
+                (if (meta-indent-looking-at-code
+                     (concat "\\<" meta-end-environment-regexp "\\>\\|"
+                             "\\<" meta-within-environment-regexp "\\>"))
+                    meta-indent-level
+                  ;; Compensate for unfinished line.
+                  (if (save-excursion
+                        (meta-indent-previous-line)
+                        (meta-indent-unfinished-line))
+                      (- meta-indent-level)
+                    0))))
+         0)))))
 
 (defun meta-indent-level-count ()
   "Count indentation change for begin-end commands in the current line."
@@ -671,18 +728,12 @@ If the list was changed, sort the list and remove duplicates first."
             (goto-char (match-beginning 0))
             (cond
              ;; Count number of begin-end keywords within line.
-             ((looking-at
+             ((meta-indent-looking-at-code
                (concat "\\<" meta-begin-environment-regexp "\\>"))
               (setq count (+ count meta-indent-level)))
-             ((looking-at
+             ((meta-indent-looking-at-code
                (concat "\\<" meta-end-environment-regexp "\\>"))
-              (setq count (- count meta-indent-level)))
-             ;; Count number of open-close parentheses within line.
-             ((looking-at "(")
-              (setq count (+ count meta-indent-level)))
-             ((looking-at ")")
-              (setq count (- count meta-indent-level)))
-             )))
+              (setq count (- count meta-indent-level))))))
         count))))
 
 
@@ -692,13 +743,13 @@ If the list was changed, sort the list and remove duplicates first."
 (defcustom meta-begin-defun-regexp
   (concat "\\(begin\\(char\\|fig\\|logochar\\)\\|def\\|mode_def\\|"
           "primarydef\\|secondarydef\\|tertiarydef\\|vardef\\)")
-  "*Regexp matching beginning of defuns in Metafont or MetaPost mode."
+  "Regexp matching beginning of defuns in Metafont or MetaPost mode."
   :type 'regexp
   :group 'meta-font)
 
 (defcustom meta-end-defun-regexp
   (concat "\\(end\\(char\\|def\\|fig\\)\\)")
-  "*Regexp matching the end of defuns in Metafont or MetaPost mode."
+  "Regexp matching the end of defuns in Metafont or MetaPost mode."
   :type 'regexp
   :group 'meta-font)
 
@@ -715,7 +766,7 @@ Returns t unless search stops due to beginning or end of buffer."
         (concat "\\<" meta-begin-defun-regexp "\\>") nil t arg)
        (progn (goto-char (match-beginning 0))
               (skip-chars-backward "%")
-              (skip-chars-backward " \t") t)))
+              (skip-chars-backward " \t\f") t)))
 
 (defun meta-end-of-defun (&optional arg)
   "Move forward to end of a defun in Metafont or MetaPost code.
@@ -729,7 +780,7 @@ Returns t unless search stops due to beginning or end of buffer."
         (concat "\\<" meta-end-defun-regexp "\\>") nil t arg)
        (progn (goto-char (match-end 0))
               (skip-chars-forward ";")
-              (skip-chars-forward " \t")
+              (skip-chars-forward " \t\f")
               (if (looking-at "\n") (forward-line 1)) t)))
 
 
@@ -793,85 +844,79 @@ The environment marked is the one that contains point or follows point."
 \f
 ;;; Syntax table, keymap and menu.
 
-(defvar meta-mode-abbrev-table nil
+(define-abbrev-table 'meta-mode-abbrev-table ()
   "Abbrev table used in Metafont or MetaPost mode.")
-(define-abbrev-table 'meta-mode-abbrev-table ())
 
-(defvar meta-mode-syntax-table nil
+(defvar meta-common-mode-syntax-table
+  (let ((st (make-syntax-table)))
+    ;; underscores are word constituents
+    (modify-syntax-entry ?_  "w"  st)
+    ;; miscellaneous non-word symbols
+    (modify-syntax-entry ?#  "_"  st)
+    (modify-syntax-entry ?@  "_"  st)
+    (modify-syntax-entry ?$  "_"  st)
+    (modify-syntax-entry ??  "_"  st)
+    (modify-syntax-entry ?!  "_"  st)
+    ;; binary operators
+    (modify-syntax-entry ?&  "."  st)
+    (modify-syntax-entry ?+  "."  st)
+    (modify-syntax-entry ?-  "."  st)
+    (modify-syntax-entry ?/  "."  st)
+    (modify-syntax-entry ?*  "."  st)
+    (modify-syntax-entry ?.  "."  st)
+    (modify-syntax-entry ?:  "."  st)
+    (modify-syntax-entry ?=  "."  st)
+    (modify-syntax-entry ?<  "."  st)
+    (modify-syntax-entry ?>  "."  st)
+    (modify-syntax-entry ?|  "."  st)
+    ;; opening and closing delimiters
+    (modify-syntax-entry ?\( "()" st)
+    (modify-syntax-entry ?\) ")(" st)
+    (modify-syntax-entry ?\[ "(]" st)
+    (modify-syntax-entry ?\] ")[" st)
+    (modify-syntax-entry ?\{ "(}" st)
+    (modify-syntax-entry ?\} "){" st)
+    ;; comment character
+    (modify-syntax-entry ?%  "<"  st)
+    (modify-syntax-entry ?\n ">"  st)
+    ;; escape character, needed for embedded TeX code
+    (modify-syntax-entry ?\\ "\\" st)
+    st)
   "Syntax table used in Metafont or MetaPost mode.")
-(if meta-mode-syntax-table
-    ()
-  (setq meta-mode-syntax-table (make-syntax-table))
-  ;; underscores are word constituents
-  (modify-syntax-entry ?_  "w"  meta-mode-syntax-table)
-  ;; miscellaneous non-word symbols
-  (modify-syntax-entry ?#  "_"  meta-mode-syntax-table)
-  (modify-syntax-entry ?@  "_"  meta-mode-syntax-table)
-  (modify-syntax-entry ?$  "_"  meta-mode-syntax-table)
-  (modify-syntax-entry ??  "_"  meta-mode-syntax-table)
-  (modify-syntax-entry ?!  "_"  meta-mode-syntax-table)
-  ;; binary operators
-  (modify-syntax-entry ?&  "."  meta-mode-syntax-table)
-  (modify-syntax-entry ?+  "."  meta-mode-syntax-table)
-  (modify-syntax-entry ?-  "."  meta-mode-syntax-table)
-  (modify-syntax-entry ?/  "."  meta-mode-syntax-table)
-  (modify-syntax-entry ?*  "."  meta-mode-syntax-table)
-  (modify-syntax-entry ?.  "."  meta-mode-syntax-table)
-  (modify-syntax-entry ?:  "."  meta-mode-syntax-table)
-  (modify-syntax-entry ?=  "."  meta-mode-syntax-table)
-  (modify-syntax-entry ?<  "."  meta-mode-syntax-table)
-  (modify-syntax-entry ?>  "."  meta-mode-syntax-table)
-  (modify-syntax-entry ?|  "."  meta-mode-syntax-table)
-  ;; opening and closing delimiters
-  (modify-syntax-entry ?\( "()" meta-mode-syntax-table)
-  (modify-syntax-entry ?\) ")(" meta-mode-syntax-table)
-  (modify-syntax-entry ?\[ "(]" meta-mode-syntax-table)
-  (modify-syntax-entry ?\] ")[" meta-mode-syntax-table)
-  (modify-syntax-entry ?\{ "(}" meta-mode-syntax-table)
-  (modify-syntax-entry ?\} "){" meta-mode-syntax-table)
-  ;; comment character
-  (modify-syntax-entry ?%  "<"  meta-mode-syntax-table)
-  (modify-syntax-entry ?\n ">"  meta-mode-syntax-table)
-  ;; escape character, needed for embedded TeX code
-  (modify-syntax-entry ?\\ "\\" meta-mode-syntax-table)
-  )
-
-(defvar meta-mode-map nil
+
+(defvar meta-common-mode-map
+  (let ((map (make-sparse-keymap)))
+    ;; Comment Paragraphs:
+    ;; (define-key map "\M-a"      'backward-sentence)
+    ;; (define-key map "\M-e"      'forward-sentence)
+    ;; (define-key map "\M-h"      'mark-paragraph)
+    ;; (define-key map "\M-q"      'fill-paragraph)
+    ;; Navigation:
+    (define-key map "\M-\C-a"   'meta-beginning-of-defun)
+    (define-key map "\M-\C-e"   'meta-end-of-defun)
+    (define-key map "\M-\C-h"   'meta-mark-defun)
+    ;; Indentation:
+    (define-key map "\M-\C-q"   'meta-indent-defun)
+    (define-key map "\C-c\C-qe" 'meta-indent-defun)
+    (define-key map "\C-c\C-qr" 'meta-indent-region)
+    (define-key map "\C-c\C-qb" 'meta-indent-buffer)
+    ;; Commenting Out:
+    (define-key map "\C-c%"     'meta-comment-defun)
+    ;; (define-key map "\C-uC-c%"  'meta-uncomment-defun)
+    (define-key map "\C-c;"     'meta-comment-region)
+    (define-key map "\C-c:"     'meta-uncomment-region)
+    ;; Symbol Completion:
+    (define-key map "\M-\t"     'meta-complete-symbol)
+    ;; Shell Commands:
+    ;; (define-key map "\C-c\C-c"  'meta-command-file)
+    ;; (define-key map "\C-c\C-k"  'meta-kill-job)
+    ;; (define-key map "\C-c\C-l"  'meta-recenter-output)
+    map)
   "Keymap used in Metafont or MetaPost mode.")
-(if meta-mode-map
-    ()
-  (setq meta-mode-map (make-sparse-keymap))
-  (define-key meta-mode-map "\t"        'meta-indent-line)
-  (define-key meta-mode-map "\C-m"      'reindent-then-newline-and-indent)
-  ;; Comment Paragraphs:
-; (define-key meta-mode-map "\M-a"      'backward-sentence)
-; (define-key meta-mode-map "\M-e"      'forward-sentence)
-; (define-key meta-mode-map "\M-h"      'mark-paragraph)
-; (define-key meta-mode-map "\M-q"      'fill-paragraph)
-  ;; Navigation:
-  (define-key meta-mode-map "\M-\C-a"   'meta-beginning-of-defun)
-  (define-key meta-mode-map "\M-\C-e"   'meta-end-of-defun)
-  (define-key meta-mode-map "\M-\C-h"   'meta-mark-defun)
-  ;; Indentation:
-  (define-key meta-mode-map "\M-\C-q"   'meta-indent-defun)
-  (define-key meta-mode-map "\C-c\C-qe" 'meta-indent-defun)
-  (define-key meta-mode-map "\C-c\C-qr" 'meta-indent-region)
-  (define-key meta-mode-map "\C-c\C-qb" 'meta-indent-buffer)
-  ;; Commenting Out:
-  (define-key meta-mode-map "\C-c%"     'meta-comment-defun)
-; (define-key meta-mode-map "\C-uC-c%"  'meta-uncomment-defun)
-  (define-key meta-mode-map "\C-c;"     'meta-comment-region)
-  (define-key meta-mode-map "\C-c:"     'meta-uncomment-region)
-  ;; Symbol Completion:
-  (define-key meta-mode-map "\M-\t"     'meta-complete-symbol)
-  ;; Shell Commands:
-; (define-key meta-mode-map "\C-c\C-c"  'meta-command-file)
-; (define-key meta-mode-map "\C-c\C-k"  'meta-kill-job)
-; (define-key meta-mode-map "\C-c\C-l"  'meta-recenter-output)
-  )
+(define-obsolete-variable-alias 'meta-mode-map 'meta-common-mode-map "24.1")
 
 (easy-menu-define
- meta-mode-menu meta-mode-map
+ meta-mode-menu meta-common-mode-map
  "Menu used in Metafont or MetaPost mode."
  (list "Meta"
        ["Forward Environment"           meta-beginning-of-defun t]
@@ -907,21 +952,21 @@ The environment marked is the one that contains point or follows point."
 ;;; Hook variables.
 
 (defcustom meta-mode-load-hook nil
-  "*Hook evaluated when first loading Metafont or MetaPost mode."
+  "Hook evaluated when first loading Metafont or MetaPost mode."
   :type 'hook
   :group 'meta-font)
 
 (defcustom meta-common-mode-hook nil
-  "*Hook evaluated by both `metafont-mode' and `metapost-mode'."
+  "Hook evaluated by both `metafont-mode' and `metapost-mode'."
   :type 'hook
   :group 'meta-font)
 
 (defcustom metafont-mode-hook nil
-  "*Hook evaluated by `metafont-mode' after `meta-common-mode-hook'."
+  "Hook evaluated by `metafont-mode' after `meta-common-mode-hook'."
   :type 'hook
   :group 'meta-font)
 (defcustom metapost-mode-hook nil
-  "*Hook evaluated by `metapost-mode' after `meta-common-mode-hook'."
+  "Hook evaluated by `metapost-mode' after `meta-common-mode-hook'."
   :type 'hook
   :group 'meta-font)
 
@@ -929,103 +974,62 @@ The environment marked is the one that contains point or follows point."
 \f
 ;;; Initialization.
 
-(defun meta-common-initialization ()
+(define-derived-mode meta-common-mode prog-mode "-Meta-common-"
   "Common initialization for Metafont or MetaPost mode."
-  (kill-all-local-variables)
-
-  (make-local-variable 'paragraph-start)
-  (make-local-variable 'paragraph-separate)
-  (setq paragraph-start
-        (concat page-delimiter "\\|$"))
-  (setq paragraph-separate
-        (concat page-delimiter "\\|$"))
-
-  (make-local-variable 'paragraph-ignore-fill-prefix)
-  (setq paragraph-ignore-fill-prefix t)
-
-  (make-local-variable 'comment-start-skip)
-  (make-local-variable 'comment-start)
-  (make-local-variable 'comment-end)
-  (make-local-variable 'comment-multi-line)
-  (setq comment-start-skip "%+[ \t]*")
-  (setq comment-start "%")
-  (setq comment-end "")
-  (setq comment-multi-line nil)
-
-  (make-local-variable 'parse-sexp-ignore-comments)
-  (setq parse-sexp-ignore-comments t)
-
-  (make-local-variable 'comment-indent-function)
-  (setq comment-indent-function 'meta-comment-indent)
-  (make-local-variable 'indent-line-function)
-  (setq indent-line-function 'meta-indent-line)
+  :abbrev-table meta-mode-abbrev-table
+  (set (make-local-variable 'paragraph-start)
+       (concat page-delimiter "\\|$"))
+  (set (make-local-variable 'paragraph-separate)
+       (concat page-delimiter "\\|$"))
+
+  (set (make-local-variable 'paragraph-ignore-fill-prefix) t)
+
+  (set (make-local-variable 'comment-start-skip) "%+[ \t\f]*")
+  (set (make-local-variable 'comment-start) "%")
+  (set (make-local-variable 'comment-end) "")
+  (set (make-local-variable 'comment-multi-line) nil)
+
+  ;; We use `back-to-indentation' but \f is no indentation sign.
+  (modify-syntax-entry ?\f "_   ")
+
+  (set (make-local-variable 'parse-sexp-ignore-comments) t)
+
+  (set (make-local-variable 'comment-indent-function) #'meta-comment-indent)
+  (set (make-local-variable 'indent-line-function) #'meta-indent-line)
   ;; No need to define a mode-specific 'indent-region-function.
   ;; Simply use the generic 'indent-region and 'comment-region.
 
   ;; Set defaults for font-lock mode.
-  (make-local-variable 'font-lock-defaults)
-  (setq font-lock-defaults
-        '(meta-font-lock-keywords
-          nil nil ((?_ . "w")) nil
-          (font-lock-comment-start-regexp . "%")))
+  (set (make-local-variable 'font-lock-defaults)
+       '(meta-font-lock-keywords
+         nil nil ((?_ . "w")) nil
+         (font-lock-comment-start-regexp . "%")))
 
   ;; Activate syntax table, keymap and menu.
-  (setq local-abbrev-table meta-mode-abbrev-table)
-  (set-syntax-table meta-mode-syntax-table)
-  (use-local-map meta-mode-map)
-  (easy-menu-add meta-mode-menu)
-  )
+  (easy-menu-add meta-mode-menu))
 
 
 ;;;###autoload
-(defun metafont-mode ()
-  "Major mode for editing Metafont sources.
-Special commands:
-\\{meta-mode-map}
-
-Turning on Metafont mode calls the value of the variables
-`meta-common-mode-hook' and `metafont-mode-hook'."
-  (interactive)
-  (meta-common-initialization)
-  (setq mode-name "Metafont")
-  (setq major-mode 'metafont-mode)
-
+(define-derived-mode metafont-mode meta-common-mode "Metafont"
+  "Major mode for editing Metafont sources."
   ;; Set defaults for completion function.
-  (make-local-variable 'meta-symbol-list)
-  (make-local-variable 'meta-symbol-changed)
-  (make-local-variable 'meta-complete-list)
-  (setq meta-symbol-list nil)
-  (setq meta-symbol-changed nil)
+  (set (make-local-variable 'meta-symbol-list) nil)
+  (set (make-local-variable 'meta-symbol-changed) nil)
   (apply 'meta-add-symbols metafont-symbol-list)
-  (setq meta-complete-list
+  (set (make-local-variable 'meta-complete-list)
         (list (list "\\<\\(\\sw+\\)" 1 'meta-symbol-list)
-              (list "" 'ispell-complete-word)))
-  (run-mode-hooks 'meta-common-mode-hook 'metafont-mode-hook))
+              (list "" 'ispell-complete-word))))
 
 ;;;###autoload
-(defun metapost-mode ()
-  "Major mode for editing MetaPost sources.
-Special commands:
-\\{meta-mode-map}
-
-Turning on MetaPost mode calls the value of the variable
-`meta-common-mode-hook' and `metafont-mode-hook'."
-  (interactive)
-  (meta-common-initialization)
-  (setq mode-name "MetaPost")
-  (setq major-mode 'metapost-mode)
-
+(define-derived-mode metapost-mode meta-common-mode "MetaPost"
+  "Major mode for editing MetaPost sources."
   ;; Set defaults for completion function.
-  (make-local-variable 'meta-symbol-list)
-  (make-local-variable 'meta-symbol-changed)
-  (make-local-variable 'meta-complete-list)
-  (setq meta-symbol-list nil)
-  (setq meta-symbol-changed nil)
+  (set (make-local-variable 'meta-symbol-list) nil)
+  (set (make-local-variable 'meta-symbol-changed) nil)
   (apply 'meta-add-symbols metapost-symbol-list)
-  (setq meta-complete-list
+  (set (make-local-variable 'meta-complete-list)
         (list (list "\\<\\(\\sw+\\)" 1 'meta-symbol-list)
-              (list "" 'ispell-complete-word)))
-  (run-mode-hooks 'meta-common-mode-hook 'metapost-mode-hook))
+              (list "" 'ispell-complete-word))))
 
 
 ;;; Just in case ...
@@ -1033,5 +1037,4 @@ Turning on MetaPost mode calls the value of the variable
 (provide 'meta-mode)
 (run-hooks 'meta-mode-load-hook)
 
-;;; arch-tag: ec2916b2-3a83-4cf7-962d-d8019370c006
 ;;; meta-mode.el ends here