comparison man/programs.texi @ 67676:76931aba1936

Amend the file for CC Mode 5.31. In particular, relax the injunction that all braces/parens at column zero start defuns.
author Alan Mackenzie <acm@muc.de>
date Mon, 19 Dec 2005 22:21:41 +0000
parents 375a247b599e
children 6fc6fd3c71cd
comparison
equal deleted inserted replaced
67675:131310d593c0 67676:76931aba1936
126 place to set up customizations for that major mode. @xref{Hooks}. 126 place to set up customizations for that major mode. @xref{Hooks}.
127 127
128 @node Defuns 128 @node Defuns
129 @section Top-Level Definitions, or Defuns 129 @section Top-Level Definitions, or Defuns
130 130
131 In Emacs, a major definition at the top level in the buffer is 131 In Emacs, a major definition at the top level in the buffer,
132 called a @dfn{defun}. The name comes from Lisp, but in Emacs we use 132 something like a function, is called a @dfn{defun}. The name comes
133 it for all languages. 133 from Lisp, but in Emacs we use it for all languages.
134 134
135 In most programming language modes, Emacs assumes that a defun is 135 In many programming language modes, Emacs assumes that a defun is
136 any pair of parentheses (or braces, if the language uses braces this 136 any pair of parentheses (or braces, if the language uses braces this
137 way) that starts at the left margin. For example, in C, the body of a 137 way) that starts at the left margin. For example, in C, the body of a
138 function definition is normally a defun, because the open-brace that 138 function definition is a defun, usually recognized as an open-brace
139 begins it is normally at the left margin. A variable's initializer 139 that begins at the left margin@footnote{Alternatively, you can set up
140 can also count as a defun, if the open-brace that begins the 140 C Mode to recognize a defun at an opening brace at the outermost
141 initializer is at the left margin. 141 level. @xref{Movement Commands,,, ccmode, the CC Mode Manual}.}. A
142 variable's initializer can also count as a defun, if the open-brace
143 that begins the initializer is at the left margin.
142 144
143 However, some language modes provide their own code for recognizing 145 However, some language modes provide their own code for recognizing
144 defuns in a way that suits the language syntax and conventions better. 146 defuns in a way that suits the language syntax and conventions better.
145 147
146 @menu 148 @menu
154 @node Left Margin Paren 156 @node Left Margin Paren
155 @subsection Left Margin Convention 157 @subsection Left Margin Convention
156 158
157 @cindex open-parenthesis in leftmost column 159 @cindex open-parenthesis in leftmost column
158 @cindex ( in leftmost column 160 @cindex ( in leftmost column
159 In most major modes, Emacs assumes that any opening delimiter found 161 Emacs assumes by default that any opening delimiter found at the
160 at the left margin is the start of a top-level definition, or defun. 162 left margin is the start of a top-level definition, or defun. You can
161 Therefore, @strong{never put an opening delimiter at the left margin 163 override this default by setting this user option:
162 unless it should have that significance.} For instance, never put an 164
165 @defvar open-paren-in-column-0-is-defun-start
166 If this user option is set to @code{t} (the default), opening
167 parentheses or braces at column zero always start defuns. When it's
168 @code{nil}, defuns are found by searching for parens or braces at the
169 outermost level.
170 @end defvar
171
172 In buffers where @code{open-paren-in-column-0-is-defun-start} is
173 @code{t}, @strong{don't put an opening delimiter at the left margin
174 unless it is a defun start}. For instance, never put an
163 open-parenthesis at the left margin in a Lisp file unless it is the 175 open-parenthesis at the left margin in a Lisp file unless it is the
164 start of a top-level list. Never put an open-brace or other opening 176 start of a top-level list.
165 delimiter at the beginning of a line of C code unless it is at top
166 level.
167 177
168 If you don't follow this convention, not only will you have trouble 178 If you don't follow this convention, not only will you have trouble
169 when you explicitly use the commands for motion by defuns; other 179 when you explicitly use the commands for motion by defuns; other
170 features that use them will also give you trouble. This includes 180 features that use them will also give you trouble. This includes
171 the indentation commands (@pxref{Program Indent}) and Font Lock 181 the indentation commands (@pxref{Program Indent}) and Font Lock
172 mode (@pxref{Font Lock}). 182 mode (@pxref{Font Lock}).
173 183
174 The most likely problem case is when you want an opening delimiter 184 The most likely problem case is when you want an opening delimiter
175 at the start of a line inside a string. To avoid trouble, put an 185 at the start of a line inside a string. To avoid trouble, put an
176 escape character (@samp{\}, in C and Emacs Lisp, @samp{/} in some 186 escape character (@samp{\}, in Emacs Lisp, @samp{/} in some other Lisp
177 other Lisp dialects) before the opening delimiter. This will not 187 dialects) before the opening delimiter. This will not affect the
178 affect the contents of the string, but will prevent that opening 188 contents of the string, but will prevent that opening delimiter from
179 delimiter from starting a defun. Here's an example: 189 starting a defun. Here's an example:
180 190
181 @example 191 @example
182 (insert "Foo: 192 (insert "Foo:
183 \(bar) 193 \(bar)
184 ") 194 ")
185 @end example 195 @end example
186 196
187 To help you catch violations of this convention, Font Lock mode 197 To help you catch violations of this convention, Font Lock mode
188 highlights confusing opening delimiters (those that ought to be 198 highlights confusing opening delimiters (those that ought to be
189 quoted) in bold red. 199 quoted) in bold red.
200
201 Some major modes, including C and related modes, set
202 @code{open-paren-in-column-0-is-defun-start} buffer-locally to
203 @code{nil}, thus freeing you from all these restrictions. This makes
204 some commands run more slowly, though.
190 205
191 In the earliest days, the original Emacs found defuns by moving 206 In the earliest days, the original Emacs found defuns by moving
192 upward a level of parentheses or braces until there were no more 207 upward a level of parentheses or braces until there were no more
193 levels to go up. This always required scanning all the way back to 208 levels to go up. This always required scanning all the way back to
194 the beginning of the buffer, even for a small function. To speed up 209 the beginning of the buffer, even for a small function. To speed up
195 the operation, we changed Emacs to assume that any opening delimiter 210 the operation, we changed Emacs to assume that any opening delimiter
196 at the left margin is the start of a defun. This heuristic is nearly 211 at the left margin is the start of a defun. This heuristic is nearly
197 always right, and avoids the need to scan back to the beginning of the 212 always right, and avoids the need to scan back to the beginning of the
198 buffer. However, it mandates following the convention described 213 buffer. However, now that modern computers are so powerful, this
199 above. 214 scanning is rarely slow enough to annoy, so we've given you a way to
215 disable the heuristic.
200 216
201 @node Moving by Defuns 217 @node Moving by Defuns
202 @subsection Moving by Defuns 218 @subsection Moving by Defuns
203 @cindex defuns 219 @cindex defuns
204 220
397 nonstandard indentation, the lines below will tend to follow it. This 413 nonstandard indentation, the lines below will tend to follow it. This
398 behavior is convenient in cases where you have overridden the standard 414 behavior is convenient in cases where you have overridden the standard
399 result of @key{TAB} because you find it unaesthetic for a particular 415 result of @key{TAB} because you find it unaesthetic for a particular
400 line. 416 line.
401 417
402 Remember that an open-parenthesis, open-brace or other opening delimiter 418 By default, an open-parenthesis, open-brace or other opening
403 at the left margin is assumed by Emacs (including the indentation routines) 419 delimiter at the left margin is assumed by Emacs (including the
404 to be the start of a function. Therefore, you must never have an opening 420 indentation routines) to be the start of a function. This speeds up
405 delimiter in column zero that is not the beginning of a function, not even 421 indentation commands. If you will be editing text which contains
406 inside a string. This restriction is vital for making the indentation 422 opening delimiters in column zero that aren't the beginning of a
407 commands fast; you must simply accept it. @xref{Left Margin Paren}, 423 functions, even inside strings or comments, you must set
408 for more information on this. 424 @code{open-paren-in-column-0-is-defun-start}. @xref{Left Margin
425 Paren}, for more information on this.
409 426
410 Normally, lines are indented with tabs and spaces. If you want Emacs 427 Normally, lines are indented with tabs and spaces. If you want Emacs
411 to use spaces only, see @ref{Just Spaces}. 428 to use spaces only, see @ref{Just Spaces}.
412 429
413 @node Multi-line Indent 430 @node Multi-line Indent
558 @table @kbd 575 @table @kbd
559 @item C-c . @key{RET} @var{style} @key{RET} 576 @item C-c . @key{RET} @var{style} @key{RET}
560 Select a predefined style @var{style} (@code{c-set-style}). 577 Select a predefined style @var{style} (@code{c-set-style}).
561 @end table 578 @end table
562 579
563 A @dfn{style} is a named collection of customizations that can 580 A @dfn{style} is a named collection of customizations that can be
564 be used in C mode and the related modes. Emacs comes with several 581 used in C mode and the related modes. @ref{Styles,,, ccmode, The CC
582 Mode Manual}, for a complete description. Emacs comes with several
565 predefined styles, including @code{gnu}, @code{k&r}, @code{bsd}, 583 predefined styles, including @code{gnu}, @code{k&r}, @code{bsd},
566 @code{stroustrup}, @code{linux}, @code{python}, @code{java}, 584 @code{stroustrup}, @code{linux}, @code{python}, @code{java},
567 @code{whitesmith}, @code{ellemtel}, @code{cc-mode}, and @code{user}. 585 @code{whitesmith}, @code{ellemtel}, and @code{awk}. Some of these
568 Some of these styles are primarily intended for one language, but any 586 styles are primarily intended for one language, but any of them can be
569 of them can be used with any of the languages supported by these 587 used with any of the languages supported by these modes. To find out
570 modes. To find out what a style looks like, select it and reindent 588 what a style looks like, select it and reindent some code, e.g., by
571 some code, e.g., by typing @key{C-M-q} at the start of a function 589 typing @key{C-M-q} at the start of a function definition.
572 definition.
573 590
574 @kindex C-c . @r{(C mode)} 591 @kindex C-c . @r{(C mode)}
575 @findex c-set-style 592 @findex c-set-style
576 To choose a style for the current buffer, use the command @kbd{C-c 593 To choose a style for the current buffer, use the command @kbd{C-c
577 .}. Specify a style name as an argument (case is not significant). 594 .}. Specify a style name as an argument (case is not significant).
578 This command affects the current buffer only, and it affects only 595 This command affects the current buffer only, and it affects only
579 future invocations of the indentation commands; it does not reindent 596 future invocations of the indentation commands; it does not reindent
580 the code in the buffer. To reindent the whole buffer in the new 597 the code already in the buffer. To reindent the whole buffer in the
581 style, you can type @kbd{C-x h C-M-\}. 598 new style, you can type @kbd{C-x h C-M-\}.
582 599
583 @vindex c-default-style 600 @vindex c-default-style
584 You can also set the variable @code{c-default-style} to specify the 601 You can also set the variable @code{c-default-style} to specify the
585 default style for various major modes. Its value should be either the 602 default style for various major modes. Its value should be either the
586 style's name (a string) or an alist, in which each element specifies 603 style's name (a string) or an alist, in which each element specifies
587 one major mode and which indentation style to use for it. For 604 one major mode and which indentation style to use for it. For
588 example, 605 example,
589 606
590 @example 607 @example
591 (setq c-default-style 608 (setq c-default-style
592 '((java-mode . "java") (other . "gnu"))) 609 '((java-mode . "java") (awk-mode . "awk") (other . "gnu")))
593 @end example 610 @end example
594 611
595 @noindent 612 @noindent
596 specifies an explicit choice for Java mode, and the default @samp{gnu} 613 specifies explicit choices for Java and AWK modes, and the default
597 style for the other C-like modes. (These settings are actually the 614 @samp{gnu} style for the other C-like modes. (These settings are
598 defaults.) This variable takes effect when you select one of the 615 actually the defaults.) This variable takes effect when you select
599 C-like major modes; thus, if you specify a new default style for Java 616 one of the C-like major modes; thus, if you specify a new default
600 mode, you can make it take effect in an existing Java mode buffer by 617 style for Java mode, you can make it take effect in an existing Java
601 typing @kbd{M-x java-mode} there. 618 mode buffer by typing @kbd{M-x java-mode} there.
602 619
603 The @code{gnu} style specifies the formatting recommended by the GNU 620 The @code{gnu} style specifies the formatting recommended by the GNU
604 Project for C; it is the default, so as to encourage use of our 621 Project for C; it is the default, so as to encourage use of our
605 recommended style. 622 recommended style.
606 623
607 @xref{Customizing Indentation,,, ccmode, the CC Mode Manual}, for 624 @xref{Indentation Engine Basics,,, ccmode, the CC Mode Manual}, and
608 more information on customizing indentation for C and related modes, 625 @ref{Customizing Indentation,,, ccmode, the CC Mode Manual}, for more
626 information on customizing indentation for C and related modes,
609 including how to override parts of an existing style and how to define 627 including how to override parts of an existing style and how to define
610 your own styles. 628 your own styles.
611 629
612 @node Parentheses 630 @node Parentheses
613 @section Commands for Editing with Parentheses 631 @section Commands for Editing with Parentheses
706 @kindex C-M-t 724 @kindex C-M-t
707 @findex transpose-sexps 725 @findex transpose-sexps
708 A somewhat random-sounding command which is nevertheless handy is 726 A somewhat random-sounding command which is nevertheless handy is
709 @kbd{C-M-t} (@code{transpose-sexps}), which drags the previous 727 @kbd{C-M-t} (@code{transpose-sexps}), which drags the previous
710 balanced expression across the next one. An argument serves as a 728 balanced expression across the next one. An argument serves as a
711 repeat count, and a negative argument drags the previous balanced 729 repeat count, moving the previous expression over that many following
712 expression backwards across those before it (thus canceling out the 730 ones. A negative argument drags the previous balanced expression
713 effect of @kbd{C-M-t} with a positive argument). An argument of zero, 731 backwards across those before it (thus canceling out the effect of
714 rather than doing nothing, transposes the balanced expressions ending 732 @kbd{C-M-t} with a positive argument). An argument of zero, rather
715 at or after point and the mark. 733 than doing nothing, transposes the balanced expressions ending at or
734 after point and the mark.
716 735
717 @kindex C-M-@@ 736 @kindex C-M-@@
718 @kindex C-M-@key{SPC} 737 @kindex C-M-@key{SPC}
719 @findex mark-sexp 738 @findex mark-sexp
720 To set the region around the next balanced expression in the buffer, 739 To set the region around the next balanced expression in the buffer,
721 use @kbd{C-M-@@} (@code{mark-sexp}), which sets mark at the same place 740 use @kbd{C-M-@@} (@code{mark-sexp}), which sets mark at the same place
722 that @kbd{C-M-f} would move to. @kbd{C-M-@@} takes arguments like 741 that @kbd{C-M-f} would move to. @kbd{C-M-@@} takes arguments like
723 @kbd{C-M-f}. In particular, a negative argument is useful for putting 742 @kbd{C-M-f}. In particular, a negative argument is useful for putting
724 the mark at the beginning of the previous balanced expression. The 743 the mark at the beginning of the previous balanced expression. The
725 alias @kbd{C-M-@key{SPC}} is equivalent to @kbd{C-M-@@}. If you use 744 alias @kbd{C-M-@key{SPC}} is equivalent to @kbd{C-M-@@}. When you
726 this command repeatedly, or in Transient Mark mode whenever the mark 745 repeat this command, or use it in Transient Mark mode when the mark is
727 is active, it extends the region by one sexp each time. 746 active, it extends the region by one sexp each time.
728 747
729 In languages that use infix operators, such as C, it is not possible 748 In languages that use infix operators, such as C, it is not possible
730 to recognize all balanced expressions as such because there can be 749 to recognize all balanced expressions as such because there can be
731 multiple possibilities at a given position. For example, C mode does 750 multiple possibilities at a given position. For example, C mode does
732 not treat @samp{foo + bar} as a single expression, even though it 751 not treat @samp{foo + bar} as a single expression, even though it
873 @item @kbd{C-x ;} 892 @item @kbd{C-x ;}
874 Set comment column (@code{comment-set-column}). 893 Set comment column (@code{comment-set-column}).
875 @item @kbd{C-M-j} 894 @item @kbd{C-M-j}
876 @itemx @kbd{M-j} 895 @itemx @kbd{M-j}
877 Like @key{RET} followed by inserting and aligning a comment 896 Like @key{RET} followed by inserting and aligning a comment
878 (@code{comment-indent-new-line}). 897 (@code{comment-indent-new-line}). @xref{Multi-Line Comments}.
879 @item @kbd{M-x comment-region} 898 @item @kbd{M-x comment-region}
880 @itemx @kbd{C-c C-c} (in C-like modes) 899 @itemx @kbd{C-c C-c} (in C-like modes)
881 Add or remove comment delimiters on all the lines in the region. 900 Add or remove comment delimiters on all the lines in the region.
882 @end table 901 @end table
883 902
946 ;;; And now, the first part of the function: 965 ;;; And now, the first part of the function:
947 ;; The following line adds one. 966 ;; The following line adds one.
948 (1+ x)) ; This line adds one. 967 (1+ x)) ; This line adds one.
949 @end example 968 @end example
950 969
951 In C code, a comment preceded on its line by nothing but whitespace 970 For C-like buffers, you can configure the exact effect of @kbd{M-;}
952 is indented like a line of code. 971 more flexibly than for most buffers by setting the user options
972 @code{c-indent-comment-alist} and
973 @code{c-indent-comments-syntactically-p}. For example, on a line
974 ending in a closing brace, @kbd{M-;} puts the comment one space after
975 the brace rather than at @code{comment-column}. For full details see
976 @ref{Comment Commands,,, ccmode, The CC Mode Manual}.
953 977
954 @node Multi-Line Comments 978 @node Multi-Line Comments
955 @subsection Multiple Lines of Comments 979 @subsection Multiple Lines of Comments
956 980
957 @kindex C-M-j 981 @kindex C-M-j
958 @kindex M-j 982 @kindex M-j
959 @cindex blank lines in programs 983 @cindex blank lines in programs
960 @findex comment-indent-new-line 984 @findex comment-indent-new-line
985
961 If you are typing a comment and wish to continue it on another line, 986 If you are typing a comment and wish to continue it on another line,
962 you can use the command @kbd{C-M-j} or @kbd{M-j} 987 you can use the command @kbd{C-M-j} or @kbd{M-j}
963 (@code{comment-indent-new-line}). This terminates the comment you are 988 (@code{comment-indent-new-line}). This terminates the comment you are
964 typing, creates a new blank line afterward, and begins a new comment 989 typing, creates a new blank line afterward, and begins a new comment
965 indented under the old one. When Auto Fill mode is on, going past the 990 indented under the old one. Or, if the language syntax permits
966 fill column while typing a comment causes the comment to be continued 991 comments to extend beyond ends of lines, it may instead continue the
967 in just this fashion. If point is not at the end of the line when you 992 existing comment on the new blank line---this is controlled by the
968 type the command, the text on the rest of the line becomes part of the 993 setting of @code{comment-multi-line} (@pxref{Options for Comments}).
969 new comment line. 994 If point is not at the end of the line when you type the command, the
995 text on the rest of the line becomes part of the new comment line.
996 When Auto Fill mode is on, going past the fill column while typing a
997 comment causes the comment to be continued in just this fashion.
970 998
971 @kindex C-c C-c (C mode) 999 @kindex C-c C-c (C mode)
972 @findex comment-region 1000 @findex comment-region
973 To turn existing lines into comment lines, use the @kbd{M-x 1001 To turn existing lines into comment lines, use the @kbd{M-x
974 comment-region} command. It adds comment delimiters to the lines that start 1002 comment-region} command (or type @kbd{C-c C-c} in C-like buffers). It
975 in the region, thus commenting them out. With a negative argument, it 1003 adds comment delimiters to the lines that start in the region, thus
976 does the opposite---it deletes comment delimiters from the lines in the 1004 commenting them out. With a negative argument, it does the
977 region. 1005 opposite---it deletes comment delimiters from the lines in the region.
978 1006
979 With a positive argument, @code{comment-region} duplicates the last 1007 With a positive argument, @code{comment-region} duplicates the last
980 character of the comment start sequence it adds; the argument specifies 1008 character of the comment start sequence it adds; the argument specifies
981 how many copies of the character to insert. Thus, in Lisp mode, 1009 how many copies of the character to insert. Thus, in Lisp mode,
982 @kbd{C-u 2 M-x comment-region} adds @samp{;;} to each line. Duplicating 1010 @kbd{C-u 2 M-x comment-region} adds @samp{;;} to each line. Duplicating
983 the comment delimiter is a way of calling attention to the comment. It 1011 the comment delimiter is a way of calling attention to the comment. It
984 can also affect how the comment is indented. In Lisp, for proper 1012 can also affect how the comment is indented. In Lisp, for proper
985 indentation, you should use an argument of two or three, if between defuns; 1013 indentation, you should use an argument of two or three, if between defuns;
986 if within a defun, it must be three. 1014 if within a defun, it must be three.
1015
1016 You can configure C Mode such that when you type a @samp{/} at the
1017 start of a line in a multi-line block comment, this closes the
1018 comment. Enable the @code{comment-close-slash} clean-up for this.
1019 @xref{Clean-ups,,, ccmode, The CC Mode Manual}.
987 1020
988 @node Options for Comments 1021 @node Options for Comments
989 @subsection Options Controlling Comments 1022 @subsection Options Controlling Comments
990 1023
991 @vindex comment-column 1024 @vindex comment-column
1036 can hold the actual string to insert. 1069 can hold the actual string to insert.
1037 1070
1038 @vindex comment-multi-line 1071 @vindex comment-multi-line
1039 The variable @code{comment-multi-line} controls how @kbd{C-M-j} 1072 The variable @code{comment-multi-line} controls how @kbd{C-M-j}
1040 (@code{indent-new-comment-line}) behaves when used inside a comment. 1073 (@code{indent-new-comment-line}) behaves when used inside a comment.
1041 Specifically, when @code{comment-multi-line} is @code{nil} (the 1074 Specifically, when @code{comment-multi-line} is @code{nil}, the
1042 default value), the command inserts a comment terminator, begins a new 1075 command inserts a comment terminator, begins a new line, and finally
1043 line, and finally inserts a comment starter. Otherwise it does not 1076 inserts a comment starter. Otherwise it does not insert the
1044 insert the terminator and starter, so it effectively continues the 1077 terminator and starter, so it effectively continues the current
1045 current comment across multiple lines. In languages that allow 1078 comment across multiple lines. In languages that allow multi-line
1046 multi-line comments, the choice of value for this variable is a matter 1079 comments, the choice of value for this variable is a matter of taste.
1047 of taste. 1080 The default for this variable depends on the major mode.
1048 1081
1049 @vindex comment-indent-function 1082 @vindex comment-indent-function
1050 The variable @code{comment-indent-function} should contain a function 1083 The variable @code{comment-indent-function} should contain a function
1051 that will be called to compute the indentation for a newly inserted 1084 that will be called to compute the indentation for a newly inserted
1052 comment or for aligning an existing comment. It is set differently by 1085 comment or for aligning an existing comment. It is set differently by
1508 @kbd{M-a} except that it moves in the other direction 1541 @kbd{M-a} except that it moves in the other direction
1509 (@code{c-end-of-statement}). 1542 (@code{c-end-of-statement}).
1510 1543
1511 @item M-x c-backward-into-nomenclature 1544 @item M-x c-backward-into-nomenclature
1512 @findex c-backward-into-nomenclature 1545 @findex c-backward-into-nomenclature
1513 Move point backward to beginning of a C++ nomenclature section or word. 1546 Move point backward to beginning of a C++ nomenclature section or
1514 With prefix argument @var{n}, move @var{n} times. If @var{n} is 1547 word. With prefix argument @var{n}, move @var{n} times. If @var{n}
1515 negative, move forward. C++ nomenclature means a symbol name in the 1548 is negative, move forward. C++ nomenclature means a symbol name in
1516 style of NamingSymbolsWithMixedCaseAndNoUnderlines; each capital letter 1549 the style of NamingSymbolsWithMixedCaseAndNoUnderlines; each capital
1517 begins a section or word. 1550 letter begins a section or word. Rather than this command, you might
1551 well prefer the newer ``Subword Mode'', which does the same thing
1552 better. @xref{Other C Commands}.
1518 1553
1519 In the GNU project, we recommend using underscores to separate words 1554 In the GNU project, we recommend using underscores to separate words
1520 within an identifier in C or C++, rather than using case distinctions. 1555 within an identifier in C or C++, rather than using case distinctions.
1521 1556
1522 @item M-x c-forward-into-nomenclature 1557 @item M-x c-forward-into-nomenclature
1527 1562
1528 @node Electric C 1563 @node Electric C
1529 @subsection Electric C Characters 1564 @subsection Electric C Characters
1530 1565
1531 In C mode and related modes, certain printing characters are 1566 In C mode and related modes, certain printing characters are
1532 ``electric''---in addition to inserting themselves, they also reindent 1567 @dfn{electric}---in addition to inserting themselves, they also
1533 the current line, and optionally also insert newlines. The 1568 reindent the current line, and optionally also insert newlines. The
1534 ``electric'' characters are @kbd{@{}, @kbd{@}}, @kbd{:}, @kbd{#}, 1569 ``electric'' characters are @kbd{@{}, @kbd{@}}, @kbd{:}, @kbd{#},
1535 @kbd{;}, @kbd{,}, @kbd{<}, @kbd{>}, @kbd{/}, @kbd{*}, @kbd{(}, and 1570 @kbd{;}, @kbd{,}, @kbd{<}, @kbd{>}, @kbd{/}, @kbd{*}, @kbd{(}, and
1536 @kbd{)}. 1571 @kbd{)}. @xref{Electric Keys,,, ccmode, The CC Mode Manual}.
1537 1572
1538 Electric characters insert newlines only when the @dfn{auto-newline} 1573 You might find electric indentation inconvenient if you are editing
1539 feature is enabled (indicated by @samp{/a} in the mode line after the 1574 chaotically indented code. If you are new to CC Mode, you might find
1540 mode name). This feature is controlled by the variable 1575 it disconcerting. You can toggle electric action with the command
1541 @code{c-auto-newline}. You can turn this feature on or off with the 1576 @kbd{C-c C-l}; when it is enabled, @samp{/l} appears in the mode line
1542 command @kbd{C-c C-a}: 1577 after the mode name:
1578
1579 @table @kbd
1580 @item C-c C-l
1581 @kindex C-c C-l @r{(C mode)}
1582 @findex c-toggle-electric-state
1583 Toggle electric action (@code{c-toggle-electric-state}). With a
1584 prefix argument, this command enables electric action if the argument
1585 is positive, disables it if it is negative.
1586 @end table
1587
1588 Electric characters insert newlines only when, in addition to the
1589 electric state, the @dfn{auto-newline} feature is enabled (indicated
1590 by @samp{/la} in the mode line after the mode name). You can turn
1591 this feature on or off with the command @kbd{C-c C-a}:
1543 1592
1544 @table @kbd 1593 @table @kbd
1545 @item C-c C-a 1594 @item C-c C-a
1546 @kindex C-c C-a @r{(C mode)} 1595 @kindex C-c C-a @r{(C mode)}
1547 @findex c-toggle-auto-state 1596 @findex c-toggle-auto-newline
1548 Toggle the auto-newline feature (@code{c-toggle-auto-state}). With a 1597 Toggle the auto-newline feature (@code{c-toggle-auto-newline}). With a
1549 prefix argument, this command turns the auto-newline feature on if the 1598 prefix argument, this command turns the auto-newline feature on if the
1550 argument is positive, and off if it is negative. 1599 argument is positive, and off if it is negative.
1551 @end table 1600 @end table
1601
1602 Usually the CC Mode style system (@pxref{Styles,,, ccmode, The CC
1603 Mode Manual}) configures the exact circumstances in which Emacs
1604 inserts auto-newlines, but you can configure this directly instead.
1605 Full details are at @ref{Custom Auto-newlines,,, ccmode, The CC Mode
1606 Manual}, but there is a short summary below.
1552 1607
1553 The colon character is electric because that is appropriate for a 1608 The colon character is electric because that is appropriate for a
1554 single colon. But when you want to insert a double colon in C++, the 1609 single colon. But when you want to insert a double colon in C++, the
1555 electric behavior of colon is inconvenient. You can insert a double 1610 electric behavior of colon is inconvenient. You can insert a double
1556 colon with no reindentation or newlines by typing @kbd{C-c :}: 1611 colon with no reindentation or newlines by typing @kbd{C-c :}:
1606 inserted. 1661 inserted.
1607 1662
1608 @vindex c-cleanup-list 1663 @vindex c-cleanup-list
1609 Electric characters can also delete newlines automatically when the 1664 Electric characters can also delete newlines automatically when the
1610 auto-newline feature is enabled. This feature makes auto-newline more 1665 auto-newline feature is enabled. This feature makes auto-newline more
1611 acceptable, by deleting the newlines in the most common cases where you 1666 acceptable, by deleting the newlines in the most common cases where
1612 do not want them. Emacs can recognize several cases in which deleting a 1667 you do not want them. Emacs can recognize several cases in which
1613 newline might be desirable; by setting the variable 1668 deleting a newline might be desirable; by setting the variable
1614 @code{c-cleanup-list}, you can specify @emph{which} of these cases that 1669 @code{c-cleanup-list}, you can specify @emph{which} of these cases
1615 should happen. The variable's value is a list of symbols, each 1670 that should happen. @xref{Clean-ups,,, ccmode, The CC Mode Manual}.
1616 describing one case for possible deletion of a newline. Here are the 1671 The variable's value is a list of symbols, each describing one case
1617 meaningful symbols, and their meanings: 1672 for possible deletion of a newline. Here is a summary of the
1673 meaningful symbols and their meanings:
1618 1674
1619 @table @code 1675 @table @code
1620 @item brace-catch-brace 1676 @item brace-catch-brace
1621 Clean up @samp{@} catch (@var{condition}) @{} constructs by placing the 1677 Clean up @samp{@} catch (@var{condition}) @{} constructs by placing the
1622 entire construct on a single line. The clean-up occurs when you type 1678 entire construct on a single line. The clean-up occurs when you type
1646 1702
1647 @item list-close-comma 1703 @item list-close-comma
1648 Clean up commas following braces in array and aggregate 1704 Clean up commas following braces in array and aggregate
1649 initializers. Clean-up occurs when you type the comma. 1705 initializers. Clean-up occurs when you type the comma.
1650 1706
1707 @item one-line-defun
1708 Remove space and newlines from a defun if this would leave it short
1709 enough to fit on a single line. This is useful for AWK pattern/action
1710 pairs. ``Short enough'' means not longer than the value of the user
1711 option @code{c-max-one-liner-length}. Clean-up occurs when you type
1712 the closing brace.
1713
1651 @item scope-operator 1714 @item scope-operator
1652 Clean up double colons which may designate a C++ scope operator, by 1715 Clean up double colons which may designate a C++ scope operator, by
1653 placing the colons together. Clean-up occurs when you type the second 1716 placing the colons together. Clean-up occurs when you type the second
1654 colon, but only when the two colons are separated by nothing but 1717 colon, but only when the two colons are separated by nothing but
1655 whitespace. 1718 whitespace.
1657 1720
1658 @node Hungry Delete 1721 @node Hungry Delete
1659 @subsection Hungry Delete Feature in C 1722 @subsection Hungry Delete Feature in C
1660 @cindex hungry deletion (C Mode) 1723 @cindex hungry deletion (C Mode)
1661 1724
1662 When the @dfn{hungry-delete} feature is enabled (indicated by 1725 If you want to delete an entire block of whitespace at point, you
1663 @samp{/h} or @samp{/ah} in the mode line after the mode name), a single 1726 can use @dfn{hungry deletion}. This deletes all the contiguous
1664 @key{DEL} command deletes all preceding whitespace, not just one space. 1727 whitespace either before point or after point in a single operation.
1665 To turn this feature on or off, use @kbd{C-c C-d}: 1728 @dfn{Whitespace} here includes tabs and newlines, but not comments or
1729 preprocessor commands.
1666 1730
1667 @table @kbd 1731 @table @kbd
1732 @item C-c C-@key{BS}
1733 @itemx C-c @key{BS}
1734 @findex c-hungry-backspace
1735 @kindex C-c C-@key{BS} (C Mode)
1736 @kindex C-c @key{BS} (C Mode)
1737 @code{c-hungry-backspace}---Delete the entire block of whitespace
1738 preceding point.
1739
1668 @item C-c C-d 1740 @item C-c C-d
1669 @kindex C-c C-d @r{(C mode)} 1741 @itemx C-c C-@key{DEL}
1742 @itemx C-c @key{DEL}
1743 @findex c-hungry-delete-forward
1744 @kindex C-c C-d (C Mode)
1745 @kindex C-c C-@key{DEL} (C Mode)
1746 @kindex C-c @key{DEL} (C Mode)
1747 @code{c-hungry-delete-forward}---Delete the entire block of whitespace
1748 following point.
1749 @end table
1750
1751 As an alternative to the above commands, you can enable @dfn{hungry
1752 delete mode}. When this feature is enabled (indicated by @samp{/h} in
1753 the mode line after the mode name), a single @key{BS} command deletes
1754 all preceding whitespace, not just one space, and a single @kbd{C-c
1755 C-d} (but @emph{not} @key{delete}) deletes all following whitespace.
1756
1757 @table @kbd
1758 @item M-x c-toggle-hungry-state
1670 @findex c-toggle-hungry-state 1759 @findex c-toggle-hungry-state
1671 Toggle the hungry-delete feature (@code{c-toggle-hungry-state}). With a 1760 Toggle the hungry-delete feature
1672 prefix argument, this command turns the hungry-delete feature on if the 1761 (@code{c-toggle-hungry-state})@footnote{This command had the binding
1673 argument is positive, and off if it is negative. 1762 @kbd{C-c C-d} in earlier versions of Emacs. @kbd{C-c C-d} is now
1674 1763 bound to @code{c-hungry-delete-forward}.}. With a prefix argument,
1675 @item C-c C-t 1764 this command turns the hungry-delete feature on if the argument is
1676 @kindex C-c C-t @r{(C mode)} 1765 positive, and off if it is negative.
1677 @findex c-toggle-auto-hungry-state
1678 Toggle the auto-newline and hungry-delete features, both at once
1679 (@code{c-toggle-auto-hungry-state}).
1680 @end table 1766 @end table
1681 1767
1682 @vindex c-hungry-delete-key 1768 @vindex c-hungry-delete-key
1683 The variable @code{c-hungry-delete-key} controls whether the 1769 The variable @code{c-hungry-delete-key} controls whether the
1684 hungry-delete feature is enabled. 1770 hungry-delete feature is enabled.
1685 1771
1686 @node Other C Commands 1772 @node Other C Commands
1687 @subsection Other Commands for C Mode 1773 @subsection Other Commands for C Mode
1688 1774
1689 @table @kbd 1775 @table @kbd
1776 @item C-c C-w
1777 @itemx M-x c-subword-mode
1778 @findex c-subword-mode
1779 Enable (or disable) @dfn{subword mode} - Emacs's word commands then
1780 recognize upper case letters in @samp{StudlyCapsIdentifiers} as word
1781 boundaries. This is indicated by the flag @samp{/w} on the mode line
1782 after the mode name (e.g. @samp{C/law}). You can even use @kbd{M-x
1783 c-subword-mode} in non-CC Mode buffers.
1784
1690 @item M-x c-context-line-break 1785 @item M-x c-context-line-break
1691 @findex c-context-line-break 1786 @findex c-context-line-break
1692 This command inserts a line break and indents the new line in a manner 1787 This command inserts a line break and indents the new line in a manner
1693 appropriate to the context. In normal code, it does the work of 1788 appropriate to the context. In normal code, it does the work of
1694 @kbd{C-j} (@code{newline-and-indent}), in a C preprocessor line it 1789 @kbd{C-j} (@code{newline-and-indent}), in a C preprocessor line it
1695 additionally inserts a @samp{\} at the line break, and within comments 1790 additionally inserts a @samp{\} at the line break, and within comments
1696 it's like @kbd{M-j} (@code{c-indent-new-comment-line}). 1791 it's like @kbd{M-j} (@code{c-indent-new-comment-line}).
1697 1792
1698 @code{c-context-line-break} isn't bound to a key by default, but it 1793 @code{c-context-line-break} isn't bound to a key by default, but it
1699 needs a binding to be useful. The following code will bind it to 1794 needs a binding to be useful. The following code will bind it to
1700 @kbd{C-j}. 1795 @kbd{C-j}. We use @code{c-initialization-hook} here to make sure
1796 the keymap is loaded before we try to change it.
1797
1701 @example 1798 @example
1702 (define-key c-mode-base-map "\C-j" 'c-context-line-break) 1799 (defun my-bind-clb ()
1800 (define-key c-mode-base-map "\C-j" 'c-context-line-break))
1801 (add-hook 'c-initialization-hook 'my-bind-clb)
1703 @end example 1802 @end example
1704 1803
1705 @item C-M-h 1804 @item C-M-h
1706 Put mark at the end of a function definition, and put point at the 1805 Put mark at the end of a function definition, and put point at the
1707 beginning (@code{c-mark-function}). 1806 beginning (@code{c-mark-function}).