dmenu-rejectnomatch-4.7.diff - sites - public wiki contents of suckless.org
 (HTM) git clone git://git.suckless.org/sites
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       dmenu-rejectnomatch-4.7.diff (2358B)
       ---
            1 diff --git a/dmenu.1 b/dmenu.1
            2 index 9eab758..61084ab 100644
            3 --- a/dmenu.1
            4 +++ b/dmenu.1
            5 @@ -3,7 +3,7 @@
            6  dmenu \- dynamic menu
            7  .SH SYNOPSIS
            8  .B dmenu
            9 -.RB [ \-bfiv ]
           10 +.RB [ \-bfirv ]
           11  .RB [ \-l
           12  .IR lines ]
           13  .RB [ \-m
           14 @@ -47,6 +47,9 @@ X until stdin reaches end\-of\-file.
           15  .B \-i
           16  dmenu matches menu items case insensitively.
           17  .TP
           18 +.B \-r
           19 +dmenu will reject any input which would result in no matching option left.
           20 +.TP
           21  .BI \-l " lines"
           22  dmenu lists items vertically, with the given number of lines.
           23  .TP
           24 diff --git a/dmenu.c b/dmenu.c
           25 index d605ab4..7505278 100644
           26 --- a/dmenu.c
           27 +++ b/dmenu.c
           28 @@ -38,6 +38,7 @@ static char *embed;
           29  static int bh, mw, mh;
           30  static int inputw = 0, promptw;
           31  static int lrpad; /* sum of left and right padding */
           32 +static int reject_no_match = 0;
           33  static size_t cursor;
           34  static struct item *items = NULL;
           35  static struct item *matches, *matchend;
           36 @@ -268,12 +269,26 @@ insert(const char *str, ssize_t n)
           37  {
           38          if (strlen(text) + n > sizeof text - 1)
           39                  return;
           40 +
           41 +        static char last[BUFSIZ] = "";
           42 +        if(reject_no_match) {
           43 +                /* store last text value in case we need to revert it */
           44 +                memcpy(last, text, BUFSIZ);
           45 +        }
           46 +
           47          /* move existing text out of the way, insert new text, and update cursor */
           48          memmove(&text[cursor + n], &text[cursor], sizeof text - cursor - MAX(n, 0));
           49          if (n > 0)
           50                  memcpy(&text[cursor], str, n);
           51          cursor += n;
           52          match();
           53 +
           54 +        if(!matches && reject_no_match) {
           55 +                /* revert to last text value if theres no match */
           56 +                memcpy(text, last, BUFSIZ);
           57 +                cursor -= n;
           58 +                match();
           59 +        }
           60  }
           61  
           62  static size_t
           63 @@ -636,7 +651,7 @@ setup(void)
           64  static void
           65  usage(void)
           66  {
           67 -        fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
           68 +        fputs("usage: dmenu [-bfirv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
           69                "             [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
           70          exit(1);
           71  }
           72 @@ -659,7 +674,9 @@ main(int argc, char *argv[])
           73                  else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
           74                          fstrncmp = strncasecmp;
           75                          fstrstr = cistrstr;
           76 -                } else if (i + 1 == argc)
           77 +                } else if (!strcmp(argv[i], "-r")) /* reject input which results in no match */
           78 +                        reject_no_match = 1;
           79 +                else if (i + 1 == argc)
           80                          usage();
           81                  /* these options take one argument */
           82                  else if (!strcmp(argv[i], "-l"))   /* number of lines in vertical list */