In this short article I'll present how to add a code beautify action to it.
Script Editor extensions are simple CTRL scripts. They need to be placed in the directory
scripts/scriptEditor/ in any of your project paths and the extension scriptfile name itself needs to end
in _ext.ctl. Any script editor started will load all files with the pattern "*_ext.ctl" it finds.
In my example I use "beautify_ext.ctl".
The code beautifying is not done by the script code itself, instead it uses one of the freely available
tools on the net. In this case it is bcpp from here
To install this tool, simply download and extract it. Then change into its directory and simply
do the usual
./configure
make
sudo make install
(Linux of course)
After this, copy the following CTRL code into a file in your project directory, e.g. as
/home/projects/test/scripts/scriptEditor/beautify_ext.ctl
Here come the gory details:
Code: Select all
// script editor extension to beautify your code
// Needs the external tool "bcpp" from http://invisible-island.net/bcpp/bcpp.html
main()
{
int menu = moduleAddMenu("MyTools");
moduleAddAction("Beautify", "", "CTRL+B", menu, -1, "beautify");
}
beautify()
{
string s = getScript();
string in = tmpnam();
string out = tmpnam();
file fd = fopen(in, "w");
fputs(s, fd);
fclose(fd);
system("bcpp -bnl -i 2 -ylcnc " + in + " " + out);
fileToString(out, s);
remove(in);
remove(out);
setScript(s);
}
That's all folks
Have fun!
Martin
P.S.:Maybe you find this link to the C-C++Beautifier-HOWTO also useful