#!/bin/sh
#==============================================================================
# MODULE     : tm_feynmf
# VERSION    : 1.0
# DESCRIPTION: A feynmf interface for TeXmacs
# COPYRIGHT  : (C) 2008 M. R. Wegewijs
#------------------------------------------------------------------------------
# COPYRIGHT  : (C) feynmf latex package
#------------------------------------------------------------------------------
# COPYRIGHT  : (C) bbox_add perlscript
#------------------------------------------------------------------------------
# Requirement: feynmf package
#    http://www.ctan.org/tex-archive/macros/latex/contrib/feynmf/
# contained in any self-respecting linux distro, e.g. Gentoo :)
#
# Usage within TeXmacs:
#   + write feynmf-commands in multi-line separate by SHIFT-ENTER,
#   + then ENTER key terminates the input and sends it to latex.
# Information:
#   + Output is a diagram made via latex (package feynmf}, dvips
#     and a perl script to get the bounding box right (script included)
#   + Temporary files are made in ~/.TeXmacs/system/tmp.
#------------------------------------------------------------------------------
# This software falls under the GNU general public license and comes WITHOUT
# ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details.
# If you don't have this file, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#==============================================================================

# option --texmacs
# for compatibility with TeXmacs interface convention and user information
if [ "$1" != "--texmacs" ]
then
	echo tm_feynmf. This script should be started only from TeXmacs.
	exit
fi

# Control characters
tmp=`echo DATA_BEGIN=X DATA_END=Y DATA_ESCAPE=Z | tr "XYZ" "\002\005\027" `
eval $tmp

#
PLUGIN_DIR=$TEXMACS_PATH/plugins/feynmf

# Defining temporary files directory and make it if it doesn't exist
TEMP_DIR=~/.TeXmacs/system/tmp
if [ -d $TEMP_DIR ]
then
	cd $TEMP_DIR
else
	mkdir -p $TEMP_DIR
	cd $TEMP_DIR
fi

# Defining temporary file name
TEMP_FILE=TMPfeynmfTMP
# For debugging, run:
#   tail -f $HOME/.TeXmacs/system/tmp/TMPfeynmfTMP.tex
# in a terminal to monitor the tex file produced.

# Startup banner
echo -n $DATA_BEGIN
#echo verbatim: TeXmacs interface to feynmf \(Feynman diagrams\)

# Prompt-input-feynmf-output loop
while [ 1 ]; do
	# Prompt
	echo -n $DATA_BEGIN
	echo -n channel:prompt
	echo -n $DATA_END
	echo -n "feynmf]"
	echo -n $DATA_END

	# Read a line from stdin
	read -r input
	    # Begin creation of LaTeX file
	    echo -E "\documentclass{article}"     > $TEMP_FILE.tex
	    echo -E "\usepackage{feynmf}"        >> $TEMP_FILE.tex
	    echo -E "\pagestyle{empty}"          >> $TEMP_FILE.tex
	    # Unitlength choosen such that size specification (1,1)
	    # produces okayish output
	    # - the linestyles in file feynmf-styles must be tuned to this!
	    echo -E "\unitlength=20mm"            >> $TEMP_FILE.tex
	    echo -E "\begin{document}"           >> $TEMP_FILE.tex
	    echo -E "\begin{fmffile}{fmftempl}"  >> $TEMP_FILE.tex

	    cat $PLUGIN_DIR/share/feynmf-styles >> $TEMP_FILE.tex

	    echo -En "\begin{fmfgraph*}"  >> $TEMP_FILE.tex
	    # Copy feynmf graph commands in LaTeX file
	    # - leading whitespace or tabs are deleted
	    #   since the size specification "(x,y)" should directly follow "\begin{fmfgraph*}"
	    # - comments involving % cannot be dealth with ! since the $input is a single string
            #   commands (no \n) anymore...
	    echo -E $input | tr  "~" "\n" | sed 's/^[ \t]*//' - | cat  >> $TEMP_FILE.tex
	    echo -E "\end{fmfgraph*}"                   >> $TEMP_FILE.tex
	    echo -E "\end{fmffile}"              >> $TEMP_FILE.tex

	    # Finish LaTeX file
	    echo -E "\end{document}"             >> $TEMP_FILE.tex

	    # uncomment for debugging
	    #cat $TEMP_FILE.tex

	    # Compile with latex 2x with metafont in between to get the feynmf diagrams right
	    latex --interaction=nonstopmode  $TEMP_FILE.tex  > /dev/null
            mf '\mode:=localfont; input fmftempl'            > /dev/null
	    latex --interaction=nonstopmode  $TEMP_FILE.tex  > /dev/null
	    # Transform to .eps and cat .eps to TeXmacs.
	    dvips -q -f -E $TEMP_FILE.dvi -o $TEMP_FILE.eps > /dev/null
	    # Fix bounding box using bbox_add script, padding with 1 point
	    # (-E option of dvips not good enough...)
	    bbox_add.pl  --padding=1 $TEMP_FILE.eps          > /dev/null
	    echo -n $DATA_BEGIN
	    echo -n verbatim:
	    echo -n $DATA_BEGIN
	    echo -n ps:
	    cat $TEMP_FILE.eps
	    echo -n $DATA_END
	    echo -ne "\n"
	    # Clean up
	    rm $TEMP_FILE.*
	    rm fmftempl.*   # Metafont stuff
done
