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/memory.h>
36 #include <mhl/string.h>
38 #include "../src/global.h"
41 #include "edit-widget.h"
42 #include "../src/color.h" /* use_colors */
43 #include "../src/main.h" /* mc_home */
44 #include "../src/wtools.h" /* message() */
47 #define SYNTAX_MARKER_DENSITY 512
50 Mispelled words are flushed from the syntax highlighting rules
51 when they have been around longer than
52 TRANSIENT_WORD_TIME_OUT seconds. At a cursor rate of 30
53 chars per second and say 3 chars + a space per word, we can
54 accumulate 450 words absolute max with a value of 60. This is
55 below this limit of 1024 words in a context.
57 #define TRANSIENT_WORD_TIME_OUT 60
59 #define UNKNOWN_FORMAT "unknown"
61 #define MAX_WORDS_PER_CONTEXT 1024
62 #define MAX_CONTEXTS 128
64 #define RULE_ON_LEFT_BORDER 1
65 #define RULE_ON_RIGHT_BORDER 2
67 #define SYNTAX_TOKEN_STAR '\001'
68 #define SYNTAX_TOKEN_PLUS '\002'
69 #define SYNTAX_TOKEN_BRACKET '\003'
70 #define SYNTAX_TOKEN_BRACE '\004'
75 char *whole_word_chars_left
;
76 char *whole_word_chars_right
;
83 unsigned char first_left
;
85 unsigned char first_right
;
87 char line_start_right
;
88 int between_delimiters
;
89 char *whole_word_chars_left
;
90 char *whole_word_chars_right
;
91 char *keyword_first_chars
;
93 /* first word is word[1] */
94 struct key_word
**keyword
;
97 struct _syntax_marker
{
99 struct syntax_rule rule
;
100 struct _syntax_marker
*next
;
103 int option_syntax_highlighting
= 1;
104 int option_auto_syntax
= 1;
105 char *option_syntax_type
= NULL
;
108 mc_defines_destroy (gpointer key
, gpointer value
, gpointer data
)
110 char **values
= value
;
116 mhl_mem_free (*values
++);
117 mhl_mem_free (value
);
122 /* Completely destroys the defines tree */
124 destroy_defines (GTree
**defines
)
126 g_tree_traverse (*defines
, mc_defines_destroy
, G_POST_ORDER
, NULL
);
127 g_tree_destroy (*defines
);
132 subst_defines (GTree
*defines
, char **argv
, char **argv_end
)
137 while (*argv
&& argv
< argv_end
) {
138 if ((t
= g_tree_lookup (defines
, *argv
))) {
141 /* Count argv array members */
143 for (p
= &argv
[1]; *p
; p
++)
146 /* Count members of definition array */
149 p
= &argv
[count
+ argc
];
151 /* Buffer overflow or infinitive loop in define */
155 /* Move rest of argv after definition members */
157 *p
-- = argv
[argc
-- + 1];
159 /* Copy definition members to argv */
160 for (p
= argv
; *t
; *p
++ = *t
++);
167 compare_word_to_right (WEdit
*edit
, long i
, const char *text
,
168 const char *whole_left
, const char *whole_right
,
171 const unsigned char *p
, *q
;
176 c
= edit_get_byte (edit
, i
- 1);
181 if (strchr (whole_left
, c
))
184 for (p
= (unsigned char *) text
, q
= p
+ strlen ((char *) p
); p
< q
; p
++, i
++) {
186 case SYNTAX_TOKEN_STAR
:
190 c
= edit_get_byte (edit
, i
);
193 if (!strchr (whole_right
, c
))
202 case SYNTAX_TOKEN_PLUS
:
207 c
= edit_get_byte (edit
, i
);
210 if (*p
== *text
&& !p
[1]) /* handle eg '+' and @+@ keywords properly */
213 if (j
&& strchr ((char *) p
+ 1, c
)) /* c exists further down, so it will get matched later */
215 if (c
== '\n' || c
== '\t' || c
== ' ') {
226 if (!strchr (whole_right
, c
)) {
239 case SYNTAX_TOKEN_BRACKET
:
245 c
= edit_get_byte (edit
, i
);
246 for (j
= 0; p
[j
] != SYNTAX_TOKEN_BRACKET
&& p
[j
]; j
++)
251 ; /* dummy command */
254 while (*p
!= SYNTAX_TOKEN_BRACKET
&& p
<= q
)
261 case SYNTAX_TOKEN_BRACE
:
264 c
= edit_get_byte (edit
, i
);
265 for (; *p
!= SYNTAX_TOKEN_BRACE
&& *p
; p
++)
270 while (*p
!= SYNTAX_TOKEN_BRACE
&& p
< q
)
274 if (*p
!= edit_get_byte (edit
, i
))
279 if (strchr (whole_right
, edit_get_byte (edit
, i
)))
284 static inline const char *xx_strchr (const unsigned char *s
, int c
)
286 while (*s
>= '\005' && *s
!= (unsigned char) c
) {
289 return (const char *) s
;
292 static inline struct syntax_rule
apply_rules_going_right (WEdit
* edit
, long i
, struct syntax_rule rule
)
294 struct context_rule
*r
;
295 int contextchanged
= 0, c
;
296 int found_right
= 0, found_left
= 0, keyword_foundleft
= 0, keyword_foundright
= 0;
299 struct syntax_rule _rule
= rule
;
301 if (!(c
= edit_get_byte (edit
, i
)))
303 is_end
= (rule
.end
== (unsigned char) i
);
305 /* check to turn off a keyword */
307 if (edit_get_byte (edit
, i
- 1) == '\n')
311 keyword_foundleft
= 1;
315 /* check to turn off a context */
316 if (_rule
.context
&& !_rule
.keyword
) {
318 r
= edit
->rules
[_rule
.context
];
319 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) {
322 _rule
.border
= RULE_ON_RIGHT_BORDER
;
323 if (r
->between_delimiters
)
325 } else if (is_end
&& rule
.border
& RULE_ON_RIGHT_BORDER
) {
327 /* always turn off a context at 4 */
330 if (!keyword_foundleft
)
332 } else if (is_end
&& rule
.border
& RULE_ON_LEFT_BORDER
) {
334 /* never turn off a context at 2 */
340 /* check to turn on a keyword */
341 if (!_rule
.keyword
) {
344 p
= (r
= edit
->rules
[_rule
.context
])->keyword_first_chars
;
346 while (*(p
= xx_strchr ((unsigned char *) p
+ 1, c
))) {
351 count
= p
- r
->keyword_first_chars
;
352 k
= r
->keyword
[count
];
353 e
= compare_word_to_right (edit
, i
, k
->keyword
, k
->whole_word_chars_left
, k
->whole_word_chars_right
, k
->line_start
);
357 _rule
.keyword
= count
;
358 keyword_foundright
= 1;
363 /* check to turn on a context */
364 if (!_rule
.context
) {
365 if (!found_left
&& is_end
) {
366 if (rule
.border
& RULE_ON_RIGHT_BORDER
) {
372 } else if (rule
.border
& RULE_ON_LEFT_BORDER
) {
373 r
= edit
->rules
[_rule
._context
];
375 if (r
->between_delimiters
) {
377 _rule
.context
= _rule
._context
;
380 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
) {
383 _rule
.border
= RULE_ON_RIGHT_BORDER
;
392 struct context_rule
**rules
= edit
->rules
;
394 for (count
= 1; rules
[count
]; count
++) {
396 if (r
->first_left
== c
) {
399 e
= compare_word_to_right (edit
, i
, r
->left
, r
->whole_word_chars_left
, r
->whole_word_chars_right
, r
->line_start_left
);
400 if (e
>= end
&& (!_rule
.keyword
|| keyword_foundright
)) {
403 _rule
.border
= RULE_ON_LEFT_BORDER
;
404 _rule
._context
= count
;
405 if (!r
->between_delimiters
)
406 if (!_rule
.keyword
) {
407 _rule
.context
= count
;
417 /* check again to turn on a keyword if the context switched */
418 if (contextchanged
&& !_rule
.keyword
) {
421 p
= (r
= edit
->rules
[_rule
.context
])->keyword_first_chars
;
422 while (*(p
= xx_strchr ((unsigned char *) p
+ 1, c
))) {
427 count
= p
- r
->keyword_first_chars
;
428 k
= r
->keyword
[count
];
429 e
= compare_word_to_right (edit
, i
, k
->keyword
, k
->whole_word_chars_left
, k
->whole_word_chars_right
, k
->line_start
);
432 _rule
.keyword
= count
;
440 static struct syntax_rule
edit_get_rule (WEdit
* edit
, long byte_index
)
444 if (byte_index
> edit
->last_get_rule
) {
445 for (i
= edit
->last_get_rule
+ 1; i
<= byte_index
; i
++) {
446 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
447 if (i
> (edit
->syntax_marker
? edit
->syntax_marker
->offset
+ SYNTAX_MARKER_DENSITY
: SYNTAX_MARKER_DENSITY
)) {
448 struct _syntax_marker
*s
;
450 s
= edit
->syntax_marker
;
451 edit
->syntax_marker
= g_malloc0 (sizeof (struct _syntax_marker
));
452 edit
->syntax_marker
->next
= s
;
453 edit
->syntax_marker
->offset
= i
;
454 edit
->syntax_marker
->rule
= edit
->rule
;
457 } else if (byte_index
< edit
->last_get_rule
) {
458 struct _syntax_marker
*s
;
461 if (!edit
->syntax_marker
) {
462 memset (&edit
->rule
, 0, sizeof (edit
->rule
));
463 for (i
= -1; i
<= byte_index
; i
++)
464 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
467 if (byte_index
>= edit
->syntax_marker
->offset
) {
468 edit
->rule
= edit
->syntax_marker
->rule
;
469 for (i
= edit
->syntax_marker
->offset
+ 1; i
<= byte_index
; i
++)
470 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
473 s
= edit
->syntax_marker
->next
;
474 MHL_PTR_FREE (edit
->syntax_marker
);
475 edit
->syntax_marker
= s
;
478 edit
->last_get_rule
= byte_index
;
482 static void translate_rule_to_color (WEdit
* edit
, struct syntax_rule rule
, int *color
)
486 k
= edit
->rules
[rule
.context
]->keyword
[rule
.keyword
];
490 void edit_get_syntax_color (WEdit
* edit
, long byte_index
, int *color
)
492 if (edit
->rules
&& byte_index
< edit
->last_byte
&&
493 option_syntax_highlighting
&& use_colors
) {
494 translate_rule_to_color (edit
, edit_get_rule (edit
, byte_index
), color
);
496 *color
= use_colors
? EDITOR_NORMAL_COLOR_INDEX
: 0;
502 Returns 0 on error/eof or a count of the number of bytes read
503 including the newline. Result must be free'd.
504 In case of an error, *line will not be modified.
506 static int read_one_line (char **line
, FILE * f
)
508 GString
*p
= g_string_new ("");
522 /* handle all of \r\n, \r, \n correctly. */
524 if ( (c
= fgetc (f
)) == '\n')
533 g_string_append_c (p
, c
);
537 g_string_free (p
, FALSE
);
539 g_string_free (p
, TRUE
);
544 static char *convert (char *s
)
578 *p
= SYNTAX_TOKEN_BRACKET
;
582 *p
= SYNTAX_TOKEN_BRACE
;
593 *p
= SYNTAX_TOKEN_STAR
;
596 *p
= SYNTAX_TOKEN_PLUS
;
609 #define whiteness(x) ((x) == '\t' || (x) == '\n' || (x) == ' ')
611 static int get_args (char *l
, char **args
, int args_size
)
615 while (argc
< args_size
) {
617 while (*p
&& whiteness (*p
))
621 for (l
= p
+ 1; *l
&& !whiteness (*l
); l
++);
624 args
[argc
++] = convert (p
);
626 args
[argc
] = (char *) NULL
;
631 #define break_a {result=line;break;}
632 #define check_a {if(!*a){result=line;break;}}
633 #define check_not_a {if(*a){result=line;break;}}
636 this_try_alloc_color_pair (const char *fg
, const char *bg
)
638 char f
[80], b
[80], *p
;
647 g_strlcpy (f
, fg
, sizeof (f
));
654 g_strlcpy (b
, bg
, sizeof (b
));
660 return try_alloc_color_pair (fg
, bg
);
663 static char *error_file_name
= 0;
665 static FILE *open_include_file (const char *filename
)
669 MHL_PTR_FREE (error_file_name
);
670 error_file_name
= mhl_str_dup (filename
);
671 if (*filename
== PATH_SEP
)
672 return fopen (filename
, "r");
674 mhl_mem_free (error_file_name
);
675 error_file_name
= g_strconcat (home_dir
, PATH_SEP_STR EDIT_DIR PATH_SEP_STR
,
676 filename
, (char *) NULL
);
677 f
= fopen (error_file_name
, "r");
681 mhl_mem_free (error_file_name
);
682 error_file_name
= g_strconcat (mc_home
, PATH_SEP_STR
"syntax" PATH_SEP_STR
,
683 filename
, (char *) NULL
);
684 return fopen (error_file_name
, "r");
687 /* returns line number on error */
689 edit_read_syntax_rules (WEdit
*edit
, FILE *f
, char **args
, int args_size
)
693 char last_fg
[32] = "", last_bg
[32] = "";
694 char whole_right
[512];
695 char whole_left
[512];
697 int save_line
= 0, line
= 0;
698 struct context_rule
**r
, *c
= 0;
699 int num_words
= -1, num_contexts
= -1;
703 int alloc_contexts
= MAX_CONTEXTS
,
704 alloc_words_per_context
= MAX_WORDS_PER_CONTEXT
,
705 max_alloc_words_per_context
= MAX_WORDS_PER_CONTEXT
;
709 strcpy (whole_left
, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
710 strcpy (whole_right
, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
712 r
= edit
->rules
= g_malloc0 (alloc_contexts
* sizeof (struct context_rule
*));
715 edit
->defines
= g_tree_new ((GCompareFunc
) strcmp
);
722 if (!read_one_line (&l
, f
)) {
727 line
= save_line
+ 1;
728 MHL_PTR_FREE (error_file_name
);
730 if (!read_one_line (&l
, f
))
736 argc
= get_args (l
, args
, args_size
);
740 } else if (!strcmp (args
[0], "include")) {
741 if (g
|| argc
!= 2) {
746 f
= open_include_file (args
[1]);
748 MHL_PTR_FREE (error_file_name
);
754 } else if (!strcmp (args
[0], "wholechars")) {
756 if (!strcmp (*a
, "left")) {
758 g_strlcpy (whole_left
, *a
, sizeof (whole_left
));
759 } else if (!strcmp (*a
, "right")) {
761 g_strlcpy (whole_right
, *a
, sizeof (whole_right
));
763 g_strlcpy (whole_left
, *a
, sizeof (whole_left
));
764 g_strlcpy (whole_right
, *a
, sizeof (whole_right
));
768 } else if (!strcmp (args
[0], "context")) {
770 if (num_contexts
== -1) {
771 if (strcmp (*a
, "default")) { /* first context is the default */
775 c
= r
[0] = g_malloc0 (sizeof (struct context_rule
));
776 c
->left
= mhl_str_dup (" ");
777 c
->right
= mhl_str_dup (" ");
780 /* Terminate previous context. */
781 r
[num_contexts
- 1]->keyword
[num_words
] = NULL
;
782 c
= r
[num_contexts
] = g_malloc0 (sizeof (struct context_rule
));
783 if (!strcmp (*a
, "exclusive")) {
785 c
->between_delimiters
= 1;
788 if (!strcmp (*a
, "whole")) {
790 c
->whole_word_chars_left
= mhl_str_dup (whole_left
);
791 c
->whole_word_chars_right
= mhl_str_dup (whole_right
);
792 } else if (!strcmp (*a
, "wholeleft")) {
794 c
->whole_word_chars_left
= mhl_str_dup (whole_left
);
795 } else if (!strcmp (*a
, "wholeright")) {
797 c
->whole_word_chars_right
= mhl_str_dup (whole_right
);
800 if (!strcmp (*a
, "linestart")) {
802 c
->line_start_left
= 1;
805 c
->left
= mhl_str_dup (*a
++);
807 if (!strcmp (*a
, "linestart")) {
809 c
->line_start_right
= 1;
812 c
->right
= mhl_str_dup (*a
++);
813 c
->first_left
= *c
->left
;
814 c
->first_right
= *c
->right
;
816 c
->keyword
= g_malloc (alloc_words_per_context
* sizeof (struct key_word
*));
818 c
->keyword
[0] = g_malloc0 (sizeof (struct key_word
));
819 subst_defines (edit
->defines
, a
, &args
[1024]);
826 g_strlcpy (last_fg
, fg
? fg
: "", sizeof (last_fg
));
827 g_strlcpy (last_bg
, bg
? bg
: "", sizeof (last_bg
));
828 c
->keyword
[0]->color
= this_try_alloc_color_pair (fg
, bg
);
829 c
->keyword
[0]->keyword
= mhl_str_dup (" ");
832 alloc_words_per_context
= MAX_WORDS_PER_CONTEXT
;
833 if (++num_contexts
>= alloc_contexts
) {
834 struct context_rule
**tmp
;
836 alloc_contexts
+= 128;
837 tmp
= g_realloc (r
, alloc_contexts
* sizeof (struct context_rule
*));
840 } else if (!strcmp (args
[0], "spellcheck")) {
846 } else if (!strcmp (args
[0], "keyword")) {
852 k
= r
[num_contexts
- 1]->keyword
[num_words
] = g_malloc0 (sizeof (struct key_word
));
853 if (!strcmp (*a
, "whole")) {
855 k
->whole_word_chars_left
= mhl_str_dup (whole_left
);
856 k
->whole_word_chars_right
= mhl_str_dup (whole_right
);
857 } else if (!strcmp (*a
, "wholeleft")) {
859 k
->whole_word_chars_left
= mhl_str_dup (whole_left
);
860 } else if (!strcmp (*a
, "wholeright")) {
862 k
->whole_word_chars_right
= mhl_str_dup (whole_right
);
865 if (!strcmp (*a
, "linestart")) {
870 if (!strcmp (*a
, "whole")) {
873 k
->keyword
= mhl_str_dup (*a
++);
874 k
->first
= *k
->keyword
;
875 subst_defines (edit
->defines
, a
, &args
[1024]);
886 k
->color
= this_try_alloc_color_pair (fg
, bg
);
889 if (++num_words
>= alloc_words_per_context
) {
890 struct key_word
**tmp
;
892 alloc_words_per_context
+= 1024;
894 if (alloc_words_per_context
> max_alloc_words_per_context
)
895 max_alloc_words_per_context
= alloc_words_per_context
;
897 tmp
= g_realloc (c
->keyword
, alloc_words_per_context
* sizeof (struct key_word
*));
900 } else if (*(args
[0]) == '#') {
901 /* do nothing for comment */
902 } else if (!strcmp (args
[0], "file")) {
904 } else if (!strcmp (args
[0], "define")) {
910 if ((argv
= g_tree_lookup (edit
->defines
, key
))) {
911 mc_defines_destroy (NULL
, argv
, NULL
);
913 key
= mhl_str_dup (key
);
915 argv
= g_new (char *, argc
- 1);
916 g_tree_insert (edit
->defines
, key
, argv
);
918 *argv
++ = mhl_str_dup (*a
++);
921 } else { /* anything else is an error */
930 /* Terminate context array. */
931 if (num_contexts
> 0) {
932 r
[num_contexts
- 1]->keyword
[num_words
] = NULL
;
933 r
[num_contexts
] = NULL
;
937 MHL_PTR_FREE (edit
->rules
);
942 if (num_contexts
== -1) {
947 char *first_chars
, *p
;
949 first_chars
= g_malloc (max_alloc_words_per_context
+ 2);
951 for (i
= 0; edit
->rules
[i
]; i
++) {
955 for (j
= 1; c
->keyword
[j
]; j
++)
956 *p
++ = c
->keyword
[j
]->first
;
958 c
->keyword_first_chars
= mhl_str_dup (first_chars
);
961 mhl_mem_free (first_chars
);
967 void edit_free_syntax_rules (WEdit
* edit
)
974 destroy_defines (&edit
->defines
);
978 edit_get_rule (edit
, -1);
979 MHL_PTR_FREE (edit
->syntax_type
);
980 edit
->syntax_type
= 0;
982 for (i
= 0; edit
->rules
[i
]; i
++) {
983 if (edit
->rules
[i
]->keyword
) {
984 for (j
= 0; edit
->rules
[i
]->keyword
[j
]; j
++) {
985 MHL_PTR_FREE (edit
->rules
[i
]->keyword
[j
]->keyword
);
986 MHL_PTR_FREE (edit
->rules
[i
]->keyword
[j
]->whole_word_chars_left
);
987 MHL_PTR_FREE (edit
->rules
[i
]->keyword
[j
]->whole_word_chars_right
);
988 MHL_PTR_FREE (edit
->rules
[i
]->keyword
[j
]);
991 MHL_PTR_FREE (edit
->rules
[i
]->left
);
992 MHL_PTR_FREE (edit
->rules
[i
]->right
);
993 MHL_PTR_FREE (edit
->rules
[i
]->whole_word_chars_left
);
994 MHL_PTR_FREE (edit
->rules
[i
]->whole_word_chars_right
);
995 MHL_PTR_FREE (edit
->rules
[i
]->keyword
);
996 MHL_PTR_FREE (edit
->rules
[i
]->keyword_first_chars
);
997 MHL_PTR_FREE (edit
->rules
[i
]);
1000 while (edit
->syntax_marker
) {
1001 struct _syntax_marker
*s
= edit
->syntax_marker
->next
;
1002 MHL_PTR_FREE (edit
->syntax_marker
);
1003 edit
->syntax_marker
= s
;
1006 MHL_PTR_FREE (edit
->rules
);
1009 /* returns -1 on file error, line number on error in file syntax */
1011 edit_read_syntax_file (WEdit
* edit
, char ***pnames
, const char *syntax_file
,
1012 const char *editor_file
, const char *first_line
,
1018 regmatch_t pmatch
[1];
1019 char *args
[1024], *l
= 0;
1025 char **tmpnames
= NULL
;
1027 f
= fopen (syntax_file
, "r");
1029 lib_file
= mhl_str_dir_plus_file (mc_home
, "syntax" PATH_SEP_STR
"Syntax");
1030 f
= fopen (lib_file
, "r");
1031 mhl_mem_free (lib_file
);
1040 if (!read_one_line (&l
, f
))
1042 (void)get_args (l
, args
, 1023); /* Final NULL */
1046 /* Looking for `include ...` lines before first `file ...` ones */
1047 if (!found
&& !strcmp (args
[0], "include")) {
1050 if (!args
[1] || !(g
= open_include_file (args
[1]))) {
1057 /* looking for `file ...' lines only */
1058 if (strcmp (args
[0], "file")) {
1063 /* must have two args or report error */
1064 if (!args
[1] || !args
[2]) {
1068 if (pnames
&& *pnames
) {
1070 /* 1: just collecting a list of names of rule sets */
1071 /* Reallocate the list if required */
1072 if (count
% NENTRIES
== 0) {
1073 if ((tmpnames
= (char**) g_realloc (*pnames
, (count
+ NENTRIES
1074 + 1) * sizeof (char*))) != NULL
)
1079 (*pnames
)[count
++] = mhl_str_dup (args
[2]);
1080 (*pnames
)[count
] = NULL
;
1083 /* 2: rule set was explicitly specified by the caller */
1084 if (!strcmp (type
, args
[2]))
1086 } else if (editor_file
&& edit
) {
1088 /* 3: auto-detect rule set from regular expressions */
1090 if (regcomp (&r
, args
[1], REG_EXTENDED
)) {
1095 /* does filename match arg 1 ? */
1096 q
= !regexec (&r
, editor_file
, 1, pmatch
, 0);
1098 if (!q
&& args
[3]) {
1099 if (regcomp (&r
, args
[3], REG_EXTENDED
)) {
1104 /* does first line match arg 3 ? */
1105 q
= !regexec (&r
, first_line
, 1, pmatch
, 0);
1112 syntax_type
= args
[2];
1113 line_error
= edit_read_syntax_rules (edit
, g
? g
: f
, args
, 1023);
1115 if (!error_file_name
) /* an included file */
1116 result
= line
+ line_error
;
1118 result
= line_error
;
1120 MHL_PTR_FREE (edit
->syntax_type
);
1121 edit
->syntax_type
= mhl_str_dup (syntax_type
);
1122 /* if there are no rules then turn off syntax highlighting for speed */
1123 if (!g
&& !edit
->rules
[1])
1124 if (!edit
->rules
[0]->keyword
[1] && !edit
->rules
[0]->spelling
) {
1125 edit_free_syntax_rules (edit
);
1143 static char *get_first_editor_line (WEdit
* edit
)
1151 for (i
= 0; i
< 255; i
++) {
1152 s
[i
] = edit_get_byte (edit
, i
);
1163 * Load rules into edit struct. Either edit or *pnames must be NULL. If
1164 * edit is NULL, a list of types will be stored into names. If type is
1165 * NULL, then the type will be selected according to the filename.
1168 edit_load_syntax (WEdit
*edit
, char ***pnames
, const char *type
)
1173 if (option_auto_syntax
)
1176 edit_free_syntax_rules (edit
);
1181 if (!option_syntax_highlighting
&& (!pnames
|| !*pnames
))
1185 if (!edit
->filename
)
1187 if (!*edit
->filename
&& !type
)
1190 f
= mhl_str_dir_plus_file (home_dir
, SYNTAX_FILE
);
1191 r
= edit_read_syntax_file (edit
, pnames
, f
, edit
? edit
->filename
: 0,
1192 get_first_editor_line (edit
), type
);
1194 edit_free_syntax_rules (edit
);
1195 message (D_ERROR
, _(" Load syntax file "),
1196 _(" Cannot open file %s \n %s "), f
,
1197 unix_error_string (errno
));
1199 edit_free_syntax_rules (edit
);
1200 message (D_ERROR
, _(" Load syntax file "),
1201 _(" Error in file %s on line %d "),
1202 error_file_name
? error_file_name
: f
, r
);
1203 MHL_PTR_FREE (error_file_name
);