blob: e25356a06124dae49a486884ccdd2872cb429a29 [file] [log] [blame]
Jeff Kingd9bae1a2010-04-01 20:12:15 -04001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Jeff Kingbe58e702008-10-05 17:43:21 -04003#include "userdiff.h"
Jeff Kingbe58e702008-10-05 17:43:21 -04004#include "attr.h"
5
6static struct userdiff_driver *drivers;
7static int ndrivers;
8static int drivers_alloc;
9
Ævar Arnfjörð Bjarmason2dd75f12022-02-24 10:33:03 +010010#define PATTERNS(lang, rx, wrx) { \
11 .name = lang, \
12 .binary = -1, \
13 .funcname = { \
14 .pattern = rx, \
15 .cflags = REG_EXTENDED, \
16 }, \
17 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
18}
19#define IPATTERN(lang, rx, wrx) { \
20 .name = lang, \
21 .binary = -1, \
22 .funcname = { \
23 .pattern = rx, \
24 .cflags = REG_EXTENDED | REG_ICASE, \
25 }, \
26 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \
27}
Junio C Hamanob6029b32021-08-10 15:12:01 -070028
29/*
30 * Built-in drivers for various languages, sorted by their names
31 * (except that the "default" is left at the end).
32 *
33 * When writing or updating patterns, assume that the contents these
34 * patterns are applied to are syntactically correct. The patterns
35 * can be simple without implementing all syntactical corner cases, as
36 * long as they are sufficiently permissive.
37 */
Jeff Kingbe58e702008-10-05 17:43:21 -040038static struct userdiff_driver builtin_drivers[] = {
Adrian Johnsone90d0652012-09-16 13:24:15 +093039IPATTERN("ada",
Adrian Johnson39a87a22014-02-03 22:03:16 +103040 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
Adrian Johnsone90d0652012-09-16 13:24:15 +093041 "!^[ \t]*with[ \t].*$\n"
42 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
43 "^[ \t]*((package|protected|task)[ \t]+.*)$",
44 /* -- */
45 "[a-zA-Z][a-zA-Z0-9_]*"
Adrian Johnson39a87a22014-02-03 22:03:16 +103046 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
Adrian Johnsone90d0652012-09-16 13:24:15 +093047 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
Victor Engmark2ff6c342020-10-22 12:45:08 +130048PATTERNS("bash",
49 /* Optional leading indentation */
50 "^[ \t]*"
51 /* Start of captured text */
52 "("
53 "("
54 /* POSIX identifier with mandatory parentheses */
55 "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))"
56 "|"
57 /* Bashism identifier with optional parentheses */
58 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))"
59 ")"
60 /* Optional whitespace */
61 "[ \t]*"
62 /* Compound command starting with `{`, `(`, `((` or `[[` */
63 "(\\{|\\(\\(?|\\[\\[)"
64 /* End of captured text */
65 ")",
66 /* -- */
67 /* Characters not in the default $IFS value */
68 "[^ \t]+"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +020069PATTERNS("bibtex",
70 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
71 /* -- */
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 17:04:16 +020072 "[={}\"]|[^={}\" \t]+"),
73PATTERNS("cpp",
74 /* Jump targets or access declarations */
75 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
76 /* functions/methods, variables, and compounds at top level */
77 "^((::[[:space:]]*)?[A-Za-z_].*)$",
78 /* -- */
Johannes Sixt350b87c2021-10-08 19:09:55 +000079 /* identifiers and keywords */
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 17:04:16 +020080 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixt350b87c2021-10-08 19:09:55 +000081 /* decimal and octal integers as well as floatingpoint numbers */
Johannes Sixt386076e2021-10-24 11:56:43 +020082 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*"
Johannes Sixt350b87c2021-10-08 19:09:55 +000083 /* hexadecimal and binary integers */
Johannes Sixt386076e2021-10-24 11:56:43 +020084 "|0[xXbB][0-9a-fA-F]+[lLuU]*"
Johannes Sixt350b87c2021-10-08 19:09:55 +000085 /* floatingpoint numbers that begin with a decimal point */
Johannes Sixt386076e2021-10-24 11:56:43 +020086 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?"
Johannes Sixtc4fdba32021-10-10 17:03:04 +000087 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"),
Ævar Arnfjörð Bjarmasonddd164d2021-04-08 17:04:16 +020088PATTERNS("csharp",
89 /* Keywords */
90 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
91 /* Methods and constructors */
92 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
93 /* Properties */
94 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
95 /* Type definitions */
Julian Verdurmenc4e31782021-03-02 00:58:09 +000096 "^[ \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 17:04:16 +020097 /* Namespace */
98 "^[ \t]*(namespace[ \t]+.*)$",
99 /* -- */
100 "[a-zA-Z_][a-zA-Z0-9_]*"
101 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
102 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
103IPATTERN("css",
104 "![:;][[:space:]]*$\n"
105 "^[:[@.#]?[_a-z0-9].*$",
106 /* -- */
107 /*
108 * This regex comes from W3C CSS specs. Should theoretically also
109 * allow ISO 10646 characters U+00A0 and higher,
110 * but they are not handled in this regex.
111 */
112 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
113 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
114),
Stephen Boyd3c817602019-08-19 14:22:43 -0700115PATTERNS("dts",
116 "!;\n"
Stephen Boyd8da56a42019-10-20 11:52:30 -0700117 "!=\n"
Stephen Boyd3c817602019-08-19 14:22:43 -0700118 /* lines beginning with a word optionally preceded by '&' or the root */
Stephen Boyd8da56a42019-10-20 11:52:30 -0700119 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)",
Stephen Boyd3c817602019-08-19 14:22:43 -0700120 /* -- */
121 /* Property names and math operators */
122 "[a-zA-Z0-9,._+?#-]+"
123 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
Łukasz Niemiera8072002019-11-08 22:38:24 +0100124PATTERNS("elixir",
125 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$",
Ed Masted1b13842019-12-13 17:55:35 +0000126 /* -- */
Łukasz Niemiera8072002019-11-08 22:38:24 +0100127 /* Atoms, names, and module attributes */
Ed Masted1b13842019-12-13 17:55:35 +0000128 "[@:]?[a-zA-Z0-9@_?!]+"
Łukasz Niemiera8072002019-11-08 22:38:24 +0100129 /* Numbers with specific base */
130 "|[-+]?0[xob][0-9a-fA-F]+"
131 /* Numbers */
132 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?"
133 /* Operators and atoms that represent them */
134 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)"
135 /* Not real operators, but should be grouped */
136 "|:?%[A-Za-z0-9_.]\\{\\}?"),
Brandon Casey909a5492010-09-10 11:18:14 -0500137IPATTERN("fortran",
Philippe Blainb79e6922020-08-12 22:30:28 +0000138 /* Don't match comment lines */
Brandon Casey909a5492010-09-10 11:18:14 -0500139 "!^([C*]|[ \t]*!)\n"
Philippe Blainb79e6922020-08-12 22:30:28 +0000140 /* Don't match 'module procedure' lines */
Brandon Casey909a5492010-09-10 11:18:14 -0500141 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
Philippe Blainb79e6922020-08-12 22:30:28 +0000142 /* Program, module, block data */
Brandon Casey909a5492010-09-10 11:18:14 -0500143 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
Philippe Blainb79e6922020-08-12 22:30:28 +0000144 /* Subroutines and functions */
Philippe Blain75c3b6b2020-08-12 22:30:29 +0000145 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
Brandon Casey909a5492010-09-10 11:18:14 -0500146 /* -- */
147 "[a-zA-Z][a-zA-Z0-9_]*"
148 "|\\.([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])\\."
149 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
150 * Don't worry about format statements without leading digits since
151 * they would have been matched above as a variable anyway. */
152 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600153 "|//|\\*\\*|::|[/<>=]="),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200154IPATTERN("fountain",
155 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
156 /* -- */
Zoë Blade69f9c872015-07-21 14:22:46 +0100157 "[^ \t-]+"),
Alban Gruin1dbf0c02018-03-01 12:19:07 +0100158PATTERNS("golang",
159 /* Functions */
160 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
161 /* Structs and interfaces */
162 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
163 /* -- */
164 "[a-zA-Z_][a-zA-Z0-9_]*"
165 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
166 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200167PATTERNS("html",
168 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
169 /* -- */
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600170 "[^<>= \t]+"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100171PATTERNS("java",
Jeff Kingbe58e702008-10-05 17:43:21 -0400172 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
Tassilo Horna8cbc892021-08-11 19:51:04 +0200173 /* Class, enum, and interface declarations */
174 "^[ \t]*(([a-z]+[ \t]+)*(class|enum|interface)[ \t]+[A-Za-z][A-Za-z0-9_$]*[ \t]+.*)$\n"
175 /* Method definitions; note that constructor signatures are not */
176 /* matched because they are indistinguishable from method calls. */
177 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
Paolo Bonzini959e2e62009-06-17 16:26:06 +0200178 /* -- */
Thomas Rast80c49c32009-01-17 17:29:48 +0100179 "[a-zA-Z_][a-zA-Z0-9_]*"
180 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
181 "|[-+*/<>%&^|=!]="
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600182 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
Jaydeep P Das09188ed2022-03-12 10:18:32 +0530183PATTERNS("kotlin",
184 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$",
185 /* -- */
186 "[a-zA-Z_][a-zA-Z0-9_]*"
187 /* hexadecimal and binary numbers */
188 "|0[xXbB][0-9a-fA-F_]+[lLuU]*"
189 /* integers and floats */
190 "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*"
191 /* floating point numbers beginning with decimal point */
192 "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?"
193 /* unary and binary operators */
194 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"),
Ash Holland09dad922020-05-02 14:15:43 +0100195PATTERNS("markdown",
196 "^ {0,3}#{1,6}[ \t].*",
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200197 /* -- */
Ash Holland09dad922020-05-02 14:15:43 +0100198 "[^<>= \t]+"),
Gustaf Hendeby53b10a12011-11-15 21:15:03 +0100199PATTERNS("matlab",
Boxuan Li2731a782019-05-30 00:15:39 +0800200 /*
201 * Octave pattern is mostly the same as matlab, except that '%%%' and
Boxuan Li91bf3822019-05-18 11:46:23 +0800202 * '##' can also be used to begin code sections, in addition to '%%'
Boxuan Li2731a782019-05-30 00:15:39 +0800203 * that is understood by both.
204 */
Boxuan Li91bf3822019-05-18 11:46:23 +0800205 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200206 /* -- */
Gustaf Hendeby53b10a12011-11-15 21:15:03 +0100207 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100208PATTERNS("objc",
Jeff Kingbe58e702008-10-05 17:43:21 -0400209 /* Negate C statements that can look like functions */
210 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
211 /* Objective-C methods */
212 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
213 /* C functions */
Paolo Bonzini959e2e62009-06-17 16:26:06 +0200214 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
Jeff Kingbe58e702008-10-05 17:43:21 -0400215 /* Objective-C class/protocol definitions */
Thomas Rast80c49c32009-01-17 17:29:48 +0100216 "^(@(implementation|interface|protocol)[ \t].*)$",
217 /* -- */
218 "[a-zA-Z_][a-zA-Z0-9_]*"
219 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600220 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100221PATTERNS("pascal",
Ævar Arnfjörð Bjarmason82512e02021-04-08 17:04:18 +0200222 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface"
223 "|implementation|initialization|finalization)[ \t]*.*)$\n"
Thomas Rast80c49c32009-01-17 17:29:48 +0100224 "^(.*=[ \t]*(class|record).*)$",
225 /* -- */
226 "[a-zA-Z_][a-zA-Z0-9_]*"
227 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600228 "|<>|<=|>=|:=|\\.\\."),
Jonathan Nieder71a5d4b2010-12-26 03:07:31 -0600229PATTERNS("perl",
Jonathan Niederea2ca442011-05-21 14:38:26 -0500230 "^package .*\n"
231 "^sub [[:alnum:]_':]+[ \t]*"
232 "(\\([^)]*\\)[ \t]*)?" /* prototype */
233 /*
234 * Attributes. A regex can't count nested parentheses,
235 * so just slurp up whatever we see, taking care not
236 * to accept lines like "sub foo; # defined elsewhere".
237 *
238 * An attribute could contain a semicolon, but at that
239 * point it seems reasonable enough to give up.
240 */
241 "(:[^;#]*)?"
242 "(\\{[ \t]*)?" /* brace can come here or on the next line */
243 "(#.*)?$\n" /* comment */
Jonathan Niederf143d9c2011-05-22 12:29:32 -0500244 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
Jonathan Niederea2ca442011-05-21 14:38:26 -0500245 "(\\{[ \t]*)?" /* brace can come here or on the next line */
246 "(#.*)?$\n"
Jonathan Nieder12f09672011-05-21 14:35:51 -0500247 "^=head[0-9] .*", /* POD */
Jonathan Nieder71a5d4b2010-12-26 03:07:31 -0600248 /* -- */
249 "[[:alpha:]_'][[:alnum:]_']*"
250 "|0[xb]?[0-9a-fA-F_]*"
251 /* taking care not to interpret 3..5 as (3.)(.5) */
252 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
253 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
254 "|&&=|\\|\\|=|//=|\\*\\*="
255 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
256 "|[-+*/%.^&<>=!|]="
257 "|=~|!~"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600258 "|<<|<>|<=>|>>"),
Björn Steinbrink6d2f2082010-05-23 20:05:40 +0200259PATTERNS("php",
Javier Spagnolettiaff92822020-10-07 03:38:18 +0000260 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n"
USAMI Kenta2c7f3aa2021-08-31 06:01:25 +0000261 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100262 /* -- */
263 "[a-zA-Z_][a-zA-Z0-9_]*"
264 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600265 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200266PATTERNS("python",
267 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100268 /* -- */
269 "[a-zA-Z_][a-zA-Z0-9_]*"
270 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600271 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100272 /* -- */
Ævar Arnfjörð Bjarmason6d1c9c52021-04-08 17:04:17 +0200273PATTERNS("ruby",
274 "^[ \t]*((class|module|def)[ \t].*)$",
Thomas Rast80c49c32009-01-17 17:29:48 +0100275 /* -- */
276 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
277 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600278 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200279PATTERNS("rust",
Konrad Borowskia04c7e02020-10-07 13:26:11 +0000280 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$",
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200281 /* -- */
282 "[a-zA-Z_][a-zA-Z0-9_]*"
Johannes Sixt33be7b32019-05-30 18:44:35 +0200283 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200284 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
Atharva Raykara4373902021-04-08 14:44:43 +0530285PATTERNS("scheme",
286 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
287 /*
288 * R7RS valid identifiers include any sequence enclosed
289 * within vertical lines having no backslashes
290 */
291 "\\|([^\\\\]*)\\|"
292 /* All other words should be delimited by spaces or parentheses */
293 "|([^][)(}{[ \t])+"),
Thomas Rast80c49c32009-01-17 17:29:48 +0100294PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
Jonathan Nieder664d44e2011-01-11 15:48:50 -0600295 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
Jeff King122aa6f2008-10-05 17:43:36 -0400296{ "default", NULL, -1, { NULL, 0 } },
Jeff Kingbe58e702008-10-05 17:43:21 -0400297};
Thomas Rast80c49c32009-01-17 17:29:48 +0100298#undef PATTERNS
Brandon Casey909a5492010-09-10 11:18:14 -0500299#undef IPATTERN
Jeff Kingbe58e702008-10-05 17:43:21 -0400300
301static struct userdiff_driver driver_true = {
Ævar Arnfjörð Bjarmason2dd75f12022-02-24 10:33:03 +0100302 .name = "diff=true",
303 .binary = 0,
Jeff Kingbe58e702008-10-05 17:43:21 -0400304};
Jeff Kingbe58e702008-10-05 17:43:21 -0400305
306static struct userdiff_driver driver_false = {
Ævar Arnfjörð Bjarmason2dd75f12022-02-24 10:33:03 +0100307 .name = "!diff",
308 .binary = 1,
Jeff Kingbe58e702008-10-05 17:43:21 -0400309};
Jeff Kingbe58e702008-10-05 17:43:21 -0400310
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200311struct find_by_namelen_data {
312 const char *name;
313 size_t len;
314 struct userdiff_driver *driver;
315};
316
317static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver,
Jeff Kingc25d9e52022-12-13 06:16:57 -0500318 enum userdiff_driver_type type UNUSED,
319 void *priv)
Jeff Kingbe58e702008-10-05 17:43:21 -0400320{
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200321 struct find_by_namelen_data *cb_data = priv;
322
323 if (!strncmp(driver->name, cb_data->name, cb_data->len) &&
324 !driver->name[cb_data->len]) {
325 cb_data->driver = driver;
326 return 1; /* tell the caller to stop iterating */
Jeff Kingbe58e702008-10-05 17:43:21 -0400327 }
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200328 return 0;
329}
330
331static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len)
332{
333 struct find_by_namelen_data udcbdata = {
334 .name = name,
335 .len = len,
336 };
337 for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata);
338 return udcbdata.driver;
Jeff Kingbe58e702008-10-05 17:43:21 -0400339}
340
Jeff Kingbe58e702008-10-05 17:43:21 -0400341static int parse_funcname(struct userdiff_funcname *f, const char *k,
342 const char *v, int cflags)
343{
344 if (git_config_string(&f->pattern, k, v) < 0)
345 return -1;
346 f->cflags = cflags;
Jeff King6680a082012-02-07 13:23:02 -0500347 return 0;
Jeff Kingbe58e702008-10-05 17:43:21 -0400348}
349
Jeff King122aa6f2008-10-05 17:43:36 -0400350static int parse_tristate(int *b, const char *k, const char *v)
351{
352 if (v && !strcasecmp(v, "auto"))
353 *b = -1;
354 else
355 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 13:23:02 -0500356 return 0;
Jeff King122aa6f2008-10-05 17:43:36 -0400357}
358
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400359static int parse_bool(int *b, const char *k, const char *v)
360{
361 *b = git_config_bool(k, v);
Jeff King6680a082012-02-07 13:23:02 -0500362 return 0;
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400363}
364
Jeff Kingc7534ef2008-10-26 00:45:55 -0400365int userdiff_config(const char *k, const char *v)
Jeff Kingbe58e702008-10-05 17:43:21 -0400366{
367 struct userdiff_driver *drv;
Jeff King0a5987f2013-01-23 01:25:07 -0500368 const char *name, *type;
Jeff Kingf5914f42020-04-10 15:44:28 -0400369 size_t namelen;
Jeff Kingbe58e702008-10-05 17:43:21 -0400370
Jeff King0a5987f2013-01-23 01:25:07 -0500371 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
372 return 0;
373
374 drv = userdiff_find_by_namelen(name, namelen);
375 if (!drv) {
376 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
377 drv = &drivers[ndrivers++];
378 memset(drv, 0, sizeof(*drv));
379 drv->name = xmemdupz(name, namelen);
380 drv->binary = -1;
381 }
382
383 if (!strcmp(type, "funcname"))
Jeff Kingbe58e702008-10-05 17:43:21 -0400384 return parse_funcname(&drv->funcname, k, v, 0);
Jeff King0a5987f2013-01-23 01:25:07 -0500385 if (!strcmp(type, "xfuncname"))
Jeff Kingbe58e702008-10-05 17:43:21 -0400386 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
Jeff King0a5987f2013-01-23 01:25:07 -0500387 if (!strcmp(type, "binary"))
Jeff King122aa6f2008-10-05 17:43:36 -0400388 return parse_tristate(&drv->binary, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500389 if (!strcmp(type, "command"))
Jeff King6680a082012-02-07 13:23:02 -0500390 return git_config_string(&drv->external, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500391 if (!strcmp(type, "textconv"))
Jeff King6680a082012-02-07 13:23:02 -0500392 return git_config_string(&drv->textconv, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500393 if (!strcmp(type, "cachetextconv"))
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400394 return parse_bool(&drv->textconv_want_cache, k, v);
Jeff King0a5987f2013-01-23 01:25:07 -0500395 if (!strcmp(type, "wordregex"))
Jeff King6680a082012-02-07 13:23:02 -0500396 return git_config_string(&drv->word_regex, k, v);
Jeff Kingbe58e702008-10-05 17:43:21 -0400397
398 return 0;
399}
400
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100401struct userdiff_driver *userdiff_find_by_name(const char *name)
402{
Jeff Kingbe58e702008-10-05 17:43:21 -0400403 int len = strlen(name);
404 return userdiff_find_by_namelen(name, len);
405}
406
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +0200407struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
408 const char *path)
Jeff Kingbe58e702008-10-05 17:43:21 -0400409{
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800410 static struct attr_check *check;
Jeff Kingbe58e702008-10-05 17:43:21 -0400411
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800412 if (!check)
413 check = attr_check_initl("diff", NULL);
Jeff Kingbe58e702008-10-05 17:43:21 -0400414 if (!path)
415 return NULL;
Junio C Hamano11877b92018-10-19 13:34:02 +0900416 git_check_attr(istate, path, check);
Jeff Kingbe58e702008-10-05 17:43:21 -0400417
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800418 if (ATTR_TRUE(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400419 return &driver_true;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800420 if (ATTR_FALSE(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400421 return &driver_false;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800422 if (ATTR_UNSET(check->items[0].value))
Jeff Kingbe58e702008-10-05 17:43:21 -0400423 return NULL;
Junio C Hamano2aef63d2017-01-27 18:01:57 -0800424 return userdiff_find_by_name(check->items[0].value);
Jeff Kingbe58e702008-10-05 17:43:21 -0400425}
Jeff King3813e692011-05-23 16:30:14 -0400426
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +0100427struct userdiff_driver *userdiff_get_textconv(struct repository *r,
428 struct userdiff_driver *driver)
Jeff King3813e692011-05-23 16:30:14 -0400429{
430 if (!driver->textconv)
431 return NULL;
432
433 if (driver->textconv_want_cache && !driver->textconv_cache) {
434 struct notes_cache *c = xmalloc(sizeof(*c));
435 struct strbuf name = STRBUF_INIT;
436
437 strbuf_addf(&name, "textconv/%s", driver->name);
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +0100438 notes_cache_init(r, c, name.buf, driver->textconv);
Jeff King3813e692011-05-23 16:30:14 -0400439 driver->textconv_cache = c;
Rene Scharfe460c7eb2017-08-30 20:20:15 +0200440 strbuf_release(&name);
Jeff King3813e692011-05-23 16:30:14 -0400441 }
442
443 return driver;
444}
Ævar Arnfjörð Bjarmasonf12fa9e2021-04-08 17:04:19 +0200445
446static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn,
447 enum userdiff_driver_type type, void *cb_data,
448 struct userdiff_driver *drv,
449 int drv_size)
450{
451 int i;
452 int ret;
453 for (i = 0; i < drv_size; i++) {
454 struct userdiff_driver *item = drv + i;
455 if ((ret = fn(item, type, cb_data)))
456 return ret;
457 }
458 return 0;
459}
460
461int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data)
462{
463 int ret;
464
465 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM,
466 cb_data, drivers, ndrivers);
467 if (ret)
468 return ret;
469
470 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN,
471 cb_data, builtin_drivers,
472 ARRAY_SIZE(builtin_drivers));
473 if (ret)
474 return ret;
475
476 return 0;
477}