Skip to content

Commit

Permalink
Merge pull request #82 from erikarn/20240514_add_attrib_match
Browse files Browse the repository at this point in the history
[macro] Add support for whole line attribute matching.
  • Loading branch information
ingwarsw authored May 14, 2024
2 parents e9773ab + 4bd558f commit eef2560
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/tf/tf-help
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,14 @@ addworld()
matched by the trigger or to display the default message of a hook.
Default: normal. See: attributes.

#/def -A
#-A
-A[ngGLAurBbhC]
Limit matching on whole-line attribute(s) (normal, gag, nohistory,
nolog, noactivity, underline, reverse, bold, bell, hilite, Color).
This should only be used with a trigger (-t).
Default: none. See: attributes.

#/def -P
#-P
-P[<part>]<attr>[;[<part>]<attr>]...
Expand Down
39 changes: 37 additions & 2 deletions src/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ struct Macro {
struct World *world; /* only trig on text from world */
int pri, num;
attr_t attr;
attr_t match_attr;
int check_match_attr;
int nsubattr;
subattr_t *subattr;
short prob, shots, invis;
Expand Down Expand Up @@ -223,14 +225,16 @@ static Macro *macro_spec(String *args, int offset, int *xmflag, ListOpts *listop
VEC_ZERO(&spec->hook);
spec->invis = 0;
spec->attr = 0;
spec->match_attr = 0;
spec->check_match_attr = 0;
spec->nsubattr = 0;
spec->subattr = NULL;
spec->flags = MACRO_TEMP;
spec->builtin = NULL;
spec->used[USED_NAME] = spec->used[USED_TRIG] =
spec->used[USED_HOOK] = spec->used[USED_KEY] = 0;

startopt(CS(args), "usSp#c#b:B:E:t:w:h:a:f:P:T:FiIn#1m:q" +
startopt(CS(args), "usSp#c#b:B:E:t:w:h:A:a:f:P:T:FiIn#1m:q" +
(listopts ? 0 : 3));
while (!error && (opt = nextopt(&ptr, &uval, NULL, &offset))) {
switch (opt) {
Expand Down Expand Up @@ -316,6 +320,11 @@ static Macro *macro_spec(String *args, int offset, int *xmflag, ListOpts *listop
error = !parse_attrs(ptr, &attrs, 0);
spec->attr = adj_attr(spec->attr, attrs);
break;
case 'A':
error = !parse_attrs(ptr, &attrs, 0);
spec->match_attr = adj_attr(spec->match_attr, attrs);
spec->check_match_attr = 1;
break;
case 'P':
if ((error = (spec->nsubattr > 0))) {
eprintf("-P can be given only once per macro.");
Expand Down Expand Up @@ -1240,6 +1249,9 @@ static conString *print_def(TFILE *file, String *buffer, Macro *p)
if (p->attr) {
Stringcat(attr2str(Stringcat(buffer, "-a"), p->attr), " ");
}
if (p->check_match_attr) {
Stringcat(attr2str(Stringcat(buffer, "-A"), p->match_attr), " ");
}
if (p->nsubattr > 0) {
int i;
mflag = MATCH_REGEXP;
Expand Down Expand Up @@ -1577,6 +1589,19 @@ int do_hook(int hooknum, const char *fmt, const char *argfmt, ...)
return ran;
}

static int
match_pattern_and_attrib_checks(String *text, Pattern *pattern, Macro *macro)
{
/* If there's no check_match_attr, just match on pattern */
if (macro->check_match_attr == 0) {
return patmatch(pattern, CS(text), NULL);
}

/* If there's check_match_attr, match on pattern and attribute */
return (text->attrs == macro->match_attr)
&& patmatch(pattern, CS(text), NULL);
}

/* Find and run one or more matches for a hook or trig.
* text is text to be matched; if NULL, *linep is used.
* If %Pn subs are to be allowed, text should be NULL.
Expand Down Expand Up @@ -1675,7 +1700,17 @@ int find_and_run_matches(String *text, int hooknum, String **linep,
if (!expr_condition) continue;
}
pattern = hooknum>=0 ? &macro->hargs : &macro->trig;
if ((hooknum>=0 && !macro->hargs.str) || patmatch(pattern, CS(text), NULL))
/* [adrian] is this it running a trigger? */
/* text->charattrs has attributes for each character */

/*
* we would need to check it against those, or convert the string via
* encode_attr() to run the match against.
*
* Also note: text->attrs is the whole-line attribute, text->charattrs
* is the per-character attributes.
*/
if ((hooknum>=0 && !macro->hargs.str) || match_pattern_and_attrib_checks(text, pattern, macro))
{
if (exec_list_long == 0) {
if (macro->fallthru) {
Expand Down
1 change: 1 addition & 0 deletions src/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -2908,6 +2908,7 @@ static void handle_socket_lines(void)

} else {
if (borg || hilite || gag) {
/* Run the trigger matches against this incoming text line */
if (find_and_run_matches(NULL, -1, &incoming_text, xworld(),
TRUE, 0))
{
Expand Down

0 comments on commit eef2560

Please sign in to comment.