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 /* these three functions are called from the outside */
49 void edit_load_syntax (WEdit
* edit
, char **names
, char *type
);
50 void edit_free_syntax_rules (WEdit
* edit
);
51 void edit_get_syntax_color (WEdit
* edit
, long byte_index
, int *fg
, int *bg
);
54 static void *mad_syntax_malloc (size_t x
, char *file
, int line
)
55 #define syntax_malloc(x) mad_syntax_malloc (x, __FILE__, __LINE__)
57 static void *syntax_malloc (size_t x
)
62 p
= mad_alloc (x
, file
, line
);
70 #define syntax_free(x) {if(x){free(x);(x)=0;}}
71 #define syntax_g_free(x) {if(x){g_free(x);(x)=0;}}
73 static long compare_word_to_right (WEdit
* edit
, long i
, char *text
, char *whole_left
, char *whole_right
, int line_start
)
79 c
= edit_get_byte (edit
, i
- 1);
84 if (strchr (whole_left
, c
))
86 for (p
= (unsigned char *) text
, q
= p
+ strlen ((char *) p
); p
< q
; p
++, i
++) {
91 c
= edit_get_byte (edit
, i
);
94 if (!strchr (whole_right
, c
))
107 c
= edit_get_byte (edit
, i
);
110 if (*p
== *text
&& !p
[1]) /* handle eg '+' and @+@ keywords properly */
113 if (j
&& strchr ((char *) p
+ 1, c
)) /* c exists further down, so it will get matched later */
115 if (c
== '\n' || c
== '\t' || c
== ' ') {
126 if (!strchr (whole_right
, c
)) {
144 c
= edit_get_byte (edit
, i
);
145 for (j
= 0; p
[j
] != '\003'; j
++)
150 j
= c
; /* dummy command */
160 c
= edit_get_byte (edit
, i
);
161 for (; *p
!= '\004'; p
++)
166 for (; *p
!= '\004'; p
++);
169 if (*p
!= edit_get_byte (edit
, i
))
174 if (strchr (whole_right
, edit_get_byte (edit
, i
)))
180 if (*s < '\005' || *s == (unsigned char) c) \
184 static inline char *xx_strchr (const unsigned char *s
, int c
)
187 XXX XXX XXX XXX XXX XXX XXX XXX
;
188 XXX XXX XXX XXX XXX XXX XXX XXX
;
194 static inline struct syntax_rule
apply_rules_going_right (WEdit
* edit
, long i
, struct syntax_rule rule
)
196 struct context_rule
*r
;
197 int contextchanged
= 0, c
;
198 int found_right
= 0, found_left
= 0, keyword_foundleft
= 0, keyword_foundright
= 0;
201 struct syntax_rule _rule
= rule
;
202 if (!(c
= edit_get_byte (edit
, i
)))
204 is_end
= (rule
.end
== (unsigned char) i
);
205 /* check to turn off a keyword */
208 k
= edit
->rules
[_rule
.context
]->keyword
[_rule
.keyword
];
209 if (edit_get_byte (edit
, i
- 1) == '\n')
213 keyword_foundleft
= 1;
216 /* check to turn off a context */
217 if (_rule
.context
&& !_rule
.keyword
) {
219 r
= edit
->rules
[_rule
.context
];
220 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) {
223 _rule
.border
= RULE_ON_RIGHT_BORDER
;
224 if (r
->between_delimiters
)
226 } else if (is_end
&& rule
.border
& RULE_ON_RIGHT_BORDER
) {
227 /* always turn off a context at 4 */
230 if (!keyword_foundleft
)
232 } else if (is_end
&& rule
.border
& RULE_ON_LEFT_BORDER
) {
233 /* never turn off a context at 2 */
238 /* check to turn on a keyword */
239 if (!_rule
.keyword
) {
241 p
= (r
= edit
->rules
[_rule
.context
])->keyword_first_chars
;
243 while (*(p
= xx_strchr ((unsigned char *) p
+ 1, c
))) {
247 count
= p
- r
->keyword_first_chars
;
248 k
= r
->keyword
[count
];
249 e
= compare_word_to_right (edit
, i
, k
->keyword
, k
->whole_word_chars_left
, k
->whole_word_chars_right
, k
->line_start
);
253 _rule
.keyword
= count
;
254 keyword_foundright
= 1;
259 /* check to turn on a context */
260 if (!_rule
.context
) {
261 if (!found_left
&& is_end
) {
262 if (rule
.border
& RULE_ON_RIGHT_BORDER
) {
267 } else if (rule
.border
& RULE_ON_LEFT_BORDER
) {
268 r
= edit
->rules
[_rule
._context
];
270 if (r
->between_delimiters
) {
272 _rule
.context
= _rule
._context
;
275 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
) {
278 _rule
.border
= RULE_ON_RIGHT_BORDER
;
286 struct context_rule
**rules
= edit
->rules
;
287 for (count
= 1; rules
[count
]; count
++) {
289 if (r
->first_left
== c
) {
291 e
= compare_word_to_right (edit
, i
, r
->left
, r
->whole_word_chars_left
, r
->whole_word_chars_right
, r
->line_start_left
);
292 if (e
>= end
&& (!_rule
.keyword
|| keyword_foundright
)) {
295 _rule
.border
= RULE_ON_LEFT_BORDER
;
296 _rule
._context
= count
;
297 if (!r
->between_delimiters
)
299 _rule
.context
= count
;
306 /* check again to turn on a keyword if the context switched */
307 if (contextchanged
&& !_rule
.keyword
) {
309 p
= (r
= edit
->rules
[_rule
.context
])->keyword_first_chars
;
310 while (*(p
= xx_strchr ((unsigned char *) p
+ 1, c
))) {
314 count
= p
- r
->keyword_first_chars
;
315 k
= r
->keyword
[count
];
316 e
= compare_word_to_right (edit
, i
, k
->keyword
, k
->whole_word_chars_left
, k
->whole_word_chars_right
, k
->line_start
);
319 _rule
.keyword
= count
;
327 static struct syntax_rule
edit_get_rule (WEdit
* edit
, long byte_index
)
330 if (byte_index
> edit
->last_get_rule
) {
331 for (i
= edit
->last_get_rule
+ 1; i
<= byte_index
; i
++) {
332 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
333 if (i
> (edit
->syntax_marker
? edit
->syntax_marker
->offset
+ SYNTAX_MARKER_DENSITY
: SYNTAX_MARKER_DENSITY
)) {
334 struct _syntax_marker
*s
;
335 s
= edit
->syntax_marker
;
336 edit
->syntax_marker
= syntax_malloc (sizeof (struct _syntax_marker
));
337 edit
->syntax_marker
->next
= s
;
338 edit
->syntax_marker
->offset
= i
;
339 edit
->syntax_marker
->rule
= edit
->rule
;
342 } else if (byte_index
< edit
->last_get_rule
) {
343 struct _syntax_marker
*s
;
345 if (!edit
->syntax_marker
) {
346 memset (&edit
->rule
, 0, sizeof (edit
->rule
));
347 for (i
= -1; i
<= byte_index
; i
++)
348 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
351 if (byte_index
>= edit
->syntax_marker
->offset
) {
352 edit
->rule
= edit
->syntax_marker
->rule
;
353 for (i
= edit
->syntax_marker
->offset
+ 1; i
<= byte_index
; i
++)
354 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
357 s
= edit
->syntax_marker
->next
;
358 syntax_free (edit
->syntax_marker
);
359 edit
->syntax_marker
= s
;
362 edit
->last_get_rule
= byte_index
;
366 static void translate_rule_to_color (WEdit
* edit
, struct syntax_rule rule
, int *fg
, int *bg
)
369 k
= edit
->rules
[rule
.context
]->keyword
[rule
.keyword
];
374 extern int use_colors
;
376 void edit_get_syntax_color (WEdit
* edit
, long byte_index
, int *fg
, int *bg
)
378 if (edit
->rules
&& byte_index
< edit
->last_byte
&&
379 option_syntax_highlighting
&& use_colors
) {
380 translate_rule_to_color (edit
, edit_get_rule (edit
, byte_index
), fg
, bg
);
382 *fg
= EDITOR_NORMAL_COLOR
;
388 Returns 0 on error/eof or a count of the number of bytes read
389 including the newline. Result must be free'd.
392 static int mad_read_one_line (char **line
, FILE * f
, char *file
, int line_
)
393 #define read_one_line(a,b) mad_read_one_line(a,b,__FILE__,__LINE__)
395 static int read_one_line (char **line
, FILE * f
)
399 int len
= 256, c
, r
= 0, i
= 0;
401 p
= mad_syntax_malloc (len
, file
, line_
);
403 p
= syntax_malloc (len
);
415 } else if (c
== '\n') {
416 r
= i
+ 1; /* extra 1 for the newline just read */
421 q
= syntax_malloc (len
* 2);
435 static char *convert (char *s
)
496 #define whiteness(x) ((x) == '\t' || (x) == '\n' || (x) == ' ')
498 static void get_args (char *l
, char **args
, int *argc
)
503 while (*p
&& whiteness (*p
))
507 for (l
= p
+ 1; *l
&& !whiteness (*l
); l
++);
518 #define break_a {result=line;break;}
519 #define check_a {if(!*a){result=line;break;}}
520 #define check_not_a {if(*a){result=line;break;}}
522 int try_alloc_color_pair (char *fg
, char *bg
);
524 int this_try_alloc_color_pair (char *fg
, char *bg
)
526 char f
[80], b
[80], *p
;
547 return try_alloc_color_pair (fg
, bg
);
550 static char *error_file_name
= 0;
552 extern char *mc_home
;
554 static FILE *open_include_file (char *filename
)
558 syntax_g_free (error_file_name
);
559 error_file_name
= g_strdup (filename
);
560 if (*filename
== PATH_SEP
)
561 return fopen (filename
, "r");
563 g_free (error_file_name
);
564 error_file_name
= g_strconcat (home_dir
, EDIT_DIR PATH_SEP_STR
,
566 f
= fopen (error_file_name
, "r");
570 g_free (error_file_name
);
571 error_file_name
= g_strconcat (mc_home
, PATH_SEP_STR
"syntax" PATH_SEP_STR
,
573 return fopen (error_file_name
, "r");
576 /* returns line number on error */
577 static int edit_read_syntax_rules (WEdit
* edit
, FILE * f
)
581 char last_fg
[32] = "", last_bg
[32] = "";
582 char whole_right
[512];
583 char whole_left
[512];
584 char *args
[1024], *l
= 0;
585 int save_line
= 0, line
= 0;
586 struct context_rule
**r
, *c
= 0;
587 int num_words
= -1, num_contexts
= -1;
588 int argc
, result
= 0;
593 strcpy (whole_left
, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
594 strcpy (whole_right
, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
596 r
= edit
->rules
= syntax_malloc (MAX_CONTEXTS
* sizeof (struct context_rule
*));
602 if (!read_one_line (&l
, f
)) {
607 line
= save_line
+ 1;
608 syntax_g_free (error_file_name
);
611 if (!read_one_line (&l
, f
))
617 get_args (l
, args
, &argc
);
621 } else if (!strcmp (args
[0], "include")) {
622 if (g
|| argc
!= 2) {
627 f
= open_include_file (args
[1]);
629 syntax_g_free (error_file_name
);
635 } else if (!strcmp (args
[0], "wholechars")) {
637 if (!strcmp (*a
, "left")) {
639 strcpy (whole_left
, *a
);
640 } else if (!strcmp (*a
, "right")) {
642 strcpy (whole_right
, *a
);
644 strcpy (whole_left
, *a
);
645 strcpy (whole_right
, *a
);
649 } else if (!strcmp (args
[0], "context")) {
651 if (num_contexts
== -1) {
652 if (strcmp (*a
, "default")) { /* first context is the default */
656 c
= r
[0] = syntax_malloc (sizeof (struct context_rule
));
657 c
->left
= (char *) strdup (" ");
658 c
->right
= (char *) strdup (" ");
661 c
= r
[num_contexts
] = syntax_malloc (sizeof (struct context_rule
));
662 if (!strcmp (*a
, "exclusive")) {
664 c
->between_delimiters
= 1;
667 if (!strcmp (*a
, "whole")) {
669 c
->whole_word_chars_left
= (char *) strdup (whole_left
);
670 c
->whole_word_chars_right
= (char *) strdup (whole_right
);
671 } else if (!strcmp (*a
, "wholeleft")) {
673 c
->whole_word_chars_left
= (char *) strdup (whole_left
);
674 } else if (!strcmp (*a
, "wholeright")) {
676 c
->whole_word_chars_right
= (char *) strdup (whole_right
);
679 if (!strcmp (*a
, "linestart")) {
681 c
->line_start_left
= 1;
684 c
->left
= (char *) strdup (*a
++);
686 if (!strcmp (*a
, "linestart")) {
688 c
->line_start_right
= 1;
691 c
->right
= (char *) strdup (*a
++);
692 c
->first_left
= *c
->left
;
693 c
->first_right
= *c
->right
;
694 c
->single_char
= (strlen (c
->right
) == 1);
696 c
->keyword
= syntax_malloc (MAX_WORDS_PER_CONTEXT
* sizeof (struct key_word
*));
698 c
->max_words
= MAX_WORDS_PER_CONTEXT
;
701 c
->keyword
[0] = syntax_malloc (sizeof (struct key_word
));
708 strcpy (last_fg
, fg
? fg
: "");
709 strcpy (last_bg
, bg
? bg
: "");
710 c
->keyword
[0]->fg
= this_try_alloc_color_pair (fg
, bg
);
711 c
->keyword
[0]->keyword
= (char *) strdup (" ");
714 } else if (!strcmp (args
[0], "spellcheck")) {
720 } else if (!strcmp (args
[0], "keyword")) {
725 k
= r
[num_contexts
- 1]->keyword
[num_words
] = syntax_malloc (sizeof (struct key_word
));
726 if (!strcmp (*a
, "whole")) {
728 k
->whole_word_chars_left
= (char *) strdup (whole_left
);
729 k
->whole_word_chars_right
= (char *) strdup (whole_right
);
730 } else if (!strcmp (*a
, "wholeleft")) {
732 k
->whole_word_chars_left
= (char *) strdup (whole_left
);
733 } else if (!strcmp (*a
, "wholeright")) {
735 k
->whole_word_chars_right
= (char *) strdup (whole_right
);
738 if (!strcmp (*a
, "linestart")) {
743 if (!strcmp (*a
, "whole")) {
746 k
->keyword
= (char *) strdup (*a
++);
747 k
->first
= *k
->keyword
;
758 k
->fg
= this_try_alloc_color_pair (fg
, bg
);
761 } else if (!strncmp (args
[0], "#", 1)) {
762 /* do nothing for comment */
763 } else if (!strcmp (args
[0], "file")) {
765 } else { /* anything else is an error */
775 syntax_free (edit
->rules
);
780 if (num_contexts
== -1) {
786 char first_chars
[MAX_WORDS_PER_CONTEXT
+ 2], *p
;
787 for (i
= 0; edit
->rules
[i
]; i
++) {
791 for (j
= 1; c
->keyword
[j
]; j
++)
792 *p
++ = c
->keyword
[j
]->first
;
794 c
->keyword_first_chars
= syntax_malloc (strlen (first_chars
) + 2);
795 strcpy (c
->keyword_first_chars
, first_chars
);
802 int edit_check_spelling (WEdit
* edit
)
807 void (*syntax_change_callback
) (CWidget
*) = 0;
809 void edit_set_syntax_change_callback (void (*callback
) (CWidget
*))
811 syntax_change_callback
= callback
;
814 void edit_free_syntax_rules (WEdit
* edit
)
821 edit_get_rule (edit
, -1);
822 syntax_free (edit
->syntax_type
);
823 edit
->syntax_type
= 0;
824 if (syntax_change_callback
)
825 (*syntax_change_callback
) (&edit
->widget
);
826 for (i
= 0; edit
->rules
[i
]; i
++) {
827 if (edit
->rules
[i
]->keyword
) {
828 for (j
= 0; edit
->rules
[i
]->keyword
[j
]; j
++) {
829 syntax_free (edit
->rules
[i
]->keyword
[j
]->keyword
);
830 syntax_free (edit
->rules
[i
]->keyword
[j
]->whole_word_chars_left
);
831 syntax_free (edit
->rules
[i
]->keyword
[j
]->whole_word_chars_right
);
832 syntax_free (edit
->rules
[i
]->keyword
[j
]);
835 syntax_free (edit
->rules
[i
]->left
);
836 syntax_free (edit
->rules
[i
]->right
);
837 syntax_free (edit
->rules
[i
]->whole_word_chars_left
);
838 syntax_free (edit
->rules
[i
]->whole_word_chars_right
);
839 syntax_free (edit
->rules
[i
]->keyword
);
840 syntax_free (edit
->rules
[i
]->keyword_first_chars
);
841 syntax_free (edit
->rules
[i
]);
843 while (edit
->syntax_marker
) {
844 struct _syntax_marker
*s
= edit
->syntax_marker
->next
;
845 syntax_free (edit
->syntax_marker
);
846 edit
->syntax_marker
= s
;
848 syntax_free (edit
->rules
);
851 /* returns -1 on file error, line number on error in file syntax */
852 static int edit_read_syntax_file (WEdit
* edit
, char **names
, char *syntax_file
, char *editor_file
, char *first_line
, char *type
)
856 regmatch_t pmatch
[1];
857 char *args
[1024], *l
= 0;
864 lib_file
= concat_dir_and_file (mc_home
, "syntax" PATH_SEP_STR
"Syntax");
865 check_for_default (lib_file
, syntax_file
);
868 f
= fopen (syntax_file
, "r");
875 if (!read_one_line (&l
, f
))
877 get_args (l
, args
, &argc
);
880 /* looking for `file ...' lines only */
881 if (strcmp (args
[0], "file")) {
885 /* must have two args or report error */
886 if (!args
[1] || !args
[2]) {
891 /* 1: just collecting a list of names of rule sets */
892 names
[count
++] = (char *) strdup (args
[2]);
895 /* 2: rule set was explicitly specified by the caller */
896 if (!strcmp (type
, args
[2]))
898 } else if (editor_file
&& edit
) {
899 /* 3: auto-detect rule set from regular expressions */
901 if (regcomp (&r
, args
[1], REG_EXTENDED
)) {
905 /* does filename match arg 1 ? */
906 q
= !regexec (&r
, editor_file
, 1, pmatch
, 0);
909 if (regcomp (&r
, args
[3], REG_EXTENDED
)) {
913 /* does first line match arg 3 ? */
914 q
= !regexec (&r
, first_line
, 1, pmatch
, 0);
920 line_error
= edit_read_syntax_rules (edit
, f
);
922 if (!error_file_name
) /* an included file */
923 result
= line
+ line_error
;
927 syntax_free (edit
->syntax_type
);
928 edit
->syntax_type
= (char *) strdup (args
[2]);
929 /* if there are no rules then turn off syntax highlighting for speed */
931 if (!edit
->rules
[0]->keyword
[1] && !edit
->rules
[0]->spelling
) {
932 edit_free_syntax_rules (edit
);
935 /* notify the callback of a change in rule set */
936 if (syntax_change_callback
)
937 (*syntax_change_callback
) (&edit
->widget
);
950 static char *get_first_editor_line (WEdit
* edit
)
957 for (i
= 0; i
< 255; i
++) {
958 s
[i
] = edit_get_byte (edit
, i
);
968 /* loads rules into edit struct. one of edit or names must be zero. if
969 edit is zero, a list of types will be stored into name. type may be zero
970 in which case the type will be selected according to the filename. */
971 void edit_load_syntax (WEdit
* edit
, char **names
, char *type
)
976 edit_free_syntax_rules (edit
);
978 if (!option_syntax_highlighting
)
984 if (!*edit
->filename
&& !type
)
987 f
= catstrs (home_dir
, SYNTAX_FILE
, 0);
988 r
= edit_read_syntax_file (edit
, names
, f
, edit
? edit
->filename
: 0, get_first_editor_line (edit
), type
);
990 edit_free_syntax_rules (edit
);
991 edit_error_dialog (_ (" Load syntax file "), _ (" File access error "));
995 edit_free_syntax_rules (edit
);
996 message (0, _(" Load syntax file "),
997 _(" Error in file %s on line %d "),
998 error_file_name
? error_file_name
: f
, r
);
999 syntax_g_free (error_file_name
);
1006 int option_syntax_highlighting
= 0;
1008 void edit_load_syntax (WEdit
* edit
, char **names
, char *type
)
1013 void edit_free_syntax_rules (WEdit
* edit
)
1018 void edit_get_syntax_color (WEdit
* edit
, long byte_index
, int *fg
, int *bg
)
1023 int edit_check_spelling (WEdit
* edit
)
1028 #endif /* HAVE_SYNTAXH */