3.4 Using the PceEmacs built-in editor

3.4.1 Activating PceEmacs

Initially edit/1 uses the editor specified in the EDITOR environment variable. There are two ways to force it to use the built-in editor. One is to set the Prolog flag editor to pce_emacs and the other is by starting the editor explicitly using the emacs/[0,1] predicates.

3.4.2 Bluffing through PceEmacs

PceEmacs closely mimics Richard Stallman's GNU-Emacs commands, adding features from modern window-based editors to make it more acceptable for beginners.16Decent merging with MS-Windows control-key conventions is difficult as many conflict with GNU-Emacs. Especially the cut/copy/paste commands conflict with important GNU-Emacs commands.

At the basis, PceEmacs maps keyboard sequences to methods defined on the extended editor object. Some frequently used commands are, with their key-binding, presented in the menu-bar above each editor window. A complete overview of the bindings for the current mode is provided through Help/Show key bindings (Control-h Control-b).

3.4.2.1 Edit modes

Modes are the heart of (Pce)Emacs. Modes define dedicated editing support for a particular kind of (source-)text. For our purpose we want Prolog mode. Their are various ways to make PceEmacs use Prolog mode for a file.

3.4.2.2 Frequently used editor commands

Below we list a few important commands and how to activate them.

These were the most commonly used commands. In section section 3.4.3 we discuss specific support for dealing with Prolog source code.

3.4.3 Prolog Mode

In the previous section (section 3.4.2) we explained the basics of PceEmacs. Here we continue with Prolog specific functionality. Possibly the most interesting is Syntax highlighting. Unlike most editors where this is based on simple patterns, PceEmacs syntax highlighting is achieved by Prolog itself actually reading and interpreting the source as you type it. There are three moments at which PceEmacs checks (part of) the syntax.

The colour schema itself is defined in library(emacs/prolog_colour). The colouring can be extended and modified using multifile predicates. Please check this source-file for details. In general, underlined objects have a popup (right-mouse button) associated for common commands such as viewing the documentation or source. Bold text is used to indicate the definition of objects (typically predicates when using plain Prolog). Other colours follow intuitive conventions. See table 3.

Clauses
Blue boldHead of an exported predicate
Red boldHead of a predicate that is not called
Black BoldHead of remaining predicates
Calls in the clause-body
BlueCall to built-in or imported predicate
RedCall to not-defined predicate
PurpleCall to dynamic predicate
Other entities
Dark greenComment
Dark blueQuoted atom or string
BrownVariable
Table 3 : Colour conventions

Layout support

Layout is not `just nice', it is essential for writing readable code. There is much debate on the proper layout of Prolog. PceEmacs, being a rather small project supports only one particular style for layout.19Defined in Prolog in the file library(emacs/prolog_mode), you may wish to extend this. Please contribute your extensions! Below are examples of typical constructs.

head(arg1, arg2).

head(arg1, arg2) :- !.

head(Arg1, arg2) :- !,
        call1(Arg1).

head(Arg1, arg2) :-
        (   if(Arg1)
        ->  then
        ;   else
        ).

head(Arg1) :-
        (   a
        ;   b
        ).

head :-
        a(many,
          long,
          arguments(with,
                    many,
                    more),
          and([ a,
                long,
                list,
                with,
                a,
              | tail
              ])).

PceEmacs uses the same conventions as GNU-Emacs. The TAB key indents the current line according to the syntax rules. Alt-q indents all lines of the current clause. It provides support for head, calls (indented 1 tab), if-then-else, disjunction and argument-lists broken across multiple lines as illustrated above.

3.4.3.1 Finding your way around

The command Alt-. extracts name and arity from the caret location and jumps (after conformation or edit) to the definition of the predicate. It does so based on the source-location database of loaded predicates also used by edit/1. This makes locating predicates reliable if all sources are loaded and up-to-date (see make/0).

In addition, references to files in use_module/[1,2], consult/1, etc. are red if the file cannot be found and underlined blue if the file can be loaded. A popup allows for opening the referenced file.