/*
 * Delete_Current.bsh - Deletes the current
 * buffer's file on disk, but doesn't close 
 * the buffer.
 *
 * Copyright (C) 2003-2004 Ollie Rutherfurd <oliver@rutherfurd.net>
 *
 * $Id: Delete_Current.bsh 21353 2012-03-14 09:46:51Z jojaba_67 $
 */

import javax.swing.SwingUtilities;
import org.gjt.sp.jedit.io.*;

//Localization
final static String NotOnDiskError = jEdit.getProperty("macro.rs.DeleteCurrent.NotOnDisk.error", "Buffer doesn't exist on disk.");
final static String DeleteNotSupportError = jEdit.getProperty("macro.rs.DeleteCurrent.DeleteNotSupport.error", "doesn't support deleting.");
final static String DeletedMessage = jEdit.getProperty("macro.rs.DeleteCurrent.Deleted.message", "Deleted:");

BufferStatusChecker(View view){
	run(){
		jEdit.checkBufferStatus(view);
	}
	return this;
}


void deleteCurrentBuffer(View view){
	Buffer buffer = view.getBuffer();

	// don't bother deleting new buffers (don't exist on disk)
	if(buffer.isNewFile()){
		Macros.error(view, NotOnDiskError );
		return;
	}

	try{
		String path = buffer.getPath();
		VFS vfs = VFSManager.getVFSForPath(path);
		int caps = vfs.getCapabilities();
		int del = VFS.DELETE_CAP;
		int res = caps & del;
		if(res == 0){
			Macros.error(view, "VFS " + vfs.getName() 
						 + " " + DeleteNotSupportError);
			return;
		}
	
		Object session = null;
		try{
			session = vfs.createVFSSession(path,view);
			if(vfs._delete(session,path,view)){
				view.getStatus().setMessageAndClear(DeletedMessage + " " + path);
			}
			// invoke buffer status check
			SwingUtilities.invokeLater(BufferStatusChecker(view));
		}
		finally{
			if(session != null)
				vfs._endVFSSession(session,view);
		}
	}
	catch(Exception e){
		Macros.error(view, e.toString());
	}
}

deleteCurrentBuffer(view);

/*

	<listitem>
		<para><filename>Delete_Current.bsh</filename></para>
		<abstract><para>
		Deletes the current buffer's file on disk, but 
		doesn't close the buffer.
		</para></abstract>
	</listitem>

*/
