]> code.delx.au - gnu-emacs/blob - lisp/progmodes/prolog.el
merge trunk
[gnu-emacs] / lisp / progmodes / prolog.el
1 ;;; prolog.el --- major mode for editing and running Prolog under Emacs
2
3 ;; Copyright (C) 1986, 1987, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
7 ;; Keywords: languages
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This package provides a major mode for editing Prolog. It knows
27 ;; about Prolog syntax and comments, and can send regions to an inferior
28 ;; Prolog interpreter process. Font locking is tuned towards GNU Prolog.
29
30 ;;; Code:
31
32 (defvar comint-prompt-regexp)
33 (defvar comint-process-echoes)
34 (require 'smie)
35
36 (defgroup prolog nil
37 "Major mode for editing and running Prolog under Emacs."
38 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
39 :group 'languages)
40
41
42 (defcustom prolog-program-name
43 (let ((names '("prolog" "gprolog" "swipl")))
44 (while (and names
45 (not (executable-find (car names))))
46 (setq names (cdr names)))
47 (or (car names) "prolog"))
48 "Program name for invoking an inferior Prolog with `run-prolog'."
49 :type 'string
50 :group 'prolog)
51
52 (defcustom prolog-consult-string "reconsult(user).\n"
53 "(Re)Consult mode (for C-Prolog and Quintus Prolog). "
54 :type 'string
55 :group 'prolog)
56
57 (defcustom prolog-compile-string "compile(user).\n"
58 "Compile mode (for Quintus Prolog)."
59 :type 'string
60 :group 'prolog)
61
62 (defcustom prolog-eof-string "end_of_file.\n"
63 "String that represents end of file for Prolog.
64 When nil, send actual operating system end of file."
65 :type 'string
66 :group 'prolog)
67
68 (defcustom prolog-indent-width 4
69 "Level of indentation in Prolog buffers."
70 :type 'integer
71 :group 'prolog)
72
73 (defvar prolog-font-lock-keywords
74 '(("\\(#[<=]=>\\|:-\\)\\|\\(#=\\)\\|\\(#[#<>\\/][=\\/]*\\|!\\)"
75 0 font-lock-keyword-face)
76 ("\\<\\(is\\|write\\|nl\\|read_\\sw+\\)\\>"
77 1 font-lock-keyword-face)
78 ("^\\(\\sw+\\)\\s-*\\((\\(.+\\))\\)*"
79 (1 font-lock-function-name-face)
80 (3 font-lock-variable-name-face)))
81 "Font-lock keywords for Prolog mode.")
82
83 (defvar prolog-mode-syntax-table
84 (let ((table (make-syntax-table)))
85 (modify-syntax-entry ?_ "w" table)
86 (modify-syntax-entry ?\\ "\\" table)
87 (modify-syntax-entry ?/ ". 14" table)
88 (modify-syntax-entry ?* ". 23" table)
89 (modify-syntax-entry ?+ "." table)
90 (modify-syntax-entry ?- "." table)
91 (modify-syntax-entry ?= "." table)
92 (modify-syntax-entry ?% "<" table)
93 (modify-syntax-entry ?\n ">" table)
94 (modify-syntax-entry ?< "." table)
95 (modify-syntax-entry ?> "." table)
96 (modify-syntax-entry ?\' "\"" table)
97 table))
98
99 (defvar prolog-mode-abbrev-table nil)
100 (define-abbrev-table 'prolog-mode-abbrev-table ())
101
102 (defun prolog-smie-forward-token ()
103 (forward-comment (point-max))
104 (buffer-substring-no-properties
105 (point)
106 (progn (cond
107 ((looking-at "[!;]") (forward-char 1))
108 ((not (zerop (skip-chars-forward "#&*+-./:<=>?@\\^`~"))))
109 ((not (zerop (skip-syntax-forward "w_'"))))
110 ;; In case of non-ASCII punctuation.
111 ((not (zerop (skip-syntax-forward ".")))))
112 (point))))
113
114 (defun prolog-smie-backward-token ()
115 (forward-comment (- (point-max)))
116 (buffer-substring-no-properties
117 (point)
118 (progn (cond
119 ((memq (char-before) '(?! ?\;)) (forward-char -1))
120 ((not (zerop (skip-chars-backward "#&*+-./:<=>?@\\^`~"))))
121 ((not (zerop (skip-syntax-backward "w_'"))))
122 ;; In case of non-ASCII punctuation.
123 ((not (zerop (skip-syntax-backward ".")))))
124 (point))))
125
126 (defconst prolog-smie-op-levels
127 ;; Rather than construct the operator levels table from the BNF,
128 ;; we directly provide the operator precedences from GNU Prolog's
129 ;; manual (7.14.10 op/3). The only problem is that GNU Prolog's
130 ;; manual uses precedence levels in the opposite sense (higher
131 ;; numbers bind less tightly) than SMIE, so we use negative numbers.
132 '(("." -10000 -10000)
133 (":-" -1200 -1200)
134 ("-->" -1200 -1200)
135 (";" -1100 -1100)
136 ("->" -1050 -1050)
137 ("," -1000 -1000)
138 ("\\+" -900 -900)
139 ("=" -700 -700)
140 ("\\=" -700 -700)
141 ("=.." -700 -700)
142 ("==" -700 -700)
143 ("\\==" -700 -700)
144 ("@<" -700 -700)
145 ("@=<" -700 -700)
146 ("@>" -700 -700)
147 ("@>=" -700 -700)
148 ("is" -700 -700)
149 ("=:=" -700 -700)
150 ("=\\=" -700 -700)
151 ("<" -700 -700)
152 ("=<" -700 -700)
153 (">" -700 -700)
154 (">=" -700 -700)
155 (":" -600 -600)
156 ("+" -500 -500)
157 ("-" -500 -500)
158 ("/\\" -500 -500)
159 ("\\/" -500 -500)
160 ("*" -400 -400)
161 ("/" -400 -400)
162 ("//" -400 -400)
163 ("rem" -400 -400)
164 ("mod" -400 -400)
165 ("<<" -400 -400)
166 (">>" -400 -400)
167 ("**" -200 -200)
168 ("^" -200 -200)
169 ;; Prefix
170 ;; ("+" 200 200)
171 ;; ("-" 200 200)
172 ;; ("\\" 200 200)
173 )
174 "Precedence levels of infix operators.")
175
176 (defun prolog-smie-rules (kind token)
177 (pcase (cons kind token)
178 (`(:elem . basic) prolog-indent-width)
179 (`(:after . ".") 0) ;; To work around smie-closer-alist.
180 (`(:after . ,(or `":-" `"->")) prolog-indent-width)))
181
182 (defun prolog-mode-variables ()
183 (make-local-variable 'paragraph-separate)
184 (setq paragraph-separate (concat "%%\\|$\\|" page-delimiter)) ;'%%..'
185 (make-local-variable 'paragraph-ignore-fill-prefix)
186 (setq paragraph-ignore-fill-prefix t)
187 (make-local-variable 'imenu-generic-expression)
188 (setq imenu-generic-expression '((nil "^\\sw+" 0)))
189
190 ;; Setup SMIE.
191 (smie-setup prolog-smie-op-levels #'prolog-smie-rules
192 :forward-token #'prolog-smie-forward-token
193 :backward-token #'prolog-smie-backward-token)
194 (set (make-local-variable 'smie-blink-matching-triggers) '(?.))
195 (set (make-local-variable 'smie-closer-alist) '((t . ".")))
196 (add-hook 'post-self-insert-hook #'smie-blink-matching-open 'append 'local)
197 ;; There's no real closer in Prolog anyway.
198 (set (make-local-variable 'smie-blink-matching-inners) t)
199
200 (make-local-variable 'comment-start)
201 (setq comment-start "%")
202 (make-local-variable 'comment-start-skip)
203 (setq comment-start-skip "\\(?:%+\\|/\\*+\\)[ \t]*")
204 (make-local-variable 'comment-end-skip)
205 (setq comment-end-skip "[ \t]*\\(\n\\|\\*+/\\)")
206 (make-local-variable 'comment-column)
207 (setq comment-column 48))
208
209 (defvar prolog-mode-map
210 (let ((map (make-sparse-keymap)))
211 (define-key map "\e\C-x" 'prolog-consult-region)
212 (define-key map "\C-c\C-l" 'inferior-prolog-load-file)
213 (define-key map "\C-c\C-z" 'switch-to-prolog)
214 map))
215
216 (easy-menu-define prolog-mode-menu prolog-mode-map "Menu for Prolog mode."
217 ;; Mostly copied from scheme-mode's menu.
218 ;; Not tremendously useful, but it's a start.
219 '("Prolog"
220 ["Indent line" indent-according-to-mode t]
221 ["Indent region" indent-region t]
222 ["Comment region" comment-region t]
223 ["Uncomment region" uncomment-region t]
224 "--"
225 ["Run interactive Prolog session" run-prolog t]
226 ))
227
228 ;;;###autoload
229 (define-derived-mode prolog-mode prog-mode "Prolog"
230 "Major mode for editing Prolog code for Prologs.
231 Blank lines and `%%...' separate paragraphs. `%'s start comments.
232 Commands:
233 \\{prolog-mode-map}
234 Entry to this mode calls the value of `prolog-mode-hook'
235 if that value is non-nil."
236 (prolog-mode-variables)
237 (set (make-local-variable 'comment-add) 1)
238 (setq font-lock-defaults '(prolog-font-lock-keywords
239 nil nil nil
240 beginning-of-line)))
241
242 (defun end-of-prolog-clause ()
243 "Go to end of clause in this line."
244 (beginning-of-line 1)
245 (let* ((eolpos (save-excursion (end-of-line) (point))))
246 (if (re-search-forward comment-start-skip eolpos 'move)
247 (goto-char (match-beginning 0)))
248 (skip-chars-backward " \t")))
249 \f
250 ;;;
251 ;;; Inferior prolog mode
252 ;;;
253 (defvar inferior-prolog-mode-map
254 (let ((map (make-sparse-keymap)))
255 ;; This map will inherit from `comint-mode-map' when entering
256 ;; inferior-prolog-mode.
257 (define-key map [remap self-insert-command]
258 'inferior-prolog-self-insert-command)
259 map))
260
261 (defvar inferior-prolog-mode-syntax-table prolog-mode-syntax-table)
262 (defvar inferior-prolog-mode-abbrev-table prolog-mode-abbrev-table)
263
264 (defvar inferior-prolog-error-regexp-alist
265 ;; GNU Prolog used to not follow the GNU standard format.
266 '(("^\\(.*?\\):\\([0-9]+\\) error: .*(char:\\([0-9]+\\)" 1 2 3)
267 gnu))
268
269 (declare-function comint-mode "comint")
270 (declare-function comint-send-string "comint" (process string))
271 (declare-function comint-send-region "comint" (process start end))
272 (declare-function comint-send-eof "comint" ())
273 (defvar compilation-error-regexp-alist)
274
275 (define-derived-mode inferior-prolog-mode comint-mode "Inferior Prolog"
276 "Major mode for interacting with an inferior Prolog process.
277
278 The following commands are available:
279 \\{inferior-prolog-mode-map}
280
281 Entry to this mode calls the value of `prolog-mode-hook' with no arguments,
282 if that value is non-nil. Likewise with the value of `comint-mode-hook'.
283 `prolog-mode-hook' is called after `comint-mode-hook'.
284
285 You can send text to the inferior Prolog from other buffers using the commands
286 `process-send-region', `process-send-string' and \\[prolog-consult-region].
287
288 Commands:
289 Tab indents for Prolog; with argument, shifts rest
290 of expression rigidly with the current line.
291 Paragraphs are separated only by blank lines and '%%'.
292 '%'s start comments.
293
294 Return at end of buffer sends line as input.
295 Return not at end copies rest of line to end and sends it.
296 \\[comint-kill-input] and \\[backward-kill-word] are kill commands, imitating normal Unix input editing.
297 \\[comint-interrupt-subjob] interrupts the shell or its current subjob if any.
298 \\[comint-stop-subjob] stops. \\[comint-quit-subjob] sends quit signal."
299 (setq comint-prompt-regexp "^| [ ?][- ] *")
300 (set (make-local-variable 'compilation-error-regexp-alist)
301 inferior-prolog-error-regexp-alist)
302 (compilation-shell-minor-mode)
303 (prolog-mode-variables))
304
305 (defvar inferior-prolog-buffer nil)
306
307 (defvar inferior-prolog-flavor 'unknown
308 "Either a symbol or a buffer position offset by one.
309 If a buffer position, the flavor has not been determined yet and
310 it is expected that the process's output has been or will
311 be inserted at that position plus one.")
312
313 (defun inferior-prolog-run (&optional name)
314 (with-current-buffer (make-comint "prolog" (or name prolog-program-name))
315 (inferior-prolog-mode)
316 (setq-default inferior-prolog-buffer (current-buffer))
317 (make-local-variable 'inferior-prolog-buffer)
318 (when (and name (not (equal name prolog-program-name)))
319 (set (make-local-variable 'prolog-program-name) name))
320 (set (make-local-variable 'inferior-prolog-flavor)
321 ;; Force re-detection.
322 (let* ((proc (get-buffer-process (current-buffer)))
323 (pmark (and proc (marker-position (process-mark proc)))))
324 (cond
325 ((null pmark) (1- (point-min)))
326 ;; The use of insert-before-markers in comint.el together with
327 ;; the potential use of comint-truncate-buffer in the output
328 ;; filter, means that it's difficult to reliably keep track of
329 ;; the buffer position where the process's output started.
330 ;; If possible we use a marker at "start - 1", so that
331 ;; insert-before-marker at `start' won't shift it. And if not,
332 ;; we fall back on using a plain integer.
333 ((> pmark (point-min)) (copy-marker (1- pmark)))
334 (t (1- pmark)))))
335 (add-hook 'comint-output-filter-functions
336 'inferior-prolog-guess-flavor nil t)))
337
338 (defun inferior-prolog-process (&optional dontstart)
339 (or (and (buffer-live-p inferior-prolog-buffer)
340 (get-buffer-process inferior-prolog-buffer))
341 (unless dontstart
342 (inferior-prolog-run)
343 ;; Try again.
344 (inferior-prolog-process))))
345
346 (defun inferior-prolog-guess-flavor (&optional ignored)
347 (save-excursion
348 (goto-char (1+ inferior-prolog-flavor))
349 (setq inferior-prolog-flavor
350 (cond
351 ((looking-at "GNU Prolog") 'gnu)
352 ((looking-at "Welcome to SWI-Prolog") 'swi)
353 ((looking-at ".*\n") 'unknown) ;There's at least one line.
354 (t inferior-prolog-flavor))))
355 (when (symbolp inferior-prolog-flavor)
356 (remove-hook 'comint-output-filter-functions
357 'inferior-prolog-guess-flavor t)
358 (if (eq inferior-prolog-flavor 'gnu)
359 (set (make-local-variable 'comint-process-echoes) t))))
360
361 ;;;###autoload
362 (defalias 'run-prolog 'switch-to-prolog)
363 ;;;###autoload
364 (defun switch-to-prolog (&optional name)
365 "Run an inferior Prolog process, input and output via buffer *prolog*.
366 With prefix argument \\[universal-prefix], prompt for the program to use."
367 (interactive
368 (list (when current-prefix-arg
369 (let ((proc (inferior-prolog-process 'dontstart)))
370 (if proc
371 (if (yes-or-no-p "Kill current process before starting new one? ")
372 (kill-process proc)
373 (error "Abort")))
374 (read-string "Run Prolog: " prolog-program-name)))))
375 (unless (inferior-prolog-process 'dontstart)
376 (inferior-prolog-run name))
377 (pop-to-buffer inferior-prolog-buffer))
378
379 (defun inferior-prolog-self-insert-command ()
380 "Insert the char in the buffer or pass it directly to the process."
381 (interactive)
382 (let* ((proc (get-buffer-process (current-buffer)))
383 (pmark (and proc (marker-position (process-mark proc)))))
384 (if (and (eq inferior-prolog-flavor 'gnu)
385 pmark
386 (null current-prefix-arg)
387 (eobp)
388 (eq (point) pmark)
389 (save-excursion
390 (goto-char (- pmark 3))
391 (looking-at " \\? ")))
392 ;; This is GNU prolog waiting to know whether you want more answers
393 ;; or not (or abort, etc...). The answer is a single char, not
394 ;; a line, so pass this char directly rather than wait for RET to
395 ;; send a whole line.
396 (comint-send-string proc (string last-command-event))
397 (call-interactively 'self-insert-command))))
398
399 (defun prolog-consult-region (compile beg end)
400 "Send the region to the Prolog process made by \"M-x run-prolog\".
401 If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode."
402 (interactive "P\nr")
403 (let ((proc (inferior-prolog-process)))
404 (comint-send-string proc
405 (if compile prolog-compile-string
406 prolog-consult-string))
407 (comint-send-region proc beg end)
408 (comint-send-string proc "\n") ;May be unnecessary
409 (if prolog-eof-string
410 (comint-send-string proc prolog-eof-string)
411 (with-current-buffer (process-buffer proc)
412 (comint-send-eof))))) ;Send eof to prolog process.
413
414 (defun prolog-consult-region-and-go (compile beg end)
415 "Send the region to the inferior Prolog, and switch to *prolog* buffer.
416 If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode."
417 (interactive "P\nr")
418 (prolog-consult-region compile beg end)
419 (pop-to-buffer inferior-prolog-buffer))
420
421 ;; inferior-prolog-mode uses the autoloaded compilation-shell-minor-mode.
422 (declare-function compilation-forget-errors "compile" ())
423
424 (defun inferior-prolog-load-file ()
425 "Pass the current buffer's file to the inferior prolog process."
426 (interactive)
427 (save-buffer)
428 (let ((file buffer-file-name)
429 (proc (inferior-prolog-process)))
430 (with-current-buffer (process-buffer proc)
431 (compilation-forget-errors)
432 (comint-send-string proc (concat "['" (file-relative-name file) "'].\n"))
433 (pop-to-buffer (current-buffer)))))
434
435 (provide 'prolog)
436
437 ;; arch-tag: f3ec6748-1272-4ab6-8826-c50cb1607636
438 ;;; prolog.el ends here