zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] allopt() and question
@ 2016-05-06  0:17 Thilo Six
  2016-05-06  6:03 ` Sebastian Gniazdowski
  0 siblings, 1 reply; 4+ messages in thread
From: Thilo Six @ 2016-05-06  0:17 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 925 bytes --]

Hello

1)
i looked at allopt() and while doing that i noticed that it would not give
correct results for some options. notable e.g. monitor and zle.

Here i get:
% builtin set -o
.
.
monitor on
.
.
zle on

but then:
% builtin set -o | command grep -E '(\<monitor\>|\<zle\>)'
monitor off
zle off

>From that I guess that Zsh resets some specific options when used in
pipes/command substitutions.
The attached allopt() works around that.

Additionally:
This version makes it possible to throw s.th. like '_H-uP' or 'NO__H-uP' at
allopt and does not hardcode length of options when printing.
KUDOS for that goes to pws: http://zsh.sourceforge.net/Guide/zshguide05.html


2)
While working on the above i noticed that 'builtin set -o' and '${options}' are
not equal. The later knows some more. See attached missing_options for a list.
This makes me wonder if that is intended.


Any comments are welcome.



kind regards,

Thilo


[-- Attachment #2: 0001-allopt-updated.patch --]
[-- Type: text/x-diff, Size: 2891 bytes --]

>From 24207db8f420e65ba4e98875ee97c398135bb9b2 Mon Sep 17 00:00:00 2001
From: Thilo Six <zsh@xk2c.de>
Date: Fri, 6 May 2016 01:34:29 +0200
Subject: [PATCH] allopt() 

* FIX: some options are reset inside pipes and command substition         
       e.g. monitor and zle         
       those were displayed with wrong state 
* FIX: do not hardcode length for print out      
       thanks to pws: http://zsh.sourceforge.net/Guide/zshguide05.html  
* MOD: make it possible to throw s.th. like '_H-uP' at allopt

---
 Functions/Misc/allopt | 56 +++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 41 insertions(+), 15 deletions(-)

diff --git a/Functions/Misc/allopt b/Functions/Misc/allopt
index 0c521f3..3955b88 100644
--- a/Functions/Misc/allopt
+++ b/Functions/Misc/allopt
@@ -6,20 +6,46 @@
 # via egrep.
 #
 # Written by Sweth Chandramouli with hacks by Bart Schaefer.
+# updated by Thilo Six
 
-listalloptions () {
-   local OPT_NAME OPT_VALUE
-   builtin set -o | while read OPT_NAME OPT_VALUE ; do
-      if [[ ${OPT_NAME#no} != ${OPT_NAME} ]] ; then
-	 OPT_VALUE=${(L)${${OPT_VALUE:s/on/OFF}:s/off/on}}
-	 OPT_NAME=${OPT_NAME#no}
-      fi
-      echo "${(r:21:)OPT_NAME} ${OPT_VALUE}"
-   done
-}
+# allopt() {
+    local OPT_NAME OPT_VALUE LENGTH i OUT
+    local -a OPT_NAMES_ALL
+    local -A OPT_ALL
+    OPT_NAMES_ALL=(${(o)${(k)options[@]}})
+
+    # http://zsh.sourceforge.net/Guide/zshguide05.html
+    LENGTH=$(( ${#${OPT_NAMES_ALL[(r)${(l.${#${(O@)OPT_NAMES_ALL//?/X}[1]}..?.)}]}} + 1 ))
+
+    ### XXX note:
+    #   this needs to be done because otherwise
+    #   the listing is inaccurat e.g. 'monitor' and 'zle' are always off
+    #   the set builtin treats them special within a pipe. compare this:
+    #   % builtin set -o
+    #       with
+    #   % builtin set -o | command grep -E '(\<monitor\>|\<zle\>)'
+    #
+    for i in ${OPT_NAMES_ALL} ; do
+        OPT_ALL+=($i ${options[$i]})
+    done
+
+    if [[ -n ${*} ]]; then
+        {
+        for i in ${OPT_NAMES_ALL} ; do echo "${i} ${OPT_ALL[$i]}" ; done | while read OPT_NAME OPT_VALUE ; do
+            if [[ ${OPT_NAME#no} != ${OPT_NAME} ]] ; then
+                OPT_VALUE=${(L)${${OPT_VALUE:s/on/OFF}:s/off/on}}
+                OPT_NAME=${OPT_NAME#no}
+            fi
+            echo "${(r:${LENGTH}:)OPT_NAME} ${OPT_VALUE}"
+        done } | egrep ${(j.|.)${${${(L)@}#no}//[-_]/}}
+    else
+        for i in ${OPT_NAMES_ALL} ; do echo "${i} ${OPT_ALL[$i]}" ; done | while read OPT_NAME OPT_VALUE ; do
+            if [[ ${OPT_NAME#no} != ${OPT_NAME} ]] ; then
+                OPT_VALUE=${(L)${${OPT_VALUE:s/on/OFF}:s/off/on}}
+                OPT_NAME=${OPT_NAME#no}
+            fi
+            echo "${(r:${LENGTH}:)OPT_NAME} ${OPT_VALUE}"
+        done
+    fi
+# }
 
-if [[ -n $@ ]]; then
-    listalloptions | egrep "${(j.|.)@}"
-else
-    listalloptions
-fi
-- 
2.8.1


[-- Attachment #3: missing_options --]
[-- Type: text/plain, Size: 1223 bytes --]

--- set	2016-05-05 22:41:01.202092134 +0000
+++ allopt	2016-05-05 22:40:06.585093869 +0000
@@ -21,8 +21,9 @@
 bashrematch
 beep
 bgnice
 braceccl
+braceexpand
 bsdecho
 caseglob
 casematch
 cbases
@@ -43,8 +44,9 @@
 cshjunkiequotes
 cshnullcmd
 cshnullglob
 debugbeforecmd
+dotglob
 dvorak
 emacs
 equals
 errexit
@@ -63,14 +65,17 @@
 globcomplete
 globdots
 globstarshort
 globsubst
+hashall
 hashcmds
 hashdirs
 hashexecutablesonly
 hashlistall
 histallowclobber
+histappend
 histbeep
+histexpand
 histexpiredupsfirst
 histfcntllock
 histfindnodups
 histignorealldups
@@ -106,11 +111,13 @@
 localloops
 localoptions
 localpatterns
 localtraps
+log
 login
 longlistjobs
 magicequalsubst
+mailwarn
 mailwarning
 markdirs
 match
 menucomplete
@@ -120,11 +127,13 @@
 multios
 nullglob
 numericglobsort
 octalzeroes
+onecmd
 overstrike
 pathdirs
 pathscript
+physical
 pipefail
 posixaliases
 posixargzero
 posixbuiltins
@@ -140,8 +149,9 @@
 promptcr
 promptpercent
 promptsp
 promptsubst
+promptvars
 pushdignoredups
 pushdminus
 pushdsilent
 pushdtohome
@@ -163,10 +173,12 @@
 shwordsplit
 singlecommand
 singlelinezle
 sourcetrace
+stdin
 sunkeyboardhack
 tify
+trackall
 transientrprompt
 trapsasync
 typesetsilent
 unset

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

* Re: [PATCH] allopt() and question
  2016-05-06  0:17 [PATCH] allopt() and question Thilo Six
@ 2016-05-06  6:03 ` Sebastian Gniazdowski
  2016-05-06  9:52   ` Thilo Six
  0 siblings, 1 reply; 4+ messages in thread
From: Sebastian Gniazdowski @ 2016-05-06  6:03 UTC (permalink / raw)
  To: Thilo Six; +Cc: Zsh hackers list

On 6 May 2016 at 02:17, Thilo Six <zsh@xk2c.de> wrote:
> but then:
> % builtin set -o | command grep -E '(\<monitor\>|\<zle\>)'
> monitor off
> zle off

This will start separate process for the first thing in pipeline. The
last thing is run in current shell. Which is cool for me, as I was
able to write _function_ n-panelize that works in current shell even
when is used as ls | n-panelize (the function list entries and allows
user to search in them).

Best regards,
Sebastian Gniazdowski


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

* Re: [PATCH] allopt() and question
  2016-05-06  6:03 ` Sebastian Gniazdowski
