Fix a few packages to work with nil tab-stop-list
[bpt/emacs.git] / lisp / progmodes / cc-mode.el
index e977a41..1ce0767 100644 (file)
@@ -1,6 +1,6 @@
 ;;; cc-mode.el --- major mode for editing C and similar languages
 
-;; Copyright (C) 1985, 1987, 1992-2013 Free Software Foundation, Inc.
+;; Copyright (C) 1985, 1987, 1992-2014 Free Software Foundation, Inc.
 
 ;; Authors:    2003- Alan Mackenzie
 ;;             1998- Martin Stjernholm
@@ -75,7 +75,7 @@
 
 ;; For Emacs < 22.2.
 (eval-and-compile
-  (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
+  (unless (fboundp 'declare-function) (defmacro declare-function (&rest _r))))
 
 (eval-when-compile
   (let ((load-path
@@ -188,7 +188,12 @@ control).  See \"cc-mode.el\" for more info."
                (setq c-block-comment-prefix
                      (symbol-value 'c-comment-continuation-stars)))
            (add-hook 'change-major-mode-hook 'c-leave-cc-mode-mode)
-           (setq c-initialization-ok t))
+           (setq c-initialization-ok t)
+           ;; Connect up with Emacs's electric-indent-mode, for >= Emacs 24.4
+            (when (fboundp 'electric-indent-local-mode)
+             (add-hook 'electric-indent-mode-hook 'c-electric-indent-mode-hook)
+              (add-hook 'electric-indent-local-mode-hook
+                        'c-electric-indent-local-mode-hook)))
        ;; Will try initialization hooks again if they failed.
        (put 'c-initialize-cc-mode initprop c-initialization-ok))))
 
@@ -578,6 +583,15 @@ that requires a literal mode spec at compile time."
   ;; setup the comment indent variable in a Emacs version portable way
   (set (make-local-variable 'comment-indent-function) 'c-comment-indent)
 
+  ;; In Emacs 24.4 onwards, prevent Emacs's built in electric indentation from
+  ;; messing up CC Mode's, and set `c-electric-flag' if `electric-indent-mode'
+  ;; has been called by the user.
+  (when (boundp 'electric-indent-inhibit) (setq electric-indent-inhibit t))
+  ;; CC-mode should obey Emacs's generic preferences, tho only do it if
+  ;; Emacs's generic preferences can be set per-buffer (Emacs>=24.4).
+  (when (fboundp 'electric-indent-local-mode)
+    (setq c-electric-flag electric-indent-mode))
+
 ;;   ;; Put submode indicators onto minor-mode-alist, but only once.
 ;;   (or (assq 'c-submode-indicators minor-mode-alist)
 ;;       (setq minor-mode-alist
@@ -807,7 +821,7 @@ Note that the style variables are always made local to the buffer."
     `(progn ,@(mapcar (lambda (hook) `(run-hooks ,hook)) hooks))))
 
 \f
-;;; Change hooks, linking with Font Lock.
+;;; Change hooks, linking with Font Lock and electric-indent-mode.
 
 ;; Buffer local variables recording Beginning/End-of-Macro position before a
 ;; change, when a macro straddles, respectively, the BEG or END (or both) of
@@ -1032,15 +1046,16 @@ Note that the style variables are always made local to the buffer."
                      (list type marked-id
                            type-pos term-pos
                            (buffer-substring-no-properties type-pos term-pos)
-                           (buffer-substring-no-properties beg end)))))))
-
-       (if c-get-state-before-change-functions
-           (mapc (lambda (fn)
-                   (funcall fn beg end))
-                 c-get-state-before-change-functions))
-       )))
-  ;; The following must be done here rather than in `c-after-change' because
-  ;; newly inserted parens would foul up the invalidation algorithm.
+                             (buffer-substring-no-properties beg end)))))))
+
+         (if c-get-state-before-change-functions
+             (let (open-paren-in-column-0-is-defun-start)
+               (mapc (lambda (fn)
+                       (funcall fn beg end))
+                     c-get-state-before-change-functions)))
+         )))
+    ;; The following must be done here rather than in `c-after-change' because
+    ;; newly inserted parens would foul up the invalidation algorithm.
   (c-invalidate-state-cache beg))
 
 (defvar c-in-after-change-fontification nil)
@@ -1062,7 +1077,7 @@ Note that the style variables are always made local to the buffer."
   ;; This calls the language variable c-before-font-lock-functions, if non nil.
   ;; This typically sets `syntax-table' properties.
 
-  (c-save-buffer-state (case-fold-search)
+  (c-save-buffer-state (case-fold-search open-paren-in-column-0-is-defun-start)
     ;; When `combine-after-change-calls' is used we might get calls
     ;; with regions outside the current narrowing.  This has been
     ;; observed in Emacs 20.7.
@@ -1150,7 +1165,7 @@ Note that the style variables are always made local to the buffer."
       (backward-char))
     new-pos))                          ; back over (, [, <.
 
-(defun c-change-set-fl-decl-start (beg end old-len)
+(defun c-change-set-fl-decl-start (_beg _end _old-len)
   ;; Set c-new-BEG to the beginning of a "local" declaration if it('s BOL) is
   ;; inside one.  This is called from an after-change-function, but the
   ;; parameters BEG END and OLD-LEN are ignored.  See `c-set-fl-decl-start'
@@ -1178,7 +1193,8 @@ Note that the style variables are always made local to the buffer."
   ;; 
   ;; Type a space in the first blank line, and the fontification of the next
   ;; line was fouled up by context fontification.
-  (let ((new-beg beg) (new-end end) new-region case-fold-search)
+  (let ((new-beg beg) (new-end end) new-region case-fold-search
+       open-paren-in-column-0-is-defun-start)
     (if c-in-after-change-fontification
        (setq c-in-after-change-fontification nil)
       (save-restriction
@@ -1223,7 +1239,7 @@ This function is called from `c-common-init', once per mode initialization."
       (make-local-hook 'font-lock-mode-hook))
   (add-hook 'font-lock-mode-hook 'c-after-font-lock-init nil t))
 
-(defun c-extend-after-change-region (beg end old-len)
+(defun c-extend-after-change-region (_beg _end _old-len)
   "Extend the region to be fontified, if necessary."
   ;; Note: the parameters are ignored here.  This somewhat indirect
   ;; implementation exists because it is minimally different from the
@@ -1236,6 +1252,25 @@ This function is called from `c-common-init', once per mode initialization."
   ;; function.
   (cons c-new-BEG c-new-END))
 
+;; Connect up to `electric-indent-mode' (Emacs 24.4 and later).
+(defun c-electric-indent-mode-hook ()
+  ;; Emacs has en/disabled `electric-indent-mode'.  Propagate this through to
+  ;; each CC Mode buffer.
+  (mapc (lambda (buf)
+          (with-current-buffer buf
+            (when c-buffer-is-cc-mode
+              ;; Don't use `c-toggle-electric-state' here due to recursion.
+              (setq c-electric-flag electric-indent-mode)
+              (c-update-modeline))))
+        (buffer-list)))
+
+(defun c-electric-indent-local-mode-hook ()
+  ;; Emacs has en/disabled `electric-indent-local-mode' for this buffer.
+  ;; Propagate this through to this buffer's value of `c-electric-flag'
+  (when c-buffer-is-cc-mode
+    (setq c-electric-flag electric-indent-mode)
+    (c-update-modeline)))
+
 \f
 ;; Support for C
 
@@ -1731,6 +1766,7 @@ Key bindings:
                filladapt-mode
                defun-prompt-regexp
                font-lock-mode
+               auto-fill-mode
                font-lock-maximum-decoration
                parse-sexp-lookup-properties
                lookup-syntax-properties))