Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Evaluating parameters in general-purpose widgets



zsh-syntax-highlighting is designed to run as a zle-line-pre-redraw widget.

Is it safe for zsh-syntax-highlighting to evaluate parameter expansions
in the input?

Things like «${:-`foo`}» and «${foo:=bar}» clearly may have side effects
and are not evaluated by z-sy-h under any circumstances.  However, what
about simple expressions such as «${foo}»?  Even those can have side
effects when $foo is provided by a module:

[[[
diff --git a/Src/Modules/parameter.c b/Src/Modules/parameter.c
index 10c47d214..789d262fe 100644
--- a/Src/Modules/parameter.c
+++ b/Src/Modules/parameter.c
@@ -1821,6 +1821,24 @@ setpmdissaliases(Param pm, HashTable ht)
     setaliases(sufaliastab, pm, ht, ALIAS_SUFFIX|DISABLED);
 }
 
+/* autoincrement */
+
+static zlong autoincrement = 0;
+
+/**/
+static zlong
+autoincrementgetfn(UNUSED(Param pm))
+{
+    return ++autoincrement;
+}
+
+/**/
+static void
+autoincrementsetfn(UNUSED(Param pm), zlong value)
+{
+    autoincrement = value;
+}
+
 static const struct gsu_scalar pmralias_gsu =
 { strgetfn, setpmralias, unsetpmalias };
 static const struct gsu_scalar pmgalias_gsu =
@@ -2168,6 +2186,8 @@ static const struct gsu_hash pmdisgaliases_gsu =
 { hashgetfn, setpmdisgaliases, stdunsetfn };
 static const struct gsu_hash pmdissaliases_gsu =
 { hashgetfn, setpmdissaliases, stdunsetfn };
+static const struct gsu_integer pmautoincrement_gsu =
+{ autoincrementgetfn, autoincrementsetfn, stdunsetfn };
 
 static const struct gsu_array funcstack_gsu =
 { funcstackgetfn, arrsetfn, stdunsetfn };
@@ -2254,7 +2274,9 @@ static struct paramdef partab[] = {
     SPECIALPMDEF("userdirs", PM_READONLY,
 	    NULL, getpmuserdir, scanpmuserdirs),
     SPECIALPMDEF("usergroups", PM_READONLY,
-	    NULL, getpmusergroups, scanpmusergroups)
+	    NULL, getpmusergroups, scanpmusergroups),
+    SPECIALPMDEF("autoincrement", PM_INTEGER,
+	    &pmautoincrement_gsu, NULL, NULL)
 };
 
 static struct features module_features = {
% 
% zmodload zsh/parameter
% print $autoincrement $autoincrement
1 2
% 
]]]

So...

1. Suppose [[ $BUFFER == 'echo $foobar' ]], is it safe for a
general-purpose widget (= z-sy-h) to evaluate $foobar?

2. Likewise, after checking that «[[ ${parameters[foobar]} != *special* ]]»?

Cheers,

Daniel

P.S. Currently, the only case in which z-sy-h expands parameters is when
they appear at command position, in order to know what the effective
command word (after expansions) is.

P.P.S. I wasn't going to commit the above, and it has some quirks (for
example, evaluating ${+autoincrement} increments the parameter), but if
there's interest let me know.



Messages sorted by: Reverse Date, Date, Thread, Author