Skip to content

Instantly share code, notes, and snippets.

@tueda
Last active November 13, 2018 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tueda/9233f5d50c1479fe68aaa546aff97a0c to your computer and use it in GitHub Desktop.
Save tueda/9233f5d50c1479fe68aaa546aff97a0c to your computer and use it in GitHub Desktop.
gnuplot framework for encapsulating terminal-dependence. http://qiita.com/tueda/items/b5bfaa1c97bb3e775ccd #gnuplot
# Latest version:
# https://gist.github.com/tueda/9233f5d50c1479fe68aaa546aff97a0c
if (!(GPVAL_VERSION >= 5)) {
print 'Error: gnuplot >= 5.0 required'
exit
}
if (!(exist('shell__'))) {
if (system('echo $SHELL')[1:6] eq '$SHELL') {
shell__ = 'dos'
} else {
shell__ = 'unix'
}
}
set termoption enhanced
set macro
# min(x, y)
#
# Returns the smallest of x and y.
min(x, y) = x < y ? x : y
# max(x, y)
#
# Returns the largest of x and y.
max(x, y) = x > y ? x : y
# strsubst(string, old, new)
#
# Returns the given string with replacing all substrings "old" with "new".
#
# Example:
# strsubst('abcabc', 'ab', 'AB') -> 'ABcABc'
strsubst__impl(str, target, subst) = \
strstrt(str, target) == 0 ? str : \
str[1:strstrt(str, target) - 1] . subst \
. strsubst__impl(str[strstrt(str, target) + strlen(target):*], target, subst)
strsubst(string, old, new) = \
strlen(old) ? strsubst__impl(string, old, new) : string
# strstrlt(str, key)
#
# Same as strstrt(str, key) but searches "key" from the last. This is from
# http://www.ss.scphys.kyoto-u.ac.jp/person/yonezawa/contents/program/gnuplot/string_function.html#strstrlt
#
# Example:
# strstrlt('abcabc', 'abc') -> 4
strstrlt(str, key) = strstrlt__impl(str, 1, key)
strstrlt__impl(str, index, key) = \
strstrt(str[index:*], key) == 0 ? \
max(index - strlen(key), 0) : \
strstrlt__impl(str, index - 1 + strstrt(str[index:*], key) + strlen(key), key)
# change_ext(filename, new_ext)
#
# Returns the file name with replacing its extension.
#
# Example:
# change_ext('test.dat', '.pdf') -> 'test.pdf'
# change_ext('test.dat', '') -> 'test'
# change_ext('test', '.pdf') -> 'test.pdf'
change_ext(filename, new_ext) = \
strstrt(filename, '/') == 0 && strstrt(filename, '\') == 0 ? \
change_ext__impl(filename, new_ext) : \
filename[1:max(strstrlt(filename, '/'), strstrlt(filename, '\'))] \
. change_ext__impl( \
filename[max(strstrlt(filename, '/'), strstrlt(filename, '\')) + 1: \
strlen(filename)], \
new_ext)
change_ext__impl(filename, new_ext) = \
strstrt(filename, '.') == 0 ? \
filename . new_ext : \
filename[1:strstrlt(filename, '.') - 1] . new_ext
# getenv(name)
#
# Retrieves the value of the environment variable.
getenv(name) = shell__ eq 'dos' ? getenv__dos(name) : getenv__unix(name)
getenv__dos(name) = \
system('echo %' . name . '%')[1:strlen(name) + 2] ne '%' . name . '%' ? \
system('echo %' . name . '%') : ''
getenv__unix(name) = system('echo $' . name)
# @InitVarInt(var_name, default_value)
# @InitVarReal(var_name, default_value)
# @InitVarStr(var_name, default_value)
#
# Sets a variable, if undefined, by the environment variable with the same name
# if exists, otherwise by the given default value.
InitVarInt = 'eval init_var_int__'
init_var_int__(var_name, default_value) = \
'if (!exists("' . var_name . '")) {' .\
'init_var__s = getenv("' . var_name . '");' .\
'if (strlen(init_var__s)) {' .\
'eval "' . var_name . ' = init_var__s + 0"' .\
'} else {' .\
'eval "' . var_name . ' = ' . default_value . ' + 0"' .\
'}' .\
'}'
InitVarReal = 'eval init_var_real__'
init_var_real__(var_name, default_value) = \
'if (!exists("' . var_name . '")) {' .\
'init_var__s = getenv("' . var_name . '");' .\
'if (strlen(init_var__s)) {' .\
'eval "' . var_name . ' = init_var__s + 0.0"' .\
'} else {' .\
'eval "' . var_name . ' = ' . default_value . ' + 0.0"' .\
'}' .\
'}'
InitVarStr = 'eval init_var_str__'
init_var_str__(var_name, default_value) = \
'if (!exists("' . var_name . '")) {' .\
'init_var__s = getenv("' . var_name . '");' .\
'if (strlen(init_var__s)) {' .\
'eval "' . var_name . ' = init_var__s"' .\
'} else {' .\
'eval "' . var_name . " = '" . default_value . "'" . '"' .\
'}' .\
'}'
# @SetTerm(filename)
# @UnsetTerm
#
# Sets/unsets the terminal by corresponding variables. Environment variables are
# also checked at the first time.
#
# Environment variables for switching the terminal:
# EPS
# EPSCAIRO
# PDF
# PDFCAIRO
# PNG
# PNGCAIRO
# TIKZ
#
# Other environment variables:
# PDFCROP
#
# The following variables will be defined:
# CAIRO
SetTerm = 'eval set_term__'
set_term__eps = \
'set terminal postscript eps enhanced color' \
. ' font "Helvetica,27" linewidth 2.5 size 5in, 3.5in'
set_term__epscairo = \
'set terminal epscairo enhanced color' \
. ' font "Helvetica,20" linewidth 5.0 size 5in, 3.5in'
set_term__pdf = \
'set terminal pdf color enhanced' \
. ' font "Helvetica,15" linewidth 2.5 size 5in, 3.5in'
set_term__pdfcairo = \
'set terminal pdfcairo enhanced color' \
. ' font "Helvetica,20" linewidth 2.5 size 5in, 3.5in'
set_term__png = \
'set terminal png enhanced' \
. ' font "Helvetica,15" linewidth 2.5'
set_term__pngcairo = \
'set terminal pngcairo enhanced color' \
. ' font "Helvetica,15" linewidth 1.5'
set_term__tikz = \
'set terminal tikz latex color' \
. ' linewidth 2.5 standalone createstyle'
set_term__(filename) = \
'set_term__flag = 0;' .\
'set_term__suffix = ".undef";' .\
'CAIRO = 0;' .\
'@InitVarInt("EPS", 0);' .\
'@InitVarInt("EPSCAIRO", 0);' .\
'@InitVarInt("PDF", 0);' .\
'@InitVarInt("PDFCAIRO", 0);' .\
'@InitVarInt("PNG", 0);' .\
'@InitVarInt("PNGCAIRO", 0);' .\
'@InitVarInt("TIKZ", 0);' .\
'if (EPS || EPSCAIRO || PDF || PDFCAIRO || PNG || PNGCAIRO || TIKZ) {' .\
'set_term__flag = 1;' .\
'set terminal push;' .\
'};' .\
'if (EPS) {' .\
'@set_term__clear; EPS = 1; set_term__suffix = ".eps";' .\
set_term__eps . '}' .\
'else { if (EPSCAIRO) {' .\
'@set_term__clear; EPSCAIRO = 1; set_term__suffix = ".eps";' .\
set_term__epscairo . '}' .\
'else { if (PDF) {' .\
'@set_term__clear; PDF = 1; set_term__suffix = ".pdf";' .\
set_term__pdf . '}' .\
'else { if (PDFCAIRO) {' .\
'@set_term__clear; PDFCAIRO = 1; set_term__suffix = ".pdf";' .\
set_term__pdfcairo . '}' .\
'else { if (PNG) {' .\
'@set_term__clear; PNG = 1; set_term__suffix = ".png";' .\
set_term__png . '}' .\
'else { if (PNGCAIRO) {' .\
'@set_term__clear; PNGCAIRO = 1; set_term__suffix = ".png";' .\
set_term__pngcairo . '}' .\
'else { if (TIKZ) {' .\
'@set_term__clear; TIKZ = 1; set_term__suffix = ".tex";' .\
set_term__tikz . '}' .\
'}}}}}};' .\
'if (set_term__suffix eq ".pdf") {' .\
'@InitVarInt("PDFCROP", 0)' .\
'};' .\
'if (set_term__flag) { ' .\
'if (EPSCAIRO || PDFCAIRO || PNGCAIRO) { CAIRO = 1 };' .\
'set_term__output = change_ext("' . filename . '", set_term__suffix);' .\
'set output set_term__output' .\
'}'
set_term__clear = \
'EPS = 0;' .\
'EPSCAIRO = 0;' .\
'PDF = 0;' .\
'PDFCAIRO = 0;' .\
'PNG = 0;' .\
'PNGCAIRO = 0;' .\
'TIKZ = 0'
UnsetTerm = \
'if (set_term__flag) {' .\
'@set_term__clear;' .\
'set output;' .\
'set terminal pop;' .\
'if (set_term__suffix eq ".pdf" && PDFCROP) {' .\
'system(sprintf("pdfcrop --margin %d \"%s\"",' .\
'set_term__pdfcrop_margin, set_term__output));' .\
'system(sprintf("mv \"%s\" \"%s\"",' .\
'change_ext(set_term__output, "-crop.pdf"),' .\
'set_term__output))' .\
'}' .\
'}'
set_term__pdfcrop_margin = 0
# texorpdfstring(texcode, string)
#
# Returns the first argument for TeX terminals, otherwise the second.
texorpdfstring(texcode, string) = \
exist('TIKZ') && TIKZ ? texcode : string
# ensuremath(string)
#
# Returns the string sandwiched by '$' for TeX terminals.
ensuremath(string) = \
(exist('TIKZ') && TIKZ) ? ensuremath__tex(string) : ensuremath__enhanced(string)
ensuremath__tex(string) = '$' . string . '$'
ensuremath__enhanced(string) = \
'{/:Italic' . \
ensuremath__italic_correction( \
ensuremath__greek( \
ensuremath__accent( \
ensuremath__italic( \
' ' . string \
)))) . '}'
ensuremath__italic(string) = \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst( \
string \
, '/', '###SLASH') \
, ':', '###COLON') \
, '%d', '%%%%{/:Normal %d}@@@@') \
, '%g', '%%%%{/:Normal %g}@@@@') \
, '%.1g', '%%%%{/:Normal %.1g}@@@@') \
, '%.2g', '%%%%{/:Normal %.2g}@@@@') \
, '%.3g', '%%%%{/:Normal %.3g}@@@@') \
, '%.4g', '%%%%{/:Normal %.4g}@@@@') \
, '%.5g', '%%%%{/:Normal %.5g}@@@@') \
, '%.6g', '%%%%{/:Normal %.6g}@@@@') \
, '%t', '%%%%{/:Normal %t}@@@@') \
, '%l', '%%%%{/:Normal %l}@@@@') \
, '%s', '%%%%{/:Normal %s}@@@@') \
, '{%T}', '%%%%{/:Normal %T}@@@@') \
, '{%L}', '%%%%{/:Normal %L}@@@@') \
, '{%S}', '%%%%{/:Normal %S}@@@@') \
, '%.0P', '%%%%{/:Normal ###FMTZP}@@@@') \
, '.', '%%%%{/:Normal .}@@@@') \
, ',', '%%%%{/:Normal ,}@@@@') \
, ';', '%%%%{/:Normal ;}@@@@') \
, '+', '%%%%{/:Normal +}@@@@') \
, '-', '%%%%{/Symbol:Normal ###MINUS}@@@@') \
, '=', '%%%%{/:Normal =}@@@@') \
, '<', '%%%%{/:Normal <}@@@@') \
, '>', '%%%%{/:Normal >}@@@@') \
, '(', '%%%%{/:Normal (}@@@@') \
, ')', '%%%%{/:Normal )}@@@@') \
, '0', '%%%%{/:Normal 0}@@@@') \
, '1', '%%%%{/:Normal 1}@@@@') \
, '2', '%%%%{/:Normal 2}@@@@') \
, '3', '%%%%{/:Normal 3}@@@@') \
, '4', '%%%%{/:Normal 4}@@@@') \
, '5', '%%%%{/:Normal 5}@@@@') \
, '6', '%%%%{/:Normal 6}@@@@') \
, '7', '%%%%{/:Normal 7}@@@@') \
, '8', '%%%%{/:Normal 8}@@@@') \
, '9', '%%%%{/:Normal 9}@@@@') \
, '###COLON', '%%%%{/:Normal :}@@@@') \
, '###FMTZP', '%.0P') \
, '###MINUS', '\055') \
, '###SLASH', '%%%%{/:Normal /}@@@@') \
, '\cos', '%%%%{/:Normal cos}@@@@') \
, '\exp', '%%%%{/:Normal exp}@@@@') \
, '\log', '%%%%{/:Normal log}@@@@') \
, '\sin', '%%%%{/:Normal sin}@@@@') \
, '\tan', '%%%%{/:Normal tan}@@@@') \
, '\cdot', '%%%%{/Symbol:Normal \327}@@@@') \
, '\ge', '%%%%{/Symbol:Normal \263}@@@@') \
, '\infty', '%%%%{/Symbol:Normal \245}@@@@') \
, '\int', '%%%%{/Symbol:Normal \362}@@@@') \
, '\le', '%%%%{/Symbol:Normal \243}@@@@') \
, '\partial', '%%%%{/Symbol:Normal \266}@@@@') \
, '\perp', '%%%%{/Symbol:Normal \136}@@@@') \
, '\pm', '%%%%{/Symbol:Normal \261}@@@@') \
, '\prod', '%%%%{/Symbol:Normal \325}@@@@') \
, '\sum', '%%%%{/Symbol:Normal \345}@@@@') \
, '\times', '%%%%{/Symbol:Normal \264}@@@@') \
, '\to', '%%%%{/Symbol:Normal \256}@@@@') \
, '\sqrt{s}', CAIRO ? '%%%%{/Symbol:Normal @{/*0.887 &{/*0.54 .}\140}\326}{s}@@@@' : \
'%%%%{/Symbol:Normal @{\140}\326}{s}@@@@') \
, '\sqrt{', '%%%%{/Symbol:Normal \326}@@@@{') \
, '\text{', '%%%%{/:Normal ')
# TODO:
# - Canceling italic correction after \text{...}.
# - In \text{...}, probably "-" should be a hyphen.
# NOTE: italic correction is needed for the case of
# [Italic] [Normal] or [Italic] ^[Italic/Normal]
# ^ ^
# HERE HERE
# but the latter is not completely implemented.
ensuremath__italic_correction(string) = \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
string \
, '}@@@@%%%%{', '}{') \
, '}@@@@^%%%%{', '}^{') \
, '}@@@@_%%%%{', '}_{') \
, '^%%%%{/:Normal ', '^{/:Normal %%%%') \
, '^%%%%{/Symbol:Normal ', '^{/Symbol:Normal %%%%') \
, '_%%%%{/:Normal ', '_{/:Normal %%%%') \
, '_%%%%{/Symbol:Normal ', '_{/Symbol:Normal %%%%') \
, ' %%%%{', ' {') \
, '}@@@@', '}') \
, '%%%%', ensuremath__sep)
ensuremath__sep = '&{/:Normal*.6 .}'
ensuremath__accent(string) = \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
string \
, '\bar{b}', '~b{.75-}') \
, '\bar{c}', '~c{.6-}') \
, '\bar{d}', '~d{.7-}') \
, '\bar{f}', '~f{.8-}') \
, '\bar{p}', '~p{.6-}') \
, '\bar{q}', '~q{.6-}') \
, '\bar{s}', '~s{.6-}') \
, '\bar{t}', '~t{.7-}') \
, '\bar{u}', '~u{.6-}') \
, '\hat{s}', CAIRO ? '~s{0.6{/:Normal*0.8 \^}}' : \
'~s{0.5\^}')
ensuremath__greek(string) = \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst(strsubst( \
strsubst(strsubst(strsubst(strsubst( \
string \
, '\alpha', '{/Symbol a}') \
, '\beta', '{/Symbol b}') \
, '\gamma', '{/Symbol g}') \
, '\delta', '{/Symbol d}') \
, '\epsilon', ensuremath__epsilon) \
, '\varepsilon', '{/Symbol e}') \
, '\zeta', '{/Symbol z}') \
, '\eta', '{/Symbol h}') \
, '\theta', '{/Symbol q}') \
, '\vartheta', '{/Symbol J}') \
, '\iota', '{/Symbol i}') \
, '\kappa', '{/Symbol k}') \
, '\lambda', '{/Symbol l}') \
, '\mu', '{/Symbol m}') \
, '\nu', '{/Symbol n}') \
, '\xi', '{/Symbol x}') \
, '\pi', '{/Symbol p}') \
, '\varpi', '{/Symbol v}') \
, '\rho', '{/Symbol r}') \
, '\sigma', '{/Symbol s}') \
, '\varsigma', '{/Symbol V}') \
, '\tau', '{/Symbol t}') \
, '\upsilon', '{/Symbol u}') \
, '\phi', '{/Symbol f}') \
, '\varphi', '{/Symbol j}') \
, '\chi', '{/Symbol c}') \
, '\psi', '{/Symbol y}') \
, '\omega', '{/Symbol w}') \
, '\Gamma', '{/Symbol G}') \
, '\Delta', '{/Symbol D}') \
, '\Theta', '{/Symbol Q}') \
, '\Lambda', '{/Symbol L}') \
, '\Xi', '{/Symbol X}') \
, '\Pi', '{/Symbol P}') \
, '\Sigma', '{/Symbol S}') \
, '\Upsilon', '{/Symbol \241}') \
, '\Phi', '{/Symbol F}') \
, '\Psi', '{/Symbol Y}') \
, '\Omega', '{/Symbol W}')
ensuremath__epsilon = '{/Symbol e}'
#ensuremath__epsilon = '{/Symbol \316}'
# vim: ft=gnuplot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment