(eieio-copy-parents-into-subclass): Fix inheritance of initargs
[emacs.git] / lisp / progmodes / prolog.el
blob2d95345b531182c56800de51618a012c6188aedc
1 ;;; prolog.el --- major mode for Prolog (and Mercury) -*- lexical-binding:t -*-
3 ;; Copyright (C) 1986-1987, 1997-1999, 2002-2003, 2011-2015 Free
4 ;; Software Foundation, Inc.
6 ;; Authors: Emil Åström <emil_astrom(at)hotmail(dot)com>
7 ;; Milan Zamazal <pdm(at)freesoft(dot)cz>
8 ;; Stefan Bruda <stefan(at)bruda(dot)ca>
9 ;; * See below for more details
10 ;; Maintainer: Stefan Bruda <stefan(at)bruda(dot)ca>
11 ;; Keywords: prolog major mode sicstus swi mercury
13 (defvar prolog-mode-version "1.22"
14 "Prolog mode version number.")
16 ;; This file is part of GNU Emacs.
18 ;; GNU Emacs is free software: you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation, either version 3 of the License, or
21 ;; (at your option) any later version.
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 ;; Original author: Masanobu UMEDA <umerin(at)mse(dot)kyutech(dot)ac(dot)jp>
32 ;; Parts of this file was taken from a modified version of the original
33 ;; by Johan Andersson, Peter Olin, Mats Carlsson, Johan Bevemyr, Stefan
34 ;; Andersson, and Per Danielsson (all SICS people), and Henrik Båkman
35 ;; at Uppsala University, Sweden.
37 ;; Some ideas and also a few lines of code have been borrowed (not stolen ;-)
38 ;; from Oz.el, the Emacs major mode for the Oz programming language,
39 ;; Copyright (C) 1993 DFKI GmbH, Germany, with permission.
40 ;; Authored by Ralf Scheidhauer and Michael Mehl
41 ;; ([scheidhr|mehl](at)dfki(dot)uni-sb(dot)de)
43 ;; More ideas and code have been taken from the SICStus debugger mode
44 ;; (http://www.csd.uu.se/~perm/source_debug/index.shtml -- broken link
45 ;; as of Mon May 5 08:23:48 EDT 2003) by Per Mildner.
47 ;; Additions for ECLiPSe and other helpful suggestions: Stephan Heuel
48 ;; <heuel(at)ipb(dot)uni-bonn(dot)de>
50 ;;; Commentary:
52 ;; This package provides a major mode for editing Prolog code, with
53 ;; all the bells and whistles one would expect, including syntax
54 ;; highlighting and auto indentation. It can also send regions to an
55 ;; inferior Prolog process.
57 ;; The code requires the comint, easymenu, info, imenu, and font-lock
58 ;; libraries. These are normally distributed with GNU Emacs and
59 ;; XEmacs.
61 ;;; Installation:
63 ;; Insert the following lines in your init file:
65 ;; (setq load-path (cons "/usr/lib/xemacs/site-lisp" load-path))
66 ;; (autoload 'run-prolog "prolog" "Start a Prolog sub-process." t)
67 ;; (autoload 'prolog-mode "prolog" "Major mode for editing Prolog programs." t)
68 ;; (autoload 'mercury-mode "prolog" "Major mode for editing Mercury programs." t)
69 ;; (setq prolog-system 'swi) ; optional, the system you are using;
70 ;; ; see `prolog-system' below for possible values
71 ;; (setq auto-mode-alist (append '(("\\.pl$" . prolog-mode)
72 ;; ("\\.m$" . mercury-mode))
73 ;; auto-mode-alist))
75 ;; where the path in the first line is the file system path to this file.
76 ;; MSDOS paths can be written like "d:/programs/emacs-19.34/site-lisp".
77 ;; Note: In XEmacs, either `/usr/lib/xemacs/site-lisp' (RPM default in
78 ;; Red Hat-based distributions) or `/usr/local/lib/xemacs/site-lisp'
79 ;; (default when compiling from sources) are automatically added to
80 ;; `load-path', so the first line is not necessary provided that you
81 ;; put this file in the appropriate place.
83 ;; The last s-expression above makes sure that files ending with .pl
84 ;; are assumed to be Prolog files and not Perl, which is the default
85 ;; Emacs setting. If this is not wanted, remove this line. It is then
86 ;; necessary to either
88 ;; o insert in your Prolog files the following comment as the first line:
90 ;; % -*- Mode: Prolog -*-
92 ;; and then the file will be open in Prolog mode no matter its
93 ;; extension, or
95 ;; o manually switch to prolog mode after opening a Prolog file, by typing
96 ;; M-x prolog-mode.
98 ;; If the command to start the prolog process ('sicstus', 'pl' or
99 ;; 'swipl' for SWI prolog, etc.) is not available in the default path,
100 ;; then it is necessary to set the value of the environment variable
101 ;; EPROLOG to a shell command to invoke the prolog process. In XEmacs
102 ;; and Emacs 20+ you can also customize the variable
103 ;; `prolog-program-name' (in the group `prolog-inferior') and provide
104 ;; a full path for your Prolog system (swi, scitus, etc.).
106 ;; Note: I (Stefan, the current maintainer) work under XEmacs. Future
107 ;; developments will thus be biased towards XEmacs (OK, I admit it,
108 ;; I am biased towards XEmacs in general), though I will do my best
109 ;; to keep the GNU Emacs compatibility. So if you work under Emacs
110 ;; and see something that does not work do drop me a line, as I have
111 ;; a smaller chance to notice this kind of bugs otherwise.
113 ;; Changelog:
115 ;; Version 1.22:
116 ;; o Allowed both 'swipl' and 'pl' as names for the SWI Prolog
117 ;; interpreter.
118 ;; o Atoms that start a line are not blindly colored as
119 ;; predicates. Instead we check that they are followed by ( or
120 ;; :- first. Patch suggested by Guy Wiener.
121 ;; Version 1.21:
122 ;; o Cleaned up the code that defines faces. The missing face
123 ;; warnings on some Emacsen should disappear.
124 ;; Version 1.20:
125 ;; o Improved the handling of clause start detection and multi-line
126 ;; comments: `prolog-clause-start' no longer finds non-predicate
127 ;; (e.g., capitalized strings) beginning of clauses.
128 ;; `prolog-tokenize' recognizes when the end point is within a
129 ;; multi-line comment.
130 ;; Version 1.19:
131 ;; o Minimal changes for Aquamacs inclusion and in general for
132 ;; better coping with finding the Prolog executable. Patch
133 ;; provided by David Reitter
134 ;; Version 1.18:
135 ;; o Fixed syntax highlighting for clause heads that do not begin at
136 ;; the beginning of the line.
137 ;; o Fixed compilation warnings under Emacs.
138 ;; o Updated the email address of the current maintainer.
139 ;; Version 1.17:
140 ;; o Minor indentation fix (patch by Markus Triska)
141 ;; o `prolog-underscore-wordchar-flag' defaults now to nil (more
142 ;; consistent to other Emacs modes)
143 ;; Version 1.16:
144 ;; o Eliminated a possible compilation warning.
145 ;; Version 1.15:
146 ;; o Introduced three new customizable variables: electric colon
147 ;; (`prolog-electric-colon-flag', default nil), electric dash
148 ;; (`prolog-electric-dash-flag', default nil), and a possibility
149 ;; to prevent the predicate template insertion from adding commas
150 ;; (`prolog-electric-dot-full-predicate-template', defaults to t
151 ;; since it seems quicker to me to just type those commas). A
152 ;; trivial adaptation of a patch by Markus Triska.
153 ;; o Improved the behavior of electric if-then-else to only skip
154 ;; forward if the parenthesis/semicolon is preceded by
155 ;; whitespace. Once more a trivial adaptation of a patch by
156 ;; Markus Triska.
157 ;; Version 1.14:
158 ;; o Cleaned up align code. `prolog-align-flag' is eliminated (since
159 ;; on a second thought it does not do anything useful). Added key
160 ;; binding (C-c C-a) and menu entry for alignment.
161 ;; o Condensed regular expressions for lower and upper case
162 ;; characters (GNU Emacs seems to go over the regexp length limit
163 ;; with the original form). My code on the matter was improved
164 ;; considerably by Markus Triska.
165 ;; o Fixed `prolog-insert-spaces-after-paren' (which used an
166 ;; uninitialized variable).
167 ;; o Minor changes to clean up the code and avoid some implicit
168 ;; package requirements.
169 ;; Version 1.13:
170 ;; o Removed the use of `map-char-table' in `prolog-build-case-strings'
171 ;; which appears to cause problems in (at least) Emacs 23.0.0.1.
172 ;; o Added if-then-else indentation + corresponding electric
173 ;; characters. New customization: `prolog-electric-if-then-else-flag'
174 ;; o Align support (requires `align'). New customization:
175 ;; `prolog-align-flag'.
176 ;; o Temporary consult files have now the same name throughout the
177 ;; session. This prevents issues with reconsulting a buffer
178 ;; (this event is no longer passed to Prolog as a request to
179 ;; consult a new file).
180 ;; o Adaptive fill mode is now turned on. Comment indentation is
181 ;; still worse than it could be though, I am working on it.
182 ;; o Improved filling and auto-filling capabilities. Now block
183 ;; comments should be [auto-]filled correctly most of the time;
184 ;; the following pattern in particular is worth noting as being
185 ;; filled correctly:
186 ;; <some code here> % some comment here that goes beyond the
187 ;; % rightmost column, possibly combined with
188 ;; % subsequent comment lines
189 ;; o `prolog-char-quote-workaround' now defaults to nil.
190 ;; o Note: Many of the above improvements have been suggested by
191 ;; Markus Triska, who also provided useful patches on the matter
192 ;; when he realized that I was slow in responding. Many thanks.
193 ;; Version 1.11 / 1.12
194 ;; o GNU Emacs compatibility fix for paragraph filling (fixed
195 ;; incorrectly in 1.11, fix fixed in 1.12).
196 ;; Version 1.10
197 ;; o Added paragraph filling in comment blocks and also correct auto
198 ;; filling for comments.
199 ;; o Fixed the possible "Regular expression too big" error in
200 ;; `prolog-electric-dot'.
201 ;; Version 1.9
202 ;; o Parenthesis expressions are now indented by default so that
203 ;; components go one underneath the other, just as for compound
204 ;; terms. You can use the old style (the second and subsequent
205 ;; lines being indented to the right in a parenthesis expression)
206 ;; by setting the customizable variable `prolog-paren-indent-p'
207 ;; (group "Prolog Indentation") to t.
208 ;; o (Somehow awkward) handling of the 0' character escape
209 ;; sequence. I am looking into a better way of doing it but
210 ;; prospects look bleak. If this breaks things for you please let
211 ;; me know and also set the `prolog-char-quote-workaround' (group
212 ;; "Prolog Other") to nil.
213 ;; Version 1.8
214 ;; o Key binding fix.
215 ;; Version 1.7
216 ;; o Fixed a number of issues with the syntax of single quotes,
217 ;; including Debian bug #324520.
218 ;; Version 1.6
219 ;; o Fixed mercury mode menu initialization (Debian bug #226121).
220 ;; o Fixed (i.e., eliminated) Delete remapping (Debian bug #229636).
221 ;; o Corrected indentation for clauses defining quoted atoms.
222 ;; Version 1.5:
223 ;; o Keywords fontifying should work in console mode so this is
224 ;; enabled everywhere.
225 ;; Version 1.4:
226 ;; o Now supports GNU Prolog--minor adaptation of a patch by Stefan
227 ;; Moeding.
228 ;; Version 1.3:
229 ;; o Info-follow-nearest-node now called correctly under Emacs too
230 ;; (thanks to Nicolas Pelletier). Should be implemented more
231 ;; elegantly (i.e., without compilation warnings) in the future.
232 ;; Version 1.2:
233 ;; o Another prompt fix, still in SWI mode (people seem to have
234 ;; changed the prompt of SWI Prolog).
235 ;; Version 1.1:
236 ;; o Fixed dots in the end of line comments causing indentation
237 ;; problems. The following code is now correctly indented (note
238 ;; the dot terminating the comment):
239 ;; a(X) :- b(X),
240 ;; c(X). % comment here.
241 ;; a(X).
242 ;; and so is this (and variants):
243 ;; a(X) :- b(X),
244 ;; c(X). /* comment here. */
245 ;; a(X).
246 ;; Version 1.0:
247 ;; o Revamped the menu system.
248 ;; o Yet another prompt recognition fix (SWI mode).
249 ;; o This is more of a renumbering than a new edition. I promoted
250 ;; the mode to version 1.0 to emphasize the fact that it is now
251 ;; mature and stable enough to be considered production (in my
252 ;; opinion anyway).
253 ;; Version 0.1.41:
254 ;; o GNU Emacs compatibility fixes.
255 ;; Version 0.1.40:
256 ;; o prolog-get-predspec is now suitable to be called as
257 ;; imenu-extract-index-name-function. The predicate index works.
258 ;; o Since imenu works now as advertised, prolog-imenu-flag is t
259 ;; by default.
260 ;; o Eliminated prolog-create-predicate-index since the imenu
261 ;; utilities now work well. Actually, this function is also
262 ;; buggy, and I see no reason to fix it since we do not need it
263 ;; anyway.
264 ;; o Fixed prolog-pred-start, prolog-clause-start, prolog-clause-info.
265 ;; o Fix for prolog-build-case-strings; now prolog-upper-case-string
266 ;; and prolog-lower-case-string are correctly initialized,
267 ;; o Various font-lock changes; most importantly, block comments (/*
268 ;; ... */) are now correctly fontified in XEmacs even when they
269 ;; extend on multiple lines.
270 ;; Version 0.1.36:
271 ;; o The debug prompt of SWI Prolog is now correctly recognized.
272 ;; Version 0.1.35:
273 ;; o Minor font-lock bug fixes.
275 ;;; TODO:
277 ;; Replace ":type 'sexp" with more precise Custom types.
279 ;;; Code:
281 (require 'comint)
283 (eval-when-compile
284 (require 'font-lock)
285 ;; We need imenu everywhere because of the predicate index!
286 (require 'imenu)
288 (require 'shell)
291 (require 'easymenu)
292 (require 'align)
295 (defgroup prolog nil
296 "Editing and running Prolog and Mercury files."
297 :group 'languages)
299 (defgroup prolog-faces nil
300 "Prolog mode specific faces."
301 :group 'font-lock)
303 (defgroup prolog-indentation nil
304 "Prolog mode indentation configuration."
305 :group 'prolog)
307 (defgroup prolog-font-lock nil
308 "Prolog mode font locking patterns."
309 :group 'prolog)
311 (defgroup prolog-keyboard nil
312 "Prolog mode keyboard flags."
313 :group 'prolog)
315 (defgroup prolog-inferior nil
316 "Inferior Prolog mode options."
317 :group 'prolog)
319 (defgroup prolog-other nil
320 "Other Prolog mode options."
321 :group 'prolog)
324 ;;-------------------------------------------------------------------
325 ;; User configurable variables
326 ;;-------------------------------------------------------------------
328 ;; General configuration
330 (defcustom prolog-system nil
331 "Prolog interpreter/compiler used.
332 The value of this variable is nil or a symbol.
333 If it is a symbol, it determines default values of other configuration
334 variables with respect to properties of the specified Prolog
335 interpreter/compiler.
337 Currently recognized symbol values are:
338 eclipse - Eclipse Prolog
339 mercury - Mercury
340 sicstus - SICStus Prolog
341 swi - SWI Prolog
342 gnu - GNU Prolog"
343 :version "24.1"
344 :group 'prolog
345 :type '(choice (const :tag "SICStus" :value sicstus)
346 (const :tag "SWI Prolog" :value swi)
347 (const :tag "GNU Prolog" :value gnu)
348 (const :tag "ECLiPSe Prolog" :value eclipse)
349 ;; Mercury shouldn't be needed since we have a separate
350 ;; major mode for it.
351 (const :tag "Default" :value nil)))
352 (make-variable-buffer-local 'prolog-system)
354 ;; NB: This alist can not be processed in prolog-mode-variables to
355 ;; create a prolog-system-version-i variable since it is needed
356 ;; prior to the call to prolog-mode-variables.
357 (defcustom prolog-system-version
358 '((sicstus (3 . 6))
359 (swi (0 . 0))
360 (mercury (0 . 0))
361 (eclipse (3 . 7))
362 (gnu (0 . 0)))
363 ;; FIXME: This should be auto-detected instead of user-provided.
364 "Alist of Prolog system versions.
365 The version numbers are of the format (Major . Minor)."
366 :version "24.1"
367 :type '(repeat (list (symbol :tag "System")
368 (cons :tag "Version numbers" (integer :tag "Major")
369 (integer :tag "Minor"))))
370 :group 'prolog)
372 ;; Indentation
374 (defcustom prolog-indent-width 4
375 "The indentation width used by the editing buffer."
376 :group 'prolog-indentation
377 :type 'integer)
379 (defcustom prolog-left-indent-regexp "\\(;\\|\\*?->\\)"
380 "Regexp for `prolog-electric-if-then-else-flag'."
381 :version "24.1"
382 :group 'prolog-indentation
383 :type 'regexp)
385 (defcustom prolog-paren-indent-p nil
386 "If non-nil, increase indentation for parenthesis expressions.
387 The second and subsequent line in a parenthesis expression other than
388 a compound term can either be indented `prolog-paren-indent' to the
389 right (if this variable is non-nil) or in the same way as for compound
390 terms (if this variable is nil, default)."
391 :version "24.1"
392 :group 'prolog-indentation
393 :type 'boolean)
395 (defcustom prolog-paren-indent 4
396 "The indentation increase for parenthesis expressions.
397 Only used in ( If -> Then ; Else) and ( Disj1 ; Disj2 ) style expressions."
398 :version "24.1"
399 :group 'prolog-indentation
400 :type 'integer)
402 (defcustom prolog-parse-mode 'beg-of-clause
403 "The parse mode used (decides from which point parsing is done).
404 Legal values:
405 'beg-of-line - starts parsing at the beginning of a line, unless the
406 previous line ends with a backslash. Fast, but has
407 problems detecting multiline /* */ comments.
408 'beg-of-clause - starts parsing at the beginning of the current clause.
409 Slow, but copes better with /* */ comments."
410 :version "24.1"
411 :group 'prolog-indentation
412 :type '(choice (const :value beg-of-line)
413 (const :value beg-of-clause)))
415 ;; Font locking
417 (defcustom prolog-keywords
418 '((eclipse
419 ("use_module" "begin_module" "module_interface" "dynamic"
420 "external" "export" "dbgcomp" "nodbgcomp" "compile"))
421 (mercury
422 ("all" "else" "end_module" "equality" "external" "fail" "func" "if"
423 "implementation" "import_module" "include_module" "inst" "instance"
424 "interface" "mode" "module" "not" "pragma" "pred" "some" "then" "true"
425 "type" "typeclass" "use_module" "where"))
426 (sicstus
427 ("block" "dynamic" "mode" "module" "multifile" "meta_predicate"
428 "parallel" "public" "sequential" "volatile"))
429 (swi
430 ("discontiguous" "dynamic" "ensure_loaded" "export" "export_list" "import"
431 "meta_predicate" "module" "module_transparent" "multifile" "require"
432 "use_module" "volatile"))
433 (gnu
434 ("built_in" "char_conversion" "discontiguous" "dynamic" "ensure_linked"
435 "ensure_loaded" "foreign" "include" "initialization" "multifile" "op"
436 "public" "set_prolog_flag"))
438 ;; FIXME: Shouldn't we just use the union of all the above here?
439 ("dynamic" "module")))
440 "Alist of Prolog keywords which is used for font locking of directives."
441 :version "24.1"
442 :group 'prolog-font-lock
443 :type 'sexp)
445 (defcustom prolog-types
446 '((mercury
447 ("char" "float" "int" "io__state" "string" "univ"))
448 (t nil))
449 "Alist of Prolog types used by font locking."
450 :version "24.1"
451 :group 'prolog-font-lock
452 :type 'sexp)
454 (defcustom prolog-mode-specificators
455 '((mercury
456 ("bound" "di" "free" "ground" "in" "mdi" "mui" "muo" "out" "ui" "uo"))
457 (t nil))
458 "Alist of Prolog mode specificators used by font locking."
459 :version "24.1"
460 :group 'prolog-font-lock
461 :type 'sexp)
463 (defcustom prolog-determinism-specificators
464 '((mercury
465 ("cc_multi" "cc_nondet" "det" "erroneous" "failure" "multi" "nondet"
466 "semidet"))
467 (t nil))
468 "Alist of Prolog determinism specificators used by font locking."
469 :version "24.1"
470 :group 'prolog-font-lock
471 :type 'sexp)
473 (defcustom prolog-directives
474 '((mercury
475 ("^#[0-9]+"))
476 (t nil))
477 "Alist of Prolog source code directives used by font locking."
478 :version "24.1"
479 :group 'prolog-font-lock
480 :type 'sexp)
483 ;; Keyboard
485 (defcustom prolog-hungry-delete-key-flag nil
486 "Non-nil means delete key consumes all preceding spaces."
487 :version "24.1"
488 :group 'prolog-keyboard
489 :type 'boolean)
491 (defcustom prolog-electric-dot-flag nil
492 "Non-nil means make dot key electric.
493 Electric dot appends newline or inserts head of a new clause.
494 If dot is pressed at the end of a line where at least one white space
495 precedes the point, it inserts a recursive call to the current predicate.
496 If dot is pressed at the beginning of an empty line, it inserts the head
497 of a new clause for the current predicate. It does not apply in strings
498 and comments.
499 It does not apply in strings and comments."
500 :version "24.1"
501 :group 'prolog-keyboard
502 :type 'boolean)
504 (defcustom prolog-electric-dot-full-predicate-template nil
505 "If nil, electric dot inserts only the current predicate's name and `('
506 for recursive calls or new clause heads. Non-nil means to also
507 insert enough commas to cover the predicate's arity and `)',
508 and dot and newline for recursive calls."
509 :version "24.1"
510 :group 'prolog-keyboard
511 :type 'boolean)
513 (defcustom prolog-electric-underscore-flag nil
514 "Non-nil means make underscore key electric.
515 Electric underscore replaces the current variable with underscore.
516 If underscore is pressed not on a variable then it behaves as usual."
517 :version "24.1"
518 :group 'prolog-keyboard
519 :type 'boolean)
521 (defcustom prolog-electric-if-then-else-flag nil
522 "Non-nil makes `(', `>' and `;' electric
523 to automatically indent if-then-else constructs."
524 :version "24.1"
525 :group 'prolog-keyboard
526 :type 'boolean)
528 (defcustom prolog-electric-colon-flag nil
529 "Makes `:' electric (inserts `:-' on a new line).
530 If non-nil, pressing `:' at the end of a line that starts in
531 the first column (i.e., clause heads) inserts ` :-' and newline."
532 :version "24.1"
533 :group 'prolog-keyboard
534 :type 'boolean)
536 (defcustom prolog-electric-dash-flag nil
537 "Makes `-' electric (inserts a `-->' on a new line).
538 If non-nil, pressing `-' at the end of a line that starts in
539 the first column (i.e., DCG heads) inserts ` -->' and newline."
540 :version "24.1"
541 :group 'prolog-keyboard
542 :type 'boolean)
544 (defcustom prolog-old-sicstus-keys-flag nil
545 "Non-nil means old SICStus Prolog mode keybindings are used."
546 :version "24.1"
547 :group 'prolog-keyboard
548 :type 'boolean)
550 ;; Inferior mode
552 (defcustom prolog-program-name
553 `(((getenv "EPROLOG") (eval (getenv "EPROLOG")))
554 (eclipse "eclipse")
555 (mercury nil)
556 (sicstus "sicstus")
557 (swi ,(if (not (executable-find "swipl")) "pl" "swipl"))
558 (gnu "gprolog")
559 (t ,(let ((names '("prolog" "gprolog" "swipl" "pl")))
560 (while (and names
561 (not (executable-find (car names))))
562 (setq names (cdr names)))
563 (or (car names) "prolog"))))
564 "Alist of program names for invoking an inferior Prolog with `run-prolog'."
565 :group 'prolog-inferior
566 :type 'sexp)
567 (defun prolog-program-name ()
568 (prolog-find-value-by-system prolog-program-name))
570 (defcustom prolog-program-switches
571 '((sicstus ("-i"))
572 (t nil))
573 "Alist of switches given to inferior Prolog run with `run-prolog'."
574 :version "24.1"
575 :group 'prolog-inferior
576 :type 'sexp)
577 (defun prolog-program-switches ()
578 (prolog-find-value-by-system prolog-program-switches))
580 (defcustom prolog-consult-string
581 '((eclipse "[%f].")
582 (mercury nil)
583 (sicstus (eval (if (prolog-atleast-version '(3 . 7))
584 "prolog:zap_file(%m,%b,consult,%l)."
585 "prolog:zap_file(%m,%b,consult).")))
586 (swi "[%f].")
587 (gnu "[%f].")
588 (t "reconsult(%f)."))
589 "Alist of strings defining predicate for reconsulting.
591 Some parts of the string are replaced:
592 `%f' by the name of the consulted file (can be a temporary file)
593 `%b' by the file name of the buffer to consult
594 `%m' by the module name and name of the consulted file separated by colon
595 `%l' by the line offset into the file. This is 0 unless consulting a
596 region of a buffer, in which case it is the number of lines before
597 the region."
598 :group 'prolog-inferior
599 :type 'sexp)
600 (defun prolog-consult-string ()
601 (prolog-find-value-by-system prolog-consult-string))
603 (defcustom prolog-compile-string
604 '((eclipse "[%f].")
605 (mercury "mmake ")
606 (sicstus (eval (if (prolog-atleast-version '(3 . 7))
607 "prolog:zap_file(%m,%b,compile,%l)."
608 "prolog:zap_file(%m,%b,compile).")))
609 (swi "[%f].")
610 (t "compile(%f)."))
611 "Alist of strings and lists defining predicate for recompilation.
613 Some parts of the string are replaced:
614 `%f' by the name of the compiled file (can be a temporary file)
615 `%b' by the file name of the buffer to compile
616 `%m' by the module name and name of the compiled file separated by colon
617 `%l' by the line offset into the file. This is 0 unless compiling a
618 region of a buffer, in which case it is the number of lines before
619 the region.
621 If `prolog-program-name' is non-nil, it is a string sent to a Prolog process.
622 If `prolog-program-name' is nil, it is an argument to the `compile' function."
623 :group 'prolog-inferior
624 :type 'sexp)
625 (defun prolog-compile-string ()
626 (prolog-find-value-by-system prolog-compile-string))
628 (defcustom prolog-eof-string "end_of_file.\n"
629 "Alist of strings that represent end of file for prolog.
630 nil means send actual operating system end of file."
631 :group 'prolog-inferior
632 :type 'sexp)
634 (defcustom prolog-prompt-regexp
635 '((eclipse "^[a-zA-Z0-9()]* *\\?- \\|^\\[[a-zA-Z]* [0-9]*\\]:")
636 (sicstus "| [ ?][- ] *")
637 (swi "^\\(\\[[a-zA-Z]*\\] \\)?[1-9]?[0-9]*[ ]?\\?- \\|^| +")
638 (gnu "^| \\?-")
639 (t "^|? *\\?-"))
640 "Alist of prompts of the prolog system command line."
641 :version "24.1"
642 :group 'prolog-inferior
643 :type 'sexp)
644 (defun prolog-prompt-regexp ()
645 (prolog-find-value-by-system prolog-prompt-regexp))
647 ;; (defcustom prolog-continued-prompt-regexp
648 ;; '((sicstus "^\\(| +\\| +\\)")
649 ;; (t "^|: +"))
650 ;; "Alist of regexps matching the prompt when consulting `user'."
651 ;; :group 'prolog-inferior
652 ;; :type 'sexp)
654 (defcustom prolog-debug-on-string "debug.\n"
655 "Predicate for enabling debug mode."
656 :version "24.1"
657 :group 'prolog-inferior
658 :type 'string)
660 (defcustom prolog-debug-off-string "nodebug.\n"
661 "Predicate for disabling debug mode."
662 :version "24.1"
663 :group 'prolog-inferior
664 :type 'string)
666 (defcustom prolog-trace-on-string "trace.\n"
667 "Predicate for enabling tracing."
668 :version "24.1"
669 :group 'prolog-inferior
670 :type 'string)
672 (defcustom prolog-trace-off-string "notrace.\n"
673 "Predicate for disabling tracing."
674 :version "24.1"
675 :group 'prolog-inferior
676 :type 'string)
678 (defcustom prolog-zip-on-string "zip.\n"
679 "Predicate for enabling zip mode for SICStus."
680 :version "24.1"
681 :group 'prolog-inferior
682 :type 'string)
684 (defcustom prolog-zip-off-string "nozip.\n"
685 "Predicate for disabling zip mode for SICStus."
686 :version "24.1"
687 :group 'prolog-inferior
688 :type 'string)
690 (defcustom prolog-use-standard-consult-compile-method-flag t
691 "Non-nil means use the standard compilation method.
692 Otherwise the new compilation method will be used. This
693 utilizes a special compilation buffer with the associated
694 features such as parsing of error messages and automatically
695 jumping to the source code responsible for the error.
697 Warning: the new method is so far only experimental and
698 does contain bugs. The recommended setting for the novice user
699 is non-nil for this variable."
700 :version "24.1"
701 :group 'prolog-inferior
702 :type 'boolean)
705 ;; Miscellaneous
707 (defcustom prolog-imenu-flag t
708 "Non-nil means add a clause index menu for all prolog files."
709 :version "24.1"
710 :group 'prolog-other
711 :type 'boolean)
713 (defcustom prolog-imenu-max-lines 3000
714 "The maximum number of lines of the file for imenu to be enabled.
715 Relevant only when `prolog-imenu-flag' is non-nil."
716 :version "24.1"
717 :group 'prolog-other
718 :type 'integer)
720 (defcustom prolog-info-predicate-index
721 "(sicstus)Predicate Index"
722 "The info node for the SICStus predicate index."
723 :version "24.1"
724 :group 'prolog-other
725 :type 'string)
727 (defcustom prolog-underscore-wordchar-flag nil
728 "Non-nil means underscore (_) is a word-constituent character."
729 :version "24.1"
730 :group 'prolog-other
731 :type 'boolean)
732 (make-obsolete-variable 'prolog-underscore-wordchar-flag
733 'superword-mode "24.4")
735 (defcustom prolog-use-sicstus-sd nil
736 "If non-nil, use the source level debugger of SICStus 3#7 and later."
737 :version "24.1"
738 :group 'prolog-other
739 :type 'boolean)
741 (defcustom prolog-char-quote-workaround nil
742 "If non-nil, declare 0 as a quote character to handle 0'<char>.
743 This is really kludgy, and unneeded (i.e. obsolete) in Emacs>=24."
744 :version "24.1"
745 :group 'prolog-other
746 :type 'boolean)
747 (make-obsolete-variable 'prolog-char-quote-workaround nil "24.1")
750 ;;-------------------------------------------------------------------
751 ;; Internal variables
752 ;;-------------------------------------------------------------------
754 ;;(defvar prolog-temp-filename "") ; Later set by `prolog-temporary-file'
756 (defvar prolog-mode-syntax-table
757 ;; The syntax accepted varies depending on the implementation used.
758 ;; Here are some of the differences:
759 ;; - SWI-Prolog accepts nested /*..*/ comments.
760 ;; - Edinburgh-style Prologs take <radix>'<number> for non-decimal number,
761 ;; whereas ISO-style Prologs use 0[obx]<number> instead.
762 ;; - In atoms \x<hex> sometimes needs a terminating \ (ISO-style)
763 ;; and sometimes not.
764 (let ((table (make-syntax-table)))
765 (modify-syntax-entry ?_ (if prolog-underscore-wordchar-flag "w" "_") table)
766 (modify-syntax-entry ?+ "." table)
767 (modify-syntax-entry ?- "." table)
768 (modify-syntax-entry ?= "." table)
769 (modify-syntax-entry ?< "." table)
770 (modify-syntax-entry ?> "." table)
771 (modify-syntax-entry ?| "." table)
772 (modify-syntax-entry ?\' "\"" table)
774 ;; Any better way to handle the 0'<char> construct?!?
775 (when (and prolog-char-quote-workaround
776 (not (fboundp 'syntax-propertize-rules)))
777 (modify-syntax-entry ?0 "\\" table))
779 (modify-syntax-entry ?% "<" table)
780 (modify-syntax-entry ?\n ">" table)
781 (if (featurep 'xemacs)
782 (progn
783 (modify-syntax-entry ?* ". 67" table)
784 (modify-syntax-entry ?/ ". 58" table)
786 ;; Emacs wants to see this it seems:
787 (modify-syntax-entry ?* ". 23b" table)
788 (modify-syntax-entry ?/ ". 14" table)
790 table))
792 (defconst prolog-atom-char-regexp
793 "[[:alnum:]_$]"
794 "Regexp specifying characters which constitute atoms without quoting.")
795 (defconst prolog-atom-regexp
796 (format "[[:lower:]$]%s*" prolog-atom-char-regexp))
798 (defconst prolog-left-paren "[[({]" ;FIXME: Why not \\s(?
799 "The characters used as left parentheses for the indentation code.")
800 (defconst prolog-right-paren "[])}]" ;FIXME: Why not \\s)?
801 "The characters used as right parentheses for the indentation code.")
803 (defconst prolog-quoted-atom-regexp
804 "\\(^\\|[^0-9]\\)\\('\\([^\n']\\|\\\\'\\)*'\\)"
805 "Regexp matching a quoted atom.")
806 (defconst prolog-string-regexp
807 "\\(\"\\([^\n\"]\\|\\\\\"\\)*\"\\)"
808 "Regexp matching a string.")
809 (defconst prolog-head-delimiter "\\(:-\\|\\+:\\|-:\\|\\+\\?\\|-\\?\\|-->\\)"
810 "A regexp for matching on the end delimiter of a head (e.g. \":-\").")
812 (defvar prolog-compilation-buffer "*prolog-compilation*"
813 "Name of the output buffer for Prolog compilation/consulting.")
815 (defvar prolog-temporary-file-name nil)
816 (defvar prolog-keywords-i nil)
817 (defvar prolog-types-i nil)
818 (defvar prolog-mode-specificators-i nil)
819 (defvar prolog-determinism-specificators-i nil)
820 (defvar prolog-directives-i nil)
821 (defvar prolog-eof-string-i nil)
822 ;; (defvar prolog-continued-prompt-regexp-i nil)
823 (defvar prolog-help-function-i nil)
825 (defvar prolog-align-rules
826 (eval-when-compile
827 (mapcar
828 (lambda (x)
829 (let ((name (car x))
830 (sym (cdr x)))
831 `(,(intern (format "prolog-%s" name))
832 (regexp . ,(format "\\(\\s-*\\)%s\\(\\s-*\\)" sym))
833 (tab-stop . nil)
834 (modes . '(prolog-mode))
835 (group . (1 2)))))
836 '(("dcg" . "-->") ("rule" . ":-") ("simplification" . "<=>")
837 ("propagation" . "==>")))))
839 ;; SMIE support
841 (require 'smie)
843 (defun prolog-smie-forward-token ()
844 ;; FIXME: Add support for 0'<char>, if needed after adding it to
845 ;; syntax-propertize-functions.
846 (forward-comment (point-max))
847 (buffer-substring-no-properties
848 (point)
849 (progn (cond
850 ((looking-at "[!;]") (forward-char 1))
851 ((not (zerop (skip-chars-forward "#&*+-./:<=>?@\\^`~"))))
852 ((not (zerop (skip-syntax-forward "w_'"))))
853 ;; In case of non-ASCII punctuation.
854 ((not (zerop (skip-syntax-forward ".")))))
855 (point))))
857 (defun prolog-smie-backward-token ()
858 ;; FIXME: Add support for 0'<char>, if needed after adding it to
859 ;; syntax-propertize-functions.
860 (forward-comment (- (point-max)))
861 (buffer-substring-no-properties
862 (point)
863 (progn (cond
864 ((memq (char-before) '(?! ?\;)) (forward-char -1))
865 ((not (zerop (skip-chars-backward "#&*+-./:<=>?@\\^`~"))))
866 ((not (zerop (skip-syntax-backward "w_'"))))
867 ;; In case of non-ASCII punctuation.
868 ((not (zerop (skip-syntax-backward ".")))))
869 (point))))
871 (defconst prolog-smie-grammar
872 ;; Rather than construct the operator levels table from the BNF,
873 ;; we directly provide the operator precedences from GNU Prolog's
874 ;; manual (7.14.10 op/3). The only problem is that GNU Prolog's
875 ;; manual uses precedence levels in the opposite sense (higher
876 ;; numbers bind less tightly) than SMIE, so we use negative numbers.
877 '(("." -10000 -10000)
878 (":-" -1200 -1200)
879 ("-->" -1200 -1200)
880 (";" -1100 -1100)
881 ("->" -1050 -1050)
882 ("," -1000 -1000)
883 ("\\+" -900 -900)
884 ("=" -700 -700)
885 ("\\=" -700 -700)
886 ("=.." -700 -700)
887 ("==" -700 -700)
888 ("\\==" -700 -700)
889 ("@<" -700 -700)
890 ("@=<" -700 -700)
891 ("@>" -700 -700)
892 ("@>=" -700 -700)
893 ("is" -700 -700)
894 ("=:=" -700 -700)
895 ("=\\=" -700 -700)
896 ("<" -700 -700)
897 ("=<" -700 -700)
898 (">" -700 -700)
899 (">=" -700 -700)
900 (":" -600 -600)
901 ("+" -500 -500)
902 ("-" -500 -500)
903 ("/\\" -500 -500)
904 ("\\/" -500 -500)
905 ("*" -400 -400)
906 ("/" -400 -400)
907 ("//" -400 -400)
908 ("rem" -400 -400)
909 ("mod" -400 -400)
910 ("<<" -400 -400)
911 (">>" -400 -400)
912 ("**" -200 -200)
913 ("^" -200 -200)
914 ;; Prefix
915 ;; ("+" 200 200)
916 ;; ("-" 200 200)
917 ;; ("\\" 200 200)
918 (:smie-closer-alist (t . "."))
920 "Precedence levels of infix operators.")
922 (defun prolog-smie-rules (kind token)
923 (pcase (cons kind token)
924 (`(:elem . basic) prolog-indent-width)
925 (`(:after . ".") '(column . 0)) ;; To work around smie-closer-alist.
926 ;; Allow indentation of if-then-else as:
927 ;; ( test
928 ;; -> thenrule
929 ;; ; elserule
930 ;; )
931 (`(:before . ,(or `"->" `";"))
932 (and (smie-rule-bolp) (smie-rule-parent-p "(") (smie-rule-parent 1)))
933 (`(:after . ,(or `":-" `"->" `"-->")) prolog-indent-width)))
936 ;;-------------------------------------------------------------------
937 ;; Prolog mode
938 ;;-------------------------------------------------------------------
940 ;; Example: (prolog-atleast-version '(3 . 6))
941 (defun prolog-atleast-version (version)
942 "Return t if the version of the current prolog system is VERSION or later.
943 VERSION is of the format (Major . Minor)"
944 ;; Version.major < major or
945 ;; Version.major = major and Version.minor <= minor
946 (let* ((thisversion (prolog-find-value-by-system prolog-system-version))
947 (thismajor (car thisversion))
948 (thisminor (cdr thisversion)))
949 (or (< (car version) thismajor)
950 (and (= (car version) thismajor)
951 (<= (cdr version) thisminor)))
954 (define-abbrev-table 'prolog-mode-abbrev-table ())
956 (defun prolog-find-value-by-system (alist)
957 "Get value from ALIST according to `prolog-system'."
958 (let ((system (or prolog-system
959 (let ((infbuf (prolog-inferior-buffer 'dont-run)))
960 (when infbuf
961 (buffer-local-value 'prolog-system infbuf))))))
962 (if (listp alist)
963 (let (result
965 (while alist
966 (setq id (car (car alist)))
967 (if (or (eq id system)
968 (eq id t)
969 (and (listp id)
970 (eval id)))
971 (progn
972 (setq result (car (cdr (car alist))))
973 (if (and (listp result)
974 (eq (car result) 'eval))
975 (setq result (eval (car (cdr result)))))
976 (setq alist nil))
977 (setq alist (cdr alist))))
978 result)
979 alist)))
981 (defconst prolog-syntax-propertize-function
982 (when (fboundp 'syntax-propertize-rules)
983 (syntax-propertize-rules
984 ;; GNU Prolog only accepts 0'\' rather than 0'', but the only
985 ;; possible meaning of 0'' is rather clear.
986 ("\\<0\\(''?\\)"
987 (1 (unless (save-excursion (nth 8 (syntax-ppss (match-beginning 0))))
988 (string-to-syntax "_"))))
989 ;; We could check that we're not inside an atom, but I don't think
990 ;; that 'foo 8'z could be a valid syntax anyway, so why bother?
991 ("\\<[1-9][0-9]*\\('\\)[0-9a-zA-Z]" (1 "_"))
992 ;; Supposedly, ISO-Prolog wants \NNN\ for octal and \xNNN\ for hexadecimal
993 ;; escape sequences in atoms, so be careful not to let the terminating \
994 ;; escape a subsequent quote.
995 ("\\\\[x0-7][0-9a-fA-F]*\\(\\\\\\)" (1 "_"))
998 (defun prolog-mode-variables ()
999 "Set some common variables to Prolog code specific values."
1000 (setq-local local-abbrev-table prolog-mode-abbrev-table)
1001 (setq-local paragraph-start (concat "[ \t]*$\\|" page-delimiter)) ;'%%..'
1002 (setq-local paragraph-separate paragraph-start)
1003 (setq-local paragraph-ignore-fill-prefix t)
1004 (setq-local normal-auto-fill-function 'prolog-do-auto-fill)
1005 (setq-local comment-start "%")
1006 (setq-local comment-end "")
1007 (setq-local comment-add 1)
1008 (setq-local comment-start-skip "\\(?:/\\*+ *\\|%%+ *\\)")
1009 (setq-local parens-require-spaces nil)
1010 ;; Initialize Prolog system specific variables
1011 (dolist (var '(prolog-keywords prolog-types prolog-mode-specificators
1012 prolog-determinism-specificators prolog-directives
1013 prolog-eof-string
1014 ;; prolog-continued-prompt-regexp
1015 prolog-help-function))
1016 (set (intern (concat (symbol-name var) "-i"))
1017 (prolog-find-value-by-system (symbol-value var))))
1018 (when (null (prolog-program-name))
1019 (setq-local compile-command (prolog-compile-string)))
1020 (setq-local font-lock-defaults
1021 '(prolog-font-lock-keywords nil nil ((?_ . "w"))))
1022 (setq-local syntax-propertize-function prolog-syntax-propertize-function)
1024 (smie-setup prolog-smie-grammar #'prolog-smie-rules
1025 :forward-token #'prolog-smie-forward-token
1026 :backward-token #'prolog-smie-backward-token))
1028 (defun prolog-mode-keybindings-common (map)
1029 "Define keybindings common to both Prolog modes in MAP."
1030 (define-key map "\C-c?" 'prolog-help-on-predicate)
1031 (define-key map "\C-c/" 'prolog-help-apropos)
1032 (define-key map "\C-c\C-d" 'prolog-debug-on)
1033 (define-key map "\C-c\C-t" 'prolog-trace-on)
1034 (define-key map "\C-c\C-z" 'prolog-zip-on)
1035 (define-key map "\C-c\r" 'run-prolog))
1037 (defun prolog-mode-keybindings-edit (map)
1038 "Define keybindings for Prolog mode in MAP."
1039 (define-key map "\M-a" 'prolog-beginning-of-clause)
1040 (define-key map "\M-e" 'prolog-end-of-clause)
1041 (define-key map "\M-q" 'prolog-fill-paragraph)
1042 (define-key map "\C-c\C-a" 'align)
1043 (define-key map "\C-\M-a" 'prolog-beginning-of-predicate)
1044 (define-key map "\C-\M-e" 'prolog-end-of-predicate)
1045 (define-key map "\M-\C-c" 'prolog-mark-clause)
1046 (define-key map "\M-\C-h" 'prolog-mark-predicate)
1047 (define-key map "\C-c\C-n" 'prolog-insert-predicate-template)
1048 (define-key map "\C-c\C-s" 'prolog-insert-predspec)
1049 (define-key map "\M-\r" 'prolog-insert-next-clause)
1050 (define-key map "\C-c\C-va" 'prolog-variables-to-anonymous)
1051 (define-key map "\C-c\C-v\C-s" 'prolog-view-predspec)
1053 ;; If we're running SICStus, then map C-c C-c e/d to enabling
1054 ;; and disabling of the source-level debugging facilities.
1055 ;(if (and (eq prolog-system 'sicstus)
1056 ; (prolog-atleast-version '(3 . 7)))
1057 ; (progn
1058 ; (define-key map "\C-c\C-ce" 'prolog-enable-sicstus-sd)
1059 ; (define-key map "\C-c\C-cd" 'prolog-disable-sicstus-sd)
1060 ; ))
1062 (if prolog-old-sicstus-keys-flag
1063 (progn
1064 (define-key map "\C-c\C-c" 'prolog-consult-predicate)
1065 (define-key map "\C-cc" 'prolog-consult-region)
1066 (define-key map "\C-cC" 'prolog-consult-buffer)
1067 (define-key map "\C-c\C-k" 'prolog-compile-predicate)
1068 (define-key map "\C-ck" 'prolog-compile-region)
1069 (define-key map "\C-cK" 'prolog-compile-buffer))
1070 (define-key map "\C-c\C-p" 'prolog-consult-predicate)
1071 (define-key map "\C-c\C-r" 'prolog-consult-region)
1072 (define-key map "\C-c\C-b" 'prolog-consult-buffer)
1073 (define-key map "\C-c\C-f" 'prolog-consult-file)
1074 (define-key map "\C-c\C-cp" 'prolog-compile-predicate)
1075 (define-key map "\C-c\C-cr" 'prolog-compile-region)
1076 (define-key map "\C-c\C-cb" 'prolog-compile-buffer)
1077 (define-key map "\C-c\C-cf" 'prolog-compile-file))
1079 ;; Inherited from the old prolog.el.
1080 (define-key map "\e\C-x" 'prolog-consult-region)
1081 (define-key map "\C-c\C-l" 'prolog-consult-file)
1082 (define-key map "\C-c\C-z" 'run-prolog))
1084 (defun prolog-mode-keybindings-inferior (_map)
1085 "Define keybindings for inferior Prolog mode in MAP."
1086 ;; No inferior mode specific keybindings now.
1089 (defvar prolog-mode-map
1090 (let ((map (make-sparse-keymap)))
1091 (prolog-mode-keybindings-common map)
1092 (prolog-mode-keybindings-edit map)
1093 map))
1096 (defvar prolog-mode-hook nil
1097 "List of functions to call after the prolog mode has initialized.")
1099 ;;;###autoload
1100 (define-derived-mode prolog-mode prog-mode "Prolog"
1101 "Major mode for editing Prolog code.
1103 Blank lines and `%%...' separate paragraphs. `%'s starts a comment
1104 line and comments can also be enclosed in /* ... */.
1106 If an optional argument SYSTEM is non-nil, set up mode for the given system.
1108 To find out what version of Prolog mode you are running, enter
1109 `\\[prolog-mode-version]'.
1111 Commands:
1112 \\{prolog-mode-map}"
1113 (setq mode-name (concat "Prolog"
1114 (cond
1115 ((eq prolog-system 'eclipse) "[ECLiPSe]")
1116 ((eq prolog-system 'sicstus) "[SICStus]")
1117 ((eq prolog-system 'swi) "[SWI]")
1118 ((eq prolog-system 'gnu) "[GNU]")
1119 (t ""))))
1120 (prolog-mode-variables)
1121 (dolist (ar prolog-align-rules) (add-to-list 'align-rules-list ar))
1122 (add-hook 'post-self-insert-hook #'prolog-post-self-insert nil t)
1123 ;; `imenu' entry moved to the appropriate hook for consistency.
1125 ;; Load SICStus debugger if suitable
1126 (if (and (eq prolog-system 'sicstus)
1127 (prolog-atleast-version '(3 . 7))
1128 prolog-use-sicstus-sd)
1129 (prolog-enable-sicstus-sd))
1131 (prolog-menu))
1133 (defvar mercury-mode-map
1134 (let ((map (make-sparse-keymap)))
1135 (set-keymap-parent map prolog-mode-map)
1136 map))
1138 ;;;###autoload
1139 (define-derived-mode mercury-mode prolog-mode "Prolog[Mercury]"
1140 "Major mode for editing Mercury programs.
1141 Actually this is just customized `prolog-mode'."
1142 (setq-local prolog-system 'mercury))
1145 ;;-------------------------------------------------------------------
1146 ;; Inferior prolog mode
1147 ;;-------------------------------------------------------------------
1149 (defvar prolog-inferior-mode-map
1150 (let ((map (make-sparse-keymap)))
1151 (prolog-mode-keybindings-common map)
1152 (prolog-mode-keybindings-inferior map)
1153 (define-key map [remap self-insert-command]
1154 'prolog-inferior-self-insert-command)
1155 map))
1157 (defvar prolog-inferior-mode-hook nil
1158 "List of functions to call after the inferior prolog mode has initialized.")
1160 (defvar prolog-inferior-error-regexp-alist
1161 '(;; GNU Prolog used to not follow the GNU standard format.
1162 ;; ("^\\(.*?\\):\\([0-9]+\\) error: .*(char:\\([0-9]+\\)" 1 2 3)
1163 ;; SWI-Prolog.
1164 ("^\\(?:\\?- *\\)?\\(\\(?:ERROR\\|\\(W\\)arning\\): *\\(.*?\\):\\([1-9][0-9]*\\):\\(?:\\([0-9]*\\):\\)?\\)\\(?:$\\| \\)"
1165 3 4 5 (2 . nil) 1)
1166 ;; GNU-Prolog now uses the GNU standard format.
1167 gnu))
1169 (defun prolog-inferior-self-insert-command ()
1170 "Insert the char in the buffer or pass it directly to the process."
1171 (interactive)
1172 (let* ((proc (get-buffer-process (current-buffer)))
1173 (pmark (and proc (marker-position (process-mark proc)))))
1174 ;; FIXME: the same treatment would be needed for SWI-Prolog, but I can't
1175 ;; seem to find any way for Emacs to figure out when to use it because
1176 ;; SWI doesn't include a " ? " or some such recognizable marker.
1177 (if (and (eq prolog-system 'gnu)
1178 pmark
1179 (null current-prefix-arg)
1180 (eobp)
1181 (eq (point) pmark)
1182 (save-excursion
1183 (goto-char (- pmark 3))
1184 ;; FIXME: check this comes from the process's output, maybe?
1185 (looking-at " \\? ")))
1186 ;; This is GNU prolog waiting to know whether you want more answers
1187 ;; or not (or abort, etc...). The answer is a single char, not
1188 ;; a line, so pass this char directly rather than wait for RET to
1189 ;; send a whole line.
1190 (comint-send-string proc (string last-command-event))
1191 (call-interactively 'self-insert-command))))
1193 (declare-function 'compilation-shell-minor-mode "compile" (&optional arg))
1194 (defvar compilation-error-regexp-alist)
1196 (define-derived-mode prolog-inferior-mode comint-mode "Inferior Prolog"
1197 "Major mode for interacting with an inferior Prolog process.
1199 The following commands are available:
1200 \\{prolog-inferior-mode-map}
1202 Entry to this mode calls the value of `prolog-mode-hook' with no arguments,
1203 if that value is non-nil. Likewise with the value of `comint-mode-hook'.
1204 `prolog-mode-hook' is called after `comint-mode-hook'.
1206 You can send text to the inferior Prolog from other buffers
1207 using the commands `send-region', `send-string' and \\[prolog-consult-region].
1209 Commands:
1210 Tab indents for Prolog; with argument, shifts rest
1211 of expression rigidly with the current line.
1212 Paragraphs are separated only by blank lines and '%%'. '%'s start comments.
1214 Return at end of buffer sends line as input.
1215 Return not at end copies rest of line to end and sends it.
1216 \\[comint-delchar-or-maybe-eof] sends end-of-file as input.
1217 \\[comint-kill-input] and \\[backward-kill-word] are kill commands,
1218 imitating normal Unix input editing.
1219 \\[comint-interrupt-subjob] interrupts the shell or its current subjob if any.
1220 \\[comint-stop-subjob] stops, likewise.
1221 \\[comint-quit-subjob] sends quit signal, likewise.
1223 To find out what version of Prolog mode you are running, enter
1224 `\\[prolog-mode-version]'."
1225 (require 'compile)
1226 (setq comint-input-filter 'prolog-input-filter)
1227 (setq mode-line-process '(": %s"))
1228 (prolog-mode-variables)
1229 (setq comint-prompt-regexp (prolog-prompt-regexp))
1230 (setq-local shell-dirstack-query "pwd.")
1231 (setq-local compilation-error-regexp-alist
1232 prolog-inferior-error-regexp-alist)
1233 (compilation-shell-minor-mode)
1234 (prolog-inferior-menu))
1236 (defun prolog-input-filter (str)
1237 (cond ((string-match "\\`\\s *\\'" str) nil) ;whitespace
1238 ((not (derived-mode-p 'prolog-inferior-mode)) t)
1239 ((= (length str) 1) nil) ;one character
1240 ((string-match "\\`[rf] *[0-9]*\\'" str) nil) ;r(edo) or f(ail)
1241 (t t)))
1243 ;; This statement was missing in Emacs 24.1, 24.2, 24.3.
1244 (define-obsolete-function-alias 'switch-to-prolog 'run-prolog "24.1")
1245 ;;;###autoload
1246 (defun run-prolog (arg)
1247 "Run an inferior Prolog process, input and output via buffer *prolog*.
1248 With prefix argument ARG, restart the Prolog process if running before."
1249 (interactive "P")
1250 ;; FIXME: It should be possible to interactively specify the command to use
1251 ;; to run prolog.
1252 (if (and arg (get-process "prolog"))
1253 (progn
1254 (process-send-string "prolog" "halt.\n")
1255 (while (get-process "prolog") (sit-for 0.1))))
1256 (let ((buff (buffer-name)))
1257 (if (not (string= buff "*prolog*"))
1258 (prolog-goto-prolog-process-buffer))
1259 ;; Load SICStus debugger if suitable
1260 (if (and (eq prolog-system 'sicstus)
1261 (prolog-atleast-version '(3 . 7))
1262 prolog-use-sicstus-sd)
1263 (prolog-enable-sicstus-sd))
1264 (prolog-mode-variables)
1265 (prolog-ensure-process)
1268 (defun prolog-inferior-guess-flavor (&optional ignored)
1269 (setq-local prolog-system
1270 (when (or (numberp prolog-system) (markerp prolog-system))
1271 (save-excursion
1272 (goto-char (1+ prolog-system))
1273 (cond
1274 ((looking-at "GNU Prolog") 'gnu)
1275 ((looking-at "Welcome to SWI-Prolog\\|%.*\\<swi_") 'swi)
1276 ((looking-at ".*\n") nil) ;There's at least one line.
1277 (t prolog-system)))))
1278 (when (symbolp prolog-system)
1279 (remove-hook 'comint-output-filter-functions
1280 'prolog-inferior-guess-flavor t)
1281 (when prolog-system
1282 (setq comint-prompt-regexp (prolog-prompt-regexp))
1283 (if (eq prolog-system 'gnu)
1284 (setq-local comint-process-echoes t)))))
1286 (defun prolog-ensure-process (&optional wait)
1287 "If Prolog process is not running, run it.
1288 If the optional argument WAIT is non-nil, wait for Prolog prompt specified by
1289 the variable `prolog-prompt-regexp'."
1290 (if (null (prolog-program-name))
1291 (error "This Prolog system has defined no interpreter."))
1292 (if (comint-check-proc "*prolog*")
1294 (with-current-buffer (get-buffer-create "*prolog*")
1295 (prolog-inferior-mode)
1296 (apply 'make-comint-in-buffer "prolog" (current-buffer)
1297 (prolog-program-name) nil (prolog-program-switches))
1298 (unless prolog-system
1299 ;; Setup auto-detection.
1300 (setq-local
1301 prolog-system
1302 ;; Force re-detection.
1303 (let* ((proc (get-buffer-process (current-buffer)))
1304 (pmark (and proc (marker-position (process-mark proc)))))
1305 (cond
1306 ((null pmark) (1- (point-min)))
1307 ;; The use of insert-before-markers in comint.el together with
1308 ;; the potential use of comint-truncate-buffer in the output
1309 ;; filter, means that it's difficult to reliably keep track of
1310 ;; the buffer position where the process's output started.
1311 ;; If possible we use a marker at "start - 1", so that
1312 ;; insert-before-marker at `start' won't shift it. And if not,
1313 ;; we fall back on using a plain integer.
1314 ((> pmark (point-min)) (copy-marker (1- pmark)))
1315 (t (1- pmark)))))
1316 (add-hook 'comint-output-filter-functions
1317 'prolog-inferior-guess-flavor nil t))
1318 (if wait
1319 (progn
1320 (goto-char (point-max))
1321 (while
1322 (save-excursion
1323 (not
1324 (re-search-backward
1325 (concat "\\(" (prolog-prompt-regexp) "\\)" "\\=")
1326 nil t)))
1327 (sit-for 0.1)))))))
1329 (defun prolog-inferior-buffer (&optional dont-run)
1330 (or (get-buffer "*prolog*")
1331 (unless dont-run
1332 (prolog-ensure-process)
1333 (get-buffer "*prolog*"))))
1335 (defun prolog-process-insert-string (process string)
1336 "Insert STRING into inferior Prolog buffer running PROCESS."
1337 ;; Copied from elisp manual, greek to me
1338 (with-current-buffer (process-buffer process)
1339 ;; FIXME: Use window-point-insertion-type instead.
1340 (let ((moving (= (point) (process-mark process))))
1341 (save-excursion
1342 ;; Insert the text, moving the process-marker.
1343 (goto-char (process-mark process))
1344 (insert string)
1345 (set-marker (process-mark process) (point)))
1346 (if moving (goto-char (process-mark process))))))
1348 ;;------------------------------------------------------------
1349 ;; Old consulting and compiling functions
1350 ;;------------------------------------------------------------
1352 (declare-function compilation-forget-errors "compile" ())
1353 (declare-function compilation-fake-loc "compile"
1354 (marker file &optional line col))
1356 (defun prolog-old-process-region (compilep start end)
1357 "Process the region limited by START and END positions.
1358 If COMPILEP is non-nil then use compilation, otherwise consulting."
1359 (prolog-ensure-process)
1360 ;(let ((tmpfile prolog-temp-filename)
1361 (let ((tmpfile (prolog-temporary-file))
1362 ;(process (get-process "prolog"))
1363 (first-line (1+ (count-lines
1364 (point-min)
1365 (save-excursion
1366 (goto-char start)
1367 (point))))))
1368 (write-region start end tmpfile)
1369 (setq start (copy-marker start))
1370 (with-current-buffer (prolog-inferior-buffer)
1371 (compilation-forget-errors)
1372 (compilation-fake-loc start tmpfile))
1373 (process-send-string
1374 "prolog" (prolog-build-prolog-command
1375 compilep tmpfile (prolog-bsts buffer-file-name)
1376 first-line))
1377 (prolog-goto-prolog-process-buffer)))
1379 (defun prolog-old-process-predicate (compilep)
1380 "Process the predicate around point.
1381 If COMPILEP is non-nil then use compilation, otherwise consulting."
1382 (prolog-old-process-region
1383 compilep (prolog-pred-start) (prolog-pred-end)))
1385 (defun prolog-old-process-buffer (compilep)
1386 "Process the entire buffer.
1387 If COMPILEP is non-nil then use compilation, otherwise consulting."
1388 (prolog-old-process-region compilep (point-min) (point-max)))
1390 (defun prolog-old-process-file (compilep)
1391 "Process the file of the current buffer.
1392 If COMPILEP is non-nil then use compilation, otherwise consulting."
1393 (save-some-buffers)
1394 (prolog-ensure-process)
1395 (with-current-buffer (prolog-inferior-buffer)
1396 (compilation-forget-errors))
1397 (process-send-string
1398 "prolog" (prolog-build-prolog-command
1399 compilep buffer-file-name
1400 (prolog-bsts buffer-file-name)))
1401 (prolog-goto-prolog-process-buffer))
1404 ;;------------------------------------------------------------
1405 ;; Consulting and compiling
1406 ;;------------------------------------------------------------
1408 ;; Interactive interface functions, used by both the standard
1409 ;; and the experimental consultation and compilation functions
1410 (defun prolog-consult-file ()
1411 "Consult file of current buffer."
1412 (interactive)
1413 (if prolog-use-standard-consult-compile-method-flag
1414 (prolog-old-process-file nil)
1415 (prolog-consult-compile-file nil)))
1417 (defun prolog-consult-buffer ()
1418 "Consult buffer."
1419 (interactive)
1420 (if prolog-use-standard-consult-compile-method-flag
1421 (prolog-old-process-buffer nil)
1422 (prolog-consult-compile-buffer nil)))
1424 (defun prolog-consult-region (beg end)
1425 "Consult region between BEG and END."
1426 (interactive "r")
1427 (if prolog-use-standard-consult-compile-method-flag
1428 (prolog-old-process-region nil beg end)
1429 (prolog-consult-compile-region nil beg end)))
1431 (defun prolog-consult-predicate ()
1432 "Consult the predicate around current point."
1433 (interactive)
1434 (if prolog-use-standard-consult-compile-method-flag
1435 (prolog-old-process-predicate nil)
1436 (prolog-consult-compile-predicate nil)))
1438 (defun prolog-compile-file ()
1439 "Compile file of current buffer."
1440 (interactive)
1441 (if prolog-use-standard-consult-compile-method-flag
1442 (prolog-old-process-file t)
1443 (prolog-consult-compile-file t)))
1445 (defun prolog-compile-buffer ()
1446 "Compile buffer."
1447 (interactive)
1448 (if prolog-use-standard-consult-compile-method-flag
1449 (prolog-old-process-buffer t)
1450 (prolog-consult-compile-buffer t)))
1452 (defun prolog-compile-region (beg end)
1453 "Compile region between BEG and END."
1454 (interactive "r")
1455 (if prolog-use-standard-consult-compile-method-flag
1456 (prolog-old-process-region t beg end)
1457 (prolog-consult-compile-region t beg end)))
1459 (defun prolog-compile-predicate ()
1460 "Compile the predicate around current point."
1461 (interactive)
1462 (if prolog-use-standard-consult-compile-method-flag
1463 (prolog-old-process-predicate t)
1464 (prolog-consult-compile-predicate t)))
1466 (defun prolog-buffer-module ()
1467 "Select Prolog module name appropriate for current buffer.
1468 Bases decision on buffer contents (-*- line)."
1469 ;; Look for -*- ... module: MODULENAME; ... -*-
1470 (let (beg end)
1471 (save-excursion
1472 (goto-char (point-min))
1473 (skip-chars-forward " \t")
1474 (and (search-forward "-*-" (line-end-position) t)
1475 (progn
1476 (skip-chars-forward " \t")
1477 (setq beg (point))
1478 (search-forward "-*-" (line-end-position) t))
1479 (progn
1480 (forward-char -3)
1481 (skip-chars-backward " \t")
1482 (setq end (point))
1483 (goto-char beg)
1484 (and (let ((case-fold-search t))
1485 (search-forward "module:" end t))
1486 (progn
1487 (skip-chars-forward " \t")
1488 (setq beg (point))
1489 (if (search-forward ";" end t)
1490 (forward-char -1)
1491 (goto-char end))
1492 (skip-chars-backward " \t")
1493 (buffer-substring beg (point)))))))))
1495 (defun prolog-build-prolog-command (compilep file buffername
1496 &optional first-line)
1497 "Make Prolog command for FILE compilation/consulting.
1498 If COMPILEP is non-nil, consider compilation, otherwise consulting."
1499 (let* ((compile-string
1500 ;; FIXME: If the process is not running yet, the auto-detection of
1501 ;; prolog-system won't help here, so we should make sure
1502 ;; we first run Prolog and then build the command.
1503 (if compilep (prolog-compile-string) (prolog-consult-string)))
1504 (module (prolog-buffer-module))
1505 (file-name (concat "'" (prolog-bsts file) "'"))
1506 (module-name (if module (concat "'" module "'")))
1507 (module-file (if module
1508 (concat module-name ":" file-name)
1509 file-name))
1510 strbeg strend
1511 (lineoffset (if first-line
1512 (- first-line 1)
1513 0)))
1515 ;; Assure that there is a buffer name
1516 (if (not buffername)
1517 (error "The buffer is not saved"))
1519 (if (not (string-match "\\`'.*'\\'" buffername)) ; Add quotes
1520 (setq buffername (concat "'" buffername "'")))
1521 (while (string-match "%m" compile-string)
1522 (setq strbeg (substring compile-string 0 (match-beginning 0)))
1523 (setq strend (substring compile-string (match-end 0)))
1524 (setq compile-string (concat strbeg module-file strend)))
1525 ;; FIXME: The code below will %-expand any %[fbl] that appears in
1526 ;; module-file.
1527 (while (string-match "%f" compile-string)
1528 (setq strbeg (substring compile-string 0 (match-beginning 0)))
1529 (setq strend (substring compile-string (match-end 0)))
1530 (setq compile-string (concat strbeg file-name strend)))
1531 (while (string-match "%b" compile-string)
1532 (setq strbeg (substring compile-string 0 (match-beginning 0)))
1533 (setq strend (substring compile-string (match-end 0)))
1534 (setq compile-string (concat strbeg buffername strend)))
1535 (while (string-match "%l" compile-string)
1536 (setq strbeg (substring compile-string 0 (match-beginning 0)))
1537 (setq strend (substring compile-string (match-end 0)))
1538 (setq compile-string (concat strbeg (format "%d" lineoffset) strend)))
1539 (concat compile-string "\n")))
1541 ;; The rest of this page is experimental code!
1543 ;; Global variables for process filter function
1544 (defvar prolog-process-flag nil
1545 "Non-nil means that a prolog task (i.e. a consultation or compilation job)
1546 is running.")
1547 (defvar prolog-consult-compile-output ""
1548 "Hold the unprocessed output from the current prolog task.")
1549 (defvar prolog-consult-compile-first-line 1
1550 "The number of the first line of the file to consult/compile.
1551 Used for temporary files.")
1552 (defvar prolog-consult-compile-file nil
1553 "The file to compile/consult (can be a temporary file).")
1554 (defvar prolog-consult-compile-real-file nil
1555 "The file name of the buffer to compile/consult.")
1557 (defvar compilation-parse-errors-function)
1559 (defun prolog-consult-compile (compilep file &optional first-line)
1560 "Consult/compile FILE.
1561 If COMPILEP is non-nil, perform compilation, otherwise perform CONSULTING.
1562 COMMAND is a string described by the variables `prolog-consult-string'
1563 and `prolog-compile-string'.
1564 Optional argument FIRST-LINE is the number of the first line in the compiled
1565 region.
1567 This function must be called from the source code buffer."
1568 (if prolog-process-flag
1569 (error "Another Prolog task is running."))
1570 (prolog-ensure-process t)
1571 (let* ((buffer (get-buffer-create prolog-compilation-buffer))
1572 (real-file buffer-file-name)
1573 (command-string (prolog-build-prolog-command compilep file
1574 real-file first-line))
1575 (process (get-process "prolog")))
1576 (with-current-buffer buffer
1577 (delete-region (point-min) (point-max))
1578 ;; FIXME: Wasn't this supposed to use prolog-inferior-mode?
1579 (compilation-mode)
1580 ;; FIXME: This doesn't seem to cooperate well with new(ish) compile.el.
1581 ;; Setting up font-locking for this buffer
1582 (setq-local font-lock-defaults
1583 '(prolog-font-lock-keywords nil nil ((?_ . "w"))))
1584 (if (eq prolog-system 'sicstus)
1585 ;; FIXME: This looks really problematic: not only is this using
1586 ;; the old compilation-parse-errors-function, but
1587 ;; prolog-parse-sicstus-compilation-errors only accepts one argument
1588 ;; whereas compile.el calls it with 2 (and did so at least since
1589 ;; Emacs-20).
1590 (setq-local compilation-parse-errors-function
1591 'prolog-parse-sicstus-compilation-errors))
1592 (setq buffer-read-only nil)
1593 (insert command-string "\n"))
1594 (display-buffer buffer)
1595 (setq prolog-process-flag t
1596 prolog-consult-compile-output ""
1597 prolog-consult-compile-first-line (if first-line (1- first-line) 0)
1598 prolog-consult-compile-file file
1599 prolog-consult-compile-real-file (if (string=
1600 file buffer-file-name)
1602 real-file))
1603 (with-current-buffer buffer
1604 (goto-char (point-max))
1605 (add-function :override (process-filter process)
1606 #'prolog-consult-compile-filter)
1607 (process-send-string "prolog" command-string)
1608 ;; (prolog-build-prolog-command compilep file real-file first-line))
1609 (while (and prolog-process-flag
1610 (accept-process-output process 10)) ; 10 secs is ok?
1611 (sit-for 0.1)
1612 (unless (get-process "prolog")
1613 (setq prolog-process-flag nil)))
1614 (insert (if compilep
1615 "\nCompilation finished.\n"
1616 "\nConsulted.\n"))
1617 (remove-function (process-filter process)
1618 #'prolog-consult-compile-filter))))
1620 (defvar compilation-error-list)
1622 (defun prolog-parse-sicstus-compilation-errors (limit)
1623 "Parse the prolog compilation buffer for errors.
1624 Argument LIMIT is a buffer position limiting searching.
1625 For use with the `compilation-parse-errors-function' variable."
1626 (setq compilation-error-list nil)
1627 (message "Parsing SICStus error messages...")
1628 (let (filepath dir file errorline)
1629 (while
1630 (re-search-backward
1631 "{\\([a-zA-Z ]* ERROR\\|Warning\\):.* in line[s ]*\\([0-9]+\\)"
1632 limit t)
1633 (setq errorline (string-to-number (match-string 2)))
1634 (save-excursion
1635 (re-search-backward
1636 "{\\(consulting\\|compiling\\|processing\\) \\(.*\\)\\.\\.\\.}"
1637 limit t)
1638 (setq filepath (match-string 2)))
1640 ;; ###### Does this work with SICStus under Windows
1641 ;; (i.e. backslashes and stuff?)
1642 (if (string-match "\\(.*/\\)\\([^/]*\\)$" filepath)
1643 (progn
1644 (setq dir (match-string 1 filepath))
1645 (setq file (match-string 2 filepath))))
1647 (setq compilation-error-list
1648 (cons
1649 (cons (save-excursion
1650 (beginning-of-line)
1651 (point-marker))
1652 (list (list file dir) errorline))
1653 compilation-error-list)
1657 (defun prolog-consult-compile-filter (process output)
1658 "Filter function for Prolog compilation PROCESS.
1659 Argument OUTPUT is a name of the output file."
1660 ;;(message "start")
1661 (setq prolog-consult-compile-output
1662 (concat prolog-consult-compile-output output))
1663 ;;(message "pccf1: %s" prolog-consult-compile-output)
1664 ;; Iterate through the lines of prolog-consult-compile-output
1665 (let (outputtype)
1666 (while (and prolog-process-flag
1668 ;; Trace question
1669 (progn
1670 (setq outputtype 'trace)
1671 (and (eq prolog-system 'sicstus)
1672 (string-match
1673 "^[ \t]*[0-9]+[ \t]*[0-9]+[ \t]*Call:.*? "
1674 prolog-consult-compile-output)))
1676 ;; Match anything
1677 (progn
1678 (setq outputtype 'normal)
1679 (string-match "^.*\n" prolog-consult-compile-output))
1681 ;;(message "outputtype: %s" outputtype)
1683 (setq output (match-string 0 prolog-consult-compile-output))
1684 ;; remove the text in output from prolog-consult-compile-output
1685 (setq prolog-consult-compile-output
1686 (substring prolog-consult-compile-output (length output)))
1687 ;;(message "pccf2: %s" prolog-consult-compile-output)
1689 ;; If temporary files were used, then we change the error
1690 ;; messages to point to the original source file.
1691 ;; FIXME: Use compilation-fake-loc instead.
1692 (cond
1694 ;; If the prolog process was in trace mode then it requires
1695 ;; user input
1696 ((and (eq prolog-system 'sicstus)
1697 (eq outputtype 'trace))
1698 (let ((input (concat (read-string output) "\n")))
1699 (process-send-string process input)
1700 (setq output (concat output input))))
1702 ((eq prolog-system 'sicstus)
1703 (if (and prolog-consult-compile-real-file
1704 (string-match
1705 "\\({.*:.* in line[s ]*\\)\\([0-9]+\\)-\\([0-9]+\\)" output))
1706 (setq output (replace-match
1707 ;; Adds a {processing ...} line so that
1708 ;; `prolog-parse-sicstus-compilation-errors'
1709 ;; finds the real file instead of the temporary one.
1710 ;; Also fixes the line numbers.
1711 (format "Added by Emacs: {processing %s...}\n%s%d-%d"
1712 prolog-consult-compile-real-file
1713 (match-string 1 output)
1714 (+ prolog-consult-compile-first-line
1715 (string-to-number
1716 (match-string 2 output)))
1717 (+ prolog-consult-compile-first-line
1718 (string-to-number
1719 (match-string 3 output))))
1720 t t output)))
1723 ((eq prolog-system 'swi)
1724 (if (and prolog-consult-compile-real-file
1725 (string-match (format
1726 "%s\\([ \t]*:[ \t]*\\)\\([0-9]+\\)"
1727 prolog-consult-compile-file)
1728 output))
1729 (setq output (replace-match
1730 ;; Real filename + text + fixed linenum
1731 (format "%s%s%d"
1732 prolog-consult-compile-real-file
1733 (match-string 1 output)
1734 (+ prolog-consult-compile-first-line
1735 (string-to-number
1736 (match-string 2 output))))
1737 t t output)))
1740 (t ())
1742 ;; Write the output in the *prolog-compilation* buffer
1743 (insert output)))
1745 ;; If the prompt is visible, then the task is finished
1746 (if (string-match (prolog-prompt-regexp) prolog-consult-compile-output)
1747 (setq prolog-process-flag nil)))
1749 (defun prolog-consult-compile-file (compilep)
1750 "Consult/compile file of current buffer.
1751 If COMPILEP is non-nil, compile, otherwise consult."
1752 (let ((file buffer-file-name))
1753 (if file
1754 (progn
1755 (save-some-buffers)
1756 (prolog-consult-compile compilep file))
1757 (prolog-consult-compile-region compilep (point-min) (point-max)))))
1759 (defun prolog-consult-compile-buffer (compilep)
1760 "Consult/compile current buffer.
1761 If COMPILEP is non-nil, compile, otherwise consult."
1762 (prolog-consult-compile-region compilep (point-min) (point-max)))
1764 (defun prolog-consult-compile-region (compilep beg end)
1765 "Consult/compile region between BEG and END.
1766 If COMPILEP is non-nil, compile, otherwise consult."
1767 ;(let ((file prolog-temp-filename)
1768 (let ((file (prolog-bsts (prolog-temporary-file)))
1769 (lines (count-lines 1 beg)))
1770 (write-region beg end file nil 'no-message)
1771 (write-region "\n" nil file t 'no-message)
1772 (prolog-consult-compile compilep file
1773 (if (bolp) (1+ lines) lines))
1774 (delete-file file)))
1776 (defun prolog-consult-compile-predicate (compilep)
1777 "Consult/compile the predicate around current point.
1778 If COMPILEP is non-nil, compile, otherwise consult."
1779 (prolog-consult-compile-region
1780 compilep (prolog-pred-start) (prolog-pred-end)))
1783 ;;-------------------------------------------------------------------
1784 ;; Font-lock stuff
1785 ;;-------------------------------------------------------------------
1787 ;; Auxiliary functions
1789 (defun prolog-font-lock-object-matcher (bound)
1790 "Find SICStus objects method name for font lock.
1791 Argument BOUND is a buffer position limiting searching."
1792 (let (point
1793 (case-fold-search nil))
1794 (while (and (not point)
1795 (re-search-forward "\\(::[ \t\n]*{\\|&\\)[ \t]*"
1796 bound t))
1797 (while (or (re-search-forward "\\=\n[ \t]*" bound t)
1798 (re-search-forward "\\=%.*" bound t)
1799 (and (re-search-forward "\\=/\\*" bound t)
1800 (re-search-forward "\\*/[ \t]*" bound t))))
1801 (setq point (re-search-forward
1802 (format "\\=\\(%s\\)" prolog-atom-regexp)
1803 bound t)))
1804 point))
1806 (defsubst prolog-face-name-p (facename)
1807 ;; Return t if FACENAME is the name of a face. This method is
1808 ;; necessary since facep in XEmacs only returns t for the actual
1809 ;; face objects (while it's only their names that are used just
1810 ;; about anywhere else) without providing a predicate that tests
1811 ;; face names. This function (including the above commentary) is
1812 ;; borrowed from cc-mode.
1813 (memq facename (face-list)))
1815 ;; Set everything up
1816 (defun prolog-font-lock-keywords ()
1817 "Set up font lock keywords for the current Prolog system."
1818 ;;(when window-system
1819 (require 'font-lock)
1821 ;; Define Prolog faces
1822 (defface prolog-redo-face
1823 '((((class grayscale)) (:italic t))
1824 (((class color)) (:foreground "darkorchid"))
1825 (t (:italic t)))
1826 "Prolog mode face for highlighting redo trace lines."
1827 :group 'prolog-faces)
1828 (defface prolog-exit-face
1829 '((((class grayscale)) (:underline t))
1830 (((class color) (background dark)) (:foreground "green"))
1831 (((class color) (background light)) (:foreground "ForestGreen"))
1832 (t (:underline t)))
1833 "Prolog mode face for highlighting exit trace lines."
1834 :group 'prolog-faces)
1835 (defface prolog-exception-face
1836 '((((class grayscale)) (:bold t :italic t :underline t))
1837 (((class color)) (:bold t :foreground "black" :background "Khaki"))
1838 (t (:bold t :italic t :underline t)))
1839 "Prolog mode face for highlighting exception trace lines."
1840 :group 'prolog-faces)
1841 (defface prolog-warning-face
1842 '((((class grayscale)) (:underline t))
1843 (((class color) (background dark)) (:foreground "blue"))
1844 (((class color) (background light)) (:foreground "MidnightBlue"))
1845 (t (:underline t)))
1846 "Face name to use for compiler warnings."
1847 :group 'prolog-faces)
1848 (defface prolog-builtin-face
1849 '((((class color) (background light)) (:foreground "Purple"))
1850 (((class color) (background dark)) (:foreground "Cyan"))
1851 (((class grayscale) (background light))
1852 :foreground "LightGray" :bold t)
1853 (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
1854 (t (:bold t)))
1855 "Face name to use for compiler warnings."
1856 :group 'prolog-faces)
1857 (defvar prolog-warning-face
1858 (if (prolog-face-name-p 'font-lock-warning-face)
1859 'font-lock-warning-face
1860 'prolog-warning-face)
1861 "Face name to use for built in predicates.")
1862 (defvar prolog-builtin-face
1863 (if (prolog-face-name-p 'font-lock-builtin-face)
1864 'font-lock-builtin-face
1865 'prolog-builtin-face)
1866 "Face name to use for built in predicates.")
1867 (defvar prolog-redo-face 'prolog-redo-face
1868 "Face name to use for redo trace lines.")
1869 (defvar prolog-exit-face 'prolog-exit-face
1870 "Face name to use for exit trace lines.")
1871 (defvar prolog-exception-face 'prolog-exception-face
1872 "Face name to use for exception trace lines.")
1874 ;; Font Lock Patterns
1875 (let (
1876 ;; "Native" Prolog patterns
1877 (head-predicates
1878 (list (format "^\\(%s\\)\\((\\|[ \t]*:-\\)" prolog-atom-regexp)
1879 1 font-lock-function-name-face))
1880 ;(list (format "^%s" prolog-atom-regexp)
1881 ; 0 font-lock-function-name-face))
1882 (head-predicates-1
1883 (list (format "\\.[ \t]*\\(%s\\)" prolog-atom-regexp)
1884 1 font-lock-function-name-face) )
1885 (variables
1886 '("\\<\\([_A-Z][a-zA-Z0-9_]*\\)"
1887 1 font-lock-variable-name-face))
1888 (important-elements
1889 (list (if (eq prolog-system 'mercury)
1890 "[][}{;|]\\|\\\\[+=]\\|<?=>?"
1891 "[][}{!;|]\\|\\*->")
1892 0 'font-lock-keyword-face))
1893 (important-elements-1
1894 '("[^-*]\\(->\\)" 1 font-lock-keyword-face))
1895 (predspecs ; module:predicate/cardinality
1896 (list (format "\\<\\(%s:\\|\\)%s/[0-9]+"
1897 prolog-atom-regexp prolog-atom-regexp)
1898 0 font-lock-function-name-face 'prepend))
1899 (keywords ; directives (queries)
1900 (list
1901 (if (eq prolog-system 'mercury)
1902 (concat
1903 "\\<\\("
1904 (regexp-opt prolog-keywords-i)
1905 "\\|"
1906 (regexp-opt
1907 prolog-determinism-specificators-i)
1908 "\\)\\>")
1909 (concat
1910 "^[?:]- *\\("
1911 (regexp-opt prolog-keywords-i)
1912 "\\)\\>"))
1913 1 prolog-builtin-face))
1914 ;; SICStus specific patterns
1915 (sicstus-object-methods
1916 (if (eq prolog-system 'sicstus)
1917 '(prolog-font-lock-object-matcher
1918 1 font-lock-function-name-face)))
1919 ;; Mercury specific patterns
1920 (types
1921 (if (eq prolog-system 'mercury)
1922 (list
1923 (regexp-opt prolog-types-i 'words)
1924 0 'font-lock-type-face)))
1925 (modes
1926 (if (eq prolog-system 'mercury)
1927 (list
1928 (regexp-opt prolog-mode-specificators-i 'words)
1929 0 'font-lock-constant-face)))
1930 (directives
1931 (if (eq prolog-system 'mercury)
1932 (list
1933 (regexp-opt prolog-directives-i 'words)
1934 0 'prolog-warning-face)))
1935 ;; Inferior mode specific patterns
1936 (prompt
1937 ;; FIXME: Should be handled by comint already.
1938 (list (prolog-prompt-regexp) 0 'font-lock-keyword-face))
1939 (trace-exit
1940 ;; FIXME: Add to compilation-error-regexp-alist instead.
1941 (cond
1942 ((eq prolog-system 'sicstus)
1943 '("[ \t]*[0-9]+[ \t]+[0-9]+[ \t]*\\(Exit\\):"
1944 1 prolog-exit-face))
1945 ((eq prolog-system 'swi)
1946 '("[ \t]*\\(Exit\\):[ \t]*([ \t0-9]*)" 1 prolog-exit-face))
1947 (t nil)))
1948 (trace-fail
1949 ;; FIXME: Add to compilation-error-regexp-alist instead.
1950 (cond
1951 ((eq prolog-system 'sicstus)
1952 '("[ \t]*[0-9]+[ \t]+[0-9]+[ \t]*\\(Fail\\):"
1953 1 prolog-warning-face))
1954 ((eq prolog-system 'swi)
1955 '("[ \t]*\\(Fail\\):[ \t]*([ \t0-9]*)" 1 prolog-warning-face))
1956 (t nil)))
1957 (trace-redo
1958 ;; FIXME: Add to compilation-error-regexp-alist instead.
1959 (cond
1960 ((eq prolog-system 'sicstus)
1961 '("[ \t]*[0-9]+[ \t]+[0-9]+[ \t]*\\(Redo\\):"
1962 1 prolog-redo-face))
1963 ((eq prolog-system 'swi)
1964 '("[ \t]*\\(Redo\\):[ \t]*([ \t0-9]*)" 1 prolog-redo-face))
1965 (t nil)))
1966 (trace-call
1967 ;; FIXME: Add to compilation-error-regexp-alist instead.
1968 (cond
1969 ((eq prolog-system 'sicstus)
1970 '("[ \t]*[0-9]+[ \t]+[0-9]+[ \t]*\\(Call\\):"
1971 1 font-lock-function-name-face))
1972 ((eq prolog-system 'swi)
1973 '("[ \t]*\\(Call\\):[ \t]*([ \t0-9]*)"
1974 1 font-lock-function-name-face))
1975 (t nil)))
1976 (trace-exception
1977 ;; FIXME: Add to compilation-error-regexp-alist instead.
1978 (cond
1979 ((eq prolog-system 'sicstus)
1980 '("[ \t]*[0-9]+[ \t]+[0-9]+[ \t]*\\(Exception\\):"
1981 1 prolog-exception-face))
1982 ((eq prolog-system 'swi)
1983 '("[ \t]*\\(Exception\\):[ \t]*([ \t0-9]*)"
1984 1 prolog-exception-face))
1985 (t nil)))
1986 (error-message-identifier
1987 ;; FIXME: Add to compilation-error-regexp-alist instead.
1988 (cond
1989 ((eq prolog-system 'sicstus)
1990 '("{\\([A-Z]* ?ERROR:\\)" 1 prolog-exception-face prepend))
1991 ((eq prolog-system 'swi)
1992 '("^[[]\\(WARNING:\\)" 1 prolog-builtin-face prepend))
1993 (t nil)))
1994 (error-whole-messages
1995 ;; FIXME: Add to compilation-error-regexp-alist instead.
1996 (cond
1997 ((eq prolog-system 'sicstus)
1998 '("{\\([A-Z]* ?ERROR:.*\\)}[ \t]*$"
1999 1 font-lock-comment-face append))
2000 ((eq prolog-system 'swi)
2001 '("^[[]WARNING:[^]]*[]]$" 0 font-lock-comment-face append))
2002 (t nil)))
2003 (error-warning-messages
2004 ;; FIXME: Add to compilation-error-regexp-alist instead.
2005 ;; Mostly errors that SICStus asks the user about how to solve,
2006 ;; such as "NAME CLASH:" for example.
2007 (cond
2008 ((eq prolog-system 'sicstus)
2009 '("^[A-Z ]*[A-Z]+:" 0 prolog-warning-face))
2010 (t nil)))
2011 (warning-messages
2012 ;; FIXME: Add to compilation-error-regexp-alist instead.
2013 (cond
2014 ((eq prolog-system 'sicstus)
2015 '("\\({ ?\\(Warning\\|WARNING\\) ?:.*}\\)[ \t]*$"
2016 2 prolog-warning-face prepend))
2017 (t nil))))
2019 ;; Make font lock list
2020 (delq
2022 (cond
2023 ((eq major-mode 'prolog-mode)
2024 (list
2025 head-predicates
2026 head-predicates-1
2027 variables
2028 important-elements
2029 important-elements-1
2030 predspecs
2031 keywords
2032 sicstus-object-methods
2033 types
2034 modes
2035 directives))
2036 ((eq major-mode 'prolog-inferior-mode)
2037 (list
2038 prompt
2039 error-message-identifier
2040 error-whole-messages
2041 error-warning-messages
2042 warning-messages
2043 predspecs
2044 trace-exit
2045 trace-fail
2046 trace-redo
2047 trace-call
2048 trace-exception))
2049 ((eq major-mode 'compilation-mode)
2050 (list
2051 error-message-identifier
2052 error-whole-messages
2053 error-warning-messages
2054 warning-messages
2055 predspecs))))
2060 (defun prolog-find-unmatched-paren ()
2061 "Return the column of the last unmatched left parenthesis."
2062 (save-excursion
2063 (goto-char (or (car (nth 9 (syntax-ppss))) (point-min)))
2064 (current-column)))
2067 (defun prolog-paren-balance ()
2068 "Return the parenthesis balance of the current line.
2069 A return value of N means N more left parentheses than right ones."
2070 (save-excursion
2071 (car (parse-partial-sexp (line-beginning-position)
2072 (line-end-position)))))
2074 (defun prolog-electric--if-then-else ()
2075 "Insert spaces after the opening parenthesis, \"then\" (->) and \"else\" (;) branches.
2076 Spaces are inserted if all preceding objects on the line are
2077 whitespace characters, parentheses, or then/else branches."
2078 (when prolog-electric-if-then-else-flag
2079 (save-excursion
2080 (let ((regexp (concat "(\\|" prolog-left-indent-regexp))
2081 level)
2082 (beginning-of-line)
2083 (skip-chars-forward " \t")
2084 ;; Treat "( If -> " lines specially.
2085 ;;(setq incr (if (looking-at "(.*->")
2086 ;; 2
2087 ;; prolog-paren-indent))
2089 ;; work on all subsequent "->", "(", ";"
2090 (while (looking-at regexp)
2091 (goto-char (match-end 0))
2092 (setq level (+ (prolog-find-unmatched-paren) prolog-paren-indent))
2094 ;; Remove old white space
2095 (let ((start (point)))
2096 (skip-chars-forward " \t")
2097 (delete-region start (point)))
2098 (indent-to level)
2099 (skip-chars-forward " \t"))
2101 (when (save-excursion
2102 (backward-char 2)
2103 (looking-at "\\s ;\\|\\s (\\|->")) ; (looking-at "\\s \\((\\|;\\)"))
2104 (skip-chars-forward " \t"))
2107 ;;;; Comment filling
2109 (defun prolog-comment-limits ()
2110 "Return the current comment limits plus the comment type (block or line).
2111 The comment limits are the range of a block comment or the range that
2112 contains all adjacent line comments (i.e. all comments that starts in
2113 the same column with no empty lines or non-whitespace characters
2114 between them)."
2115 (let ((here (point))
2116 lit-limits-b lit-limits-e lit-type beg end
2118 (save-restriction
2119 ;; Widen to catch comment limits correctly.
2120 (widen)
2121 (setq end (line-end-position)
2122 beg (line-beginning-position))
2123 (save-excursion
2124 (beginning-of-line)
2125 (setq lit-type (if (search-forward-regexp "%" end t) 'line 'block))
2126 ; (setq lit-type 'line)
2127 ;(if (search-forward-regexp "^[ \t]*%" end t)
2128 ; (setq lit-type 'line)
2129 ; (if (not (search-forward-regexp "%" end t))
2130 ; (setq lit-type 'block)
2131 ; (if (not (= (forward-line 1) 0))
2132 ; (setq lit-type 'block)
2133 ; (setq done t
2134 ; ret (prolog-comment-limits)))
2135 ; ))
2136 (if (eq lit-type 'block)
2137 (progn
2138 (goto-char here)
2139 (when (looking-at "/\\*") (forward-char 2))
2140 (when (and (looking-at "\\*") (> (point) (point-min))
2141 (forward-char -1) (looking-at "/"))
2142 (forward-char 1))
2143 (when (save-excursion (search-backward "/*" nil t))
2144 (list (save-excursion (search-backward "/*") (point))
2145 (or (search-forward "*/" nil t) (point-max)) lit-type)))
2146 ;; line comment
2147 (setq lit-limits-b (- (point) 1)
2148 lit-limits-e end)
2149 (condition-case nil
2150 (if (progn (goto-char lit-limits-b)
2151 (looking-at "%"))
2152 (let ((col (current-column)) done)
2153 (setq beg (point)
2154 end lit-limits-e)
2155 ;; Always at the beginning of the comment
2156 ;; Go backward now
2157 (beginning-of-line)
2158 (while (and (zerop (setq done (forward-line -1)))
2159 (search-forward-regexp "^[ \t]*%"
2160 (line-end-position) t)
2161 (= (+ 1 col) (current-column)))
2162 (setq beg (- (point) 1)))
2163 (when (= done 0)
2164 (forward-line 1))
2165 ;; We may have a line with code above...
2166 (when (and (zerop (setq done (forward-line -1)))
2167 (search-forward "%" (line-end-position) t)
2168 (= (+ 1 col) (current-column)))
2169 (setq beg (- (point) 1)))
2170 (when (= done 0)
2171 (forward-line 1))
2172 ;; Go forward
2173 (goto-char lit-limits-b)
2174 (beginning-of-line)
2175 (while (and (zerop (forward-line 1))
2176 (search-forward-regexp "^[ \t]*%"
2177 (line-end-position) t)
2178 (= (+ 1 col) (current-column)))
2179 (setq end (line-end-position)))
2180 (list beg end lit-type))
2181 (list lit-limits-b lit-limits-e lit-type)
2183 (error (list lit-limits-b lit-limits-e lit-type))))
2184 ))))
2186 (defun prolog-guess-fill-prefix ()
2187 ;; fill 'txt entities?
2188 (when (save-excursion
2189 (end-of-line)
2190 (nth 4 (syntax-ppss)))
2191 (let* ((bounds (prolog-comment-limits))
2192 (cbeg (car bounds))
2193 (type (nth 2 bounds))
2194 beg end)
2195 (save-excursion
2196 (end-of-line)
2197 (setq end (point))
2198 (beginning-of-line)
2199 (setq beg (point))
2200 (if (and (eq type 'line)
2201 (> cbeg beg)
2202 (save-excursion (not (search-forward-regexp "^[ \t]*%"
2203 cbeg t))))
2204 (progn
2205 (goto-char cbeg)
2206 (search-forward-regexp "%+[ \t]*" end t)
2207 (prolog-replace-in-string (buffer-substring beg (point))
2208 "[^ \t%]" " "))
2209 ;(goto-char beg)
2210 (if (search-forward-regexp "^[ \t]*\\(%+\\|\\*+\\|/\\*+\\)[ \t]*"
2211 end t)
2212 (prolog-replace-in-string (buffer-substring beg (point)) "/" " ")
2213 (beginning-of-line)
2214 (when (search-forward-regexp "^[ \t]+" end t)
2215 (buffer-substring beg (point)))))))))
2217 (defun prolog-fill-paragraph ()
2218 "Fill paragraph comment at or after point."
2219 (interactive)
2220 (let* ((bounds (prolog-comment-limits))
2221 (type (nth 2 bounds)))
2222 (if (eq type 'line)
2223 (let ((fill-prefix (prolog-guess-fill-prefix)))
2224 (fill-paragraph nil))
2225 (save-excursion
2226 (save-restriction
2227 ;; exclude surrounding lines that delimit a multiline comment
2228 ;; and don't contain alphabetic characters, like "/*******",
2229 ;; "- - - */" etc.
2230 (save-excursion
2231 (backward-paragraph)
2232 (unless (bobp) (forward-line))
2233 (if (string-match "^/\\*[^a-zA-Z]*$" (thing-at-point 'line))
2234 (narrow-to-region (point-at-eol) (point-max))))
2235 (save-excursion
2236 (forward-paragraph)
2237 (forward-line -1)
2238 (if (string-match "^[^a-zA-Z]*\\*/$" (thing-at-point 'line))
2239 (narrow-to-region (point-min) (point-at-bol))))
2240 (let ((fill-prefix (prolog-guess-fill-prefix)))
2241 (fill-paragraph nil))))
2244 (defun prolog-do-auto-fill ()
2245 "Carry out Auto Fill for Prolog mode.
2246 In effect it sets the `fill-prefix' when inside comments and then calls
2247 `do-auto-fill'."
2248 (let ((fill-prefix (prolog-guess-fill-prefix)))
2249 (do-auto-fill)
2252 (defalias 'prolog-replace-in-string
2253 (if (fboundp 'replace-in-string)
2254 #'replace-in-string
2255 (lambda (str regexp newtext &optional literal)
2256 (replace-regexp-in-string regexp newtext str nil literal))))
2258 ;;-------------------------------------------------------------------
2259 ;; Online help
2260 ;;-------------------------------------------------------------------
2262 (defvar prolog-help-function
2263 '((mercury nil)
2264 (eclipse prolog-help-online)
2265 ;; (sicstus prolog-help-info)
2266 (sicstus prolog-find-documentation)
2267 (swi prolog-help-online)
2268 (t prolog-help-online))
2269 "Alist for the name of the function for finding help on a predicate.")
2271 (defun prolog-help-on-predicate ()
2272 "Invoke online help on the atom under cursor."
2273 (interactive)
2275 (cond
2276 ;; Redirect help for SICStus to `prolog-find-documentation'.
2277 ((eq prolog-help-function-i 'prolog-find-documentation)
2278 (prolog-find-documentation))
2280 ;; Otherwise, ask for the predicate name and then call the function
2281 ;; in prolog-help-function-i
2283 (let* ((word (prolog-atom-under-point))
2284 (predicate (read-string
2285 (format "Help on predicate%s: "
2286 (if word
2287 (concat " (default " word ")")
2288 ""))
2289 nil nil word))
2290 ;;point
2292 (if prolog-help-function-i
2293 (funcall prolog-help-function-i predicate)
2294 (error "Sorry, no help method defined for this Prolog system."))))
2298 (autoload 'Info-goto-node "info" nil t)
2299 (declare-function Info-follow-nearest-node "info" (&optional FORK))
2301 (defun prolog-help-info (predicate)
2302 (let ((buffer (current-buffer))
2303 oldp
2304 (str (concat "^\\* " (regexp-quote predicate) " */")))
2305 (pop-to-buffer nil)
2306 (Info-goto-node prolog-info-predicate-index)
2307 (if (not (re-search-forward str nil t))
2308 (error (format "Help on predicate `%s' not found." predicate)))
2310 (setq oldp (point))
2311 (if (re-search-forward str nil t)
2312 ;; Multiple matches, ask user
2313 (let ((max 2)
2315 ;; Count matches
2316 (while (re-search-forward str nil t)
2317 (setq max (1+ max)))
2319 (goto-char oldp)
2320 (re-search-backward "[^ /]" nil t)
2321 (recenter 0)
2322 (setq n (read-string ;; was read-input, which is obsolete
2323 (format "Several matches, choose (1-%d): " max) "1"))
2324 (forward-line (- (string-to-number n) 1)))
2325 ;; Single match
2326 (re-search-backward "[^ /]" nil t))
2328 ;; (Info-follow-nearest-node (point))
2329 (prolog-Info-follow-nearest-node)
2330 (re-search-forward (concat "^`" (regexp-quote predicate)) nil t)
2331 (beginning-of-line)
2332 (recenter 0)
2333 (pop-to-buffer buffer)))
2335 (defun prolog-Info-follow-nearest-node ()
2336 (if (featurep 'xemacs)
2337 (Info-follow-nearest-node (point))
2338 (Info-follow-nearest-node)))
2340 (defun prolog-help-online (predicate)
2341 (prolog-ensure-process)
2342 (process-send-string "prolog" (concat "help(" predicate ").\n"))
2343 (display-buffer "*prolog*"))
2345 (defun prolog-help-apropos (string)
2346 "Find Prolog apropos on given STRING.
2347 This function is only available when `prolog-system' is set to `swi'."
2348 (interactive "sApropos: ")
2349 (cond
2350 ((eq prolog-system 'swi)
2351 (prolog-ensure-process)
2352 (process-send-string "prolog" (concat "apropos(" string ").\n"))
2353 (display-buffer "*prolog*"))
2355 (error "Sorry, no Prolog apropos available for this Prolog system."))))
2357 (defun prolog-atom-under-point ()
2358 "Return the atom under or left to the point."
2359 (save-excursion
2360 (let ((nonatom_chars "[](){},\. \t\n")
2361 start)
2362 (skip-chars-forward (concat "^" nonatom_chars))
2363 (skip-chars-backward nonatom_chars)
2364 (skip-chars-backward (concat "^" nonatom_chars))
2365 (setq start (point))
2366 (skip-chars-forward (concat "^" nonatom_chars))
2367 (buffer-substring-no-properties start (point))
2371 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2372 ;; Help function with completion
2373 ;; Stolen from Per Mildner's SICStus debugger mode and modified
2375 (defun prolog-find-documentation ()
2376 "Go to the Info node for a predicate in the SICStus Info manual."
2377 (interactive)
2378 (let ((pred (prolog-read-predicate)))
2379 (prolog-goto-predicate-info pred)))
2381 (defvar prolog-info-alist nil
2382 "Alist with all builtin predicates.
2383 Only for internal use by `prolog-find-documentation'")
2385 ;; Very similar to prolog-help-info except that that function cannot
2386 ;; cope with arity and that it asks the user if there are several
2387 ;; functors with different arity. This function also uses
2388 ;; prolog-info-alist for finding the info node, rather than parsing
2389 ;; the predicate index.
2390 (defun prolog-goto-predicate-info (predicate)
2391 "Go to the info page for PREDICATE, which is a PredSpec."
2392 (interactive)
2393 (string-match "\\(.*\\)/\\([0-9]+\\).*$" predicate)
2394 (let ((buffer (current-buffer))
2395 (name (match-string 1 predicate))
2396 (arity (string-to-number (match-string 2 predicate)))
2397 ;oldp
2398 ;(str (regexp-quote predicate))
2400 (pop-to-buffer nil)
2402 (Info-goto-node
2403 prolog-info-predicate-index) ;; We must be in the SICStus pages
2404 (Info-goto-node (car (cdr (assoc predicate prolog-info-alist))))
2406 (prolog-find-term (regexp-quote name) arity "^`")
2408 (recenter 0)
2409 (pop-to-buffer buffer))
2412 (defun prolog-read-predicate ()
2413 "Read a PredSpec from the user.
2414 Returned value is a string \"FUNCTOR/ARITY\".
2415 Interaction supports completion."
2416 (let ((default (prolog-atom-under-point)))
2417 ;; If the predicate index is not yet built, do it now
2418 (if (not prolog-info-alist)
2419 (prolog-build-info-alist))
2420 ;; Test if the default string could be the base for completion.
2421 ;; Discard it if not.
2422 (if (eq (try-completion default prolog-info-alist) nil)
2423 (setq default nil))
2424 ;; Read the PredSpec from the user
2425 (completing-read
2426 (if (zerop (length default))
2427 "Help on predicate: "
2428 (concat "Help on predicate (default " default "): "))
2429 prolog-info-alist nil t nil nil default)))
2431 (defun prolog-build-info-alist (&optional verbose)
2432 "Build an alist of all builtins and library predicates.
2433 Each element is of the form (\"NAME/ARITY\" . (INFO-NODE1 INFO-NODE2 ...)).
2434 Typically there is just one Info node associated with each name
2435 If an optional argument VERBOSE is non-nil, print messages at the beginning
2436 and end of list building."
2437 (if verbose
2438 (message "Building info alist..."))
2439 (setq prolog-info-alist
2440 (let ((l ())
2441 (last-entry (cons "" ())))
2442 (save-excursion
2443 (save-window-excursion
2444 ;; select any window but the minibuffer (as we cannot switch
2445 ;; buffers in minibuffer window.
2446 ;; I am not sure this is the right/best way
2447 (if (active-minibuffer-window) ; nil if none active
2448 (select-window (next-window)))
2449 ;; Do this after going away from minibuffer window
2450 (save-window-excursion
2451 (info))
2452 (Info-goto-node prolog-info-predicate-index)
2453 (goto-char (point-min))
2454 (while (re-search-forward
2455 "^\\* \\(.+\\)/\\([0-9]+\\)\\([^\n:*]*\\):" nil t)
2456 (let* ((name (match-string 1))
2457 (arity (string-to-number (match-string 2)))
2458 (comment (match-string 3))
2459 (fa (format "%s/%d%s" name arity comment))
2460 info-node)
2461 (beginning-of-line)
2462 ;; Extract the info node name
2463 (setq info-node (progn
2464 (re-search-forward ":[ \t]*\\([^:]+\\).$")
2465 (match-string 1)
2467 ;; ###### Easier? (from Milan version 0.1.28)
2468 ;; (setq info-node (Info-extract-menu-node-name))
2469 (if (equal fa (car last-entry))
2470 (setcdr last-entry (cons info-node (cdr last-entry)))
2471 (setq last-entry (cons fa (list info-node))
2472 l (cons last-entry l)))))
2473 (nreverse l)
2474 ))))
2475 (if verbose
2476 (message "Building info alist... done.")))
2479 ;;-------------------------------------------------------------------
2480 ;; Miscellaneous functions
2481 ;;-------------------------------------------------------------------
2483 ;; For Windows. Change backslash to slash. SICStus handles either
2484 ;; path separator but backslash must be doubled, therefore use slash.
2485 (defun prolog-bsts (string)
2486 "Change backslashes to slashes in STRING."
2487 (let ((str1 (copy-sequence string))
2488 (len (length string))
2489 (i 0))
2490 (while (< i len)
2491 (if (char-equal (aref str1 i) ?\\)
2492 (aset str1 i ?/))
2493 (setq i (1+ i)))
2494 str1))
2496 ;;(defun prolog-temporary-file ()
2497 ;; "Make temporary file name for compilation."
2498 ;; (make-temp-name
2499 ;; (concat
2500 ;; (or
2501 ;; (getenv "TMPDIR")
2502 ;; (getenv "TEMP")
2503 ;; (getenv "TMP")
2504 ;; (getenv "SYSTEMP")
2505 ;; "/tmp")
2506 ;; "/prolcomp")))
2507 ;;(setq prolog-temp-filename (prolog-bsts (prolog-temporary-file)))
2509 (defun prolog-temporary-file ()
2510 "Make temporary file name for compilation."
2511 (if prolog-temporary-file-name
2512 ;; We already have a file, erase content and continue
2513 (progn
2514 (write-region "" nil prolog-temporary-file-name nil 'silent)
2515 prolog-temporary-file-name)
2516 ;; Actually create the file and set `prolog-temporary-file-name'
2517 ;; accordingly.
2518 (setq prolog-temporary-file-name
2519 (make-temp-file "prolcomp" nil ".pl"))))
2521 (defun prolog-goto-prolog-process-buffer ()
2522 "Switch to the prolog process buffer and go to its end."
2523 (switch-to-buffer-other-window "*prolog*")
2524 (goto-char (point-max))
2527 (defun prolog-enable-sicstus-sd ()
2528 "Enable the source level debugging facilities of SICStus 3.7 and later."
2529 (interactive)
2530 (require 'pltrace) ; Load the SICStus debugger code
2531 ;; Turn on the source level debugging by default
2532 (add-hook 'prolog-inferior-mode-hook 'pltrace-on)
2533 (if (not prolog-use-sicstus-sd)
2534 (progn
2535 ;; If there is a *prolog* buffer, then call pltrace-on
2536 (if (get-buffer "*prolog*")
2537 ;; Avoid compilation warnings by using eval
2538 (eval '(pltrace-on)))
2539 (setq prolog-use-sicstus-sd t)
2542 (defun prolog-disable-sicstus-sd ()
2543 "Disable the source level debugging facilities of SICStus 3.7 and later."
2544 (interactive)
2545 (setq prolog-use-sicstus-sd nil)
2546 ;; Remove the hook
2547 (remove-hook 'prolog-inferior-mode-hook 'pltrace-on)
2548 ;; If there is a *prolog* buffer, then call pltrace-off
2549 (if (get-buffer "*prolog*")
2550 ;; Avoid compile warnings by using eval
2551 (eval '(pltrace-off))))
2553 (defun prolog-toggle-sicstus-sd ()
2554 ;; FIXME: Use define-minor-mode.
2555 "Toggle the source level debugging facilities of SICStus 3.7 and later."
2556 (interactive)
2557 (if prolog-use-sicstus-sd
2558 (prolog-disable-sicstus-sd)
2559 (prolog-enable-sicstus-sd)))
2561 (defun prolog-debug-on (&optional arg)
2562 "Enable debugging.
2563 When called with prefix argument ARG, disable debugging instead."
2564 (interactive "P")
2565 (if arg
2566 (prolog-debug-off)
2567 (prolog-process-insert-string (get-process "prolog")
2568 prolog-debug-on-string)
2569 (process-send-string "prolog" prolog-debug-on-string)))
2571 (defun prolog-debug-off ()
2572 "Disable debugging."
2573 (interactive)
2574 (prolog-process-insert-string (get-process "prolog")
2575 prolog-debug-off-string)
2576 (process-send-string "prolog" prolog-debug-off-string))
2578 (defun prolog-trace-on (&optional arg)
2579 "Enable tracing.
2580 When called with prefix argument ARG, disable tracing instead."
2581 (interactive "P")
2582 (if arg
2583 (prolog-trace-off)
2584 (prolog-process-insert-string (get-process "prolog")
2585 prolog-trace-on-string)
2586 (process-send-string "prolog" prolog-trace-on-string)))
2588 (defun prolog-trace-off ()
2589 "Disable tracing."
2590 (interactive)
2591 (prolog-process-insert-string (get-process "prolog")
2592 prolog-trace-off-string)
2593 (process-send-string "prolog" prolog-trace-off-string))
2595 (defun prolog-zip-on (&optional arg)
2596 "Enable zipping (for SICStus 3.7 and later).
2597 When called with prefix argument ARG, disable zipping instead."
2598 (interactive "P")
2599 (if (not (and (eq prolog-system 'sicstus)
2600 (prolog-atleast-version '(3 . 7))))
2601 (error "Only works for SICStus 3.7 and later"))
2602 (if arg
2603 (prolog-zip-off)
2604 (prolog-process-insert-string (get-process "prolog")
2605 prolog-zip-on-string)
2606 (process-send-string "prolog" prolog-zip-on-string)))
2608 (defun prolog-zip-off ()
2609 "Disable zipping (for SICStus 3.7 and later)."
2610 (interactive)
2611 (prolog-process-insert-string (get-process "prolog")
2612 prolog-zip-off-string)
2613 (process-send-string "prolog" prolog-zip-off-string))
2615 ;; (defun prolog-create-predicate-index ()
2616 ;; "Create an index for all predicates in the buffer."
2617 ;; (let ((predlist '())
2618 ;; clauseinfo
2619 ;; object
2620 ;; pos
2621 ;; )
2622 ;; (goto-char (point-min))
2623 ;; ;; Replace with prolog-clause-start!
2624 ;; (while (re-search-forward "^.+:-" nil t)
2625 ;; (setq pos (match-beginning 0))
2626 ;; (setq clauseinfo (prolog-clause-info))
2627 ;; (setq object (prolog-in-object))
2628 ;; (setq predlist (append
2629 ;; predlist
2630 ;; (list (cons
2631 ;; (if (and (eq prolog-system 'sicstus)
2632 ;; (prolog-in-object))
2633 ;; (format "%s::%s/%d"
2634 ;; object
2635 ;; (nth 0 clauseinfo)
2636 ;; (nth 1 clauseinfo))
2637 ;; (format "%s/%d"
2638 ;; (nth 0 clauseinfo)
2639 ;; (nth 1 clauseinfo)))
2640 ;; pos
2641 ;; ))))
2642 ;; (prolog-end-of-predicate))
2643 ;; predlist))
2645 (defun prolog-get-predspec ()
2646 (save-excursion
2647 (let ((state (prolog-clause-info))
2648 (object (prolog-in-object)))
2649 (if (or (equal (nth 0 state) "")
2650 (nth 4 (syntax-ppss)))
2652 (if (and (eq prolog-system 'sicstus)
2653 object)
2654 (format "%s::%s/%d"
2655 object
2656 (nth 0 state)
2657 (nth 1 state))
2658 (format "%s/%d"
2659 (nth 0 state)
2660 (nth 1 state)))
2661 ))))
2663 ;; For backward compatibility. Stolen from custom.el.
2664 (or (fboundp 'match-string)
2665 ;; Introduced in Emacs 19.29.
2666 (defun match-string (num &optional string)
2667 "Return string of text matched by last search.
2668 NUM specifies which parenthesized expression in the last regexp.
2669 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
2670 Zero means the entire text matched by the whole regexp or whole string.
2671 STRING should be given if the last search was by `string-match' on STRING."
2672 (if (match-beginning num)
2673 (if string
2674 (substring string (match-beginning num) (match-end num))
2675 (buffer-substring (match-beginning num) (match-end num))))))
2677 (defun prolog-pred-start ()
2678 "Return the starting point of the first clause of the current predicate."
2679 ;; FIXME: Use SMIE.
2680 (save-excursion
2681 (goto-char (prolog-clause-start))
2682 ;; Find first clause, unless it was a directive
2683 (if (and (not (looking-at "[:?]-"))
2684 (not (looking-at "[ \t]*[%/]")) ; Comment
2687 (let* ((pinfo (prolog-clause-info))
2688 (predname (nth 0 pinfo))
2689 (arity (nth 1 pinfo))
2690 (op (point)))
2691 (while (and (re-search-backward
2692 (format "^%s\\([(\\.]\\| *%s\\)"
2693 predname prolog-head-delimiter) nil t)
2694 (= arity (nth 1 (prolog-clause-info)))
2696 (setq op (point)))
2697 (if (eq prolog-system 'mercury)
2698 ;; Skip to the beginning of declarations of the predicate
2699 (progn
2700 (goto-char (prolog-beginning-of-clause))
2701 (while (and (not (eq (point) op))
2702 (looking-at
2703 (format ":-[ \t]*\\(pred\\|mode\\)[ \t]+%s"
2704 predname)))
2705 (setq op (point))
2706 (goto-char (prolog-beginning-of-clause)))))
2708 (point))))
2710 (defun prolog-pred-end ()
2711 "Return the position at the end of the last clause of the current predicate."
2712 ;; FIXME: Use SMIE.
2713 (save-excursion
2714 (goto-char (prolog-clause-end)) ; If we are before the first predicate.
2715 (goto-char (prolog-clause-start))
2716 (let* ((pinfo (prolog-clause-info))
2717 (predname (nth 0 pinfo))
2718 (arity (nth 1 pinfo))
2719 oldp
2720 (notdone t)
2721 (op (point)))
2722 (if (looking-at "[:?]-")
2723 ;; This was a directive
2724 (progn
2725 (if (and (eq prolog-system 'mercury)
2726 (looking-at
2727 (format ":-[ \t]*\\(pred\\|mode\\)[ \t]+\\(%s+\\)"
2728 prolog-atom-regexp)))
2729 ;; Skip predicate declarations
2730 (progn
2731 (setq predname (buffer-substring-no-properties
2732 (match-beginning 2) (match-end 2)))
2733 (while (re-search-forward
2734 (format
2735 "\n*\\(:-[ \t]*\\(pred\\|mode\\)[ \t]+\\)?%s[( \t]"
2736 predname)
2737 nil t))))
2738 (goto-char (prolog-clause-end))
2739 (setq op (point)))
2740 ;; It was not a directive, find the last clause
2741 (while (and notdone
2742 (re-search-forward
2743 (format "^%s\\([(\\.]\\| *%s\\)"
2744 predname prolog-head-delimiter) nil t)
2745 (= arity (nth 1 (prolog-clause-info))))
2746 (setq oldp (point))
2747 (setq op (prolog-clause-end))
2748 (if (>= oldp op)
2749 ;; End of clause not found.
2750 (setq notdone nil)
2751 ;; Continue while loop
2752 (goto-char op))))
2753 op)))
2755 (defun prolog-clause-start (&optional not-allow-methods)
2756 "Return the position at the start of the head of the current clause.
2757 If NOTALLOWMETHODS is non-nil then do not match on methods in
2758 objects (relevant only if `prolog-system' is set to `sicstus')."
2759 (save-excursion
2760 (let ((notdone t)
2761 (retval (point-min)))
2762 (end-of-line)
2764 ;; SICStus object?
2765 (if (and (not not-allow-methods)
2766 (eq prolog-system 'sicstus)
2767 (prolog-in-object))
2768 (while (and
2769 notdone
2770 ;; Search for a head or a fact
2771 (re-search-backward
2772 ;; If in object, then find method start.
2773 ;; "^[ \t]+[a-z$].*\\(:-\\|&\\|:: {\\|,\\)"
2774 "^[ \t]+[a-z$].*\\(:-\\|&\\|:: {\\)" ; The comma causes
2775 ; problems since we cannot assume
2776 ; that the line starts at column 0,
2777 ; thus we don't know if the line
2778 ; is a head or a subgoal
2779 (point-min) t))
2780 (if (>= (prolog-paren-balance) 0) ; To no match on " a) :-"
2781 ;; Start of method found
2782 (progn
2783 (setq retval (point))
2784 (setq notdone nil)))
2785 ) ; End of while
2787 ;; Not in object
2788 (while (and
2789 notdone
2790 ;; Search for a text at beginning of a line
2791 ;; ######
2792 ;; (re-search-backward "^[a-z$']" nil t))
2793 (let ((case-fold-search nil))
2794 (re-search-backward "^\\([[:lower:]$']\\|[:?]-\\)"
2795 nil t)))
2796 (let ((bal (prolog-paren-balance)))
2797 (cond
2798 ((> bal 0)
2799 ;; Start of clause found
2800 (progn
2801 (setq retval (point))
2802 (setq notdone nil)))
2803 ((and (= bal 0)
2804 (looking-at
2805 (format ".*\\(\\.\\|%s\\|!,\\)[ \t]*\\(%%.*\\|\\)$"
2806 prolog-head-delimiter)))
2807 ;; Start of clause found if the line ends with a '.' or
2808 ;; a prolog-head-delimiter
2809 (progn
2810 (setq retval (point))
2811 (setq notdone nil))
2813 (t nil) ; Do nothing
2814 ))))
2816 retval)))
2818 (defun prolog-clause-end (&optional not-allow-methods)
2819 "Return the position at the end of the current clause.
2820 If NOTALLOWMETHODS is non-nil then do not match on methods in
2821 objects (relevant only if `prolog-system' is set to `sicstus')."
2822 (save-excursion
2823 (beginning-of-line) ; Necessary since we use "^...." for the search.
2824 (if (re-search-forward
2825 (if (and (not not-allow-methods)
2826 (eq prolog-system 'sicstus)
2827 (prolog-in-object))
2828 (format
2829 "^\\(%s\\|%s\\|[^\n\'\"%%]\\)*&[ \t]*\\(\\|%%.*\\)$\\|[ \t]*}"
2830 prolog-quoted-atom-regexp prolog-string-regexp)
2831 (format
2832 "^\\(%s\\|%s\\|[^\n\'\"%%]\\)*\\.[ \t]*\\(\\|%%.*\\)$"
2833 prolog-quoted-atom-regexp prolog-string-regexp))
2834 nil t)
2835 (if (and (nth 8 (syntax-ppss))
2836 (not (eobp)))
2837 (progn
2838 (forward-char)
2839 (prolog-clause-end))
2840 (point))
2841 (point))))
2843 (defun prolog-clause-info ()
2844 "Return a (name arity) list for the current clause."
2845 (save-excursion
2846 (goto-char (prolog-clause-start))
2847 (let* ((op (point))
2848 (predname
2849 (if (looking-at prolog-atom-char-regexp)
2850 (progn
2851 (skip-chars-forward "^ (\\.")
2852 (buffer-substring op (point)))
2853 ""))
2854 (arity 0))
2855 ;; Retrieve the arity.
2856 (if (looking-at prolog-left-paren)
2857 (let ((endp (save-excursion
2858 (forward-list) (point))))
2859 (setq arity 1)
2860 (forward-char 1) ; Skip the opening paren.
2861 (while (progn
2862 (skip-chars-forward "^[({,'\"")
2863 (< (point) endp))
2864 (if (looking-at ",")
2865 (progn
2866 (setq arity (1+ arity))
2867 (forward-char 1) ; Skip the comma.
2869 ;; We found a string, list or something else we want
2870 ;; to skip over.
2871 (forward-sexp 1))
2873 (list predname arity))))
2875 (defun prolog-in-object ()
2876 "Return object name if the point is inside a SICStus object definition."
2877 ;; Return object name if the last line that starts with a character
2878 ;; that is neither white space nor a comment start
2879 (save-excursion
2880 (if (save-excursion
2881 (beginning-of-line)
2882 (looking-at "\\([^\n ]+\\)[ \t]*::[ \t]*{"))
2883 ;; We were in the head of the object
2884 (match-string 1)
2885 ;; We were not in the head
2886 (if (and (re-search-backward "^[a-z$'}]" nil t)
2887 (looking-at "\\([^\n ]+\\)[ \t]*::[ \t]*{"))
2888 (match-string 1)
2889 nil))))
2891 (defun prolog-beginning-of-clause ()
2892 "Move to the beginning of current clause.
2893 If already at the beginning of clause, move to previous clause."
2894 (interactive)
2895 (let ((point (point))
2896 (new-point (prolog-clause-start)))
2897 (if (and (>= new-point point)
2898 (> point 1))
2899 (progn
2900 (goto-char (1- point))
2901 (goto-char (prolog-clause-start)))
2902 (goto-char new-point)
2903 (skip-chars-forward " \t"))))
2905 ;; (defun prolog-previous-clause ()
2906 ;; "Move to the beginning of the previous clause."
2907 ;; (interactive)
2908 ;; (forward-char -1)
2909 ;; (prolog-beginning-of-clause))
2911 (defun prolog-end-of-clause ()
2912 "Move to the end of clause.
2913 If already at the end of clause, move to next clause."
2914 (interactive)
2915 (let ((point (point))
2916 (new-point (prolog-clause-end)))
2917 (if (and (<= new-point point)
2918 (not (eq new-point (point-max))))
2919 (progn
2920 (goto-char (1+ point))
2921 (goto-char (prolog-clause-end)))
2922 (goto-char new-point))))
2924 ;; (defun prolog-next-clause ()
2925 ;; "Move to the beginning of the next clause."
2926 ;; (interactive)
2927 ;; (prolog-end-of-clause)
2928 ;; (forward-char)
2929 ;; (prolog-end-of-clause)
2930 ;; (prolog-beginning-of-clause))
2932 (defun prolog-beginning-of-predicate ()
2933 "Go to the nearest beginning of predicate before current point.
2934 Return the final point or nil if no such a beginning was found."
2935 ;; FIXME: Hook into beginning-of-defun.
2936 (interactive)
2937 (let ((op (point))
2938 (pos (prolog-pred-start)))
2939 (if pos
2940 (if (= op pos)
2941 (if (not (bobp))
2942 (progn
2943 (goto-char pos)
2944 (backward-char 1)
2945 (setq pos (prolog-pred-start))
2946 (if pos
2947 (progn
2948 (goto-char pos)
2949 (point)))))
2950 (goto-char pos)
2951 (point)))))
2953 (defun prolog-end-of-predicate ()
2954 "Go to the end of the current predicate."
2955 ;; FIXME: Hook into end-of-defun.
2956 (interactive)
2957 (let ((op (point)))
2958 (goto-char (prolog-pred-end))
2959 (if (= op (point))
2960 (progn
2961 (forward-line 1)
2962 (prolog-end-of-predicate)))))
2964 (defun prolog-insert-predspec ()
2965 "Insert the predspec for the current predicate."
2966 (interactive)
2967 (let* ((pinfo (prolog-clause-info))
2968 (predname (nth 0 pinfo))
2969 (arity (nth 1 pinfo)))
2970 (insert (format "%s/%d" predname arity))))
2972 (defun prolog-view-predspec ()
2973 "Insert the predspec for the current predicate."
2974 (interactive)
2975 (let* ((pinfo (prolog-clause-info))
2976 (predname (nth 0 pinfo))
2977 (arity (nth 1 pinfo)))
2978 (message (format "%s/%d" predname arity))))
2980 (defun prolog-insert-predicate-template ()
2981 "Insert the template for the current clause."
2982 (interactive)
2983 (let* ((n 1)
2984 oldp
2985 (pinfo (prolog-clause-info))
2986 (predname (nth 0 pinfo))
2987 (arity (nth 1 pinfo)))
2988 (insert predname)
2989 (if (> arity 0)
2990 (progn
2991 (insert "(")
2992 (when prolog-electric-dot-full-predicate-template
2993 (setq oldp (point))
2994 (while (< n arity)
2995 (insert ",")
2996 (setq n (1+ n)))
2997 (insert ")")
2998 (goto-char oldp))
3002 (defun prolog-insert-next-clause ()
3003 "Insert newline and the name of the current clause."
3004 (interactive)
3005 (insert "\n")
3006 (prolog-insert-predicate-template))
3008 (defun prolog-insert-module-modeline ()
3009 "Insert a modeline for module specification.
3010 This line should be first in the buffer.
3011 The module name should be written manually just before the semi-colon."
3012 (interactive)
3013 (insert "%%% -*- Module: ; -*-\n")
3014 (backward-char 6))
3016 (defalias 'prolog-uncomment-region
3017 (if (fboundp 'uncomment-region) #'uncomment-region
3018 (lambda (beg end)
3019 "Uncomment the region between BEG and END."
3020 (interactive "r")
3021 (comment-region beg end -1))))
3023 (defun prolog-indent-predicate ()
3024 "Indent the current predicate."
3025 (interactive)
3026 (indent-region (prolog-pred-start) (prolog-pred-end) nil))
3028 (defun prolog-indent-buffer ()
3029 "Indent the entire buffer."
3030 (interactive)
3031 (indent-region (point-min) (point-max) nil))
3033 (defun prolog-mark-clause ()
3034 "Put mark at the end of this clause and move point to the beginning."
3035 (interactive)
3036 (let ((pos (point)))
3037 (goto-char (prolog-clause-end))
3038 (forward-line 1)
3039 (beginning-of-line)
3040 (set-mark (point))
3041 (goto-char pos)
3042 (goto-char (prolog-clause-start))))
3044 (defun prolog-mark-predicate ()
3045 "Put mark at the end of this predicate and move point to the beginning."
3046 (interactive)
3047 (goto-char (prolog-pred-end))
3048 (let ((pos (point)))
3049 (forward-line 1)
3050 (beginning-of-line)
3051 (set-mark (point))
3052 (goto-char pos)
3053 (goto-char (prolog-pred-start))))
3055 (defun prolog-electric--colon ()
3056 "If `prolog-electric-colon-flag' is non-nil, insert the electric `:' construct.
3057 That is, insert space (if appropriate), `:-' and newline if colon is pressed
3058 at the end of a line that starts in the first column (i.e., clause heads)."
3059 (when (and prolog-electric-colon-flag
3060 (eq (char-before) ?:)
3061 (not current-prefix-arg)
3062 (eolp)
3063 (not (memq (char-after (line-beginning-position))
3064 '(?\s ?\t ?\%))))
3065 (unless (memq (char-before (1- (point))) '(?\s ?\t))
3066 (save-excursion (forward-char -1) (insert " ")))
3067 (insert "-\n")
3068 (indent-according-to-mode)))
3070 (defun prolog-electric--dash ()
3071 "If `prolog-electric-dash-flag' is non-nil, insert the electric `-' construct.
3072 that is, insert space (if appropriate), `-->' and newline if dash is pressed
3073 at the end of a line that starts in the first column (i.e., DCG heads)."
3074 (when (and prolog-electric-dash-flag
3075 (eq (char-before) ?-)
3076 (not current-prefix-arg)
3077 (eolp)
3078 (not (memq (char-after (line-beginning-position))
3079 '(?\s ?\t ?\%))))
3080 (unless (memq (char-before (1- (point))) '(?\s ?\t))
3081 (save-excursion (forward-char -1) (insert " ")))
3082 (insert "->\n")
3083 (indent-according-to-mode)))
3085 (defun prolog-electric--dot ()
3086 "Make dot electric, if `prolog-electric-dot-flag' is non-nil.
3087 When invoked at the end of nonempty line, insert dot and newline.
3088 When invoked at the end of an empty line, insert a recursive call to
3089 the current predicate.
3090 When invoked at the beginning of line, insert a head of a new clause
3091 of the current predicate."
3092 ;; Check for situations when the electricity should not be active
3093 (if (or (not prolog-electric-dot-flag)
3094 (not (eq (char-before) ?\.))
3095 current-prefix-arg
3096 (nth 8 (syntax-ppss))
3097 ;; Do not be electric in a floating point number or an operator
3098 (not
3099 (save-excursion
3100 (forward-char -1)
3101 (skip-chars-backward " \t")
3102 (let ((num (> (skip-chars-backward "0-9") 0)))
3103 (or (bolp)
3104 (memq (char-syntax (char-before))
3105 (if num '(?w ?_) '(?\) ?w ?_)))))))
3106 ;; Do not be electric if inside a parenthesis pair.
3107 (not (= (car (syntax-ppss))
3110 nil ;;Not electric.
3111 (cond
3112 ;; Beginning of line
3113 ((save-excursion (forward-char -1) (bolp))
3114 (delete-region (1- (point)) (point)) ;Delete the dot that called us.
3115 (prolog-insert-predicate-template))
3116 ;; At an empty line with at least one whitespace
3117 ((save-excursion
3118 (beginning-of-line)
3119 (looking-at "[ \t]+\\.$"))
3120 (delete-region (1- (point)) (point)) ;Delete the dot that called us.
3121 (prolog-insert-predicate-template)
3122 (when prolog-electric-dot-full-predicate-template
3123 (save-excursion
3124 (end-of-line)
3125 (insert ".\n"))))
3126 ;; Default
3128 (insert "\n"))
3131 (defun prolog-electric--underscore ()
3132 "Replace variable with an underscore.
3133 If `prolog-electric-underscore-flag' is non-nil and the point is
3134 on a variable then replace the variable with underscore and skip
3135 the following comma and whitespace, if any."
3136 (when prolog-electric-underscore-flag
3137 (let ((case-fold-search nil))
3138 (when (and (not (nth 8 (syntax-ppss)))
3139 (eq (char-before) ?_)
3140 (save-excursion
3141 (skip-chars-backward "[:alpha:]_")
3142 (looking-at "\\_<[_[:upper:]][[:alnum:]_]*\\_>")))
3143 (replace-match "_")
3144 (skip-chars-forward ", \t\n")))))
3146 (defun prolog-post-self-insert ()
3147 (pcase last-command-event
3148 (`?_ (prolog-electric--underscore))
3149 (`?- (prolog-electric--dash))
3150 (`?: (prolog-electric--colon))
3151 ((or `?\( `?\; `?>) (prolog-electric--if-then-else))
3152 (`?. (prolog-electric--dot))))
3154 (defun prolog-find-term (functor arity &optional prefix)
3155 "Go to the position at the start of the next occurrence of a term.
3156 The term is specified with FUNCTOR and ARITY. The optional argument
3157 PREFIX is the prefix of the search regexp."
3158 (let* (;; If prefix is not set then use the default "\\<"
3159 (prefix (if (not prefix)
3160 "\\<"
3161 prefix))
3162 (regexp (concat prefix functor))
3163 (i 1))
3165 ;; Build regexp for the search if the arity is > 0
3166 (if (= arity 0)
3167 ;; Add that the functor must be at the end of a word. This
3168 ;; does not work if the arity is > 0 since the closing )
3169 ;; is not a word constituent.
3170 (setq regexp (concat regexp "\\>"))
3171 ;; Arity is > 0, add parens and commas
3172 (setq regexp (concat regexp "("))
3173 (while (< i arity)
3174 (setq regexp (concat regexp ".+,"))
3175 (setq i (1+ i)))
3176 (setq regexp (concat regexp ".+)")))
3178 ;; Search, and return position
3179 (if (re-search-forward regexp nil t)
3180 (goto-char (match-beginning 0))
3181 (error "Term not found"))
3184 (defun prolog-variables-to-anonymous (beg end)
3185 "Replace all variables within a region BEG to END by anonymous variables."
3186 (interactive "r")
3187 (save-excursion
3188 (let ((case-fold-search nil))
3189 (goto-char end)
3190 (while (re-search-backward "\\<[A-Z_][a-zA-Z_0-9]*\\>" beg t)
3191 (progn
3192 (replace-match "_")
3193 (backward-char)))
3196 ;;(defun prolog-regexp-dash-continuous-chars (chars)
3197 ;; (let ((ints (mapcar #'prolog-char-to-int (string-to-list chars)))
3198 ;; (beg 0)
3199 ;; (end 0))
3200 ;; (if (null ints)
3201 ;; chars
3202 ;; (while (and (< (+ beg 1) (length chars))
3203 ;; (not (or (= (+ (nth beg ints) 1) (nth (+ beg 1) ints))
3204 ;; (= (nth beg ints) (nth (+ beg 1) ints)))))
3205 ;; (setq beg (+ beg 1)))
3206 ;; (setq beg (+ beg 1)
3207 ;; end beg)
3208 ;; (while (and (< (+ end 1) (length chars))
3209 ;; (or (= (+ (nth end ints) 1) (nth (+ end 1) ints))
3210 ;; (= (nth end ints) (nth (+ end 1) ints))))
3211 ;; (setq end (+ end 1)))
3212 ;; (if (equal (substring chars end) "")
3213 ;; (substring chars 0 beg)
3214 ;; (concat (substring chars 0 beg) "-"
3215 ;; (prolog-regexp-dash-continuous-chars (substring chars end))))
3216 ;; )))
3218 ;;(defun prolog-condense-character-sets (regexp)
3219 ;; "Condense adjacent characters in character sets of REGEXP."
3220 ;; (let ((next -1))
3221 ;; (while (setq next (string-match "\\[\\(.*?\\)\\]" regexp (1+ next)))
3222 ;; (setq regexp (replace-match (prolog-dash-letters (match-string 1 regexp))
3223 ;; t t regexp 1))))
3224 ;; regexp)
3226 ;;-------------------------------------------------------------------
3227 ;; Menu stuff (both for the editing buffer and for the inferior
3228 ;; prolog buffer)
3229 ;;-------------------------------------------------------------------
3231 (unless (fboundp 'region-exists-p)
3232 (defun region-exists-p ()
3233 "Non-nil if the mark is set. Lobotomized version for Emacsen that do not provide their own."
3234 (mark)))
3237 ;; GNU Emacs ignores `easy-menu-add' so the order in which the menus
3238 ;; are defined _is_ important!
3240 (easy-menu-define
3241 prolog-menu-help (list prolog-mode-map prolog-inferior-mode-map)
3242 "Help menu for the Prolog mode."
3243 ;; FIXME: Does it really deserve a whole menu to itself?
3244 `(,(if (featurep 'xemacs) "Help"
3245 ;; Not sure it's worth the trouble. --Stef
3246 ;; (add-to-list 'menu-bar-final-items
3247 ;; (easy-menu-intern "Prolog-Help"))
3248 "Prolog-help")
3249 ["On predicate" prolog-help-on-predicate prolog-help-function-i]
3250 ["Apropos" prolog-help-apropos (eq prolog-system 'swi)]
3251 "---"
3252 ["Describe mode" describe-mode t]))
3254 (easy-menu-define
3255 prolog-edit-menu-runtime prolog-mode-map
3256 "Runtime Prolog commands available from the editing buffer"
3257 ;; FIXME: Don't use a whole menu for just "Run Mercury". --Stef
3258 `("System"
3259 ;; Runtime menu name.
3260 ,@(unless (featurep 'xemacs)
3261 '(:label (cond ((eq prolog-system 'eclipse) "ECLiPSe")
3262 ((eq prolog-system 'mercury) "Mercury")
3263 (t "System"))))
3265 ;; Consult items, NIL for mercury.
3266 ["Consult file" prolog-consult-file
3267 :included (not (eq prolog-system 'mercury))]
3268 ["Consult buffer" prolog-consult-buffer
3269 :included (not (eq prolog-system 'mercury))]
3270 ["Consult region" prolog-consult-region :active (region-exists-p)
3271 :included (not (eq prolog-system 'mercury))]
3272 ["Consult predicate" prolog-consult-predicate
3273 :included (not (eq prolog-system 'mercury))]
3275 ;; Compile items, NIL for everything but SICSTUS.
3276 ,(if (featurep 'xemacs) "---"
3277 ["---" nil :included (eq prolog-system 'sicstus)])
3278 ["Compile file" prolog-compile-file
3279 :included (eq prolog-system 'sicstus)]
3280 ["Compile buffer" prolog-compile-buffer
3281 :included (eq prolog-system 'sicstus)]
3282 ["Compile region" prolog-compile-region :active (region-exists-p)
3283 :included (eq prolog-system 'sicstus)]
3284 ["Compile predicate" prolog-compile-predicate
3285 :included (eq prolog-system 'sicstus)]
3287 ;; Debug items, NIL for Mercury.
3288 ,(if (featurep 'xemacs) "---"
3289 ["---" nil :included (not (eq prolog-system 'mercury))])
3290 ;; FIXME: Could we use toggle or radio buttons? --Stef
3291 ["Debug" prolog-debug-on :included (not (eq prolog-system 'mercury))]
3292 ["Debug off" prolog-debug-off
3293 ;; In SICStus, these are pairwise disjunctive,
3294 ;; so it's enough with a single "off"-command
3295 :included (not (memq prolog-system '(mercury sicstus)))]
3296 ["Trace" prolog-trace-on :included (not (eq prolog-system 'mercury))]
3297 ["Trace off" prolog-trace-off
3298 :included (not (memq prolog-system '(mercury sicstus)))]
3299 ["Zip" prolog-zip-on :included (and (eq prolog-system 'sicstus)
3300 (prolog-atleast-version '(3 . 7)))]
3301 ["All debug off" prolog-debug-off
3302 :included (eq prolog-system 'sicstus)]
3303 ["Source level debugging"
3304 prolog-toggle-sicstus-sd
3305 :included (and (eq prolog-system 'sicstus)
3306 (prolog-atleast-version '(3 . 7)))
3307 :style toggle
3308 :selected prolog-use-sicstus-sd]
3310 "---"
3311 ["Run" run-prolog
3312 :suffix (cond ((eq prolog-system 'eclipse) "ECLiPSe")
3313 ((eq prolog-system 'mercury) "Mercury")
3314 (t "Prolog"))]))
3316 (easy-menu-define
3317 prolog-edit-menu-insert-move prolog-mode-map
3318 "Commands for Prolog code manipulation."
3319 '("Prolog"
3320 ["Comment region" comment-region (region-exists-p)]
3321 ["Uncomment region" prolog-uncomment-region (region-exists-p)]
3322 ["Add comment/move to comment" indent-for-comment t]
3323 ["Convert variables in region to '_'" prolog-variables-to-anonymous
3324 :active (region-exists-p) :included (not (eq prolog-system 'mercury))]
3325 "---"
3326 ["Insert predicate template" prolog-insert-predicate-template t]
3327 ["Insert next clause head" prolog-insert-next-clause t]
3328 ["Insert predicate spec" prolog-insert-predspec t]
3329 ["Insert module modeline" prolog-insert-module-modeline t]
3330 "---"
3331 ["Beginning of clause" prolog-beginning-of-clause t]
3332 ["End of clause" prolog-end-of-clause t]
3333 ["Beginning of predicate" prolog-beginning-of-predicate t]
3334 ["End of predicate" prolog-end-of-predicate t]
3335 "---"
3336 ["Indent line" indent-according-to-mode t]
3337 ["Indent region" indent-region (region-exists-p)]
3338 ["Indent predicate" prolog-indent-predicate t]
3339 ["Indent buffer" prolog-indent-buffer t]
3340 ["Align region" align (region-exists-p)]
3341 "---"
3342 ["Mark clause" prolog-mark-clause t]
3343 ["Mark predicate" prolog-mark-predicate t]
3344 ["Mark paragraph" mark-paragraph t]
3347 (defun prolog-menu ()
3348 "Add the menus for the Prolog editing buffers."
3350 (easy-menu-add prolog-edit-menu-insert-move)
3351 (easy-menu-add prolog-edit-menu-runtime)
3353 ;; Add predicate index menu
3354 (setq-local imenu-create-index-function
3355 'imenu-default-create-index-function)
3356 ;;Milan (this has problems with object methods...) ###### Does it? (Stefan)
3357 (setq-local imenu-prev-index-position-function
3358 #'prolog-beginning-of-predicate)
3359 (setq-local imenu-extract-index-name-function #'prolog-get-predspec)
3361 (if (and prolog-imenu-flag
3362 (< (count-lines (point-min) (point-max)) prolog-imenu-max-lines))
3363 (imenu-add-to-menubar "Predicates"))
3365 (easy-menu-add prolog-menu-help))
3367 (easy-menu-define
3368 prolog-inferior-menu-all prolog-inferior-mode-map
3369 "Menu for the inferior Prolog buffer."
3370 `("Prolog"
3371 ;; Runtime menu name.
3372 ,@(unless (featurep 'xemacs)
3373 '(:label (cond ((eq prolog-system 'eclipse) "ECLiPSe")
3374 ((eq prolog-system 'mercury) "Mercury")
3375 (t "Prolog"))))
3377 ;; Debug items, NIL for Mercury.
3378 ,(if (featurep 'xemacs) "---"
3379 ["---" nil :included (not (eq prolog-system 'mercury))])
3380 ;; FIXME: Could we use toggle or radio buttons? --Stef
3381 ["Debug" prolog-debug-on :included (not (eq prolog-system 'mercury))]
3382 ["Debug off" prolog-debug-off
3383 ;; In SICStus, these are pairwise disjunctive,
3384 ;; so it's enough with a single "off"-command
3385 :included (not (memq prolog-system '(mercury sicstus)))]
3386 ["Trace" prolog-trace-on :included (not (eq prolog-system 'mercury))]
3387 ["Trace off" prolog-trace-off
3388 :included (not (memq prolog-system '(mercury sicstus)))]
3389 ["Zip" prolog-zip-on :included (and (eq prolog-system 'sicstus)
3390 (prolog-atleast-version '(3 . 7)))]
3391 ["All debug off" prolog-debug-off
3392 :included (eq prolog-system 'sicstus)]
3393 ["Source level debugging"
3394 prolog-toggle-sicstus-sd
3395 :included (and (eq prolog-system 'sicstus)
3396 (prolog-atleast-version '(3 . 7)))
3397 :style toggle
3398 :selected prolog-use-sicstus-sd]
3400 ;; Runtime.
3401 "---"
3402 ["Interrupt Prolog" comint-interrupt-subjob t]
3403 ["Quit Prolog" comint-quit-subjob t]
3404 ["Kill Prolog" comint-kill-subjob t]))
3407 (defun prolog-inferior-menu ()
3408 "Create the menus for the Prolog inferior buffer.
3409 This menu is dynamically created because one may change systems during
3410 the life of an Emacs session."
3411 (easy-menu-add prolog-inferior-menu-all)
3412 (easy-menu-add prolog-menu-help))
3414 (defun prolog-mode-version ()
3415 "Echo the current version of Prolog mode in the minibuffer."
3416 (interactive)
3417 (message "Using Prolog mode version %s" prolog-mode-version))
3419 (provide 'prolog)
3421 ;;; prolog.el ends here