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 / cl-indent.el < prev    next >
Encoding:
Text File  |  1992-06-29  |  18.9 KB  |  467 lines

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