home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2 / Openstep-4.2-Intel-User.iso / usr / lib / emacs / lisp / prolog.el < prev    next >
Lisp/Scheme  |  1991-01-09  |  9KB  |  268 lines

  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-consult-string "reconsult(user).\n"
  26.   "*(Re)Consult mode (for C-Prolog and Quintus Prolog). ")
  27.  
  28. (defvar prolog-compile-string "compile(user).\n"
  29.   "*Compile mode (for Quintus Prolog).")
  30.  
  31. (defvar prolog-eof-string "end_of_file.\n"
  32.   "*String that represents end of file for prolog.
  33. nil means send actual operaing system end of file.")
  34.  
  35. (defvar prolog-indent-width 4)
  36.  
  37. (if prolog-mode-syntax-table
  38.     ()
  39.   (let ((table (make-syntax-table)))
  40.     (modify-syntax-entry ?_ "w" table)
  41.     (modify-syntax-entry ?\\ "\\" table)
  42.     (modify-syntax-entry ?/ "." table)
  43.     (modify-syntax-entry ?* "." 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.     (setq prolog-mode-syntax-table table)))
  52.  
  53. (define-abbrev-table 'prolog-mode-abbrev-table ())
  54.  
  55. (defun prolog-mode-variables ()
  56.   (set-syntax-table prolog-mode-syntax-table)
  57.   (setq local-abbrev-table prolog-mode-abbrev-table)
  58.   (make-local-variable 'paragraph-start)
  59.   (setq paragraph-start (concat "^%%\\|^$\\|" page-delimiter)) ;'%%..'
  60.   (make-local-variable 'paragraph-separate)
  61.   (setq paragraph-separate paragraph-start)
  62.   (make-local-variable 'paragraph-ignore-fill-prefix)
  63.   (setq paragraph-ignore-fill-prefix t)
  64.   (make-local-variable 'indent-line-function)
  65.   (setq indent-line-function 'prolog-indent-line)
  66.   (make-local-variable 'comment-start)
  67.   (setq comment-start "%")
  68.   (make-local-variable 'comment-start-skip)
  69.   (setq comment-start-skip "%+ *")
  70.   (make-local-variable 'comment-column)
  71.   (setq comment-column 48)
  72.   (make-local-variable 'comment-indent-hook)
  73.   (setq comment-indent-hook 'prolog-comment-indent))
  74.  
  75. (defun prolog-mode-commands (map)
  76.   (define-key map "\t" 'prolog-indent-line)
  77.   (define-key map "\e\C-x" 'prolog-consult-region))
  78.  
  79. (if prolog-mode-map
  80.     nil
  81.   (setq prolog-mode-map (make-sparse-keymap))
  82.   (prolog-mode-commands prolog-mode-map))
  83.  
  84. (defun prolog-mode ()
  85.   "Major mode for editing Prolog code for Prologs.
  86. Blank lines and `%%...' separate paragraphs.  `%'s start comments.
  87. Commands:
  88. \\{prolog-mode-map}
  89. Entry to this mode calls the value of prolog-mode-hook
  90. if that value is non-nil."
  91.   (interactive)
  92.   (kill-all-local-variables)
  93.   (use-local-map prolog-mode-map)
  94.   (setq major-mode 'prolog-mode)
  95.   (setq mode-name "Prolog")
  96.   (prolog-mode-variables)
  97.   (run-hooks 'prolog-mode-hook))
  98.  
  99. (defun prolog-indent-line (&optional whole-exp)
  100.   "Indent current line as Prolog code.
  101. With argument, indent any additional lines of the same clause
  102. rigidly along with this one (not yet)."
  103.   (interactive "p")
  104.   (let ((indent (prolog-indent-level))
  105.     (pos (- (point-max) (point))) beg)
  106.     (beginning-of-line)
  107.     (setq beg (point))
  108.     (skip-chars-forward " \t")
  109.     (if (zerop (- indent (current-column)))
  110.     nil
  111.       (delete-region beg (point))
  112.       (indent-to indent))
  113.     (if (> (- (point-max) pos) (point))
  114.     (goto-char (- (point-max) pos)))
  115.     ))
  116.  
  117. (defun prolog-indent-level ()
  118.   "Compute prolog indentation level."
  119.   (save-excursion
  120.     (beginning-of-line)
  121.     (skip-chars-forward " \t")
  122.     (cond
  123.      ((looking-at "%%%") 0)        ;Large comment starts
  124.      ((looking-at "%[^%]") comment-column) ;Small comment starts
  125.      ((bobp) 0)                ;Beginning of buffer
  126.      (t
  127.       (let ((empty t) ind more less)
  128.     (if (looking-at ")")
  129.         (setq less t)        ;Find close
  130.       (setq less nil))
  131.     ;; See previous indentation
  132.     (while empty
  133.       (forward-line -1)
  134.       (beginning-of-line)
  135.        (if (bobp)
  136.            (setq empty nil)
  137.          (skip-chars-forward " \t")
  138.          (if (not (or (looking-at "%[^%]") (looking-at "\n")))
  139.          (setq empty nil))))
  140.      (if (bobp)
  141.          (setq ind 0)        ;Beginning of buffer
  142.       (setq ind (current-column)))    ;Beginning of clause
  143.     ;; See its beginning
  144.     (if (looking-at "%%[^%]")
  145.         ind
  146.       ;; Real prolog code
  147.       (if (looking-at "(")
  148.           (setq more t)        ;Find open
  149.         (setq more nil))
  150.       ;; See its tail
  151.       (end-of-prolog-clause)
  152.       (or (bobp) (forward-char -1))
  153.       (cond ((looking-at "[,(;>]")
  154.          (if (and more (looking-at "[^,]"))
  155.              (+ ind prolog-indent-width) ;More indentation
  156.            (max tab-width ind))) ;Same indentation
  157.         ((looking-at "-") tab-width) ;TAB
  158.         ((or less (looking-at "[^.]"))
  159.          (max (- ind prolog-indent-width) 0)) ;Less indentation
  160.         (t 0))            ;No indentation
  161.       )))
  162.      )))
  163.  
  164. (defun end-of-prolog-clause ()
  165.   "Go to end of clause in this line."
  166.   (beginning-of-line 1)
  167.   (let* ((eolpos (save-excursion (end-of-line) (point))))
  168.     (if (re-search-forward comment-start-skip eolpos 'move)
  169.     (goto-char (match-beginning 0)))
  170.     (skip-chars-backward " \t")))
  171.  
  172. (defun prolog-comment-indent ()
  173.   "Compute prolog comment indentation."
  174.   (cond ((looking-at "%%%") 0)
  175.     ((looking-at "%%") (prolog-indent-level))
  176.     (t
  177.      (save-excursion
  178.            (skip-chars-backward " \t")
  179.            (max (1+ (current-column)) ;Insert one space at least
  180.             comment-column)))
  181.     ))
  182.  
  183.  
  184. ;;;
  185. ;;; Inferior prolog mode
  186. ;;;
  187. (defvar inferior-prolog-mode-map nil)
  188.  
  189. ;; Moved into inferior-prolog-mode
  190. ;;(if inferior-prolog-mode-map
  191. ;;    nil
  192. ;;  (setq inferior-prolog-mode-map (copy-alist shell-mode-map))
  193. ;;  (prolog-mode-commands inferior-prolog-mode-map))
  194.  
  195. (defun inferior-prolog-mode ()
  196.   "Major mode for interacting with an inferior Prolog process.
  197.  
  198. The following commands are available:
  199. \\{inferior-prolog-mode-map}
  200.  
  201. Entry to this mode calls the value of prolog-mode-hook with no arguments,
  202. if that value is non-nil.  Likewise with the value of shell-mode-hook.
  203. prolog-mode-hook is called after shell-mode-hook.
  204.  
  205. You can send text to the inferior Prolog from other buffers
  206. using the commands send-region, send-string and \\[prolog-consult-region].
  207.  
  208. Commands:
  209. Tab indents for Prolog; with argument, shifts rest
  210.  of expression rigidly with the current line.
  211. Paragraphs are separated only by blank lines and '%%'. '%'s start comments.
  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. \\[shell-send-eof] sends end-of-file as input.
  216. \\[kill-shell-input] and \\[backward-kill-word] are kill commands, imitating normal Unix input editing.
  217. \\[interrupt-shell-subjob] interrupts the shell or its current subjob if any.
  218. \\[stop-shell-subjob] stops, likewise. \\[quit-shell-subjob] sends quit signal, likewise."
  219.   (interactive)
  220.   (kill-all-local-variables)
  221.   (setq major-mode 'inferior-prolog-mode)
  222.   (setq mode-name "Inferior Prolog")
  223.   (setq mode-line-process '(": %s"))
  224.   (prolog-mode-variables)
  225.   (require 'shell)
  226.   (if inferior-prolog-mode-map
  227.       nil
  228.     (setq inferior-prolog-mode-map (copy-alist shell-mode-map))
  229.     (prolog-mode-commands inferior-prolog-mode-map))
  230.   (use-local-map inferior-prolog-mode-map)
  231.   (make-local-variable 'last-input-start)
  232.   (setq last-input-start (make-marker))
  233.   (make-local-variable 'last-input-end)
  234.   (setq last-input-end (make-marker))
  235.   (make-variable-buffer-local 'shell-prompt-pattern)
  236.   (setq shell-prompt-pattern "^| [ ?][- ] *") ;Set prolog prompt pattern
  237.   (run-hooks 'shell-mode-hook 'prolog-mode-hook))
  238.  
  239. (defun run-prolog ()
  240.   "Run an inferior Prolog process, input and output via buffer *prolog*."
  241.   (interactive)
  242.   (require 'shell)
  243.   (switch-to-buffer (make-shell "prolog" "prolog"))
  244.   (inferior-prolog-mode))
  245.  
  246. (defun prolog-consult-region (compile beg end)
  247.   "Send the region to the Prolog process made by M-x run-prolog.
  248.  If COMPILE (prefix arg) is not nil,
  249.  use compile mode rather than consult mode."
  250.   (interactive "P\nr")
  251.   (save-excursion
  252.     (if compile
  253.     (send-string "prolog" prolog-compile-string)
  254.       (send-string "prolog" prolog-consult-string))
  255.     (send-region "prolog" beg end)
  256.     (send-string "prolog" "\n")        ;May be unnecessary
  257.     (if prolog-eof-string
  258.     (send-string "prolog" prolog-eof-string)
  259.       (process-send-eof "prolog")))) ;Send eof to prolog process.
  260.  
  261. (defun prolog-consult-region-and-go (compile beg end)
  262.   "Send the region to the inferior Prolog, and switch to *prolog* buffer.
  263.  If COMPILE (prefix arg) is not nil,
  264.  use compile mode rather than consult mode."
  265.   (interactive "P\nr")
  266.   (prolog-consult-region compile beg end)
  267.   (switch-to-buffer "*prolog*"))
  268.