conditional on terminal type (or 'set' variable in general) / read a 'set' variable? - gnuplot

I have a series of gnuplot scripts which I have developed using the default 'qt' terminal on my OS X system. This has the convenient feature of closing the closing the qt window when the script completes, so I added:
pause mouse "mouse button 2 or 3 to close\n";
Later in development I want to output to pdf, but now the pause hangs my command line terminal until I hit return. I wanted to do:
set terminal pdf
...
if (terminal eq qt) pause mouse "mouse button 2 or 3 to close\n";
but this gives me:
line 45: undefined variable: terminal
I now have a workaround of setting a different variable and setting the terminal from it:
term = 'qt'
if (term eq 'pdf') set terminal pdf ; set output 'rToR.pdf'
...
if (term eq 'qt') pause mouse "mouse button 2 or 3 to close\n";
which may be arguably better for some code design aspects, but requires a layer of redundancy and isn't actually what I want to do.
Therefore, is there way to / how do I access the value of a gnuplot setting like 'terminal'?

See show variables all for a list of available variables. In your case you need GPVAL_TERM:
if (GPVAL_TERM eq 'qt') { ... }
if (GPVAL_TERM eq 'pdfcairo') { ... }
With set terminal pdf usually, the pdfcairo terminal is selected, so you need the string 'pdfcairo' for the comparison.

Related

Why is terminal size undefined after gnuplot startup?

After startup of gnuplot (console) even without specifying a terminal and a canvas size there are default values defined, in my case wxt and 640,384.
Without having plotted anything, if I type in console or use in a script:
print GPVAL_TERM_XSIZE, GPVAL_TERM_YSIZE
I will get an error:
undefined variable: GPVAL_TERM_XSIZE
I know that many other GVPAL_... variables depend on the plot and the data itself. So, it's clear that they are only defined if you have plotted something.
But why are GPVAL_TERM_XSIZE and GPVAL_TERM_YSIZE undefined after startup if there are default values?
Even if I specify a size, the following will still give the same error:
set term wxt size 500,400
print GPVAL_TERM_XSIZE, GPVAL_TERM_YSIZE
I know that I can (or I have to) do some dummy plot to initialize the GPVAL_TERM_XSIZE and GPVAL_TERM_YSIZE values. But why is this necessary?
Thank you for explanations.
On program entry the name of the current terminal (usually the default one but you might have changed it in ./gnuplot) is printed to the console.
But the terminal is not actually opened until the first plot command.
This allows time for you to change the terminal or set different terminal properties before plotting. When the terminal is first opened, it fills in various GPVAL_TERM values including its current size.
The GPVAL_* program variables are updated after every plot command. Those that are dependent on the most recent plot, including all the GPVAL_TERM_* variables, don't exist until after the first plot.

How to use `set term push` and `set term pop` in gnuplot

I am using gnuplot 5.2.7 on Arch Linux. I want to temporarily change the terminal's configuration, plot something, and then restore it (I have no terminal configuration in my initialization file). I think pop and push can be used to this effect, but I'm having no success.
This is what I do in a gnuplot session. First I set the terminal to wxt and push it, then plot a sine wave:
gnuplot> set term wxt 1 ; set term push
Terminal type is now 'wxt'
Options are '1 enhanced'
pushed terminal wxt 1 enhanced
gnuplot> plot sin(x)
So far this works. Now I want to temporarily change the background to cyan, and then revert to default background:
gnuplot> set term wxt 1 background "cyan"
Terminal type is now 'wxt'
Options are '1 background '#00ffff' enhanced'
gnuplot> plot sin(x)
gnuplot> set term pop
restored terminal is wxt 1 background '#00ffff' enhanced
gnuplot>
As you can see, poping the terminal didn't restore the background. The next plot comes up with a cyan background.
Gnuplot's manual (pdf) states, in page 257, that:
The command set term push remembers the current terminal including its
settings while set term pop restores it.
What am I doing wrong?
From the gnuplot manual:
The command set term push remembers the current terminal including its
settings while set term pop restores it. This is equivalent to save
term and load term, but without accessing the filesystem. Therefore
they can be used to achieve platform independent restoring of the
terminal after printing, for instance. After gnuplot's startup, the
default terminal or that from startup file is pushed automatically.
Therefore portable scripts can rely that set term pop restores the
default terminal on a given platform unless another terminal has been
pushed explicitly.
Actually, it's not completely clear to me what is the benefit of terminal push and terminal pop? Well, restoring the default terminal. The only advantage I can (currently) think of is that in a long gnuplot script when you are switching back and forth to different terminals you don't have to type in all the parameters of your default terminal again and again. And in case you change some terminal settings you otherwise would have to change all the occurrences in your script.
Maybe the following is useful to you: at the beginning of the code define your terminals with your backgrounds or other settings as string variables and then later call them as macro with #. So with this, I don't see a difference between calling #TerminalDefault and set terminal pop, except that #TerminalDefault will also restore if you had the same terminal before but just with different settings.
Code:
### workaround for terminal push & pop with same terminal but different settings
reset session
TerminalDefault = 'set term wxt 0 background "white"'
TerminalCyan = 'set term wxt 0 background "cyan"'
TerminalYellow = 'set term wxt 0 background "yellow"'
TerminalPNG = 'set term png background "green"'
#TerminalDefault
plot x
pause -1 TerminalDefault
#TerminalCyan
plot x**2
pause -1 TerminalCyan
#TerminalPNG
set output "Test.png"
plot x**3
set output
pause -1 TerminalPNG
#TerminalDefault
plot x**4
pause -1 TerminalDefault
### end of code

Does "%{\033[0m%}" (code zero) reset the font color to "tput setaf" or to Xterm default color?

I know that you can use escape sequence %{\033[0m%} (code zero) to change the color of the prompt back to "default" color (usually, after the prompt).
But what is the "default" that is used for this?
Default color of the terminal program itself?
Default color set via tput setaf $COLOR, assuming you use it to set font color different from terminal's default color?
Else?
It's terminal-dependent, but with regard to existing practice, it would have to be the former:
the default color of the terminal (program).
The other suggested choice would not be correct, since tput setaf does not set a "default color", but rather a "current color". Whether any choice of tput setaf corresponds to the terminal's default color is (at best) terminal-specific, since there is no relevant standard one way or the other.
ECMA 48 gives terse (one line) descriptions of SGR 0 (which is what's mentioned in the question), and SGR 30-37, 40-47 (the setaf/setab choices commonly used for terminal descriptions that tput would use).
There's another possible choice, i.e., tput op (selecting the original color pair), which is used in several terminal descriptions to send SGR 39 and 49. Again, this is terminal-specific and its relationship to SGR 0 is not covered by any standard. Those happen to use the same "default color" for xterm and Linux console, but (read ECMA-48) the standard does not give that level of detail regarding "default color" and the absence of "video attributes".
Further reading:
ECMA-48: Control Functions for Coded Character Sets
Who did it? (a brief history of the Linux console color model)

Temporarily colour Linux Terminal foreground and background text

How can I temporarily set the colors of my Linux Terminal? I wish to add to a script, a color setting so that I remember to run the partner script a few minutes later. The color change would be set on in one script and removed in the other and would act as a visible reminder to me.
So the color change needs to be applied after the terminal is started up and needs to stay for all subsequent typing until it is removed by another equivalent command.
My terminal is GNOME Terminal 2.31.3
There's more than one way to change colors. VTE (the actual terminal within GNOME Terminal) implements these features (generally from xterm):
"ANSI color" escapes (which would be useless to you, since many programs reset those)
changing the text foreground and background using non-ANSI dynamic colors (resetters are rare)
changing the color palette (again, resetters are rare)
The latter two are documented in XTerm Control Sequences, in the section Operating System Commands:
OSC Ps ; Pt BEL
OSC Ps ; Pt ST
gives the form of the control sequence, e.g., escape]Ps;PtBEL
The dynamic colors are described here:
The 10 colors (below) which may be set or queried using 1 0
through 1 9 are denoted dynamic colors, since the correspond-
ing control sequences were the first means for setting xterm's
colors dynamically, i.e., after it was started. They are not
the same as the ANSI colors. These controls may be disabled
using the allowColorOps resource. At least one parameter is
expected for Pt. Each successive parameter changes the next
color in the list. The value of Ps tells the starting point
in the list. The colors are specified by name or RGB specifi-
cation as per XParseColor.
and other colors here:
Ps = 4 ; c; spec -> Change Color Number c to the color spec-
ified by spec. This can be a name or RGB specification as per
XParseColor. Any number of c/spec pairs may be given. The
color numbers correspond to the ANSI colors 0-7, their bright
versions 8-15, and if supported, the remainder of the 88-color
or 256-color table.

gnuplot multiple graphs are not interactive

I am drawing multiple graphs via a shell script in gnuplot.
The graphs are drawn correctly, but am not am able to zoom in . Does any variable need to be set?
Here is the code:
--- for loop of script starts---
gnuplot -persist <<EOF
set term x11 1
set title "IP : $ip Upstream capacity:$UP_CAP kbps"
plot 'trace-0-dir1.txt' using (\$1-$min1):(\$2-\$1-$mindelay1) with lp
set term x11 2
set title "IP: $ip Downstream capacity:$DOWN_CAP kbps"
plot 'trace-0-dir2.txt' using (\$1-$min2):(\$2-\$1-$mindelay2) with lp
EOF
---for loop ends---
Once you switch away from the "x11 1" window the zooming is disabled. To regain control, you should switch back to the specific window (set term x11 1). Another issue is the x11 terminal. You should use wxt which can keep windows alive.
You can solve your issue by using the wxt terminal and separating the two plot commands, therefore not switching away from the window:
--- for loop of script starts---
gnuplot -persist <<EOF
set term wxt
set title "first"
plot x
EOF
gnuplot -persist <<EOF
set term wxt
set title "second"
plot x**2
EOF
---for loop ends---
With this, you have two zoomable windows open and you can still use your shell variables.
In general, you do not have to have console open in order to have active windows, just an appropriate terminal. Especially, the command
gnuplot --persist -e 'plot[0:10] sqrt(x)'
produces scrollable and zoomable windows if used with wxt. Try
gnuplot --persist -e 'set term wxt; plot[0:10] sqrt(x)'
Hope this helps.
AFAIK you are only able to zoom, scroll etc. if the gnuplot console is still active. Meaning, gnuplot must still be running.
To give zooming and scrolling a try, input the following sequence:
Enter the gnuplot console by typing gnuplot
plot a function with plot[0:10] sqrt(x) for example.
Try zooming (Ctrl + Mouse wheel) and scrolling (Mouse wheel / Shift + Mouse wheel) before exiting the gnuplot console.
If you run a script like
gnuplot --persist -e 'plot[0:10] sqrt(x)'
you cannot scroll or zoom anymore.

Resources