//------------------------------------------------------------------------------
// This function fills file name, line number and ending column number
// for the selected token as well as a token itself.
//------------------------------------------------------------------------------
bool CCommands::getSelection(CComBSTR& file, CComBSTR& token, long& offset, long& line, long& column) {
ITextDocument* iDocument;
ITextSelection* iSelection;

	// Getting the currently opened document object.

	if(m_pApplication->get_ActiveDocument((IDispatch **)&iDocument)!=S_OK) 
		return false;
	if(iDocument==NULL) return false;

	// Reading the file name
	BSTR bstrName;
	iDocument->get_FullName(&bstrName);
	file=bstrName;

	// Getting text selection object from the active document
	if(iDocument->get_Selection((IDispatch **)&iSelection)!=S_OK) 
		return false;

	// We do not need active document object any more.
	iDocument->Release();

	if(iSelection==NULL) return false;

	// Reading currently selected token
	BSTR bstrToken;
	if(iSelection->get_Text(&bstrToken)!=S_OK) 
		return false;
	token = bstrToken;

	long size = token.Length();
	if(iSelection->get_TopLine(&line)!=S_OK)     return false;
	if(iSelection->get_CurrentColumn(&column)!=S_OK) return false;

	VARIANT selMove;
	selMove.vt = VT_INT;
	selMove.intVal = dsMove;
	VARIANT selExtend;
	selExtend.vt = VT_INT;
	selExtend.intVal = dsExtend;
	VARIANT repCount;
	repCount.vt = VT_INT;
	repCount.intVal = 1;

	// To determine which end of the selection the cursor is at,
	// we extend the selection one character left from the cursor 
	// and see whether the selection has gotten longer or shorter.

	bool cursor_at_right_end = true;

	if (column <= 1)
	{
		cursor_at_right_end = false;
	}
	else
	{
		long newsize;
		iSelection->CharLeft(selExtend, repCount);

		if(iSelection->get_Text(&bstrToken)==S_OK) 
		{
			CComBSTR newToken = bstrToken;
			newsize = newToken.Length();
			if (newsize > size)
			{
				// If extending left increases the size, the
				// cursor must be at the left end.
				cursor_at_right_end = false;
			}
		}
		iSelection->CharRight(selExtend, repCount);
	}

	// Calculating column in file characters, not in screen
	// position (detecting tabs)
	long lineOffset = 0;
	long tempColumn = column;
	while (tempColumn > 1) {
		iSelection->CharLeft(selMove, repCount);
		iSelection->get_CurrentColumn(&tempColumn);
		lineOffset++;
	}

	// Restoring selection after calculation:
	// First, go to the end without the cursor
	if (cursor_at_right_end)
	{
		iSelection->MoveTo(line, column-size, selMove);
	}
	else // Cursor at left end of selection
	{
		iSelection->MoveTo(line, column+size, selMove);
	}
	// Now make the selection
	iSelection->MoveTo(line, column, selExtend);

	iSelection->Release();
	column = lineOffset;
	if(size!=0)
		column--;

	offset = -1; // can not determine the offset
	return true;
}