+static struct hunk_header_regexp {
+ char *name;
+ char *regexp;
+ struct hunk_header_regexp *next;
+} *hunk_header_regexp_list, **hunk_header_regexp_tail;
+
+static int hunk_header_config(const char *var, const char *value)
+{
+ static const char funcname[] = "funcname.";
+ struct hunk_header_regexp *hh;
+
+ if (prefixcmp(var, funcname))
+ return 0;
+ var += strlen(funcname);
+ for (hh = hunk_header_regexp_list; hh; hh = hh->next)
+ if (!strcmp(var, hh->name)) {
+ free(hh->regexp);
+ hh->regexp = xstrdup(value);
+ return 0;
+ }
+ hh = xcalloc(1, sizeof(*hh));
+ hh->name = xstrdup(var);
+ hh->regexp = xstrdup(value);
+ hh->next = NULL;
+ *hunk_header_regexp_tail = hh;
+ return 0;
+}
+
+static const char *hunk_header_regexp(const char *ident)
+{
+ struct hunk_header_regexp *hh;
+
+ if (!hunk_header_regexp_tail) {
+ hunk_header_regexp_tail = &hunk_header_regexp_list;
+ git_config(hunk_header_config);
+ }
+ for (hh = hunk_header_regexp_list; hh; hh = hh->next)
+ if (!strcmp(ident, hh->name))
+ return hh->regexp;
+ return NULL;
+}
+
+static const char *diff_hunk_header_regexp(struct diff_filespec *one)
+{
+ const char *ident, *regexp;
+
+ diff_filespec_check_attr(one);
+ ident = one->hunk_header_ident;
+
+ if (!ident)
+ /*
+ * If the config file has "funcname.default" defined, that
+ * regexp is used; otherwise NULL is returned and xemit uses
+ * the built-in default.
+ */
+ return hunk_header_regexp("default");
+
+ /* Look up custom "funcname.$ident" regexp from config. */
+ regexp = hunk_header_regexp(ident);
+ if (regexp)
+ return regexp;
+
+ /*
+ * And define built-in fallback patterns here. Note that
+ * these can be overriden by the user's config settings.
+ */
+ if (!strcmp(ident, "java"))
+ return "!^[ ]*\\(catch\\|do\\|for\\|if\\|instanceof\\|"
+ "new\\|return\\|switch\\|throw\\|while\\)\n"
+ "^[ ]*\\(\\([ ]*"
+ "[A-Za-z_][A-Za-z_0-9]*\\)\\{2,\\}"
+ "[ ]*([^;]*$\\)";
+
+ return NULL;
+}
+