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>
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)
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.
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.
33 (defvar prolog-mode-syntax-table nil)
34 (defvar prolog-mode-abbrev-table nil)
35 (defvar prolog-mode-map nil)
37 (defvar prolog-program-name "prolog"
38 "*Program name for invoking an inferior Prolog with `run-prolog'.")
40 (defvar prolog-consult-string "reconsult(user).\n"
41 "*(Re)Consult mode (for C-Prolog and Quintus Prolog). ")
43 (defvar prolog-compile-string "compile(user).\n"
44 "*Compile mode (for Quintus Prolog).")
46 (defvar prolog-eof-string "end_of_file.\n"
47 "*String that represents end of file for prolog.
48 nil means send actual operating system end of file.")
50 (defvar prolog-indent-width 4)
52 (if prolog-mode-syntax-table
54 (let ((table (make-syntax-table)))
55 (modify-syntax-entry ?_ "w" table)
56 (modify-syntax-entry ?\\ "\\" table)
57 (modify-syntax-entry ?/ "." table)
58 (modify-syntax-entry ?* "." table)
59 (modify-syntax-entry ?+ "." table)
60 (modify-syntax-entry ?- "." table)
61 (modify-syntax-entry ?= "." table)
62 (modify-syntax-entry ?% "<" table)
63 (modify-syntax-entry ?\n ">" table)
64 (modify-syntax-entry ?< "." table)
65 (modify-syntax-entry ?> "." table)
66 (modify-syntax-entry ?\' "\"" table)
67 (setq prolog-mode-syntax-table table)))
69 (define-abbrev-table 'prolog-mode-abbrev-table ())
71 (defun prolog-mode-variables ()
72 (set-syntax-table prolog-mode-syntax-table)
73 (setq local-abbrev-table prolog-mode-abbrev-table)
74 (make-local-variable 'paragraph-start)
75 (setq paragraph-start (concat "%%\\|$\\|" page-delimiter)) ;'%%..'
76 (make-local-variable 'paragraph-separate)
77 (setq paragraph-separate paragraph-start)
78 (make-local-variable 'paragraph-ignore-fill-prefix)
79 (setq paragraph-ignore-fill-prefix t)
80 (make-local-variable 'indent-line-function)
81 (setq indent-line-function 'prolog-indent-line)
82 (make-local-variable 'comment-start)
83 (setq comment-start "%")
84 (make-local-variable 'comment-start-skip)
85 (setq comment-start-skip "%+ *")
86 (make-local-variable 'comment-column)
87 (setq comment-column 48)
88 (make-local-variable 'comment-indent-function)
89 (setq comment-indent-function 'prolog-comment-indent))
91 (defun prolog-mode-commands (map)
92 (define-key map "\t" 'prolog-indent-line)
93 (define-key map "\e\C-x" 'prolog-consult-region))
97 (setq prolog-mode-map (make-sparse-keymap))
98 (prolog-mode-commands prolog-mode-map))
101 (defun prolog-mode ()
102 "Major mode for editing Prolog code for Prologs.
103 Blank lines and `%%...' separate paragraphs. `%'s start comments.
106 Entry to this mode calls the value of `prolog-mode-hook'
107 if that value is non-nil."
109 (kill-all-local-variables)
110 (use-local-map prolog-mode-map)
111 (setq major-mode 'prolog-mode)
112 (setq mode-name "Prolog")
113 (prolog-mode-variables)
114 (run-hooks 'prolog-mode-hook))
116 (defun prolog-indent-line (&optional whole-exp)
117 "Indent current line as Prolog code.
118 With argument, indent any additional lines of the same clause
119 rigidly along with this one (not yet)."
121 (let ((indent (prolog-indent-level))
122 (pos (- (point-max) (point))) beg)
125 (skip-chars-forward " \t")
126 (if (zerop (- indent (current-column)))
128 (delete-region beg (point))
130 (if (> (- (point-max) pos) (point))
131 (goto-char (- (point-max) pos)))
134 (defun prolog-indent-level ()
135 "Compute prolog indentation level."
138 (skip-chars-forward " \t")
140 ((looking-at "%%%") 0) ;Large comment starts
141 ((looking-at "%[^%]") comment-column) ;Small comment starts
142 ((bobp) 0) ;Beginning of buffer
144 (let ((empty t) ind more less)
146 (setq less t) ;Find close
148 ;; See previous indentation
154 (skip-chars-forward " \t")
155 (if (not (or (looking-at "%[^%]") (looking-at "\n")))
158 (setq ind 0) ;Beginning of buffer
159 (setq ind (current-column))) ;Beginning of clause
161 (if (looking-at "%%[^%]")
165 (setq more t) ;Find open
168 (end-of-prolog-clause)
169 (or (bobp) (forward-char -1))
170 (cond ((looking-at "[,(;>]")
171 (if (and more (looking-at "[^,]"))
172 (+ ind prolog-indent-width) ;More indentation
173 (max tab-width ind))) ;Same indentation
174 ((looking-at "-") tab-width) ;TAB
175 ((or less (looking-at "[^.]"))
176 (max (- ind prolog-indent-width) 0)) ;Less indentation
177 (t 0)) ;No indentation
181 (defun end-of-prolog-clause ()
182 "Go to end of clause in this line."
183 (beginning-of-line 1)
184 (let* ((eolpos (save-excursion (end-of-line) (point))))
185 (if (re-search-forward comment-start-skip eolpos 'move)
186 (goto-char (match-beginning 0)))
187 (skip-chars-backward " \t")))
189 (defun prolog-comment-indent ()
190 "Compute prolog comment indentation."
191 (cond ((looking-at "%%%") 0)
192 ((looking-at "%%") (prolog-indent-level))
195 (skip-chars-backward " \t")
196 ;; Insert one space at least, except at left margin.
197 (max (+ (current-column) (if (bolp) 0 1))
203 ;;; Inferior prolog mode
205 (defvar inferior-prolog-mode-map nil)
207 (defun inferior-prolog-mode ()
208 "Major mode for interacting with an inferior Prolog process.
210 The following commands are available:
211 \\{inferior-prolog-mode-map}
213 Entry to this mode calls the value of `prolog-mode-hook' with no arguments,
214 if that value is non-nil. Likewise with the value of `comint-mode-hook'.
215 `prolog-mode-hook' is called after `comint-mode-hook'.
217 You can send text to the inferior Prolog from other buffers
218 using the commands `send-region', `send-string' and \\[prolog-consult-region].
221 Tab indents for Prolog; with argument, shifts rest
222 of expression rigidly with the current line.
223 Paragraphs are separated only by blank lines and '%%'.
226 Return at end of buffer sends line as input.
227 Return not at end copies rest of line to end and sends it.
228 \\[comint-kill-input] and \\[backward-kill-word] are kill commands, imitating normal Unix input editing.
229 \\[comint-interrupt-subjob] interrupts the shell or its current subjob if any.
230 \\[comint-stop-subjob] stops. \\[comint-quit-subjob] sends quit signal."
234 (setq major-mode 'inferior-prolog-mode
235 mode-name "Inferior Prolog"
236 comint-prompt-regexp "^| [ ?][- ] *")
237 (prolog-mode-variables)
238 (if inferior-prolog-mode-map nil
239 (setq inferior-prolog-mode-map (copy-keymap comint-mode-map))
240 (prolog-mode-commands inferior-prolog-mode-map))
241 (use-local-map inferior-prolog-mode-map)
242 (run-hooks 'prolog-mode-hook))
246 "Run an inferior Prolog process, input and output via buffer *prolog*."
249 (switch-to-buffer (make-comint "prolog" prolog-program-name))
250 (inferior-prolog-mode))
252 (defun prolog-consult-region (compile beg end)
253 "Send the region to the Prolog process made by \"M-x run-prolog\".
254 If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode."
258 (send-string "prolog" prolog-compile-string)
259 (send-string "prolog" prolog-consult-string))
260 (send-region "prolog" beg end)
261 (send-string "prolog" "\n") ;May be unnecessary
262 (if prolog-eof-string
263 (send-string "prolog" prolog-eof-string)
264 (process-send-eof "prolog")))) ;Send eof to prolog process.
266 (defun prolog-consult-region-and-go (compile beg end)
267 "Send the region to the inferior Prolog, and switch to *prolog* buffer.
268 If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode."
270 (prolog-consult-region compile beg end)
271 (switch-to-buffer "*prolog*"))
273 ;;; prolog.el ends here