⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commands.cpp

📁 VIM文本编辑器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"
#include <comdef.h>	// For _bstr_t
#include "VisVim.h"
#include "Commands.h"
#include "OleAut.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

#endif


// Change directory before opening file?
#define CD_SOURCE		0	// Cd to source path
#define CD_SOURCE_PARENT	1	// Cd to parent directory of source path
#define CD_NONE			2	// No cd


static BOOL g_bEnableVim = TRUE;	// Vim enabled
static BOOL g_bDevStudioEditor = FALSE;	// Open file in Dev Studio editor simultaneously
static int g_ChangeDir = CD_NONE;	// CD after file open?


static COleAutomationControl VimOle;	// OLE automation object for com. with Vim


static void VimSetEnableState (BOOL bEnableState);
static BOOL VimOpenFile (BSTR& FileName, long LineNr);
static DISPID VimGetDispatchId (char* Method);
static void VimErrDiag ();
static void VimChangeDir (BSTR& FileName, DISPID DispatchId);
static void DebugMsg (char* Msg, char* Arg = NULL);


/////////////////////////////////////////////////////////////////////////////
// CCommands

CCommands::CCommands ()
{
	m_pApplication == NULL;
	m_pApplicationEventsObj = NULL;
	m_pDebuggerEventsObj = NULL;
}

CCommands::~CCommands ()
{
	ASSERT (m_pApplication != NULL);
	m_pApplication->Release ();
}

void CCommands::SetApplicationObject (IApplication * pApplication)
{
	// This function assumes pApplication has already been AddRef'd
	// for us, which CDSAddIn did in its QueryInterface call
	// just before it called us.
	m_pApplication = pApplication;

	// Create Application event handlers
	XApplicationEventsObj::CreateInstance (&m_pApplicationEventsObj);
	m_pApplicationEventsObj->AddRef ();
	m_pApplicationEventsObj->Connect (m_pApplication);
	m_pApplicationEventsObj->m_pCommands = this;

#ifdef NEVER
	// Create Debugger event handler
	CComPtr < IDispatch > pDebugger;
	if (SUCCEEDED (m_pApplication->get_Debugger (&pDebugger))
	    && pDebugger != NULL)
	{
		XDebuggerEventsObj::CreateInstance (&m_pDebuggerEventsObj);
		m_pDebuggerEventsObj->AddRef ();
		m_pDebuggerEventsObj->Connect (pDebugger);
		m_pDebuggerEventsObj->m_pCommands = this;
	}
#endif

	// Get settings from registry HKEY_CURRENT_USER\Software\Vim\VisVim
	HKEY hAppKey = GetAppKey ("Vim");
	if (hAppKey)
	{
		HKEY hSectionKey = GetSectionKey (hAppKey, "VisVim");
		if (hSectionKey)
		{
			g_bEnableVim = GetRegistryInt (hSectionKey, "EnableVim",
						       g_bEnableVim);
			g_bDevStudioEditor = GetRegistryInt(hSectionKey,"DevStudioEditor",
							    g_bDevStudioEditor);
			g_ChangeDir = GetRegistryInt (hSectionKey, "ChangeDir",
						      g_ChangeDir);
			RegCloseKey (hSectionKey);
		}
		RegCloseKey (hAppKey);
	}
}

void CCommands::UnadviseFromEvents ()
{
	// Destroy OLE connection
	VimOle.DeleteObject ();

	ASSERT (m_pApplicationEventsObj != NULL);
	m_pApplicationEventsObj->Disconnect (m_pApplication);
	m_pApplicationEventsObj->Release ();
	m_pApplicationEventsObj = NULL;

#ifdef NEVER
	if (m_pDebuggerEventsObj)
	{
		// Since we were able to connect to the Debugger events, we
		// should be able to access the Debugger object again to
		// unadvise from its events (thus the VERIFY_OK below--see
		// stdafx.h).
		CComPtr < IDispatch > pDebugger;
		VERIFY_OK (m_pApplication->get_Debugger (&pDebugger));
		ASSERT (pDebugger != NULL);
		m_pDebuggerEventsObj->Disconnect (pDebugger);
		m_pDebuggerEventsObj->Release ();
		m_pDebuggerEventsObj = NULL;
	}
#endif
}


/////////////////////////////////////////////////////////////////////////////
// Event handlers

// Application events

