diff options
-rw-r--r-- | pw.1 | 20 | ||||
-rw-r--r-- | pw.c | 34 |
2 files changed, 44 insertions, 10 deletions
@@ -191,7 +191,7 @@ and also terminate colon mode without executing a command. Colon commands are documented in the COLON COMMANDS section below. -.IP "[\fIpos\fP]\fB/\fP\fIpattern\fP, [\fIpos\fP]\fB?\fP\fIpattern\fP" +.IP "[\fIpos\fP]\fB/[!]\fP\fIpattern\fP, [\fIpos\fP]\fB?[!]\fP\fIpattern\fP" Set a trigger if a non-empty .I pattern is specified, or else cancel a trigger if an empty pattern is specified. @@ -285,6 +285,24 @@ expression (ERE) or (BRE) depending on the current setting. This is true even if it is recalled from a history entry which had been created under a different mode. +If the +.I pattern +is preceded by the character +.B ! +then it is logically inverted. The trigger will activate on lines which do +.I not +match the pattern. To write an uninverted pattern which begins with +.BR ! , +precede the +.B ! +with a backslash. This is not a regex character escape sequence, but part +of the trigger command syntax. It must not be used anywhere in a pattern, +other than immediately after the +.B / +or +.B ? +command character. + In trigger mode, the status string .B TRIG/ or @@ -314,7 +314,7 @@ static void drawstatus(unsigned hist, int columns, unsigned stat, char *cmd) ptr += snprintf(ptr, end - ptr, ", "); if (i > 0) ptr += snprintf(ptr, end - ptr, "[%d]", i + 1); - ptr += snprintf(ptr, end - ptr, "%s", gr->pat); + ptr += snprintf(ptr, end - ptr, "%s%s", gr->inv ? "!" : "", gr->pat); first = 0; } } @@ -758,9 +758,12 @@ int main(int argc, char **argv) int trig = 1; for (int i = 0; i < lim; i++) { grep *gr = triglist[i]; - if (gr && regexec(&gr->rx, circbuf[i], 0, NULL, 0) != 0) { - trig = 0; - break; + if (gr) { + int match = regexec(&gr->rx, circbuf[i], 0, NULL, 0) == 0; + if (match == gr->inv) { + trig = 0; + break; + } } } if (trig) @@ -769,9 +772,12 @@ int main(int argc, char **argv) int trig = 1; for (int j = nlines - 1, i = 0; j >= 0 && i < maxtrig; j--, i++) { grep *gr = triglist[i]; - if (gr && regexec(&gr->rx, circbuf[j], 0, NULL, 0) != 0) { - trig = 0; - break; + if (gr) { + int match = regexec(&gr->rx, circbuf[j], 0, NULL, 0) == 0; + if (match == gr->inv) { + trig = 0; + break; + } } } if (trig) @@ -1083,9 +1089,19 @@ int main(int argc, char **argv) } } else if (kbd_state == kbd_trig && (int) trig < maxlines && trig < maxtrig) { int err; - char *pat = dsdup(cmdbuf + 1); + char *rx = cmdbuf + 1; + int inv = 0; regex_t regex; + if (strncmp(rx, "\\!", 2) == 0) { + rx++; + } else if (rx[0] == '!') { + rx++; + inv = 1; + } + + char *pat = dsdup(rx); + if (*pat && (err = regcomp(®ex, pat, regex_flags | REG_NOSUB))) { regerror(err, ®ex, cmdbuf, sizeof cmdbuf); if (columns < (int) sizeof cmdbuf - 1) @@ -1121,7 +1137,7 @@ int main(int argc, char **argv) regfree(®ex); regcomp(&gr->rx, pat, regex_flags | REG_NOSUB); gr->pat = dsdup(pat); - gr->inv = 0; + gr->inv = inv; triglist[trig] = gr; } } |