1 /* editor syntax highlighting.
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 Authors: 1998 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include <sys/types.h>
35 #include <mhl/string.h>
37 #include "../src/global.h"
40 #include "edit-widget.h"
41 #include "../src/color.h" /* use_colors */
42 #include "../src/main.h" /* mc_home */
43 #include "../src/wtools.h" /* message() */
46 #define SYNTAX_MARKER_DENSITY 512
49 Mispelled words are flushed from the syntax highlighting rules
50 when they have been around longer than
51 TRANSIENT_WORD_TIME_OUT seconds. At a cursor rate of 30
52 chars per second and say 3 chars + a space per word, we can
53 accumulate 450 words absolute max with a value of 60. This is
54 below this limit of 1024 words in a context.
56 #define TRANSIENT_WORD_TIME_OUT 60
58 #define UNKNOWN_FORMAT "unknown"
60 #define MAX_WORDS_PER_CONTEXT 1024
61 #define MAX_CONTEXTS 128
63 #define RULE_ON_LEFT_BORDER 1
64 #define RULE_ON_RIGHT_BORDER 2
66 #define SYNTAX_TOKEN_STAR '\001'
67 #define SYNTAX_TOKEN_PLUS '\002'
68 #define SYNTAX_TOKEN_BRACKET '\003'
69 #define SYNTAX_TOKEN_BRACE '\004'
74 char *whole_word_chars_left
;
75 char *whole_word_chars_right
;
82 unsigned char first_left
;
84 unsigned char first_right
;
86 char line_start_right
;
87 int between_delimiters
;
88 char *whole_word_chars_left
;
89 char *whole_word_chars_right
;
90 char *keyword_first_chars
;
92 /* first word is word[1] */
93 struct key_word
**keyword
;
96 struct _syntax_marker
{
98 struct syntax_rule rule
;
99 struct _syntax_marker
*next
;
102 int option_syntax_highlighting
= 1;
103 int option_auto_syntax
= 1;
104 char *option_syntax_type
= NULL
;
106 #define syntax_g_free(x) do {g_free(x); (x)=0;} while (0)
109 mc_defines_destroy (gpointer key
, gpointer value
, gpointer data
)
111 char **values
= value
;
123 /* Completely destroys the defines tree */
125 destroy_defines (GTree
**defines
)
127 g_tree_traverse (*defines
, mc_defines_destroy
, G_POST_ORDER
, NULL
);
128 g_tree_destroy (*defines
);
133 subst_defines (GTree
*defines
, char **argv
, char **argv_end
)
138 while (*argv
&& argv
< argv_end
) {
139 if ((t
= g_tree_lookup (defines
, *argv
))) {
142 /* Count argv array members */
144 for (p
= &argv
[1]; *p
; p
++)
147 /* Count members of definition array */
150 p
= &argv
[count
+ argc
];
152 /* Buffer overflow or infinitive loop in define */
156 /* Move rest of argv after definition members */
158 *p
-- = argv
[argc
-- + 1];
160 /* Copy definition members to argv */
161 for (p
= argv
; *t
; *p
++ = *t
++);
168 compare_word_to_right (WEdit
*edit
, long i
, const char *text
,
169 const char *whole_left
, const char *whole_right
,
172 const unsigned char *p
, *q
;
177 c
= edit_get_byte (edit
, i
- 1);
182 if (strchr (whole_left
, c
))
185 for (p
= (unsigned char *) text
, q
= p
+ strlen ((char *) p
); p
< q
; p
++, i
++) {
187 case SYNTAX_TOKEN_STAR
:
191 c
= edit_get_byte (edit
, i
);
194 if (!strchr (whole_right
, c
))
203 case SYNTAX_TOKEN_PLUS
:
208 c
= edit_get_byte (edit
, i
);
211 if (*p
== *text
&& !p
[1]) /* handle eg '+' and @+@ keywords properly */
214 if (j
&& strchr ((char *) p
+ 1, c
)) /* c exists further down, so it will get matched later */
216 if (c
== '\n' || c
== '\t' || c
== ' ') {
227 if (!strchr (whole_right
, c
)) {
240 case SYNTAX_TOKEN_BRACKET
:
246 c
= edit_get_byte (edit
, i
);
247 for (j
= 0; p
[j
] != SYNTAX_TOKEN_BRACKET
&& p
[j
]; j
++)
252 ; /* dummy command */
255 while (*p
!= SYNTAX_TOKEN_BRACKET
&& p
<= q
)
262 case SYNTAX_TOKEN_BRACE
:
265 c
= edit_get_byte (edit
, i
);
266 for (; *p
!= SYNTAX_TOKEN_BRACE
&& *p
; p
++)
271 while (*p
!= SYNTAX_TOKEN_BRACE
&& p
< q
)
275 if (*p
!= edit_get_byte (edit
, i
))
280 if (strchr (whole_right
, edit_get_byte (edit
, i
)))
285 static inline const char *xx_strchr (const unsigned char *s
, int c
)
287 while (*s
>= '\005' && *s
!= (unsigned char) c
) {
290 return (const char *) s
;
293 static inline struct syntax_rule
apply_rules_going_right (WEdit
* edit
, long i
, struct syntax_rule rule
)
295 struct context_rule
*r
;
296 int contextchanged
= 0, c
;
297 int found_right
= 0, found_left
= 0, keyword_foundleft
= 0, keyword_foundright
= 0;
300 struct syntax_rule _rule
= rule
;
302 if (!(c
= edit_get_byte (edit
, i
)))
304 is_end
= (rule
.end
== (unsigned char) i
);
306 /* check to turn off a keyword */
308 if (edit_get_byte (edit
, i
- 1) == '\n')
312 keyword_foundleft
= 1;
316 /* check to turn off a context */
317 if (_rule
.context
&& !_rule
.keyword
) {
319 r
= edit
->rules
[_rule
.context
];
320 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) {
323 _rule
.border
= RULE_ON_RIGHT_BORDER
;
324 if (r
->between_delimiters
)
326 } else if (is_end
&& rule
.border
& RULE_ON_RIGHT_BORDER
) {
328 /* always turn off a context at 4 */
331 if (!keyword_foundleft
)
333 } else if (is_end
&& rule
.border
& RULE_ON_LEFT_BORDER
) {
335 /* never turn off a context at 2 */
341 /* check to turn on a keyword */
342 if (!_rule
.keyword
) {
345 p
= (r
= edit
->rules
[_rule
.context
])->keyword_first_chars
;
347 while (*(p
= xx_strchr ((unsigned char *) p
+ 1, c
))) {
352 count
= p
- r
->keyword_first_chars
;
353 k
= r
->keyword
[count
];
354 e
= compare_word_to_right (edit
, i
, k
->keyword
, k
->whole_word_chars_left
, k
->whole_word_chars_right
, k
->line_start
);
358 _rule
.keyword
= count
;
359 keyword_foundright
= 1;
364 /* check to turn on a context */
365 if (!_rule
.context
) {
366 if (!found_left
&& is_end
) {
367 if (rule
.border
& RULE_ON_RIGHT_BORDER
) {
373 } else if (rule
.border
& RULE_ON_LEFT_BORDER
) {
374 r
= edit
->rules
[_rule
._context
];
376 if (r
->between_delimiters
) {
378 _rule
.context
= _rule
._context
;
381 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
) {
384 _rule
.border
= RULE_ON_RIGHT_BORDER
;
393 struct context_rule
**rules
= edit
->rules
;
395 for (count
= 1; rules
[count
]; count
++) {
397 if (r
->first_left
== c
) {
400 e
= compare_word_to_right (edit
, i
, r
->left
, r
->whole_word_chars_left
, r
->whole_word_chars_right
, r
->line_start_left
);
401 if (e
>= end
&& (!_rule
.keyword
|| keyword_foundright
)) {
404 _rule
.border
= RULE_ON_LEFT_BORDER
;
405 _rule
._context
= count
;
406 if (!r
->between_delimiters
)
407 if (!_rule
.keyword
) {
408 _rule
.context
= count
;
418 /* check again to turn on a keyword if the context switched */
419 if (contextchanged
&& !_rule
.keyword
) {
422 p
= (r
= edit
->rules
[_rule
.context
])->keyword_first_chars
;
423 while (*(p
= xx_strchr ((unsigned char *) p
+ 1, c
))) {
428 count
= p
- r
->keyword_first_chars
;
429 k
= r
->keyword
[count
];
430 e
= compare_word_to_right (edit
, i
, k
->keyword
, k
->whole_word_chars_left
, k
->whole_word_chars_right
, k
->line_start
);
433 _rule
.keyword
= count
;
441 static struct syntax_rule
edit_get_rule (WEdit
* edit
, long byte_index
)
445 if (byte_index
> edit
->last_get_rule
) {
446 for (i
= edit
->last_get_rule
+ 1; i
<= byte_index
; i
++) {
447 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
448 if (i
> (edit
->syntax_marker
? edit
->syntax_marker
->offset
+ SYNTAX_MARKER_DENSITY
: SYNTAX_MARKER_DENSITY
)) {
449 struct _syntax_marker
*s
;
451 s
= edit
->syntax_marker
;
452 edit
->syntax_marker
= g_malloc0 (sizeof (struct _syntax_marker
));
453 edit
->syntax_marker
->next
= s
;
454 edit
->syntax_marker
->offset
= i
;
455 edit
->syntax_marker
->rule
= edit
->rule
;
458 } else if (byte_index
< edit
->last_get_rule
) {
459 struct _syntax_marker
*s
;
462 if (!edit
->syntax_marker
) {
463 memset (&edit
->rule
, 0, sizeof (edit
->rule
));
464 for (i
= -1; i
<= byte_index
; i
++)
465 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
468 if (byte_index
>= edit
->syntax_marker
->offset
) {
469 edit
->rule
= edit
->syntax_marker
->rule
;
470 for (i
= edit
->syntax_marker
->offset
+ 1; i
<= byte_index
; i
++)
471 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
474 s
= edit
->syntax_marker
->next
;
475 syntax_g_free (edit
->syntax_marker
);
476 edit
->syntax_marker
= s
;
479 edit
->last_get_rule
= byte_index
;
483 static void translate_rule_to_color (WEdit
* edit
, struct syntax_rule rule
, int *color
)
487 k
= edit
->rules
[rule
.context
]->keyword
[rule
.keyword
];
491 void edit_get_syntax_color (WEdit
* edit
, long byte_index
, int *color
)
493 if (edit
->rules
&& byte_index
< edit
->last_byte
&&
494 option_syntax_highlighting
&& use_colors
) {
495 translate_rule_to_color (edit
, edit_get_rule (edit
, byte_index
), color
);
497 *color
= use_colors
? EDITOR_NORMAL_COLOR_INDEX
: 0;
503 Returns 0 on error/eof or a count of the number of bytes read
504 including the newline. Result must be free'd.
505 In case of an error, *line will not be modified.
507 static int read_one_line (char **line
, FILE * f
)
509 GString
*p
= g_string_new ("");
523 /* handle all of \r\n, \r, \n correctly. */
525 if ( (c
= fgetc (f
)) == '\n')
534 g_string_append_c (p
, c
);
538 g_string_free (p
, FALSE
);
540 g_string_free (p
, TRUE
);
545 static char *convert (char *s
)
579 *p
= SYNTAX_TOKEN_BRACKET
;
583 *p
= SYNTAX_TOKEN_BRACE
;
594 *p
= SYNTAX_TOKEN_STAR
;
597 *p
= SYNTAX_TOKEN_PLUS
;
610 #define whiteness(x) ((x) == '\t' || (x) == '\n' || (x) == ' ')
612 static int get_args (char *l
, char **args
, int args_size
)
616 while (argc
< args_size
) {
618 while (*p
&& whiteness (*p
))
622 for (l
= p
+ 1; *l
&& !whiteness (*l
); l
++);
625 args
[argc
++] = convert (p
);
627 args
[argc
] = (char *) NULL
;
632 #define break_a {result=line;break;}
633 #define check_a {if(!*a){result=line;break;}}
634 #define check_not_a {if(*a){result=line;break;}}
637 this_try_alloc_color_pair (const char *fg
, const char *bg
)
639 char f
[80], b
[80], *p
;
648 g_strlcpy (f
, fg
, sizeof (f
));
655 g_strlcpy (b
, bg
, sizeof (b
));
661 return try_alloc_color_pair (fg
, bg
);
664 static char *error_file_name
= 0;
666 static FILE *open_include_file (const char *filename
)
670 syntax_g_free (error_file_name
);
671 error_file_name
= g_strdup (filename
);
672 if (*filename
== PATH_SEP
)
673 return fopen (filename
, "r");
675 g_free (error_file_name
);
676 error_file_name
= g_strconcat (home_dir
, PATH_SEP_STR EDIT_DIR PATH_SEP_STR
,
677 filename
, (char *) NULL
);
678 f
= fopen (error_file_name
, "r");
682 g_free (error_file_name
);
683 error_file_name
= g_strconcat (mc_home
, PATH_SEP_STR
"syntax" PATH_SEP_STR
,
684 filename
, (char *) NULL
);
685 return fopen (error_file_name
, "r");
688 /* returns line number on error */
690 edit_read_syntax_rules (WEdit
*edit
, FILE *f
, char **args
, int args_size
)
694 char last_fg
[32] = "", last_bg
[32] = "";
695 char whole_right
[512];
696 char whole_left
[512];
698 int save_line
= 0, line
= 0;
699 struct context_rule
**r
, *c
= 0;
700 int num_words
= -1, num_contexts
= -1;
704 int alloc_contexts
= MAX_CONTEXTS
,
705 alloc_words_per_context
= MAX_WORDS_PER_CONTEXT
,
706 max_alloc_words_per_context
= MAX_WORDS_PER_CONTEXT
;
710 strcpy (whole_left
, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
711 strcpy (whole_right
, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
713 r
= edit
->rules
= g_malloc0 (alloc_contexts
* sizeof (struct context_rule
*));
716 edit
->defines
= g_tree_new ((GCompareFunc
) strcmp
);
723 if (!read_one_line (&l
, f
)) {
728 line
= save_line
+ 1;
729 syntax_g_free (error_file_name
);
731 if (!read_one_line (&l
, f
))
737 argc
= get_args (l
, args
, args_size
);
741 } else if (!strcmp (args
[0], "include")) {
742 if (g
|| argc
!= 2) {
747 f
= open_include_file (args
[1]);
749 syntax_g_free (error_file_name
);
755 } else if (!strcmp (args
[0], "wholechars")) {
757 if (!strcmp (*a
, "left")) {
759 g_strlcpy (whole_left
, *a
, sizeof (whole_left
));
760 } else if (!strcmp (*a
, "right")) {
762 g_strlcpy (whole_right
, *a
, sizeof (whole_right
));
764 g_strlcpy (whole_left
, *a
, sizeof (whole_left
));
765 g_strlcpy (whole_right
, *a
, sizeof (whole_right
));
769 } else if (!strcmp (args
[0], "context")) {
771 if (num_contexts
== -1) {
772 if (strcmp (*a
, "default")) { /* first context is the default */
776 c
= r
[0] = g_malloc0 (sizeof (struct context_rule
));
777 c
->left
= g_strdup (" ");
778 c
->right
= g_strdup (" ");
781 /* Terminate previous context. */
782 r
[num_contexts
- 1]->keyword
[num_words
] = NULL
;
783 c
= r
[num_contexts
] = g_malloc0 (sizeof (struct context_rule
));
784 if (!strcmp (*a
, "exclusive")) {
786 c
->between_delimiters
= 1;
789 if (!strcmp (*a
, "whole")) {
791 c
->whole_word_chars_left
= g_strdup (whole_left
);
792 c
->whole_word_chars_right
= g_strdup (whole_right
);
793 } else if (!strcmp (*a
, "wholeleft")) {
795 c
->whole_word_chars_left
= g_strdup (whole_left
);
796 } else if (!strcmp (*a
, "wholeright")) {
798 c
->whole_word_chars_right
= g_strdup (whole_right
);
801 if (!strcmp (*a
, "linestart")) {
803 c
->line_start_left
= 1;
806 c
->left
= g_strdup (*a
++);
808 if (!strcmp (*a
, "linestart")) {
810 c
->line_start_right
= 1;
813 c
->right
= g_strdup (*a
++);
814 c
->first_left
= *c
->left
;
815 c
->first_right
= *c
->right
;
817 c
->keyword
= g_malloc (alloc_words_per_context
* sizeof (struct key_word
*));
819 c
->keyword
[0] = g_malloc0 (sizeof (struct key_word
));
820 subst_defines (edit
->defines
, a
, &args
[1024]);
827 g_strlcpy (last_fg
, fg
? fg
: "", sizeof (last_fg
));
828 g_strlcpy (last_bg
, bg
? bg
: "", sizeof (last_bg
));
829 c
->keyword
[0]->color
= this_try_alloc_color_pair (fg
, bg
);
830 c
->keyword
[0]->keyword
= g_strdup (" ");
833 alloc_words_per_context
= MAX_WORDS_PER_CONTEXT
;
834 if (++num_contexts
>= alloc_contexts
) {
835 struct context_rule
**tmp
;
837 alloc_contexts
+= 128;
838 tmp
= g_realloc (r
, alloc_contexts
* sizeof (struct context_rule
*));
841 } else if (!strcmp (args
[0], "spellcheck")) {
847 } else if (!strcmp (args
[0], "keyword")) {
853 k
= r
[num_contexts
- 1]->keyword
[num_words
] = g_malloc0 (sizeof (struct key_word
));
854 if (!strcmp (*a
, "whole")) {
856 k
->whole_word_chars_left
= g_strdup (whole_left
);
857 k
->whole_word_chars_right
= g_strdup (whole_right
);
858 } else if (!strcmp (*a
, "wholeleft")) {
860 k
->whole_word_chars_left
= g_strdup (whole_left
);
861 } else if (!strcmp (*a
, "wholeright")) {
863 k
->whole_word_chars_right
= g_strdup (whole_right
);
866 if (!strcmp (*a
, "linestart")) {
871 if (!strcmp (*a
, "whole")) {
874 k
->keyword
= g_strdup (*a
++);
875 k
->first
= *k
->keyword
;
876 subst_defines (edit
->defines
, a
, &args
[1024]);
887 k
->color
= this_try_alloc_color_pair (fg
, bg
);
890 if (++num_words
>= alloc_words_per_context
) {
891 struct key_word
**tmp
;
893 alloc_words_per_context
+= 1024;
895 if (alloc_words_per_context
> max_alloc_words_per_context
)
896 max_alloc_words_per_context
= alloc_words_per_context
;
898 tmp
= g_realloc (c
->keyword
, alloc_words_per_context
* sizeof (struct key_word
*));
901 } else if (*(args
[0]) == '#') {
902 /* do nothing for comment */
903 } else if (!strcmp (args
[0], "file")) {
905 } else if (!strcmp (args
[0], "define")) {
911 if ((argv
= g_tree_lookup (edit
->defines
, key
))) {
912 mc_defines_destroy (NULL
, argv
, NULL
);
914 key
= g_strdup (key
);
916 argv
= g_new (char *, argc
- 1);
917 g_tree_insert (edit
->defines
, key
, argv
);
919 *argv
++ = g_strdup (*a
++);
922 } else { /* anything else is an error */
931 /* Terminate context array. */
932 if (num_contexts
> 0) {
933 r
[num_contexts
- 1]->keyword
[num_words
] = NULL
;
934 r
[num_contexts
] = NULL
;
938 syntax_g_free (edit
->rules
);
943 if (num_contexts
== -1) {
948 char *first_chars
, *p
;
950 first_chars
= g_malloc (max_alloc_words_per_context
+ 2);
952 for (i
= 0; edit
->rules
[i
]; i
++) {
956 for (j
= 1; c
->keyword
[j
]; j
++)
957 *p
++ = c
->keyword
[j
]->first
;
959 c
->keyword_first_chars
= g_strdup (first_chars
);
962 g_free (first_chars
);
968 void edit_free_syntax_rules (WEdit
* edit
)
975 destroy_defines (&edit
->defines
);
979 edit_get_rule (edit
, -1);
980 syntax_g_free (edit
->syntax_type
);
981 edit
->syntax_type
= 0;
983 for (i
= 0; edit
->rules
[i
]; i
++) {
984 if (edit
->rules
[i
]->keyword
) {
985 for (j
= 0; edit
->rules
[i
]->keyword
[j
]; j
++) {
986 syntax_g_free (edit
->rules
[i
]->keyword
[j
]->keyword
);
987 syntax_g_free (edit
->rules
[i
]->keyword
[j
]->whole_word_chars_left
);
988 syntax_g_free (edit
->rules
[i
]->keyword
[j
]->whole_word_chars_right
);
989 syntax_g_free (edit
->rules
[i
]->keyword
[j
]);
992 syntax_g_free (edit
->rules
[i
]->left
);
993 syntax_g_free (edit
->rules
[i
]->right
);
994 syntax_g_free (edit
->rules
[i
]->whole_word_chars_left
);
995 syntax_g_free (edit
->rules
[i
]->whole_word_chars_right
);
996 syntax_g_free (edit
->rules
[i
]->keyword
);
997 syntax_g_free (edit
->rules
[i
]->keyword_first_chars
);
998 syntax_g_free (edit
->rules
[i
]);
1001 while (edit
->syntax_marker
) {
1002 struct _syntax_marker
*s
= edit
->syntax_marker
->next
;
1003 syntax_g_free (edit
->syntax_marker
);
1004 edit
->syntax_marker
= s
;
1007 syntax_g_free (edit
->rules
);
1010 /* returns -1 on file error, line number on error in file syntax */
1012 edit_read_syntax_file (WEdit
* edit
, char ***pnames
, const char *syntax_file
,
1013 const char *editor_file
, const char *first_line
,
1019 regmatch_t pmatch
[1];
1020 char *args
[1024], *l
= 0;
1026 char **tmpnames
= NULL
;
1028 f
= fopen (syntax_file
, "r");
1030 lib_file
= mhl_str_dir_plus_file (mc_home
, "syntax" PATH_SEP_STR
"Syntax");
1031 f
= fopen (lib_file
, "r");
1041 if (!read_one_line (&l
, f
))
1043 (void)get_args (l
, args
, 1023); /* Final NULL */
1047 /* Looking for `include ...` lines before first `file ...` ones */
1048 if (!found
&& !strcmp (args
[0], "include")) {
1051 if (!args
[1] || !(g
= open_include_file (args
[1]))) {
1058 /* looking for `file ...' lines only */
1059 if (strcmp (args
[0], "file")) {
1064 /* must have two args or report error */
1065 if (!args
[1] || !args
[2]) {
1069 if (pnames
&& *pnames
) {
1071 /* 1: just collecting a list of names of rule sets */
1072 /* Reallocate the list if required */
1073 if (count
% NENTRIES
== 0) {
1074 if ((tmpnames
= (char**) g_realloc (*pnames
, (count
+ NENTRIES
1075 + 1) * sizeof (char*))) != NULL
)
1080 (*pnames
)[count
++] = g_strdup (args
[2]);
1081 (*pnames
)[count
] = NULL
;
1084 /* 2: rule set was explicitly specified by the caller */
1085 if (!strcmp (type
, args
[2]))
1087 } else if (editor_file
&& edit
) {
1089 /* 3: auto-detect rule set from regular expressions */
1091 if (regcomp (&r
, args
[1], REG_EXTENDED
)) {
1096 /* does filename match arg 1 ? */
1097 q
= !regexec (&r
, editor_file
, 1, pmatch
, 0);
1099 if (!q
&& args
[3]) {
1100 if (regcomp (&r
, args
[3], REG_EXTENDED
)) {
1105 /* does first line match arg 3 ? */
1106 q
= !regexec (&r
, first_line
, 1, pmatch
, 0);
1113 syntax_type
= args
[2];
1114 line_error
= edit_read_syntax_rules (edit
, g
? g
: f
, args
, 1023);
1116 if (!error_file_name
) /* an included file */
1117 result
= line
+ line_error
;
1119 result
= line_error
;
1121 syntax_g_free (edit
->syntax_type
);
1122 edit
->syntax_type
= g_strdup (syntax_type
);
1124 /* if there are no rules then turn off syntax highlighting for speed */
1125 if (!g
&& !edit
->rules
[1])
1126 if (!edit
->rules
[0]->keyword
[1] && !edit
->rules
[0]->spelling
) {
1127 edit_free_syntax_rules (edit
);
1145 static char *get_first_editor_line (WEdit
* edit
)
1153 for (i
= 0; i
< 255; i
++) {
1154 s
[i
] = edit_get_byte (edit
, i
);
1165 * Load rules into edit struct. Either edit or *pnames must be NULL. If
1166 * edit is NULL, a list of types will be stored into names. If type is
1167 * NULL, then the type will be selected according to the filename.
1170 edit_load_syntax (WEdit
*edit
, char ***pnames
, const char *type
)
1175 if (option_auto_syntax
)
1178 edit_free_syntax_rules (edit
);
1183 if (!option_syntax_highlighting
&& (!pnames
|| !*pnames
))
1187 if (!edit
->filename
)
1189 if (!*edit
->filename
&& !type
)
1192 f
= mhl_str_dir_plus_file (home_dir
, SYNTAX_FILE
);
1193 r
= edit_read_syntax_file (edit
, pnames
, f
, edit
? edit
->filename
: 0,
1194 get_first_editor_line (edit
), type
);
1196 edit_free_syntax_rules (edit
);
1197 message (D_ERROR
, _(" Load syntax file "),
1198 _(" Cannot open file %s \n %s "), f
,
1199 unix_error_string (errno
));
1201 edit_free_syntax_rules (edit
);
1202 message (D_ERROR
, _(" Load syntax file "),
1203 _(" Error in file %s on line %d "),
1204 error_file_name
? error_file_name
: f
, r
);
1205 syntax_g_free (error_file_name
);