#Makefile for LaTeX'ing stuff without over-running TeX on a file.
#Made for use with vile, but works anywhere.
#

FILE=$(basename $(SRC))

tex: $(FILE).dvi

xdvi: tex
	xdvi -geom +170+30 $(FILE)

gv: tex dvips
	ghostview $(FILE).ps

view: tex xdvi

dvips: tex
	dvips -t letter -o $(FILE).ps $(FILE)

lpr: tex
	dvips -t letter -f $(FILE).dvi | lpr

$(FILE).dvi: $(FILE).tex
	@perl -e '$$|=1; @stack=();\
	push(@stack, "$(FILE)");\
	open(PIPE, "latex \\\\nonstopmode \\\\input $(FILE) |");\
	while(<PIPE>) {\
		print unless(m/^l\.\d/);\
		pop(@stack) while(s/^[^\(]*\)//);\
		push(@stack, $$1) while(s/\(([^\(\)\s]*)//);\
		pop(@stack) while(s/\)//);\
		print if(s/^l\.([\d]*)/$$stack[-1]:$$1:/);\
	}\
	close(PIPE);'

clean:
	/bin/rm -f $(FILE).aux $(FILE).log

clean_all:
	/bin/rm -f $(FILE).aux $(FILE).log $(FILE).dvi $(FILE).ps

#Spell checking:
spell:
	@perl -e '$$|=1; $$attribute="U";\
	open(PIPE, "ispell -l < $(SRC) |"); open(IN, "<$(SRC)");\
	chop($$word=<PIPE>); $$len=length($$word);\
	while(<IN>) {\
		while(!eof(PIPE) && s/$$word/\x1$$len$$attribute:$$word/) {\
			chop($$word=<PIPE>); $$len=length($$word);\
		}\
		print;\
	}\
	close(PIPE); close(IN);'
	