@ 2016-05-06  9:52   ` Thilo Six
  2016-05-10 18:55     ` Thilo Six
  0 siblings, 1 reply; 4+ messages in thread
From: Thilo Six @ 2016-05-06  9:52 UTC (permalink / raw)
  To: zsh-workers

Hello Sebastian,

Sebastian Gniazdowski schrieb/wrote:

>> % builtin set -o | command grep -E '(\<monitor\>|\<zle\>)'
>> monitor off
>> zle off
> 
> This will start separate process for the first thing in pipeline. The
> last thing is run in current shell. Which is cool for me, 

Now that you mention it i can remeber to have read about that. Back then a bash
user where this is not the default. But iirc they have nowadays a option for it.
Still to me this seem not to be the whole story.
For one it only occurs with only some specific shell options and second:

% foo="$(builtin set -o)"
% echo $foo | command grep -E '(\<monitor\>|\<zle\>)'
monitor               off
zle                   off
% allopt '(\<monitor\>|\<zle\>)'
monitor               on
zle                   on

Or am i missing s.th.?

> as I was
> able to write _function_ n-panelize that works in current shell even
> when is used as ls | n-panelize (the function list entries and allows
> user to search in them).

I use sentaku for that: https://github.com/rcmdnk/sentaku


kind regards,

     Thilo





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

