Prolog Mode for Emacs

A variant of this very code now comes bundled with Emacs (23 and above). The bundled version includes extra features such as SMIE integration (which in turn handles correctly and completely Prolog's 0' construct). Therefore if you are using a newer Emacs you may be better off just using what comes bundled with it.

This being said, some people consider this version superior in several ways. It is also the case that the new, bundled version is not available to older Emacsen, while this version works with any reasonably new version of Emacs and XEmacs. For these two reasons this page will continue to exist, and the code herein will continue to be maintained for the foreseeable future.

This Prolog mode has all the features one 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).


  • prolog.el version 1.28 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.


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))

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).

One new customizable variable has been added in version 1.28:

  • prolog-align-small-comments-flag (group “Prolog Indentation”) specifies how to handle “small” comments (the ones that start with a simgle “%”) as follows: When this variable is non-nil (default) small comments are aligned to `comment-column' (default 32) in all circumstances. When the variable is set to nil however small comments that begin a line (blank characters notwithstanding) will be treated just like “large” comments (the ones that start with “%%”). Note that small comments that start somewhere other than at the beginning of the line are still aligned to `comment-column'.

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.