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 / lisp-mode.el < prev    next >
Encoding:
Text File  |  1993-01-15  |  23.7 KB  |  630 lines

  1. ;; Lisp mode, and its idiosyncratic commands.
  2. ;; Copyright (C) 1985-1993 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 2, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  
  21. (defvar lisp-mode-syntax-table nil "")
  22. (defvar emacs-lisp-mode-syntax-table nil "")
  23. (defvar lisp-mode-abbrev-table nil "")
  24.  
  25. (if (not emacs-lisp-mode-syntax-table)
  26.     (let ((i 0))
  27.       (setq emacs-lisp-mode-syntax-table (make-syntax-table))
  28.       (while (< i ?0)
  29.     (modify-syntax-entry i "_   " emacs-lisp-mode-syntax-table)
  30.     (setq i (1+ i)))
  31.       (setq i (1+ ?9))
  32.       (while (< i ?A)
  33.     (modify-syntax-entry i "_   " emacs-lisp-mode-syntax-table)
  34.     (setq i (1+ i)))
  35.       (setq i (1+ ?Z))
  36.       (while (< i ?a)
  37.     (modify-syntax-entry i "_   " emacs-lisp-mode-syntax-table)
  38.     (setq i (1+ i)))
  39.       (setq i (1+ ?z))
  40.       (while (< i 128)
  41.     (modify-syntax-entry i "_   " emacs-lisp-mode-syntax-table)
  42.     (setq i (1+ i)))
  43.       (modify-syntax-entry ?  "    " emacs-lisp-mode-syntax-table)
  44.       (modify-syntax-entry ?\t "    " emacs-lisp-mode-syntax-table)
  45.       (modify-syntax-entry ?\n ">   " emacs-lisp-mode-syntax-table)
  46.       (modify-syntax-entry ?\f ">   " emacs-lisp-mode-syntax-table)
  47.       (modify-syntax-entry ?\; "<   " emacs-lisp-mode-syntax-table)
  48.       (modify-syntax-entry ?` "'   " emacs-lisp-mode-syntax-table)
  49.       (modify-syntax-entry ?' "'   " emacs-lisp-mode-syntax-table)
  50.       (modify-syntax-entry ?, "'   " emacs-lisp-mode-syntax-table)
  51.       ;; Used to be singlequote; changed for flonums.
  52.       (modify-syntax-entry ?. "_   " emacs-lisp-mode-syntax-table)
  53.       (modify-syntax-entry ?# "'   " emacs-lisp-mode-syntax-table)
  54.       (modify-syntax-entry ?\" "\"    " emacs-lisp-mode-syntax-table)
  55.       (modify-syntax-entry ?\\ "\\   " emacs-lisp-mode-syntax-table)
  56.       (modify-syntax-entry ?\( "()  " emacs-lisp-mode-syntax-table)
  57.       (modify-syntax-entry ?\) ")(  " emacs-lisp-mode-syntax-table)
  58.       (modify-syntax-entry ?\[ "(]  " emacs-lisp-mode-syntax-table)
  59.       (modify-syntax-entry ?\] ")[  " emacs-lisp-mode-syntax-table)))
  60.  
  61. (if lisp-mode-syntax-table
  62.     nil
  63.   (setq lisp-mode-syntax-table
  64.     (copy-syntax-table emacs-lisp-mode-syntax-table))
  65.   (modify-syntax-entry ?\[ "_   " lisp-mode-syntax-table)
  66.   (modify-syntax-entry ?\] "_   " lisp-mode-syntax-table)
  67.  
  68.   ;;
  69.   ;; If emacs was compiled with NEW_SYNTAX, then do CL's #| |# block comments.
  70.   ;;
  71.   (if (= 8 (length (parse-partial-sexp (point) (point))))
  72.       (progn
  73.     (modify-syntax-entry ?#  "' 58" lisp-mode-syntax-table)
  74.     (modify-syntax-entry ?|  ". 67" lisp-mode-syntax-table))
  75.     ;; else, old style
  76.     (modify-syntax-entry ?\| "\"   " lisp-mode-syntax-table)))
  77.  
  78. (define-abbrev-table 'lisp-mode-abbrev-table ())
  79.  
  80. (defun lisp-mode-variables (lisp-syntax)
  81.   (if lisp-syntax
  82.       (set-syntax-table lisp-mode-syntax-table))
  83.   (setq local-abbrev-table lisp-mode-abbrev-table)
  84.   (make-local-variable 'paragraph-start)
  85.   (setq paragraph-start (concat "^$\\|" page-delimiter))
  86.   (make-local-variable 'paragraph-separate)
  87.   (setq paragraph-separate paragraph-start)
  88.   (make-local-variable 'paragraph-ignore-fill-prefix)
  89.   (setq paragraph-ignore-fill-prefix t)
  90.   (make-local-variable 'indent-line-function)
  91.   (setq indent-line-function 'lisp-indent-line)
  92.   (make-local-variable 'indent-region-function)
  93.   (setq indent-region-function 'lisp-indent-region)
  94.   (make-local-variable 'parse-sexp-ignore-comments)
  95.   (setq parse-sexp-ignore-comments t)
  96.   (make-local-variable 'comment-start)
  97.   (setq comment-start ";")
  98.   (make-local-variable 'comment-start-skip)
  99.   (setq comment-start-skip ";+ *")
  100.   (make-local-variable 'comment-column)
  101.   (setq comment-column 40)
  102.   (make-local-variable 'comment-indent-hook)
  103.   (setq comment-indent-hook 'lisp-comment-indent))
  104.  
  105. (defvar shared-lisp-mode-map ()
  106.   "Keymap for commands shared by all sorts of Lisp modes.")
  107.  
  108. (if shared-lisp-mode-map
  109.     ()
  110.    (setq shared-lisp-mode-map (make-sparse-keymap))
  111.    (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp)
  112.    (define-key shared-lisp-mode-map "\177" 'backward-delete-char-untabify)
  113.    (define-key shared-lisp-mode-map "\t" 'lisp-indent-line))
  114.  
  115. (defvar emacs-lisp-mode-map ()
  116.   "Keymap for Emacs Lisp mode.
  117. All commands in shared-lisp-mode-map are inherited by this map.")
  118.  
  119. (if emacs-lisp-mode-map
  120.     ()
  121.   (setq emacs-lisp-mode-map (make-sparse-keymap))
  122.   (set-keymap-parent emacs-lisp-mode-map shared-lisp-mode-map)
  123.   (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun))
  124.  
  125. (defun emacs-lisp-mode ()
  126.   "Major mode for editing Lisp code to run in Emacs.
  127. Commands:
  128. Delete converts tabs to spaces as it moves back.
  129. Blank lines separate paragraphs.  Semicolons start comments.
  130. \\{emacs-lisp-mode-map}
  131. Entry to this mode calls the value of emacs-lisp-mode-hook
  132. if that value is non-nil."
  133.   (interactive)
  134.   (kill-all-local-variables)
  135.   (use-local-map emacs-lisp-mode-map)
  136.   (set-syntax-table emacs-lisp-mode-syntax-table)
  137.   (setq major-mode 'emacs-lisp-mode)
  138.   (setq mode-name "Emacs-Lisp")
  139.   (lisp-mode-variables nil)
  140.   (run-hooks 'emacs-lisp-mode-hook))
  141.  
  142. (defvar lisp-mode-map ()
  143.   "Keymap for ordinary Lisp mode.
  144. All commands in shared-lisp-mode-map are inherited by this map.")
  145.  
  146. (if lisp-mode-map
  147.     ()
  148.   (setq lisp-mode-map (make-sparse-keymap))
  149.   (set-keymap-parent lisp-mode-map shared-lisp-mode-map)
  150.   (define-key lisp-mode-map "\e\C-x" 'lisp-send-defun)
  151.   (define-key lisp-mode-map "\C-c\C-l" 'run-lisp))
  152.  
  153. (defun lisp-mode ()
  154.   "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
  155. Commands:
  156. Delete converts tabs to spaces as it moves back.
  157. Blank lines separate paragraphs.  Semicolons start comments.
  158. \\{lisp-mode-map}
  159. Note that `run-lisp' may be used either to start an inferior Lisp job
  160. or to switch back to an existing one.
  161.  
  162. Entry to this mode calls the value of lisp-mode-hook
  163. if that value is non-nil."
  164.   (interactive)
  165.   (kill-all-local-variables)
  166.   (use-local-map lisp-mode-map)
  167.   (setq major-mode 'lisp-mode)
  168.   (setq mode-name "Lisp")
  169.   (lisp-mode-variables t)
  170.   (set-syntax-table lisp-mode-syntax-table)
  171.   (run-hooks 'lisp-mode-hook))
  172.  
  173. ;; This will do unless shell.el is loaded.
  174. (defun lisp-send-defun nil
  175.   "Send the current defun to the Lisp process made by M-x run-lisp."
  176.   (interactive)
  177.   (error "Process lisp does not exist"))
  178.  
  179. (defvar lisp-interaction-mode-map ()
  180.   "Keymap for Lisp Interaction moe.
  181. All commands in shared-lisp-mode-map are inherited by this map.")
  182.  
  183. (if lisp-interaction-mode-map
  184.     ()
  185.   (setq lisp-interaction-mode-map (make-sparse-keymap))
  186.   (set-keymap-parent lisp-interaction-mode-map shared-lisp-mode-map)
  187.   (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun)
  188.   (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp)
  189.   )
  190.  
  191. (defun lisp-interaction-mode ()
  192.   "Major mode for typing and evaluating Lisp forms.
  193. Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
  194. before point, and prints its value into the buffer, advancing point.
  195.  
  196. Commands:
  197. Delete converts tabs to spaces as it moves back.
  198. Paragraphs are separated only by blank lines.  Semicolons start comments.
  199. \\{lisp-interaction-mode-map}
  200. Entry to this mode calls the value of lisp-interaction-mode-hook
  201. if that value is non-nil."
  202.   (interactive)
  203.   (kill-all-local-variables)
  204.   (use-local-map lisp-interaction-mode-map)
  205.   (setq major-mode 'lisp-interaction-mode)
  206.   (setq mode-name "Lisp Interaction")
  207.   (set-syntax-table emacs-lisp-mode-syntax-table)
  208.   (lisp-mode-variables nil)
  209.   (run-hooks 'lisp-interaction-mode-hook))
  210.  
  211. (defun eval-print-last-sexp (arg)
  212.   "Evaluate sexp before point; print value into current buffer."
  213.   (interactive "P")
  214.   (eval-region
  215.     (let ((stab (syntax-table)))
  216.       (unwind-protect
  217.       (save-excursion
  218.         (set-syntax-table emacs-lisp-mode-syntax-table)
  219.         (forward-sexp -1)
  220.         (point))
  221.     (set-syntax-table stab)))
  222.     (point)
  223.     (current-buffer)))
  224.  
  225. (defun eval-last-sexp (arg)
  226.   "Evaluate sexp before point; print value in minibuffer.
  227. With argument, print output into current buffer."
  228.   (interactive "P")
  229.   (eval-region
  230.     (let ((stab (syntax-table)))
  231.       (unwind-protect
  232.       (save-excursion
  233.         (set-syntax-table emacs-lisp-mode-syntax-table)
  234.         (forward-sexp -1)
  235.         (point))
  236.     (set-syntax-table stab)))
  237.     (point)
  238.     (if arg (current-buffer) t)))
  239.  
  240. (defun eval-defun (arg)
  241.   "Evaluate defun that point is in or before.
  242. Print value in minibuffer.
  243. With argument, insert value in current buffer after the defun."
  244.   (interactive "P")
  245.   (save-excursion
  246.     (end-of-defun)
  247.     (let ((end (point)))
  248.       (beginning-of-defun)
  249.       (eval-region (point) end
  250.            (if arg (current-buffer) t)))))
  251.  
  252. (defun lisp-comment-indent ()
  253.   (if (looking-at "\\s<\\s<\\s<")
  254.       (current-column)
  255.     (if (looking-at "\\s<\\s<")
  256.     (let ((tem (calculate-lisp-indent)))
  257.       (if (listp tem) (car tem) tem))
  258.       (skip-chars-backward " \t")
  259.       (max (if (bolp) 0 (1+ (current-column)))
  260.        comment-column))))
  261.  
  262. (defconst lisp-indent-offset nil "")
  263. (defconst lisp-indent-function 'lisp-indent-function "")
  264.  
  265. (defun lisp-indent-line (&optional whole-exp)
  266.   "Indent current line as Lisp code.
  267. With argument, indent any additional lines of the same expression
  268. rigidly along with this one."
  269.   (interactive "P")
  270.   (let ((indent (calculate-lisp-indent)) shift-amt beg end
  271.     (pos (- (point-max) (point))))
  272.     (beginning-of-line)
  273.     (setq beg (point))
  274.     (skip-chars-forward " \t")
  275.     (if (looking-at "\\s<\\s<\\s<")
  276.     ;; Don't alter indentation of a ;;; comment line.
  277.     nil
  278.       (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
  279.       ;; Single-semicolon comment lines should be indented
  280.       ;; as comment lines, not as code.
  281.       (progn (indent-for-comment) (forward-char -1))
  282.     (if (listp indent) (setq indent (car indent)))
  283.     (setq shift-amt (- indent (current-column)))
  284.     (if (zerop shift-amt)
  285.         nil
  286.       (delete-region beg (point))
  287.       (indent-to indent)))
  288.       ;; If initial point was within line's indentation,
  289.       ;; position after the indentation.  Else stay at same point in text.
  290.       (if (> (- (point-max) pos) (point))
  291.       (goto-char (- (point-max) pos)))
  292.       ;; If desired, shift remaining lines of expression the same amount.
  293.       (and whole-exp (not (zerop shift-amt))
  294.        (save-excursion
  295.          (goto-char beg)
  296.          (forward-sexp 1)
  297.          (setq end (point))
  298.          (goto-char beg)
  299.          (forward-line 1)
  300.          (setq beg (point))
  301.          (> end beg))
  302.        (indent-code-rigidly beg end shift-amt)))))
  303.  
  304. (defun calculate-lisp-indent (&optional parse-start)
  305.   "Return appropriate indentation for current line as Lisp code.
  306. In usual case returns an integer: the column to indent to.
  307. Can instead return a list, whose car is the column to indent to.
  308. This means that following lines at the same level of indentation
  309. should not necessarily be indented the same way.
  310. The second element of the list is the buffer position
  311. of the start of the containing expression."
  312.   (save-excursion
  313.     (beginning-of-line)
  314.     (let ((indent-point (point))
  315.           state paren-depth
  316.           ;; setting this to a number inhibits calling hook
  317.           (desired-indent nil)
  318.           (retry t)
  319.           last-sexp containing-sexp)
  320.       (if parse-start
  321.           (goto-char parse-start)
  322.           (beginning-of-defun))
  323.       ;; Find outermost containing sexp
  324.       (while (< (point) indent-point)
  325.         (setq state (parse-partial-sexp (point) indent-point 0)))
  326.       ;; Find innermost containing sexp
  327.       (while (and retry
  328.           state
  329.                   (> (setq paren-depth (elt state 0)) 0))
  330.         (setq retry nil)
  331.         (setq last-sexp (elt state 2))
  332.         (setq containing-sexp (elt state 1))
  333.         ;; Position following last unclosed open.
  334.         (goto-char (1+ containing-sexp))
  335.         ;; Is there a complete sexp since then?
  336.         (if (and last-sexp (> last-sexp (point)))
  337.             ;; Yes, but is there a containing sexp after that?
  338.             (let ((peek (parse-partial-sexp last-sexp indent-point 0)))
  339.               (if (setq retry (car (cdr peek))) (setq state peek)))))
  340.       (if retry
  341.           nil
  342.         ;; Innermost containing sexp found
  343.         (goto-char (1+ containing-sexp))
  344.         (if (not last-sexp)
  345.         ;; indent-point immediately follows open paren.
  346.         ;; Don't call hook.
  347.             (setq desired-indent (current-column))
  348.       ;; Find the start of first element of containing sexp.
  349.       (parse-partial-sexp (point) last-sexp 0 t)
  350.       (cond ((looking-at "\\s(")
  351.          ;; First element of containing sexp is a list.
  352.          ;; Indent under that list.
  353.          )
  354.         ((> (save-excursion (forward-line 1) (point))
  355.             last-sexp)
  356.          ;; This is the first line to start within the containing sexp.
  357.          ;; It's almost certainly a function call.
  358.          (if (= (point) last-sexp)
  359.              ;; Containing sexp has nothing before this line
  360.              ;; except the first element.  Indent under that element.
  361.              nil
  362.            ;; Skip the first element, find start of second (the first
  363.            ;; argument of the function call) and indent under.
  364.            (progn (forward-sexp 1)
  365.               (parse-partial-sexp (point) last-sexp 0 t)))
  366.          (backward-prefix-chars))
  367.         (t
  368.          ;; Indent beneath first sexp on same line as last-sexp.
  369.          ;; Again, it's almost certainly a function call.
  370.          (goto-char last-sexp)
  371.          (beginning-of-line)
  372.          (parse-partial-sexp (point) last-sexp 0 t)
  373.          (backward-prefix-chars)))))
  374.       ;; Point is at the point to indent under unless we are inside a string.
  375.       ;; Call indentation hook except when overriden by lisp-indent-offset
  376.       ;; or if the desired indentation has already been computed.
  377.       (let ((normal-indent (current-column)))
  378.         (cond ((elt state 3)
  379.                ;; Inside a string, don't change indentation.
  380.                (goto-char indent-point)
  381.                (skip-chars-forward " \t")
  382.                (current-column))
  383.               ((and (integerp lisp-indent-offset) containing-sexp)
  384.                ;; Indent by constant offset
  385.                (goto-char containing-sexp)
  386.                (+ (current-column) lisp-indent-offset))
  387.               (desired-indent)
  388.               ((and (boundp 'lisp-indent-function)
  389.                     lisp-indent-function
  390.                     (not retry))
  391.                (or (funcall lisp-indent-function indent-point state)
  392.                    normal-indent))
  393.               (t
  394.                normal-indent))))))
  395.  
  396. (defun lisp-indent-function (indent-point state)
  397.   (let ((normal-indent (current-column)))
  398.     (goto-char (1+ (elt state 1)))
  399.     (parse-partial-sexp (point) last-sexp 0 t)
  400.     (if (and (elt state 2)
  401.              (not (looking-at "\\sw\\|\\s_")))
  402.         ;; car of form doesn't seem to be a a symbol
  403.         (progn
  404.           (if (not (> (save-excursion (forward-line 1) (point))
  405.                       last-sexp))
  406.               (progn (goto-char last-sexp)
  407.                      (beginning-of-line)
  408.                      (parse-partial-sexp (point) last-sexp 0 t)))
  409.           ;; Indent under the list or under the first sexp on the
  410.           ;; same line as last-sexp.  Note that first thing on that
  411.           ;; line has to be complete sexp since we are inside the
  412.           ;; innermost containing sexp.
  413.           (backward-prefix-chars)
  414.           (current-column))
  415.       (let ((function (buffer-substring (point)
  416.                     (progn (forward-sexp 1) (point))))
  417.         method)
  418.     (setq method (get (intern-soft function) 'lisp-indent-function))
  419.     (cond ((or (eq method 'defun)
  420.            (and (null method)
  421.             (> (length function) 3)
  422.             (string-match "\\`def" function)))
  423.            (lisp-indent-defform state indent-point))
  424.           ((integerp method)
  425.            (lisp-indent-specform method state
  426.                      indent-point normal-indent))
  427.           (method
  428.         (funcall method state indent-point)))))))
  429.  
  430. (defconst lisp-body-indent 2 "")
  431.  
  432. (defun lisp-indent-specform (count state indent-point normal-indent)
  433.   (let ((containing-form-start (elt state 1))
  434.         (i count)
  435.         body-indent containing-form-column)
  436.     ;; Move to the start of containing form, calculate indentation
  437.     ;; to use for non-distinguished forms (> count), and move past the
  438.     ;; function symbol.  lisp-indent-function guarantees that there is at
  439.     ;; least one word or symbol character following open paren of containing
  440.     ;; form.
  441.     (goto-char containing-form-start)
  442.     (setq containing-form-column (current-column))
  443.     (setq body-indent (+ lisp-body-indent containing-form-column))
  444.     (forward-char 1)
  445.     (forward-sexp 1)
  446.     ;; Now find the start of the last form.
  447.     (parse-partial-sexp (point) indent-point 1 t)
  448.     (while (and (< (point) indent-point)
  449.                 (condition-case ()
  450.                     (progn
  451.                       (setq count (1- count))
  452.                       (forward-sexp 1)
  453.                       (parse-partial-sexp (point) indent-point 1 t))
  454.                   (error nil))))
  455.     ;; Point is sitting on first character of last (or count) sexp.
  456.     (if (> count 0)
  457.         ;; A distinguished form.  If it is the first or second form use double
  458.         ;; lisp-body-indent, else normal indent.  With lisp-body-indent bound
  459.         ;; to 2 (the default), this just happens to work the same with if as
  460.         ;; the older code, but it makes unwind-protect, condition-case,
  461.         ;; with-output-to-temp-buffer, et. al. much more tasteful.  The older,
  462.         ;; less hacked, behavior can be obtained by replacing below with
  463.         ;; (list normal-indent containing-form-start).
  464.         (if (<= (- i count) 1)
  465.             (list (+ containing-form-column (* 2 lisp-body-indent))
  466.                   containing-form-start)
  467.             (list normal-indent containing-form-start))
  468.       ;; A non-distinguished form.  Use body-indent if there are no
  469.       ;; distinguished forms and this is the first undistinguished form,
  470.       ;; or if this is the first undistinguished form and the preceding
  471.       ;; distinguished form has indentation at least as great as body-indent.
  472.       (if (or (and (= i 0) (= count 0))
  473.               (and (= count 0) (<= body-indent normal-indent)))
  474.           body-indent
  475.           normal-indent))))
  476.  
  477. (defun lisp-indent-defform (state indent-point)
  478.   (goto-char (car (cdr state)))
  479.   (forward-line 1)
  480.   (if (> (point) (car (cdr (cdr state))))
  481.       (progn
  482.     (goto-char (car (cdr state)))
  483.     (+ lisp-body-indent (current-column)))))
  484.  
  485.  
  486. ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
  487. ;; like defun if the first form is placed on the next line, otherwise
  488. ;; it is indented like any other form (i.e. forms line up under first).
  489.  
  490. (put 'lambda 'lisp-indent-function 'defun)
  491. (put 'progn 'lisp-indent-function 0)
  492. (put 'prog1 'lisp-indent-function 1)
  493. (put 'prog2 'lisp-indent-function 2)
  494. (put 'save-excursion 'lisp-indent-function 0)
  495. (put 'save-window-excursion 'lisp-indent-function 0)
  496. (put 'save-restriction 'lisp-indent-function 0)
  497. (put 'let 'lisp-indent-function 1)
  498. (put 'let* 'lisp-indent-function 1)
  499. (put 'while 'lisp-indent-function 1)
  500. (put 'if 'lisp-indent-function 2)
  501. (put 'catch 'lisp-indent-function 1)
  502. (put 'condition-case 'lisp-indent-function 2)
  503. (put 'unwind-protect 'lisp-indent-function 1)
  504. (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
  505.  
  506. (defun indent-sexp (&optional endpos)
  507.   "Indent each line of the list starting just after point.
  508. If optional arg ENDPOS is given, indent each line, stopping when
  509. ENDPOS is encountered."
  510.   (interactive)
  511.   (let ((indent-stack (list nil)) (next-depth 0) last-depth bol
  512.     outer-loop-done inner-loop-done state this-indent
  513.     (last-point (point)))
  514.     ;; Get error now if we don't have a complete sexp after point.
  515.     (save-excursion (forward-sexp 1))
  516.     (save-excursion
  517.       (setq outer-loop-done nil)
  518.       (while (if endpos (< (point) endpos)
  519.            (not outer-loop-done))
  520.     (setq last-depth next-depth
  521.           inner-loop-done nil)
  522.     ;; Parse this line so we can learn the state
  523.     ;; to indent the next line.
  524.     ;; This inner loop goes through only once
  525.     ;; unless a line ends inside a string.
  526.     (while (and (not inner-loop-done)
  527.             (not (setq outer-loop-done (eobp))))
  528.       (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  529.                       nil nil state))
  530.       (setq next-depth (car state))
  531.       ;; If the line contains a comment other than the sort
  532.       ;; that is indented like code,
  533.       ;; indent it now with indent-for-comment.
  534.       ;; Comments indented like code are right already.
  535.       ;; In any case clear the in-comment flag in the state
  536.       ;; because parse-partial-sexp never sees the newlines.
  537.       (if (car (nthcdr 4 state))
  538.           (progn (indent-for-comment)
  539.              (end-of-line)
  540.              (setcar (nthcdr 4 state) nil)))
  541.       ;; If this line ends inside a string,
  542.       ;; go straight to next line, remaining within the inner loop,
  543.       ;; and turn off the \-flag.
  544.       (if (car (nthcdr 3 state))
  545.           (progn
  546.         (forward-line 1)
  547.         (setcar (nthcdr 5 state) nil))
  548.         (setq inner-loop-done t)))
  549.     (and endpos
  550.          (while (<= next-depth 0)
  551.            (setq indent-stack (append indent-stack (list nil)))
  552.            (setq next-depth (1+ next-depth))
  553.            (setq last-depth (1+ last-depth))))
  554.     (setq outer-loop-done (<= next-depth 0))
  555.     (if outer-loop-done
  556.         nil
  557.       (while (> last-depth next-depth)
  558.         (setq indent-stack (cdr indent-stack)
  559.           last-depth (1- last-depth)))
  560.       (while (< last-depth next-depth)
  561.         (setq indent-stack (cons nil indent-stack)
  562.           last-depth (1+ last-depth)))
  563.       ;; Now go to the next line and indent it according
  564.       ;; to what we learned from parsing the previous one.
  565.       (forward-line 1)
  566.       (setq bol (point))
  567.       (skip-chars-forward " \t")
  568.       ;; But not if the line is blank, or just a comment
  569.       ;; (except for double-semi comments; indent them as usual).
  570.       (if (or (eobp) (looking-at "\\s<\\|\n"))
  571.           nil
  572.         (if (and (car indent-stack)
  573.              (>= (car indent-stack) 0))
  574.         (setq this-indent (car indent-stack))
  575.           (let ((val (calculate-lisp-indent
  576.               (if (car indent-stack) (- (car indent-stack))))))
  577.         (if (integerp val)
  578.             (setcar indent-stack
  579.                 (setq this-indent val))
  580.           (setcar indent-stack (- (car (cdr val))))
  581.           (setq this-indent (car val)))))
  582.         (if (/= (current-column) this-indent)
  583.         (progn (delete-region bol (point))
  584.                (indent-to this-indent)))))
  585.     (or outer-loop-done
  586.         (setq outer-loop-done (= (point) last-point))
  587.         (setq last-point (point)))))))
  588.  
  589. ;; Indent every line whose first char is between START and END inclusive.
  590. (defun lisp-indent-region (start end)
  591.   (save-excursion
  592.     (goto-char start)
  593.     (and (bolp) (not (eolp))
  594.      (lisp-indent-line))
  595.     (let ((endmark (copy-marker end)))
  596.       (indent-sexp endmark)
  597.       (set-marker endmark nil))))
  598.  
  599. (defun indent-code-rigidly (start end arg &optional nochange-regexp)
  600.   "Indent all lines of code, starting in the region, sideways by ARG columns.
  601. Does not affect lines starting inside comments or strings,
  602. assuming that the start of the region is not inside them.
  603. Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
  604. The last is a regexp which, if matched at the beginning of a line,
  605. means don't indent that line."
  606.   (interactive "r\np")
  607.   (let (state)
  608.     (save-excursion
  609.       (goto-char end)
  610.       (setq end (point-marker))
  611.       (goto-char start)
  612.       (or (bolp)
  613.       (setq state (parse-partial-sexp (point)
  614.                       (progn
  615.                         (forward-line 1) (point))
  616.                       nil nil state)))
  617.       (while (< (point) end)
  618.     (or (car (nthcdr 3 state))
  619.         (and nochange-regexp
  620.          (looking-at nochange-regexp))
  621.         ;; If line does not start in string, indent it
  622.         (let ((indent (current-indentation)))
  623.           (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  624.           (or (eolp)
  625.           (indent-to (max 0 (+ indent arg)) 0))))
  626.     (setq state (parse-partial-sexp (point)
  627.                     (progn
  628.                       (forward-line 1) (point))
  629.                     nil nil state))))))
  630.