+2014-02-17 Alan Mackenzie <acm@muc.de>
+
+ Connect electric-indent-mode up with CC Mode. Bug #15478.
+ * progmodes/cc-mode.el (c-initialize-cc-mode): add CC Mode hooks
+ to electric-indent-{,local-}-mode.
+ (c-basic-common-init): Set electric-indent-inhibit. Initialise
+ c-electric-flag from electric-indent-mode.
+ (c-electric-indent-mode-hook, c-electric-indent-local-mode-hook):
+ New hook functions which propagate electric-indent-mode to CC
+ Mode.
+
+ * progmodes/cc-cmds.el (c-toggle-electric-state): When C-c C-l is
+ hit, toggle electric-indent-local-moode.
+
+ * electric.el (electric-indent-mode-has-been-called): New
+ variable.
+
2014-02-17 Juanma Barranquero <lekktu@gmail.com>
* frameset.el (frameset-cfg-id): New function.
(let ((electric-indent-mode nil))
(newline arg 'interactive)))
+(defvar electric-indent-mode-has-been-called 0
+ "How many times has `electric-indent-mode' been called?
+It's > 1 if it's been called at least once by the user.")
;;;###autoload
(define-minor-mode electric-indent-mode
"Toggle on-the-fly reindentation (Electric Indent mode).
:global t :group 'electricity
:initialize 'custom-initialize-delay
:init-value t
+ (setq electric-indent-mode-has-been-called
+ (1+ electric-indent-mode-has-been-called))
(if (not electric-indent-mode)
(progn
(when (eq (lookup-key global-map [?\C-j])
(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-mode)
+ (add-hook 'electric-indent-mode-hook 'c-electric-indent-mode-hook)
+ (when (fboundp 'electric-indent-local-mode)
+ (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))))
;; 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))
+ (when (and (boundp 'electric-indent-mode-has-been-called)
+ (> electric-indent-mode-has-been-called 1))
+ (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
`(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
;; 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.
+ (when (and (boundp 'electric-indent-mode-has-been-called)
+ (> electric-indent-mode-has-been-called 1))
+ (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