📄 syntax.c
字号:
case ' ': *p = ' '; s--; break; case 'n': *p = '\n'; break; case 'r': *p = '\r'; break; case 't': *p = '\t'; break; case 's': *p = ' '; break; case '*': *p = '*'; break; case '\\': *p = '\\'; break; case '[': case ']': *p = '\003'; break; case '{': case '}': *p = '\004'; break; default: *p = *s; break; } break; case '*': *p = '\001'; break; case '+': *p = '\002'; break; default: *p = *s; break; } s++; p++; } *p = '\0'; return r;}#define whiteness(x) ((x) == '\t' || (x) == '\n' || (x) == ' ')static void get_args (char *l, char **args, int *argc){ *argc = 0; l--; for (;;) { char *p; for (p = l + 1; *p && whiteness (*p); p++); if (!*p) break; for (l = p + 1; *l && !whiteness (*l); l++); *l = '\0'; *args = strdup_convert (p); (*argc)++; args++; } *args = 0;}static void free_args (char **args){ while (*args) { syntax_free (*args); *args = 0; args++; }}#define check_a {if(!*a){result=line;break;}}#define check_not_a {if(*a){result=line;break;}}#ifdef MIDNIGHTint try_alloc_color_pair (char *fg, char *bg);int this_try_alloc_color_pair (char *fg, char *bg){ char f[80], b[80], *p; if (bg) if (!*bg) bg = 0; if (fg) if (!*fg) fg = 0; if (fg) { strcpy (f, fg); p = strchr (f, '/'); if (p) *p = '\0'; fg = f; } if (bg) { strcpy (b, bg); p = strchr (b, '/'); if (p) *p = '\0'; bg = b; } return try_alloc_color_pair (fg, bg);}#else#ifdef GTKint allocate_color (WEdit *edit, gchar *color);int this_allocate_color (WEdit *edit, char *fg){ char *p; if (fg) if (!*fg) fg = 0; if (!fg) return allocate_color (edit, 0); p = strchr (fg, '/'); if (!p) return allocate_color (edit, fg); return allocate_color (edit, p + 1);}#elseint this_allocate_color (WEdit *edit, char *fg){ char *p; if (fg) if (!*fg) fg = 0; if (!fg) return allocate_color (0); p = strchr (fg, '/'); if (!p) return allocate_color (fg); return allocate_color (p + 1);}#endif#endifstatic char *error_file_name = 0;static FILE *open_include_file (char *filename){ FILE *f; char p[MAX_PATH_LEN]; syntax_free (error_file_name); error_file_name = (char *) strdup (filename); if (*filename == '/') return fopen (filename, "r"); strcpy (p, home_dir); strcat (p, EDIT_DIR "/"); strcat (p, filename); syntax_free (error_file_name); error_file_name = (char *) strdup (p); f = fopen (p, "r"); if (f) return f; strcpy (p, LIBDIR "/syntax/"); strcat (p, filename); syntax_free (error_file_name); error_file_name = (char *) strdup (p); return fopen (p, "r");}/* returns line number on error */static int edit_read_syntax_rules (WEdit * edit, FILE * f){ FILE *g = 0; char *fg, *bg; char last_fg[32] = "", last_bg[32] = ""; char whole_right[512]; char whole_left[512]; char *args[1024], *l = 0; int save_line = 0, line = 0; struct context_rule **r, *c = 0; int num_words = -1, num_contexts = -1; int argc, result = 0; int i, j; args[0] = 0; strcpy (whole_left, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890"); strcpy (whole_right, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890"); r = edit->rules = syntax_malloc (MAX_CONTEXTS * sizeof (struct context_rule *)); for (;;) { char **a; line++; l = 0; if (!read_one_line (&l, f)) { if (g) { fclose (f); f = g; g = 0; line = save_line + 1; syntax_free (error_file_name); if (l) syntax_free (l); if (!read_one_line (&l, f)) break; } else { break; } } get_args (l, args, &argc); a = args + 1; if (!args[0]) { /* do nothing */ } else if (!strcmp (args[0], "include")) { if (g || argc != 2) { result = line; break; } g = f; f = open_include_file (args[1]); if (!f) { syntax_free (error_file_name); result = line; break; } save_line = line; line = 0; } else if (!strcmp (args[0], "wholechars")) { check_a; if (!strcmp (*a, "left")) { a++; strcpy (whole_left, *a); } else if (!strcmp (*a, "right")) { a++; strcpy (whole_right, *a); } else { strcpy (whole_left, *a); strcpy (whole_right, *a); } a++; check_not_a; } else if (!strcmp (args[0], "context")) { check_a; if (num_contexts == -1) { if (strcmp (*a, "default")) { /* first context is the default */ *a = 0; check_a; } a++; c = r[0] = syntax_malloc (sizeof (struct context_rule)); c->left = (char *) strdup (" "); c->right = (char *) strdup (" "); num_contexts = 0; } else { c = r[num_contexts] = syntax_malloc (sizeof (struct context_rule)); if (!strcmp (*a, "exclusive")) { a++; c->between_delimiters = 1; } check_a; if (!strcmp (*a, "whole")) { a++; c->whole_word_chars_left = (char *) strdup (whole_left); c->whole_word_chars_right = (char *) strdup (whole_right); } else if (!strcmp (*a, "wholeleft")) { a++; c->whole_word_chars_left = (char *) strdup (whole_left); } else if (!strcmp (*a, "wholeright")) { a++; c->whole_word_chars_right = (char *) strdup (whole_right); } check_a; if (!strcmp (*a, "linestart")) { a++; c->line_start_left = 1; } check_a; c->left = (char *) strdup (*a++); check_a; if (!strcmp (*a, "linestart")) { a++; c->line_start_right = 1; } check_a; c->right = (char *) strdup (*a++); c->first_left = *c->left; c->first_right = *c->right; c->single_char = (strlen (c->right) == 1); } c->keyword = syntax_malloc (MAX_WORDS_PER_CONTEXT * sizeof (struct key_word *));#if 0 c->max_words = MAX_WORDS_PER_CONTEXT;#endif num_words = 1; c->keyword[0] = syntax_malloc (sizeof (struct key_word)); fg = *a; if (*a) a++; bg = *a; if (*a) a++; strcpy (last_fg, fg ? fg : ""); strcpy (last_bg, bg ? bg : "");#ifdef MIDNIGHT c->keyword[0]->fg = this_try_alloc_color_pair (fg, bg);#else c->keyword[0]->fg = this_allocate_color (edit, fg); c->keyword[0]->bg = this_allocate_color (edit, bg);#endif c->keyword[0]->keyword = (char *) strdup (" "); check_not_a; num_contexts++; } else if (!strcmp (args[0], "spellcheck")) { if (!c) { result = line; break; } c->spelling = 1; } else if (!strcmp (args[0], "keyword")) { struct key_word *k; if (num_words == -1) *a = 0; check_a; k = r[num_contexts - 1]->keyword[num_words] = syntax_malloc (sizeof (struct key_word)); if (!strcmp (*a, "whole")) { a++; k->whole_word_chars_left = (char *) strdup (whole_left); k->whole_word_chars_right = (char *) strdup (whole_right); } else if (!strcmp (*a, "wholeleft")) { a++; k->whole_word_chars_left = (char *) strdup (whole_left); } else if (!strcmp (*a, "wholeright")) { a++; k->whole_word_chars_right = (char *) strdup (whole_right); } check_a; if (!strcmp (*a, "linestart")) { a++; k->line_start = 1; } check_a; if (!strcmp (*a, "whole")) { *a = 0; check_a; } k->keyword = (char *) strdup (*a++); k->first = *k->keyword; fg = *a; if (*a) a++; bg = *a; if (*a) a++; if (!fg) fg = last_fg; if (!bg) bg = last_bg;#ifdef MIDNIGHT k->fg = this_try_alloc_color_pair (fg, bg);#else k->fg = this_allocate_color (edit, fg); k->bg = this_allocate_color (edit, bg);#endif check_not_a; num_words++; } else if (!strncmp (args[0], "#", 1)) { /* do nothing for comment */ } else if (!strcmp (args[0], "file")) { break; } else { /* anything else is an error */ *a = 0; check_a; } free_args (args); syntax_free (l); } free_args (args); syntax_free (l); if (!edit->rules[0]) syntax_free (edit->rules); if (result) return result; if (num_contexts == -1) { result = line; return result; } { char first_chars[MAX_WORDS_PER_CONTEXT + 2], *p; for (i = 0; edit->rules[i]; i++) { c = edit->rules[i]; p = first_chars; *p++ = (char) 1; for (j = 1; c->keyword[j]; j++) *p++ = c->keyword[j]->first; *p = '\0'; c->keyword_first_chars = syntax_malloc (strlen (first_chars) + 2); strcpy (c->keyword_first_chars, first_chars); } } return result;}#if !defined (GTK) && !defined (MIDNIGHT)/* strdup and append c */static char *strdupc (char *s, int c){ char *t; int l; strcpy (t = syntax_malloc ((l = strlen (s)) + 3), s); t[l] = c; t[l + 1] = '\0'; return t;}static void edit_syntax_clear_keyword (WEdit * edit, int context, int j){ struct context_rule *c; struct _syntax_marker *s; c = edit->rules[context];/* first we clear any instances of this keyword in our cache chain (we used to just clear the cache chain, but this slows things down) */ for (s = edit->syntax_marker; s; s = s->next) if (s->rule.keyword == j) s->rule.keyword = 0; else if (s->rule.keyword > j) s->rule.keyword--; syntax_free (c->keyword[j]->keyword); syntax_free (c->keyword[j]->whole_word_chars_left); syntax_free (c->keyword[j]->whole_word_chars_right); syntax_free (c->keyword[j]); memcpy (&c->keyword[j], &c->keyword[j + 1], (MAX_WORDS_PER_CONTEXT - j - 1) * sizeof (struct keyword *)); strcpy (&c->keyword_first_chars[j], &c->keyword_first_chars[j + 1]);}FILE *spelling_pipe_in = 0;FILE *spelling_pipe_out = 0;pid_t ispell_pid = 0;/* adds a keyword for underlining into the keyword list for this context, returns 1 if too many words */static int edit_syntax_add_keyword (WEdit * edit, char *keyword, int context, time_t t){ int j; char *s; struct context_rule *c; c = edit->rules[context]; for (j = 1; c->keyword[j]; j++) {/* if a keyword has been around for more than TRANSIENT_WORD_TIME_OUT seconds, then remove it - we don't want to run out of space or makes syntax highlighting to slow */ if (c->keyword[j]->time) { if (c->keyword[j]->time + TRANSIENT_WORD_TIME_OUT < t) { edit->force |= REDRAW_PAGE; edit_syntax_clear_keyword (edit, context, j); j--; } } }/* are we out of space? */ if (j >= MAX_WORDS_PER_CONTEXT - 2) return 1;/* add the new keyword and date it */ c->keyword[j + 1] = 0; c->keyword[j] = syntax_malloc (sizeof (struct key_word));#ifdef MIDNIGHT c->keyword[j]->fg = SPELLING_ERROR;#else c->keyword[j]->fg = c->keyword[0]->fg; c->keyword[j]->bg = SPELLING_ERROR;#endif c->keyword[j]->keyword = (char *) strdup (keyword); c->keyword[j]->first = *c->keyword[j]->keyword; c->keyword[j]->whole_word_chars_left = (char *) strdup ("-'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ牎ⅲぅΗЖ┅
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -