0
|
1 ;;; prolog.el --- major mode for editing and running Prolog under Emacs
|
|
2
|
|
3 ;; Copyright (C) 1986, 1987, 1993 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
|
|
6 ;; Keywords: languages
|
|
7
|
|
8 ;; This file is part of XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; 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.
|
|
14
|
|
15 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
16
|
21 ;; along with XEmacs; 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.
|
0
|
24
|
2
|
25 ;;; Synched up with: Not synched with FSF, we appear to have a newer version
|
0
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; This package provides a major mode for editing Prolog. It knows
|
|
30 ;; about Prolog syntax and comments, and can send regions to an inferior
|
|
31 ;; Prolog interpreter process.
|
|
32
|
|
33 ;;; Code:
|
|
34
|
|
35 (defvar prolog-mode-syntax-table nil)
|
|
36 (defvar prolog-mode-abbrev-table nil)
|
|
37 (defvar prolog-mode-map nil)
|
|
38
|
|
39 (defvar prolog-program-name "prolog"
|
|
40 "*Program name for invoking an inferior Prolog with `run-prolog'.")
|
|
41
|
|
42 (defvar prolog-consult-string "reconsult(user).\n"
|
|
43 "*(Re)Consult mode (for C-Prolog and Quintus Prolog). ")
|
|
44
|
|
45 (defvar prolog-compile-string "compile(user).\n"
|
|
46 "*Compile mode (for Quintus Prolog).")
|
|
47
|
|
48 (defvar prolog-eof-string "end_of_file.\n"
|
|
49 "*String that represents end of file for prolog.
|
|
50 nil means send actual operating system end of file.")
|
|
51
|
|
52 (defvar prolog-indent-width 4)
|
|
53
|
|
54 (defconst prolog-font-lock-keywords (purecopy
|
|
55 (list
|
|
56 (cons (concat
|
|
57 "[( \t]\\("
|
|
58 (mapconcat 'identity
|
|
59 '("write" "writeq" "nl" "is" "call" "read" "get" "get0"
|
|
60 "tell" "told" "open" "close" "format" "put"
|
|
61 "assert" "asserta" "assertz"
|
|
62 "retract" "retractall" "clause"
|
|
63 "record" "recorda" "abolish"
|
|
64 "setof" "bagof" "findall" "sort" "compare"
|
|
65 "var" "nonvar" "integer" "float" "number" "ground"
|
|
66 "atom" "atomic" "simple" "callable" "compound"
|
|
67 "functor" "arg" "copy_term" "numbervars"
|
|
68 "atom_chars" "number_chars" "atom_to_chars"
|
|
69 "length" "unix" "halt"
|
|
70 "op" "dynamic" "meta_predicate" "raise_exception"
|
|
71 "module" "ensure_loaded" "use_module"
|
|
72 "fail" "true"
|
|
73 "module_interface" "begin_module" "define_struct"
|
|
74 "import" "export" "global" "tool" "external"
|
|
75 "define_macro" "local" "library" "from"
|
|
76 )
|
|
77 "\\|")
|
|
78 "\\)[ .,\t\n()]")
|
|
79 1)
|
|
80 '("^[a-z][a-zA-Z0-9_]+" 0 font-lock-function-name-face)
|
|
81 ; '("[@!$#]" 0 font-lock-function-name-face)
|
|
82 '("!" 0 font-lock-function-name-face)
|
|
83 '("@" 0 font-lock-function-name-face)
|
|
84 '("<=>" 0 font-lock-function-name-face)
|
|
85 '("==>" 0 font-lock-function-name-face)
|
|
86 '(";" 0 font-lock-function-name-face)
|
|
87 '(":-" 0 font-lock-function-name-face)
|
|
88 '("\\[" 0 font-lock-keyword-face)
|
|
89 '("\\]" 0 font-lock-keyword-face)
|
|
90 '("|" 0 font-lock-keyword-face)
|
|
91 ))
|
|
92 "Additional expressions to highlight in Prolog mode.")
|
|
93
|
|
94 (if prolog-mode-syntax-table
|
|
95 ()
|
|
96 (let ((table (make-syntax-table)))
|
|
97 (modify-syntax-entry ?/ ". 14" table)
|
|
98 (modify-syntax-entry ?* ". 23" table)
|
|
99 (modify-syntax-entry ?% "< b" table)
|
|
100 (modify-syntax-entry ?\n "> b" table)
|
|
101 (modify-syntax-entry ?_ "w" table)
|
|
102 (modify-syntax-entry ?\\ "\\" table)
|
|
103 (modify-syntax-entry ?\' "\"" table)
|
|
104 (modify-syntax-entry ?+ "." table)
|
|
105 (modify-syntax-entry ?- "." table)
|
|
106 (modify-syntax-entry ?= "." table)
|
|
107 (modify-syntax-entry ?< "." table)
|
|
108 (modify-syntax-entry ?> "." table)
|
|
109 (setq prolog-mode-syntax-table table)))
|
|
110
|
|
111 (define-abbrev-table 'prolog-mode-abbrev-table ())
|
|
112
|
|
113 (defun prolog-mode-variables ()
|
|
114 (set-syntax-table prolog-mode-syntax-table)
|
|
115 (setq local-abbrev-table prolog-mode-abbrev-table)
|
|
116 (make-local-variable 'paragraph-start)
|
|
117 (setq paragraph-start (concat "%%\\|$\\|" page-delimiter)) ;'%%..'
|
|
118 (make-local-variable 'paragraph-separate)
|
|
119 (setq paragraph-separate paragraph-start)
|
|
120 (make-local-variable 'paragraph-ignore-fill-prefix)
|
|
121 (setq paragraph-ignore-fill-prefix t)
|
|
122 (make-local-variable 'indent-line-function)
|
|
123 (setq indent-line-function 'prolog-indent-line)
|
|
124 (make-local-variable 'comment-start)
|
|
125 (setq comment-start "%")
|
|
126 (make-local-variable 'comment-start-skip)
|
|
127 (setq comment-start-skip "%+ *")
|
|
128 (make-local-variable 'comment-column)
|
|
129 (setq comment-column 48)
|
|
130 (make-local-variable 'comment-indent-function)
|
|
131 (setq comment-indent-function 'prolog-comment-indent))
|
|
132
|
|
133 (defun prolog-mode-commands (map)
|
|
134 (define-key map "\t" 'prolog-indent-line)
|
|
135 (define-key map "\e\C-x" 'prolog-consult-region))
|
|
136
|
|
137 (if prolog-mode-map
|
|
138 nil
|
|
139 (setq prolog-mode-map (make-sparse-keymap))
|
|
140 (prolog-mode-commands prolog-mode-map))
|
|
141
|
|
142 ;;;###autoload
|
|
143 (defun prolog-mode ()
|
|
144 "Major mode for editing Prolog code for Prologs.
|
|
145 Blank lines and `%%...' separate paragraphs. `%'s start comments.
|
|
146 Commands:
|
|
147 \\{prolog-mode-map}
|
|
148 Entry to this mode calls the value of `prolog-mode-hook'
|
|
149 if that value is non-nil."
|
|
150 (interactive)
|
|
151 (kill-all-local-variables)
|
|
152 (use-local-map prolog-mode-map)
|
|
153 (setq major-mode 'prolog-mode)
|
|
154 (setq mode-name "Prolog")
|
|
155 (prolog-mode-variables)
|
|
156 (run-hooks 'prolog-mode-hook))
|
|
157
|
|
158 (defun prolog-indent-line (&optional whole-exp)
|
|
159 "Indent current line as Prolog code.
|
|
160 With argument, indent any additional lines of the same clause
|
|
161 rigidly along with this one (not yet)."
|
|
162 (interactive "p")
|
|
163 (let ((indent (prolog-indent-level))
|
|
164 (pos (- (point-max) (point))) beg)
|
|
165 (beginning-of-line)
|
|
166 (setq beg (point))
|
|
167 (skip-chars-forward " \t")
|
|
168 (if (zerop (- indent (current-column)))
|
|
169 nil
|
|
170 (delete-region beg (point))
|
|
171 (indent-to indent))
|
|
172 (if (> (- (point-max) pos) (point))
|
|
173 (goto-char (- (point-max) pos)))
|
|
174 ))
|
|
175
|
|
176 (defun prolog-indent-level ()
|
|
177 "Compute prolog indentation level."
|
|
178 (save-excursion
|
|
179 (beginning-of-line)
|
|
180 (skip-chars-forward " \t")
|
|
181 (cond
|
|
182 ((looking-at "%%%") 0) ;Large comment starts
|
|
183 ((looking-at "%[^%]") comment-column) ;Small comment starts
|
|
184 ((bobp) 0) ;Beginning of buffer
|
|
185 (t
|
|
186 (let ((empty t) ind more less)
|
|
187 (if (looking-at ")")
|
|
188 (setq less t) ;Find close
|
|
189 (setq less nil))
|
|
190 ;; See previous indentation
|
|
191 (while empty
|
|
192 (forward-line -1)
|
|
193 (beginning-of-line)
|
|
194 (if (bobp)
|
|
195 (setq empty nil)
|
|
196 (skip-chars-forward " \t")
|
|
197 (if (not (or (looking-at "%[^%]") (looking-at "\n")))
|
|
198 (setq empty nil))))
|
|
199 (if (bobp)
|
|
200 (setq ind 0) ;Beginning of buffer
|
|
201 (setq ind (current-column))) ;Beginning of clause
|
|
202 ;; See its beginning
|
|
203 (if (looking-at "%%[^%]")
|
|
204 ind
|
|
205 ;; Real prolog code
|
|
206 (if (looking-at "(")
|
|
207 (setq more t) ;Find open
|
|
208 (setq more nil))
|
|
209 ;; See its tail
|
|
210 (end-of-prolog-clause)
|
|
211 (or (bobp) (forward-char -1))
|
|
212 (cond ((looking-at "[,(;>]")
|
|
213 (if (and more (looking-at "[^,]"))
|
|
214 (+ ind prolog-indent-width) ;More indentation
|
|
215 (max tab-width ind))) ;Same indentation
|
|
216 ((looking-at "-") tab-width) ;TAB
|
|
217 ((or less (looking-at "[^.]"))
|
|
218 (max (- ind prolog-indent-width) 0)) ;Less indentation
|
|
219 (t 0)) ;No indentation
|
|
220 )))
|
|
221 )))
|
|
222
|
|
223 (defun end-of-prolog-clause ()
|
|
224 "Go to end of clause in this line."
|
|
225 (beginning-of-line 1)
|
|
226 (let* ((eolpos (save-excursion (end-of-line) (point))))
|
|
227 (if (re-search-forward comment-start-skip eolpos 'move)
|
|
228 (goto-char (match-beginning 0)))
|
|
229 (skip-chars-backward " \t")))
|
|
230
|
|
231 (defun prolog-comment-indent ()
|
|
232 "Compute prolog comment indentation."
|
|
233 (cond ((looking-at "%%%") 0)
|
|
234 ((looking-at "%%") (prolog-indent-level))
|
|
235 (t
|
|
236 (save-excursion
|
|
237 (skip-chars-backward " \t")
|
|
238 ;; Insert one space at least, except at left margin.
|
|
239 (max (+ (current-column) (if (bolp) 0 1))
|
|
240 comment-column)))
|
|
241 ))
|
|
242
|
|
243
|
|
244 ;;;
|
|
245 ;;; Inferior prolog mode
|
|
246 ;;;
|
|
247 (defvar inferior-prolog-mode-map nil)
|
|
248
|
|
249 ;;;###autoload
|
|
250 (defun inferior-prolog-mode ()
|
|
251 "Major mode for interacting with an inferior Prolog process.
|
|
252
|
|
253 The following commands are available:
|
|
254 \\{inferior-prolog-mode-map}
|
|
255
|
|
256 Entry to this mode calls the value of `prolog-mode-hook' with no arguments,
|
|
257 if that value is non-nil. Likewise with the value of `comint-mode-hook'.
|
|
258 `prolog-mode-hook' is called after `comint-mode-hook'.
|
|
259
|
|
260 You can send text to the inferior Prolog from other buffers
|
|
261 using the commands `send-region', `send-string' and \\[prolog-consult-region].
|
|
262
|
|
263 Commands:
|
|
264 Tab indents for Prolog; with argument, shifts rest
|
|
265 of expression rigidly with the current line.
|
|
266 Paragraphs are separated only by blank lines and '%%'.
|
|
267 '%'s start comments.
|
|
268
|
|
269 Return at end of buffer sends line as input.
|
|
270 Return not at end copies rest of line to end and sends it.
|
|
271 \\[comint-kill-input] and \\[backward-kill-word] are kill commands, imitating normal Unix input editing.
|
|
272 \\[comint-interrupt-subjob] interrupts the shell or its current subjob if any.
|
|
273 \\[comint-stop-subjob] stops. \\[comint-quit-subjob] sends quit signal."
|
|
274 (interactive)
|
|
275 (require 'comint)
|
|
276 (comint-mode)
|
|
277 (setq major-mode 'inferior-prolog-mode
|
|
278 mode-name "Inferior Prolog"
|
|
279 comint-prompt-regexp "^| [ ?][- ] *")
|
|
280 (prolog-mode-variables)
|
|
281 (if inferior-prolog-mode-map nil
|
|
282 (setq inferior-prolog-mode-map (copy-keymap comint-mode-map))
|
|
283 (prolog-mode-commands inferior-prolog-mode-map))
|
|
284 (use-local-map inferior-prolog-mode-map)
|
|
285 (run-hooks 'prolog-mode-hook))
|
|
286
|
|
287 ;;;###autoload
|
|
288 (defun run-prolog ()
|
|
289 "Run an inferior Prolog process, input and output via buffer *prolog*."
|
|
290 (interactive)
|
|
291 (require 'comint)
|
|
292 (switch-to-buffer (make-comint "prolog" prolog-program-name))
|
|
293 (inferior-prolog-mode))
|
|
294
|
|
295 (defun prolog-consult-region (compile beg end)
|
|
296 "Send the region to the Prolog process made by \"M-x run-prolog\".
|
|
297 If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode."
|
|
298 (interactive "P\nr")
|
|
299 (save-excursion
|
|
300 (if compile
|
|
301 (send-string "prolog" prolog-compile-string)
|
|
302 (send-string "prolog" prolog-consult-string))
|
|
303 (send-region "prolog" beg end)
|
|
304 (send-string "prolog" "\n") ;May be unnecessary
|
|
305 (if prolog-eof-string
|
|
306 (send-string "prolog" prolog-eof-string)
|
|
307 (process-send-eof "prolog")))) ;Send eof to prolog process.
|
|
308
|
|
309 (defun prolog-consult-region-and-go (compile beg end)
|
|
310 "Send the region to the inferior Prolog, and switch to *prolog* buffer.
|
|
311 If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode."
|
|
312 (interactive "P\nr")
|
|
313 (prolog-consult-region compile beg end)
|
|
314 (switch-to-buffer "*prolog*"))
|
|
315
|
|
316 ;;; prolog.el ends here
|