* Re: [PATCH] allopt() and question
  2016-05-06  9:52   ` Thilo Six
@ 2016-05-10 18:55     ` Thilo Six
  0 siblings, 0 replies; 4+ messages in thread
From: Thilo Six @ 2016-05-10 18:55 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 3001 bytes --]

Hello

>>> % builtin set -o | command grep -E '(\<monitor\>|\<zle\>)'
>>> monitor off
>>> zle off
>>
>> This will start separate process for the first thing in pipeline. The
>> last thing is run in current shell. Which is cool for me, 
> 
> Now that you mention it i can remeber to have read about that. Back then a bash
> user where this is not the default. But iirc they have nowadays a option for it.
> Still to me this seem not to be the whole story.
> For one it only occurs with only some specific shell options and second:
> 
> % foo="$(builtin set -o)"
> % echo $foo | command grep -E '(\<monitor\>|\<zle\>)'
> monitor               off
> zle                   off
> % allopt '(\<monitor\>|\<zle\>)'
> monitor               on
> zle                   on
> 
> Or am i missing s.th.?

i have been working on making the patch better. During that i noticed
this does work as expected:
    for i in ${OPT_NAMES_ALL} ; do OPT_ALL+=($i ${options[$i]}) ; done

This does not:
    for i in ${OPT_NAMES_ALL} ; do OPT_ALL+=($i ${options[$i]}) ; done | while

So it seems as i allready thought that Zsh is reseting some options when inside
a pipe. Same for 'set -o' and 'setopt' by the way.

Now to the "improvements"-
Meantime i have learned about v and k modifiers which saves a for loop.

The main point i did not like about the previous version was code duplication.
The original version of allopt used a function for that.

There are two reasons why i did went the other approach first:
1)
I often fire up a new instance enter some chars and hit <Tab> just to check if
that command exists already. Mostly in cases where i write new functions to
prevent name clashes.
Function definitions inside functions are hidden from the shell unless the outer
function is run once. So this potentially calls then for name clashes.

