Emacs and Pascal

2025-07-13

https://gitlab.com/bolsen80/pascal-emacs.

I have been making incremental improvements in Emacs for writing Object Pascal code. This is me taking some public notes on the question.

  1. I use https://github.com/LongDirtyAnimAlf/fpcupdeluxe. I do have Lazarus open for a lot of things even if I don't stare at it. The big thing is that I want a QT build, not the default, which is GTK.

  2. The tool lazbuild is very handy for outside the IDE. You give it a single file like lazbuild myproject.lpr and it handles all the dependencies for you. It's rather important because ...

  3. An LSP is useful. After going through the forks (I mentioned prior), I settled on for now: https://github.com/Axiomworks/pascal-language-server-isopod. In my repository for capturing all these things, I extend lsp-pascal to handle initializationOptions which is rather critical for this to be effective.

  4. Emacs has weak support for Pascal, but at least it has opascal-mode. With LSP and opascal-mode, I have a config that looks like this:

  (setq lsp-pascal-initialization-options
      `( :maximumCompletions 100
         :fpcOptions [
                      "-n/home/bolsen/fpcupdeluxe/fpc/bin/x86_64-linux/fpc.cfg"
                      "-Fu/home/bolsen/fpcupdeluxe/lazarus/components/codetools"
                      ]
         :includeWorkspaceFoldersAsIncludePaths t
         :includeWorkspaceFoldersAsUnitPaths t
         :syntaxErrorReportingMode t
         :symbolDatabase "/home/bolsen/.config/pasls/symbols.db"))

(add-to-list 'auto-mode-alist '("\\.pas\\'" . opascal-mode))
(add-to-list 'auto-mode-alist
             '("\\.(pas\\|lpr\\|lpk\\|dpr\\|dpk\\|pp\\|inc)\\'" . opascal-mode))
(add-hook 'opascal-mode-hook #'lsp)
(setq lsp-pascal-fpcdir "~/fpcupdeluxe/fpcsrc/")
(setq lsp-pascal-lazarusdir "~/fpcupdeluxe/lazarus/")
(setq lsp-pascal-command "~/pasls/pascal-language-server-isopod/server/lib/x86_64-linux/pasls")
(setq lsp-pascal-pp "~/fpcupdeluxe/fpc/bin/x86_64-linux/")
(setq lsp-pascal-fpctargetcpu "x86_64")

Of course, I am using my customized lsp-pascal.

  1. compliation-mode is super useful. I bound <f9> to compile, and create a quick Makefile to use the default. The important thing is to get highlighting and linkability in the compile output, but alas, that is not in Emacs by default. Thanks to this little tidbit: http://praveen.kumar.in/2011/03/09/making-gnu-emacs-detect-custom-error-messages-a-maven-example/, the problem is solved quicker than I presumed.
 (add-to-list 'compilation-error-regexp-alist 'fpc)
(add-to-list 'compilation-error-regexp-alist-alist
             '(fpc "^\\(.+\\)(\\([0-9]+\\),\\([0-9]+\\)) \\(.+\\):.+" 1 2 3 (4)))

This works with fpc and through lazbuild.

  1. As I write this, I started to tinker with ptop, which is a formatter. It's a cmd line program, so far unlike other formatters, so it makes it easy to write a format-on-save thing. I can run it like this: ptop -c ptop.cfg ifp.pas ifp.pas. I added my config to the repo, specifically so it doesn't capitalize keywords and doesn't put trailing spaces after terms that usually have newlines after it.

  2. As an aside, instantfpc is a useful thing to throw code over the fence and get an answer quickly. It's not a REPL, but I can probably get a few miles away from it, which might be good enough. It requires less ceremony actually than formal Pascal programs, which is pretty nice.

There are some open questions, if they are really pain points:

  1. Can I have a tool to make and update package XML? Lazarus is fine for that right now. It's also simple XML, so adding new references to existing files is easy.
  2. Do I need to jump into Lazarus to install things? Lazarus is still in the workflow (and probably still best for GUI development) so I am not sure at the moment.
  3. Also, installing packages is still dependent on Lazarus. I am going to look at fpcup CLI to see what good things it has.

In all, I got myself at this point to be more efficient. The thing now is to reproduce all of this.

In: pascal