📄 indent.c
字号:
(type_code != preesc) && (type_code != form_feed)) { if (force_nl && (type_code != semicolon) && (type_code != lbrace || !btype_2)) { /* we should force a broken line here */ if (verbose && !flushed_nl) diag(0, "Line broken"); flushed_nl = false; dump_line(); ps.want_blank = false; /* dont insert blank at line start */ force_nl = false; } ps.in_stmt = true; /* turn on flag which causes an extra level of indentation. this is turned off by a ; or '}' */ if (s_com != e_com) { /* the turkey has embedded a comment in a line. fix it */ *e_code++ = ' '; for (t_ptr = s_com; *t_ptr; ++t_ptr) { if (e_code >= l_code) { register nsize = l_code - s_code + 400; codebuf = (char *) realloc(codebuf, nsize); e_code = codebuf + (e_code - s_code) + 1; l_code = codebuf + nsize - 5; s_code = codebuf + 1; } *e_code++ = *t_ptr; } *e_code++ = ' '; *e_code = '\0'; /* null terminate code sect */ ps.want_blank = false; e_com = s_com; } } else if (type_code != comment) /* preserve force_nl thru a comment */ force_nl = false; /* cancel forced newline after newline, form feed, etc */ /*-----------------------------------------------------*\ | do switch on type of token scanned | \*-----------------------------------------------------*/ if (e_code >= l_code) { register nsize = l_code - s_code + 400; codebuf = (char *) realloc(codebuf, nsize); e_code = codebuf + (e_code - s_code) + 1; l_code = codebuf + nsize - 5; s_code = codebuf + 1; } switch (type_code) { /* now, decide what to do with the token */ case form_feed: /* found a form feed in line */ ps.use_ff = true; /* a form feed is treated much like a newline */ dump_line(); ps.want_blank = false; break; case newline: if (ps.last_token != comma || ps.p_l_follow > 0 || !ps.leave_comma || ps.block_init || !break_comma || s_com != e_com) { dump_line(); ps.want_blank = false; } ++line_no; /* keep track of input line number */ break; case lparen: /* got a '(' or '[' */ ++ps.p_l_follow; /* count parens to make Healy happy */ if (ps.want_blank && *token != '[' && (ps.last_token != ident || proc_calls_space || (ps.its_a_keyword && (!ps.sizeof_keyword || Bill_Shannon)))) *e_code++ = ' '; if (ps.in_decl && !ps.block_init) if (troff && !ps.dumped_decl_indent && !is_procname && ps.last_token == decl) { ps.dumped_decl_indent = 1; sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token); e_code += strlen(e_code); } else { while ((e_code - s_code) < dec_ind) { if (e_code >= l_code) { register nsize = l_code - s_code + 400; codebuf = (char *) realloc(codebuf, nsize); e_code = codebuf + (e_code - s_code) + 1; l_code = codebuf + nsize - 5; s_code = codebuf + 1; } *e_code++ = ' '; } *e_code++ = token[0]; } else *e_code++ = token[0]; ps.paren_indents[ps.p_l_follow - 1] = e_code - s_code; if (sp_sw && ps.p_l_follow == 1 && ex_expr_indent && ps.paren_indents[0] < 2 * ps.ind_size) ps.paren_indents[0] = 2 * ps.ind_size; ps.want_blank = false; if (ps.in_or_st && *token == '(' && ps.tos <= 2) { /* this is a kluge to make sure that declarations will be aligned right if proc decl has an explicit type on it, i.e. "int a(x) {..." */ parse(semicolon); /* I said this was a kluge... */ ps.in_or_st = false; /* turn off flag for structure decl or initialization */ } if (ps.sizeof_keyword) ps.sizeof_mask |= 1 << ps.p_l_follow; break; case rparen: /* got a ')' or ']' */ if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.sizeof_mask) { ps.last_u_d = true; ps.cast_mask &= (1 << ps.p_l_follow) - 1; } ps.sizeof_mask &= (1 << ps.p_l_follow) - 1; if (--ps.p_l_follow < 0) { ps.p_l_follow = 0; diag(0, "Extra %c", *token); } if (e_code == s_code) /* if the paren starts the line */ ps.paren_level = ps.p_l_follow; /* then indent it */ *e_code++ = token[0]; ps.want_blank = true; if (sp_sw && (ps.p_l_follow == 0)) { /* check for end of if (...), or some such */ sp_sw = false; force_nl = true; /* must force newline after if */ ps.last_u_d = true; /* inform lexi that a following operator is unary */ ps.in_stmt = false; /* dont use stmt continuation indentation */ parse(hd_type); /* let parser worry about if, or whatever */ } ps.search_brace = btype_2; /* this should insure that constructs such as main(){...} and int[]{...} have their braces put in the right place */ break; case unary_op: /* this could be any unary operation */ if (ps.want_blank) *e_code++ = ' '; if (troff && !ps.dumped_decl_indent && ps.in_decl && !is_procname) { sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token); ps.dumped_decl_indent = 1; e_code += strlen(e_code); } else { char *res = token; if (ps.in_decl && !ps.block_init) { /* if this is a unary op in a declaration, we should indent this token */ for (i = 0; token[i]; ++i); /* find length of token */ while ((e_code - s_code) < (dec_ind - i)) { if (e_code >= l_code) { register nsize = l_code - s_code + 400; codebuf = (char *) realloc(codebuf, nsize); e_code = codebuf + (e_code - s_code) + 1; l_code = codebuf + nsize - 5; s_code = codebuf + 1; } *e_code++ = ' '; /* pad it */ } } if (troff && token[0] == '-' && token[1] == '>') res = "\\(->"; for (t_ptr = res; *t_ptr; ++t_ptr) { if (e_code >= l_code) { register nsize = l_code - s_code + 400; codebuf = (char *) realloc(codebuf, nsize); e_code = codebuf + (e_code - s_code) + 1; l_code = codebuf + nsize - 5; s_code = codebuf + 1; } *e_code++ = *t_ptr; } } ps.want_blank = false; break; case binary_op: /* any binary operation */ if (ps.want_blank) *e_code++ = ' '; { char *res = token; if (troff) switch (token[0]) { case '<': if (token[1] == '=') res = "\\(<="; break; case '>': if (token[1] == '=') res = "\\(>="; break; case '!': if (token[1] == '=') res = "\\(!="; break; case '|': if (token[1] == '|') res = "\\(br\\(br"; else if (token[1] == 0) res = "\\(br"; break; } for (t_ptr = res; *t_ptr; ++t_ptr) { if (e_code >= l_code) { register nsize = l_code - s_code + 400; codebuf = (char *) realloc(codebuf, nsize); e_code = codebuf + (e_code - s_code) + 1; l_code = codebuf + nsize - 5; s_code = codebuf + 1; } *e_code++ = *t_ptr; /* move the operator */ } } ps.want_blank = true; break; case postop: /* got a trailing ++ or -- */ *e_code++ = token[0]; *e_code++ = token[1]; ps.want_blank = true; break; case question: /* got a ? */ squest++; /* this will be used when a later colon appears so we can distinguish the <c>?<n>:<n> construct */ if (ps.want_blank) *e_code++ = ' '; *e_code++ = '?'; ps.want_blank = true; break; case casestmt: /* got word 'case' or 'default' */ scase = true; /* so we can process the later colon properly */ goto copy_id; case colon: /* got a ':' */ if (squest > 0) { /* it is part of the <c>?<n>: <n> construct */ --squest; if (ps.want_blank) *e_code++ = ' '; *e_code++ = ':'; ps.want_blank = true; break; } if (ps.in_decl) { *e_code++ = ':'; ps.want_blank = false; break; } ps.in_stmt = false; /* seeing a label does not imply we are in a stmt */ for (t_ptr = s_code; *t_ptr; ++t_ptr) *e_lab++ = *t_ptr; /* turn everything so far into a label */ e_code = s_code; *e_lab++ = ':'; *e_lab++ = ' '; *e_lab = '\0'; force_nl = ps.pcase = scase; /* ps.pcase will be used by dump_line to decide how to indent the label. force_nl will force a case n: to be on a line by itself */ scase = false; ps.want_blank = false; break; case semicolon: /* got a ';' */ ps.in_or_st = false; /* we are not in an initialization or structure declaration */ scase = false; /* these will only need resetting in a error */ squest = 0; if (ps.last_token == rparen) ps.in_par_decl = 0; ps.cast_mask = 0; ps.sizeof_mask = 0; ps.block_init = 0; ps.block_init_level = 0; ps.just_saw_decl--; if (ps.in_decl && s_code == e_code && !ps.block_init) while ((e_code - s_code) < (dec_ind - 1)) { if (e_code >= l_code) { register nsize = l_code - s_code + 400; codebuf = (char *) realloc(codebuf, nsize); e_code = codebuf + (e_code - s_code) + 1; l_code = codebuf + nsize - 5; s_code = codebuf + 1; } *e_code++ = ' '; } ps.in_decl = (ps.dec_nest > 0);/* if we were in a first level structure declaration, we arent any more */ if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) { /* This should be true iff there were unbalanced parens in the stmt. It is a bit complicated, because the semicolon might be in a for stmt */ diag(1, "Unbalanced parens"); ps.p_l_follow = 0; if (sp_sw) { /* this is a check for a if, while, etc. with unbalanced parens */ sp_sw = false; parse(hd_type); /* dont lose the if, or whatever */ } } *e_code++ = ';'; ps.want_blank = true; ps.in_stmt = (ps.p_l_follow > 0); /* we are no longer in the middle of a stmt */ if (!sp_sw) { /* if not if for (;;) */ parse(semicolon); /* let parser know about end of stmt */ force_nl = true; /* force newline after a end of stmt */ } break; case lbrace: /* got a '{' */ ps.in_stmt = false; /* dont indent the {} */ if (!ps.block_init) force_nl = true; /* force other stuff on same line as '{' onto new line */ else if (ps.block_init_level <= 0) ps.block_init_level = 1; else ps.block_init_level++; if (s_code != e_code && !ps.block_init) { if (!btype_2) { dump_line(); ps.want_blank = false; } else if (ps.in_par_decl && !ps.in_or_st) { ps.i_l_follow = 0; dump_line(); ps.want_blank = false; } } if (ps.in_par_decl) prefix_blankline_requested = 0; if (ps.p_l_follow > 0) { /* check for preceeding unbalanced parens */ diag(1, "Unbalanced parens"); ps.p_l_follow = 0; if (sp_sw) { /* check for unclosed if, for, etc. */ sp_sw = false; parse(hd_type); ps.ind_level = ps.i_l_follow; } } if (s_code == e_code) ps.ind_stmt = false; /* dont put extra indentation on line with '{' */ if (ps.in_decl && ps.in_or_st) { /* this is either a structure declaration or an init */ di_stack[ps.dec_nest++] = dec_ind; /* ? dec_ind = 0; */ } else { ps.decl_on_line = false; /* we cant be in the middle of a declaration, so dont do special indentation of comments */ if (bl_at_proctop && ps.in_par_decl) postfix_blankline_requested = 1; ps.in_par_decl = 0; } dec_ind = 0; parse(lbrace); /* let parser know about this */ if (ps.want_blank) /* put a blank before '{' if '{' is not at start of line */ *e_code++ = ' '; ps.want_blank = false; *e_code++ = '{'; ps.just_saw_decl = 0; break; case rbrace: /* got a '}' */ if (ps.p_stack[ps.tos] == decl && !ps.block_init) /* semicolons can be omitted in declarations */ parse(semicolon); if (ps.p_l_follow)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -