Subject: [PATCH] make 'set +o' useful and POSIX compatible
From: Martijn Dekker <[email-masked]>
To: Zsh hackers list <[email-masked]>
Date: Sat, 27 Feb 2016 04:11:28 +0100
List-Id: Zsh Workers List <zsh-workers.zsh.org>

The command 'set -o' shows the current shell options in an unspecified
format. Less well-known is the variant 'set +o', which should output the
current shell options "in a format that is suitable for reinput to the
shell as commands that achieve the same options settings".[*]

That means it should be possible to do

        save_options=$(set +o)

then change some options, then later restore the shell options with

        eval "$save_options"

On zsh (as well as all pdksh variants), 'set +o' is currently inadequate
for that purpose because it only outputs the currently active shell
options, and not the inactive ones.

The zshbuiltins(1) man page also implies that 'set +o' should print the
complete current option states.

This is my first time trying my hand at a zsh patch.

Thanks,

- M.

[*]
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_25_03
    (scroll down to '+o')

diff --git a/Src/options.c b/Src/options.c
index 17c46c3..18619c8 100644
--- a/Src/options.c
+++ b/Src/options.c
@@ -847,9 +847,10 @@ printoptionnodestate(HashNode hn, int hadplus)
     int optno = on->optno;

     if (hadplus) {
-        if (defset(on, emulation) != isset(optno))
-	    printf("set -o %s%s\n", defset(on, emulation) ?
-		   "no" : "", on->node.nam);
+	printf("set %co %s%s\n",
+	       defset(on, emulation) != isset(optno) ? '-' : '+',
+	       defset(on, emulation) ? "no" : "",
+	       on->node.nam);
     } else {
 	if (defset(on, emulation))
 	    printf("no%-19s %s\n", on->node.nam, isset(optno) ? "off" : "on");