zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: broader support for highlight groups
@ 2024-03-02  1:21 Oliver Kiddle
  2024-03-02  1:39 ` Bart Schaefer
  2024-03-02  7:57 ` PATCH: broader support for highlight groups Mikael Magnusson
  0 siblings, 2 replies; 7+ messages in thread
From: Oliver Kiddle @ 2024-03-02  1:21 UTC (permalink / raw)
  To: Zsh workers

This broadens the support for %H to completion explanation strings
(with and without complist) and WATCHFMT, both of which already support
%B/%S/%U/%F/%K. This also affects the list-prompt style as a
side-effect.

Completion for prompt strings also gains support for %H.

The return value from parsehighlight() has been changed to go one
past the end character because that's now convenient for more of the
callers than leaving it on the end character. The one change of int to
size_t in watch.c is to silence a (-Wextra) compiler warning.

We still lack a way to reset attributes. Rather than %r, which does
have tenuous conflicts with completion explanations and some obscure
uses of zformat, I'm tending towards special-casing %H{reset} and/or
%H{none}. And perhaps disabling the feature of allowing left-over
attributes in the prompt to bleed over into the user's input where those
attributes came from a highlight group. That feature only exists for
backward-compatibility and anyone using a new feature like highlight
groups can also set the default key in zle_highlight. Any opinions?

Oliver

diff --git a/Completion/Zsh/Type/_ps1234 b/Completion/Zsh/Type/_ps1234
index 07dea5905..e4391dc00 100644
--- a/Completion/Zsh/Type/_ps1234
+++ b/Completion/Zsh/Type/_ps1234
@@ -1,17 +1,18 @@
 #compdef -value-,PROMPT,-default- -value-,PROMPT2,-default- -value-,PROMPT3,-default- -value-,PROMPT4,-default- -value-,RPROMPT,-default- -value-,RPROMPT2,-default- -value-,PS1,-default- -value-,PS2,-default- -value-,PS3,-default- -value-,PS4,-default- -value-,RPS1,-default- -value-,RPS2,-default- -value-,SPROMPT,-default- -value-,PROMPT_EOL_MARK,-default-
 
-local -a specs ccol
-local expl grp cols bs suf pre changed=1 ret=1
+local -a specs ccol suf
+local expl grp cols bs pre changed=1 ret=1
 local -A ansi
 
 [[ -z $compstate[quote] ]] && bs='\'
+suf=( -S '' )
 
 # first strip off any complete prompt specifications leaving only the
 # current, incomplete, one
 while (( changed )); do
   changed=0
-  compset -P '%[DFK](\\|){[^}]#}' && changed=1 # formats with arg: %x{...}
-  compset -P '%[0-9-\\]#[^DFK(0-9-<>\\\[]' && changed=1 # normal formats
+  compset -P '%[DFHK](\\|){[^}]#}' && changed=1 # formats with arg: %x{...}
+  compset -P '%[0-9-\\]#[^DFHK(0-9-<>\\\[]' && changed=1 # normal formats
   compset -P '%[0-9-\\]#(<[^<]#<|>[^>]#>|\[[^\]]#\])' && changed=1 # truncations
   compset -P '%[0-9-\\]#(\\|)\([0-9-]#[^0-9]?|[^%]' && changed=1 # start of ternary
   compset -P '[^%]##' && changed=1 # sundry other characters
@@ -41,15 +42,15 @@ if compset -P '%[FK]'; then
   grp="$expl[expl[(i)-J]+1]"
   print -v ccol -f "($grp)=%s=%s" ${(kv)ansi}
   _comp_colors+=( $ccol )
-  compadd "$expl[@]" $suf $pre -k ansi && ret=0
-  if (( $#suf )) && compset -P "(<->|%v)"; then
+  compadd "$expl[@]" "$suf[@]" $pre -k ansi && ret=0
+  if [[ $ISUFFIX != (\\|)}* ]] && compset -P "(<->|%v)"; then
     _wanted ansi-colors expl 'closing brace' compadd -S '' \} && ret=0
   elif (( $+terminfo[colors] )); then
     (( cols = $terminfo[colors] - 1 ))
     (( cols = cols > 255 ? 255 : cols ))
     _description -V terminal-colors expl 'terminal color'
     grp="$expl[expl[(i)-J]+1]"
-    compadd "$expl[@]" $suf $pre {0..$cols}
+    compadd "$expl[@]" "$suf[@]" $pre {0..$cols}
     for c in {0..$cols}; do
       _comp_colors+=( "($grp)=${c}=${${${(%):-%F{$c\}}#?\[}%m}" )
     done
@@ -93,11 +94,14 @@ elif compset -P '%[0-9-\\]#(\\|)\([0-9-]#'; then
     'w:day of week (Sunday = 0)'
   )
   [[ $IPREFIX != *- ]] && _describe -t ternary-prompt-expressions \
-      'ternary prompt format test character' specs $suf && ret=0
+      'ternary prompt format test character' specs "$suf[@]" && ret=0
   _message -e numbers number
 elif compset -P '%D(\\|){'; then
   compset -S '(\\|)}*'
   _date_formats zsh && ret=0
+elif compset -P '%H(\\|){'; then
+  compset -S '(\\|)}*' || suf=( -S "$bs}" )
+  _wanted highlight-groups expl 'highlight group' compadd "$suf[@]" -k .zle.hlgroups && ret=0
 elif [[ -prefix '%' ]] ||
       ! zstyle -t ":completion:${curcontext}:prompt-format-specifiers" prefix-needed
 then
@@ -152,6 +156,7 @@ then
       'B:start bold'
       'b:stop bold'
       'E:clear to end of line'
+      'H{:use highlight group'
       'U:start underline'
       'u:stop underline'
       'S:start standout'
diff --git a/Doc/Zsh/compsys.yo b/Doc/Zsh/compsys.yo
index 3f708eb5a..f75298a1b 100644
--- a/Doc/Zsh/compsys.yo
+++ b/Doc/Zsh/compsys.yo
@@ -2023,8 +2023,8 @@ position shown as a percentage of the total length otherwise.  In each
 case the form with the uppercase letter will be replaced by a string of fixed
 width, padded to the  right with spaces, while the lowercase form will
 be replaced by a variable width string.  As in other prompt strings, the
-escape sequences `tt(%S)', `tt(%s)', `tt(%B)', `tt(%b)', `tt(%U)',
-`tt(%u)' for entering and leaving the display modes
+escape sequence `tt(%H)` along with `tt(%S)', `tt(%s)', `tt(%B)', `tt(%b)',
+`tt(%U)', `tt(%u)' for entering and leaving the display modes
 standout, bold and underline, and `tt(%F)', `tt(%f)', `tt(%K)', `tt(%k)' for
 changing the foreground background colour, are also available, as is the form
 `tt(%{)...tt(%})' for enclosing escape sequences which display with zero
diff --git a/Doc/Zsh/compwid.yo b/Doc/Zsh/compwid.yo
index 9461ace17..b0c9b0a5f 100644
--- a/Doc/Zsh/compwid.yo
+++ b/Doc/Zsh/compwid.yo
@@ -597,7 +597,8 @@ ifnzman((see noderef(Prompt Expansion)))\
 ifzman(as described in the section EXPANSION OF PROMPT SEQUENCES in
 zmanref(zshmisc)):
 `tt(%B)', `tt(%S)', `tt(%U)', `tt(%F)', `tt(%K)' and their lower case
-counterparts, as well as `tt(%{)...tt(%})'.  `tt(%F)', `tt(%K)' and
+counterparts, as well as `tt(%H)' and `tt(%{)...tt(%})'.  `tt(%F)',
+`tt(%K)', `tt(%H)' and
 `tt(%{)...tt(%})' take arguments in the same form as prompt
 expansion.  (Note that the sequence `tt(%G)' is not available; an
 argument to `tt(%{)' should be used instead.)  The sequence `tt(%%)'
diff --git a/Src/Modules/watch.c b/Src/Modules/watch.c
index 97d4fa608..ba17cf940 100644
--- a/Src/Modules/watch.c
+++ b/Src/Modules/watch.c
@@ -373,6 +373,13 @@ watchlog2(int inout, WATCH_STRUCT_UTMP *u, char *fmt, int prnt, int fini)
 		case 'f':
 		    tunsetattrs(TXTFGCOLOUR);
 		    break;
+		case 'H':
+		    if (*fmt == '{') {
+			fmt = parsehighlight(fmt + 1, '}', &atr);
+			if (atr && atr != TXT_ERROR)
+			    treplaceattrs(atr);
+		    }
+		    break;
 		case 'K':
 		    if (*fmt == '{') {
 			fmt++;
@@ -428,7 +435,7 @@ watchlog_match(char *teststr, char *actual, size_t buflen)
     int ret = 0;
     Patprog pprog;
     char *str = dupstring(teststr);
-    int len = strnlen(actual, buflen);
+    size_t len = strnlen(actual, buflen);
     char *user = metafy(actual, len,
 	    len == buflen ? META_HEAPDUP : META_USEHEAP);
 
diff --git a/Src/Zle/complist.c b/Src/Zle/complist.c
index 9cb89a60d..5619160a9 100644
--- a/Src/Zle/complist.c
+++ b/Src/Zle/complist.c
@@ -1181,6 +1181,13 @@ compprintfmt(char *fmt, int n, int dopr, int doesc, int ml, int *stop)
 		    if (dopr)
 			tunsetattrs(TXTBGCOLOUR);
 		    break;
+		case ZWC('H'):
+		    if (*p == '{') {
+			p = parsehighlight(p + 1, '}', &atr);
+			if (atr != TXT_ERROR && dopr)
+			    treplaceattrs(atr);
+		    }
+		    break;
 		case ZWC('{'):
 		    if (arg)
 			cc += arg;
diff --git a/Src/Zle/zle_tricky.c b/Src/Zle/zle_tricky.c
index 225ce8c74..aa3c71bc2 100644
--- a/Src/Zle/zle_tricky.c
+++ b/Src/Zle/zle_tricky.c
@@ -2501,6 +2501,14 @@ printfmt(char *fmt, int n, int dopr, int doesc)
 		case 'k':
 		    tunsetattrs(TXTBGCOLOUR);
 		    break;
+		case 'H':
+		    if (p[1] == '{') {
+			p = parsehighlight(p + 2, '}', &atr);
+			--p;
+			if (atr != TXT_ERROR)
+			    treplaceattrs(atr);
+		    }
+		    break;
 		case '{':
 		    if (arg)
 			cc += arg;
diff --git a/Src/prompt.c b/Src/prompt.c
index 7acbe0e47..e10b05215 100644
--- a/Src/prompt.c
+++ b/Src/prompt.c
@@ -270,7 +270,8 @@ zattrescape(zattr atr, int *len)
 }
 
 /* Parse the argument for %H */
-static char *
+/**/
+mod_export char *
 parsehighlight(char *arg, char endchar, zattr *atr)
 {
     static int entered = 0;
@@ -295,9 +296,9 @@ parsehighlight(char *arg, char endchar, zattr *atr)
     } else
 	*atr = TXT_ERROR;
     if (ep)
-	*ep = endchar;
+	*ep++ = endchar;
     else
-	ep = strchr(arg, '\0') - 1;
+	ep = strchr(arg, '\0');
     entered = 0;
     return ep;
 }
@@ -635,6 +636,7 @@ putpromptchar(int doprint, int endchar)
 	    case 'H':
 		if (bv->fm[1] == '{') {
 		    bv->fm = parsehighlight(bv->fm + 2, '}', &atr);
+		    --bv->fm;
 		    if (atr != TXT_ERROR) {
 			treplaceattrs(atr);
 			applytextattributes(TSC_PROMPT);


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-03-30 19:15 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-02  1:21 PATCH: broader support for highlight groups Oliver Kiddle
2024-03-02  1:39 ` Bart Schaefer
2024-03-30  1:06   ` sticky-note and prompt color leftovers (Re: PATCH: broader support for highlight groups) Oliver Kiddle
2024-03-30  3:16     ` Bart Schaefer
2024-03-30 12:23       ` Oliver Kiddle
2024-03-30 19:14         ` Bart Schaefer
2024-03-02  7:57 ` PATCH: broader support for highlight groups Mikael Magnusson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).