Add defgroup; use defcustom for user vars.
[emacs.git] / lisp / progmodes / prolog.el
blob3bb39760ec8dd5bf6a58c6e5ab84ef71b5eaf90a
1 ;;; prolog.el --- major mode for editing and running Prolog under Emacs
3 ;; Copyright (C) 1986, 1987 Free Software Foundation, Inc.
5 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: languages
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This package provides a major mode for editing Prolog. It knows
28 ;; about Prolog syntax and comments, and can send regions to an inferior
29 ;; Prolog interpreter process.
31 ;;; Code:
33 (defvar prolog-mode-syntax-table nil)
34 (defvar prolog-mode-abbrev-table nil)
35 (defvar prolog-mode-map nil)
37 (defgroup prolog nil
38 "Major mode for editing and running Prolog under Emacs"
39 :group 'languages)
42 (defcustom prolog-program-name "prolog"
43 "*Program name for invoking an inferior Prolog with `run-prolog'."
44 :type 'string
45 :group 'prolog)
47 (defcustom prolog-consult-string "reconsult(user).\n"
48 "*(Re)Consult mode (for C-Prolog and Quintus Prolog). "
49 :type 'string
50 :group 'prolog)
52 (defcustom prolog-compile-string "compile(user).\n"
53 "*Compile mode (for Quintus Prolog)."
54 :type 'string
55 :group 'prolog)
57 (defcustom prolog-eof-string "end_of_file.\n"
58 "*String that represents end of file for prolog.
59 nil means send actual operating system end of file."
60 :type 'string
61 :group 'prolog)
63 (defcustom prolog-indent-width 4
64 "Level of indentation in Prolog buffers."
65 :type 'integer
66 :group 'prolog)
68 (if prolog-mode-syntax-table
70 (let ((table (make-syntax-table)))
71 (modify-syntax-entry ?_ "w" table)
72 (modify-syntax-entry ?\\ "\\" table)
73 (modify-syntax-entry ?/ "." table)
74 (modify-syntax-entry ?* "." table)
75 (modify-syntax-entry ?+ "." table)
76 (modify-syntax-entry ?- "." table)
77 (modify-syntax-entry ?= "." table)
78 (modify-syntax-entry ?% "<" table)
79 (modify-syntax-entry ?\n ">" table)
80 (modify-syntax-entry ?< "." table)
81 (modify-syntax-entry ?> "." table)
82 (modify-syntax-entry ?\' "\"" table)
83 (setq prolog-mode-syntax-table table)))
85 (define-abbrev-table 'prolog-mode-abbrev-table ())
87 (defun prolog-mode-variables ()
88 (set-syntax-table prolog-mode-syntax-table)
89 (setq local-abbrev-table prolog-mode-abbrev-table)
90 (make-local-variable 'paragraph-start)
91 (setq paragraph-start (concat "%%\\|$\\|" page-delimiter)) ;'%%..'
92 (make-local-variable 'paragraph-separate)
93 (setq paragraph-separate paragraph-start)
94 (make-local-variable 'paragraph-ignore-fill-prefix)
95 (setq paragraph-ignore-fill-prefix t)
96 (make-local-variable 'indent-line-function)
97 (setq indent-line-function 'prolog-indent-line)
98 (make-local-variable 'comment-start)
99 (setq comment-start "%")
100 (make-local-variable 'comment-start-skip)
101 (setq comment-start-skip "%+ *")
102 (make-local-variable 'comment-column)
103 (setq comment-column 48)
104 (make-local-variable 'comment-indent-function)
105 (setq comment-indent-function 'prolog-comment-indent))
107 (defun prolog-mode-commands (map)
108 (define-key map "\t" 'prolog-indent-line)
109 (define-key map "\e\C-x" 'prolog-consult-region))
111 (if prolog-mode-map
113 (setq prolog-mode-map (make-sparse-keymap))
114 (prolog-mode-commands prolog-mode-map))
116 ;;;###autoload
117 (defun prolog-mode ()
118 "Major mode for editing Prolog code for Prologs.
119 Blank lines and `%%...' separate paragraphs. `%'s start comments.
120 Commands:
121 \\{prolog-mode-map}
122 Entry to this mode calls the value of `prolog-mode-hook'
123 if that value is non-nil."
124 (interactive)
125 (kill-all-local-variables)
126 (use-local-map prolog-mode-map)
127 (setq major-mode 'prolog-mode)
128 (setq mode-name "Prolog")
129 (prolog-mode-variables)
130 (run-hooks 'prolog-mode-hook))
132 (defun prolog-indent-line (&optional whole-exp)
133 "Indent current line as Prolog code.
134 With argument, indent any additional lines of the same clause
135 rigidly along with this one (not yet)."
136 (interactive "p")
137 (let ((indent (prolog-indent-level))
138 (pos (- (point-max) (point))) beg)
139 (beginning-of-line)
140 (setq beg (point))
141 (skip-chars-forward " \t")
142 (if (zerop (- indent (current-column)))
144 (delete-region beg (point))
145 (indent-to indent))
146 (if (> (- (point-max) pos) (point))
147 (goto-char (- (point-max) pos)))
150 (defun prolog-indent-level ()
151 "Compute prolog indentation level."
152 (save-excursion
153 (beginning-of-line)
154 (skip-chars-forward " \t")
155 (cond
156 ((looking-at "%%%") 0) ;Large comment starts
157 ((looking-at "%[^%]") comment-column) ;Small comment starts
158 ((bobp) 0) ;Beginning of buffer
160 (let ((empty t) ind more less)
161 (if (looking-at ")")
162 (setq less t) ;Find close
163 (setq less nil))
164 ;; See previous indentation
165 (while empty
166 (forward-line -1)
167 (beginning-of-line)
168 (if (bobp)
169 (setq empty nil)
170 (skip-chars-forward " \t")
171 (if (not (or (looking-at "%[^%]") (looking-at "\n")))
172 (setq empty nil))))
173 (if (bobp)
174 (setq ind 0) ;Beginning of buffer
175 (setq ind (current-column))) ;Beginning of clause
176 ;; See its beginning
177 (if (looking-at "%%[^%]")
179 ;; Real prolog code
180 (if (looking-at "(")
181 (setq more t) ;Find open
182 (setq more nil))
183 ;; See its tail
184 (end-of-prolog-clause)
185 (or (bobp) (forward-char -1))
186 (cond ((looking-at "[,(;>]")
187 (if (and more (looking-at "[^,]"))
188 (+ ind prolog-indent-width) ;More indentation
189 (max tab-width ind))) ;Same indentation
190 ((looking-at "-") tab-width) ;TAB
191 ((or less (looking-at "[^.]"))
192 (max (- ind prolog-indent-width) 0)) ;Less indentation
193 (t 0)) ;No indentation
197 (defun end-of-prolog-clause ()
198 "Go to end of clause in this line."
199 (beginning-of-line 1)
200 (let* ((eolpos (save-excursion (end-of-line) (point))))
201 (if (re-search-forward comment-start-skip eolpos 'move)
202 (goto-char (match-beginning 0)))
203 (skip-chars-backward " \t")))
205 (defun prolog-comment-indent ()
206 "Compute prolog comment indentation."
207 (cond ((looking-at "%%%") 0)
208 ((looking-at "%%") (prolog-indent-level))
210 (save-excursion
211 (skip-chars-backward " \t")
212 ;; Insert one space at least, except at left margin.
213 (max (+ (current-column) (if (bolp) 0 1))
214 comment-column)))
219 ;;; Inferior prolog mode
221 (defvar inferior-prolog-mode-map nil)
223 (defun inferior-prolog-mode ()
224 "Major mode for interacting with an inferior Prolog process.
226 The following commands are available:
227 \\{inferior-prolog-mode-map}
229 Entry to this mode calls the value of `prolog-mode-hook' with no arguments,
230 if that value is non-nil. Likewise with the value of `comint-mode-hook'.
231 `prolog-mode-hook' is called after `comint-mode-hook'.
233 You can send text to the inferior Prolog from other buffers
234 using the commands `send-region', `send-string' and \\[prolog-consult-region].
236 Commands:
237 Tab indents for Prolog; with argument, shifts rest
238 of expression rigidly with the current line.
239 Paragraphs are separated only by blank lines and '%%'.
240 '%'s start comments.
242 Return at end of buffer sends line as input.
243 Return not at end copies rest of line to end and sends it.
244 \\[comint-kill-input] and \\[backward-kill-word] are kill commands, imitating normal Unix input editing.
245 \\[comint-interrupt-subjob] interrupts the shell or its current subjob if any.
246 \\[comint-stop-subjob] stops. \\[comint-quit-subjob] sends quit signal."
247 (interactive)
248 (require 'comint)
249 (comint-mode)
250 (setq major-mode 'inferior-prolog-mode
251 mode-name "Inferior Prolog"
252 comint-prompt-regexp "^| [ ?][- ] *")
253 (prolog-mode-variables)
254 (if inferior-prolog-mode-map nil
255 (setq inferior-prolog-mode-map (copy-keymap comint-mode-map))
256 (prolog-mode-commands inferior-prolog-mode-map))
257 (use-local-map inferior-prolog-mode-map)
258 (run-hooks 'prolog-mode-hook))
260 ;;;###autoload
261 (defun run-prolog ()
262 "Run an inferior Prolog process, input and output via buffer *prolog*."
263 (interactive)
264 (require 'comint)
265 (switch-to-buffer (make-comint "prolog" prolog-program-name))
266 (inferior-prolog-mode))
268 (defun prolog-consult-region (compile beg end)
269 "Send the region to the Prolog process made by \"M-x run-prolog\".
270 If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode."
271 (interactive "P\nr")
272 (save-excursion
273 (if compile
274 (send-string "prolog" prolog-compile-string)
275 (send-string "prolog" prolog-consult-string))
276 (send-region "prolog" beg end)
277 (send-string "prolog" "\n") ;May be unnecessary
278 (if prolog-eof-string
279 (send-string "prolog" prolog-eof-string)
280 (process-send-eof "prolog")))) ;Send eof to prolog process.
282 (defun prolog-consult-region-and-go (compile beg end)
283 "Send the region to the inferior Prolog, and switch to *prolog* buffer.
284 If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode."
285 (interactive "P\nr")
286 (prolog-consult-region compile beg end)
287 (switch-to-buffer "*prolog*"))
289 ;;; prolog.el ends here