mud++0.35/etc/
mud++0.35/etc/guilds/
mud++0.35/help/propert/
mud++0.35/mudC/
mud++0.35/player/
mud++0.35/src/interface/
mud++0.35/src/os/cygwin32/
mud++0.35/src/os/win32/
mud++0.35/src/os/win32/bcppbuilder/
mud++0.35/src/osaddon/
mud++0.35/src/util/
/*
....[@@@..[@@@..............[@.................. MUD++ is a written from
....[@..[@..[@..[@..[@..[@@@@@....[@......[@.... scratch multi-user swords and
....[@..[@..[@..[@..[@..[@..[@..[@@@@@..[@@@@@.. sorcery game written in C++.
....[@......[@..[@..[@..[@..[@....[@......[@.... This server is an ongoing
....[@......[@..[@@@@@..[@@@@@.................. development project.  All 
................................................ contributions are welcome. 
....Copyright(C).1995.Melvin.Smith.............. Enjoy. 
------------------------------------------------------------------------------
Melvin Smith (aka Fusion)         msmith@hom.net 
MUD++ development mailing list    mudpp@van.ml.org
------------------------------------------------------------------------------
editor.cc
*/

#include "config.h"
#include "io.h"
#include "edit.h"
#include "pc.h"
#include "global.h"

const String & Editor::getPrompt()
{
	if ( !text ) 
		return prompt;
  	else 
  		return text->getPrompt(); 
}

void Editor::editText( const String & initial )
{
	if( pc->getTextEditor() )
	{
		String temp = initial;
		temp.strip( '\r' );
		filename = pc->getName();
		filename += ".tmp";
		OutputFile outf( filename.chars() );
		if( !outf )
		{
			pc->out( "Error creating edit file.\n\r" );
			return;
		}
		outf << temp << endl;
		outf.close();
/*
		command << "vi " << filename;
		pc->do_shell( command );
*/
		pcs.remove( pc );
		shellpcs.addTop( pc );
		pc->startShell( pc->getTextEditor(), filename );
		return;
	}
	text = new TextEditor( pc, initial );
	state = ED_DESC;
	pc->out( text->format() );
	return;
}

void Editor::editFile( const String & name )
{
	savefile = name;

	if ( pc->getTextEditor() )
	{
		InputFile inf(savefile);
		String init;
		if ( inf )
		{
			init = String(inf.getBuf());
			inf.close();
		}
		init.strip( '\r' );
		filename = pc->getName();
		filename += ".tmp";
		OutputFile outf( filename.chars() );
		if( !outf )
		{
			pc->out( "Error creating edit file.\n\r" );
			return;
		}
		outf << init << endl;
		outf.close();

		pcs.remove( pc );
		shellpcs.addTop( pc );
		pc->startShell( pc->getTextEditor(), filename );
		return;
	}
	else
	{
		InputFile inf(savefile);
		if ( !inf )
		{
			text = new TextEditor( pc, String("") );
		}
		else
		{
			text = new TextEditor( pc, String( inf.getBuf() ) );
			inf.close();
		}
		pc->out( text->format() );
		return;
	}
}

void TextfileEditor::command( const String & arg )
{

	if ( state == ED_NONE )
	{
		editFile( arg );
		state = ED_DESC;
		return;
	}

	if ( text )
	{
		text->command( arg );
		if ( text->done() )
		{
			if ( text->getState() != ED_CANCEL )
			{
				OutputFile outf(savefile.chars());
				if ( !outf )
				{
					pc->out("Error creating file.\n\r");
				}
				else
				{
					outf << text->asStr();
					outf.close();
				}
			}
			delete text;
			pc->quitEditor();
			return;
		}
		return;
	}
	else if ( filename )
	{
		remove( savefile.chars() );
		rename( filename.chars(), savefile.chars() );
		remove( filename.chars() );
		pc->quitEditor();
		return;
	}

}