]> code.delx.au - gnu-emacs/blobdiff - lisp/progmodes/cc-mode.el
(compilation-next-error-function):
[gnu-emacs] / lisp / progmodes / cc-mode.el
index 69bf72b258e9a5db66b87b6c75081d3b07c0f96d..0f17a003ef4ceaced21ce8a14d9d9c29123b8f71 100644 (file)
@@ -24,9 +24,9 @@
 ;; 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., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with this program; see the file COPYING.  If not, write to
+;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
 (cc-require 'cc-align)
 (cc-require 'cc-menus)
 
-;; SILENCE the compiler.
+;; Silence the compiler.
 (cc-bytecomp-defvar comment-line-break-function) ; (X)Emacs 20+
 (cc-bytecomp-defvar adaptive-fill-first-line-regexp) ; Emacs 20+
 (cc-bytecomp-defun set-keymap-parents) ; XEmacs
+(cc-bytecomp-defun run-mode-hooks)     ; Emacs 21.1+
+(cc-bytecomp-obsolete-fun make-local-hook) ; Marked obsolete in Emacs 21.1.
 
 ;; We set these variables during mode init, yet we don't require
 ;; font-lock.
 ;; (c-init-language-vars some-mode)
 ;; (c-common-init 'some-mode) ; Or perhaps (c-basic-common-init 'some-mode)
 ;;
+;; If you're not writing a derived mode using the language variable
+;; system, then some-mode is one of the language modes directly
+;; supported by CC Mode.  You can then use (c-init-language-vars-for
+;; 'some-mode) instead of `c-init-language-vars'.
+;; `c-init-language-vars-for' is a function that avoids the rather
+;; large expansion of `c-init-language-vars'.
+;;
+;; If you use `c-basic-common-init' then you might want to call
+;; `c-font-lock-init' too to set up CC Mode's font lock support.
+;;
 ;; See cc-langs.el for further info.  A small example of a derived mode
 ;; is also available at <http://cc-mode.sourceforge.net/
 ;; derived-mode-ex.el>.
 (defun c-leave-cc-mode-mode ()
   (setq c-buffer-is-cc-mode nil))
 
+(defun c-init-language-vars-for (mode)
+  "Initialize the language variables for one of the language modes
+directly supported by CC Mode.  This can be used instead of the
+`c-init-language-vars' macro if the language you want to use is one of
+those, rather than a derived language defined through the language
+variable system (see \"cc-langs.el\")."
+  ;; This function does not do any hidden buffer changes.
+  (cond ((eq mode 'c-mode)    (c-init-language-vars c-mode))
+       ((eq mode 'c++-mode)  (c-init-language-vars c++-mode))
+       ((eq mode 'objc-mode) (c-init-language-vars objc-mode))
+       ((eq mode 'java-mode) (c-init-language-vars java-mode))
+       ((eq mode 'idl-mode)  (c-init-language-vars idl-mode))
+       ((eq mode 'pike-mode) (c-init-language-vars pike-mode))
+       ((eq mode 'awk-mode)  (c-init-language-vars awk-mode))
+       (t (error "Unsupported mode %s" mode))))
+
 ;;;###autoload
 (defun c-initialize-cc-mode (&optional new-style-init)
   "Initialize CC Mode for use in the current buffer.
 If the optional NEW-STYLE-INIT is nil or left out then all necessary
 initialization to run CC Mode for the C language is done.  Otherwise
-only some basic setup is done, and a call to `c-init-language-vars',
-is necessary too (which gives more control).  See \"cc-mode.el\" for
-more info."
+only some basic setup is done, and a call to `c-init-language-vars' or
+`c-init-language-vars-for' is necessary too (which gives more
+control).  See \"cc-mode.el\" for more info."
   ;;
   ;; This function does not do any hidden buffer changes.
 
@@ -165,7 +193,7 @@ more info."
        (put 'c-initialize-cc-mode initprop c-initialization-ok))))
 
   (unless new-style-init
-    (c-init-c-language-vars)))
+    (c-init-language-vars-for 'c-mode)))
 
 \f
 ;;; Common routines.
@@ -175,12 +203,15 @@ more info."
 
 (defun c-make-inherited-keymap ()
   (let ((map (make-sparse-keymap)))
+    ;; Necessary to use `cc-bytecomp-fboundp' below since this
+    ;; function is called from top-level forms that are evaluated
+    ;; while cc-bytecomp is active when one does M-x eval-buffer.
     (cond
      ;; XEmacs
-     ((fboundp 'set-keymap-parents)
+     ((cc-bytecomp-fboundp 'set-keymap-parents)
       (set-keymap-parents map c-mode-base-map))
      ;; Emacs
-     ((fboundp 'set-keymap-parent)
+     ((cc-bytecomp-fboundp 'set-keymap-parent)
       (set-keymap-parent map c-mode-base-map))
      ;; incompatible
      (t (error "CC Mode is incompatible with this version of Emacs")))
@@ -465,7 +496,6 @@ that requires a literal mode spec at compile time."
 
   ;; Install the functions that ensure that various internal caches
   ;; don't become invalid due to buffer changes.
-  (make-local-hook 'after-change-functions)
   (add-hook 'after-change-functions 'c-after-change nil t))
 
 (defun c-after-font-lock-init ()
@@ -491,8 +521,6 @@ This does not load the font-lock package.  Use after
          c-beginning-of-syntax
          (font-lock-mark-block-function
           . c-mark-function)))
-
-  (make-local-hook 'font-lock-mode-hook)
   (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
 
 (defun c-setup-doc-comment-style ()
@@ -519,7 +547,7 @@ This function does not do any hidden buffer changes."
   (unless mode
     ;; Called from an old third party package.  The fallback is to
     ;; initialize for C.
-    (c-init-c-language-vars))
+    (c-init-language-vars-for 'c-mode))
 
   (c-basic-common-init mode c-default-style)
   (when mode
@@ -534,7 +562,8 @@ This function does not do any hidden buffer changes."
   (let ((rfn (assq mode c-require-final-newline)))
     (when rfn
       (make-local-variable 'require-final-newline)
-      (setq require-final-newline (cdr rfn)))))
+      (and (cdr rfn)
+          (setq require-final-newline mode-require-final-newline)))))
 
 (defun c-postprocess-file-styles ()
   "Function that post processes relevant file local variables in CC Mode.
@@ -563,6 +592,13 @@ Note that the style variables are always made local to the buffer."
 
 (add-hook 'hack-local-variables-hook 'c-postprocess-file-styles)
 
+(defmacro c-run-mode-hooks (&rest hooks)
+  ;; Emacs 21.1 has introduced a system with delayed mode hooks that
+  ;; require the use of the new function `run-mode-hooks'.
+  (if (cc-bytecomp-fboundp 'run-mode-hooks)
+      `(run-mode-hooks ,@hooks)
+    `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
+
 \f
 ;; Support for C
 
@@ -615,9 +651,6 @@ Note that the style variables are always made local to the buffer."
 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.y\\(acc\\)?\\'" . c-mode))
 ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lex\\'" . c-mode))
 
-(defun c-init-c-language-vars ()
-  (c-init-language-vars c-mode))
-
 ;;;###autoload
 (defun c-mode ()
   "Major mode for editing K&R and ANSI C code.
@@ -642,12 +675,11 @@ Key bindings:
        local-abbrev-table c-mode-abbrev-table
        abbrev-mode t)
   (use-local-map c-mode-map)
-  (c-init-c-language-vars)
+  (c-init-language-vars-for 'c-mode)
   (c-common-init 'c-mode)
   (easy-menu-add c-c-menu)
   (cc-imenu-init cc-imenu-c-generic-expression)
-  (run-hooks 'c-mode-common-hook)
-  (run-hooks 'c-mode-hook)
+  (c-run-mode-hooks 'c-mode-common-hook 'c-mode-hook)
   (c-update-modeline))
 
 \f
@@ -706,12 +738,11 @@ Key bindings:
        local-abbrev-table c++-mode-abbrev-table
        abbrev-mode t)
   (use-local-map c++-mode-map)
-  (c-init-language-vars c++-mode)
+  (c-init-language-vars-for 'c++-mode)
   (c-common-init 'c++-mode)
   (easy-menu-add c-c++-menu)
   (cc-imenu-init cc-imenu-c++-generic-expression)
-  (run-hooks 'c-mode-common-hook)
-  (run-hooks 'c++-mode-hook)
+  (c-run-mode-hooks 'c-mode-common-hook 'c++-mode-hook)
   (c-update-modeline))
 
 \f
@@ -771,12 +802,11 @@ Key bindings:
   ;; end of the @-style directives.
   (setq c-type-decl-end-used t)
   (use-local-map objc-mode-map)
-  (c-init-language-vars objc-mode)
+  (c-init-language-vars-for 'objc-mode)
   (c-common-init 'objc-mode)
   (easy-menu-add c-objc-menu)
   (cc-imenu-init nil 'cc-imenu-objc-function)
-  (run-hooks 'c-mode-common-hook)
-  (run-hooks 'objc-mode-hook)
+  (c-run-mode-hooks 'c-mode-common-hook 'objc-mode-hook)
   (c-update-modeline))
 
 \f
@@ -842,12 +872,11 @@ Key bindings:
        local-abbrev-table java-mode-abbrev-table
        abbrev-mode t)
   (use-local-map java-mode-map)
-  (c-init-language-vars java-mode)
+  (c-init-language-vars-for 'java-mode)
   (c-common-init 'java-mode)
   (easy-menu-add c-java-menu)
   (cc-imenu-init cc-imenu-java-generic-expression)
-  (run-hooks 'c-mode-common-hook)
-  (run-hooks 'java-mode-hook)
+  (c-run-mode-hooks 'c-mode-common-hook 'java-mode-hook)
   (c-update-modeline))
 
 \f
@@ -901,12 +930,11 @@ Key bindings:
        mode-name "IDL"
        local-abbrev-table idl-mode-abbrev-table)
   (use-local-map idl-mode-map)
-  (c-init-language-vars idl-mode)
+  (c-init-language-vars-for 'idl-mode)
   (c-common-init 'idl-mode)
   (easy-menu-add c-idl-menu)
   ;;(cc-imenu-init cc-imenu-idl-generic-expression) ;TODO
-  (run-hooks 'c-mode-common-hook)
-  (run-hooks 'idl-mode-hook)
+  (c-run-mode-hooks 'c-mode-common-hook 'idl-mode-hook)
   (c-update-modeline))
 
 \f
@@ -964,12 +992,11 @@ Key bindings:
        local-abbrev-table pike-mode-abbrev-table
        abbrev-mode t)
   (use-local-map pike-mode-map)
-  (c-init-language-vars pike-mode)
+  (c-init-language-vars-for 'pike-mode)
   (c-common-init 'pike-mode)
   (easy-menu-add c-pike-menu)
   ;;(cc-imenu-init cc-imenu-pike-generic-expression) ;TODO
-  (run-hooks 'c-mode-common-hook)
-  (run-hooks 'pike-mode-hook)
+  (c-run-mode-hooks 'c-mode-common-hook 'pike-mode-hook)
   (c-update-modeline))
 
 \f
@@ -984,7 +1011,7 @@ Key bindings:
 
 ;;; Autoload directives must be on the top level, so we construct an
 ;;; autoload form instead.
-;;;###autoload (autoload 'awk-mode "cc-mode" "Major mode for editing AWK code.")
+;;;###autoload (autoload 'awk-mode "cc-mode" "Major mode for editing AWK code." t)
 
 (if (not (memq 'syntax-properties c-emacs-features))
     (autoload 'awk-mode "awk-mode" "Major mode for editing AWK code."  t)
@@ -1039,7 +1066,7 @@ Key bindings:
           local-abbrev-table awk-mode-abbrev-table
           abbrev-mode t)
     (use-local-map awk-mode-map)
-    (c-init-language-vars awk-mode)
+    (c-init-language-vars-for 'awk-mode)
     (c-common-init 'awk-mode)
     ;; The rest of CC Mode does not (yet) use `font-lock-syntactic-keywords',
     ;; so it's not set by `c-font-lock-init'.
@@ -1061,8 +1088,7 @@ Key bindings:
     ;; in cc-engine.el, just before (defun c-fast-in-literal ...
     (defalias 'c-in-literal 'c-slow-in-literal)
 
-    (run-hooks 'c-mode-common-hook)
-    (run-hooks 'awk-mode-hook)
+    (c-run-mode-hooks 'c-mode-common-hook 'awk-mode-hook)
     (c-update-modeline))
 ) ;; closes the (if (not (memq 'syntax-properties c-emacs-features))
 
@@ -1152,4 +1178,6 @@ Key bindings:
 
 \f
 (cc-provide 'cc-mode)
+
+;;; arch-tag: 7825e5c4-fd09-439f-a04d-4c13208ba3d7
 ;;; cc-mode.el ends here