(* *****************************************************************
** File    : Indent.PAS
** Created : 28.07.01
** Author  : M. Leute
**
** Macro for FoldMaster
**   Supporting GNU Indent Version 2.2.5
**   needs version 1.50 of FoldMaster to compile with no errors
**
** Adds some support for GNU- Indent tool
**
********************************************************************
** Version     Date      Change
** 1.50        28.07.01  Creation
** **************************************************************-**)
PROGRAM Indent;

(*{{{  Consts*)
const
     GNUINDENT      = 'GNU-Indent';
     GNUINDENTINT   = 'GNU-Indent-Int';
(*}}}*)
(*{{{  Variables*)
var
   InstPath       : String;
(*}}}*)

(*{{{  procedure run_indent;*)
procedure run_indent;
begin
   FileSave; FileReload(0);
   ToolRun(GNUINDENT);
end;
(*}}}*)
(*{{{  procedure Indent_Project;*)
{ Iterates project and calls indent for each .c and .cpp
  file.
}
procedure Indent_Project;
var fn : string;
    i  : integer;
begin
   if (NOT IsProject) then exit;
   i := 0;
   repeat
      fn := IterateProjectModules(false, i);
      if (fn <> '') then begin
         if ((FileNameGetExt(fn) = '.C') OR (FileNameGetExt(fn) = '.CPP')) then begin
            ToolRunParameter(GNUINDENTINT, fn);
         end;
      end;
      inc(i);
   until (fn = '');
   ReloadFiles;
end;
(*}}}*)

(*{{{  procedure insvar_gnu_style;*)
procedure insvar_gnu_style;
begin
   InsertLine    ('INDENT : GNU Style');
   InsertVariable('INDENT_OPT',
      '-nbad -bap -nbc -bbo -bl -bli2 -bls -ncdb -nce -cp1 -cs -di2 '+
      '-ndj -nfc1 -nfca -hnl -i2 -ip5 -lp -pcs -nprs -psl -nsc -nsob '+
      '-ts4');
   NewLine;
end;
(*}}}*)
(*{{{  procedure insvar_kr_style;*)
procedure insvar_kr_style;
begin
   InsertLine    ('INDENT : K&R Style');
   InsertVariable('INDENT_OPT',
      '-nbad -bap -bbo -nbc -br -brs -c33 -cd33 -ncdb -ce -ci4 -cli0 '+
      '-cp33 -cs -d0 -di1 -nfc1 -nfca -hnl -i4 -ip0 -l75 -lp -npcs '+
      '-nprs -npsl -nsc -nsob -nss -ts4');
   NewLine;
end;
(*}}}*)
(*{{{  procedure insvar_berkley_style;*)
procedure insvar_berkley_style;
begin
   InsertLine    ('INDENT : Berkley Style');
   InsertVariable('INDENT_OPT',
      '-nbad -nbap -bbo -bc -br -brs -c33 -cd33 -cdb -ce -ci4 -cli0 '+
      '-cp33 -di16 -fc1 -fca -hnl -i4 -ip4 -l75 -lp -npcs -nprs -psl -sc '+
      '-nsob -nss -ts4');
   NewLine;
end;
(*}}}*)
(*{{{  procedure insvar_empty;*)
procedure insvar_empty;
begin
   InsertLine    ('INDENT : User defined');
   InsertVariable('INDENT_OPT', '');
   NewLine;
end;
(*}}}*)

(*{{{  procedure define_gnu_indent;*)
{ Define and install GNU- INDENT
}
procedure define_gnu_indent;
begin
   (* ------------------------------------------------- *)
   (* Definition of Indent *)
   (* Indent is startet from the same directory
      as the inputfile is located. An outputfile is not specified.
      So indent will replace the input- file. Indent works with
      *.c and *.cpp files. It will be available as menue,
      and as RUN- Tool. *)
   ToolDefine       (GNUINDENT, InstPath+'\indent.exe',
                     '$DRIVEDIR($EDNAME)',
                     '$(INDENT_OPT) $NAMEEXT($EDNAME)');
   ToolDefineType   (GNUINDENT, '.c;.cpp', '', 'C/C++', 3);
   ToolDefineMode   (GNUINDENT, '', 2);
   ToolDefineConnect(GNUINDENT, '', 0);
   ToolDefineOptions(GNUINDENT, 0, 0);
end;
(*}}}*)
(*{{{  procedure define_indent_internal;*)
{ Macro to define internal tool
  used to iterate project
}
procedure define_indent_internal;
begin
   (* ------------------------------------------------- *)
   (* Definition of GNU-Indent-Int
      as hidden tool only to be called from macro with
      ToolRunParameter *)
   ToolDefine       (GNUINDENTINT, InstPath+'\indent.exe',
                     '$DRIVEDIR($EDNAME)',
                     '$(INDENT_OPT) ');
   ToolDefineType   (GNUINDENTINT, '', '', '', 0);
   ToolDefineMode   (GNUINDENTINT, '', 2);
   ToolDefineConnect(GNUINDENTINT, '', 0);
   ToolDefineOptions(GNUINDENTINT, 0, 0);
end;
(*}}}*)

BEGIN
   (*{{{  Define Installation Directory*)
   { ----------------------------------------------------- }
   { Define Installation Directory }
   { ----------------------------------------------------- }
   { Test if variable GNUINDENT_INSTPATH is available. If not,
     get it from the user }
   InstPath := GetVarValue('GNUINDENT_INSTPATH');
   if (InstPath = '') then begin
      { Let user search for path }
      Beep;
      if (DirectoryInput ('GNU- Indent- Installation Path', InstPath)) then begin
         SetGlobalVar('GNUINDENT_INSTPATH', InstPath);
      end;
   end;
   (*}}}*)

   { ----------------------------------------------------- }
   { Define and install GNU- Indent Tool  }
   { ----------------------------------------------------- }
   if (NOT ToolAvail(GNUINDENT))    then define_gnu_indent;
   if (NOT ToolAvail(GNUINDENTINT)) then define_indent_internal;

   MacroMenuAdd('RUN_INDENT');
   MacroMenuAdd('INDENT_PROJECT');
   MacroMenuAdd('');
   MacroMenuAdd('insvar_gnu_style');
   MacroMenuAdd('insvar_kr_style');
   MacroMenuAdd('insvar_berkley_style');
   MacroMenuAdd('insvar_empty');

   OptionsSave;

END.

