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