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 / lisp-mode.el.z / lisp-mode.el
Encoding:
Text File  |  1998-05-21  |  37.5 KB  |  1,006 lines

  1. ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands.
  2.  
  3. ;; Copyright (C) 1985, 1996 Free Software Foundation, Inc.
  4. ;; Copyright (C) 1995 Tinker Systems
  5.  
  6. ;; Maintainer: FSF
  7. ;; Keywords: lisp, languages
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  24. ;; 02111-1307, USA.
  25.  
  26. ;;; Synched up with: FSF 19.34 (but starting to diverge).
  27.  
  28. ;;; Commentary:
  29.  
  30. ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
  31. ;; This mode is documented in the Emacs manual
  32.  
  33. ;; July/05/97 slb Converted to use easymenu.
  34.  
  35. ;;; Code:
  36.  
  37. (defvar lisp-mode-syntax-table nil "")
  38. (defvar emacs-lisp-mode-syntax-table nil "")
  39. (defvar lisp-mode-abbrev-table nil "")
  40.  
  41. ;; XEmacs change
  42. (defvar lisp-interaction-mode-popup-menu nil)
  43. (defvar lisp-interaction-mode-popup-menu-1
  44.   (purecopy '("Lisp-Interaction"
  45.           ["Evaluate Last S-expression" eval-last-sexp      t]
  46.           ["Evaluate Entire Buffer"     eval-current-buffer t]
  47.           ["Evaluate Region"    eval-region    (region-exists-p)]
  48.           "---"
  49.           ["Evaluate This Defun"      eval-defun          t]
  50.           ;; FSF says "Instrument Function for Debugging"
  51.           ["Debug This Defun"         edebug-defun        t]
  52.           "---"
  53.           ["Trace a Function"   trace-function-background t]
  54.           ["Untrace All Functions"    untrace-all (fboundp 'untrace-all)]
  55.           "---"
  56.           ["Comment Out Region"    comment-region    (region-exists-p)]
  57.           ["Indent Region"        indent-region    (region-exists-p)]
  58.           ["Indent Line"        lisp-indent-line t]
  59.           "---"
  60.           ["Debug On Error" (setq debug-on-error (not debug-on-error))
  61.            :style toggle :selected debug-on-error]
  62.           ["Debug On Quit" (setq debug-on-quit (not debug-on-quit))
  63.            :style toggle :selected debug-on-quit]
  64.           ["Debug on Signal" (setq debug-on-signal (not debug-on-signal))
  65.            :style toggle :selected debug-on-signal]
  66.           )))
  67.  
  68. (defvar emacs-lisp-mode-popup-menu nil)
  69. (defvar emacs-lisp-mode-popup-menu-1
  70.   (purecopy
  71.    (nconc
  72.     '("Emacs-Lisp"
  73.       ["Byte-compile This File" emacs-lisp-byte-compile t]
  74.       ["Byte-recompile Directory..." byte-recompile-directory t]
  75.       "---")
  76.     (cdr lisp-interaction-mode-popup-menu-1))))
  77.  
  78. ;Don't have a menubar entry in Lisp Interaction mode.  Otherwise, the
  79. ;*scratch* buffer has a Lisp menubar item!  Very confusing.
  80. ;(defvar lisp-interaction-mode-menubar-menu
  81. ;  (purecopy (cons "Lisp" (cdr lisp-interaction-mode-popup-menu))))
  82.  
  83. (defvar emacs-lisp-mode-menubar-menu nil)
  84. (defvar emacs-lisp-mode-menubar-menu-1
  85.   (purecopy (cons "Lisp" (cdr emacs-lisp-mode-popup-menu-1))))
  86.  
  87. (if (not emacs-lisp-mode-syntax-table)
  88.     (let ((i 0))
  89.       (setq emacs-lisp-mode-syntax-table (make-syntax-table))
  90.       (while (< i ?0)
  91.     (modify-syntax-entry i "_   " emacs-lisp-mode-syntax-table)
  92.     (setq i (1+ i)))
  93.       (setq i (1+ ?9))
  94.       (while (< i ?A)
  95.     (modify-syntax-entry i "_   " emacs-lisp-mode-syntax-table)
  96.     (setq i (1+ i)))
  97.       (setq i (1+ ?Z))
  98.       (while (< i ?a)
  99.     (modify-syntax-entry i "_   " emacs-lisp-mode-syntax-table)
  100.     (setq i (1+ i)))
  101.       (setq i (1+ ?z))
  102.       (while (< i 128)
  103.     (modify-syntax-entry i "_   " emacs-lisp-mode-syntax-table)
  104.     (setq i (1+ i)))
  105.       (modify-syntax-entry ?  "    " emacs-lisp-mode-syntax-table)
  106.       (modify-syntax-entry ?\t "    " emacs-lisp-mode-syntax-table)
  107.       (modify-syntax-entry ?\n ">   " emacs-lisp-mode-syntax-table)
  108.       ;; Give CR the same syntax as newline, for selective-display.
  109.       (modify-syntax-entry ?\^m ">   " emacs-lisp-mode-syntax-table)
  110.       ;; XEmacs change
  111.       ;; Treat ^L as whitespace.
  112.       (modify-syntax-entry ?\f "    " emacs-lisp-mode-syntax-table)
  113.       (modify-syntax-entry ?\; "<   " emacs-lisp-mode-syntax-table)
  114.       (modify-syntax-entry ?` "'   " emacs-lisp-mode-syntax-table)
  115.       (modify-syntax-entry ?' "'   " emacs-lisp-mode-syntax-table)
  116.       (modify-syntax-entry ?, "'   " emacs-lisp-mode-syntax-table)
  117.       ;; Used to be singlequote; changed for flonums.
  118.       (modify-syntax-entry ?. "_   " emacs-lisp-mode-syntax-table)
  119.       (modify-syntax-entry ?# "'   " emacs-lisp-mode-syntax-table)
  120.       (modify-syntax-entry ?\" "\"    " emacs-lisp-mode-syntax-table)
  121.       (modify-syntax-entry ?\\ "\\   " emacs-lisp-mode-syntax-table)
  122.       (modify-syntax-entry ?\( "()  " emacs-lisp-mode-syntax-table)
  123.       (modify-syntax-entry ?\) ")(  " emacs-lisp-mode-syntax-table)
  124.       (modify-syntax-entry ?\[ "(]  " emacs-lisp-mode-syntax-table)
  125.       (modify-syntax-entry ?\] ")[  " emacs-lisp-mode-syntax-table)))
  126.  
  127. (if (not lisp-mode-syntax-table)
  128.     (progn (setq lisp-mode-syntax-table
  129.          (copy-syntax-table emacs-lisp-mode-syntax-table))
  130.        (modify-syntax-entry ?\| "\"   " lisp-mode-syntax-table)
  131.        (modify-syntax-entry ?\[ "_   " lisp-mode-syntax-table)
  132.        ;; XEmacs changes
  133.        (modify-syntax-entry ?\] "_   " lisp-mode-syntax-table)
  134.            ;;
  135.            ;; If emacs was compiled with NEW_SYNTAX, then do
  136.            ;;  CL's #| |# block comments.
  137.            (if (= 8 (length (parse-partial-sexp (point) (point))))
  138.                (progn
  139.                  (modify-syntax-entry ?#  "' 58" lisp-mode-syntax-table)
  140.                  (modify-syntax-entry ?|  ". 67" lisp-mode-syntax-table))
  141.          ;; else, old style
  142.          (modify-syntax-entry ?\| "\"   " lisp-mode-syntax-table))))
  143.  
  144. (define-abbrev-table 'lisp-mode-abbrev-table ())
  145.  
  146. ;(defvar lisp-imenu-generic-expression
  147. ;      '(
  148. ;     (nil 
  149. ;      "^\\s-*(def\\(un\\|subst\\|macro\\|advice\\)\\s-+\\([-A-Za-z0-9+]+\\)" 2)
  150. ;     ("Variables" 
  151. ;      "^\\s-*(def\\(var\\|const\\)\\s-+\\([-A-Za-z0-9+]+\\)" 2)
  152. ;     ("Types" 
  153. ;      "^\\s-*(def\\(type\\|struct\\|class\\|ine-condition\\)\\s-+\\([-A-Za-z0-9+]+\\)" 
  154. ;      2))
  155. ;
  156. ;  "Imenu generic expression for Lisp mode.  See `imenu-generic-expression'.")
  157.  
  158. (defun lisp-mode-variables (lisp-syntax)
  159.   (cond (lisp-syntax
  160.      (set-syntax-table lisp-mode-syntax-table)))
  161.   (setq local-abbrev-table lisp-mode-abbrev-table)
  162.   (make-local-variable 'paragraph-start)
  163.   (setq paragraph-start (concat page-delimiter "\\|$" ))
  164.   (make-local-variable 'paragraph-separate)
  165.   (setq paragraph-separate paragraph-start)
  166.   (make-local-variable 'paragraph-ignore-fill-prefix)
  167.   (setq paragraph-ignore-fill-prefix t)
  168.   (make-local-variable 'fill-paragraph-function)
  169.   (setq fill-paragraph-function 'lisp-fill-paragraph)
  170.   ;; Adaptive fill mode gets in the way of auto-fill,
  171.   ;; and should make no difference for explicit fill
  172.   ;; because lisp-fill-paragraph should do the job.
  173.   (make-local-variable 'adaptive-fill-mode)
  174.   (setq adaptive-fill-mode nil)
  175.   (make-local-variable 'indent-line-function)
  176.   (setq indent-line-function 'lisp-indent-line)
  177.   (make-local-variable 'indent-region-function)
  178.   (setq indent-region-function 'lisp-indent-region)
  179.   (make-local-variable 'parse-sexp-ignore-comments)
  180.   (setq parse-sexp-ignore-comments t)
  181.   (make-local-variable 'outline-regexp)
  182.   (setq outline-regexp ";;; \\|(....")
  183.   (make-local-variable 'comment-start)
  184.   (setq comment-start ";")
  185.   ;; XEmacs change
  186.   (set (make-local-variable 'block-comment-start) ";;")
  187.   (make-local-variable 'comment-start-skip)
  188.   ;; Look within the line for a ; following an even number of backslashes
  189.   ;; after either a non-backslash or the line beginning.
  190.   (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
  191.   (make-local-variable 'comment-column)
  192.   (setq comment-column 40)
  193.   (make-local-variable 'comment-indent-function)
  194.   (setq comment-indent-function 'lisp-comment-indent)
  195.   ;; XEmacs changes
  196. ;  (make-local-variable 'imenu-generic-expression)
  197. ;  (setq imenu-generic-expression lisp-imenu-generic-expression)
  198.   (set (make-local-variable 'dabbrev-case-fold-search) nil)
  199.   (set (make-local-variable 'dabbrev-case-replace) nil)
  200.   )
  201.  
  202.  
  203. (defvar shared-lisp-mode-map ()
  204.   "Keymap for commands shared by all sorts of Lisp modes.")
  205.  
  206. (if shared-lisp-mode-map
  207.     ()
  208.    (setq shared-lisp-mode-map (make-sparse-keymap))
  209.    ;; XEmacs changes
  210.    (set-keymap-name shared-lisp-mode-map 'shared-lisp-mode-map)
  211.    (define-key shared-lisp-mode-map "\M-;" 'lisp-indent-for-comment)
  212.    (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp))
  213.  
  214. (defvar emacs-lisp-mode-map ()
  215.   "Keymap for Emacs Lisp mode.
  216. All commands in `shared-lisp-mode-map' are inherited by this map.")
  217.  
  218. (if emacs-lisp-mode-map
  219.     ()
  220.   ;; XEmacs:  Ignore FSF nconc stuff
  221.   (setq emacs-lisp-mode-map (make-sparse-keymap))
  222.   (set-keymap-name emacs-lisp-mode-map 'emacs-lisp-mode-map)
  223.   (set-keymap-parents emacs-lisp-mode-map (list shared-lisp-mode-map))
  224.   (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
  225.   (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
  226.   ;; XEmacs: Not sure what the FSF menu bindings are.  I hope XEmacs
  227.   ;; doesn't need them.
  228. )
  229.  
  230. (defun emacs-lisp-byte-compile ()
  231.   "Byte compile the file containing the current buffer."
  232.   (interactive)
  233.   (if buffer-file-name
  234.       ;; XEmacs change.  Force buffer save first
  235.       (progn
  236.     (save-buffer)
  237.     (byte-compile-file buffer-file-name))
  238.     (error "The buffer must be saved in a file first.")))
  239.  
  240. (defun emacs-lisp-byte-compile-and-load ()
  241.   "Byte-compile the current file (if it has changed), then load compiled code."
  242.   (interactive)
  243.   (or buffer-file-name
  244.       (error "The buffer must be saved in a file first"))
  245.   (require 'bytecomp)
  246.   ;; Recompile if file or buffer has changed since last compilation.
  247.   (if (and (buffer-modified-p)
  248.        (y-or-n-p (format "save buffer %s first? " (buffer-name))))
  249.       (save-buffer))
  250.   (let ((compiled-file-name (byte-compile-dest-file buffer-file-name)))
  251.     (if (file-newer-than-file-p compiled-file-name buffer-file-name)
  252.     (load-file compiled-file-name)
  253.       (byte-compile-file buffer-file-name t))))
  254.  
  255. (defun emacs-lisp-mode ()
  256.   "Major mode for editing Lisp code to run in Emacs.
  257. Commands:
  258. Delete converts tabs to spaces as it moves back.
  259. Blank lines separate paragraphs.  Semicolons start comments.
  260. \\{emacs-lisp-mode-map}
  261. Entry to this mode calls the value of `emacs-lisp-mode-hook'
  262. if that value is non-nil."
  263.   (interactive)
  264.   (kill-all-local-variables)
  265.   (use-local-map emacs-lisp-mode-map)
  266.   (set-syntax-table emacs-lisp-mode-syntax-table)
  267.   ;; XEmacs changes
  268.   (setq major-mode 'emacs-lisp-mode
  269.     ;; mode-popup-menu emacs-lisp-mode-popup-menu
  270.     mode-name "Emacs-Lisp")
  271.   ;; (if (and (featurep 'menubar)
  272.            ;; current-menubar)
  273.       ;; (progn
  274.     ;; make a local copy of the menubar, so our modes don't
  275.     ;; change the global menubar
  276.     ;; (set-buffer-menubar current-menubar)
  277.     ;; (add-submenu nil emacs-lisp-mode-menubar-menu)))
  278.   (unless emacs-lisp-mode-popup-menu
  279.     (easy-menu-define emacs-lisp-mode-popup-menu emacs-lisp-mode-map ""
  280.               emacs-lisp-mode-popup-menu-1))
  281.   (easy-menu-add emacs-lisp-mode-popup-menu)
  282.   (lisp-mode-variables nil)
  283.   (run-hooks 'emacs-lisp-mode-hook))
  284.  
  285. (defvar lisp-mode-map ()
  286.   "Keymap for ordinary Lisp mode.
  287. All commands in `shared-lisp-mode-map' are inherited by this map.")
  288.  
  289. (if lisp-mode-map
  290.     ()
  291.   ;; XEmacs changes
  292.   (setq lisp-mode-map (make-sparse-keymap))
  293.   (set-keymap-name lisp-mode-map 'lisp-mode-map)
  294.   (set-keymap-parents lisp-mode-map (list shared-lisp-mode-map))
  295.   (define-key lisp-mode-map "\e\C-x" 'lisp-send-defun)
  296.   ;; gag, no.  use ilisp.  -jwz
  297. ;;  (define-key lisp-mode-map "\C-c\C-z" 'run-lisp)
  298.   )
  299.  
  300. (defun lisp-mode ()
  301.   "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
  302. Commands:
  303. Delete converts tabs to spaces as it moves back.
  304. Blank lines separate paragraphs.  Semicolons start comments.
  305. \\{lisp-mode-map}
  306. Note that `run-lisp' may be used either to start an inferior Lisp job
  307. or to switch back to an existing one.
  308.  
  309. Entry to this mode calls the value of `lisp-mode-hook'
  310. if that value is non-nil."
  311.   (interactive)
  312.   (kill-all-local-variables)
  313.   (use-local-map lisp-mode-map)
  314.   (setq major-mode 'lisp-mode)
  315.   (setq mode-name "Lisp")
  316.   (lisp-mode-variables t)
  317.   (set-syntax-table lisp-mode-syntax-table)
  318.   (run-hooks 'lisp-mode-hook))
  319.  
  320. ;; This will do unless shell.el is loaded.
  321. ;; XEmacs change
  322. (defun lisp-send-defun ()
  323.   "Send the current defun to the Lisp process made by \\[run-lisp]."
  324.   (interactive)
  325.   (error "Process lisp does not exist"))
  326.  
  327. ;; XEmacs change: emacs-lisp-mode-map is a more appropriate parent.
  328. (defvar lisp-interaction-mode-map ()
  329.   "Keymap for Lisp Interaction mode.
  330. All commands in `shared-lisp-mode-map' are inherited by this map.")
  331.  
  332. (if lisp-interaction-mode-map
  333.     ()
  334.   ;; XEmacs set keymap our way
  335.   (setq lisp-interaction-mode-map (make-sparse-keymap))
  336.   (set-keymap-name lisp-interaction-mode-map 'lisp-interaction-mode-map)
  337.   (set-keymap-parents lisp-interaction-mode-map (list emacs-lisp-mode-map))
  338.   (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun)
  339.   (define-key lisp-interaction-mode-map "\e\t" 'lisp-complete-symbol)
  340.   (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp))
  341.  
  342. (defun lisp-interaction-mode ()
  343.   "Major mode for typing and evaluating Lisp forms.
  344. Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
  345. before point, and prints its value into the buffer, advancing point.
  346.  
  347. Commands:
  348. Delete converts tabs to spaces as it moves back.
  349. Paragraphs are separated only by blank lines.
  350. Semicolons start comments.
  351. \\{lisp-interaction-mode-map}
  352. Entry to this mode calls the value of `lisp-interaction-mode-hook'
  353. if that value is non-nil."
  354.   (interactive)
  355.   (kill-all-local-variables)
  356.   (use-local-map lisp-interaction-mode-map)
  357.   (setq major-mode 'lisp-interaction-mode)
  358.   (setq mode-name "Lisp Interaction")
  359.   ;; XEmacs change
  360.   ;; (setq mode-popup-menu lisp-interaction-mode-popup-menu)
  361.   (unless lisp-interaction-mode-popup-menu
  362.     (easy-menu-define lisp-interaction-mode-popup-menu
  363.               lisp-interaction-mode-map
  364.               ""
  365.               lisp-interaction-mode-popup-menu-1))
  366.   (easy-menu-add lisp-interaction-mode-popup-menu)
  367.  
  368.   (set-syntax-table emacs-lisp-mode-syntax-table)
  369.   (lisp-mode-variables nil)
  370.   (run-hooks 'lisp-interaction-mode-hook))
  371.  
  372. (defun eval-print-last-sexp ()
  373.   "Evaluate sexp before point; print value into current buffer."
  374.   (interactive)
  375.   (let ((standard-output (current-buffer)))
  376.     (terpri)
  377.     (eval-last-sexp t)
  378.     (terpri)))
  379.  
  380. ;; XEmacs change
  381. (defcustom eval-interactive-verbose t
  382.   "*Non-nil means that interactive evaluation can print messages.
  383. The messages are printed when the expression is treated differently
  384. using `\\[eval-last-sexp]' and `\\[eval-defun]' than it than it would have been
  385. treated noninteractively.
  386.  
  387. The printed messages are \"defvar treated as defconst\" and \"defcustom
  388.  evaluation forced\".  See `eval-interactive' for more details."
  389.   :type 'boolean
  390.   :group 'lisp)
  391.  
  392. (defun eval-interactive (expr)
  393.   "Like `eval' except that it transforms defvars to defconsts.
  394. The evaluation of defcustom forms is forced."
  395.   (cond ((and (consp expr)
  396.           (eq (car expr) 'defvar)
  397.           (> (length expr) 2))
  398.      (eval (cons 'defconst (cdr expr)))
  399.      (and eval-interactive-verbose
  400.           (message "defvar treated as defconst"))
  401.      (sit-for 1)
  402.      (message "")
  403.      (nth 1 expr))
  404.     ((and (consp expr)
  405.           (eq (car expr) 'defcustom)
  406.           (> (length expr) 2)
  407.           (default-boundp (nth 1 expr)))
  408.      ;; Force variable to be bound
  409.      (set-default (nth 1 expr) (eval (nth 2 expr)))
  410.      ;; And evaluate the defcustom
  411.      (eval expr)
  412.      (and eval-interactive-verbose
  413.           (message "defcustom evaluation forced"))
  414.      (sit-for 1)
  415.      (message "")
  416.      (nth 1 expr))
  417.     (t
  418.      (eval expr))))
  419.  
  420. ;; XEmacs change, based on Bob Weiner suggestion
  421. (defun eval-last-sexp (eval-last-sexp-arg-internal) ;dynamic scoping wonderment
  422.   "Evaluate sexp before point; print value in minibuffer.
  423. With argument, print output into current buffer."
  424.   (interactive "P")
  425.   (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t))
  426.     (opoint (point)))
  427.     (prin1 (let ((stab (syntax-table))
  428.          expr)
  429.          (eval-interactive
  430.           (unwind-protect
  431.           (save-excursion
  432.             (set-syntax-table emacs-lisp-mode-syntax-table)
  433.             (forward-sexp -1)
  434.             (save-restriction
  435.               (narrow-to-region (point-min) opoint)
  436.               (setq expr (read (current-buffer)))
  437.               (if (and (consp expr)
  438.                    (eq (car expr) 'interactive))
  439.               (list 'quote
  440.                 (call-interactively
  441.                  (eval (` (lambda (&rest args)
  442.                         (, expr) args)))))
  443.             expr)))
  444.         (set-syntax-table stab)))))))
  445.  
  446. (defun eval-defun (eval-defun-arg-internal)
  447.   "Evaluate defun that point is in or before.
  448. Print value in minibuffer.
  449. With argument, insert value in current buffer after the defun."
  450.   (interactive "P")
  451.   (let ((standard-output (if eval-defun-arg-internal (current-buffer) t)))
  452.     (prin1 (eval-interactive (save-excursion
  453.                    (end-of-defun)
  454.                    (beginning-of-defun)
  455.                    (read (current-buffer)))))))
  456.  
  457.  
  458. (defun lisp-comment-indent ()
  459.   (if (looking-at "\\s<\\s<\\s<")
  460.       (current-column)
  461.     (if (looking-at "\\s<\\s<")
  462.     (let ((tem (calculate-lisp-indent)))
  463.       (if (listp tem) (car tem) tem))
  464.       (skip-chars-backward " \t")
  465.       (max (if (bolp) 0 (1+ (current-column)))
  466.        comment-column))))
  467.  
  468. ;; XEmacs change
  469. (defun lisp-indent-for-comment ()
  470.   "Indent this line's comment appropriately, or insert an empty comment.
  471. If adding a new comment on a blank line, use `block-comment-start' instead
  472. of `comment-start' to open the comment."
  473.   ;; by Stig@hackvan.com
  474.   ;; #### - This functionality, the recognition of block-comment-{start,end},
  475.   ;; will perhaps be standardized across modes and move to indent-for-comment.
  476.   (interactive)
  477.   (if (and block-comment-start
  478.        (save-excursion (beginning-of-line) (looking-at "^[ \t]*$")))
  479.       (insert block-comment-start))
  480.   (indent-for-comment))
  481.  
  482. (defconst lisp-indent-offset nil "")
  483. (defconst lisp-indent-function 'lisp-indent-function "")
  484.  
  485. (defun lisp-indent-line (&optional whole-exp)
  486.   "Indent current line as Lisp code.
  487. With argument, indent any additional lines of the same expression
  488. rigidly along with this one."
  489.   (interactive "P")
  490.   (let ((indent (calculate-lisp-indent)) shift-amt beg end
  491.     (pos (- (point-max) (point))))
  492.     (beginning-of-line)
  493.     (setq beg (point))
  494.     (skip-chars-forward " \t")
  495.     (if (looking-at "\\s<\\s<\\s<")
  496.     ;; Don't alter indentation of a ;;; comment line.
  497.     (goto-char (- (point-max) pos))
  498.       (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
  499.       ;; Single-semicolon comment lines should be indented
  500.       ;; as comment lines, not as code.
  501.       (progn (indent-for-comment) (forward-char -1))
  502.     (if (listp indent) (setq indent (car indent)))
  503.     (setq shift-amt (- indent (current-column)))
  504.     (if (zerop shift-amt)
  505.         nil
  506.       (delete-region beg (point))
  507.       (indent-to indent)))
  508.       ;; If initial point was within line's indentation,
  509.       ;; position after the indentation.  Else stay at same point in text.
  510.       (if (> (- (point-max) pos) (point))
  511.       (goto-char (- (point-max) pos)))
  512.       ;; If desired, shift remaining lines of expression the same amount.
  513.       (and whole-exp (not (zerop shift-amt))
  514.        (save-excursion
  515.          (goto-char beg)
  516.          (forward-sexp 1)
  517.          (setq end (point))
  518.          (goto-char beg)
  519.          (forward-line 1)
  520.          (setq beg (point))
  521.          (> end beg))
  522.        (indent-code-rigidly beg end shift-amt)))))
  523.  
  524. (defvar calculate-lisp-indent-last-sexp)
  525.  
  526. (defun calculate-lisp-indent (&optional parse-start)
  527.   "Return appropriate indentation for current line as Lisp code.
  528. In usual case returns an integer: the column to indent to.
  529. Can instead return a list, whose car is the column to indent to.
  530. This means that following lines at the same level of indentation
  531. should not necessarily be indented the same way.
  532. The second element of the list is the buffer position
  533. of the start of the containing expression."
  534.   (save-excursion
  535.     (beginning-of-line)
  536.     (let ((indent-point (point))
  537.       ;; XEmacs change (remove paren-depth)
  538.           state ;;paren-depth
  539.           ;; setting this to a number inhibits calling hook
  540.           (desired-indent nil)
  541.           (retry t)
  542.           calculate-lisp-indent-last-sexp containing-sexp)
  543.       (if parse-start
  544.           (goto-char parse-start)
  545.           (beginning-of-defun))
  546.       ;; Find outermost containing sexp
  547.       (while (< (point) indent-point)
  548.         (setq state (parse-partial-sexp (point) indent-point 0)))
  549.       ;; Find innermost containing sexp
  550.       (while (and retry
  551.           state
  552.           ;; XEmacs change (remove paren-depth)
  553.                   (> ;;(setq paren-depth (elt state 0))
  554.              (elt state 0)
  555.              0))
  556.         (setq retry nil)
  557.         (setq calculate-lisp-indent-last-sexp (elt state 2))
  558.         (setq containing-sexp (elt state 1))
  559.         ;; Position following last unclosed open.
  560.         (goto-char (1+ containing-sexp))
  561.         ;; Is there a complete sexp since then?
  562.         (if (and calculate-lisp-indent-last-sexp
  563.          (> calculate-lisp-indent-last-sexp (point)))
  564.             ;; Yes, but is there a containing sexp after that?
  565.             (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
  566.                         indent-point 0)))
  567.               (if (setq retry (car (cdr peek))) (setq state peek)))))
  568.       (if retry
  569.           nil
  570.         ;; Innermost containing sexp found
  571.         (goto-char (1+ containing-sexp))
  572.         (if (not calculate-lisp-indent-last-sexp)
  573.         ;; indent-point immediately follows open paren.
  574.         ;; Don't call hook.
  575.             (setq desired-indent (current-column))
  576.       ;; Find the start of first element of containing sexp.
  577.       (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
  578.       (cond ((looking-at "\\s(")
  579.          ;; First element of containing sexp is a list.
  580.          ;; Indent under that list.
  581.          )
  582.         ((> (save-excursion (forward-line 1) (point))
  583.             calculate-lisp-indent-last-sexp)
  584.          ;; This is the first line to start within the containing sexp.
  585.          ;; It's almost certainly a function call.
  586.          (if (= (point) calculate-lisp-indent-last-sexp)
  587.              ;; Containing sexp has nothing before this line
  588.              ;; except the first element.  Indent under that element.
  589.              nil
  590.            ;; Skip the first element, find start of second (the first
  591.            ;; argument of the function call) and indent under.
  592.            (progn (forward-sexp 1)
  593.               (parse-partial-sexp (point)
  594.                           calculate-lisp-indent-last-sexp
  595.                           0 t)))
  596.          (backward-prefix-chars))
  597.         (t
  598.          ;; Indent beneath first sexp on same line as
  599.          ;; calculate-lisp-indent-last-sexp.  Again, it's
  600.          ;; almost certainly a function call.
  601.          (goto-char calculate-lisp-indent-last-sexp)
  602.          (beginning-of-line)
  603.          (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
  604.                      0 t)
  605.          (backward-prefix-chars)))))
  606.       ;; Point is at the point to indent under unless we are inside a string.
  607.       ;; Call indentation hook except when overridden by lisp-indent-offset
  608.       ;; or if the desired indentation has already been computed.
  609.       (let ((normal-indent (current-column)))
  610.         (cond ((elt state 3)
  611.                ;; Inside a string, don't change indentation.
  612.                (goto-char indent-point)
  613.                (skip-chars-forward " \t")
  614.                (current-column))
  615.               (desired-indent)
  616.               ((and (boundp 'lisp-indent-function)
  617.                     lisp-indent-function
  618.                     (not retry))
  619.                (or (funcall lisp-indent-function indent-point state)
  620.                    normal-indent))
  621.           ;; XEmacs change:
  622.               ;; lisp-indent-offset shouldn't override lisp-indent-function !
  623.               ((and (integerp lisp-indent-offset) containing-sexp)
  624.                ;; Indent by constant offset
  625.                (goto-char containing-sexp)
  626.                (+ normal-indent lisp-indent-offset))
  627.               (t
  628.                normal-indent))))))
  629.  
  630. (defun lisp-indent-function (indent-point state)
  631.   ;; free reference to `calculate-lisp-indent-last-sexp'
  632.   ;; in #'calculate-lisp-indent
  633.   (let ((normal-indent (current-column)))
  634.     (goto-char (1+ (elt state 1)))
  635.     (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
  636.     (if (and (elt state 2)
  637.              (not (looking-at "\\sw\\|\\s_")))
  638.         ;; car of form doesn't seem to be a a symbol
  639.         (progn
  640.           (if (not (> (save-excursion (forward-line 1) (point))
  641.                       calculate-lisp-indent-last-sexp))
  642.               (progn (goto-char calculate-lisp-indent-last-sexp)
  643.                      (beginning-of-line)
  644.                      (parse-partial-sexp (point)
  645.                      calculate-lisp-indent-last-sexp 0 t)))
  646.           ;; Indent under the list or under the first sexp on the same
  647.           ;; line as calculate-lisp-indent-last-sexp.  Note that first
  648.           ;; thing on that line has to be complete sexp since we are
  649.           ;; inside the innermost containing sexp.
  650.           (backward-prefix-chars)
  651.           (current-column))
  652.       (let ((function (buffer-substring (point)
  653.                     (progn (forward-sexp 1) (point))))
  654.         method)
  655.     (setq method (or (get (intern-soft function) 'lisp-indent-function)
  656.              (get (intern-soft function) 'lisp-indent-hook)))
  657.     (cond ((or (eq method 'defun)
  658.            (and (null method)
  659.             (> (length function) 3)
  660.             (string-match "\\`def" function)))
  661.            (lisp-indent-defform state indent-point))
  662.           ((integerp method)
  663.            (lisp-indent-specform method state
  664.                      indent-point normal-indent))
  665.           (method
  666.         (funcall method state indent-point)))))))
  667.  
  668. (defconst lisp-body-indent 2
  669.   "Number of columns to indent the second line of a `(def...)' form.")
  670.  
  671. (defun lisp-indent-specform (count state indent-point normal-indent)
  672.   (let ((containing-form-start (elt state 1))
  673.         (i count)
  674.         body-indent containing-form-column)
  675.     ;; Move to the start of containing form, calculate indentation
  676.     ;; to use for non-distinguished forms (> count), and move past the
  677.     ;; function symbol.  lisp-indent-function guarantees that there is at
  678.     ;; least one word or symbol character following open paren of containing
  679.     ;; form.
  680.     (goto-char containing-form-start)
  681.     (setq containing-form-column (current-column))
  682.     (setq body-indent (+ lisp-body-indent containing-form-column))
  683.     (forward-char 1)
  684.     (forward-sexp 1)
  685.     ;; Now find the start of the last form.
  686.     (parse-partial-sexp (point) indent-point 1 t)
  687.     (while (and (< (point) indent-point)
  688.                 (condition-case ()
  689.                     (progn
  690.                       (setq count (1- count))
  691.                       (forward-sexp 1)
  692.                       (parse-partial-sexp (point) indent-point 1 t))
  693.                   (error nil))))
  694.     ;; Point is sitting on first character of last (or count) sexp.
  695.     (if (> count 0)
  696.         ;; A distinguished form.  If it is the first or second form use double
  697.         ;; lisp-body-indent, else normal indent.  With lisp-body-indent bound
  698.         ;; to 2 (the default), this just happens to work the same with if as
  699.         ;; the older code, but it makes unwind-protect, condition-case,
  700.         ;; with-output-to-temp-buffer, et. al. much more tasteful.  The older,
  701.         ;; less hacked, behavior can be obtained by replacing below with
  702.         ;; (list normal-indent containing-form-start).
  703.         (if (<= (- i count) 1)
  704.             (list (+ containing-form-column (* 2 lisp-body-indent))
  705.                   containing-form-start)
  706.             (list normal-indent containing-form-start))
  707.       ;; A non-distinguished form.  Use body-indent if there are no
  708.       ;; distinguished forms and this is the first undistinguished form,
  709.       ;; or if this is the first undistinguished form and the preceding
  710.       ;; distinguished form has indentation at least as great as body-indent.
  711.       (if (or (and (= i 0) (= count 0))
  712.               (and (= count 0) (<= body-indent normal-indent)))
  713.           body-indent
  714.           normal-indent))))
  715.  
  716. (defun lisp-indent-defform (state indent-point)
  717.   (goto-char (car (cdr state)))
  718.   (forward-line 1)
  719.   (if (> (point) (car (cdr (cdr state))))
  720.       (progn
  721.     (goto-char (car (cdr state)))
  722.     (+ lisp-body-indent (current-column)))))
  723.  
  724.  
  725. ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
  726. ;; like defun if the first form is placed on the next line, otherwise
  727. ;; it is indented like any other form (i.e. forms line up under first).
  728.  
  729. (put 'lambda 'lisp-indent-function 'defun)
  730. (put 'autoload 'lisp-indent-function 'defun)
  731. (put 'progn 'lisp-indent-function 0)
  732. (put 'prog1 'lisp-indent-function 1)
  733. (put 'prog2 'lisp-indent-function 2)
  734. (put 'save-excursion 'lisp-indent-function 0)
  735. (put 'save-window-excursion 'lisp-indent-function 0)
  736. (put 'save-selected-window 'lisp-indent-function 0)
  737. (put 'save-restriction 'lisp-indent-function 0)
  738. (put 'save-match-data 'lisp-indent-function 0)
  739. (put 'let 'lisp-indent-function 1)
  740. (put 'let* 'lisp-indent-function 1)
  741. (put 'while 'lisp-indent-function 1)
  742. (put 'if 'lisp-indent-function 2)
  743. (put 'catch 'lisp-indent-function 1)
  744. (put 'condition-case 'lisp-indent-function 2)
  745. (put 'unwind-protect 'lisp-indent-function 1)
  746. (put 'save-current-buffer 'lisp-indent-function 0)
  747. (put 'with-current-buffer 'lisp-indent-function 1)
  748. (put 'with-temp-file 'lisp-indent-function 1)
  749. (put 'with-temp-buffer 'lisp-indent-function 0)
  750. (put 'with-output-to-string 'lisp-indent-function 0)
  751. (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
  752. (put 'display-message 'lisp-indent-function 1)
  753. (put 'display-warning 'lisp-indent-function 1)
  754. (put 'global-set-key 'lisp-indent-function 1)
  755.  
  756. (defun indent-sexp (&optional endpos)
  757.   "Indent each line of the list starting just after point.
  758. If optional arg ENDPOS is given, indent each line, stopping when
  759. ENDPOS is encountered."
  760.   (interactive)
  761.   (let ((indent-stack (list nil))
  762.     (next-depth 0) 
  763.     ;; If ENDPOS is non-nil, use nil as STARTING-POINT
  764.     ;; so that calculate-lisp-indent will find the beginning of
  765.     ;; the defun we are in.
  766.     ;; If ENDPOS is nil, it is safe not to scan before point
  767.     ;; since every line we indent is more deeply nested than point is.
  768.     (starting-point (if endpos nil (point)))
  769.     (last-point (point))
  770.     last-depth bol outer-loop-done inner-loop-done state this-indent)
  771.     (or endpos
  772.     ;; Get error now if we don't have a complete sexp after point.
  773.     (save-excursion (forward-sexp 1)))
  774.     (save-excursion
  775.       (setq outer-loop-done nil)
  776.       (while (if endpos (< (point) endpos)
  777.            (not outer-loop-done))
  778.     (setq last-depth next-depth
  779.           inner-loop-done nil)
  780.     ;; Parse this line so we can learn the state
  781.     ;; to indent the next line.
  782.     ;; This inner loop goes through only once
  783.     ;; unless a line ends inside a string.
  784.     (while (and (not inner-loop-done)
  785.             (not (setq outer-loop-done (eobp))))
  786.       (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  787.                       nil nil state))
  788.       (setq next-depth (car state))
  789.       ;; If the line contains a comment other than the sort
  790.       ;; that is indented like code,
  791.       ;; indent it now with indent-for-comment.
  792.       ;; Comments indented like code are right already.
  793.       ;; In any case clear the in-comment flag in the state
  794.       ;; because parse-partial-sexp never sees the newlines.
  795.       (if (car (nthcdr 4 state))
  796.           (progn (indent-for-comment)
  797.              (end-of-line)
  798.              (setcar (nthcdr 4 state) nil)))
  799.       ;; If this line ends inside a string,
  800.       ;; go straight to next line, remaining within the inner loop,
  801.       ;; and turn off the \-flag.
  802.       (if (car (nthcdr 3 state))
  803.           (progn
  804.         (forward-line 1)
  805.         (setcar (nthcdr 5 state) nil))
  806.         (setq inner-loop-done t)))
  807.     (and endpos
  808.          (<= next-depth 0)
  809.          (progn
  810.            (setq indent-stack (append indent-stack
  811.                       (make-list (- next-depth) nil))
  812.              last-depth (- last-depth next-depth)
  813.              next-depth 0)))
  814.     (or outer-loop-done endpos
  815.         (setq outer-loop-done (<= next-depth 0)))
  816.     (if outer-loop-done
  817.         (forward-line 1)
  818.       (while (> last-depth next-depth)
  819.         (setq indent-stack (cdr indent-stack)
  820.           last-depth (1- last-depth)))
  821.       (while (< last-depth next-depth)
  822.         (setq indent-stack (cons nil indent-stack)
  823.           last-depth (1+ last-depth)))
  824.       ;; Now go to the next line and indent it according
  825.       ;; to what we learned from parsing the previous one.
  826.       (forward-line 1)
  827.       (setq bol (point))
  828.       (skip-chars-forward " \t")
  829.       ;; But not if the line is blank, or just a comment
  830.       ;; (except for double-semi comments; indent them as usual).
  831.       (if (or (eobp) (looking-at "\\s<\\|\n"))
  832.           nil
  833.         (if (and (car indent-stack)
  834.              (>= (car indent-stack) 0))
  835.         (setq this-indent (car indent-stack))
  836.           (let ((val (calculate-lisp-indent
  837.               (if (car indent-stack) (- (car indent-stack))
  838.                 starting-point))))
  839.         (if (integerp val)
  840.             (setcar indent-stack
  841.                 (setq this-indent val))
  842.           (setcar indent-stack (- (car (cdr val))))
  843.           (setq this-indent (car val)))))
  844.         (if (/= (current-column) this-indent)
  845.         (progn (delete-region bol (point))
  846.                (indent-to this-indent)))))
  847.     (or outer-loop-done
  848.         (setq outer-loop-done (= (point) last-point))
  849.         (setq last-point (point)))))))
  850.  
  851. ;; Indent every line whose first char is between START and END inclusive.
  852. (defun lisp-indent-region (start end)
  853.   (save-excursion
  854.     (let ((endmark (copy-marker end)))
  855.       (goto-char start)
  856.       (and (bolp) (not (eolp))
  857.        (lisp-indent-line))
  858.       (indent-sexp endmark)
  859.       (set-marker endmark nil))))
  860.  
  861. ;;;; Lisp paragraph filling commands.
  862.  
  863. (defun lisp-fill-paragraph (&optional justify)
  864.   "Like \\[fill-paragraph], but handle Emacs Lisp comments.
  865. If any of the current line is a comment, fill the comment or the
  866. paragraph of it that point is in, preserving the comment's indentation
  867. and initial semicolons."
  868.   (interactive "P")
  869.   (let (
  870.     ;; Non-nil if the current line contains a comment.
  871.     has-comment
  872.  
  873.     ;; Non-nil if the current line contains code and a comment.
  874.     has-code-and-comment
  875.  
  876.     ;; If has-comment, the appropriate fill-prefix for the comment.
  877.     comment-fill-prefix
  878.     )
  879.  
  880.     ;; Figure out what kind of comment we are looking at.
  881.     (save-excursion
  882.       (beginning-of-line)
  883.       (cond
  884.  
  885.        ;; A line with nothing but a comment on it?
  886.        ((looking-at "[ \t]*;[; \t]*")
  887.     (setq has-comment t
  888.           comment-fill-prefix (buffer-substring (match-beginning 0)
  889.                             (match-end 0))))
  890.  
  891.        ;; A line with some code, followed by a comment?  Remember that the
  892.        ;; semi which starts the comment shouldn't be part of a string or
  893.        ;; character.
  894.        ;; XEmacs Try this the FSF and see if it works.
  895. ;       ((progn
  896. ;      (while (not (looking-at ";\\|$"))
  897. ;        (skip-chars-forward "^;\n\"\\\\?")
  898. ;        (cond
  899. ;         ((eq (char-after (point)) ?\\) (forward-char 2))
  900. ;         ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1))))
  901. ;      (looking-at ";+[\t ]*"))
  902. ;    (setq has-comment t)
  903.        ((condition-case nil
  904.         (save-restriction
  905.           (narrow-to-region (point-min)
  906.                 (save-excursion (end-of-line) (point)))
  907.           (while (not (looking-at ";\\|$"))
  908.         (skip-chars-forward "^;\n\"\\\\?")
  909.         (cond
  910.          ((eq (char-after (point)) ?\\) (forward-char 2))
  911.          ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1))))
  912.           (looking-at ";+[\t ]*"))
  913.       (error nil))
  914.     (setq has-comment t has-code-and-comment t)
  915.     (setq comment-fill-prefix
  916.           (concat (make-string (/ (current-column) 8) ?\t)
  917.               (make-string (% (current-column) 8) ?\ )
  918.               (buffer-substring (match-beginning 0) (match-end 0)))))))
  919.  
  920.     (if (not has-comment)
  921.     (fill-paragraph justify)
  922.  
  923.       ;; Narrow to include only the comment, and then fill the region.
  924.       (save-excursion
  925.     (save-restriction
  926.       (beginning-of-line)
  927.       (narrow-to-region
  928.        ;; Find the first line we should include in the region to fill.
  929.        (save-excursion
  930.          (while (and (zerop (forward-line -1))
  931.              (looking-at "^[ \t]*;")))
  932.          ;; We may have gone too far.  Go forward again.
  933.          (or (looking-at ".*;")
  934.          (forward-line 1))
  935.          (point))
  936.        ;; Find the beginning of the first line past the region to fill.
  937.        (save-excursion
  938.          (while (progn (forward-line 1)
  939.                (looking-at "^[ \t]*;")))
  940.          (point)))
  941.  
  942.       ;; Lines with only semicolons on them can be paragraph boundaries.
  943.       (let* ((paragraph-start (concat paragraph-start "\\|[ \t;]*$"))
  944.          (paragraph-separate (concat paragraph-start "\\|[ \t;]*$"))
  945.          (paragraph-ignore-fill-prefix nil)
  946.          (fill-prefix comment-fill-prefix)
  947.          (after-line (if has-code-and-comment
  948.                  (save-excursion
  949.                    (forward-line 1) (point))))
  950.          (end (progn
  951.             (forward-paragraph)
  952.             (or (bolp) (newline 1))
  953.             (point)))
  954.          ;; If this comment starts on a line with code,
  955.          ;; include that like in the filling.
  956.          (beg (progn (backward-paragraph)
  957.                  (if (eq (point) after-line)
  958.                  (forward-line -1))
  959.                  (point))))
  960.         (fill-region-as-paragraph beg end
  961.                       justify nil
  962.                       (save-excursion
  963.                     (goto-char beg)
  964.                     (if (looking-at fill-prefix)
  965.                         nil
  966.                       (re-search-forward comment-start-skip)
  967.                       (point))))))))
  968.     t))
  969.  
  970. (defun indent-code-rigidly (start end arg &optional nochange-regexp)
  971.   "Indent all lines of code, starting in the region, sideways by ARG columns.
  972. Does not affect lines starting inside comments or strings, assuming that
  973. the start of the region is not inside them.
  974.  
  975. Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
  976. The last is a regexp which, if matched at the beginning of a line,
  977. means don't indent that line."
  978.   (interactive "r\np")
  979.   (let (state)
  980.     (save-excursion
  981.       (goto-char end)
  982.       (setq end (point-marker))
  983.       (goto-char start)
  984.       (or (bolp)
  985.       (setq state (parse-partial-sexp (point)
  986.                       (progn
  987.                         (forward-line 1) (point))
  988.                       nil nil state)))
  989.       (while (< (point) end)
  990.     (or (car (nthcdr 3 state))
  991.         (and nochange-regexp
  992.          (looking-at nochange-regexp))
  993.         ;; If line does not start in string, indent it
  994.         (let ((indent (current-indentation)))
  995.           (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  996.           (or (eolp)
  997.           (indent-to (max 0 (+ indent arg)) 0))))
  998.     (setq state (parse-partial-sexp (point)
  999.                     (progn
  1000.                       (forward-line 1) (point))
  1001.                     nil nil state))))))
  1002.  
  1003. (provide 'lisp-mode)
  1004.  
  1005. ;;; lisp-mode.el ends here
  1006.