home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / modes / prolog.el < prev    next >
Encoding:
Text File  |  1993-03-14  |  8.7 KB  |  259 lines

  1. ;; Major mode for editing Prolog, and for running Prolog under Emacs
  2. ;; Copyright (C) 1986, 1987, 1993 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 2, 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.
  36. nil means send actual operaing system end of file.")
  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 ?/  ". 14" table)
  44.     (modify-syntax-entry ?*  ". 23" table)
  45.     (modify-syntax-entry ?%  "< b"  table)
  46.     (modify-syntax-entry ?\n "> b"  table)
  47.     (modify-syntax-entry ?_  "w"  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.     (modify-syntax-entry ?>  "." table)
  55.     (setq prolog-mode-syntax-table table)))
  56.  
  57. (define-abbrev-table 'prolog-mode-abbrev-table ())
  58.  
  59. (defun prolog-mode-variables ()
  60.   (set-syntax-table prolog-mode-syntax-table)
  61.   (setq local-abbrev-table prolog-mode-abbrev-table)
  62.   (make-local-variable 'paragraph-start)
  63.   (setq paragraph-start (concat "^%%\\|^$\\|" page-delimiter)) ;'%%..'
  64.   (make-local-variable 'paragraph-separate)
  65.   (setq paragraph-separate paragraph-start)
  66.   (make-local-variable 'paragraph-ignore-fill-prefix)
  67.   (setq paragraph-ignore-fill-prefix t)
  68.   (make-local-variable 'indent-line-function)
  69.   (setq indent-line-function 'prolog-indent-line)
  70.   (make-local-variable 'comment-start)
  71.   (setq comment-start "%")
  72.   (make-local-variable 'comment-start-skip)
  73.   (setq comment-start-skip "%+ *")
  74.   (make-local-variable 'comment-column)
  75.   (setq comment-column 48)
  76.   (make-local-variable 'comment-indent-hook)
  77.   (setq comment-indent-hook 'prolog-comment-indent))
  78.  
  79. (defun prolog-mode-commands (map)
  80.   (define-key map "\t" 'prolog-indent-line)
  81.   (define-key map "\e\C-x" 'prolog-consult-region))
  82.  
  83. (if prolog-mode-map
  84.     nil
  85.   (setq prolog-mode-map (make-sparse-keymap))
  86.   (prolog-mode-commands prolog-mode-map))
  87.  
  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}
  93. Entry to this mode calls the value of prolog-mode-hook
  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.  
  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.
  203.  
  204. You can send text to the inferior Prolog from other buffers
  205. using the commands send-region, send-string and \\[prolog-consult-region].
  206.  
  207. Commands:
  208. Tab indents for Prolog; with argument, shifts rest
  209.  of expression rigidly with the current line.
  210. Paragraphs are separated only by blank lines and '%%'. '%'s start comments.
  211.  
  212. Return at end of buffer sends line as input.
  213. Return not at end copies rest of line to end and sends it.
  214. \\[comint-kill-input] and \\[backward-kill-word] are kill commands, imitating normal Unix input editing.
  215. \\[comint-interrupt-subjob] interrupts the shell or its current subjob if any.
  216. \\[comint-stop-subjob] stops. \\[comint-quit-subjob] sends quit signal."
  217.   (interactive)
  218.   (require 'comint)
  219.   (comint-mode)
  220.   (setq major-mode 'inferior-prolog-mode
  221.     mode-name "Inferior Prolog"
  222.     comint-prompt-regexp "^| [ ?][- ] *")
  223.   (prolog-mode-variables)
  224.   (if inferior-prolog-mode-map nil
  225.     (setq inferior-prolog-mode-map (copy-keymap comint-mode-map))
  226.     (prolog-mode-commands inferior-prolog-mode-map))
  227.   (use-local-map inferior-prolog-mode-map)
  228.   (run-hooks 'prolog-mode-hook))
  229.  
  230. (defun run-prolog ()
  231.   "Run an inferior Prolog process, input and output via buffer *prolog*."
  232.   (interactive)
  233.   (require 'comint)
  234.   (switch-to-buffer (make-comint "prolog" prolog-program-name))
  235.   (inferior-prolog-mode))
  236.  
  237. (defun prolog-consult-region (compile beg end)
  238.   "Send the region to the Prolog process made by M-x run-prolog.
  239.  If COMPILE (prefix arg) is not nil,
  240.  use compile mode rather than consult mode."
  241.   (interactive "P\nr")
  242.   (save-excursion
  243.     (if compile
  244.     (send-string "prolog" prolog-compile-string)
  245.       (send-string "prolog" prolog-consult-string))
  246.     (send-region "prolog" beg end)
  247.     (send-string "prolog" "\n")        ;May be unnecessary
  248.     (if prolog-eof-string
  249.     (send-string "prolog" prolog-eof-string)
  250.       (process-send-eof "prolog")))) ;Send eof to prolog process.
  251.  
  252. (defun prolog-consult-region-and-go (compile beg end)
  253.   "Send the region to the inferior Prolog, and switch to *prolog* buffer.
  254.  If COMPILE (prefix arg) is not nil,
  255.  use compile mode rather than consult mode."
  256.   (interactive "P\nr")
  257.   (prolog-consult-region compile beg end)
  258.   (switch-to-buffer "*prolog*"))
  259.