[Solved] How can I read specific Ide settings ? lazarus / 13-General ---------------------------------------------------------------------- loaded · 2022-02-07 16:43 Hi All, Is it possible to access the ide settings from the exe compiled by the IDE? I need the directory path where FPC is currently installed. I would be glad if you give your ideas and suggestions. Respects. ---------------------------------------------------------------------- ASBzone · 2022-02-07 17:15 What OS are you trying to do this from? In Windows, if FPC is in your path, you can determine where the executable is running from using the WHERE command or looking at the process list. Can you elaborate on the use case you are trying to work with? -ASB ---------------------------------------------------------------------- loaded · 2022-02-07 17:28 Quote from: ASBzone on February 07, 2022, 06:15:59 pmWhat OS are you trying to do this from? Windows OS Quote from: ASBzone on February 07, 2022, 06:15:59 pmIn Windows, if FPC is in your path, you can determine where the executable is running from using the WHERE command or looking at the process list. Yes, I can do this manually. But the need for something like this arose, both to be a bit technical and to be familiar with the IDE. Quote from: ASBzone on February 07, 2022, 06:15:59 pmCan you elaborate on the use case you are trying to work with? https://forum.lazarus.freepascal.org/index.php/topic,58173.msg433271.h tml#msg433271  As I mentioned in the link, I'm thinking of doing a study that will add units automatically. I hope this work will improve me a lot. In fact, I've already made a few clicks of progress. ---------------------------------------------------------------------- ASBzone · 2022-02-07 17:41 Thanks for the link, @loaded.   That was helpful for me to understand what you are trying to accomplish. Unfortunately, I also don't know how to help you much, either. ---------------------------------------------------------------------- loaded · 2022-02-07 18:03 Quote from: ASBzone on February 07, 2022, 06:41:21 pmUnfortunately, I also don't know how to help you much, either.  It doesn't matter, just answering is enough. Thank you, your existence is enough. ---------------------------------------------------------------------- balazsszekely · 2022-02-07 18:31 @loaded Unfortunately that information is not compiled into the exe. However all is not lost, please do the following: 1. Create a package 2. When the package is loaded, wait until the IDE starts. Just start a timer and check if LazarusIDE and PackageEditingInterface is not null. Don't forget to add LazIDEIntf and PackageIntf to uses. 3. If the IDE started, tap into the project openings: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    function OnProjectOpened(Sender: TObject; AProject: TLazProject): TModalResult;    LazarusIDE.AddHandlerOnProjectOpened(@OnProjectOpened);    Now every time a project is opened in the IDE, you will be notified via OnProjectOpened event. This is also true for your project which has to know the FPC path(ProjectX) 4. Add a new unit to you ProjectX, call it uFPCPath. Also add a variable to the interface section: var FPCPath: String = ''; 5. Next time when ProjectX is opened do something like this inside OnProjectOpened: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses ProjectIntf;//..for I := 0 to AProject.FileCount - 1 do  if  ExtractFileName(AProject.Files[I].Filename) = 'uFPCPath' then  begin     //we got our file  end;  6. Load the file in a StringList, loop through the items until you find: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var FPCPath: String = ''; and replace it with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses IDEOptionsIntf; var FPCPath: String = QuotedStr(IDEEnvironmentOptions.GetParsedCompilerFilename) + ';' Save back the stringlist to file. 7. If everything went well, each time you open ProjectX, you should have the FPC path as a variable inside unit uFPCPath.pas and it will be compiled into your executable. Please note I did not test the above code, you may have to modify it slightly. ---------------------------------------------------------------------- loaded · 2022-02-07 18:45 My esteemed master Getmem, You are a Living Wise. Thank you for taking your precious time to share your experience. What you wrote will probably lead me to the solution (Looks like I'll have to sweat a little). Thanks again. ---------------------------------------------------------------------- Martin_fr · 2022-02-07 18:46 Well, here goes. When "lazarus" or "startlazarus" is started, it takes the following steps. 1) Find the directory in which "lazarus.exe" /"startlazarus" is.  There is no 100% way for your app to know this. On Windows, you could try the "inno setup" registry entries (see tools/install/win/lazarus.iss), but that only works for your "primary install" (if you have more than one..., if you did not use inno ...) On other OS => depends if a package manager was used, and if that stores info that you can find. 2) In this directory it looks for "lazarus.cfg" This is an optional file (usually only for 2ndary installs). This file may contain a line --ppc= or --primary-config-path= If the file exists, and has such a line then the given path is the "pcp" Otherwise it is On Win:  %AppData%\Local\lazarus      %AppData% is a system path, usually C:\Users\USERNAME\ On Linux: ~/.lazarus 3) In the PCP is the file environmentoptions.xml It's xml, it can be parsed. If you look at it, you will find where the compiler path is. The compiler path may however contain ide-macros, such as $(LazarusDir) => which need to be replaced. I don't have a full list of them, or how they are computed. The  code dealing with that is in the IDE. (not sure which file, start searching in ide/environmentoptions.pas) This code is part of the "ide" project. It is not part of a package. There is no good way to use it in your own code. As long as you keep to GPL, you can just copy the relevant units... You can modify "search path" options... However, I have no idea how much code you need to pull in, if you want to try and use that code (once found). Consider passing the path to FPC  as a command line option to your app. Menu RUN > RUN PARAMS ---------------------------------------------------------------------- MarkMLl · 2022-02-07 18:49 Try this: in your main file (usually .lpr) use something like this to set a global variable: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   B5k5_Project := {$I %FILE% } ;        (* Possibly used for .ini files etc.    *)  You can then inspect and parse the result. There might be other useful strings, but I can't remember where they're documented and you'll probably need to be careful with backward compatibility. [Slightly later] Thinking back to an earlier discussion, on unix this should work Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i pwd := {$I %PWD%};  although that's obviously looking at the state of the shell rather than the working/project directory assumed by the IDE. MarkMLl ---------------------------------------------------------------------- balazsszekely · 2022-02-07 18:53 Quote from: MarkMLl on February 07, 2022, 07:49:06 pmTry this: in your main file (usually .lpr) use something like this to set a global variable: Code: [Select]  B5k5_Project := {$I %FILE% } ;        (* Possibly used for .ini files etc.    *) You can then inspect and parse the result. There might be other useful strings, but I can't remember where they're documented and you'll probably need to be careful with backward compatibility. MarkMLl That will print out the project file. He needs the FPC install path. ---------------------------------------------------------------------- loaded · 2022-02-07 19:13 Many thanks to Martin_fr and MarkMLI for your answers. As the masters come, this place turns into a gold mine. Although I cannot do exactly the same operation I want from the information here, I think I can do a similar one. Of course, as always, I have to do a little trial and error. By the way, the hint given by MarkMLI seems to be very useful for me to detect errors. Much Regards. ---------------------------------------------------------------------- MarkMLl · 2022-02-07 19:22 Quote from: GetMem on February 07, 2022, 07:53:25 pmThat will print out the project file. He needs the FPC install path. [Checks] True, my apologies. MarkMLl ---------------------------------------------------------------------- loaded · 2022-02-07 19:44 MarkMLl, I realized that the information you provided will have a very important use in another of my projects. I'll probably catch bugs more easily now. For me, even a piece of bread and a crumb of knowledge is very valuable. I would never waste either of them. Thank you very much. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm_Main.P_action_errors(const msg: String; E: Exception);begin....  DataModule1.TrayIcon1.BalloonHint:=msg + '-> ' + E.ClassName + '  '+ E.Message;.....end; ..... try...except  on E: Exception do P_action_errors({$i %CURRENTROUTINE%},E );end; ---------------------------------------------------------------------- MarkMLl · 2022-02-07 20:02 Quote from: loaded on February 07, 2022, 08:44:32 pm Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm_Main.P_action_errors(const msg: String; E: Exception);  on E: Exception do P_action_errors({$i %CURRENTROUTINE%},E );[/quote] You might find some variant of this useful: [code=pascal]{$undef HAS_CURRENTROUTINE     }{$if FPC_FULLVERSION >= 030200 }        (* Requires FPC 2.2.4 minimum           *){$define HAS_CURRENTROUTINE    }        (* Requires FPC 3.2.0 minimum           *){$assertions on              }        (* Make sure name checks are operative  *){$endif FPC_FULLVERSION        }  Rationale: FPC_FULLVERSION was introduced by FPC 2.2.4, this is older than anybody is likely to be using but I think is worth noting. Provided that this is available, you can check that the current version of FPC is not < 3.2.0, since if it is then CURRENTROUTINE will not be available. I've also been very pleasantly surprised to find that multi-line macros work, hence e.g. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$macro on  }{$ifdef HAS_CURRENTROUTINE }{$define TEST_NAME:= Assert(thisName = {$I %CURRENTROUTINE%} + '()', 'Internal error: ' +                      'compiler-generated ' + {$I %CURRENTROUTINE%} + '()' +                      'does not match hardcoded ' + thisName) }{$else                   }{$define TEST_NAME:= }{$endif HAS_CURRENTROUTINE } {$ifdef ERROR2 }{$define EXIT_ERROR:= begin                        if fatal then begin                          ErrorLine;                        WriteLn('*** In ', PeekRule(), ': unable to parse ', PoppedRule())                      end;                        exit                    end }{$else         }{$define EXIT_ERROR:= exit }{$endif ERROR2 }  which allows me to tidy up earlier code enormously by doing things like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i (* The sigil defines the special character indicating expansion of an identifier  in an output string.*)function sigil(var defs: text; fatal: boolean= true): boolean; const  thisName= 'sigil()'; var  ch: ansistring; begin  TEST_NAME;  result := true;  PushRule(thisName);  try    if not Expect_Literal(defs, 'sigil', false) then      exit(false);    if not Expect_Literal(defs, ' ') then      EXIT_ERROR;    if not Expect_Ch(defs, ch) then      EXIT_ERROR;    if not (ch[1] in ['!'..'~']) then      EXIT_ERROR;    if not Expect_Literal(defs, ';') then      EXIT_ERROR;    emit('Sigil: ' + ch);    result := true  finally    PopRule()  endend { sigil } ;  MarkMLl ---------------------------------------------------------------------- loaded · 2022-02-07 20:19 To tell the truth, I am far from grasping the above lines at the moment. I felt like a primary school kid.  Now I'm going to do a little trial and error with what I have, maybe one day in the future I can write something like this. ---------------------------------------------------------------------- balazsszekely · 2022-02-07 20:49 @loaded See attached package and project. 1. Install package 2. Rebuild IDE 3. Open then run test project PS: Sometimes when unit ufpcpath is open in the IDE, the editor doesn't get refreshed. But the path is there and valid. If you close and reopen ufpcpath the path is visible again. ---------------------------------------------------------------------- loaded · 2022-02-08 05:08 Once again I am grateful to you. Thanks a lot. I would like to point out that I think it was a very professional work. Mr. GetMem, the man who whispered the codes. ---------------------------------------------------------------------- balazsszekely · 2022-02-08 05:44 @loaded I'm glad it's working. One small correction though is needed in unit fpcsource_intf.pas, please change: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if (P > 0) thento: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if (P > 0) and (Str <> 'var FPCSource: String = ' + QuotedStr(IDEEnvironmentOptions.GetParsedCompilerFilename) + ';') thenOnce the FPC path is there, the package does not rewrite the variable each time you open the project and does not trigger the "Some files has changed" dialog. ---------------------------------------------------------------------- loaded · 2022-02-08 06:01 It's understood, actually this was also very good because; it is better to have such corrections in terms of understanding the issue. ---------------------------------------------------------------------- PascalDragon · 2022-02-08 08:35 Quote from: loaded on February 07, 2022, 06:28:59 pmQuote from: ASBzone on February 07, 2022, 06:15:59 pmCan you elaborate on the use case you are trying to work with? https://forum.lazarus.freepascal.org/index.php/topic,58173.msg433271.h tml#msg433271  As I mentioned in the link, I'm thinking of doing a study that will add units automatically. I hope this work will improve me a lot. In fact, I've already made a few clicks of progress.  If your endgoal is to have something that can be used within the IDE you should familiarize yourself with creating an IDE package (e.g. something that extends or uses Cody) and then you'll have access to the IDE settings (and other functionality like adding a unit to the uses clause) anyway without going through hurdles as those mentioned in this thread. Cause what would be the use of your study if you'll need to use completely different functionality in the end? ---------------------------------------------------------------------- loaded · 2022-02-08 11:24 PascalDragon,As I mentioned above, I am currently in the development phase of myself. If possible, I will try to implement what you said in the future.