26.12.4 Other Commands for C Mode
M-x c-context-line-break
β
This command inserts a line break and indents the new line in a manner appropriate to the context. In normal code, it does the work of RET
(newline
), in a C preprocessor line it additionally inserts a β\
β at the line break, and within comments itβs like M-j
(c-indent-new-comment-line
).
c-context-line-break
isnβt bound to a key by default, but it needs a binding to be useful. The following code will bind it to RET
. We use c-initialization-hook
here to make sure the keymap is loaded before we try to change it.
(defun my-bind-clb ()
(define-key c-mode-base-map "\C-m"
'c-context-line-break))
(add-hook 'c-initialization-hook 'my-bind-clb)
C-M-h
β
Put mark at the end of a function definition, and put point at the beginning (c-mark-function
).
M-q
β
Fill a paragraph, handling C and C++ comments (c-fill-paragraph
). If any part of the current line is a comment or within a comment, this command fills the comment or the paragraph of it that point is in, preserving the comment indentation and comment delimiters.
C-c C-e
β
Run the C preprocessor on the text in the region, and show the result, which includes the expansion of all the macro calls (c-macro-expand
). The buffer text before the region is also included in preprocessing, for the sake of macros defined there, but the output from this part isnβt shown.
When you are debugging C code that uses macros, sometimes it is hard to figure out precisely how the macros expand. With this command, you donβt have to figure it out; you can see the expansions.
C-c C-\
β
Insert or align β\
β characters at the ends of the lines of the region (c-backslash-region
). This is useful after writing or editing a C macro definition.
If a line already ends in β\
β, this command adjusts the amount of whitespace before it. Otherwise, it inserts a new β\
β. However, the last line in the region is treated specially; no β\
β is inserted on that line, and any β\
β there is deleted.
M-x cpp-highlight-buffer
β
Highlight parts of the text according to its preprocessor conditionals. This command displays another buffer named *CPP Edit*
, which serves as a graphic menu for selecting how to display particular kinds of conditionals and their contents. After changing various settings, click on β[A]pply these settings
β (or go to that buffer and type a
) to rehighlight the C mode buffer accordingly.
C-c C-s
β
Display the syntactic information about the current source line (c-show-syntactic-information
). This information directs how the line is indented.
M-x cwarn-mode
β
M-x global-cwarn-mode
β
CWarn minor mode highlights certain suspicious C and C++ constructions:
- Assignments inside expressions.
- Semicolon following immediately after β
if
β, βfor
β, and βwhile
β (except after a βdo β¦ while
β statement); - C++ functions with reference parameters.
You can enable the mode for one buffer with the command M-x cwarn-mode
, or for all suitable buffers with the command M-x global-cwarn-mode
or by customizing the variable global-cwarn-mode
. You must also enable Font Lock mode to make it work.
M-x hide-ifdef-mode
β
Hide-ifdef minor mode hides selected code within β#if
β and β#ifdef
β preprocessor blocks. If you change the variable hide-ifdef-shadow
to t
, Hide-ifdef minor mode shadows preprocessor blocks by displaying them with a less prominent face, instead of hiding them entirely. See the documentation string of hide-ifdef-mode
for more information.
M-x ff-find-related-file
β
Find a file related in a special way to the file visited by the current buffer. Typically this will be the header file corresponding to a C/C++ source file, or vice versa. The variable ff-related-file-alist
specifies how to compute related file names.