home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / modes / cl-indent.el < prev    next >
Encoding:
Text File  |  1995-03-25  |  19.8 KB  |  491 lines

  1. ;; Lisp mode, and its idiosyncratic commands.
  2. ;; Copyright (C) 1987, 1993 Free Software Foundation, Inc.
  3. ;; Written by Richard Mlynarik July 1987
  4.  
  5. ;; This file is part of XEmacs.
  6.  
  7. ;; XEmacs is free software; you can redistribute it and/or modify it
  8. ;; 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. ;; XEmacs is distributed in the hope that it will be useful, but
  13. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. ;; General Public License for more details.
  16.  
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  19. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. ;;>> TODO
  22. ;; :foo
  23. ;;   bar
  24. ;; :baz
  25. ;;   zap
  26. ;; &key (like &body)??
  27.  
  28. ;; &rest 1 in lambda-lists doesn't work
  29. ;;  -- really want (foo bar
  30. ;;                  baz)
  31. ;;     not (foo bar
  32. ;;              baz)
  33. ;;  Need something better than &rest for such cases
  34.  
  35.  
  36. ;;; Hairy lisp indentation.
  37.  
  38. (defvar lisp-indent-maximum-backtracking 3
  39.   "*Maximum depth to backtrack out from a sublist for structured indentation.
  40. If this variable is  0, no backtracking will occur and forms such as  flet
  41. may not be correctly indented.")
  42.  
  43. (defvar lisp-tag-indentation 1
  44.   "*Indentation of tags relative to containing list.
  45. This variable is used by the function  lisp-indent-tagbody.")
  46.  
  47. (defvar lisp-tag-body-indentation 3
  48.   "*Indentation of non-tagged lines relative to containing list.
  49. This variable is used by the function  lisp-indent-tagbody  to indent normal
  50. lines (lines without tags).
  51. The indentation is relative to the indentation of the parenthesis enclosing
  52. he special form.  If the value is  t, the body of tags will be indented
  53. as a block at the same indentation as the first s-expression following
  54. the tag.  In this case, any forms before the first tag are indented
  55. by lisp-body-indent.")
  56.  
  57.  
  58. ;;;###autoload
  59. (defun common-lisp-indent-function (indent-point state)
  60.   (let ((normal-indent (current-column)))
  61.     ;; Walk up list levels until we see something
  62.     ;;  which does special things with subforms.
  63.     (let ((depth 0)
  64.           ;; Path describes the position of point in terms of
  65.           ;;  list-structure with respect to contining lists.
  66.           ;; `foo' has a path of (0 4 1) in `((a b c (d foo) f) g)'
  67.           (path ())
  68.           ;; set non-nil when somebody works out the indentation to use
  69.           calculated
  70.           (last-point indent-point)
  71.           ;; the position of the open-paren of the innermost containing list
  72.           (containing-form-start (elt state 1))
  73.           ;; the column of the above
  74.           sexp-column)
  75.       ;; Move to start of innermost containing list
  76.       (goto-char containing-form-start)
  77.       (setq sexp-column (current-column))
  78.       ;; Look over successively less-deep containing forms
  79.       (while (and (not calculated)
  80.                   (< depth lisp-indent-maximum-backtracking))
  81.         (let ((containing-sexp (point)))
  82.           (forward-char 1)
  83.           (parse-partial-sexp (point) indent-point 1 t)
  84.           ;; Move to the car of the relevant containing form
  85.           (let (tem function method)
  86.             (if (not (looking-at "\\sw\\|\\s_"))
  87.                 ;; This form doesn't seem to start with a symbol
  88.                 (setq function nil method nil)
  89.               (setq tem (point))
  90.               (forward-sexp 1)
  91.               (setq function (downcase (buffer-substring tem (point))))
  92.               (goto-char tem)
  93.               (setq tem (intern-soft function)
  94.                     method (get tem 'common-lisp-indent-function))
  95.               (cond ((and (null method)
  96.                           (string-match ":[^:]+" function))
  97.                      ;; The pleblisp package feature
  98.                      (setq function (substring function
  99.                                                (1+ (match-beginning 0)))
  100.                            method (get (intern-soft function)
  101.                                        'common-lisp-indent-function)))
  102.                     ((and (null method))
  103.                      ;; backwards compatibility
  104.                      (setq method (get tem 'lisp-indent-function)))))
  105.             (let ((n 0))
  106.               ;; How far into the containing form is the current form?
  107.               (if (< (point) indent-point)
  108.                   (while (condition-case ()
  109.                              (progn
  110.                                (forward-sexp 1)
  111.                                (if (>= (point) indent-point)
  112.                                    nil
  113.                                  (parse-partial-sexp (point)
  114.                                                      indent-point 1 t)
  115.                                  (setq n (1+ n))
  116.                                  t))
  117.                            (error nil))))
  118.               (setq path (cons n path)))
  119.  
  120.             ;; backwards compatibility.
  121.             (cond ((null function))
  122.                   ((null method)
  123.                    (if (null (cdr path))
  124.                        ;; (package prefix was stripped off above)
  125.                        (setq method (cond ((string-match "\\`def"
  126.                                                          function)
  127.                                            '(4 (&whole 4 &rest 1) &body))
  128.                                           ((string-match "\\`\\(with\\|do\\)-"
  129.                                                          function)
  130.                                            '(4 &body))))))
  131.                   ;; backwards compatibility.  Bletch.
  132.                   ((eq method 'defun)
  133.                    (setq method '(4 (&whole 4 &rest 1) &body))))
  134.  
  135.             (cond ((and (memq (char-after (1- containing-sexp)) '(?\' ?\`))
  136.                         (not (eq (char-after (- containing-sexp 2)) ?\#)))
  137.                    ;; No indentation for "'(...)" elements
  138.                    (setq calculated (1+ sexp-column)))
  139.           ((or (eq (char-after (1- containing-sexp)) ?\,)
  140.                (and (eq (char-after (1- containing-sexp)) ?\@)
  141.                 (eq (char-after (- containing-sexp 2)) ?\,)))
  142.            ;; ",(...)" or ",@(...)"
  143.            (setq calculated normal-indent))
  144.                   ((eq (char-after (1- containing-sexp)) ?\#)
  145.                    ;; "#(...)"
  146.                    (setq calculated (1+ sexp-column)))
  147.                   ((null method))
  148.                   ((integerp method)
  149.                    ;; convenient top-level hack.
  150.                    ;;  (also compatible with lisp-indent-function)
  151.                    ;; The number specifies how many `distinguished'
  152.                    ;;  forms there are before the body starts
  153.                    ;; Equivalent to (4 4 ... &body)
  154.                    (setq calculated (cond ((cdr path)
  155.                                            normal-indent)
  156.                                           ((<= (car path) method)
  157.                                            ;; `distinguished' form
  158.                                            (list (+ sexp-column 4)
  159.                                                  containing-form-start))
  160.                                           ((= (car path) (1+ method))
  161.                                            ;; first body form.
  162.                                            (+ sexp-column lisp-body-indent))
  163.                                           (t
  164.                                            ;; other body form
  165.                                            normal-indent))))
  166.                   ((symbolp method)
  167.                    (setq calculated (funcall method
  168.                                              path state indent-point
  169.                                              sexp-column normal-indent)))
  170.                   (t
  171.                    (setq calculated (lisp-indent-259
  172.                                       method path state indent-point
  173.                                       sexp-column normal-indent)))))
  174.           (goto-char containing-sexp)
  175.           (setq last-point containing-sexp)
  176.           (if (not calculated)
  177.               (condition-case ()
  178.                    (progn (backward-up-list 1)
  179.                           (setq depth (1+ depth)))
  180.                 (error (setq depth lisp-indent-maximum-backtracking))))))
  181.       calculated)))
  182.  
  183.  
  184. (defun lisp-indent-report-bad-format (m)
  185.   (error "%s has a badly-formed %s property: %s"
  186.          ;; Love them free variable references!!
  187.          function 'common-lisp-indent-function m))
  188.  
  189. ;; Blame the crufty control structure on dynamic scoping
  190. ;;  -- not on me!
  191. (defun lisp-indent-259 (method path state indent-point
  192.                         sexp-column normal-indent)
  193.   (catch 'exit
  194.     (let ((p path)
  195.           (containing-form-start (elt state 1))
  196.           n tem tail)
  197.       ;; Isn't tail-recursion wonderful?
  198.       (while p
  199.         ;; This while loop is for destructuring.
  200.         ;; p is set to (cdr p) each iteration.
  201.         (if (not (consp method)) (lisp-indent-report-bad-format method))
  202.         (setq n (1- (car p))
  203.               p (cdr p)
  204.               tail nil)
  205.         (while n
  206.           ;; This while loop is for advancing along a method
  207.           ;; until the relevant (possibly &rest/&body) pattern
  208.           ;; is reached.
  209.           ;; n is set to (1- n) and method to (cdr method)
  210.           ;; each iteration.
  211. ; (message "trying %s for %s %s" method p function) (sit-for 1)
  212.           (setq tem (car method))
  213.  
  214.           (or (eq tem 'nil)             ;default indentation
  215. ;             (eq tem '&lambda)         ;abbrev for (&whole 4 (&rest 1))
  216.               (and (eq tem '&body) (null (cdr method)))
  217.               (and (eq tem '&rest)
  218.                    (consp (cdr method)) (null (cdr (cdr method))))
  219.               (integerp tem)            ;explicit indentation specified
  220.               (and (consp tem)          ;destructuring
  221.                    (eq (car tem) '&whole)
  222.                    (or (symbolp (car (cdr tem)))
  223.                        (integerp (car (cdr tem)))))
  224.               (and (symbolp tem)        ;a function to call to do the work.
  225.                    (null (cdr method)))
  226.               (lisp-indent-report-bad-format method))
  227.  
  228.           (cond ((and tail (not (consp tem)))
  229.                  ;; indent tail of &rest in same way as first elt of rest
  230.                  (throw 'exit normal-indent))
  231.                 ((eq tem '&body)
  232.                  ;; &body means (&rest <lisp-body-indent>)
  233.                  (throw 'exit
  234.                    (if (and (= n 0)     ;first body form
  235.                             (null p))   ;not in subforms
  236.                        (+ sexp-column
  237.                           lisp-body-indent)
  238.                        normal-indent)))
  239.                 ((eq tem '&rest)
  240.                  ;; this pattern holds for all remaining forms
  241.                  (setq tail (> n 0)
  242.                        n 0
  243.                        method (cdr method)))
  244.                 ((> n 0)
  245.                  ;; try next element of pattern
  246.                  (setq n (1- n)
  247.                        method (cdr method))
  248.                  (if (< n 0)
  249.                      ;; Too few elements in pattern.
  250.                      (throw 'exit normal-indent)))
  251.                 ((eq tem 'nil)
  252.                  (throw 'exit (list normal-indent containing-form-start)))
  253. ;               ((eq tem '&lambda)
  254. ;                ;; abbrev for (&whole 4 &rest 1)
  255. ;                (throw 'exit
  256. ;                  (cond ((null p)
  257. ;                         (list (+ sexp-column 4) containing-form-start))
  258. ;                        ((null (cdr p))
  259. ;                         (+ sexp-column 1))
  260. ;                        (t normal-indent))))
  261.                 ((integerp tem)
  262.                  (throw 'exit
  263.                    (if (null p)         ;not in subforms
  264.                        (list (+ sexp-column tem) containing-form-start)
  265.                        normal-indent)))
  266.                 ((symbolp tem)          ;a function to call
  267.                  (throw 'exit
  268.                    (funcall tem path state indent-point
  269.                             sexp-column normal-indent)))
  270.                 (t
  271.                  ;; must be a destructing frob
  272.                  (if (not (null p))
  273.                      ;; descend
  274.                      (setq method (cdr (cdr tem))
  275.                            n nil)
  276.                    (setq tem (car (cdr tem)))
  277.                    (throw 'exit
  278.                      (cond (tail
  279.                             normal-indent)
  280.                            ((eq tem 'nil)
  281.                             (list normal-indent
  282.                                   containing-form-start))
  283.                            ((integerp tem)
  284.                             (list (+ sexp-column tem)
  285.                                   containing-form-start))
  286.                            (t
  287.                             (funcall tem path state indent-point
  288.                                      sexp-column normal-indent))))))))))))
  289.  
  290. (defun lisp-indent-tagbody (path state indent-point sexp-column normal-indent)
  291.   (if (not (null (cdr path)))
  292.       normal-indent
  293.     (save-excursion
  294.       (goto-char indent-point)
  295.       (beginning-of-line)
  296.       (skip-chars-forward " \t")
  297.       (list (cond ((looking-at "\\sw\\|\\s_")
  298.                    ;; a tagbody tag
  299.                    (+ sexp-column lisp-tag-indentation))
  300.                   ((integerp lisp-tag-body-indentation)
  301.                    (+ sexp-column lisp-tag-body-indentation))
  302.                   ((eq lisp-tag-body-indentation 't)
  303.                    (condition-case ()
  304.                        (progn (backward-sexp 1) (current-column))
  305.                      (error (1+ sexp-column))))
  306.                   (t (+ sexp-column lisp-body-indent)))
  307. ;            (cond ((integerp lisp-tag-body-indentation)
  308. ;                   (+ sexp-column lisp-tag-body-indentation))
  309. ;                  ((eq lisp-tag-body-indentation 't)
  310. ;                   normal-indent)
  311. ;                  (t
  312. ;                   (+ sexp-column lisp-body-indent)))
  313.             (elt state 1)
  314.             ))))
  315.  
  316. (defun lisp-indent-do (path state indent-point sexp-column normal-indent)
  317.   (if (>= (car path) 3)
  318.       (let ((lisp-tag-body-indentation lisp-body-indent))
  319.         (funcall (function lisp-indent-tagbody)
  320.          path state indent-point sexp-column normal-indent))
  321.     (funcall (function lisp-indent-259)
  322.          '((&whole nil &rest
  323.          ;; the following causes wierd indentation
  324.          ;;(&whole 1 1 2 nil)
  325.         )
  326.            (&whole nil &rest 1))
  327.          path state indent-point sexp-column normal-indent)))
  328.  
  329. (defun lisp-indent-function-lambda-hack (path state indent-point
  330.                                          sexp-column normal-indent)
  331.   ;; indent (function (lambda () <newline> <body-forms>)) kludgily.
  332.   (if (or (cdr path) ; wtf?
  333.           (> (car path) 3))
  334.       ;; line up under previous body form
  335.       normal-indent
  336.     ;; line up under function rather than under lambda in order to
  337.     ;;  conserve horizontal space.  (Which is what #' is for.)
  338.     (condition-case ()
  339.         (save-excursion
  340.           (backward-up-list 2)
  341.           (forward-char 1)
  342.           (if (looking-at "\\(lisp:+\\)?function\\(\\Sw\\|\\S_\\)")
  343.               (+ lisp-body-indent -1 (current-column))
  344.               (+ sexp-column lisp-body-indent)))
  345.        (error (+ sexp-column lisp-body-indent)))))
  346.  
  347. (defun lisp-indent-defmethod (path state indent-point
  348.                               sexp-column normal-indent)
  349.   ;; Look for a method combination specifier...
  350.   (let* ((combined (if (and (>= (car path) 3)
  351.                             (null (cdr path)))
  352.                        (save-excursion
  353.                          (goto-char (car (cdr state)))
  354.                          (forward-char)
  355.                          (forward-sexp)
  356.                          (forward-sexp)
  357.                          (forward-sexp)
  358.                          (backward-sexp)
  359.                          (if (looking-at ":")
  360.                              t
  361.                              nil))
  362.                        nil))
  363.      (method (if combined
  364.              '(4 4 (&whole 4 &rest 1) &body)
  365.              '(4 (&whole 4 &rest 1) &body))))
  366.     (funcall (function lisp-indent-259)
  367.          method
  368.          path state indent-point sexp-column normal-indent)))
  369.  
  370. (let ((l '((block 1)
  371.        (catch 1)
  372.            (case        (4 &rest (&whole 2 &rest 1)))
  373.            (ccase . case) (ecase . case)
  374.            (typecase . case) (etypecase . case) (ctypecase . case)
  375.            (catch 1)
  376.            (cond        (&rest (&whole 2 &rest 1)))
  377.            (block 1)
  378.            (defvar      (4 2 2))
  379.            (defconstant . defvar) (defparameter . defvar)
  380.            (define-modify-macro
  381.                         (4 &body))
  382.            (define-setf-method
  383.                         (4 (&whole 4 &rest 1) &body))
  384.            (defsetf     (4 (&whole 4 &rest 1) 4 &body))
  385.            (defun       (4 (&whole 4 &rest 1) &body))
  386.            (defmacro . defun) (deftype . defun)
  387.            (defmethod lisp-indent-defmethod)
  388.            (defstruct   ((&whole 4 &rest (&whole 2 &rest 1))
  389.                          &rest (&whole 2 &rest 1)))
  390.            (destructuring-bind
  391.                         ((&whole 6 &rest 1) 4 &body))
  392.            (do          lisp-indent-do)
  393.            (do* . do)
  394.            (dolist      ((&whole 4 2 1) &body))
  395.            (dotimes . dolist)
  396.            (eval-when    1)
  397.            (flet        ((&whole 4 &rest (&whole 1 (&whole 4 &rest 1) &body))
  398.                          &body))
  399.            (labels . flet)
  400.            (macrolet . flet)
  401.            ;; `else-body' style
  402.            (if          (nil nil &body))
  403.            ;; single-else style (then and else equally indented)
  404.            (if          (&rest nil))
  405.            ;(lambda     ((&whole 4 &rest 1) &body))
  406.            (lambda      ((&whole 4 &rest 1)
  407.                          &rest lisp-indent-function-lambda-hack))
  408.            (let         ((&whole 4 &rest (&whole 1 1 2)) &body))
  409.            (let* . let)
  410.            (compiler-let . let) ;barf
  411.            (locally    1)
  412.            ;(loop ...)
  413.            (multiple-value-bind
  414.                         ((&whole 6 &rest 1) 4 &body))
  415.            (multiple-value-call
  416.             (4 &body))
  417.            (multiple-value-list 1)
  418.            (multiple-value-prog1 1)
  419.            (multiple-value-setq
  420.             (4 2))
  421.            ;; Combines the worst features of BLOCK, LET and TAGBODY
  422.            (prog        ((&whole 4 &rest 1) &rest lisp-indent-tagbody))
  423.            (prog* . prog)
  424.            (prog1 1)
  425.            (prog2 2)
  426.            (progn 0)
  427.            (progv       (4 4 &body))
  428.            (return 0)
  429.            (return-from (nil &body))
  430.            (tagbody     lisp-indent-tagbody)
  431.            (throw 1)
  432.            (unless 1)
  433.            (unwind-protect
  434.                         (5 &body))
  435.            (when 1))))
  436.   (while l
  437.     (put (car (car l)) 'common-lisp-indent-function
  438.          (if (symbolp (cdr (car l)))
  439.              (get (cdr (car l)) 'common-lisp-indent-function)
  440.              (car (cdr (car l)))))
  441.     (setq l (cdr l))))
  442.  
  443.  
  444. ;(defun foo (x)
  445. ;  (tagbody
  446. ;   foo
  447. ;     (bar)
  448. ;   baz
  449. ;     (when (losing)
  450. ;       (with-big-loser
  451. ;           (yow)
  452. ;         ((lambda ()
  453. ;            foo)
  454. ;          big)))
  455. ;     (flet ((foo (bar baz zap)
  456. ;              (zip))
  457. ;            (zot ()
  458. ;              quux))
  459. ;       (do ()
  460. ;           ((lose)
  461. ;            (foo 1))
  462. ;         (quux)
  463. ;        foo
  464. ;         (lose))
  465. ;       (cond ((x)
  466. ;              (win 1 2
  467. ;                   (foo)))
  468. ;             (t
  469. ;              (lose
  470. ;                3))))))
  471.           
  472.  
  473. ;(put 'while    'common-lisp-indent-function 1)
  474. ;(put 'defwrapper'common-lisp-indent-function ...)
  475. ;(put 'def 'common-lisp-indent-function ...)
  476. ;(put 'defflavor        'common-lisp-indent-function ...)
  477. ;(put 'defsubst 'common-lisp-indent-function ...)
  478.  
  479. ;(put 'with-restart 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
  480. ;(put 'restart-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (* 1)))))
  481. ;(put 'define-condition 'common-lisp-indent-function '((1 6) (2 6 ((* 1))) (3 4 ((* 1))) (4 &body)))
  482. ;(put 'with-condition-handler 'common-lisp-indent-function '((1 4 ((* 1))) (2 &body)))
  483. ;(put 'condition-case 'common-lisp-indent-function '((1 4) (* 2 ((0 1) (1 3) (2 &body)))))
  484.  
  485.  
  486. ;;;; Turn it on.
  487. ;(setq lisp-indent-function 'common-lisp-indent-function)
  488.  
  489. ;; To disable this stuff, (setq lisp-indent-function 'lisp-indent-function)
  490.  
  491.