]> code.delx.au - gnu-emacs/blob - lisp/progmodes/prolog.el
Remove * in docstrings.
[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
4 ;; 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 2, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; This package provides a major mode for editing Prolog. It knows
29 ;; about Prolog syntax and comments, and can send regions to an inferior
30 ;; Prolog interpreter process. Font locking is tuned towards GNU Prolog.
31
32 ;;; Code:
33
34 (defvar comint-prompt-regexp)
35
36
37 (defgroup prolog nil
38 "Major mode for editing and running Prolog under Emacs."
39 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
40 :group 'languages)
41
42
43 (defcustom prolog-program-name
44 (let ((names '("prolog" "gprolog" "swipl")))
45 (while (and names
46 (not (executable-find (car names))))
47 (setq names (cdr names)))
48 (or (car names) "prolog"))
49 "Program name for invoking an inferior Prolog with `run-prolog'."
50 :type 'string
51 :group 'prolog)
52
53 (defcustom prolog-consult-string "reconsult(user).\n"
54 "(Re)Consult mode (for C-Prolog and Quintus Prolog). "
55 :type 'string
56 :group 'prolog)
57
58 (defcustom prolog-compile-string "compile(user).\n"
59 "Compile mode (for Quintus Prolog)."
60 :type 'string
61 :group 'prolog)
62
63 (defcustom prolog-eof-string "end_of_file.\n"
64 "String that represents end of file for Prolog.
65 When nil, send actual operating system end of file."
66 :type 'string
67 :group 'prolog)
68
69 (defcustom prolog-indent-width 4
70 "Level of indentation in Prolog buffers."
71 :type 'integer
72 :group 'prolog)
73
74 (defvar prolog-font-lock-keywords
75 '(("\\(#[<=]=>\\|:-\\)\\|\\(#=\\)\\|\\(#[#<>\\/][=\\/]*\\|!\\)"
76 0 font-lock-keyword-face)
77 ("\\<\\(is\\|write\\|nl\\|read_\\sw+\\)\\>"
78 1 font-lock-keyword-face)
79 ("^\\(\\sw+\\)\\s-*\\((\\(.+\\))\\)*"
80 (1 font-lock-function-name-face)
81 (3 font-lock-variable-name-face)))
82 "Font-lock keywords for Prolog mode.")
83
84 (defvar prolog-mode-syntax-table
85 (let ((table (make-syntax-table)))
86 (modify-syntax-entry ?_ "w" table)
87 (modify-syntax-entry ?\\ "\\" table)
88 (modify-syntax-entry ?/ ". 14" table)
89 (modify-syntax-entry ?* ". 23" table)
90 (modify-syntax-entry ?+ "." table)
91 (modify-syntax-entry ?- "." table)
92 (modify-syntax-entry ?= "." table)
93 (modify-syntax-entry ?% "<" table)
94 (modify-syntax-entry ?\n ">" table)
95 (modify-syntax-entry ?< "." table)
96 (modify-syntax-entry ?> "." table)
97 (modify-syntax-entry ?\' "\"" table)
98 table))
99
100 (defvar prolog-mode-abbrev-table nil)
101 (define-abbrev-table 'prolog-mode-abbrev-table ())
102
103 (defun prolog-mode-variables ()
104 (make-local-variable 'paragraph-separate)
105 (setq paragraph-separate (concat "%%\\|$\\|" page-delimiter)) ;'%%..'
106 (make-local-variable 'paragraph-ignore-fill-prefix)
107 (setq paragraph-ignore-fill-prefix t)
108 (make-local-variable 'imenu-generic-expression)
109 (setq imenu-generic-expression '((nil "^\\sw+" 0)))
110 (make-local-variable 'indent-line-function)
111 (setq indent-line-function 'prolog-indent-line)
112 (make-local-variable 'comment-start)
113 (setq comment-start "%")
114 (make-local-variable 'comment-start-skip)
115 (setq comment-start-skip "\\(?:%+\\|/\\*+\\)[ \t]*")
116 (make-local-variable 'comment-end-skip)
117 (setq comment-end-skip "[ \t]*\\(\n\\|\\*+/\\)")
118 (make-local-variable 'comment-column)
119 (setq comment-column 48))
120
121 (defvar prolog-mode-map
122 (let ((map (make-sparse-keymap)))
123 (define-key map "\e\C-x" 'prolog-consult-region)
124 (define-key map "\C-c\C-l" 'inferior-prolog-load-file)
125 (define-key map "\C-c\C-z" 'switch-to-prolog)
126 map))
127
128 (easy-menu-define prolog-mode-menu prolog-mode-map "Menu for Prolog mode."
129 ;; Mostly copied from scheme-mode's menu.
130 ;; Not tremendously useful, but it's a start.
131 '("Prolog"
132 ["Indent line" indent-according-to-mode t]
133 ["Indent region" indent-region t]
134 ["Comment region" comment-region t]
135 ["Uncomment region" uncomment-region t]
136 "--"
137 ["Run interactive Prolog session" run-prolog t]
138 ))
139
140 ;;;###autoload
141 (defun prolog-mode ()
142 "Major mode for editing Prolog code for Prologs.
143 Blank lines and `%%...' separate paragraphs. `%'s start comments.
144 Commands:
145 \\{prolog-mode-map}
146 Entry to this mode calls the value of `prolog-mode-hook'
147 if that value is non-nil."
148 (interactive)
149 (kill-all-local-variables)
150 (use-local-map prolog-mode-map)
151 (set-syntax-table prolog-mode-syntax-table)
152 (setq major-mode 'prolog-mode)
153 (setq mode-name "Prolog")
154 (prolog-mode-variables)
155 (set (make-local-variable 'comment-add) 1)
156 ;; font lock
157 (setq font-lock-defaults '(prolog-font-lock-keywords
158 nil nil nil
159 beginning-of-line))
160 (run-mode-hooks 'prolog-mode-hook))
161
162 (defun prolog-indent-line ()
163 "Indent current line as Prolog code.
164 With argument, indent any additional lines of the same clause
165 rigidly along with this one (not yet)."
166 (interactive "p")
167 (let ((indent (prolog-indent-level))
168 (pos (- (point-max) (point))))
169 (beginning-of-line)
170 (indent-line-to indent)
171 (if (> (- (point-max) pos) (point))
172 (goto-char (- (point-max) pos)))))
173
174 (defun prolog-indent-level ()
175 "Compute Prolog indentation level."
176 (save-excursion
177 (beginning-of-line)
178 (skip-chars-forward " \t")
179 (cond
180 ((looking-at "%%%") 0) ;Large comment starts
181 ((looking-at "%[^%]") comment-column) ;Small comment starts
182 ((bobp) 0) ;Beginning of buffer
183 (t
184 (let ((empty t) ind more less)
185 (if (looking-at ")")
186 (setq less t) ;Find close
187 (setq less nil))
188 ;; See previous indentation
189 (while empty
190 (forward-line -1)
191 (beginning-of-line)
192 (if (bobp)
193 (setq empty nil)
194 (skip-chars-forward " \t")
195 (if (not (or (looking-at "%[^%]") (looking-at "\n")))
196 (setq empty nil))))
197 (if (bobp)
198 (setq ind 0) ;Beginning of buffer
199 (setq ind (current-column))) ;Beginning of clause
200 ;; See its beginning
201 (if (looking-at "%%[^%]")
202 ind
203 ;; Real prolog code
204 (if (looking-at "(")
205 (setq more t) ;Find open
206 (setq more nil))
207 ;; See its tail
208 (end-of-prolog-clause)
209 (or (bobp) (forward-char -1))
210 (cond ((looking-at "[,(;>]")
211 (if (and more (looking-at "[^,]"))
212 (+ ind prolog-indent-width) ;More indentation
213 (max tab-width ind))) ;Same indentation
214 ((looking-at "-") tab-width) ;TAB
215 ((or less (looking-at "[^.]"))
216 (max (- ind prolog-indent-width) 0)) ;Less indentation
217 (t 0)) ;No indentation
218 )))
219 )))
220
221 (defun end-of-prolog-clause ()
222 "Go to end of clause in this line."
223 (beginning-of-line 1)
224 (let* ((eolpos (save-excursion (end-of-line) (point))))
225 (if (re-search-forward comment-start-skip eolpos 'move)
226 (goto-char (match-beginning 0)))
227 (skip-chars-backward " \t")))
228 \f
229 ;;;
230 ;;; Inferior prolog mode
231 ;;;
232 (defvar inferior-prolog-mode-map
233 (let ((map (make-sparse-keymap)))
234 ;; This map will inherit from `comint-mode-map' when entering
235 ;; inferior-prolog-mode.
236 map))
237
238 (defvar inferior-prolog-mode-syntax-table prolog-mode-syntax-table)
239 (defvar inferior-prolog-mode-abbrev-table prolog-mode-abbrev-table)
240
241 (define-derived-mode inferior-prolog-mode comint-mode "Inferior Prolog"
242 "Major mode for interacting with an inferior Prolog process.
243
244 The following commands are available:
245 \\{inferior-prolog-mode-map}
246
247 Entry to this mode calls the value of `prolog-mode-hook' with no arguments,
248 if that value is non-nil. Likewise with the value of `comint-mode-hook'.
249 `prolog-mode-hook' is called after `comint-mode-hook'.
250
251 You can send text to the inferior Prolog from other buffers using the commands
252 `process-send-region', `process-send-string' and \\[prolog-consult-region].
253
254 Commands:
255 Tab indents for Prolog; with argument, shifts rest
256 of expression rigidly with the current line.
257 Paragraphs are separated only by blank lines and '%%'.
258 '%'s start comments.
259
260 Return at end of buffer sends line as input.
261 Return not at end copies rest of line to end and sends it.
262 \\[comint-kill-input] and \\[backward-kill-word] are kill commands, imitating normal Unix input editing.
263 \\[comint-interrupt-subjob] interrupts the shell or its current subjob if any.
264 \\[comint-stop-subjob] stops. \\[comint-quit-subjob] sends quit signal."
265 (setq comint-prompt-regexp "^| [ ?][- ] *")
266 (prolog-mode-variables))
267
268 (defvar inferior-prolog-buffer nil)
269
270 (defun inferior-prolog-run (&optional name)
271 (with-current-buffer (make-comint "prolog" (or name prolog-program-name))
272 (inferior-prolog-mode)
273 (setq-default inferior-prolog-buffer (current-buffer))
274 (make-local-variable 'inferior-prolog-buffer)
275 (when (and name (not (equal name prolog-program-name)))
276 (set (make-local-variable 'prolog-program-name) name))
277 (set (make-local-variable 'inferior-prolog-flavor)
278 ;; Force re-detection.
279 (let* ((proc (get-buffer-process (current-buffer)))
280 (pmark (and proc (marker-position (process-mark proc)))))
281 (cond
282 ((null pmark) (1- (point-min)))
283 ;; The use of insert-before-markers in comint.el together with
284 ;; the potential use of comint-truncate-buffer in the output
285 ;; filter, means that it's difficult to reliably keep track of
286 ;; the buffer position where the process's output started.
287 ;; If possible we use a marker at "start - 1", so that
288 ;; insert-before-marker at `start' won't shift it. And if not,
289 ;; we fall back on using a plain integer.
290 ((> pmark (point-min)) (copy-marker (1- pmark)))
291 (t (1- pmark)))))
292 (add-hook 'comint-output-filter-functions
293 'inferior-prolog-guess-flavor nil t)))
294
295 (defun inferior-prolog-process (&optional dontstart)
296 (or (and (buffer-live-p inferior-prolog-buffer)
297 (get-buffer-process inferior-prolog-buffer))
298 (unless dontstart
299 (inferior-prolog-run)
300 ;; Try again.
301 (inferior-prolog-process))))
302
303 ;;;###autoload
304 (defalias 'run-prolog 'switch-to-prolog)
305 ;;;###autoload
306 (defun switch-to-prolog (&optional name)
307 "Run an inferior Prolog process, input and output via buffer *prolog*.
308 With prefix argument \\[universal-prefix], prompt for the program to use."
309 (interactive
310 (list (when current-prefix-arg
311 (let ((proc (inferior-prolog-process 'dontstart)))
312 (if proc
313 (if (yes-or-no-p "Kill current process before starting new one? ")
314 (kill-process proc)
315 (error "Abort")))
316 (read-string "Run Prolog: " prolog-program-name)))))
317 (unless (inferior-prolog-process 'dontstart)
318 (inferior-prolog-run name))
319 (pop-to-buffer inferior-prolog-buffer))
320
321 (defun prolog-consult-region (compile beg end)
322 "Send the region to the Prolog process made by \"M-x run-prolog\".
323 If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode."
324 (interactive "P\nr")
325 (let ((proc (inferior-prolog-process)))
326 (comint-send-string proc
327 (if compile prolog-compile-string
328 prolog-consult-string))
329 (comint-send-region proc beg end)
330 (comint-send-string proc "\n") ;May be unnecessary
331 (if prolog-eof-string
332 (comint-send-string proc prolog-eof-string)
333 (with-current-buffer (process-buffer proc)
334 (comint-send-eof))))) ;Send eof to prolog process.
335
336 (defun prolog-consult-region-and-go (compile beg end)
337 "Send the region to the inferior Prolog, and switch to *prolog* buffer.
338 If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode."
339 (interactive "P\nr")
340 (prolog-consult-region compile beg end)
341 (switch-to-buffer "*prolog*"))
342
343 (defun inferior-prolog-load-file ()
344 "Pass the current buffer's file to the inferior prolog process."
345 (interactive)
346 (save-buffer)
347 (let ((file buffer-file-name)
348 (proc (inferior-prolog-process)))
349 (with-current-buffer (process-buffer proc)
350 (comint-send-string proc (concat "['" (file-relative-name file) "'].\n"))
351 (pop-to-buffer (current-buffer)))))
352
353 (provide 'prolog)
354
355 ;; arch-tag: f3ec6748-1272-4ab6-8826-c50cb1607636
356 ;;; prolog.el ends here