"Irrelevant since 1959!"
Wednesday, December 3, 2008

« Earlier articles Later articles »

9/30/2007

More ActionScript magic for jEdit:
Context-sensitive reference material!

Filed under: by VeryVito at 11:24 pm — [Tag on del.icio.us]

We're making progress on our quest to create the perfect Mac OS X ActionScript editor: Today, we're going to integrate the Adobe AS2 and AS3 Language References directly into the open-source jEdit editor, bringing context-sensitive help files up at the stroke of a key!

To do this, we're going to need three things: The InfoViewer plugin (which should already be installed with your base jEdit application), and two jEdit macros that we'll provide here. If you need a refresher on how to install plugins or new macros, check the previous tutorials in this series.

The InfoViewer is basically a small browser in a floating or docked panel within jEdit. If you don't already see it, you should be able to find it under your Plugins menu. I prefer to dock mine at the right-hand side of the editor, so it stays out of the way but is generally available. You can do this by selecting the small dropdown arrow in the upper left of any floating dock and choosing which side of the interface on which to dock it:

Docking InfoViewer

I've created two macros — one to search for information within the ActionScript 2 reference files, and one for the ActionScript 3 files. You can install them both, or just the one that interests you (I've got them both installed myself, and I've assigned the keyboard shortcuts CMD-F1 to search the AS2 files, and SHIFT-F1 for the AS3 files). To install, you can copy the code below, or simply download and extract the files from this zipped archive and save them in your ~/.jedit/macros directory (or better yet, make a subdirectory called "ActionScript" within the macros folder and keep all your AS macros in one neat section — rather like a TextMate "bundle").

The macro scripts:

// Macro command to search the AS3 Language Reference to find a (likely) page from the manual
// that describes the current "word" and display the result in the InfoViewer panel within jEdit.
// Could be improved, perhaps, but it sure seems to work.
//
// Beanshell script written by Mike Fahy (VeryVito) of Turdhead.com
// and released into the public domain. Yep, free as in beer.
//
// Questions, comments or large amounts of cash may be sent to nospam@turdhead.com
//
// NOTE: These scripts rely on the reference files installed with Adobe Flash CS3.
// If you are using a different version of Flash, are on a system other than OS X
// or have otherwise changed your default path to the ActionScript reference files,
// you must change the value of "folder" below to reflect the correct path.

import java.util.regex.*;

String folder = new String ("/Library/Application Support/Adobe/Flash CS3/en/Configuration/HelpPanel/Help/ActionScriptLangRefV3");

String helpTOC = "help_toc.xml";

textArea.goToPrevCharacter(false);
textArea.selectWord();

String record = null;
String urlToOpen  = null;
String wordToFind = textArea.getSelectedText();
String fileToLoad = null;
String regEx = "level(\\d+)\\s+name\\s*\\=\\s*\"(" + wordToFind + ").*\".*?href\\s*\\=\\s*\"(.*?)\"";

Pattern regPat = Pattern.compile(regEx);
Matcher matcher;
BufferedReader bufr;

try {
	bufr = new BufferedReader (new FileReader (folder + "/" + helpTOC));

	while ( (record = bufr.readLine()) != null ) {
		matcher = regPat.matcher(record);
		if (matcher.find() && (Integer.parseInt(matcher.group(1)) > 2))	{ // I personally don't want to see anything lower than "level2." This may help refine the results a bit.

			fileToLoad = matcher.group(3);

			infoviewer.InfoViewerPlugin.openURL(view, "file://" + folder + "/" +  fileToLoad);
			break;
		}
	} 

	if (fileToLoad == null)	{
		Macros.error(view, "Sorry. No help found in " + folder);
		//infoviewer.InfoViewerPlugin.openURL(view, "file://" + folder + "/help_toc.xml");
	}

}	catch (java.io.IOException e)	{
	Macros.error(view, "An error occurred while trying to parse " + helpTOC);
}
The source for Look_up_in_AS3_reference.bsh
// Macro command to search the AS2 Language Reference to find a (likely) page from the manual
// that describes the current "word" and display the result in the InfoViewer panel within jEdit.
// Could be improved, perhaps, but it sure seems to work.
//
// Beanshell script written by Mike Fahy (VeryVito) of Turdhead.com
// and released into the public domain. Yep, free as in beer.
//
// Questions, comments or large amounts of cash may be sent to nospam@turdhead.com
//
// NOTE: These scripts rely on the reference files installed with Adobe Flash CS3.
// If you are using a different version of Flash, are on a system other than OS X
// or have otherwise changed your default path to the ActionScript reference files,
// you must change the value of "folder" below to reflect the correct path.

import java.util.regex.*;

String folder = new String ("/Library/Application Support/Adobe/Flash CS3/en/Configuration/HelpPanel/Help/ActionScriptLangRefV2");

String helpTOC = "help_toc.xml";

textArea.goToPrevCharacter(false);
textArea.selectWord();

String record = null;
String urlToOpen  = null;
String wordToFind = textArea.getSelectedText();
String fileToLoad = null;
String regEx = "href\\s*\\=\\s*\"(.*?)\"\\s*name\\s*\\=\\s*\"(" + wordToFind + ".*)\"";

Pattern regPat = Pattern.compile(regEx);
Matcher matcher;
BufferedReader bufr;

try {
	bufr = new BufferedReader (new FileReader (folder + "/" + helpTOC));

	while ( (record = bufr.readLine()) != null ) {
		matcher = regPat.matcher(record);
		if (matcher.find())	{

			fileToLoad = matcher.group(1);

			infoviewer.InfoViewerPlugin.openURL(view, "file://" + folder + "/" +  fileToLoad);
			break;
		}
	} 

	if (fileToLoad == null)	{
		Macros.error(view, "Sorry. No help found in " + folder);
		//infoviewer.InfoViewerPlugin.openURL(view, "file://" + folder + "/help_toc.xml");
	}

}	catch (java.io.IOException e)	{
	Macros.error(view, "An error occurred while trying to parse " + helpTOC);
}
The source for Look_up_in_AS2_reference.bsh

