CC MODE Version 5 Documentation. Node: Customizing Semi-colons and Commas

PREV Custom Brace and Colon Hanging UP Advanced Customizations NEXT Other Special Indentations

6.4.3: Customizing Semi-colons and Commas

You can also customize the insertion of newlines after semi-colons and commas, when the auto-newline minor mode is enabled (see Minor Modes). This is controlled by the variable c-hanging-semi&comma-criteria, which contains a list of functions that are called in the order they appear. Each function is called with zero arguments, and is expected to return one of the following values:

If every function in the list is called without a determination being made, then no newline is added. The default value for this variable is a list containing a single function which inserts newlines only after semi-colons which do not appear inside parenthesis lists (i.e. those that separate for-clause statements).

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


(defun c-semi&comma-no-newlines-before-nonblanks ()
  (save-excursion
    (if (and (eq last-command-char ?\;)
             (zerop (forward-line 1))
             (not (looking-at "^[ \t]*$")))
        'stop
      nil)))

The default value of c-hanging-semi&comma-criteria is a list containing just the function c-semi&comma-inside-parenlist, which suppresses newlines after semicolons inside parenthesis lists (e.g. for-loops). 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 (i.e. in C++ or Java).

PREV Custom Brace and Colon Hanging UP Advanced Customizations NEXT Other Special Indentations