📄 shift.c
字号:
for (i=0; i<nChars; i++) { *lineOutPtr++ = ' '; whiteWidth++; /* if we're now at a tab stop, change last 8 spaces to a tab */ if (tabsAllowed && atTabStop(whiteWidth, tabDist)) { lineOutPtr -= tabDist; *lineOutPtr++ = '\t'; } } /* move remainder of line */ while (*lineInPtr!='\0' && (lineInPtr - line) < lineLen) *lineOutPtr++ = *lineInPtr++; *lineOutPtr = '\0'; return lineOut; } }}static char *shiftLineLeft(char *line, int lineLen, int tabDist, int nChars){ char *lineOut; int i, whiteWidth, lastWhiteWidth, whiteGoal; char *lineInPtr, *lineOutPtr; lineInPtr = line; lineOut = XtMalloc(lineLen + tabDist + 1); lineOutPtr = lineOut; whiteWidth = 0; lastWhiteWidth = 0; while (TRUE) { if (*lineInPtr == '\0' || (lineInPtr - line) >= lineLen) { /* nothing on line, wipe it out */ *lineOut = '\0'; return lineOut; } else if (*lineInPtr == ' ') { /* white space continues with space, advance one character */ whiteWidth++; *lineOutPtr++ = *lineInPtr++; } else if (*lineInPtr == '\t') { /* white space continues with tab, advance to next tab stop */ /* save the position, though, in case we need to remove the tab */ lastWhiteWidth = whiteWidth; whiteWidth = nextTab(whiteWidth, tabDist); *lineOutPtr++ = *lineInPtr++; } else { /* end of white space, remove nChars characters */ for (i=1; i<=nChars; i++) { if (lineOutPtr > lineOut) { if (*(lineOutPtr-1) == ' ') { /* end of white space is a space, just remove it */ lineOutPtr--; } else { /* end of white space is a tab, remove it and add back spaces */ lineOutPtr--; whiteGoal = whiteWidth - i; whiteWidth = lastWhiteWidth; while (whiteWidth < whiteGoal) { *lineOutPtr++ = ' '; whiteWidth++; } } } } /* move remainder of line */ while (*lineInPtr!='\0' && (lineInPtr - line) < lineLen) *lineOutPtr++ = *lineInPtr++; /* add a null */ *lineOutPtr = '\0'; return lineOut; } }} static int atTabStop(int pos, int tabDist){ return (pos%tabDist == 0);}static int nextTab(int pos, int tabDist){ return (pos/tabDist)*tabDist + tabDist;}static int countLines(const char *text){ int count = 1; while(*text != '\0') { if (*text++ == '\n') { count++; } } return count;}/*** Find the implied left margin of a text string (the number of columns to the** first non-whitespace character on any line) up to either the terminating** null character at the end of the string, or "length" characters, whever** comes first.*/static int findLeftMargin(char *text, int length, int tabDist){ char *c; int col = 0, leftMargin = INT_MAX; int inMargin = True; for (c=text; *c!='\0' && c-text<length; c++) { if (*c == '\t') { col += BufCharWidth('\t', col, tabDist, '\0'); } else if (*c == ' ') { col++; } else if (*c == '\n') { col = 0; inMargin = True; } else { /* non-whitespace */ if (col < leftMargin && inMargin) leftMargin = col; inMargin = False; } } /* if no non-white text is found, the leftMargin will never be set */ if (leftMargin == INT_MAX) return 0; return leftMargin;}/*** Fill multiple paragraphs between rightMargin and an implied left margin** and first line indent determined by analyzing the text. alignWithFirst** aligns subsequent paragraphs with the margins of the first paragraph (a** capability not currently used in NEdit, but carried over from code for** previous versions which did all paragraphs together).*/static char *fillParagraphs(char *text, int rightMargin, int tabDist, int useTabs, char nullSubsChar, int *filledLen, int alignWithFirst){ int paraStart, paraEnd, fillEnd; char *c, ch, *secondLineStart, *paraText, *filledText; int firstLineLen, firstLineIndent, leftMargin, len; textBuffer *buf; /* Create a buffer to accumulate the filled paragraphs */ buf = BufCreate(); BufSetAll(buf, text); /* ** Loop over paragraphs, filling each one, and accumulating the results ** in buf */ paraStart = 0; for (;;) { /* Skip over white space */ while (paraStart < buf->length) { ch = BufGetCharacter(buf, paraStart); if (ch != ' ' && ch != '\t' && ch != '\n') break; paraStart++; } if (paraStart >= buf->length) break; paraStart = BufStartOfLine(buf, paraStart); /* Find the end of the paragraph */ paraEnd = findParagraphEnd(buf, paraStart); /* Operate on either the one paragraph, or to make them all identical, do all of them together (fill paragraph can format all the paragraphs it finds with identical specs if it gets passed more than one) */ fillEnd = alignWithFirst ? buf->length : paraEnd; /* Get the paragraph in a text string (or all of the paragraphs if we're making them all the same) */ paraText = BufGetRange(buf, paraStart, fillEnd); /* Find separate left margins for the first and for the first line of the paragraph, and for rest of the remainder of the paragraph */ for (c=paraText ; *c!='\0' && *c!='\n'; c++); firstLineLen = c - paraText; secondLineStart = *c == '\0' ? paraText : c + 1; firstLineIndent = findLeftMargin(paraText, firstLineLen, tabDist); leftMargin = findLeftMargin(secondLineStart, paraEnd - paraStart - (secondLineStart - paraText), tabDist); /* Fill the paragraph */ filledText = fillParagraph(paraText, leftMargin, firstLineIndent, rightMargin, tabDist, useTabs, nullSubsChar, &len); XtFree(paraText); /* Replace it in the buffer */ BufReplace(buf, paraStart, fillEnd, filledText); XtFree(filledText); /* move on to the next paragraph */ paraStart += len; } /* Free the buffer and return its contents */ filledText = BufGetAll(buf); *filledLen = buf->length; BufFree(buf); return filledText;}/*** Trim leading space, and arrange text to fill between leftMargin and** rightMargin (except for the first line which fills from firstLineIndent),** re-creating whitespace to the left of the text using tabs (if allowTabs is** True) calculated using tabDist, and spaces. Returns a newly allocated** string as the function result, and the length of the new string in filledLen.*/static char *fillParagraph(char *text, int leftMargin, int firstLineIndent, int rightMargin, int tabDist, int allowTabs, char nullSubsChar, int *filledLen){ char *cleanedText, *outText, *indentString, *leadIndentStr, *outPtr, *c, *b; int col, cleanedLen, indentLen, leadIndentLen, nLines = 1; int inWhitespace, inMargin; /* remove leading spaces, convert newlines to spaces */ cleanedText = XtMalloc(strlen(text)+1); outPtr = cleanedText; inMargin = True; for (c=text; *c!='\0'; c++) { if (*c == '\t' || *c == ' ') { if (!inMargin) *outPtr++ = *c; } else if (*c == '\n') { if (inMargin) { /* a newline before any text separates paragraphs, so leave it in, back up, and convert the previous space back to \n */ if (outPtr > cleanedText && *(outPtr-1) == ' ') *(outPtr-1) = '\n'; *outPtr++ = '\n'; nLines +=2; } else *outPtr++ = ' '; inMargin = True; } else { *outPtr++ = *c; inMargin = False; } } cleanedLen = outPtr - cleanedText; *outPtr = '\0'; /* Put back newlines breaking text at word boundaries within the margins. Algorithm: scan through characters, counting columns, and when the margin width is exceeded, search backward for beginning of the word and convert the last whitespace character into a newline */ col = firstLineIndent; for (c=cleanedText; *c!='\0'; c++) { if (*c == '\n') col = leftMargin; else col += BufCharWidth(*c, col, tabDist, nullSubsChar); if (col-1 > rightMargin) { inWhitespace = True; for (b=c; b>=cleanedText && *b!='\n'; b--) { if (*b == '\t' || *b == ' ') { if (!inWhitespace) { *b = '\n'; c = b; col = leftMargin; nLines++; break; } } else inWhitespace = False; } } } nLines++; /* produce a string to prepend to lines to indent them to the left margin */ leadIndentStr = makeIndentString(firstLineIndent, tabDist, allowTabs, &leadIndentLen); indentString = makeIndentString(leftMargin, tabDist, allowTabs, &indentLen); /* allocate memory for the finished string */ outText = XtMalloc(sizeof(char) * (cleanedLen + leadIndentLen + indentLen * (nLines-1) + 1)); outPtr = outText; /* prepend the indent string to each line of the filled text */ strncpy(outPtr, leadIndentStr, leadIndentLen); outPtr += leadIndentLen; for (c=cleanedText; *c!='\0'; c++) { *outPtr++ = *c; if (*c == '\n') { strncpy(outPtr, indentString, indentLen); outPtr += indentLen; } } /* convert any trailing space to newline. Add terminating null */ if (*(outPtr-1) == ' ') *(outPtr-1) = '\n'; *outPtr = '\0'; /* clean up, return result */ XtFree(cleanedText); XtFree(leadIndentStr); XtFree(indentString); *filledLen = outPtr - outText; return outText;}static char *makeIndentString(int indent, int tabDist, int allowTabs, int *nChars){ char *indentString, *outPtr; int i; outPtr = indentString = XtMalloc(sizeof(char) * indent + 1); if (allowTabs) { for (i=0; i<indent/tabDist; i++) *outPtr++ = '\t'; for (i=0; i<indent%tabDist; i++) *outPtr++ = ' '; } else { for (i=0; i<indent; i++) *outPtr++ = ' '; } *outPtr = '\0'; *nChars = outPtr - indentString; return indentString;}/*** Find the boundaries of the paragraph containing pos*/static int findParagraphEnd(textBuffer *buf, int startPos){ char c; int pos; static char whiteChars[] = " \t"; pos = BufEndOfLine(buf, startPos)+1; while (pos < buf->length) { c = BufGetCharacter(buf, pos); if (c == '\n') break; if (strchr(whiteChars, c) != NULL) pos++; else pos = BufEndOfLine(buf, pos)+1; } return pos < buf->length ? pos : buf->length;}static int findParagraphStart(textBuffer *buf, int startPos){ char c; int pos, parStart; static char whiteChars[] = " \t"; if (startPos == 0) return 0; parStart = BufStartOfLine(buf, startPos); pos = parStart - 2; while (pos > 0) { c = BufGetCharacter(buf, pos); if (c == '\n') break; if (strchr(whiteChars, c) != NULL) pos--; else { parStart = BufStartOfLine(buf, pos); pos = parStart - 2; } } return parStart > 0 ? parStart : 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -