[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5. Text Filling and Line Breaking

Since there's a lot of normal text in comments and string literals, CC Mode provides features to edit these like in text mode. The goal is to do it as seamlessly as possible, i.e. you can use auto fill mode, sentence and paragraph movement, paragraph filling, adaptive filling etc wherever there's a piece of normal text without having to think much about it. CC Mode should keep the indentation, fix the comment line decorations, and so on, for you. It does that by hooking in on the different line breaking functions and tuning relevant variables as necessary.

To make Emacs recognize comments and treat text in them as normal paragraphs, CC Mode makes several standard variables(11) buffer local and modifies them according to the language syntax and the comment line prefix.

User Option: c-comment-prefix-regexp
This style variable contains the regexp used to recognize the comment line prefix, which is the line decoration that starts every line in a comment. The default is `//+\\|\\**', which matches C++ style line comments like

// blah blah

with two or more slashes in front of them, and C style block comments like

 * blah blah

with zero or more stars at the beginning of every line. If you change this variable, please make sure it still matches the comment starter (i.e. //) of line comments and the line prefix inside block comments.

Also note that since CC Mode uses the value of c-comment-prefix-regexp to set up several other variables at mode initialization, there won't be any effect if you just change it inside a CC Mode buffer. You need to call the command c-setup-paragraph-variables too, to update those other variables with the new value. That's also the case if you modify this variable in a mode hook, since CC Mode sets up all variables before calling them.

Line breaks are by default handled (almost) the same regardless whether they are made by auto fill mode (see section `Auto Fill' in The Emacs Editor), paragraph filling (e.g. with M-q), or explicitly with M-j or similar methods. In string literals, the new line gets the same indentation as the previous nonempty line (may be changed with the string syntactic symbol). In comments, CC Mode uses c-comment-prefix-regexp to adapt the line prefix from the other lines in the comment.

CC Mode uses adaptive fill mode (see section `Adaptive Fill' in The Emacs Editor) to make Emacs correctly keep the line prefix when filling paragraphs. That also makes Emacs preserve the text indentation inside the comment line prefix. E.g. in the following comment, both paragraphs will be filled with the left margins of the texts kept intact:

/* Make a balanced b-tree of the nodes in the incoming
 * stream.  But, to quote the famous words of Donald E.
 * Knuth,
 *     Beware of bugs in the above code; I have only
 *     proved it correct, not tried it.

It's also possible to use other adaptive filling packages, notably Kyle E. Jones' Filladapt package(12), which handles things like bulleted lists nicely. There's a convenience function c-setup-filladapt that tunes the relevant variables in Filladapt for use in CC Mode. Call it from a mode hook, e.g. with something like this in your `.emacs':

(defun my-c-mode-common-hook ()
  (filladapt-mode 1))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

User Option: c-block-comment-prefix
Normally the comment line prefix inserted for a new line inside a comment is deduced from other lines in it. However there's one situation when there's no hint about what the prefix should look like, namely when a block comment is broken for the first time. This style variable(13) is used then as the comment prefix. It defaults to `* '(14), which makes a comment

/* Got O(n^2) here, which is a Bad Thing. */

break into

/* Got O(n^2) here,
 * which is a Bad Thing. */

Note that it won't work to adjust the indentation by putting leading spaces in c-block-comment-prefix, since CC Mode still uses the normal indentation engine to indent the line. Thus, the right way to fix the indentation is by customizing the c syntactic symbol. It defaults to c-lineup-C-comments, which handles the indentation of most common comment styles, see 11. Indentation Functions.

User Option: c-ignore-auto-fill
When auto fill mode is enabled, CC Mode can selectively ignore it depending on the context the line break would occur in, e.g. to never break a line automatically inside a string literal. This variable takes a list of symbols for the different contexts where auto-filling never should occur:

Inside a string or character literal.
Inside a C style block comment.
Inside a C++ style line comment.
Inside a preprocessor directive.
Anywhere else, i.e. in normal code.

By default, c-ignore-auto-fill is set to '(string cpp code), which means that auto-filling only occurs in comments when auto-fill mode is activated. In literals, it's often desirable to have explicit control over newlines. In preprocessor directives, the necessary `\' escape character before the newline is not automatically inserted, so an automatic line break would produce invalid code. In normal code, line breaks are normally dictated by some logical structure in the code rather than the last whitespace character, so automatic line breaks there will produce poor results in the current implementation.

The commands that do the actual work follow.

M-q (c-fill-paragraph)
This is the replacement for fill-paragraph in CC Mode buffers. It's used to fill multiline string literals and both block and line style comments. In Java buffers, the Javadoc markup words are recognized as paragraph starters. The line oriented Pike autodoc markup words are recognized in the same way in Pike mode.

The function keeps the comment starters and enders of block comments as they were before the filling. This means that a comment ender on the same line as the paragraph being filled will be filled with the paragraph, and one on a line by itself will stay as it is. The comment starter is handled similarly(15).

M-j (c-indent-new-comment-line)
This is the replacement for indent-new-comment-line. It breaks the line at point and indents the new line like the current one.

If inside a comment and comment-multi-line is non-nil, the indentation and line prefix are preserved. If inside a comment and comment-multi-line is nil, a new comment of the same type is started on the next line and indented as appropriate for comments.

Note that CC Mode sets comment-multi-line to t at startup. The reason is that M-j could otherwise produce sequences of single line block comments for texts that should logically be treated as one comment, and the rest of the paragraph handling code (e.g. M-q and M-a) can't cope with that, which would lead to inconsistent behavior.

M-x c-context-line-break
This is a function that works like indent-new-comment-line in comments and newline-and-indent elsewhere, thus combining those two in a way that uses each one in the context it's best suited for. I.e. in comments the comment line prefix and indentation is kept for the new line, and in normal code it's indented according to context by the indentation engine.

In macros it acts like newline-and-indent but additionally inserts and optionally aligns the line ending backslash so that the macro remains unbroken. See section 6. Macro Handling, for details about the backslash alignment.

It's not bound to a key by default, but it's intended to be used on the RET key. If you like the behavior of newline-and-indent on RET, you should consider switching to this function.

M-x c-context-open-line
This is to C-o (M-x open-line) as c-context-line-break is to RET. I.e. it works just like c-context-line-break but leaves the point before the inserted line break.

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by XEmacs Webmaster on October, 2 2007 using texi2html