Scott Hurring » HOWTO » jEdit: Run current buffer with PHP

HOWTO
info
Updated: Nov 06, 2005

Problem

In the course of using jEdit while developing PHP code, i thought it would be pretty nice to be able to hit some key sequence and have jEdit pipe the current buffer through PHP and then show me the results. I found two quick ways of doing it.
Solution 1: Macros
Solution 2: Commando / Console

Solution 1: Using Macros

Make a copy of the Run_Script.bsh bean

For a windows install, first locate the file "Run_Script.bsh" in the default jEdit installation directory. On windows, it is: C:\Program Files\jEdit\macros\Misc\Run_Script.bsh. For linux, it's probably somewhere in /usr/share/jedit/

Copy that file to your personal ~/.jedit/ directory and rename it. You don't really need to do this, although i find that it helps when upgrading jEdit to have any customized scripts in your personal directory (so they wont get over-written). I renamed mine "Run_Skript.bsh". On a windows computer, the macro script is: C:\Documents and Settings\$USER\.jedit\macros\Run_Skript.bsh, on linux, put it into ~/.jedit/macros/Run_Skript.bsh

Modify the Run_Script.bsh bean

Edit "Run_Skript.bsh" and add a case for when the buffer is in "php mode". (I added an else-if between perl and python to keep the file in alphabetical order).

Make sure you change "C:\\php\\php.exe" to the proper location of the PHP executable on your machine.

For example, my "Run_Skript.bsh" looks something like this now:

else if(mode.equals("perl")){
execScript("perl", "perl " + path);
}

// ------ New code
// PHP mode. Put the proper path to the PHP executable!
else if(mode.equals("php")) {
execScript("php", "C:\\php\\php.exe  " + path);
}
// ------ End new code

else if(mode.equals("python")) {
execScript("python", "python " + path);
}

Now you have the macro script setup, it's time to bind it to a keystroke. Go to the menu "Utilities -> Global Options -> Shortcuts" and select "Macros" from the "Edit Shortcuts:" dropdown.

Find the name of the macro script ("Run_Skript") and bind it to a keystroke of your choice.

From now on, when you're in a PHP buffer, you can whack the key and it will run your script thru PHP and show you the output in the console.


Solution 2: Using Console/Commando:

The commando PHP.xml file is here (Right-click to save.)

First, go to "Plugins" and install Console and Commando and any dependancies they might require.

I basically looked at some of the existing Commando files and hacked together one for PHP. My PHP.xml is a kludge, i personally prefer the above Macro method rather than doing this thru Commando. YMMV.

But anyway, when you've installed Commando, copy my PHP.xml file into your local ".jedit\console\commando" directory. For me (on Win2k) it's: C:\Documents and Settings\scott\.jedit\console\commando

Then, please open up PHP.xml and edit it to set the proper path of your PHP executable.

I've had errors trying to use PHP installed in "C:\Program Files\PHP\" with this method, so perhaps you might prefer to install PHP into "C:\PHP\" and use that.

Now, to run it, go to "Plugins -> Console -> PHP" and it should pop up a dialog box asking for some options. Usually, you'll just want to hit Enter. Commando will then run PHP on the current buffer and show you the output in a newly created buffer, named something like "Untitled-1".

I am currently looking for a way to have it keep reusing the same buffer name, preferably something like "PHP Output", and NOT keep opening new buffers... but i haven't really put much time or effort into this.

Anyone have any ideas? Drop me a line. Thanks.


Notes

Software Used