Add bracket matching to text editor.

#2437 from the Patch Tracker.
Contributed by Ricki Myers (themyers).  Thanks!

Highlight color is set in UserPref theme colors.
This commit is contained in:
Stephen Swaney
2005-04-28 15:41:06 +00:00
parent ac1cccdf59
commit c0f129d0be
5 changed files with 141 additions and 7 deletions

View File

@@ -47,5 +47,7 @@ void add_text_fs(char *file);
void free_txt_data(void);
void pop_space_text(struct SpaceText *st);
void do_brackets(void);
#endif

View File

@@ -430,6 +430,7 @@ enum {
TH_NORMAL,
TH_FACE_DOT,
TH_FACEDOT_SIZE,
TH_BRACKET
};
/* XXX WARNING: previous is saved in file, so do not change order! */

View File

@@ -88,7 +88,8 @@ typedef struct ThemeSpace {
char normal[4];
char vertex_size, facedot_size;
char pad1[2];
char bracket[4]; /* bracket matching */
char pad1[2], bpad2[4];
} ThemeSpace;

View File

@@ -85,15 +85,14 @@
#define TEXTXLOC 38
/* locals */
/* forward declarations */
void drawtextspace(ScrArea *sa, void *spacedata);
void winqreadtextspace(struct ScrArea *sa, void *spacedata, struct BWinEvent *evt);
/* missing locals */
void txt_copy_selectbuffer (Text *text);
void txt_paste_clipboard(Text *text); /* blank on non Win32 */
void txt_copy_clipboard(Text *text); /* blank on non Win32 */
int check_bracket(char *string);
static void *last_txt_find_string= NULL;
@@ -330,7 +329,8 @@ static void draw_cursor(SpaceText *st) {
if(i) glRecti(x, curarea->winy-st->lheight*(h)-2, i, curarea->winy-st->lheight*(h+1)-2);
}
do_brackets();
BIF_ThemeColor(TH_TEXT);
}
@@ -1454,3 +1454,126 @@ void winqreadtextspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
}
}
}
void do_brackets(void)
{
SpaceText *st = curarea->spacedata.first;
Text *text = st->text;
TextLine *tmp, *start;
char test[2];
int d, pos, open, x, y, x2, y2, h=0;
if(!text) return;
tmp = text->curl;
start = text->curl;
test[0] = (unsigned char) tmp->line[text->curc];
test[1] = '\0';
d = check_bracket(test);
if (!d) /* If not pri char */
{
test[0] = (unsigned char) tmp->line[text->curc-1];
test[1] = '\0';
d = check_bracket(test);
if(!d) {
return; /*If the current char or prev is not a bracket then return*/
} else { /* current char */
h= txt_get_span(text->lines.first, start) - st->top;
x = text_draw(st, start->line, st->left, text->curc-1, 0, 0, 0);
y = text_draw(st, start->line, st->left, text->curc, 0, 0, 0);
if (d < 4) {
pos = text->curc;
} else {
pos = text->curc-2;
}
}
} else { /* is pri char */
h= txt_get_span(text->lines.first, start) - st->top;
x = text_draw(st, start->line, st->left, text->curc, 0, 0, 0);
y = text_draw(st, start->line, st->left, text->curc+1, 0, 0, 0);
if (d < 4) {
pos = text->curc+1;
} else {
pos = text->curc-1;
}
}
if (d < 4) /*reading forward*/
{
open = 1;
while ( tmp ) {
while (pos <= tmp->len) {
test[0] = (unsigned char) tmp->line[pos];
test[1] = '\0';
if(check_bracket(test) == d) {
open++;
} else if (check_bracket(test) == d+3) {
open--;
if (open == 0) {
BIF_ThemeColor(TH_BRACKET);
glRecti(x, curarea->winy-st->lheight*(h)-2, y, curarea->winy-st->lheight*(h+1)-2);
h= txt_get_span(text->lines.first, tmp) - st->top;
x2= text_draw(st, tmp->line, st->left, pos, 0, 0, 0);
y2= text_draw(st, tmp->line, st->left, pos+1, 0, 0, 0);
glRecti(x2, curarea->winy-st->lheight*(h)-2, y2, curarea->winy-st->lheight*(h+1)-2);
BIF_ThemeColor(TH_TEXT);
return;
}
}
pos++;
}
tmp = tmp->next;
pos = 0;
}
} else { /* reading back */
open = 1;
while ( tmp ) {
while (pos >= 0) {
test[0] = (unsigned char) tmp->line[pos];
test[1] = '\0';
if(check_bracket(test) == d) {
open++;
} else if (check_bracket(test) == d-3) {
open--;
if (open == 0) {
BIF_ThemeColor(TH_BRACKET);
glRecti(x, curarea->winy-st->lheight*(h)-2, y, curarea->winy-st->lheight*(h+1)-2);
h= txt_get_span(text->lines.first, tmp) - st->top;
x2= text_draw(st, tmp->line, st->left, pos, 0, 0, 0);
y2= text_draw(st, tmp->line, st->left, pos+1, 0, 0, 0);
glRecti(x2, curarea->winy-st->lheight*(h)-2, y2, curarea->winy-st->lheight*(h+1)-2);
BIF_ThemeColor(TH_TEXT);
return;
}
}
pos--;
}
tmp = tmp->prev;
if (tmp) {
pos = tmp->len;
}
}
}
}
int check_bracket(char *string)
{
int number, a = 0;
char other[][3] = {"(", "[", "{", ")", "]", "}"};
number = 6;
while(a < number) {
if(strcmp(other[a], string) == 0)
{
return a+1;
}
a++;
}
return 0;
}

View File

@@ -488,6 +488,8 @@ char *BIF_ThemeGetColorPtr(bTheme *btheme, int spacetype, int colorid)
cp= &ts->facedot_size; break;
case TH_NORMAL:
cp= ts->normal; break;
case TH_BRACKET:
cp= ts->bracket; break;
}
@@ -648,7 +650,10 @@ void BIF_InitTheme(void)
SETCOL(btheme->text.shade1, 143, 143, 143, 255);
SETCOL(btheme->text.shade2, 0xc6, 0x77, 0x77, 255);
SETCOL(btheme->text.hilite, 255, 0, 0, 255);
/* Bracket matching */
SETCOL(btheme->text.bracket, 0xff, 0xff, 0x0, 100);
/* space oops */
btheme->toops= btheme->tv3d;
SETCOL(btheme->toops.back, 153, 153, 153, 255);
@@ -770,6 +775,8 @@ char *BIF_ThemeColorsPup(int spacetype)
sprintf(str, "Scroll Bar %%x%d|", TH_SHADE1); strcat(cp, str);
sprintf(str, "Selected Text %%x%d|", TH_SHADE2); strcat(cp, str);
sprintf(str, "Cursor %%x%d|", TH_HILITE); strcat(cp, str);
strcat(cp,"%l|");
sprintf(str, "Bracket Matching %%x%d|", TH_BRACKET); strcat(cp, str);
}
}
return cp;