2)
One thing i haven't verified yet is, if a local PARAMATER in the outer function
is avail to the inner defined function.
If so that would be nice. If Not that would have reduced code duplication at the
end and doubled it at start of the function where the PARAMETERs are defined.

Actually there is way to have both, or better yet one without the other.
See attached.


All of this is TTBOMK @ $EPOCHSECONDS.


Final: I attach a plain autoload func file. If you are actually interested i
produce a proper patch.


OT:

One last thing i want to say if i am allowed:
When one searches around the web after zsh one of the top most named things is:
what one can put into his PS1.
That i nice actually.
Also modifiers are great.

But if i have to name my top most Zsh killer feature it would be this tiny
little sentence which is easly overlooked:

      One commonly encountered difference is that variables substituted  onto
      the  command line are not split into words.

White-listing of splitting is so much more naturally then black-listing it.

THANK you so much for making a sensible shell!


And now i shut up. ;)

Nice day everyone!



kind regards,

     Thilo









[-- Attachment #2: allopt --]
[-- Type: text/plain, Size: 2020 bytes --]

# vim: ts=8:sw=4:sts=4:et:ft=zsh:
# kate: byte-order-marker false; dynamic-word-wrap false; indent-mode normal; indent-pasted-text true; indent-width 4; keep-extra-spaces false; newline-at-eof true; remove-trailing-spaces all; replace-tabs true; replace-tabs-save true; show-tabs true; smart-home true; syntax Zsh; tab-width 8; word-wrap false;
#-----------------------------------------------------------------------
#
# This function lists options with the no's in front removed for
# improved comprehension, i.e. `norcs off' becomes `rcs on'.
# The format is otherwise like that with `kshoptionprint' set,
# i.e. you can see all options whether on or off.
# It can take a list of option names or parts thereof to search for
# via egrep.
#
# Written by Sweth Chandramouli with hacks by Bart Schaefer.
# updated by Thilo Six

# allopt() {
    local    OPT_NAME OPT_VALUE LENGTH i
    local -a OPT_NAMES_ALL
    local -A OPT_ALL
    OPT_NAMES_ALL=(${(ok@)options})

    # http://zsh.sourceforge.net/Guide/zshguide05.html
    LENGTH=$(( ${#${OPT_NAMES_ALL[(r)${(l.${#${(O@)OPT_NAMES_ALL//?/X}[1]}..?.)}]}} + 1 ))

    ### XXX note:
    #   make a backup of ${(@)options}
    #   this needs to be done because otherwise
    #   the listing is inaccurat e.g. 'monitor' and 'zle' are always off
    #   the set builtin treats them special within a pipe. compare this:
    #   % builtin set -o
    #       with
    #   % builtin set -o | command grep -E '(\<monitor\>|\<zle\>)'
    #
    OPT_ALL=(${(kv@)options})

    {
        for i in ${OPT_NAMES_ALL} ; do echo "${i} ${OPT_ALL[$i]}" ; done | while read OPT_NAME OPT_VALUE ; do
            if [[ ${OPT_NAME#no} != ${OPT_NAME} ]] ; then
                OPT_VALUE=${(L)${${OPT_VALUE:s/on/OFF}:s/off/on}}
                OPT_NAME=${OPT_NAME#no}
            fi
            echo "${(r:${LENGTH}:)OPT_NAME} ${OPT_VALUE}"
        done
    } | {
        if [[ -n ${*} ]] ; then
            egrep ${(j.|.)${${${(L)@}#no}//[-_]/}}
        else
            cat
        fi
    }
# }


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

end of thread, other threads:[~2016-05-10 18:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-06  0:17 [PATCH] allopt() and question Thilo Six
2016-05-06  6:03 ` Sebastian Gniazdowski
2016-05-06  9:52   ` Thilo Six
2016-05-10 18:55     ` Thilo Six

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).