hasseg.org

jEdit Macros for Running Compilers

Filed under Flex, jEdit, Programming, Scripts

compileMxml shell script displays Growl notifications I've been using jEdit as my ActionScript 3 editor for a while now. I Like the extension and customization possibilities it gives me, and I've used them to create macros for compiling my Flex applications and then bound them to specific keyboard shortcuts to enable quick access to running the compiler.

For a while, I had the compiler command and the parameters (i.e. the file to compile) hardcoded in my macro, but recently I changed this to a more reusable solution where you have one macro for selecting the file ("buffer" in jEdit lingo) to be compiled and another for actually running the compiler on that file. Then I simply bind keystrokes for both of them. This enables me to easily and quickly run the compiler whenever I want to, and also change the file to be compiled (and, of course, also the compiler used – automatically based on the file extension) if I switch projects.

Select_file_to_compile.bsh

First, let’s take a look at Select_file_to_compile.bsh, which is used to select the file (as you can see, it saves the file path into a jEdit property which the other script will later read):

// SETTINGS: --------------------------------------
	
	/* 
	* set allowed file extensions here: (include leading point)
	*/
	allowedExtensions = new String[] {".mxml", ".java"};
	
// /settings - - - - - - - - - - - - - - - - - - - 


// Macro implementation begins here:  ------------

currentBufferPath = jEdit.getProperty("macro.compile_specific_buffer.buffer_path", "");

allBuffers = jEdit.getBuffers();

mxmlBuffers = new Object[allBuffers.length];

int currentlySelectedIndex = 0;
int j = 0;
for (int i = 0; i < allBuffers.length; i++) {
	Boolean extensionMatch = false;
	for (int k = 0; k < allowedExtensions.length; k++) 
		if (allBuffers[i].getName().endsWith(allowedExtensions[k])) extensionMatch = true;
	
	if (extensionMatch == true) {
		if (currentBufferPath.equals(allBuffers[i].getPath())) currentlySelectedIndex = j;
		mxmlBuffers[j++] = allBuffers[i].getPath();
	}
}

if (j == 0) {
	
	Macros.error(view, "Can not find any open buffers with the specified extensions!");
	
}else{
	
	listItems = new Object[j];
	for (int i = 0; i < j; i++) listItems[i] = mxmlBuffers[i];
	
	result = JOptionPane.showInputDialog(view,
		"Choose which file to pass to the compiler the next time you run the compile macro:",
		"Select file to compile",
		JOptionPane.QUESTION_MESSAGE,
		null, listItems, listItems[currentlySelectedIndex]);	
	
	if (result != null) jEdit.setProperty("macro.compile_specific_buffer.buffer_path", result);
	
}

Compile_previously_selected_file.bsh

Then there’s Compile_previously_selected_file.bsh which invokes the compiling process. Notice the displayDockedConsole parameter in the settings – a false value will make sure that the Console plugin will not open up whenever the compiling starts. I Want to keep it closed because I don’t make this macro call the mxml compiler directly, but instead through a bash shell script (compileMxml.bash – link below) that displays a Growl notification whenever the compiling finishes, telling me whether or not the application was compiled successfully. In case of errors, the notification will be sticky, so that if I leave my computer to compile and then come back later, I’ll still be able to see it.

// SETTINGS: --------------------------------------
	
	/* 
	* match file extensions and compilers here (no 
	* trailing space after compiler command):
	*/
	compilers = new HashMap();
	compilers.put(".mxml", "/path/to/flex/sdk/bin/mxmlc --file-specs");
	compilers.put(".java", "/path/to/java/sdk/bin/javac ");
	
	
	/*
	* whether or not to display the console when compiling
	* (only works if the console is docked)
	*/
	displayDockedConsole = false;
	
// /settings - - - - - - - - - - - - - - - - - - - 



// Macro implementation begins here:  ------------

if (compilers.isEmpty()) {
	
	Macros.error(view, "No compilers specified!");
	
}else{
	
	theBufferPath = jEdit.getProperty("macro.compile_specific_buffer.buffer_path","");
	
	compilerCommand = "";
	extensions = compilers.keySet().toArray();
	for (int i = 0; i < extensions.length; i++) {
		if (theBufferPath.endsWith(extensions[i])) compilerCommand = compilers.get(extensions[i]);
	}
	
	
	
	if (theBufferPath.equals("")) {
		
		Macros.error(view, "No file has been specified to be compiled! Use the \"Select_file_to_compile\" macro to specify it.");
		
	}else if (compilerCommand.equals("")) {
		
		Macros.error(view, "No compiler set for this file extension! \n(file path: \""+theBufferPath+"\")");
		
	}else{
		
		os = System.getProperty("os.name");
		if(os.indexOf("Windows") != -1) path = "\"" + path + "\"";
		
		clearConsole(view);
		runInSystemShell(view, compilerCommand + " " + theBufferPath);
		
		if (displayDockedConsole == false) {
			if (view.getDockableWindowManager().isDockableWindowDocked("console") == true) {
				if (view.getDockableWindowManager().isDockableWindowVisible("console") == true)
					view.getDockableWindowManager().removeDockableWindow("console");
			}
		}
		
	}
	
}

Download scripts

Categories