2 Editor syntax highlighting.
4 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
6 The Free Software Foundation, Inc.
10 Egmont Koblinger <egmont@gmail.com>, 2010
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * \brief Source: editor syntax highlighting
32 * \author Mikhail Pobolovets
35 * Mispelled words are flushed from the syntax highlighting rules
36 * when they have been around longer than
37 * TRANSIENT_WORD_TIME_OUT seconds. At a cursor rate of 30
38 * chars per second and say 3 chars + a space per word, we can
39 * accumulate 450 words absolute max with a value of 60. This is
40 * below this limit of 1024 words in a context.
47 #include <sys/types.h>
55 #include "lib/global.h"
56 #include "lib/search.h" /* search engine */
58 #include "lib/strutil.h" /* utf string functions */
60 #include "lib/widget.h" /* message() */
62 #include "edit-impl.h"
63 #include "editwidget.h"
65 /*** global variables ****************************************************************************/
67 int option_syntax_highlighting
= 1;
68 int option_auto_syntax
= 1;
70 /*** file scope macro definitions ****************************************************************/
73 #define SYNTAX_MARKER_DENSITY 512
75 #define TRANSIENT_WORD_TIME_OUT 60
77 #define UNKNOWN_FORMAT "unknown"
79 #define MAX_WORDS_PER_CONTEXT 1024
80 #define MAX_CONTEXTS 128
82 #define RULE_ON_LEFT_BORDER 1
83 #define RULE_ON_RIGHT_BORDER 2
85 #define SYNTAX_TOKEN_STAR '\001'
86 #define SYNTAX_TOKEN_PLUS '\002'
87 #define SYNTAX_TOKEN_BRACKET '\003'
88 #define SYNTAX_TOKEN_BRACE '\004'
90 #define whiteness(x) ((x) == '\t' || (x) == '\n' || (x) == ' ')
93 #define break_a {result=line;break;}
94 #define check_a {if(!*a){result=line;break;}}
95 #define check_not_a {if(*a){result=line;break;}}
97 /*** file scope type declarations ****************************************************************/
103 char *whole_word_chars_left
;
104 char *whole_word_chars_right
;
112 unsigned char first_left
;
114 unsigned char first_right
;
115 char line_start_left
;
116 char line_start_right
;
117 int between_delimiters
;
118 char *whole_word_chars_left
;
119 char *whole_word_chars_right
;
120 char *keyword_first_chars
;
122 /* first word is word[1] */
123 struct key_word
**keyword
;
129 edit_syntax_rule_t rule
;
132 /*** file scope variables ************************************************************************/
134 static char *error_file_name
= NULL
;
136 /*** file scope functions ************************************************************************/
137 /* --------------------------------------------------------------------------------------------- */
140 mc_defines_destroy (gpointer key
, gpointer value
, gpointer data
)
142 char **values
= value
;
154 /* --------------------------------------------------------------------------------------------- */
155 /** Completely destroys the defines tree */
158 destroy_defines (GTree
** defines
)
160 g_tree_foreach (*defines
, mc_defines_destroy
, NULL
);
161 g_tree_destroy (*defines
);
165 /* --------------------------------------------------------------------------------------------- */
167 /** Wrapper for case insensitive mode */
169 xx_tolower (const WEdit
* edit
, int c
)
171 return edit
->is_case_insensitive
? tolower (c
) : c
;
174 /* --------------------------------------------------------------------------------------------- */
177 subst_defines (GTree
* defines
, char **argv
, char **argv_end
)
182 while (*argv
!= NULL
&& argv
< argv_end
)
184 t
= g_tree_lookup (defines
, *argv
);
189 /* Count argv array members */
191 for (p
= &argv
[1]; *p
!= NULL
; p
++)
194 /* Count members of definition array */
195 for (p
= t
; *p
!= NULL
; p
++)
197 p
= &argv
[count
+ argc
];
199 /* Buffer overflow or infinitive loop in define */
203 /* Move rest of argv after definition members */
205 *p
-- = argv
[argc
-- + 1];
207 /* Copy definition members to argv */
208 for (p
= argv
; *t
!= NULL
; *p
++ = *t
++)
215 /* --------------------------------------------------------------------------------------------- */
218 compare_word_to_right (const WEdit
* edit
, off_t i
, const char *text
,
219 const char *whole_left
, const char *whole_right
, long line_start
)
221 const unsigned char *p
, *q
;
227 c
= xx_tolower (edit
, edit_get_byte (edit
, i
- 1));
228 if ((line_start
!= 0 && c
!= '\n') || (whole_left
!= NULL
&& strchr (whole_left
, c
) != NULL
))
231 for (p
= (unsigned char *) text
, q
= p
+ str_term_width1 ((char *) p
); p
< q
; p
++, i
++)
235 case SYNTAX_TOKEN_STAR
:
240 c
= xx_tolower (edit
, edit_get_byte (edit
, i
));
241 if (*p
== '\0' && whole_right
!= NULL
&& strchr (whole_right
, c
) == NULL
)
250 case SYNTAX_TOKEN_PLUS
:
256 c
= xx_tolower (edit
, edit_get_byte (edit
, i
));
260 if (*p
== *text
&& p
[1] == '\0') /* handle eg '+' and @+@ keywords properly */
263 if (j
!= 0 && strchr ((char *) p
+ 1, c
) != NULL
) /* c exists further down, so it will get matched later */
265 if (c
== '\n' || c
== '\t' || c
== ' ' ||
266 (whole_right
!= NULL
&& strchr (whole_right
, c
) == NULL
))
281 case SYNTAX_TOKEN_BRACKET
:
288 c
= xx_tolower (edit
, edit_get_byte (edit
, i
));
289 for (j
= 0; p
[j
] != SYNTAX_TOKEN_BRACKET
&& p
[j
]; j
++)
297 while (*p
!= SYNTAX_TOKEN_BRACKET
&& p
<= q
)
304 case SYNTAX_TOKEN_BRACE
:
307 c
= xx_tolower (edit
, edit_get_byte (edit
, i
));
308 for (; *p
!= SYNTAX_TOKEN_BRACE
&& *p
; p
++)
313 while (*p
!= SYNTAX_TOKEN_BRACE
&& p
< q
)
317 if (*p
!= xx_tolower (edit
, edit_get_byte (edit
, i
)))
321 return (whole_right
!= NULL
&&
322 strchr (whole_right
, xx_tolower (edit
, edit_get_byte (edit
, i
))) != NULL
) ? -1 : i
;
325 /* --------------------------------------------------------------------------------------------- */
328 xx_strchr (const WEdit
* edit
, const unsigned char *s
, int char_byte
)
330 while (*s
>= '\005' && xx_tolower (edit
, *s
) != char_byte
)
333 return (const char *) s
;
336 /* --------------------------------------------------------------------------------------------- */
338 static edit_syntax_rule_t
339 apply_rules_going_right (WEdit
* edit
, off_t i
, edit_syntax_rule_t rule
)
341 struct context_rule
*r
;
343 gboolean contextchanged
= FALSE
;
344 gboolean found_left
= FALSE
, found_right
= FALSE
;
345 gboolean keyword_foundleft
= FALSE
, keyword_foundright
= FALSE
;
348 edit_syntax_rule_t _rule
= rule
;
350 c
= xx_tolower (edit
, edit_get_byte (edit
, i
));
353 is_end
= (rule
.end
== i
);
355 /* check to turn off a keyword */
358 if (edit_get_byte (edit
, i
- 1) == '\n')
363 keyword_foundleft
= TRUE
;
367 /* check to turn off a context */
368 if (_rule
.context
&& !_rule
.keyword
)
372 r
= edit
->rules
[_rule
.context
];
373 if (r
->first_right
== c
&& !(rule
.border
& RULE_ON_RIGHT_BORDER
)
375 compare_word_to_right (edit
, i
, r
->right
, r
->whole_word_chars_left
,
376 r
->whole_word_chars_right
, r
->line_start_right
)) > 0)
380 _rule
.border
= RULE_ON_RIGHT_BORDER
;
381 if (r
->between_delimiters
)
384 else if (is_end
&& rule
.border
& RULE_ON_RIGHT_BORDER
)
386 /* always turn off a context at 4 */
389 if (!keyword_foundleft
)
392 else if (is_end
&& rule
.border
& RULE_ON_LEFT_BORDER
)
394 /* never turn off a context at 2 */
400 /* check to turn on a keyword */
405 r
= edit
->rules
[_rule
.context
];
406 p
= r
->keyword_first_chars
;
409 while (*(p
= xx_strchr (edit
, (unsigned char *) p
+ 1, c
)) != '\0')
415 count
= p
- r
->keyword_first_chars
;
416 k
= r
->keyword
[count
];
417 e
= compare_word_to_right (edit
, i
, k
->keyword
, k
->whole_word_chars_left
,
418 k
->whole_word_chars_right
, k
->line_start
);
423 _rule
.keyword
= count
;
424 keyword_foundright
= TRUE
;
430 /* check to turn on a context */
433 if (!found_left
&& is_end
)
435 if (rule
.border
& RULE_ON_RIGHT_BORDER
)
439 contextchanged
= TRUE
;
443 else if (rule
.border
& RULE_ON_LEFT_BORDER
)
445 r
= edit
->rules
[_rule
._context
];
447 if (r
->between_delimiters
)
449 _rule
.context
= _rule
._context
;
450 contextchanged
= TRUE
;
453 if (r
->first_right
== c
)
457 e
= compare_word_to_right (edit
, i
, r
->right
, r
->whole_word_chars_left
,
458 r
->whole_word_chars_right
, r
->line_start_right
);
463 _rule
.border
= RULE_ON_RIGHT_BORDER
;
474 struct context_rule
**rules
= edit
->rules
;
476 for (count
= 1; rules
[count
]; count
++)
479 if (r
->first_left
== c
)
483 e
= compare_word_to_right (edit
, i
, r
->left
, r
->whole_word_chars_left
,
484 r
->whole_word_chars_right
, r
->line_start_left
);
485 if (e
>= end
&& (!_rule
.keyword
|| keyword_foundright
))
489 _rule
.border
= RULE_ON_LEFT_BORDER
;
490 _rule
._context
= count
;
491 if (!r
->between_delimiters
&& !_rule
.keyword
)
493 _rule
.context
= count
;
494 contextchanged
= TRUE
;
503 /* check again to turn on a keyword if the context switched */
504 if (contextchanged
&& !_rule
.keyword
)
508 r
= edit
->rules
[_rule
.context
];
509 p
= r
->keyword_first_chars
;
511 while (*(p
= xx_strchr (edit
, (unsigned char *) p
+ 1, c
)) != '\0')
517 count
= p
- r
->keyword_first_chars
;
518 k
= r
->keyword
[count
];
519 e
= compare_word_to_right (edit
, i
, k
->keyword
, k
->whole_word_chars_left
,
520 k
->whole_word_chars_right
, k
->line_start
);
524 _rule
.keyword
= count
;
533 /* --------------------------------------------------------------------------------------------- */
535 static edit_syntax_rule_t
536 edit_get_rule (WEdit
* edit
, off_t byte_index
)
540 if (byte_index
> edit
->last_get_rule
)
542 for (i
= edit
->last_get_rule
+ 1; i
<= byte_index
; i
++)
544 off_t d
= SYNTAX_MARKER_DENSITY
;
546 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
548 if (edit
->syntax_marker
!= NULL
)
549 d
+= ((syntax_marker_t
*) edit
->syntax_marker
->data
)->offset
;
555 s
= g_new (syntax_marker_t
, 1);
557 s
->rule
= edit
->rule
;
558 edit
->syntax_marker
= g_slist_prepend (edit
->syntax_marker
, s
);
562 else if (byte_index
< edit
->last_get_rule
)
568 if (edit
->syntax_marker
== NULL
)
570 memset (&edit
->rule
, 0, sizeof (edit
->rule
));
571 for (i
= -1; i
<= byte_index
; i
++)
572 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
576 s
= (syntax_marker_t
*) edit
->syntax_marker
->data
;
578 if (byte_index
>= s
->offset
)
580 edit
->rule
= s
->rule
;
581 for (i
= s
->offset
+ 1; i
<= byte_index
; i
++)
582 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
587 edit
->syntax_marker
= g_slist_delete_link (edit
->syntax_marker
, edit
->syntax_marker
);
590 edit
->last_get_rule
= byte_index
;
594 /* --------------------------------------------------------------------------------------------- */
597 translate_rule_to_color (const WEdit
* edit
, edit_syntax_rule_t rule
)
599 return edit
->rules
[rule
.context
]->keyword
[rule
.keyword
]->color
;
602 /* --------------------------------------------------------------------------------------------- */
604 Returns 0 on error/eof or a count of the number of bytes read
605 including the newline. Result must be free'd.
606 In case of an error, *line will not be modified.
610 read_one_line (char **line
, FILE * f
)
615 /* not reallocate string too often */
616 p
= g_string_sized_new (64);
635 /* handle all of \r\n, \r, \n correctly. */
648 g_string_append_c (p
, c
);
651 *line
= g_string_free (p
, FALSE
);
653 g_string_free (p
, TRUE
);
658 /* --------------------------------------------------------------------------------------------- */
698 *p
= SYNTAX_TOKEN_BRACKET
;
702 *p
= SYNTAX_TOKEN_BRACE
;
713 *p
= SYNTAX_TOKEN_STAR
;
716 *p
= SYNTAX_TOKEN_PLUS
;
729 /* --------------------------------------------------------------------------------------------- */
732 get_args (char *l
, char **args
, int args_size
)
736 while (argc
< args_size
)
739 while (*p
!= '\0' && whiteness (*p
))
743 for (l
= p
+ 1; *l
!= '\0' && !whiteness (*l
); l
++)
747 args
[argc
++] = convert (p
);
749 args
[argc
] = (char *) NULL
;
753 /* --------------------------------------------------------------------------------------------- */
756 this_try_alloc_color_pair (const char *fg
, const char *bg
, const char *attrs
)
758 char f
[80], b
[80], a
[80], *p
;
760 if (bg
!= NULL
&& *bg
== '\0')
762 if (fg
!= NULL
&& *fg
== '\0')
764 if (attrs
!= NULL
&& *attrs
== '\0')
767 if ((fg
== NULL
) && (bg
== NULL
))
768 return EDITOR_NORMAL_COLOR
;
772 g_strlcpy (f
, fg
, sizeof (f
));
780 g_strlcpy (b
, bg
, sizeof (b
));
786 if ((fg
== NULL
) || (bg
== NULL
))
788 /* get colors from skin */
791 editnormal
= mc_skin_get ("editor", "_default_", "default;default");
795 g_strlcpy (f
, editnormal
, sizeof (f
));
800 g_strlcpy (f
, "default", sizeof (f
));
805 p
= strchr (editnormal
, ';');
806 if ((p
!= NULL
) && (*(++p
) != '\0'))
807 g_strlcpy (b
, p
, sizeof (b
));
809 g_strlcpy (b
, "default", sizeof (b
));
818 g_strlcpy (a
, attrs
, sizeof (a
));
822 /* get_args() mangles the + signs, unmangle 'em */
824 while ((p
= strchr (p
, SYNTAX_TOKEN_PLUS
)) != NULL
)
828 return tty_try_alloc_color_pair (fg
, bg
, attrs
);
831 /* --------------------------------------------------------------------------------------------- */
834 open_include_file (const char *filename
)
838 MC_PTR_FREE (error_file_name
);
839 error_file_name
= g_strdup (filename
);
840 if (g_path_is_absolute (filename
))
841 return fopen (filename
, "r");
843 g_free (error_file_name
);
845 g_build_filename (mc_config_get_data_path (), EDIT_DIR
, filename
, (char *) NULL
);
846 f
= fopen (error_file_name
, "r");
850 g_free (error_file_name
);
851 error_file_name
= g_build_filename (mc_global
.sysconfig_dir
, "syntax", filename
, (char *) NULL
);
852 f
= fopen (error_file_name
, "r");
856 g_free (error_file_name
);
858 g_build_filename (mc_global
.share_data_dir
, "syntax", filename
, (char *) NULL
);
860 return fopen (error_file_name
, "r");
863 /* --------------------------------------------------------------------------------------------- */
866 xx_lowerize_line (WEdit
* edit
, char *line
, size_t len
)
868 if (edit
->is_case_insensitive
)
871 for (i
= 0; i
< len
; ++i
)
872 line
[i
] = tolower (line
[i
]);
876 /* --------------------------------------------------------------------------------------------- */
877 /** returns line number on error */
880 edit_read_syntax_rules (WEdit
* edit
, FILE * f
, char **args
, int args_size
)
883 char *fg
, *bg
, *attrs
;
884 char last_fg
[32] = "", last_bg
[32] = "", last_attrs
[64] = "";
885 char whole_right
[512];
886 char whole_left
[512];
888 int save_line
= 0, line
= 0;
889 struct context_rule
**r
, *c
= NULL
;
890 int num_words
= -1, num_contexts
= -1;
894 int alloc_contexts
= MAX_CONTEXTS
,
895 alloc_words_per_context
= MAX_WORDS_PER_CONTEXT
,
896 max_alloc_words_per_context
= MAX_WORDS_PER_CONTEXT
;
899 edit
->is_case_insensitive
= FALSE
;
901 strcpy (whole_left
, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
902 strcpy (whole_right
, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
904 r
= edit
->rules
= g_malloc0 (alloc_contexts
* sizeof (struct context_rule
*));
907 edit
->defines
= g_tree_new ((GCompareFunc
) strcmp
);
917 len
= read_one_line (&l
, f
);
919 xx_lowerize_line (edit
, l
, len
);
928 line
= save_line
+ 1;
929 MC_PTR_FREE (error_file_name
);
931 len
= read_one_line (&l
, f
);
934 xx_lowerize_line (edit
, l
, len
);
937 argc
= get_args (l
, args
, args_size
);
943 else if (strcmp (args
[0], "include") == 0)
945 if (g
!= NULL
|| argc
!= 2)
951 f
= open_include_file (args
[1]);
954 MC_PTR_FREE (error_file_name
);
961 else if (strcmp (args
[0], "caseinsensitive") == 0)
963 edit
->is_case_insensitive
= TRUE
;
965 else if (strcmp (args
[0], "wholechars") == 0)
968 if (strcmp (*a
, "left") == 0)
971 g_strlcpy (whole_left
, *a
, sizeof (whole_left
));
973 else if (strcmp (*a
, "right") == 0)
976 g_strlcpy (whole_right
, *a
, sizeof (whole_right
));
980 g_strlcpy (whole_left
, *a
, sizeof (whole_left
));
981 g_strlcpy (whole_right
, *a
, sizeof (whole_right
));
986 else if (strcmp (args
[0], "context") == 0)
989 if (num_contexts
== -1)
991 if (strcmp (*a
, "default") != 0)
992 { /* first context is the default */
996 c
= r
[0] = g_malloc0 (sizeof (struct context_rule
));
997 c
->left
= g_strdup (" ");
998 c
->right
= g_strdup (" ");
1003 /* Terminate previous context. */
1004 r
[num_contexts
- 1]->keyword
[num_words
] = NULL
;
1005 c
= r
[num_contexts
] = g_malloc0 (sizeof (struct context_rule
));
1006 if (strcmp (*a
, "exclusive") == 0)
1009 c
->between_delimiters
= 1;
1012 if (strcmp (*a
, "whole") == 0)
1015 c
->whole_word_chars_left
= g_strdup (whole_left
);
1016 c
->whole_word_chars_right
= g_strdup (whole_right
);
1018 else if (strcmp (*a
, "wholeleft") == 0)
1021 c
->whole_word_chars_left
= g_strdup (whole_left
);
1023 else if (strcmp (*a
, "wholeright") == 0)
1026 c
->whole_word_chars_right
= g_strdup (whole_right
);
1029 if (strcmp (*a
, "linestart") == 0)
1032 c
->line_start_left
= 1;
1035 c
->left
= g_strdup (*a
++);
1037 if (strcmp (*a
, "linestart") == 0)
1040 c
->line_start_right
= 1;
1043 c
->right
= g_strdup (*a
++);
1044 c
->first_left
= *c
->left
;
1045 c
->first_right
= *c
->right
;
1047 c
->keyword
= g_malloc (alloc_words_per_context
* sizeof (struct key_word
*));
1049 c
->keyword
[0] = g_malloc0 (sizeof (struct key_word
));
1050 subst_defines (edit
->defines
, a
, &args
[1024]);
1060 g_strlcpy (last_fg
, fg
!= NULL
? fg
: "", sizeof (last_fg
));
1061 g_strlcpy (last_bg
, bg
!= NULL
? bg
: "", sizeof (last_bg
));
1062 g_strlcpy (last_attrs
, attrs
!= NULL
? attrs
: "", sizeof (last_attrs
));
1063 c
->keyword
[0]->color
= this_try_alloc_color_pair (fg
, bg
, attrs
);
1064 c
->keyword
[0]->keyword
= g_strdup (" ");
1067 alloc_words_per_context
= MAX_WORDS_PER_CONTEXT
;
1068 if (++num_contexts
>= alloc_contexts
)
1070 struct context_rule
**tmp
;
1072 alloc_contexts
+= 128;
1073 tmp
= g_realloc (r
, alloc_contexts
* sizeof (struct context_rule
*));
1077 else if (strcmp (args
[0], "spellcheck") == 0)
1086 else if (strcmp (args
[0], "keyword") == 0)
1090 if (num_words
== -1)
1093 k
= r
[num_contexts
- 1]->keyword
[num_words
] = g_malloc0 (sizeof (struct key_word
));
1094 if (strcmp (*a
, "whole") == 0)
1097 k
->whole_word_chars_left
= g_strdup (whole_left
);
1098 k
->whole_word_chars_right
= g_strdup (whole_right
);
1100 else if (strcmp (*a
, "wholeleft") == 0)
1103 k
->whole_word_chars_left
= g_strdup (whole_left
);
1105 else if (strcmp (*a
, "wholeright") == 0)
1108 k
->whole_word_chars_right
= g_strdup (whole_right
);
1111 if (strcmp (*a
, "linestart") == 0)
1117 if (strcmp (*a
, "whole") == 0)
1121 k
->keyword
= g_strdup (*a
++);
1122 k
->first
= *k
->keyword
;
1123 subst_defines (edit
->defines
, a
, &args
[1024]);
1139 k
->color
= this_try_alloc_color_pair (fg
, bg
, attrs
);
1142 if (++num_words
>= alloc_words_per_context
)
1144 struct key_word
**tmp
;
1146 alloc_words_per_context
+= 1024;
1148 if (alloc_words_per_context
> max_alloc_words_per_context
)
1149 max_alloc_words_per_context
= alloc_words_per_context
;
1151 tmp
= g_realloc (c
->keyword
, alloc_words_per_context
* sizeof (struct key_word
*));
1155 else if (*(args
[0]) == '#')
1157 /* do nothing for comment */
1159 else if (strcmp (args
[0], "file") == 0)
1163 else if (strcmp (args
[0], "define") == 0)
1170 argv
= g_tree_lookup (edit
->defines
, key
);
1172 mc_defines_destroy (NULL
, argv
, NULL
);
1174 key
= g_strdup (key
);
1176 argv
= g_new (char *, argc
- 1);
1177 g_tree_insert (edit
->defines
, key
, argv
);
1179 *argv
++ = g_strdup (*a
++);
1183 { /* anything else is an error */
1192 /* Terminate context array. */
1193 if (num_contexts
> 0)
1195 r
[num_contexts
- 1]->keyword
[num_words
] = NULL
;
1196 r
[num_contexts
] = NULL
;
1199 if (edit
->rules
[0] == NULL
)
1200 MC_PTR_FREE (edit
->rules
);
1204 char *first_chars
, *p
;
1206 if (num_contexts
== -1)
1209 first_chars
= g_malloc0 (max_alloc_words_per_context
+ 2);
1211 for (i
= 0; edit
->rules
[i
] != NULL
; i
++)
1216 for (j
= 1; c
->keyword
[j
] != NULL
; j
++)
1217 *p
++ = c
->keyword
[j
]->first
;
1219 c
->keyword_first_chars
= g_strdup (first_chars
);
1222 g_free (first_chars
);
1228 /* --------------------------------------------------------------------------------------------- */
1230 /* returns -1 on file error, line number on error in file syntax */
1232 edit_read_syntax_file (WEdit
* edit
, char ***pnames
, const char *syntax_file
,
1233 const char *editor_file
, const char *first_line
, const char *type
)
1237 char *args
[1024], *l
= NULL
;
1242 gboolean found
= FALSE
;
1243 char **tmpnames
= NULL
;
1245 f
= fopen (syntax_file
, "r");
1248 lib_file
= g_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
1249 f
= fopen (lib_file
, "r");
1260 if (read_one_line (&l
, f
) == 0)
1262 (void) get_args (l
, args
, 1023); /* Final NULL */
1263 if (args
[0] == NULL
)
1266 /* Looking for `include ...` lines before first `file ...` ones */
1267 if (!found
&& strcmp (args
[0], "include") == 0)
1272 if (!args
[1] || !(g
= open_include_file (args
[1])))
1280 /* looking for `file ...' lines only */
1281 if (strcmp (args
[0], "file") != 0)
1286 /* must have two args or report error */
1287 if (!args
[1] || !args
[2])
1292 if (pnames
&& *pnames
)
1294 /* 1: just collecting a list of names of rule sets */
1295 /* Reallocate the list if required */
1296 if (count
% NENTRIES
== 0)
1299 (char **) g_try_realloc (*pnames
, (count
+ NENTRIES
+ 1) * sizeof (char *));
1300 if (tmpnames
== NULL
)
1304 (*pnames
)[count
++] = g_strdup (args
[2]);
1305 (*pnames
)[count
] = NULL
;
1309 /* 2: rule set was explicitly specified by the caller */
1310 if (strcmp (type
, args
[2]) == 0)
1313 else if (editor_file
&& edit
)
1315 /* 3: auto-detect rule set from regular expressions */
1318 q
= mc_search (args
[1], editor_file
, MC_SEARCH_T_REGEX
);
1319 /* does filename match arg 1 ? */
1322 /* does first line match arg 3 ? */
1323 q
= mc_search (args
[3], first_line
, MC_SEARCH_T_REGEX
);
1330 syntax_type
= args
[2];
1331 line_error
= edit_read_syntax_rules (edit
, g
? g
: f
, args
, 1023);
1334 if (!error_file_name
) /* an included file */
1335 result
= line
+ line_error
;
1337 result
= line_error
;
1341 g_free (edit
->syntax_type
);
1342 edit
->syntax_type
= g_strdup (syntax_type
);
1343 /* if there are no rules then turn off syntax highlighting for speed */
1344 if (!g
&& !edit
->rules
[1])
1345 if (!edit
->rules
[0]->keyword
[1] && !edit
->rules
[0]->spelling
)
1347 edit_free_syntax_rules (edit
);
1365 /* --------------------------------------------------------------------------------------------- */
1368 get_first_editor_line (WEdit
* edit
)
1378 for (i
= 0; i
< sizeof (s
) - 1; i
++)
1380 s
[i
] = edit_get_byte (edit
, i
);
1388 s
[sizeof (s
) - 1] = '\0';
1394 /* --------------------------------------------------------------------------------------------- */
1395 /*** public functions ****************************************************************************/
1396 /* --------------------------------------------------------------------------------------------- */
1399 edit_get_syntax_color (WEdit
* edit
, off_t byte_index
)
1401 if (!tty_use_colors ())
1404 if (edit
->rules
!= NULL
&& byte_index
< edit
->last_byte
&& option_syntax_highlighting
)
1405 return translate_rule_to_color (edit
, edit_get_rule (edit
, byte_index
));
1407 return EDITOR_NORMAL_COLOR
;
1410 /* --------------------------------------------------------------------------------------------- */
1413 edit_free_syntax_rules (WEdit
* edit
)
1420 destroy_defines (&edit
->defines
);
1424 edit_get_rule (edit
, -1);
1425 MC_PTR_FREE (edit
->syntax_type
);
1427 for (i
= 0; edit
->rules
[i
]; i
++)
1429 if (edit
->rules
[i
]->keyword
)
1431 for (j
= 0; edit
->rules
[i
]->keyword
[j
]; j
++)
1433 MC_PTR_FREE (edit
->rules
[i
]->keyword
[j
]->keyword
);
1434 MC_PTR_FREE (edit
->rules
[i
]->keyword
[j
]->whole_word_chars_left
);
1435 MC_PTR_FREE (edit
->rules
[i
]->keyword
[j
]->whole_word_chars_right
);
1436 MC_PTR_FREE (edit
->rules
[i
]->keyword
[j
]);
1439 MC_PTR_FREE (edit
->rules
[i
]->left
);
1440 MC_PTR_FREE (edit
->rules
[i
]->right
);
1441 MC_PTR_FREE (edit
->rules
[i
]->whole_word_chars_left
);
1442 MC_PTR_FREE (edit
->rules
[i
]->whole_word_chars_right
);
1443 MC_PTR_FREE (edit
->rules
[i
]->keyword
);
1444 MC_PTR_FREE (edit
->rules
[i
]->keyword_first_chars
);
1445 MC_PTR_FREE (edit
->rules
[i
]);
1448 g_slist_foreach (edit
->syntax_marker
, (GFunc
) g_free
, NULL
);
1449 g_slist_free (edit
->syntax_marker
);
1450 edit
->syntax_marker
= NULL
;
1451 MC_PTR_FREE (edit
->rules
);
1452 tty_color_free_all_tmp ();
1455 /* --------------------------------------------------------------------------------------------- */
1457 * Load rules into edit struct. Either edit or *pnames must be NULL. If
1458 * edit is NULL, a list of types will be stored into names. If type is
1459 * NULL, then the type will be selected according to the filename.
1460 * type must be edit->syntax_type or NULL
1463 edit_load_syntax (WEdit
* edit
, char ***pnames
, const char *type
)
1468 if (option_auto_syntax
)
1475 saved_type
= g_strdup (type
); /* save edit->syntax_type */
1476 edit_free_syntax_rules (edit
);
1477 edit
->syntax_type
= saved_type
; /* restore edit->syntax_type */
1480 if (!tty_use_colors ())
1483 if (!option_syntax_highlighting
&& (!pnames
|| !*pnames
))
1486 if (edit
!= NULL
&& edit
->filename_vpath
== NULL
)
1489 f
= mc_config_get_full_path (EDIT_SYNTAX_FILE
);
1494 tmp_f
= vfs_path_to_str (edit
->filename_vpath
);
1495 r
= edit_read_syntax_file (edit
, pnames
, f
, tmp_f
,
1496 get_first_editor_line (edit
),
1497 option_auto_syntax
? NULL
: edit
->syntax_type
);
1501 r
= edit_read_syntax_file (NULL
, pnames
, f
, NULL
, "", NULL
);
1504 edit_free_syntax_rules (edit
);
1505 message (D_ERROR
, _("Load syntax file"),
1506 _("Cannot open file %s\n%s"), f
, unix_error_string (errno
));
1510 edit_free_syntax_rules (edit
);
1511 message (D_ERROR
, _("Load syntax file"),
1512 _("Error in file %s on line %d"), error_file_name
? error_file_name
: f
, r
);
1513 MC_PTR_FREE (error_file_name
);
1519 /* --------------------------------------------------------------------------------------------- */
1522 edit_get_syntax_type (const WEdit
* edit
)
1524 return edit
->syntax_type
;
1527 /* --------------------------------------------------------------------------------------------- */