📄 c.c
字号:
do_expr(BACKWARD, NO); ToIndent(); new_indent = calc_pos(linebuf, curchar); /* do_expr() calls b_paren, which returns a pointer to a structure, and that pointer is in BP so we have to save away the matching paren and restore it in the following line ... sigh */ *bp = savematch; } } if (brace == NO) new_indent += (increment - (new_indent % increment)); break; case '(': if (CArgIndent == ALIGN_ARGS) { f_char(1); new_indent = calc_pos(linebuf, curchar); } else new_indent = matching_indent + CArgIndent; break; } SetDot(&save); } /* new_indent is the "correct" place to indent. Now we check to see if what we consider as the correct place to indent is to the LEFT of where we already are. If it is, and we are NOT handling a brace, then we assume that the person wants to tab in further than what we think is right (for some reason) and so we allow that. */ ToIndent(); current_indent = calc_pos(linebuf, curchar); if (brace == NO && new_indent <= current_indent) new_indent = current_indent + (increment - (current_indent % increment)); Bol(); DelWtSpace(); /* nice uniform Tabs*Space* */ n_indent(new_indent); return bp;}private voidre_indent(incr)int incr;{ Line *l1, *l2, *lp; int c1, c2; Mark *m = CurMark(); Bufpos savedot; DOTsave(&savedot); l1 = curline; c1 = curchar; l2 = m->m_line; c2 = m->m_char; (void) fixorder(&l1, &c1, &l2, &c2); for (lp = l1; lp != l2->l_next; lp = lp->l_next) { int indent; SetLine(lp); ToIndent(); indent = calc_pos(linebuf, curchar); if (indent != 0 || linebuf[0] != '\0') n_indent(indent + incr); } SetDot(&savedot);}voidLRShift(){ int amnt; if (is_an_arg()) amnt = arg_value(); else amnt = CIndIncrmt; re_indent(-amnt);}voidRRShift(){ int amnt; if (is_an_arg()) amnt = arg_value(); else amnt = CIndIncrmt; re_indent(amnt);}#ifdef CMT_FMTchar CmtFmt[80] = "/*%n%! * %c%!%n */";voidComment(){ FillComment(CmtFmt);}/* Strip leading and trailing white space. Skip over any imbedded '\r's. */private voidstrip_c(from, to)char *from, *to;{ register char *fr_p = from, *to_p = to, c; while ((c = *fr_p) != '\0') { if (c == ' ' || c == '\t' || c == '\r') fr_p += 1; else break; } while ((c = *fr_p) != '\0') { if (c != '\r') *to_p++ = c; fr_p += 1; } while (--to_p >= to) if (*to_p != ' ' && *to_p != '\t') break; *++to_p = '\0';}private char open_c[20], /* the open comment format string */ open_pat[20], /* the search pattern for open comment */ l_header[20], /* the prefix for each comment line */ l_trailer[20], /* the suffix ... */ close_c[20], close_pat[20];private char *const comment_body[] = { open_c, l_header, l_trailer, close_c};private int nlflags;/* Fill in the data structures above from the format string. Don't return if there's trouble. */private voidparse_cmt_fmt(str)char *str;{ register char *fmtp = str; register char *const *c_body = comment_body, *body_p = *c_body; int c, newlines = 1; /* pick apart the comment string */ while ((c = *fmtp++) != '\0') { if (c != '%') { *body_p++ = c; continue; } switch(c = *fmtp++) { case 'n': if (newlines == 2 || newlines == 3) complain("%n not allowed in line header or trailer: %s", fmtp - 2); nlflags += newlines; *body_p++ = '\r'; break; case 't': *body_p++ = '\t'; break; case '%': *body_p++ = '%'; break; case '!': case 'c': newlines += 1; *body_p++ = '\0'; body_p = *++c_body; break; default: complain("[Unknown comment escape: %%%c]", c); break; } } *body_p = '\0'; /* make search patterns */ strip_c(open_c, open_pat); strip_c(close_c, close_pat);}#define NL_IN_OPEN_C ((nlflags % 4) == 1)#define NL_IN_CLOSE_C (nlflags >= 4)private voidFillComment(format)char *format;{ int saveRMargin, indent_pos; bool close_at_dot = NO; size_t header_len, trailer_len; register char *cp; static const char inside_err[] = "[Must be between %s and %s to re-format]"; Bufpos open_c_pt, close_c_pt, tmp_bp, *match_o, *match_c; Mark *entry_mark, *open_c_mark, *savedot; parse_cmt_fmt(format); /* figure out if we're "inside" a comment */ if ((match_o = dosearch(open_pat, BACKWARD, NO)) == NULL) complain("No opening %s to match to.", open_pat); open_c_pt = *match_o; if ((match_c = dosearch(close_pat, BACKWARD, NO)) != NULL && inorder(open_c_pt.p_line, open_c_pt.p_char, match_c->p_line, match_c->p_char)) complain(inside_err, open_pat, close_pat); if ((match_o = dosearch(open_pat, FORWARD, NO)) != NULL) { tmp_bp = *match_o; match_o = &tmp_bp; } if ((match_c = dosearch(close_pat, FORWARD, NO)) != NULL) close_c_pt = *match_c; /* Here's where we figure out whether to format from dot or from the close comment. Note that we've already searched backwards to find the open comment symbol for the comment we are formatting. The open symbol mentioned below refers to the possible existence of the next comment. There are 5 cases: 1) no open or close symbol ==> dot 2) open, but no close symbol ==> dot 3) close, but no open ==> close 4) open, close are inorder ==> dot 5) open, close are not inorder ==> close */ if (match_o == (Bufpos *)NULL) { if (match_c == (Bufpos *)NULL) close_at_dot = YES; } else if (match_c == (Bufpos *)NULL) close_at_dot = YES; else if (inorder(match_o->p_line, match_o->p_char, match_c->p_line, match_c->p_char)) close_at_dot = YES; if (close_at_dot) { close_c_pt.p_line = curline; close_c_pt.p_char = curchar; } else { SetDot(match_c); } SetDot(&open_c_pt); open_c_mark = MakeMark(curline, curchar, M_FLOATER); indent_pos = calc_pos(linebuf, curchar); /* search for a close comment; delete it if it exits */ SetDot(&close_c_pt); if (!close_at_dot) del_char(BACKWARD, (int)strlen(close_pat), NO); entry_mark = MakeMark(curline, curchar, M_FLOATER); ToMark(open_c_mark); /* always separate the comment body from anything preceeding it */ LineInsert(1); DelWtSpace(); Bol(); for (cp = open_c; *cp; cp++) { if (*cp == '\r') { if (!eolp()) LineInsert(1); else line_move(FORWARD, 1, NO); } else if (*cp == ' ' || *cp == '\t') { if (linebuf[curchar] != *cp) insert_c(*cp, 1); } else { /* Since we matched the open comment string on this line, we don't need to worry about crossing line boundaries. */ curchar += 1; } } savedot = MakeMark(curline, curchar, M_FLOATER); /* We need to strip the line header pattern of leading white space since we need to match the line after all of its leading whitespace is gone. */ for (cp = l_header; *cp && (jisspace(*cp)); cp++) ; header_len = strlen(cp); trailer_len = strlen(l_trailer); /* Strip each comment line of the open and close comment strings before reformatting it. */ do { Bol(); DelWtSpace(); if (header_len && strncmp(linebuf, cp, header_len)==0) del_char(FORWARD, (int)header_len, NO); if (trailer_len) { Eol(); if (((size_t)curchar > trailer_len) && (strncmp(&linebuf[curchar - trailer_len], l_trailer, trailer_len)==0)) del_char(BACKWARD, (int)trailer_len, NO); } if (curline->l_next != NULL) line_move(FORWARD, 1, NO); else break; } while (curline != entry_mark->m_line->l_next); do_set_mark(savedot->m_line, savedot->m_char); ToMark(entry_mark); saveRMargin = RMargin; RMargin = saveRMargin - strlen(l_header) - strlen(l_trailer) - indent_pos + 2; do_rfill(NO); RMargin = saveRMargin; /* get back to the start of the comment */ PopMark(); do { if (curline != open_c_mark->m_line->l_next) { Bol(); n_indent(indent_pos); ins_str(l_header, NO); } Eol(); if (NL_IN_CLOSE_C || (curline != entry_mark->m_line)) ins_str(l_trailer, NO); if (curline->l_next != NULL) line_move(FORWARD, 1, NO); else break; } while (curline != entry_mark->m_line->l_next); /* handle the close comment symbol */ if (curline == entry_mark->m_line->l_next) { line_move(BACKWARD, 1, NO); Eol(); } DelWtSpace(); /* if the addition of the close symbol would cause the line to be too long, put the close symbol on the next line. */ if (!(NL_IN_CLOSE_C) && (int)strlen(close_c) + calc_pos(linebuf, curchar) > RMargin) { LineInsert(1); n_indent(indent_pos); } for (cp = close_c; *cp; cp++) { if (*cp == '\r') { LineInsert(1); n_indent(indent_pos); } else insert_c(*cp, 1); } ToMark(open_c_mark); Eol(); del_char(FORWARD, 1, NO);}#endif /* CMT_FMT */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -