dmenu-highlight-20201211-fcdc159.diff 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. From fcdc1593ed418166f20b7e691a49b1e6eefc116e Mon Sep 17 00:00:00 2001
  2. From: Nathaniel Evan <nathanielevan@zohomail.com>
  3. Date: Fri, 11 Dec 2020 11:08:12 +0700
  4. Subject: [PATCH] Highlight matched text in a different color scheme
  5. ---
  6. config.def.h | 3 +++
  7. dmenu.c | 44 +++++++++++++++++++++++++++++++++++++++++---
  8. 2 files changed, 44 insertions(+), 3 deletions(-)
  9. diff --git a/config.def.h b/config.def.h
  10. index 1edb647..79be73a 100644
  11. --- a/config.def.h
  12. +++ b/config.def.h
  13. @@ -11,7 +11,10 @@ static const char *colors[SchemeLast][2] = {
  14. /* fg bg */
  15. [SchemeNorm] = { "#bbbbbb", "#222222" },
  16. [SchemeSel] = { "#eeeeee", "#005577" },
  17. + [SchemeSelHighlight] = { "#ffc978", "#005577" },
  18. + [SchemeNormHighlight] = { "#ffc978", "#222222" },
  19. [SchemeOut] = { "#000000", "#00ffff" },
  20. + [SchemeOutHighlight] = { "#ffc978", "#00ffff" },
  21. };
  22. /* -l option; if nonzero, dmenu uses vertical list with given number of lines */
  23. static unsigned int lines = 0;
  24. diff --git a/dmenu.c b/dmenu.c
  25. index 65f25ce..cce1ad1 100644
  26. --- a/dmenu.c
  27. +++ b/dmenu.c
  28. @@ -26,8 +26,7 @@
  29. #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
  30. /* enums */
  31. -enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */
  32. -
  33. +enum { SchemeNorm, SchemeSel, SchemeOut, SchemeNormHighlight, SchemeSelHighlight, SchemeOutHighlight, SchemeLast }; /* color schemes */
  34. struct item {
  35. char *text;
  36. struct item *left, *right;
  37. @@ -113,6 +112,43 @@ cistrstr(const char *s, const char *sub)
  38. return NULL;
  39. }
  40. +static void
  41. +drawhighlights(struct item *item, int x, int y, int maxw)
  42. +{
  43. + char restorechar, tokens[sizeof text], *highlight, *token;
  44. + int indentx, highlightlen;
  45. +
  46. + drw_setscheme(drw, scheme[item == sel ? SchemeSelHighlight : item->out ? SchemeOutHighlight : SchemeNormHighlight]);
  47. + strcpy(tokens, text);
  48. + for (token = strtok(tokens, " "); token; token = strtok(NULL, " ")) {
  49. + highlight = fstrstr(item->text, token);
  50. + while (highlight) {
  51. + // Move item str end, calc width for highlight indent, & restore
  52. + highlightlen = highlight - item->text;
  53. + restorechar = *highlight;
  54. + item->text[highlightlen] = '\0';
  55. + indentx = TEXTW(item->text);
  56. + item->text[highlightlen] = restorechar;
  57. +
  58. + // Move highlight str end, draw highlight, & restore
  59. + restorechar = highlight[strlen(token)];
  60. + highlight[strlen(token)] = '\0';
  61. + if (indentx - (lrpad / 2) - 1 < maxw)
  62. + drw_text(
  63. + drw,
  64. + x + indentx - (lrpad / 2) - 1,
  65. + y,
  66. + MIN(maxw - indentx, TEXTW(highlight) - lrpad),
  67. + bh, 0, highlight, 0
  68. + );
  69. + highlight[strlen(token)] = restorechar;
  70. +
  71. + if (strlen(highlight) - strlen(token) < strlen(token)) break;
  72. + highlight = fstrstr(highlight + strlen(token), token);
  73. + }
  74. + }
  75. +}
  76. +
  77. static int
  78. drawitem(struct item *item, int x, int y, int w)
  79. {
  80. @@ -123,7 +159,9 @@ drawitem(struct item *item, int x, int y, int w)
  81. else
  82. drw_setscheme(drw, scheme[SchemeNorm]);
  83. - return drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0);
  84. + int r = drw_text(drw, x, y, w, bh, lrpad / 2, item->text, 0);
  85. + drawhighlights(item, x, y, w);
  86. + return r;
  87. }
  88. static void
  89. --
  90. 2.29.2