HRESULT CCommands::XApplicationEvents::BeforeBuildStart ()
{
	AFX_MANAGE_STATE (AfxGetStaticModuleState ());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::BuildFinish (long nNumErrors, long nNumWarnings)
{
	AFX_MANAGE_STATE (AfxGetStaticModuleState ());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::BeforeApplicationShutDown ()
{
	AFX_MANAGE_STATE (AfxGetStaticModuleState ());
	return S_OK;
}

// The open document event handle is the place where the real interface work
// is done.
// Vim gets called from here.
//
HRESULT CCommands::XApplicationEvents::DocumentOpen (IDispatch * theDocument)
{
	AFX_MANAGE_STATE (AfxGetStaticModuleState ());

	if (! g_bEnableVim)
		// Vim not enabled or empty command line entered
		return S_OK;

	// First get the current file name and line number

	// Get the document object
	CComQIPtr < ITextDocument, &IID_ITextDocument > pDoc (theDocument);
	if (! pDoc)
		return S_OK;

	BSTR FileName;
	long LineNr = -1;

	// Get the document name
	if (FAILED (pDoc->get_FullName (&FileName)))
		return S_OK;

	LPDISPATCH pDispSel;

	// Get a selection object dispatch pointer
	if (SUCCEEDED (pDoc->get_Selection (&pDispSel)))
	{
		// Get the selection object
		CComQIPtr < ITextSelection, &IID_ITextSelection > pSel (pDispSel);

		if (pSel)
			// Get the selection line number
			pSel->get_CurrentLine (&LineNr);

		pDispSel->Release ();
	}

	// Open the file in Vim and position to the current line
	if (VimOpenFile (FileName, LineNr))
	{
		if (! g_bDevStudioEditor)
		{
			// Close the document in developer studio
			CComVariant vSaveChanges = dsSaveChangesPrompt;
			DsSaveStatus Saved;

			pDoc->Close (vSaveChanges, &Saved);
		}
	}

	// We're done here
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::BeforeDocumentClose (IDispatch * theDocument)
{
	AFX_MANAGE_STATE (AfxGetStaticModuleState ());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::DocumentSave (IDispatch * theDocument)
{
	AFX_MANAGE_STATE (AfxGetStaticModuleState ());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::NewDocument (IDispatch * theDocument)
{
	AFX_MANAGE_STATE (AfxGetStaticModuleState ());

	if (! g_bEnableVim)
		// Vim not enabled or empty command line entered
		return S_OK;

	// First get the current file name and line number

	CComQIPtr < ITextDocument, &IID_ITextDocument > pDoc (theDocument);
	if (! pDoc)
		return S_OK;

	BSTR FileName;
	HRESULT hr;

	hr = pDoc->get_FullName (&FileName);
	if (FAILED (hr))
		return S_OK;

	// Open the file in Vim and position to the current line
	if (VimOpenFile (FileName, 0))
	{
		if (! g_bDevStudioEditor)
		{
			// Close the document in developer studio
			CComVariant vSaveChanges = dsSaveChangesPrompt;
			DsSaveStatus Saved;

			pDoc->Close (vSaveChanges, &Saved);
		}
	}

	return S_OK;
}

HRESULT CCommands::XApplicationEvents::WindowActivate (IDispatch * theWindow)
{
	AFX_MANAGE_STATE (AfxGetStaticModuleState ());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::WindowDeactivate (IDispatch * theWindow)
{
	AFX_MANAGE_STATE (AfxGetStaticModuleState ());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::WorkspaceOpen ()
{
	AFX_MANAGE_STATE (AfxGetStaticModuleState ());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::WorkspaceClose ()
{
	AFX_MANAGE_STATE (AfxGetStaticModuleState ());
	return S_OK;
}

HRESULT CCommands::XApplicationEvents::NewWorkspace ()
{
	AFX_MANAGE_STATE (AfxGetStaticModuleState ());
	return S_OK;
}

// Debugger event

HRESULT CCommands::XDebuggerEvents::BreakpointHit (IDispatch * pBreakpoint)
{
	AFX_MANAGE_STATE (AfxGetStaticModuleState ());
	return S_OK;
}


/////////////////////////////////////////////////////////////////////////////
// VisVim dialog

class CMainDialog : public CDialog
{
    public:
	CMainDialog (CWnd * pParent = NULL);	// Standard constructor

	//{{AFX_DATA(CMainDialog)
	enum { IDD = IDD_ADDINMAIN };
	BOOL	m_bDevStudioEditor;
	int	m_ChangeDir;
	//}}AFX_DATA

	//{{AFX_VIRTUAL(CMainDialog)
    protected:
	virtual void DoDataExchange (CDataExchange * pDX);	// DDX/DDV support
	//}}AFX_VIRTUAL

    protected:
	//{{AFX_MSG(CMainDialog)
	afx_msg void OnEnable();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -