/**
Get_Class_Name.bsh - a BeanShell macro for the jEdit text editor replaces the 
selected text with the current class name.

This macro walks though several steps in an attempt to guess the correct class
name.

First, it searches backwards for the nearest occurance of the keyword 'class,'
so it should work for inner class definitions.  It does not, however, do any 
fancy code parsing, so if you run this macro at any point after an inner class 
definition, it will return the name of the inner class, not the parent class.

If that fails, it attempts to guess the class name from the file name; i.e. it 
would return "FooBar" if the buffer was named "FooBar.cc."  If this fails (if, 
for example, the buffer is untitled), it returns an empty string.

Copyright (C)  2004 Thomas Galvin - software@thomas-galvin.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
*/

void setCaret(int selectionStart, int selectionEnd)
{
  textArea.setCaretPosition(selectionStart);
  textArea.moveCaretPosition(selectionEnd);
}

String getClassName()
{
  int selectionStart = textArea.getSelectionStart();
  int selectionEnd = textArea.getSelectionEnd();
  
  String text = textArea.getText();
  int index = text.lastIndexOf("class", selectionStart);
  if(index != -1)
  {
    textArea.setCaretPosition(index);
    int lineNumber = textArea.getCaretLine();
    int lineEnd = textArea.getLineEndOffset(lineNumber);
    String lineText = text.substring(index, lineEnd);
    
    StringTokenizer tokenizer = new StringTokenizer(lineText);
    tokenizer.nextToken(); //eat "class"
    if(tokenizer.hasMoreTokens())
    {
      setCaret(selectionStart, selectionEnd);
      return tokenizer.nextToken();
    }
  }
  
  
  String fileClassName = buffer.getName();
  int index = fileClassName.lastIndexOf('.');
  if(index != -1)
  {
    fileClassName = fileClassName.substring(0, index);
    if(fileClassName.toLowerCase().indexOf("untitled") == -1)
    {
      return fileClassName;
    }
  }
  setCaret(selectionStart, selectionEnd);
  
  String className = buffer.getName();
  int index = name.lastIndexOf('.');
  if(index != -1)
  {
    className = className.substring(0, index);
    if(className.toLowerCase().indexOf("untitled") == -1)
    {
      return className;
    }
  }
  
  return "";
}

String className = getClassName();
if( buffer.isReadOnly() )
	Macros.error( view, "Buffer is read-only." );
else if( className != null && !className.equals("") )
{
  textArea.setSelectedText(className);
}
