Skip to content

Instantly share code, notes, and snippets.

@Naatan
Last active November 21, 2017 08:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Naatan/7375244 to your computer and use it in GitHub Desktop.
Save Naatan/7375244 to your computer and use it in GitHub Desktop.
Komodo Macro - Execute file/selection as PHP
new function()
{
/**
* Execute Macro
*
* @returns {void}
*/
this.exec = function()
{
// Ensure we're set up correcly
komodo.assertMacroVersion(3);
if (komodo.view) { komodo.view.setFocus() };
// Get text to execute
var ke = komodo.editor;
if(!ke.selText)
{
// Use all text in file
var text = ko.views.manager.currentView.scimoz.text.trim()
}
else
{
// Use selected text
var text = komodo.interpolate('%s').trim();
}
// Prepend php opening tag if necessary
if (text.substr(0,2) != '<?')
{
text = '<?php ' + text;
}
// Write temporary file
var file = this.writeFile(text);
// get PHP binary path
var phpBinary = Components.classes['@activestate.com/koPrefService;1']
.getService(Components.interfaces.koIPrefService)
.prefs.getStringPref('phpDefaultInterpreter');
// Run php
ko.run.runEncodedCommand(window, phpBinary + ' -d display_errors=true ' + file);
};
/**
* Write data to file
*
* @param {string} data Data to write to file
*
* @returns {string} File path
*/
this.writeFile = function(data)
{
// Get temp file path
var tmpFile = Components.classes["@mozilla.org/file/directory_service;1"].
getService(Components.interfaces.nsIProperties).
get("TmpD", Components.interfaces.nsIFile);
tmpFile.append("krsp.php");
// Initialize (create) file
var file = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(tmpFile.path);
// Open stream to file
var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
createInstance(Components.interfaces.nsIFileOutputStream);
foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);
// Use converter to ensure UTF-8 encoding
var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
createInstance(Components.interfaces.nsIConverterOutputStream);
// Write to file
converter.init(foStream, "UTF-8", 0, 0);
converter.writeString(data);
converter.close();
return tmpFile.path;
};
this.exec();
}
@lsloan
Copy link

lsloan commented Jun 17, 2014

See my fork of your gist which checks for missing PHP interpreter.

@kAlvaro
Copy link

kAlvaro commented Jul 4, 2014

I get this:

`-d display_errors=true C:\Users\.....\AppData\Local\Temp\krsp.php` returned 1

... And this:

"-d" no se reconoce como un comando interno o externo, programa o archivo por lotes ejecutable.

As far as I can tell, my PHP settings are not anything special. I have "Find on Path" everywhere :-? And my PHP interpreter is in the path.

@Naatan
Copy link
Author

Naatan commented Jul 9, 2014

Try running the command manually from your command prompt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment