1 /* editor syntax highlighting.
3 Copyright (C) 1996, 1997, 1998 the Free Software Foundation
5 Authors: 1998 Paul Sheer
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 #define SYNTAX_MARKER_DENSITY 512
32 Mispelled words are flushed from the syntax highlighting rules
33 when they have been around longer than
34 TRANSIENT_WORD_TIME_OUT seconds. At a cursor rate of 30
35 chars per second and say 3 chars + a space per word, we can
36 accumulate 450 words absolute max with a value of 60. This is
37 below this limit of 1024 words in a context.
39 #define TRANSIENT_WORD_TIME_OUT 60
41 #define UNKNOWN_FORMAT "unknown"
45 int option_syntax_highlighting
= 1;
46 int option_auto_spellcheck
= 1;
48 static inline void *syntax_malloc (size_t x
)
56 #define syntax_free(x) {if(x){free(x);(x)=0;}}
57 #define syntax_g_free(x) {if(x){g_free(x);(x)=0;}}
59 static long compare_word_to_right (WEdit
* edit
, long i
, char *text
, char *whole_left
, char *whole_right
, int line_start
)
65 c
= edit_get_byte (edit
, i
- 1);
70 if (strchr (whole_left
, c
))
72 for (p
= (unsigned char *) text
, q
= p
+ strlen ((char *) p
); p
< q
; p
++, i
++) {
77 c
= edit_get_byte (edit
, i
);
80 if (!strchr (whole_right
, c
))
93 c
= edit_get_byte (edit
, i
);
96 if (*p
== *text
&& !p
[1]) /* handle eg '+' and @+@ keywords properly */
99 if (j
&& strchr ((char *) p
+ 1, c
)) /* c exists further down, so it will get matched later */
101 if (c
== '\n' || c
== '\t' || c
== ' ') {
112 if (!strchr (whole_right
, c
)) {
130 c
= edit_get_byte (edit
, i
);
131 for (j
= 0; p
[j
] != '\003'; j
++)
136 j
= c
; /* dummy command */
146 c
= edit_get_byte (edit
, i
);
147 for (; *p
!= '\004'; p
++)
152 for (; *p
!= '\004'; p
++);
155 if (*p
!= edit_get_byte (edit
, i
))
160 if (strchr (whole_right
, edit_get_byte (edit
, i
)))
166 if (*s < '\005' || *s == (unsigned char) c) \
170 static inline char *xx_strchr (const unsigned char *s
, int c
)
173 XXX XXX XXX XXX XXX XXX XXX XXX
;
174 XXX XXX XXX XXX XXX XXX XXX XXX
;
180 static inline struct syntax_rule
apply_rules_going_right (WEdit
* edit
, long i
, struct syntax_rule rule
)
182 struct context_rule
*r
;
183 int contextchanged
= 0, c
;
184 int found_right
= 0, found_left
= 0, keyword_foundleft
= 0, keyword_foundright
= 0;
187 struct syntax_rule _rule
= rule
;
188 if (!(c
= edit_get_byte (edit
, i
)))
190 is_end
= (rule
.end
== (unsigned char) i
);
191 /* check to turn off a keyword */
194 k
= edit
->rules
[_rule
.context
]->keyword
[_rule
.keyword
];
195 if (edit_get_byte (edit
, i
- 1) == '\n')
199 keyword_foundleft
= 1;
202 /* check to turn off a context */
203 if (_rule
.context
&& !_rule
.keyword
) {
205 r
= edit
->rules
[_rule
.context
];
206 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) {
209 _rule
.border
= RULE_ON_RIGHT_BORDER
;
210 if (r
->between_delimiters
)
212 } else if (is_end
&& rule
.border
& RULE_ON_RIGHT_BORDER
) {
213 /* always turn off a context at 4 */
216 if (!keyword_foundleft
)
218 } else if (is_end
&& rule
.border
& RULE_ON_LEFT_BORDER
) {
219 /* never turn off a context at 2 */
224 /* check to turn on a keyword */
225 if (!_rule
.keyword
) {
227 p
= (r
= edit
->rules
[_rule
.context
])->keyword_first_chars
;
229 while (*(p
= xx_strchr ((unsigned char *) p
+ 1, c
))) {
233 count
= p
- r
->keyword_first_chars
;
234 k
= r
->keyword
[count
];
235 e
= compare_word_to_right (edit
, i
, k
->keyword
, k
->whole_word_chars_left
, k
->whole_word_chars_right
, k
->line_start
);
239 _rule
.keyword
= count
;
240 keyword_foundright
= 1;
245 /* check to turn on a context */
246 if (!_rule
.context
) {
247 if (!found_left
&& is_end
) {
248 if (rule
.border
& RULE_ON_RIGHT_BORDER
) {
253 } else if (rule
.border
& RULE_ON_LEFT_BORDER
) {
254 r
= edit
->rules
[_rule
._context
];
256 if (r
->between_delimiters
) {
258 _rule
.context
= _rule
._context
;
261 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
) {
264 _rule
.border
= RULE_ON_RIGHT_BORDER
;
272 struct context_rule
**rules
= edit
->rules
;
273 for (count
= 1; rules
[count
]; count
++) {
275 if (r
->first_left
== c
) {
277 e
= compare_word_to_right (edit
, i
, r
->left
, r
->whole_word_chars_left
, r
->whole_word_chars_right
, r
->line_start_left
);
278 if (e
>= end
&& (!_rule
.keyword
|| keyword_foundright
)) {
281 _rule
.border
= RULE_ON_LEFT_BORDER
;
282 _rule
._context
= count
;
283 if (!r
->between_delimiters
)
284 if (!_rule
.keyword
) {
285 _rule
.context
= count
;
294 /* check again to turn on a keyword if the context switched */
295 if (contextchanged
&& !_rule
.keyword
) {
297 p
= (r
= edit
->rules
[_rule
.context
])->keyword_first_chars
;
298 while (*(p
= xx_strchr ((unsigned char *) p
+ 1, c
))) {
302 count
= p
- r
->keyword_first_chars
;
303 k
= r
->keyword
[count
];
304 e
= compare_word_to_right (edit
, i
, k
->keyword
, k
->whole_word_chars_left
, k
->whole_word_chars_right
, k
->line_start
);
307 _rule
.keyword
= count
;
315 static struct syntax_rule
edit_get_rule (WEdit
* edit
, long byte_index
)
318 if (byte_index
> edit
->last_get_rule
) {
319 for (i
= edit
->last_get_rule
+ 1; i
<= byte_index
; i
++) {
320 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
321 if (i
> (edit
->syntax_marker
? edit
->syntax_marker
->offset
+ SYNTAX_MARKER_DENSITY
: SYNTAX_MARKER_DENSITY
)) {
322 struct _syntax_marker
*s
;
323 s
= edit
->syntax_marker
;
324 edit
->syntax_marker
= syntax_malloc (sizeof (struct _syntax_marker
));
325 edit
->syntax_marker
->next
= s
;
326 edit
->syntax_marker
->offset
= i
;
327 edit
->syntax_marker
->rule
= edit
->rule
;
330 } else if (byte_index
< edit
->last_get_rule
) {
331 struct _syntax_marker
*s
;
333 if (!edit
->syntax_marker
) {
334 memset (&edit
->rule
, 0, sizeof (edit
->rule
));
335 for (i
= -1; i
<= byte_index
; i
++)
336 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
339 if (byte_index
>= edit
->syntax_marker
->offset
) {
340 edit
->rule
= edit
->syntax_marker
->rule
;
341 for (i
= edit
->syntax_marker
->offset
+ 1; i
<= byte_index
; i
++)
342 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
345 s
= edit
->syntax_marker
->next
;
346 syntax_free (edit
->syntax_marker
);
347 edit
->syntax_marker
= s
;
350 edit
->last_get_rule
= byte_index
;
354 static void translate_rule_to_color (WEdit
* edit
, struct syntax_rule rule
, int *color
)
357 k
= edit
->rules
[rule
.context
]->keyword
[rule
.keyword
];
361 void edit_get_syntax_color (WEdit
* edit
, long byte_index
, int *color
)
363 if (edit
->rules
&& byte_index
< edit
->last_byte
&&
364 option_syntax_highlighting
&& use_colors
) {
365 translate_rule_to_color (edit
, edit_get_rule (edit
, byte_index
), color
);
367 *color
= use_colors
? EDITOR_NORMAL_COLOR_INDEX
: 0;
373 Returns 0 on error/eof or a count of the number of bytes read
374 including the newline. Result must be free'd.
377 static int mad_read_one_line (char **line
, FILE * f
, char *file
, int line_
)
378 #define read_one_line(a,b) mad_read_one_line(a,b,__FILE__,__LINE__)
380 static int read_one_line (char **line
, FILE * f
)
384 int len
= 256, c
, r
= 0, i
= 0;
385 p
= syntax_malloc (len
);
396 } else if (c
== '\n') {
397 r
= i
+ 1; /* extra 1 for the newline just read */
402 q
= syntax_malloc (len
* 2);
416 static char *convert (char *s
)
480 #define whiteness(x) ((x) == '\t' || (x) == '\n' || (x) == ' ')
482 static void get_args (char *l
, char **args
, int *argc
)
487 while (*p
&& whiteness (*p
))
491 for (l
= p
+ 1; *l
&& !whiteness (*l
); l
++);
502 #define break_a {result=line;break;}
503 #define check_a {if(!*a){result=line;break;}}
504 #define check_not_a {if(*a){result=line;break;}}
506 int try_alloc_color_pair (char *fg
, char *bg
);
508 int this_try_alloc_color_pair (char *fg
, char *bg
)
510 char f
[80], b
[80], *p
;
531 return try_alloc_color_pair (fg
, bg
);
534 static char *error_file_name
= 0;
536 static FILE *open_include_file (char *filename
)
540 syntax_g_free (error_file_name
);
541 error_file_name
= g_strdup (filename
);
542 if (*filename
== PATH_SEP
)
543 return fopen (filename
, "r");
545 g_free (error_file_name
);
546 error_file_name
= g_strconcat (home_dir
, EDIT_DIR PATH_SEP_STR
,
548 f
= fopen (error_file_name
, "r");
552 g_free (error_file_name
);
553 error_file_name
= g_strconcat (mc_home
, PATH_SEP_STR
"syntax" PATH_SEP_STR
,
555 return fopen (error_file_name
, "r");
558 /* returns line number on error */
559 static int edit_read_syntax_rules (WEdit
* edit
, FILE * f
)
563 char last_fg
[32] = "", last_bg
[32] = "";
564 char whole_right
[512];
565 char whole_left
[512];
566 char *args
[1024], *l
= 0;
567 int save_line
= 0, line
= 0;
568 struct context_rule
**r
, *c
= 0;
569 int num_words
= -1, num_contexts
= -1;
570 int argc
, result
= 0;
575 strcpy (whole_left
, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
576 strcpy (whole_right
, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
578 r
= edit
->rules
= syntax_malloc (MAX_CONTEXTS
* sizeof (struct context_rule
*));
584 if (!read_one_line (&l
, f
)) {
589 line
= save_line
+ 1;
590 syntax_g_free (error_file_name
);
593 if (!read_one_line (&l
, f
))
599 get_args (l
, args
, &argc
);
603 } else if (!strcmp (args
[0], "include")) {
604 if (g
|| argc
!= 2) {
609 f
= open_include_file (args
[1]);
611 syntax_g_free (error_file_name
);
617 } else if (!strcmp (args
[0], "wholechars")) {
619 if (!strcmp (*a
, "left")) {
621 strcpy (whole_left
, *a
);
622 } else if (!strcmp (*a
, "right")) {
624 strcpy (whole_right
, *a
);
626 strcpy (whole_left
, *a
);
627 strcpy (whole_right
, *a
);
631 } else if (!strcmp (args
[0], "context")) {
633 if (num_contexts
== -1) {
634 if (strcmp (*a
, "default")) { /* first context is the default */
638 c
= r
[0] = syntax_malloc (sizeof (struct context_rule
));
639 c
->left
= (char *) strdup (" ");
640 c
->right
= (char *) strdup (" ");
643 c
= r
[num_contexts
] = syntax_malloc (sizeof (struct context_rule
));
644 if (!strcmp (*a
, "exclusive")) {
646 c
->between_delimiters
= 1;
649 if (!strcmp (*a
, "whole")) {
651 c
->whole_word_chars_left
= (char *) strdup (whole_left
);
652 c
->whole_word_chars_right
= (char *) strdup (whole_right
);
653 } else if (!strcmp (*a
, "wholeleft")) {
655 c
->whole_word_chars_left
= (char *) strdup (whole_left
);
656 } else if (!strcmp (*a
, "wholeright")) {
658 c
->whole_word_chars_right
= (char *) strdup (whole_right
);
661 if (!strcmp (*a
, "linestart")) {
663 c
->line_start_left
= 1;
666 c
->left
= (char *) strdup (*a
++);
668 if (!strcmp (*a
, "linestart")) {
670 c
->line_start_right
= 1;
673 c
->right
= (char *) strdup (*a
++);
674 c
->first_left
= *c
->left
;
675 c
->first_right
= *c
->right
;
677 c
->single_char
= (strlen (c
->right
) == 1);
680 c
->keyword
= syntax_malloc (MAX_WORDS_PER_CONTEXT
* sizeof (struct key_word
*));
682 c
->max_words
= MAX_WORDS_PER_CONTEXT
;
685 c
->keyword
[0] = syntax_malloc (sizeof (struct key_word
));
692 strcpy (last_fg
, fg
? fg
: "");
693 strcpy (last_bg
, bg
? bg
: "");
694 c
->keyword
[0]->color
= this_try_alloc_color_pair (fg
, bg
);
695 c
->keyword
[0]->keyword
= (char *) strdup (" ");
698 } else if (!strcmp (args
[0], "spellcheck")) {
704 } else if (!strcmp (args
[0], "keyword")) {
709 k
= r
[num_contexts
- 1]->keyword
[num_words
] = syntax_malloc (sizeof (struct key_word
));
710 if (!strcmp (*a
, "whole")) {
712 k
->whole_word_chars_left
= (char *) strdup (whole_left
);
713 k
->whole_word_chars_right
= (char *) strdup (whole_right
);
714 } else if (!strcmp (*a
, "wholeleft")) {
716 k
->whole_word_chars_left
= (char *) strdup (whole_left
);
717 } else if (!strcmp (*a
, "wholeright")) {
719 k
->whole_word_chars_right
= (char *) strdup (whole_right
);
722 if (!strcmp (*a
, "linestart")) {
727 if (!strcmp (*a
, "whole")) {
730 k
->keyword
= (char *) strdup (*a
++);
731 k
->first
= *k
->keyword
;
742 k
->color
= this_try_alloc_color_pair (fg
, bg
);
745 } else if (!strncmp (args
[0], "#", 1)) {
746 /* do nothing for comment */
747 } else if (!strcmp (args
[0], "file")) {
749 } else { /* anything else is an error */
759 syntax_free (edit
->rules
);
764 if (num_contexts
== -1) {
770 char first_chars
[MAX_WORDS_PER_CONTEXT
+ 2], *p
;
771 for (i
= 0; edit
->rules
[i
]; i
++) {
775 for (j
= 1; c
->keyword
[j
]; j
++)
776 *p
++ = c
->keyword
[j
]->first
;
778 c
->keyword_first_chars
= syntax_malloc (strlen (first_chars
) + 2);
779 strcpy (c
->keyword_first_chars
, first_chars
);
786 int edit_check_spelling (WEdit
* edit
)
791 void (*syntax_change_callback
) (CWidget
*) = 0;
793 void edit_set_syntax_change_callback (void (*callback
) (CWidget
*))
795 syntax_change_callback
= callback
;
798 void edit_free_syntax_rules (WEdit
* edit
)
805 edit_get_rule (edit
, -1);
806 syntax_free (edit
->syntax_type
);
807 edit
->syntax_type
= 0;
808 if (syntax_change_callback
)
809 (*syntax_change_callback
) (&edit
->widget
);
810 for (i
= 0; edit
->rules
[i
]; i
++) {
811 if (edit
->rules
[i
]->keyword
) {
812 for (j
= 0; edit
->rules
[i
]->keyword
[j
]; j
++) {
813 syntax_free (edit
->rules
[i
]->keyword
[j
]->keyword
);
814 syntax_free (edit
->rules
[i
]->keyword
[j
]->whole_word_chars_left
);
815 syntax_free (edit
->rules
[i
]->keyword
[j
]->whole_word_chars_right
);
816 syntax_free (edit
->rules
[i
]->keyword
[j
]);
819 syntax_free (edit
->rules
[i
]->left
);
820 syntax_free (edit
->rules
[i
]->right
);
821 syntax_free (edit
->rules
[i
]->whole_word_chars_left
);
822 syntax_free (edit
->rules
[i
]->whole_word_chars_right
);
823 syntax_free (edit
->rules
[i
]->keyword
);
824 syntax_free (edit
->rules
[i
]->keyword_first_chars
);
825 syntax_free (edit
->rules
[i
]);
827 while (edit
->syntax_marker
) {
828 struct _syntax_marker
*s
= edit
->syntax_marker
->next
;
829 syntax_free (edit
->syntax_marker
);
830 edit
->syntax_marker
= s
;
832 syntax_free (edit
->rules
);
835 /* returns -1 on file error, line number on error in file syntax */
836 static int edit_read_syntax_file (WEdit
* edit
, char **names
, char *syntax_file
, char *editor_file
, char *first_line
, char *type
)
840 regmatch_t pmatch
[1];
841 char *args
[1024], *l
= 0;
848 f
= fopen (syntax_file
, "r");
850 lib_file
= concat_dir_and_file (mc_home
, "syntax" PATH_SEP_STR
"Syntax");
851 f
= fopen (lib_file
, "r");
860 if (!read_one_line (&l
, f
))
862 get_args (l
, args
, &argc
);
865 /* looking for `file ...' lines only */
866 if (strcmp (args
[0], "file")) {
870 /* must have two args or report error */
871 if (!args
[1] || !args
[2]) {
876 /* 1: just collecting a list of names of rule sets */
877 names
[count
++] = (char *) strdup (args
[2]);
880 /* 2: rule set was explicitly specified by the caller */
881 if (!strcmp (type
, args
[2]))
883 } else if (editor_file
&& edit
) {
884 /* 3: auto-detect rule set from regular expressions */
886 if (regcomp (&r
, args
[1], REG_EXTENDED
)) {
890 /* does filename match arg 1 ? */
891 q
= !regexec (&r
, editor_file
, 1, pmatch
, 0);
894 if (regcomp (&r
, args
[3], REG_EXTENDED
)) {
898 /* does first line match arg 3 ? */
899 q
= !regexec (&r
, first_line
, 1, pmatch
, 0);
905 line_error
= edit_read_syntax_rules (edit
, f
);
907 if (!error_file_name
) /* an included file */
908 result
= line
+ line_error
;
912 syntax_free (edit
->syntax_type
);
913 edit
->syntax_type
= (char *) strdup (args
[2]);
914 /* if there are no rules then turn off syntax highlighting for speed */
916 if (!edit
->rules
[0]->keyword
[1] && !edit
->rules
[0]->spelling
) {
917 edit_free_syntax_rules (edit
);
920 /* notify the callback of a change in rule set */
921 if (syntax_change_callback
)
922 (*syntax_change_callback
) (&edit
->widget
);
935 static char *get_first_editor_line (WEdit
* edit
)
942 for (i
= 0; i
< 255; i
++) {
943 s
[i
] = edit_get_byte (edit
, i
);
953 /* loads rules into edit struct. one of edit or names must be zero. if
954 edit is zero, a list of types will be stored into name. type may be zero
955 in which case the type will be selected according to the filename. */
956 void edit_load_syntax (WEdit
* edit
, char **names
, char *type
)
961 edit_free_syntax_rules (edit
);
966 if (!option_syntax_highlighting
)
972 if (!*edit
->filename
&& !type
)
975 f
= catstrs (home_dir
, SYNTAX_FILE
, 0);
976 r
= edit_read_syntax_file (edit
, names
, f
, edit
? edit
->filename
: 0, get_first_editor_line (edit
), type
);
978 edit_free_syntax_rules (edit
);
979 edit_error_dialog (_ (" Load syntax file "), _ (" File access error "));
983 edit_free_syntax_rules (edit
);
984 message (0, _(" Load syntax file "),
985 _(" Error in file %s on line %d "),
986 error_file_name
? error_file_name
: f
, r
);
987 syntax_g_free (error_file_name
);
994 int option_syntax_highlighting
= 0;
996 void edit_load_syntax (WEdit
* edit
, char **names
, char *type
)
1001 void edit_free_syntax_rules (WEdit
* edit
)
1006 void edit_get_syntax_color (WEdit
* edit
, long byte_index
, int *color
)
1008 *color
= use_colors
? EDITOR_NORMAL_COLOR_INDEX
: 0;
1011 int edit_check_spelling (WEdit
* edit
)
1016 #endif /* HAVE_SYNTAXH */