Function: c-electric-paren

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

Signature

(c-electric-paren ARG)

Documentation

Insert a parenthesis.

If c-syntactic-indentation and c-electric-flag are both non-nil, the line is reindented unless a numeric ARG is supplied, or the parenthesis is inserted inside a literal.

Whitespace between a function name and the parenthesis may get added or removed; see the variable c-cleanup-list.

Also, if c-electric-flag and c-auto-newline are both non-nil, some newline cleanups are done if appropriate; see the variable c-cleanup-list.

Probably introduced at or before Emacs version 20.4.

Key Bindings

Source Code

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

If `c-syntactic-indentation' and `c-electric-flag' are both non-nil, the
line is reindented unless a numeric ARG is supplied, or the parenthesis
is inserted inside a literal.

Whitespace between a function name and the parenthesis may get added or
removed; see the variable `c-cleanup-list'.

Also, if `c-electric-flag' and `c-auto-newline' are both non-nil, some
newline cleanups are done if appropriate; see the variable `c-cleanup-list'."
  (interactive "*P")
  (let ((literal (c-save-buffer-state () (c-in-literal)))
	;; shut this up
	(c-echo-syntactic-information-p nil)
	case-fold-search)
    (let (post-self-insert-hook) ; The only way to get defined functionality
				 ; from `self-insert-command'.
      (self-insert-command (prefix-numeric-value arg)))

    (if (and (not arg) (not literal))
	(let* (;; We want to inhibit blinking the paren since this will
	       ;; be most disruptive.  We'll blink it ourselves
	       ;; afterwards.
	       (old-blink-paren blink-paren-function)
	       blink-paren-function)
	  (if (and c-syntactic-indentation c-electric-flag)
	      (indent-according-to-mode))

	  ;; If we're at EOL, check for new-line clean-ups.
	  (when (and c-electric-flag c-auto-newline
		     (looking-at "[ \t]*\\\\?$"))

	    ;; clean up brace-elseif-brace
	    (when
		(and (memq 'brace-elseif-brace c-cleanup-list)
		     (eq (c-last-command-char) ?\()
		     (re-search-backward
		      (concat "}"
			      "\\([ \t\n]\\|\\\\\n\\)*"
			      "else"
			      "\\([ \t\n]\\|\\\\\n\\)+"
			      "if"
			      "\\([ \t\n]\\|\\\\\n\\)*"
			      "("
			      "\\=")
		      nil t)
		     (not  (c-save-buffer-state () (c-in-literal))))
	      (delete-region (match-beginning 0) (match-end 0))
	      (insert-and-inherit "} else if ("))

	    ;; clean up brace-catch-brace
	    (when
		(and (memq 'brace-catch-brace c-cleanup-list)
		     (eq (c-last-command-char) ?\()
		     (re-search-backward
		      (concat "}"
			      "\\([ \t\n]\\|\\\\\n\\)*"
			      "catch"
			      "\\([ \t\n]\\|\\\\\n\\)*"
			      "("
			      "\\=")
		      nil t)
		     (not  (c-save-buffer-state () (c-in-literal))))
	      (delete-region (match-beginning 0) (match-end 0))
	      (insert-and-inherit "} catch (")))

	  ;; Apply `electric-pair-mode' stuff.
	  (when (and (boundp 'electric-pair-mode)
		     electric-pair-mode)
	    (let (post-self-insert-hook)
	      (electric-pair-post-self-insert-function)))

	  ;; Check for clean-ups at function calls.  These two DON'T need
	  ;; `c-electric-flag' or `c-syntactic-indentation' set.
	  ;; Point is currently just after the inserted paren.
	  (let (beg (end (1- (point))))
	    (cond

	     ;; space-before-funcall clean-up?
	     ((and (memq 'space-before-funcall c-cleanup-list)
		   (eq (c-last-command-char) ?\()
		   (save-excursion
		     (backward-char)
		     (skip-chars-backward " \t")
		     (setq beg (point))
		     (and (c-save-buffer-state () (c-on-identifier))
                          ;; Don't add a space into #define FOO()....
                          (not (and (c-beginning-of-macro)
                                    (c-forward-over-cpp-define-id)
                                    (eq (point) beg))))))
	      (save-excursion
		(delete-region beg end)
		(goto-char beg)
		(insert ?\ )))

	     ;; compact-empty-funcall clean-up?
	     ((c-save-buffer-state ()
		(and (memq 'compact-empty-funcall c-cleanup-list)
		     (eq (c-last-command-char) ?\))
		     (save-excursion
		       (c-safe (backward-char 2))
		       (when (looking-at "()")
			 (setq end (point))
			 (skip-chars-backward " \t")
			 (setq beg (point))
			 (c-on-identifier)))))
	      (delete-region beg end))))
	  (and (eq last-input-event ?\))
	       (not executing-kbd-macro)
	       old-blink-paren
	       (funcall old-blink-paren)))

      ;; Apply `electric-pair-mode' stuff inside a string or comment.
      (when (and (boundp 'electric-pair-mode) electric-pair-mode)
	(let (post-self-insert-hook)
	  (electric-pair-post-self-insert-function))))
    (c--call-post-self-insert-hook-more-safely)))