Tuesday 17 May 2022

Using the Free Pascal IDE for a week

Code can really be written with any text editor that you're comfortable with. I tend to use Vim for quick edits and writing notes, and on Linux I occasionally use Geany or Mousepad. But when you want to do more than quickly update a few files, you need a full Integrated Development Environment (IDE).
An IDE understands the language that you're writing in, and can run additional tools like a compiler and a debugger from the same environment.

The most full featured IDE for Free Pascal, bar none, is Lazarus. The ease with which you can write, refactor, debug and compile programs in Lazarus makes me miss it when I have to write code in another language. It's fast and perfectly suited for the task... except in one instance.

Lazarus is designed for coding GUI applications, but recently I've spent most of my free time writing a terminal based game. It used to be possible to run a terminal session 'attached' to Lazarus, so that it could be debugged from the IDE, but since a previous update this always seems to fail on my PC. I can still use Lazarus to write my code, but when I encounter an error I need some other way to diagnose it.

Introducing FP IDE

I'm not sure what the rationale is for maintaining the text mode FP IDE. It seems kinda quaint in 2022 to maintain a recreation of the old Turbo Pascal editor, but I'm so glad that it's there. The IDE might not have all the bells and whistles of something like Lazarus, but it's lightweight, easy to use, and reliable. I first learned Pascal back in the Borland days, so the IDE does feel familiar to me. Although I'm not sure how a younger programmer, encountering it for the first time would feel about it. Without the haze of experience and nostalgia informing their opinion.

Regardless, it comes with Free Pascal and runs in the terminal. It does require a little setting up first though. Note, I'm running Linux here but the same should work in Windows with a little tweaking to use the correct file paths. Also, I've found the best Linux terminal to run the IDE in is Xterm. Other terminal emulators seem to catch your keyboard commands for their own menus.

Setting it up

The first time that you run FP IDE it creates a couple of config files in the starting directory, fp.cfg and fp.ini. The first thing that you notice when opening the IDE is the retro, blue colour scheme. The second thing that you'll notice is that it doesn't actually compile Pascal programs out of the box.
I'm not sure why this is the default setup, but you need to tell the IDE where to find the Free Pascal files on your system. A quick Google search shows that this step trips a lot of people up, so let's kill two birds with one stone and get both of these things resolved.

Open fp.cfg in a text editor and you'll see three sections:

#IFDEF NORMAL
#IFDEF DEBUG
#IFDEF RELEASE

These correspond to the different build profiles that you'll use. Personally, I spend all my development time using the debug profile, and then only use release at the very end of my development cycle.

In each section, add the following lines:
 -Fu/usr/lib/fpc/$fpcversion/units/$fpctarget
 -Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/*
 -Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/rtl

You can also add this in the IDE itself in Options > Directories > Units, but it's a little quicker to copy and paste this in a text editor.

Unlike Lazarus, the FP IDE doesn't use 'Projects' where all of a programs files are managed. Instead it deals with directories/folders. If the program that you're working on has files stored in subfolders, explicitly add them here. Each line begins with -Fu followed by the full path to the directory, with no space after -Fu.
As an example, here's my fp.cfg file below.

# Automatically created file, don't edit.
#IFDEF NORMAL
 -TLinux
 -Mfpc
 -Sg
 -Si
 -Os
 -CpATHLON64
 -OpCOREI
 -Fu/home/cypher/Development/MyProg/
 -Fu/home/cypher/Development/MyProg/AdditionalUnits
 -Fu/usr/lib/fpc/$fpcversion/units/$fpctarget
 -Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/*
 -Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/rtl
 -g-
 -p-
 -b-
#ENDIF

#IFDEF DEBUG
 -TLinux
 -Mfpc
 -Sg
 -Si
 -Sa
 -Cr
 -Ci
 -Co
 -CR
 -Os
 -CpATHLON64
 -OpCOREI
 -Fu/home/cypher/Development/MyProg/
 -Fu/home/cypher/Development/MyProg/AdditionalUnits
 -Fu/usr/lib/fpc/$fpcversion/units/$fpctarget
 -Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/*
 -Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/rtl
 -g
 -p-
 -b-
#ENDIF

#IFDEF RELEASE
 -TLinux
 -Mfpc
 -Sg
 -Si
 -CX
 -Os
 -CpATHLON64
 -OpCOREI
 -Fu/home/cypher/Development/MyProg/
 -Fu/home/cypher/Development/MyProg/AdditionalUnits
 -Fu/usr/lib/fpc/$fpcversion/units/$fpctarget
 -Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/*
 -Fu/usr/lib/fpc/$fpcversion/units/$fpctarget/rtl
 -XS
 -g-
 -p-
 -b-
#ENDIF

Some Quality of Life improvements

The next steps aren't necessary in order to use the IDE, but I find that it makes it a more comfortable environment to work in.

If you're happy with the blue colour scheme, you can ignore this step. Personally, I prefer something darker so I put together my own colour palette. It's incomplete, but it's good enough to use day to day.
View the file at https://github.com/cyberfilth/FP-IDE-theme and paste the [Colors] section into your own fp.ini file.

The next thing that I do is download the help files, so that I can access them through the IDE's built-in help viewer. The HTML documentation can be downloaded from https://www.freepascal.org/down/docs/docs-canada.var then add them in the IDE by going to Help > Files and adding the path to the HTML files.

Now the IDE is set up. It took a little more work than opening Lazarus for the first time, but now I can find bugs in my code a lot easier. Until now I've been writing code in Lazarus and firing up the FP IDE whenever I need to debug something. I did wonder though if I can just use the FP IDE for everything.

Since I'm currently writing a roguelike game, I thought that I'd open it up in the IDE. Before doing that, I amended the main filename from the Lazarus format to plain Pascal. That just means changing the suffix .lpr to .pas.  
Once the main program file is opened up, select it as the primary file.



One of the main differences in the interface, compared to Lazarus, is that instead of a tabbed interface it has a series of windows. With the right font size, I actually prefer this interface as it means that I can see several related files on screen at once.




The biggest thing that I'm missing so far is a good auto complete. The IDE will offer suggestions for completing Pascal keywords but, unlike Lazarus, it won't try to autocomplete your own variables or procedure names.

Regardless, I'm going to try and restrict myself to just using this IDE for the next week to see if I can still use it for my hobby project. My codebase is currently about 17,000 lines of code and I'm curious to see what it's like to navigate it using the IDE... wish me luck!

0 comments:

Post a Comment

Search This Blog

Powered by Blogger.

Using the Free Pascal IDE for a week

Code can really be written with any text editor that you're comfortable with. I tend to use Vim for quick edits and writing notes, and ...