1992-05-30 22:12:04 +00:00
|
|
|
|
;;; prolog.el --- major mode for editing and running Prolog under Emacs
|
|
|
|
|
|
2005-08-01 08:43:45 +00:00
|
|
|
|
;; Copyright (C) 1986, 1987, 2001, 2002, 2003, 2004, 2005
|
|
|
|
|
;; Free Software Foundation, Inc.
|
1992-07-22 04:22:30 +00:00
|
|
|
|
|
1997-05-27 19:51:23 +00:00
|
|
|
|
;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
|
1992-07-17 20:24:00 +00:00
|
|
|
|
;; Keywords: languages
|
1992-07-16 21:47:34 +00:00
|
|
|
|
|
1989-12-11 04:58:09 +00:00
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
1992-07-16 21:47:34 +00:00
|
|
|
|
;; the Free Software Foundation; either version 2, or (at your option)
|
1989-12-11 04:58:09 +00:00
|
|
|
|
;; any later version.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
1996-01-14 07:34:30 +00:00
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
2005-07-04 17:55:18 +00:00
|
|
|
|
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
|
;; Boston, MA 02110-1301, USA.
|
1989-12-11 04:58:09 +00:00
|
|
|
|
|
1993-03-22 05:42:35 +00:00
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;; This package provides a major mode for editing Prolog. It knows
|
|
|
|
|
;; about Prolog syntax and comments, and can send regions to an inferior
|
2002-06-01 00:55:41 +00:00
|
|
|
|
;; Prolog interpreter process. Font locking is tuned towards GNU Prolog.
|
1993-03-22 05:42:35 +00:00
|
|
|
|
|
1992-07-16 21:47:34 +00:00
|
|
|
|
;;; Code:
|
|
|
|
|
|
2005-08-30 11:12:14 +00:00
|
|
|
|
(defvar comint-prompt-regexp)
|
|
|
|
|
|
|
|
|
|
|
1997-04-12 08:35:41 +00:00
|
|
|
|
(defgroup prolog nil
|
2005-07-04 03:18:35 +00:00
|
|
|
|
"Major mode for editing and running Prolog under Emacs."
|
1997-04-12 08:35:41 +00:00
|
|
|
|
:group 'languages)
|
|
|
|
|
|
2002-06-01 00:55:41 +00:00
|
|
|
|
|
2004-02-08 23:37:11 +00:00
|
|
|
|
(defcustom prolog-program-name
|
|
|
|
|
(let ((names '("prolog" "gprolog")))
|
|
|
|
|
(while (and names
|
|
|
|
|
(not (executable-find (car names))))
|
|
|
|
|
(setq names (cdr names)))
|
|
|
|
|
(or (car names) "prolog"))
|
1997-04-12 08:35:41 +00:00
|
|
|
|
"*Program name for invoking an inferior Prolog with `run-prolog'."
|
|
|
|
|
:type 'string
|
|
|
|
|
:group 'prolog)
|
1989-12-11 04:58:09 +00:00
|
|
|
|
|
1997-04-12 08:35:41 +00:00
|
|
|
|
(defcustom prolog-consult-string "reconsult(user).\n"
|
|
|
|
|
"*(Re)Consult mode (for C-Prolog and Quintus Prolog). "
|
|
|
|
|
:type 'string
|
|
|
|
|
:group 'prolog)
|
1989-12-11 04:58:09 +00:00
|
|
|
|
|
1997-04-12 08:35:41 +00:00
|
|
|
|
(defcustom prolog-compile-string "compile(user).\n"
|
|
|
|
|
"*Compile mode (for Quintus Prolog)."
|
|
|
|
|
:type 'string
|
|
|
|
|
:group 'prolog)
|
1989-12-11 04:58:09 +00:00
|
|
|
|
|
1997-04-12 08:35:41 +00:00
|
|
|
|
(defcustom prolog-eof-string "end_of_file.\n"
|
2005-07-04 09:42:58 +00:00
|
|
|
|
"*String that represents end of file for Prolog.
|
|
|
|
|
When nil, send actual operating system end of file."
|
1997-04-12 08:35:41 +00:00
|
|
|
|
:type 'string
|
|
|
|
|
:group 'prolog)
|
1989-12-11 04:58:09 +00:00
|
|
|
|
|
1997-04-12 08:35:41 +00:00
|
|
|
|
(defcustom prolog-indent-width 4
|
|
|
|
|
"Level of indentation in Prolog buffers."
|
|
|
|
|
:type 'integer
|
|
|
|
|
:group 'prolog)
|
1989-12-11 04:58:09 +00:00
|
|
|
|
|
2002-06-01 00:55:41 +00:00
|
|
|
|
(defvar prolog-font-lock-keywords
|
|
|
|
|
'(("\\(#[<=]=>\\|:-\\)\\|\\(#=\\)\\|\\(#[#<>\\/][=\\/]*\\|!\\)"
|
|
|
|
|
0 font-lock-keyword-face)
|
|
|
|
|
("\\<\\(is\\|write\\|nl\\|read_\\sw+\\)\\>"
|
|
|
|
|
1 font-lock-keyword-face)
|
|
|
|
|
("^\\(\\sw+\\)\\s-*\\((\\(.+\\))\\)*"
|
|
|
|
|
(1 font-lock-function-name-face)
|
|
|
|
|
(3 font-lock-variable-name-face)))
|
|
|
|
|
"Font-lock keywords for Prolog mode.")
|
|
|
|
|
|
2004-02-08 23:37:11 +00:00
|
|
|
|
(defvar prolog-mode-syntax-table
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(let ((table (make-syntax-table)))
|
|
|
|
|
(modify-syntax-entry ?_ "w" table)
|
|
|
|
|
(modify-syntax-entry ?\\ "\\" table)
|
2002-06-01 00:55:41 +00:00
|
|
|
|
(modify-syntax-entry ?/ ". 14" table)
|
|
|
|
|
(modify-syntax-entry ?* ". 23" table)
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(modify-syntax-entry ?+ "." table)
|
|
|
|
|
(modify-syntax-entry ?- "." table)
|
|
|
|
|
(modify-syntax-entry ?= "." table)
|
|
|
|
|
(modify-syntax-entry ?% "<" table)
|
1994-07-12 03:10:30 +00:00
|
|
|
|
(modify-syntax-entry ?\n ">" table)
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(modify-syntax-entry ?< "." table)
|
|
|
|
|
(modify-syntax-entry ?> "." table)
|
|
|
|
|
(modify-syntax-entry ?\' "\"" table)
|
2004-02-08 23:37:11 +00:00
|
|
|
|
table))
|
1989-12-11 04:58:09 +00:00
|
|
|
|
|
2004-02-08 23:37:11 +00:00
|
|
|
|
(defvar prolog-mode-abbrev-table nil)
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(define-abbrev-table 'prolog-mode-abbrev-table ())
|
|
|
|
|
|
|
|
|
|
(defun prolog-mode-variables ()
|
|
|
|
|
(make-local-variable 'paragraph-separate)
|
2004-02-08 23:37:11 +00:00
|
|
|
|
(setq paragraph-separate (concat "%%\\|$\\|" page-delimiter)) ;'%%..'
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(make-local-variable 'paragraph-ignore-fill-prefix)
|
|
|
|
|
(setq paragraph-ignore-fill-prefix t)
|
1997-12-04 04:28:41 +00:00
|
|
|
|
(make-local-variable 'imenu-generic-expression)
|
2004-12-01 19:48:52 +00:00
|
|
|
|
(setq imenu-generic-expression '((nil "^\\sw+" 0)))
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(make-local-variable 'indent-line-function)
|
|
|
|
|
(setq indent-line-function 'prolog-indent-line)
|
|
|
|
|
(make-local-variable 'comment-start)
|
|
|
|
|
(setq comment-start "%")
|
|
|
|
|
(make-local-variable 'comment-start-skip)
|
2004-02-08 23:37:11 +00:00
|
|
|
|
(setq comment-start-skip "\\(?:%+\\|/\\*+\\)[ \t]*")
|
|
|
|
|
(make-local-variable 'comment-end-skip)
|
|
|
|
|
(setq comment-end-skip "[ \t]*\\(\n\\|\\*+/\\)")
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(make-local-variable 'comment-column)
|
2004-02-08 23:37:11 +00:00
|
|
|
|
(setq comment-column 48))
|
1989-12-11 04:58:09 +00:00
|
|
|
|
|
2004-02-08 23:37:11 +00:00
|
|
|
|
(defvar prolog-mode-map
|
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
|
(define-key map "\e\C-x" 'prolog-consult-region)
|
|
|
|
|
map))
|
1989-12-11 04:58:09 +00:00
|
|
|
|
|
1991-05-09 21:50:55 +00:00
|
|
|
|
;;;###autoload
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(defun prolog-mode ()
|
|
|
|
|
"Major mode for editing Prolog code for Prologs.
|
|
|
|
|
Blank lines and `%%...' separate paragraphs. `%'s start comments.
|
|
|
|
|
Commands:
|
|
|
|
|
\\{prolog-mode-map}
|
1991-04-12 20:15:51 +00:00
|
|
|
|
Entry to this mode calls the value of `prolog-mode-hook'
|
1989-12-11 04:58:09 +00:00
|
|
|
|
if that value is non-nil."
|
|
|
|
|
(interactive)
|
|
|
|
|
(kill-all-local-variables)
|
|
|
|
|
(use-local-map prolog-mode-map)
|
2004-02-08 23:37:11 +00:00
|
|
|
|
(set-syntax-table prolog-mode-syntax-table)
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(setq major-mode 'prolog-mode)
|
|
|
|
|
(setq mode-name "Prolog")
|
|
|
|
|
(prolog-mode-variables)
|
2002-06-01 00:55:41 +00:00
|
|
|
|
;; font lock
|
|
|
|
|
(setq font-lock-defaults '(prolog-font-lock-keywords
|
|
|
|
|
nil nil nil
|
|
|
|
|
beginning-of-line))
|
2004-02-08 23:37:11 +00:00
|
|
|
|
(run-mode-hooks 'prolog-mode-hook))
|
1989-12-11 04:58:09 +00:00
|
|
|
|
|
|
|
|
|
(defun prolog-indent-line (&optional whole-exp)
|
|
|
|
|
"Indent current line as Prolog code.
|
|
|
|
|
With argument, indent any additional lines of the same clause
|
|
|
|
|
rigidly along with this one (not yet)."
|
|
|
|
|
(interactive "p")
|
|
|
|
|
(let ((indent (prolog-indent-level))
|
|
|
|
|
(pos (- (point-max) (point))) beg)
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(setq beg (point))
|
|
|
|
|
(skip-chars-forward " \t")
|
|
|
|
|
(if (zerop (- indent (current-column)))
|
|
|
|
|
nil
|
|
|
|
|
(delete-region beg (point))
|
|
|
|
|
(indent-to indent))
|
|
|
|
|
(if (> (- (point-max) pos) (point))
|
|
|
|
|
(goto-char (- (point-max) pos)))
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
(defun prolog-indent-level ()
|
2005-07-04 09:42:58 +00:00
|
|
|
|
"Compute Prolog indentation level."
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(save-excursion
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(skip-chars-forward " \t")
|
|
|
|
|
(cond
|
|
|
|
|
((looking-at "%%%") 0) ;Large comment starts
|
|
|
|
|
((looking-at "%[^%]") comment-column) ;Small comment starts
|
|
|
|
|
((bobp) 0) ;Beginning of buffer
|
|
|
|
|
(t
|
|
|
|
|
(let ((empty t) ind more less)
|
|
|
|
|
(if (looking-at ")")
|
|
|
|
|
(setq less t) ;Find close
|
|
|
|
|
(setq less nil))
|
|
|
|
|
;; See previous indentation
|
|
|
|
|
(while empty
|
|
|
|
|
(forward-line -1)
|
|
|
|
|
(beginning-of-line)
|
|
|
|
|
(if (bobp)
|
|
|
|
|
(setq empty nil)
|
|
|
|
|
(skip-chars-forward " \t")
|
|
|
|
|
(if (not (or (looking-at "%[^%]") (looking-at "\n")))
|
|
|
|
|
(setq empty nil))))
|
|
|
|
|
(if (bobp)
|
|
|
|
|
(setq ind 0) ;Beginning of buffer
|
|
|
|
|
(setq ind (current-column))) ;Beginning of clause
|
|
|
|
|
;; See its beginning
|
|
|
|
|
(if (looking-at "%%[^%]")
|
|
|
|
|
ind
|
|
|
|
|
;; Real prolog code
|
|
|
|
|
(if (looking-at "(")
|
|
|
|
|
(setq more t) ;Find open
|
|
|
|
|
(setq more nil))
|
|
|
|
|
;; See its tail
|
|
|
|
|
(end-of-prolog-clause)
|
|
|
|
|
(or (bobp) (forward-char -1))
|
|
|
|
|
(cond ((looking-at "[,(;>]")
|
|
|
|
|
(if (and more (looking-at "[^,]"))
|
|
|
|
|
(+ ind prolog-indent-width) ;More indentation
|
|
|
|
|
(max tab-width ind))) ;Same indentation
|
|
|
|
|
((looking-at "-") tab-width) ;TAB
|
|
|
|
|
((or less (looking-at "[^.]"))
|
|
|
|
|
(max (- ind prolog-indent-width) 0)) ;Less indentation
|
|
|
|
|
(t 0)) ;No indentation
|
|
|
|
|
)))
|
|
|
|
|
)))
|
|
|
|
|
|
|
|
|
|
(defun end-of-prolog-clause ()
|
|
|
|
|
"Go to end of clause in this line."
|
|
|
|
|
(beginning-of-line 1)
|
|
|
|
|
(let* ((eolpos (save-excursion (end-of-line) (point))))
|
|
|
|
|
(if (re-search-forward comment-start-skip eolpos 'move)
|
|
|
|
|
(goto-char (match-beginning 0)))
|
|
|
|
|
(skip-chars-backward " \t")))
|
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Inferior prolog mode
|
|
|
|
|
;;;
|
2004-02-08 23:37:11 +00:00
|
|
|
|
(defvar inferior-prolog-mode-map
|
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
|
;; This map will inherit from `comint-mode-map' when entering
|
|
|
|
|
;; inferior-prolog-mode.
|
|
|
|
|
map))
|
|
|
|
|
|
|
|
|
|
(defvar inferior-prolog-mode-syntax-table prolog-mode-syntax-table)
|
|
|
|
|
(defvar inferior-prolog-mode-abbrev-table prolog-mode-abbrev-table)
|
1989-12-11 04:58:09 +00:00
|
|
|
|
|
2004-02-08 23:37:11 +00:00
|
|
|
|
(define-derived-mode inferior-prolog-mode comint-mode "Inferior Prolog"
|
1989-12-11 04:58:09 +00:00
|
|
|
|
"Major mode for interacting with an inferior Prolog process.
|
|
|
|
|
|
|
|
|
|
The following commands are available:
|
|
|
|
|
\\{inferior-prolog-mode-map}
|
|
|
|
|
|
1991-04-12 20:15:51 +00:00
|
|
|
|
Entry to this mode calls the value of `prolog-mode-hook' with no arguments,
|
|
|
|
|
if that value is non-nil. Likewise with the value of `comint-mode-hook'.
|
|
|
|
|
`prolog-mode-hook' is called after `comint-mode-hook'.
|
1989-12-11 04:58:09 +00:00
|
|
|
|
|
2005-05-18 10:19:31 +00:00
|
|
|
|
You can send text to the inferior Prolog from other buffers using the commands
|
|
|
|
|
`process-send-region', `process-send-string' and \\[prolog-consult-region].
|
1989-12-11 04:58:09 +00:00
|
|
|
|
|
|
|
|
|
Commands:
|
|
|
|
|
Tab indents for Prolog; with argument, shifts rest
|
|
|
|
|
of expression rigidly with the current line.
|
1991-04-12 20:15:51 +00:00
|
|
|
|
Paragraphs are separated only by blank lines and '%%'.
|
|
|
|
|
'%'s start comments.
|
1989-12-11 04:58:09 +00:00
|
|
|
|
|
|
|
|
|
Return at end of buffer sends line as input.
|
|
|
|
|
Return not at end copies rest of line to end and sends it.
|
|
|
|
|
\\[comint-kill-input] and \\[backward-kill-word] are kill commands, imitating normal Unix input editing.
|
|
|
|
|
\\[comint-interrupt-subjob] interrupts the shell or its current subjob if any.
|
|
|
|
|
\\[comint-stop-subjob] stops. \\[comint-quit-subjob] sends quit signal."
|
2004-02-08 23:37:11 +00:00
|
|
|
|
(setq comint-prompt-regexp "^| [ ?][- ] *")
|
|
|
|
|
(prolog-mode-variables))
|
1989-12-11 04:58:09 +00:00
|
|
|
|
|
1991-05-09 21:50:55 +00:00
|
|
|
|
;;;###autoload
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(defun run-prolog ()
|
|
|
|
|
"Run an inferior Prolog process, input and output via buffer *prolog*."
|
|
|
|
|
(interactive)
|
|
|
|
|
(require 'comint)
|
2004-02-08 23:37:11 +00:00
|
|
|
|
(pop-to-buffer (make-comint "prolog" prolog-program-name))
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(inferior-prolog-mode))
|
|
|
|
|
|
|
|
|
|
(defun prolog-consult-region (compile beg end)
|
1991-04-12 20:15:51 +00:00
|
|
|
|
"Send the region to the Prolog process made by \"M-x run-prolog\".
|
|
|
|
|
If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode."
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(interactive "P\nr")
|
|
|
|
|
(save-excursion
|
|
|
|
|
(if compile
|
2005-05-18 10:19:31 +00:00
|
|
|
|
(process-send-string "prolog" prolog-compile-string)
|
|
|
|
|
(process-send-string "prolog" prolog-consult-string))
|
|
|
|
|
(process-send-region "prolog" beg end)
|
|
|
|
|
(process-send-string "prolog" "\n") ;May be unnecessary
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(if prolog-eof-string
|
2005-05-18 10:19:31 +00:00
|
|
|
|
(process-send-string "prolog" prolog-eof-string)
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(process-send-eof "prolog")))) ;Send eof to prolog process.
|
|
|
|
|
|
|
|
|
|
(defun prolog-consult-region-and-go (compile beg end)
|
|
|
|
|
"Send the region to the inferior Prolog, and switch to *prolog* buffer.
|
1991-04-12 20:15:51 +00:00
|
|
|
|
If COMPILE (prefix arg) is not nil, use compile mode rather than consult mode."
|
1989-12-11 04:58:09 +00:00
|
|
|
|
(interactive "P\nr")
|
|
|
|
|
(prolog-consult-region compile beg end)
|
|
|
|
|
(switch-to-buffer "*prolog*"))
|
1992-05-30 22:12:04 +00:00
|
|
|
|
|
1997-06-22 18:57:55 +00:00
|
|
|
|
(provide 'prolog)
|
|
|
|
|
|
2003-09-01 15:45:59 +00:00
|
|
|
|
;;; arch-tag: f3ec6748-1272-4ab6-8826-c50cb1607636
|
1992-05-30 22:12:04 +00:00
|
|
|
|
;;; prolog.el ends here
|