📄 highlight.c
字号:
for (i=0; patterns[i].style!=0; i++) { if (patterns[i].startRE != NULL) free((char *)patterns[i].startRE); if (patterns[i].endRE != NULL) free((char *)patterns[i].endRE); if (patterns[i].errorRE != NULL) XtFree((char *)patterns[i].errorRE); if (patterns[i].subPatternRE != NULL) free((char *)patterns[i].subPatternRE); } for (i=0; patterns[i].style!=0; i++) if (patterns[i].subPatterns != NULL) XtFree((char *)patterns[i].subPatterns); XtFree((char *)patterns);}/*** Find the highlightPattern structure with a given name in the window.*/highlightPattern *FindPatternOfWindow(WindowInfo *window, char *name){ windowHighlightData *hData = (windowHighlightData *)window->highlightData; patternSet *set; int i; if (hData && (set = hData->patternSetForWindow)) { for (i = 0; i < set->nPatterns; i++) if (strcmp(set->patterns[i].name, name) == 0) return &set->patterns[i]; } return NULL;}/*** Picks up the entry in the style buffer for the position (if any). Rather** like styleOfPos() in textDisp.c. Returns the style code or zero.*/int HighlightCodeOfPos(WindowInfo *window, int pos){ windowHighlightData *highlightData = (windowHighlightData *)window->highlightData; textBuffer *styleBuf = highlightData ? highlightData->styleBuffer : NULL; int hCode = 0; if (styleBuf != NULL) { hCode = (unsigned char)BufGetCharacter(styleBuf, pos); if (hCode == UNFINISHED_STYLE) { /* encountered "unfinished" style, trigger parsing */ handleUnparsedRegion(window, highlightData->styleBuffer, pos); hCode = (unsigned char)BufGetCharacter(styleBuf, pos); } } return hCode;}/*** Returns the length over which a particular highlight code applies, starting** at pos. If the initial code value *checkCode is zero, the highlight code of** pos is used.*//* YOO: This is called form only one other function, which uses a constant for checkCode and never evaluates it after the call. */int HighlightLengthOfCodeFromPos(WindowInfo *window, int pos, int *checkCode){ windowHighlightData *highlightData = (windowHighlightData *)window->highlightData; textBuffer *styleBuf = highlightData ? highlightData->styleBuffer : NULL; int hCode = 0; int oldPos = pos; if (styleBuf != NULL) { hCode = (unsigned char)BufGetCharacter(styleBuf, pos); if (!hCode) return 0; if (hCode == UNFINISHED_STYLE) { /* encountered "unfinished" style, trigger parsing */ handleUnparsedRegion(window, highlightData->styleBuffer, pos); hCode = (unsigned char)BufGetCharacter(styleBuf, pos); } if (*checkCode == 0) *checkCode = hCode; while (hCode == *checkCode || hCode == UNFINISHED_STYLE) { if (hCode == UNFINISHED_STYLE) { /* encountered "unfinished" style, trigger parsing, then loop */ handleUnparsedRegion(window, highlightData->styleBuffer, pos); hCode = (unsigned char)BufGetCharacter(styleBuf, pos); } else { /* advance the position and get the new code */ hCode = (unsigned char)BufGetCharacter(styleBuf, ++pos); } } } return pos - oldPos;}/*** Returns the length over which a particular style applies, starting at pos. ** If the initial code value *checkCode is zero, the highlight code of pos ** is used.*/int StyleLengthOfCodeFromPos(WindowInfo *window, int pos, const char **checkStyleName){ windowHighlightData *highlightData = (windowHighlightData *)window->highlightData; textBuffer *styleBuf = highlightData ? highlightData->styleBuffer : NULL; int hCode = 0; int oldPos = pos; styleTableEntry *entry; if (styleBuf != NULL) { hCode = (unsigned char)BufGetCharacter(styleBuf, pos); if (!hCode) return 0; if (hCode == UNFINISHED_STYLE) { /* encountered "unfinished" style, trigger parsing */ handleUnparsedRegion(window, highlightData->styleBuffer, pos); hCode = (unsigned char)BufGetCharacter(styleBuf, pos); } entry = styleTableEntryOfCode(window, hCode); if (entry == NULL) return 0; if ((*checkStyleName) == NULL) (*checkStyleName) = entry->styleName; while (hCode == UNFINISHED_STYLE || ((entry = styleTableEntryOfCode(window, hCode)) && strcmp(entry->styleName, (*checkStyleName)) == 0)) { if (hCode == UNFINISHED_STYLE) { /* encountered "unfinished" style, trigger parsing, then loop */ handleUnparsedRegion(window, highlightData->styleBuffer, pos); hCode = (unsigned char)BufGetCharacter(styleBuf, pos); } else { /* advance the position and get the new code */ hCode = (unsigned char)BufGetCharacter(styleBuf, ++pos); } } } return pos - oldPos;}/*** Returns a pointer to the entry in the style table for the entry of code** hCode (if any).*/static styleTableEntry *styleTableEntryOfCode(WindowInfo *window, int hCode){ windowHighlightData *highlightData = (windowHighlightData *)window->highlightData; hCode -= UNFINISHED_STYLE; /* get the correct index value */ if (!highlightData || hCode < 0 || hCode >= highlightData->nStyles) return NULL; return &highlightData->styleTable[hCode];}/*** Functions to return style information from the highlighting style table.*/char *HighlightNameOfCode(WindowInfo *window, int hCode){ styleTableEntry *entry = styleTableEntryOfCode(window, hCode); return entry ? entry->highlightName : "";}char *HighlightStyleOfCode(WindowInfo *window, int hCode){ styleTableEntry *entry = styleTableEntryOfCode(window, hCode); return entry ? entry->styleName : "";}char *HighlightColorOfCode(WindowInfo *window, int hCode){ styleTableEntry *entry = styleTableEntryOfCode(window, hCode); return entry ? entry->colorName : "";}char *HighlightBackgroundColorOfCode(WindowInfo *window, int hCode){ styleTableEntry *entry = styleTableEntryOfCode(window, hCode); return entry && entry->bgColorName ? entry->bgColorName : "";}Pixel HighlightColorValueOfCode(WindowInfo *window, int hCode, int *r, int *g, int *b){ styleTableEntry *entry = styleTableEntryOfCode(window, hCode); if (entry) { *r = entry->red; *g = entry->green; *b = entry->blue; return entry->color; } else { /* pick up foreground color of the (first) text widget of the window */ XColor colorDef; Colormap cMap; Display *display = XtDisplay(window->textArea); *r = *g = *b = 0; XtVaGetValues(window->textArea, XtNcolormap, &cMap, XtNforeground, &colorDef.pixel, NULL); if (XQueryColor(display, cMap, &colorDef)) { *r = colorDef.red; *g = colorDef.green; *b = colorDef.blue; } return colorDef.pixel; }}Pixel GetHighlightBGColorOfCode(WindowInfo *window, int hCode, int *r, int *g, int *b){ styleTableEntry *entry = styleTableEntryOfCode(window, hCode); if (entry && entry->bgColorName) { *r = entry->bgRed; *g = entry->bgGreen; *b = entry->bgBlue; return entry->bgColor; } else { /* pick up background color of the (first) text widget of the window */ XColor colorDef; Colormap cMap; Display *display = XtDisplay(window->textArea); *r = *g = *b = 0; XtVaGetValues(window->textArea, XtNcolormap, &cMap, XtNbackground, &colorDef.pixel, NULL); if (XQueryColor(display, cMap, &colorDef)) { *r = colorDef.red; *g = colorDef.green; *b = colorDef.blue; } return colorDef.pixel; }}int HighlightCodeIsBold(WindowInfo *window, int hCode){ styleTableEntry *entry = styleTableEntryOfCode(window, hCode); return entry ? entry->isBold : 0;}int HighlightCodeIsItalic(WindowInfo *window, int hCode){ styleTableEntry *entry = styleTableEntryOfCode(window, hCode); return entry ? entry->isItalic : 0;}/*** Callback to parse an "unfinished" region of the buffer. "unfinished" means** that the buffer has been parsed with pass 1 patterns, but this section has** not yet been exposed, and thus never had pass 2 patterns applied. This** callback is invoked when the text widget's display routines encounter one** of these unfinished regions. "pos" is the first position encountered which** needs re-parsing. This routine applies pass 2 patterns to a chunk of** the buffer of size PASS_2_REPARSE_CHUNK_SIZE beyond pos.*/static void handleUnparsedRegion(WindowInfo *window, textBuffer *styleBuf, int pos){ textBuffer *buf = window->buffer; int beginParse, endParse, beginSafety, endSafety, p; windowHighlightData *highlightData = (windowHighlightData *)window->highlightData; reparseContext *context = &highlightData->contextRequirements; highlightDataRec *pass2Patterns = highlightData->pass2Patterns; char *string, *styleString, *stringPtr, *stylePtr, c, prevChar; int firstPass2Style = (unsigned char)pass2Patterns[1].style; /* If there are no pass 2 patterns to process, do nothing (but this should never be triggered) */ if (pass2Patterns == NULL) return; /* Find the point at which to begin parsing to ensure that the character at pos is parsed correctly (beginSafety), at most one context distance back from pos, unless there is a pass 1 section from which to start */ beginParse = pos; beginSafety = backwardOneContext(buf, context, beginParse); for (p=beginParse; p>=beginSafety; p--) { c = BufGetCharacter(styleBuf, p); if (c != UNFINISHED_STYLE && c != PLAIN_STYLE && (unsigned char)c < firstPass2Style) { beginSafety = p + 1; break; } } /* Decide where to stop (endParse), and the extra distance (endSafety) necessary to ensure that the changes at endParse are correct. Stop at the end of the unfinished region, or a max. of PASS_2_REPARSE_CHUNK_SIZE characters forward from the requested position */ endParse = min(buf->length, pos + PASS_2_REPARSE_CHUNK_SIZE); endSafety = forwardOneContext(buf, context, endParse); for (p=pos; p<endSafety; p++) { c = BufGetCharacter(styleBuf, p); if (c != UNFINISHED_STYLE && c != PLAIN_STYLE && (unsigned char)c < firstPass2Style) { endParse = min(endParse, p); endSafety = p; break; } else if (c != UNFINISHED_STYLE && p < endParse) { endParse = p; if ((unsigned char)c < firstPass2Style) endSafety = p; else endSafety = forwardOneContext(buf, context, endParse); break; } } /* Copy the buffer range into a string */ /* printf("callback pass2 parsing from %d thru %d w/ safety from %d thru %d\n", beginParse, endParse, beginSafety, endSafety); */ string = stringPtr = BufGetRange(buf, beginSafety, endSafety); styleString = stylePtr = BufGetRange(styleBuf, beginSafety, endSafety); /* Parse it with pass 2 patterns */ prevChar = getPrevChar(buf, beginSafety); parseString(pass2Patterns, &stringPtr, &stylePtr, endParse - beginSafety, &prevChar, False, GetWindowDelimiters(window), string); /* Update the style buffer the new style information, but only between beginParse and endParse. Skip the safety region */ styleString[endParse-beginSafety] = '\0'; BufReplace(styleBuf, beginParse, endParse, &styleString[beginParse-beginSafety]); XtFree(styleString); XtFree(string); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -