libt3highlight
highlight.h
1 /* Copyright (C) 2011-2012 G.P. Halkes
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License version 3, as
4  published by the Free Software Foundation.
5 
6  This program is distributed in the hope that it will be useful,
7  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  GNU General Public License for more details.
10 
11  You should have received a copy of the GNU General Public License
12  along with this program. If not, see <http://www.gnu.org/licenses/>.
13 */
14 #ifndef T3_HIGHLIGHT_H
15 #define T3_HIGHLIGHT_H
16 
17 #include <t3config/config.h>
18 #include <t3highlight/highlight_api.h>
19 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
41 #define T3_HIGHLIGHT_VERSION 0
42 
46 #define T3_ERR_INVALID_FORMAT (-96)
47 
48 #define T3_ERR_INVALID_REGEX (-95)
49 
50 #define T3_ERR_NO_SYNTAX (-94)
51 
52 #define T3_ERR_UNDEFINED_USE (-93)
53 
54 #define T3_ERR_INVALID_NAME (-92)
55 
56 #define T3_ERR_EMPTY_START_CYCLE (-91)
57 
58 #define T3_ERR_USE_CYCLE (-90)
59 
64 #define T3_HIGHLIGHT_UTF8 (1<<0)
65 
75 #define T3_HIGHLIGHT_UTF8_NOCHECK (1<<1)
76 
77 #define T3_HIGHLIGHT_USE_PATH (1<<2)
78 
79 #define T3_HIGHLIGHT_VERBOSE_ERROR (1<<3)
80 
86 typedef struct t3_highlight_t t3_highlight_t;
91 
95 typedef struct {
96  char *name;
97  char *lang_file;
99 
104 typedef struct {
105  int error;
107  char *file_name;
109  char *extra;
112 
119 T3_HIGHLIGHT_API t3_highlight_lang_t *t3_highlight_list(int flags, t3_highlight_error_t *error);
123 T3_HIGHLIGHT_API void t3_highlight_free_list(t3_highlight_lang_t *list);
124 
139 T3_HIGHLIGHT_API t3_highlight_t *t3_highlight_load(const char *name,
140  int (*map_style)(void *, const char *), void *map_style_data, int flags, t3_highlight_error_t *error);
154 T3_HIGHLIGHT_API t3_highlight_t *t3_highlight_load_by_filename(const char *name,
155  int (*map_style)(void *, const char *), void *map_style_data, int flags, t3_highlight_error_t *error);
167 T3_HIGHLIGHT_API t3_highlight_t *t3_highlight_load_by_langname(const char *name,
168  int (*map_style)(void *, const char *), void *map_style_data, int flags, t3_highlight_error_t *error);
181 T3_HIGHLIGHT_API t3_highlight_t *t3_highlight_load_by_detect(const char *line, size_t line_length, t3_bool first,
182  int (*map_style)(void *, const char *), void *map_style_data, int flags, t3_highlight_error_t *error);
183 
196 T3_HIGHLIGHT_API t3_highlight_t *t3_highlight_new(t3_config_t *syntax,
197  int (*map_style)(void *, const char *), void *map_style_data, int flags, t3_highlight_error_t *error);
198 
202 T3_HIGHLIGHT_API void t3_highlight_free(t3_highlight_t *highlight);
203 
205 T3_HIGHLIGHT_API const char *t3_highlight_get_langfile(const t3_highlight_t *highlight);
206 
245 T3_HIGHLIGHT_API t3_bool t3_highlight_match(t3_highlight_match_t *match, const char *str, size_t size);
246 
253 T3_HIGHLIGHT_API t3_highlight_match_t *t3_highlight_new_match(const t3_highlight_t *highlight);
257 T3_HIGHLIGHT_API void t3_highlight_free_match(t3_highlight_match_t *match);
266 T3_HIGHLIGHT_API void t3_highlight_reset(t3_highlight_match_t *match, int state);
270 T3_HIGHLIGHT_API size_t t3_highlight_get_start(t3_highlight_match_t *match);
272 T3_HIGHLIGHT_API size_t t3_highlight_get_match_start(t3_highlight_match_t *match);
274 T3_HIGHLIGHT_API size_t t3_highlight_get_end(t3_highlight_match_t *match);
276 T3_HIGHLIGHT_API int t3_highlight_get_begin_attr(t3_highlight_match_t *match);
278 T3_HIGHLIGHT_API int t3_highlight_get_match_attr(t3_highlight_match_t *match);
280 T3_HIGHLIGHT_API int t3_highlight_get_state(t3_highlight_match_t *match);
282 T3_HIGHLIGHT_API int t3_highlight_next_line(t3_highlight_match_t *match);
283 
288 T3_HIGHLIGHT_API const char *t3_highlight_strerror(int error);
289 
298 T3_HIGHLIGHT_API long t3_highlight_get_version(void);
299 
311 T3_HIGHLIGHT_API t3_bool t3_highlight_utf8check(const char *line, size_t size);
312 
329 T3_HIGHLIGHT_API t3_bool t3_highlight_detect(const char *line, size_t line_length, t3_bool first,
330  int flags, t3_highlight_lang_t *lang, t3_highlight_error_t *error);
331 
340 T3_HIGHLIGHT_API t3_bool t3_highlight_lang_by_filename(const char *filename, int flags, t3_highlight_lang_t *lang,
341  t3_highlight_error_t *error);
342 
349 T3_HIGHLIGHT_API void t3_highlight_free_lang(t3_highlight_lang_t lang);
350 
351 #ifdef __cplusplus
352 } /* extern "C" */
353 #endif
354 
355 #endif
void t3_highlight_free_match(t3_highlight_match_t *match)
Free t3_highlight_match_t structure.
Definition: match.c:303
t3_highlight_t * t3_highlight_load(const char *name, int(*map_style)(void *, const char *), void *map_style_data, int flags, t3_highlight_error_t *error)
Load a highlighting pattern, using a language file name.
Definition: io.c:262
int t3_highlight_get_match_attr(t3_highlight_match_t *match)
Get the attribute for the match section of the result.
Definition: match.c:327
int line_number
Line number where the error occured, but only if T3_HIGHLIGHT_VERBOSE_ERROR is set.
Definition: highlight.h:106
char * file_name
File name in which the error occured or NULL, but only if T3_HIGHLIGHT_VERBOSE_ERROR is set...
Definition: highlight.h:107
t3_highlight_t * t3_highlight_new(t3_config_t *syntax, int(*map_style)(void *, const char *), void *map_style_data, int flags, t3_highlight_error_t *error)
Create a highlighting pattern from a previously created configuration.
Definition: highlight.c:39
t3_bool t3_highlight_detect(const char *line, size_t line_length, t3_bool first, int flags, t3_highlight_lang_t *lang, t3_highlight_error_t *error)
Detect the language of a file from line data.
Definition: io.c:345
void t3_highlight_free(t3_highlight_t *highlight)
Free all memory associated with a highlighting pattern.
Definition: highlight.c:503
t3_highlight_t * t3_highlight_load_by_filename(const char *name, int(*map_style)(void *, const char *), void *map_style_data, int flags, t3_highlight_error_t *error)
Load a highlighting pattern, using a source file name.
Definition: io.c:250
t3_bool t3_highlight_lang_by_filename(const char *filename, int flags, t3_highlight_lang_t *lang, t3_highlight_error_t *error)
Detect the language of a file from its name.
Definition: io.c:438
t3_highlight_t * t3_highlight_load_by_langname(const char *name, int(*map_style)(void *, const char *), void *map_style_data, int flags, t3_highlight_error_t *error)
Load a highlighting pattern, using a language name.
Definition: io.c:256
int t3_highlight_get_state(t3_highlight_match_t *match)
Get the state represented by match.
Definition: match.c:331
An opaque struct representing a match and current state during highlighting.
Definition: internal.h:102
t3_bool t3_highlight_utf8check(const char *line, size_t size)
Check that a string is valid UTF8.
Definition: utf8.c:17
void t3_highlight_free_lang(t3_highlight_lang_t lang)
Free the data allocated for a single t3_highlight_lang_t.
Definition: io.c:442
An opaque struct representing a highlighting pattern.
Definition: internal.h:83
const char * t3_highlight_get_langfile(const t3_highlight_t *highlight)
Get the language file associated with this highlighting pattern.
Definition: io.c:334
t3_highlight_match_t * t3_highlight_new_match(const t3_highlight_t *highlight)
Allocate and initialize a new t3_highlight_match_t structure.
Definition: match.c:277
long t3_highlight_get_version(void)
Get the value of T3_HIGHLIGHT_VERSION corresponding to the actual used library.
Definition: highlight.c:551
t3_highlight_lang_t * t3_highlight_list(int flags, t3_highlight_error_t *error)
List the known languages.
Definition: io.c:132
char t3_bool
A boolean type that does not clash with C++ or C99 bool.
Definition: highlight_api.h:47
A struct representing a display name/language file name tuple.
Definition: highlight.h:95
char * lang_file
Name of the language file, to be used with t3_highlight_load.
Definition: highlight.h:97
size_t t3_highlight_get_end(t3_highlight_match_t *match)
Get the end of the match.
Definition: match.c:319
void t3_highlight_reset(t3_highlight_match_t *match, int state)
Reset a t3_highlight_match_t structure.
void t3_highlight_free_list(t3_highlight_lang_t *list)
Free a list returned by t3_highlight_list.
Definition: io.c:164
t3_highlight_t * t3_highlight_load_by_detect(const char *line, size_t line_length, t3_bool first, int(*map_style)(void *, const char *), void *map_style_data, int flags, t3_highlight_error_t *error)
Load a highlighting pattern, based on auto-detection from the line content.
Definition: io.c:423
size_t t3_highlight_get_match_start(t3_highlight_match_t *match)
Get the start of the match.
Definition: match.c:315
A struct with error information.
Definition: highlight.h:104
char * extra
Further information about the error or NULL, but only if T3_HIGHLIGHT_VERBOSE_ERROR is set...
Definition: highlight.h:109
char * name
Display name of a language.
Definition: highlight.h:96
t3_bool t3_highlight_match(t3_highlight_match_t *match, const char *str, size_t size)
Find the next highlighting match in a subject string.
Definition: match.c:195
int t3_highlight_get_begin_attr(t3_highlight_match_t *match)
Get the attribute for the pre-match section of the result.
Definition: match.c:323
const char * t3_highlight_strerror(int error)
Get a string description for an error code.
Definition: highlight.c:528
int t3_highlight_next_line(t3_highlight_match_t *match)
Set up match for highlighting the next line of input.
Definition: match.c:335
size_t t3_highlight_get_start(t3_highlight_match_t *match)
Get the start index of a result.
Definition: match.c:311