8.3 Hanging Semicolons and Commas

User Option: c-hanging-semi&comma-criteria

This style variable takes a list of functions; these get called when you type a semicolon or comma. The functions are called in order without arguments. When these functions are entered, point is just after the newly inserted ‘;’ or ‘,’ and they must preserve point (e.g., by using save-excursion). During the call, the variable c-syntactic-context is bound to the syntactic context of the current line31 see Custom Brace Hanging. These functions don’t insert newlines themselves, rather they direct CC Mode whether or not to do so. They should return one of the following values:

t

A newline is to be inserted after the ‘;’ or ‘,’, and no more functions from the list are to be called.

stop

No more functions from the list are to be called, and no newline is to be inserted.

nil

No determination has been made, and the next function in the list is to be called.

Note that auto-newlines are never inserted before a semicolon or comma. If every function in the list is called without a determination being made, then no newline is added.

In AWK mode, this variable is set by default to nil. In the other modes, the default value is a list containing a single function, c-semi&comma-inside-parenlist. This inserts newlines after all semicolons, apart from those separating for-clause statements.

Function: c-semi&comma-no-newlines-before-nonblanks

This is an example of a criteria function, provided by CC Mode. It prevents newlines from being inserted after semicolons when there is a non-blank following line. Otherwise, it makes no determination. To use, add this function to the front of the c-hanging-semi&comma-criteria list.

(defun c-semi&comma-no-newlines-before-nonblanks ()
  (save-excursion
    (if (and (= (c-last-command-char) ?\;)
	     (zerop (forward-line 1))
	     (bolp)      ; forward-line has funny behavior at eob.
	     (not (looking-at "^[ \t]*$")))
	'stop
      nil)))
Function: c-semi&comma-inside-parenlist
Function: c-semi&comma-no-newlines-for-oneline-inliners

The function c-semi&comma-inside-parenlist is what prevents newlines from being inserted inside the parenthesis list of for statements. In addition to c-semi&comma-no-newlines-before-nonblanks described above, CC Mode also comes with the criteria function c-semi&comma-no-newlines-for-oneline-inliners, which suppresses newlines after semicolons inside one-line inline method definitions (e.g., in C++ or Java).


Footnotes

(31)

This was first introduced in CC Mode 5.31.