🌐 AI搜索 & 代理 主页
blob: e399543823bdf2c0c8f24fb87f7622ac4d8a366b [file] [log] [blame]
Elijah Newren36bf1952023-02-24 00:09:241#include "git-compat-util.h"
Brandon Williamsb2141fc2017-06-14 18:07:362#include "config.h"
Jeff Kingbe58e702008-10-05 21:43:213#include "userdiff.h"
Jeff Kingbe58e702008-10-05 21:43:214#include "attr.h"
Elijah Newren36bf1952023-02-24 00:09:245#include "strbuf.h"
Jeff Kingbe58e702008-10-05 21:43:216
7static struct userdiff_driver *drivers;
8static int ndrivers;
9static int drivers_alloc;
10
Ævar Arnfjörð Bjarmason2dd75f12022-02-24 09:33:0311#define PATTERNS(lang, rx, wrx) { \
12 .name = lang, \
13 .binary = -1, \
14 .funcname = { \
15 .pattern = rx, \
16 .cflags = REG_EXTENDED, \
17 }, \
18 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
René Scharfebe391442023-04-06 20:19:1119 .word_regex_multi_byte = wrx "|[^[:space:]]", \
Ævar Arnfjörð Bjarmason2dd75f12022-02-24 09:33:0320}
21#define IPATTERN(lang, rx, wrx) { \
22 .name = lang, \
23 .binary = -1, \
24 .funcname = { \
25 .pattern = rx, \
26 .cflags = REG_EXTENDED | REG_ICASE, \
27 }, \
28 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
René Scharfebe391442023-04-06 20:19:1129 .word_regex_multi_byte = wrx "|[^[:space:]]", \
Ævar Arnfjörð Bjarmason2dd75f12022-02-24 09:33:0330}
Junio C Hamanob6029b32021-08-10 22:12:0131
32/*
33 * Built-in drivers for various languages, sorted by their names
34 * (except that the "default" is left at the end).
35 *
36 * When writing or updating patterns, assume that the contents these
37 * patterns are applied to are syntactically correct. The patterns
38 * can be simple without implementing all syntactical corner cases, as
39 * long as they are sufficiently permissive.
40 */
Jeff Kingbe58e702008-10-05 21:43:2141static struct userdiff_driver builtin_drivers[] = {
Adrian Johnsone90d0652012-09-16 03:54:1542IPATTERN("ada",
Adrian Johnson39a87a22014-02-03 11:33:1643 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
Adrian Johnsone90d0652012-09-16 03:54:1544 "!^[ \t]*with[ \t].*$\n"
45 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
46 "^[ \t]*((package|protected|task)[ \t]+.*)$",
47 /* -- */
48 "[a-zA-Z][a-zA-Z0-9_]*"
Adrian Johnson39a87a22014-02-03 11:33:1649 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
Adrian Johnsone90d0652012-09-16 03:54:1550 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
Victor Engmark2ff6c342020-10-21 23:45:0851PATTERNS("bash",
52 /* Optional leading indentation */
53 "^[ \t]*"
54 /* Start of captured text */
55 "("
56 "("
57 /* POSIX identifier with mandatory parentheses */
58 "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
59 "|"
60 /* Bashism identifier with optional parentheses */
61 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
62 ")"
63 /* Optional whitespace */
64 "[ \t]*"
65 /* Compound command starting with `{`, `(`, `((` or `[[` */
66 "(\\{|\\(\\(?|\\[\\[)"
67 /* End of captured text */
68 ")",
69 /* -- */
70 /* Characters not in the default $IFS value */
71 "[^ \t]+"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 15:04:1772PATTERNS("bibtex",
73 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
74 /* -- */
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 15:04:1675 "[={}\"]|[^={}\" \t]+"),
76PATTERNS("cpp",
77 /* Jump targets or access declarations */
78 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
79 /* functions/methods, variables, and compounds at top level */
80 "^((::[[:space:]]*)?[A-Za-z_].*)$",
81 /* -- */
Johannes Sixt350b87c2021-10-08 19:09:5582 /* identifiers and keywords */
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 15:04:1683 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixt350b87c2021-10-08 19:09:5584 /* decimal and octal integers as well as floatingpoint numbers */
Johannes Sixt386076e2021-10-24 09:56:4385 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
Johannes Sixt350b87c2021-10-08 19:09:5586 /* hexadecimal and binary integers */
Johannes Sixt386076e2021-10-24 09:56:4387 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
Johannes Sixt350b87c2021-10-08 19:09:5588 /* floatingpoint numbers that begin with a decimal point */
Johannes Sixt386076e2021-10-24 09:56:4389 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?"
Johannes Sixtc4fdba32021-10-10 17:03:0490 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 15:04:1691PATTERNS("csharp",
92 /* Keywords */
93 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
94 /* Methods and constructors */
95 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
96 /* Properties */
97 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
98 /* Type definitions */
Julian Verdurmenc4e31782021-03-02 00:58:0999 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n"
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 15:04:16100 /* Namespace */
101 "^[ \t]*(namespace[ \t]+.*)$",
102 /* -- */
103 "[a-zA-Z_][a-zA-Z0-9_]*"
104 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
105 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
106IPATTERN("css",
107 "![:;][[:space:]]*$\n"
108 "^[:[@.#]?[_a-z0-9].*$",
109 /* -- */
110 /*
111 * This regex comes from W3C CSS specs. Should theoretically also
112 * allow ISO 10646 characters U+00A0 and higher,
113 * but they are not handled in this regex.
114 */
115 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
116 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
117),
Stephen Boyd3c817602019-08-19 21:22:43118PATTERNS("dts",
119 "!;\n"
Stephen Boyd8da56a42019-10-20 18:52:30120 "!=\n"
Stephen Boyd3c817602019-08-19 21:22:43121 /* lines beginning with a word optionally preceded by '&' or the root */
Stephen Boyd8da56a42019-10-20 18:52:30122 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
Stephen Boyd3c817602019-08-19 21:22:43123 /* -- */
124 /* Property names and math operators */
125 "[a-zA-Z0-9,._+?#-]+"
126 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
Łukasz Niemiera8072002019-11-08 21:38:24127PATTERNS("elixir",
128 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
Ed Masted1b13842019-12-13 17:55:35129 /* -- */
Łukasz Niemiera8072002019-11-08 21:38:24130 /* Atoms, names, and module attributes */
Ed Masted1b13842019-12-13 17:55:35131 "[@:]?[a-zA-Z0-9@_?!]+"
Łukasz Niemiera8072002019-11-08 21:38:24132 /* Numbers with specific base */
133 "|[-+]?0[xob][0-9a-fA-F]+"
134 /* Numbers */
135 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
136 /* Operators and atoms that represent them */
137 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
138 /* Not real operators, but should be grouped */
139 "|:?%[A-Za-z0-9_.]\\{\\}?"),
Brandon Casey909a5492010-09-10 16:18:14140IPATTERN("fortran",
Philippe Blainb79e6922020-08-12 22:30:28141 /* Don't match comment lines */
Brandon Casey909a5492010-09-10 16:18:14142 "!^([C*]|[ \t]*!)\n"
Philippe Blainb79e6922020-08-12 22:30:28143 /* Don't match 'module procedure' lines */
Brandon Casey909a5492010-09-10 16:18:14144 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
Philippe Blainb79e6922020-08-12 22:30:28145 /* Program, module, block data */
Brandon Casey909a5492010-09-10 16:18:14146 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
Philippe Blainb79e6922020-08-12 22:30:28147 /* Subroutines and functions */
Philippe Blain75c3b6b2020-08-12 22:30:29148 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
Brandon Casey909a5492010-09-10 16:18:14149 /* -- */
150 "[a-zA-Z][a-zA-Z0-9_]*"
151 "|\\.([Ee][Qq]|[Nn][Ee]|[Gg][TtEe]|[Ll][TtEe]|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]|[Aa][Nn][Dd]|[Oo][Rr]|[Nn]?[Ee][Qq][Vv]|[Nn][Oo][Tt])\\."
152 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
153 * Don't worry about format statements without leading digits since
154 * they would have been matched above as a variable anyway. */
155 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
Jonathan Nieder664d44e2011-01-11 21:48:50156 "|//|\\*\\*|::|[/<>=]="),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 15:04:17157IPATTERN("fountain",
158 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
159 /* -- */
Zoë Blade69f9c872015-07-21 13:22:46160 "[^ \t-]+"),
Alban Gruin1dbf0c02018-03-01 11:19:07161PATTERNS("golang",
162 /* Functions */
163 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
164 /* Structs and interfaces */
165 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
166 /* -- */
167 "[a-zA-Z_][a-zA-Z0-9_]*"
168 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
169 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 15:04:17170PATTERNS("html",
171 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
172 /* -- */
Jonathan Nieder664d44e2011-01-11 21:48:50173 "[^<>= \t]+"),
Thomas Rast80c49c32009-01-17 16:29:48174PATTERNS("java",
Jeff Kingbe58e702008-10-05 21:43:21175 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
Andrei Rybak575e6fc2023-02-07 23:42:58176 /* Class, enum, interface, and record declarations */
Andrei Rybak93d52ed2023-02-07 23:42:59177 "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n"
Tassilo Horna8cbc892021-08-11 17:51:04178 /* Method definitions; note that constructor signatures are not */
179 /* matched because they are indistinguishable from method calls. */
180 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
Paolo Bonzini959e2e62009-06-17 14:26:06181 /* -- */
Thomas Rast80c49c32009-01-17 16:29:48182 "[a-zA-Z_][a-zA-Z0-9_]*"
183 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
184 "|[-+*/<>%&^|=!]="
Jonathan Nieder664d44e2011-01-11 21:48:50185 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
Jaydeep P Das09188ed2022-03-12 04:48:32186PATTERNS("kotlin",
187 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$",
188 /* -- */
189 "[a-zA-Z_][a-zA-Z0-9_]*"
190 /* hexadecimal and binary numbers */
191 "|0[xXbB][0-9a-fA-F_]+[lLuU]*"
192 /* integers and floats */
193 "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*"
194 /* floating point numbers beginning with decimal point */
195 "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?"
196 /* unary and binary operators */
197 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"),
Ash Holland09dad922020-05-02 13:15:43198PATTERNS("markdown",
199 "^ {0,3}#{1,6}[ \t].*",
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 15:04:17200 /* -- */
Ash Holland09dad922020-05-02 13:15:43201 "[^<>= \t]+"),
Gustaf Hendeby53b10a12011-11-15 20:15:03202PATTERNS("matlab",
Boxuan Li2731a782019-05-29 16:15:39203 /*
204 * Octave pattern is mostly the same as matlab, except that '%%%' and
Boxuan Li91bf3822019-05-18 03:46:23205 * '##' can also be used to begin code sections, in addition to '%%'
Boxuan Li2731a782019-05-29 16:15:39206 * that is understood by both.
207 */
Boxuan Li91bf3822019-05-18 03:46:23208 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 15:04:17209 /* -- */
Gustaf Hendeby53b10a12011-11-15 20:15:03210 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
Thomas Rast80c49c32009-01-17 16:29:48211PATTERNS("objc",
Jeff Kingbe58e702008-10-05 21:43:21212 /* Negate C statements that can look like functions */
213 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
214 /* Objective-C methods */
215 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
216 /* C functions */
Paolo Bonzini959e2e62009-06-17 14:26:06217 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
Jeff Kingbe58e702008-10-05 21:43:21218 /* Objective-C class/protocol definitions */
Thomas Rast80c49c32009-01-17 16:29:48219 "^(@(implementation|interface|protocol)[ \t].*)$",
220 /* -- */
221 "[a-zA-Z_][a-zA-Z0-9_]*"
222 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 21:48:50223 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
Thomas Rast80c49c32009-01-17 16:29:48224PATTERNS("pascal",
Ævar Arnfjörð Bjarmason82512e02021-04-08 15:04:18225 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
226 "|implementation|initialization|finalization)[ \t]*.*)$\n"
Thomas Rast80c49c32009-01-17 16:29:48227 "^(.*=[ \t]*(class|record).*)$",
228 /* -- */
229 "[a-zA-Z_][a-zA-Z0-9_]*"
230 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 21:48:50231 "|<>|<=|>=|:=|\\.\\."),
Jonathan Nieder71a5d4b2010-12-26 09:07:31232PATTERNS("perl",
Jonathan Niederea2ca442011-05-21 19:38:26233 "^package .*\n"
234 "^sub [[:alnum:]_':]+[ \t]*"
235 "(\\([^)]*\\)[ \t]*)?" /* prototype */
236 /*
237 * Attributes. A regex can't count nested parentheses,
238 * so just slurp up whatever we see, taking care not
239 * to accept lines like "sub foo; # defined elsewhere".
240 *
241 * An attribute could contain a semicolon, but at that
242 * point it seems reasonable enough to give up.
243 */
244 "(:[^;#]*)?"
245 "(\\{[ \t]*)?" /* brace can come here or on the next line */
246 "(#.*)?$\n" /* comment */
Jonathan Niederf143d9c2011-05-22 17:29:32247 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
Jonathan Niederea2ca442011-05-21 19:38:26248 "(\\{[ \t]*)?" /* brace can come here or on the next line */
249 "(#.*)?$\n"
Jonathan Nieder12f09672011-05-21 19:35:51250 "^=head[0-9] .*", /* POD */
Jonathan Nieder71a5d4b2010-12-26 09:07:31251 /* -- */
252 "[[:alpha:]_'][[:alnum:]_']*"
253 "|0[xb]?[0-9a-fA-F_]*"
254 /* taking care not to interpret 3..5 as (3.)(.5) */
255 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
256 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
257 "|&&=|\\|\\|=|//=|\\*\\*="
258 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
259 "|[-+*/%.^&<>=!|]="
260 "|=~|!~"
Jonathan Nieder664d44e2011-01-11 21:48:50261 "|<<|<>|<=>|>>"),
Björn Steinbrink6d2f2082010-05-23 18:05:40262PATTERNS("php",
Javier Spagnolettiaff92822020-10-07 03:38:18263 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
USAMI Kenta2c7f3aa2021-08-31 06:01:25264 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
Thomas Rast80c49c32009-01-17 16:29:48265 /* -- */
266 "[a-zA-Z_][a-zA-Z0-9_]*"
267 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 21:48:50268 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 15:04:17269PATTERNS("python",
270 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
Thomas Rast80c49c32009-01-17 16:29:48271 /* -- */
272 "[a-zA-Z_][a-zA-Z0-9_]*"
273 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 21:48:50274 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
Thomas Rast80c49c32009-01-17 16:29:48275 /* -- */
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 15:04:17276PATTERNS("ruby",
277 "^[ \t]*((class|module|def)[ \t].*)$",
Thomas Rast80c49c32009-01-17 16:29:48278 /* -- */
279 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
280 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
Jonathan Nieder664d44e2011-01-11 21:48:50281 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
Marc-André Lureaud74e7862019-05-16 23:58:15282PATTERNS("rust",
Konrad Borowskia04c7e02020-10-07 13:26:11283 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
Marc-André Lureaud74e7862019-05-16 23:58:15284 /* -- */
285 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixt33be7b32019-05-30 16:44:35286 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
Marc-André Lureaud74e7862019-05-16 23:58:15287 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
Atharva Raykara4373902021-04-08 09:14:43288PATTERNS("scheme",
289 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
290 /*
291 * R7RS valid identifiers include any sequence enclosed
292 * within vertical lines having no backslashes
293 */
294 "\\|([^\\\\]*)\\|"
295 /* All other words should be delimited by spaces or parentheses */
296 "|([^][)(}{[ \t])+"),
Thomas Rast80c49c32009-01-17 16:29:48297PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
René Scharfebe391442023-04-06 20:19:11298 "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"),
John Caia4cf9002023-02-20 21:04:42299{ "default", NULL, NULL, -1, { NULL, 0 } },
Jeff Kingbe58e702008-10-05 21:43:21300};
Thomas Rast80c49c32009-01-17 16:29:48301#undef PATTERNS
Brandon Casey909a5492010-09-10 16:18:14302#undef IPATTERN
Jeff Kingbe58e702008-10-05 21:43:21303
304static struct userdiff_driver driver_true = {
Ævar Arnfjörð Bjarmason2dd75f12022-02-24 09:33:03305 .name = "diff=true",
306 .binary = 0,
Jeff Kingbe58e702008-10-05 21:43:21307};
Jeff Kingbe58e702008-10-05 21:43:21308
309static struct userdiff_driver driver_false = {
Ævar Arnfjörð Bjarmason2dd75f12022-02-24 09:33:03310 .name = "!diff",
311 .binary = 1,
Jeff Kingbe58e702008-10-05 21:43:21312};
Jeff Kingbe58e702008-10-05 21:43:21313
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 15:04:19314struct find_by_namelen_data {
315 const char *name;
316 size_t len;
317 struct userdiff_driver *driver;
318};
319
320static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver,
Jeff Kingc25d9e52022-12-13 11:16:57321 enum userdiff_driver_type type UNUSED,
322 void *priv)
Jeff Kingbe58e702008-10-05 21:43:21323{
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 15:04:19324 struct find_by_namelen_data *cb_data = priv;
325
326 if (!strncmp(driver->name, cb_data->name, cb_data->len) &&
327 !driver->name[cb_data->len]) {
328 cb_data->driver = driver;
329 return 1; /* tell the caller to stop iterating */
Jeff Kingbe58e702008-10-05 21:43:21330 }
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 15:04:19331 return 0;
332}
333
René Scharfebe391442023-04-06 20:19:11334static int regexec_supports_multi_byte_chars(void)
335{
336 static const char not_space[] = "[^[:space:]]";
337 static const char utf8_multi_byte_char[] = "\xc2\xa3";
338 regex_t re;
339 regmatch_t match;
340 static int result = -1;
341
342 if (result != -1)
343 return result;
344 if (regcomp(&re, not_space, REG_EXTENDED))
345 BUG("invalid regular expression: %s", not_space);
346 result = !regexec(&re, utf8_multi_byte_char, 1, &match, 0) &&
347 match.rm_so == 0 &&
348 match.rm_eo == strlen(utf8_multi_byte_char);
349 regfree(&re);
350 return result;
351}
352
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 15:04:19353static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len)
354{
355 struct find_by_namelen_data udcbdata = {
356 .name = name,
357 .len = len,
358 };
359 for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata);
360 return udcbdata.driver;
Jeff Kingbe58e702008-10-05 21:43:21361}
362
Jeff Kingbe58e702008-10-05 21:43:21363static int parse_funcname(struct userdiff_funcname *f, const char *k,
364 const char *v, int cflags)
365{
366 if (git_config_string(&f->pattern, k, v) < 0)
367 return -1;
368 f->cflags = cflags;
Jeff King6680a082012-02-07 18:23:02369 return 0;
Jeff Kingbe58e702008-10-05 21:43:21370}
371
Jeff King122aa6f2008-10-05 21:43:36372static int parse_tristate(int *b, const char *k, const char *v)
373{
374 if (v && !strcasecmp(v, "auto"))
375 *b = -1;
376 else
377 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 18:23:02378 return 0;
Jeff King122aa6f2008-10-05 21:43:36379}
380
Jeff Kingd9bae1a2010-04-02 00:12:15381static int parse_bool(int *b, const char *k, const char *v)
382{
383 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 18:23:02384 return 0;
Jeff Kingd9bae1a2010-04-02 00:12:15385}
386
Jeff Kingc7534ef2008-10-26 04:45:55387int userdiff_config(const char *k, const char *v)
Jeff Kingbe58e702008-10-05 21:43:21388{
389 struct userdiff_driver *drv;
Jeff King0a5987f2013-01-23 06:25:07390 const char *name, *type;
Jeff Kingf5914f42020-04-10 19:44:28391 size_t namelen;
Jeff Kingbe58e702008-10-05 21:43:21392
Jeff King0a5987f2013-01-23 06:25:07393 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
394 return 0;
395
396 drv = userdiff_find_by_namelen(name, namelen);
397 if (!drv) {
398 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
399 drv = &drivers[ndrivers++];
400 memset(drv, 0, sizeof(*drv));
401 drv->name = xmemdupz(name, namelen);
402 drv->binary = -1;
403 }
404
405 if (!strcmp(type, "funcname"))
Jeff Kingbe58e702008-10-05 21:43:21406 return parse_funcname(&drv->funcname, k, v, 0);
Jeff King0a5987f2013-01-23 06:25:07407 if (!strcmp(type, "xfuncname"))
Jeff Kingbe58e702008-10-05 21:43:21408 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
Jeff King0a5987f2013-01-23 06:25:07409 if (!strcmp(type, "binary"))
Jeff King122aa6f2008-10-05 21:43:36410 return parse_tristate(&drv->binary, k, v);
Jeff King0a5987f2013-01-23 06:25:07411 if (!strcmp(type, "command"))
Jeff King6680a082012-02-07 18:23:02412 return git_config_string(&drv->external, k, v);
Jeff King0a5987f2013-01-23 06:25:07413 if (!strcmp(type, "textconv"))
Jeff King6680a082012-02-07 18:23:02414 return git_config_string(&drv->textconv, k, v);
Jeff King0a5987f2013-01-23 06:25:07415 if (!strcmp(type, "cachetextconv"))
Jeff Kingd9bae1a2010-04-02 00:12:15416 return parse_bool(&drv->textconv_want_cache, k, v);
Jeff King0a5987f2013-01-23 06:25:07417 if (!strcmp(type, "wordregex"))
Jeff King6680a082012-02-07 18:23:02418 return git_config_string(&drv->word_regex, k, v);
John Caia4cf9002023-02-20 21:04:42419 if (!strcmp(type, "algorithm"))
420 return git_config_string(&drv->algorithm, k, v);
Jeff Kingbe58e702008-10-05 21:43:21421
422 return 0;
423}
424
Nguyễn Thái Ngọc Duy3b335762018-12-09 10:25:21425struct userdiff_driver *userdiff_find_by_name(const char *name)
426{
Jeff Kingbe58e702008-10-05 21:43:21427 int len = strlen(name);
René Scharfebe391442023-04-06 20:19:11428 struct userdiff_driver *driver = userdiff_find_by_namelen(name, len);
429 if (driver && driver->word_regex_multi_byte) {
430 if (regexec_supports_multi_byte_chars())
431 driver->word_regex = driver->word_regex_multi_byte;
432 driver->word_regex_multi_byte = NULL;
433 }
434 return driver;
Jeff Kingbe58e702008-10-05 21:43:21435}
436
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:33437struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
438 const char *path)
Jeff Kingbe58e702008-10-05 21:43:21439{
Junio C Hamano2aef63d2017-01-28 02:01:57440 static struct attr_check *check;
Jeff Kingbe58e702008-10-05 21:43:21441
Junio C Hamano2aef63d2017-01-28 02:01:57442 if (!check)
443 check = attr_check_initl("diff", NULL);
Jeff Kingbe58e702008-10-05 21:43:21444 if (!path)
445 return NULL;
John Cai44451a22023-05-06 04:15:29446 git_check_attr(istate, path, check);
Jeff Kingbe58e702008-10-05 21:43:21447
Junio C Hamano2aef63d2017-01-28 02:01:57448 if (ATTR_TRUE(check->items[0].value))
Jeff Kingbe58e702008-10-05 21:43:21449 return &driver_true;
Junio C Hamano2aef63d2017-01-28 02:01:57450 if (ATTR_FALSE(check->items[0].value))
Jeff Kingbe58e702008-10-05 21:43:21451 return &driver_false;
Junio C Hamano2aef63d2017-01-28 02:01:57452 if (ATTR_UNSET(check->items[0].value))
Jeff Kingbe58e702008-10-05 21:43:21453 return NULL;
Junio C Hamano2aef63d2017-01-28 02:01:57454 return userdiff_find_by_name(check->items[0].value);
Jeff Kingbe58e702008-10-05 21:43:21455}
Jeff King3813e692011-05-23 20:30:14456
Nguyễn Thái Ngọc Duybd7ad452018-11-10 05:49:06457struct userdiff_driver *userdiff_get_textconv(struct repository *r,
458 struct userdiff_driver *driver)
Jeff King3813e692011-05-23 20:30:14459{
460 if (!driver->textconv)
461 return NULL;
462
463 if (driver->textconv_want_cache && !driver->textconv_cache) {
464 struct notes_cache *c = xmalloc(sizeof(*c));
465 struct strbuf name = STRBUF_INIT;
466
467 strbuf_addf(&name, "textconv/%s", driver->name);
Nguyễn Thái Ngọc Duybd7ad452018-11-10 05:49:06468 notes_cache_init(r, c, name.buf, driver->textconv);
Jeff King3813e692011-05-23 20:30:14469 driver->textconv_cache = c;
Rene Scharfe460c7eb2017-08-30 18:20:15470 strbuf_release(&name);
Jeff King3813e692011-05-23 20:30:14471 }
472
473 return driver;
474}
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 15:04:19475
476static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn,
477 enum userdiff_driver_type type, void *cb_data,
478 struct userdiff_driver *drv,
479 int drv_size)
480{
481 int i;
482 int ret;
483 for (i = 0; i < drv_size; i++) {
484 struct userdiff_driver *item = drv + i;
485 if ((ret = fn(item, type, cb_data)))
486 return ret;
487 }
488 return 0;
489}
490
491int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data)
492{
493 int ret;
494
495 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM,
496 cb_data, drivers, ndrivers);
497 if (ret)
498 return ret;
499
500 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN,
501 cb_data, builtin_drivers,
502 ARRAY_SIZE(builtin_drivers));
503 if (ret)
504 return ret;
505
506 return 0;
507}