;;; From: Thorbjoern Hansen <hansen@forwiss.uni-passau.de>
;;; Date: Wed Jan 31 19:10:01 1996
;;; Subject: Re: Changing indentation of the closing ")" in Lisp?
;;; SYNOPSIS: Jari's emacs lisp indentation function.
;;; Original by Jari Aalto <jaalto@tre.tele.nokia.fi>, jan 1996.
;;; Modified by Thorbjxrn Hansen <hansen@forwiss.uni-passau.de>, jan 1996.
;;
;; Description:
;;
;;   If a line starts with one or several `)', indent the line to the
;;   same level as the `(' that matches the last `)'. Other lines are
;;   indented as by `lisp-indent-line'."
;;
;; Example:
;;
;;   New indentation                    Old indentation
;;
;;   (if something                      (if something
;;       (progn (something)                 (progn (something)
;;              (something)                        (something)
;;       )                                         )
;;     (while something                   (while something
;;       (something)                        (something)
;;     )                                    )
;;   )                                    )
;;
;; Installation:
;;
;;   Put this into your ~/.emacs
;;
;;   (defun my-lisp-hook ()
;;     (define-key shared-lisp-mode-map "\t" 'my-lisp-indent-line)
;;     (setq indent-line-function 'my-lisp-indent-line)
;;     (require 'lisp-patch)
;;   )
;;
;;   (add-hook 'emacs-lisp-mode-hook 'my-lisp-hook)
;;   (add-hook 'lisp-interaction-mode-hook 'my-lisp-hook)
;;   (add-hook 'lisp-mode-hook 'my-lisp-hook)

(defun my-lisp-indent-line (&optional whole-exp)
  "Indent current line as Lisp code.
   With argument, indent any additional lines of the same expression
   rigidly along with this one.
   If a line starts with one or several `)', indent the line to the same
   level as the `(' that matches the last `)'. Other lines are indented
   as by `lisp-indent-line'."
  (interactive "P")
  (let ( col start-line end-paren start-paren cursor-in-indentation)
    (cond ((save-excursion
             (beginning-of-line)
             (looking-at "^[ \t]*\\(\\))[ \t)]*"))
           ;; We found a line that starts with `)'.

           (setq start-line (match-beginning 0)
                 start-paren (match-beginning 1)
                 end-paren (match-end 0)
                 cursor-in-indentation (< (point) start-paren))

           ;; Find the indentation of the `(' that matches the last `)'.
           (save-excursion
             (goto-char end-paren)
             (condition-case nil
                 (progn
                   (backward-sexp 1)
                   (setq col (current-column))
                 )
               (error (message "No matching paren found."))
             )
           )

           ;; If we found a matching `(', indent according to `col'.
           (cond (col
                  (delete-region start-line start-paren)
                  (save-excursion
                    (goto-char start-line)
                    (indent-to col)
                  )
                  ;; If initial point was within the line's
                  ;; indentation, position after the indentation. Else
                  ;; stay at same point in text.
                  (if cursor-in-indentation
                      (goto-char (+ start-line col))
                  )
                 )
           )
          )
          (t
           (call-interactively 'lisp-indent-line whole-exp)
          )
    )
  )
)

; (provide 'lisp-indent)
(provide 'indentel)

;; EOF

