Prolog Mode for Emacs

So the Prolog mode that comes with Emacs sucks. Here is then a better alternative, with all the features you would expect, including syntax highlighting, auto indent, and full interaction with an inferior Prolog process.

This mode supports in principle any Prolog system, though I am primarily maintaining it with SWI Prolog in mind. It also has some special tweaks for GNU Prolog (starting from version 1.4) and SICStus Prolog, and explicitly supports Prolog's close relatives Eclipse, Mercury, and XSB (starting from version 1.23).

This page is also available in Bulgarian (maintained by a third party).

Downloads

  • prolog.el version 1.25 [gzipped] works with Emacs 20 and above and reasonably recent versions of XEmacs (tested on 21.1 and higher). For a change log see the beginning of the source file.
  • A Debian package is also available.
  • An utterly unmaintained version for Emacs 19: prolog-0.1.31.el.

Installation

In a nutshell, place the downloaded file where Emacs can see it, and insert into your ~/.emacs or ~/.xemacs/init.el the following lines:

(autoload 'run-prolog "prolog" "Start a Prolog sub-process." t)
(autoload 'prolog-mode "prolog" "Major mode for editing Prolog programs." t)
(autoload 'mercury-mode "prolog" "Major mode for editing Mercury programs." t)
(setq prolog-system 'swi)
(setq auto-mode-alist (append '(("\\.pl$" . prolog-mode)
                                ("\\.m$" . mercury-mode))
                               auto-mode-alist))

See the comments at the beginning of the source file for the gory details, and this installation guide for an even more detailed explanation.

Note that the default behaviour of prolog-insert-predicate-template has changed in version 1.15. The new behaviour is to just insert the predicate name followed by a parenthesis (as opposed to the old behaviour of inserting the predicate name, matching parentheses, and enough commata to separate all the arguments). Indeed, it seems more productive to just type those commata and closing parenthesis. If the old behaviour is desired, set prolog-electric-dot-full-predicate-template (group “Prolog Keyboard”) to non-nil.

Starting from version 1.25 “ansi-color” is required (since most Prolog interpreters seem to use ANSI escape sequences). The variable prolog-inferior-ansi-color controls whether the ANSI escape sequences are translated into text properties (non-nil, default) or filtered out (nil).

Note that two new customizable variables have been added in version 1.9:

  • prolog-paren-indent-p (group “Prolog Indentation”) governs how parenthesis expressions (other than compound terms) are indented. If the variable is nil (default) then components are indented one underneath the other (just as for compount terms); otherwise the second and subsequent lines are indented `prolog-paren-indent' characters to the right (old behaviour).
  • prolog-char-quote-workaround (group “Prolog Other”) allows for a somehow correct handling of the 0' character escape by setting 0 as a quote character (when non-nil); setting the variable to nil (default) ignores the 0' construct.
    This should not be customizable (the character escape should be recognized as such no matter what) but the current implementation is kludgy (and I have no idea how to do it better) so for the time being I provide a way to disable the whole thing. Please let me know about any idea to do this better and most importantly about any breakage caused by the current implementation.

Further note that some more customizable variables have been added (and might be added in future versions), please check the changelog provided at the beginning of the source file.