How to beautify your CTRL code

Discussion about recent product features & solutions!
1 post • Page 1 of 1
mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

How to beautify your CTRL code

Post by mkoller »

One of the nice features of our script editor is the possibility to add extensions to it.
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);
}
After that, start your gedi and you'll find in any script editor a new menu "MyTools" with the "Beautify" action in it.

That's all folks ;-)
Have fun!

Martin

P.S.:Maybe you find this link to the C-C++Beautifier-HOWTO also useful

1 post • Page 1 of 1