emacs-devel
[Top][All Lists]
Advanced

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

Re: Deriving from cc-mode with define-derived-mode


From: Stefan Monnier
Subject: Re: Deriving from cc-mode with define-derived-mode
Date: Mon, 07 Sep 2015 11:16:55 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

> mode deriving from cc-mode in a modern way? Or at least some examples of
> modes deriving from cc-mode that use define-derived-mode? Or any other

FWIW, you could define for example c-mode in the following way:

   (defvar c-mode-syntax-table
     (funcall (c-lang-const c-make-mode-syntax-table c))
     "Syntax table used in c-mode buffers.")
   
   (c-define-abbrev-table 'c-mode-abbrev-table
     '(("else" "else" c-electric-continued-statement 0)
       ("while" "while" c-electric-continued-statement 0))
     "Abbreviation table used in c-mode buffers.")
   
   (defvar c-mode-map
     (let ((map (make-sparse-keymap)))
       ;; Add bindings which are only useful for C.
       map)
     "Keymap used in c-mode buffers.")
   
   (define-derived-mode c-mode c-derivative-mode "C"
     "Major mode for editing K&R and ANSI C code.
   To submit a problem report, enter `\\[c-submit-bug-report]' from a
   c-mode buffer.  This automatically sets up a mail buffer with version
   information already added.  You just need to add a description of the
   problem, including a reproducible test case, and send the message.
   
   To see what version of CC Mode you are running, enter `\\[c-version]'.
   
   The hook `c-mode-common-hook' is run with no args at mode
   initialization, then `c-mode-hook'.
   
   Key bindings:
   \\{c-mode-map}"
     (c-init-language-vars-for 'c-mode)
     (c-make-macro-with-semi-re) ; matches macro names whose expansion ends 
with ;
     (c-common-init 'c-mode)
     (easy-menu-add c-c-menu)
     (cc-imenu-init cc-imenu-c-generic-expression))

where c-derivative-mode would be defined as

   (define-derived-mode c-mode-common prog-mode "CC-generic"
     "Pseudo major mode, parent of all modes using the CC engine."
     (c-initialize-cc-mode t)
     (setq abbrev-mode t)                  ;FIXME: Why?
     ;; FIXME: Alan doesn't like this because there's no guarantee
     ;; mode-line-process is close to the major mode name.
     (set (make-local-variable 'mode-line-process) c-submode-indicators))
   
   (defvar c-derivative-mode-map
     ;; FIXME: We can't have the menu on this keymap, because the menus for C,
     ;; C++, and ObjC can't be shared: the only difference between them is their
     ;; title, but easy-menu offers no way to compute the title dynamically.
     (let ((map (make-sparse-keymap)))
       ;; Add bindings which are useful for any C derivative.
       (define-key map "\C-c\C-e"  'c-macro-expand)
       map)
     "Keymap used in c-derivative-mode buffers.")
   
   (define-derived-mode c-derivative-mode c-mode-common "C-derivative"
     "Pseudo major mode, parent of all modes for C derivatives.
   A C derivative is a language which is a superset of C (or is
     C itself).")


-- Stefan



reply via email to

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