annotate lisp/modes/lisp-mode.el @ 0:376386a54a3c r19-14

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