home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / modes / scheme.el.z / scheme.el
Encoding:
Text File  |  1998-05-21  |  18.9 KB  |  517 lines

  1. ;;; scheme.el --- Scheme mode, and its idiosyncratic commands.
  2.  
  3. ;; Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Bill Rozas <jinz@prep.ai.mit.edu>
  6. ;; Keywords: languages, lisp
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. ;; 02111-1307, USA.
  24.  
  25. ;;; Synched up with: FSF 19.34.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; Adapted from Lisp mode by Bill Rozas, jinx@prep.
  30. ;; Initially a query replace of Lisp mode, except for the indentation 
  31. ;; of special forms.  Probably the code should be merged at some point 
  32. ;; so that there is sharing between both libraries.
  33.  
  34. ;;; Code:
  35.  
  36. (defvar scheme-mode-syntax-table nil "")
  37. (if (not scheme-mode-syntax-table)
  38.     (let ((i 0))
  39.       (setq scheme-mode-syntax-table (make-syntax-table))
  40.       (set-syntax-table scheme-mode-syntax-table)
  41.  
  42.       ;; Default is atom-constituent.
  43.       (while (< i 256)
  44.     (modify-syntax-entry i "_   ")
  45.     (setq i (1+ i)))
  46.  
  47.       ;; Word components.
  48.       (setq i ?0)
  49.       (while (<= i ?9)
  50.     (modify-syntax-entry i "w   ")
  51.     (setq i (1+ i)))
  52.       (setq i ?A)
  53.       (while (<= i ?Z)
  54.     (modify-syntax-entry i "w   ")
  55.     (setq i (1+ i)))
  56.       (setq i ?a)
  57.       (while (<= i ?z)
  58.     (modify-syntax-entry i "w   ")
  59.     (setq i (1+ i)))
  60.  
  61.       ;; Whitespace
  62.       (modify-syntax-entry ?\t "    ")
  63.       (modify-syntax-entry ?\n ">   ")
  64.       (modify-syntax-entry ?\f "    ")
  65.       (modify-syntax-entry ?\r "    ")
  66.       (modify-syntax-entry ?  "    ")
  67.  
  68.       ;; These characters are delimiters but otherwise undefined.
  69.       ;; Brackets and braces balance for editing convenience.
  70.       (modify-syntax-entry ?[ "(]  ")
  71.       (modify-syntax-entry ?] ")[  ")
  72.       (modify-syntax-entry ?{ "(}  ")
  73.       (modify-syntax-entry ?} "){  ")
  74.       (modify-syntax-entry ?\| "  23")
  75.  
  76.       ;; Other atom delimiters
  77.       (modify-syntax-entry ?\( "()  ")
  78.       (modify-syntax-entry ?\) ")(  ")
  79.       (modify-syntax-entry ?\; "<   ")
  80.       (modify-syntax-entry ?\" "\"    ")
  81.       (modify-syntax-entry ?' "  p")
  82.       (modify-syntax-entry ?` "  p")
  83.  
  84.       ;; Special characters
  85.       (modify-syntax-entry ?, "_ p")
  86.       (modify-syntax-entry ?@ "_ p")
  87.       (modify-syntax-entry ?# "_ p14")
  88.       (modify-syntax-entry ?\\ "\\   ")))
  89.  
  90. (defvar scheme-mode-abbrev-table nil "")
  91. (define-abbrev-table 'scheme-mode-abbrev-table ())
  92.  
  93. (defun scheme-mode-variables ()
  94.   (set-syntax-table scheme-mode-syntax-table)
  95.   (setq local-abbrev-table scheme-mode-abbrev-table)
  96.   (make-local-variable 'paragraph-start)
  97.   (setq paragraph-start (concat "$\\|" page-delimiter))
  98.   (make-local-variable 'paragraph-separate)
  99.   (setq paragraph-separate paragraph-start)
  100.   (make-local-variable 'paragraph-ignore-fill-prefix)
  101.   (setq paragraph-ignore-fill-prefix t)
  102.   (make-local-variable 'indent-line-function)
  103.   (setq indent-line-function 'scheme-indent-line)
  104.   (make-local-variable 'parse-sexp-ignore-comments)
  105.   (setq parse-sexp-ignore-comments t)
  106.   (make-local-variable 'comment-start)
  107.   (setq comment-start ";")
  108.   (make-local-variable 'comment-start-skip)
  109.   ;; Look within the line for a ; following an even number of backslashes
  110.   ;; after either a non-backslash or the line beginning.
  111.   (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+[ \t]*")
  112.   (make-local-variable 'comment-column)
  113.   (setq comment-column 40)
  114.   (make-local-variable 'comment-indent-function)
  115.   (setq comment-indent-function 'scheme-comment-indent)
  116.   (make-local-variable 'parse-sexp-ignore-comments)
  117.   (setq parse-sexp-ignore-comments t)
  118.   (setq mode-line-process '("" scheme-mode-line-process)))
  119.  
  120. (defvar scheme-mode-line-process "")
  121.  
  122. (defun scheme-mode-commands (map)
  123.   (define-key map "\t" 'scheme-indent-line)
  124.   (define-key map "\e\C-q" 'scheme-indent-sexp))
  125.  
  126. (defvar scheme-mode-map nil)
  127. (if (not scheme-mode-map)
  128.     (progn
  129.       (setq scheme-mode-map (make-sparse-keymap))
  130.       (scheme-mode-commands scheme-mode-map)))
  131.  
  132. ;;;###autoload
  133. (defun scheme-mode ()
  134.   "Major mode for editing Scheme code.
  135. Editing commands are similar to those of lisp-mode.
  136.  
  137. In addition, if an inferior Scheme process is running, some additional
  138. commands will be defined, for evaluating expressions and controlling
  139. the interpreter, and the state of the process will be displayed in the
  140. modeline of all Scheme buffers.  The names of commands that interact
  141. with the Scheme process start with \"xscheme-\".  For more information
  142. see the documentation for xscheme-interaction-mode.
  143.  
  144. Commands:
  145. Delete converts tabs to spaces as it moves back.
  146. Blank lines separate paragraphs.  Semicolons start comments.
  147. \\{scheme-mode-map}
  148. Entry to this mode calls the value of scheme-mode-hook
  149. if that value is non-nil."
  150.   (interactive)
  151.   (kill-all-local-variables)
  152.   (scheme-mode-initialize)
  153.   (scheme-mode-variables)
  154.   (run-hooks 'scheme-mode-hook))
  155.  
  156. (defun scheme-mode-initialize ()
  157.   (use-local-map scheme-mode-map)
  158.   (setq major-mode 'scheme-mode)
  159.   (setq mode-name "Scheme"))
  160.  
  161. (defvar scheme-mit-dialect t
  162.   "If non-nil, scheme mode is specialized for MIT Scheme.
  163. Set this to nil if you normally use another dialect.")
  164.  
  165. (defun scheme-comment-indent (&optional pos)
  166.   (save-excursion
  167.     (if pos (goto-char pos))
  168.     (cond ((looking-at ";;;") (current-column))
  169.       ((looking-at ";;")
  170.        (let ((tem (calculate-scheme-indent)))
  171.          (if (listp tem) (car tem) tem)))
  172.       (t
  173.        (skip-chars-backward " \t")
  174.        (max (if (bolp) 0 (1+ (current-column)))
  175.         comment-column)))))
  176.  
  177. (defvar scheme-indent-offset nil "")
  178. (defvar scheme-indent-function 'scheme-indent-function "")
  179.  
  180. (defun scheme-indent-line (&optional whole-exp)
  181.   "Indent current line as Scheme code.
  182. With argument, indent any additional lines of the same expression
  183. rigidly along with this one."
  184.   (interactive "P")
  185.   (let ((indent (calculate-scheme-indent)) shift-amt beg end
  186.     (pos (- (point-max) (point))))
  187.     (beginning-of-line)
  188.     (setq beg (point))
  189.     (skip-chars-forward " \t")
  190.     (if (looking-at "[ \t]*;;;")
  191.     ;; Don't alter indentation of a ;;; comment line.
  192.     nil
  193.       (if (listp indent) (setq indent (car indent)))
  194.       (setq shift-amt (- indent (current-column)))
  195.       (if (zerop shift-amt)
  196.       nil
  197.     (delete-region beg (point))
  198.     (indent-to indent))
  199.       ;; If initial point was within line's indentation,
  200.       ;; position after the indentation.  Else stay at same point in text.
  201.       (if (> (- (point-max) pos) (point))
  202.       (goto-char (- (point-max) pos)))
  203.       ;; If desired, shift remaining lines of expression the same amount.
  204.       (and whole-exp (not (zerop shift-amt))
  205.        (save-excursion
  206.          (goto-char beg)
  207.          (forward-sexp 1)
  208.          (setq end (point))
  209.          (goto-char beg)
  210.          (forward-line 1)
  211.          (setq beg (point))
  212.          (> end beg))
  213.        (indent-code-rigidly beg end shift-amt)))))
  214.  
  215. (defun calculate-scheme-indent (&optional parse-start)
  216.   "Return appropriate indentation for current line as scheme code.
  217. In usual case returns an integer: the column to indent to.
  218. Can instead return a list, whose car is the column to indent to.
  219. This means that following lines at the same level of indentation
  220. should not necessarily be indented the same way.
  221. The second element of the list is the buffer position
  222. of the start of the containing expression."
  223.   (save-excursion
  224.     (beginning-of-line)
  225.     (let ((indent-point (point)) state paren-depth desired-indent (retry t)
  226.       last-sexp containing-sexp first-sexp-list-p)
  227.       (if parse-start
  228.       (goto-char parse-start)
  229.     (beginning-of-defun))
  230.       ;; Find outermost containing sexp
  231.       (while (< (point) indent-point)
  232.     (setq state (parse-partial-sexp (point) indent-point 0)))
  233.       ;; Find innermost containing sexp
  234.       (while (and retry (setq paren-depth (car state)) (> paren-depth 0))
  235.     (setq retry nil)
  236.     (setq last-sexp (nth 2 state))
  237.     (setq containing-sexp (car (cdr state)))
  238.     ;; Position following last unclosed open.
  239.     (goto-char (1+ containing-sexp))
  240.     ;; Is there a complete sexp since then?
  241.     (if (and last-sexp (> last-sexp (point)))
  242.         ;; Yes, but is there a containing sexp after that?
  243.         (let ((peek (parse-partial-sexp last-sexp indent-point 0)))
  244.           (if (setq retry (car (cdr peek))) (setq state peek))))
  245.     (if (not retry)
  246.         ;; Innermost containing sexp found
  247.         (progn
  248.           (goto-char (1+ containing-sexp))
  249.           (if (not last-sexp)
  250.           ;; indent-point immediately follows open paren.
  251.           ;; Don't call hook.
  252.           (setq desired-indent (current-column))
  253.         ;; Move to first sexp after containing open paren
  254.         (parse-partial-sexp (point) last-sexp 0 t)
  255.         (setq first-sexp-list-p (looking-at "\\s("))
  256.         (cond
  257.          ((> (save-excursion (forward-line 1) (point))
  258.              last-sexp)
  259.           ;; Last sexp is on same line as containing sexp.
  260.           ;; It's almost certainly a function call.
  261.           (parse-partial-sexp (point) last-sexp 0 t)
  262.           (if (/= (point) last-sexp)
  263.               ;; Indent beneath first argument or, if only one sexp
  264.               ;; on line, indent beneath that.
  265.               (progn (forward-sexp 1)
  266.                  (parse-partial-sexp (point) last-sexp 0 t)))
  267.           (backward-prefix-chars))
  268.          (t
  269.           ;; Indent beneath first sexp on same line as last-sexp.
  270.           ;; Again, it's almost certainly a function call.
  271.           (goto-char last-sexp)
  272.           (beginning-of-line)
  273.           (parse-partial-sexp (point) last-sexp 0 t)
  274.           (backward-prefix-chars)))))))
  275.       ;; If looking at a list, don't call hook.
  276.       (if first-sexp-list-p
  277.       (setq desired-indent (current-column)))
  278.       ;; Point is at the point to indent under unless we are inside a string.
  279.       ;; Call indentation hook except when overridden by scheme-indent-offset
  280.       ;; or if the desired indentation has already been computed.
  281.       (cond ((car (nthcdr 3 state))
  282.          ;; Inside a string, don't change indentation.
  283.          (goto-char indent-point)
  284.          (skip-chars-forward " \t")
  285.          (setq desired-indent (current-column)))
  286.         ((and (integerp scheme-indent-offset) containing-sexp)
  287.          ;; Indent by constant offset
  288.          (goto-char containing-sexp)
  289.          (setq desired-indent (+ scheme-indent-offset (current-column))))
  290.         ((not (or desired-indent
  291.               (and (boundp 'scheme-indent-function)
  292.                scheme-indent-function
  293.                (not retry)
  294.                (setq desired-indent
  295.                  (funcall scheme-indent-function
  296.                       indent-point state)))))
  297.          ;; Use default indentation if not computed yet
  298.          (setq desired-indent (current-column))))
  299.       desired-indent)))
  300.  
  301. (defun scheme-indent-function (indent-point state)
  302.   (let ((normal-indent (current-column)))
  303.     (save-excursion
  304.       (goto-char (1+ (car (cdr state))))
  305.       (re-search-forward "\\sw\\|\\s_")
  306.       (if (/= (point) (car (cdr state)))
  307.       (let ((function (buffer-substring (progn (forward-char -1) (point))
  308.                         (progn (forward-sexp 1) (point))))
  309.         method)
  310.         ;; Who cares about this, really?
  311.         ;(if (not (string-match "\\\\\\||" function)))
  312.         (setq function (downcase function))
  313.         (setq method (get (intern-soft function) 'scheme-indent-function))
  314.         (cond ((integerp method)
  315.            (scheme-indent-specform method state indent-point))
  316.           (method
  317.            (funcall method state indent-point))
  318.           ((and (> (length function) 3)
  319.             (string-equal (substring function 0 3) "def"))
  320.            (scheme-indent-defform state indent-point))))))))
  321.  
  322. (defvar scheme-body-indent 2 "")
  323.  
  324. (defun scheme-indent-specform (count state indent-point)
  325.   (let ((containing-form-start (car (cdr state))) (i count)
  326.     body-indent containing-form-column)
  327.     ;; Move to the start of containing form, calculate indentation
  328.     ;; to use for non-distinguished forms (> count), and move past the
  329.     ;; function symbol.  scheme-indent-function guarantees that there is at
  330.     ;; least one word or symbol character following open paren of containing
  331.     ;; form.
  332.     (goto-char containing-form-start)
  333.     (setq containing-form-column (current-column))
  334.     (setq body-indent (+ scheme-body-indent containing-form-column))
  335.     (forward-char 1)
  336.     (forward-sexp 1)
  337.     ;; Now find the start of the last form.
  338.     (parse-partial-sexp (point) indent-point 1 t)
  339.     (while (and (< (point) indent-point)
  340.         (condition-case nil
  341.             (progn
  342.               (setq count (1- count))
  343.               (forward-sexp 1)
  344.               (parse-partial-sexp (point) indent-point 1 t))
  345.           (error nil))))
  346.     ;; Point is sitting on first character of last (or count) sexp.
  347.     (cond ((> count 0)
  348.        ;; A distinguished form.  Use double scheme-body-indent.
  349.        (list (+ containing-form-column (* 2 scheme-body-indent))
  350.          containing-form-start))
  351.       ;; A non-distinguished form. Use body-indent if there are no
  352.       ;; distinguished forms and this is the first undistinguished
  353.       ;; form, or if this is the first undistinguished form and
  354.       ;; the preceding distinguished form has indentation at least
  355.       ;; as great as body-indent.
  356.       ((and (= count 0)
  357.         (or (= i 0)
  358.             (<= body-indent normal-indent)))
  359.        body-indent)
  360.       (t
  361.        normal-indent))))
  362.  
  363. (defun scheme-indent-defform (state indent-point)
  364.   (goto-char (car (cdr state)))
  365.   (forward-line 1)
  366.   (if (> (point) (car (cdr (cdr state))))
  367.       (progn
  368.     (goto-char (car (cdr state)))
  369.     (+ scheme-body-indent (current-column)))))
  370.  
  371. ;;; Let is different in Scheme
  372.  
  373. (defun would-be-symbol (string)
  374.   (not (string-equal (substring string 0 1) "(")))
  375.  
  376. (defun next-sexp-as-string ()
  377.   ;; Assumes that protected by a save-excursion
  378.   (forward-sexp 1)
  379.   (let ((the-end (point)))
  380.     (backward-sexp 1)
  381.     (buffer-substring (point) the-end)))
  382.  
  383. ;; This is correct but too slow.
  384. ;; The one below works almost always.
  385. ;;(defun scheme-let-indent (state indent-point)
  386. ;;  (if (would-be-symbol (next-sexp-as-string))
  387. ;;      (scheme-indent-specform 2 state indent-point)
  388. ;;      (scheme-indent-specform 1 state indent-point)))
  389.  
  390. (defun scheme-let-indent (state indent-point)
  391.   (skip-chars-forward " \t")
  392.   (if (looking-at "[-a-zA-Z0-9+*/?!@$%^&_:~]")
  393.       (scheme-indent-specform 2 state indent-point)
  394.       (scheme-indent-specform 1 state indent-point)))
  395.  
  396. ;; (put 'begin 'scheme-indent-function 0), say, causes begin to be indented
  397. ;; like defun if the first form is placed on the next line, otherwise
  398. ;; it is indented like any other form (i.e. forms line up under first).
  399.  
  400. (put 'begin 'scheme-indent-function 0)
  401. (put 'case 'scheme-indent-function 1)
  402. (put 'delay 'scheme-indent-function 0)
  403. (put 'do 'scheme-indent-function 2)
  404. (put 'lambda 'scheme-indent-function 1)
  405. (put 'let 'scheme-indent-function 'scheme-let-indent)
  406. (put 'let* 'scheme-indent-function 1)
  407. (put 'letrec 'scheme-indent-function 1)
  408. (put 'sequence 'scheme-indent-function 0)
  409.  
  410. (put 'call-with-input-file 'scheme-indent-function 1)
  411. (put 'with-input-from-file 'scheme-indent-function 1)
  412. (put 'with-input-from-port 'scheme-indent-function 1)
  413. (put 'call-with-output-file 'scheme-indent-function 1)
  414. (put 'with-output-to-file 'scheme-indent-function 1)
  415. (put 'with-output-to-port 'scheme-indent-function 1)
  416.  
  417. ;;;; MIT Scheme specific indentation.
  418.  
  419. (if scheme-mit-dialect
  420.     (progn
  421.       (put 'fluid-let 'scheme-indent-function 1)
  422.       (put 'in-package 'scheme-indent-function 1)
  423.       (put 'let-syntax 'scheme-indent-function 1)
  424.       (put 'local-declare 'scheme-indent-function 1)
  425.       (put 'macro 'scheme-indent-function 1)
  426.       (put 'make-environment 'scheme-indent-function 0)
  427.       (put 'named-lambda 'scheme-indent-function 1)
  428.       (put 'using-syntax 'scheme-indent-function 1)
  429.  
  430.       (put 'with-input-from-string 'scheme-indent-function 1)
  431.       (put 'with-output-to-string 'scheme-indent-function 0)
  432.       (put 'with-values 'scheme-indent-function 1)
  433.  
  434.       (put 'syntax-table-define 'scheme-indent-function 2)
  435.       (put 'list-transform-positive 'scheme-indent-function 1)
  436.       (put 'list-transform-negative 'scheme-indent-function 1)
  437.       (put 'list-search-positive 'scheme-indent-function 1)
  438.       (put 'list-search-negative 'scheme-indent-function 1)
  439.  
  440.       (put 'access-components 'scheme-indent-function 1)
  441.       (put 'assignment-components 'scheme-indent-function 1)
  442.       (put 'combination-components 'scheme-indent-function 1)
  443.       (put 'comment-components 'scheme-indent-function 1)
  444.       (put 'conditional-components 'scheme-indent-function 1)
  445.       (put 'disjunction-components 'scheme-indent-function 1)
  446.       (put 'declaration-components 'scheme-indent-function 1)
  447.       (put 'definition-components 'scheme-indent-function 1)
  448.       (put 'delay-components 'scheme-indent-function 1)
  449.       (put 'in-package-components 'scheme-indent-function 1)
  450.       (put 'lambda-components 'scheme-indent-function 1)
  451.       (put 'lambda-components* 'scheme-indent-function 1)
  452.       (put 'lambda-components** 'scheme-indent-function 1)
  453.       (put 'open-block-components 'scheme-indent-function 1)
  454.       (put 'pathname-components 'scheme-indent-function 1)
  455.       (put 'procedure-components 'scheme-indent-function 1)
  456.       (put 'sequence-components 'scheme-indent-function 1)
  457.       (put 'unassigned\?-components 'scheme-indent-function 1)
  458.       (put 'unbound\?-components 'scheme-indent-function 1)
  459.       (put 'variable-components 'scheme-indent-function 1)))
  460.  
  461. (defun scheme-indent-sexp ()
  462.   "Indent each line of the list starting just after point."
  463.   (interactive)
  464.   (let ((indent-stack (list nil)) (next-depth 0) bol
  465.     outer-loop-done inner-loop-done state this-indent)
  466.     (save-excursion (forward-sexp 1))
  467.     (save-excursion
  468.       (setq outer-loop-done nil)
  469.       (while (not outer-loop-done)
  470.     (setq last-depth next-depth
  471.           innerloop-done nil)
  472.     (while (and (not innerloop-done)
  473.             (not (setq outer-loop-done (eobp))))
  474.       (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  475.                       nil nil state))
  476.       (setq next-depth (car state))
  477.       (if (car (nthcdr 4 state))
  478.           (progn (indent-for-comment)
  479.              (end-of-line)
  480.              (setcar (nthcdr 4 state) nil)))
  481.       (if (car (nthcdr 3 state))
  482.           (progn
  483.         (forward-line 1)
  484.         (setcar (nthcdr 5 state) nil))
  485.         (setq innerloop-done t)))
  486.     (if (setq outer-loop-done (<= next-depth 0))
  487.         nil
  488.       (while (> last-depth next-depth)
  489.         (setq indent-stack (cdr indent-stack)
  490.           last-depth (1- last-depth)))
  491.       (while (< last-depth next-depth)
  492.         (setq indent-stack (cons nil indent-stack)
  493.           last-depth (1+ last-depth)))
  494.       (forward-line 1)
  495.       (setq bol (point))
  496.       (skip-chars-forward " \t")
  497.       (if (or (eobp) (looking-at "[;\n]"))
  498.           nil
  499.         (if (and (car indent-stack)
  500.              (>= (car indent-stack) 0))
  501.         (setq this-indent (car indent-stack))
  502.           (let ((val (calculate-scheme-indent
  503.               (if (car indent-stack) (- (car indent-stack))))))
  504.         (if (integerp val)
  505.             (setcar indent-stack
  506.                 (setq this-indent val))
  507.           (if (cdr val)
  508.               (setcar indent-stack (- (car (cdr val)))))
  509.           (setq this-indent (car val)))))
  510.         (if (/= (current-column) this-indent)
  511.         (progn (delete-region bol (point))
  512.                (indent-to this-indent)))))))))
  513.  
  514. (provide 'scheme)
  515.  
  516. ;;; scheme.el ends here
  517.