Next: , Previous: , Up: CC Mode   [Contents][Index]

9 Clean-ups

Clean-ups are mechanisms which remove (or exceptionally, add) whitespace in specific circumstances and are complementary to colon and brace hanging. You enable a clean-up by adding its symbol into c-cleanup-list, e.g., like this:

(add-to-list 'c-cleanup-list 'space-before-funcall)

On the surface, it would seem that clean-ups overlap the functionality provided by the c-hanging-*-alist variables. Clean-ups, however, are used to adjust code “after-the-fact”, i.e., to adjust the whitespace in constructs later than when they were typed.

Most of the clean-ups remove automatically inserted newlines, and are only active when auto-newline minor mode is turned on. Others will work all the time. Note that clean-ups are only performed when there is nothing but whitespace appearing between the individual components of the construct, and (apart from comment-close-slash) when the construct does not occur within a literal (see Auto-newline Insertion).

User Option: c-cleanup-list

You configure CC Mode’s clean-ups by setting the style variable c-cleanup-list, which is a list of clean-up symbols. By default, CC Mode cleans up only the scope-operator construct, which is necessary for proper C++ support.

These are the clean-ups that are only active when electric and auto-newline minor modes are enabled:

brace-else-brace

Clean up ‘} else {’ constructs by placing the entire construct on a single line. Clean up occurs when the open brace after the ‘else’ is typed. So for example, this:

void spam(int i)
{
    if( i==7 ) {
        dosomething();
    }
    else
    {

appears like this after the last open brace is typed:

void spam(int i)
{
    if( i==7 ) {
        dosomething();
    } else {
brace-elseif-brace

Similar to the brace-else-brace clean-up, but this cleans up ‘} else if (...) {’ constructs. For example:

void spam(int i)
{
    if( i==7 ) {
        dosomething();
    }
    else if( i==3 )
    {

appears like this after the last open parenthesis is typed:

void spam(int i)
{
    if( i==7 ) {
        dosomething();
    } else if(

and like this after the last open brace is typed:

void spam(int i)
{
    if( i==7 ) {
        dosomething();
    } else if( i==3 ) {
brace-catch-brace

Analogous to brace-elseif-brace, but cleans up ‘} catch (...) {’ in C++ and Java mode.

empty-defun-braces

Clean up braces following a top-level function or class definition that contains no body. Clean up occurs when the closing brace is typed. Thus the following:

class Spam
{
}

is transformed into this when the close brace is typed:

class Spam
{}
defun-close-semi

Clean up the terminating semicolon on top-level function or class definitions when they follow a close brace. Clean up occurs when the semicolon is typed. So for example, the following:

class Spam
{
...
}
;

is transformed into this when the semicolon is typed:

class Spam
{
...
};
list-close-comma

Clean up commas following braces in array and aggregate initializers. Clean up occurs when the comma is typed. The space before the comma is zapped just like the space before the semicolon in defun-close-semi.

scope-operator

Clean up double colons which might designate a C++ scope operator split across multiple lines32. Clean up occurs when the second colon is typed. You will always want scope-operator in the c-cleanup-list when you are editing C++ code.

one-liner-defun

Clean up a single line of code enclosed by defun braces by removing the whitespace before and after the code. The clean-up happens when the closing brace is typed. If the variable c-max-one-liner-length is set, the cleanup is only done if the resulting line would be no longer than the value of that variable.

For example, consider this AWK code:

BEGIN {
    FS = "\t" # use <TAB> as a field separator
}

It gets compacted to the following when the closing brace is typed:

BEGIN {FS = "\t"} # use <TAB> as a field separator
User Option: c-max-one-liner-length

The maximum length of the resulting line for which the clean-up one-liner-defun will be triggered. This length is that of the entire line, including any leading whitespace and any trailing comment. Its default value is 80. If the value is zero or nil, no limit applies.

The following clean-ups are always active when they occur on c-cleanup-list, regardless of whether Electric minor mode or Auto-newline minor mode are enabled:

space-before-funcall

Insert a space between the function name and the opening parenthesis of a function call. This produces function calls in the style mandated by the GNU coding standards, e.g., ‘signal (SIGINT, SIG_IGN)’ and ‘abort ()’. Clean up occurs when the opening parenthesis is typed. This clean-up should never be active in AWK Mode, since such a space is syntactically invalid for user defined functions.

compact-empty-funcall

Clean up any space between the function name and the opening parenthesis of a function call that has no arguments. This is typically used together with space-before-funcall if you prefer the GNU function call style for functions with arguments but think it looks ugly when it’s only an empty parenthesis pair. I.e., you will get ‘signal (SIGINT, SIG_IGN)’, but ‘abort()’. Clean up occurs when the closing parenthesis is typed.

comment-close-slash

When inside a block comment, terminate the comment when you type a slash at the beginning of a line (i.e., immediately after the comment prefix). This clean-up removes whitespace preceding the slash and if needed, inserts a star to complete the token ‘*/’. Type C-q / in this situation if you just want a literal ‘/’ inserted.


Footnotes

(32)

Certain C++ constructs introduce ambiguous situations, so scope-operator clean-ups might not always be correct. This usually only occurs when scoped identifiers appear in switch label tags.

Next: Indentation Engine Basics, Previous: Customizing Auto-newlines, Up: CC Mode   [Contents][Index]