#################################################
# This is an example of an init file ~/.fudgitrc
# It defines a lot of useful macros to be used
# in conjunction with gnuplot.
# Some of the ideas are from J. Ross Thomson.

#################################################
# Print a message.
echo
echo Hello there!
echo

#################################################
# Which plotting program to use.
# Default is /usr/local/bin/gnuplot
# set plotting "/usr/bin/gnuplot"

#################################################
# Use less pager instead of more.
# set pager "/usr/local/bin/less -M"

#################################################
# Some very cool prompts. Try them on ansi color!
# set prompt-fm "^[[33;1mfudgit^[[31m>^[[0m "
# set prompt-cm "^[[31;1mcmode^[[33m^[[5m>^[[0m "
# set prompt-pm "^[[31;1mpmode^[[33m^[[5m>^[[0m "

#################################################
# Macro to edit a script and then load it.
# Usage vload file
macro vl!oad 1
    vi $1
    load $1
stop

#################################################
# A macro to keep gnuplot in the same directory
# where fudgit is.
# The & means to take the unaliased definition
# Usage: cd directory
macro cd 1
    pmode cd '$1'
    &cd $1
	# Uncomment to have $Cwd in your xterm title
	#echo -n "]2;Fudgit: $Cwd "
	echo $Cwd
stop

#################################################
# Create a postscript language file of the current plot (square).
# (appends .ps to the name)
# Usage: sqpost filename
macro sqpost 1
    pmode
        set size 0.7,0.92
        set term post port 'Helvetica-Bold'
        set output '$1.ps'
        replot
        set term x11
        replot
    fmode
stop

#################################################
# Create a hard copy using the laser printer (square).
# Usage: psqpost printer_name
macro psqpost 1
    sqpost $Tmp.ps
    ! (sleep 5; lpr -P$1 $Tmp.ps ; rm -f $Tmp.ps ) &
stop

#################################################
# Creates a fig language file of the current plot.
# (appends .fig to the name)
# Usage: fig filename
macro fig 1
    pmode
        set size 0.7,1
        set term bfig
        set output '$1.fig'
        replot
        set term x11
        replot
    fmode
stop

##################################################################
# Some more Gnuplot macros.
#
# The following set of macros allows auto-replot using
# a restricted set of commands.
# Points or line and keys can be chosen from the command name too.
# Basic commands are:
#
# ResetPlot: Start a new plot;
# [t][lp]plot: plot the given vectors;
#              options: t -> titles (requires a third string argument);
#                       l -> lines;
#                       p -> points;
#
# Examples:
#
# plot Y vs. X with associated key "curve #1" and default data style:
# fudgit> tplot X Y "curve #1" 
# plot Z vs. X with points and ZFIT vs. X with line:
# fudgit> pplot X Z
# fudgit> tlplot X ZFIT "fitted model"
#############################################################

# Here we start:
#################################################
# The variable plot_num implements auto-replot
alias ResetPlot let plot_num = 0.0
# Reset now.
ResetPlot

#################################################
# Auto-replot for gnuplot (between ResetPlot calls).
# Uses default line or points (set data style ? ).
# Title is "Y vs. X", can be removed by nokey.
# Usage: plot X Y
macro pl!ot 2
    save vector $1 $2 $Tmp.$plot_num
    if (plot_num) then
         pm replot '$Tmp.$plot_num' title '$2 vs. $1'
    else 
         pm plot '$Tmp.$plot_num' title '$2 vs. $1'
    endif 
    let plot_num++
stop

#################################################
# Auto-replot for gnuplot with points.
# Title is "Y vs. X", can be removed by nokey.
# Usage: pplot X Y
macro ppl!ot 2
    save vector $1 $2 $Tmp.$plot_num
    if (plot_num) then
         pm replot '$Tmp.$plot_num' title '$2 vs. $1' with points 1 2 
    else 
         pm plot '$Tmp.$plot_num' title '$2 vs. $1' with points 1 2
    endif 
    let plot_num++
stop

#################################################
# Auto-replot for gnuplot with lines.
# Title is "Y vs. X", can be removed by nokey.
# Usage: lplot X Y
macro lpl!ot 2
    save vector $1 $2 $Tmp.$plot_num
    if (plot_num) then
         pm replot '$Tmp.$plot_num' title '$2 vs. $1' with lines
    else 
         pm plot '$Tmp.$plot_num' title '$2 vs. $1' with lines
    endif 
    let plot_num++
stop

#################################################
# Auto-replot for gnuplot with default lines/points and given titles.
# Usage: tplot X Y title
macro tpl!ot 3
    save vector $1 $2 $Tmp.$plot_num
    if (plot_num) then
         pm replot '$Tmp.$plot_num' title '$3'
    else 
         pm plot '$Tmp.$plot_num' title '$3'
    endif 
    let plot_num++
stop

#################################################
# Auto-replot for gnuplot with lines and given titles.
# Usage: tlplot X Y title
macro tlpl!ot 3
    save vector $1 $2 $Tmp.$plot_num
    if (plot_num) then
         pm replot '$Tmp.$plot_num' title '$3' with lines
    else 
         pm plot '$Tmp.$plot_num' title '$3' with lines
    endif 
    let plot_num++
stop

#################################################
# Auto-replot for gnuplot with points and given titles.
# Usage: tpplot X Y title
macro tppl!ot 3
    save vector $1 $2 $Tmp.$plot_num
    if (plot_num) then
         pm replot '$Tmp.$plot_num' title '$3' with points 1 2
    else 
         pm plot '$Tmp.$plot_num' title '$3' with points 1 2
    endif 
    let plot_num++
stop

############################################################
# The following macros are used to generate gnuplot scripts
# instead of generating direct commands.
############################################################

#################################################
# Init batch plot
# Usage: initbatch
macro initbat!ch 0
    set output $Tmp.batch
stop

#################################################
# Plot for gnuplot batch plots and titles. Default data style.
# Generates a gnuplot script to be loaded by gnuplot later on.
# You can create the whole family b[t][lp]plot above if
# you need it. (Or as an exercise :-) ).
# Usage: btplot X Y title
macro btpl!ot 3
    save vector $1 $2 $Tmp.$plot_num
    if (plot_num) then
        let print ", '$Tmp.$plot_num' title '$3'"
    else 
        let print "plot '$Tmp.$plot_num' title '$3'"
    endif 
    let plot_num++
stop

#################################################
# Tell gnuplot to load the script.
# Usage: batchplot
macro bat!chplot 0
    let print "\n" 
    set output stdout
    pmode load "$Tmp.batch"
    pmode pause 0  "Batch plotted"
stop

#################################################
# A bunch of useful Gnuplot aliases.
alias rep!lot    pm replot
alias log        pm set log
alias nolog      pm set nolog
alias xrange     pm set xrange
alias yrange     pm set yrange
alias auto       pm set auto
alias title      pm set title
alias xlabel     pm set xlabel
alias ylabel     pm set ylabel
alias line!s     pm set data style line
alias point!s    pm set data style points
alias nokey      pm set nokey
alias key        pm set key
alias format     pm set format 
alias source     load

#################################################
# A bunch of useful UNiX aliases.
alias date    !date
alias mv      !mv -i
alias cp      !cp -i
alias rm      !rm -i
alias m       !more -c

#################################################
# I mean show and not shell when I only type sh.
alias sh     show

