Line | Branch | Exec | Source |
---|---|---|---|
1 | static const struct EmacsModeEntry { | ||
2 | char name[15]; | ||
3 | uint8_t type; // FileTypeEnum | ||
4 | } emacs_modes[] = { | ||
5 | // Strings already in builtin_filetype_names[] need not be included here | ||
6 | {"autoconf", M4}, | ||
7 | {"c++", CPLUSPLUS}, | ||
8 | {"cperl", PERL}, | ||
9 | {"latex", TEX}, | ||
10 | {"makefile", MAKE}, | ||
11 | {"muttrc", CONFIG}, | ||
12 | {"nroff", ROFF}, | ||
13 | {"nxml", XML}, | ||
14 | {"shell-script", SH}, | ||
15 | }; | ||
16 | |||
17 | 46 | static ssize_t find_var_delim(const char *start, size_t len) | |
18 | { | ||
19 | 46 | const char *delim = xmemmem(start, len, STRN("-*-")); | |
20 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 18 times.
|
46 | return delim ? delim - start : -1; |
21 | } | ||
22 | |||
23 | 32 | static FileTypeEnum filetype_from_emacs_var(const StringView line) | |
24 | { | ||
25 | 32 | ssize_t delim_offset = find_var_delim(line.data, MIN(line.length, 128)); | |
26 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 18 times.
|
32 | if (delim_offset < 0) { |
27 | return NONE; | ||
28 | } | ||
29 | |||
30 | 14 | StringView vars = line; | |
31 | 14 | strview_remove_prefix(&vars, delim_offset + STRLEN("-*-")); | |
32 | 14 | strview_trim_left(&vars); | |
33 | |||
34 | 14 | delim_offset = find_var_delim(vars.data, MIN(vars.length, 128)); | |
35 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | if (delim_offset <= 0) { |
36 | return NONE; | ||
37 | } | ||
38 | |||
39 | 14 | vars.length = delim_offset; | |
40 | 14 | strview_trim_right(&vars); | |
41 | 14 | LOG_DEBUG("found emacs file-local vars: '%.*s'", (int)vars.length, vars.data); | |
42 | |||
43 | // TODO: Handle multiple local vars like e.g. "mode: example; other: xyz;", | ||
44 | // in addition to a single major mode string (by searching for "[Mm]ode:")? | ||
45 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | if (vars.length > FILETYPE_NAME_MAX) { |
46 | return NONE; | ||
47 | } | ||
48 | |||
49 | // Write the extracted var string into a buffer, to null-terminate and | ||
50 | // convert to lowercase (making the lookup effectively case-insensitive) | ||
51 | 14 | char name[FILETYPE_NAME_MAX + 1]; | |
52 | 14 | name[vars.length] = '\0'; | |
53 |
2/2✓ Branch 0 taken 187 times.
✓ Branch 1 taken 14 times.
|
201 | for (size_t i = 0; i < vars.length; i++) { |
54 | 187 | name[i] = ascii_tolower(vars.data[i]); | |
55 | } | ||
56 | |||
57 | // TODO: Remove "-ts" (tree-sitter), "-base" and "-ts-base" suffixes | ||
58 | // from name, before lookup? | ||
59 | 14 | const struct EmacsModeEntry *entry = BSEARCH(name, emacs_modes, vstrcmp); | |
60 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
|
14 | if (entry) { |
61 | 2 | return entry->type; | |
62 | } | ||
63 | |||
64 | 12 | ssize_t ft = BSEARCH_IDX(name, builtin_filetype_names, vstrcmp); | |
65 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | return (ft >= 0) ? (FileTypeEnum)ft : NONE; |
66 | } | ||
67 | |||
68 | 164 | static FileTypeEnum filetype_from_signature(const StringView line) | |
69 | { | ||
70 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 99 times.
|
164 | if (line.length < 5) { |
71 | return NONE; | ||
72 | } | ||
73 | |||
74 |
7/7✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 20 times.
|
65 | switch (line.data[0]) { |
75 | 9 | case '<': | |
76 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
|
9 | if (strview_has_prefix_icase(&line, "<!DOCTYPE HTML")) { |
77 | return HTML; | ||
78 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
|
7 | } else if (strview_has_prefix(&line, "<!DOCTYPE")) { |
79 | return XML; | ||
80 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | } else if (strview_has_prefix(&line, "<?xml")) { |
81 | return XML; | ||
82 | } | ||
83 | break; | ||
84 | 3 | case '%': | |
85 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (strview_has_prefix(&line, "%YAML")) { |
86 | return YAML; | ||
87 | } | ||
88 | break; | ||
89 | 17 | case '#': | |
90 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
|
17 | if (strview_has_prefix(&line, "#compdef ")) { |
91 | return SH; | ||
92 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1 times.
|
16 | } else if (strview_has_prefix(&line, "#autoload")) { |
93 | return SH; | ||
94 | } | ||
95 | break; | ||
96 | 1 | case 'I': | |
97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | return strview_has_prefix(&line, "ISO-10303-21;") ? STEP : NONE; |
98 | 3 | case 'd': | |
99 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | return strview_has_prefix(&line, "diff --git") ? DIFF : NONE; |
100 | case '/': | ||
101 | case '.': | ||
102 | case ';': | ||
103 | case '-': | ||
104 | case '\\': | ||
105 | // See: "emacs style file-local variables" in test_find_ft_firstline() | ||
106 | break; | ||
107 | default: | ||
108 | return NONE; | ||
109 | } | ||
110 | |||
111 | 32 | return filetype_from_emacs_var(line); | |
112 | } | ||
113 |