To use either of the macros, simply move your cursor to the class, method or property name you need help with (or simply type it in), and run the macro. If the script can find the information requested, it will be displayed within the InfoViewer panel.

Note: If your ActionScript reference files are located somewhere besides the folder indicated in the scripts above (and if you're using Windows, they will be!), you will need to edit the "file" variable to point to the correct directory.

As you might notice, the macros used by jEdit actually use the Java syntax. In fact, it is Java (in a form known as a BeanShell), and it can be pretty dang powerful. In fact, when I first attempted to make macros for my own use, I used a convoluted system of shell scripts, perl scripts and OS X's built-in Apache server to cobble something together to do this (because, frankly, those are the tools I know best). But I quickly realized that unleashing that setup on an unsuspecting public would do little to promote jEdit as an easily extensible tool. Thus, I went back to the drawing board tonight and rewrote the scripts to rely solely on jEdit's native macro language — and I'm impressed with how easy it was to figure out.

If you want to learn more about creating your own macros (and please share them here if you do!), check out the Macro Basics guide at jEdit.org. Meanwhile, enjoy your getting-better-every-day ActionScript environment!

Print this article Print this article

12 Responses to “More ActionScript magic for jEdit:
Context-sensitive reference material!”

  1. Thanks! Now I got even more reasons to love jEdit!


  2. Glad to hear it, Keith! That’s pretty much what I’m hoping to accomplish with this series. Stay tuned!


  3. Thanks, this was an excellent idea. I took your script and modified it to work for me with the Flex SDK language reference instead of the Flash CS3 docs. Link below:

    http://hasseg.org/blog/?p=98


  4. Excellent, Ali! I’ll add a link to your scripts on the main jEdit page here. By the way, the “growl notification” on your compile macros are dead sexy.


  5. This is all so sweet, finally!

    On question thou, does anybody know how to get the sidekick codebrowser to work? i have code completion and everything but i just cant get it to display the list of functions like on your screener. I selected Ctags as a parser, and i have “parse on keystroke” an tryed parsing manualy too.. I just get the filename in the sidekick window. Any hello would be greatly appreciated :)


  6. Hi Ben, good question. I thought I had decent code browsing, but once you asked, I realized it just wasn’t good enough… See this new update to see what I did to change it.

    Meanwhile… you have code completion working?? Are you using the TextAutoComplete plugin for this, or do you have a specifically ActionScript-oriented solution?


  7. What about auto-completion? Any suggestions for that? In X-Code i could type ESC and get a list of AS. If I type textfield.set then hit esc it would give me all AS with set in them. Nice feature saves on typing.


  8. I wrote too soon. it wasn’t working and now it does.


  9. Hi - had no problem following previous articles and jedit looks very useful, but installing this macro throws an error message:

    Beanshell error
    Sourced file: /Users/greyarea/.jedit/macros/Look_up_in_AS2_reference.bsh Token Parsing Error: Lexical error at line 28, column 22. Encountered: “s” (115), after : “”href\”:

    Have checked and rechecked the ‘folder’ string is correct. I know jack about Java, so is this a problem with the macro or the path to the AS2 help? running jedit 4.3pre11 using Java 1.5.0_07

    Cheers

    dgb


  10. Hi David. Wow, thanks for bringing this to my attention – apparently the code as presented above is riddled with errors: It worked when published, but apparently, the new formatting engine I’m using tries to “fix it” by removing what it sees as unimportant characters (and thus renders it useless). I’m working on fixing this, but in the meantime, I recommend downloading the working zipped version for now. The problem should be fixed now. Sorry for the confusion!


  11. Hey!

    Worked a treat - thanks very much. On to the next article…

    cheers

    dgb


  12. Hi
    just for anyone else reading this - on my mac added the macro to the mouse ctrl click context menu. At bottom of the context menu is ‘Custumize This Menu’. Choose that. At the bottom of the ‘Options..’ pane is a plus sign. Click on that. In the dialog that appears, click on the pop-up menu and choose ‘Macros’. Scroll to ‘Look up in AS2 reference’( or AS3 if installed). Click both OK buttons. Ping - almost like ctrl clicking in the actionscript palette in Flash.

    cheers

    dgb



So what do you think?

Please note that to protect our readers from spammers and other sleazeballs, we moderate all comments on this board. (And be assured, we'd sooner die than sell, give away or otherwise sully the reputation of your email address!)





Your message may not appear immediately upon submission, as we have to read it first to make sure you're not a machine selling poker sites, mortgages or Cialis. If you're a real person, though, we'd love to hear from you! Just fill out the form above.


« Earlier articles Later articles »

 

Shameless begging, panhandling and soul-sucking indignity

Want to show your appreciation for Turdhead.com, its products or services? Although it's certainly not necessary, we'd be pleased as punch if you felt like donating any amount you'd like, or even if you just wanted to give us a nod on Digg.com. Honestly, we have no pride whatsoever (the name of the site is Turdhead.com, after all), and we'll take any recognition we can get for our efforts here.

 


 

Search

Or use Blingo! to search and win prizes.



Get Chitika eMiniMalls

Design and Sell Merchandise Online for Free

Copyright © 2004-2008 by Turdhead.com. All rights reserved. So there. Questions? Write us!
But first, get a load of these cryptic yet official looking numbers:
[ 1608118v | 176p | 855c | s | 12.29.05d ]