Function: c-electric-semi&comma

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

Signature

(c-electric-semi&comma ARG)

Documentation

Insert a comma or semicolon.

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

(a) When the auto-newline feature is turned on (indicated by "/la" on
the mode line) a newline might be inserted. See the variable c-hanging-semi&comma-criteria for how newline insertion is determined.

(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, a comma following a brace list or a
semicolon following a defun might be cleaned up, depending on the settings of c-cleanup-list.

Key Bindings

Source Code

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

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

\(a) When the auto-newline feature is turned on (indicated by \"/la\" on
the mode line) a newline might be inserted.  See the variable
`c-hanging-semi&comma-criteria' for how newline insertion is determined.

\(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, a comma following a brace list or a
semicolon following a defun might be cleaned up, depending on the
settings of `c-cleanup-list'."
  (interactive "*P")
  (c-with-string-fences
   (let* (lim literal c-syntactic-context
	      (here (point))
	      ;; shut this up
	      (c-echo-syntactic-information-p nil))

     (c-save-buffer-state ()
       (setq lim (c-most-enclosing-brace (c-parse-state))
	     literal (c-in-literal lim)))

     (let (post-self-insert-hook)	; Disable random functionality.
       (self-insert-command (prefix-numeric-value arg)))

     (if (and c-electric-flag (not literal) (not arg))
	 ;; do all cleanups and newline insertions if c-auto-newline is on.
	 (if (or (not c-auto-newline)
		 (not (looking-at "[ \t]*\\\\?$")))
	     (if c-syntactic-indentation
		 (c-indent-line))
	   ;; clean ups: list-close-comma or defun-close-semi
	   (let ((pos (- (point-max) (point))))
	     (if (c-save-buffer-state ()
		   (and (or (and
			     (eq (c-last-command-char) ?,)
			     (memq 'list-close-comma c-cleanup-list))
			    (and
			     (eq (c-last-command-char) ?\;)
			     (memq 'defun-close-semi c-cleanup-list)))
			(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 lim))))
		 (delete-region (point) here))
	     (goto-char (- (point-max) pos)))
	   ;; reindent line
	   (when c-syntactic-indentation
	     (setq c-syntactic-context (c-guess-basic-syntax))
	     (c-indent-line c-syntactic-context))
	   ;; check to see if a newline should be added
	   (let ((criteria c-hanging-semi&comma-criteria)
		 answer add-newline-p)
	     (while criteria
	       (setq answer (funcall (car criteria)))
	       ;; only nil value means continue checking
	       (if (not answer)
		   (setq criteria (cdr criteria))
		 (setq criteria nil)
		 ;; only 'stop specifically says do not add a newline
		 (setq add-newline-p (not (eq answer 'stop)))
		 ))
	     (if add-newline-p
		 (c-newline-and-indent)))))))
  (c--call-post-self-insert-hook-more-safely))