Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.9 KiB

  1. From e9e1691a9b4ffa2db45cd5108b76b9a68610ba37 Mon Sep 17 00:00:00 2001
  2. From: takase1121 <takase1121@outlook.com>
  3. Date: Sun, 17 May 2020 07:05:32 +0800
  4. Subject: [PATCH] Adds high-priority items.
  5. This allows users to specify a list of high priority commands that should be
  6. displayed before other matches with the following arrangement:
  7. match -> prefix && high priority -> prefix -> substring
  8. As you can see, high priority for substring matches is not implemented or will not
  9. be implemented depending on needs.
  10. ---
  11. config.def.h | 1 +
  12. dmenu.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++-----
  13. 2 files changed, 59 insertions(+), 6 deletions(-)
  14. diff --git a/config.def.h b/config.def.h
  15. index 1edb647..65e03fb 100644
  16. --- a/config.def.h
  17. +++ b/config.def.h
  18. @@ -12,6 +12,7 @@ static const char *colors[SchemeLast][2] = {
  19. [SchemeNorm] = { "#bbbbbb", "#222222" },
  20. [SchemeSel] = { "#eeeeee", "#005577" },
  21. [SchemeOut] = { "#000000", "#00ffff" },
  22. + [SchemeHp] = { "#bbbbbb", "#333333" }
  23. };
  24. /* -l option; if nonzero, dmenu uses vertical list with given number of lines */
  25. static unsigned int lines = 0;
  26. diff --git a/dmenu.c b/dmenu.c
  27. index 6b8f51b..7bf4099 100644
  28. --- a/dmenu.c
  29. +++ b/dmenu.c
  30. @@ -26,14 +26,16 @@
  31. #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
  32. /* enums */
  33. -enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */
  34. +enum { SchemeNorm, SchemeSel, SchemeHp, SchemeOut, SchemeLast }; /* color schemes */
  35. struct item {
  36. char *text;
  37. struct item *left, *right;
  38. - int out;
  39. + int out, hp;
  40. };
  41. +static char **hpitems = NULL;
  42. +static int hplength = 0;
  43. static char text[BUFSIZ] = "";
  44. static char *embed;
  45. static int bh, mw, mh;
  46. @@ -58,6 +60,36 @@ static Clr *scheme[SchemeLast];
  47. static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
  48. static char *(*fstrstr)(const char *, const char *) = strstr;
  49. +static char**
  50. +tokenize(char *source, const char *delim, int *llen) {
  51. + int listlength = 0;
  52. + char **list = malloc(1 * sizeof(char*));
  53. + char *token = strtok(source, delim);
  54. +
  55. + while (token) {
  56. + if (!(list = realloc(list, sizeof(char*) * (listlength + 1))))
  57. + die("Unable to realloc %d bytes\n", sizeof(char*) * (listlength + 1));
  58. + if (!(list[listlength] = strdup(token)))
  59. + die("Unable to strdup %d bytes\n", strlen(token) + 1);
  60. + token = strtok(NULL, delim);
  61. + listlength++;
  62. + }
  63. +
  64. + *llen = listlength;
  65. + return list;
  66. +}
  67. +
  68. +static int
  69. +arrayhas(char **list, int length, char *item) {
  70. + for (int i = 0; i < length; i++) {
  71. + int len1 = strlen(list[i]);
  72. + int len2 = strlen(item);
  73. + if (fstrncmp(list[i], item, len1 > len2 ? len2 : len1) == 0)
  74. + return 1;
  75. + }
  76. + return 0;
  77. +}
  78. +
  79. static void
  80. appenditem(struct item *item, struct item **list, struct item **last)
  81. {
  82. @@ -118,6 +150,8 @@ drawitem(struct item *item, int x, int y, int w)
  83. {
  84. if (item == sel)
  85. drw_setscheme(drw, scheme[SchemeSel]);
  86. + else if (item->hp)
  87. + drw_setscheme(drw, scheme[SchemeHp]);
  88. else if (item->out)
  89. drw_setscheme(drw, scheme[SchemeOut]);
  90. else
  91. @@ -219,7 +253,7 @@ match(void)
  92. char buf[sizeof text], *s;
  93. int i, tokc = 0;
  94. size_t len, textsize;
  95. - struct item *item, *lprefix, *lsubstr, *prefixend, *substrend;
  96. + struct item *item, *lhpprefix, *lprefix, *lsubstr, *hpprefixend, *prefixend, *substrend;
  97. strcpy(buf, text);
  98. /* separate input text into tokens to be matched individually */
  99. @@ -228,7 +262,7 @@ match(void)
  100. die("cannot realloc %u bytes:", tokn * sizeof *tokv);
  101. len = tokc ? strlen(tokv[0]) : 0;
  102. - matches = lprefix = lsubstr = matchend = prefixend = substrend = NULL;
  103. + matches = lhpprefix = lprefix = lsubstr = matchend = hpprefixend = prefixend = substrend = NULL;
  104. textsize = strlen(text) + 1;
  105. for (item = items; item && item->text; item++) {
  106. for (i = 0; i < tokc; i++)
  107. @@ -236,14 +270,24 @@ match(void)
  108. break;
  109. if (i != tokc) /* not all tokens match */
  110. continue;
  111. - /* exact matches go first, then prefixes, then substrings */
  112. + /* exact matches go first, then prefixes with high priority, then prefixes, then substrings */
  113. if (!tokc || !fstrncmp(text, item->text, textsize))
  114. appenditem(item, &matches, &matchend);
  115. + else if (item->hp && !fstrncmp(tokv[0], item->text, len))
  116. + appenditem(item, &lhpprefix, &hpprefixend);
  117. else if (!fstrncmp(tokv[0], item->text, len))
  118. appenditem(item, &lprefix, &prefixend);
  119. else
  120. appenditem(item, &lsubstr, &substrend);
  121. }
  122. + if (lhpprefix) {
  123. + if (matches) {
  124. + matchend->right = lhpprefix;
  125. + lhpprefix->left = matchend;
  126. + } else
  127. + matches = lhpprefix;
  128. + matchend = hpprefixend;
  129. + }
  130. if (lprefix) {
  131. if (matches) {
  132. matchend->right = lprefix;
  133. @@ -535,6 +579,7 @@ readstdin(void)
  134. if (!(items[i].text = strdup(buf)))
  135. die("cannot strdup %u bytes:", strlen(buf) + 1);
  136. items[i].out = 0;
  137. + items[i].hp = arrayhas(hpitems, hplength, items[i].text);
  138. drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
  139. if (tmpmax > inputw) {
  140. inputw = tmpmax;
  141. @@ -683,7 +728,8 @@ static void
  142. usage(void)
  143. {
  144. fputs("usage: dmenu [-bfiv] [-l lines] [-p prompt] [-fn font] [-m monitor]\n"
  145. - " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n", stderr);
  146. + " [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]\n"
  147. + " [-hb color] [-hf color] [-hp items]\n", stderr);
  148. exit(1);
  149. }
  150. @@ -724,8 +770,14 @@ main(int argc, char *argv[])
  151. colors[SchemeSel][ColBg] = argv[++i];
  152. else if (!strcmp(argv[i], "-sf")) /* selected foreground color */
  153. colors[SchemeSel][ColFg] = argv[++i];
  154. + else if (!strcmp(argv[i], "-hb")) /* high priority background color */
  155. + colors[SchemeHp][ColBg] = argv[++i];
  156. + else if (!strcmp(argv[i], "-hf")) /* low priority background color */
  157. + colors[SchemeHp][ColFg] = argv[++i];
  158. else if (!strcmp(argv[i], "-w")) /* embedding window id */
  159. embed = argv[++i];
  160. + else if (!strcmp(argv[i], "-hp"))
  161. + hpitems = tokenize(argv[++i], ",", &hplength);
  162. else
  163. usage();
  164. --
  165. 2.26.2