Function: c-electric-brace

c-electric-brace is an interactive and byte-compiled function defined in cc-cmds.el.gz.

Signature

(c-electric-brace ARG)

Documentation

Insert a brace.

If c-electric-flag is non-nil, the brace is not inside a literal and a numeric ARG hasn't been supplied, the command performs several electric actions:

(a) If the auto-newline feature is turned on (indicated by "/la" on
the mode line) newlines are inserted before and after the brace as directed by the settings in c-hanging-braces-alist.

(b) Any auto-newlines are indented. The original line is also
reindented unless c-syntactic-indentation is nil.

(c) If auto-newline is turned on, various newline cleanups based on the
settings of c-cleanup-list are done.

Probably introduced at or before Emacs version 19.23.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/cc-cmds.el.gz
(defun c-electric-brace (arg)
  "Insert a brace.

If `c-electric-flag' is non-nil, the brace is not inside a literal and a
numeric ARG hasn't been supplied, the command performs several electric
actions:

\(a) If the auto-newline feature is turned on (indicated by \"/la\" on
the mode line) newlines are inserted before and after the brace as
directed by the settings in `c-hanging-braces-alist'.

\(b) Any auto-newlines are indented.  The original line is also
reindented unless `c-syntactic-indentation' is nil.

\(c) If auto-newline is turned on, various newline cleanups based on the
settings of `c-cleanup-list' are done."

  (interactive "*P")
  (let (safepos literal
	;; We want to inhibit blinking the paren since this would be
	;; most disruptive.  We'll blink it ourselves later on.
	(old-blink-paren blink-paren-function)
	blink-paren-function case-fold-search
	(at-eol (looking-at "[ \t]*\\\\?$"))
	(active-region (and (fboundp 'use-region-p) (use-region-p)))
	got-pair-} electric-pair-deletion)

    (c-save-buffer-state ()
      (setq safepos (c-safe-position (point) (c-parse-state))
	    literal (c-in-literal safepos)))

    ;; Insert the brace.  Note that expand-abbrev might reindent
    ;; the line here if there's a preceding "else" or something.
    (let (post-self-insert-hook) ; the only way to get defined functionality
				 ; from `self-insert-command'.
      (self-insert-command (prefix-numeric-value arg)))

    ;; Emulate `electric-pair-mode'.
    (when (and (boundp 'electric-pair-mode)
	       electric-pair-mode)
      (let ((size (buffer-size))
	    post-self-insert-hook)
	(electric-pair-post-self-insert-function)
	(setq got-pair-} (and at-eol
			      (eq (c-last-command-char) ?{)
			      (eq (char-after) ?}))
	      electric-pair-deletion (< (buffer-size) size))))

    ;; Perform any required CC Mode electric actions.
    (cond
     ((or literal arg (not c-electric-flag) active-region))
     ((not at-eol)
      (c-indent-line))
     (electric-pair-deletion
      (c-indent-line)
      (c-do-brace-electrics 'ignore nil))
     (t (c-do-brace-electrics nil nil)
	(when got-pair-}
	  (save-excursion
	    (forward-char)
	    (c-do-brace-electrics 'assume 'ignore))
	  (c-indent-line))))

    ;; blink the paren
    (and (eq (c-last-command-char) ?\})
	 (not executing-kbd-macro)
	 old-blink-paren
	 (save-excursion
	   (c-save-buffer-state nil
	     (c-backward-syntactic-ws safepos))
	   (funcall old-blink-paren)))
    (c--call-post-self-insert-hook-more-safely)))