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
)
145 g_strfreev ((char **) value
);
150 /* --------------------------------------------------------------------------------------------- */
151 /** Completely destroys the defines tree */
154 destroy_defines (GTree
** defines
)
156 g_tree_foreach (*defines
, mc_defines_destroy
, NULL
);
157 g_tree_destroy (*defines
);
161 /* --------------------------------------------------------------------------------------------- */
163 /** Wrapper for case insensitive mode */
165 xx_tolower (const WEdit
* edit
, int c
)
167 return edit
->is_case_insensitive
? tolower (c
) : c
;
170 /* --------------------------------------------------------------------------------------------- */
173 subst_defines (GTree
* defines
, char **argv
, char **argv_end
)
178 while (*argv
!= NULL
&& argv
< argv_end
)
180 t
= g_tree_lookup (defines
, *argv
);
185 /* Count argv array members */
187 for (p
= &argv
[1]; *p
!= NULL
; p
++)
190 /* Count members of definition array */
191 for (p
= t
; *p
!= NULL
; p
++)
193 p
= &argv
[count
+ argc
];
195 /* Buffer overflow or infinitive loop in define */
199 /* Move rest of argv after definition members */
201 *p
-- = argv
[argc
-- + 1];
203 /* Copy definition members to argv */
204 for (p
= argv
; *t
!= NULL
; *p
++ = *t
++)
211 /* --------------------------------------------------------------------------------------------- */
214 compare_word_to_right (const WEdit
* edit
, off_t i
, const char *text
,
215 const char *whole_left
, const char *whole_right
, long line_start
)
217 const unsigned char *p
, *q
;
223 c
= xx_tolower (edit
, edit_get_byte (edit
, i
- 1));
224 if ((line_start
!= 0 && c
!= '\n') || (whole_left
!= NULL
&& strchr (whole_left
, c
) != NULL
))
227 for (p
= (unsigned char *) text
, q
= p
+ str_term_width1 ((char *) p
); p
< q
; p
++, i
++)
231 case SYNTAX_TOKEN_STAR
:
236 c
= xx_tolower (edit
, edit_get_byte (edit
, i
));
237 if (*p
== '\0' && whole_right
!= NULL
&& strchr (whole_right
, c
) == NULL
)
246 case SYNTAX_TOKEN_PLUS
:
252 c
= xx_tolower (edit
, edit_get_byte (edit
, i
));
256 if (*p
== *text
&& p
[1] == '\0') /* handle eg '+' and @+@ keywords properly */
259 if (j
!= 0 && strchr ((char *) p
+ 1, c
) != NULL
) /* c exists further down, so it will get matched later */
261 if (c
== '\n' || c
== '\t' || c
== ' ' ||
262 (whole_right
!= NULL
&& strchr (whole_right
, c
) == NULL
))
277 case SYNTAX_TOKEN_BRACKET
:
284 c
= xx_tolower (edit
, edit_get_byte (edit
, i
));
285 for (j
= 0; p
[j
] != SYNTAX_TOKEN_BRACKET
&& p
[j
]; j
++)
293 while (*p
!= SYNTAX_TOKEN_BRACKET
&& p
<= q
)
300 case SYNTAX_TOKEN_BRACE
:
303 c
= xx_tolower (edit
, edit_get_byte (edit
, i
));
304 for (; *p
!= SYNTAX_TOKEN_BRACE
&& *p
; p
++)
309 while (*p
!= SYNTAX_TOKEN_BRACE
&& p
< q
)
313 if (*p
!= xx_tolower (edit
, edit_get_byte (edit
, i
)))
317 return (whole_right
!= NULL
&&
318 strchr (whole_right
, xx_tolower (edit
, edit_get_byte (edit
, i
))) != NULL
) ? -1 : i
;
321 /* --------------------------------------------------------------------------------------------- */
324 xx_strchr (const WEdit
* edit
, const unsigned char *s
, int char_byte
)
326 while (*s
>= '\005' && xx_tolower (edit
, *s
) != char_byte
)
329 return (const char *) s
;
332 /* --------------------------------------------------------------------------------------------- */
334 static edit_syntax_rule_t
335 apply_rules_going_right (WEdit
* edit
, off_t i
, edit_syntax_rule_t rule
)
337 struct context_rule
*r
;
339 gboolean contextchanged
= FALSE
;
340 gboolean found_left
= FALSE
, found_right
= FALSE
;
341 gboolean keyword_foundleft
= FALSE
, keyword_foundright
= FALSE
;
344 edit_syntax_rule_t _rule
= rule
;
346 c
= xx_tolower (edit
, edit_get_byte (edit
, i
));
349 is_end
= (rule
.end
== i
);
351 /* check to turn off a keyword */
354 if (edit_get_byte (edit
, i
- 1) == '\n')
359 keyword_foundleft
= TRUE
;
363 /* check to turn off a context */
364 if (_rule
.context
&& !_rule
.keyword
)
368 r
= edit
->rules
[_rule
.context
];
369 if (r
->first_right
== c
&& !(rule
.border
& RULE_ON_RIGHT_BORDER
)
371 compare_word_to_right (edit
, i
, r
->right
, r
->whole_word_chars_left
,
372 r
->whole_word_chars_right
, r
->line_start_right
)) > 0)
376 _rule
.border
= RULE_ON_RIGHT_BORDER
;
377 if (r
->between_delimiters
)
380 else if (is_end
&& rule
.border
& RULE_ON_RIGHT_BORDER
)
382 /* always turn off a context at 4 */
385 if (!keyword_foundleft
)
388 else if (is_end
&& rule
.border
& RULE_ON_LEFT_BORDER
)
390 /* never turn off a context at 2 */
396 /* check to turn on a keyword */
401 r
= edit
->rules
[_rule
.context
];
402 p
= r
->keyword_first_chars
;
405 while (*(p
= xx_strchr (edit
, (unsigned char *) p
+ 1, c
)) != '\0')
411 count
= p
- r
->keyword_first_chars
;
412 k
= r
->keyword
[count
];
413 e
= compare_word_to_right (edit
, i
, k
->keyword
, k
->whole_word_chars_left
,
414 k
->whole_word_chars_right
, k
->line_start
);
419 _rule
.keyword
= count
;
420 keyword_foundright
= TRUE
;
426 /* check to turn on a context */
429 if (!found_left
&& is_end
)
431 if (rule
.border
& RULE_ON_RIGHT_BORDER
)
435 contextchanged
= TRUE
;
439 else if (rule
.border
& RULE_ON_LEFT_BORDER
)
441 r
= edit
->rules
[_rule
._context
];
443 if (r
->between_delimiters
)
445 _rule
.context
= _rule
._context
;
446 contextchanged
= TRUE
;
449 if (r
->first_right
== c
)
453 e
= compare_word_to_right (edit
, i
, r
->right
, r
->whole_word_chars_left
,
454 r
->whole_word_chars_right
, r
->line_start_right
);
459 _rule
.border
= RULE_ON_RIGHT_BORDER
;
470 struct context_rule
**rules
= edit
->rules
;
472 for (count
= 1; rules
[count
]; count
++)
475 if (r
->first_left
== c
)
479 e
= compare_word_to_right (edit
, i
, r
->left
, r
->whole_word_chars_left
,
480 r
->whole_word_chars_right
, r
->line_start_left
);
481 if (e
>= end
&& (!_rule
.keyword
|| keyword_foundright
))
485 _rule
.border
= RULE_ON_LEFT_BORDER
;
486 _rule
._context
= count
;
487 if (!r
->between_delimiters
&& !_rule
.keyword
)
489 _rule
.context
= count
;
490 contextchanged
= TRUE
;
499 /* check again to turn on a keyword if the context switched */
500 if (contextchanged
&& !_rule
.keyword
)
504 r
= edit
->rules
[_rule
.context
];
505 p
= r
->keyword_first_chars
;
507 while (*(p
= xx_strchr (edit
, (unsigned char *) p
+ 1, c
)) != '\0')
513 count
= p
- r
->keyword_first_chars
;
514 k
= r
->keyword
[count
];
515 e
= compare_word_to_right (edit
, i
, k
->keyword
, k
->whole_word_chars_left
,
516 k
->whole_word_chars_right
, k
->line_start
);
520 _rule
.keyword
= count
;
529 /* --------------------------------------------------------------------------------------------- */
531 static edit_syntax_rule_t
532 edit_get_rule (WEdit
* edit
, off_t byte_index
)
536 if (byte_index
> edit
->last_get_rule
)
538 for (i
= edit
->last_get_rule
+ 1; i
<= byte_index
; i
++)
540 off_t d
= SYNTAX_MARKER_DENSITY
;
542 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
544 if (edit
->syntax_marker
!= NULL
)
545 d
+= ((syntax_marker_t
*) edit
->syntax_marker
->data
)->offset
;
551 s
= g_new (syntax_marker_t
, 1);
553 s
->rule
= edit
->rule
;
554 edit
->syntax_marker
= g_slist_prepend (edit
->syntax_marker
, s
);
558 else if (byte_index
< edit
->last_get_rule
)
564 if (edit
->syntax_marker
== NULL
)
566 memset (&edit
->rule
, 0, sizeof (edit
->rule
));
567 for (i
= -1; i
<= byte_index
; i
++)
568 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
572 s
= (syntax_marker_t
*) edit
->syntax_marker
->data
;
574 if (byte_index
>= s
->offset
)
576 edit
->rule
= s
->rule
;
577 for (i
= s
->offset
+ 1; i
<= byte_index
; i
++)
578 edit
->rule
= apply_rules_going_right (edit
, i
, edit
->rule
);
583 edit
->syntax_marker
= g_slist_delete_link (edit
->syntax_marker
, edit
->syntax_marker
);
586 edit
->last_get_rule
= byte_index
;
590 /* --------------------------------------------------------------------------------------------- */
593 translate_rule_to_color (const WEdit
* edit
, edit_syntax_rule_t rule
)
595 return edit
->rules
[rule
.context
]->keyword
[rule
.keyword
]->color
;
598 /* --------------------------------------------------------------------------------------------- */
600 Returns 0 on error/eof or a count of the number of bytes read
601 including the newline. Result must be free'd.
602 In case of an error, *line will not be modified.
606 read_one_line (char **line
, FILE * f
)
611 /* not reallocate string too often */
612 p
= g_string_sized_new (64);
631 /* handle all of \r\n, \r, \n correctly. */
644 g_string_append_c (p
, c
);
647 *line
= g_string_free (p
, FALSE
);
649 g_string_free (p
, TRUE
);
654 /* --------------------------------------------------------------------------------------------- */
694 *p
= SYNTAX_TOKEN_BRACKET
;
698 *p
= SYNTAX_TOKEN_BRACE
;
709 *p
= SYNTAX_TOKEN_STAR
;
712 *p
= SYNTAX_TOKEN_PLUS
;
725 /* --------------------------------------------------------------------------------------------- */
728 get_args (char *l
, char **args
, int args_size
)
732 while (argc
< args_size
)
735 while (*p
!= '\0' && whiteness (*p
))
739 for (l
= p
+ 1; *l
!= '\0' && !whiteness (*l
); l
++)
743 args
[argc
++] = convert (p
);
745 args
[argc
] = (char *) NULL
;
749 /* --------------------------------------------------------------------------------------------- */
752 this_try_alloc_color_pair (const char *fg
, const char *bg
, const char *attrs
)
754 char f
[80], b
[80], a
[80], *p
;
756 if (bg
!= NULL
&& *bg
== '\0')
758 if (fg
!= NULL
&& *fg
== '\0')
760 if (attrs
!= NULL
&& *attrs
== '\0')
763 if ((fg
== NULL
) && (bg
== NULL
))
764 return EDITOR_NORMAL_COLOR
;
768 g_strlcpy (f
, fg
, sizeof (f
));
776 g_strlcpy (b
, bg
, sizeof (b
));
782 if ((fg
== NULL
) || (bg
== NULL
))
784 /* get colors from skin */
787 editnormal
= mc_skin_get ("editor", "_default_", "default;default");
791 g_strlcpy (f
, editnormal
, sizeof (f
));
796 g_strlcpy (f
, "default", sizeof (f
));
801 p
= strchr (editnormal
, ';');
802 if ((p
!= NULL
) && (*(++p
) != '\0'))
803 g_strlcpy (b
, p
, sizeof (b
));
805 g_strlcpy (b
, "default", sizeof (b
));
814 g_strlcpy (a
, attrs
, sizeof (a
));
818 /* get_args() mangles the + signs, unmangle 'em */
820 while ((p
= strchr (p
, SYNTAX_TOKEN_PLUS
)) != NULL
)
824 return tty_try_alloc_color_pair (fg
, bg
, attrs
);
827 /* --------------------------------------------------------------------------------------------- */
830 open_include_file (const char *filename
)
834 MC_PTR_FREE (error_file_name
);
835 error_file_name
= g_strdup (filename
);
836 if (g_path_is_absolute (filename
))
837 return fopen (filename
, "r");
839 g_free (error_file_name
);
841 g_build_filename (mc_config_get_data_path (), EDIT_DIR
, filename
, (char *) NULL
);
842 f
= fopen (error_file_name
, "r");
846 g_free (error_file_name
);
847 error_file_name
= g_build_filename (mc_global
.sysconfig_dir
, "syntax", filename
, (char *) NULL
);
848 f
= fopen (error_file_name
, "r");
852 g_free (error_file_name
);
854 g_build_filename (mc_global
.share_data_dir
, "syntax", filename
, (char *) NULL
);
856 return fopen (error_file_name
, "r");
859 /* --------------------------------------------------------------------------------------------- */
862 xx_lowerize_line (WEdit
* edit
, char *line
, size_t len
)
864 if (edit
->is_case_insensitive
)
867 for (i
= 0; i
< len
; ++i
)
868 line
[i
] = tolower (line
[i
]);
872 /* --------------------------------------------------------------------------------------------- */
873 /** returns line number on error */
876 edit_read_syntax_rules (WEdit
* edit
, FILE * f
, char **args
, int args_size
)
879 char *fg
, *bg
, *attrs
;
880 char last_fg
[32] = "", last_bg
[32] = "", last_attrs
[64] = "";
881 char whole_right
[512];
882 char whole_left
[512];
884 int save_line
= 0, line
= 0;
885 struct context_rule
**r
, *c
= NULL
;
886 int num_words
= -1, num_contexts
= -1;
890 int alloc_contexts
= MAX_CONTEXTS
,
891 alloc_words_per_context
= MAX_WORDS_PER_CONTEXT
,
892 max_alloc_words_per_context
= MAX_WORDS_PER_CONTEXT
;
895 edit
->is_case_insensitive
= FALSE
;
897 strcpy (whole_left
, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
898 strcpy (whole_right
, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
900 r
= edit
->rules
= g_malloc0 (alloc_contexts
* sizeof (struct context_rule
*));
903 edit
->defines
= g_tree_new ((GCompareFunc
) strcmp
);
913 len
= read_one_line (&l
, f
);
915 xx_lowerize_line (edit
, l
, len
);
924 line
= save_line
+ 1;
925 MC_PTR_FREE (error_file_name
);
927 len
= read_one_line (&l
, f
);
930 xx_lowerize_line (edit
, l
, len
);
933 argc
= get_args (l
, args
, args_size
);
939 else if (strcmp (args
[0], "include") == 0)
941 if (g
!= NULL
|| argc
!= 2)
947 f
= open_include_file (args
[1]);
950 MC_PTR_FREE (error_file_name
);
957 else if (strcmp (args
[0], "caseinsensitive") == 0)
959 edit
->is_case_insensitive
= TRUE
;
961 else if (strcmp (args
[0], "wholechars") == 0)
964 if (strcmp (*a
, "left") == 0)
967 g_strlcpy (whole_left
, *a
, sizeof (whole_left
));
969 else if (strcmp (*a
, "right") == 0)
972 g_strlcpy (whole_right
, *a
, sizeof (whole_right
));
976 g_strlcpy (whole_left
, *a
, sizeof (whole_left
));
977 g_strlcpy (whole_right
, *a
, sizeof (whole_right
));
982 else if (strcmp (args
[0], "context") == 0)
985 if (num_contexts
== -1)
987 if (strcmp (*a
, "default") != 0)
988 { /* first context is the default */
992 c
= r
[0] = g_malloc0 (sizeof (struct context_rule
));
993 c
->left
= g_strdup (" ");
994 c
->right
= g_strdup (" ");
999 /* Terminate previous context. */
1000 r
[num_contexts
- 1]->keyword
[num_words
] = NULL
;
1001 c
= r
[num_contexts
] = g_malloc0 (sizeof (struct context_rule
));
1002 if (strcmp (*a
, "exclusive") == 0)
1005 c
->between_delimiters
= 1;
1008 if (strcmp (*a
, "whole") == 0)
1011 c
->whole_word_chars_left
= g_strdup (whole_left
);
1012 c
->whole_word_chars_right
= g_strdup (whole_right
);
1014 else if (strcmp (*a
, "wholeleft") == 0)
1017 c
->whole_word_chars_left
= g_strdup (whole_left
);
1019 else if (strcmp (*a
, "wholeright") == 0)
1022 c
->whole_word_chars_right
= g_strdup (whole_right
);
1025 if (strcmp (*a
, "linestart") == 0)
1028 c
->line_start_left
= 1;
1031 c
->left
= g_strdup (*a
++);
1033 if (strcmp (*a
, "linestart") == 0)
1036 c
->line_start_right
= 1;
1039 c
->right
= g_strdup (*a
++);
1040 c
->first_left
= *c
->left
;
1041 c
->first_right
= *c
->right
;
1043 c
->keyword
= g_malloc (alloc_words_per_context
* sizeof (struct key_word
*));
1045 c
->keyword
[0] = g_malloc0 (sizeof (struct key_word
));
1046 subst_defines (edit
->defines
, a
, &args
[1024]);
1056 g_strlcpy (last_fg
, fg
!= NULL
? fg
: "", sizeof (last_fg
));
1057 g_strlcpy (last_bg
, bg
!= NULL
? bg
: "", sizeof (last_bg
));
1058 g_strlcpy (last_attrs
, attrs
!= NULL
? attrs
: "", sizeof (last_attrs
));
1059 c
->keyword
[0]->color
= this_try_alloc_color_pair (fg
, bg
, attrs
);
1060 c
->keyword
[0]->keyword
= g_strdup (" ");
1063 alloc_words_per_context
= MAX_WORDS_PER_CONTEXT
;
1064 if (++num_contexts
>= alloc_contexts
)
1066 struct context_rule
**tmp
;
1068 alloc_contexts
+= 128;
1069 tmp
= g_realloc (r
, alloc_contexts
* sizeof (struct context_rule
*));
1073 else if (strcmp (args
[0], "spellcheck") == 0)
1082 else if (strcmp (args
[0], "keyword") == 0)
1086 if (num_words
== -1)
1089 k
= r
[num_contexts
- 1]->keyword
[num_words
] = g_malloc0 (sizeof (struct key_word
));
1090 if (strcmp (*a
, "whole") == 0)
1093 k
->whole_word_chars_left
= g_strdup (whole_left
);
1094 k
->whole_word_chars_right
= g_strdup (whole_right
);
1096 else if (strcmp (*a
, "wholeleft") == 0)
1099 k
->whole_word_chars_left
= g_strdup (whole_left
);
1101 else if (strcmp (*a
, "wholeright") == 0)
1104 k
->whole_word_chars_right
= g_strdup (whole_right
);
1107 if (strcmp (*a
, "linestart") == 0)
1113 if (strcmp (*a
, "whole") == 0)
1117 k
->keyword
= g_strdup (*a
++);
1118 k
->first
= *k
->keyword
;
1119 subst_defines (edit
->defines
, a
, &args
[1024]);
1135 k
->color
= this_try_alloc_color_pair (fg
, bg
, attrs
);
1138 if (++num_words
>= alloc_words_per_context
)
1140 struct key_word
**tmp
;
1142 alloc_words_per_context
+= 1024;
1144 if (alloc_words_per_context
> max_alloc_words_per_context
)
1145 max_alloc_words_per_context
= alloc_words_per_context
;
1147 tmp
= g_realloc (c
->keyword
, alloc_words_per_context
* sizeof (struct key_word
*));
1151 else if (*(args
[0]) == '#')
1153 /* do nothing for comment */
1155 else if (strcmp (args
[0], "file") == 0)
1159 else if (strcmp (args
[0], "define") == 0)
1166 argv
= g_tree_lookup (edit
->defines
, key
);
1168 mc_defines_destroy (NULL
, argv
, NULL
);
1170 key
= g_strdup (key
);
1172 argv
= g_new (char *, argc
- 1);
1173 g_tree_insert (edit
->defines
, key
, argv
);
1175 *argv
++ = g_strdup (*a
++);
1179 { /* anything else is an error */
1188 /* Terminate context array. */
1189 if (num_contexts
> 0)
1191 r
[num_contexts
- 1]->keyword
[num_words
] = NULL
;
1192 r
[num_contexts
] = NULL
;
1195 if (edit
->rules
[0] == NULL
)
1196 MC_PTR_FREE (edit
->rules
);
1200 char *first_chars
, *p
;
1202 if (num_contexts
== -1)
1205 first_chars
= g_malloc0 (max_alloc_words_per_context
+ 2);
1207 for (i
= 0; edit
->rules
[i
] != NULL
; i
++)
1212 for (j
= 1; c
->keyword
[j
] != NULL
; j
++)
1213 *p
++ = c
->keyword
[j
]->first
;
1215 c
->keyword_first_chars
= g_strdup (first_chars
);
1218 g_free (first_chars
);
1224 /* --------------------------------------------------------------------------------------------- */
1226 /* returns -1 on file error, line number on error in file syntax */
1228 edit_read_syntax_file (WEdit
* edit
, char ***pnames
, const char *syntax_file
,
1229 const char *editor_file
, const char *first_line
, const char *type
)
1233 char *args
[1024], *l
= NULL
;
1238 gboolean found
= FALSE
;
1239 char **tmpnames
= NULL
;
1241 f
= fopen (syntax_file
, "r");
1244 lib_file
= g_build_filename (mc_global
.share_data_dir
, "syntax", "Syntax", (char *) NULL
);
1245 f
= fopen (lib_file
, "r");
1256 if (read_one_line (&l
, f
) == 0)
1258 (void) get_args (l
, args
, 1023); /* Final NULL */
1259 if (args
[0] == NULL
)
1262 /* Looking for `include ...` lines before first `file ...` ones */
1263 if (!found
&& strcmp (args
[0], "include") == 0)
1268 if (!args
[1] || !(g
= open_include_file (args
[1])))
1276 /* looking for `file ...' lines only */
1277 if (strcmp (args
[0], "file") != 0)
1282 /* must have two args or report error */
1283 if (!args
[1] || !args
[2])
1288 if (pnames
&& *pnames
)
1290 /* 1: just collecting a list of names of rule sets */
1291 /* Reallocate the list if required */
1292 if (count
% NENTRIES
== 0)
1295 (char **) g_try_realloc (*pnames
, (count
+ NENTRIES
+ 1) * sizeof (char *));
1296 if (tmpnames
== NULL
)
1300 (*pnames
)[count
++] = g_strdup (args
[2]);
1301 (*pnames
)[count
] = NULL
;
1305 /* 2: rule set was explicitly specified by the caller */
1306 if (strcmp (type
, args
[2]) == 0)
1309 else if (editor_file
&& edit
)
1311 /* 3: auto-detect rule set from regular expressions */
1314 q
= mc_search (args
[1], editor_file
, MC_SEARCH_T_REGEX
);
1315 /* does filename match arg 1 ? */
1318 /* does first line match arg 3 ? */
1319 q
= mc_search (args
[3], first_line
, MC_SEARCH_T_REGEX
);
1326 syntax_type
= args
[2];
1327 line_error
= edit_read_syntax_rules (edit
, g
? g
: f
, args
, 1023);
1330 if (!error_file_name
) /* an included file */
1331 result
= line
+ line_error
;
1333 result
= line_error
;
1337 g_free (edit
->syntax_type
);
1338 edit
->syntax_type
= g_strdup (syntax_type
);
1339 /* if there are no rules then turn off syntax highlighting for speed */
1340 if (!g
&& !edit
->rules
[1])
1341 if (!edit
->rules
[0]->keyword
[1] && !edit
->rules
[0]->spelling
)
1343 edit_free_syntax_rules (edit
);
1361 /* --------------------------------------------------------------------------------------------- */
1364 get_first_editor_line (WEdit
* edit
)
1374 for (i
= 0; i
< sizeof (s
) - 1; i
++)
1376 s
[i
] = edit_get_byte (edit
, i
);
1384 s
[sizeof (s
) - 1] = '\0';
1390 /* --------------------------------------------------------------------------------------------- */
1391 /*** public functions ****************************************************************************/
1392 /* --------------------------------------------------------------------------------------------- */
1395 edit_get_syntax_color (WEdit
* edit
, off_t byte_index
)
1397 if (!tty_use_colors ())
1400 if (edit
->rules
!= NULL
&& byte_index
< edit
->last_byte
&& option_syntax_highlighting
)
1401 return translate_rule_to_color (edit
, edit_get_rule (edit
, byte_index
));
1403 return EDITOR_NORMAL_COLOR
;
1406 /* --------------------------------------------------------------------------------------------- */
1409 edit_free_syntax_rules (WEdit
* edit
)
1416 destroy_defines (&edit
->defines
);
1420 edit_get_rule (edit
, -1);
1421 MC_PTR_FREE (edit
->syntax_type
);
1423 for (i
= 0; edit
->rules
[i
]; i
++)
1425 if (edit
->rules
[i
]->keyword
)
1427 for (j
= 0; edit
->rules
[i
]->keyword
[j
]; j
++)
1429 MC_PTR_FREE (edit
->rules
[i
]->keyword
[j
]->keyword
);
1430 MC_PTR_FREE (edit
->rules
[i
]->keyword
[j
]->whole_word_chars_left
);
1431 MC_PTR_FREE (edit
->rules
[i
]->keyword
[j
]->whole_word_chars_right
);
1432 MC_PTR_FREE (edit
->rules
[i
]->keyword
[j
]);
1435 MC_PTR_FREE (edit
->rules
[i
]->left
);
1436 MC_PTR_FREE (edit
->rules
[i
]->right
);
1437 MC_PTR_FREE (edit
->rules
[i
]->whole_word_chars_left
);
1438 MC_PTR_FREE (edit
->rules
[i
]->whole_word_chars_right
);
1439 MC_PTR_FREE (edit
->rules
[i
]->keyword
);
1440 MC_PTR_FREE (edit
->rules
[i
]->keyword_first_chars
);
1441 MC_PTR_FREE (edit
->rules
[i
]);
1444 g_slist_foreach (edit
->syntax_marker
, (GFunc
) g_free
, NULL
);
1445 g_slist_free (edit
->syntax_marker
);
1446 edit
->syntax_marker
= NULL
;
1447 MC_PTR_FREE (edit
->rules
);
1448 tty_color_free_all_tmp ();
1451 /* --------------------------------------------------------------------------------------------- */
1453 * Load rules into edit struct. Either edit or *pnames must be NULL. If
1454 * edit is NULL, a list of types will be stored into names. If type is
1455 * NULL, then the type will be selected according to the filename.
1456 * type must be edit->syntax_type or NULL
1459 edit_load_syntax (WEdit
* edit
, char ***pnames
, const char *type
)
1464 if (option_auto_syntax
)
1471 saved_type
= g_strdup (type
); /* save edit->syntax_type */
1472 edit_free_syntax_rules (edit
);
1473 edit
->syntax_type
= saved_type
; /* restore edit->syntax_type */
1476 if (!tty_use_colors ())
1479 if (!option_syntax_highlighting
&& (!pnames
|| !*pnames
))
1482 if (edit
!= NULL
&& edit
->filename_vpath
== NULL
)
1485 f
= mc_config_get_full_path (EDIT_SYNTAX_FILE
);
1490 tmp_f
= vfs_path_to_str (edit
->filename_vpath
);
1491 r
= edit_read_syntax_file (edit
, pnames
, f
, tmp_f
,
1492 get_first_editor_line (edit
),
1493 option_auto_syntax
? NULL
: edit
->syntax_type
);
1497 r
= edit_read_syntax_file (NULL
, pnames
, f
, NULL
, "", NULL
);
1500 edit_free_syntax_rules (edit
);
1501 message (D_ERROR
, _("Load syntax file"),
1502 _("Cannot open file %s\n%s"), f
, unix_error_string (errno
));
1506 edit_free_syntax_rules (edit
);
1507 message (D_ERROR
, _("Load syntax file"),
1508 _("Error in file %s on line %d"), error_file_name
? error_file_name
: f
, r
);
1509 MC_PTR_FREE (error_file_name
);
1515 /* --------------------------------------------------------------------------------------------- */
1518 edit_get_syntax_type (const WEdit
* edit
)
1520 return edit
->syntax_type
;
1523 /* --------------------------------------------------------------------------------------------- */