1 /* editor syntax highlighting.
3 Copyright (C) 1996, 1997, 1998 the Free Software Foundation
5 Authors: 1998 Paul Sheer
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #define SYNTAX_MARKER_DENSITY 512
30 Mispelled words are flushed from the syntax highlighting rules
31 when they have been around longer than
32 TRANSIENT_WORD_TIME_OUT seconds. At a cursor rate of 30
33 chars per second and say 3 chars + a space per word, we can
34 accumulate 450 words absolute max with a value of 60. This is
35 below this limit of 1024 words in a context.
37 #define TRANSIENT_WORD_TIME_OUT 60
39 #define UNKNOWN_FORMAT "unknown"
43 int option_syntax_highlighting
= 1;
44 int option_auto_spellcheck
= 1;
46 static inline void *syntax_malloc (size_t x
)
54 #define syntax_free(x) {if(x){free(x);(x)=0;}}
55 #define syntax_g_free(x) {if(x){g_free(x);(x)=0;}}
57 static long compare_word_to_right (WEdit
* edit
, long i
, char *text
, char *whole_left
, char *whole_right
, int line_start
)
63 c
= edit_get_byte (edit
, i
- 1);
68 if (strchr (whole_left
, c
))
70 for (p
= (unsigned char *) text
, q
= p
+ strlen ((char *) p
); p
< q
; p
++, i
++) {
75 c
= edit_get_byte (edit
, i
);
78 if (!strchr (whole_right
, c
))
91 c
= edit_get_byte (edit
, i
);
94 if (*p
== *text
&& !p
[1]) /* handle eg '+' and @+@ keywords properly */
97 if (j
&& strchr ((char *) p
+ 1, c
)) /* c exists further down, so it will get matched later */
99 if (c
== '\n' || c
== '\t' || c
== ' ') {
110 if (!strchr (whole_right
, c
)) {
128 c
= edit_get_byte (edit
, i
);
129 for (j
= 0; p
[j
] != '\003'; j
++)
134 j
= c
; /* dummy command */
144 c
= edit_get_byte (edit
, i
);
145 for (; *p
!= '\004'; p
++)
150 for (; *p
!= '\004'; p
++);
153 if (*p
!= edit_get_byte (edit
, i
))
158 if (strchr (whole_right
, edit_get_byte (edit
, i
)))
164 if (*s < '\005' || *s == (unsigned char) c) \
168 static inline char *xx_strchr (const unsigned char *s
, int c
)
171 XXX XXX XXX XXX XXX XXX XXX XXX
;
172 XXX XXX XXX XXX XXX XXX XXX XXX
;
178 static inline struct syntax_rule
apply_rules_going_right (WEdit
* edit
, long i
, struct syntax_rule rule
)
180 struct context_rule
*r
;
181 int contextchanged
= 0, c
;
182 int found_right
= 0, found_left
= 0, keyword_foundleft
= 0, keyword_foundright
= 0;
185 struct syntax_rule _rule
= rule
;
186 if (!(c
= edit_get_byte (edit
, i
)))
188 is_end
= (rule
.end
== (unsigned char) i
);
189 /* check to turn off a keyword */
191 if (edit_get_byte (edit
, i
- 1) == '\n')
195 keyword_foundleft
= 1;
198 /* check to turn off a context */
199 if (_rule
.context
&& !_rule
.keyword
) {
201 r
= edit
->rules
[_rule
.context
];
202 if (r
->first_right
== c
&& !(rule
.border
& RULE_ON_RIGHT_BORDER
) && (e
= compare_word_to_right (edit
, i
, r
->right
, r
->whole_word_chars_left
, r
->whole_word_chars_right
, r
->line_start_right
)) > 0) {
205 _rule
.border
= RULE_ON_RIGHT_BORDER
;
206 if (r
->between_delimiters
)
208 } else if (is_end
&& rule
.border
& RULE_ON_RIGHT_BORDER
) {
209 /* always turn off a context at 4 */
212 if (!keyword_foundleft
)
214 } else if (is_end
&& rule
.border
& RULE_ON_LEFT_BORDER
) {
215 /* never turn off a context at 2 */
220 /* check to turn on a keyword */
221 if (!_rule
.keyword
) {
223 p
= (r
= edit
->rules
[_rule
.context
])->keyword_first_chars
;
225 while (*(p
= xx_strchr ((unsigned char *) p
+ 1, c
))) {
229 count
= p
- r
->keyword_first_chars
;
230 k
= r
->keyword
[count
];
231 e
= compare_word_to_right (edit
, i
, k
->keyword
, k
->whole_word_chars_left
, k
->whole_word_chars_right
, k
->line_start
);
235 _rule
.keyword
= count
;
236 keyword_foundright
= 1;
241 /* check to turn on a context */
242 if (!_rule
.context
) {
243 if (!found_left
&& is_end
) {
244 if (rule
.border
& RULE_ON_RIGHT_BORDER
) {
249 } else if (rule
.border
& RULE_ON_LEFT_BORDER
) {
250 r
= edit
->rules
[_rule
._context
];
252 if (r
->between_delimiters
) {
254 _rule
.context
= _rule
._context
;
257 if (r
->first_right
== c
&& (e
= compare_word_to_right (edit
, i
, r
->right
, r
->whole_word_chars_left
, r
->whole_word_chars_right
, r
->line_start_right
)) >= end
) {
260 _rule
.border
= RULE_ON_RIGHT_BORDER
;
268 struct context_rule
**rules
= edit
->rules
;
269 for (count
= 1; rules
[count
]; count
++) {
271 if (r
->first_left
== c
) {
273 e
= compare_word_to_right (edit
, i
, r
->left
, r
->whole_word_chars_left
, r
->whole_word_chars_right
, r
->line_start_left
);
274 if (e
>= end
&& (!_rule
.keyword
|| keyword_foundright
)) {
277 _rule
.border
= RULE_ON_LEFT_BORDER
;
278 _rule
._context
= count
;
279 if (!r
->between_delimiters
)
280 if (!_rule
.keyword
) {
281 _rule
.context
= count
;
290 /* check again to turn on a keyword if the context switched */
291 if (contextchanged
&& !_rule
.keyword
) {
293 p
= (r
= edit
->rules
[_rule
.context
])->keyword_first_chars
;
294 while (*(p
= xx_strchr ((unsigned char *) p
+ 1, c
))) {
298 count
= p
- r
->keyword_first_chars
;
299 k
= r
->keyword
[count
];
300 e
= compare_word_to_right (edit
, i
, k
->keyword
, k
->whole_word_chars_left
, k
->whole_word_chars_right
, k
->line_start
);
303 _rule
.keyword
= count
;
311 static struct syntax_rule
edit_get_rule (WEdit
* edit
, long byte_index
)
314 if (byte_index
> edit
->last_get_rule
) {
315 for (i
= edit
->last_get_rule
+ 1; i
<= byte_index
; i
++) {
316 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
317 if (i
> (edit
->syntax_marker
? edit
->syntax_marker
->offset
+ SYNTAX_MARKER_DENSITY
: SYNTAX_MARKER_DENSITY
)) {
318 struct _syntax_marker
*s
;
319 s
= edit
->syntax_marker
;
320 edit
->syntax_marker
= syntax_malloc (sizeof (struct _syntax_marker
));
321 edit
->syntax_marker
->next
= s
;
322 edit
->syntax_marker
->offset
= i
;
323 edit
->syntax_marker
->rule
= edit
->rule
;
326 } else if (byte_index
< edit
->last_get_rule
) {
327 struct _syntax_marker
*s
;
329 if (!edit
->syntax_marker
) {
330 memset (&edit
->rule
, 0, sizeof (edit
->rule
));
331 for (i
= -1; i
<= byte_index
; i
++)
332 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
335 if (byte_index
>= edit
->syntax_marker
->offset
) {
336 edit
->rule
= edit
->syntax_marker
->rule
;
337 for (i
= edit
->syntax_marker
->offset
+ 1; i
<= byte_index
; i
++)
338 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
341 s
= edit
->syntax_marker
->next
;
342 syntax_free (edit
->syntax_marker
);
343 edit
->syntax_marker
= s
;
346 edit
->last_get_rule
= byte_index
;
350 static void translate_rule_to_color (WEdit
* edit
, struct syntax_rule rule
, int *color
)
353 k
= edit
->rules
[rule
.context
]->keyword
[rule
.keyword
];
357 void edit_get_syntax_color (WEdit
* edit
, long byte_index
, int *color
)
359 if (edit
->rules
&& byte_index
< edit
->last_byte
&&
360 option_syntax_highlighting
&& use_colors
) {
361 translate_rule_to_color (edit
, edit_get_rule (edit
, byte_index
), color
);
363 *color
= use_colors
? EDITOR_NORMAL_COLOR_INDEX
: 0;
369 Returns 0 on error/eof or a count of the number of bytes read
370 including the newline. Result must be free'd.
372 static int read_one_line (char **line
, FILE * f
)
375 int len
= 256, c
, r
= 0, i
= 0;
376 p
= syntax_malloc (len
);
387 } else if (c
== '\n') {
388 r
= i
+ 1; /* extra 1 for the newline just read */
393 q
= syntax_malloc (len
* 2);
407 static char *convert (char *s
)
471 #define whiteness(x) ((x) == '\t' || (x) == '\n' || (x) == ' ')
473 static void get_args (char *l
, char **args
, int *argc
)
478 while (*p
&& whiteness (*p
))
482 for (l
= p
+ 1; *l
&& !whiteness (*l
); l
++);
493 #define break_a {result=line;break;}
494 #define check_a {if(!*a){result=line;break;}}
495 #define check_not_a {if(*a){result=line;break;}}
497 int try_alloc_color_pair (char *fg
, char *bg
);
499 int this_try_alloc_color_pair (char *fg
, char *bg
)
501 char f
[80], b
[80], *p
;
522 return try_alloc_color_pair (fg
, bg
);
525 static char *error_file_name
= 0;
527 static FILE *open_include_file (char *filename
)
531 syntax_g_free (error_file_name
);
532 error_file_name
= g_strdup (filename
);
533 if (*filename
== PATH_SEP
)
534 return fopen (filename
, "r");
536 g_free (error_file_name
);
537 error_file_name
= g_strconcat (home_dir
, EDIT_DIR PATH_SEP_STR
,
539 f
= fopen (error_file_name
, "r");
543 g_free (error_file_name
);
544 error_file_name
= g_strconcat (mc_home
, PATH_SEP_STR
"syntax" PATH_SEP_STR
,
546 return fopen (error_file_name
, "r");
549 /* returns line number on error */
550 static int edit_read_syntax_rules (WEdit
* edit
, FILE * f
)
554 char last_fg
[32] = "", last_bg
[32] = "";
555 char whole_right
[512];
556 char whole_left
[512];
557 char *args
[1024], *l
= 0;
558 int save_line
= 0, line
= 0;
559 struct context_rule
**r
, *c
= 0;
560 int num_words
= -1, num_contexts
= -1;
561 int argc
, result
= 0;
566 strcpy (whole_left
, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
567 strcpy (whole_right
, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
569 r
= edit
->rules
= syntax_malloc (MAX_CONTEXTS
* sizeof (struct context_rule
*));
575 if (!read_one_line (&l
, f
)) {
580 line
= save_line
+ 1;
581 syntax_g_free (error_file_name
);
584 if (!read_one_line (&l
, f
))
590 get_args (l
, args
, &argc
);
594 } else if (!strcmp (args
[0], "include")) {
595 if (g
|| argc
!= 2) {
600 f
= open_include_file (args
[1]);
602 syntax_g_free (error_file_name
);
608 } else if (!strcmp (args
[0], "wholechars")) {
610 if (!strcmp (*a
, "left")) {
612 strcpy (whole_left
, *a
);
613 } else if (!strcmp (*a
, "right")) {
615 strcpy (whole_right
, *a
);
617 strcpy (whole_left
, *a
);
618 strcpy (whole_right
, *a
);
622 } else if (!strcmp (args
[0], "context")) {
624 if (num_contexts
== -1) {
625 if (strcmp (*a
, "default")) { /* first context is the default */
629 c
= r
[0] = syntax_malloc (sizeof (struct context_rule
));
630 c
->left
= (char *) strdup (" ");
631 c
->right
= (char *) strdup (" ");
634 c
= r
[num_contexts
] = syntax_malloc (sizeof (struct context_rule
));
635 if (!strcmp (*a
, "exclusive")) {
637 c
->between_delimiters
= 1;
640 if (!strcmp (*a
, "whole")) {
642 c
->whole_word_chars_left
= (char *) strdup (whole_left
);
643 c
->whole_word_chars_right
= (char *) strdup (whole_right
);
644 } else if (!strcmp (*a
, "wholeleft")) {
646 c
->whole_word_chars_left
= (char *) strdup (whole_left
);
647 } else if (!strcmp (*a
, "wholeright")) {
649 c
->whole_word_chars_right
= (char *) strdup (whole_right
);
652 if (!strcmp (*a
, "linestart")) {
654 c
->line_start_left
= 1;
657 c
->left
= (char *) strdup (*a
++);
659 if (!strcmp (*a
, "linestart")) {
661 c
->line_start_right
= 1;
664 c
->right
= (char *) strdup (*a
++);
665 c
->first_left
= *c
->left
;
666 c
->first_right
= *c
->right
;
668 c
->keyword
= syntax_malloc (MAX_WORDS_PER_CONTEXT
* sizeof (struct key_word
*));
670 c
->max_words
= MAX_WORDS_PER_CONTEXT
;
673 c
->keyword
[0] = syntax_malloc (sizeof (struct key_word
));
680 strcpy (last_fg
, fg
? fg
: "");
681 strcpy (last_bg
, bg
? bg
: "");
682 c
->keyword
[0]->color
= this_try_alloc_color_pair (fg
, bg
);
683 c
->keyword
[0]->keyword
= (char *) strdup (" ");
686 } else if (!strcmp (args
[0], "spellcheck")) {
692 } else if (!strcmp (args
[0], "keyword")) {
697 k
= r
[num_contexts
- 1]->keyword
[num_words
] = syntax_malloc (sizeof (struct key_word
));
698 if (!strcmp (*a
, "whole")) {
700 k
->whole_word_chars_left
= (char *) strdup (whole_left
);
701 k
->whole_word_chars_right
= (char *) strdup (whole_right
);
702 } else if (!strcmp (*a
, "wholeleft")) {
704 k
->whole_word_chars_left
= (char *) strdup (whole_left
);
705 } else if (!strcmp (*a
, "wholeright")) {
707 k
->whole_word_chars_right
= (char *) strdup (whole_right
);
710 if (!strcmp (*a
, "linestart")) {
715 if (!strcmp (*a
, "whole")) {
718 k
->keyword
= (char *) strdup (*a
++);
719 k
->first
= *k
->keyword
;
730 k
->color
= this_try_alloc_color_pair (fg
, bg
);
733 } else if (*(args
[0]) == '#') {
734 /* do nothing for comment */
735 } else if (!strcmp (args
[0], "file")) {
737 } else { /* anything else is an error */
747 syntax_free (edit
->rules
);
752 if (num_contexts
== -1) {
758 char first_chars
[MAX_WORDS_PER_CONTEXT
+ 2], *p
;
759 for (i
= 0; edit
->rules
[i
]; i
++) {
763 for (j
= 1; c
->keyword
[j
]; j
++)
764 *p
++ = c
->keyword
[j
]->first
;
766 c
->keyword_first_chars
= syntax_malloc (strlen (first_chars
) + 2);
767 strcpy (c
->keyword_first_chars
, first_chars
);
774 int edit_check_spelling (WEdit
* edit
)
779 void (*syntax_change_callback
) (CWidget
*) = 0;
781 void edit_set_syntax_change_callback (void (*callback
) (CWidget
*))
783 syntax_change_callback
= callback
;
786 void edit_free_syntax_rules (WEdit
* edit
)
793 edit_get_rule (edit
, -1);
794 syntax_free (edit
->syntax_type
);
795 edit
->syntax_type
= 0;
796 if (syntax_change_callback
)
797 (*syntax_change_callback
) (&edit
->widget
);
798 for (i
= 0; edit
->rules
[i
]; i
++) {
799 if (edit
->rules
[i
]->keyword
) {
800 for (j
= 0; edit
->rules
[i
]->keyword
[j
]; j
++) {
801 syntax_free (edit
->rules
[i
]->keyword
[j
]->keyword
);
802 syntax_free (edit
->rules
[i
]->keyword
[j
]->whole_word_chars_left
);
803 syntax_free (edit
->rules
[i
]->keyword
[j
]->whole_word_chars_right
);
804 syntax_free (edit
->rules
[i
]->keyword
[j
]);
807 syntax_free (edit
->rules
[i
]->left
);
808 syntax_free (edit
->rules
[i
]->right
);
809 syntax_free (edit
->rules
[i
]->whole_word_chars_left
);
810 syntax_free (edit
->rules
[i
]->whole_word_chars_right
);
811 syntax_free (edit
->rules
[i
]->keyword
);
812 syntax_free (edit
->rules
[i
]->keyword_first_chars
);
813 syntax_free (edit
->rules
[i
]);
815 while (edit
->syntax_marker
) {
816 struct _syntax_marker
*s
= edit
->syntax_marker
->next
;
817 syntax_free (edit
->syntax_marker
);
818 edit
->syntax_marker
= s
;
820 syntax_free (edit
->rules
);
823 /* returns -1 on file error, line number on error in file syntax */
824 static int edit_read_syntax_file (WEdit
* edit
, char **names
, char *syntax_file
, char *editor_file
, char *first_line
, char *type
)
828 regmatch_t pmatch
[1];
829 char *args
[1024], *l
= 0;
836 f
= fopen (syntax_file
, "r");
838 lib_file
= concat_dir_and_file (mc_home
, "syntax" PATH_SEP_STR
"Syntax");
839 f
= fopen (lib_file
, "r");
848 if (!read_one_line (&l
, f
))
850 get_args (l
, args
, &argc
);
853 /* looking for `file ...' lines only */
854 if (strcmp (args
[0], "file")) {
858 /* must have two args or report error */
859 if (!args
[1] || !args
[2]) {
864 /* 1: just collecting a list of names of rule sets */
865 names
[count
++] = (char *) strdup (args
[2]);
868 /* 2: rule set was explicitly specified by the caller */
869 if (!strcmp (type
, args
[2]))
871 } else if (editor_file
&& edit
) {
872 /* 3: auto-detect rule set from regular expressions */
874 if (regcomp (&r
, args
[1], REG_EXTENDED
)) {
878 /* does filename match arg 1 ? */
879 q
= !regexec (&r
, editor_file
, 1, pmatch
, 0);
882 if (regcomp (&r
, args
[3], REG_EXTENDED
)) {
886 /* does first line match arg 3 ? */
887 q
= !regexec (&r
, first_line
, 1, pmatch
, 0);
893 line_error
= edit_read_syntax_rules (edit
, f
);
895 if (!error_file_name
) /* an included file */
896 result
= line
+ line_error
;
900 syntax_free (edit
->syntax_type
);
901 edit
->syntax_type
= (char *) strdup (args
[2]);
902 /* if there are no rules then turn off syntax highlighting for speed */
904 if (!edit
->rules
[0]->keyword
[1] && !edit
->rules
[0]->spelling
) {
905 edit_free_syntax_rules (edit
);
908 /* notify the callback of a change in rule set */
909 if (syntax_change_callback
)
910 (*syntax_change_callback
) (&edit
->widget
);
923 static char *get_first_editor_line (WEdit
* edit
)
930 for (i
= 0; i
< 255; i
++) {
931 s
[i
] = edit_get_byte (edit
, i
);
941 /* loads rules into edit struct. one of edit or names must be zero. if
942 edit is zero, a list of types will be stored into name. type may be zero
943 in which case the type will be selected according to the filename. */
944 void edit_load_syntax (WEdit
* edit
, char **names
, char *type
)
949 edit_free_syntax_rules (edit
);
954 if (!option_syntax_highlighting
)
960 if (!*edit
->filename
&& !type
)
963 f
= catstrs (home_dir
, SYNTAX_FILE
, 0);
964 r
= edit_read_syntax_file (edit
, names
, f
, edit
? edit
->filename
: 0, get_first_editor_line (edit
), type
);
966 edit_free_syntax_rules (edit
);
967 edit_error_dialog (_ (" Load syntax file "), _ (" File access error "));
971 edit_free_syntax_rules (edit
);
972 message (0, _(" Load syntax file "),
973 _(" Error in file %s on line %d "),
974 error_file_name
? error_file_name
: f
, r
);
975 syntax_g_free (error_file_name
);
982 int option_syntax_highlighting
= 0;
984 void edit_load_syntax (WEdit
* edit
, char **names
, char *type
)
989 void edit_free_syntax_rules (WEdit
* edit
)
994 void edit_get_syntax_color (WEdit
* edit
, long byte_index
, int *color
)
996 *color
= use_colors
? EDITOR_NORMAL_COLOR_INDEX
: 0;
999 int edit_check_spelling (WEdit
* edit
)
1004 #endif /* HAVE_SYNTAXH */