Function: c-do-brace-electrics

c-do-brace-electrics is a byte-compiled function defined in cc-cmds.el.gz.

Signature

(c-do-brace-electrics BEFORE AFTER)

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/cc-cmds.el.gz
(defun c-do-brace-electrics (before after)
  ;; Point is just after a brace.  Indent the various lines, add any required
  ;; auto newlines, and apply pertinent clean ups.  It is assumed that the
  ;; caller has checked that point is at EOL if need be, and that the brace is
  ;; not in a comment or string, and suchlike.
  ;;
  ;; BEFORE and AFTER qualify the newlines required before and after the
  ;; brace as follows:
  ;; If
  ;;  o - nil: insert a newline or not according to `c-hanging-braces-alist'.
  ;;  o - 'ignore: don't insert a newline.
  ;;  o - 'assume: insert a newline.
  ;;
  ;; The return value has no significance.
  (let (;; shut this up too
	(c-echo-syntactic-information-p nil)
	newlines
	ln-syntax br-syntax syntax)  ; Syntactic context of the original line,
					; of the brace itself, of the line the
					; brace ends up on.
    (c-save-buffer-state ((c-syntactic-indentation-in-macros t)
			  (c-auto-newline-analysis t))
      (setq ln-syntax (c-guess-basic-syntax)))
    (if c-syntactic-indentation
	(c-indent-line ln-syntax))

    (when c-auto-newline
      (backward-char)
      (setq br-syntax (c-point-syntax)
	    newlines (c-brace-newlines br-syntax))

      ;; Insert the BEFORE newline, if wanted, and reindent the newline.
      (if (or (and (null before) (memq 'before newlines)
		   (> (current-column) (current-indentation)))
	      (eq before 'assume))
	  (if c-syntactic-indentation
	      ;; Only a plain newline for now - it's indented
	      ;; after the cleanups when the line has its final
	      ;; appearance.
	      (newline)
	    (c-newline-and-indent)))
      (forward-char)

      ;; `syntax' is the syntactic context of the line which ends up
      ;; with the brace on it.
      (setq syntax (if (memq 'before newlines) br-syntax ln-syntax))

      ;; Do all appropriate clean ups
      (let ((here (point))
	    (pos (- (point-max) (point)))
	    mbeg mend
	    )

	;; `}': clean up empty defun braces
	(when (c-save-buffer-state ()
		(and (memq 'empty-defun-braces c-cleanup-list)
		     (eq (c-last-command-char) ?\})
		     (c-intersect-lists '(defun-close class-close inline-close)
					syntax)
		     (progn
		       (forward-char -1)
		       (c-skip-ws-backward)
		       (eq (char-before) ?\{))
		     ;; make sure matching open brace isn't in a comment
		     (not (c-in-literal))))
	  (delete-region (point) (1- here))
	  (setq here (- (point-max) pos)))
	(goto-char here)

	;; `}': compact to a one-liner defun?
	(save-match-data
	  (when
	      (and (eq (c-last-command-char) ?\})
		   (memq 'one-liner-defun c-cleanup-list)
		   (c-intersect-lists '(defun-close) syntax)
		   (c-try-one-liner))
	    (setq here (- (point-max) pos))))

	;; `{': clean up brace-else-brace and brace-elseif-brace
	(when (eq (c-last-command-char) ?\{)
	  (cond
	   ((and (memq 'brace-else-brace c-cleanup-list)
		 (re-search-backward
		  (concat "}"
			  "\\([ \t\n]\\|\\\\\n\\)*"
			  "else"
			  "\\([ \t\n]\\|\\\\\n\\)*"
			  "{"
			  "\\=")
		  nil t))
	    (delete-region (match-beginning 0) (match-end 0))
	    (insert-and-inherit "} else {"))
	   ((and (memq 'brace-elseif-brace c-cleanup-list)
		 (progn
		   (goto-char (1- here))
		   (setq mend (point))
		   (c-skip-ws-backward)
		   (setq mbeg (point))
		   (eq (char-before) ?\)))
		 (zerop (c-save-buffer-state nil (c-backward-token-2 1 t)))
		 (eq (char-after) ?\()
		 (re-search-backward
		  (concat "}"
			  "\\([ \t\n]\\|\\\\\n\\)*"
			  "else"
			  "\\([ \t\n]\\|\\\\\n\\)+"
			  "if"
			  "\\([ \t\n]\\|\\\\\n\\)*"
			  "\\=")
		  nil t))
	    (delete-region mbeg mend)
	    (goto-char mbeg)
	    (insert ?\ ))))

	(goto-char (- (point-max) pos))

	;; Indent the line after the cleanups since it might
	;; very well indent differently due to them, e.g. if
	;; c-indent-one-line-block is used together with the
	;; one-liner-defun cleanup.
	(when c-syntactic-indentation
	  (c-indent-line)))

      ;; does a newline go after the brace?
      (if (or (and (null after) (memq 'after newlines))
	      (eq after 'assume))
	  (c-newline-and-indent)))))