4.5 Minor Modes

CC Mode contains several minor-mode-like features that you might find useful while writing new code or editing old code:

comment style

This specifies whether comment commands (such as M-;) insert block comments or line comments8.

electric mode

When this is enabled, certain visible characters cause reformatting as they are typed. This is normally helpful, but can be a nuisance when editing chaotically formatted code. It can also be disconcerting, especially for users who are new to CC Mode.

auto-newline mode

This automatically inserts newlines where you’d probably want to type them yourself, e.g. after typing ‘}’s. Its action is suppressed when electric mode is disabled.

hungry-delete mode

This lets you delete a contiguous block of whitespace with a single key - for example, the newline and indentation just inserted by auto-newline when you want to back up and write a comment after the last statement.

subword mode

This mode makes basic word movement commands like M-f (forward-word) and M-b (backward-word) treat the parts of sillycapsed symbols as different words. E.g. ‘NSGraphicsContext’ is treated as three words ‘NS’, ‘Graphics’, and ‘Context’.

syntactic-indentation mode

When this is enabled (which it normally is), indentation commands such as C-j indent lines of code according to their syntactic structure. Otherwise, a line is simply indented to the same level as the previous one and TAB adjusts the indentation in steps of ‘c-basic-offset’.

Full details on how these minor modes work are at Electric Keys and Keywords, Auto-newline Insertion, Hungry Deletion of Whitespace, Subword Movement and Editing, and Indentation Engine Basics.

You can toggle each of these minor modes on and off, and you can configure CC Mode so that it starts up with your favorite combination of them (see Sample .emacs File). By default, when you initialize a buffer, the comment style is set to the default for the major mode, electric mode and syntactic-indentation mode are enabled, but the other two modes are disabled.

CC Mode displays the current state of the first five of these minor modes on the mode line by appending characters to the major mode’s name: ‘/’ or ‘*’ to indicate the comment style (respectively line or block), and one letter for each of the other minor modes which is enabled - ‘l’ for electric mode, ‘a’ for auto-newline mode, ‘h’ for hungry delete mode, and ‘w’ for subword mode. If the comment style was block and all the other modes were enabled, you’d see ‘C/*lahw9.

Here are the commands to toggle these modes:

C-c C-k (c-toggle-comment-style)

Toggle the comment style between line style and block style. In modes (such as AWK Mode) which only have one of these styles, this function does nothing.

C-c C-l (c-toggle-electric-state)

Toggle electric minor mode. When the command turns the mode off, it also suppresses auto-newline mode.

C-c C-a (c-toggle-auto-newline)

Toggle auto-newline minor mode. When the command turns the mode on, it also enables electric minor mode.

M-x c-toggle-hungry-state10

Toggle hungry-delete minor mode.

M-x c-toggle-auto-hungry-state11

Toggle both auto-newline and hungry delete minor modes.

C-c C-w (M-x c-subword-mode)

Toggle subword mode.

M-x c-toggle-syntactic-indentation

Toggle syntactic-indentation mode.

Common to all the toggle functions above is that if they are called programmatically, they take an optional numerical argument. For c-toggle-comment style, a positive value will select block comments, a negative value will select line comments. For the other functions, a positive value will turn on the minor mode (or both of them in the case of c-toggle-auto-hungry-state) and a negative value will turn it (or them) off.


Footnotes

(8)

You can emphasize non-default style comments in your code by giving their delimiters font-lock-warning-face. See Marking “Wrong” style comments.

(9)

The ‘C’ would be replaced with the name of the language in question for the other languages CC Mode supports.

(10)

Prior to CC Mode 5.31, this command was bound to C-c C-d.

(11)

Prior to CC Mode 5.31, this command was bound to C-c C-t.