GNU Emacs 32.0.50 (9008bf64c3) のマニュアルを PLaMo 翻訳で機械翻訳したものです。誤訳を含むことがあります。原文: https://www.gnu.org/software/emacs/manual/html_node/elisp/Example-Major-Modes.html ・ emacs-doc.conao3.com

24.2.9 メジャーモードの例

テキストモードは、ファンダメンタルモードを除けば、おそらく最もシンプルなモードでしょう。以下に、上述の多くの規約を示すtext-mode.elからの抜粋を挙げます。

;; Create the syntax table for this mode.
(defvar text-mode-syntax-table
  (let ((st (make-syntax-table)))
    (modify-syntax-entry ?\" ".   " st)
    (modify-syntax-entry ?\\ ".   " st)
    ;; Add 'p' so M-c on 'hello' leads to 'Hello', not 'hello'.
    (modify-syntax-entry ?' "w p" st)
    ...
    st)
  "Syntax table used while in `text-mode'.")

現在の実際の mode コマンドの定義は以下の通りです。

(define-derived-mode text-mode nil "Text"
  "Major mode for editing text written for humans to read.
In this mode, paragraphs are delimited only by blank or white lines.
You can thus get the full benefit of adaptive filling
 (see the variable `adaptive-fill-mode').
\\{text-mode-map}
Turning on Text mode runs the normal hook `text-mode-hook'."
  (setq-local require-final-newline mode-require-final-newline))

3つのLispモード(Lispモード、Emacs Lispモード、Lisp Interactionモード)は、Textモードよりも機能が多く、それに伴いコードも複雑になっています。以下に、これらのモードがどのように記述されているかを示すlisp-mode.elからの抜粋を掲載します。

Lispモードの構文テーブルと略語テーブルは、次のように定義されています。

;; Create mode-specific table variables.
(define-abbrev-table 'lisp-mode-abbrev-table ()
  "Abbrev table for Lisp mode.")

(defvar lisp-mode-syntax-table
  (let ((table (make-syntax-table lisp--mode-syntax-table)))
    (modify-syntax-entry ?\[ "_   " table)
    (modify-syntax-entry ?\] "_   " table)
    (modify-syntax-entry ?# "' 14" table)
    (modify-syntax-entry ?| "\" 23bn" table)
    table)
  "Syntax table used in `lisp-mode'.")

Lisp用の3つのモードは、コードの多くを共有しています。例えば、LispモードとEmacs LispモードはLisp Dataモードを継承しており、Lisp Interaction ModeはEmacs Lispモードを継承しています。

Lisp Dataモードでは、とりわけLispのコメントを処理するためにcomment-start変数が設定されます:

  (setq-local comment-start ";")
  ...

Lispモードはそれぞれ、キーマップがわずかに異なります。例えば、Lispモードでは C-c C-z が run-lisp にバインドされていますが、他のLispモードではそうではありません。ただし、すべてのLispモードに共通するコマンドがいくつかあります。以下のコードで、それらの共通コマンドを設定します。

(defvar-keymap lisp-mode-shared-map
  :parent prog-mode-map
  :doc "Keymap for commands shared by all sorts of Lisp modes."
  "C-M-q" #'indent-sexp
  "DEL" #'backward-delete-char-untabify)

そして、こちらがLispモードのキーマップを設定するためのコードです:

(defvar-keymap lisp-mode-map
  :doc "Keymap for ordinary Lisp mode.
All commands in `lisp-mode-shared-map' are inherited by this map."
  :parent lisp-mode-shared-map
  "C-M-x" #'lisp-eval-defun
  "C-c C-z" #'run-lisp)

最後に、Lispモード用のメジャーモードコマンドは以下の通りです。

(define-derived-mode lisp-mode lisp-data-mode "Lisp"
  "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
Commands:
Delete converts tabs to spaces as it moves back.
Blank lines separate paragraphs.  Semicolons start comments.

\\{lisp-mode-map}
Note that `run-lisp' may be used either to start an inferior Lisp job
or to switch back to an existing one."
  (setq-local find-tag-default-function 'lisp-find-tag-default)
  (setq-local comment-start-skip
              "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
  (setq imenu-case-fold-search t))