Overview
As described in the Emacs document, there are only two steps to implement the contraction style: the first step is to find the corresponding type of the contraction style based on the content and context; the second step is, the offset of the expression anchor Based on the contraction style. The following describes how to customize the CC-mode style.
Type of the scale-in style
For example, in C code, use c-c. then you can use the tab twice to create multiple built-in styles for selection. Each style has more or less set the import and contraction offset, which is different from each other, it shows different import and contraction styles.
Offset
The offset is generated by setting the variable. There are two very important variables
c-basic-offset
c-offsets-alist
This is the details of the former, it is a common integer; the detailed description of the latter here, this is an association list, stores the key-value pairs of the syntax symbol and offset, use
c-set-offset
Modify the elements. The following describes how to modify the offset associated with these operators,
(setq c-basic-offset 4) (c-set-offset ‘substatement-open 0) (c-set-offset ‘arglist-intro ‘+) (c-set-offset ‘arglist-cont-nonempty ‘+) (c-set-offset ‘statement-case-intro ‘+) (c-set-offset ‘case-label 0) (c-set-offset ‘statement-case-open 0)
The "+" is described in detail here. It is nothing more than the addition, subtraction, multiplication, and division of the value of the C-Basic-offset variable ", it indicates the offset of 1 x C-Basic-offset, where 4 is used. In addition, there are many syntax Symbol names, so we do not need to master them all. If we need to compress a given statement, but we do not know the name of its syntax symbol, use c-S on the relevant line to know the name of its syntax symbol, and then modify it accordingly.
Style example
(setq c-basic-offset 4) (c-set-offset ‘substatement-open 0) (c-set-offset ‘statement-case-intro ‘+) (c-set-offset ‘case-label 0) (c-set-offset ‘statement-case-open 0)
The style above can achieve the following results,
switch (a) { case 1: break; default: break; }
The complete code is as follows:
(defun my-c-mode-hook () (setq c-basic-offset 4) (c-set-offset ‘substatement-open 0) (c-set-offset ‘statement-case-intro ‘+) (c-set-offset ‘case-label 0) (c-set-offset ‘statement-case-open 0))(add-hook ‘c-mode-common-hook ‘my-c-mode-hook)
Define your own style name
We must have such a requirement that different coding styles are used in different projects, and the built-in styles in Emacs cannot meet the requirements, so we can customize different styles, we recommend that you use this method. In short, you can customize it by modifying the built-in style, saving time and effort,
(defun my-c-mode-hook () (c-add-style "ownstyle" ‘( "k&r" (c-basic-offset . 4))))(add-hook ‘c-mode-common-hook ‘my-c-mode-hook)
Only the basic scale-in K & R is modified to 4, and other styles are consistent with K & R styles. Restart Emacs, open the c file, and use the c-c. Tab tab to view the ownstyle.