EmEditor (text editor) Forum Index
   Macro Samples
     Open file under Cursor
Register To Post

Threaded | Oldest First Previous Topic | Next Topic | Bottom
Poster Thread
shaohao
Posted on: 3/21/2008 10:10 am
Not too shy to talk
Joined: 11/12/2006
From:
Posts: 21
Open file under Cursor
This macro is useful when you are reading source code.
How to use:
1. Add this macro to toolbar as a button.
2. Open a source code file.
3. Move the cursor the the include file string (such as : #include "File.h", move the cursor to "File.h" or just click the file name to select the string)
4. Press the macro button.
5. The file will be searched in the pre-defined search list and open it when hit.
6. Add you own search path to "inc" array variable.


#title = ""
#tooltip = "Open file under cursor"

Debug = 0;

if ( Debug) Window.OutputBar.Clear();
function OutputDebugStr(str) {
	if ( Debug) {
		Window.OutputBar.writeln( str);
		Window.OutputBar.Visible = true;
		Window.OutputBar.SetFocus();
	}
}

// include path as VC++ does
inc = new Array( "D:\\System\\Microsoft Visual Studio 8\\VC\\include"
               , "D:\\System\\Microsoft Visual Studio 8\\VC\\atlmfc\\include"
               , "D:\\System\\Microsoft Visual Studio 8\\VC\\PlatformSDK\\include"
               , "D:\\System\\Microsoft Visual Studio 8\\SDK\\v2.0\\include"
               , "D:\\System\\WTL\\include"
               , "D:\\System\\wxWidgets-2.8.7\\include"
               );

// get position of current cursor
row    = document.selection.GetActivePointY( eePosLogical);
column = document.selection.GetActivePointX( eePosLogical);

// get the text of the line where the cursor pointed to
text = document.GetLine( row);

// parse the text to get to the file which is going to be opened
texta = text.split( ' ');
col = 0;
fn = '';
for (var i in texta) {
	len = texta[i].length;
	if ( len == 0)
		col++;
	else
		col += (len+1);
	if ( col >= column) {
		fn = texta[i];
		break;
	}
}

// trim the file name
fn = fn.replace( /[ \"<>]/g, "");

// find file in all pre-defined include directories
//   add current directory into pre-defined list automatically
inc.unshift( Window.document.Path);
for (var dir in inc) {
	path = inc[dir] + "\\" + fn;
	OutputDebugStr(path);
	fso = new ActiveXObject("Scripting.FileSystemObject");
	if ( fso.FileExists( path)) {
		editor.OpenFile( path, 0, eeOpenAllowNewWindow);
		break;
	}
}
Threaded | Oldest First Previous Topic | Next Topic | Top


Register To Post
 
English čeština Deutsch español français italiano 日本語 한국어 Русский 简体中文 繁體中文