Prolog development with Emacs
This page shows you how to configure GNU Emacs and XEmacs for
effective Prolog development.
Note: For smaller editing tasks, SWI-Prolog ships with
its own editor, an Emacs clone called PceEmacs. You can try it
This built-in editor provides a limited subset of Emacs features.
Since some of the features shown on this page are
inspired by PceEmacs, I call this collection PceProlog.
Editing Prolog code
There is an
mode for Emacs, maintained by Stefan Bruda.
Recent GNU Emacs versions also ship with a variant of this mode.
Unfortunately though, the variant that ships with GNU Emacs
contains modifications that cause severe regressions with
indenting and navigation. Therefore, I recommend you use the
version that is directly supplied by Stefan until these mistakes
To obtain PceEmacs-style behaviour and use the mode with
SWI-Prolog, add to your .emacs:
(setq prolog-system 'swi
prolog-program-switches '((swi ("-G128M" "-T128M" "-L128M" "-O"))
Note in particular the use
prolog-electric-if-then-else-flag: Non-nil makes `(', `>'
and `;' electric to automatically indent if-then-else constructs.
For nice comment blocks as in PceEmacs, add the following to your
.emacs to insert a comment block with C-c q:
(defun prolog-insert-comment-block ()
"Insert a PceEmacs-style comment block like /* - - ... - - */ "
(let ((dashes "-"))
(dotimes (_ 36) (setq dashes (concat "- " dashes)))
(insert (format "/* %s\n\n%s */" dashes dashes))
(global-set-key "\C-cq" 'prolog-insert-comment-block)
Use ediprolog to
evaluate embedded queries in Emacs buffers:
As of GNU Emacs 24.3, you can install ediprolog from the
Emacs Lisp Package Archive (ELPA) with:
M-x package-install RET ediprolog RET
As of GNU Emacs version 22.1, flymake ships with Emacs. This
enables on-the-fly syntax checking, using the Prolog compiler as
checker. To use it with SWI-Prolog, add the following to your
'(("ERROR: (?\\(.*?\\):\\([0-9]+\\)" 1 2)
("Warning: (\\(.*\\):\\([0-9]+\\)" 1 2)))
(defun flymake-prolog-init ()
(let* ((temp-file (flymake-init-create-temp-buffer-copy
(list "swipl" (list "-q" "-t" "halt" "-s " local-file))))
Tracing Prolog execution
There are Emacs definitions that mimic a limited subset of
SWI-Prolog's graphical tracer within the Emacs buffer. They are
available as etrace.
etrace is mostly a proof of concept at this point and may
be useful for you if you do not have X11 installed.
Instead of tracing, consider
debugging to locate mistakes in Prolog programs.
More about Prolog: The Power of Prolog